adds appendfile() function to main module + adds TODOs for source files which do not terminate with a blank line

Fri, 26 Aug 2016 14:15:29 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 26 Aug 2016 14:15:29 +0200
changeset 53
5e47a26a16f0
parent 52
33ded421c512
child 54
b3f24e23bc25

adds appendfile() function to main module + adds TODOs for source files which do not terminate with a blank line

src/c2html.c file | annotate | diff | comparison | revisions
src/highlighter.c file | annotate | diff | comparison | revisions
--- a/src/c2html.c	Fri Aug 26 13:49:19 2016 +0200
+++ b/src/c2html.c	Fri Aug 26 14:15:29 2016 +0200
@@ -34,7 +34,24 @@
 
 #include "ucx/list.h"
 
-void printhelp() {
+static int appendfile(const char *filename, FILE *fout,
+        char *copybuf, size_t copybuflen, const char *errmsg) {
+    FILE *headerfile = fopen(filename, "r");
+    if (!headerfile) {
+        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);
+    return 0;
+}
+
+static void printhelp() {
     printf("Formats source code using HTML.\n\nUsage:\n"
         "  c2html [Options] FILE\n\n"
         " Options:\n"
@@ -49,16 +66,14 @@
         "\n");
 }
 
-void formatlines(highlighter_func highlighter,
+static void formatlines(highlighter_func highlighter,
         UcxList *in, write_func out, void *stream, int showlineno) {
     
     /* compute width of line numbering */
-    int lnw;
+    int lnw = 0;
     if (showlineno) {
         size_t lines = ucx_list_size(in);
-        lnw = 1;
-        int p = 1;
-        while ((p*=10) < lines) lnw++;
+        for (size_t p = 1; p < lines ; p*=10) lnw++;
     }
     
     /* start monospace formatting */
@@ -198,19 +213,9 @@
         }
         
         /* Prepend header file */
-        {
-            FILE *headerfile = fopen(settings.headerfile, "r");
-            if (!headerfile) {
-                perror("Error opening header file");
-                if (fout != stdout) {
-                    fclose(fout);
-                }
-                return EXIT_FAILURE;
-            }
-            ucx_stream_copy(headerfile, fout,
-                    (read_func) fread, (write_func) fwrite,
-                    filebuf, FILEBUF_SIZE, (size_t)-1);
-            fclose(headerfile);
+        if (appendfile(settings.headerfile, fout, filebuf, FILEBUF_SIZE,
+                "Error opening header file")) {
+            return EXIT_FAILURE;
         }
 
         /* Process input file */
@@ -248,21 +253,10 @@
         }
         
         /* Append footer file */
-        {
-            FILE *footerfile = fopen(settings.footerfile, "r");
-            if (!footerfile) {
-                perror("Error opening footer file");
-                if (fout != stdout) {
-                    fclose(fout);
-                }
-                return EXIT_FAILURE;
-            }
-            ucx_stream_copy(footerfile, fout,
-                    (read_func) fread, (write_func) fwrite,
-                    filebuf, FILEBUF_SIZE, (size_t)-1);
-            fclose(footerfile);
+        if (appendfile(settings.footerfile, fout, filebuf, FILEBUF_SIZE,
+                "Error opening footer file")) {
+            return EXIT_FAILURE;
         }
-
         
         free(filebuf);
 
--- a/src/highlighter.c	Fri Aug 26 13:49:19 2016 +0200
+++ b/src/highlighter.c	Fri Aug 26 14:15:29 2016 +0200
@@ -126,7 +126,10 @@
     char c;
     do {
         c = src[++sp];
-        if (!c) break;
+        if (!c) {
+            /* TODO: might cause problems if code file does not end with NL */
+            break;
+        }
         
         /* comments */
         if (!isstring && c == '/') {
@@ -268,7 +271,10 @@
     char c;
     do {
         c = src[++sp];
-        if (!c) break;
+        if (!c) {
+            /* TODO: might cause problems if code file does not end with NL */
+            break;
+        }
         
         /* comments */
         if (!isstring && c == '/') {

mercurial