X-Git-Url: https://git.xonotic.org/?p=xonotic%2Fgmqcc.git;a=blobdiff_plain;f=ftepp.c;h=595b520fec5439f34a1614f737f9f83f7ef3c150;hp=6fb8c6f8caeb93fd5c8c158abd1c35932a00b18f;hb=b8e536d409a98bcd80d266e85dcc6b9a7c2a3c2c;hpb=6284cc673dff2de0e3c9b706beb847136591a0eb;ds=sidebyside diff --git a/ftepp.c b/ftepp.c old mode 100755 new mode 100644 index 6fb8c6f..595b520 --- a/ftepp.c +++ b/ftepp.c @@ -65,8 +65,7 @@ typedef struct ftepp_s { bool output_on; ppcondition *conditions; /*ppmacro **macros;*/ - ht macros; /* hashtable */ - + ht macros; /* hashtable */ char *output_string; char *itemname; @@ -198,7 +197,12 @@ char *ftepp_predef_timestamp(lex_file *context) { return value; } -const ftepp_predef_t ftepp_predefs[FTEPP_PREDEF_COUNT] = { +typedef struct { + const char *name; + char *(*func)(lex_file *); +} ftepp_predef_t; + +static const ftepp_predef_t ftepp_predefs[] = { { "__LINE__", &ftepp_predef_line }, { "__FILE__", &ftepp_predef_file }, { "__COUNTER__", &ftepp_predef_counter }, @@ -210,6 +214,25 @@ const ftepp_predef_t ftepp_predefs[FTEPP_PREDEF_COUNT] = { { "__TIME_STAMP__", &ftepp_predef_timestamp } }; +static GMQCC_INLINE int ftepp_predef_index(const char *name) { + /* no hashtable here, we simply check for one to exist the naive way */ + int i; + for(i = 0; i < (int)(sizeof(ftepp_predefs)/sizeof(*ftepp_predefs)); i++) + if (!strcmp(ftepp_predefs[i].name, name)) + return i; + return -1; +} + +bool ftepp_predef_exists(const char *name) { + return ftepp_predef_index(name) != -1; +} + +/* singleton because we're allowed */ +static GMQCC_INLINE char *(*ftepp_predef(const char *name))(lex_file *context) { + int i = ftepp_predef_index(name); + return (i != -1) ? ftepp_predefs[i].func : NULL; +} + #define ftepp_tokval(f) ((f)->lex->tok.value) #define ftepp_ctx(f) ((f)->lex->tok.ctx) @@ -1652,7 +1675,6 @@ static bool ftepp_preprocess(ftepp_t *ftepp) /* predef stuff */ char *expand = NULL; - size_t i; ftepp->lex->flags.preprocessing = true; ftepp->lex->flags.mergelines = false; @@ -1673,15 +1695,14 @@ static bool ftepp_preprocess(ftepp_t *ftepp) case TOKEN_TYPENAME: /* is it a predef? */ if (OPTS_FLAG(FTEPP_PREDEFS)) { - for (i = 0; i < sizeof(ftepp_predefs) / sizeof (*ftepp_predefs); i++) { - if (!strcmp(ftepp_predefs[i].name, ftepp_tokval(ftepp))) { - expand = ftepp_predefs[i].func(ftepp->lex); - ftepp_out(ftepp, expand, false); - ftepp_next(ftepp); /* skip */ - - mem_d(expand); /* free memory */ - break; - } + char *(*predef)(lex_file*) = ftepp_predef(ftepp_tokval(ftepp)); + if (predef) { + expand = predef(ftepp->lex); + ftepp_out (ftepp, expand, false); + ftepp_next(ftepp); + + mem_d(expand); + break; } }