Fri, 21 Oct 2011 15:09:26 +0200
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
1 /*
2 * cline.c
3 *
4 * Created on: 23.05.2011
5 * Author: Mike
6 */
8 #include "cline.h"
9 #include "scanner.h"
10 #include "settings.h"
11 #include "arguments.h"
12 #include "stream.h"
14 void printHelpText() {
15 const char* helpText =
16 "\nUsage:"
17 "\n cline [-hrmvV][-s suffix][-b level][<directory>]"
18 "\n cline [-hrmvV][-S suffix][-b level][<directory>]"
19 "\n\nCounts the line terminator characters (\\n) within all"
20 " files in the specified\ndirectory."
21 "\n\nOptions:"
22 "\n -b <level> - binary file heuristics level (default medium)"
23 "\n One of: ignore low medium high"
24 "\n -h, --help - this help text"
25 "\n -m - print information about matching files only"
26 "\n -s <suffixes> - only count files with these suffixes (separated"
27 "\n by commas)"
28 "\n -S <suffixes> - count any file except those with these suffixes"
29 "\n (separated by commas)"
30 "\n -r, -R - includes subdirectories"
31 "\n -v, --version - print out version information"
32 "\n -V - turn verbose output off, print the result only"
33 "\n\n"
34 "The default call without any options is:"
35 "\n cline ./\n"
36 "So each file in the working directory is counted. If you want to count C"
37 "\nsource code in your working directory and its subdirectories, type:"
38 "\n cline -rs .c\n";
40 printf(helpText);
41 }
43 int exit_with_version(settings_t* settings) {
44 printf("cline - Revision: %s", VERSION);
45 destroy_settings_t(settings);
46 return 0;
47 }
49 int exit_with_help(settings_t* settings, int code) {
50 printHelpText();
51 destroy_settings_t(settings);
52 return code;
53 }
55 int main(int argc, char** argv) {
57 /* Settings */
58 settings_t *settings = new_settings_t();
59 if (settings == NULL) {
60 fprintf(stderr, "Memory allocation failed.\n");
61 return 1;
62 }
64 /* Get arguments */
65 char* directory = "./";
66 char* suffix = " ";
67 int checked = 0;
69 for (int t = 1 ; t < argc ; t++) {
71 int argflags = checkArgument(argv[t], "hsSrRmvVb");
73 /* s, S */
74 if ((argflags & 6) > 0) {
75 if (registerArgument(&checked, 6)) {
76 return exit_with_help(settings, 1);
77 }
78 settings->includeSuffixes = (argflags & 2) > 0;
79 t++;
80 if (t >= argc) {
81 return exit_with_help(settings, 1);
82 }
83 suffix = argv[t];
84 }
85 /* h */
86 if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) {
87 return exit_with_help(settings, 0);
88 }
89 /* r, R */
90 if ((argflags & 24) > 0) {
91 if (registerArgument(&checked, 24)) {
92 return exit_with_help(settings, 1);
93 }
94 settings->recursive = true;
95 }
96 /* m */
97 if ((argflags & 32) > 0) {
98 if (registerArgument(&checked, 32)) {
99 return exit_with_help(settings, 1);
100 }
101 settings->matchesOnly = true;
102 }
103 /* v */
104 if ((argflags & 64) > 0 || strcmp(argv[t], "--version") == 0) {
105 return exit_with_version(settings);
106 }
107 /* V */
108 if ((argflags & 128) > 0) {
109 if (registerArgument(&checked, 128)) {
110 return exit_with_help(settings, 1);
111 }
112 settings->verbose = false;
113 }
114 /* b */
115 if ((argflags & 256) > 0) {
116 if (registerArgument(&checked, 256)) {
117 return exit_with_help(settings, 1);
118 }
119 t++;
120 if (t >= argc) {
121 return exit_with_help(settings, 1);
122 }
123 if (stricmp(argv[t], "ignore") == 0) {
124 settings->bfileHeuristics->level = BFILE_IGNORE;
125 } else if (stricmp(argv[t], "low") == 0) {
126 settings->bfileHeuristics->level = BFILE_LOW_ACCURACY;
127 } else if (stricmp(argv[t], "medium") == 0) {
128 settings->bfileHeuristics->level = BFILE_MEDIUM_ACCURACY;
129 } else if (stricmp(argv[t], "high") == 0) {
130 settings->bfileHeuristics->level = BFILE_HIGH_ACCURACY;
131 } else {
132 return exit_with_help(settings, 1);
133 }
134 }
135 /* Path */
136 if (argflags == 0) {
137 if (registerArgument(&checked, 1024)) {
138 return exit_with_help(settings, 1);
139 }
140 directory = argv[t];
141 }
142 }
144 /* Configure output */
145 if (!settings->verbose) {
146 close_stdout();
147 }
149 /* Find tokens */
150 char* finder = strtok(suffix, ",");
151 while (finder != NULL) {
152 add_string(settings->suffixList, finder);
153 finder = strtok(NULL, ",");
154 }
156 /* Scan directory */
157 int lines = scanDirectory((scanner_t){directory, 0}, settings);
158 destroy_settings_t(settings);
160 /* Print double line and line count */
161 for (int t = 0 ; t < 79 ; t++) {
162 printf("=");
163 }
164 printf("\n%73d lines\n", lines);
166 if (!settings->verbose) {
167 reopen_stdout();
168 printf("%d", lines);
169 }
171 fflush(stdout);
172 return 0;
173 }