cline.c

changeset 12
902cb8d2053c
parent 11
06cbd0ec003d
child 14
ee9333c91dda
equal deleted inserted replaced
11:06cbd0ec003d 12:902cb8d2053c
6 */ 6 */
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 12
12 void printHelpText(const char* prgName) { 13 void printHelpText() {
13 // Help text 14 // Help text
14 const char* helpText = 15 const char* helpText =
15 "\nUsage:" 16 "\nUsage:"
16 "\n %s [-hrm][-s suffix][<directory>]" 17 "\n cline [-hrm][-s suffix][<directory>]"
17 "\n %s [-hrm][-S suffix][<directory>]" 18 "\n cline [-hrm][-S suffix][<directory>]"
18 "\n\nCounts the line terminator characters (\\n) within all" 19 "\n\nCounts the line terminator characters (\\n) within all"
19 " files in the specified\ndirectory." 20 " files in the specified\ndirectory."
20 "\n\nOptions:" 21 "\n\nOptions:"
21 "\n -h, --help - this help text" 22 "\n -h, --help - this help text"
22 "\n -m - print information about matching files only" 23 "\n -m - print information about matching files only"
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\n" 29 "\n\n"
29 "The default call without any options is:" 30 "The default call without any options is:"
30 "\n %s ./\n" 31 "\n cline ./\n"
31 "So each file in the working directory is counted. If you want to count C" 32 "So each file in the working directory is counted. If you want to count C"
32 "\nsource code in your working directory and its subdirectories, type:" 33 "\nsource code in your working directory and its subdirectories, type:"
33 "\n %s -rs .c\n"; 34 "\n cline -rs .c\n";
34 35
35 printf(helpText, prgName, prgName, prgName, prgName); 36 printf(helpText);
36 } 37 }
37 38
38 int exit_with_help(char* prgName, settings_t* settings, int code) { 39 int exit_with_help(settings_t* settings, int code) {
39 printHelpText(prgName); 40 printHelpText();
40 destroy_settings_t(settings); 41 destroy_settings_t(settings);
41 return code; 42 return code;
42 } 43 }
43 44
44 int main(int argc, char** argv) { 45 int main(int argc, char** argv) {
46 // Settings 47 // Settings
47 settings_t *settings = new_settings_t(); 48 settings_t *settings = new_settings_t();
48 if (settings == NULL) { 49 if (settings == NULL) {
49 fprintf(stderr, "Memory allocation failed.\n"); 50 fprintf(stderr, "Memory allocation failed.\n");
50 return 1; 51 return 1;
51 }
52
53 // Program name
54 char* prgName = strrchr(argv[0], settings->fileSeparator);
55
56 if (prgName == NULL) {
57 prgName = argv[0];
58 } else {
59 prgName++;
60 } 52 }
61 53
62 // Get arguments 54 // Get arguments
63 char* directory = "./"; 55 char* directory = "./";
64 char* suffix = " "; 56 char* suffix = " ";
70 int argflags = checkArgument(argv[t], "hsSrRm"); 62 int argflags = checkArgument(argv[t], "hsSrRm");
71 63
72 // s 64 // s
73 if ((argflags & 2) > 0) { 65 if ((argflags & 2) > 0) {
74 if (registerArgument(&checked, 6)) { 66 if (registerArgument(&checked, 6)) {
75 return exit_with_help(prgName, settings, 1); 67 return exit_with_help(settings, 1);
76 } 68 }
77 settings->includeSuffixes = true; 69 settings->includeSuffixes = true;
78 t++; 70 t++;
79 if (t >= argc) { 71 if (t >= argc) {
80 return exit_with_help(prgName, settings, 1); 72 return exit_with_help(settings, 1);
81 } 73 }
82 suffix = argv[t]; 74 suffix = argv[t];
83 } 75 }
84 // S 76 // S
85 if ((argflags & 4) > 0) { 77 if ((argflags & 4) > 0) {
86 if (registerArgument(&checked, 6)) { 78 if (registerArgument(&checked, 6)) {
87 return exit_with_help(prgName, settings, 1); 79 return exit_with_help(settings, 1);
88 } 80 }
89 settings->includeSuffixes = false; 81 settings->includeSuffixes = false;
90 t++; 82 t++;
91 if (t >= argc) { 83 if (t >= argc) {
92 return exit_with_help(prgName, settings, 1); 84 return exit_with_help(settings, 1);
93 } 85 }
94 suffix = argv[t]; 86 suffix = argv[t];
95 } 87 }
96 // h 88 // h
97 if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { 89 if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) {
98 return exit_with_help(prgName, settings, 1); 90 return exit_with_help(settings, 1);
99 } 91 }
100 // r, R 92 // r, R
101 if ((argflags & 24) > 0) { 93 if ((argflags & 24) > 0) {
102 if (registerArgument(&checked, 24)) { 94 if (registerArgument(&checked, 24)) {
103 return exit_with_help(prgName, settings, 1); 95 return exit_with_help(settings, 1);
104 } 96 }
105 settings->recursive = true; 97 settings->recursive = true;
106 } 98 }
107 // m 99 // m
108 if ((argflags & 32) > 0) { 100 if ((argflags & 32) > 0) {
109 if (registerArgument(&checked, 32)) { 101 if (registerArgument(&checked, 32)) {
110 return exit_with_help(prgName, settings, 1); 102 return exit_with_help(settings, 1);
111 } 103 }
112 settings->matchesOnly = true; 104 settings->matchesOnly = true;
113 } 105 }
114 // Path 106 // Path
115 if (argflags == 0) { 107 if (argflags == 0) {
116 if (registerArgument(&checked, 1024)) { 108 if (registerArgument(&checked, 1024)) {
117 return exit_with_help(prgName, settings, 1); 109 return exit_with_help(settings, 1);
118 } 110 }
119 directory = argv[t]; 111 directory = argv[t];
120 } 112 }
121 } 113 }
122 114

mercurial