preparing changes for individual sum feature

Sat, 25 Jul 2020 18:28:01 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 25 Jul 2020 18:28:01 +0200
changeset 60
69be673a4fd0
parent 59
c5409c8be36f
child 61
9c8d768f0244

preparing changes for individual sum feature

src/cline.c file | annotate | diff | comparison | revisions
src/scanner.c file | annotate | diff | comparison | revisions
src/scanner.h file | annotate | diff | comparison | revisions
src/settings.c file | annotate | diff | comparison | revisions
src/settings.h file | annotate | diff | comparison | revisions
--- a/src/cline.c	Sat Jul 25 17:53:54 2020 +0200
+++ b/src/cline.c	Sat Jul 25 18:28:01 2020 +0200
@@ -44,6 +44,7 @@
     "\n  -e <start> <end>    - Excludes lines between <start> and <end>"
     "\n                        You may use these options multiple times"
     "\n  -h, --help          - this help text"
+//  "\n  -i                  - print out individual sums per file extension"
     "\n  -m                  - print information about matching files only"
     "\n  -s <suffixes>       - only count files with these suffixes (separated"
     "\n                        by commas)"
@@ -106,7 +107,7 @@
 
   for (int t = 1 ; t < argc ; t++) {
 
-    int argflags = checkArgument(argv[t], "hsSrRmvVbeE");
+    int argflags = checkArgument(argv[t], "hsSrRmvVbeEi");
     int paropt = 0;
 
     /* h */
@@ -198,6 +199,9 @@
       add_string(settings->regex->pattern_list, argv[t]);
       add_string(settings->regex->pattern_list, "$");
     }
+    if ((argflags & 2048) > 0) {
+        settings->individual_sums = true;
+    }
     if (argflags == 0) {
       /* SHORTCUTS */
       if (strcmp(argv[t], "--exclude-cstyle-comments") == 0) {
@@ -222,18 +226,19 @@
 
   /* Scan directories */
   if (regex_compile_all(settings->regex)) {
+    scanresult_t result;
     /* Don't waste memory when only the total sum is needed */
     string_list_t *output = settings->verbose ? new_string_list_t() : NULL;
     char *outbuf;
     
-    int lineSum = 0, lines;
+    int total = 0;
     if (directories->count == 0) {
         add_string(directories, ".");
     }
     for (int t = 0 ; t < directories->count ; t++) {
-      lines = scanDirectory((scanner_t){directories->items[t], 0}, settings,
-          output);
-      lineSum += lines;
+      scanDirectory((scanner_t){directories->items[t], 0}, settings,
+          output, &result);
+      total += result.directory;
       if (directories->count > 1 ) {
         outbuf = (char*) malloc(81);
         memset(outbuf, '-', 79);
@@ -241,7 +246,8 @@
         outbuf[80] = 0;
         add_string(output, outbuf);
         outbuf = (char*) malloc(81);
-        snprintf(outbuf, 81, "%-63s%10d lines\n", directories->items[t], lines);
+        snprintf(outbuf, 81, "%-63s%10d lines\n", directories->items[t],
+                result.directory);
         add_string(output, outbuf);
         outbuf = (char*) malloc(81);
         memset(outbuf, '-', 79);
@@ -262,7 +268,7 @@
       for (int t = 0 ; t < 79 ; t++) {
         printf("=");
       }
-      printf("\n%73d lines\n", lineSum);
+      printf("\n%73d lines\n", total);
 
       if (settings->confusing_lnlen &&
           settings->regex->pattern_list->count > 0) {
@@ -272,7 +278,7 @@
           "\nThe result might be wrong.\n", REGEX_MAX_LINELENGTH);
       }
     } else {
-      printf("%d", lineSum);
+      printf("%d", total);
     }
     destroy_string_list_t(output);
     destroy_settings_t(settings);
--- a/src/scanner.c	Sat Jul 25 17:53:54 2020 +0200
+++ b/src/scanner.c	Sat Jul 25 18:28:01 2020 +0200
@@ -112,11 +112,11 @@
   return list;
 }
 
-int scanDirectory(scanner_t scanner, settings_t* settings,
-    string_list_t* output) {
+void scanDirectory(scanner_t scanner, settings_t* settings,
+    string_list_t* output, scanresult_t* result) {
 
-  int lines, a;
-  int lineSum = 0;
+  result->directory = 0;
+  int a;
   bool bfile;
   char *outbuf;
 
@@ -128,15 +128,17 @@
     if (!S_ISREG(filelist->st_mode)) {
       if (settings->recursive && S_ISDIR(filelist->st_mode)) {
         string_list_t *recoutput = new_string_list_t();
-        lines = scanDirectory(
+        scanresult_t recresult;
+        scanDirectory(
             (scanner_t) {filelist->filename, scanner.spaces+1},
-            settings, recoutput);
-        lineSum += lines;
+            settings, recoutput, &recresult);
+        result->directory += recresult.directory;
         if (!settings->matchesOnly || recoutput->count > 0) {
           outbuf = (char*) malloc(81);
           snprintf(outbuf, 81, "%*s/%*s%13d lines\n",
               filelist->displayname_len+scanner.spaces, filelist->displayname,
-              60-filelist->displayname_len-scanner.spaces-1, "", lines);
+              60-filelist->displayname_len-scanner.spaces-1, "",
+              recresult.directory);
           add_string(output, outbuf);
           for (int i = 0 ; i < recoutput->count ; i++) {
             add_string(output, recoutput->items[i]);
@@ -155,7 +157,7 @@
         && !testSuffix(filelist->displayname, settings->excludeSuffixes)) {
 
         /* Count lines */
-        lines = 0;
+        int lines = 0;
         bfile = false;
         bfile_reset(settings->bfileHeuristics);
         regex_parser_reset(settings->regex);
@@ -209,7 +211,7 @@
               add_string(output, outbuf);
             }
           } else {
-            lineSum += lines;
+            result->directory += lines;
             outbuf = (char*) malloc(81);
             snprintf(outbuf, 81, "%*s%*s%13d lines\n",
                 filelist->displayname_len+scanner.spaces, filelist->displayname,
@@ -235,6 +237,4 @@
     filelist = filelist->next;
     free(freethis);
   }
-
-  return lineSum;
 }
--- a/src/scanner.h	Sat Jul 25 17:53:54 2020 +0200
+++ b/src/scanner.h	Sat Jul 25 18:28:01 2020 +0200
@@ -36,12 +36,16 @@
   int spaces;
 } scanner_t;
 
+typedef struct {
+  int directory;
+} scanresult_t;
+
 #ifdef _cplusplus
 extern "C" {
 #endif
 
-int scanDirectory(scanner_t scanner, settings_t* settings,
-        string_list_t* output);
+void scanDirectory(scanner_t scanner, settings_t* settings,
+        string_list_t* output, scanresult_t* result);
 
 #ifdef _cplusplus
 }
--- a/src/settings.c	Sat Jul 25 17:53:54 2020 +0200
+++ b/src/settings.c	Sat Jul 25 18:28:01 2020 +0200
@@ -36,12 +36,13 @@
   #endif /* _WIN32 */
     settings->recursive          = false;
     settings->matchesOnly        = false;
-    settings->includeSuffixes         = new_string_list_t();
+    settings->includeSuffixes    = new_string_list_t();
     settings->excludeSuffixes    = new_string_list_t();
     settings->verbose            = true;
     settings->bfileHeuristics    = new_bfile_heuristics_t();
     settings->confusing_lnlen    = false;
     settings->regex              = new_regex_parser_t();
+    settings->individual_sums           = false;
   }
 
   return settings;
--- a/src/settings.h	Sat Jul 25 17:53:54 2020 +0200
+++ b/src/settings.h	Sat Jul 25 18:28:01 2020 +0200
@@ -42,6 +42,7 @@
   bool matchesOnly;
   bool verbose;
   bool confusing_lnlen; /* this flag is set by the scanner */
+  bool individual_sums;
 } settings_t;
 
 #ifdef _cplusplus

mercurial