src/c2html.c

changeset 42
7f2403c637a7
parent 41
c06ab07fd29d
child 43
a8cee98c8832
--- a/src/c2html.c	Tue Aug 23 14:13:46 2016 +0200
+++ b/src/c2html.c	Tue Aug 23 14:24:57 2016 +0200
@@ -47,25 +47,6 @@
         "\n");
 }
 
-int copyfile(char *filename, FILE *dest) {
-    if (!filename) {
-        return 0;
-    }
-    
-    FILE *src = fopen(filename, "r");
-    if (src) {
-        char buf[4096];
-        int r;
-        while ((r = fread(buf, 1, 4096, src)) > 0) {
-            fwrite(buf, 1, r, dest);
-        }
-        fclose(src);
-        return 0;
-    } else {
-        return 1;
-    }
-}
-
 #define WRITECONST(stream, out, cstr) out(cstr, 1, sizeof(cstr)-1, stream)
 int formatfile(
         highlighter_t *highlighter,
@@ -147,6 +128,8 @@
     highlighter->parser = jparseline;
 }
 
+#define FILEBUF_SIZE 4096
+
 enum source_type {
     SOURCE_C,
     SOURCE_JAVA,
@@ -217,10 +200,10 @@
             fout = stdout;
         }
         
-        if (copyfile(settings.headerfile, fout)) {
-            perror("Error opening header file");
-            retcode = EXIT_FAILURE;
-            goto prog_end;
+        char *filebuf = malloc(FILEBUF_SIZE);
+        if (!filebuf) {
+            perror("Error allocating file buffer");
+            return EXIT_FAILURE;
         }
         
         highlighter_t highlighter;
@@ -240,28 +223,38 @@
                 retcode = EXIT_FAILURE;
                 goto prog_end;
         }
+        
+        {
+            FILE *headerfile = fopen(settings.headerfile, "r");
+            if (!headerfile) {
+                perror("Error opening header file");
+                retcode = EXIT_FAILURE;
+                goto prog_end;
+            }
+            ucx_stream_copy(headerfile, fout,
+                    (read_func) fread, (write_func) fwrite,
+                    filebuf, FILEBUF_SIZE, (size_t)-1);
+            fclose(headerfile);
+        }
 
         FILE *inputfile = fopen(settings.infilename, "r");
         if (inputfile) {
-            UcxBuffer *filebuf = ucx_buffer_new(NULL,
-                    8192, UCX_BUFFER_AUTOEXTEND);
+            UcxBuffer *content = ucx_buffer_new(NULL,
+                    FILEBUF_SIZE*2, UCX_BUFFER_AUTOEXTEND);
             {
-                const size_t tmpbufsize = 4096;
-                char *tmpbuf = malloc(tmpbufsize);
-                ucx_stream_copy(inputfile, filebuf, (read_func) fread,
+                ucx_stream_copy(inputfile, content, (read_func) fread,
                         (write_func) ucx_buffer_write,
-                        tmpbuf, tmpbufsize, (size_t)-1);
-                free(tmpbuf);
+                        filebuf, FILEBUF_SIZE, (size_t)-1);
             }
             fclose(inputfile);
             
-            UcxList *inputlines = ucx_list_append(NULL, filebuf->space);
-            for (size_t i = 1 ; i < filebuf->size ; i++) {
-                if (filebuf->space[i] == '\r') {
-                    filebuf->space[i] = '\n'; i++;
+            UcxList *inputlines = ucx_list_append(NULL, content->space);
+            for (size_t i = 1 ; i < content->size ; i++) {
+                if (content->space[i] == '\r') {
+                    content->space[i] = '\n'; i++;
                 }
-                if (filebuf->space[i] == '\n' && i+1 < filebuf->size) {
-                    ucx_list_append(inputlines, filebuf->space+i+1);
+                if (content->space[i] == '\n' && i+1 < content->size) {
+                    ucx_list_append(inputlines, content->space+i+1);
                 }
             }
             
@@ -271,17 +264,27 @@
                     (write_func) fwrite,
                     fout,
                     settings.showlinenumbers);
-            ucx_buffer_free(filebuf);
+            ucx_buffer_free(content);
         } else {
             perror("Error opening input file");
             retcode = EXIT_FAILURE;
         }
         
-        if (copyfile(settings.footerfile, fout)) {
-            perror("Error opening footer file");
-            retcode = EXIT_FAILURE;
+        {
+            FILE *footerfile = fopen(settings.footerfile, "r");
+            if (!footerfile) {
+                perror("Error opening footer file");
+                retcode = EXIT_FAILURE;
+                goto prog_end;
+            }
+            ucx_stream_copy(footerfile, fout,
+                    (read_func) fread, (write_func) fwrite,
+                    filebuf, FILEBUF_SIZE, (size_t)-1);
+            fclose(footerfile);
         }
         
+        free(filebuf);
+        
         prog_end:        
         if (fout != stdout) {
             fclose(fout);

mercurial