1.1 --- a/src/c2html.c Tue Aug 23 14:13:46 2016 +0200 1.2 +++ b/src/c2html.c Tue Aug 23 14:24:57 2016 +0200 1.3 @@ -47,25 +47,6 @@ 1.4 "\n"); 1.5 } 1.6 1.7 -int copyfile(char *filename, FILE *dest) { 1.8 - if (!filename) { 1.9 - return 0; 1.10 - } 1.11 - 1.12 - FILE *src = fopen(filename, "r"); 1.13 - if (src) { 1.14 - char buf[4096]; 1.15 - int r; 1.16 - while ((r = fread(buf, 1, 4096, src)) > 0) { 1.17 - fwrite(buf, 1, r, dest); 1.18 - } 1.19 - fclose(src); 1.20 - return 0; 1.21 - } else { 1.22 - return 1; 1.23 - } 1.24 -} 1.25 - 1.26 #define WRITECONST(stream, out, cstr) out(cstr, 1, sizeof(cstr)-1, stream) 1.27 int formatfile( 1.28 highlighter_t *highlighter, 1.29 @@ -147,6 +128,8 @@ 1.30 highlighter->parser = jparseline; 1.31 } 1.32 1.33 +#define FILEBUF_SIZE 4096 1.34 + 1.35 enum source_type { 1.36 SOURCE_C, 1.37 SOURCE_JAVA, 1.38 @@ -217,10 +200,10 @@ 1.39 fout = stdout; 1.40 } 1.41 1.42 - if (copyfile(settings.headerfile, fout)) { 1.43 - perror("Error opening header file"); 1.44 - retcode = EXIT_FAILURE; 1.45 - goto prog_end; 1.46 + char *filebuf = malloc(FILEBUF_SIZE); 1.47 + if (!filebuf) { 1.48 + perror("Error allocating file buffer"); 1.49 + return EXIT_FAILURE; 1.50 } 1.51 1.52 highlighter_t highlighter; 1.53 @@ -240,28 +223,38 @@ 1.54 retcode = EXIT_FAILURE; 1.55 goto prog_end; 1.56 } 1.57 + 1.58 + { 1.59 + FILE *headerfile = fopen(settings.headerfile, "r"); 1.60 + if (!headerfile) { 1.61 + perror("Error opening header file"); 1.62 + retcode = EXIT_FAILURE; 1.63 + goto prog_end; 1.64 + } 1.65 + ucx_stream_copy(headerfile, fout, 1.66 + (read_func) fread, (write_func) fwrite, 1.67 + filebuf, FILEBUF_SIZE, (size_t)-1); 1.68 + fclose(headerfile); 1.69 + } 1.70 1.71 FILE *inputfile = fopen(settings.infilename, "r"); 1.72 if (inputfile) { 1.73 - UcxBuffer *filebuf = ucx_buffer_new(NULL, 1.74 - 8192, UCX_BUFFER_AUTOEXTEND); 1.75 + UcxBuffer *content = ucx_buffer_new(NULL, 1.76 + FILEBUF_SIZE*2, UCX_BUFFER_AUTOEXTEND); 1.77 { 1.78 - const size_t tmpbufsize = 4096; 1.79 - char *tmpbuf = malloc(tmpbufsize); 1.80 - ucx_stream_copy(inputfile, filebuf, (read_func) fread, 1.81 + ucx_stream_copy(inputfile, content, (read_func) fread, 1.82 (write_func) ucx_buffer_write, 1.83 - tmpbuf, tmpbufsize, (size_t)-1); 1.84 - free(tmpbuf); 1.85 + filebuf, FILEBUF_SIZE, (size_t)-1); 1.86 } 1.87 fclose(inputfile); 1.88 1.89 - UcxList *inputlines = ucx_list_append(NULL, filebuf->space); 1.90 - for (size_t i = 1 ; i < filebuf->size ; i++) { 1.91 - if (filebuf->space[i] == '\r') { 1.92 - filebuf->space[i] = '\n'; i++; 1.93 + UcxList *inputlines = ucx_list_append(NULL, content->space); 1.94 + for (size_t i = 1 ; i < content->size ; i++) { 1.95 + if (content->space[i] == '\r') { 1.96 + content->space[i] = '\n'; i++; 1.97 } 1.98 - if (filebuf->space[i] == '\n' && i+1 < filebuf->size) { 1.99 - ucx_list_append(inputlines, filebuf->space+i+1); 1.100 + if (content->space[i] == '\n' && i+1 < content->size) { 1.101 + ucx_list_append(inputlines, content->space+i+1); 1.102 } 1.103 } 1.104 1.105 @@ -271,17 +264,27 @@ 1.106 (write_func) fwrite, 1.107 fout, 1.108 settings.showlinenumbers); 1.109 - ucx_buffer_free(filebuf); 1.110 + ucx_buffer_free(content); 1.111 } else { 1.112 perror("Error opening input file"); 1.113 retcode = EXIT_FAILURE; 1.114 } 1.115 1.116 - if (copyfile(settings.footerfile, fout)) { 1.117 - perror("Error opening footer file"); 1.118 - retcode = EXIT_FAILURE; 1.119 + { 1.120 + FILE *footerfile = fopen(settings.footerfile, "r"); 1.121 + if (!footerfile) { 1.122 + perror("Error opening footer file"); 1.123 + retcode = EXIT_FAILURE; 1.124 + goto prog_end; 1.125 + } 1.126 + ucx_stream_copy(footerfile, fout, 1.127 + (read_func) fread, (write_func) fwrite, 1.128 + filebuf, FILEBUF_SIZE, (size_t)-1); 1.129 + fclose(footerfile); 1.130 } 1.131 1.132 + free(filebuf); 1.133 + 1.134 prog_end: 1.135 if (fout != stdout) { 1.136 fclose(fout);