functions.c

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

mercurial