src/c2html.c

Thu, 11 Jul 2024 20:05:26 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 11 Jul 2024 20:05:26 +0200
changeset 81
9e483a0e3f52
parent 75
c72b250866ab
permissions
-rw-r--r--

Added tag v3.1 for changeset 0f1c55d2a90a

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
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
32 #include <cx/array_list.h>
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
33 #include <cx/printf.h>
70
60cecca5e484 fix illegal memory access when input file does not end with line break
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
34 #include <string.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
35
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
36 size_t c2html_format(
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
37 CxList const *lines,
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
38 void *outbuf,
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
39 cx_write_func wfnc,
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
40 c2html_highlighter_func highlighter,
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
41 int showln
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
42 ) {
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
43 /* total written bytes */
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
44 size_t written = 0;
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
45
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
46 /* compute width of line numbering */
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
47 int lnw = 0;
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
48 if (showln) {
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
49 size_t no_lines = cxListSize(lines);
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
50 for (size_t p = 1; p < no_lines; p *= 10) lnw++;
55
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
51 }
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
52
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
53 /* start code formatting */
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
54 written += wfnc("<div class=\"c2html-code\">\n", 1, 26, outbuf);
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 */
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
57 int lineno = 0;
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 71
diff changeset
58 c2html_highlighter_data hd = {0};
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
59 cxBufferInit(&hd.primary_buffer, NULL, 256, NULL, CX_BUFFER_AUTO_EXTEND);
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
60 cxBufferInit(&hd.secondary_buffer, NULL, 32, NULL, CX_BUFFER_AUTO_EXTEND);
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
61 CxBuffer out_line;
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
62 cxBufferInit(&out_line, NULL, 128, NULL, CX_BUFFER_AUTO_EXTEND);
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
63
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
64 CxIterator in_lines = cxListIterator(lines);
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
65 cx_foreach(char*, in_line, in_lines) {
44
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
66 /* 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
67 lineno++;
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
68 cxBufferClear(&out_line);
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
69
44
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
70 /* write line number */
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
71 if (showln) {
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
72 cx_bprintf(&out_line, "<a class=\"c2html-lineno\" name=\"l%d\" "
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
73 "href=\"#l%d\">%*d </a>",
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
74 lineno, lineno, lnw, lineno);
44
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
75 }
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
76
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
77 /* process code line */
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
78 highlighter(in_line, &out_line, &hd);
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
79
50
17408c3607ce minor fixes and macro removals
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
80 /* write code line */
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
81 written += wfnc(out_line.space, 1, out_line.size, outbuf);
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
82 }
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
83
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
84 /* end code formatting */
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
85 written += wfnc("</div>\n", 1, 7, outbuf);
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
86
44
2b4ac35d061d cleans up formatfile function up to the parser call
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
87 /* cleanup and return */
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
88 cxBufferDestroy(&hd.primary_buffer);
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
89 cxBufferDestroy(&hd.secondary_buffer);
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
90 cxBufferDestroy(&out_line);
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
91
55
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
92 return written;
24
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
93 }
e43dee5892f4 improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
94
70
60cecca5e484 fix illegal memory access when input file does not end with line break
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
95 size_t c2html_textformat(
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
96 char const *inputbuffer,
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
97 void *outbuf,
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
98 cx_write_func wfnc,
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
99 c2html_highlighter_func highlighter,
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
100 int showln
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
101 ) {
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
102 /* a rough estimate for the number of lines */
70
60cecca5e484 fix illegal memory access when input file does not end with line break
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
103 size_t inputbuflen = strlen(inputbuffer);
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
104 size_t est_cap = 16 + inputbuflen / 40;
1
12c482ea4fc4 first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
105
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
106 /* create the line pointer array */
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
107 CxList *lines = cxArrayListCreateSimple(CX_STORE_POINTERS, est_cap);
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
108 cxListAdd(lines, inputbuffer);
71
d5af9261231d fix incorrect highlighting when input file starts with line break
Mike Becker <universe@uap-core.de>
parents: 70
diff changeset
109 for (size_t i = 0; i < inputbuflen; i++) {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
110 if (inputbuffer[i] == '\n' && i + 1 < inputbuflen) {
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
111 cxListAdd(lines, inputbuffer + i + 1);
57
eba880c1705c improves API and adds functions for strings
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
112 }
eba880c1705c improves API and adds functions for strings
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
113 }
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
114
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
115 /* invoke the other function */
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
116 size_t n = c2html_format(lines, outbuf, wfnc, highlighter, showln);
57
eba880c1705c improves API and adds functions for strings
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
117
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
118 /* cleanup and return */
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
119 cxListDestroy(lines);
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
120 return n;
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
121 }

mercurial