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

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

mercurial