improve code and increase const-correctness

Sat, 04 Jul 2026 12:09:37 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 04 Jul 2026 12:09:37 +0200
changeset 102
665b60727a89
parent 101
0cb645809b1a
child 103
31fa205db85a

improve code and increase const-correctness

relates to #917

src/arguments.c file | annotate | diff | comparison | revisions
src/arguments.h file | annotate | diff | comparison | revisions
src/cline.c file | annotate | diff | comparison | revisions
src/regex_parser.c file | annotate | diff | comparison | revisions
src/regex_parser.h file | annotate | diff | comparison | revisions
src/scanner.c file | annotate | diff | comparison | revisions
src/scanner.h file | annotate | diff | comparison | revisions
src/settings.c file | annotate | diff | comparison | revisions
src/settings.h file | annotate | diff | comparison | revisions
src/string_list.c file | annotate | diff | comparison | revisions
src/string_list.h file | annotate | diff | comparison | revisions
--- a/src/arguments.c	Sat Jul 04 11:10:51 2026 +0200
+++ b/src/arguments.c	Sat Jul 04 12:09:37 2026 +0200
@@ -26,7 +26,7 @@
 
 #include "arguments.h"
 
-int checkArgument(const char* arg, const char* expected) {
+int checkArgument(const char *arg, const char *expected) {
   size_t len = strlen(expected);
   int ret = 0;
 
@@ -41,24 +41,26 @@
   return ret;
 }
 
-bool registerArgument(int* reg, int mask) {
+bool registerArgument(int *reg, int mask) {
   bool ret = (*reg & mask) > 0;
   *reg |= mask;
   return ret;
 }
 
-bool checkParamOpt(int* paropt) {
+bool checkParamOpt(int *paropt) {
   bool ret = *paropt == 0;
   *paropt = 1;
   return ret;
 }
 
-void parseCSL(char* csl, string_list* list) {
+void parseCSL(const char *csl, string_list *list) {
   if (csl != NULL) {
-    char* finder = strtok(csl, ",");
+    char *buf = strdup(csl);
+    char *finder = strtok(buf, ",");
     while (finder != NULL) {
       add_string(list, finder);
       finder = strtok(NULL, ",");
     }
+    free(buf);
   }
 }
--- a/src/arguments.h	Sat Jul 04 11:10:51 2026 +0200
+++ b/src/arguments.h	Sat Jul 04 12:09:37 2026 +0200
@@ -37,7 +37,7 @@
 int checkArgument(const char*, const char*);
 bool checkParamOpt(int*);
 bool registerArgument(int*, int);
-void parseCSL(char*, string_list*);
+void parseCSL(const char*, string_list*);
 
 #ifdef _cplusplus
 }
--- a/src/cline.c	Sat Jul 04 11:10:51 2026 +0200
+++ b/src/cline.c	Sat Jul 04 12:09:37 2026 +0200
@@ -81,13 +81,13 @@
     "\n");
 }
 
-static int exit_with_version(settings* settings) {
+static int exit_with_version(settings *settings) {
   printf("cline - Version: " VERSION "\n");
   destroy_settings(settings);
   return 0;
 }
 
-static int exit_with_help(settings* settings, int code) {
+static int exit_with_help(settings *settings, int code) {
   printf("cline - Version: " VERSION "\n");
   printHelpText();
   destroy_settings(settings);
@@ -121,10 +121,10 @@
   settings->excludeDirs->free_item = free;
 }
 
-static const char * sepline_double = "===============================================================================\n";
-static const char * sepline_single = "-------------------------------------------------------------------------------\n";
+static const char *sepline_double = "===============================================================================\n";
+static const char *sepline_single = "-------------------------------------------------------------------------------\n";
 
-int main(int argc, char** argv) {
+int main(int argc, char **argv) {
 
   /* Settings */
   settings *settings = new_settings();
@@ -139,8 +139,8 @@
     fprintf(stderr, "Memory allocation failed.\n");
     return 1;
   }
-  char* includeSuffix = NULL;
-  char* excludeSuffix = NULL;
+  const char *includeSuffix = NULL;
+  const char *excludeSuffix = NULL;
   int checked = 0;
 
   for (int t = 1 ; t < argc ; t++) {
@@ -309,8 +309,8 @@
   normalize_excluded_dirs(settings);
 
   /* Scan directories */
-  scanresult* result = new_scanresult(settings);
-  const char* result_type = settings->count_chars ? "chars" : "lines";
+  scanresult *result = new_scanresult(settings);
+  const char *result_type = settings->count_chars ? "chars" : "lines";
   bool has_output = false;
   unsigned total = 0;
   if (directories->count == 0) {
--- a/src/regex_parser.c	Sat Jul 04 11:10:51 2026 +0200
+++ b/src/regex_parser.c	Sat Jul 04 12:09:37 2026 +0200
@@ -27,8 +27,8 @@
 #include "regex_parser.h"
 #include <ctype.h>
 
-regex_parser* new_regex_parser() {
-  regex_parser* ret = malloc(sizeof(regex_parser));
+regex_parser *new_regex_parser() {
+  regex_parser *ret = malloc(sizeof(regex_parser));
   if (ret != NULL) {
     ret->pattern_list = new_string_list();
     ret->matched_counted = 0;
@@ -40,11 +40,11 @@
   return ret;
 }
 
-void regex_parser_reset(regex_parser* parser) {
+void regex_parser_reset(regex_parser *parser) {
   parser->pattern_match = parser->matched_counted = 0;
 }
 
-void regex_destcomppats(regex_parser* parser) {
+void regex_destcomppats(regex_parser *parser) {
   if (parser->compiled_patterns != NULL) {
     for (unsigned i = 0 ; i < parser->compiled_pattern_count ; i++) {
         regfree(parser->compiled_patterns + i);
@@ -55,17 +55,17 @@
   }
 }
 
-void destroy_regex_parser(regex_parser* parser) {
+void destroy_regex_parser(regex_parser *parser) {
   regex_destcomppats(parser);
   destroy_string_list(parser->pattern_list);
   free(parser);
 }
 
-bool regex_parser_matching(regex_parser* parser) {
+bool regex_parser_matching(regex_parser *parser) {
   return parser->pattern_match > 0;
 }
 
-static unsigned regex_parser_count_chars(const char* input,
+static unsigned regex_parser_count_chars(const char *input,
                                          unsigned start, unsigned end) {
   unsigned ret = 0;
   for (unsigned i = start ; i < end ; i++) {
@@ -74,7 +74,7 @@
   return ret;
 }
 
-int regex_parser_do(regex_parser* parser, char* input) {
+int regex_parser_do(regex_parser *parser, const char *input) {
   int err = REG_NOMATCH;
   if (parser->compiled_pattern_count > 0) {
     regmatch_t match;
@@ -139,7 +139,7 @@
   return err;
 }
 
-bool regex_compile_all(regex_parser* parser) {
+bool regex_compile_all(regex_parser *parser) {
   size_t pcount = parser->pattern_list->count;
   if (pcount > 0) {
     regex_destcomppats(parser);
--- a/src/regex_parser.h	Sat Jul 04 11:10:51 2026 +0200
+++ b/src/regex_parser.h	Sat Jul 04 12:09:37 2026 +0200
@@ -40,8 +40,8 @@
 #endif
 
 typedef struct {
-  string_list* pattern_list; /* even entries: start ; odd entries: end */
-  regex_t* compiled_patterns;
+  string_list *pattern_list; /* even entries: start ; odd entries: end */
+  regex_t *compiled_patterns;
   size_t compiled_pattern_count;
   unsigned int pattern_match; /* save position of end pattern to match -
                                  NULL when a start pattern shall match first */
@@ -53,13 +53,13 @@
 extern "C" {
 #endif
 
-regex_parser* new_regex_parser();
+regex_parser *new_regex_parser();
 void destroy_regex_parser(regex_parser*);
-void regex_parser_reset(regex_parser* parser);
+void regex_parser_reset(regex_parser *parser);
 
 bool regex_parser_matching(regex_parser*);
 bool regex_compile_all(regex_parser*);
-int regex_parser_do(regex_parser*, char*);
+int regex_parser_do(regex_parser*, const char*);
 
 #ifdef _cplusplus
 }
--- a/src/scanner.c	Sat Jul 04 11:10:51 2026 +0200
+++ b/src/scanner.c	Sat Jul 04 12:09:37 2026 +0200
@@ -35,7 +35,7 @@
 #include <windows.h>
 #include <shlwapi.h>
 
-void get_working_dir(const char** out_ptr, size_t *out_len) {
+void get_working_dir(const char **out_ptr, size_t *out_len) {
   static char cwd[MAX_PATH];
   if (GetCurrentDirectoryA(MAX_PATH, cwd) == 0) {
     fprintf(stderr, "Could not get current working directory.\n");
@@ -57,7 +57,7 @@
 }
 #else
 #include <unistd.h>
-void get_working_dir(const char** out_ptr, size_t *out_len) {
+void get_working_dir(const char **out_ptr, size_t *out_len) {
   static char cwd[PATH_MAX];
   if (getcwd(cwd, sizeof(cwd)) == NULL) {
     fprintf(stderr, "Could not get current working directory.\n");
@@ -83,12 +83,12 @@
   char *displayname;
   unsigned displayname_len;
   char *filename;
-  char *ext;
+  const char *ext;
   unsigned st_mode;
   struct filelist *next;
 };
 
-static bool testSuffix(char* filename, string_list* list) {
+static bool testSuffix(const char *filename, string_list *list) {
   bool ret = false;
   size_t tokenlen, fnamelen = strlen(filename);
   for (size_t t = 0 ; t < list->count ; t++) {
@@ -104,8 +104,8 @@
   return ret;
 }
 
-static void addResultPerExtension(scanresult_ext* result,
-                                  char* ext, unsigned value) {
+static void addResultPerExtension(scanresult_ext *result,
+                                  const char *ext, unsigned value) {
   if (!result) return;
   
   if (!ext) ext = "w/o";
@@ -119,8 +119,8 @@
   
   if (result->count == result->capacity) {
     unsigned newcap = result->capacity+8;
-    char** extarr = realloc(result->extensions, newcap*sizeof(char*));
-    unsigned* resultarr = realloc(result->result, newcap*sizeof(unsigned));
+    char **extarr = realloc(result->extensions, newcap*sizeof(char*));
+    unsigned *resultarr = realloc(result->result, newcap*sizeof(unsigned));
     if (!extarr || !resultarr) {
       fprintf(stderr, "Memory allocation error.\n");
       abort();
@@ -135,15 +135,15 @@
   result->count++;
 }
 
-scanresult* new_scanresult(settings* settings) {
-  scanresult* result = calloc(1, sizeof(scanresult));
+scanresult *new_scanresult(settings *settings) {
+  scanresult *result = calloc(1, sizeof(scanresult));
   if (settings->individual_sums) {
     result->ext = calloc(1, sizeof(scanresult_ext));
   }
   return result;
 }
 
-void destroy_scanresult(scanresult* result) {
+void destroy_scanresult(scanresult *result) {
   if (result->ext) {
     if (result->ext->count > 0) {
       for (unsigned i = 0 ; i < result->ext->count ; i++) {
@@ -157,9 +157,9 @@
   free(result);
 }
 
-static struct filelist *buildFileList(scanner scanner, settings* settings) {
+static struct filelist *buildFileList(scanner scanner, settings *settings) {
 
-  struct filelist* list = NULL;
+  struct filelist *list = NULL;
   DIR *dirf;
   struct dirent *entry;
   struct stat statbuf;
@@ -186,7 +186,7 @@
       
       /* Construct full pathname string */
       size_t dirnamelen = strlen(scanner.dir);
-      char *filename = malloc(2+dirnamelen+newentry->displayname_len);
+      char *filename = malloc(2 + dirnamelen + newentry->displayname_len);
       memcpy(filename, scanner.dir, dirnamelen);
       if (filename[dirnamelen - 1] != settings->fileSeparator) {
         filename[dirnamelen++] = settings->fileSeparator;
@@ -233,7 +233,7 @@
   return list;
 }
 
-static bool is_dir_excluded(settings* settings, const char* dir) {
+static bool is_dir_excluded(settings *settings, const char *dir) {
   const string_list * const list = settings->excludeDirs;
 
   for (size_t i = 0 ; i < list->count ; i++) {
@@ -251,7 +251,7 @@
       }
     } else {
       /* compare the full paths */
-      char* absdir = make_path_absolute(dir);
+      char *absdir = make_path_absolute(dir);
       if (pathcmp(list->items[i], absdir) == 0) {
         free(absdir);
         return true;
@@ -262,8 +262,8 @@
   return false;
 }
 
-void scanDirectory(scanner scnr, settings* settings,
-    string_list* output, scanresult* result) {
+void scanDirectory(scanner scnr, settings *settings,
+    string_list *output, scanresult *result) {
 
   result->result = 0;
   bool bfile;
@@ -435,7 +435,7 @@
 #else
   static char fs = '/';
 #endif /* _WIN32 */
-  char* abspath;
+  char *abspath;
   if (path_is_relative(path)) {
     if (cwdlen == 0) {
       get_working_dir(&cwd, &cwdlen);
@@ -457,7 +457,7 @@
   }
   /* make path canonical */
   size_t abspathlen = strlen(abspath);
-  char* canonical = malloc(abspathlen + 1);
+  char *canonical = malloc(abspathlen + 1);
   size_t canonicallen = 0;
   for (size_t j = 0; j < abspathlen; j++) {
     canonical[canonicallen++] = abspath[j];
--- a/src/scanner.h	Sat Jul 04 11:10:51 2026 +0200
+++ b/src/scanner.h	Sat Jul 04 12:09:37 2026 +0200
@@ -31,30 +31,30 @@
 #include "string_list.h"
 
 typedef struct {
-  char *dir;
+  const char *dir;
   unsigned spaces;
 } scanner;
 
 typedef struct {
   unsigned count;
   unsigned capacity;
-  char** extensions;
-  unsigned* result;
+  char **extensions;
+  unsigned *result;
 } scanresult_ext;
 
 typedef struct {
   unsigned result;
-  scanresult_ext* ext;
+  scanresult_ext *ext;
 } scanresult;
 
 #ifdef _cplusplus
 extern "C" {
 #endif
 
-void scanDirectory(scanner scanner, settings* settings,
-        string_list* output, scanresult* result);
+void scanDirectory(scanner scanner, settings *settings,
+        string_list *output, scanresult *result);
 
-scanresult* new_scanresult(settings* settings);
+scanresult *new_scanresult(settings *settings);
 void destroy_scanresult(scanresult*);
 
 char *make_path_absolute(const char *path);
--- a/src/settings.c	Sat Jul 04 11:10:51 2026 +0200
+++ b/src/settings.c	Sat Jul 04 12:09:37 2026 +0200
@@ -26,7 +26,7 @@
 
 #include "settings.h"
 
-settings* new_settings() {
+settings *new_settings() {
   settings *s = malloc(sizeof(settings));
   if (s != NULL) {
   #ifdef _WIN32
@@ -51,7 +51,7 @@
   return s;
 }
 
-void destroy_settings(settings* settings) {
+void destroy_settings(settings *settings) {
   destroy_regex_parser(settings->regex);
   destroy_string_list(settings->includeSuffixes);
   destroy_string_list(settings->excludeSuffixes);
--- a/src/settings.h	Sat Jul 04 11:10:51 2026 +0200
+++ b/src/settings.h	Sat Jul 04 12:09:37 2026 +0200
@@ -33,11 +33,11 @@
 #include "regex_parser.h"
 
 typedef struct {
-  string_list* includeSuffixes;
-  string_list* excludeSuffixes;
-  string_list* excludeDirs;
-  regex_parser* regex;
-  bfile_heuristics* bfileHeuristics;
+  string_list *includeSuffixes;
+  string_list *excludeSuffixes;
+  string_list *excludeDirs;
+  regex_parser *regex;
+  bfile_heuristics *bfileHeuristics;
   char fileSeparator;
   bool recursive;
   bool matchesOnly;
@@ -52,7 +52,7 @@
 extern "C" {
 #endif
 
-settings* new_settings();
+settings *new_settings();
 void destroy_settings(settings*);
 
 #ifdef _cplusplus
--- a/src/string_list.c	Sat Jul 04 11:10:51 2026 +0200
+++ b/src/string_list.c	Sat Jul 04 12:09:37 2026 +0200
@@ -28,12 +28,12 @@
 
 #include <assert.h>
 
-static void do_not_free(void* item) {
+static void do_not_free(void *item) {
   (void) item; /* do not do anything with the item */
 }
 
-string_list* new_string_list() {
-  string_list* stringList = malloc(sizeof(string_list));
+string_list *new_string_list() {
+  string_list *stringList = malloc(sizeof(string_list));
   stringList->count = 0;
   stringList->capacity = 32;
   stringList->items = calloc(stringList->capacity, sizeof(char*));
@@ -42,7 +42,7 @@
   return stringList;
 }
 
-void destroy_string_list(string_list* list) {
+void destroy_string_list(string_list *list) {
   if (list) {
     if (list->items) {
       for (size_t i = 0 ; i < list->count ; i++) {
@@ -54,14 +54,14 @@
   }
 }
 
-void add_string(string_list* list, char* item) {
+void add_string(string_list *list, char *item) {
   assert(list != NULL);
   if (list->count < list->capacity) {
     list->items[list->count++] = item;
     return;
   }
   size_t newCapacity = list->capacity * 2;
-  char** reallocated_list =
+  char **reallocated_list =
     realloc(list->items, sizeof(char*) * newCapacity);
   assert(reallocated_list != NULL);
   list->capacity = newCapacity;
--- a/src/string_list.h	Sat Jul 04 11:10:51 2026 +0200
+++ b/src/string_list.h	Sat Jul 04 12:09:37 2026 +0200
@@ -32,7 +32,7 @@
 typedef struct {
   size_t count;
   size_t capacity;
-  char** items;
+  char **items;
   void (*free_item)(void*);
 } string_list;
 
@@ -40,7 +40,7 @@
 extern "C" {
 #endif
 
-string_list* new_string_list();
+string_list *new_string_list();
 void destroy_string_list(string_list*);
 void add_string(string_list*, char*);
 

mercurial