src/c2html.c

Tue, 23 Aug 2016 17:31:15 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 17:31:15 +0200
changeset 49
f86f0b054464
parent 48
b2724c711203
child 50
17408c3607ce
permissions
-rw-r--r--

cleans up includes

23
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
1 /*
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
3 *
35
abaf2489c549 adjusted copyright info
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
4 * Copyright 2016 Mike Becker. All rights reserved.
23
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
5 *
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
6 * Redistribution and use in source and binary forms, with or without
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
7 * modification, are permitted provided that the following conditions are met:
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
8 *
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
10 * notice, this list of conditions and the following disclaimer.
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
11 *
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
14 * documentation and/or other materials provided with the distribution.
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
15 *
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
27 *
f44a185b678b fixed licenses
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
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
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
34 printf("Formats source code using HTML.\n\nUsage:\n"
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
35 " c2html [Options] FILE\n\n"
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
36 " Options:\n"
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
37 " -h Prints this help message\n"
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
38 " -j Highlight Java instead of C source code\n"
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
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
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
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
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
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
48
b2724c711203 highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
48 static void plain_highlighter(char *src, UcxBuffer *dest, int* x) {
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
46
534a4ef4143d refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
56 int formatlines(highlighter_func highlighter,
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 /* allocate line buffer */
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
69 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
70 if(!line) {
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
71 return 1;
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
72 }
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
73
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
74 /* start monospace formatting */
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
75 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
76
44
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
77 /* 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
78 size_t lineno = 0;
47
c39ecbbca7c0 words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
79 int multiline_comment = 0;
46
534a4ef4143d refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
80
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
81 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
82 /* 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
83 lineno++;
44
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
84 ucx_buffer_clear(line);
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
85
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
86 /* write line number */
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
87 if (showlineno) {
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
88 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
89 "<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
90 lineno, lineno, lnw, lineno);
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
91 }
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
92
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
93 /* process code line */
47
c39ecbbca7c0 words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
94 highlighter(sourceline->data, line, &multiline_comment);
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
95
48
b2724c711203 highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
96 /* write code line and reset buffer */
44
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
97 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
98 }
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
99
44
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
100 /* end monospace formatting */
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
101 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
102
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
103 /* 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
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 return 0;
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
106 }
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
107
42
7f2403c637a7 replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
108 #define FILEBUF_SIZE 4096
7f2403c637a7 replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
109
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
110 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
111 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
112 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
113 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
114 };
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
115
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
116 int main(int argc, char** argv) {
43
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
117
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
118 /* 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
119 Settings settings;
22
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
120 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
121 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
122 enum source_type sourcetype = SOURCE_C;
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
123
43
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
124 /* Parse command line */
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
125 char optc;
37
1a67185e5496 adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
126 while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) {
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
127 switch (optc) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
128 case 'o':
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
129 if (!(optarg[0] == '-' && optarg[1] == 0)) {
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
130 settings.outfilename = optarg;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
131 }
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
132 break;
22
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
133 case 'F':
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
134 settings.footerfile = optarg;
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
135 break;
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
136 case 'H':
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
137 settings.headerfile = optarg;
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
138 break;
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
139 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
140 sourcetype = SOURCE_JAVA;
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
141 break;
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
142 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
143 sourcetype = SOURCE_PLAIN;
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
144 break;
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
145 case 'l':
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
146 settings.showlinenumbers = 0;
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
147 break;
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
148 case 'h':
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
149 printhelp();
38
77c158821738 use macros for exit codes
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
150 return EXIT_SUCCESS;
37
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 case 'V':
1a67185e5496 adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
153 #ifdef VERSION_DEVELOP
1a67185e5496 adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
154 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
155 #else
1a67185e5496 adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
156 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
157 #endif
38
77c158821738 use macros for exit codes
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
158 return EXIT_SUCCESS;
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
159 default:
38
77c158821738 use macros for exit codes
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
160 return EXIT_FAILURE;
11
c59fe73459fd option for output file
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
161 }
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
162 }
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
163
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
164 if (optind != argc-1) {
11
c59fe73459fd option for output file
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
165 printhelp();
43
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
166 return EXIT_FAILURE;
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
167 } else {
47
c39ecbbca7c0 words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
168 /* Choose highlighter */
46
534a4ef4143d refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
169 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
170 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
171 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
172 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
173 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
174 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
175 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
176 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
177 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
178 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
179 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
180 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
181 fprintf(stderr, "error in enum source_type\n");
43
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
182 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
183 }
42
7f2403c637a7 replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
184
43
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
185 /* Open output file */
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
186 settings.infilename = argv[optind];
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
187 FILE *fout;
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
188 if (settings.outfilename) {
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
189 fout = fopen(settings.outfilename, "w");
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
190 if (!fout) {
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
191 perror("Error opening output file");
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
192 return EXIT_FAILURE;
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
193 }
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
194 } else {
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
195 fout = stdout;
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
196 }
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
197
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
198 /* Allocate file buffer */
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
199 char *filebuf = malloc(FILEBUF_SIZE);
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
200 if (!filebuf) {
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
201 perror("Error allocating file buffer");
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
202 return EXIT_FAILURE;
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
203 }
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
204
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
205 /* Prepend header file */
42
7f2403c637a7 replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
206 {
7f2403c637a7 replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
207 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
208 if (!headerfile) {
7f2403c637a7 replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
209 perror("Error opening header file");
43
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
210 if (fout != stdout) {
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
211 fclose(fout);
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
212 }
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
213 return EXIT_FAILURE;
42
7f2403c637a7 replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
214 }
7f2403c637a7 replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
215 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
216 (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
217 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
218 fclose(headerfile);
7f2403c637a7 replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
219 }
11
c59fe73459fd option for output file
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
220
43
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
221 /* 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
222 FILE *inputfile = fopen(settings.infilename, "r");
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
223 if (inputfile) {
42
7f2403c637a7 replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
224 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
225 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
226 {
42
7f2403c637a7 replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
227 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
228 (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
229 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
230 }
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 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
232
42
7f2403c637a7 replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
233 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
234 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
235 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
236 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
237 }
42
7f2403c637a7 replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
238 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
239 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
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 }
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
242
46
534a4ef4143d refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
243 formatlines(hltr, inputlines,
534a4ef4143d refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
244 (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
245
42
7f2403c637a7 replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
246 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
247 } else {
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
248 perror("Error opening input file");
43
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
249 if (fout != stdout) {
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
250 fclose(fout);
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
251 }
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
252 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
253 }
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
254
43
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
255 /* Append footer file */
42
7f2403c637a7 replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
256 {
7f2403c637a7 replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
257 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
258 if (!footerfile) {
7f2403c637a7 replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
259 perror("Error opening footer file");
43
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
260 if (fout != stdout) {
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
261 fclose(fout);
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
262 }
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
263 return EXIT_FAILURE;
42
7f2403c637a7 replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
264 }
7f2403c637a7 replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
265 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
266 (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
267 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
268 fclose(footerfile);
22
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
269 }
43
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
270
22
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
271
42
7f2403c637a7 replaces custom copyfile() function with ucx_stream_copy calls
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
272 free(filebuf);
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
273
43
a8cee98c8832 cleans up main function
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
274 return EXIT_SUCCESS;
1
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 }
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
277

mercurial