-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexacount.c
More file actions
414 lines (345 loc) · 11.9 KB
/
Copy pathlexacount.c
File metadata and controls
414 lines (345 loc) · 11.9 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
LexaCount - A lightweight command-line tool to count source lines of code.
Description:
LexaCount simplifies the process of analyzing and understanding the structure
of source code in software projects. It provides various options to adapt to
different needs, such as excluding lines with only brackets or using table output.
Copyright (c) 2025 anic17 Software
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include "lexacount.h"
int in_string(char *s, int chr)
{
return strrchr(s, chr) != NULL;
}
int strlen_n(const char *s)
{
if (!s)
return 0;
return strlen(s);
}
void show_version()
{
printf("LexaCount v%s - Count source lines of code.\n", lexacount_version);
}
void show_copyright()
{
printf("Copyright (c) 2025 anic17 Software\n");
}
void help()
{
show_version();
printf(
"Usage:\n"
" lexacount [-b] [-t] [-l <list file>] [file1] [file2] ...\n\n"
"Switches:\n"
" -b, --bracket Do not count lines containing only brackets or parenthesis\n"
" -h, --help Display this help menu\n"
" -l, --list Load file names from a file\n"
" -t, --table Print the output using tables\n"
" -v, --version Display program version\n"
" -x, --exclude Exclude files that failed to open\n\n"
);
show_copyright();
}
void missing_param(char *s)
{
fprintf(stderr, "Error: Required parameter after '%s'. See 'lexacount --help' for more information.\n", s);
exit(1);
}
size_t count_loc(char *filename, item_count *ic, text_fields *tf, param *p, int *result)
{
ic->loc = 0;
ic->comment = 0;
ic->blank = 0;
ic->bracket = 0;
filename[strcspn(filename, "\r\n")] = '\0';
if (filename[0] == '\0')
{
*result = -EINVAL;
return 0;
}
FILE *count_fp = fopen(filename, "rb");
if (!count_fp)
{
if (!p->excludeFiles)
fprintf(stderr, "Error: %s: %s\n", filename, strerror(errno));
*result = -errno;
return 0;
}
bool isLineBlank = true;
bool isLineOnlyBrackets = false;
bool isFileEmpty = true;
bool quoteStatus = false;
size_t comment_idx = 0;
size_t multi_comment_idx = 0;
int chr = 0;
size_t clen[3] = {strlen_n(tf->comments[0]), strlen_n(tf->comments[1]), strlen_n(tf->comments[2])};
while ((chr = fgetc(count_fp)) != EOF)
{
isFileEmpty = false;
if (in_string(tf->quotes, chr))
quoteStatus ^= chr;
if (isLineBlank && !quoteStatus)
{
if (comment_idx >= clen[0] - 1)
{
ic->comment++;
comment_idx = 0;
continue;
}
if (chr == tf->comments[0][comment_idx])
{
comment_idx++;
continue;
}
}
if ((!isspace(chr) || (!in_string(tf->brackets, chr) && p->excludeBrackets)))
{
if (in_string(tf->brackets, chr) && p->excludeBrackets)
{
isLineOnlyBrackets = true;
}
else
{
if (!isspace(chr))
{
isLineBlank = false;
isLineOnlyBrackets = false;
}
}
}
if (chr == '\n')
{
if (isLineOnlyBrackets && isLineBlank)
ic->bracket++;
else if (isLineBlank)
ic->blank++;
ic->loc++;
isLineBlank = true;
isLineOnlyBrackets = false;
quoteStatus = 0;
comment_idx = 0;
}
}
if (!isFileEmpty)
{
if (isLineOnlyBrackets && isLineBlank)
ic->bracket++;
else if (isLineBlank)
ic->blank++;
ic->loc++;
}
fclose(count_fp);
*result = 1; // > 0 means success, == 0 means error
return ic->loc;
}
void print_table_header(const char *formatspec, bool excludeBrackets)
{
printf("| ");
printf(formatspec, "File");
printf(" | %-10s | %-10s | %-10s |", "Code lines", "Blank", "Comments");
if (excludeBrackets)
printf(" %-10s |", "Brackets");
printf("\n");
}
void print_table_separator(size_t flen, param *p)
{
printf("|");
for (size_t k = 0; k < flen + 2; k++)
putchar('-');
printf("|------------|------------|------------|");
if (p->excludeBrackets)
printf("------------|");
printf("\n");
}
void print_table_row(const char *formatspec, const char *filename, item_count *ic, param *p)
{
printf("| ");
printf(formatspec, filename);
printf(" | %-10zu | %-10zu | %-10zu |", ic->loc - ic->blank - ic->comment - ic->bracket * (!!p->excludeBrackets), ic->blank, ic->comment);
if (p->excludeBrackets)
printf(" %-10zu |", ic->bracket);
printf("\n");
}
void display_lines(const char *formatspec, const char *filename, item_count *ic, param *p)
{
if (p->tableOutput)
print_table_row(formatspec, filename, ic, p);
else
{
printf("%s: %zu code lines", filename, ic->loc - ic->blank - ic->comment - ic->bracket * (!!p->excludeBrackets));
if (ic->blank > 0 || ic->comment > 0 || ic->bracket > 0)
{
printf(" (and");
if (ic->blank > 0)
printf(" %zu blank line%s", ic->blank, (ic->blank == 1) ? "" : "s");
if (ic->comment > 0)
printf("%s%zu comment%s", (ic->blank > 0) ? " and " : " ", ic->comment, (ic->comment == 1) ? "" : "s");
if (ic->bracket > 0 && p->excludeBrackets)
printf("%s%zu line%s with only brackets", ((ic->blank > 0) || (ic->comment > 0)) ? " and " : " ", ic->bracket, (ic->bracket == 1) ? "" : "s");
printf(")");
}
printf("\n");
}
}
int main(int argc, char *argv[])
{
if (argc < 2 || !strcmp(argv[1], "--help") || !strcmp(argv[1], "/?") || !strcmp(argv[1], "-h"))
{
help();
return 0;
}
char *loc_file = argv[1];
FILE *list;
char *comments[] = {"//", "/*", "*/"};
char **fnames = calloc(argc - 1, sizeof(char *)), **list_fnames = calloc(argc - 1, sizeof(char *));
size_t fname_count = 0, list_fname_count = 0;
size_t longest_fname = 0;
item_count ic = {0, 0, 0, 0}, total = {0, 0, 0, 0};
text_fields tf = {comments, "{}[]();", "'\""};
param p = {NULL, false, false};
for (int i = 1; i < argc; i++)
{
if (!strcmp(argv[i], "-l") || !strcmp(argv[i], "--list"))
{
if (i + 1 < argc)
{
if (argv[i] != NULL)
{
list_fnames[list_fname_count] = calloc(strlen_n(argv[i + 1]) + 1, sizeof(char));
memcpy(list_fnames[list_fname_count], argv[i + 1], strlen_n(argv[i + 1]));
if (strlen_n(list_fnames[list_fname_count]) > longest_fname)
longest_fname = strlen_n(list_fnames[list_fname_count]);
list_fname_count++;
}
i++;
}
else
{
missing_param(argv[i]);
}
}
else if (!strcmp(argv[i], "-b") || !strcmp(argv[i], "--bracket"))
{
p.excludeBrackets = true;
}
else if (!strcmp(argv[i], "-t") || !strcmp(argv[i], "--table"))
{
p.tableOutput = true;
}
else if (!strcmp(argv[i], "-x") || !strcmp(argv[i], "--exclude"))
{
p.excludeFiles = true;
}
else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version"))
{
show_version();
show_copyright();
exit(0);
}
else
{
if (argv[i] != NULL)
{
fnames[fname_count] = calloc(strlen_n(argv[i]) + 1, sizeof(char));
memcpy(fnames[fname_count], argv[i], strlen_n(argv[i]));
if (strlen_n(fnames[fname_count]) > longest_fname)
longest_fname = strlen_n(fnames[fname_count]);
fname_count++;
}
}
}
size_t count = 0;
char filename[512];
if (longest_fname < 11)
longest_fname = 11;
char *fspec = calloc(sizeof(char), longest_fname + 10);
snprintf(fspec, longest_fname, "%%-%zus", longest_fname);
int count_retcode = 0;
for (size_t i = 0; i < list_fname_count; i++) // Load all filenames from all the list files into memory
{
list = fopen(list_fnames[i], "rb");
if (!list)
{
if (!p.excludeFiles)
fprintf(stderr, "Error: %s: %s", list_fnames[i], strerror(errno)); // List file error
return -errno;
}
while (fgets(filename, sizeof filename, list)) // If someone knows a better way to avoid incorrect padding, let me know
{
if (strlen_n(filename) > longest_fname)
{
longest_fname = strlen_n(filename);
snprintf(fspec, longest_fname, "%%-%zus", longest_fname);
}
}
fclose(list);
}
if (p.tableOutput)
{
print_table_header(fspec, p.excludeBrackets);
if (list_fname_count == 0)
print_table_separator(longest_fname, &p);
}
for (size_t i = 0; i < list_fname_count; i++) // Process the filenames from the list files
{
list = fopen(list_fnames[i], "rb");
if (!list)
{
if (!p.excludeFiles)
fprintf(stderr, "Error: %s: %s", list_fnames[i], strerror(errno)); // Files inside list file error
return errno;
}
if (p.tableOutput)
print_table_separator(longest_fname, &p);
while (fgets(filename, sizeof filename, list)) // Process each individual file from each list
{
count_loc(filename, &ic, &tf, &p, &count_retcode);
if (count_retcode >= 0)
{
total.loc += ic.loc;
total.comment += ic.comment;
total.blank += ic.blank;
total.bracket += ic.bracket;
display_lines(fspec, filename, &ic, &p);
}
count++;
}
fclose(list);
}
int valid_file = 0;
for (size_t i = 0; i < fname_count; i++) // Process normal files
{
if ((valid_file = count_loc(fnames[i], &ic, &tf, &p, &count_retcode)) >= 0)
display_lines(fspec, fnames[i], &ic, &p);
total.loc += ic.loc;
total.comment += ic.comment;
total.blank += ic.blank;
total.bracket += ic.bracket;
count++;
}
if (p.tableOutput)
print_table_separator(longest_fname, &p);
if (valid_file >= 0)
display_lines(fspec, "Total lines", &total, &p);
free(fspec);
free(list_fnames);
free(fnames);
return 0;
}