src/cline.c

changeset 82
44b3332dfe03
parent 80
55dc85743dd1
equal deleted inserted replaced
81:54515e5d60f5 82:44b3332dfe03
259 259
260 /* Find tokens */ 260 /* Find tokens */
261 parseCSL(includeSuffix, settings->includeSuffixes); 261 parseCSL(includeSuffix, settings->includeSuffixes);
262 parseCSL(excludeSuffix, settings->excludeSuffixes); 262 parseCSL(excludeSuffix, settings->excludeSuffixes);
263 263
264 /* Compiler regular expressions, if any specified */
265 if (!regex_compile_all(settings->regex)) {
266 return 1;
267 }
268
264 /* Scan directories */ 269 /* Scan directories */
265 if (regex_compile_all(settings->regex)) { 270 scanresult_t* result = new_scanresult_t(settings);
266 scanresult_t* result = new_scanresult_t(settings); 271 const char* result_type = settings->count_chars ? "chars" : "lines";
272 bool has_output = false;
273 unsigned total = 0;
274 if (directories->count == 0) {
275 add_string(directories, "./");
276 }
277 for (unsigned t = 0 ; t < directories->count ; t++) {
267 /* Don't waste memory when only the total sum is needed */ 278 /* Don't waste memory when only the total sum is needed */
268 string_list_t *output = settings->verbose ? new_string_list_t() : NULL; 279 string_list_t *output = settings->verbose ? new_string_list_t() : NULL;
269 char *outbuf; 280 scanDirectory((scanner_t){directories->items[t], 0}, settings, output, result);
270 const char* result_type = settings->count_chars ? "chars" : "lines"; 281 total += result->result;
271 282 if (settings->verbose) {
272 unsigned total = 0; 283 has_output |= output->count > 0;
273 if (directories->count == 0) {
274 add_string(directories, "./");
275 }
276 for (unsigned t = 0 ; t < directories->count ; t++) {
277 scanDirectory((scanner_t){directories->items[t], 0}, settings,
278 output, result);
279 total += result->result;
280 if (directories->count > 1) {
281 add_string(output, strdup(sepline_single));
282 outbuf = (char*) malloc(81);
283 snprintf(outbuf, 81, "%-63s%10u %s\n", directories->items[t],
284 result->result, result_type);
285 add_string(output, outbuf);
286 add_string(output, strdup(sepline_single));
287 }
288 }
289 destroy_string_list_t(directories);
290
291 /* Print result */
292 if (output != NULL) {
293 for (int i = 0 ; i < output->count ; i++) { 284 for (int i = 0 ; i < output->count ; i++) {
294 printf("%s", output->items[i]); 285 printf("%s", output->items[i]);
295 free(output->items[i]); 286 free(output->items[i]);
296 } 287 }
297 288 destroy_string_list_t(output);
298 if (result->ext) { 289 if (directories->count > 1) {
299 if (result->ext->count > 0) { 290 has_output = true;
300 fwrite(sepline_double, 1, 80, stdout); 291 fputs(sepline_single, stdout);
301 printf("\nIndividual sums:\n"); 292 printf("%-63s%10u %s\n", directories->items[t],
302 for (unsigned t = 0 ; t < result->ext->count ; t++) { 293 result->result, result_type);
303 printf(" %-62s%10u %s\n", 294 fputs(sepline_single, stdout);
304 result->ext->extensions[t], 295 }
305 result->ext->result[t], 296 }
306 result_type); 297 }
307 } 298 destroy_string_list_t(directories);
299
300 /* Print result */
301 if (settings->verbose) {
302 if (result->ext) {
303 if (result->ext->count > 0) {
304 has_output = true;
305 fwrite(sepline_double, 1, 80, stdout);
306 printf("\nIndividual sums:\n");
307 for (unsigned t = 0 ; t < result->ext->count ; t++) {
308 printf(" %-62s%10u %s\n",
309 result->ext->extensions[t],
310 result->ext->result[t],
311 result_type);
308 } 312 }
309 } 313 }
310 314 }
311 if (output->count > 0) { 315
312 fwrite(sepline_double, 1, 80, stdout); 316 if (has_output) {
313 printf("%73d %s\n", total, result_type); 317 fwrite(sepline_double, 1, 80, stdout);
314 } else { 318 printf("%73d %s\n", total, result_type);
315 /* If we only need to output a total, output it differently */
316 printf("%d %s\n", total, result_type);
317 }
318
319 if (settings->confusing_lnlen &&
320 settings->regex->pattern_list->count > 0) {
321
322 printf("\nSome files contain too long lines.\n"
323 "The parser currently supports a maximum line length of %u."
324 "\nThe result might be wrong.\n", MAX_LINELENGTH);
325 }
326 } else { 319 } else {
327 printf("%u", total); 320 /* If there was not any output so far, skip formatting */
328 } 321 printf("%d %s\n", total, result_type);
329 destroy_scanresult_t(result); 322 }
330 destroy_string_list_t(output); 323
331 destroy_settings_t(settings); 324 if (settings->confusing_lnlen &&
332 } 325 settings->regex->pattern_list->count > 0) {
333 326
334 fflush(stdout); 327 printf("\nSome files contain too long lines.\n"
335 fflush(stderr); 328 "The parser currently supports a maximum line length of %u."
329 "\nThe result might be wrong.\n", MAX_LINELENGTH);
330 }
331 } else {
332 printf("%u", total);
333 }
334 destroy_scanresult_t(result);
335 destroy_settings_t(settings);
336
336 return 0; 337 return 0;
337 } 338 }

mercurial