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] {