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