introduced parser

Wed, 12 Jun 2013 13:40:23 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 12 Jun 2013 13:40:23 +0200
changeset 4
323f674931fe
parent 3
b7a6d546bd1d
child 5
412f1896874b

introduced parser

src/c2html.c file | annotate | diff | comparison | revisions
src/c2html.h file | annotate | diff | comparison | revisions
     1.1 --- a/src/c2html.c	Fri May 24 13:57:51 2013 +0200
     1.2 +++ b/src/c2html.c	Wed Jun 12 13:40:23 2013 +0200
     1.3 @@ -32,13 +32,24 @@
     1.4  #include <string.h>
     1.5  #include <fcntl.h>
     1.6  #include <unistd.h>
     1.7 -#include "c2html.h"
     1.8 +#include <ctype.h>
     1.9 +
    1.10 +#define INPUTBUF_SIZE 2048
    1.11 +
    1.12 +
    1.13 +typedef struct {
    1.14 +  size_t count;
    1.15 +  size_t capacity;
    1.16 +  size_t maxlinewidth;
    1.17 +  char** lines;
    1.18 +} inputfile_t;
    1.19  
    1.20  inputfile_t *inputfilebuffer(size_t capacity) {
    1.21    inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t));
    1.22    inputfile->lines = (char**) malloc(capacity * sizeof(char*));
    1.23    inputfile->capacity = capacity;
    1.24    inputfile->count = 0;
    1.25 +  inputfile->maxlinewidth = 0;
    1.26    
    1.27    return inputfile;
    1.28  }
    1.29 @@ -52,6 +63,8 @@
    1.30      inputfile->lines = realloc(inputfile->lines, inputfile->capacity);
    1.31    }
    1.32    inputfile->lines[inputfile->count] = l;
    1.33 +  inputfile->maxlinewidth =
    1.34 +          width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth;
    1.35    inputfile->count++;
    1.36  }
    1.37  
    1.38 @@ -70,15 +83,14 @@
    1.39    
    1.40    inputfile_t *inputfile = inputfilebuffer(512);
    1.41    
    1.42 -  const size_t bufsize = 1024;
    1.43 -  char buf[bufsize];
    1.44 +  char buf[INPUTBUF_SIZE];
    1.45    ssize_t r;
    1.46    
    1.47 -  size_t maxlinewidth = 80;
    1.48 +  size_t maxlinewidth = 256;
    1.49    char *line = (char*) malloc(maxlinewidth);
    1.50    size_t col = 0;
    1.51    
    1.52 -  while ((r = read(fd, buf, bufsize)) > 0) {
    1.53 +  while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) {
    1.54      for (size_t i = 0 ; i < r ; i++) {
    1.55        if (col >= maxlinewidth-4) {
    1.56          maxlinewidth <<= 1;
    1.57 @@ -89,12 +101,6 @@
    1.58          line[col] = 0;
    1.59          addline(inputfile, line, col);        
    1.60          col = 0;
    1.61 -      } else if (buf[i] == '<') {
    1.62 -        line[col++] = '&'; line[col++] = 'l';
    1.63 -        line[col++] = 't'; line[col++] = ';';
    1.64 -      } else if (buf[i] == '>') {
    1.65 -        line[col++] = '&'; line[col++] = 'g';
    1.66 -        line[col++] = 't'; line[col++] = ';';
    1.67        } else {
    1.68          line[col++] = buf[i];
    1.69        }
    1.70 @@ -108,6 +114,29 @@
    1.71    return inputfile;
    1.72  }
    1.73  
    1.74 +void parseline(char *src, char *dest) {
    1.75 +  size_t sp = 0, dp = 0;
    1.76 +  /* indent */
    1.77 +  while (isspace(src[sp])) {
    1.78 +    dest[dp++] = src[sp++];
    1.79 +  }
    1.80 +  for (char c = src[sp] ; c ; c=src[++sp]) {
    1.81 +    switch (c) {
    1.82 +      case '<':
    1.83 +        memcpy(&(dest[dp]), "&lt;", 4);
    1.84 +        dp += 4;
    1.85 +        break;
    1.86 +      case '>':
    1.87 +        memcpy(&(dest[dp]), "&gt;", 4);
    1.88 +        dp += 4;
    1.89 +        break;
    1.90 +      default:
    1.91 +        dest[dp++] = c;
    1.92 +    }
    1.93 +  }
    1.94 +  dest[dp] = 0;
    1.95 +}
    1.96 +
    1.97  void printhelp() {
    1.98    printf("Formats source code using HTML.\n\nUsage:\n"
    1.99        "  c2html [FILE...]"
   1.100 @@ -116,7 +145,7 @@
   1.101    
   1.102  }
   1.103  
   1.104 -int lnw(size_t lnc) {
   1.105 +int lnint(size_t lnc) {
   1.106    int w = 1, p = 1;
   1.107    while ((p*=10) < lnc) w++;
   1.108    return w;
   1.109 @@ -132,10 +161,14 @@
   1.110      inputfile_t *inputfile = readinput(argv[1]);
   1.111      if (inputfile) {
   1.112        printf("<pre>\n");
   1.113 +      char *line = (char*) malloc(inputfile->maxlinewidth*64);
   1.114 +      int lnw = lnint(inputfile->count);
   1.115        for (int i = 0 ; i < inputfile->count ; i++) {
   1.116 +        parseline(inputfile->lines[i], line);
   1.117          printf("<span class=\"c2html-lineno\">%*d:</span> %s\n",
   1.118 -            lnw(inputfile->count), i, inputfile->lines[i]);
   1.119 +            lnw, i, line);
   1.120        }
   1.121 +      free(line);
   1.122        printf("</pre>\n");
   1.123        freeinputfilebuffer(inputfile);
   1.124      }
     2.1 --- a/src/c2html.h	Fri May 24 13:57:51 2013 +0200
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,55 +0,0 @@
     2.4 -/*
     2.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 - *
     2.7 - * Copyright 2013 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 -
    2.33 -
    2.34 -#ifndef C2HTML_H
    2.35 -#define	C2HTML_H
    2.36 -
    2.37 -#ifdef	__cplusplus
    2.38 -extern "C" {
    2.39 -#endif
    2.40 -
    2.41 -  typedef struct {
    2.42 -    size_t count;
    2.43 -    size_t capacity;
    2.44 -    char** lines;
    2.45 -  } inputfile_t;
    2.46 -  
    2.47 -inputfile_t *inputfilebuffer(size_t capacity);
    2.48 -void addline(inputfile_t *inputfile, char* line, size_t width);
    2.49 -void freeinputfilebuffer(inputfile_t *inputfile);
    2.50 -
    2.51 -
    2.52 -
    2.53 -#ifdef	__cplusplus
    2.54 -}
    2.55 -#endif
    2.56 -
    2.57 -#endif	/* C2HTML_H */
    2.58 -

mercurial