Line Count in my Qt Project.
-
I had some time during breakfast :-)
#include <QCoreApplication> #include <QDir> #include <QFile> #include <QTextStream> #include <QVector> int countLines(QString path){ QFile f(path); int cnt = 0; if(f.open(QIODevice::ReadOnly | QIODevice::Text)){ QTextStream read(&f); while(!read.atEnd()){ read.readLine(); cnt++; } } f.close(); return cnt; } int parseDir(QString path){ int cnt = 0; QDir dir(path); QStringList dirs = dir.entryList(QDir::AllDirs |QDir::NoDotAndDotDot); QStringList file = dir.entryList(QDir::Files); for(QString dir : dirs){ cnt += parseDir(path + "/"+dir); } for(QString s : file){ if(s.splitRef('.').last() == "h" || s.splitRef('.').last() == "cpp") cnt += countLines(path + "/"+s); } return cnt; } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); int count = 0; count += PathDir(a.arguments().last()); QTextStream out(stdout); out << "Lines in project: " << a.arguments().last() << ": "<< endl <<count << endl; return a.exec(); }
-
@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; }
-
Thanks All for the help.
Moving to solved. -
find and grep...find and grep
-
@Kent-Dorfman would you like to elaborate a bit more?
-
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.
-
@wrosecrans
This don't do the folder search recursive right. ? -
-
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
-
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.
-
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.