src/cline.c

changeset 66
be2084398c37
parent 62
7f5f9f43d0c0
equal deleted inserted replaced
65:49fa681f3a7e 66:be2084398c37
37 "\n\nCounts the line terminator characters (\\n) within all" 37 "\n\nCounts the line terminator characters (\\n) within all"
38 " files in the specified\ndirectories." 38 " files in the specified\ndirectories."
39 "\n\nOptions:" 39 "\n\nOptions:"
40 "\n -b <level> - binary file heuristics level (default medium)" 40 "\n -b <level> - binary file heuristics level (default medium)"
41 "\n One of: ignore low medium high" 41 "\n One of: ignore low medium high"
42 "\n -c - Count non-whitespace characters instead of lines"
42 "\n -E <pattern> - Excludes any line matching the <pattern>" 43 "\n -E <pattern> - Excludes any line matching the <pattern>"
43 "\n -e <start> <end> - Excludes lines between <start> and <end>" 44 "\n -e <start> <end> - Excludes lines between <start> and <end>"
44 "\n You may use these options multiple times" 45 "\n You may use these options multiple times"
45 "\n -h, --help - this help text" 46 "\n -h, --help - this help text"
46 "\n -i - print out individual sums per file extension" 47 "\n -i - print out individual sums per file extension"
61 "\n cline ./\n\n" 62 "\n cline ./\n\n"
62 "So each file in the working directory is counted. If you want to count C" 63 "So each file in the working directory is counted. If you want to count C"
63 "\nsource code in your working directory and its subdirectories, type:" 64 "\nsource code in your working directory and its subdirectories, type:"
64 "\n cline -rs .c\n" 65 "\n cline -rs .c\n"
65 "\nIf you want to exclude comment lines, you may use the -e/-E option." 66 "\nIf you want to exclude comment lines, you may use the -e/-E option."
66 "\nAfter a line matches the regex pattern <start> any following line is" 67 "\nAfter a line matches the regex pattern <start>, this and any following"
67 "\nnot counted unless a line matches the <end> pattern. A line is still " 68 "\nline is not counted unless a line matches the <end> pattern. A line is"
68 "\ncounted when it does not start or end with the respective patterns." 69 "\nstill counted when it does not start or end with the respective pattern."
69 "\nPlease note, that cline does not remove whitespace characters as this" 70 "\nPlease note, that cline does not trim the lines before matching against"
70 "\nmight not be reasonable in some cases." 71 "\nthe pattern."
71 "\n\nExample (C without comments):" 72 "\n\nExample (C without comments):"
72 "\n cline -s .c,.h --exclude-cstyle-comments" 73 "\n cline -s .c,.h --exclude-cstyle-comments"
73 "\n"); 74 "\n");
74 } 75 }
75 76
105 char* excludeSuffix = NULL; 106 char* excludeSuffix = NULL;
106 int checked = 0; 107 int checked = 0;
107 108
108 for (int t = 1 ; t < argc ; t++) { 109 for (int t = 1 ; t < argc ; t++) {
109 110
110 int argflags = checkArgument(argv[t], "hsSrRmvVbeEi"); 111 int argflags = checkArgument(argv[t], "hsSrRmvVbeEic");
111 int paropt = 0; 112 int paropt = 0;
112 113
113 /* h */ 114 /* h */
114 if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { 115 if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) {
115 return exit_with_help(settings, 0); 116 return exit_with_help(settings, 0);
199 add_string(settings->regex->pattern_list, argv[t]); 200 add_string(settings->regex->pattern_list, argv[t]);
200 add_string(settings->regex->pattern_list, "$"); 201 add_string(settings->regex->pattern_list, "$");
201 } 202 }
202 /* i */ 203 /* i */
203 if ((argflags & 2048) > 0) { 204 if ((argflags & 2048) > 0) {
204 // cannot be used together with -V 205 /* cannot be used together with -V */
205 if (registerArgument(&checked, 128)) { 206 if (registerArgument(&checked, 128)) {
206 return exit_with_help(settings, 1); 207 return exit_with_help(settings, 1);
207 } 208 }
208 settings->individual_sums = true; 209 settings->individual_sums = true;
210 }
211 if ((argflags & 4096) > 0) {
212 if (registerArgument(&checked, 4096)) {
213 return exit_with_help(settings, 1);
214 }
215 settings->count_chars = true;
216 settings->regex->count_chars = true;
209 } 217 }
210 if (argflags == 0) { 218 if (argflags == 0) {
211 /* SHORTCUTS */ 219 /* SHORTCUTS */
212 if (strcmp(argv[t], "--exclude-cstyle-comments") == 0) { 220 if (strcmp(argv[t], "--exclude-cstyle-comments") == 0) {
213 add_string(settings->regex->pattern_list, "\\s*//"); 221 add_string(settings->regex->pattern_list, "\\s*//");
233 if (regex_compile_all(settings->regex)) { 241 if (regex_compile_all(settings->regex)) {
234 scanresult_t* result = new_scanresult_t(settings); 242 scanresult_t* result = new_scanresult_t(settings);
235 /* Don't waste memory when only the total sum is needed */ 243 /* Don't waste memory when only the total sum is needed */
236 string_list_t *output = settings->verbose ? new_string_list_t() : NULL; 244 string_list_t *output = settings->verbose ? new_string_list_t() : NULL;
237 char *outbuf; 245 char *outbuf;
246 const char* result_type = settings->count_chars ? "chars" : "lines";
238 247
239 int total = 0; 248 unsigned total = 0;
240 if (directories->count == 0) { 249 if (directories->count == 0) {
241 add_string(directories, "."); 250 add_string(directories, ".");
242 } 251 }
243 for (int t = 0 ; t < directories->count ; t++) { 252 for (unsigned t = 0 ; t < directories->count ; t++) {
244 scanDirectory((scanner_t){directories->items[t], 0}, settings, 253 scanDirectory((scanner_t){directories->items[t], 0}, settings,
245 output, result); 254 output, result);
246 total += result->lines; 255 total += result->result;
247 if (directories->count > 1 ) { 256 if (directories->count > 1 ) {
248 outbuf = (char*) malloc(81); 257 outbuf = (char*) malloc(81);
249 memset(outbuf, '-', 79); 258 memset(outbuf, '-', 79);
250 outbuf[79] = '\n'; 259 outbuf[79] = '\n';
251 outbuf[80] = 0; 260 outbuf[80] = 0;
252 add_string(output, outbuf); 261 add_string(output, outbuf);
253 outbuf = (char*) malloc(81); 262 outbuf = (char*) malloc(81);
254 snprintf(outbuf, 81, "%-63s%10d lines\n", directories->items[t], 263 snprintf(outbuf, 81, "%-63s%10u %s\n", directories->items[t],
255 result->lines); 264 result->result, result_type);
256 add_string(output, outbuf); 265 add_string(output, outbuf);
257 outbuf = (char*) malloc(81); 266 outbuf = (char*) malloc(81);
258 memset(outbuf, '-', 79); 267 memset(outbuf, '-', 79);
259 outbuf[79] = '\n'; 268 outbuf[79] = '\n';
260 outbuf[80] = 0; 269 outbuf[80] = 0;
270 free(output->items[i]); 279 free(output->items[i]);
271 } 280 }
272 281
273 if (result->ext) { 282 if (result->ext) {
274 if (result->ext->count > 0) { 283 if (result->ext->count > 0) {
275 for (int t = 0 ; t < 79 ; t++) { 284 for (unsigned t = 0 ; t < 79 ; t++) {
276 printf("="); 285 printf("=");
277 } 286 }
278 printf("\nIndividual sums:\n"); 287 printf("\nIndividual sums:\n");
279 for (int t = 0 ; t < result->ext->count ; t++) { 288 for (unsigned t = 0 ; t < result->ext->count ; t++) {
280 printf(" %-62s%10d lines\n", 289 printf(" %-62s%10u %s\n",
281 result->ext->extensions[t], 290 result->ext->extensions[t],
282 result->ext->lines[t]); 291 result->ext->result[t],
292 result_type);
283 } 293 }
284 } 294 }
285 } 295 }
286 296
287 for (int t = 0 ; t < 79 ; t++) { 297 for (unsigned t = 0 ; t < 79 ; t++) {
288 printf("="); 298 printf("=");
289 } 299 }
290 printf("\n%73d lines\n", total); 300 printf("\n%73d %s\n", total, result_type);
291 301
292 if (settings->confusing_lnlen && 302 if (settings->confusing_lnlen &&
293 settings->regex->pattern_list->count > 0) { 303 settings->regex->pattern_list->count > 0) {
294 304
295 printf("\nSome files contain too long lines.\n" 305 printf("\nSome files contain too long lines.\n"
296 "The regex parser currently supports a maximum line length of %d." 306 "The parser currently supports a maximum line length of %u."
297 "\nThe result might be wrong.\n", REGEX_MAX_LINELENGTH); 307 "\nThe result might be wrong.\n", MAX_LINELENGTH);
298 } 308 }
299 } else { 309 } else {
300 printf("%d", total); 310 printf("%u", total);
301 } 311 }
302 destroy_scanresult_t(result); 312 destroy_scanresult_t(result);
303 destroy_string_list_t(output); 313 destroy_string_list_t(output);
304 destroy_settings_t(settings); 314 destroy_settings_t(settings);
305 } 315 }

mercurial