src/c2html.c

changeset 51
f25ba6fd7a08
parent 50
17408c3607ce
child 52
33ded421c512
equal deleted inserted replaced
50:17408c3607ce 51:f25ba6fd7a08
43 " -l Disable line numbers\n" 43 " -l Disable line numbers\n"
44 " -V, -v Prints version and exits\n" 44 " -V, -v Prints version and exits\n"
45 "\n"); 45 "\n");
46 } 46 }
47 47
48 static void plain_highlighter(char *src, UcxBuffer *dest, int* x) { 48 static void plain_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
49 while (*src && *src != '\n') { 49 while (*src && *src != '\n') {
50 put_htmlescaped(dest, *src); 50 put_htmlescaped(dest, *src);
51 src++; 51 src++;
52 } 52 }
53 ucx_buffer_putc(dest, '\n'); 53 ucx_buffer_putc(dest, '\n');
63 lnw = 1; 63 lnw = 1;
64 int p = 1; 64 int p = 1;
65 while ((p*=10) < lines) lnw++; 65 while ((p*=10) < lines) lnw++;
66 } 66 }
67 67
68 /* allocate line buffer */
69 UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
70 if(!line) {
71 perror("Error allocating line buffer for output");
72 return;
73 }
74
75 /* start monospace formatting */ 68 /* start monospace formatting */
76 out("<pre>\n", 1, 6, stream); 69 out("<pre>\n", 1, 6, stream);
77 70
78 /* process lines */ 71 /* process lines */
79 size_t lineno = 0; 72 size_t lineno = 0;
80 int multiline_comment = 0; 73 HighlighterData *hd = new_highlighter_data();
74 UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
75 if(!line || !hd) {
76 perror("Error allocating buffer for output");
77 return;
78 }
81 79
82 UCX_FOREACH(sourceline, in) { 80 UCX_FOREACH(sourceline, in) {
83 /* increase line number and clean line buffer */ 81 /* increase line number and clean line buffer */
84 lineno++; 82 lineno++;
85 ucx_buffer_clear(line); 83 ucx_buffer_clear(line);
90 "<a name=\"l%d\" href=\"#l%d\">%*d </a></span> ", 88 "<a name=\"l%d\" href=\"#l%d\">%*d </a></span> ",
91 lineno, lineno, lnw, lineno); 89 lineno, lineno, lnw, lineno);
92 } 90 }
93 91
94 /* process code line */ 92 /* process code line */
95 highlighter(sourceline->data, line, &multiline_comment); 93 highlighter(sourceline->data, line, hd);
96 94
97 /* write code line */ 95 /* write code line */
98 out(line->space, 1, line->size, stream); 96 out(line->space, 1, line->size, stream);
99 } 97 }
100 98
101 /* end monospace formatting */ 99 /* end monospace formatting */
102 out("</pre>\n", 1, 7, stream); 100 out("</pre>\n", 1, 7, stream);
103 101
104 /* cleanup and return */ 102 /* cleanup and return */
103 free_highlighter_data(hd);
105 ucx_buffer_free(line); 104 ucx_buffer_free(line);
106 } 105 }
107 106
108 #define FILEBUF_SIZE 4096 107 #define FILEBUF_SIZE 4096
109 108

mercurial