src/c2html.c

changeset 1
12c482ea4fc4
parent 0
e78e463e1aa9
child 2
3bced1fe9193
     1.1 --- a/src/c2html.c	Fri May 24 11:04:56 2013 +0200
     1.2 +++ b/src/c2html.c	Fri May 24 13:35:06 2013 +0200
     1.3 @@ -1,4 +1,146 @@
     1.4 -int main(int argc, char** argv) {
     1.5 -  return 0;
     1.6 +/*
     1.7 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.8 + *
     1.9 + * Copyright 2013 Mike Becker. All rights reserved.
    1.10 + *
    1.11 + * Redistribution and use in source and binary forms, with or without
    1.12 + * modification, are permitted provided that the following conditions are met:
    1.13 + *
    1.14 + *   1. Redistributions of source code must retain the above copyright
    1.15 + *      notice, this list of conditions and the following disclaimer.
    1.16 + *
    1.17 + *   2. Redistributions in binary form must reproduce the above copyright
    1.18 + *      notice, this list of conditions and the following disclaimer in the
    1.19 + *      documentation and/or other materials provided with the distribution.
    1.20 + *
    1.21 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.22 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.23 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.24 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.25 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.26 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.27 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.28 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.29 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.30 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.31 + * POSSIBILITY OF SUCH DAMAGE.
    1.32 + *
    1.33 + */
    1.34 +
    1.35 +#include <stdio.h>
    1.36 +#include <stdlib.h>
    1.37 +#include <string.h>
    1.38 +#include <fcntl.h>
    1.39 +#include <unistd.h>
    1.40 +#include "c2html.h"
    1.41 +
    1.42 +inputfile_t *inputfilebuffer(size_t capacity) {
    1.43 +  inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t));
    1.44 +  inputfile->lines = (char**) malloc(capacity * sizeof(char*));
    1.45 +  inputfile->capacity = capacity;
    1.46 +  inputfile->count = 0;
    1.47 +  
    1.48 +  return inputfile;
    1.49  }
    1.50  
    1.51 +void addline(inputfile_t *inputfile, char* line, size_t width) {
    1.52 +  char *l = (char*) malloc(width+1);
    1.53 +  memcpy(l, line, width);
    1.54 +  l[width] = 0;
    1.55 +  if (inputfile->count >= inputfile->capacity) {
    1.56 +    inputfile->capacity <<= 1;
    1.57 +    inputfile->lines = realloc(inputfile->lines, inputfile->capacity);
    1.58 +  }
    1.59 +  inputfile->lines[inputfile->count] = l;
    1.60 +  inputfile->count++;
    1.61 +}
    1.62 +
    1.63 +void freeinputfilebuffer(inputfile_t *inputfile) {
    1.64 +  for (int i = 0 ; i < inputfile->count ; i++) {
    1.65 +    free(inputfile->lines[i]);
    1.66 +  }
    1.67 +  free(inputfile->lines);
    1.68 +  free(inputfile);
    1.69 +}
    1.70 +
    1.71 +inputfile_t *readinput(char *filename) {
    1.72 +
    1.73 +  int fd = open(filename, O_RDONLY);
    1.74 +  if (fd == -1) return NULL;
    1.75 +  
    1.76 +  inputfile_t *inputfile = inputfilebuffer(512);
    1.77 +  
    1.78 +  const size_t bufsize = 1024;
    1.79 +  char buf[bufsize];
    1.80 +  ssize_t r;
    1.81 +  
    1.82 +  size_t maxlinewidth = 80;
    1.83 +  char *line = (char*) malloc(maxlinewidth);
    1.84 +  size_t col = 0;
    1.85 +  
    1.86 +  while ((r = read(fd, buf, bufsize)) > 0) {
    1.87 +    for (size_t i = 0 ; i < r ; i++) {
    1.88 +      if (col >= maxlinewidth-4) {
    1.89 +        maxlinewidth <<= 1;
    1.90 +        line = realloc(line, maxlinewidth);
    1.91 +      }
    1.92 +
    1.93 +      if (buf[i] == '\n') {
    1.94 +        line[col] = 0;
    1.95 +        addline(inputfile, line, col);        
    1.96 +        col = 0;
    1.97 +      } else if (buf[i] == '<') {
    1.98 +        line[col++] = '&'; line[col++] = 'l';
    1.99 +        line[col++] = 't'; line[col++] = ';';
   1.100 +      } else if (buf[i] == '>') {
   1.101 +        line[col++] = '&'; line[col++] = 'g';
   1.102 +        line[col++] = 't'; line[col++] = ';';
   1.103 +      } else {
   1.104 +        line[col++] = buf[i];
   1.105 +      }
   1.106 +    }
   1.107 +  }
   1.108 +  
   1.109 +  free(line);
   1.110 +  
   1.111 +  close(fd);
   1.112 +  
   1.113 +  return inputfile;
   1.114 +}
   1.115 +
   1.116 +void printhelp() {
   1.117 +  printf("Formats source code using HTML.\n\nUsage:\n"
   1.118 +      "  c2html [FILE...]"
   1.119 +      "\n");
   1.120 +  
   1.121 +  
   1.122 +}
   1.123 +
   1.124 +int lnw(size_t lnc) {
   1.125 +  int w = 1, p = 1;
   1.126 +  while ((p*=10) < lnc) w++;
   1.127 +  return w;
   1.128 +}
   1.129 +
   1.130 +int main(int argc, char** argv) {
   1.131 +  
   1.132 +  if (argc == 1) {
   1.133 +    printhelp();
   1.134 +    return 0;
   1.135 +  } else {
   1.136 +    
   1.137 +    inputfile_t *inputfile = readinput(argv[1]);
   1.138 +    if (inputfile) {
   1.139 +      printf("<pre>\n");
   1.140 +      for (int i = 0 ; i < inputfile->count ; i++) {
   1.141 +        printf("%*d: %s\n", lnw(inputfile->count),
   1.142 +            i, inputfile->lines[i]);
   1.143 +      }
   1.144 +      printf("</pre>\n");
   1.145 +      freeinputfilebuffer(inputfile);
   1.146 +    }
   1.147 +  
   1.148 +    return 0;
   1.149 +  }
   1.150 +}
   1.151 +

mercurial