improved code structure and added option for disabling line numbers

Sun, 19 Apr 2015 10:48:00 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 19 Apr 2015 10:48:00 +0200
changeset 24
e43dee5892f4
parent 23
f44a185b678b
child 25
f82aa7afe872

improved code structure and added option for disabling line numbers

LICENSE file | annotate | diff | comparison | revisions
src/c2html.c file | annotate | diff | comparison | revisions
src/c2html.h file | annotate | diff | comparison | revisions
src/ccodegen.c file | annotate | diff | comparison | revisions
src/ccodegen.h file | annotate | diff | comparison | revisions
src/codegens.c file | annotate | diff | comparison | revisions
src/codegens.h file | annotate | diff | comparison | revisions
src/javacodegen.c file | annotate | diff | comparison | revisions
src/javacodegen.h file | annotate | diff | comparison | revisions
     1.1 --- a/LICENSE	Thu Jan 23 14:44:20 2014 +0100
     1.2 +++ b/LICENSE	Sun Apr 19 10:48:00 2015 +0200
     1.3 @@ -1,4 +1,4 @@
     1.4 -Copyright 2014 Mike Becker. All rights reserved.
     1.5 +Copyright 2015 Mike Becker. All rights reserved.
     1.6  
     1.7  Redistribution and use in source and binary forms, with or without
     1.8  modification, are permitted provided that the following conditions are met:
     2.1 --- a/src/c2html.c	Thu Jan 23 14:44:20 2014 +0100
     2.2 +++ b/src/c2html.c	Sun Apr 19 10:48:00 2015 +0200
     2.3 @@ -1,7 +1,7 @@
     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 + * Copyright 2015 Mike Becker. All rights reserved.
     2.9   *
    2.10   * Redistribution and use in source and binary forms, with or without
    2.11   * modification, are permitted provided that the following conditions are met:
    2.12 @@ -26,8 +26,6 @@
    2.13   * POSSIBILITY OF SUCH DAMAGE.
    2.14   *
    2.15   */
    2.16 -#include <errno.h>
    2.17 -
    2.18  #include "c2html.h"
    2.19  
    2.20  inputfile_t *inputfilebuffer(size_t capacity) {
    2.21 @@ -111,6 +109,7 @@
    2.22          "  -H <header>           Prepend header file\n"
    2.23          "  -F <footer>           Append footer file\n"
    2.24          "  -p                    Disable highlighting (plain text)\n"
    2.25 +        "  -l                    Disable line numbers\n"
    2.26          "\n");
    2.27  
    2.28  
    2.29 @@ -137,26 +136,80 @@
    2.30          fclose(src);
    2.31          return 0;
    2.32      } else {
    2.33 -        return errno;
    2.34 +        return -1;
    2.35      }
    2.36  }
    2.37  
    2.38 +#define WRITECONST(stream, out, cstr) out(cstr, 1, sizeof(cstr)-1, stream)
    2.39 +int formatfile(
    2.40 +        highlighter_t *highlighter,
    2.41 +        inputfile_t *in,
    2.42 +        fmt_write_func out,
    2.43 +        void *stream,
    2.44 +        _Bool showln) {
    2.45 +    // formats an input file and writes the result to out
    2.46 +    
    2.47 +    char *line = malloc(in->maxlinewidth*64);
    2.48 +    if(!line) {
    2.49 +        return 1;
    2.50 +    }
    2.51 +    WRITECONST(stream, out, "<pre>\n");
    2.52 +    
    2.53 +    int lnw = lnint(in->count);
    2.54 +    for (int i = 0 ; i < in->count ; i++) {
    2.55 +        char *ln = line;
    2.56 +        if (highlighter) {
    2.57 +            highlighter->parser(in->lines[i], line, highlighter);
    2.58 +        } else {
    2.59 +            ln = in->lines[i];
    2.60 +        }
    2.61 +        
    2.62 +        // write line number
    2.63 +        if (showln) {
    2.64 +            WRITECONST(stream, out, "<span class=\"c2html-lineno\">");
    2.65 +            char lnbuf[16];
    2.66 +            int len = snprintf(lnbuf, 16, "%*d ", lnw, i+1);
    2.67 +            out(lnbuf, 1, len, stream);
    2.68 +            WRITECONST(stream, out, "</span> ");
    2.69 +        }
    2.70 +        
    2.71 +        // write formated (or plain) code line
    2.72 +        out(ln, 1, strlen(ln), stream);
    2.73 +    }
    2.74 +    
    2.75 +    WRITECONST(stream, out, "</pre>\n");
    2.76 +    free(line);
    2.77 +    return 0;
    2.78 +}
    2.79 +
    2.80 +void init_c_highlighter(highlighter_t *highlighter) {
    2.81 +    memset(highlighter, 0, sizeof(highlighter_t));
    2.82 +    highlighter->isdirective = iscdirective;
    2.83 +    highlighter->istype = isctype;
    2.84 +    highlighter->keywords = ckeywords;
    2.85 +    highlighter->parser = cparseline;
    2.86 +}
    2.87 +
    2.88 +void init_java_highlighter(highlighter_t *highlighter) {
    2.89 +    memset(highlighter, 0, sizeof(highlighter_t));
    2.90 +    highlighter->isdirective = isjdirective;
    2.91 +    highlighter->istype = isjtype;
    2.92 +    highlighter->keywords = jkeywords;
    2.93 +    highlighter->parser = jparseline;
    2.94 +}
    2.95 +
    2.96  int main(int argc, char** argv) {
    2.97      int retcode = EXIT_SUCCESS;
    2.98      
    2.99      settings_t settings;
   2.100      memset(&settings, 0, sizeof(settings));
   2.101      settings.highlight = 1;
   2.102 +    settings.showlinenumbers = 1;
   2.103      
   2.104 -    highlighter_t highlighter;
   2.105 -    memset(&highlighter, 0, sizeof(highlighter));
   2.106 -    highlighter.isdirective = iscdirective;
   2.107 -    highlighter.istype = isctype;
   2.108 -    highlighter.keywords = ckeywords;
   2.109 -    highlighter.parser = cparseline;
   2.110 +    int lang = C2HTML_C;
   2.111  
   2.112      char optc;
   2.113 -    while ((optc = getopt(argc, argv, "hjo:pH:F:")) != -1) {
   2.114 +    while ((optc = getopt(argc, argv, "hljo:pH:F:")) != -1) {
   2.115          switch (optc) {
   2.116              case 'o':
   2.117                  if (!(optarg[0] == '-' && optarg[1] == 0)) {
   2.118 @@ -170,14 +223,14 @@
   2.119                  settings.headerfile = optarg;
   2.120                  break;
   2.121              case 'j':
   2.122 -                highlighter.isdirective = isjdirective;
   2.123 -                highlighter.istype = isjtype;
   2.124 -                highlighter.keywords = jkeywords;
   2.125 -                highlighter.parser = jparseline;
   2.126 +                lang = C2HTML_JAVA;
   2.127                  break;
   2.128              case 'p':
   2.129                  settings.highlight = 0;
   2.130                  break;
   2.131 +            case 'l':
   2.132 +                settings.showlinenumbers = 0;
   2.133 +                break;
   2.134              case 'h':
   2.135                  printhelp();
   2.136                  return 0;
   2.137 @@ -196,7 +249,7 @@
   2.138              fout = fopen(settings.outfilename, "w");
   2.139              if (!fout) {
   2.140                  perror("Error opening output file");
   2.141 -                return errno;
   2.142 +                return -1;
   2.143              }
   2.144          } else {
   2.145              fout = stdout;
   2.146 @@ -204,43 +257,43 @@
   2.147          
   2.148          if (copyfile(settings.headerfile, fout)) {
   2.149              perror("Error opening header file");
   2.150 -            retcode = errno;
   2.151 +            retcode = -1;
   2.152              goto prog_end;
   2.153          }
   2.154 +        
   2.155 +        highlighter_t highlighter;
   2.156 +        highlighter_t *hptr = &highlighter;
   2.157 +        switch (lang) {
   2.158 +            case C2HTML_C:
   2.159 +                init_c_highlighter(&highlighter);
   2.160 +                break;
   2.161 +            case C2HTML_JAVA:
   2.162 +                init_java_highlighter(&highlighter);
   2.163 +                break;
   2.164 +            default:
   2.165 +                hptr = NULL;
   2.166 +                break;
   2.167 +        }
   2.168 +        if (!settings.highlight) {
   2.169 +            hptr = NULL;
   2.170 +        }
   2.171  
   2.172          inputfile_t *inputfile = readinput(settings.infilename);
   2.173          if (inputfile) {
   2.174 -            char *line;
   2.175 -            if (settings.highlight) {
   2.176 -                line = (char*) malloc(inputfile->maxlinewidth*64);
   2.177 -            } else {
   2.178 -                line = NULL;
   2.179 -            }
   2.180 -            fprintf(fout, "<pre>\n");
   2.181 -            int lnw = lnint(inputfile->count);
   2.182 -            for (int i = 0 ; i < inputfile->count ; i++) {
   2.183 -                if (settings.highlight) {
   2.184 -                    highlighter.parser(inputfile->lines[i], line, &highlighter);
   2.185 -                } else {
   2.186 -                    line = inputfile->lines[i];
   2.187 -                }
   2.188 -                fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s",
   2.189 -                    lnw, i+1, line);
   2.190 -            }
   2.191 -            if (settings.highlight) {
   2.192 -                free(line);
   2.193 -            }
   2.194 -            fprintf(fout, "</pre>\n");
   2.195 -
   2.196 -            freeinputfilebuffer(inputfile);
   2.197 -            
   2.198 -            if (copyfile(settings.footerfile, fout)) {
   2.199 -                perror("Error opening footer file");
   2.200 -                retcode = errno;
   2.201 -            }
   2.202 +            formatfile(
   2.203 +                    hptr,
   2.204 +                    inputfile,
   2.205 +                    (fmt_write_func)fwrite,
   2.206 +                    fout,
   2.207 +                    settings.showlinenumbers);
   2.208          } else {
   2.209              perror("Error opening input file");
   2.210 -            retcode = errno;
   2.211 +            retcode = -1;
   2.212 +        }
   2.213 +        
   2.214 +        if (copyfile(settings.footerfile, fout)) {
   2.215 +            perror("Error opening footer file");
   2.216 +            retcode = -1;
   2.217          }
   2.218          
   2.219          prog_end:        
     3.1 --- a/src/c2html.h	Thu Jan 23 14:44:20 2014 +0100
     3.2 +++ b/src/c2html.h	Sun Apr 19 10:48:00 2015 +0200
     3.3 @@ -1,7 +1,7 @@
     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 + * Copyright 2015 Mike Becker. All rights reserved.
     3.9   *
    3.10   * Redistribution and use in source and binary forms, with or without
    3.11   * modification, are permitted provided that the following conditions are met:
    3.12 @@ -47,12 +47,16 @@
    3.13  
    3.14  #define INPUTBUF_SIZE 2048
    3.15  
    3.16 +#define C2HTML_C    0
    3.17 +#define C2HTML_JAVA 1
    3.18 +    
    3.19  typedef struct {
    3.20      char* outfilename;
    3.21      char* headerfile;
    3.22      char* footerfile;
    3.23      char* infilename;
    3.24      int highlight;
    3.25 +    int showlinenumbers;
    3.26  } settings_t;
    3.27  
    3.28  typedef struct {
    3.29 @@ -62,6 +66,7 @@
    3.30      char** lines;
    3.31  } inputfile_t;
    3.32  
    3.33 +typedef size_t(*fmt_write_func)(const void*, size_t, size_t, void*);
    3.34  
    3.35  #ifdef	__cplusplus
    3.36  }
     4.1 --- a/src/ccodegen.c	Thu Jan 23 14:44:20 2014 +0100
     4.2 +++ b/src/ccodegen.c	Sun Apr 19 10:48:00 2015 +0200
     4.3 @@ -1,7 +1,7 @@
     4.4  /*
     4.5   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     4.6   *
     4.7 - * Copyright 2014 Mike Becker. All rights reserved.
     4.8 + * Copyright 2015 Mike Becker. All rights reserved.
     4.9   *
    4.10   * Redistribution and use in source and binary forms, with or without
    4.11   * modification, are permitted provided that the following conditions are met:
     5.1 --- a/src/ccodegen.h	Thu Jan 23 14:44:20 2014 +0100
     5.2 +++ b/src/ccodegen.h	Sun Apr 19 10:48:00 2015 +0200
     5.3 @@ -1,7 +1,7 @@
     5.4  /*
     5.5   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     5.6   *
     5.7 - * Copyright 2014 Mike Becker. All rights reserved.
     5.8 + * Copyright 2015 Mike Becker. All rights reserved.
     5.9   *
    5.10   * Redistribution and use in source and binary forms, with or without
    5.11   * modification, are permitted provided that the following conditions are met:
     6.1 --- a/src/codegens.c	Thu Jan 23 14:44:20 2014 +0100
     6.2 +++ b/src/codegens.c	Sun Apr 19 10:48:00 2015 +0200
     6.3 @@ -1,7 +1,7 @@
     6.4  /*
     6.5   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     6.6   *
     6.7 - * Copyright 2014 Mike Becker. All rights reserved.
     6.8 + * Copyright 2015 Mike Becker. All rights reserved.
     6.9   *
    6.10   * Redistribution and use in source and binary forms, with or without
    6.11   * modification, are permitted provided that the following conditions are met:
     7.1 --- a/src/codegens.h	Thu Jan 23 14:44:20 2014 +0100
     7.2 +++ b/src/codegens.h	Sun Apr 19 10:48:00 2015 +0200
     7.3 @@ -1,7 +1,7 @@
     7.4  /*
     7.5   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     7.6   *
     7.7 - * Copyright 2014 Mike Becker. All rights reserved.
     7.8 + * Copyright 2015 Mike Becker. All rights reserved.
     7.9   *
    7.10   * Redistribution and use in source and binary forms, with or without
    7.11   * modification, are permitted provided that the following conditions are met:
     8.1 --- a/src/javacodegen.c	Thu Jan 23 14:44:20 2014 +0100
     8.2 +++ b/src/javacodegen.c	Sun Apr 19 10:48:00 2015 +0200
     8.3 @@ -1,7 +1,7 @@
     8.4  /*
     8.5   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     8.6   *
     8.7 - * Copyright 2014 Mike Becker. All rights reserved.
     8.8 + * Copyright 2015 Mike Becker. All rights reserved.
     8.9   *
    8.10   * Redistribution and use in source and binary forms, with or without
    8.11   * modification, are permitted provided that the following conditions are met:
     9.1 --- a/src/javacodegen.h	Thu Jan 23 14:44:20 2014 +0100
     9.2 +++ b/src/javacodegen.h	Sun Apr 19 10:48:00 2015 +0200
     9.3 @@ -1,7 +1,7 @@
     9.4  /*
     9.5   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     9.6   *
     9.7 - * Copyright 2014 Mike Becker. All rights reserved.
     9.8 + * Copyright 2015 Mike Becker. All rights reserved.
     9.9   *
    9.10   * Redistribution and use in source and binary forms, with or without
    9.11   * modification, are permitted provided that the following conditions are met:

mercurial