Mon, 13 Nov 2017 14:17:46 +0100
removes build/ucx from makefile
universe@23 | 1 | /* |
universe@23 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@23 | 3 | * |
universe@35 | 4 | * Copyright 2016 Mike Becker. All rights reserved. |
universe@23 | 5 | * |
universe@23 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@23 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@23 | 8 | * |
universe@23 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@23 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@23 | 11 | * |
universe@23 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@23 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@23 | 14 | * documentation and/or other materials provided with the distribution. |
universe@23 | 15 | * |
universe@23 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@23 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@23 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@23 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@23 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@23 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@23 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@23 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@23 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@23 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@23 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@23 | 27 | * |
universe@23 | 28 | */ |
universe@52 | 29 | |
universe@22 | 30 | #include "c2html.h" |
universe@1 | 31 | |
universe@61 | 32 | #include <ucx/list.h> |
universe@61 | 33 | #include <ucx/utils.h> |
universe@1 | 34 | |
universe@55 | 35 | #define try_write(wfnc, str, n, buf, written, maxlen) \ |
universe@55 | 36 | { \ |
universe@55 | 37 | size_t m = maxlen-written; \ |
universe@55 | 38 | written += wfnc(str, 1, n > m ? m : n, buf); \ |
universe@53 | 39 | } |
universe@53 | 40 | |
universe@55 | 41 | static size_t formatlines(c2html_highlighter_func highlighter, UcxList *in, |
universe@55 | 42 | void *outbuf, write_func wfnc, size_t maxlen, int showlineno) { |
universe@55 | 43 | /* total written bytes */ |
universe@55 | 44 | size_t written = 0; |
olaf@24 | 45 | |
universe@44 | 46 | /* compute width of line numbering */ |
universe@53 | 47 | int lnw = 0; |
universe@44 | 48 | if (showlineno) { |
universe@44 | 49 | size_t lines = ucx_list_size(in); |
universe@53 | 50 | for (size_t p = 1; p < lines ; p*=10) lnw++; |
universe@44 | 51 | } |
universe@44 | 52 | |
universe@44 | 53 | /* start monospace formatting */ |
universe@55 | 54 | try_write(wfnc, "<pre>\n", 6, outbuf, written, maxlen); |
universe@39 | 55 | |
universe@44 | 56 | /* process lines */ |
universe@39 | 57 | size_t lineno = 0; |
universe@55 | 58 | c2html_highlighter_data* hd = malloc(sizeof(c2html_highlighter_data)); |
universe@55 | 59 | hd->multiline_comment = 0; |
universe@55 | 60 | hd->primary_buffer = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND); |
universe@55 | 61 | hd->secondary_buffer = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND); |
universe@51 | 62 | UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); |
universe@46 | 63 | |
universe@39 | 64 | UCX_FOREACH(sourceline, in) { |
universe@44 | 65 | /* increase line number and clean line buffer */ |
universe@39 | 66 | lineno++; |
universe@44 | 67 | ucx_buffer_clear(line); |
universe@44 | 68 | |
universe@44 | 69 | /* write line number */ |
universe@44 | 70 | if (showlineno) { |
universe@60 | 71 | ucx_bprintf(line, "<a class=\"c2html-lineno\" name=\"l%d\" " |
universe@60 | 72 | "href=\"#l%d\">%*d </a>", |
universe@44 | 73 | lineno, lineno, lnw, lineno); |
universe@44 | 74 | } |
universe@44 | 75 | |
universe@45 | 76 | /* process code line */ |
universe@51 | 77 | highlighter(sourceline->data, line, hd); |
olaf@24 | 78 | |
universe@50 | 79 | /* write code line */ |
universe@55 | 80 | try_write(wfnc, line->space, line->size, outbuf, written, maxlen); |
universe@55 | 81 | |
universe@55 | 82 | if (written == maxlen) break; |
olaf@24 | 83 | } |
olaf@24 | 84 | |
universe@44 | 85 | /* end monospace formatting */ |
universe@55 | 86 | try_write(wfnc, "</pre>\n", 7, outbuf, written, maxlen); |
universe@44 | 87 | |
universe@44 | 88 | /* cleanup and return */ |
universe@55 | 89 | ucx_buffer_free(hd->primary_buffer); |
universe@55 | 90 | ucx_buffer_free(hd->secondary_buffer); |
universe@55 | 91 | free(hd); |
universe@39 | 92 | ucx_buffer_free(line); |
universe@55 | 93 | |
universe@55 | 94 | return written; |
olaf@24 | 95 | } |
olaf@24 | 96 | |
universe@55 | 97 | size_t c2html_formatn(void* inputbuffer, read_func rfnc, |
universe@55 | 98 | char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc, |
universe@55 | 99 | size_t maxlen, c2html_highlighter_func hltr, int showln) { |
universe@55 | 100 | |
universe@55 | 101 | UcxBuffer *content = ucx_buffer_new(NULL, ibuflen*2, UCX_BUFFER_AUTOEXTEND); |
universe@61 | 102 | ucx_stream_bcopy(inputbuffer, content, rfnc, (write_func) ucx_buffer_write, |
universe@61 | 103 | ibuf, ibuflen); |
universe@42 | 104 | |
universe@57 | 105 | size_t n = c2html_bformatn(content->space, content->size, |
universe@57 | 106 | outputbuffer, wfnc, maxlen, hltr, showln); |
universe@55 | 107 | |
universe@55 | 108 | ucx_buffer_free(content); |
universe@55 | 109 | return n; |
universe@1 | 110 | } |
universe@1 | 111 | |
universe@55 | 112 | size_t c2html_format(void* inputbuffer, read_func rfnc, |
universe@55 | 113 | char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc, |
universe@55 | 114 | c2html_highlighter_func hltr, int showln) { |
universe@55 | 115 | return c2html_formatn(inputbuffer, rfnc, ibuf, ibuflen, |
universe@55 | 116 | outputbuffer, wfnc, (size_t)-1, hltr, showln); |
universe@55 | 117 | } |
universe@55 | 118 | |
universe@57 | 119 | size_t c2html_fformat(FILE* inputfile, char *ibuf, size_t ibuflen, |
universe@55 | 120 | void* outputbuffer, write_func wfnc, |
universe@55 | 121 | c2html_highlighter_func hltr, int showln) { |
universe@55 | 122 | return c2html_format(inputfile, (read_func) fread, ibuf, ibuflen, |
universe@55 | 123 | outputbuffer, wfnc, hltr, showln); |
universe@55 | 124 | } |
universe@55 | 125 | |
universe@57 | 126 | void c2html_fformatf(FILE *inputfile, char *ibuf, size_t ibuflen, |
universe@55 | 127 | FILE* outputfile, c2html_highlighter_func hltr, int showln) { |
universe@55 | 128 | c2html_format(inputfile, (read_func) fread, ibuf, ibuflen, |
universe@55 | 129 | outputfile, (write_func) fwrite, hltr, showln); |
universe@55 | 130 | } |
universe@57 | 131 | |
universe@57 | 132 | size_t c2html_bformatn(const char* inputbuffer, size_t inputbuflen, |
universe@57 | 133 | void* outputbuffer, write_func wfnc, |
universe@57 | 134 | size_t maxlen, c2html_highlighter_func hltr, int showln) { |
universe@57 | 135 | UcxList *lines = ucx_list_append(NULL, (char*)inputbuffer); |
universe@57 | 136 | for (size_t i = 1 ; i < inputbuflen ; i++) { |
universe@57 | 137 | if (inputbuffer[i] == '\n' && i+1 < inputbuflen) { |
universe@57 | 138 | ucx_list_append(lines, (char*)inputbuffer+i+1); |
universe@57 | 139 | } |
universe@57 | 140 | } |
universe@57 | 141 | |
universe@57 | 142 | size_t n = formatlines(hltr, lines, outputbuffer, wfnc, maxlen, showln); |
universe@57 | 143 | |
universe@57 | 144 | ucx_list_free(lines); |
universe@57 | 145 | return n; |
universe@57 | 146 | } |
universe@57 | 147 | |
universe@57 | 148 | size_t c2html_bformat(const char* inputbuffer, size_t inputbuflen, |
universe@57 | 149 | void* outputbuffer, write_func wfnc, |
universe@57 | 150 | c2html_highlighter_func hltr, int showln) { |
universe@57 | 151 | return c2html_bformatn(inputbuffer, inputbuflen, outputbuffer, wfnc, |
universe@57 | 152 | (size_t)-1, hltr, showln); |
universe@57 | 153 | } |
universe@57 | 154 | |
universe@57 | 155 | void c2html_bformatf(const char* inputbuffer, size_t inputbuflen, |
universe@57 | 156 | FILE* outputfile, c2html_highlighter_func hltr, int showln) { |
universe@57 | 157 | c2html_bformatn(inputbuffer, inputbuflen, outputfile, |
universe@57 | 158 | (write_func) fwrite, (size_t)-1, hltr, showln); |
universe@57 | 159 | } |