-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
199 lines (178 loc) · 5.8 KB
/
main.cpp
File metadata and controls
199 lines (178 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <QCommandLineParser>
#include <QCoreApplication>
#include <QDir>
static int otherFiles = 0;
static int codeFiles = 0;
static int headerFiles = 0;
static int resourceFiles = 0;
static int jsonFiles = 0;
static int imageFiles = 0;
static int emptyLines = 0;
static int commentLines = 0;
static int preprocessorLines = 0;
static int codeLines = 0;
static int jsonLines = 0;
static bool recurse = false;
static bool incJson = false;
QStringList imageSuffixes { "png", "jpg", "jpeg", "tiff", "svg", "ico", "bmp", "gif", "tif", "webp", "HEIC", "HEIF"};
QTextStream out (stdout);
void processCode (const QString& path)
{
QFile in (path);
if (!in.open (QFile::ReadOnly))
{
qDebug ().nospace ().noquote () << "Unable to open file \"" << path << "\".";
return;
}
auto lines = in.readAll ().split ('\n');
for (auto& line : lines)
{
line = line.trimmed ();
if (line.isEmpty ())
{
emptyLines++;
}
else
{
if (line.startsWith ("//") || line.startsWith ("/*"))
{
commentLines++;
}
else if (line.startsWith ("#"))
{
preprocessorLines++;
}
else
{
codeLines++;
}
}
}
}
void processJson (const QString& path)
{
QFile in (path);
if (!in.open (QFile::ReadOnly))
{
qDebug ().nospace ().noquote () << "Unable to open file \"" << path << "\".";
return;
}
auto lines = in.readAll ().split ('\n');
jsonLines += lines.count ();
}
void processFile (const QFileInfo& entry)
{
auto suffix = entry.suffix ();
if (suffix == QStringLiteral ("cpp") || suffix == QStringLiteral ("c"))
{
codeFiles++;
processCode (entry.absoluteFilePath ());
}
else if (suffix == QStringLiteral ("h"))
{
headerFiles++;
processCode (entry.absoluteFilePath ());
}
else if (suffix == QStringLiteral ("qrc"))
{
resourceFiles++;
}
else if (suffix == QStringLiteral ("json") && incJson)
{
jsonFiles++;
processJson (entry.absoluteFilePath ());
}
else if (imageSuffixes.contains (suffix))
{
imageFiles++;
}
else
{
otherFiles++;
}
}
bool processFolder (const QString& path)
{
const QDir dir (path);
if (!dir.exists ())
{
qDebug ().nospace ().noquote () << "No such folder \"" << path << "\".";
return false;
}
if (path.endsWith (QStringLiteral ("/build")))
{
return false;
}
auto entries = dir.entryInfoList (QDir::Files | QDir::NoDotAndDotDot);
for (const auto& entry : std::as_const (entries))
{
processFile (entry);
}
if (recurse)
{
entries = dir.entryInfoList (QDir::Dirs | QDir::NoDotAndDotDot);
for (const auto& entry : std::as_const (entries))
{
processFolder (entry.absoluteFilePath ());
}
}
return true;
}
int main (int argc, char* argv[])
{
const QCoreApplication app (argc, argv);
QCoreApplication::setApplicationName (QStringLiteral ("lineCounter"));
QCoreApplication::setApplicationVersion (QStringLiteral (VERSION));
QCoreApplication::setOrganizationName (QStringLiteral ("Netherwood Industries"));
QCommandLineParser parser;
parser.setApplicationDescription (QStringLiteral ("Calculate line metrics for C/C++ code."));
parser.addHelpOption ();
parser.addVersionOption ();
parser.addPositionalArgument (QStringLiteral ("root"), QStringLiteral ("Path to root of code project"));
parser.addOption (QCommandLineOption (QStringList () << QByteArrayLiteral ("recurse") << QByteArrayLiteral ("r"), QStringLiteral ("Recurse into sub directories")));
parser.addOption (QCommandLineOption (QStringList () << QByteArrayLiteral ("include-json") << QByteArrayLiteral ("j"), QStringLiteral ("Additionally, show JSON files & lines.")));
parser.process (app);
recurse = parser.isSet (QStringLiteral ("recurse"));
incJson = parser.isSet ("j");
if (parser.positionalArguments ().isEmpty ())
{
parser.showHelp (100);
}
QFileInfo info (parser.positionalArguments ().constFirst ());
bool showFileInfo = true;
if (info.exists () && info.isFile ())
{
processFile (info);
showFileInfo = false;
}
if (processFolder (parser.positionalArguments ().constFirst ()))
{
const QLocale locale;
if (showFileInfo)
{
out << "Files: " << '\n';
out << " Code: " << locale.toString (codeFiles) << '\n';
out << " Header: " << locale.toString (headerFiles) << '\n';
out << " Resource: " << locale.toString (resourceFiles) << '\n';
if (incJson)
{
out << " Json: " << locale.toString(jsonFiles) << '\n';
}
out << " Images: " << locale.toString (imageFiles) << '\n';
out << " Other: " << locale.toString (otherFiles) << '\n';
out << " TOTAL: " << locale.toString (codeFiles + headerFiles + resourceFiles + otherFiles) << "\n\n";
}
out << "Lines: " << '\n';
out << " Code: " << locale.toString (codeLines) << '\n';
out << " Comment: " << locale.toString (commentLines) << '\n';
out << " Preprocessor: " << locale.toString (preprocessorLines) << '\n';
out << " Empty: " << locale.toString (emptyLines) << '\n';
if (incJson)
{
out << " JSON: " << locale.toString (jsonLines) << '\n';
}
out << " TOTAL " << locale.toString (codeLines + commentLines + preprocessorLines + emptyLines + jsonLines) << '\n';
return 0;
}
return 100;
}