diff -r 69be673a4fd0 -r 9c8d768f0244 src/scanner.c --- a/src/scanner.c Sat Jul 25 18:28:01 2020 +0200 +++ b/src/scanner.c Mon Jul 27 17:19:56 2020 +0200 @@ -26,7 +26,6 @@ #include "scanner.h" -#include "suffix_fnc.h" #include "bfile_heuristics.h" #include "regex_parser.h" #include @@ -37,11 +36,82 @@ char *displayname; int displayname_len; char *filename; + char *ext; int st_mode; filelist_t *next; }; -filelist_t *buildFileList(scanner_t scanner, settings_t* settings, +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++) { + tokenlen = strlen(list->items[t]); + if (fnamelen >= tokenlen && tokenlen > 0) { + if (strncmp(filename+fnamelen-tokenlen, + list->items[t], tokenlen) == 0) { + ret = true; + break; + } + } + } + return ret; +} + +static void addLinesPerExtension(scanresult_ext_t* result, + char* ext, int lines) { + if (!result) return; + + if (!ext) ext = "w/o"; + + for (int i = 0 ; i < result->count ; i++) { + if (strcasecmp(result->extensions[i], ext) == 0) { + result->lines[i] += lines; + return; + } + } + + if (result->count == result->capacity) { + int newcap = result->capacity+8; + char** extarr = realloc(result->extensions, newcap*sizeof(char*)); + int* linesarr = realloc(result->lines, newcap*sizeof(int)); + if (!extarr || !linesarr) { + fprintf(stderr, "Memory allocation error.\n"); + abort(); + } + result->extensions = extarr; + result->lines = linesarr; + result->capacity = newcap; + } + + result->extensions[result->count] = strdup(ext); + result->lines[result->count] = lines; + result->count++; +} + +scanresult_t* new_scanresult_t(settings_t* settings) { + scanresult_t* result = calloc(1, sizeof(scanresult_t)); + if (settings->individual_sums) { + result->ext = calloc(1, sizeof(scanresult_ext_t)); + } + return result; +} + +void destroy_scanresult_t(scanresult_t* result) { + if (result->ext) { + if (result->ext->count > 0) { + for (int i = 0 ; i < result->ext->count ; i++) { + free(result->ext->extensions[i]); + } + free(result->ext->extensions); + free(result->ext->lines); + } + free(result->ext); + } + free(result); +} + + +static filelist_t *buildFileList(scanner_t scanner, settings_t* settings, filelist_t* list) { DIR *dirf; @@ -76,6 +146,9 @@ memcpy(filename+dirnamelen+1, entry->d_name, newentry->displayname_len); filename[1+dirnamelen+newentry->displayname_len] = 0; newentry->filename = filename; + + /* Obtain file extension */ + newentry->ext = strrchr(newentry->displayname, '.'); /* Check for subdirectory */ if (stat(filename, &statbuf) == 0) { @@ -115,7 +188,7 @@ void scanDirectory(scanner_t scanner, settings_t* settings, string_list_t* output, scanresult_t* result) { - result->directory = 0; + result->lines = 0; int a; bool bfile; char *outbuf; @@ -129,16 +202,17 @@ if (settings->recursive && S_ISDIR(filelist->st_mode)) { string_list_t *recoutput = new_string_list_t(); scanresult_t recresult; + recresult.ext = result->ext; scanDirectory( (scanner_t) {filelist->filename, scanner.spaces+1}, settings, recoutput, &recresult); - result->directory += recresult.directory; + result->lines += recresult.lines; 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, "", - recresult.directory); + recresult.lines); add_string(output, outbuf); for (int i = 0 ; i < recoutput->count ; i++) { add_string(output, recoutput->items[i]); @@ -211,7 +285,8 @@ add_string(output, outbuf); } } else { - result->directory += lines; + addLinesPerExtension(result->ext, filelist->ext, lines); + result->lines += lines; outbuf = (char*) malloc(81); snprintf(outbuf, 81, "%*s%*s%13d lines\n", filelist->displayname_len+scanner.spaces, filelist->displayname,