Fri, 27 May 2011 14:45:16 +0200
encapsulated suffix list in type suffix_list_t
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
1 | #include "cline.h" |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
2 | #include "functions.h" |
0 | 3 | |
6
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
4 | suffix_list_t* new_suffix_list_t() { |
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
5 | suffix_list_t* suffixList = malloc(sizeof(suffix_list_t*)); |
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
6 | suffixList->count = 0; |
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
7 | suffixList->items = NULL; |
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
8 | } |
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
9 | |
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
10 | void destroy_suffix_list_t(suffix_list_t* list) { |
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
11 | while (--list->count >= 0) { |
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
12 | free(list->items[list->count]); |
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
13 | } |
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
14 | free(list); |
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
15 | } |
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
16 | |
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
17 | void add_suffix(suffix_list_t* list, char* item) { |
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
18 | char** reallocated_list = |
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
19 | realloc(list->items, sizeof(char**) * list->count + 1); |
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
20 | if (reallocated_list != NULL) { |
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
21 | list->items = reallocated_list; |
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
22 | list->items[list->count] = item; |
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
23 | list->count++; |
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
24 | } |
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
25 | } |
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
26 | |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
27 | settings_t* new_settings_t() { |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
28 | settings_t *settings = malloc(sizeof(settings_t*)); |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
29 | if (settings != NULL) { |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
30 | #ifdef _WIN32 |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
31 | settings->fileSeparator = '\\'; |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
32 | #else |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
33 | settings->fileSeparator = '/'; |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
34 | #endif /* _WIN32 */ |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
35 | settings->recursive = false; |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
36 | settings->includeSuffixes = false; |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
37 | settings->matchesOnly = false; |
6
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
38 | settings->suffixList = new_suffix_list_t(); |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
39 | } |
4
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
40 | |
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
41 | return settings; |
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
42 | } |
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
43 | |
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
44 | void destroy_settings_t(settings_t* settings) { |
6
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
45 | destroy_suffix_list_t(settings->suffixList); |
4
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
46 | free(settings); |
0 | 47 | } |
48 | ||
1 | 49 | void printHelpText(const char* prgName) { |
0 | 50 | // Help text |
51 | const char* helpText = | |
1 | 52 | "\nUsage:" |
53 | "\n %s [-hrm][-s suffix][<directory>]" | |
54 | "\n %s [-hrm][-S suffix][<directory>]" | |
0 | 55 | "\n\nCounts the line terminator characters (\\n) within all" |
56 | " files in the specified\ndirectory." | |
57 | "\n\nOptions:" | |
58 | "\n -h, --help - this help text" | |
1 | 59 | "\n -m - print information about matching files only" |
60 | "\n -s <suffixes> - only count files with these suffixes (separated" | |
0 | 61 | "\n by commas)" |
1 | 62 | "\n -S <suffixes> - count any file except those with these suffixes" |
0 | 63 | "\n (separated by commas)" |
1 | 64 | "\n -r, -R - includes subdirectories" |
0 | 65 | "\n\n" |
1 | 66 | "The default call without any options is:" |
67 | "\n %s ./\n" | |
0 | 68 | "That means each file in each subdirectory is counted. If you want to count" |
69 | "\nC source code in your working directory and its subdirectories, type:" | |
1 | 70 | "\n %s -rs .c\n"; |
71 | ||
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
72 | printf(helpText, prgName, prgName, prgName, prgName); |
1 | 73 | } |
74 | ||
75 | int main(int argc, char** argv) { | |
0 | 76 | |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
77 | // Settings |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
78 | settings_t *settings = new_settings_t(); |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
79 | if (settings == NULL) { |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
80 | fprintf(stderr, "Memory allocation failed.\n"); |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
81 | return 1; |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
82 | } |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
83 | |
0 | 84 | // Program name |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
85 | char* prgName = strrchr(argv[0], settings->fileSeparator); |
1 | 86 | |
87 | if (prgName == NULL) { | |
0 | 88 | prgName = argv[0]; |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
89 | } else { |
0 | 90 | prgName++; |
91 | } | |
92 | ||
93 | // Defaults | |
1 | 94 | char* _suffix = " "; |
95 | char _directory[3]; | |
96 | _directory[0] = '.'; | |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
97 | _directory[1] = settings->fileSeparator; |
1 | 98 | _directory[2] = 0; |
0 | 99 | |
100 | // Get arguments | |
101 | char* directory; | |
102 | char* suffix; | |
103 | bool showHelp = false; | |
104 | char checked = 0; | |
105 | ||
1 | 106 | for (int t = 1 ; t < argc ; t++) { |
107 | ||
108 | int argflags = checkArgument(argv[t], "hsSrRm"); | |
109 | ||
110 | // s | |
111 | if ((argflags & 2) > 0) { | |
112 | if ((checked & 1) > 0) { | |
113 | printHelpText(prgName); | |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
114 | destroy_settings_t(settings); |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
115 | return 1; |
0 | 116 | } |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
117 | settings->includeSuffixes = true; |
1 | 118 | t++; |
119 | if (t >= argc) { | |
120 | printHelpText(prgName); | |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
121 | destroy_settings_t(settings); |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
122 | return 1; |
1 | 123 | } |
124 | suffix = argv[t]; | |
0 | 125 | checked |= 1; |
126 | } | |
1 | 127 | // S |
128 | if ((argflags & 4) > 0) { | |
129 | if ((checked & 1) > 0) { | |
130 | printHelpText(prgName); | |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
131 | destroy_settings_t(settings); |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
132 | return 1; |
0 | 133 | } |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
134 | settings->includeSuffixes = false; |
1 | 135 | t++; |
136 | if (t >= argc) { | |
137 | printHelpText(prgName); | |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
138 | destroy_settings_t(settings); |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
139 | return 1; |
1 | 140 | } |
141 | suffix = argv[t]; | |
0 | 142 | checked |= 1; |
143 | } | |
1 | 144 | // h |
145 | if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { | |
146 | if ((checked & 2) > 0) { | |
147 | printHelpText(prgName); | |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
148 | destroy_settings_t(settings); |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
149 | return 1; |
0 | 150 | } |
151 | checked |= 2; | |
152 | showHelp = true; | |
153 | } | |
1 | 154 | // r, R |
155 | if ((argflags & 24) > 0) { | |
156 | if ((checked & 4) > 0) { | |
157 | printHelpText(prgName); | |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
158 | destroy_settings_t(settings); |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
159 | return 1; |
0 | 160 | } |
161 | checked |= 4; | |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
162 | settings->recursive = true; |
0 | 163 | } |
1 | 164 | if ((argflags & 32) > 0) { |
165 | if ((checked & 32) > 0) { | |
166 | printHelpText(prgName); | |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
167 | destroy_settings_t(settings); |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
168 | return 1; |
0 | 169 | } |
1 | 170 | checked |= 32; |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
171 | settings->matchesOnly = true; |
0 | 172 | } |
6
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
173 | // Path |
1 | 174 | if (argflags == 0) { |
175 | if ((checked & 8) > 0) { | |
176 | printHelpText(prgName); | |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
177 | destroy_settings_t(settings); |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
178 | return 1; |
0 | 179 | } |
1 | 180 | checked |= 8; |
0 | 181 | directory = argv[t]; |
182 | } | |
183 | } | |
184 | ||
185 | // Show help and quit | |
1 | 186 | if (showHelp) { |
187 | printHelpText(prgName); | |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
188 | destroy_settings_t(settings); |
0 | 189 | return 0; |
190 | } | |
191 | ||
192 | // Default values | |
1 | 193 | if ((checked & 1) == 0) { |
0 | 194 | suffix = _suffix; |
195 | } | |
196 | ||
1 | 197 | if ((checked & 8) == 0) { |
0 | 198 | directory = _directory; |
199 | } | |
200 | ||
201 | // Find tokens | |
6
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
202 | char* finder = strtok(suffix, ","); |
1 | 203 | while (finder != NULL) { |
6
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
204 | add_suffix(settings->suffixList, finder); |
0 | 205 | finder = strtok(NULL, ","); |
206 | } | |
207 | ||
208 | // Open directory | |
209 | DIR *dir = opendir(directory); | |
1 | 210 | if (dir == NULL) { |
0 | 211 | perror("Operation failed"); |
4
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
212 | destroy_settings_t(settings); |
0 | 213 | return 1; |
214 | } | |
215 | ||
216 | // Scan directory | |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
217 | int lines = scanDirectory(dir, 0, directory, settings); |
4
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
218 | closedir(dir); |
0 | 219 | |
220 | // Print double line and line count | |
1 | 221 | #ifdef _WIN32 |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
222 | const int columns = 79; |
1 | 223 | #else |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
224 | const int columns = 80; |
1 | 225 | #endif /* _WIN32 */ |
226 | ||
227 | for (int t = 0 ; t < columns ; t++) { | |
0 | 228 | printf("="); |
229 | } | |
1 | 230 | #ifdef _WIN32 |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
231 | printf("\n%73d lines\n", lines); |
1 | 232 | #else |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
233 | printf("\n%74d lines\n", lines); |
1 | 234 | #endif /* _WIN32 */ |
0 | 235 | |
4
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
236 | destroy_settings_t(settings); |
6
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
237 | |
0 | 238 | return 0; |
239 | } |