Released version 3-2015061300
[notion.git] / libtu / map.c
blob98f0dd60f813418502046af1a94c17b6bcf0ce5d
1 /*
2 * libtu/map.c
4 * Copyright (c) Tuomo Valkonen 1999-2002.
6 * You may distribute and modify this library under the terms of either
7 * the Clarified Artistic License or the GNU LGPL, version 2.1 or later.
8 */
10 #include <string.h>
11 #include "map.h"
14 int stringintmap_ndx(const StringIntMap *map, const char *str)
16 int i;
18 for(i=0; map[i].string!=NULL; i++){
19 if(strcmp(str, map[i].string)==0)
20 return i;
23 return -1;
27 int stringintmap_value(const StringIntMap *map, const char *str, int dflt)
29 int i=stringintmap_ndx(map, str);
30 return (i==-1 ? dflt : map[i].value);
34 const char *stringintmap_key(const StringIntMap *map, int value,
35 const char *dflt)
37 int i;
39 for(i=0; map[i].string!=NULL; ++i){
40 if(map[i].value==value){
41 return map[i].string;
45 return dflt;
51 int stringfunptrmap_ndx(const StringFunPtrMap *map, const char *str)
53 int i;
55 for(i=0; map[i].string!=NULL; i++){
56 if(strcmp(str, map[i].string)==0)
57 return i;
60 return -1;
64 FunPtr stringfunptrmap_value(const StringFunPtrMap *map, const char *str, FunPtr dflt)
66 int i=stringfunptrmap_ndx(map, str);
67 return (i==-1 ? dflt : map[i].value);
71 const char *stringfunptrmap_key(const StringFunPtrMap *map, FunPtr value,
72 const char *dflt)
74 int i;
76 for(i=0; map[i].string!=NULL; ++i){
77 if(map[i].value==value){
78 return map[i].string;
82 return dflt;