--- a/src/frontend.c Thu Nov 10 18:44:48 2016 +0100 +++ b/src/frontend.c Mon Apr 24 20:54:38 2023 +0200 @@ -33,9 +33,7 @@ #include <string.h> #include "c2html.h" -#include "ucx/utils.h" - -#define FILEBUF_SIZE 4096 +#include <cx/utils.h> typedef struct { char* outfilename; @@ -45,20 +43,17 @@ int showlinenumbers; } Settings; -static int appendfile(const char *filename, FILE *fout, - char *copybuf, size_t copybuflen, const char *errmsg) { - FILE *headerfile = fopen(filename, "r"); - if (!headerfile) { +static int appendfile(const char *filename, FILE *fout, const char *errmsg) { + FILE *fin = fopen(filename, "r"); + if (!fin) { 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); + cx_stream_copy(fin, fout, (cx_read_func) fread, (cx_write_func) fwrite); + fclose(fin); return 0; } @@ -86,7 +81,7 @@ c2html_highlighter_func hltr = c2html_c_highlighter; /* Parse command line */ - char optc; + int optc; while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) { switch (optc) { case 'o': @@ -142,15 +137,8 @@ fout = stdout; } - /* Allocate file buffer */ - char *filebuf = malloc(FILEBUF_SIZE); - if (!filebuf) { - perror("Error allocating file buffer"); - return EXIT_FAILURE; - } - /* Prepend header file */ - if (appendfile(settings.headerfile, fout, filebuf, FILEBUF_SIZE, + if (appendfile(settings.headerfile, fout, "Error opening header file")) { return EXIT_FAILURE; } @@ -158,11 +146,17 @@ /* Process input file */ FILE *inputfile = fopen(settings.infilename, "r"); if (inputfile) { - c2html_fformatf( - inputfile, filebuf, FILEBUF_SIZE, - fout, hltr, settings.showlinenumbers + CxBuffer fbuf; + cxBufferInit(&fbuf, NULL, 4096, NULL, CX_BUFFER_AUTO_EXTEND); + cx_stream_copy(inputfile, &fbuf, (cx_read_func) fread, + (cx_write_func) cxBufferWrite); + fclose(inputfile); + c2html_bformat( + fbuf.space, fbuf.size, + fout, (cx_write_func ) fwrite, hltr, + settings.showlinenumbers ); - fclose(inputfile); + cxBufferDestroy(&fbuf); } else { perror("Error opening input file"); if (fout != stdout) { @@ -172,12 +166,10 @@ } /* Append footer file */ - if (appendfile(settings.footerfile, fout, filebuf, FILEBUF_SIZE, + if (appendfile(settings.footerfile, fout, "Error opening footer file")) { return EXIT_FAILURE; } - - free(filebuf); return EXIT_SUCCESS; }