Fri, 30 Aug 2013 11:23:44 +0200
replaced function static variables with struct members
src/c2html.c | file | annotate | diff | comparison | revisions |
1.1 --- a/src/c2html.c Fri Aug 30 10:51:49 2013 +0200 1.2 +++ b/src/c2html.c Fri Aug 30 11:23:44 2013 +0200 1.3 @@ -73,11 +73,17 @@ 1.4 return word[0] == '@'; 1.5 } 1.6 1.7 -typedef struct { 1.8 +typedef struct _highlighter_t highlighter_t; 1.9 + 1.10 +struct _highlighter_t { 1.11 const char** keywords; 1.12 int(*istype)(char*,size_t); 1.13 int(*isdirective)(char*); 1.14 -} highlighter_t; 1.15 + void(*parser)(char*,char*,highlighter_t*); 1.16 + int iscommentml; 1.17 + char word[WORDBUF_SIZE]; 1.18 + char includefile[FILENAME_MAX]; 1.19 +}; 1.20 1.21 typedef struct { 1.22 char* outfilename; 1.23 @@ -193,23 +199,23 @@ 1.24 return 1; 1.25 } 1.26 1.27 -void parseline(char *src, char *dest, highlighter_t *highlighter) { 1.28 +void parseline(char *src, char *dest, highlighter_t *hltr) { 1.29 + hltr->parser(src, dest, hltr); 1.30 +} 1.31 + 1.32 +void cjparseline(char *src, char *dest, highlighter_t *hltr) { 1.33 size_t sp = 0, dp = 0; 1.34 /* indent */ 1.35 while (isspace(src[sp])) { 1.36 dest[dp++] = src[sp++]; 1.37 } 1.38 1.39 - static char word[WORDBUF_SIZE]; 1.40 - static char includefile[FILENAME_MAX]; 1.41 - 1.42 - memset(word, 0, WORDBUF_SIZE); 1.43 + memset(hltr->word, 0, WORDBUF_SIZE); 1.44 size_t wp = 0, ifp = 0; 1.45 int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0; 1.46 - static int iscommentml; 1.47 int isescaping = 0; 1.48 1.49 - if (iscommentml) { 1.50 + if (hltr->iscommentml) { 1.51 iscomment = 1; 1.52 memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); 1.53 dp += 29; 1.54 @@ -218,15 +224,15 @@ 1.55 for (char c = src[sp] ; c ; c=src[++sp]) { 1.56 /* comments */ 1.57 if (c == '/') { 1.58 - if (iscommentml && sp > 0 && src[sp-1] == '*') { 1.59 + if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') { 1.60 iscomment = 0; 1.61 - iscommentml = 0; 1.62 + hltr->iscommentml = 0; 1.63 memcpy(&(dest[dp]), "/</span>", 8); 1.64 dp += 8; 1.65 continue; 1.66 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { 1.67 iscomment = 1; 1.68 - iscommentml = (src[sp+1] == '*'); 1.69 + hltr->iscommentml = (src[sp+1] == '*'); 1.70 memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); 1.71 dp += 29; 1.72 } 1.73 @@ -247,7 +253,7 @@ 1.74 if (parseinclude) { 1.75 dest[dp++] = '\"'; 1.76 dest[dp++] = '>'; 1.77 - memcpy(&(dest[dp]), includefile, ifp); 1.78 + memcpy(&(dest[dp]), hltr->includefile, ifp); 1.79 dp += ifp; 1.80 1.81 dp = writeescapedchar(dest, dp, c); 1.82 @@ -260,7 +266,7 @@ 1.83 dp += 35; 1.84 dp = writeescapedchar(dest, dp, c); 1.85 ifp = 0; 1.86 - includefile[ifp++] = '\"'; 1.87 + hltr->includefile[ifp++] = '\"'; 1.88 parseinclude = 1; 1.89 } 1.90 } else if (c == '>') { 1.91 @@ -269,7 +275,7 @@ 1.92 dp += 7; 1.93 } else { 1.94 if (parseinclude) { 1.95 - includefile[ifp++] = c; 1.96 + hltr->includefile[ifp++] = c; 1.97 } 1.98 dp = writeescapedchar(dest, dp, c); 1.99 } 1.100 @@ -293,21 +299,21 @@ 1.101 /* interpret word int_t */ 1.102 if (wp > 0 && wp < WORDBUF_SIZE) { 1.103 int closespan = 1; 1.104 - if (iskeyword(word, highlighter->keywords)) { 1.105 + if (iskeyword(hltr->word, hltr->keywords)) { 1.106 memcpy(&(dest[dp]), 1.107 "<span class=\"c2html-keyword\">", 29); 1.108 dp += 29; 1.109 - } else if (highlighter->istype(word, wp)) { 1.110 + } else if (hltr->istype(hltr->word, wp)) { 1.111 memcpy(&(dest[dp]), 1.112 "<span class=\"c2html-type\">", 26); 1.113 dp += 26; 1.114 - } else if (highlighter->isdirective(word)) { 1.115 + } else if (hltr->isdirective(hltr->word)) { 1.116 isinclude = !strncmp( 1.117 - "#include", word, WORDBUF_SIZE); 1.118 + "#include", hltr->word, WORDBUF_SIZE); 1.119 memcpy(&(dest[dp]), 1.120 "<span class=\"c2html-directive\">", 31); 1.121 dp += 31; 1.122 - } else if (iscapsonly(word, wp)) { 1.123 + } else if (iscapsonly(hltr->word, wp)) { 1.124 memcpy(&(dest[dp]), 1.125 "<span class=\"c2html-macroconst\">", 32); 1.126 dp += 32; 1.127 @@ -315,23 +321,23 @@ 1.128 closespan = 0; 1.129 } 1.130 for (int i = 0 ; i < wp ; i++) { 1.131 - dp = writeescapedchar(dest, dp, word[i]); 1.132 + dp = writeescapedchar(dest, dp, hltr->word[i]); 1.133 } 1.134 if (closespan) { 1.135 memcpy(&(dest[dp]), "</span>", 7); 1.136 dp += 7; 1.137 } 1.138 } 1.139 - memset(word, 0, WORDBUF_SIZE); 1.140 + memset(hltr->word, 0, WORDBUF_SIZE); 1.141 wp = 0; 1.142 dp = writeescapedchar(dest, dp, c); 1.143 } else { 1.144 /* read word */ 1.145 if (wp < WORDBUF_SIZE) { 1.146 - word[wp++] = c; 1.147 + hltr->word[wp++] = c; 1.148 } else if (wp == WORDBUF_SIZE) { 1.149 for (int i = 0 ; i < WORDBUF_SIZE ; i++) { 1.150 - dp = writeescapedchar(dest, dp, word[i]); 1.151 + dp = writeescapedchar(dest, dp, hltr->word[i]); 1.152 } 1.153 wp++; 1.154 dp = writeescapedchar(dest, dp, c); 1.155 @@ -367,15 +373,16 @@ 1.156 } 1.157 1.158 int main(int argc, char** argv) { 1.159 - 1.160 settings_t settings; 1.161 settings.outfilename = NULL; 1.162 settings.highlight = 1; 1.163 1.164 highlighter_t highlighter; 1.165 + memset(&highlighter, 0, sizeof(highlighter)); 1.166 highlighter.isdirective = iscdirective; 1.167 highlighter.istype = isctype; 1.168 highlighter.keywords = ckeywords; 1.169 + highlighter.parser = cjparseline; 1.170 1.171 char optc; 1.172 while ((optc = getopt(argc, argv, "hjo:p")) != -1) {