1 /*
2  *  tvheadend
3  *  Copyright (C) 2007 Andreas Öman
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef STRTAB_H_
20 #define STRTAB_H_
21 
22 #include "htsmsg.h"
23 #include "tvh_locale.h"
24 
25 #include <strings.h>
26 
27 struct strtab {
28   const char *str;
29   int val;
30 };
31 
32 struct strtab_u32 {
33   const char *str;
34   uint32_t val;
35 };
36 
37 struct strtab_str {
38   const char *str;
39   const char *val;
40 };
41 
42 static int str2val0(const char *str, const struct strtab tab[], int l)
43      __attribute((unused));
44 
45 static inline int
str2val0(const char * str,const struct strtab tab[],int l)46 str2val0(const char *str, const struct strtab tab[], int l)
47 {
48   int i;
49   for(i = 0; i < l; i++)
50     if(!strcasecmp(str, tab[i].str))
51       return tab[i].val;
52 
53   return -1;
54 }
55 
56 #define str2val(str, tab) str2val0(str, tab, sizeof(tab) / sizeof(tab[0]))
57 
58 
59 
60 static int str2val0_def(const char *str, struct strtab tab[], int l, int def)
61      __attribute((unused));
62 
63 static inline int
str2val0_def(const char * str,struct strtab tab[],int l,int def)64 str2val0_def(const char *str, struct strtab tab[], int l, int def)
65 {
66   int i;
67   if(str)
68     for(i = 0; i < l; i++)
69       if(!strcasecmp(str, tab[i].str))
70 	return tab[i].val;
71   return def;
72 }
73 
74 #define str2val_def(str, tab, def) \
75  str2val0_def(str, tab, sizeof(tab) / sizeof(tab[0]), def)
76 
77 
78 static const char * val2str0(int val, const struct strtab tab[], int l)
79      __attribute__((unused));
80 
81 static inline const char *
val2str0(int val,const struct strtab tab[],int l)82 val2str0(int val, const struct strtab tab[], int l)
83 {
84   int i;
85   for(i = 0; i < l; i++)
86     if(tab[i].val == val)
87       return tab[i].str;
88   return NULL;
89 }
90 
91 #define val2str(val, tab) val2str0(val, tab, sizeof(tab) / sizeof(tab[0]))
92 
93 static inline htsmsg_t *
strtab2htsmsg0(const struct strtab tab[],int n,int i18n,const char * lang)94 strtab2htsmsg0(const struct strtab tab[], int n, int i18n, const char *lang)
95 {
96   int i;
97   htsmsg_t *e, *l = htsmsg_create_list();
98   for (i = 0; i < n; i++) {
99     e = htsmsg_create_map();
100     htsmsg_add_s32(e, "key", tab[i].val);
101     htsmsg_add_str(e, "val", i18n ? tvh_gettext_lang(lang, tab[i].str) : tab[i].str);
102     htsmsg_add_msg(l, NULL, e);
103   }
104   return l;
105 }
106 
107 #define strtab2htsmsg(tab,i18n,lang) strtab2htsmsg0(tab, sizeof(tab) / sizeof(tab[0]), i18n, lang)
108 
109 static inline htsmsg_t *
strtab2htsmsg0_u32(const struct strtab_u32 tab[],uint32_t n,int i18n,const char * lang)110 strtab2htsmsg0_u32(const struct strtab_u32 tab[], uint32_t n, int i18n, const char *lang)
111 {
112   uint32_t i;
113   htsmsg_t *e, *l = htsmsg_create_list();
114   for (i = 0; i < n; i++) {
115     e = htsmsg_create_map();
116     htsmsg_add_u32(e, "key", tab[i].val);
117     htsmsg_add_str(e, "val", i18n ? tvh_gettext_lang(lang, tab[i].str) : tab[i].str);
118     htsmsg_add_msg(l, NULL, e);
119   }
120   return l;
121 }
122 
123 #define strtab2htsmsg_u32(tab,i18n,lang) strtab2htsmsg0_u32(tab, sizeof(tab) / sizeof(tab[0]), i18n, lang)
124 
125 static inline htsmsg_t *
strtab2htsmsg0_str(const struct strtab_str tab[],uint32_t n,int i18n,const char * lang)126 strtab2htsmsg0_str(const struct strtab_str tab[], uint32_t n, int i18n, const char *lang)
127 {
128   uint32_t i;
129   htsmsg_t *e, *l = htsmsg_create_list();
130   for (i = 0; i < n; i++) {
131     e = htsmsg_create_key_val(tab[i].val, i18n ? tvh_gettext_lang(lang, tab[i].str) : tab[i].str);
132     htsmsg_add_msg(l, NULL, e);
133   }
134   return l;
135 }
136 
137 #define strtab2htsmsg_str(tab,i18n,lang) strtab2htsmsg0_str(tab, sizeof(tab) / sizeof(tab[0]), i18n, lang)
138 
139 #endif /* STRTAB_H_ */
140