diff -r a8cee98c8832 -r 2b4ac35d061d src/c2html.c --- 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); + /* 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; } - WRITECONST(stream, out, "
\n"); - int lnw; - { - lnw = 1; - int p = 1; - while ((p*=10) < lines) lnw++; - } + /* start monospace formatting */ + out("\n", 1, 7, stream); + + /* cleanup and return */ ucx_buffer_free(line); return 0; }\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, "" + "%*d ", + 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, ""); - char lnbuf[128]; - int len; - // line number link - len = snprintf(lnbuf, 128, "", - 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, " "); - } + 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, "\n"); + /* end monospace formatting */ + out("