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