functions.c

changeset 3
510d6b198dde
parent 1
34a5e235d16e
child 6
be923400164c
equal deleted inserted replaced
2:29ac790c27ad 3:510d6b198dde
1 #include "cline.h"
2 #include "functions.h"
3
4 int checkArgument(const char* arg, const char* expected) {
5 int len = strlen(expected);
6 int ret = 0;
7
8 if (arg[0] == '-') {
9 if (arg[1] != '-') {
10 for (int t = 0 ; t < len ; t++) {
11 ret |= (strchr(arg, expected[t]) > 0) << t;
12 }
13 }
14 }
15
16 return ret;
17 }
18
19 bool testSuffix(char* filename, settings_t* settings) {
20 bool ret = false;
21 int tokenlen, fnamelen = strlen(filename);
22 for (int t = 0 ; t < settings->suffixc ; t++) {
23 tokenlen = strlen(settings->suffixv[t]);
24 if (fnamelen >= tokenlen && tokenlen > 0) {
25 if (strncmp(filename+fnamelen-tokenlen,
26 settings->suffixv[t], tokenlen) == 0) {
27 ret = true;
28 break;
29 }
30 }
31 }
32 return ret ^ !settings->includeSuffixes;
33 }
34
35 int scanDirectory(DIR *dir, const int spaces,
36 char* currdir, settings_t* settings) {
37 DIR *subdir;
38 char* subdirname;
39 struct dirent *entry;
40 int lines, digits, a;
41 int lineSum = 0;
42
43 while ((entry = readdir(dir)) != NULL) {
44 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
45 // Print occurence
46 char entryname[strlen(entry->d_name)+spaces];
47 for (int t = 0 ; t < spaces ; t++) {
48 entryname[t]=' ';
49 }
50 entryname[spaces] = 0;
51 strcat(entryname, entry->d_name);
52
53 // Check for subdirectory
54 char subdirname[(1+strlen(currdir)+strlen(entry->d_name))];
55 strcpy(subdirname, currdir);
56 strncat(subdirname, &settings->fileSeparator, 1);
57 strcat(subdirname, entry->d_name);
58 if ((subdir = opendir(subdirname)) != NULL) {
59 printf("%-60s\n", entryname);
60 if (settings->recursive) {
61 lineSum += scanDirectory(subdir, spaces+1, subdirname, settings);
62 }
63 closedir(subdir);
64 continue;
65 }
66
67 // Count lines
68 lines = 0;
69 char filename[(1+strlen(currdir)+strlen(entry->d_name))];
70 strcpy(filename, currdir);
71 strncat(filename, &settings->fileSeparator, 1);
72 strcat(filename, entry->d_name);
73 if (testSuffix(filename, settings)) {
74 FILE *file = fopen(filename, "r");
75 if (file == NULL) {
76 perror(" File acces failed");
77 continue;
78 }
79
80 do {
81 a = fgetc(file);
82
83 if (a == 10) {
84 lines++;
85 }
86 } while (a != EOF);
87 fclose(file);
88
89 // Print line count
90 #ifdef _WIN32
91 printf("%-60s%13d lines\n", entryname, lines);
92 #else
93 printf("%-60s%14d lines\n", entryname, lines);
94 #endif /* _WIN32 */
95
96 lineSum += lines;
97 }
98 else {
99 if (!settings->matchesOnly) {
100 // Print hint
101 #ifdef _WIN32
102 printf("%-60s%19s\n", entryname, "no match");
103 #else
104 printf("%-60s%20s\n", entryname, "no match");
105 #endif /* _WIN32 */
106 }
107 }
108 }
109 }
110 return lineSum;
111 }

mercurial