Fri, 21 Jun 2013 11:42:18 +0200
strings and directives
src/c2html.c | file | annotate | diff | comparison | revisions | |
test/header.html | file | annotate | diff | comparison | revisions |
1.1 --- a/src/c2html.c Wed Jun 12 14:50:27 2013 +0200 1.2 +++ b/src/c2html.c Fri Jun 21 11:42:18 2013 +0200 1.3 @@ -37,6 +37,9 @@ 1.4 #define INPUTBUF_SIZE 2048 1.5 #define WORDBUF_SIZE 16 1.6 1.7 +#define istype(word, len) (word[len-2] == '_' && word[len-1] == 't') 1.8 +#define isdirective(word) (word[0] == '#') 1.9 + 1.10 const char* keywords[] = { 1.11 "auto", "break", "case", "char", "const", "continue", "default", "do", 1.12 "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", 1.13 @@ -146,8 +149,6 @@ 1.14 return 0; 1.15 } 1.16 1.17 -#define istype(word, len) (word[len-2] == '_' && word[len-1] == 't') 1.18 - 1.19 void parseline(char *src, char *dest) { 1.20 size_t sp = 0, dp = 0; 1.21 /* indent */ 1.22 @@ -157,47 +158,68 @@ 1.23 char word[WORDBUF_SIZE]; 1.24 memset(word, 0, WORDBUF_SIZE); 1.25 size_t wp = 0; 1.26 - int closespan; 1.27 + int isstring = 0, iscomment = 0; 1.28 + int isescaping = 0; 1.29 for (char c = src[sp] ; c ; c=src[++sp]) { 1.30 - if (!isalnum(c) && c != '_') { 1.31 - /* interpret word int_t */ 1.32 - if (wp > 0 && wp < WORDBUF_SIZE) { 1.33 - if (iskeyword(word)) { 1.34 - memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29); 1.35 - dp += 29; 1.36 - closespan = 1; 1.37 - } else if (istype(word, wp)) { 1.38 - memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26); 1.39 - dp += 26; 1.40 - closespan = 1; 1.41 - } else { 1.42 - closespan = 0; 1.43 - } 1.44 - for (int i = 0 ; i < wp ; i++) { 1.45 - dp = writeescapedchar(dest, dp, word[i]); 1.46 - } 1.47 - if (closespan) { 1.48 - memcpy(&(dest[dp]), "</span>", 7); 1.49 - dp += 7; 1.50 - } 1.51 - memset(word, 0, WORDBUF_SIZE); 1.52 - wp = 0; 1.53 - } 1.54 - dp = writeescapedchar(dest, dp, c); 1.55 - } else { 1.56 - /* read word */ 1.57 - if (wp < WORDBUF_SIZE) { 1.58 - word[wp++] = c; 1.59 - } else if (wp == WORDBUF_SIZE) { 1.60 - for (int i = 0 ; i < WORDBUF_SIZE ; i++) { 1.61 - dp = writeescapedchar(dest, dp, word[i]); 1.62 - } 1.63 - wp++; 1.64 + /* comments and strings */ 1.65 + if (!isescaping && (c == '\'' || c == '\"')) { 1.66 + isstring ^= 1; 1.67 + if (isstring) { 1.68 + memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28); 1.69 + dp += 28; 1.70 dp = writeescapedchar(dest, dp, c); 1.71 } else { 1.72 dp = writeescapedchar(dest, dp, c); 1.73 + memcpy(&(dest[dp]), "</span>", 7); 1.74 + dp += 7; 1.75 + } 1.76 + } else { 1.77 + if (iscomment || isstring) { 1.78 + dp = writeescapedchar(dest, dp, c); 1.79 + } else if (!isalnum(c) && c != '_' && c != '#') { 1.80 + /* interpret word int_t */ 1.81 + if (wp > 0 && wp < WORDBUF_SIZE) { 1.82 + int closespan = 1; 1.83 + if (iskeyword(word)) { 1.84 + memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29); 1.85 + dp += 29; 1.86 + } else if (istype(word, wp)) { 1.87 + memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26); 1.88 + dp += 26; 1.89 + } else if (isdirective(word)) { 1.90 + memcpy(&(dest[dp]), "<span class=\"c2html-directive\">", 31); 1.91 + dp += 31; 1.92 + } else { 1.93 + closespan = 0; 1.94 + } 1.95 + for (int i = 0 ; i < wp ; i++) { 1.96 + dp = writeescapedchar(dest, dp, word[i]); 1.97 + } 1.98 + if (closespan) { 1.99 + memcpy(&(dest[dp]), "</span>", 7); 1.100 + dp += 7; 1.101 + } 1.102 + memset(word, 0, WORDBUF_SIZE); 1.103 + wp = 0; 1.104 + } 1.105 + dp = writeescapedchar(dest, dp, c); 1.106 + } else { 1.107 + /* read word */ 1.108 + if (wp < WORDBUF_SIZE) { 1.109 + word[wp++] = c; 1.110 + } else if (wp == WORDBUF_SIZE) { 1.111 + for (int i = 0 ; i < WORDBUF_SIZE ; i++) { 1.112 + dp = writeescapedchar(dest, dp, word[i]); 1.113 + } 1.114 + wp++; 1.115 + dp = writeescapedchar(dest, dp, c); 1.116 + } else { 1.117 + dp = writeescapedchar(dest, dp, c); 1.118 + } 1.119 } 1.120 } 1.121 + 1.122 + isescaping = !isescaping & (c == '\\'); 1.123 } 1.124 dest[dp] = 0; 1.125 }
2.1 --- a/test/header.html Wed Jun 12 14:50:27 2013 +0200 2.2 +++ b/test/header.html Fri Jun 21 11:42:18 2013 +0200 2.3 @@ -13,6 +13,12 @@ 2.4 span.c2html-type { 2.5 color: cornflowerblue; 2.6 } 2.7 + span.c2html-directive { 2.8 + color: green; 2.9 + } 2.10 + span.c2html-string { 2.11 + color: orange; 2.12 + } 2.13 </style> 2.14 </head> 2.15 <body>