src/c2html.c

Wed, 31 Aug 2016 14:41:56 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 31 Aug 2016 14:41:56 +0200
changeset 55
bf54085ce341
parent 53
5e47a26a16f0
child 57
eba880c1705c
permissions
-rw-r--r--

adds appropriate public API

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 */
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
29
22
f463693b5eeb added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
30 #include "c2html.h"
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
31
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
32 #include "ucx/list.h"
55
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
33 #include "ucx/utils.h"
53
5e47a26a16f0 adds appendfile() function to main module + adds TODOs for source files which do not terminate with a blank line
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
34
55
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
35 #define try_write(wfnc, str, n, buf, written, maxlen) \
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
36 { \
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
37 size_t m = maxlen-written; \
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
38 written += wfnc(str, 1, n > m ? m : n, buf); \
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
39 }
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
40
55
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
41 static size_t formatlines(c2html_highlighter_func highlighter, UcxList *in,
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
42 void *outbuf, write_func wfnc, size_t maxlen, int showlineno) {
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
43 /* total written bytes */
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
44 size_t written = 0;
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
45
44
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
46 /* compute width of line numbering */
53
5e47a26a16f0 adds appendfile() function to main module + adds TODOs for source files which do not terminate with a blank line
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
47 int lnw = 0;
44
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
48 if (showlineno) {
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
49 size_t lines = ucx_list_size(in);
53
5e47a26a16f0 adds appendfile() function to main module + adds TODOs for source files which do not terminate with a blank line
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
50 for (size_t p = 1; p < lines ; p*=10) lnw++;
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
51 }
44
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
52
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
53 /* start monospace formatting */
55
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
54 try_write(wfnc, "<pre>\n", 6, outbuf, written, maxlen);
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
55
44
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
56 /* 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
57 size_t lineno = 0;
55
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
58 c2html_highlighter_data* hd = malloc(sizeof(c2html_highlighter_data));
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
59 hd->multiline_comment = 0;
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
60 hd->primary_buffer = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND);
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
61 hd->secondary_buffer = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND);
51
f25ba6fd7a08 replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
62 UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
46
534a4ef4143d refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
63
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
64 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
65 /* 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
66 lineno++;
44
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
67 ucx_buffer_clear(line);
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
68
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
69 /* write line number */
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
70 if (showlineno) {
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
71 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
72 "<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
73 lineno, lineno, lnw, lineno);
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
74 }
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
75
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
76 /* process code line */
51
f25ba6fd7a08 replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
77 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
78
50
17408c3607ce minor fixes and macro removals
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
79 /* write code line */
55
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
80 try_write(wfnc, line->space, line->size, outbuf, written, maxlen);
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
81
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
82 if (written == maxlen) break;
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
83 }
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
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 /* end monospace formatting */
55
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
86 try_write(wfnc, "</pre>\n", 7, outbuf, written, maxlen);
44
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
87
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
88 /* cleanup and return */
55
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
89 ucx_buffer_free(hd->primary_buffer);
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
90 ucx_buffer_free(hd->secondary_buffer);
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
91 free(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
92 ucx_buffer_free(line);
55
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
93
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
94 return written;
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
95 }
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
96
55
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
97 size_t c2html_formatn(void* inputbuffer, read_func rfnc,
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
98 char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc,
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
99 size_t maxlen, c2html_highlighter_func hltr, int showln) {
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
100
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
101 UcxBuffer *content = ucx_buffer_new(NULL, ibuflen*2, UCX_BUFFER_AUTOEXTEND);
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
102 ucx_stream_copy(inputbuffer, content, rfnc, (write_func) ucx_buffer_write,
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
103 ibuf, ibuflen, (size_t)-1);
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
104
55
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
105 UcxList *lines = ucx_list_append(NULL, content->space);
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
106 for (size_t i = 1 ; i < content->size ; i++) {
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
107 if (content->space[i] == '\r') {
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
108 content->space[i] = '\n'; i++;
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
109 }
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
110 if (content->space[i] == '\n' && i+1 < content->size) {
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
111 ucx_list_append(lines, content->space+i+1);
11
c59fe73459fd option for output file
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
112 }
19
2e812df2b231 formatted with 4 spaces
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
113 }
55
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
114
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
115 size_t n = formatlines(hltr, lines, outputbuffer, wfnc, maxlen, showln);
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
116
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
117 ucx_buffer_free(content);
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
118 return n;
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
119 }
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
120
55
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
121 size_t c2html_format(void* inputbuffer, read_func rfnc,
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
122 char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc,
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
123 c2html_highlighter_func hltr, int showln) {
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
124 return c2html_formatn(inputbuffer, rfnc, ibuf, ibuflen,
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
125 outputbuffer, wfnc, (size_t)-1, hltr, showln);
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
126 }
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
127
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
128 size_t c2html_format_file(FILE* inputfile, char *ibuf, size_t ibuflen,
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
129 void* outputbuffer, write_func wfnc,
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
130 c2html_highlighter_func hltr, int showln) {
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
131 return c2html_format(inputfile, (read_func) fread, ibuf, ibuflen,
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
132 outputbuffer, wfnc, hltr, showln);
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
133 }
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
134
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
135 void c2html_fformat_file(FILE *inputfile, char *ibuf, size_t ibuflen,
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
136 FILE* outputfile, c2html_highlighter_func hltr, int showln) {
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
137 c2html_format(inputfile, (read_func) fread, ibuf, ibuflen,
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
138 outputfile, (write_func) fwrite, hltr, showln);
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
139 }

mercurial