src/c2html.c

changeset 53
5e47a26a16f0
parent 52
33ded421c512
child 55
bf54085ce341
equal deleted inserted replaced
52:33ded421c512 53:5e47a26a16f0
32 #include "c2html.h" 32 #include "c2html.h"
33 #include "highlighter.h" 33 #include "highlighter.h"
34 34
35 #include "ucx/list.h" 35 #include "ucx/list.h"
36 36
37 void printhelp() { 37 static int appendfile(const char *filename, FILE *fout,
38 char *copybuf, size_t copybuflen, const char *errmsg) {
39 FILE *headerfile = fopen(filename, "r");
40 if (!headerfile) {
41 perror(errmsg);
42 if (fout != stdout) {
43 fclose(fout);
44 }
45 return 1;
46 }
47 ucx_stream_copy(headerfile, fout,
48 (read_func) fread, (write_func) fwrite,
49 copybuf, copybuflen, (size_t)-1);
50 fclose(headerfile);
51 return 0;
52 }
53
54 static void printhelp() {
38 printf("Formats source code using HTML.\n\nUsage:\n" 55 printf("Formats source code using HTML.\n\nUsage:\n"
39 " c2html [Options] FILE\n\n" 56 " c2html [Options] FILE\n\n"
40 " Options:\n" 57 " Options:\n"
41 " -h Prints this help message\n" 58 " -h Prints this help message\n"
42 " -j Highlight Java instead of C source code\n" 59 " -j Highlight Java instead of C source code\n"
47 " -l Disable line numbers\n" 64 " -l Disable line numbers\n"
48 " -V, -v Prints version and exits\n" 65 " -V, -v Prints version and exits\n"
49 "\n"); 66 "\n");
50 } 67 }
51 68
52 void formatlines(highlighter_func highlighter, 69 static void formatlines(highlighter_func highlighter,
53 UcxList *in, write_func out, void *stream, int showlineno) { 70 UcxList *in, write_func out, void *stream, int showlineno) {
54 71
55 /* compute width of line numbering */ 72 /* compute width of line numbering */
56 int lnw; 73 int lnw = 0;
57 if (showlineno) { 74 if (showlineno) {
58 size_t lines = ucx_list_size(in); 75 size_t lines = ucx_list_size(in);
59 lnw = 1; 76 for (size_t p = 1; p < lines ; p*=10) lnw++;
60 int p = 1;
61 while ((p*=10) < lines) lnw++;
62 } 77 }
63 78
64 /* start monospace formatting */ 79 /* start monospace formatting */
65 out("<pre>\n", 1, 6, stream); 80 out("<pre>\n", 1, 6, stream);
66 81
196 perror("Error allocating file buffer"); 211 perror("Error allocating file buffer");
197 return EXIT_FAILURE; 212 return EXIT_FAILURE;
198 } 213 }
199 214
200 /* Prepend header file */ 215 /* Prepend header file */
201 { 216 if (appendfile(settings.headerfile, fout, filebuf, FILEBUF_SIZE,
202 FILE *headerfile = fopen(settings.headerfile, "r"); 217 "Error opening header file")) {
203 if (!headerfile) { 218 return EXIT_FAILURE;
204 perror("Error opening header file");
205 if (fout != stdout) {
206 fclose(fout);
207 }
208 return EXIT_FAILURE;
209 }
210 ucx_stream_copy(headerfile, fout,
211 (read_func) fread, (write_func) fwrite,
212 filebuf, FILEBUF_SIZE, (size_t)-1);
213 fclose(headerfile);
214 } 219 }
215 220
216 /* Process input file */ 221 /* Process input file */
217 FILE *inputfile = fopen(settings.infilename, "r"); 222 FILE *inputfile = fopen(settings.infilename, "r");
218 if (inputfile) { 223 if (inputfile) {
246 } 251 }
247 return EXIT_FAILURE; 252 return EXIT_FAILURE;
248 } 253 }
249 254
250 /* Append footer file */ 255 /* Append footer file */
251 { 256 if (appendfile(settings.footerfile, fout, filebuf, FILEBUF_SIZE,
252 FILE *footerfile = fopen(settings.footerfile, "r"); 257 "Error opening footer file")) {
253 if (!footerfile) { 258 return EXIT_FAILURE;
254 perror("Error opening footer file"); 259 }
255 if (fout != stdout) {
256 fclose(fout);
257 }
258 return EXIT_FAILURE;
259 }
260 ucx_stream_copy(footerfile, fout,
261 (read_func) fread, (write_func) fwrite,
262 filebuf, FILEBUF_SIZE, (size_t)-1);
263 fclose(footerfile);
264 }
265
266 260
267 free(filebuf); 261 free(filebuf);
268 262
269 return EXIT_SUCCESS; 263 return EXIT_SUCCESS;
270 } 264 }

mercurial