47749873a677c92894aa7db59dc686a247317f60
[util-vserver.git] / util-vserver / lib / val2text.hc
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
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; version 2 of the License.
8 //  
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //  
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #define VAL2TEXT(TYPE,SHORT)                                            \
27   ssize_t                                                               \
28   utilvserver_value2text_##SHORT(char const *str, size_t len,           \
29                                  struct Mapping_ ##SHORT const *map,    \
30                                  size_t map_len)                        \
31   {                                                                     \
32     size_t      i;                                                      \
33     if (len==0) len=strlen(str);                                        \
34                                                                         \
35     for (i=0; i<map_len; ++i)                                           \
36       if (len==map[i].len &&                                            \
37           strncasecmp(map[i].id, str, len)==0)                          \
38         return i;                                                       \
39                                                                         \
40     return -1;                                                          \
41   }
42
43 #define TEXT2VAL(TYPE,SHORT)                                            \
44   static ssize_t                                                        \
45   searchValue(TYPE val,                                                 \
46               struct Mapping_##SHORT const *map,  size_t map_len)       \
47   {                                                                     \
48     size_t              i;                                              \
49     for (i=0; i<map_len; ++i)                                           \
50       if (val == map[i].val) return i;                                  \
51     return -1;                                                          \
52   }                                                                     \
53                                                                         \
54   ssize_t                                                               \
55   utilvserver_text2value_##SHORT(TYPE *val,                             \
56                                  struct Mapping_##SHORT const *map,     \
57                                  size_t map_len)                        \
58   {                                                                     \
59     ssize_t     idx;                                                    \
60     TYPE        del_val;                                                \
61     if (*val==0)                                                        \
62       return -1;                                                        \
63                                                                         \
64     del_val = *val;                                                     \
65     idx     = searchValue(del_val, map, map_len);                       \
66                                                                         \
67     if (idx==-1) {                                                      \
68       size_t    i;                                                      \
69       for (i=0; i<sizeof(*val)*8 && (*val&(1<<i))==0; ++i) {}           \
70       del_val = (1<<i);                                                 \
71       idx     = searchValue(del_val, map, map_len);                     \
72     }                                                                   \
73     *val &= ~del_val;                                                   \
74     return idx;                                                         \
75   }