src/c2html.c

Fri, 24 May 2013 13:53:52 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 24 May 2013 13:53:52 +0200
changeset 2
3bced1fe9193
parent 1
12c482ea4fc4
child 4
323f674931fe
permissions
-rw-r--r--

extra style for line numbers

     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 "c2html.h"
    37 inputfile_t *inputfilebuffer(size_t capacity) {
    38   inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t));
    39   inputfile->lines = (char**) malloc(capacity * sizeof(char*));
    40   inputfile->capacity = capacity;
    41   inputfile->count = 0;
    43   return inputfile;
    44 }
    46 void addline(inputfile_t *inputfile, char* line, size_t width) {
    47   char *l = (char*) malloc(width+1);
    48   memcpy(l, line, width);
    49   l[width] = 0;
    50   if (inputfile->count >= inputfile->capacity) {
    51     inputfile->capacity <<= 1;
    52     inputfile->lines = realloc(inputfile->lines, inputfile->capacity);
    53   }
    54   inputfile->lines[inputfile->count] = l;
    55   inputfile->count++;
    56 }
    58 void freeinputfilebuffer(inputfile_t *inputfile) {
    59   for (int i = 0 ; i < inputfile->count ; i++) {
    60     free(inputfile->lines[i]);
    61   }
    62   free(inputfile->lines);
    63   free(inputfile);
    64 }
    66 inputfile_t *readinput(char *filename) {
    68   int fd = open(filename, O_RDONLY);
    69   if (fd == -1) return NULL;
    71   inputfile_t *inputfile = inputfilebuffer(512);
    73   const size_t bufsize = 1024;
    74   char buf[bufsize];
    75   ssize_t r;
    77   size_t maxlinewidth = 80;
    78   char *line = (char*) malloc(maxlinewidth);
    79   size_t col = 0;
    81   while ((r = read(fd, buf, bufsize)) > 0) {
    82     for (size_t i = 0 ; i < r ; i++) {
    83       if (col >= maxlinewidth-4) {
    84         maxlinewidth <<= 1;
    85         line = realloc(line, maxlinewidth);
    86       }
    88       if (buf[i] == '\n') {
    89         line[col] = 0;
    90         addline(inputfile, line, col);        
    91         col = 0;
    92       } else if (buf[i] == '<') {
    93         line[col++] = '&'; line[col++] = 'l';
    94         line[col++] = 't'; line[col++] = ';';
    95       } else if (buf[i] == '>') {
    96         line[col++] = '&'; line[col++] = 'g';
    97         line[col++] = 't'; line[col++] = ';';
    98       } else {
    99         line[col++] = buf[i];
   100       }
   101     }
   102   }
   104   free(line);
   106   close(fd);
   108   return inputfile;
   109 }
   111 void printhelp() {
   112   printf("Formats source code using HTML.\n\nUsage:\n"
   113       "  c2html [FILE...]"
   114       "\n");
   117 }
   119 int lnw(size_t lnc) {
   120   int w = 1, p = 1;
   121   while ((p*=10) < lnc) w++;
   122   return w;
   123 }
   125 int main(int argc, char** argv) {
   127   if (argc == 1) {
   128     printhelp();
   129     return 0;
   130   } else {
   132     inputfile_t *inputfile = readinput(argv[1]);
   133     if (inputfile) {
   134       printf("<pre>\n");
   135       for (int i = 0 ; i < inputfile->count ; i++) {
   136         printf("<span class=\"c2html-lineno\">%*d:</span> %s\n",
   137             lnw(inputfile->count), i, inputfile->lines[i]);
   138       }
   139       printf("</pre>\n");
   140       freeinputfilebuffer(inputfile);
   141     }
   143     return 0;
   144   }
   145 }

mercurial