From afcc0c6c478427cf4ca499a57314d6aa2afbe67c Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Fri, 7 Jan 2022 16:49:47 +0000 Subject: [PATCH] Allow to get the partition nr from the device node --- src/main.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 9c46fa2..823859d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,6 +25,45 @@ #include #include +#include + +std::pair parseDevice(const QString& selectedDevice) +{ + QString device; + QString partitionNr; + if (selectedDevice.length()) { + const bool isNvme = selectedDevice.startsWith(QStringLiteral("nvme")); + // calculate the number of numbers on the last part of the info. + int numSize = 0; + for (int i = selectedDevice.length() - 1; i > 0; i--) { + if (selectedDevice[i].isNumber()) { + numSize += 1; + } else { + if (isNvme) { + // the entry is just an nvme without a partition. + if (selectedDevice[i] != QLatin1Char('p')) { + numSize = 0; + break; + } + } + break; + } + } + partitionNr = selectedDevice.mid(selectedDevice.length() - numSize); + + if (numSize != 0) { + // nvme has a `p` that also needs to be removed. + if (isNvme) { + numSize += 1; + } + + device = selectedDevice.mid(0, selectedDevice.length() - numSize); + } else { + device = selectedDevice; + } + } + return {device, partitionNr}; +} int Q_DECL_IMPORT main(int argc, char* argv[]) { @@ -75,6 +114,7 @@ int Q_DECL_IMPORT main(int argc, char* argv[]) return 0; const QString selectedDevice = parser.value(deviceOption); + auto [device, partitionNr] = parseDevice(selectedDevice); MainWindow* mainWindow = new MainWindow(); QObject::connect(mainWindow, &MainWindow::scanFinished, mainWindow, [mainWindow, selectedDevice] {