Mon, 13 Nov 2017 14:17:46 +0100
removes build/ucx from makefile
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, "<a class=\"c2html-lineno\" name=\"l%d\" "
72 "href=\"#l%d\">%*d </a>",
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_bcopy(inputbuffer, content, rfnc, (write_func) ucx_buffer_write,
103 ibuf, ibuflen);
105 size_t n = c2html_bformatn(content->space, content->size,
106 outputbuffer, wfnc, maxlen, hltr, showln);
108 ucx_buffer_free(content);
109 return n;
110 }
112 size_t c2html_format(void* inputbuffer, read_func rfnc,
113 char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc,
114 c2html_highlighter_func hltr, int showln) {
115 return c2html_formatn(inputbuffer, rfnc, ibuf, ibuflen,
116 outputbuffer, wfnc, (size_t)-1, hltr, showln);
117 }
119 size_t c2html_fformat(FILE* inputfile, char *ibuf, size_t ibuflen,
120 void* outputbuffer, write_func wfnc,
121 c2html_highlighter_func hltr, int showln) {
122 return c2html_format(inputfile, (read_func) fread, ibuf, ibuflen,
123 outputbuffer, wfnc, hltr, showln);
124 }
126 void c2html_fformatf(FILE *inputfile, char *ibuf, size_t ibuflen,
127 FILE* outputfile, c2html_highlighter_func hltr, int showln) {
128 c2html_format(inputfile, (read_func) fread, ibuf, ibuflen,
129 outputfile, (write_func) fwrite, hltr, showln);
130 }
132 size_t c2html_bformatn(const char* inputbuffer, size_t inputbuflen,
133 void* outputbuffer, write_func wfnc,
134 size_t maxlen, c2html_highlighter_func hltr, int showln) {
135 UcxList *lines = ucx_list_append(NULL, (char*)inputbuffer);
136 for (size_t i = 1 ; i < inputbuflen ; i++) {
137 if (inputbuffer[i] == '\n' && i+1 < inputbuflen) {
138 ucx_list_append(lines, (char*)inputbuffer+i+1);
139 }
140 }
142 size_t n = formatlines(hltr, lines, outputbuffer, wfnc, maxlen, showln);
144 ucx_list_free(lines);
145 return n;
146 }
148 size_t c2html_bformat(const char* inputbuffer, size_t inputbuflen,
149 void* outputbuffer, write_func wfnc,
150 c2html_highlighter_func hltr, int showln) {
151 return c2html_bformatn(inputbuffer, inputbuflen, outputbuffer, wfnc,
152 (size_t)-1, hltr, showln);
153 }
155 void c2html_bformatf(const char* inputbuffer, size_t inputbuflen,
156 FILE* outputfile, c2html_highlighter_func hltr, int showln) {
157 c2html_bformatn(inputbuffer, inputbuflen, outputfile,
158 (write_func) fwrite, (size_t)-1, hltr, showln);
159 }