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

mercurial