Mon, 27 Jul 2020 17:19:56 +0200
adds option to compute individual sums
configure.ac | file | annotate | diff | comparison | revisions | |
src/Makefile.am | file | annotate | diff | comparison | revisions | |
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/suffix_fnc.c | file | annotate | diff | comparison | revisions | |
src/suffix_fnc.h | file | annotate | diff | comparison | revisions |
1.1 --- a/configure.ac Sat Jul 25 18:28:01 2020 +0200 1.2 +++ b/configure.ac Mon Jul 27 17:19:56 2020 +0200 1.3 @@ -22,7 +22,7 @@ 1.4 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.5 1.6 AC_PREREQ([2.69]) 1.7 -AC_INIT(cline, 1.2, universe@uap-core.de) 1.8 +AC_INIT(cline, 1.3, universe@uap-core.de) 1.9 AM_INIT_AUTOMAKE 1.10 1.11 # Conditionals
2.1 --- a/src/Makefile.am Sat Jul 25 18:28:01 2020 +0200 2.2 +++ b/src/Makefile.am Mon Jul 27 17:19:56 2020 +0200 2.3 @@ -29,7 +29,7 @@ 2.4 2.5 bin_PROGRAMS = cline 2.6 cline_SOURCES = arguments.c bfile_heuristics.c cline.c regex_parser.c \ 2.7 - scanner.c settings.c string_list.c suffix_fnc.c 2.8 + scanner.c settings.c string_list.c 2.9 2.10 EXTRA_DIST = arguments.h bfile_heuristics.h cline.h regex_parser.h scanner.h \ 2.11 - settings.h stdinc.h string_list.h suffix_fnc.h 2.12 + settings.h stdinc.h string_list.h
3.1 --- a/src/cline.c Sat Jul 25 18:28:01 2020 +0200 3.2 +++ b/src/cline.c Mon Jul 27 17:19:56 2020 +0200 3.3 @@ -44,7 +44,8 @@ 3.4 "\n -e <start> <end> - Excludes lines between <start> and <end>" 3.5 "\n You may use these options multiple times" 3.6 "\n -h, --help - this help text" 3.7 -// "\n -i - print out individual sums per file extension" 3.8 + "\n -i - print out individual sums per file extension" 3.9 + "\n (cannot be used together with -V)" 3.10 "\n -m - print information about matching files only" 3.11 "\n -s <suffixes> - only count files with these suffixes (separated" 3.12 "\n by commas)" 3.13 @@ -199,8 +200,13 @@ 3.14 add_string(settings->regex->pattern_list, argv[t]); 3.15 add_string(settings->regex->pattern_list, "$"); 3.16 } 3.17 + /* i */ 3.18 if ((argflags & 2048) > 0) { 3.19 - settings->individual_sums = true; 3.20 + // cannot be used together with -V 3.21 + if (registerArgument(&checked, 128)) { 3.22 + return exit_with_help(settings, 1); 3.23 + } 3.24 + settings->individual_sums = true; 3.25 } 3.26 if (argflags == 0) { 3.27 /* SHORTCUTS */ 3.28 @@ -226,7 +232,7 @@ 3.29 3.30 /* Scan directories */ 3.31 if (regex_compile_all(settings->regex)) { 3.32 - scanresult_t result; 3.33 + scanresult_t* result = new_scanresult_t(settings); 3.34 /* Don't waste memory when only the total sum is needed */ 3.35 string_list_t *output = settings->verbose ? new_string_list_t() : NULL; 3.36 char *outbuf; 3.37 @@ -237,8 +243,8 @@ 3.38 } 3.39 for (int t = 0 ; t < directories->count ; t++) { 3.40 scanDirectory((scanner_t){directories->items[t], 0}, settings, 3.41 - output, &result); 3.42 - total += result.directory; 3.43 + output, result); 3.44 + total += result->lines; 3.45 if (directories->count > 1 ) { 3.46 outbuf = (char*) malloc(81); 3.47 memset(outbuf, '-', 79); 3.48 @@ -247,7 +253,7 @@ 3.49 add_string(output, outbuf); 3.50 outbuf = (char*) malloc(81); 3.51 snprintf(outbuf, 81, "%-63s%10d lines\n", directories->items[t], 3.52 - result.directory); 3.53 + result->lines); 3.54 add_string(output, outbuf); 3.55 outbuf = (char*) malloc(81); 3.56 memset(outbuf, '-', 79); 3.57 @@ -265,6 +271,20 @@ 3.58 free(output->items[i]); 3.59 } 3.60 3.61 + if (result->ext) { 3.62 + if (result->ext->count > 0) { 3.63 + for (int t = 0 ; t < 79 ; t++) { 3.64 + printf("="); 3.65 + } 3.66 + printf("\nIndividual sums:\n"); 3.67 + for (int t = 0 ; t < result->ext->count ; t++) { 3.68 + printf(" %-62s%10d lines\n", 3.69 + result->ext->extensions[t], 3.70 + result->ext->lines[t]); 3.71 + } 3.72 + } 3.73 + } 3.74 + 3.75 for (int t = 0 ; t < 79 ; t++) { 3.76 printf("="); 3.77 } 3.78 @@ -280,6 +300,7 @@ 3.79 } else { 3.80 printf("%d", total); 3.81 } 3.82 + destroy_scanresult_t(result); 3.83 destroy_string_list_t(output); 3.84 destroy_settings_t(settings); 3.85 }
4.1 --- a/src/scanner.c Sat Jul 25 18:28:01 2020 +0200 4.2 +++ b/src/scanner.c Mon Jul 27 17:19:56 2020 +0200 4.3 @@ -26,7 +26,6 @@ 4.4 4.5 4.6 #include "scanner.h" 4.7 -#include "suffix_fnc.h" 4.8 #include "bfile_heuristics.h" 4.9 #include "regex_parser.h" 4.10 #include <sys/stat.h> 4.11 @@ -37,11 +36,82 @@ 4.12 char *displayname; 4.13 int displayname_len; 4.14 char *filename; 4.15 + char *ext; 4.16 int st_mode; 4.17 filelist_t *next; 4.18 }; 4.19 4.20 -filelist_t *buildFileList(scanner_t scanner, settings_t* settings, 4.21 +static bool testSuffix(char* filename, string_list_t* list) { 4.22 + bool ret = false; 4.23 + int tokenlen, fnamelen = strlen(filename); 4.24 + for (int t = 0 ; t < list->count ; t++) { 4.25 + tokenlen = strlen(list->items[t]); 4.26 + if (fnamelen >= tokenlen && tokenlen > 0) { 4.27 + if (strncmp(filename+fnamelen-tokenlen, 4.28 + list->items[t], tokenlen) == 0) { 4.29 + ret = true; 4.30 + break; 4.31 + } 4.32 + } 4.33 + } 4.34 + return ret; 4.35 +} 4.36 + 4.37 +static void addLinesPerExtension(scanresult_ext_t* result, 4.38 + char* ext, int lines) { 4.39 + if (!result) return; 4.40 + 4.41 + if (!ext) ext = "w/o"; 4.42 + 4.43 + for (int i = 0 ; i < result->count ; i++) { 4.44 + if (strcasecmp(result->extensions[i], ext) == 0) { 4.45 + result->lines[i] += lines; 4.46 + return; 4.47 + } 4.48 + } 4.49 + 4.50 + if (result->count == result->capacity) { 4.51 + int newcap = result->capacity+8; 4.52 + char** extarr = realloc(result->extensions, newcap*sizeof(char*)); 4.53 + int* linesarr = realloc(result->lines, newcap*sizeof(int)); 4.54 + if (!extarr || !linesarr) { 4.55 + fprintf(stderr, "Memory allocation error.\n"); 4.56 + abort(); 4.57 + } 4.58 + result->extensions = extarr; 4.59 + result->lines = linesarr; 4.60 + result->capacity = newcap; 4.61 + } 4.62 + 4.63 + result->extensions[result->count] = strdup(ext); 4.64 + result->lines[result->count] = lines; 4.65 + result->count++; 4.66 +} 4.67 + 4.68 +scanresult_t* new_scanresult_t(settings_t* settings) { 4.69 + scanresult_t* result = calloc(1, sizeof(scanresult_t)); 4.70 + if (settings->individual_sums) { 4.71 + result->ext = calloc(1, sizeof(scanresult_ext_t)); 4.72 + } 4.73 + return result; 4.74 +} 4.75 + 4.76 +void destroy_scanresult_t(scanresult_t* result) { 4.77 + if (result->ext) { 4.78 + if (result->ext->count > 0) { 4.79 + for (int i = 0 ; i < result->ext->count ; i++) { 4.80 + free(result->ext->extensions[i]); 4.81 + } 4.82 + free(result->ext->extensions); 4.83 + free(result->ext->lines); 4.84 + } 4.85 + free(result->ext); 4.86 + } 4.87 + free(result); 4.88 +} 4.89 + 4.90 + 4.91 +static filelist_t *buildFileList(scanner_t scanner, settings_t* settings, 4.92 filelist_t* list) { 4.93 4.94 DIR *dirf; 4.95 @@ -76,6 +146,9 @@ 4.96 memcpy(filename+dirnamelen+1, entry->d_name, newentry->displayname_len); 4.97 filename[1+dirnamelen+newentry->displayname_len] = 0; 4.98 newentry->filename = filename; 4.99 + 4.100 + /* Obtain file extension */ 4.101 + newentry->ext = strrchr(newentry->displayname, '.'); 4.102 4.103 /* Check for subdirectory */ 4.104 if (stat(filename, &statbuf) == 0) { 4.105 @@ -115,7 +188,7 @@ 4.106 void scanDirectory(scanner_t scanner, settings_t* settings, 4.107 string_list_t* output, scanresult_t* result) { 4.108 4.109 - result->directory = 0; 4.110 + result->lines = 0; 4.111 int a; 4.112 bool bfile; 4.113 char *outbuf; 4.114 @@ -129,16 +202,17 @@ 4.115 if (settings->recursive && S_ISDIR(filelist->st_mode)) { 4.116 string_list_t *recoutput = new_string_list_t(); 4.117 scanresult_t recresult; 4.118 + recresult.ext = result->ext; 4.119 scanDirectory( 4.120 (scanner_t) {filelist->filename, scanner.spaces+1}, 4.121 settings, recoutput, &recresult); 4.122 - result->directory += recresult.directory; 4.123 + result->lines += recresult.lines; 4.124 if (!settings->matchesOnly || recoutput->count > 0) { 4.125 outbuf = (char*) malloc(81); 4.126 snprintf(outbuf, 81, "%*s/%*s%13d lines\n", 4.127 filelist->displayname_len+scanner.spaces, filelist->displayname, 4.128 60-filelist->displayname_len-scanner.spaces-1, "", 4.129 - recresult.directory); 4.130 + recresult.lines); 4.131 add_string(output, outbuf); 4.132 for (int i = 0 ; i < recoutput->count ; i++) { 4.133 add_string(output, recoutput->items[i]); 4.134 @@ -211,7 +285,8 @@ 4.135 add_string(output, outbuf); 4.136 } 4.137 } else { 4.138 - result->directory += lines; 4.139 + addLinesPerExtension(result->ext, filelist->ext, lines); 4.140 + result->lines += lines; 4.141 outbuf = (char*) malloc(81); 4.142 snprintf(outbuf, 81, "%*s%*s%13d lines\n", 4.143 filelist->displayname_len+scanner.spaces, filelist->displayname,
5.1 --- a/src/scanner.h Sat Jul 25 18:28:01 2020 +0200 5.2 +++ b/src/scanner.h Mon Jul 27 17:19:56 2020 +0200 5.3 @@ -37,7 +37,15 @@ 5.4 } scanner_t; 5.5 5.6 typedef struct { 5.7 - int directory; 5.8 + int count; 5.9 + int capacity; 5.10 + char** extensions; 5.11 + int* lines; 5.12 +} scanresult_ext_t; 5.13 + 5.14 +typedef struct { 5.15 + int lines; 5.16 + scanresult_ext_t* ext; 5.17 } scanresult_t; 5.18 5.19 #ifdef _cplusplus 5.20 @@ -47,6 +55,10 @@ 5.21 void scanDirectory(scanner_t scanner, settings_t* settings, 5.22 string_list_t* output, scanresult_t* result); 5.23 5.24 +scanresult_t* new_scanresult_t(settings_t* settings); 5.25 +void destroy_scanresult_t(scanresult_t*); 5.26 + 5.27 + 5.28 #ifdef _cplusplus 5.29 } 5.30 #endif
6.1 --- a/src/suffix_fnc.c Sat Jul 25 18:28:01 2020 +0200 6.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 6.3 @@ -1,44 +0,0 @@ 6.4 -/* 6.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 6.6 - * Copyright 2018 Mike Becker. All rights reserved. 6.7 - * 6.8 - * Redistribution and use in source and binary forms, with or without 6.9 - * modification, are permitted provided that the following conditions are met: 6.10 - * 6.11 - * 1. Redistributions of source code must retain the above copyright 6.12 - * notice, this list of conditions and the following disclaimer. 6.13 - * 6.14 - * 2. Redistributions in binary form must reproduce the above copyright 6.15 - * notice, this list of conditions and the following disclaimer in the 6.16 - * documentation and/or other materials provided with the distribution. 6.17 - * 6.18 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 6.19 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 6.20 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 6.21 - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 6.22 - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 6.23 - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 6.24 - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 6.25 - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 6.26 - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 6.27 - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 6.28 - */ 6.29 - 6.30 -#include "suffix_fnc.h" 6.31 - 6.32 -bool testSuffix(char* filename, string_list_t* list) { 6.33 - bool ret = false; 6.34 - int tokenlen, fnamelen = strlen(filename); 6.35 - for (int t = 0 ; t < list->count ; t++) { 6.36 - tokenlen = strlen(list->items[t]); 6.37 - if (fnamelen >= tokenlen && tokenlen > 0) { 6.38 - if (strncmp(filename+fnamelen-tokenlen, 6.39 - list->items[t], tokenlen) == 0) { 6.40 - ret = true; 6.41 - break; 6.42 - } 6.43 - } 6.44 - } 6.45 - return ret; 6.46 -} 6.47 -
7.1 --- a/src/suffix_fnc.h Sat Jul 25 18:28:01 2020 +0200 7.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 7.3 @@ -1,36 +0,0 @@ 7.4 -/* 7.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 7.6 - * Copyright 2018 Mike Becker. All rights reserved. 7.7 - * 7.8 - * Redistribution and use in source and binary forms, with or without 7.9 - * modification, are permitted provided that the following conditions are met: 7.10 - * 7.11 - * 1. Redistributions of source code must retain the above copyright 7.12 - * notice, this list of conditions and the following disclaimer. 7.13 - * 7.14 - * 2. Redistributions in binary form must reproduce the above copyright 7.15 - * notice, this list of conditions and the following disclaimer in the 7.16 - * documentation and/or other materials provided with the distribution. 7.17 - * 7.18 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 7.19 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 7.20 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 7.21 - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 7.22 - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 7.23 - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 7.24 - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 7.25 - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 7.26 - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 7.27 - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 7.28 - * 7.29 - */ 7.30 - 7.31 -#ifndef SUFFIX_FNC_H_ 7.32 -#define SUFFIX_FNC_H_ 7.33 - 7.34 -#include "stdinc.h" 7.35 -#include "string_list.h" 7.36 - 7.37 -bool testSuffix(char*, string_list_t*); 7.38 - 7.39 -#endif /* SUFFIX_FNC_H_ */