src/c2html.c

changeset 46
534a4ef4143d
parent 45
1f3835182aeb
child 47
c39ecbbca7c0
equal deleted inserted replaced
45:1f3835182aeb 46:534a4ef4143d
46 " -V, -v Prints version and exits\n" 46 " -V, -v Prints version and exits\n"
47 "\n"); 47 "\n");
48 } 48 }
49 49
50 /* TODO: remove this workaround after refactoring highlighter structure */ 50 /* TODO: remove this workaround after refactoring highlighter structure */
51 static void plainparseline(char *src, UcxBuffer *dest, highlighter_t* hltr) { 51 static void plainparseline(char *src, UcxBuffer *dest, HighlighterData* hltr) {
52 size_t dp = 0; 52 size_t dp = 0;
53 char *buf = dest->space + dest->pos; 53 char *buf = dest->space + dest->pos;
54 while (*src && *src != '\n') { 54 while (*src && *src != '\n') {
55 dp = writeescapedchar(buf, dp, *src); 55 dp = writeescapedchar(buf, dp, *src);
56 src++; 56 src++;
59 buf[dp] = '\0'; 59 buf[dp] = '\0';
60 dest->pos += dp; 60 dest->pos += dp;
61 dest->size += dp; 61 dest->size += dp;
62 } 62 }
63 63
64 int formatfile( 64 int formatlines(highlighter_func highlighter,
65 highlighter_t *highlighter, 65 UcxList *in, write_func out, void *stream, int showlineno) {
66 UcxList *in, write_func out, void *stream,
67 int showlineno) {
68 66
69 /* compute width of line numbering */ 67 /* compute width of line numbering */
70 int lnw; 68 int lnw;
71 if (showlineno) { 69 if (showlineno) {
72 size_t lines = ucx_list_size(in); 70 size_t lines = ucx_list_size(in);
84 /* start monospace formatting */ 82 /* start monospace formatting */
85 out("<pre>\n", 1, 6, stream); 83 out("<pre>\n", 1, 6, stream);
86 84
87 /* process lines */ 85 /* process lines */
88 size_t lineno = 0; 86 size_t lineno = 0;
87 HighlighterData highlighter_data;
88 memset(&highlighter_data, 0, sizeof(HighlighterData));
89
89 UCX_FOREACH(sourceline, in) { 90 UCX_FOREACH(sourceline, in) {
90 /* increase line number and clean line buffer */ 91 /* increase line number and clean line buffer */
91 lineno++; 92 lineno++;
92 ucx_buffer_clear(line); 93 ucx_buffer_clear(line);
93 94
97 "<a name=\"l%d\" href=\"#l%d\">%*d </a></span> ", 98 "<a name=\"l%d\" href=\"#l%d\">%*d </a></span> ",
98 lineno, lineno, lnw, lineno); 99 lineno, lineno, lnw, lineno);
99 } 100 }
100 101
101 /* process code line */ 102 /* process code line */
102 highlighter->parser(sourceline->data, line, highlighter); 103 highlighter(sourceline->data, line, &highlighter_data);
103 104
104 /* write code line */ 105 /* write code line */
105 out(line->space, 1, line->size, stream); 106 out(line->space, 1, line->size, stream);
106 } 107 }
107 108
172 if (optind != argc-1) { 173 if (optind != argc-1) {
173 printhelp(); 174 printhelp();
174 return EXIT_FAILURE; 175 return EXIT_FAILURE;
175 } else { 176 } else {
176 /* Configure highlighter */ 177 /* Configure highlighter */
177 highlighter_t *highlighter = calloc(1, sizeof(highlighter_t)); 178 highlighter_func hltr = NULL;
178 switch (sourcetype) { 179 switch (sourcetype) {
179 case SOURCE_C: 180 case SOURCE_C:
180 highlighter->isdirective = check_cdirective; 181 hltr = cparseline;
181 highlighter->istype = check_ctype;
182 highlighter->keywords = ckeywords;
183 highlighter->parser = cparseline;
184 break; 182 break;
185 case SOURCE_JAVA: 183 case SOURCE_JAVA:
186 highlighter->isdirective = check_jdirective; 184 hltr = jparseline;
187 highlighter->istype = check_jtype;
188 highlighter->keywords = jkeywords;
189 highlighter->parser = jparseline;
190 break; 185 break;
191 case SOURCE_PLAIN: 186 case SOURCE_PLAIN:
192 highlighter->parser = plainparseline; 187 hltr = plainparseline;
193 break; 188 break;
194 default: /* should be unreachable */ 189 default: /* should be unreachable */
195 fprintf(stderr, "error in enum source_type\n"); 190 fprintf(stderr, "error in enum source_type\n");
196 return EXIT_FAILURE; 191 return EXIT_FAILURE;
197 } 192 }
252 if (content->space[i] == '\n' && i+1 < content->size) { 247 if (content->space[i] == '\n' && i+1 < content->size) {
253 ucx_list_append(inputlines, content->space+i+1); 248 ucx_list_append(inputlines, content->space+i+1);
254 } 249 }
255 } 250 }
256 251
257 formatfile( 252 formatlines(hltr, inputlines,
258 highlighter, 253 (write_func) fwrite, fout, settings.showlinenumbers);
259 inputlines,
260 (write_func) fwrite,
261 fout,
262 settings.showlinenumbers);
263 254
264 free(highlighter);
265 ucx_buffer_free(content); 255 ucx_buffer_free(content);
266 } else { 256 } else {
267 perror("Error opening input file"); 257 perror("Error opening input file");
268 if (fout != stdout) { 258 if (fout != stdout) {
269 fclose(fout); 259 fclose(fout);

mercurial