src/c2html.c

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 2
3bced1fe9193
child 5
412f1896874b
permissions
-rw-r--r--

introduced parser

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2013 Mike Becker. All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  *
    28  */
    30 #include <stdio.h>
    31 #include <stdlib.h>
    32 #include <string.h>
    33 #include <fcntl.h>
    34 #include <unistd.h>
    35 #include <ctype.h>
    37 #define INPUTBUF_SIZE 2048
    40 typedef struct {
    41   size_t count;
    42   size_t capacity;
    43   size_t maxlinewidth;
    44   char** lines;
    45 } inputfile_t;
    47 inputfile_t *inputfilebuffer(size_t capacity) {
    48   inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t));
    49   inputfile->lines = (char**) malloc(capacity * sizeof(char*));
    50   inputfile->capacity = capacity;
    51   inputfile->count = 0;
    52   inputfile->maxlinewidth = 0;
    54   return inputfile;
    55 }
    57 void addline(inputfile_t *inputfile, char* line, size_t width) {
    58   char *l = (char*) malloc(width+1);
    59   memcpy(l, line, width);
    60   l[width] = 0;
    61   if (inputfile->count >= inputfile->capacity) {
    62     inputfile->capacity <<= 1;
    63     inputfile->lines = realloc(inputfile->lines, inputfile->capacity);
    64   }
    65   inputfile->lines[inputfile->count] = l;
    66   inputfile->maxlinewidth =
    67           width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth;
    68   inputfile->count++;
    69 }
    71 void freeinputfilebuffer(inputfile_t *inputfile) {
    72   for (int i = 0 ; i < inputfile->count ; i++) {
    73     free(inputfile->lines[i]);
    74   }
    75   free(inputfile->lines);
    76   free(inputfile);
    77 }
    79 inputfile_t *readinput(char *filename) {
    81   int fd = open(filename, O_RDONLY);
    82   if (fd == -1) return NULL;
    84   inputfile_t *inputfile = inputfilebuffer(512);
    86   char buf[INPUTBUF_SIZE];
    87   ssize_t r;
    89   size_t maxlinewidth = 256;
    90   char *line = (char*) malloc(maxlinewidth);
    91   size_t col = 0;
    93   while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) {
    94     for (size_t i = 0 ; i < r ; i++) {
    95       if (col >= maxlinewidth-4) {
    96         maxlinewidth <<= 1;
    97         line = realloc(line, maxlinewidth);
    98       }
   100       if (buf[i] == '\n') {
   101         line[col] = 0;
   102         addline(inputfile, line, col);        
   103         col = 0;
   104       } else {
   105         line[col++] = buf[i];
   106       }
   107     }
   108   }
   110   free(line);
   112   close(fd);
   114   return inputfile;
   115 }
   117 void parseline(char *src, char *dest) {
   118   size_t sp = 0, dp = 0;
   119   /* indent */
   120   while (isspace(src[sp])) {
   121     dest[dp++] = src[sp++];
   122   }
   123   for (char c = src[sp] ; c ; c=src[++sp]) {
   124     switch (c) {
   125       case '<':
   126         memcpy(&(dest[dp]), "&lt;", 4);
   127         dp += 4;
   128         break;
   129       case '>':
   130         memcpy(&(dest[dp]), "&gt;", 4);
   131         dp += 4;
   132         break;
   133       default:
   134         dest[dp++] = c;
   135     }
   136   }
   137   dest[dp] = 0;
   138 }
   140 void printhelp() {
   141   printf("Formats source code using HTML.\n\nUsage:\n"
   142       "  c2html [FILE...]"
   143       "\n");
   146 }
   148 int lnint(size_t lnc) {
   149   int w = 1, p = 1;
   150   while ((p*=10) < lnc) w++;
   151   return w;
   152 }
   154 int main(int argc, char** argv) {
   156   if (argc == 1) {
   157     printhelp();
   158     return 0;
   159   } else {
   161     inputfile_t *inputfile = readinput(argv[1]);
   162     if (inputfile) {
   163       printf("<pre>\n");
   164       char *line = (char*) malloc(inputfile->maxlinewidth*64);
   165       int lnw = lnint(inputfile->count);
   166       for (int i = 0 ; i < inputfile->count ; i++) {
   167         parseline(inputfile->lines[i], line);
   168         printf("<span class=\"c2html-lineno\">%*d:</span> %s\n",
   169             lnw, i, line);
   170       }
   171       free(line);
   172       printf("</pre>\n");
   173       freeinputfilebuffer(inputfile);
   174     }
   176     return 0;
   177   }
   178 }

mercurial