Mon, 30 May 2011 08:45:08 +0200
encapsulated common operations
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" | |
7
1b55f3fa52c9
Fixed helpText and removed extra variables for default values
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
68 | "So each file in the working directory is counted. If you want to count C" |
1b55f3fa52c9
Fixed helpText and removed extra variables for default values
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
69 | "\nsource 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 | ||
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
75 | int exit_with_help(char* prgName, settings_t* settings, int code) { |
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
76 | printHelpText(prgName); |
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
77 | destroy_settings_t(settings); |
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
78 | return code; |
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
79 | } |
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
80 | |
1 | 81 | int main(int argc, char** argv) { |
0 | 82 | |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
83 | // Settings |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
84 | 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
|
85 | if (settings == NULL) { |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
86 | 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
|
87 | return 1; |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
88 | } |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
89 | |
0 | 90 | // Program name |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
91 | char* prgName = strrchr(argv[0], settings->fileSeparator); |
1 | 92 | |
93 | if (prgName == NULL) { | |
0 | 94 | prgName = argv[0]; |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
95 | } else { |
0 | 96 | prgName++; |
97 | } | |
98 | ||
99 | // Get arguments | |
7
1b55f3fa52c9
Fixed helpText and removed extra variables for default values
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
100 | char* directory = "./"; |
1b55f3fa52c9
Fixed helpText and removed extra variables for default values
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
101 | char* suffix = " "; |
0 | 102 | bool showHelp = false; |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
103 | int checked = 0; |
0 | 104 | |
1 | 105 | for (int t = 1 ; t < argc ; t++) { |
106 | ||
107 | int argflags = checkArgument(argv[t], "hsSrRm"); | |
108 | ||
109 | // s | |
110 | if ((argflags & 2) > 0) { | |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
111 | if (registerArgument(&checked, 6)) { |
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
112 | return exit_with_help(prgName, settings, 1); |
0 | 113 | } |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
114 | settings->includeSuffixes = true; |
1 | 115 | t++; |
116 | if (t >= argc) { | |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
117 | return exit_with_help(prgName, settings, 1); |
1 | 118 | } |
119 | suffix = argv[t]; | |
0 | 120 | } |
1 | 121 | // S |
122 | if ((argflags & 4) > 0) { | |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
123 | if (registerArgument(&checked, 6)) { |
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
124 | return exit_with_help(prgName, settings, 1); |
0 | 125 | } |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
126 | settings->includeSuffixes = false; |
1 | 127 | t++; |
128 | if (t >= argc) { | |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
129 | return exit_with_help(prgName, settings, 1); |
1 | 130 | } |
131 | suffix = argv[t]; | |
0 | 132 | } |
1 | 133 | // h |
134 | if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { | |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
135 | if (registerArgument(&checked, 1)) { |
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
136 | return exit_with_help(prgName, settings, 1); |
0 | 137 | } |
138 | showHelp = true; | |
139 | } | |
1 | 140 | // r, R |
141 | if ((argflags & 24) > 0) { | |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
142 | if (registerArgument(&checked, 24)) { |
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
143 | return exit_with_help(prgName, settings, 1); |
0 | 144 | } |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
145 | settings->recursive = true; |
0 | 146 | } |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
147 | // m |
1 | 148 | if ((argflags & 32) > 0) { |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
149 | if (registerArgument(&checked, 32)) { |
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
150 | return exit_with_help(prgName, settings, 1); |
0 | 151 | } |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
152 | settings->matchesOnly = true; |
0 | 153 | } |
6
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
154 | // Path |
1 | 155 | if (argflags == 0) { |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
156 | if (registerArgument(&checked, 1024)) { |
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
157 | return exit_with_help(prgName, settings, 1); |
0 | 158 | } |
159 | directory = argv[t]; | |
160 | } | |
161 | } | |
162 | ||
163 | // Show help and quit | |
1 | 164 | if (showHelp) { |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
165 | return exit_with_help(prgName, settings, 0); |
0 | 166 | } |
167 | ||
168 | // Find tokens | |
6
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
169 | char* finder = strtok(suffix, ","); |
1 | 170 | while (finder != NULL) { |
6
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
171 | add_suffix(settings->suffixList, finder); |
0 | 172 | finder = strtok(NULL, ","); |
173 | } | |
174 | ||
175 | // Open directory | |
176 | DIR *dir = opendir(directory); | |
1 | 177 | if (dir == NULL) { |
0 | 178 | perror("Operation failed"); |
4
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
179 | destroy_settings_t(settings); |
0 | 180 | return 1; |
181 | } | |
182 | ||
183 | // Scan directory | |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
184 | 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
|
185 | closedir(dir); |
7
1b55f3fa52c9
Fixed helpText and removed extra variables for default values
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
186 | destroy_settings_t(settings); |
0 | 187 | |
188 | // Print double line and line count | |
1 | 189 | #ifdef _WIN32 |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
190 | const int columns = 79; |
1 | 191 | #else |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
192 | const int columns = 80; |
1 | 193 | #endif /* _WIN32 */ |
194 | ||
195 | for (int t = 0 ; t < columns ; t++) { | |
0 | 196 | printf("="); |
197 | } | |
1 | 198 | #ifdef _WIN32 |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
199 | printf("\n%73d lines\n", lines); |
1 | 200 | #else |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
201 | printf("\n%74d lines\n", lines); |
1 | 202 | #endif /* _WIN32 */ |
0 | 203 | |
204 | return 0; | |
205 | } |