1 #include "cline.h" |
1 #include "cline.h" |
2 #include "functions.h" |
2 #include "functions.h" |
3 |
3 |
4 settings_t* new_settings_t() { |
4 settings_t* new_settings_t() { |
5 settings_t *settings = malloc(sizeof(settings_t*)); |
5 settings_t *settings = malloc(sizeof(settings_t*)); |
6 #ifdef _WIN32 |
6 if (settings != NULL) { |
7 settings->fileSeparator = '\\'; |
7 #ifdef _WIN32 |
8 #else |
8 settings->fileSeparator = '\\'; |
9 settings->fileSeparator = '/'; |
9 #else |
10 #endif /* _WIN32 */ |
10 settings->fileSeparator = '/'; |
11 settings->suffixc = 1; |
11 #endif /* _WIN32 */ |
12 settings->recursive = false; |
12 settings->suffixc = 1; |
13 settings->includeSuffixes = false; |
13 settings->recursive = false; |
14 settings->matchesOnly = false; |
14 settings->includeSuffixes = false; |
|
15 settings->matchesOnly = false; |
|
16 } |
15 |
17 |
16 return settings; |
18 return settings; |
17 } |
19 } |
18 |
20 |
19 void destroy_settings_t(settings_t* settings) { |
21 void destroy_settings_t(settings_t* settings) { |
51 |
53 |
52 int main(int argc, char** argv) { |
54 int main(int argc, char** argv) { |
53 |
55 |
54 // Settings |
56 // Settings |
55 settings_t *settings = new_settings_t(); |
57 settings_t *settings = new_settings_t(); |
|
58 if (settings == NULL) { |
|
59 fprintf(stderr, "Memory allocation failed.\n"); |
|
60 return 1; |
|
61 } |
56 |
62 |
57 // Program name |
63 // Program name |
58 char* prgName = strrchr(argv[0], settings->fileSeparator); |
64 char* prgName = strrchr(argv[0], settings->fileSeparator); |
59 |
65 |
60 if (prgName == NULL) { |
66 if (prgName == NULL) { |
82 |
88 |
83 // s |
89 // s |
84 if ((argflags & 2) > 0) { |
90 if ((argflags & 2) > 0) { |
85 if ((checked & 1) > 0) { |
91 if ((checked & 1) > 0) { |
86 printHelpText(prgName); |
92 printHelpText(prgName); |
87 return -1; |
93 destroy_settings_t(settings); |
|
94 return 1; |
88 } |
95 } |
89 settings->includeSuffixes = true; |
96 settings->includeSuffixes = true; |
90 t++; |
97 t++; |
91 if (t >= argc) { |
98 if (t >= argc) { |
92 printHelpText(prgName); |
99 printHelpText(prgName); |
93 return -1; |
100 destroy_settings_t(settings); |
|
101 return 1; |
94 } |
102 } |
95 suffix = argv[t]; |
103 suffix = argv[t]; |
96 checked |= 1; |
104 checked |= 1; |
97 } |
105 } |
98 // S |
106 // S |
99 if ((argflags & 4) > 0) { |
107 if ((argflags & 4) > 0) { |
100 if ((checked & 1) > 0) { |
108 if ((checked & 1) > 0) { |
101 printHelpText(prgName); |
109 printHelpText(prgName); |
102 return -1; |
110 destroy_settings_t(settings); |
|
111 return 1; |
103 } |
112 } |
104 settings->includeSuffixes = false; |
113 settings->includeSuffixes = false; |
105 t++; |
114 t++; |
106 if (t >= argc) { |
115 if (t >= argc) { |
107 printHelpText(prgName); |
116 printHelpText(prgName); |
108 return -1; |
117 destroy_settings_t(settings); |
|
118 return 1; |
109 } |
119 } |
110 suffix = argv[t]; |
120 suffix = argv[t]; |
111 checked |= 1; |
121 checked |= 1; |
112 } |
122 } |
113 // h |
123 // h |
114 if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { |
124 if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { |
115 if ((checked & 2) > 0) { |
125 if ((checked & 2) > 0) { |
116 printHelpText(prgName); |
126 printHelpText(prgName); |
117 return -1; |
127 destroy_settings_t(settings); |
|
128 return 1; |
118 } |
129 } |
119 checked |= 2; |
130 checked |= 2; |
120 showHelp = true; |
131 showHelp = true; |
121 } |
132 } |
122 // r, R |
133 // r, R |
123 if ((argflags & 24) > 0) { |
134 if ((argflags & 24) > 0) { |
124 if ((checked & 4) > 0) { |
135 if ((checked & 4) > 0) { |
125 printHelpText(prgName); |
136 printHelpText(prgName); |
126 return -1; |
137 destroy_settings_t(settings); |
|
138 return 1; |
127 } |
139 } |
128 checked |= 4; |
140 checked |= 4; |
129 settings->recursive = true; |
141 settings->recursive = true; |
130 } |
142 } |
131 if ((argflags & 32) > 0) { |
143 if ((argflags & 32) > 0) { |
132 if ((checked & 32) > 0) { |
144 if ((checked & 32) > 0) { |
133 printHelpText(prgName); |
145 printHelpText(prgName); |
134 return -1; |
146 destroy_settings_t(settings); |
|
147 return 1; |
135 } |
148 } |
136 checked |= 32; |
149 checked |= 32; |
137 settings->matchesOnly = true; |
150 settings->matchesOnly = true; |
138 } |
151 } |
139 // other |
152 // other |
140 if (argflags == 0) { |
153 if (argflags == 0) { |
141 if ((checked & 8) > 0) { |
154 if ((checked & 8) > 0) { |
142 printHelpText(prgName); |
155 printHelpText(prgName); |
143 return -1; |
156 destroy_settings_t(settings); |
|
157 return 1; |
144 } |
158 } |
145 checked |= 8; |
159 checked |= 8; |
146 directory = argv[t]; |
160 directory = argv[t]; |
147 } |
161 } |
148 } |
162 } |
149 |
163 |
150 // Show help and quit |
164 // Show help and quit |
151 if (showHelp) { |
165 if (showHelp) { |
152 printHelpText(prgName); |
166 printHelpText(prgName); |
|
167 destroy_settings_t(settings); |
153 return 0; |
168 return 0; |
154 } |
169 } |
155 |
170 |
156 // Default values |
171 // Default values |
157 if ((checked & 1) == 0) { |
172 if ((checked & 1) == 0) { |
170 finder = strchr(finder+1, ','); |
185 finder = strchr(finder+1, ','); |
171 } |
186 } |
172 settings->suffixv = (char**) malloc(sizeof(char**)*settings->suffixc); |
187 settings->suffixv = (char**) malloc(sizeof(char**)*settings->suffixc); |
173 if (settings->suffixv == NULL) { |
188 if (settings->suffixv == NULL) { |
174 fprintf(stderr, "Memory allocation failed.\n"); |
189 fprintf(stderr, "Memory allocation failed.\n"); |
|
190 destroy_settings_t(settings); |
175 return 1; |
191 return 1; |
176 } |
192 } |
177 finder = strtok(suffix, ","); |
193 finder = strtok(suffix, ","); |
178 int c = 0; |
194 int c = 0; |
179 while (finder != NULL) { |
195 while (finder != NULL) { |