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. Line Count in my Qt Project.
Forum Updated to NodeBB v4.3 + New Features

Line Count in my Qt Project.

Scheduled Pinned Locked Moved Solved General and Desktop
25 Posts 10 Posters 17.0k Views 4 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.
  • jsulmJ jsulm

    @J.Hilk Well, it is a bit more complex :-) For example your version counts empty lines and comment lines.

    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #15

    @jsulm well obviously :-P
    but that could be addressed e.g:

    int countLines(QString path){
        QFile f(path);
        int cnt = 0;
    
        if(f.open(QIODevice::ReadOnly | QIODevice::Text)){
            QTextStream read(&f);
            QString line;
            bool comment = false;
            while(!read.atEnd()){
                line = read.readLine();
                line = line.simplified();
                line.replace(" ","");
                if(line.size() >0){
                    if(line.leftRef(2) != "//"){
                        if(line.contains("/*"))
                            comment = true;
                        if(line.contains("*/"))
                            comment = false;
                        if(!comment)
                            cnt++;
                    }
                }
            }
        }
        f.close();
        return cnt;
    }
    

    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    1 Reply Last reply
    2
    • Pradeep P NP Offline
      Pradeep P NP Offline
      Pradeep P N
      wrote on last edited by
      #16

      Thanks All for the help.
      Moving to solved.

      Pradeep Nimbalkar.
      Upvote the answer(s) that helped you to solve the issue...
      Keep code clean.

      1 Reply Last reply
      2
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #17

        find and grep...find and grep

        I light my way forward with the fires of all the bridges I've burned behind me.

        aha_1980A 1 Reply Last reply
        0
        • Kent-DorfmanK Kent-Dorfman

          find and grep...find and grep

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #18

          @Kent-Dorfman would you like to elaborate a bit more?

          Qt has to stay free or it will die.

          1 Reply Last reply
          2
          • W Offline
            W Offline
            wrosecrans
            wrote on last edited by
            #19
            find . -iname \*.h -o -iname \*.cpp -exec grep \\\; {} \; | wc -l
            

            Would find all the .h and .cpp find in the current directory, and count the number of lines with a semicolon. For the most things, that's probably a pretty useful count of code LOC, and requires no extra tools to be installed.

            Pradeep P NP 1 Reply Last reply
            1
            • fcarneyF Offline
              fcarneyF Offline
              fcarney
              wrote on last edited by
              #20

              Is this for some kind of line-counting match?

              C++ is a perfectly valid school of magic.

              1 Reply Last reply
              0
              • W wrosecrans
                find . -iname \*.h -o -iname \*.cpp -exec grep \\\; {} \; | wc -l
                

                Would find all the .h and .cpp find in the current directory, and count the number of lines with a semicolon. For the most things, that's probably a pretty useful count of code LOC, and requires no extra tools to be installed.

                Pradeep P NP Offline
                Pradeep P NP Offline
                Pradeep P N
                wrote on last edited by
                #21

                @wrosecrans
                This don't do the folder search recursive right. ?

                Pradeep Nimbalkar.
                Upvote the answer(s) that helped you to solve the issue...
                Keep code clean.

                aha_1980A 1 Reply Last reply
                1
                • Pradeep P NP Pradeep P N

                  @wrosecrans
                  This don't do the folder search recursive right. ?

                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on last edited by
                  #22

                  @Pradeep-P-N

                  I have not tested this code, but find is recursive so it should work.

                  Regards

                  Qt has to stay free or it will die.

                  1 Reply Last reply
                  1
                  • Kent-DorfmanK Offline
                    Kent-DorfmanK Offline
                    Kent-Dorfman
                    wrote on last edited by Kent-Dorfman
                    #23

                    yes, the default behaviour of find "in unix" is recursive. grep is a pattern matcher. I use

                    find . -type f -iname \*.h -o -iname \*.hpp -o \
                    -iname \*.c -o -iname \*.cc -o -iname \*.cxx \
                    -o -iname \*.cpp | \
                    xargs -n 1 egrep -v "^[ \t]*$" | wc -l
                    

                    prints total of all target file lines (found recursively). ignores blank lines

                    I light my way forward with the fires of all the bridges I've burned behind me.

                    Pradeep P NP 1 Reply Last reply
                    0
                    • Kent-DorfmanK Kent-Dorfman

                      yes, the default behaviour of find "in unix" is recursive. grep is a pattern matcher. I use

                      find . -type f -iname \*.h -o -iname \*.hpp -o \
                      -iname \*.c -o -iname \*.cc -o -iname \*.cxx \
                      -o -iname \*.cpp | \
                      xargs -n 1 egrep -v "^[ \t]*$" | wc -l
                      

                      prints total of all target file lines (found recursively). ignores blank lines

                      Pradeep P NP Offline
                      Pradeep P NP Offline
                      Pradeep P N
                      wrote on last edited by
                      #24

                      Hi @Kent-Dorfman
                      Cool, Linux commands are always awesome.
                      I just Added *.qml to the command so i can include qml also for the line count.

                      find . -type f -iname \*.h -o -iname \*.hpp -o -iname \*.qml -o \
                      -iname \*.c -o -iname \*.cc -o -iname \*.cxx \
                      -o -iname \*.cpp | \
                      xargs -n 1 egrep -v "^[ \t]*$" | wc -l
                      

                      Thanks a lot again
                      @mrjj @Eddy @jsulm @J-Hilk @Kent-Dorfman @aha_1980 @wrosecrans .

                      Have a great day.

                      Pradeep Nimbalkar.
                      Upvote the answer(s) that helped you to solve the issue...
                      Keep code clean.

                      1 Reply Last reply
                      5
                      • D Offline
                        D Offline
                        Dr. Abel
                        wrote on last edited by
                        #25

                        This tool will help you.
                        cloc-1.74.

                        It is collected in PyMake project.
                        github https://github.com/AbelTian/PyMake.git (fetch)
                        github https://github.com/AbelTian/PyMake.git (push)
                        origin https://gitee.com/drabel/PyMake (fetch)
                        origin https://gitee.com/drabel/PyMake (push)

                        In demo/ dir.

                        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