Tue, 23 Aug 2016 15:55:02 +0200
refactors highlighter_t and removes abstraction overhead
23 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
35 | 4 | * Copyright 2016 Mike Becker. All rights reserved. |
23 | 5 | * |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | * | |
28 | */ | |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
29 | #include "c2html.h" |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
30 | |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
31 | #include "ucx/buffer.h" |
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
32 | #include "ucx/list.h" |
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
33 | #include "ucx/utils.h" |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
34 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
35 | void printhelp() { |
19 | 36 | printf("Formats source code using HTML.\n\nUsage:\n" |
37 | " c2html [Options] FILE\n\n" | |
38 | " Options:\n" | |
39 | " -h Prints this help message\n" | |
40 | " -j Highlight Java instead of C source code\n" | |
41 | " -o <output> Output file (stdout, if not specified)\n" | |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
42 | " -H <header> Prepend header file\n" |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
43 | " -F <footer> Append footer file\n" |
19 | 44 | " -p Disable highlighting (plain text)\n" |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
45 | " -l Disable line numbers\n" |
37
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
46 | " -V, -v Prints version and exits\n" |
19 | 47 | "\n"); |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
48 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
49 | |
45
1f3835182aeb
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
Mike Becker <universe@uap-core.de>
parents:
44
diff
changeset
|
50 | /* TODO: remove this workaround after refactoring highlighter structure */ |
46
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
51 | static void plainparseline(char *src, UcxBuffer *dest, HighlighterData* hltr) { |
45
1f3835182aeb
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
Mike Becker <universe@uap-core.de>
parents:
44
diff
changeset
|
52 | size_t dp = 0; |
1f3835182aeb
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
Mike Becker <universe@uap-core.de>
parents:
44
diff
changeset
|
53 | char *buf = dest->space + dest->pos; |
1f3835182aeb
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
Mike Becker <universe@uap-core.de>
parents:
44
diff
changeset
|
54 | while (*src && *src != '\n') { |
1f3835182aeb
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
Mike Becker <universe@uap-core.de>
parents:
44
diff
changeset
|
55 | dp = writeescapedchar(buf, dp, *src); |
1f3835182aeb
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
Mike Becker <universe@uap-core.de>
parents:
44
diff
changeset
|
56 | src++; |
1f3835182aeb
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
Mike Becker <universe@uap-core.de>
parents:
44
diff
changeset
|
57 | } |
1f3835182aeb
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
Mike Becker <universe@uap-core.de>
parents:
44
diff
changeset
|
58 | buf[dp++] = '\n'; |
1f3835182aeb
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
Mike Becker <universe@uap-core.de>
parents:
44
diff
changeset
|
59 | buf[dp] = '\0'; |
1f3835182aeb
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
Mike Becker <universe@uap-core.de>
parents:
44
diff
changeset
|
60 | dest->pos += dp; |
1f3835182aeb
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
Mike Becker <universe@uap-core.de>
parents:
44
diff
changeset
|
61 | dest->size += dp; |
1f3835182aeb
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
Mike Becker <universe@uap-core.de>
parents:
44
diff
changeset
|
62 | } |
1f3835182aeb
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
Mike Becker <universe@uap-core.de>
parents:
44
diff
changeset
|
63 | |
46
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
64 | int formatlines(highlighter_func highlighter, |
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
65 | UcxList *in, write_func out, void *stream, int showlineno) { |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
66 | |
44
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
67 | /* compute width of line numbering */ |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
68 | int lnw; |
44
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
69 | if (showlineno) { |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
70 | size_t lines = ucx_list_size(in); |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
71 | lnw = 1; |
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
72 | int p = 1; |
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
73 | while ((p*=10) < lines) lnw++; |
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
74 | } |
44
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
75 | |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
76 | /* allocate line buffer */ |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
77 | UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
78 | if(!line) { |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
79 | return 1; |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
80 | } |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
81 | |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
82 | /* start monospace formatting */ |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
83 | out("<pre>\n", 1, 6, stream); |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
84 | |
44
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
85 | /* process lines */ |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
86 | size_t lineno = 0; |
46
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
87 | HighlighterData highlighter_data; |
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
88 | memset(&highlighter_data, 0, sizeof(HighlighterData)); |
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
89 | |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
90 | UCX_FOREACH(sourceline, in) { |
44
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
91 | /* increase line number and clean line buffer */ |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
92 | lineno++; |
44
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
93 | ucx_buffer_clear(line); |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
94 | |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
95 | /* write line number */ |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
96 | if (showlineno) { |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
97 | ucx_bprintf(line, "<span class=\"c2html-lineno\">" |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
98 | "<a name=\"l%d\" href=\"#l%d\">%*d </a></span> ", |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
99 | lineno, lineno, lnw, lineno); |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
100 | } |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
101 | |
45
1f3835182aeb
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
Mike Becker <universe@uap-core.de>
parents:
44
diff
changeset
|
102 | /* process code line */ |
46
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
103 | highlighter(sourceline->data, line, &highlighter_data); |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
104 | |
44
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
105 | /* write code line */ |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
106 | out(line->space, 1, line->size, stream); |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
107 | } |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
108 | |
44
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
109 | /* end monospace formatting */ |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
110 | out("</pre>\n", 1, 7, stream); |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
111 | |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
112 | /* cleanup and return */ |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
113 | ucx_buffer_free(line); |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
114 | return 0; |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
115 | } |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
116 | |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
117 | #define FILEBUF_SIZE 4096 |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
118 | |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
119 | enum source_type { |
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
120 | SOURCE_C, |
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
121 | SOURCE_JAVA, |
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
122 | SOURCE_PLAIN |
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
123 | }; |
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
124 | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
125 | int main(int argc, char** argv) { |
43 | 126 | |
127 | /* Default settings */ | |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
128 | Settings settings; |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
129 | memset(&settings, 0, sizeof(settings)); |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
130 | settings.showlinenumbers = 1; |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
131 | enum source_type sourcetype = SOURCE_C; |
19 | 132 | |
43 | 133 | /* Parse command line */ |
19 | 134 | char optc; |
37
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
135 | while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) { |
19 | 136 | switch (optc) { |
137 | case 'o': | |
138 | if (!(optarg[0] == '-' && optarg[1] == 0)) { | |
139 | settings.outfilename = optarg; | |
140 | } | |
141 | break; | |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
142 | case 'F': |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
143 | settings.footerfile = optarg; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
144 | break; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
145 | case 'H': |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
146 | settings.headerfile = optarg; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
147 | break; |
19 | 148 | case 'j': |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
149 | sourcetype = SOURCE_JAVA; |
19 | 150 | break; |
151 | case 'p': | |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
152 | sourcetype = SOURCE_PLAIN; |
19 | 153 | break; |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
154 | case 'l': |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
155 | settings.showlinenumbers = 0; |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
156 | break; |
19 | 157 | case 'h': |
158 | printhelp(); | |
38
77c158821738
use macros for exit codes
Mike Becker <universe@uap-core.de>
parents:
37
diff
changeset
|
159 | return EXIT_SUCCESS; |
37
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
160 | case 'v': |
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
161 | case 'V': |
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
162 | #ifdef VERSION_DEVELOP |
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
163 | printf("%d.%d (unstable)\n", VERSION_MAJOR, VERSION_MINOR); |
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
164 | #else |
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
165 | printf("%d.%d\n", VERSION_MAJOR, VERSION_MINOR); |
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
166 | #endif |
38
77c158821738
use macros for exit codes
Mike Becker <universe@uap-core.de>
parents:
37
diff
changeset
|
167 | return EXIT_SUCCESS; |
19 | 168 | default: |
38
77c158821738
use macros for exit codes
Mike Becker <universe@uap-core.de>
parents:
37
diff
changeset
|
169 | return EXIT_FAILURE; |
11 | 170 | } |
19 | 171 | } |
172 | ||
173 | if (optind != argc-1) { | |
11 | 174 | printhelp(); |
43 | 175 | return EXIT_FAILURE; |
19 | 176 | } else { |
43 | 177 | /* Configure highlighter */ |
46
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
178 | highlighter_func hltr = NULL; |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
179 | switch (sourcetype) { |
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
180 | case SOURCE_C: |
46
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
181 | hltr = cparseline; |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
182 | break; |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
183 | case SOURCE_JAVA: |
46
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
184 | hltr = jparseline; |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
185 | break; |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
186 | case SOURCE_PLAIN: |
46
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
187 | hltr = plainparseline; |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
188 | break; |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
189 | default: /* should be unreachable */ |
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
190 | fprintf(stderr, "error in enum source_type\n"); |
43 | 191 | return EXIT_FAILURE; |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
192 | } |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
193 | |
43 | 194 | /* Open output file */ |
195 | settings.infilename = argv[optind]; | |
196 | FILE *fout; | |
197 | if (settings.outfilename) { | |
198 | fout = fopen(settings.outfilename, "w"); | |
199 | if (!fout) { | |
200 | perror("Error opening output file"); | |
201 | return EXIT_FAILURE; | |
202 | } | |
203 | } else { | |
204 | fout = stdout; | |
205 | } | |
206 | ||
207 | /* Allocate file buffer */ | |
208 | char *filebuf = malloc(FILEBUF_SIZE); | |
209 | if (!filebuf) { | |
210 | perror("Error allocating file buffer"); | |
211 | return EXIT_FAILURE; | |
212 | } | |
213 | ||
214 | /* Prepend header file */ | |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
215 | { |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
216 | FILE *headerfile = fopen(settings.headerfile, "r"); |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
217 | if (!headerfile) { |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
218 | perror("Error opening header file"); |
43 | 219 | if (fout != stdout) { |
220 | fclose(fout); | |
221 | } | |
222 | return EXIT_FAILURE; | |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
223 | } |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
224 | ucx_stream_copy(headerfile, fout, |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
225 | (read_func) fread, (write_func) fwrite, |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
226 | filebuf, FILEBUF_SIZE, (size_t)-1); |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
227 | fclose(headerfile); |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
228 | } |
11 | 229 | |
43 | 230 | /* Process input file */ |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
231 | FILE *inputfile = fopen(settings.infilename, "r"); |
19 | 232 | if (inputfile) { |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
233 | UcxBuffer *content = ucx_buffer_new(NULL, |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
234 | FILEBUF_SIZE*2, UCX_BUFFER_AUTOEXTEND); |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
235 | { |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
236 | ucx_stream_copy(inputfile, content, (read_func) fread, |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
237 | (write_func) ucx_buffer_write, |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
238 | filebuf, FILEBUF_SIZE, (size_t)-1); |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
239 | } |
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
240 | fclose(inputfile); |
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
241 | |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
242 | UcxList *inputlines = ucx_list_append(NULL, content->space); |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
243 | for (size_t i = 1 ; i < content->size ; i++) { |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
244 | if (content->space[i] == '\r') { |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
245 | content->space[i] = '\n'; i++; |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
246 | } |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
247 | if (content->space[i] == '\n' && i+1 < content->size) { |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
248 | ucx_list_append(inputlines, content->space+i+1); |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
249 | } |
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
250 | } |
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
251 | |
46
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
252 | formatlines(hltr, inputlines, |
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
253 | (write_func) fwrite, fout, settings.showlinenumbers); |
45
1f3835182aeb
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
Mike Becker <universe@uap-core.de>
parents:
44
diff
changeset
|
254 | |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
255 | ucx_buffer_free(content); |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
256 | } else { |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
257 | perror("Error opening input file"); |
43 | 258 | if (fout != stdout) { |
259 | fclose(fout); | |
260 | } | |
261 | return EXIT_FAILURE; | |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
262 | } |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
263 | |
43 | 264 | /* Append footer file */ |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
265 | { |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
266 | FILE *footerfile = fopen(settings.footerfile, "r"); |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
267 | if (!footerfile) { |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
268 | perror("Error opening footer file"); |
43 | 269 | if (fout != stdout) { |
270 | fclose(fout); | |
271 | } | |
272 | return EXIT_FAILURE; | |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
273 | } |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
274 | ucx_stream_copy(footerfile, fout, |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
275 | (read_func) fread, (write_func) fwrite, |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
276 | filebuf, FILEBUF_SIZE, (size_t)-1); |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
277 | fclose(footerfile); |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
278 | } |
43 | 279 | |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
280 | |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
281 | free(filebuf); |
19 | 282 | |
43 | 283 | return EXIT_SUCCESS; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
284 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
285 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
286 |