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