# HG changeset patch # User Mike Becker # Date 1472213729 -7200 # Node ID 5e47a26a16f0492ddf3fefd027f974ea27d49641 # Parent 33ded421c512375f9e694ba62b6d217b7ac94abe adds appendfile() function to main module + adds TODOs for source files which do not terminate with a blank line diff -r 33ded421c512 -r 5e47a26a16f0 src/c2html.c --- a/src/c2html.c Fri Aug 26 13:49:19 2016 +0200 +++ b/src/c2html.c Fri Aug 26 14:15:29 2016 +0200 @@ -34,7 +34,24 @@ #include "ucx/list.h" -void printhelp() { +static int appendfile(const char *filename, FILE *fout, + char *copybuf, size_t copybuflen, const char *errmsg) { + FILE *headerfile = fopen(filename, "r"); + if (!headerfile) { + perror(errmsg); + if (fout != stdout) { + fclose(fout); + } + return 1; + } + ucx_stream_copy(headerfile, fout, + (read_func) fread, (write_func) fwrite, + copybuf, copybuflen, (size_t)-1); + fclose(headerfile); + return 0; +} + +static void printhelp() { printf("Formats source code using HTML.\n\nUsage:\n" " c2html [Options] FILE\n\n" " Options:\n" @@ -49,16 +66,14 @@ "\n"); } -void formatlines(highlighter_func highlighter, +static void formatlines(highlighter_func highlighter, UcxList *in, write_func out, void *stream, int showlineno) { /* compute width of line numbering */ - int lnw; + int lnw = 0; if (showlineno) { size_t lines = ucx_list_size(in); - lnw = 1; - int p = 1; - while ((p*=10) < lines) lnw++; + for (size_t p = 1; p < lines ; p*=10) lnw++; } /* start monospace formatting */ @@ -198,19 +213,9 @@ } /* Prepend header file */ - { - FILE *headerfile = fopen(settings.headerfile, "r"); - if (!headerfile) { - perror("Error opening header file"); - if (fout != stdout) { - fclose(fout); - } - return EXIT_FAILURE; - } - ucx_stream_copy(headerfile, fout, - (read_func) fread, (write_func) fwrite, - filebuf, FILEBUF_SIZE, (size_t)-1); - fclose(headerfile); + if (appendfile(settings.headerfile, fout, filebuf, FILEBUF_SIZE, + "Error opening header file")) { + return EXIT_FAILURE; } /* Process input file */ @@ -248,21 +253,10 @@ } /* Append footer file */ - { - FILE *footerfile = fopen(settings.footerfile, "r"); - if (!footerfile) { - perror("Error opening footer file"); - if (fout != stdout) { - fclose(fout); - } - return EXIT_FAILURE; - } - ucx_stream_copy(footerfile, fout, - (read_func) fread, (write_func) fwrite, - filebuf, FILEBUF_SIZE, (size_t)-1); - fclose(footerfile); + if (appendfile(settings.footerfile, fout, filebuf, FILEBUF_SIZE, + "Error opening footer file")) { + return EXIT_FAILURE; } - free(filebuf); diff -r 33ded421c512 -r 5e47a26a16f0 src/highlighter.c --- a/src/highlighter.c Fri Aug 26 13:49:19 2016 +0200 +++ b/src/highlighter.c Fri Aug 26 14:15:29 2016 +0200 @@ -126,7 +126,10 @@ char c; do { c = src[++sp]; - if (!c) break; + if (!c) { + /* TODO: might cause problems if code file does not end with NL */ + break; + } /* comments */ if (!isstring && c == '/') { @@ -268,7 +271,10 @@ char c; do { c = src[++sp]; - if (!c) break; + if (!c) { + /* TODO: might cause problems if code file does not end with NL */ + break; + } /* comments */ if (!isstring && c == '/') {