1.1 --- a/src/cline.c Wed May 22 10:57:17 2013 +0200 1.2 +++ b/src/cline.c Wed May 22 13:00:36 2013 +0200 1.3 @@ -33,7 +33,6 @@ 1.4 #include "scanner.h" 1.5 #include "settings.h" 1.6 #include "arguments.h" 1.7 -#include "stream.h" 1.8 #include "regex_parser.h" 1.9 1.10 void printHelpText() { 1.11 @@ -219,48 +218,65 @@ 1.12 } 1.13 } 1.14 1.15 - /* Configure output */ 1.16 - if (!settings->verbose) { 1.17 - close_stdout(); 1.18 - } 1.19 - 1.20 /* Find tokens */ 1.21 parseCSL(includeSuffix, settings->includeSuffixes); 1.22 parseCSL(excludeSuffix, settings->excludeSuffixes); 1.23 1.24 /* Scan directories */ 1.25 if (regex_compile_all(settings->regex)) { 1.26 - int lines = 0; 1.27 + /* Don't waste memory when only the total sum is needed */ 1.28 + string_list_t *output = settings->verbose ? new_string_list_t() : NULL; 1.29 + char *outbuf; 1.30 + 1.31 + int lineSum = 0, lines; 1.32 if (directories->count == 0) { 1.33 add_string(directories, "."); 1.34 } 1.35 for (int t = 0 ; t < directories->count ; t++) { 1.36 - if (t > 0) { 1.37 - for (int u = 0 ; u < 79 ; u++) { 1.38 - printf("-"); 1.39 - } 1.40 - printf("\n"); 1.41 + lines = scanDirectory((scanner_t){directories->items[t], 0}, settings, 1.42 + output); 1.43 + lineSum += lines; 1.44 + if (directories->count > 1 ) { 1.45 + outbuf = (char*) malloc(81); 1.46 + memset(outbuf, '-', 79); 1.47 + outbuf[79] = '\n'; 1.48 + outbuf[80] = 0; 1.49 + add_string(output, outbuf); 1.50 + outbuf = (char*) malloc(81); 1.51 + snprintf(outbuf, 81, "%-63s%10d lines\n", directories->items[t], lines); 1.52 + add_string(output, outbuf); 1.53 + outbuf = (char*) malloc(81); 1.54 + memset(outbuf, '-', 79); 1.55 + outbuf[79] = '\n'; 1.56 + outbuf[80] = 0; 1.57 + add_string(output, outbuf); 1.58 } 1.59 - lines += scanDirectory((scanner_t){directories->items[t], 0}, settings); 1.60 } 1.61 destroy_string_list_t(directories); 1.62 1.63 - /* Print double line and line count */ 1.64 - for (int t = 0 ; t < 79 ; t++) { 1.65 - printf("="); 1.66 + /* Print result */ 1.67 + if (settings->verbose) { 1.68 + for (int i = 0 ; i < output->count ; i++) { 1.69 + printf("%s", output->items[i]); 1.70 + free(output->items[i]); 1.71 + } 1.72 + 1.73 + for (int t = 0 ; t < 79 ; t++) { 1.74 + printf("="); 1.75 + } 1.76 + printf("\n%73d lines\n", lineSum); 1.77 + 1.78 + if (settings->confusing_lnlen && 1.79 + settings->regex->pattern_list->count > 0) { 1.80 + 1.81 + printf("\nSome files contain too long lines.\n" 1.82 + "The regex parser currently supports a maximum line length of %d." 1.83 + "\nThe result might be wrong.\n", REGEX_MAX_LINELENGTH); 1.84 + } 1.85 + } else { 1.86 + printf("%d", lineSum); 1.87 } 1.88 - printf("\n%73d lines\n", lines); 1.89 - 1.90 - if (settings->confusing_lnlen && settings->regex->pattern_list->count > 0) { 1.91 - printf("\nSome files contain too long lines.\n" 1.92 - "The regex parser currently supports a maximum line length of %d." 1.93 - "\nThe result might be wrong.\n", REGEX_MAX_LINELENGTH); 1.94 - } 1.95 - 1.96 - if (!settings->verbose) { 1.97 - reopen_stdout(); 1.98 - printf("%d", lines); 1.99 - } 1.100 + destroy_string_list_t(output); 1.101 destroy_settings_t(settings); 1.102 } 1.103