[Solved] ReadAll() (ls)



  • I need to offer a selection of mounted drives to the user for a procedure. The fragment below will show me the the contents of the directory where drive mounts live. How can I parse out the sd?? values (sda1,sdb1,etc) from the QString "command" and offer them to the user for choice?

    @ QProcess mount_dir;
    mount_dir.setProcessChannelMode(QProcess::MergedChannels);
    cstring = adb + " -s " + daddr+port + " shell su -c ls /storage/";
    mount_dir.start(cstring);
    mount_dir.waitForFinished(-1);
    command=mount_dir.readAll();@

    Thanks!



  • Did you get the output of the ls command from QProcess in your program ? If yes, it should be simple QString parsing. QString documentation should help you.

    If you have not got the output from QProcess do let me know. I can help with that.


  • Lifetime Qt Champion

    Hi,

    Please have a look at QProcess's "extended description":http://qt-project.org/doc/qt-5/qprocess.html#details you'll see how to properly build a parameter list and start a process with it



  • Did you get the output of the ls command from QProcess in your program ? If >yes, it should be simple QString parsing.

    Actually, parsing is what I'd like a hand with. QProcess is working great.

    I have a qstring, "command" as shown in the code above. Among other things, it will contain strings of a pattern "sd??", *sda1,sdb1,sdc1, etc).

    Is there a way I can grab any substring matching the pattern "sd??" from the qstring returned by readAll()?


  • Lifetime Qt Champion

    QRegularExpression comes to mind



  • Yeah, I saw that and came across this fragment in my search:

    @
    QRegularExpression re("(\d+) (\w+)");
    QRegularExpressionMatch match = re.match("1234 word");
    if (match.hasMatch()) {
    QString digits = match.captured(1); // "1234"
    QString letters = match.captured(2); // "word
    }
    @

    Unfortunately, I'm not one of the 1 out of 10,000 coders who speak regex.

    So, what would a regex for "sd??" be?



  • This should help you.
    @
    QString str = "/dev/sda1";
    int index = str.indexOf("sd");
    QString newStr = str.mid(index,4);
    qDebug() << "Sub String ="<<newStr@


  • Lifetime Qt Champion

    There are several possibilities e.g.

    "[a-z]{2}([a-z0-9]+)"

    If you are looking for specifically sd plus something:

    "sd([a-z0-9]+)"

    From the top of my head.

    You can build the "Regular Expressions Example" to help you build the regexp



  • Thanks for this. That tool is very handy.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.