first prototype: creates unformatted output with line numbers

Fri, 24 May 2013 13:35:06 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 24 May 2013 13:35:06 +0200
changeset 1
12c482ea4fc4
parent 0
e78e463e1aa9
child 2
3bced1fe9193

first prototype: creates unformatted output with line numbers

LICENSE file | annotate | diff | comparison | revisions
Makefile file | annotate | diff | comparison | revisions
src/c2html.c file | annotate | diff | comparison | revisions
src/c2html.h file | annotate | diff | comparison | revisions
test/footer.htm file | annotate | diff | comparison | revisions
test/header.htm file | annotate | diff | comparison | revisions
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/LICENSE	Fri May 24 13:35:06 2013 +0200
     1.3 @@ -0,0 +1,23 @@
     1.4 +Copyright 2013 Mike Becker. All rights reserved.
     1.5 +
     1.6 +Redistribution and use in source and binary forms, with or without
     1.7 +modification, are permitted provided that the following conditions are met:
     1.8 +
     1.9 +  1. Redistributions of source code must retain the above copyright
    1.10 +     notice, this list of conditions and the following disclaimer.
    1.11 +
    1.12 +  2. Redistributions in binary form must reproduce the above copyright
    1.13 +     notice, this list of conditions and the following disclaimer in the
    1.14 +     documentation and/or other materials provided with the distribution.
    1.15 +
    1.16 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.17 +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.18 +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.19 +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.20 +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.21 +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.22 +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.23 +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.24 +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.25 +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.26 +POSSIBILITY OF SUCH DAMAGE.
     2.1 --- a/Makefile	Fri May 24 11:04:56 2013 +0200
     2.2 +++ b/Makefile	Fri May 24 13:35:06 2013 +0200
     2.3 @@ -36,6 +36,9 @@
     2.4  build:
     2.5  	$(MKDIR) build
     2.6  	
     2.7 +test: compile
     2.8 +	./build/$(BIN) src/c2html.c > build/body.htm
     2.9 +	cat test/header.htm build/body.htm test/footer.htm > build/code.htm
    2.10 +	
    2.11  clean:
    2.12  	$(RM) -f -R build
    2.13 -
     3.1 --- a/src/c2html.c	Fri May 24 11:04:56 2013 +0200
     3.2 +++ b/src/c2html.c	Fri May 24 13:35:06 2013 +0200
     3.3 @@ -1,4 +1,146 @@
     3.4 -int main(int argc, char** argv) {
     3.5 -  return 0;
     3.6 +/*
     3.7 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.8 + *
     3.9 + * Copyright 2013 Mike Becker. All rights reserved.
    3.10 + *
    3.11 + * Redistribution and use in source and binary forms, with or without
    3.12 + * modification, are permitted provided that the following conditions are met:
    3.13 + *
    3.14 + *   1. Redistributions of source code must retain the above copyright
    3.15 + *      notice, this list of conditions and the following disclaimer.
    3.16 + *
    3.17 + *   2. Redistributions in binary form must reproduce the above copyright
    3.18 + *      notice, this list of conditions and the following disclaimer in the
    3.19 + *      documentation and/or other materials provided with the distribution.
    3.20 + *
    3.21 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    3.22 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    3.23 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    3.24 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    3.25 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    3.26 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    3.27 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    3.28 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    3.29 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    3.30 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    3.31 + * POSSIBILITY OF SUCH DAMAGE.
    3.32 + *
    3.33 + */
    3.34 +
    3.35 +#include <stdio.h>
    3.36 +#include <stdlib.h>
    3.37 +#include <string.h>
    3.38 +#include <fcntl.h>
    3.39 +#include <unistd.h>
    3.40 +#include "c2html.h"
    3.41 +
    3.42 +inputfile_t *inputfilebuffer(size_t capacity) {
    3.43 +  inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t));
    3.44 +  inputfile->lines = (char**) malloc(capacity * sizeof(char*));
    3.45 +  inputfile->capacity = capacity;
    3.46 +  inputfile->count = 0;
    3.47 +  
    3.48 +  return inputfile;
    3.49  }
    3.50  
    3.51 +void addline(inputfile_t *inputfile, char* line, size_t width) {
    3.52 +  char *l = (char*) malloc(width+1);
    3.53 +  memcpy(l, line, width);
    3.54 +  l[width] = 0;
    3.55 +  if (inputfile->count >= inputfile->capacity) {
    3.56 +    inputfile->capacity <<= 1;
    3.57 +    inputfile->lines = realloc(inputfile->lines, inputfile->capacity);
    3.58 +  }
    3.59 +  inputfile->lines[inputfile->count] = l;
    3.60 +  inputfile->count++;
    3.61 +}
    3.62 +
    3.63 +void freeinputfilebuffer(inputfile_t *inputfile) {
    3.64 +  for (int i = 0 ; i < inputfile->count ; i++) {
    3.65 +    free(inputfile->lines[i]);
    3.66 +  }
    3.67 +  free(inputfile->lines);
    3.68 +  free(inputfile);
    3.69 +}
    3.70 +
    3.71 +inputfile_t *readinput(char *filename) {
    3.72 +
    3.73 +  int fd = open(filename, O_RDONLY);
    3.74 +  if (fd == -1) return NULL;
    3.75 +  
    3.76 +  inputfile_t *inputfile = inputfilebuffer(512);
    3.77 +  
    3.78 +  const size_t bufsize = 1024;
    3.79 +  char buf[bufsize];
    3.80 +  ssize_t r;
    3.81 +  
    3.82 +  size_t maxlinewidth = 80;
    3.83 +  char *line = (char*) malloc(maxlinewidth);
    3.84 +  size_t col = 0;
    3.85 +  
    3.86 +  while ((r = read(fd, buf, bufsize)) > 0) {
    3.87 +    for (size_t i = 0 ; i < r ; i++) {
    3.88 +      if (col >= maxlinewidth-4) {
    3.89 +        maxlinewidth <<= 1;
    3.90 +        line = realloc(line, maxlinewidth);
    3.91 +      }
    3.92 +
    3.93 +      if (buf[i] == '\n') {
    3.94 +        line[col] = 0;
    3.95 +        addline(inputfile, line, col);        
    3.96 +        col = 0;
    3.97 +      } else if (buf[i] == '<') {
    3.98 +        line[col++] = '&'; line[col++] = 'l';
    3.99 +        line[col++] = 't'; line[col++] = ';';
   3.100 +      } else if (buf[i] == '>') {
   3.101 +        line[col++] = '&'; line[col++] = 'g';
   3.102 +        line[col++] = 't'; line[col++] = ';';
   3.103 +      } else {
   3.104 +        line[col++] = buf[i];
   3.105 +      }
   3.106 +    }
   3.107 +  }
   3.108 +  
   3.109 +  free(line);
   3.110 +  
   3.111 +  close(fd);
   3.112 +  
   3.113 +  return inputfile;
   3.114 +}
   3.115 +
   3.116 +void printhelp() {
   3.117 +  printf("Formats source code using HTML.\n\nUsage:\n"
   3.118 +      "  c2html [FILE...]"
   3.119 +      "\n");
   3.120 +  
   3.121 +  
   3.122 +}
   3.123 +
   3.124 +int lnw(size_t lnc) {
   3.125 +  int w = 1, p = 1;
   3.126 +  while ((p*=10) < lnc) w++;
   3.127 +  return w;
   3.128 +}
   3.129 +
   3.130 +int main(int argc, char** argv) {
   3.131 +  
   3.132 +  if (argc == 1) {
   3.133 +    printhelp();
   3.134 +    return 0;
   3.135 +  } else {
   3.136 +    
   3.137 +    inputfile_t *inputfile = readinput(argv[1]);
   3.138 +    if (inputfile) {
   3.139 +      printf("<pre>\n");
   3.140 +      for (int i = 0 ; i < inputfile->count ; i++) {
   3.141 +        printf("%*d: %s\n", lnw(inputfile->count),
   3.142 +            i, inputfile->lines[i]);
   3.143 +      }
   3.144 +      printf("</pre>\n");
   3.145 +      freeinputfilebuffer(inputfile);
   3.146 +    }
   3.147 +  
   3.148 +    return 0;
   3.149 +  }
   3.150 +}
   3.151 +
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/c2html.h	Fri May 24 13:35:06 2013 +0200
     4.3 @@ -0,0 +1,55 @@
     4.4 +/*
     4.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     4.6 + *
     4.7 + * Copyright 2013 Mike Becker. All rights reserved.
     4.8 + *
     4.9 + * Redistribution and use in source and binary forms, with or without
    4.10 + * modification, are permitted provided that the following conditions are met:
    4.11 + *
    4.12 + *   1. Redistributions of source code must retain the above copyright
    4.13 + *      notice, this list of conditions and the following disclaimer.
    4.14 + *
    4.15 + *   2. Redistributions in binary form must reproduce the above copyright
    4.16 + *      notice, this list of conditions and the following disclaimer in the
    4.17 + *      documentation and/or other materials provided with the distribution.
    4.18 + *
    4.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    4.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    4.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    4.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    4.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    4.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    4.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    4.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    4.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    4.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    4.29 + * POSSIBILITY OF SUCH DAMAGE.
    4.30 + *
    4.31 + */
    4.32 +
    4.33 +
    4.34 +#ifndef C2HTML_H
    4.35 +#define	C2HTML_H
    4.36 +
    4.37 +#ifdef	__cplusplus
    4.38 +extern "C" {
    4.39 +#endif
    4.40 +
    4.41 +  typedef struct {
    4.42 +    size_t count;
    4.43 +    size_t capacity;
    4.44 +    char** lines;
    4.45 +  } inputfile_t;
    4.46 +  
    4.47 +inputfile_t *inputfilebuffer(size_t capacity);
    4.48 +void addline(inputfile_t *inputfile, char* line, size_t width);
    4.49 +void freeinputfilebuffer(inputfile_t *inputfile);
    4.50 +
    4.51 +
    4.52 +
    4.53 +#ifdef	__cplusplus
    4.54 +}
    4.55 +#endif
    4.56 +
    4.57 +#endif	/* C2HTML_H */
    4.58 +
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test/footer.htm	Fri May 24 13:35:06 2013 +0200
     5.3 @@ -0,0 +1,3 @@
     5.4 +  </body>
     5.5 +</html>
     5.6 +
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/test/header.htm	Fri May 24 13:35:06 2013 +0200
     6.3 @@ -0,0 +1,7 @@
     6.4 +<!DOCTYPE html>
     6.5 +<html>
     6.6 +  <head>
     6.7 +    <title>c2html</title>
     6.8 +  </head>
     6.9 +  <body>
    6.10 +

mercurial