# HG changeset patch # User Mike Becker # Date 1373466676 -7200 # Node ID fa0bcd0444eb0f6de914486303e06f00ea4a630c # Parent 398a7589297f1fe771b136211f7330cfca26f50a prepared java highlighting diff -r 398a7589297f -r fa0bcd0444eb src/c2html.c --- 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]), "", 29); dp += 29; - } else if (istype(word, wp)) { + } else if (highlighter->istype(word, wp)) { memcpy(&(dest[dp]), "", 26); dp += 26; - } else if (isdirective(word)) { + } else if (highlighter->isdirective(word)) { isinclude = !strncmp("#include", word, WORDBUF_SIZE); memcpy(&(dest[dp]), "", 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]; }