Thu, 01 Dec 2011 17:04:30 +0100
Added line buffer (and warning message - there is no regexp parser, though)
10 | 1 | /* |
2 | * cline.c | |
3 | * | |
4 | * Created on: 23.05.2011 | |
20
43725438ac50
Changed author comments + added signatures for upcomming bfile heuristics
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
5 | * Author: Mike |
10 | 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" |
16
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
12 | #include "stream.h" |
0 | 13 | |
12
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
14 | void printHelpText() { |
0 | 15 | const char* helpText = |
1 | 16 | "\nUsage:" |
21
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
17 | "\n cline [-hrmvV][-s suffix][-b level][<directory>]" |
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
18 | "\n cline [-hrmvV][-S suffix][-b level][<directory>]" |
0 | 19 | "\n\nCounts the line terminator characters (\\n) within all" |
20 | " files in the specified\ndirectory." | |
21 | "\n\nOptions:" | |
21
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
22 | "\n -b <level> - binary file heuristics level (default medium)" |
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
23 | "\n One of: ignore low medium high" |
0 | 24 | "\n -h, --help - this help text" |
1 | 25 | "\n -m - print information about matching files only" |
26 | "\n -s <suffixes> - only count files with these suffixes (separated" | |
0 | 27 | "\n by commas)" |
1 | 28 | "\n -S <suffixes> - count any file except those with these suffixes" |
0 | 29 | "\n (separated by commas)" |
1 | 30 | "\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
|
31 | "\n -v, --version - print out version information" |
16
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
32 | "\n -V - turn verbose output off, print the result only" |
0 | 33 | "\n\n" |
1 | 34 | "The default call without any options is:" |
12
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
35 | "\n cline ./\n" |
7
1b55f3fa52c9
Fixed helpText and removed extra variables for default values
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
36 | "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
|
37 | "\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
|
38 | "\n cline -rs .c\n"; |
1 | 39 | |
12
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
40 | printf(helpText); |
1 | 41 | } |
42 | ||
14
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
43 | int exit_with_version(settings_t* settings) { |
16
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
44 | printf("cline - Revision: %s", VERSION); |
14
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
45 | 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
|
46 | return 0; |
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
47 | } |
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
48 | |
12
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
49 | int exit_with_help(settings_t* settings, int code) { |
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
50 | printHelpText(); |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
51 | destroy_settings_t(settings); |
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
52 | return code; |
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
53 | } |
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
54 | |
1 | 55 | int main(int argc, char** argv) { |
0 | 56 | |
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
57 | /* Settings */ |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
58 | 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
|
59 | if (settings == NULL) { |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
60 | 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
|
61 | return 1; |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
62 | } |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
63 | |
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
64 | /* Get arguments */ |
7
1b55f3fa52c9
Fixed helpText and removed extra variables for default values
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
65 | char* directory = "./"; |
1b55f3fa52c9
Fixed helpText and removed extra variables for default values
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
66 | char* suffix = " "; |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
67 | int checked = 0; |
0 | 68 | |
1 | 69 | for (int t = 1 ; t < argc ; t++) { |
70 | ||
21
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
71 | int argflags = checkArgument(argv[t], "hsSrRmvVb"); |
1 | 72 | |
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
73 | /* s, S */ |
14
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
74 | if ((argflags & 6) > 0) { |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
75 | if (registerArgument(&checked, 6)) { |
12
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
76 | return exit_with_help(settings, 1); |
0 | 77 | } |
14
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
78 | settings->includeSuffixes = (argflags & 2) > 0; |
1 | 79 | t++; |
80 | if (t >= argc) { | |
12
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
81 | return exit_with_help(settings, 1); |
1 | 82 | } |
83 | suffix = argv[t]; | |
0 | 84 | } |
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
85 | /* h */ |
1 | 86 | 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
|
87 | return exit_with_help(settings, 0); |
0 | 88 | } |
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
89 | /* r, R */ |
1 | 90 | if ((argflags & 24) > 0) { |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
91 | if (registerArgument(&checked, 24)) { |
12
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
92 | return exit_with_help(settings, 1); |
0 | 93 | } |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
94 | settings->recursive = true; |
0 | 95 | } |
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
96 | /* m */ |
1 | 97 | if ((argflags & 32) > 0) { |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
98 | if (registerArgument(&checked, 32)) { |
12
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
99 | return exit_with_help(settings, 1); |
0 | 100 | } |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
101 | settings->matchesOnly = true; |
0 | 102 | } |
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
103 | /* v */ |
14
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
104 | 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
|
105 | 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
|
106 | } |
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
107 | /* V */ |
16
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
108 | if ((argflags & 128) > 0) { |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
109 | if (registerArgument(&checked, 128)) { |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
110 | return exit_with_help(settings, 1); |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
111 | } |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
112 | settings->verbose = false; |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
113 | } |
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
114 | /* b */ |
21
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
115 | if ((argflags & 256) > 0) { |
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
116 | if (registerArgument(&checked, 256)) { |
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
117 | return exit_with_help(settings, 1); |
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
118 | } |
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
119 | t++; |
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
120 | if (t >= argc) { |
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
121 | return exit_with_help(settings, 1); |
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
122 | } |
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
123 | if (stricmp(argv[t], "ignore") == 0) { |
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
124 | settings->bfileHeuristics->level = BFILE_IGNORE; |
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
125 | } else if (stricmp(argv[t], "low") == 0) { |
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
126 | settings->bfileHeuristics->level = BFILE_LOW_ACCURACY; |
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
127 | } else if (stricmp(argv[t], "medium") == 0) { |
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
128 | settings->bfileHeuristics->level = BFILE_MEDIUM_ACCURACY; |
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
129 | } else if (stricmp(argv[t], "high") == 0) { |
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
130 | settings->bfileHeuristics->level = BFILE_HIGH_ACCURACY; |
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
131 | } else { |
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
132 | return exit_with_help(settings, 1); |
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
133 | } |
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
134 | } |
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
135 | /* Path */ |
1 | 136 | if (argflags == 0) { |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
137 | if (registerArgument(&checked, 1024)) { |
12
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
138 | return exit_with_help(settings, 1); |
0 | 139 | } |
140 | directory = argv[t]; | |
141 | } | |
142 | } | |
143 | ||
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
144 | /* Configure output */ |
16
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
145 | if (!settings->verbose) { |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
146 | close_stdout(); |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
147 | } |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
148 | |
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
149 | /* Find tokens */ |
6
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
150 | char* finder = strtok(suffix, ","); |
1 | 151 | while (finder != NULL) { |
19
8bac9fd0629d
generalized suffix_list to string_list
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
152 | add_string(settings->suffixList, finder); |
0 | 153 | finder = strtok(NULL, ","); |
154 | } | |
155 | ||
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
156 | /* Scan directory */ |
23
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
157 | int lines = scanDirectory((scanner_t){directory, 0}, settings); |
7
1b55f3fa52c9
Fixed helpText and removed extra variables for default values
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
158 | destroy_settings_t(settings); |
0 | 159 | |
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
160 | /* Print double line and line count */ |
21
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
161 | for (int t = 0 ; t < 79 ; t++) { |
0 | 162 | printf("="); |
163 | } | |
21
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
164 | printf("\n%73d lines\n", lines); |
0 | 165 | |
25
802c5382f499
Added line buffer (and warning message - there is no regexp parser, though)
Mike Becker <universe@uap-core.de>
parents:
23
diff
changeset
|
166 | if (settings->confusing_lnlen) { |
802c5382f499
Added line buffer (and warning message - there is no regexp parser, though)
Mike Becker <universe@uap-core.de>
parents:
23
diff
changeset
|
167 | /* TODO: display this only when the regexp parser is used */ |
802c5382f499
Added line buffer (and warning message - there is no regexp parser, though)
Mike Becker <universe@uap-core.de>
parents:
23
diff
changeset
|
168 | printf("\nSome files contain too long lines.\n" |
802c5382f499
Added line buffer (and warning message - there is no regexp parser, though)
Mike Becker <universe@uap-core.de>
parents:
23
diff
changeset
|
169 | "The regexp parser currently supports a maximum line length of 2048." |
802c5382f499
Added line buffer (and warning message - there is no regexp parser, though)
Mike Becker <universe@uap-core.de>
parents:
23
diff
changeset
|
170 | "\nThe result might be wrong.\n"); |
802c5382f499
Added line buffer (and warning message - there is no regexp parser, though)
Mike Becker <universe@uap-core.de>
parents:
23
diff
changeset
|
171 | } |
802c5382f499
Added line buffer (and warning message - there is no regexp parser, though)
Mike Becker <universe@uap-core.de>
parents:
23
diff
changeset
|
172 | |
16
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
173 | if (!settings->verbose) { |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
174 | reopen_stdout(); |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
175 | printf("%d", lines); |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
176 | } |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
177 | |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
178 | fflush(stdout); |
0 | 179 | return 0; |
180 | } |