# HG changeset patch # User Mike Becker # Date 1371037223 -7200 # Node ID 323f674931feedd85169036d13dbf2005d56a4ea # Parent b7a6d546bd1d13697ccd3e8791dafa3346232238 introduced parser diff -r b7a6d546bd1d -r 323f674931fe src/c2html.c --- a/src/c2html.c Fri May 24 13:57:51 2013 +0200 +++ b/src/c2html.c Wed Jun 12 13:40:23 2013 +0200 @@ -32,13 +32,24 @@ #include #include #include -#include "c2html.h" +#include + +#define INPUTBUF_SIZE 2048 + + +typedef struct { + size_t count; + size_t capacity; + size_t maxlinewidth; + char** lines; +} inputfile_t; inputfile_t *inputfilebuffer(size_t capacity) { inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t)); inputfile->lines = (char**) malloc(capacity * sizeof(char*)); inputfile->capacity = capacity; inputfile->count = 0; + inputfile->maxlinewidth = 0; return inputfile; } @@ -52,6 +63,8 @@ inputfile->lines = realloc(inputfile->lines, inputfile->capacity); } inputfile->lines[inputfile->count] = l; + inputfile->maxlinewidth = + width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth; inputfile->count++; } @@ -70,15 +83,14 @@ inputfile_t *inputfile = inputfilebuffer(512); - const size_t bufsize = 1024; - char buf[bufsize]; + char buf[INPUTBUF_SIZE]; ssize_t r; - size_t maxlinewidth = 80; + size_t maxlinewidth = 256; char *line = (char*) malloc(maxlinewidth); size_t col = 0; - while ((r = read(fd, buf, bufsize)) > 0) { + while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) { for (size_t i = 0 ; i < r ; i++) { if (col >= maxlinewidth-4) { maxlinewidth <<= 1; @@ -89,12 +101,6 @@ line[col] = 0; addline(inputfile, line, col); col = 0; - } else if (buf[i] == '<') { - line[col++] = '&'; line[col++] = 'l'; - line[col++] = 't'; line[col++] = ';'; - } else if (buf[i] == '>') { - line[col++] = '&'; line[col++] = 'g'; - line[col++] = 't'; line[col++] = ';'; } else { line[col++] = buf[i]; } @@ -108,6 +114,29 @@ return inputfile; } +void parseline(char *src, char *dest) { + size_t sp = 0, dp = 0; + /* indent */ + while (isspace(src[sp])) { + dest[dp++] = src[sp++]; + } + for (char c = src[sp] ; c ; c=src[++sp]) { + switch (c) { + case '<': + memcpy(&(dest[dp]), "<", 4); + dp += 4; + break; + case '>': + memcpy(&(dest[dp]), ">", 4); + dp += 4; + break; + default: + dest[dp++] = c; + } + } + dest[dp] = 0; +} + void printhelp() { printf("Formats source code using HTML.\n\nUsage:\n" " c2html [FILE...]" @@ -116,7 +145,7 @@ } -int lnw(size_t lnc) { +int lnint(size_t lnc) { int w = 1, p = 1; while ((p*=10) < lnc) w++; return w; @@ -132,10 +161,14 @@ inputfile_t *inputfile = readinput(argv[1]); if (inputfile) { printf("
\n");
+      char *line = (char*) malloc(inputfile->maxlinewidth*64);
+      int lnw = lnint(inputfile->count);
       for (int i = 0 ; i < inputfile->count ; i++) {
+        parseline(inputfile->lines[i], line);
         printf("%*d: %s\n",
-            lnw(inputfile->count), i, inputfile->lines[i]);
+            lnw, i, line);
       }
+      free(line);
       printf("
\n"); freeinputfilebuffer(inputfile); } diff -r b7a6d546bd1d -r 323f674931fe src/c2html.h --- a/src/c2html.h Fri May 24 13:57:51 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Mike Becker. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - */ - - -#ifndef C2HTML_H -#define C2HTML_H - -#ifdef __cplusplus -extern "C" { -#endif - - typedef struct { - size_t count; - size_t capacity; - char** lines; - } inputfile_t; - -inputfile_t *inputfilebuffer(size_t capacity); -void addline(inputfile_t *inputfile, char* line, size_t width); -void freeinputfilebuffer(inputfile_t *inputfile); - - - -#ifdef __cplusplus -} -#endif - -#endif /* C2HTML_H */ -