From: Tomáš E. <eb...@dr...> - 2011-02-07 03:29:21
|
From: Tomáš Ebenlendr <eb...@uc...> This patch is already committed in http://drak.ucw.cz/~ebik/git/libtu This patch is used by my reimplementation of proportional/elastic tab sizes algorithm. --------------------------------------------------------------------- Adds map between function pointers and strings. This should allow named alternative implementation of a function. --- map.c | 36 ++++++++++++++++++++++++++++++++++++ map.h | 27 +++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 0 deletions(-) diff --git a/map.c b/map.c index 7e5f202..98f0dd6 100644 --- a/map.c +++ b/map.c @@ -45,3 +45,39 @@ const char *stringintmap_key(const StringIntMap *map, int value, return dflt; } + + + +int stringfunptrmap_ndx(const StringFunPtrMap *map, const char *str) +{ + int i; + + for(i=0; map[i].string!=NULL; i++){ + if(strcmp(str, map[i].string)==0) + return i; + } + + return -1; +} + + +FunPtr stringfunptrmap_value(const StringFunPtrMap *map, const char *str, FunPtr dflt) +{ + int i=stringfunptrmap_ndx(map, str); + return (i==-1 ? dflt : map[i].value); +} + + +const char *stringfunptrmap_key(const StringFunPtrMap *map, FunPtr value, + const char *dflt) +{ + int i; + + for(i=0; map[i].string!=NULL; ++i){ + if(map[i].value==value){ + return map[i].string; + } + } + + return dflt; +} diff --git a/map.h b/map.h index d66ae74..3a0db39 100644 --- a/map.h +++ b/map.h @@ -24,4 +24,31 @@ extern int stringintmap_value(const StringIntMap *map, const char *str, extern const char *stringintmap_key(const StringIntMap *map, int value, const char *dflt); + + +typedef void (*FunPtr)(void); + +#define DECLFUNPTRMAP(NAME) \ +typedef struct _String##NAME##Map{\ + const char *string;\ + NAME value;\ +} String##NAME##Map + +DECLFUNPTRMAP(FunPtr); + +#define END_STRINGPTRMAP {NULL, NULL} + +/* Return the index of str in map or -1 if not found. */ +extern int stringfunptrmap_ndx(const StringFunPtrMap *map, const char *str); +extern FunPtr stringfunptrmap_value(const StringFunPtrMap *map, const char *str, + FunPtr dflt); +extern const char *stringfunptrmap_key(const StringFunPtrMap *map, + FunPtr value, const char *dflt); + + +#define STRINGFUNPTRMAP_NDX(MAP,STR) stringfunptrmap_ndx((StringFunPtrMap *)MAP, STR) +#define STRINGFUNPTRMAP_VALUE(TYPE,MAP,STR,DFLT) (TYPE)stringfunptrmap_value((StringFunPtrMap *)MAP, STR, (FunPtr)DFLT) +#define STRINGFUNPTRMAP_KEY(MAP,VALUE,DFLT) stringfunptrmap_key((StringFunPtrMap *)MAP, (FunPtr)VALUE, DFLT) + + #endif /* LIBTU_MAP_H */ -- 1.7.1 |