src/scanner.c

changeset 61
9c8d768f0244
parent 60
69be673a4fd0
child 66
be2084398c37
     1.1 --- a/src/scanner.c	Sat Jul 25 18:28:01 2020 +0200
     1.2 +++ b/src/scanner.c	Mon Jul 27 17:19:56 2020 +0200
     1.3 @@ -26,7 +26,6 @@
     1.4  
     1.5  
     1.6  #include "scanner.h"
     1.7 -#include "suffix_fnc.h"
     1.8  #include "bfile_heuristics.h"
     1.9  #include "regex_parser.h"
    1.10  #include <sys/stat.h>
    1.11 @@ -37,11 +36,82 @@
    1.12    char *displayname;
    1.13    int displayname_len;
    1.14    char *filename;
    1.15 +  char *ext;
    1.16    int st_mode;
    1.17    filelist_t *next;
    1.18  };
    1.19  
    1.20 -filelist_t *buildFileList(scanner_t scanner, settings_t* settings,
    1.21 +static bool testSuffix(char* filename, string_list_t* list) {
    1.22 +  bool ret = false;
    1.23 +  int tokenlen, fnamelen = strlen(filename);
    1.24 +  for (int t = 0 ; t < list->count ; t++) {
    1.25 +    tokenlen = strlen(list->items[t]);
    1.26 +    if (fnamelen >= tokenlen && tokenlen > 0) {
    1.27 +      if (strncmp(filename+fnamelen-tokenlen,
    1.28 +                  list->items[t], tokenlen) == 0) {
    1.29 +        ret = true;
    1.30 +        break;
    1.31 +      }
    1.32 +    }
    1.33 +  }
    1.34 +  return ret;
    1.35 +}
    1.36 +
    1.37 +static void addLinesPerExtension(scanresult_ext_t* result,
    1.38 +        char* ext, int lines) {
    1.39 +  if (!result) return;
    1.40 +  
    1.41 +  if (!ext) ext = "w/o";
    1.42 +  
    1.43 +  for (int i = 0 ; i < result->count ; i++) {
    1.44 +    if (strcasecmp(result->extensions[i], ext) == 0) {
    1.45 +      result->lines[i] += lines;
    1.46 +      return;
    1.47 +    }
    1.48 +  }
    1.49 +  
    1.50 +  if (result->count == result->capacity) {
    1.51 +    int newcap = result->capacity+8;
    1.52 +    char** extarr = realloc(result->extensions, newcap*sizeof(char*));
    1.53 +    int* linesarr = realloc(result->lines, newcap*sizeof(int));
    1.54 +    if (!extarr || !linesarr) {
    1.55 +      fprintf(stderr, "Memory allocation error.\n");
    1.56 +      abort();
    1.57 +    }
    1.58 +    result->extensions = extarr;
    1.59 +    result->lines = linesarr;
    1.60 +    result->capacity = newcap;
    1.61 +  }
    1.62 +  
    1.63 +  result->extensions[result->count] = strdup(ext);
    1.64 +  result->lines[result->count] = lines;
    1.65 +  result->count++;
    1.66 +}
    1.67 +
    1.68 +scanresult_t* new_scanresult_t(settings_t* settings) {
    1.69 +  scanresult_t* result = calloc(1, sizeof(scanresult_t));
    1.70 +  if (settings->individual_sums) {
    1.71 +    result->ext = calloc(1, sizeof(scanresult_ext_t));
    1.72 +  }
    1.73 +  return result;
    1.74 +}
    1.75 +
    1.76 +void destroy_scanresult_t(scanresult_t* result) {
    1.77 +  if (result->ext) {
    1.78 +    if (result->ext->count > 0) {
    1.79 +      for (int i = 0 ; i < result->ext->count ; i++) {
    1.80 +        free(result->ext->extensions[i]);
    1.81 +      }
    1.82 +      free(result->ext->extensions);
    1.83 +      free(result->ext->lines);
    1.84 +    }
    1.85 +    free(result->ext);
    1.86 +  }
    1.87 +  free(result);
    1.88 +}
    1.89 +
    1.90 +
    1.91 +static filelist_t *buildFileList(scanner_t scanner, settings_t* settings,
    1.92      filelist_t* list) {
    1.93    
    1.94    DIR *dirf;
    1.95 @@ -76,6 +146,9 @@
    1.96        memcpy(filename+dirnamelen+1, entry->d_name, newentry->displayname_len);
    1.97        filename[1+dirnamelen+newentry->displayname_len] = 0;
    1.98        newentry->filename = filename;
    1.99 +      
   1.100 +      /* Obtain file extension */
   1.101 +      newentry->ext = strrchr(newentry->displayname, '.');
   1.102  
   1.103        /* Check for subdirectory */
   1.104        if (stat(filename, &statbuf) == 0) {
   1.105 @@ -115,7 +188,7 @@
   1.106  void scanDirectory(scanner_t scanner, settings_t* settings,
   1.107      string_list_t* output, scanresult_t* result) {
   1.108  
   1.109 -  result->directory = 0;
   1.110 +  result->lines = 0;
   1.111    int a;
   1.112    bool bfile;
   1.113    char *outbuf;
   1.114 @@ -129,16 +202,17 @@
   1.115        if (settings->recursive && S_ISDIR(filelist->st_mode)) {
   1.116          string_list_t *recoutput = new_string_list_t();
   1.117          scanresult_t recresult;
   1.118 +        recresult.ext = result->ext;
   1.119          scanDirectory(
   1.120              (scanner_t) {filelist->filename, scanner.spaces+1},
   1.121              settings, recoutput, &recresult);
   1.122 -        result->directory += recresult.directory;
   1.123 +        result->lines += recresult.lines;
   1.124          if (!settings->matchesOnly || recoutput->count > 0) {
   1.125            outbuf = (char*) malloc(81);
   1.126            snprintf(outbuf, 81, "%*s/%*s%13d lines\n",
   1.127                filelist->displayname_len+scanner.spaces, filelist->displayname,
   1.128                60-filelist->displayname_len-scanner.spaces-1, "",
   1.129 -              recresult.directory);
   1.130 +              recresult.lines);
   1.131            add_string(output, outbuf);
   1.132            for (int i = 0 ; i < recoutput->count ; i++) {
   1.133              add_string(output, recoutput->items[i]);
   1.134 @@ -211,7 +285,8 @@
   1.135                add_string(output, outbuf);
   1.136              }
   1.137            } else {
   1.138 -            result->directory += lines;
   1.139 +            addLinesPerExtension(result->ext, filelist->ext, lines);
   1.140 +            result->lines += lines;
   1.141              outbuf = (char*) malloc(81);
   1.142              snprintf(outbuf, 81, "%*s%*s%13d lines\n",
   1.143                  filelist->displayname_len+scanner.spaces, filelist->displayname,

mercurial