prepared java highlighting

Wed, 10 Jul 2013 16:31:16 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 10 Jul 2013 16:31:16 +0200
changeset 16
fa0bcd0444eb
parent 15
398a7589297f
child 17
7ea86024aef0

prepared java highlighting

src/c2html.c file | annotate | diff | comparison | revisions
--- a/src/c2html.c	Wed Jul 10 14:38:56 2013 +0200
+++ b/src/c2html.c	Wed Jul 10 16:31:16 2013 +0200
@@ -37,16 +37,36 @@
 #define INPUTBUF_SIZE 2048
 #define WORDBUF_SIZE 16
 
-#define istype(word, len) (word[len-2] == '_' && word[len-1] == 't')
-#define isdirective(word) (word[0] == '#')
-
-const char* keywords[] = {
+const char* ckeywords[] = {
   "auto", "break", "case", "char", "const", "continue", "default", "do", 
   "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", 
-  "long", "register", "return", "short", "signed", "sizeof", "static", "struct", 
-  "switch", "typedef", "union", "unsigned", "void", "volatile", "while", NULL
+  "long", "register", "return", "short", "signed", "sizeof", "static",
+  "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",
+  "while", NULL
 };
 
+int istype(char *word, size_t len) {
+  return (word[len-2] == '_' && word[len-1] == 't');
+}
+
+int isdirective(char *word) {
+  return (word[0] == '#');
+}
+
+int notypes(char *word, size_t len) {
+  return 0;
+}
+
+int nodirectives(char *word) {
+  return 0;
+}
+
+typedef struct {
+  const char** keywords;
+  int(*istype)(char*,size_t);
+  int(*isdirective)(char*);
+} highlighter_t;
+
 typedef struct {
   char* outfilename;
   char* infilename;
@@ -145,7 +165,7 @@
   return dp;
 }
 
-int iskeyword(char *word) {
+int iskeyword(char *word, const char** keywords) {
   for (int i = 0 ; keywords[i] ; i++) {
     if (strncmp(keywords[i], word, WORDBUF_SIZE) == 0) {
       return 1;
@@ -163,7 +183,7 @@
   return 1;
 }
 
-void parseline(char *src, char *dest) {
+void parseline(char *src, char *dest, highlighter_t *highlighter) {
   size_t sp = 0, dp = 0;
   /* indent */
   while (isspace(src[sp])) {
@@ -262,13 +282,13 @@
           /* interpret word int_t */
           if (wp > 0 && wp < WORDBUF_SIZE) {
             int closespan = 1;
-            if (iskeyword(word)) {
+            if (iskeyword(word, highlighter->keywords)) {
               memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29);
               dp += 29;
-            } else if (istype(word, wp)) {
+            } else if (highlighter->istype(word, wp)) {
               memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26);
               dp += 26;
-            } else if (isdirective(word)) {
+            } else if (highlighter->isdirective(word)) {
               isinclude = !strncmp("#include", word, WORDBUF_SIZE);
               memcpy(&(dest[dp]), "<span class=\"c2html-directive\">", 31);
               dp += 31;
@@ -335,6 +355,11 @@
   settings.outfilename = NULL;
   settings.highlight = 1;
   
+  highlighter_t highlighter;
+  highlighter.isdirective = isdirective;
+  highlighter.istype = istype;
+  highlighter.keywords = ckeywords;
+  
   char optc;
   while ((optc = getopt(argc, argv, "ho:p")) != -1) {
     switch (optc) {
@@ -378,7 +403,7 @@
       int lnw = lnint(inputfile->count);
       for (int i = 0 ; i < inputfile->count ; i++) {
         if (settings.highlight) {
-          parseline(inputfile->lines[i], line);
+          parseline(inputfile->lines[i], line, &highlighter);
         } else {
           line = inputfile->lines[i];
         }

mercurial