added command line parameters for header and footer file

Thu, 23 Jan 2014 14:17:06 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 23 Jan 2014 14:17:06 +0100
changeset 22
f463693b5eeb
parent 21
537aec525835
child 23
f44a185b678b

added command line parameters for header and footer file

Makefile file | annotate | diff | comparison | revisions
src/c2html.c file | annotate | diff | comparison | revisions
src/c2html.h file | annotate | diff | comparison | revisions
--- a/Makefile	Thu Jan 23 09:19:37 2014 +0100
+++ b/Makefile	Thu Jan 23 14:17:06 2014 +0100
@@ -37,12 +37,12 @@
 	$(MKDIR) build
 	
 test: compile
-	./build/$(BIN) $(ARGS) src/c2html.c > build/body.html
-	cat test/header.html build/body.html test/footer.html > build/code.html
+	./build/$(BIN) $(ARGS) src/c2html.c -o build/code.html \
+	-H test/header.html -F test/footer.html
 
 test-java: compile
-	./build/$(BIN) $(ARGS) -j test/Game.java > build/body.html
-	cat test/jheader.html build/body.html test/footer.html > build/code.html
+	./build/$(BIN) $(ARGS) -j test/Game.java -o build/code.html \
+	-H test/header.html -F test/footer.html
 	
 clean:
 	$(RM) -f -R build
--- a/src/c2html.c	Thu Jan 23 09:19:37 2014 +0100
+++ b/src/c2html.c	Thu Jan 23 14:17:06 2014 +0100
@@ -1,56 +1,6 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2014 Mike Becker. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- */
+#include <errno.h>
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <ctype.h>
-
-#include "javacodegen.h"
-#include "ccodegen.h"
-
-#define INPUTBUF_SIZE 2048
-
-typedef struct {
-    char* outfilename;
-    char* infilename;
-    int highlight;
-} settings_t;
-
-typedef struct {
-    size_t count;
-    size_t capacity;
-    size_t maxlinewidth;
-    char** lines;
-} inputfile_t;
+#include "c2html.h"
 
 inputfile_t *inputfilebuffer(size_t capacity) {
     inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t));
@@ -130,6 +80,8 @@
         "  -h                    Prints this help message\n"
         "  -j                    Highlight Java instead of C source code\n"
         "  -o <output>           Output file (stdout, if not specified)\n"
+        "  -H <header>           Prepend header file\n"
+        "  -F <footer>           Append footer file\n"
         "  -p                    Disable highlighting (plain text)\n"
         "\n");
 
@@ -142,11 +94,32 @@
     return w;
 }
 
+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 errno;
+    }
+}
+
 int main(int argc, char** argv) {
+    int retcode = EXIT_SUCCESS;
+    
     settings_t settings;
-    settings.outfilename = NULL;
+    memset(&settings, 0, sizeof(settings));
     settings.highlight = 1;
-
+    
     highlighter_t highlighter;
     memset(&highlighter, 0, sizeof(highlighter));
     highlighter.isdirective = iscdirective;
@@ -155,13 +128,19 @@
     highlighter.parser = cparseline;
 
     char optc;
-    while ((optc = getopt(argc, argv, "hjo:p")) != -1) {
+    while ((optc = getopt(argc, argv, "hjo:pH:F:")) != -1) {
         switch (optc) {
             case 'o':
                 if (!(optarg[0] == '-' && optarg[1] == 0)) {
                     settings.outfilename = optarg;
                 }
                 break;
+            case 'F':
+                settings.footerfile = optarg;
+                break;
+            case 'H':
+                settings.headerfile = optarg;
+                break;
             case 'j':
                 highlighter.isdirective = isjdirective;
                 highlighter.istype = isjtype;
@@ -184,21 +163,31 @@
         return 1;
     } else {
         settings.infilename = argv[optind];
+        FILE *fout;
+        if (settings.outfilename) {
+            fout = fopen(settings.outfilename, "w");
+            if (!fout) {
+                perror("Error opening output file");
+                return errno;
+            }
+        } else {
+            fout = stdout;
+        }
+        
+        if (copyfile(settings.headerfile, fout)) {
+            perror("Error opening header file");
+            retcode = errno;
+            goto prog_end;
+        }
 
         inputfile_t *inputfile = readinput(settings.infilename);
         if (inputfile) {
-            FILE *fout;
             char *line;
             if (settings.highlight) {
                 line = (char*) malloc(inputfile->maxlinewidth*64);
             } else {
                 line = NULL;
             }
-            if (settings.outfilename) {
-                fout = fopen(settings.outfilename, "w");
-            } else {
-                fout = stdout;
-            }
             fprintf(fout, "<pre>\n");
             int lnw = lnint(inputfile->count);
             for (int i = 0 ; i < inputfile->count ; i++) {
@@ -215,14 +204,23 @@
             }
             fprintf(fout, "</pre>\n");
 
-            if (fout != stdout) {
-                fclose(fout);
+            freeinputfilebuffer(inputfile);
+            
+            if (copyfile(settings.footerfile, fout)) {
+                perror("Error opening footer file");
+                retcode = errno;
             }
-
-            freeinputfilebuffer(inputfile);
+        } else {
+            perror("Error opening input file");
+            retcode = errno;
+        }
+        
+        prog_end:        
+        if (fout != stdout) {
+            fclose(fout);
         }
 
-        return 0;
+        return retcode;
     }
 }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/c2html.h	Thu Jan 23 14:17:06 2014 +0100
@@ -0,0 +1,71 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Mike Becker. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef C2HTML_H
+#define	C2HTML_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <ctype.h>
+
+#include "javacodegen.h"
+#include "ccodegen.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+
+#define INPUTBUF_SIZE 2048
+
+typedef struct {
+    char* outfilename;
+    char* headerfile;
+    char* footerfile;
+    char* infilename;
+    int highlight;
+} settings_t;
+
+typedef struct {
+    size_t count;
+    size_t capacity;
+    size_t maxlinewidth;
+    char** lines;
+} inputfile_t;
+
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* C2HTML_H */
+

mercurial