Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. how to extract "what follows " from string ??
Forum Updated to NodeBB v4.3 + New Features

how to extract "what follows " from string ??

Scheduled Pinned Locked Moved Unsolved C++ Gurus
4 Posts 2 Posters 419 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on 23 May 2023, 18:18 last edited by
    #1

    I am posting full response to system command.

    ○ bluetooth.service - Bluetooth service
         Loaded: loaded (/lib/systemd/system/bluetooth.service; disabled; vendor preset: enabled)
         Active: inactive (dead)
           Docs: man:bluetoothd(8)
    

    I am interested in retrieving and analyzing part of the string "bluetooth.service; disabled;"
    I can ask "contains ( bluetooth.service)" but I need to analyze " disabled " because it varies - it can be "enabled" also.
    Is my only choice to analyze / check for both using "contains " ?
    bluetooth.service; disabled;
    bluetooth.service; enabled;

    Cheers

    J 1 Reply Last reply 23 May 2023, 19:07
    0
    • A Anonymous_Banned275
      23 May 2023, 18:18

      I am posting full response to system command.

      ○ bluetooth.service - Bluetooth service
           Loaded: loaded (/lib/systemd/system/bluetooth.service; disabled; vendor preset: enabled)
           Active: inactive (dead)
             Docs: man:bluetoothd(8)
      

      I am interested in retrieving and analyzing part of the string "bluetooth.service; disabled;"
      I can ask "contains ( bluetooth.service)" but I need to analyze " disabled " because it varies - it can be "enabled" also.
      Is my only choice to analyze / check for both using "contains " ?
      bluetooth.service; disabled;
      bluetooth.service; enabled;

      Cheers

      J Offline
      J Offline
      JoeCFD
      wrote on 23 May 2023, 19:07 last edited by JoeCFD
      #2

      @AnneRanch
      is the following code good enough?

      #include <QCoreApplication>
      #include <QProcess>
      #include <QDebug>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          QString serviceName = "bluetooth"; // Replace with the name of the service you want to check
      
          // Execute the 'systemctl is-active' command to check the service status
          QProcess process;
          process.start("systemctl", QStringList() << "is-active" << serviceName);
          process.waitForFinished();
      
          QByteArray output = process.readAllStandardOutput();
          QString status = output.trimmed();
      
          if (status == "active") {
              enabled;
              qDebug() << "Service" << serviceName << "is active.";
          } else {
               disabled;
              qDebug() << "Service" << serviceName << "is not active.";
          }
      
          return 0;
      }
      

      if you prefer to use contain

      after you get the line
      QString info_str = "Loaded: loaded (/lib/systemd/system/bluetooth.service; disabled; vendor preset: enabled)",  do
      if  ( true == info_str.simplified().contains( QString(  "bluetooth.service; enabled" ) ) ) {
          enabled;
      }
      else {
          disabled;
      }
      
      do not forget simplified();
      
      A 1 Reply Last reply 23 May 2023, 20:29
      1
      • J JoeCFD
        23 May 2023, 19:07

        @AnneRanch
        is the following code good enough?

        #include <QCoreApplication>
        #include <QProcess>
        #include <QDebug>
        
        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
        
            QString serviceName = "bluetooth"; // Replace with the name of the service you want to check
        
            // Execute the 'systemctl is-active' command to check the service status
            QProcess process;
            process.start("systemctl", QStringList() << "is-active" << serviceName);
            process.waitForFinished();
        
            QByteArray output = process.readAllStandardOutput();
            QString status = output.trimmed();
        
            if (status == "active") {
                enabled;
                qDebug() << "Service" << serviceName << "is active.";
            } else {
                 disabled;
                qDebug() << "Service" << serviceName << "is not active.";
            }
        
            return 0;
        }
        

        if you prefer to use contain

        after you get the line
        QString info_str = "Loaded: loaded (/lib/systemd/system/bluetooth.service; disabled; vendor preset: enabled)",  do
        if  ( true == info_str.simplified().contains( QString(  "bluetooth.service; enabled" ) ) ) {
            enabled;
        }
        else {
            disabled;
        }
        
        do not forget simplified();
        
        A Offline
        A Offline
        Anonymous_Banned275
        wrote on 23 May 2023, 20:29 last edited by
        #3

        @JoeCFD OK, that assumes there are only two options. I can change to "switch" , no problem.
        What is the advantage of using "simplified"?
        ( I can look it up ...)

        J 1 Reply Last reply 23 May 2023, 21:44
        0
        • A Anonymous_Banned275
          23 May 2023, 20:29

          @JoeCFD OK, that assumes there are only two options. I can change to "switch" , no problem.
          What is the advantage of using "simplified"?
          ( I can look it up ...)

          J Offline
          J Offline
          JoeCFD
          wrote on 23 May 2023, 21:44 last edited by
          #4

          @AnneRanch simplilfied() will squeeze possibly more spaces between bluetooth.service; and disabled into only one space. This will make sure contains() call more accurate.

          1 Reply Last reply
          0

          1/4

          23 May 2023, 18:18

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved