源代码:https://gitee.com/qianqiu-10000/statistical.git
1.首先有个方法可以判断一行代码属于注释还是代码
1.1输入一行字符串
1.2去掉前后空字符
1.3如果当前处在多行注释状态,直接跳过一些判断,多行注释+1,然后判断是否结束多行注释
1.4如果没有处在多行注释状态,一点点判断
1.5自己再添加其他判断条件
void myAnalyzer::oneLine(const QString &line)
{
QString newline = line.simplified();
if(!m_multiFlag) //多行注释中不需要判断
{
if(newline.isEmpty()) {
m_linePointer = &m_emptyLines;
}
else if(newline.startsWith('#')) { //该行是预编译宏
m_linePointer = &m_microLines;
}
else if(newline.startsWith("//")) { //该行是注释
m_linePointer = &m_annoSingleLines;
}
else if(newline.startsWith("/*")) { //注释开始
m_linePointer = &m_annoMultiLines;
m_multiFlag = true;
} else {
m_linePointer = &m_codeLines;
}
}
(*m_linePointer)++;
if(m_multiFlag && newline.endsWith("*/")) {
m_multiFlag = false;
}
}
2.有一个方法打开文件,逐行读取判断
bool myAnalyzer::procFile(const QString &name)
{
initialize();
QFile file(name);
if(file.open(QIODevice::ReadOnly | QIODevice::Text))
{
while(!file.atEnd()) {
oneLine(file.readLine());
}
file.close();
return true;
}
return false;
}
3.还要有一个类,在当前目录下查找*.h/*.cpp/*.cxx文件,对每一个文件打开分析。然后获取当目录下的文件夹,对每个文件夹路径递归查找。
void myFindFiles::run(const QString &dir)
{
initialize();
QStandardItem *root = new QStandardItem;
root->setText(dir);
root->setData(dir);
m_model.appendRow(root);
recursion(root);
QList<QStandardItem *>line;
QStandardItem *item = new QStandardItem;
item->setText("统计");
line.append(item);
item = new QStandardItem;
item->setText(QString::number(m_codeLines));
line.append(item);
item = new QStandardItem;
item->setText(QString::number(m_emptyLines));
line.append(item);
item = new QStandardItem;
item->setText(QString::number(m_microLines));
line.append(item);
item = new QStandardItem;
item->setText(QString::number(m_annoMultiLines));
line.append(item);
item = new QStandardItem;
item->setText(QString::number(m_annoSingleLines));
line.append(item);
m_model.appendRow(line);
}
void myFindFiles::recursion(QStandardItem *node)
{
QDir workDir(node->data().toString());
if(workDir.isEmpty()) {
return;
}
if(!workDir.isReadable()) {
return;
}
QStringList fileList = workDir.entryList(
QStringList() << "*.cpp" << "*.h" << "*.cxx",
QDir::Files);
myAnalyzer analyzer;
for(int i = 0; i < fileList.count(); i++)
{
QString file = fileList.at(i);
if(! analyzer.procFile(workDir.path() + "/" + file)) {
continue;
}
int codeLines = analyzer.codeLines();
int emptyLines = analyzer.emptyLines();
int microLines = analyzer.microLines();
int annoMultiLines = analyzer.annoMultiLines();
int annoSingleLines = analyzer.annoSingleLines();
m_codeLines += codeLines;
m_emptyLines += emptyLines;
m_microLines += microLines;
m_annoMultiLines += annoMultiLines;
m_annoSingleLines += annoSingleLines;
QList<QStandardItem *>line;
QStandardItem *item = new QStandardItem;
item->setText(file);
item->setData(workDir.absolutePath() + "/" + file);
line.append(item);
item = new QStandardItem;
item->setText(QString::number(codeLines));
line.append(item);
item = new QStandardItem;
item->setText(QString::number(emptyLines));
line.append(item);
item = new QStandardItem;
item->setText(QString::number(microLines));
line.append(item);
item = new QStandardItem;
item->setText(QString::number(annoMultiLines));
line.append(item);
item = new QStandardItem;
item->setText(QString::number(annoSingleLines));
line.append(item);
node->appendRow(line);
}
QStringList dirList = workDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
foreach(QString d, dirList) {
QStandardItem *item = new QStandardItem;
item->setText(d);
item->setData(workDir.path() + "/" + d);
node->appendRow(item);
recursion(item);
}
}
4.数据保存在一棵树中(QStandardItemModel),然后用QTreeView显示,效果如下
标签:setText,写个,代码,item,QString,new,line,工具,QStandardItem From: https://blog.csdn.net/weixin_69505365/article/details/140822180