Wed, 31 Aug 2016 14:41:56 +0200
adds appropriate public API
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2016 Mike Becker. All rights reserved.
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 */
30 #include "c2html.h"
32 #include "ucx/list.h"
33 #include "ucx/utils.h"
35 #define try_write(wfnc, str, n, buf, written, maxlen) \
36 { \
37 size_t m = maxlen-written; \
38 written += wfnc(str, 1, n > m ? m : n, buf); \
39 }
41 static size_t formatlines(c2html_highlighter_func highlighter, UcxList *in,
42 void *outbuf, write_func wfnc, size_t maxlen, int showlineno) {
43 /* total written bytes */
44 size_t written = 0;
46 /* compute width of line numbering */
47 int lnw = 0;
48 if (showlineno) {
49 size_t lines = ucx_list_size(in);
50 for (size_t p = 1; p < lines ; p*=10) lnw++;
51 }
53 /* start monospace formatting */
54 try_write(wfnc, "<pre>\n", 6, outbuf, written, maxlen);
56 /* process lines */
57 size_t lineno = 0;
58 c2html_highlighter_data* hd = malloc(sizeof(c2html_highlighter_data));
59 hd->multiline_comment = 0;
60 hd->primary_buffer = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND);
61 hd->secondary_buffer = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND);
62 UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
64 UCX_FOREACH(sourceline, in) {
65 /* increase line number and clean line buffer */
66 lineno++;
67 ucx_buffer_clear(line);
69 /* write line number */
70 if (showlineno) {
71 ucx_bprintf(line, "<span class=\"c2html-lineno\">"
72 "<a name=\"l%d\" href=\"#l%d\">%*d </a></span> ",
73 lineno, lineno, lnw, lineno);
74 }
76 /* process code line */
77 highlighter(sourceline->data, line, hd);
79 /* write code line */
80 try_write(wfnc, line->space, line->size, outbuf, written, maxlen);
82 if (written == maxlen) break;
83 }
85 /* end monospace formatting */
86 try_write(wfnc, "</pre>\n", 7, outbuf, written, maxlen);
88 /* cleanup and return */
89 ucx_buffer_free(hd->primary_buffer);
90 ucx_buffer_free(hd->secondary_buffer);
91 free(hd);
92 ucx_buffer_free(line);
94 return written;
95 }
97 size_t c2html_formatn(void* inputbuffer, read_func rfnc,
98 char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc,
99 size_t maxlen, c2html_highlighter_func hltr, int showln) {
101 UcxBuffer *content = ucx_buffer_new(NULL, ibuflen*2, UCX_BUFFER_AUTOEXTEND);
102 ucx_stream_copy(inputbuffer, content, rfnc, (write_func) ucx_buffer_write,
103 ibuf, ibuflen, (size_t)-1);
105 UcxList *lines = ucx_list_append(NULL, content->space);
106 for (size_t i = 1 ; i < content->size ; i++) {
107 if (content->space[i] == '\r') {
108 content->space[i] = '\n'; i++;
109 }
110 if (content->space[i] == '\n' && i+1 < content->size) {
111 ucx_list_append(lines, content->space+i+1);
112 }
113 }
115 size_t n = formatlines(hltr, lines, outputbuffer, wfnc, maxlen, showln);
117 ucx_buffer_free(content);
118 return n;
119 }
121 size_t c2html_format(void* inputbuffer, read_func rfnc,
122 char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc,
123 c2html_highlighter_func hltr, int showln) {
124 return c2html_formatn(inputbuffer, rfnc, ibuf, ibuflen,
125 outputbuffer, wfnc, (size_t)-1, hltr, showln);
126 }
128 size_t c2html_format_file(FILE* inputfile, char *ibuf, size_t ibuflen,
129 void* outputbuffer, write_func wfnc,
130 c2html_highlighter_func hltr, int showln) {
131 return c2html_format(inputfile, (read_func) fread, ibuf, ibuflen,
132 outputbuffer, wfnc, hltr, showln);
133 }
135 void c2html_fformat_file(FILE *inputfile, char *ibuf, size_t ibuflen,
136 FILE* outputfile, c2html_highlighter_func hltr, int showln) {
137 c2html_format(inputfile, (read_func) fread, ibuf, ibuflen,
138 outputfile, (write_func) fwrite, hltr, showln);
139 }