encapsulated suffix list in type suffix_list_t

Fri, 27 May 2011 14:45:16 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 27 May 2011 14:45:16 +0200
changeset 6
be923400164c
parent 5
9393eff3d2f9
child 7
1b55f3fa52c9

encapsulated suffix list in type suffix_list_t

cline.c file | annotate | diff | comparison | revisions
cline.h file | annotate | diff | comparison | revisions
functions.c file | annotate | diff | comparison | revisions
--- a/cline.c	Fri May 27 13:20:15 2011 +0200
+++ b/cline.c	Fri May 27 14:45:16 2011 +0200
@@ -1,6 +1,29 @@
 #include "cline.h"
 #include "functions.h"
 
+suffix_list_t* new_suffix_list_t() {
+  suffix_list_t* suffixList = malloc(sizeof(suffix_list_t*));
+  suffixList->count = 0;
+  suffixList->items = NULL;
+}
+
+void destroy_suffix_list_t(suffix_list_t* list) {
+  while (--list->count >= 0) {
+    free(list->items[list->count]);
+  }
+  free(list);
+}
+
+void add_suffix(suffix_list_t* list, char* item) {
+  char** reallocated_list =
+    realloc(list->items, sizeof(char**) * list->count + 1);
+  if (reallocated_list != NULL) {
+    list->items = reallocated_list;
+    list->items[list->count] = item;
+    list->count++;
+  }
+}
+
 settings_t* new_settings_t() {
   settings_t *settings = malloc(sizeof(settings_t*));
   if (settings != NULL) {
@@ -9,19 +32,17 @@
   #else
     settings->fileSeparator      = '/';
   #endif /* _WIN32 */
-    settings->suffixc            = 1;
     settings->recursive          = false;
     settings->includeSuffixes    = false;
     settings->matchesOnly        = false;
+    settings->suffixList         = new_suffix_list_t();
   }
   
   return settings;
 }
 
 void destroy_settings_t(settings_t* settings) {
-  if (settings->suffixv != NULL) {
-    free(settings->suffixv);
-  }
+  destroy_suffix_list_t(settings->suffixList);
   free(settings);
 }
 
@@ -149,7 +170,7 @@
       checked |= 32;
       settings->matchesOnly = true;
     }
-    // other
+    // Path
     if (argflags == 0) {
       if ((checked & 8) > 0) {
         printHelpText(prgName);
@@ -178,23 +199,9 @@
   }
 
   // Find tokens
-  char* finder;
-  finder = strchr(suffix, ',');
+  char* finder = strtok(suffix, ",");
   while (finder != NULL) {
-    settings->suffixc++;
-    finder = strchr(finder+1, ',');
-  }
-  settings->suffixv = (char**) malloc(sizeof(char**)*settings->suffixc);
-  if (settings->suffixv == NULL) {
-    fprintf(stderr, "Memory allocation failed.\n");
-    destroy_settings_t(settings);
-    return 1;
-  }
-  finder = strtok(suffix, ",");
-  int c = 0;
-  while (finder != NULL) {
-    settings->suffixv[c] = finder;
-    c++;
+    add_suffix(settings->suffixList, finder);
     finder = strtok(NULL, ",");
   }
 
@@ -227,5 +234,6 @@
   #endif /* _WIN32 */
 
   destroy_settings_t(settings);
+
   return 0;
 }
--- a/cline.h	Fri May 27 13:20:15 2011 +0200
+++ b/cline.h	Fri May 27 14:45:16 2011 +0200
@@ -7,10 +7,15 @@
 #include <stdlib.h>
 #include <dirent.h>
 
+
+typedef struct _suffix_list {
+  int count;
+  char** items;
+} suffix_list_t;
+
 typedef struct _settings {
   char fileSeparator;
-  int suffixc;
-  char** suffixv;
+  suffix_list_t* suffixList;
   bool recursive;
   bool includeSuffixes;
   bool matchesOnly;
@@ -21,6 +26,9 @@
 #endif
 settings_t* new_settings_t();
 void destroy_settings_t(settings_t*);
+suffix_list_t* new_suffix_list_t();
+void destroy_suffix_list_t(suffix_list_t*);
+void add_suffix(suffix_list_t*, char*);
 
 void printHelpText(const char*);
 #ifdef _cplusplus
--- a/functions.c	Fri May 27 13:20:15 2011 +0200
+++ b/functions.c	Fri May 27 14:45:16 2011 +0200
@@ -19,11 +19,11 @@
 bool testSuffix(char* filename, settings_t* settings) {
   bool ret = false;
   int tokenlen, fnamelen = strlen(filename);
-  for (int t = 0 ; t < settings->suffixc ; t++) {
-    tokenlen = strlen(settings->suffixv[t]);
+  for (int t = 0 ; t < settings->suffixList->count ; t++) {
+    tokenlen = strlen(settings->suffixList->items[t]);
     if (fnamelen >= tokenlen && tokenlen > 0) {
       if (strncmp(filename+fnamelen-tokenlen,
-                  settings->suffixv[t], tokenlen) == 0) {
+                  settings->suffixList->items[t], tokenlen) == 0) {
         ret = true;
         break;
       }

mercurial