src/c2html.c

changeset 53
5e47a26a16f0
parent 52
33ded421c512
child 55
bf54085ce341
     1.1 --- a/src/c2html.c	Fri Aug 26 13:49:19 2016 +0200
     1.2 +++ b/src/c2html.c	Fri Aug 26 14:15:29 2016 +0200
     1.3 @@ -34,7 +34,24 @@
     1.4  
     1.5  #include "ucx/list.h"
     1.6  
     1.7 -void printhelp() {
     1.8 +static int appendfile(const char *filename, FILE *fout,
     1.9 +        char *copybuf, size_t copybuflen, const char *errmsg) {
    1.10 +    FILE *headerfile = fopen(filename, "r");
    1.11 +    if (!headerfile) {
    1.12 +        perror(errmsg);
    1.13 +        if (fout != stdout) {
    1.14 +            fclose(fout);
    1.15 +        }
    1.16 +        return 1;
    1.17 +    }
    1.18 +    ucx_stream_copy(headerfile, fout,
    1.19 +            (read_func) fread, (write_func) fwrite,
    1.20 +            copybuf, copybuflen, (size_t)-1);
    1.21 +    fclose(headerfile);
    1.22 +    return 0;
    1.23 +}
    1.24 +
    1.25 +static void printhelp() {
    1.26      printf("Formats source code using HTML.\n\nUsage:\n"
    1.27          "  c2html [Options] FILE\n\n"
    1.28          " Options:\n"
    1.29 @@ -49,16 +66,14 @@
    1.30          "\n");
    1.31  }
    1.32  
    1.33 -void formatlines(highlighter_func highlighter,
    1.34 +static void formatlines(highlighter_func highlighter,
    1.35          UcxList *in, write_func out, void *stream, int showlineno) {
    1.36      
    1.37      /* compute width of line numbering */
    1.38 -    int lnw;
    1.39 +    int lnw = 0;
    1.40      if (showlineno) {
    1.41          size_t lines = ucx_list_size(in);
    1.42 -        lnw = 1;
    1.43 -        int p = 1;
    1.44 -        while ((p*=10) < lines) lnw++;
    1.45 +        for (size_t p = 1; p < lines ; p*=10) lnw++;
    1.46      }
    1.47      
    1.48      /* start monospace formatting */
    1.49 @@ -198,19 +213,9 @@
    1.50          }
    1.51          
    1.52          /* Prepend header file */
    1.53 -        {
    1.54 -            FILE *headerfile = fopen(settings.headerfile, "r");
    1.55 -            if (!headerfile) {
    1.56 -                perror("Error opening header file");
    1.57 -                if (fout != stdout) {
    1.58 -                    fclose(fout);
    1.59 -                }
    1.60 -                return EXIT_FAILURE;
    1.61 -            }
    1.62 -            ucx_stream_copy(headerfile, fout,
    1.63 -                    (read_func) fread, (write_func) fwrite,
    1.64 -                    filebuf, FILEBUF_SIZE, (size_t)-1);
    1.65 -            fclose(headerfile);
    1.66 +        if (appendfile(settings.headerfile, fout, filebuf, FILEBUF_SIZE,
    1.67 +                "Error opening header file")) {
    1.68 +            return EXIT_FAILURE;
    1.69          }
    1.70  
    1.71          /* Process input file */
    1.72 @@ -248,21 +253,10 @@
    1.73          }
    1.74          
    1.75          /* Append footer file */
    1.76 -        {
    1.77 -            FILE *footerfile = fopen(settings.footerfile, "r");
    1.78 -            if (!footerfile) {
    1.79 -                perror("Error opening footer file");
    1.80 -                if (fout != stdout) {
    1.81 -                    fclose(fout);
    1.82 -                }
    1.83 -                return EXIT_FAILURE;
    1.84 -            }
    1.85 -            ucx_stream_copy(footerfile, fout,
    1.86 -                    (read_func) fread, (write_func) fwrite,
    1.87 -                    filebuf, FILEBUF_SIZE, (size_t)-1);
    1.88 -            fclose(footerfile);
    1.89 +        if (appendfile(settings.footerfile, fout, filebuf, FILEBUF_SIZE,
    1.90 +                "Error opening footer file")) {
    1.91 +            return EXIT_FAILURE;
    1.92          }
    1.93 -
    1.94          
    1.95          free(filebuf);
    1.96  

mercurial