Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QStringList object unavalable in for loop
Forum Updated to NodeBB v4.3 + New Features

QStringList object unavalable in for loop

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 408 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.
  • R Offline
    R Offline
    rcx11
    wrote on last edited by
    #1

    I'm attempting to make a fake **argv[] and argc set to use with Boost Program Options. A line is read from a script file, and I'm using Program Options to parse it. As it is now, I'm reading the line from file successfully into a QString, and I'm splitting into a QStringList, apparently also successfully. I then try to make a C-style char * array. After reading the second string into the array, the QStringList is shown as unavailable, and I SEGFAULT when trying to execute the third loop.

    The line being read (AppName substituted):

    AppName -b black -c white -t $''
    

    My code:

    //Simulate argv** and argc
    QStringList Qargv = line.split(' ', Qt::KeepEmptyParts);
    int fargc = Qargv.count();      // Fake argc
    char* fargv[fargc];     // Fake argv
    for (int j = 0; j < fargc; j++)
         strcpy(fargv[j], Qargv.at(j).toStdString().c_str());
    

    I've also tried Qargv.at(j).toLocal8bit().constData() with the same results. I've also attempted to move the declaration of Qargv up a scope, copy the C-string to an intermediate char * variable before assignment, and copying to a std::string to convert to C-string. Some results have been interesting, but this is the most succinct and easy to describe. It always goes <not available> after copying "-b" into fargv[1].

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by ChrisW67
      #2

      You have not allocated memory to copy to the data at this address Qargv.at(j).toStdString().c_str() into. In your code each fargv[j] is uninitialized. Each fargv pointer should point to a block of memory that is large enough to hold the argument plus a \0 byte.

      Depending on the data lifetime requirements and whether Boost program options modifies the data (I suspect not), there is possibly a neater way to do this using a QByteArray. Load the line into a QByteArray, swap the spaces/delimiters for \0, and build fargv with pointers into that buffer.

      1 Reply Last reply
      2
      • R Offline
        R Offline
        rcx11
        wrote on last edited by
        #3

        Thanks for the help. I haven't messed with memory allocation much, so I didn't think about it. The code has been adjusted.

        //Simulate argv** and argc
        QStringList Qargv = line.split(' ', Qt::KeepEmptyParts);
        int fargc = Qargv.count();      // Fake argc
        char **fargv = new char*[fargc];     // Fake **argv.
        for (int j = 0; j < fargc; j++)
           fargv[j] = qstrdup(Qargv.at(j).toStdString().c_str());
        

        It now does mostly what I want it to do. What I have to figure out now is how to break up that QStringList so that I can keep all parts within $' ... ' together.

        JonBJ 1 Reply Last reply
        0
        • R rcx11

          Thanks for the help. I haven't messed with memory allocation much, so I didn't think about it. The code has been adjusted.

          //Simulate argv** and argc
          QStringList Qargv = line.split(' ', Qt::KeepEmptyParts);
          int fargc = Qargv.count();      // Fake argc
          char **fargv = new char*[fargc];     // Fake **argv.
          for (int j = 0; j < fargc; j++)
             fargv[j] = qstrdup(Qargv.at(j).toStdString().c_str());
          

          It now does mostly what I want it to do. What I have to figure out now is how to break up that QStringList so that I can keep all parts within $' ... ' together.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @rcx11 said in QStringList object unavalable in for loop:

          I can keep all parts within $' ... ' together.

          In that case splitting on space may not be what you intend. Show an example of what the complete string line looks like (I assume not just with the $'' empty at the end, else what is the problem?) and how you would want it to be split into separate arguments.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rcx11
            wrote on last edited by rcx11
            #5

            I fiddled around and got it working.

            QStringList preserveReminder;
            QStringList Qargv;
            if (line.contains("$\'"))
            {
               preserveReminder = line.split("$\'", Qt::SkipEmptyParts);   // Split the reminder from the rest because I need to prserve it as a single string
               Qargv = preserveReminder.at(0).split(' ', Qt::SkipEmptyParts); // Split everything else into separate strings.
               Qargv.append("$\'" + preserveReminder.at(1));   // Add the second string back with a restored $'
            }
            else
               Qargv = line.split(' ', Qt::SkipEmptyParts);
            

            The nature of what I'm writing, I don't have to worry about multiple $' .. .' segments.

            1 Reply Last reply
            0

            • Login

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