Thu, 25 Aug 2016 12:16:57 +0200
replaces stack buffers with UCX buffers
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/list.h" |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
32 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
33 | void printhelp() { |
19 | 34 | printf("Formats source code using HTML.\n\nUsage:\n" |
35 | " c2html [Options] FILE\n\n" | |
36 | " Options:\n" | |
37 | " -h Prints this help message\n" | |
38 | " -j Highlight Java instead of C source code\n" | |
39 | " -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
|
40 | " -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
|
41 | " -F <footer> Append footer file\n" |
19 | 42 | " -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
|
43 | " -l Disable line numbers\n" |
37
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
44 | " -V, -v Prints version and exits\n" |
19 | 45 | "\n"); |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
46 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
47 | |
51
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
48 | static void plain_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) { |
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
|
49 | while (*src && *src != '\n') { |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
50 | put_htmlescaped(dest, *src); |
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
|
51 | 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
|
52 | } |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
53 | ucx_buffer_putc(dest, '\n'); |
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
|
54 | } |
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 | |
50
17408c3607ce
minor fixes and macro removals
Mike Becker <universe@uap-core.de>
parents:
49
diff
changeset
|
56 | void formatlines(highlighter_func highlighter, |
46
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
57 | 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
|
58 | |
44
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
59 | /* 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
|
60 | int lnw; |
44
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
61 | if (showlineno) { |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
62 | 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
|
63 | 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
|
64 | 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
|
65 | 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
|
66 | } |
44
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
67 | |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
68 | /* start monospace formatting */ |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
69 | 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
|
70 | |
44
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
71 | /* 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
|
72 | size_t lineno = 0; |
51
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
73 | HighlighterData *hd = new_highlighter_data(); |
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
74 | UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); |
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
75 | if(!line || !hd) { |
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
76 | perror("Error allocating buffer for output"); |
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
77 | return; |
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
78 | } |
46
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
79 | |
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
|
80 | 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
|
81 | /* 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
|
82 | lineno++; |
44
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
83 | ucx_buffer_clear(line); |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
84 | |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
85 | /* write line number */ |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
86 | if (showlineno) { |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
87 | 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
|
88 | "<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
|
89 | lineno, lineno, lnw, lineno); |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
90 | } |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
91 | |
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
|
92 | /* process code line */ |
51
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
93 | highlighter(sourceline->data, line, hd); |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
94 | |
50
17408c3607ce
minor fixes and macro removals
Mike Becker <universe@uap-core.de>
parents:
49
diff
changeset
|
95 | /* write code line */ |
44
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
96 | 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
|
97 | } |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
98 | |
44
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
99 | /* end monospace formatting */ |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
100 | 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
|
101 | |
2b4ac35d061d
cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents:
43
diff
changeset
|
102 | /* cleanup and return */ |
51
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
103 | free_highlighter_data(hd); |
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
|
104 | 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
|
105 | } |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
106 | |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
107 | #define FILEBUF_SIZE 4096 |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
108 | |
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
|
109 | 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
|
110 | 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
|
111 | 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
|
112 | 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
|
113 | }; |
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
|
114 | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
115 | int main(int argc, char** argv) { |
43 | 116 | |
117 | /* 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
|
118 | Settings settings; |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
119 | 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
|
120 | 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
|
121 | enum source_type sourcetype = SOURCE_C; |
19 | 122 | |
43 | 123 | /* Parse command line */ |
19 | 124 | char optc; |
37
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
125 | while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) { |
19 | 126 | switch (optc) { |
127 | case 'o': | |
128 | if (!(optarg[0] == '-' && optarg[1] == 0)) { | |
129 | settings.outfilename = optarg; | |
130 | } | |
131 | break; | |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
132 | case 'F': |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
133 | settings.footerfile = optarg; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
134 | break; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
135 | case 'H': |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
136 | settings.headerfile = optarg; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
137 | break; |
19 | 138 | 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
|
139 | sourcetype = SOURCE_JAVA; |
19 | 140 | break; |
141 | 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
|
142 | sourcetype = SOURCE_PLAIN; |
19 | 143 | break; |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
144 | case 'l': |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
145 | settings.showlinenumbers = 0; |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
146 | break; |
19 | 147 | case 'h': |
148 | printhelp(); | |
38
77c158821738
use macros for exit codes
Mike Becker <universe@uap-core.de>
parents:
37
diff
changeset
|
149 | return EXIT_SUCCESS; |
37
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
150 | case 'v': |
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
151 | case 'V': |
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
152 | #ifdef VERSION_DEVELOP |
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
153 | 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
|
154 | #else |
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
155 | 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
|
156 | #endif |
38
77c158821738
use macros for exit codes
Mike Becker <universe@uap-core.de>
parents:
37
diff
changeset
|
157 | return EXIT_SUCCESS; |
19 | 158 | default: |
38
77c158821738
use macros for exit codes
Mike Becker <universe@uap-core.de>
parents:
37
diff
changeset
|
159 | return EXIT_FAILURE; |
11 | 160 | } |
19 | 161 | } |
162 | ||
163 | if (optind != argc-1) { | |
11 | 164 | printhelp(); |
43 | 165 | return EXIT_FAILURE; |
19 | 166 | } else { |
47
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
167 | /* Choose highlighter */ |
46
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
168 | 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
|
169 | 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
|
170 | case SOURCE_C: |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
171 | hltr = c_highlighter; |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
172 | 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
|
173 | case SOURCE_JAVA: |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
174 | hltr = java_highlighter; |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
175 | 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
|
176 | case SOURCE_PLAIN: |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
177 | hltr = plain_highlighter; |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
178 | 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
|
179 | 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
|
180 | fprintf(stderr, "error in enum source_type\n"); |
43 | 181 | 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
|
182 | } |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
183 | |
43 | 184 | /* Open output file */ |
185 | settings.infilename = argv[optind]; | |
186 | FILE *fout; | |
187 | if (settings.outfilename) { | |
188 | fout = fopen(settings.outfilename, "w"); | |
189 | if (!fout) { | |
190 | perror("Error opening output file"); | |
191 | return EXIT_FAILURE; | |
192 | } | |
193 | } else { | |
194 | fout = stdout; | |
195 | } | |
196 | ||
197 | /* Allocate file buffer */ | |
198 | char *filebuf = malloc(FILEBUF_SIZE); | |
199 | if (!filebuf) { | |
200 | perror("Error allocating file buffer"); | |
201 | return EXIT_FAILURE; | |
202 | } | |
203 | ||
204 | /* Prepend header file */ | |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
205 | { |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
206 | 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
|
207 | if (!headerfile) { |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
208 | perror("Error opening header file"); |
43 | 209 | if (fout != stdout) { |
210 | fclose(fout); | |
211 | } | |
212 | return EXIT_FAILURE; | |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
213 | } |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
214 | 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
|
215 | (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
|
216 | 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
|
217 | fclose(headerfile); |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
218 | } |
11 | 219 | |
43 | 220 | /* 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
|
221 | FILE *inputfile = fopen(settings.infilename, "r"); |
19 | 222 | if (inputfile) { |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
223 | 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
|
224 | 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
|
225 | { |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
226 | 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
|
227 | (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
|
228 | 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
|
229 | } |
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
|
230 | 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
|
231 | |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
232 | 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
|
233 | 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
|
234 | 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
|
235 | 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
|
236 | } |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
237 | 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
|
238 | 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
|
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 | } |
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 | |
46
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
242 | formatlines(hltr, inputlines, |
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
243 | (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
|
244 | |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
245 | 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
|
246 | } else { |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
247 | perror("Error opening input file"); |
43 | 248 | if (fout != stdout) { |
249 | fclose(fout); | |
250 | } | |
251 | 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
|
252 | } |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
253 | |
43 | 254 | /* Append footer file */ |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
255 | { |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
256 | 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
|
257 | if (!footerfile) { |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
258 | perror("Error opening footer file"); |
43 | 259 | if (fout != stdout) { |
260 | fclose(fout); | |
261 | } | |
262 | return EXIT_FAILURE; | |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
263 | } |
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
264 | 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
|
265 | (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
|
266 | 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
|
267 | fclose(footerfile); |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
268 | } |
43 | 269 | |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
270 | |
42
7f2403c637a7
replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
271 | free(filebuf); |
19 | 272 | |
43 | 273 | return EXIT_SUCCESS; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
274 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
275 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
276 |