Wed, 10 Jul 2013 16:31:16 +0200
prepared java highlighting
src/c2html.c | file | annotate | diff | comparison | revisions |
1.1 --- a/src/c2html.c Wed Jul 10 14:38:56 2013 +0200 1.2 +++ b/src/c2html.c Wed Jul 10 16:31:16 2013 +0200 1.3 @@ -37,16 +37,36 @@ 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 +const char* ckeywords[] = { 1.12 "auto", "break", "case", "char", "const", "continue", "default", "do", 1.13 "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", 1.14 - "long", "register", "return", "short", "signed", "sizeof", "static", "struct", 1.15 - "switch", "typedef", "union", "unsigned", "void", "volatile", "while", NULL 1.16 + "long", "register", "return", "short", "signed", "sizeof", "static", 1.17 + "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", 1.18 + "while", NULL 1.19 }; 1.20 1.21 +int istype(char *word, size_t len) { 1.22 + return (word[len-2] == '_' && word[len-1] == 't'); 1.23 +} 1.24 + 1.25 +int isdirective(char *word) { 1.26 + return (word[0] == '#'); 1.27 +} 1.28 + 1.29 +int notypes(char *word, size_t len) { 1.30 + return 0; 1.31 +} 1.32 + 1.33 +int nodirectives(char *word) { 1.34 + return 0; 1.35 +} 1.36 + 1.37 +typedef struct { 1.38 + const char** keywords; 1.39 + int(*istype)(char*,size_t); 1.40 + int(*isdirective)(char*); 1.41 +} highlighter_t; 1.42 + 1.43 typedef struct { 1.44 char* outfilename; 1.45 char* infilename; 1.46 @@ -145,7 +165,7 @@ 1.47 return dp; 1.48 } 1.49 1.50 -int iskeyword(char *word) { 1.51 +int iskeyword(char *word, const char** keywords) { 1.52 for (int i = 0 ; keywords[i] ; i++) { 1.53 if (strncmp(keywords[i], word, WORDBUF_SIZE) == 0) { 1.54 return 1; 1.55 @@ -163,7 +183,7 @@ 1.56 return 1; 1.57 } 1.58 1.59 -void parseline(char *src, char *dest) { 1.60 +void parseline(char *src, char *dest, highlighter_t *highlighter) { 1.61 size_t sp = 0, dp = 0; 1.62 /* indent */ 1.63 while (isspace(src[sp])) { 1.64 @@ -262,13 +282,13 @@ 1.65 /* interpret word int_t */ 1.66 if (wp > 0 && wp < WORDBUF_SIZE) { 1.67 int closespan = 1; 1.68 - if (iskeyword(word)) { 1.69 + if (iskeyword(word, highlighter->keywords)) { 1.70 memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29); 1.71 dp += 29; 1.72 - } else if (istype(word, wp)) { 1.73 + } else if (highlighter->istype(word, wp)) { 1.74 memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26); 1.75 dp += 26; 1.76 - } else if (isdirective(word)) { 1.77 + } else if (highlighter->isdirective(word)) { 1.78 isinclude = !strncmp("#include", word, WORDBUF_SIZE); 1.79 memcpy(&(dest[dp]), "<span class=\"c2html-directive\">", 31); 1.80 dp += 31; 1.81 @@ -335,6 +355,11 @@ 1.82 settings.outfilename = NULL; 1.83 settings.highlight = 1; 1.84 1.85 + highlighter_t highlighter; 1.86 + highlighter.isdirective = isdirective; 1.87 + highlighter.istype = istype; 1.88 + highlighter.keywords = ckeywords; 1.89 + 1.90 char optc; 1.91 while ((optc = getopt(argc, argv, "ho:p")) != -1) { 1.92 switch (optc) { 1.93 @@ -378,7 +403,7 @@ 1.94 int lnw = lnint(inputfile->count); 1.95 for (int i = 0 ; i < inputfile->count ; i++) { 1.96 if (settings.highlight) { 1.97 - parseline(inputfile->lines[i], line); 1.98 + parseline(inputfile->lines[i], line, &highlighter); 1.99 } else { 1.100 line = inputfile->lines[i]; 1.101 }