cline.c

changeset 16
bc9a0fefd892
parent 14
ee9333c91dda
child 19
8bac9fd0629d
equal deleted inserted replaced
15:9a262e046ab8 16:bc9a0fefd892
7 7
8 #include "cline.h" 8 #include "cline.h"
9 #include "scanner.h" 9 #include "scanner.h"
10 #include "settings.h" 10 #include "settings.h"
11 #include "arguments.h" 11 #include "arguments.h"
12 #include "stream.h"
12 13
13 void printHelpText() { 14 void printHelpText() {
14 const char* helpText = 15 const char* helpText =
15 "\nUsage:" 16 "\nUsage:"
16 "\n cline [-hrm][-s suffix][<directory>]" 17 "\n cline [-hrm][-s suffix][<directory>]"
24 "\n by commas)" 25 "\n by commas)"
25 "\n -S <suffixes> - count any file except those with these suffixes" 26 "\n -S <suffixes> - count any file except those with these suffixes"
26 "\n (separated by commas)" 27 "\n (separated by commas)"
27 "\n -r, -R - includes subdirectories" 28 "\n -r, -R - includes subdirectories"
28 "\n -v, --version - print out version information" 29 "\n -v, --version - print out version information"
30 "\n -V - turn verbose output off, print the result only"
29 "\n\n" 31 "\n\n"
30 "The default call without any options is:" 32 "The default call without any options is:"
31 "\n cline ./\n" 33 "\n cline ./\n"
32 "So each file in the working directory is counted. If you want to count C" 34 "So each file in the working directory is counted. If you want to count C"
33 "\nsource code in your working directory and its subdirectories, type:" 35 "\nsource code in your working directory and its subdirectories, type:"
35 37
36 printf(helpText); 38 printf(helpText);
37 } 39 }
38 40
39 int exit_with_version(settings_t* settings) { 41 int exit_with_version(settings_t* settings) {
40 printf("cline - Version: %s", VERSION); 42 printf("cline - Revision: %s", VERSION);
41 destroy_settings_t(settings); 43 destroy_settings_t(settings);
42 return 0; 44 return 0;
43 } 45 }
44 46
45 int exit_with_help(settings_t* settings, int code) { 47 int exit_with_help(settings_t* settings, int code) {
62 char* suffix = " "; 64 char* suffix = " ";
63 int checked = 0; 65 int checked = 0;
64 66
65 for (int t = 1 ; t < argc ; t++) { 67 for (int t = 1 ; t < argc ; t++) {
66 68
67 int argflags = checkArgument(argv[t], "hsSrRmv"); 69 int argflags = checkArgument(argv[t], "hsSrRmvV");
68 70
69 // s, S 71 // s, S
70 if ((argflags & 6) > 0) { 72 if ((argflags & 6) > 0) {
71 if (registerArgument(&checked, 6)) { 73 if (registerArgument(&checked, 6)) {
72 return exit_with_help(settings, 1); 74 return exit_with_help(settings, 1);
98 } 100 }
99 // v 101 // v
100 if ((argflags & 64) > 0 || strcmp(argv[t], "--version") == 0) { 102 if ((argflags & 64) > 0 || strcmp(argv[t], "--version") == 0) {
101 return exit_with_version(settings); 103 return exit_with_version(settings);
102 } 104 }
105 // V
106 if ((argflags & 128) > 0) {
107 if (registerArgument(&checked, 128)) {
108 return exit_with_help(settings, 1);
109 }
110 settings->verbose = false;
111 }
103 // Path 112 // Path
104 if (argflags == 0) { 113 if (argflags == 0) {
105 if (registerArgument(&checked, 1024)) { 114 if (registerArgument(&checked, 1024)) {
106 return exit_with_help(settings, 1); 115 return exit_with_help(settings, 1);
107 } 116 }
108 directory = argv[t]; 117 directory = argv[t];
109 } 118 }
119 }
120
121 // Configure output
122 if (!settings->verbose) {
123 close_stdout();
110 } 124 }
111 125
112 // Find tokens 126 // Find tokens
113 char* finder = strtok(suffix, ","); 127 char* finder = strtok(suffix, ",");
114 while (finder != NULL) { 128 while (finder != NULL) {
128 int lines = scanDirectory(dir, 0, directory, settings); 142 int lines = scanDirectory(dir, 0, directory, settings);
129 closedir(dir); 143 closedir(dir);
130 destroy_settings_t(settings); 144 destroy_settings_t(settings);
131 145
132 // Print double line and line count 146 // Print double line and line count
133 #ifdef _WIN32 147 #ifdef _WIN32
134 const int columns = 79; 148 const int columns = 79;
135 #else 149 #else
136 const int columns = 80; 150 const int columns = 80;
137 #endif /* _WIN32 */ 151 #endif /* _WIN32 */
138 152
139 for (int t = 0 ; t < columns ; t++) { 153 for (int t = 0 ; t < columns ; t++) {
140 printf("="); 154 printf("=");
141 } 155 }
142 #ifdef _WIN32 156 #ifdef _WIN32
143 printf("\n%73d lines\n", lines); 157 printf("\n%73d lines\n", lines);
144 #else 158 #else
145 printf("\n%74d lines\n", lines); 159 printf("\n%74d lines\n", lines);
146 #endif /* _WIN32 */ 160 #endif /* _WIN32 */
147 161
162 if (!settings->verbose) {
163 reopen_stdout();
164 printf("%d", lines);
165 }
166
167 fflush(stdout);
148 return 0; 168 return 0;
149 } 169 }

mercurial