src/c2html.c

changeset 24
e43dee5892f4
parent 23
f44a185b678b
child 25
f82aa7afe872
     1.1 --- a/src/c2html.c	Thu Jan 23 14:44:20 2014 +0100
     1.2 +++ b/src/c2html.c	Sun Apr 19 10:48:00 2015 +0200
     1.3 @@ -1,7 +1,7 @@
     1.4  /*
     1.5   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6   *
     1.7 - * Copyright 2014 Mike Becker. All rights reserved.
     1.8 + * Copyright 2015 Mike Becker. All rights reserved.
     1.9   *
    1.10   * Redistribution and use in source and binary forms, with or without
    1.11   * modification, are permitted provided that the following conditions are met:
    1.12 @@ -26,8 +26,6 @@
    1.13   * POSSIBILITY OF SUCH DAMAGE.
    1.14   *
    1.15   */
    1.16 -#include <errno.h>
    1.17 -
    1.18  #include "c2html.h"
    1.19  
    1.20  inputfile_t *inputfilebuffer(size_t capacity) {
    1.21 @@ -111,6 +109,7 @@
    1.22          "  -H <header>           Prepend header file\n"
    1.23          "  -F <footer>           Append footer file\n"
    1.24          "  -p                    Disable highlighting (plain text)\n"
    1.25 +        "  -l                    Disable line numbers\n"
    1.26          "\n");
    1.27  
    1.28  
    1.29 @@ -137,26 +136,80 @@
    1.30          fclose(src);
    1.31          return 0;
    1.32      } else {
    1.33 -        return errno;
    1.34 +        return -1;
    1.35      }
    1.36  }
    1.37  
    1.38 +#define WRITECONST(stream, out, cstr) out(cstr, 1, sizeof(cstr)-1, stream)
    1.39 +int formatfile(
    1.40 +        highlighter_t *highlighter,
    1.41 +        inputfile_t *in,
    1.42 +        fmt_write_func out,
    1.43 +        void *stream,
    1.44 +        _Bool showln) {
    1.45 +    // formats an input file and writes the result to out
    1.46 +    
    1.47 +    char *line = malloc(in->maxlinewidth*64);
    1.48 +    if(!line) {
    1.49 +        return 1;
    1.50 +    }
    1.51 +    WRITECONST(stream, out, "<pre>\n");
    1.52 +    
    1.53 +    int lnw = lnint(in->count);
    1.54 +    for (int i = 0 ; i < in->count ; i++) {
    1.55 +        char *ln = line;
    1.56 +        if (highlighter) {
    1.57 +            highlighter->parser(in->lines[i], line, highlighter);
    1.58 +        } else {
    1.59 +            ln = in->lines[i];
    1.60 +        }
    1.61 +        
    1.62 +        // write line number
    1.63 +        if (showln) {
    1.64 +            WRITECONST(stream, out, "<span class=\"c2html-lineno\">");
    1.65 +            char lnbuf[16];
    1.66 +            int len = snprintf(lnbuf, 16, "%*d ", lnw, i+1);
    1.67 +            out(lnbuf, 1, len, stream);
    1.68 +            WRITECONST(stream, out, "</span> ");
    1.69 +        }
    1.70 +        
    1.71 +        // write formated (or plain) code line
    1.72 +        out(ln, 1, strlen(ln), stream);
    1.73 +    }
    1.74 +    
    1.75 +    WRITECONST(stream, out, "</pre>\n");
    1.76 +    free(line);
    1.77 +    return 0;
    1.78 +}
    1.79 +
    1.80 +void init_c_highlighter(highlighter_t *highlighter) {
    1.81 +    memset(highlighter, 0, sizeof(highlighter_t));
    1.82 +    highlighter->isdirective = iscdirective;
    1.83 +    highlighter->istype = isctype;
    1.84 +    highlighter->keywords = ckeywords;
    1.85 +    highlighter->parser = cparseline;
    1.86 +}
    1.87 +
    1.88 +void init_java_highlighter(highlighter_t *highlighter) {
    1.89 +    memset(highlighter, 0, sizeof(highlighter_t));
    1.90 +    highlighter->isdirective = isjdirective;
    1.91 +    highlighter->istype = isjtype;
    1.92 +    highlighter->keywords = jkeywords;
    1.93 +    highlighter->parser = jparseline;
    1.94 +}
    1.95 +
    1.96  int main(int argc, char** argv) {
    1.97      int retcode = EXIT_SUCCESS;
    1.98      
    1.99      settings_t settings;
   1.100      memset(&settings, 0, sizeof(settings));
   1.101      settings.highlight = 1;
   1.102 +    settings.showlinenumbers = 1;
   1.103      
   1.104 -    highlighter_t highlighter;
   1.105 -    memset(&highlighter, 0, sizeof(highlighter));
   1.106 -    highlighter.isdirective = iscdirective;
   1.107 -    highlighter.istype = isctype;
   1.108 -    highlighter.keywords = ckeywords;
   1.109 -    highlighter.parser = cparseline;
   1.110 +    int lang = C2HTML_C;
   1.111  
   1.112      char optc;
   1.113 -    while ((optc = getopt(argc, argv, "hjo:pH:F:")) != -1) {
   1.114 +    while ((optc = getopt(argc, argv, "hljo:pH:F:")) != -1) {
   1.115          switch (optc) {
   1.116              case 'o':
   1.117                  if (!(optarg[0] == '-' && optarg[1] == 0)) {
   1.118 @@ -170,14 +223,14 @@
   1.119                  settings.headerfile = optarg;
   1.120                  break;
   1.121              case 'j':
   1.122 -                highlighter.isdirective = isjdirective;
   1.123 -                highlighter.istype = isjtype;
   1.124 -                highlighter.keywords = jkeywords;
   1.125 -                highlighter.parser = jparseline;
   1.126 +                lang = C2HTML_JAVA;
   1.127                  break;
   1.128              case 'p':
   1.129                  settings.highlight = 0;
   1.130                  break;
   1.131 +            case 'l':
   1.132 +                settings.showlinenumbers = 0;
   1.133 +                break;
   1.134              case 'h':
   1.135                  printhelp();
   1.136                  return 0;
   1.137 @@ -196,7 +249,7 @@
   1.138              fout = fopen(settings.outfilename, "w");
   1.139              if (!fout) {
   1.140                  perror("Error opening output file");
   1.141 -                return errno;
   1.142 +                return -1;
   1.143              }
   1.144          } else {
   1.145              fout = stdout;
   1.146 @@ -204,43 +257,43 @@
   1.147          
   1.148          if (copyfile(settings.headerfile, fout)) {
   1.149              perror("Error opening header file");
   1.150 -            retcode = errno;
   1.151 +            retcode = -1;
   1.152              goto prog_end;
   1.153          }
   1.154 +        
   1.155 +        highlighter_t highlighter;
   1.156 +        highlighter_t *hptr = &highlighter;
   1.157 +        switch (lang) {
   1.158 +            case C2HTML_C:
   1.159 +                init_c_highlighter(&highlighter);
   1.160 +                break;
   1.161 +            case C2HTML_JAVA:
   1.162 +                init_java_highlighter(&highlighter);
   1.163 +                break;
   1.164 +            default:
   1.165 +                hptr = NULL;
   1.166 +                break;
   1.167 +        }
   1.168 +        if (!settings.highlight) {
   1.169 +            hptr = NULL;
   1.170 +        }
   1.171  
   1.172          inputfile_t *inputfile = readinput(settings.infilename);
   1.173          if (inputfile) {
   1.174 -            char *line;
   1.175 -            if (settings.highlight) {
   1.176 -                line = (char*) malloc(inputfile->maxlinewidth*64);
   1.177 -            } else {
   1.178 -                line = NULL;
   1.179 -            }
   1.180 -            fprintf(fout, "<pre>\n");
   1.181 -            int lnw = lnint(inputfile->count);
   1.182 -            for (int i = 0 ; i < inputfile->count ; i++) {
   1.183 -                if (settings.highlight) {
   1.184 -                    highlighter.parser(inputfile->lines[i], line, &highlighter);
   1.185 -                } else {
   1.186 -                    line = inputfile->lines[i];
   1.187 -                }
   1.188 -                fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s",
   1.189 -                    lnw, i+1, line);
   1.190 -            }
   1.191 -            if (settings.highlight) {
   1.192 -                free(line);
   1.193 -            }
   1.194 -            fprintf(fout, "</pre>\n");
   1.195 -
   1.196 -            freeinputfilebuffer(inputfile);
   1.197 -            
   1.198 -            if (copyfile(settings.footerfile, fout)) {
   1.199 -                perror("Error opening footer file");
   1.200 -                retcode = errno;
   1.201 -            }
   1.202 +            formatfile(
   1.203 +                    hptr,
   1.204 +                    inputfile,
   1.205 +                    (fmt_write_func)fwrite,
   1.206 +                    fout,
   1.207 +                    settings.showlinenumbers);
   1.208          } else {
   1.209              perror("Error opening input file");
   1.210 -            retcode = errno;
   1.211 +            retcode = -1;
   1.212 +        }
   1.213 +        
   1.214 +        if (copyfile(settings.footerfile, fout)) {
   1.215 +            perror("Error opening footer file");
   1.216 +            retcode = -1;
   1.217          }
   1.218          
   1.219          prog_end:        

mercurial