src/c2html.c

changeset 45
1f3835182aeb
parent 44
2b4ac35d061d
child 46
534a4ef4143d
equal deleted inserted replaced
44:2b4ac35d061d 45:1f3835182aeb
45 " -l Disable line numbers\n" 45 " -l Disable line numbers\n"
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 */
51 static void plainparseline(char *src, UcxBuffer *dest, highlighter_t* hltr) {
52 size_t dp = 0;
53 char *buf = dest->space + dest->pos;
54 while (*src && *src != '\n') {
55 dp = writeescapedchar(buf, dp, *src);
56 src++;
57 }
58 buf[dp++] = '\n';
59 buf[dp] = '\0';
60 dest->pos += dp;
61 dest->size += dp;
62 }
63
50 int formatfile( 64 int formatfile(
51 highlighter_t *highlighter, 65 highlighter_t *highlighter,
52 UcxList *in, 66 UcxList *in, write_func out, void *stream,
53 write_func out, void *stream,
54 int showlineno) { 67 int showlineno) {
55 68
56 /* compute width of line numbering */ 69 /* compute width of line numbering */
57 int lnw; 70 int lnw;
58 if (showlineno) { 71 if (showlineno) {
83 ucx_bprintf(line, "<span class=\"c2html-lineno\">" 96 ucx_bprintf(line, "<span class=\"c2html-lineno\">"
84 "<a name=\"l%d\" href=\"#l%d\">%*d </a></span> ", 97 "<a name=\"l%d\" href=\"#l%d\">%*d </a></span> ",
85 lineno, lineno, lnw, lineno); 98 lineno, lineno, lnw, lineno);
86 } 99 }
87 100
88 /* TODO: backwards compatibility: replace line->space in all occasions 101 /* process code line */
89 * and use UcxBuffer functions 102 highlighter->parser(sourceline->data, line, highlighter);
90 */
91 char *buf = line->space + line->pos;
92 if (highlighter) {
93 highlighter->parser(sourceline->data, buf, highlighter);
94 } else {
95 char *c = sourceline->data;
96 size_t dp = 0;
97 while (*c && *c != '\n') {
98 dp = writeescapedchar(buf, dp, *c);
99 c++;
100 }
101 buf[dp++] = '\n';
102 buf[dp] = '\0';
103 }
104 line->size = strlen(line->space);
105 103
106 /* write code line */ 104 /* write code line */
107 out(line->space, 1, line->size, stream); 105 out(line->space, 1, line->size, stream);
108 } 106 }
109 107
111 out("</pre>\n", 1, 7, stream); 109 out("</pre>\n", 1, 7, stream);
112 110
113 /* cleanup and return */ 111 /* cleanup and return */
114 ucx_buffer_free(line); 112 ucx_buffer_free(line);
115 return 0; 113 return 0;
116 }
117
118 void init_c_highlighter(highlighter_t *highlighter) {
119 memset(highlighter, 0, sizeof(highlighter_t));
120 highlighter->isdirective = check_cdirective;
121 highlighter->istype = check_ctype;
122 highlighter->keywords = ckeywords;
123 highlighter->parser = cparseline;
124 }
125
126 void init_java_highlighter(highlighter_t *highlighter) {
127 memset(highlighter, 0, sizeof(highlighter_t));
128 highlighter->isdirective = check_jdirective;
129 highlighter->istype = check_jtype;
130 highlighter->keywords = jkeywords;
131 highlighter->parser = jparseline;
132 } 114 }
133 115
134 #define FILEBUF_SIZE 4096 116 #define FILEBUF_SIZE 4096
135 117
136 enum source_type { 118 enum source_type {
190 if (optind != argc-1) { 172 if (optind != argc-1) {
191 printhelp(); 173 printhelp();
192 return EXIT_FAILURE; 174 return EXIT_FAILURE;
193 } else { 175 } else {
194 /* Configure highlighter */ 176 /* Configure highlighter */
195 highlighter_t highlighter; 177 highlighter_t *highlighter = calloc(1, sizeof(highlighter_t));
196 highlighter_t *hptr = &highlighter;
197 switch (sourcetype) { 178 switch (sourcetype) {
198 case SOURCE_C: 179 case SOURCE_C:
199 init_c_highlighter(&highlighter); 180 highlighter->isdirective = check_cdirective;
181 highlighter->istype = check_ctype;
182 highlighter->keywords = ckeywords;
183 highlighter->parser = cparseline;
200 break; 184 break;
201 case SOURCE_JAVA: 185 case SOURCE_JAVA:
202 init_java_highlighter(&highlighter); 186 highlighter->isdirective = check_jdirective;
187 highlighter->istype = check_jtype;
188 highlighter->keywords = jkeywords;
189 highlighter->parser = jparseline;
203 break; 190 break;
204 case SOURCE_PLAIN: 191 case SOURCE_PLAIN:
205 hptr = NULL; 192 highlighter->parser = plainparseline;
206 break; 193 break;
207 default: /* should be unreachable */ 194 default: /* should be unreachable */
208 fprintf(stderr, "error in enum source_type\n"); 195 fprintf(stderr, "error in enum source_type\n");
209 return EXIT_FAILURE; 196 return EXIT_FAILURE;
210 } 197 }
266 ucx_list_append(inputlines, content->space+i+1); 253 ucx_list_append(inputlines, content->space+i+1);
267 } 254 }
268 } 255 }
269 256
270 formatfile( 257 formatfile(
271 hptr, 258 highlighter,
272 inputlines, 259 inputlines,
273 (write_func) fwrite, 260 (write_func) fwrite,
274 fout, 261 fout,
275 settings.showlinenumbers); 262 settings.showlinenumbers);
263
264 free(highlighter);
276 ucx_buffer_free(content); 265 ucx_buffer_free(content);
277 } else { 266 } else {
278 perror("Error opening input file"); 267 perror("Error opening input file");
279 if (fout != stdout) { 268 if (fout != stdout) {
280 fclose(fout); 269 fclose(fout);

mercurial