| 79 "\n\nExample (C without comments):" |
79 "\n\nExample (C without comments):" |
| 80 "\n cline -s .c,.h --exclude-cstyle-comments" |
80 "\n cline -s .c,.h --exclude-cstyle-comments" |
| 81 "\n"); |
81 "\n"); |
| 82 } |
82 } |
| 83 |
83 |
| 84 static int exit_with_version(settings_t* settings) { |
84 static int exit_with_version(settings* settings) { |
| 85 printf("cline - Version: " VERSION "\n"); |
85 printf("cline - Version: " VERSION "\n"); |
| 86 destroy_settings_t(settings); |
86 destroy_settings(settings); |
| 87 return 0; |
87 return 0; |
| 88 } |
88 } |
| 89 |
89 |
| 90 static int exit_with_help(settings_t* settings, int code) { |
90 static int exit_with_help(settings* settings, int code) { |
| 91 printf("cline - Version: " VERSION "\n"); |
91 printf("cline - Version: " VERSION "\n"); |
| 92 printHelpText(); |
92 printHelpText(); |
| 93 destroy_settings_t(settings); |
93 destroy_settings(settings); |
| 94 return code; |
94 return code; |
| 95 } |
95 } |
| 96 |
96 |
| 97 static void normalize_excluded_dirs(settings_t *settings) { |
97 static void normalize_excluded_dirs(settings *settings) { |
| 98 /* normalize all paths */ |
98 /* normalize all paths */ |
| 99 for (int i = 0 ; i < settings->excludeDirs->count ; i++) { |
99 for (int i = 0 ; i < settings->excludeDirs->count ; i++) { |
| 100 char *arg = strdup(settings->excludeDirs->items[i]); |
100 char *arg = strdup(settings->excludeDirs->items[i]); |
| 101 if (strpbrk(arg, "/\\") == NULL) { |
101 if (strpbrk(arg, "/\\") == NULL) { |
| 102 /* do not normalize names */ |
102 /* do not normalize names */ |
| 125 static const char * sepline_single = "-------------------------------------------------------------------------------\n"; |
125 static const char * sepline_single = "-------------------------------------------------------------------------------\n"; |
| 126 |
126 |
| 127 int main(int argc, char** argv) { |
127 int main(int argc, char** argv) { |
| 128 |
128 |
| 129 /* Settings */ |
129 /* Settings */ |
| 130 settings_t *settings = new_settings_t(); |
130 settings *settings = new_settings(); |
| 131 if (settings == NULL) { |
131 if (settings == NULL) { |
| 132 fprintf(stderr, "Memory allocation failed.\n"); |
132 fprintf(stderr, "Memory allocation failed.\n"); |
| 133 return 1; |
133 return 1; |
| 134 } |
134 } |
| 135 |
135 |
| 136 /* Get arguments */ |
136 /* Get arguments */ |
| 137 string_list_t *directories = new_string_list_t(); |
137 string_list *directories = new_string_list(); |
| 138 if (directories == NULL) { |
138 if (directories == NULL) { |
| 139 fprintf(stderr, "Memory allocation failed.\n"); |
139 fprintf(stderr, "Memory allocation failed.\n"); |
| 140 return 1; |
140 return 1; |
| 141 } |
141 } |
| 142 char* includeSuffix = NULL; |
142 char* includeSuffix = NULL; |
| 298 parseCSL(includeSuffix, settings->includeSuffixes); |
298 parseCSL(includeSuffix, settings->includeSuffixes); |
| 299 parseCSL(excludeSuffix, settings->excludeSuffixes); |
299 parseCSL(excludeSuffix, settings->excludeSuffixes); |
| 300 |
300 |
| 301 /* Compiler regular expressions, if any specified */ |
301 /* Compiler regular expressions, if any specified */ |
| 302 if (!regex_compile_all(settings->regex)) { |
302 if (!regex_compile_all(settings->regex)) { |
| 303 destroy_string_list_t(directories); |
303 destroy_string_list(directories); |
| 304 destroy_settings_t(settings); |
304 destroy_settings(settings); |
| 305 return 1; |
305 return 1; |
| 306 } |
306 } |
| 307 |
307 |
| 308 /* Normalize paths for excluded directories */ |
308 /* Normalize paths for excluded directories */ |
| 309 normalize_excluded_dirs(settings); |
309 normalize_excluded_dirs(settings); |
| 310 |
310 |
| 311 /* Scan directories */ |
311 /* Scan directories */ |
| 312 scanresult_t* result = new_scanresult_t(settings); |
312 scanresult* result = new_scanresult(settings); |
| 313 const char* result_type = settings->count_chars ? "chars" : "lines"; |
313 const char* result_type = settings->count_chars ? "chars" : "lines"; |
| 314 bool has_output = false; |
314 bool has_output = false; |
| 315 unsigned total = 0; |
315 unsigned total = 0; |
| 316 if (directories->count == 0) { |
316 if (directories->count == 0) { |
| 317 add_string(directories, "."); |
317 add_string(directories, "."); |
| 318 } |
318 } |
| 319 for (unsigned t = 0 ; t < directories->count ; t++) { |
319 for (unsigned t = 0 ; t < directories->count ; t++) { |
| 320 /* Don't waste memory when only the total sum is needed */ |
320 /* Don't waste memory when only the total sum is needed */ |
| 321 string_list_t *output = NULL; |
321 string_list *output = NULL; |
| 322 if (settings->verbose) { |
322 if (settings->verbose) { |
| 323 output = new_string_list_t(); |
323 output = new_string_list(); |
| 324 output->free_item = free; |
324 output->free_item = free; |
| 325 } |
325 } |
| 326 scanDirectory((scanner_t){directories->items[t], 0}, settings, output, result); |
326 scanDirectory((scanner){directories->items[t], 0}, settings, output, result); |
| 327 total += result->result; |
327 total += result->result; |
| 328 if (settings->verbose) { |
328 if (settings->verbose) { |
| 329 has_output |= output->count > 0; |
329 has_output |= output->count > 0; |
| 330 for (int i = 0 ; i < output->count ; i++) { |
330 for (int i = 0 ; i < output->count ; i++) { |
| 331 printf("%s", output->items[i]); |
331 printf("%s", output->items[i]); |
| 332 } |
332 } |
| 333 destroy_string_list_t(output); |
333 destroy_string_list(output); |
| 334 if (directories->count > 1) { |
334 if (directories->count > 1) { |
| 335 has_output = true; |
335 has_output = true; |
| 336 fputs(sepline_single, stdout); |
336 fputs(sepline_single, stdout); |
| 337 printf("%-63s%10u %s\n", directories->items[t], |
337 printf("%-63s%10u %s\n", directories->items[t], |
| 338 result->result, result_type); |
338 result->result, result_type); |
| 339 fputs(sepline_single, stdout); |
339 fputs(sepline_single, stdout); |
| 340 } |
340 } |
| 341 } |
341 } |
| 342 } |
342 } |
| 343 destroy_string_list_t(directories); |
343 destroy_string_list(directories); |
| 344 |
344 |
| 345 /* Print result */ |
345 /* Print result */ |
| 346 if (settings->verbose) { |
346 if (settings->verbose) { |
| 347 if (result->ext) { |
347 if (result->ext) { |
| 348 if (result->ext->count > 0) { |
348 if (result->ext->count > 0) { |