Tue, 23 Aug 2016 15:07:29 +0200
cleans up formatfile function up to the parser call
src/c2html.c | file | annotate | diff | comparison | revisions |
--- a/src/c2html.c Tue Aug 23 14:31:02 2016 +0200 +++ b/src/c2html.c Tue Aug 23 15:07:29 2016 +0200 @@ -47,67 +47,70 @@ "\n"); } -#define WRITECONST(stream, out, cstr) out(cstr, 1, sizeof(cstr)-1, stream) int formatfile( highlighter_t *highlighter, UcxList *in, - write_func out, - void *stream, + write_func out, void *stream, int showlineno) { - size_t lines = ucx_list_size(in); - UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); - if(!line) { - return 1; - } - WRITECONST(stream, out, "<pre>\n"); - + /* compute width of line numbering */ int lnw; - { + if (showlineno) { + size_t lines = ucx_list_size(in); lnw = 1; int p = 1; while ((p*=10) < lines) lnw++; } + + /* allocate line buffer */ + UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); + if(!line) { + return 1; + } + + /* start monospace formatting */ + out("<pre>\n", 1, 6, stream); + /* process lines */ size_t lineno = 0; UCX_FOREACH(sourceline, in) { + /* increase line number and clean line buffer */ lineno++; + ucx_buffer_clear(line); + + /* write line number */ + if (showlineno) { + ucx_bprintf(line, "<span class=\"c2html-lineno\">" + "<a name=\"l%d\" href=\"#l%d\">%*d </a></span> ", + lineno, lineno, lnw, lineno); + } + /* TODO: backwards compatibility: replace line->space in all occasions * and use UcxBuffer functions */ + char *buf = line->space + line->pos; if (highlighter) { - highlighter->parser(sourceline->data, line->space, highlighter); + highlighter->parser(sourceline->data, buf, highlighter); } else { char *c = sourceline->data; size_t dp = 0; while (*c && *c != '\n') { - dp = writeescapedchar(line->space, dp, *c); + dp = writeescapedchar(buf, dp, *c); c++; } - line->space[dp++] = '\n'; - line->space[dp] = '\0'; + buf[dp++] = '\n'; + buf[dp] = '\0'; } - - // write line number - if (showlineno) { - WRITECONST(stream, out, "<span class=\"c2html-lineno\">"); - char lnbuf[128]; - int len; - // line number link - len = snprintf(lnbuf, 128, "<a name=\"l%d\" href=\"#l%d\">", - lineno, lineno); - out(lnbuf, 1, len, stream); - // justified line number - len = snprintf(lnbuf, 128, "%*d ", lnw, lineno); - out(lnbuf, 1, len, stream); - WRITECONST(stream, out, "</a></span> "); - } + line->size = strlen(line->space); - // write formated (or plain) code line - out(line->space, 1, strlen(line->space), stream); + /* write code line */ + out(line->space, 1, line->size, stream); } - WRITECONST(stream, out, "</pre>\n"); + /* end monospace formatting */ + out("</pre>\n", 1, 7, stream); + + /* cleanup and return */ ucx_buffer_free(line); return 0; }