yw_map

Name

yw_map -- lookup string in keyword -> int mapping

Synopsis


     #include <yw/map.h>
   

int yw_map(const char *key, const YwMapEntry *entries, YwMapCache **cache);

DESCRIPTION

This function searches entries for key, and returns value field of entry, that matched. If cache is not NULL, it is used to store cache of entries. YwMapEntry datatype is defined as follows:

        typedef struct YwMapEntry_struct YwMapEntry;
        struct YwMapEntry_struct {
                const char *key;
                int value;
        };
    
Last entry has key NULL, it's value is returned when no other entry matched. Example of use:

        enum {
               ev_born,
               ev_eat,
               ev_die,
               ev_oops
        };
        
        int lookup(const char *key)
        {
               YwMapEntry e[] = {
                      { "born", ev_born },
                      { "eat", ev_eat },
                      { "die", ev_die },
                      { NULL, ev_oops }
               };
               static YwMapCache *cache = NULL;
        
               return yw_map(key, e, &cache);
        }
    
Note, that cache is initialized to NULL, before passing to yw_map, it is also static, so caching makes sense :^) In this example cache is never released -- it's OK for most cases, when caches are static variables, used throughout lifetime of a program. This function is multithread safe -- i.e. concurently running threads won't create *cache at the same time.

RETURN VALUE

value of entry, that matched key, or value of entry with key of NULL, when no entry matched.

SEE ALSO

yw_map_free_cache (3),

INFO

Generated from: map.c,v 1.5 2001/05/05 11:36:18 dobrek Exp.