src/frontend.c

changeset 66
1b12cf799fee
parent 57
eba880c1705c
child 67
5da2cb5aea6b
     1.1 --- a/src/frontend.c	Thu Nov 10 18:44:48 2016 +0100
     1.2 +++ b/src/frontend.c	Mon Apr 24 20:54:38 2023 +0200
     1.3 @@ -33,9 +33,7 @@
     1.4  #include <string.h>
     1.5  
     1.6  #include "c2html.h"
     1.7 -#include "ucx/utils.h"
     1.8 -
     1.9 -#define FILEBUF_SIZE 4096
    1.10 +#include <cx/utils.h>
    1.11  
    1.12  typedef struct {
    1.13      char* outfilename;
    1.14 @@ -45,20 +43,17 @@
    1.15      int showlinenumbers;
    1.16  } Settings;
    1.17  
    1.18 -static int appendfile(const char *filename, FILE *fout,
    1.19 -        char *copybuf, size_t copybuflen, const char *errmsg) {
    1.20 -    FILE *headerfile = fopen(filename, "r");
    1.21 -    if (!headerfile) {
    1.22 +static int appendfile(const char *filename, FILE *fout, const char *errmsg) {
    1.23 +    FILE *fin = fopen(filename, "r");
    1.24 +    if (!fin) {
    1.25          perror(errmsg);
    1.26          if (fout != stdout) {
    1.27              fclose(fout);
    1.28          }
    1.29          return 1;
    1.30      }
    1.31 -    ucx_stream_copy(headerfile, fout,
    1.32 -            (read_func) fread, (write_func) fwrite,
    1.33 -            copybuf, copybuflen, (size_t)-1);
    1.34 -    fclose(headerfile);
    1.35 +    cx_stream_copy(fin, fout, (cx_read_func) fread, (cx_write_func) fwrite);
    1.36 +    fclose(fin);
    1.37      return 0;
    1.38  }
    1.39  
    1.40 @@ -86,7 +81,7 @@
    1.41      c2html_highlighter_func hltr = c2html_c_highlighter;
    1.42  
    1.43      /* Parse command line */
    1.44 -    char optc;
    1.45 +    int optc;
    1.46      while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) {
    1.47          switch (optc) {
    1.48              case 'o':
    1.49 @@ -142,15 +137,8 @@
    1.50              fout = stdout;
    1.51          }
    1.52          
    1.53 -        /* Allocate file buffer  */
    1.54 -        char *filebuf = malloc(FILEBUF_SIZE);
    1.55 -        if (!filebuf) {
    1.56 -            perror("Error allocating file buffer");
    1.57 -            return EXIT_FAILURE;
    1.58 -        }
    1.59 -        
    1.60          /* Prepend header file */
    1.61 -        if (appendfile(settings.headerfile, fout, filebuf, FILEBUF_SIZE,
    1.62 +        if (appendfile(settings.headerfile, fout,
    1.63                  "Error opening header file")) {
    1.64              return EXIT_FAILURE;
    1.65          }
    1.66 @@ -158,11 +146,17 @@
    1.67          /* Process input file */
    1.68          FILE *inputfile = fopen(settings.infilename, "r");
    1.69          if (inputfile) {
    1.70 -            c2html_fformatf(
    1.71 -                    inputfile, filebuf, FILEBUF_SIZE,
    1.72 -                    fout, hltr, settings.showlinenumbers
    1.73 +            CxBuffer fbuf;
    1.74 +            cxBufferInit(&fbuf, NULL, 4096, NULL, CX_BUFFER_AUTO_EXTEND);
    1.75 +            cx_stream_copy(inputfile, &fbuf, (cx_read_func) fread,
    1.76 +                           (cx_write_func) cxBufferWrite);
    1.77 +            fclose(inputfile);
    1.78 +            c2html_bformat(
    1.79 +                    fbuf.space, fbuf.size,
    1.80 +                    fout, (cx_write_func ) fwrite, hltr,
    1.81 +                    settings.showlinenumbers
    1.82              );
    1.83 -            fclose(inputfile);
    1.84 +            cxBufferDestroy(&fbuf);
    1.85          } else {
    1.86              perror("Error opening input file");
    1.87              if (fout != stdout) {
    1.88 @@ -172,12 +166,10 @@
    1.89          }
    1.90          
    1.91          /* Append footer file */
    1.92 -        if (appendfile(settings.footerfile, fout, filebuf, FILEBUF_SIZE,
    1.93 +        if (appendfile(settings.footerfile, fout,
    1.94                  "Error opening footer file")) {
    1.95              return EXIT_FAILURE;
    1.96          }
    1.97 -        
    1.98 -        free(filebuf);
    1.99  
   1.100          return EXIT_SUCCESS;
   1.101      }

mercurial