Fri, 27 May 2011 12:49:33 +0200
Fixed missing return statement in new_settings_t.
Added support for c++ compilers.
Added destroy_settings_t function
1 #include "cline.h"
2 #include "functions.h"
4 settings_t* new_settings_t() {
5 settings_t *settings = malloc(sizeof(settings_t*));
6 #ifdef _WIN32
7 settings->fileSeparator = '\\';
8 #else
9 settings->fileSeparator = '/';
10 #endif /* _WIN32 */
11 settings->suffixc = 1;
12 settings->recursive = false;
13 settings->includeSuffixes = false;
14 settings->matchesOnly = false;
16 return settings;
17 }
19 void destroy_settings_t(settings_t* settings) {
20 if (settings->suffixv != NULL) {
21 free(settings->suffixv);
22 }
23 free(settings);
24 }
26 void printHelpText(const char* prgName) {
27 // Help text
28 const char* helpText =
29 "\nUsage:"
30 "\n %s [-hrm][-s suffix][<directory>]"
31 "\n %s [-hrm][-S suffix][<directory>]"
32 "\n\nCounts the line terminator characters (\\n) within all"
33 " files in the specified\ndirectory."
34 "\n\nOptions:"
35 "\n -h, --help - this help text"
36 "\n -m - print information about matching files only"
37 "\n -s <suffixes> - only count files with these suffixes (separated"
38 "\n by commas)"
39 "\n -S <suffixes> - count any file except those with these suffixes"
40 "\n (separated by commas)"
41 "\n -r, -R - includes subdirectories"
42 "\n\n"
43 "The default call without any options is:"
44 "\n %s ./\n"
45 "That means each file in each subdirectory is counted. If you want to count"
46 "\nC source code in your working directory and its subdirectories, type:"
47 "\n %s -rs .c\n";
49 printf(helpText, prgName, prgName, prgName, prgName);
50 }
52 int main(int argc, char** argv) {
54 // Settings
55 settings_t *settings = new_settings_t();
57 // Program name
58 char* prgName = strrchr(argv[0], settings->fileSeparator);
60 if (prgName == NULL) {
61 prgName = argv[0];
62 } else {
63 prgName++;
64 }
66 // Defaults
67 char* _suffix = " ";
68 char _directory[3];
69 _directory[0] = '.';
70 _directory[1] = settings->fileSeparator;
71 _directory[2] = 0;
73 // Get arguments
74 char* directory;
75 char* suffix;
76 bool showHelp = false;
77 char checked = 0;
79 for (int t = 1 ; t < argc ; t++) {
81 int argflags = checkArgument(argv[t], "hsSrRm");
83 // s
84 if ((argflags & 2) > 0) {
85 if ((checked & 1) > 0) {
86 printHelpText(prgName);
87 return -1;
88 }
89 settings->includeSuffixes = true;
90 t++;
91 if (t >= argc) {
92 printHelpText(prgName);
93 return -1;
94 }
95 suffix = argv[t];
96 checked |= 1;
97 }
98 // S
99 if ((argflags & 4) > 0) {
100 if ((checked & 1) > 0) {
101 printHelpText(prgName);
102 return -1;
103 }
104 settings->includeSuffixes = false;
105 t++;
106 if (t >= argc) {
107 printHelpText(prgName);
108 return -1;
109 }
110 suffix = argv[t];
111 checked |= 1;
112 }
113 // h
114 if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) {
115 if ((checked & 2) > 0) {
116 printHelpText(prgName);
117 return -1;
118 }
119 checked |= 2;
120 showHelp = true;
121 }
122 // r, R
123 if ((argflags & 24) > 0) {
124 if ((checked & 4) > 0) {
125 printHelpText(prgName);
126 return -1;
127 }
128 checked |= 4;
129 settings->recursive = true;
130 }
131 if ((argflags & 32) > 0) {
132 if ((checked & 32) > 0) {
133 printHelpText(prgName);
134 return -1;
135 }
136 checked |= 32;
137 settings->matchesOnly = true;
138 }
139 // other
140 if (argflags == 0) {
141 if ((checked & 8) > 0) {
142 printHelpText(prgName);
143 return -1;
144 }
145 checked |= 8;
146 directory = argv[t];
147 }
148 }
150 // Show help and quit
151 if (showHelp) {
152 printHelpText(prgName);
153 return 0;
154 }
156 // Default values
157 if ((checked & 1) == 0) {
158 suffix = _suffix;
159 }
161 if ((checked & 8) == 0) {
162 directory = _directory;
163 }
165 // Find tokens
166 char* finder;
167 finder = strchr(suffix, ',');
168 while (finder != NULL) {
169 settings->suffixc++;
170 finder = strchr(finder+1, ',');
171 }
172 settings->suffixv = (char**) malloc(sizeof(char**)*settings->suffixc);
173 if (settings->suffixv == NULL) {
174 fprintf(stderr, "Memory allocation failed.\n");
175 return 1;
176 }
177 finder = strtok(suffix, ",");
178 int c = 0;
179 while (finder != NULL) {
180 settings->suffixv[c] = finder;
181 c++;
182 finder = strtok(NULL, ",");
183 }
185 // Open directory
186 DIR *dir = opendir(directory);
187 if (dir == NULL) {
188 perror("Operation failed");
189 destroy_settings_t(settings);
190 return 1;
191 }
193 // Scan directory
194 int lines = scanDirectory(dir, 0, directory, settings);
195 closedir(dir);
197 // Print double line and line count
198 #ifdef _WIN32
199 const int columns = 79;
200 #else
201 const int columns = 80;
202 #endif /* _WIN32 */
204 for (int t = 0 ; t < columns ; t++) {
205 printf("=");
206 }
207 #ifdef _WIN32
208 printf("\n%73d lines\n", lines);
209 #else
210 printf("\n%74d lines\n", lines);
211 #endif /* _WIN32 */
213 destroy_settings_t(settings);
214 return 0;
215 }