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 |
1.1 --- a/src/c2html.c Tue Aug 23 14:31:02 2016 +0200 1.2 +++ b/src/c2html.c Tue Aug 23 15:07:29 2016 +0200 1.3 @@ -47,67 +47,70 @@ 1.4 "\n"); 1.5 } 1.6 1.7 -#define WRITECONST(stream, out, cstr) out(cstr, 1, sizeof(cstr)-1, stream) 1.8 int formatfile( 1.9 highlighter_t *highlighter, 1.10 UcxList *in, 1.11 - write_func out, 1.12 - void *stream, 1.13 + write_func out, void *stream, 1.14 int showlineno) { 1.15 - size_t lines = ucx_list_size(in); 1.16 1.17 + /* compute width of line numbering */ 1.18 + int lnw; 1.19 + if (showlineno) { 1.20 + size_t lines = ucx_list_size(in); 1.21 + lnw = 1; 1.22 + int p = 1; 1.23 + while ((p*=10) < lines) lnw++; 1.24 + } 1.25 + 1.26 + /* allocate line buffer */ 1.27 UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); 1.28 if(!line) { 1.29 return 1; 1.30 } 1.31 - WRITECONST(stream, out, "<pre>\n"); 1.32 1.33 - int lnw; 1.34 - { 1.35 - lnw = 1; 1.36 - int p = 1; 1.37 - while ((p*=10) < lines) lnw++; 1.38 - } 1.39 + /* start monospace formatting */ 1.40 + out("<pre>\n", 1, 6, stream); 1.41 1.42 + /* process lines */ 1.43 size_t lineno = 0; 1.44 UCX_FOREACH(sourceline, in) { 1.45 + /* increase line number and clean line buffer */ 1.46 lineno++; 1.47 + ucx_buffer_clear(line); 1.48 + 1.49 + /* write line number */ 1.50 + if (showlineno) { 1.51 + ucx_bprintf(line, "<span class=\"c2html-lineno\">" 1.52 + "<a name=\"l%d\" href=\"#l%d\">%*d </a></span> ", 1.53 + lineno, lineno, lnw, lineno); 1.54 + } 1.55 + 1.56 /* TODO: backwards compatibility: replace line->space in all occasions 1.57 * and use UcxBuffer functions 1.58 */ 1.59 + char *buf = line->space + line->pos; 1.60 if (highlighter) { 1.61 - highlighter->parser(sourceline->data, line->space, highlighter); 1.62 + highlighter->parser(sourceline->data, buf, highlighter); 1.63 } else { 1.64 char *c = sourceline->data; 1.65 size_t dp = 0; 1.66 while (*c && *c != '\n') { 1.67 - dp = writeescapedchar(line->space, dp, *c); 1.68 + dp = writeescapedchar(buf, dp, *c); 1.69 c++; 1.70 } 1.71 - line->space[dp++] = '\n'; 1.72 - line->space[dp] = '\0'; 1.73 + buf[dp++] = '\n'; 1.74 + buf[dp] = '\0'; 1.75 } 1.76 - 1.77 - // write line number 1.78 - if (showlineno) { 1.79 - WRITECONST(stream, out, "<span class=\"c2html-lineno\">"); 1.80 - char lnbuf[128]; 1.81 - int len; 1.82 - // line number link 1.83 - len = snprintf(lnbuf, 128, "<a name=\"l%d\" href=\"#l%d\">", 1.84 - lineno, lineno); 1.85 - out(lnbuf, 1, len, stream); 1.86 - // justified line number 1.87 - len = snprintf(lnbuf, 128, "%*d ", lnw, lineno); 1.88 - out(lnbuf, 1, len, stream); 1.89 - WRITECONST(stream, out, "</a></span> "); 1.90 - } 1.91 + line->size = strlen(line->space); 1.92 1.93 - // write formated (or plain) code line 1.94 - out(line->space, 1, strlen(line->space), stream); 1.95 + /* write code line */ 1.96 + out(line->space, 1, line->size, stream); 1.97 } 1.98 1.99 - WRITECONST(stream, out, "</pre>\n"); 1.100 + /* end monospace formatting */ 1.101 + out("</pre>\n", 1, 7, stream); 1.102 + 1.103 + /* cleanup and return */ 1.104 ucx_buffer_free(line); 1.105 return 0; 1.106 }