cline.c

changeset 6
be923400164c
parent 5
9393eff3d2f9
child 7
1b55f3fa52c9
equal deleted inserted replaced
5:9393eff3d2f9 6:be923400164c
1 #include "cline.h" 1 #include "cline.h"
2 #include "functions.h" 2 #include "functions.h"
3
4 suffix_list_t* new_suffix_list_t() {
5 suffix_list_t* suffixList = malloc(sizeof(suffix_list_t*));
6 suffixList->count = 0;
7 suffixList->items = NULL;
8 }
9
10 void destroy_suffix_list_t(suffix_list_t* list) {
11 while (--list->count >= 0) {
12 free(list->items[list->count]);
13 }
14 free(list);
15 }
16
17 void add_suffix(suffix_list_t* list, char* item) {
18 char** reallocated_list =
19 realloc(list->items, sizeof(char**) * list->count + 1);
20 if (reallocated_list != NULL) {
21 list->items = reallocated_list;
22 list->items[list->count] = item;
23 list->count++;
24 }
25 }
3 26
4 settings_t* new_settings_t() { 27 settings_t* new_settings_t() {
5 settings_t *settings = malloc(sizeof(settings_t*)); 28 settings_t *settings = malloc(sizeof(settings_t*));
6 if (settings != NULL) { 29 if (settings != NULL) {
7 #ifdef _WIN32 30 #ifdef _WIN32
8 settings->fileSeparator = '\\'; 31 settings->fileSeparator = '\\';
9 #else 32 #else
10 settings->fileSeparator = '/'; 33 settings->fileSeparator = '/';
11 #endif /* _WIN32 */ 34 #endif /* _WIN32 */
12 settings->suffixc = 1;
13 settings->recursive = false; 35 settings->recursive = false;
14 settings->includeSuffixes = false; 36 settings->includeSuffixes = false;
15 settings->matchesOnly = false; 37 settings->matchesOnly = false;
38 settings->suffixList = new_suffix_list_t();
16 } 39 }
17 40
18 return settings; 41 return settings;
19 } 42 }
20 43
21 void destroy_settings_t(settings_t* settings) { 44 void destroy_settings_t(settings_t* settings) {
22 if (settings->suffixv != NULL) { 45 destroy_suffix_list_t(settings->suffixList);
23 free(settings->suffixv);
24 }
25 free(settings); 46 free(settings);
26 } 47 }
27 48
28 void printHelpText(const char* prgName) { 49 void printHelpText(const char* prgName) {
29 // Help text 50 // Help text
147 return 1; 168 return 1;
148 } 169 }
149 checked |= 32; 170 checked |= 32;
150 settings->matchesOnly = true; 171 settings->matchesOnly = true;
151 } 172 }
152 // other 173 // Path
153 if (argflags == 0) { 174 if (argflags == 0) {
154 if ((checked & 8) > 0) { 175 if ((checked & 8) > 0) {
155 printHelpText(prgName); 176 printHelpText(prgName);
156 destroy_settings_t(settings); 177 destroy_settings_t(settings);
157 return 1; 178 return 1;
176 if ((checked & 8) == 0) { 197 if ((checked & 8) == 0) {
177 directory = _directory; 198 directory = _directory;
178 } 199 }
179 200
180 // Find tokens 201 // Find tokens
181 char* finder; 202 char* finder = strtok(suffix, ",");
182 finder = strchr(suffix, ',');
183 while (finder != NULL) { 203 while (finder != NULL) {
184 settings->suffixc++; 204 add_suffix(settings->suffixList, finder);
185 finder = strchr(finder+1, ',');
186 }
187 settings->suffixv = (char**) malloc(sizeof(char**)*settings->suffixc);
188 if (settings->suffixv == NULL) {
189 fprintf(stderr, "Memory allocation failed.\n");
190 destroy_settings_t(settings);
191 return 1;
192 }
193 finder = strtok(suffix, ",");
194 int c = 0;
195 while (finder != NULL) {
196 settings->suffixv[c] = finder;
197 c++;
198 finder = strtok(NULL, ","); 205 finder = strtok(NULL, ",");
199 } 206 }
200 207
201 // Open directory 208 // Open directory
202 DIR *dir = opendir(directory); 209 DIR *dir = opendir(directory);
225 #else 232 #else
226 printf("\n%74d lines\n", lines); 233 printf("\n%74d lines\n", lines);
227 #endif /* _WIN32 */ 234 #endif /* _WIN32 */
228 235
229 destroy_settings_t(settings); 236 destroy_settings_t(settings);
237
230 return 0; 238 return 0;
231 } 239 }

mercurial