1.1 --- a/src/c2html.c Thu Nov 10 18:44:48 2016 +0100 1.2 +++ b/src/c2html.c Mon Apr 24 20:54:38 2023 +0200 1.3 @@ -29,131 +29,93 @@ 1.4 1.5 #include "c2html.h" 1.6 1.7 -#include "ucx/list.h" 1.8 -#include "ucx/utils.h" 1.9 +#include <cx/array_list.h> 1.10 +#include <cx/printf.h> 1.11 1.12 -#define try_write(wfnc, str, n, buf, written, maxlen) \ 1.13 - { \ 1.14 - size_t m = maxlen-written; \ 1.15 - written += wfnc(str, 1, n > m ? m : n, buf); \ 1.16 +size_t c2html_format( 1.17 + CxList const *lines, 1.18 + void *outbuf, 1.19 + cx_write_func wfnc, 1.20 + c2html_highlighter_func highlighter, 1.21 + int showln 1.22 +) { 1.23 + /* total written bytes */ 1.24 + size_t written = 0; 1.25 + 1.26 + /* compute width of line numbering */ 1.27 + int lnw = 0; 1.28 + if (showln) { 1.29 + size_t no_lines = cxListSize(lines); 1.30 + for (size_t p = 1; p < no_lines; p *= 10) lnw++; 1.31 } 1.32 1.33 -static size_t formatlines(c2html_highlighter_func highlighter, UcxList *in, 1.34 - void *outbuf, write_func wfnc, size_t maxlen, int showlineno) { 1.35 - /* total written bytes */ 1.36 - size_t written = 0; 1.37 - 1.38 - /* compute width of line numbering */ 1.39 - int lnw = 0; 1.40 - if (showlineno) { 1.41 - size_t lines = ucx_list_size(in); 1.42 - for (size_t p = 1; p < lines ; p*=10) lnw++; 1.43 - } 1.44 - 1.45 - /* start monospace formatting */ 1.46 - try_write(wfnc, "<pre>\n", 6, outbuf, written, maxlen); 1.47 + /* start code formatting */ 1.48 + written += wfnc("<div class=\"c2html-code\">\n", 1, 26, outbuf); 1.49 1.50 /* process lines */ 1.51 - size_t lineno = 0; 1.52 - c2html_highlighter_data* hd = malloc(sizeof(c2html_highlighter_data)); 1.53 - hd->multiline_comment = 0; 1.54 - hd->primary_buffer = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND); 1.55 - hd->secondary_buffer = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND); 1.56 - UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); 1.57 - 1.58 - UCX_FOREACH(sourceline, in) { 1.59 + int lineno = 0; 1.60 + c2html_highlighter_data hd; 1.61 + hd.multiline_comment = 0; 1.62 + cxBufferInit(&hd.primary_buffer, NULL, 256, NULL, CX_BUFFER_AUTO_EXTEND); 1.63 + cxBufferInit(&hd.secondary_buffer, NULL, 32, NULL, CX_BUFFER_AUTO_EXTEND); 1.64 + CxBuffer out_line; 1.65 + cxBufferInit(&out_line, NULL, 128, NULL, CX_BUFFER_AUTO_EXTEND); 1.66 + 1.67 + CxIterator in_lines = cxListIterator(lines); 1.68 + cx_foreach(char*, in_line, in_lines) { 1.69 /* increase line number and clean line buffer */ 1.70 lineno++; 1.71 - ucx_buffer_clear(line); 1.72 - 1.73 + cxBufferClear(&out_line); 1.74 + 1.75 /* write line number */ 1.76 - if (showlineno) { 1.77 - ucx_bprintf(line, "<a class=\"c2html-lineno\" name=\"l%d\" " 1.78 - "href=\"#l%d\">%*d </a>", 1.79 - lineno, lineno, lnw, lineno); 1.80 + if (showln) { 1.81 + cx_bprintf(&out_line, "<a class=\"c2html-lineno\" name=\"l%d\" " 1.82 + "href=\"#l%d\">%*d </a>", 1.83 + lineno, lineno, lnw, lineno); 1.84 } 1.85 - 1.86 + 1.87 /* process code line */ 1.88 - highlighter(sourceline->data, line, hd); 1.89 - 1.90 + highlighter(in_line, &out_line, &hd); 1.91 + 1.92 /* write code line */ 1.93 - try_write(wfnc, line->space, line->size, outbuf, written, maxlen); 1.94 - 1.95 - if (written == maxlen) break; 1.96 + written += wfnc(out_line.space, 1, out_line.size, outbuf); 1.97 } 1.98 - 1.99 - /* end monospace formatting */ 1.100 - try_write(wfnc, "</pre>\n", 7, outbuf, written, maxlen); 1.101 - 1.102 + 1.103 + /* end code formatting */ 1.104 + written += wfnc("</div>\n", 1, 7, outbuf); 1.105 + 1.106 /* cleanup and return */ 1.107 - ucx_buffer_free(hd->primary_buffer); 1.108 - ucx_buffer_free(hd->secondary_buffer); 1.109 - free(hd); 1.110 - ucx_buffer_free(line); 1.111 - 1.112 + cxBufferDestroy(&hd.primary_buffer); 1.113 + cxBufferDestroy(&hd.secondary_buffer); 1.114 + cxBufferDestroy(&out_line); 1.115 + 1.116 return written; 1.117 } 1.118 1.119 -size_t c2html_formatn(void* inputbuffer, read_func rfnc, 1.120 - char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc, 1.121 - size_t maxlen, c2html_highlighter_func hltr, int showln) { 1.122 - 1.123 - UcxBuffer *content = ucx_buffer_new(NULL, ibuflen*2, UCX_BUFFER_AUTOEXTEND); 1.124 - ucx_stream_copy(inputbuffer, content, rfnc, (write_func) ucx_buffer_write, 1.125 - ibuf, ibuflen, (size_t)-1); 1.126 +size_t c2html_bformat( 1.127 + char const *inputbuffer, 1.128 + size_t inputbuflen, 1.129 + void *outbuf, 1.130 + cx_write_func wfnc, 1.131 + c2html_highlighter_func highlighter, 1.132 + int showln 1.133 +) { 1.134 + /* a rough estimate for the number of lines */ 1.135 + size_t est_cap = 16 + inputbuflen / 40; 1.136 1.137 - size_t n = c2html_bformatn(content->space, content->size, 1.138 - outputbuffer, wfnc, maxlen, hltr, showln); 1.139 - 1.140 - ucx_buffer_free(content); 1.141 - return n; 1.142 -} 1.143 - 1.144 -size_t c2html_format(void* inputbuffer, read_func rfnc, 1.145 - char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc, 1.146 - c2html_highlighter_func hltr, int showln) { 1.147 - return c2html_formatn(inputbuffer, rfnc, ibuf, ibuflen, 1.148 - outputbuffer, wfnc, (size_t)-1, hltr, showln); 1.149 -} 1.150 - 1.151 -size_t c2html_fformat(FILE* inputfile, char *ibuf, size_t ibuflen, 1.152 - void* outputbuffer, write_func wfnc, 1.153 - c2html_highlighter_func hltr, int showln) { 1.154 - return c2html_format(inputfile, (read_func) fread, ibuf, ibuflen, 1.155 - outputbuffer, wfnc, hltr, showln); 1.156 -} 1.157 - 1.158 -void c2html_fformatf(FILE *inputfile, char *ibuf, size_t ibuflen, 1.159 - FILE* outputfile, c2html_highlighter_func hltr, int showln) { 1.160 - c2html_format(inputfile, (read_func) fread, ibuf, ibuflen, 1.161 - outputfile, (write_func) fwrite, hltr, showln); 1.162 -} 1.163 - 1.164 -size_t c2html_bformatn(const char* inputbuffer, size_t inputbuflen, 1.165 - void* outputbuffer, write_func wfnc, 1.166 - size_t maxlen, c2html_highlighter_func hltr, int showln) { 1.167 - UcxList *lines = ucx_list_append(NULL, (char*)inputbuffer); 1.168 - for (size_t i = 1 ; i < inputbuflen ; i++) { 1.169 - if (inputbuffer[i] == '\n' && i+1 < inputbuflen) { 1.170 - ucx_list_append(lines, (char*)inputbuffer+i+1); 1.171 + /* create the line pointer array */ 1.172 + CxList *lines = cxArrayListCreateSimple(CX_STORE_POINTERS, est_cap); 1.173 + cxListAdd(lines, inputbuffer); 1.174 + for (size_t i = 1; i < inputbuflen; i++) { 1.175 + if (inputbuffer[i] == '\n' && i + 1 < inputbuflen) { 1.176 + cxListAdd(lines, inputbuffer + i + 1); 1.177 } 1.178 } 1.179 - 1.180 - size_t n = formatlines(hltr, lines, outputbuffer, wfnc, maxlen, showln); 1.181 - 1.182 - ucx_list_free(lines); 1.183 + 1.184 + /* invoke the other function */ 1.185 + size_t n = c2html_format(lines, outbuf, wfnc, highlighter, showln); 1.186 + 1.187 + /* cleanup and return */ 1.188 + cxListDestroy(lines); 1.189 return n; 1.190 -} 1.191 - 1.192 -size_t c2html_bformat(const char* inputbuffer, size_t inputbuflen, 1.193 - void* outputbuffer, write_func wfnc, 1.194 - c2html_highlighter_func hltr, int showln) { 1.195 - return c2html_bformatn(inputbuffer, inputbuflen, outputbuffer, wfnc, 1.196 - (size_t)-1, hltr, showln); 1.197 -} 1.198 - 1.199 -void c2html_bformatf(const char* inputbuffer, size_t inputbuflen, 1.200 - FILE* outputfile, c2html_highlighter_func hltr, int showln) { 1.201 - c2html_bformatn(inputbuffer, inputbuflen, outputfile, 1.202 - (write_func) fwrite, (size_t)-1, hltr, showln); 1.203 -} 1.204 +} 1.205 \ No newline at end of file