Line Count in my Qt Project.
-
Hi All,
Is there any way to find out the total number lines count in my Qt Project.
Thank You.
-
Hi
You mean lines in all cpp and h files?I dont think Creator can show you.
-
this plugin looks promising
-
@Pradeep-P-N There is well known command line tool for that: sloccount
-
@Pradeep-P-N Why not just use Google?
Then you will find https://www.dwheeler.com/sloccount/
"how can i use it with Qt" - you don't use it with Qt (as Qt is not a programming language), you use it with your C++ source code. It does not support QML though. -
This is as good of a reason as it gets to write a small program, that does the work for you.
Recursively opening your source file folder and counting how often
QIODevice::readline
can be called.Shouldn't take more than a few minutes to do.
-
sloccount topmost-source-code-directory
so like
sloccount c:\projects\testproject -
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.