cline.c

Thu, 15 Sep 2011 13:38:03 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 15 Sep 2011 13:38:03 +0200
changeset 11
06cbd0ec003d
parent 10
ecf787666f44
parent 9
1dd63a32ffc4
child 12
902cb8d2053c
permissions
-rw-r--r--

Merge with 1dd63a32ffc486b71399045cf8443ec6ac9d7bf5

10
ecf787666f44 refactored sources
Mike Becker <universe@uap-core.de>
parents: 8
diff changeset
1 /*
ecf787666f44 refactored sources
Mike Becker <universe@uap-core.de>
parents: 8
diff changeset
2 * cline.c
ecf787666f44 refactored sources
Mike Becker <universe@uap-core.de>
parents: 8
diff changeset
3 *
ecf787666f44 refactored sources
Mike Becker <universe@uap-core.de>
parents: 8
diff changeset
4 * Created on: 23.05.2011
ecf787666f44 refactored sources
Mike Becker <universe@uap-core.de>
parents: 8
diff changeset
5 * Author: beckermi
ecf787666f44 refactored sources
Mike Becker <universe@uap-core.de>
parents: 8
diff changeset
6 */
6
be923400164c encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
7
10
ecf787666f44 refactored sources
Mike Becker <universe@uap-core.de>
parents: 8
diff changeset
8 #include "cline.h"
ecf787666f44 refactored sources
Mike Becker <universe@uap-core.de>
parents: 8
diff changeset
9 #include "scanner.h"
ecf787666f44 refactored sources
Mike Becker <universe@uap-core.de>
parents: 8
diff changeset
10 #include "settings.h"
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
12 void printHelpText(const char* prgName) {
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 // Help text
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 const char* helpText =
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
15 "\nUsage:"
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
16 "\n %s [-hrm][-s suffix][<directory>]"
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
17 "\n %s [-hrm][-S suffix][<directory>]"
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 "\n\nCounts the line terminator characters (\\n) within all"
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 " files in the specified\ndirectory."
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 "\n\nOptions:"
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 "\n -h, --help - this help text"
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
22 "\n -m - print information about matching files only"
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
23 "\n -s <suffixes> - only count files with these suffixes (separated"
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 "\n by commas)"
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
25 "\n -S <suffixes> - count any file except those with these suffixes"
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 "\n (separated by commas)"
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
27 "\n -r, -R - includes subdirectories"
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 "\n\n"
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
29 "The default call without any options is:"
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
30 "\n %s ./\n"
7
1b55f3fa52c9 Fixed helpText and removed extra variables for default values
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
31 "So each file in the working directory is counted. If you want to count C"
1b55f3fa52c9 Fixed helpText and removed extra variables for default values
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
32 "\nsource code in your working directory and its subdirectories, type:"
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
33 "\n %s -rs .c\n";
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
34
3
510d6b198dde Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
35 printf(helpText, prgName, prgName, prgName, prgName);
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
36 }
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
37
8
28319b20968c encapsulated common operations
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
38 int exit_with_help(char* prgName, settings_t* settings, int code) {
28319b20968c encapsulated common operations
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
39 printHelpText(prgName);
28319b20968c encapsulated common operations
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
40 destroy_settings_t(settings);
28319b20968c encapsulated common operations
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
41 return code;
28319b20968c encapsulated common operations
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
42 }
28319b20968c encapsulated common operations
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
43
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
44 int main(int argc, char** argv) {
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45
3
510d6b198dde Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
46 // Settings
510d6b198dde Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
47 settings_t *settings = new_settings_t();
5
9393eff3d2f9 Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
48 if (settings == NULL) {
9393eff3d2f9 Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
49 fprintf(stderr, "Memory allocation failed.\n");
9393eff3d2f9 Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
50 return 1;
9393eff3d2f9 Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
51 }
3
510d6b198dde Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
52
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 // Program name
3
510d6b198dde Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
54 char* prgName = strrchr(argv[0], settings->fileSeparator);
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
55
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
56 if (prgName == NULL) {
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 prgName = argv[0];
3
510d6b198dde Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
58 } else {
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 prgName++;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 // Get arguments
7
1b55f3fa52c9 Fixed helpText and removed extra variables for default values
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
63 char* directory = "./";
1b55f3fa52c9 Fixed helpText and removed extra variables for default values
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
64 char* suffix = " ";
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 bool showHelp = false;
8
28319b20968c encapsulated common operations
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
66 int checked = 0;
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
68 for (int t = 1 ; t < argc ; t++) {
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
69
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
70 int argflags = checkArgument(argv[t], "hsSrRm");
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
71
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
72 // s
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
73 if ((argflags & 2) > 0) {
8
28319b20968c encapsulated common operations
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
74 if (registerArgument(&checked, 6)) {
28319b20968c encapsulated common operations
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
75 return exit_with_help(prgName, settings, 1);
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 }
3
510d6b198dde Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
77 settings->includeSuffixes = true;
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
78 t++;
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
79 if (t >= argc) {
8
28319b20968c encapsulated common operations
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
80 return exit_with_help(prgName, settings, 1);
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
81 }
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
82 suffix = argv[t];
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 }
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
84 // S
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
85 if ((argflags & 4) > 0) {
8
28319b20968c encapsulated common operations
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
86 if (registerArgument(&checked, 6)) {
28319b20968c encapsulated common operations
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
87 return exit_with_help(prgName, settings, 1);
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 }
3
510d6b198dde Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
89 settings->includeSuffixes = false;
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
90 t++;
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
91 if (t >= argc) {
8
28319b20968c encapsulated common operations
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
92 return exit_with_help(prgName, settings, 1);
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
93 }
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
94 suffix = argv[t];
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 }
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
96 // h
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
97 if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) {
10
ecf787666f44 refactored sources
Mike Becker <universe@uap-core.de>
parents: 8
diff changeset
98 return exit_with_help(prgName, settings, 1);
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 }
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
100 // r, R
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
101 if ((argflags & 24) > 0) {
8
28319b20968c encapsulated common operations
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
102 if (registerArgument(&checked, 24)) {
28319b20968c encapsulated common operations
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
103 return exit_with_help(prgName, settings, 1);
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 }
3
510d6b198dde Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
105 settings->recursive = true;
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 }
8
28319b20968c encapsulated common operations
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
107 // m
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
108 if ((argflags & 32) > 0) {
8
28319b20968c encapsulated common operations
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
109 if (registerArgument(&checked, 32)) {
28319b20968c encapsulated common operations
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
110 return exit_with_help(prgName, settings, 1);
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 }
3
510d6b198dde Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
112 settings->matchesOnly = true;
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 }
6
be923400164c encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
114 // Path
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
115 if (argflags == 0) {
8
28319b20968c encapsulated common operations
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
116 if (registerArgument(&checked, 1024)) {
28319b20968c encapsulated common operations
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
117 return exit_with_help(prgName, settings, 1);
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 directory = argv[t];
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 // Find tokens
6
be923400164c encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
124 char* finder = strtok(suffix, ",");
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
125 while (finder != NULL) {
6
be923400164c encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
126 add_suffix(settings->suffixList, finder);
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 finder = strtok(NULL, ",");
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 // Open directory
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 DIR *dir = opendir(directory);
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
132 if (dir == NULL) {
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 perror("Operation failed");
4
c3acfb3b4957 Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
134 destroy_settings_t(settings);
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 return 1;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 }
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 // Scan directory
3
510d6b198dde Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
139 int lines = scanDirectory(dir, 0, directory, settings);
4
c3acfb3b4957 Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
140 closedir(dir);
7
1b55f3fa52c9 Fixed helpText and removed extra variables for default values
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
141 destroy_settings_t(settings);
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 // Print double line and line count
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
144 #ifdef _WIN32
3
510d6b198dde Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
145 const int columns = 79;
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
146 #else
3
510d6b198dde Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
147 const int columns = 80;
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
148 #endif /* _WIN32 */
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
149
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
150 for (int t = 0 ; t < columns ; t++) {
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 printf("=");
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152 }
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
153 #ifdef _WIN32
3
510d6b198dde Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
154 printf("\n%73d lines\n", lines);
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
155 #else
3
510d6b198dde Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
156 printf("\n%74d lines\n", lines);
1
34a5e235d16e cline version 2
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
157 #endif /* _WIN32 */
0
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159 return 0;
518bfd1cc1e8 cline project init
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 }

mercurial