Mon, 23 May 2011 16:54:56 +0200
removed binary files from repository and added hgignore
1 | 1 | #include "include.h" |
2 | #include "v2.h" | |
3 | ||
4 | ||
5 | #ifdef _WIN32 | |
6 | static char fileSeparator = '\\'; | |
7 | #else | |
8 | static char fileSeparator = '/'; | |
9 | #endif /* _WIN32 */ | |
0 | 10 | |
11 | static int suffixc; | |
12 | static char** suffixv; | |
13 | static bool recursive; | |
14 | static bool includeSuffixes; | |
1 | 15 | static bool matchesOnly; |
0 | 16 | |
1 | 17 | bool testSuffix(char* filename) { |
0 | 18 | bool ret = false; |
19 | int tokenlen, fnamelen = strlen(filename); | |
1 | 20 | for (int t = 0 ; t < suffixc ; t++) { |
0 | 21 | tokenlen = strlen(suffixv[t]); |
1 | 22 | if (fnamelen >= tokenlen && tokenlen > 0) { |
23 | if (strncmp(filename+fnamelen-tokenlen, suffixv[t], tokenlen) == 0) { | |
0 | 24 | ret = true; |
25 | break; | |
26 | } | |
27 | } | |
28 | } | |
29 | return ret ^ !includeSuffixes; | |
30 | } | |
31 | ||
1 | 32 | int scanDirectory(DIR *dir, const int spaces, char* currdir) { |
0 | 33 | DIR *subdir; |
34 | char* subdirname; | |
35 | struct dirent *entry; | |
36 | int lines, digits, a; | |
37 | int lineSum = 0; | |
38 | ||
1 | 39 | while ((entry = readdir(dir)) != NULL) { |
40 | if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { | |
0 | 41 | // Print occurence |
42 | char entryname[strlen(entry->d_name)+spaces]; | |
1 | 43 | for (int t = 0 ; t < spaces ; t++) { |
0 | 44 | entryname[t]=' '; |
45 | } | |
46 | entryname[spaces] = 0; | |
47 | strcat(entryname, entry->d_name); | |
48 | ||
49 | // Check for subdirectory | |
50 | char subdirname[(1+strlen(currdir)+strlen(entry->d_name))]; | |
51 | strcpy(subdirname, currdir); | |
1 | 52 | strncat(subdirname, &fileSeparator, 1); |
0 | 53 | strcat(subdirname, entry->d_name); |
1 | 54 | if ((subdir = opendir(subdirname)) != NULL) { |
55 | printf("%-60s\n", entryname); | |
56 | if (recursive) { | |
0 | 57 | lineSum += scanDirectory(subdir, spaces+1, subdirname); |
58 | } | |
59 | closedir(subdir); | |
60 | continue; | |
61 | } | |
62 | ||
63 | // Count lines | |
64 | lines = 0; | |
65 | char filename[(1+strlen(currdir)+strlen(entry->d_name))]; | |
66 | strcpy(filename, currdir); | |
1 | 67 | strncat(filename, &fileSeparator, 1); |
0 | 68 | strcat(filename, entry->d_name); |
1 | 69 | if (testSuffix(filename)) { |
0 | 70 | FILE *file = fopen(filename, "r"); |
1 | 71 | if (file == NULL) { |
0 | 72 | perror(" File acces failed"); |
73 | continue; | |
74 | } | |
75 | ||
1 | 76 | do { |
0 | 77 | a = fgetc(file); |
78 | ||
1 | 79 | if (a == 10) { |
0 | 80 | lines++; |
81 | } | |
82 | } while (a != EOF); | |
83 | fclose(file); | |
84 | ||
85 | // Print line count | |
1 | 86 | #ifdef _WIN32 |
87 | printf("%-60s%13d lines\n", entryname, lines); | |
88 | #else | |
89 | printf("%-60s%14d lines\n", entryname, lines); | |
90 | #endif /* _WIN32 */ | |
0 | 91 | |
92 | lineSum += lines; | |
93 | } | |
1 | 94 | else { |
95 | if (!matchesOnly) { | |
96 | // Print hint | |
97 | #ifdef _WIN32 | |
98 | printf("%-60s%19s\n", entryname, "no match"); | |
99 | #else | |
100 | printf("%-60s%20s\n", entryname, "no match"); | |
101 | #endif /* _WIN32 */ | |
102 | } | |
0 | 103 | } |
104 | } | |
105 | } | |
106 | return lineSum; | |
107 | } | |
108 | ||
1 | 109 | void printHelpText(const char* prgName) { |
0 | 110 | // Help text |
111 | const char* helpText = | |
1 | 112 | "\nUsage:" |
113 | "\n %s [-hrm][-s suffix][<directory>]" | |
114 | "\n %s [-hrm][-S suffix][<directory>]" | |
0 | 115 | "\n\nCounts the line terminator characters (\\n) within all" |
116 | " files in the specified\ndirectory." | |
117 | "\n\nOptions:" | |
118 | "\n -h, --help - this help text" | |
1 | 119 | "\n -m - print information about matching files only" |
120 | "\n -s <suffixes> - only count files with these suffixes (separated" | |
0 | 121 | "\n by commas)" |
1 | 122 | "\n -S <suffixes> - count any file except those with these suffixes" |
0 | 123 | "\n (separated by commas)" |
1 | 124 | "\n -r, -R - includes subdirectories" |
0 | 125 | "\n\n" |
1 | 126 | "The default call without any options is:" |
127 | "\n %s ./\n" | |
0 | 128 | "That means each file in each subdirectory is counted. If you want to count" |
129 | "\nC source code in your working directory and its subdirectories, type:" | |
1 | 130 | "\n %s -rs .c\n"; |
131 | ||
132 | printf(helpText, prgName, prgName, prgName, prgName); | |
133 | } | |
134 | ||
135 | int main(int argc, char** argv) { | |
0 | 136 | |
137 | // Program name | |
1 | 138 | char* prgName = strrchr(argv[0], fileSeparator); |
139 | ||
140 | if (prgName == NULL) { | |
0 | 141 | prgName = argv[0]; |
142 | } | |
1 | 143 | else { |
0 | 144 | prgName++; |
145 | } | |
146 | ||
147 | // Defaults | |
1 | 148 | char* _suffix = " "; |
149 | char _directory[3]; | |
150 | _directory[0] = '.'; | |
151 | _directory[1] = fileSeparator; | |
152 | _directory[2] = 0; | |
0 | 153 | |
154 | // Get arguments | |
155 | char* directory; | |
156 | char* suffix; | |
157 | bool showHelp = false; | |
1 | 158 | recursive = false; |
0 | 159 | includeSuffixes = false; |
160 | char checked = 0; | |
161 | ||
1 | 162 | for (int t = 1 ; t < argc ; t++) { |
163 | ||
164 | int argflags = checkArgument(argv[t], "hsSrRm"); | |
165 | ||
166 | // s | |
167 | if ((argflags & 2) > 0) { | |
168 | if ((checked & 1) > 0) { | |
169 | printHelpText(prgName); | |
0 | 170 | return -1; |
171 | } | |
172 | includeSuffixes = true; | |
1 | 173 | t++; |
174 | if (t >= argc) { | |
175 | printHelpText(prgName); | |
176 | return -1; | |
177 | } | |
178 | suffix = argv[t]; | |
0 | 179 | checked |= 1; |
180 | } | |
1 | 181 | // S |
182 | if ((argflags & 4) > 0) { | |
183 | if ((checked & 1) > 0) { | |
184 | printHelpText(prgName); | |
0 | 185 | return -1; |
186 | } | |
187 | includeSuffixes = false; | |
1 | 188 | t++; |
189 | if (t >= argc) { | |
190 | printHelpText(prgName); | |
191 | return -1; | |
192 | } | |
193 | suffix = argv[t]; | |
0 | 194 | checked |= 1; |
195 | } | |
1 | 196 | // h |
197 | if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { | |
198 | if ((checked & 2) > 0) { | |
199 | printHelpText(prgName); | |
0 | 200 | return -1; |
201 | } | |
202 | checked |= 2; | |
203 | showHelp = true; | |
204 | } | |
1 | 205 | // r, R |
206 | if ((argflags & 24) > 0) { | |
207 | if ((checked & 4) > 0) { | |
208 | printHelpText(prgName); | |
0 | 209 | return -1; |
210 | } | |
211 | checked |= 4; | |
212 | recursive = true; | |
213 | } | |
1 | 214 | if ((argflags & 32) > 0) { |
215 | if ((checked & 32) > 0) { | |
216 | printHelpText(prgName); | |
0 | 217 | return -1; |
218 | } | |
1 | 219 | checked |= 32; |
220 | matchesOnly = true; | |
0 | 221 | } |
1 | 222 | // other |
223 | if (argflags == 0) { | |
224 | if ((checked & 8) > 0) { | |
225 | printHelpText(prgName); | |
0 | 226 | return -1; |
227 | } | |
1 | 228 | checked |= 8; |
0 | 229 | directory = argv[t]; |
230 | } | |
231 | } | |
232 | ||
233 | // Show help and quit | |
1 | 234 | if (showHelp) { |
235 | printHelpText(prgName); | |
0 | 236 | return 0; |
237 | } | |
238 | ||
239 | // Default values | |
1 | 240 | if ((checked & 1) == 0) { |
0 | 241 | suffix = _suffix; |
242 | } | |
243 | ||
1 | 244 | if ((checked & 8) == 0) { |
0 | 245 | directory = _directory; |
246 | } | |
247 | ||
248 | // Find tokens | |
249 | char* finder; | |
250 | suffixc = 1; | |
251 | finder = strchr(suffix, ','); | |
1 | 252 | while (finder != NULL) { |
0 | 253 | suffixc++; |
254 | finder = strchr(finder+1, ','); | |
255 | } | |
256 | suffixv = (char**) malloc(sizeof(suffixv)*suffixc); | |
1 | 257 | if (suffixv == NULL) { |
0 | 258 | fprintf(stderr, "Memory allocation failed.\n"); |
259 | return 1; | |
260 | } | |
261 | finder = strtok(suffix, ","); | |
262 | int c = 0; | |
1 | 263 | while (finder != NULL) { |
0 | 264 | suffixv[c] = finder; |
265 | c++; | |
266 | finder = strtok(NULL, ","); | |
267 | } | |
268 | ||
269 | // Open directory | |
270 | DIR *dir = opendir(directory); | |
1 | 271 | if (dir == NULL) { |
0 | 272 | perror("Operation failed"); |
273 | free(suffixv); | |
274 | return 1; | |
275 | } | |
276 | ||
277 | // Scan directory | |
278 | int lines = scanDirectory(dir, 0, directory); | |
279 | ||
280 | // Print double line and line count | |
1 | 281 | #ifdef _WIN32 |
282 | const int columns = 79; | |
283 | #else | |
284 | const int columns = 80; | |
285 | #endif /* _WIN32 */ | |
286 | ||
287 | for (int t = 0 ; t < columns ; t++) { | |
0 | 288 | printf("="); |
289 | } | |
1 | 290 | #ifdef _WIN32 |
291 | printf("\n%73d lines\n", lines); | |
292 | #else | |
293 | printf("\n%74d lines\n", lines); | |
294 | #endif /* _WIN32 */ | |
0 | 295 | |
296 | closedir(dir); | |
297 | free(suffixv); | |
298 | return 0; | |
299 | } |