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