replaced function static variables with struct members

Fri, 30 Aug 2013 11:23:44 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 30 Aug 2013 11:23:44 +0200
changeset 20
ebbf0776c1bc
parent 19
2e812df2b231
child 21
537aec525835

replaced function static variables with struct members

src/c2html.c file | annotate | diff | comparison | revisions
--- a/src/c2html.c	Fri Aug 30 10:51:49 2013 +0200
+++ b/src/c2html.c	Fri Aug 30 11:23:44 2013 +0200
@@ -73,11 +73,17 @@
     return word[0] == '@';
 }
 
-typedef struct {
+typedef struct _highlighter_t highlighter_t;
+
+struct _highlighter_t {
     const char** keywords;
     int(*istype)(char*,size_t);
     int(*isdirective)(char*);
-} highlighter_t;
+    void(*parser)(char*,char*,highlighter_t*);
+    int iscommentml;
+    char word[WORDBUF_SIZE];
+    char includefile[FILENAME_MAX];
+};
 
 typedef struct {
     char* outfilename;
@@ -193,23 +199,23 @@
     return 1;
 }
 
-void parseline(char *src, char *dest, highlighter_t *highlighter) {
+void parseline(char *src, char *dest, highlighter_t *hltr) {
+    hltr->parser(src, dest, hltr);
+}
+
+void cjparseline(char *src, char *dest, highlighter_t *hltr) {
     size_t sp = 0, dp = 0;
     /* indent */
     while (isspace(src[sp])) {
         dest[dp++] = src[sp++];
     }
 
-    static char word[WORDBUF_SIZE];
-    static char includefile[FILENAME_MAX];
-
-    memset(word, 0, WORDBUF_SIZE);
+    memset(hltr->word, 0, WORDBUF_SIZE);
     size_t wp = 0, ifp = 0;
     int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
-    static int iscommentml;
     int isescaping = 0;
 
-    if (iscommentml) {
+    if (hltr->iscommentml) {
         iscomment = 1;
         memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
         dp += 29;
@@ -218,15 +224,15 @@
     for (char c = src[sp] ; c ; c=src[++sp]) {
         /* comments */
         if (c == '/') {
-            if (iscommentml && sp > 0 && src[sp-1] == '*') {
+            if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') {
                 iscomment = 0;
-                iscommentml = 0;
+                hltr->iscommentml = 0;
                 memcpy(&(dest[dp]), "/</span>", 8);
                 dp += 8;
                 continue;
             } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
                 iscomment = 1;
-                iscommentml = (src[sp+1] == '*');
+                hltr->iscommentml = (src[sp+1] == '*');
                 memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
                 dp += 29;
             }
@@ -247,7 +253,7 @@
                 if (parseinclude) {
                     dest[dp++] = '\"';
                     dest[dp++] = '>';
-                    memcpy(&(dest[dp]), includefile, ifp);
+                    memcpy(&(dest[dp]), hltr->includefile, ifp);
                     dp += ifp;
 
                     dp = writeescapedchar(dest, dp, c);
@@ -260,7 +266,7 @@
                     dp += 35;
                     dp = writeescapedchar(dest, dp, c);
                     ifp = 0;
-                    includefile[ifp++] = '\"';
+                    hltr->includefile[ifp++] = '\"';
                     parseinclude = 1;
                 }
             } else if (c == '>') {
@@ -269,7 +275,7 @@
                 dp += 7;
             } else {
                 if (parseinclude) {
-                    includefile[ifp++] = c;
+                    hltr->includefile[ifp++] = c;
                 }
                 dp = writeescapedchar(dest, dp, c);
             }
@@ -293,21 +299,21 @@
                     /* interpret word int_t */
                     if (wp > 0 && wp < WORDBUF_SIZE) {
                         int closespan = 1;
-                        if (iskeyword(word, highlighter->keywords)) {
+                        if (iskeyword(hltr->word, hltr->keywords)) {
                             memcpy(&(dest[dp]),
                                 "<span class=\"c2html-keyword\">", 29);
                             dp += 29;
-                        } else if (highlighter->istype(word, wp)) {
+                        } else if (hltr->istype(hltr->word, wp)) {
                             memcpy(&(dest[dp]),
                                 "<span class=\"c2html-type\">", 26);
                             dp += 26;
-                        } else if (highlighter->isdirective(word)) {
+                        } else if (hltr->isdirective(hltr->word)) {
                             isinclude = !strncmp(
-                                "#include", word, WORDBUF_SIZE);
+                                "#include", hltr->word, WORDBUF_SIZE);
                             memcpy(&(dest[dp]),
                                 "<span class=\"c2html-directive\">", 31);
                             dp += 31;
-                        } else if (iscapsonly(word, wp)) {
+                        } else if (iscapsonly(hltr->word, wp)) {
                             memcpy(&(dest[dp]),
                                 "<span class=\"c2html-macroconst\">", 32);
                             dp += 32;
@@ -315,23 +321,23 @@
                             closespan = 0;
                         }
                         for (int i = 0 ; i < wp ; i++) {
-                            dp = writeescapedchar(dest, dp, word[i]);
+                            dp = writeescapedchar(dest, dp, hltr->word[i]);
                         }
                         if (closespan) {
                             memcpy(&(dest[dp]), "</span>", 7);
                             dp += 7;
                         }
                     }
-                    memset(word, 0, WORDBUF_SIZE);
+                    memset(hltr->word, 0, WORDBUF_SIZE);
                     wp = 0;
                     dp = writeescapedchar(dest, dp, c);
                 } else {
                     /* read word */
                     if (wp < WORDBUF_SIZE) {
-                        word[wp++] = c;
+                        hltr->word[wp++] = c;
                     } else if (wp == WORDBUF_SIZE) {
                         for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
-                            dp = writeescapedchar(dest, dp, word[i]);
+                            dp = writeescapedchar(dest, dp, hltr->word[i]);
                         }
                         wp++;
                         dp = writeescapedchar(dest, dp, c);
@@ -367,15 +373,16 @@
 }
 
 int main(int argc, char** argv) {
-
     settings_t settings;
     settings.outfilename = NULL;
     settings.highlight = 1;
 
     highlighter_t highlighter;
+    memset(&highlighter, 0, sizeof(highlighter));
     highlighter.isdirective = iscdirective;
     highlighter.istype = isctype;
     highlighter.keywords = ckeywords;
+    highlighter.parser = cjparseline;
 
     char optc;
     while ((optc = getopt(argc, argv, "hjo:p")) != -1) {

mercurial