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