src/scanner.c

changeset 66
be2084398c37
parent 61
9c8d768f0244
child 76
d11c154e33e1
--- a/src/scanner.c	Fri Jun 03 18:13:46 2022 +0200
+++ b/src/scanner.c	Fri Jun 03 20:05:15 2022 +0200
@@ -29,22 +29,23 @@
 #include "bfile_heuristics.h"
 #include "regex_parser.h"
 #include <sys/stat.h>
+#include <ctype.h>
 
 typedef struct filelist filelist_t;
 
 struct filelist {
   char *displayname;
-  int displayname_len;
+  unsigned displayname_len;
   char *filename;
   char *ext;
-  int st_mode;
+  unsigned st_mode;
   filelist_t *next;
 };
 
 static bool testSuffix(char* filename, string_list_t* list) {
   bool ret = false;
-  int tokenlen, fnamelen = strlen(filename);
-  for (int t = 0 ; t < list->count ; t++) {
+  size_t tokenlen, fnamelen = strlen(filename);
+  for (size_t t = 0 ; t < list->count ; t++) {
     tokenlen = strlen(list->items[t]);
     if (fnamelen >= tokenlen && tokenlen > 0) {
       if (strncmp(filename+fnamelen-tokenlen,
@@ -57,34 +58,34 @@
   return ret;
 }
 
-static void addLinesPerExtension(scanresult_ext_t* result,
-        char* ext, int lines) {
+static void addResultPerExtension(scanresult_ext_t* result,
+                                  char* ext, unsigned value) {
   if (!result) return;
   
   if (!ext) ext = "w/o";
   
-  for (int i = 0 ; i < result->count ; i++) {
+  for (unsigned i = 0 ; i < result->count ; i++) {
     if (strcasecmp(result->extensions[i], ext) == 0) {
-      result->lines[i] += lines;
+      result->result[i] += value;
       return;
     }
   }
   
   if (result->count == result->capacity) {
-    int newcap = result->capacity+8;
+    unsigned newcap = result->capacity+8;
     char** extarr = realloc(result->extensions, newcap*sizeof(char*));
-    int* linesarr = realloc(result->lines, newcap*sizeof(int));
-    if (!extarr || !linesarr) {
+    unsigned* resultarr = realloc(result->result, newcap*sizeof(unsigned));
+    if (!extarr || !resultarr) {
       fprintf(stderr, "Memory allocation error.\n");
       abort();
     }
     result->extensions = extarr;
-    result->lines = linesarr;
+    result->result = resultarr;
     result->capacity = newcap;
   }
   
   result->extensions[result->count] = strdup(ext);
-  result->lines[result->count] = lines;
+  result->result[result->count] = value;
   result->count++;
 }
 
@@ -99,21 +100,20 @@
 void destroy_scanresult_t(scanresult_t* result) {
   if (result->ext) {
     if (result->ext->count > 0) {
-      for (int i = 0 ; i < result->ext->count ; i++) {
+      for (unsigned i = 0 ; i < result->ext->count ; i++) {
         free(result->ext->extensions[i]);
       }
       free(result->ext->extensions);
-      free(result->ext->lines);
+      free(result->ext->result);
     }
     free(result->ext);
   }
   free(result);
 }
 
+static filelist_t *buildFileList(scanner_t scanner, settings_t* settings) {
 
-static filelist_t *buildFileList(scanner_t scanner, settings_t* settings,
-    filelist_t* list) {
-  
+  filelist_t* list = NULL;
   DIR *dirf;
   struct dirent *entry;
   struct stat statbuf;
@@ -159,7 +159,7 @@
       }
       
       if (list) {
-        // create fake root to have a pointer on the true root
+        /* create fake root to have a pointer on the true root */
         filelist_t root;
         root.next = list;
         filelist_t *parent = &root;
@@ -188,12 +188,12 @@
 void scanDirectory(scanner_t scanner, settings_t* settings,
     string_list_t* output, scanresult_t* result) {
 
-  result->lines = 0;
-  int a;
+  result->result = 0;
   bool bfile;
   char *outbuf;
+  const char *result_type = settings->count_chars ? "chars" : "lines";
 
-  filelist_t *filelist = buildFileList(scanner, settings, NULL);
+  filelist_t *filelist = buildFileList(scanner, settings);
 
   while (filelist != NULL) {
 
@@ -206,23 +206,24 @@
         scanDirectory(
             (scanner_t) {filelist->filename, scanner.spaces+1},
             settings, recoutput, &recresult);
-        result->lines += recresult.lines;
+        result->result += recresult.result;
         if (!settings->matchesOnly || recoutput->count > 0) {
           outbuf = (char*) malloc(81);
-          snprintf(outbuf, 81, "%*s/%*s%13d lines\n",
+          snprintf(outbuf, 81, "%*s/%*s%13u %s\n",
               filelist->displayname_len+scanner.spaces, filelist->displayname,
               60-filelist->displayname_len-scanner.spaces-1, "",
-              recresult.lines);
+              recresult.result, result_type);
           add_string(output, outbuf);
-          for (int i = 0 ; i < recoutput->count ; i++) {
+          for (unsigned i = 0 ; i < recoutput->count ; i++) {
             add_string(output, recoutput->items[i]);
           }
         }
         destroy_string_list_t(recoutput);
       } else {
         outbuf = (char*) malloc(81);
-        snprintf(outbuf, 81, "%*s\n", filelist->displayname_len+scanner.spaces,
-          filelist->displayname);
+        snprintf(outbuf, 81, "%*s\n",
+                 filelist->displayname_len+scanner.spaces,
+                 filelist->displayname);
         add_string(output, outbuf);
       }
     } else {
@@ -230,44 +231,55 @@
         || testSuffix(filelist->displayname, settings->includeSuffixes))
         && !testSuffix(filelist->displayname, settings->excludeSuffixes)) {
 
-        /* Count lines */
-        int lines = 0;
+        /* Count */
+        unsigned res_value = 0;
         bfile = false;
         bfile_reset(settings->bfileHeuristics);
         regex_parser_reset(settings->regex);
-        char line_buffer[REGEX_MAX_LINELENGTH];
-        int line_buffer_offset = 0;
+        char line_buffer[MAX_LINELENGTH];
+        unsigned line_buffer_pos = 0;
 
         FILE *file = fopen(filelist->filename, "r");
         if (file == NULL) {
           outbuf = (char*) malloc(81);
-          snprintf(outbuf, 81, "%*s", filelist->displayname_len+scanner.spaces,
-              filelist->displayname);
+          snprintf(outbuf, 81, "%*s",
+                   filelist->displayname_len+scanner.spaces,
+                   filelist->displayname);
           add_string(output, outbuf);
           perror("  File acces failed");
         } else {
+          int a;
           do {
             a = fgetc(file);
 
             bfile = bfile_check(settings->bfileHeuristics, a);
 
+            /* ignore carriage return completely */
+            if (a == 13) continue;
+
             if (a == 10 || a == EOF) {
-              line_buffer[line_buffer_offset] = 0;
+              line_buffer[line_buffer_pos] = 0;
               if (regex_parser_do(settings->regex, line_buffer) == 0) {
-                /* Only subtract lines when matching has finished */
+                /* Subtract excluded lines/chars when matching has finished */
                 if (!regex_parser_matching(settings->regex)) {
-                  lines -= settings->regex->matched_lines;
+                  res_value -= settings->regex->matched_counted;
                 }
               }
 
-              line_buffer_offset = 0;
-              lines++;
+              if (settings->count_chars) {
+                for (size_t i = 0 ; i < line_buffer_pos ; i++) {
+                  if (!isspace(line_buffer[i])) res_value++;
+                }
+              } else {
+                res_value++;
+              }
+              line_buffer_pos = 0;
             } else {
-              if (line_buffer_offset < REGEX_MAX_LINELENGTH) {
-                line_buffer[line_buffer_offset] = a;
-                line_buffer_offset++;
+              if (line_buffer_pos < MAX_LINELENGTH) {
+                line_buffer[line_buffer_pos] = (char) a;
+                line_buffer_pos++;
               } else {
-                line_buffer[line_buffer_offset-1] = 0;
+                line_buffer[line_buffer_pos - 1] = 0;
                 settings->confusing_lnlen = true;
               }
             }
@@ -285,12 +297,17 @@
               add_string(output, outbuf);
             }
           } else {
-            addLinesPerExtension(result->ext, filelist->ext, lines);
-            result->lines += lines;
+            addResultPerExtension(result->ext, filelist->ext, res_value);
+            result->result += res_value;
             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, "", lines);
+            snprintf(outbuf, 81, "%*s%*s%13u %s\n",
+                     filelist->displayname_len+scanner.spaces,
+                     filelist->displayname,
+                     60-filelist->displayname_len-scanner.spaces,
+                     "",
+                     res_value,
+                     result_type
+            );
             add_string(output, outbuf);
           }
         }

mercurial