Sun, 19 Apr 2015 10:48:00 +0200
improved code structure and added option for disabling line numbers
21 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
4 | * Copyright 2015 Mike Becker. All rights reserved. |
21 | 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 | */ | |
29 | ||
30 | #include "ccodegen.h" | |
31 | #include <string.h> | |
32 | #include <ctype.h> | |
33 | ||
34 | const char* ckeywords[] = { | |
35 | "auto", "break", "case", "char", "const", "continue", "default", "do", | |
36 | "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", | |
37 | "long", "register", "return", "short", "signed", "sizeof", "static", | |
38 | "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", | |
39 | "while", NULL | |
40 | }; | |
41 | ||
42 | int isctype(char *word, size_t len) { | |
43 | return (word[len-2] == '_' && word[len-1] == 't'); | |
44 | } | |
45 | ||
46 | int iscdirective(char *word) { | |
47 | return (word[0] == '#'); | |
48 | } | |
49 | ||
50 | void cparseline(char *src, char *dest, highlighter_t *hltr) { | |
51 | size_t sp = 0, dp = 0; | |
52 | /* indent */ | |
53 | while (isspace(src[sp])) { | |
54 | dest[dp++] = src[sp++]; | |
55 | } | |
56 | ||
57 | memset(hltr->word, 0, WORDBUF_SIZE); | |
58 | size_t wp = 0, ifp = 0; | |
59 | int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0; | |
60 | int isescaping = 0; | |
61 | ||
62 | if (hltr->iscommentml) { | |
63 | iscomment = 1; | |
64 | memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); | |
65 | dp += 29; | |
66 | } | |
67 | ||
68 | for (char c = src[sp] ; c ; c=src[++sp]) { | |
69 | /* comments */ | |
70 | if (c == '/') { | |
71 | if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') { | |
72 | iscomment = 0; | |
73 | hltr->iscommentml = 0; | |
74 | memcpy(&(dest[dp]), "/</span>", 8); | |
75 | dp += 8; | |
76 | continue; | |
77 | } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { | |
78 | iscomment = 1; | |
79 | hltr->iscommentml = (src[sp+1] == '*'); | |
80 | memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); | |
81 | dp += 29; | |
82 | } | |
83 | } | |
84 | ||
85 | if (iscomment) { | |
86 | if (c == '\n') { | |
87 | memcpy(&(dest[dp]), "</span>", 7); | |
88 | dp += 7; | |
89 | } | |
90 | dp = writeescapedchar(dest, dp, c); | |
91 | } else if (isinclude) { | |
92 | if (c == '<') { | |
93 | memcpy(&(dest[dp]), "<span class=\"c2html-stdinclude\">", 32); | |
94 | dp += 32; | |
95 | dp = writeescapedchar(dest, dp, c); | |
96 | } else if (c == '\"') { | |
97 | if (parseinclude) { | |
98 | dest[dp++] = '\"'; | |
99 | dest[dp++] = '>'; | |
100 | memcpy(&(dest[dp]), hltr->includefile, ifp); | |
101 | dp += ifp; | |
102 | ||
103 | dp = writeescapedchar(dest, dp, c); | |
104 | memcpy(&(dest[dp]), "</a>", 4); | |
105 | dp += 4; | |
106 | parseinclude = 0; | |
107 | } else { | |
108 | memcpy(&(dest[dp]), | |
109 | "<a class=\"c2html-userinclude\" href=", 35); | |
110 | dp += 35; | |
111 | dp = writeescapedchar(dest, dp, c); | |
112 | ifp = 0; | |
113 | hltr->includefile[ifp++] = '\"'; | |
114 | parseinclude = 1; | |
115 | } | |
116 | } else if (c == '>') { | |
117 | dp = writeescapedchar(dest, dp, c); | |
118 | memcpy(&(dest[dp]), "</span>", 7); | |
119 | dp += 7; | |
120 | } else { | |
121 | if (parseinclude) { | |
122 | hltr->includefile[ifp++] = c; | |
123 | } | |
124 | dp = writeescapedchar(dest, dp, c); | |
125 | } | |
126 | } else { | |
127 | /* strings */ | |
128 | if (!isescaping && (c == '\'' || c == '\"')) { | |
129 | isstring ^= 1; | |
130 | if (isstring) { | |
131 | memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28); | |
132 | dp += 28; | |
133 | dp = writeescapedchar(dest, dp, c); | |
134 | } else { | |
135 | dp = writeescapedchar(dest, dp, c); | |
136 | memcpy(&(dest[dp]), "</span>", 7); | |
137 | dp += 7; | |
138 | } | |
139 | } else { | |
140 | if (isstring) { | |
141 | dp = writeescapedchar(dest, dp, c); | |
142 | } else if (!iswordcharacter(c)) { | |
143 | /* interpret word int_t */ | |
144 | if (wp > 0 && wp < WORDBUF_SIZE) { | |
145 | int closespan = 1; | |
146 | if (iskeyword(hltr->word, hltr->keywords)) { | |
147 | memcpy(&(dest[dp]), | |
148 | "<span class=\"c2html-keyword\">", 29); | |
149 | dp += 29; | |
150 | } else if (hltr->istype(hltr->word, wp)) { | |
151 | memcpy(&(dest[dp]), | |
152 | "<span class=\"c2html-type\">", 26); | |
153 | dp += 26; | |
154 | } else if (hltr->isdirective(hltr->word)) { | |
155 | isinclude = !strncmp( | |
156 | "#include", hltr->word, WORDBUF_SIZE); | |
157 | memcpy(&(dest[dp]), | |
158 | "<span class=\"c2html-directive\">", 31); | |
159 | dp += 31; | |
160 | } else if (iscapsonly(hltr->word, wp)) { | |
161 | memcpy(&(dest[dp]), | |
162 | "<span class=\"c2html-macroconst\">", 32); | |
163 | dp += 32; | |
164 | } else { | |
165 | closespan = 0; | |
166 | } | |
167 | for (int i = 0 ; i < wp ; i++) { | |
168 | dp = writeescapedchar(dest, dp, hltr->word[i]); | |
169 | } | |
170 | if (closespan) { | |
171 | memcpy(&(dest[dp]), "</span>", 7); | |
172 | dp += 7; | |
173 | } | |
174 | } | |
175 | memset(hltr->word, 0, WORDBUF_SIZE); | |
176 | wp = 0; | |
177 | dp = writeescapedchar(dest, dp, c); | |
178 | } else { | |
179 | /* read word */ | |
180 | if (wp < WORDBUF_SIZE) { | |
181 | hltr->word[wp++] = c; | |
182 | } else if (wp == WORDBUF_SIZE) { | |
183 | for (int i = 0 ; i < WORDBUF_SIZE ; i++) { | |
184 | dp = writeescapedchar(dest, dp, hltr->word[i]); | |
185 | } | |
186 | wp++; | |
187 | dp = writeescapedchar(dest, dp, c); | |
188 | } else { | |
189 | dp = writeescapedchar(dest, dp, c); | |
190 | } | |
191 | } | |
192 | } | |
193 | ||
194 | isescaping = !isescaping & (c == '\\'); | |
195 | } | |
196 | } | |
197 | dest[dp] = 0; | |
198 | } |