src/cline.c

changeset 66
be2084398c37
parent 62
7f5f9f43d0c0
--- a/src/cline.c	Fri Jun 03 18:13:46 2022 +0200
+++ b/src/cline.c	Fri Jun 03 20:05:15 2022 +0200
@@ -39,6 +39,7 @@
     "\n\nOptions:"
     "\n  -b <level>          - binary file heuristics level (default medium)"
     "\n                        One of: ignore low medium high"
+    "\n  -c                  - Count non-whitespace characters instead of lines"
     "\n  -E <pattern>        - Excludes any line matching the <pattern>"
     "\n  -e <start> <end>    - Excludes lines between <start> and <end>"
     "\n                        You may use these options multiple times"
@@ -63,11 +64,11 @@
     "\nsource code in your working directory and its subdirectories, type:"
     "\n  cline -rs .c\n"
     "\nIf you want to exclude comment lines, you may use the -e/-E option."
-    "\nAfter a line matches the regex pattern <start> any following line is"
-    "\nnot counted unless a line matches the <end> pattern. A line is still "
-    "\ncounted when it does not start or end with the respective patterns."
-    "\nPlease note, that cline does not remove whitespace characters as this"
-    "\nmight not be reasonable in some cases."
+    "\nAfter a line matches the regex pattern <start>, this and any following"
+    "\nline is not counted unless a line matches the <end> pattern. A line is"
+    "\nstill counted when it does not start or end with the respective pattern."
+    "\nPlease note, that cline does not trim the lines before matching against"
+    "\nthe pattern."
     "\n\nExample (C without comments):"
     "\n  cline -s .c,.h --exclude-cstyle-comments"
     "\n");
@@ -107,7 +108,7 @@
 
   for (int t = 1 ; t < argc ; t++) {
 
-    int argflags = checkArgument(argv[t], "hsSrRmvVbeEi");
+    int argflags = checkArgument(argv[t], "hsSrRmvVbeEic");
     int paropt = 0;
 
     /* h */
@@ -201,12 +202,19 @@
     }
     /* i */
     if ((argflags & 2048) > 0) {
-      // cannot be used together with -V
+      /* cannot be used together with -V */
       if (registerArgument(&checked, 128)) {
         return exit_with_help(settings, 1);
       }
       settings->individual_sums = true;
     }
+    if ((argflags & 4096) > 0) {
+        if (registerArgument(&checked, 4096)) {
+            return exit_with_help(settings, 1);
+        }
+        settings->count_chars = true;
+        settings->regex->count_chars = true;
+    }
     if (argflags == 0) {
       /* SHORTCUTS */
       if (strcmp(argv[t], "--exclude-cstyle-comments") == 0) {
@@ -235,15 +243,16 @@
     /* Don't waste memory when only the total sum is needed */
     string_list_t *output = settings->verbose ? new_string_list_t() : NULL;
     char *outbuf;
+    const char* result_type = settings->count_chars ? "chars" : "lines";
     
-    int total = 0;
+    unsigned total = 0;
     if (directories->count == 0) {
         add_string(directories, ".");
     }
-    for (int t = 0 ; t < directories->count ; t++) {
+    for (unsigned t = 0 ; t < directories->count ; t++) {
       scanDirectory((scanner_t){directories->items[t], 0}, settings,
           output, result);
-      total += result->lines;
+      total += result->result;
       if (directories->count > 1 ) {
         outbuf = (char*) malloc(81);
         memset(outbuf, '-', 79);
@@ -251,8 +260,8 @@
         outbuf[80] = 0;
         add_string(output, outbuf);
         outbuf = (char*) malloc(81);
-        snprintf(outbuf, 81, "%-63s%10d lines\n", directories->items[t],
-                result->lines);
+        snprintf(outbuf, 81, "%-63s%10u %s\n", directories->items[t],
+                result->result, result_type);
         add_string(output, outbuf);
         outbuf = (char*) malloc(81);
         memset(outbuf, '-', 79);
@@ -272,32 +281,33 @@
       
       if (result->ext) {
         if (result->ext->count > 0) {
-          for (int t = 0 ; t < 79 ; t++) {
+          for (unsigned t = 0 ; t < 79 ; t++) {
             printf("=");
           }
           printf("\nIndividual sums:\n");
-          for (int t = 0 ; t < result->ext->count ; t++) {
-            printf(" %-62s%10d lines\n",
-                    result->ext->extensions[t],
-                    result->ext->lines[t]);
+          for (unsigned t = 0 ; t < result->ext->count ; t++) {
+            printf(" %-62s%10u %s\n",
+                   result->ext->extensions[t],
+                   result->ext->result[t],
+                   result_type);
           }
         }
       }
       
-      for (int t = 0 ; t < 79 ; t++) {
+      for (unsigned t = 0 ; t < 79 ; t++) {
         printf("=");
       }
-      printf("\n%73d lines\n", total);
+      printf("\n%73d %s\n", total, result_type);
 
       if (settings->confusing_lnlen &&
           settings->regex->pattern_list->count > 0) {
 
         printf("\nSome files contain too long lines.\n"
-          "The regex parser currently supports a maximum line length of %d."
-          "\nThe result might be wrong.\n", REGEX_MAX_LINELENGTH);
+          "The parser currently supports a maximum line length of %u."
+          "\nThe result might be wrong.\n", MAX_LINELENGTH);
       }
     } else {
-      printf("%d", total);
+      printf("%u", total);
     }
     destroy_scanresult_t(result);
     destroy_string_list_t(output);

mercurial