Wed, 31 Aug 2016 14:41:56 +0200
adds appropriate public API
Makefile | file | annotate | diff | comparison | revisions | |
src/c2html.c | file | annotate | diff | comparison | revisions | |
src/c2html.h | file | annotate | diff | comparison | revisions | |
src/frontend.c | file | annotate | diff | comparison | revisions | |
src/highlighter.c | file | annotate | diff | comparison | revisions | |
src/highlighter.h | file | annotate | diff | comparison | revisions |
1.1 --- a/Makefile Wed Aug 31 12:58:48 2016 +0200 1.2 +++ b/Makefile Wed Aug 31 14:41:56 2016 +0200 1.3 @@ -31,8 +31,8 @@ 1.4 1.5 include $(CONF).mk 1.6 1.7 - 1.8 -SRC = c2html.c 1.9 +SRC = frontend.c 1.10 +SRC += c2html.c 1.11 SRC += highlighter.c 1.12 SRC += ucx/allocator.c 1.13 SRC += ucx/buffer.c
2.1 --- a/src/c2html.c Wed Aug 31 12:58:48 2016 +0200 2.2 +++ b/src/c2html.c Wed Aug 31 14:41:56 2016 +0200 2.3 @@ -27,47 +27,21 @@ 2.4 * 2.5 */ 2.6 2.7 -#include <unistd.h> 2.8 - 2.9 #include "c2html.h" 2.10 -#include "highlighter.h" 2.11 2.12 #include "ucx/list.h" 2.13 +#include "ucx/utils.h" 2.14 2.15 -static int appendfile(const char *filename, FILE *fout, 2.16 - char *copybuf, size_t copybuflen, const char *errmsg) { 2.17 - FILE *headerfile = fopen(filename, "r"); 2.18 - if (!headerfile) { 2.19 - perror(errmsg); 2.20 - if (fout != stdout) { 2.21 - fclose(fout); 2.22 - } 2.23 - return 1; 2.24 +#define try_write(wfnc, str, n, buf, written, maxlen) \ 2.25 + { \ 2.26 + size_t m = maxlen-written; \ 2.27 + written += wfnc(str, 1, n > m ? m : n, buf); \ 2.28 } 2.29 - ucx_stream_copy(headerfile, fout, 2.30 - (read_func) fread, (write_func) fwrite, 2.31 - copybuf, copybuflen, (size_t)-1); 2.32 - fclose(headerfile); 2.33 - return 0; 2.34 -} 2.35 2.36 -static void printhelp() { 2.37 - printf("Formats source code using HTML.\n\nUsage:\n" 2.38 - " c2html [Options] FILE\n\n" 2.39 - " Options:\n" 2.40 - " -h Prints this help message\n" 2.41 - " -j Highlight Java instead of C source code\n" 2.42 - " -o <output> Output file (stdout, if not specified)\n" 2.43 - " -H <header> Prepend header file\n" 2.44 - " -F <footer> Append footer file\n" 2.45 - " -p Disable highlighting (plain text)\n" 2.46 - " -l Disable line numbers\n" 2.47 - " -V, -v Prints version and exits\n" 2.48 - "\n"); 2.49 -} 2.50 - 2.51 -static void formatlines(highlighter_func highlighter, 2.52 - UcxList *in, write_func out, void *stream, int showlineno) { 2.53 +static size_t formatlines(c2html_highlighter_func highlighter, UcxList *in, 2.54 + void *outbuf, write_func wfnc, size_t maxlen, int showlineno) { 2.55 + /* total written bytes */ 2.56 + size_t written = 0; 2.57 2.58 /* compute width of line numbering */ 2.59 int lnw = 0; 2.60 @@ -77,16 +51,15 @@ 2.61 } 2.62 2.63 /* start monospace formatting */ 2.64 - out("<pre>\n", 1, 6, stream); 2.65 + try_write(wfnc, "<pre>\n", 6, outbuf, written, maxlen); 2.66 2.67 /* process lines */ 2.68 size_t lineno = 0; 2.69 - HighlighterData *hd = new_highlighter_data(); 2.70 + c2html_highlighter_data* hd = malloc(sizeof(c2html_highlighter_data)); 2.71 + hd->multiline_comment = 0; 2.72 + hd->primary_buffer = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND); 2.73 + hd->secondary_buffer = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND); 2.74 UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); 2.75 - if(!line || !hd) { 2.76 - perror("Error allocating buffer for output"); 2.77 - return; 2.78 - } 2.79 2.80 UCX_FOREACH(sourceline, in) { 2.81 /* increase line number and clean line buffer */ 2.82 @@ -104,163 +77,63 @@ 2.83 highlighter(sourceline->data, line, hd); 2.84 2.85 /* write code line */ 2.86 - out(line->space, 1, line->size, stream); 2.87 + try_write(wfnc, line->space, line->size, outbuf, written, maxlen); 2.88 + 2.89 + if (written == maxlen) break; 2.90 } 2.91 2.92 /* end monospace formatting */ 2.93 - out("</pre>\n", 1, 7, stream); 2.94 + try_write(wfnc, "</pre>\n", 7, outbuf, written, maxlen); 2.95 2.96 /* cleanup and return */ 2.97 - free_highlighter_data(hd); 2.98 + ucx_buffer_free(hd->primary_buffer); 2.99 + ucx_buffer_free(hd->secondary_buffer); 2.100 + free(hd); 2.101 ucx_buffer_free(line); 2.102 + 2.103 + return written; 2.104 } 2.105 2.106 -#define FILEBUF_SIZE 4096 2.107 +size_t c2html_formatn(void* inputbuffer, read_func rfnc, 2.108 + char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc, 2.109 + size_t maxlen, c2html_highlighter_func hltr, int showln) { 2.110 + 2.111 + UcxBuffer *content = ucx_buffer_new(NULL, ibuflen*2, UCX_BUFFER_AUTOEXTEND); 2.112 + ucx_stream_copy(inputbuffer, content, rfnc, (write_func) ucx_buffer_write, 2.113 + ibuf, ibuflen, (size_t)-1); 2.114 2.115 -enum source_type { 2.116 - SOURCE_C, 2.117 - SOURCE_JAVA, 2.118 - SOURCE_PLAIN 2.119 -}; 2.120 - 2.121 -int main(int argc, char** argv) { 2.122 - 2.123 - /* Default settings */ 2.124 - Settings settings; 2.125 - memset(&settings, 0, sizeof(settings)); 2.126 - settings.showlinenumbers = 1; 2.127 - enum source_type sourcetype = SOURCE_C; 2.128 - 2.129 - /* Parse command line */ 2.130 - char optc; 2.131 - while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) { 2.132 - switch (optc) { 2.133 - case 'o': 2.134 - if (!(optarg[0] == '-' && optarg[1] == 0)) { 2.135 - settings.outfilename = optarg; 2.136 - } 2.137 - break; 2.138 - case 'F': 2.139 - settings.footerfile = optarg; 2.140 - break; 2.141 - case 'H': 2.142 - settings.headerfile = optarg; 2.143 - break; 2.144 - case 'j': 2.145 - sourcetype = SOURCE_JAVA; 2.146 - break; 2.147 - case 'p': 2.148 - sourcetype = SOURCE_PLAIN; 2.149 - break; 2.150 - case 'l': 2.151 - settings.showlinenumbers = 0; 2.152 - break; 2.153 - case 'h': 2.154 - printhelp(); 2.155 - return EXIT_SUCCESS; 2.156 - case 'v': 2.157 - case 'V': 2.158 -#ifdef VERSION_DEVELOP 2.159 - printf("%d.%d (unstable)\n", VERSION_MAJOR, VERSION_MINOR); 2.160 -#else 2.161 - printf("%d.%d\n", VERSION_MAJOR, VERSION_MINOR); 2.162 -#endif 2.163 - return EXIT_SUCCESS; 2.164 - default: 2.165 - return EXIT_FAILURE; 2.166 + UcxList *lines = ucx_list_append(NULL, content->space); 2.167 + for (size_t i = 1 ; i < content->size ; i++) { 2.168 + if (content->space[i] == '\r') { 2.169 + content->space[i] = '\n'; i++; 2.170 + } 2.171 + if (content->space[i] == '\n' && i+1 < content->size) { 2.172 + ucx_list_append(lines, content->space+i+1); 2.173 } 2.174 } 2.175 - 2.176 - if (optind != argc-1) { 2.177 - printhelp(); 2.178 - return EXIT_FAILURE; 2.179 - } else { 2.180 - /* Choose highlighter */ 2.181 - highlighter_func hltr = NULL; 2.182 - switch (sourcetype) { 2.183 - case SOURCE_C: 2.184 - hltr = c_highlighter; 2.185 - break; 2.186 - case SOURCE_JAVA: 2.187 - hltr = java_highlighter; 2.188 - break; 2.189 - case SOURCE_PLAIN: 2.190 - hltr = plain_highlighter; 2.191 - break; 2.192 - default: /* should be unreachable */ 2.193 - fprintf(stderr, "error in enum source_type\n"); 2.194 - return EXIT_FAILURE; 2.195 - } 2.196 - 2.197 - /* Open output file */ 2.198 - settings.infilename = argv[optind]; 2.199 - FILE *fout; 2.200 - if (settings.outfilename) { 2.201 - fout = fopen(settings.outfilename, "w"); 2.202 - if (!fout) { 2.203 - perror("Error opening output file"); 2.204 - return EXIT_FAILURE; 2.205 - } 2.206 - } else { 2.207 - fout = stdout; 2.208 - } 2.209 - 2.210 - /* Allocate file buffer */ 2.211 - char *filebuf = malloc(FILEBUF_SIZE); 2.212 - if (!filebuf) { 2.213 - perror("Error allocating file buffer"); 2.214 - return EXIT_FAILURE; 2.215 - } 2.216 - 2.217 - /* Prepend header file */ 2.218 - if (appendfile(settings.headerfile, fout, filebuf, FILEBUF_SIZE, 2.219 - "Error opening header file")) { 2.220 - return EXIT_FAILURE; 2.221 - } 2.222 - 2.223 - /* Process input file */ 2.224 - FILE *inputfile = fopen(settings.infilename, "r"); 2.225 - if (inputfile) { 2.226 - UcxBuffer *content = ucx_buffer_new(NULL, 2.227 - FILEBUF_SIZE*2, UCX_BUFFER_AUTOEXTEND); 2.228 - { 2.229 - ucx_stream_copy(inputfile, content, (read_func) fread, 2.230 - (write_func) ucx_buffer_write, 2.231 - filebuf, FILEBUF_SIZE, (size_t)-1); 2.232 - } 2.233 - fclose(inputfile); 2.234 - 2.235 - UcxList *inputlines = ucx_list_append(NULL, content->space); 2.236 - for (size_t i = 1 ; i < content->size ; i++) { 2.237 - if (content->space[i] == '\r') { 2.238 - content->space[i] = '\n'; i++; 2.239 - } 2.240 - if (content->space[i] == '\n' && i+1 < content->size) { 2.241 - ucx_list_append(inputlines, content->space+i+1); 2.242 - } 2.243 - } 2.244 - 2.245 - formatlines(hltr, inputlines, 2.246 - (write_func) fwrite, fout, settings.showlinenumbers); 2.247 - 2.248 - ucx_buffer_free(content); 2.249 - } else { 2.250 - perror("Error opening input file"); 2.251 - if (fout != stdout) { 2.252 - fclose(fout); 2.253 - } 2.254 - return EXIT_FAILURE; 2.255 - } 2.256 - 2.257 - /* Append footer file */ 2.258 - if (appendfile(settings.footerfile, fout, filebuf, FILEBUF_SIZE, 2.259 - "Error opening footer file")) { 2.260 - return EXIT_FAILURE; 2.261 - } 2.262 - 2.263 - free(filebuf); 2.264 - 2.265 - return EXIT_SUCCESS; 2.266 - } 2.267 + 2.268 + size_t n = formatlines(hltr, lines, outputbuffer, wfnc, maxlen, showln); 2.269 + 2.270 + ucx_buffer_free(content); 2.271 + return n; 2.272 } 2.273 2.274 +size_t c2html_format(void* inputbuffer, read_func rfnc, 2.275 + char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc, 2.276 + c2html_highlighter_func hltr, int showln) { 2.277 + return c2html_formatn(inputbuffer, rfnc, ibuf, ibuflen, 2.278 + outputbuffer, wfnc, (size_t)-1, hltr, showln); 2.279 +} 2.280 + 2.281 +size_t c2html_format_file(FILE* inputfile, char *ibuf, size_t ibuflen, 2.282 + void* outputbuffer, write_func wfnc, 2.283 + c2html_highlighter_func hltr, int showln) { 2.284 + return c2html_format(inputfile, (read_func) fread, ibuf, ibuflen, 2.285 + outputbuffer, wfnc, hltr, showln); 2.286 +} 2.287 + 2.288 +void c2html_fformat_file(FILE *inputfile, char *ibuf, size_t ibuflen, 2.289 + FILE* outputfile, c2html_highlighter_func hltr, int showln) { 2.290 + c2html_format(inputfile, (read_func) fread, ibuf, ibuflen, 2.291 + outputfile, (write_func) fwrite, hltr, showln); 2.292 +}
3.1 --- a/src/c2html.h Wed Aug 31 12:58:48 2016 +0200 3.2 +++ b/src/c2html.h Wed Aug 31 14:41:56 2016 +0200 3.3 @@ -30,6 +30,9 @@ 3.4 #ifndef C2HTML_H 3.5 #define C2HTML_H 3.6 3.7 +#include <stdio.h> 3.8 +#include "highlighter.h" 3.9 + 3.10 #ifdef __cplusplus 3.11 extern "C" { 3.12 #endif 3.13 @@ -38,14 +41,102 @@ 3.14 #define VERSION_MINOR 0 3.15 #define VERSION_DEVELOP 1 /* set this to zero for release version */ 3.16 3.17 -typedef struct { 3.18 - char* outfilename; 3.19 - char* headerfile; 3.20 - char* footerfile; 3.21 - char* infilename; 3.22 - int showlinenumbers; 3.23 -} Settings; 3.24 +/** 3.25 + * Reads a source file from the input buffer and writes at most 3.26 + * <code>maxlen</code> bytes of the formatted output to the output buffer. 3.27 + * 3.28 + * The output buffer must either be large enough to hold <code>maxlen</code> 3.29 + * bytes or the write function must trigger an automatic extension of the buffer. 3.30 + * 3.31 + * @param inputbuffer the input buffer 3.32 + * @param rfnc a read function operating for the input buffer 3.33 + * @param ibuf intermediate processing buffer 3.34 + * @param ibuflen length of intermediate processing buffer (recommended: 4 KB) 3.35 + * @param outputbuffer the output buffer 3.36 + * @param wfnc a write function for the output buffer 3.37 + * @param maxlen the maximum amount bytes which will be written to the 3.38 + * output buffer 3.39 + * @param hltr the highlighter function 3.40 + * @param showln zero, if line numbers shall be omitted, nonzero otherwise 3.41 + * 3.42 + * @return total amount of bytes written to the output buffer 3.43 + * 3.44 + * @see c2html_plain_highlighter() 3.45 + * @see c2html_c_highlighter() 3.46 + * @see c2html_java_highlighter() 3.47 + */ 3.48 +size_t c2html_formatn(void* inputbuffer, read_func rfnc, 3.49 + char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc, 3.50 + size_t maxlen, c2html_highlighter_func hltr, int showln); 3.51 + 3.52 +/** 3.53 + * Reads a source file from the input buffer and writes the formatted output 3.54 + * to an output buffer. 3.55 + * 3.56 + * The output buffer must either be large enough to hold the formatted data 3.57 + * or the write function must trigger an automatic extension of the buffer. 3.58 + * 3.59 + * @param inputbuffer the input buffer 3.60 + * @param rfnc a read function operating for the input buffer 3.61 + * @param ibuf intermediate processing buffer 3.62 + * @param ibuflen length of intermediate processing buffer (recommended: 4 KB) 3.63 + * @param outputbuffer the output buffer 3.64 + * @param wfnc a write function for the output buffer 3.65 + * @param hltr the highlighter function 3.66 + * @param showln zero, if line numbers shall be omitted, nonzero otherwise 3.67 + * 3.68 + * @return total amount of bytes written to the output buffer 3.69 + * 3.70 + * @see c2html_plain_highlighter() 3.71 + * @see c2html_c_highlighter() 3.72 + * @see c2html_java_highlighter() 3.73 + */ 3.74 +size_t c2html_format(void* inputbuffer, read_func rfnc, 3.75 + char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc, 3.76 + c2html_highlighter_func hltr, int showln); 3.77 3.78 +/** 3.79 + * Reads a source file from the specified input file stream and writes the 3.80 + * formatted output to an output buffer. 3.81 + * 3.82 + * The output buffer must either be large enough to hold the formatted data 3.83 + * or the write function must trigger an automatic extension of the buffer. 3.84 + * 3.85 + * @param inputfile the input file stream 3.86 + * @param ibuf intermediate processing buffer 3.87 + * @param ibuflen length of intermediate processing buffer (recommended: 4 KB) 3.88 + * @param outputbuffer the output buffer 3.89 + * @param wfnc a write function for the output buffer 3.90 + * @param hltr the highlighter function 3.91 + * @param showln zero, if line numbers shall be omitted, nonzero otherwise 3.92 + * 3.93 + * @return total amount of bytes written to the output buffer 3.94 + * 3.95 + * @see c2html_plain_highlighter() 3.96 + * @see c2html_c_highlighter() 3.97 + * @see c2html_java_highlighter() 3.98 + */ 3.99 +size_t c2html_format_file(FILE* inputfile, char *ibuf, size_t ibuflen, 3.100 + void* outputbuffer, write_func wfnc, 3.101 + c2html_highlighter_func hltr, int showln); 3.102 + 3.103 +/** 3.104 + * Reads a source file from the specified input file stream and directly writes 3.105 + * the formatted output to the output file stream. 3.106 + * 3.107 + * @param inputfile the input file stream 3.108 + * @param ibuf intermediate processing buffer 3.109 + * @param ibuflen length of intermediate processing buffer (recommended: 4 KB) 3.110 + * @param outputfile the output file stream 3.111 + * @param hltr the highlighter function 3.112 + * @param showln zero, if line numbers shall be omitted, nonzero otherwise 3.113 + * 3.114 + * @see c2html_plain_highlighter() 3.115 + * @see c2html_c_highlighter() 3.116 + * @see c2html_java_highlighter() 3.117 + */ 3.118 +void c2html_fformat_file(FILE *inputfile, char *ibuf, size_t ibuflen, 3.119 + FILE* outputfile, c2html_highlighter_func hltr, int showln); 3.120 3.121 #ifdef __cplusplus 3.122 }
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/src/frontend.c Wed Aug 31 14:41:56 2016 +0200 4.3 @@ -0,0 +1,185 @@ 4.4 +/* 4.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 4.6 + * 4.7 + * Copyright 2016 Mike Becker. All rights reserved. 4.8 + * 4.9 + * Redistribution and use in source and binary forms, with or without 4.10 + * modification, are permitted provided that the following conditions are met: 4.11 + * 4.12 + * 1. Redistributions of source code must retain the above copyright 4.13 + * notice, this list of conditions and the following disclaimer. 4.14 + * 4.15 + * 2. Redistributions in binary form must reproduce the above copyright 4.16 + * notice, this list of conditions and the following disclaimer in the 4.17 + * documentation and/or other materials provided with the distribution. 4.18 + * 4.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 4.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 4.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 4.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 4.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 4.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 4.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 4.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 4.29 + * POSSIBILITY OF SUCH DAMAGE. 4.30 + * 4.31 + */ 4.32 + 4.33 +#include <stdio.h> 4.34 +#include <stdlib.h> 4.35 +#include <unistd.h> 4.36 +#include <string.h> 4.37 + 4.38 +#include "c2html.h" 4.39 +#include "ucx/utils.h" 4.40 + 4.41 +#define FILEBUF_SIZE 4096 4.42 + 4.43 +typedef struct { 4.44 + char* outfilename; 4.45 + char* headerfile; 4.46 + char* footerfile; 4.47 + char* infilename; 4.48 + int showlinenumbers; 4.49 +} Settings; 4.50 + 4.51 +static int appendfile(const char *filename, FILE *fout, 4.52 + char *copybuf, size_t copybuflen, const char *errmsg) { 4.53 + FILE *headerfile = fopen(filename, "r"); 4.54 + if (!headerfile) { 4.55 + perror(errmsg); 4.56 + if (fout != stdout) { 4.57 + fclose(fout); 4.58 + } 4.59 + return 1; 4.60 + } 4.61 + ucx_stream_copy(headerfile, fout, 4.62 + (read_func) fread, (write_func) fwrite, 4.63 + copybuf, copybuflen, (size_t)-1); 4.64 + fclose(headerfile); 4.65 + return 0; 4.66 +} 4.67 + 4.68 +static void printhelp() { 4.69 + printf("Formats source code using HTML.\n\nUsage:\n" 4.70 + " c2html [Options] FILE\n\n" 4.71 + " Options:\n" 4.72 + " -h Prints this help message\n" 4.73 + " -j Highlight Java instead of C source code\n" 4.74 + " -o <output> Output file (stdout, if not specified)\n" 4.75 + " -H <header> Prepend header file\n" 4.76 + " -F <footer> Append footer file\n" 4.77 + " -p Disable highlighting (plain text)\n" 4.78 + " -l Disable line numbers\n" 4.79 + " -V, -v Prints version and exits\n" 4.80 + "\n"); 4.81 +} 4.82 + 4.83 +int main(int argc, char** argv) { 4.84 + 4.85 + /* Default settings */ 4.86 + Settings settings; 4.87 + memset(&settings, 0, sizeof(settings)); 4.88 + settings.showlinenumbers = 1; 4.89 + c2html_highlighter_func hltr = c2html_c_highlighter; 4.90 + 4.91 + /* Parse command line */ 4.92 + char optc; 4.93 + while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) { 4.94 + switch (optc) { 4.95 + case 'o': 4.96 + if (!(optarg[0] == '-' && optarg[1] == 0)) { 4.97 + settings.outfilename = optarg; 4.98 + } 4.99 + break; 4.100 + case 'F': 4.101 + settings.footerfile = optarg; 4.102 + break; 4.103 + case 'H': 4.104 + settings.headerfile = optarg; 4.105 + break; 4.106 + case 'j': 4.107 + hltr = c2html_java_highlighter; 4.108 + break; 4.109 + case 'p': 4.110 + hltr = c2html_plain_highlighter; 4.111 + break; 4.112 + case 'l': 4.113 + settings.showlinenumbers = 0; 4.114 + break; 4.115 + case 'h': 4.116 + printhelp(); 4.117 + return EXIT_SUCCESS; 4.118 + case 'v': 4.119 + case 'V': 4.120 +#ifdef VERSION_DEVELOP 4.121 + printf("%d.%d (unstable)\n", VERSION_MAJOR, VERSION_MINOR); 4.122 +#else 4.123 + printf("%d.%d\n", VERSION_MAJOR, VERSION_MINOR); 4.124 +#endif 4.125 + return EXIT_SUCCESS; 4.126 + default: 4.127 + return EXIT_FAILURE; 4.128 + } 4.129 + } 4.130 + 4.131 + if (optind != argc-1) { 4.132 + printhelp(); 4.133 + return EXIT_FAILURE; 4.134 + } else { 4.135 + /* Open output file */ 4.136 + settings.infilename = argv[optind]; 4.137 + FILE *fout; 4.138 + if (settings.outfilename) { 4.139 + fout = fopen(settings.outfilename, "w"); 4.140 + if (!fout) { 4.141 + perror("Error opening output file"); 4.142 + return EXIT_FAILURE; 4.143 + } 4.144 + } else { 4.145 + fout = stdout; 4.146 + } 4.147 + 4.148 + /* Allocate file buffer */ 4.149 + char *filebuf = malloc(FILEBUF_SIZE); 4.150 + if (!filebuf) { 4.151 + perror("Error allocating file buffer"); 4.152 + return EXIT_FAILURE; 4.153 + } 4.154 + 4.155 + /* Prepend header file */ 4.156 + if (appendfile(settings.headerfile, fout, filebuf, FILEBUF_SIZE, 4.157 + "Error opening header file")) { 4.158 + return EXIT_FAILURE; 4.159 + } 4.160 + 4.161 + /* Process input file */ 4.162 + FILE *inputfile = fopen(settings.infilename, "r"); 4.163 + if (inputfile) { 4.164 + c2html_fformat_file( 4.165 + inputfile, filebuf, FILEBUF_SIZE, 4.166 + fout, hltr, settings.showlinenumbers 4.167 + ); 4.168 + fclose(inputfile); 4.169 + } else { 4.170 + perror("Error opening input file"); 4.171 + if (fout != stdout) { 4.172 + fclose(fout); 4.173 + } 4.174 + return EXIT_FAILURE; 4.175 + } 4.176 + 4.177 + /* Append footer file */ 4.178 + if (appendfile(settings.footerfile, fout, filebuf, FILEBUF_SIZE, 4.179 + "Error opening footer file")) { 4.180 + return EXIT_FAILURE; 4.181 + } 4.182 + 4.183 + free(filebuf); 4.184 + 4.185 + return EXIT_SUCCESS; 4.186 + } 4.187 +} 4.188 +
5.1 --- a/src/highlighter.c Wed Aug 31 12:58:48 2016 +0200 5.2 +++ b/src/highlighter.c Wed Aug 31 14:41:56 2016 +0200 5.3 @@ -29,23 +29,12 @@ 5.4 5.5 #include "highlighter.h" 5.6 5.7 -HighlighterData* new_highlighter_data() { 5.8 - HighlighterData* hd = malloc(sizeof(HighlighterData)); 5.9 - if (hd == NULL) { 5.10 - return NULL; 5.11 - } else { 5.12 - hd->multiline_comment = 0; 5.13 - hd->primary_buffer = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND); 5.14 - hd->secondary_buffer = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND); 5.15 - return hd; 5.16 - } 5.17 -} 5.18 - 5.19 -void free_highlighter_data(HighlighterData *hd) { 5.20 - ucx_buffer_free(hd->primary_buffer); 5.21 - ucx_buffer_free(hd->secondary_buffer); 5.22 - free(hd); 5.23 -} 5.24 +#include <stdlib.h> 5.25 +#include <stdio.h> 5.26 +#include <string.h> 5.27 +#include <ctype.h> 5.28 +#include "ucx/string.h" 5.29 +#include "ucx/utils.h" 5.30 5.31 static void put_htmlescaped(UcxBuffer *dest, char c) { 5.32 if (c == '>') { 5.33 @@ -84,7 +73,7 @@ 5.34 5.35 /* Plaintext Highlighter */ 5.36 5.37 -void plain_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) { 5.38 +void c2html_plain_highlighter(char *src, UcxBuffer *dest, c2html_highlighter_data *hd) { 5.39 while (*src && *src != '\n') { 5.40 put_htmlescaped(dest, *src); 5.41 src++; 5.42 @@ -102,7 +91,7 @@ 5.43 "while", NULL 5.44 }; 5.45 5.46 -void c_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) { 5.47 +void c2html_c_highlighter(char *src, UcxBuffer *dest, c2html_highlighter_data *hd) { 5.48 /* reset buffers without clearing them */ 5.49 hd->primary_buffer->size = hd->primary_buffer->pos = 0; 5.50 hd->secondary_buffer->size = hd->secondary_buffer->pos = 0; 5.51 @@ -245,7 +234,7 @@ 5.52 "volatile", "const", "float", "native", "super", "while", NULL 5.53 }; 5.54 5.55 -void java_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) { 5.56 +void c2html_java_highlighter(char *src, UcxBuffer *dest, c2html_highlighter_data *hd) { 5.57 /* reset buffers without clearing them */ 5.58 hd->primary_buffer->size = hd->primary_buffer->pos = 0; 5.59 hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
6.1 --- a/src/highlighter.h Wed Aug 31 12:58:48 2016 +0200 6.2 +++ b/src/highlighter.h Wed Aug 31 14:41:56 2016 +0200 6.3 @@ -30,13 +30,7 @@ 6.4 #ifndef CODEGENS_H 6.5 #define CODEGENS_H 6.6 6.7 -#include <stdlib.h> 6.8 -#include <stdio.h> 6.9 -#include <string.h> 6.10 -#include <ctype.h> 6.11 -#include "ucx/string.h" 6.12 #include "ucx/buffer.h" 6.13 -#include "ucx/utils.h" 6.14 6.15 #ifdef __cplusplus 6.16 extern "C" { 6.17 @@ -46,16 +40,16 @@ 6.18 int multiline_comment; 6.19 UcxBuffer* primary_buffer; 6.20 UcxBuffer* secondary_buffer; 6.21 -} HighlighterData; 6.22 +} c2html_highlighter_data; 6.23 6.24 -HighlighterData* new_highlighter_data(); 6.25 -void free_highlighter_data(HighlighterData*); 6.26 +#define C2HTML_HIGHLIGHTER_SIGNATURE \ 6.27 +char*,UcxBuffer*, c2html_highlighter_data* 6.28 6.29 -typedef void(*highlighter_func)(char*,UcxBuffer*,HighlighterData*); 6.30 +typedef void(*c2html_highlighter_func)(C2HTML_HIGHLIGHTER_SIGNATURE); 6.31 6.32 -void plain_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd); 6.33 -void c_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd); 6.34 -void java_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd); 6.35 +void c2html_plain_highlighter(C2HTML_HIGHLIGHTER_SIGNATURE); 6.36 +void c2html_c_highlighter(C2HTML_HIGHLIGHTER_SIGNATURE); 6.37 +void c2html_java_highlighter(C2HTML_HIGHLIGHTER_SIGNATURE); 6.38 6.39 #ifdef __cplusplus 6.40 }