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
     1.1 --- a/Makefile	Thu Jan 23 09:19:37 2014 +0100
     1.2 +++ b/Makefile	Thu Jan 23 14:17:06 2014 +0100
     1.3 @@ -37,12 +37,12 @@
     1.4  	$(MKDIR) build
     1.5  	
     1.6  test: compile
     1.7 -	./build/$(BIN) $(ARGS) src/c2html.c > build/body.html
     1.8 -	cat test/header.html build/body.html test/footer.html > build/code.html
     1.9 +	./build/$(BIN) $(ARGS) src/c2html.c -o build/code.html \
    1.10 +	-H test/header.html -F test/footer.html
    1.11  
    1.12  test-java: compile
    1.13 -	./build/$(BIN) $(ARGS) -j test/Game.java > build/body.html
    1.14 -	cat test/jheader.html build/body.html test/footer.html > build/code.html
    1.15 +	./build/$(BIN) $(ARGS) -j test/Game.java -o build/code.html \
    1.16 +	-H test/header.html -F test/footer.html
    1.17  	
    1.18  clean:
    1.19  	$(RM) -f -R build
     2.1 --- a/src/c2html.c	Thu Jan 23 09:19:37 2014 +0100
     2.2 +++ b/src/c2html.c	Thu Jan 23 14:17:06 2014 +0100
     2.3 @@ -1,56 +1,6 @@
     2.4 -/*
     2.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 - *
     2.7 - * Copyright 2014 Mike Becker. All rights reserved.
     2.8 - *
     2.9 - * Redistribution and use in source and binary forms, with or without
    2.10 - * modification, are permitted provided that the following conditions are met:
    2.11 - *
    2.12 - *   1. Redistributions of source code must retain the above copyright
    2.13 - *      notice, this list of conditions and the following disclaimer.
    2.14 - *
    2.15 - *   2. Redistributions in binary form must reproduce the above copyright
    2.16 - *      notice, this list of conditions and the following disclaimer in the
    2.17 - *      documentation and/or other materials provided with the distribution.
    2.18 - *
    2.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    2.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    2.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    2.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    2.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    2.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    2.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    2.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    2.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    2.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    2.29 - * POSSIBILITY OF SUCH DAMAGE.
    2.30 - *
    2.31 - */
    2.32 +#include <errno.h>
    2.33  
    2.34 -#include <stdio.h>
    2.35 -#include <stdlib.h>
    2.36 -#include <string.h>
    2.37 -#include <fcntl.h>
    2.38 -#include <unistd.h>
    2.39 -#include <ctype.h>
    2.40 -
    2.41 -#include "javacodegen.h"
    2.42 -#include "ccodegen.h"
    2.43 -
    2.44 -#define INPUTBUF_SIZE 2048
    2.45 -
    2.46 -typedef struct {
    2.47 -    char* outfilename;
    2.48 -    char* infilename;
    2.49 -    int highlight;
    2.50 -} settings_t;
    2.51 -
    2.52 -typedef struct {
    2.53 -    size_t count;
    2.54 -    size_t capacity;
    2.55 -    size_t maxlinewidth;
    2.56 -    char** lines;
    2.57 -} inputfile_t;
    2.58 +#include "c2html.h"
    2.59  
    2.60  inputfile_t *inputfilebuffer(size_t capacity) {
    2.61      inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t));
    2.62 @@ -130,6 +80,8 @@
    2.63          "  -h                    Prints this help message\n"
    2.64          "  -j                    Highlight Java instead of C source code\n"
    2.65          "  -o <output>           Output file (stdout, if not specified)\n"
    2.66 +        "  -H <header>           Prepend header file\n"
    2.67 +        "  -F <footer>           Append footer file\n"
    2.68          "  -p                    Disable highlighting (plain text)\n"
    2.69          "\n");
    2.70  
    2.71 @@ -142,11 +94,32 @@
    2.72      return w;
    2.73  }
    2.74  
    2.75 +int copyfile(char *filename, FILE *dest) {
    2.76 +    if (!filename) {
    2.77 +        return 0;
    2.78 +    }
    2.79 +    
    2.80 +    FILE *src = fopen(filename, "r");
    2.81 +    if (src) {
    2.82 +        char buf[4096];
    2.83 +        int r;
    2.84 +        while ((r = fread(buf, 1, 4096, src)) > 0) {
    2.85 +            fwrite(buf, 1, r, dest);
    2.86 +        }
    2.87 +        fclose(src);
    2.88 +        return 0;
    2.89 +    } else {
    2.90 +        return errno;
    2.91 +    }
    2.92 +}
    2.93 +
    2.94  int main(int argc, char** argv) {
    2.95 +    int retcode = EXIT_SUCCESS;
    2.96 +    
    2.97      settings_t settings;
    2.98 -    settings.outfilename = NULL;
    2.99 +    memset(&settings, 0, sizeof(settings));
   2.100      settings.highlight = 1;
   2.101 -
   2.102 +    
   2.103      highlighter_t highlighter;
   2.104      memset(&highlighter, 0, sizeof(highlighter));
   2.105      highlighter.isdirective = iscdirective;
   2.106 @@ -155,13 +128,19 @@
   2.107      highlighter.parser = cparseline;
   2.108  
   2.109      char optc;
   2.110 -    while ((optc = getopt(argc, argv, "hjo:p")) != -1) {
   2.111 +    while ((optc = getopt(argc, argv, "hjo:pH:F:")) != -1) {
   2.112          switch (optc) {
   2.113              case 'o':
   2.114                  if (!(optarg[0] == '-' && optarg[1] == 0)) {
   2.115                      settings.outfilename = optarg;
   2.116                  }
   2.117                  break;
   2.118 +            case 'F':
   2.119 +                settings.footerfile = optarg;
   2.120 +                break;
   2.121 +            case 'H':
   2.122 +                settings.headerfile = optarg;
   2.123 +                break;
   2.124              case 'j':
   2.125                  highlighter.isdirective = isjdirective;
   2.126                  highlighter.istype = isjtype;
   2.127 @@ -184,21 +163,31 @@
   2.128          return 1;
   2.129      } else {
   2.130          settings.infilename = argv[optind];
   2.131 +        FILE *fout;
   2.132 +        if (settings.outfilename) {
   2.133 +            fout = fopen(settings.outfilename, "w");
   2.134 +            if (!fout) {
   2.135 +                perror("Error opening output file");
   2.136 +                return errno;
   2.137 +            }
   2.138 +        } else {
   2.139 +            fout = stdout;
   2.140 +        }
   2.141 +        
   2.142 +        if (copyfile(settings.headerfile, fout)) {
   2.143 +            perror("Error opening header file");
   2.144 +            retcode = errno;
   2.145 +            goto prog_end;
   2.146 +        }
   2.147  
   2.148          inputfile_t *inputfile = readinput(settings.infilename);
   2.149          if (inputfile) {
   2.150 -            FILE *fout;
   2.151              char *line;
   2.152              if (settings.highlight) {
   2.153                  line = (char*) malloc(inputfile->maxlinewidth*64);
   2.154              } else {
   2.155                  line = NULL;
   2.156              }
   2.157 -            if (settings.outfilename) {
   2.158 -                fout = fopen(settings.outfilename, "w");
   2.159 -            } else {
   2.160 -                fout = stdout;
   2.161 -            }
   2.162              fprintf(fout, "<pre>\n");
   2.163              int lnw = lnint(inputfile->count);
   2.164              for (int i = 0 ; i < inputfile->count ; i++) {
   2.165 @@ -215,14 +204,23 @@
   2.166              }
   2.167              fprintf(fout, "</pre>\n");
   2.168  
   2.169 -            if (fout != stdout) {
   2.170 -                fclose(fout);
   2.171 +            freeinputfilebuffer(inputfile);
   2.172 +            
   2.173 +            if (copyfile(settings.footerfile, fout)) {
   2.174 +                perror("Error opening footer file");
   2.175 +                retcode = errno;
   2.176              }
   2.177 -
   2.178 -            freeinputfilebuffer(inputfile);
   2.179 +        } else {
   2.180 +            perror("Error opening input file");
   2.181 +            retcode = errno;
   2.182 +        }
   2.183 +        
   2.184 +        prog_end:        
   2.185 +        if (fout != stdout) {
   2.186 +            fclose(fout);
   2.187          }
   2.188  
   2.189 -        return 0;
   2.190 +        return retcode;
   2.191      }
   2.192  }
   2.193  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/c2html.h	Thu Jan 23 14:17:06 2014 +0100
     3.3 @@ -0,0 +1,71 @@
     3.4 +/*
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + *
     3.7 + * Copyright 2014 Mike Becker. All rights reserved.
     3.8 + *
     3.9 + * Redistribution and use in source and binary forms, with or without
    3.10 + * modification, are permitted provided that the following conditions are met:
    3.11 + *
    3.12 + *   1. Redistributions of source code must retain the above copyright
    3.13 + *      notice, this list of conditions and the following disclaimer.
    3.14 + *
    3.15 + *   2. Redistributions in binary form must reproduce the above copyright
    3.16 + *      notice, this list of conditions and the following disclaimer in the
    3.17 + *      documentation and/or other materials provided with the distribution.
    3.18 + *
    3.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    3.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    3.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    3.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    3.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    3.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    3.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    3.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    3.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    3.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    3.29 + * POSSIBILITY OF SUCH DAMAGE.
    3.30 + *
    3.31 + */
    3.32 +
    3.33 +#ifndef C2HTML_H
    3.34 +#define	C2HTML_H
    3.35 +
    3.36 +#include <stdio.h>
    3.37 +#include <stdlib.h>
    3.38 +#include <string.h>
    3.39 +#include <fcntl.h>
    3.40 +#include <unistd.h>
    3.41 +#include <ctype.h>
    3.42 +
    3.43 +#include "javacodegen.h"
    3.44 +#include "ccodegen.h"
    3.45 +
    3.46 +#ifdef	__cplusplus
    3.47 +extern "C" {
    3.48 +#endif
    3.49 +
    3.50 +
    3.51 +#define INPUTBUF_SIZE 2048
    3.52 +
    3.53 +typedef struct {
    3.54 +    char* outfilename;
    3.55 +    char* headerfile;
    3.56 +    char* footerfile;
    3.57 +    char* infilename;
    3.58 +    int highlight;
    3.59 +} settings_t;
    3.60 +
    3.61 +typedef struct {
    3.62 +    size_t count;
    3.63 +    size_t capacity;
    3.64 +    size_t maxlinewidth;
    3.65 +    char** lines;
    3.66 +} inputfile_t;
    3.67 +
    3.68 +
    3.69 +#ifdef	__cplusplus
    3.70 +}
    3.71 +#endif
    3.72 +
    3.73 +#endif	/* C2HTML_H */
    3.74 +

mercurial