src/c2html.c

changeset 9
6b1fba10c4cb
parent 8
417cd3b29f97
child 10
925172e535a9
equal deleted inserted replaced
8:417cd3b29f97 9:6b1fba10c4cb
147 } 147 }
148 } 148 }
149 return 0; 149 return 0;
150 } 150 }
151 151
152 int iscapsonly(char *word, size_t wp) {
153 for (size_t i = 0 ; i < wp ; i++) {
154 if (!isupper(word[i]) && word[i] != '_') {
155 return 0;
156 }
157 }
158 return 1;
159 }
160
152 void parseline(char *src, char *dest) { 161 void parseline(char *src, char *dest) {
153 size_t sp = 0, dp = 0; 162 size_t sp = 0, dp = 0;
154 /* indent */ 163 /* indent */
155 while (isspace(src[sp])) { 164 while (isspace(src[sp])) {
156 dest[dp++] = src[sp++]; 165 dest[dp++] = src[sp++];
165 if (iscommentml) { 174 if (iscommentml) {
166 iscomment = 1; 175 iscomment = 1;
167 memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); 176 memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
168 dp += 29; 177 dp += 29;
169 } 178 }
170 // a fucking /* comment */ 179
171 /* again // / */
172 for (char c = src[sp] ; c ; c=src[++sp]) { 180 for (char c = src[sp] ; c ; c=src[++sp]) {
173 /* comments */ 181 /* comments */
174 if (c == '/') { 182 if (c == '/') {
175 if (iscommentml && sp > 0 && src[sp-1] == '*') { 183 if (iscommentml && sp > 0 && src[sp-1] == '*') {
176 iscomment = 0; 184 iscomment = 0;
219 memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26); 227 memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26);
220 dp += 26; 228 dp += 26;
221 } else if (isdirective(word)) { 229 } else if (isdirective(word)) {
222 memcpy(&(dest[dp]), "<span class=\"c2html-directive\">", 31); 230 memcpy(&(dest[dp]), "<span class=\"c2html-directive\">", 31);
223 dp += 31; 231 dp += 31;
232 } else if (iscapsonly(word, wp)) {
233 memcpy(&(dest[dp]), "<span class=\"c2html-macroconst\">", 32);
234 dp += 32;
224 } else { 235 } else {
225 closespan = 0; 236 closespan = 0;
226 } 237 }
227 for (int i = 0 ; i < wp ; i++) { 238 for (int i = 0 ; i < wp ; i++) {
228 dp = writeescapedchar(dest, dp, word[i]); 239 dp = writeescapedchar(dest, dp, word[i]);
229 } 240 }
230 if (closespan) { 241 if (closespan) {
231 memcpy(&(dest[dp]), "</span>", 7); 242 memcpy(&(dest[dp]), "</span>", 7);
232 dp += 7; 243 dp += 7;
233 } 244 }
234 memset(word, 0, WORDBUF_SIZE);
235 wp = 0;
236 } 245 }
246 memset(word, 0, WORDBUF_SIZE);
247 wp = 0;
237 dp = writeescapedchar(dest, dp, c); 248 dp = writeescapedchar(dest, dp, c);
238 } else { 249 } else {
239 /* read word */ 250 /* read word */
240 if (wp < WORDBUF_SIZE) { 251 if (wp < WORDBUF_SIZE) {
241 word[wp++] = c; 252 word[wp++] = c;
284 char *line = (char*) malloc(inputfile->maxlinewidth*64); 295 char *line = (char*) malloc(inputfile->maxlinewidth*64);
285 int lnw = lnint(inputfile->count); 296 int lnw = lnint(inputfile->count);
286 for (int i = 0 ; i < inputfile->count ; i++) { 297 for (int i = 0 ; i < inputfile->count ; i++) {
287 parseline(inputfile->lines[i], line); 298 parseline(inputfile->lines[i], line);
288 printf("<span class=\"c2html-lineno\">%*d:</span> %s", 299 printf("<span class=\"c2html-lineno\">%*d:</span> %s",
289 lnw, i, line); 300 lnw, i+1, line);
290 } 301 }
291 free(line); 302 free(line);
292 printf("</pre>\n"); 303 printf("</pre>\n");
293 freeinputfilebuffer(inputfile); 304 freeinputfilebuffer(inputfile);
294 } 305 }

mercurial