1.1 --- a/cline.c Fri May 27 13:20:15 2011 +0200 1.2 +++ b/cline.c Fri May 27 14:45:16 2011 +0200 1.3 @@ -1,6 +1,29 @@ 1.4 #include "cline.h" 1.5 #include "functions.h" 1.6 1.7 +suffix_list_t* new_suffix_list_t() { 1.8 + suffix_list_t* suffixList = malloc(sizeof(suffix_list_t*)); 1.9 + suffixList->count = 0; 1.10 + suffixList->items = NULL; 1.11 +} 1.12 + 1.13 +void destroy_suffix_list_t(suffix_list_t* list) { 1.14 + while (--list->count >= 0) { 1.15 + free(list->items[list->count]); 1.16 + } 1.17 + free(list); 1.18 +} 1.19 + 1.20 +void add_suffix(suffix_list_t* list, char* item) { 1.21 + char** reallocated_list = 1.22 + realloc(list->items, sizeof(char**) * list->count + 1); 1.23 + if (reallocated_list != NULL) { 1.24 + list->items = reallocated_list; 1.25 + list->items[list->count] = item; 1.26 + list->count++; 1.27 + } 1.28 +} 1.29 + 1.30 settings_t* new_settings_t() { 1.31 settings_t *settings = malloc(sizeof(settings_t*)); 1.32 if (settings != NULL) { 1.33 @@ -9,19 +32,17 @@ 1.34 #else 1.35 settings->fileSeparator = '/'; 1.36 #endif /* _WIN32 */ 1.37 - settings->suffixc = 1; 1.38 settings->recursive = false; 1.39 settings->includeSuffixes = false; 1.40 settings->matchesOnly = false; 1.41 + settings->suffixList = new_suffix_list_t(); 1.42 } 1.43 1.44 return settings; 1.45 } 1.46 1.47 void destroy_settings_t(settings_t* settings) { 1.48 - if (settings->suffixv != NULL) { 1.49 - free(settings->suffixv); 1.50 - } 1.51 + destroy_suffix_list_t(settings->suffixList); 1.52 free(settings); 1.53 } 1.54 1.55 @@ -149,7 +170,7 @@ 1.56 checked |= 32; 1.57 settings->matchesOnly = true; 1.58 } 1.59 - // other 1.60 + // Path 1.61 if (argflags == 0) { 1.62 if ((checked & 8) > 0) { 1.63 printHelpText(prgName); 1.64 @@ -178,23 +199,9 @@ 1.65 } 1.66 1.67 // Find tokens 1.68 - char* finder; 1.69 - finder = strchr(suffix, ','); 1.70 + char* finder = strtok(suffix, ","); 1.71 while (finder != NULL) { 1.72 - settings->suffixc++; 1.73 - finder = strchr(finder+1, ','); 1.74 - } 1.75 - settings->suffixv = (char**) malloc(sizeof(char**)*settings->suffixc); 1.76 - if (settings->suffixv == NULL) { 1.77 - fprintf(stderr, "Memory allocation failed.\n"); 1.78 - destroy_settings_t(settings); 1.79 - return 1; 1.80 - } 1.81 - finder = strtok(suffix, ","); 1.82 - int c = 0; 1.83 - while (finder != NULL) { 1.84 - settings->suffixv[c] = finder; 1.85 - c++; 1.86 + add_suffix(settings->suffixList, finder); 1.87 finder = strtok(NULL, ","); 1.88 } 1.89 1.90 @@ -227,5 +234,6 @@ 1.91 #endif /* _WIN32 */ 1.92 1.93 destroy_settings_t(settings); 1.94 + 1.95 return 0; 1.96 }