# HG changeset patch # User Mike Becker # Date 1306500316 -7200 # Node ID be923400164ca8413056ddf01a5baa4a4b09bc0d # Parent 9393eff3d2f9bc2133289fbb45591f5e263a8ef2 encapsulated suffix list in type suffix_list_t diff -r 9393eff3d2f9 -r be923400164c cline.c --- 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; } diff -r 9393eff3d2f9 -r be923400164c cline.h --- 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 #include + +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 diff -r 9393eff3d2f9 -r be923400164c functions.c --- 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; }