Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Loop trough files and replace variable values between quotes
Qt 6.12 LTS is out! See what's new in the release blog

Loop trough files and replace variable values between quotes

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 3 Posters 9.3k Views 2 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.
  • mrjjM mrjj

    @WD30EFRX
    ahh, it's inside source files.
    Well for the 0x0 ones, then
    "0x
    should be enough.
    pszTimestamp is a bit worse.
    Can we see what you have tried so far as not to suggest the same?

    ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #5

    @mrjj

    So far i've used the following code

    QStack<QString> dirs;
        dirs.push(QString(argv[3]));
    
        // continue if there are sub directory
        while(dirs.size()){
            QDir currentDir(dirs.pop());
            QStringList child =  currentDir.entryList(filters);
    
            QStringList::const_iterator itbegin = child.begin();
            QStringList::const_iterator itend = child.end();
            for(; itbegin != itend ; ++itbegin){
                const QString nextFile(currentDir.path() + "/" + (*itbegin));
                //std::cout << "Open " << (const char*)nextFile.toLatin1() << std::endl;
    
                // Open het bestand en plaats inhoud in een string
                QString oldContent;
                {
                    QFile inFile(nextFile);
                    if(!inFile.open(QIODevice::ReadOnly)){
                        std::cout << "Cannot open " << (const char*)nextFile.toLatin1() << "\n";
    
                    }
                    QTextStream inStream(&inFile);
                    oldContent = inStream.readAll();
                    inFile.close();
                }
    
                // Is there anything to change? - Geen match
                if( oldContent.indexOf(oldText) == -1){
                       // std::cout << "Nothing to do in " << (const char*)nextFile.toLatin1() << "\n";
                }
                else{
                    QString newContent = oldContent.replace(oldText,newText);
                    {
                        QFile inFile(nextFile);
                        if(!inFile.open(QIODevice::WriteOnly)){
                            std::cout << "Cannot open to write " << (const char*)nextFile.toLatin1() << "\n";
    
                        }
    
                        QTextStream outStream(&inFile);
                        outStream << newContent; // schrijf naar bestand
                        inFile.close();
                        std::cout << "Source changed in " << (const char*)nextFile.toLatin1() << "\n";
    
                    }
                }
            }
    
    
            // Get subdirectories and files
            QStringList childdir =  currentDir.entryList(QDir::NoDotAndDotDot | QDir::Dirs);
            QStringList::const_iterator itbegindir = childdir.begin();
            QStringList::const_iterator itenddir = childdir.end();
            for(; itbegindir != itenddir ; ++itbegindir){
                dirs.push(currentDir.path() + "/" + (*itbegindir));
                //std::cout << "Add " << (const char*)(currentDir.path() + "/" + (*itbegindir)).toLatin1() << std::endl;
            }
        }
    

    While this does the job, i really need to re-loop every file for every line i have to replace.
    So i'm looking for something cleaner and more performant.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #6

      Hmm, dont seem really bad and u use readall so
      relooping the in memory file should be pretty fast?

      How do you recognize pszTimestamp cases?

      ? 1 Reply Last reply
      0
      • mrjjM mrjj

        Hmm, dont seem really bad and u use readall so
        relooping the in memory file should be pretty fast?

        How do you recognize pszTimestamp cases?

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #7

        @mrjj

        Not yet. Still looking for a way to extract and replace the values that are inside quotes after the first known part of the string.
        If i had such function, it would be easy to exchange all variables from the partially known line

        Hence my question :)

        mrjjM 1 Reply Last reply
        0
        • ? A Former User

          @mrjj

          Not yet. Still looking for a way to extract and replace the values that are inside quotes after the first known part of the string.
          If i had such function, it would be easy to exchange all variables from the partially known line

          Hence my question :)

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #8

          @WD30EFRX said:
          Well, its a nice problem :)
          Is this something you must do many times since u care about the performance?

          will it always start with "const char* " for those pszTimestamp cases?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #9

            Hi and welcome to devnet,

            You provided only input examples. Can you also provide a complete before/after example ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            ? 1 Reply Last reply
            0
            • SGaistS SGaist

              Hi and welcome to devnet,

              You provided only input examples. Can you also provide a complete before/after example ?

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #10

              @SGaist

              Hi!

              The output variables are generated by an external algortithm much like Bitcoin.
              So new hash values and variables need to get 'computed' first and are all related to each other.

              So the output is truly random, the only part that i know for sure are the leading string that identify them in the sources.

              So to shorten my request, what i need is a nice, optimized function to :

              • Open the file
              • Match a whole line by the leading string
              • Split is somewhere after the leading string and replace the variables OR replace with a regex between the quotes
              • Write back to file

              My current code opens every file multiple times for each new string i need to replace.
              So optimized, it would open 1 file and look for ALL the predefined strings i need to replace.

              Hope this helps :)

              Thank you

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #11

                Do you know all the strings you have to search before opening the file ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                ? 1 Reply Last reply
                0
                • SGaistS SGaist

                  Do you know all the strings you have to search before opening the file ?

                  ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #12

                  @SGaist

                  Yes i do.

                  Thank you

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #13

                    Then I'd build something like a vector containing the string/regexp to search along with their replacement and loop on that over a buffer containing your file data. Then you wouldn't need to open every file several times.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    ? 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Then I'd build something like a vector containing the string/regexp to search along with their replacement and loop on that over a buffer containing your file data. Then you wouldn't need to open every file several times.

                      ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #14

                      @SGaist

                      Thank you.

                      Right now i'm looking to replace the fixed lines using their known line numbers, split the strings, extract the values and join them back.

                      This seems to work. However, i have several multiline codeblocks that needs replacement between {}, and now i'm stucked with those. :)

                      1 Reply Last reply
                      0
                      • mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #15

                        Hi
                        several multiline codeblocks ?
                        That is a new type it seems?

                        ? 1 Reply Last reply
                        0
                        • mrjjM mrjj

                          Hi
                          several multiline codeblocks ?
                          That is a new type it seems?

                          ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #16

                          @mrjj

                          As example:

                          int64_t DoSomeFunctionHere(int64_t param1, int64_t param2)
                          {
                              Some line of code here
                              Some line of code here
                              return Param1 + Param2;
                          }
                          

                          So i need to look for DoSomeFunctionHere and replace everything that follows inside the { and }

                          1 Reply Last reply
                          0
                          • mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #17

                            Ok, so that is still "0x0" type
                            for "Some line of code here"
                            or new case?

                            ? 1 Reply Last reply
                            0
                            • mrjjM mrjj

                              Ok, so that is still "0x0" type
                              for "Some line of code here"
                              or new case?

                              ? Offline
                              ? Offline
                              A Former User
                              wrote on last edited by
                              #18

                              @mrjj

                              It's combined. But i think i'll look for someone to hire to complete my project since my deadline is very near :)

                              mrjjM 1 Reply Last reply
                              0
                              • ? A Former User

                                @mrjj

                                It's combined. But i think i'll look for someone to hire to complete my project since my deadline is very near :)

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #19

                                @WD30EFRX
                                Ok that could be an option too,
                                If u use something like
                                https://www.freelancer.com

                                Make sure to describe the problem very carefully with
                                actual samples of what to find and what to replace with and the
                                different cases. Also what " first known part of the string." is. and so on.
                                Also, if the files are in subfolder etc.

                                Else you wont get a good estimate of the cost.

                                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
                                • Unsolved