cline.c

changeset 6
be923400164c
parent 5
9393eff3d2f9
child 7
1b55f3fa52c9
--- 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;
 }

mercurial