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