added doxygen annnotations
[util-vserver.git] / util-vserver / lib / listparser.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 #include <strings.h>
26
27 #define TONUMBER_uint64(S,E,B)          strtoll(S,E,B)
28 #define TONUMBER_uint32(S,E,B)          strtol (S,E,B)
29
30 #define ISNUMBER(TYPE,SHORT)                                    \
31   static inline ALWAYSINLINE bool                               \
32   isNumber_##SHORT(char const *str,TYPE *res, char end_chr)     \
33   {                                                             \
34     char        *err_ptr;                                       \
35     *res = TONUMBER_##SHORT(str, &err_ptr, 0);                  \
36     return err_ptr>str && *err_ptr==end_chr;                    \
37   }
38
39
40 #define LISTPARSER(TYPE,SHORT)                                          \
41   ISNUMBER(TYPE,SHORT)                                                  \
42   int                                                                   \
43   utilvserver_listparser_ ## SHORT(char const *str, size_t len,         \
44                                    char const **err_ptr,                \
45                                    size_t *err_len,                     \
46                                    TYPE *flag,                          \
47                                    TYPE *mask,                          \
48                                    TYPE (*func)(char const *, size_t))  \
49   {                                                                     \
50     if (len==0) len = strlen(str);                                      \
51     for (;len>0;) {                                                     \
52       char const        *ptr = strchr(str, ',');                        \
53       size_t            cnt;                                            \
54       TYPE              tmp;                                            \
55       bool              is_neg;                                         \
56                                                                         \
57       is_neg = *str=='!' || *str=='~';                                  \
58       if (is_neg) {                                                     \
59         ++str;                                                          \
60         --len;                                                          \
61       }                                                                 \
62                                                                         \
63       cnt = ptr ? (size_t)(ptr-str) : len;                              \
64       if (cnt>=len) { cnt=len; len=0; }                                 \
65       else len-=(cnt+1);                                                \
66                                                                         \
67       if (cnt==0)                                                       \
68         tmp = 0;                                                        \
69       else if (strncasecmp(str,"all",cnt)==0 ||                         \
70                strncasecmp(str,"any",cnt)==0)                           \
71         tmp = ~(TYPE)(0);                                               \
72       else if (!isNumber_##SHORT(str, &tmp, str[cnt]))                  \
73         tmp = (*func)(str,cnt);                                         \
74                                                                         \
75       if (tmp!=0) {                                                     \
76         if (!is_neg) *flag |=  tmp;                                     \
77         else         *flag &= ~tmp;                                     \
78         *mask |= tmp;                                                   \
79       }                                                                 \
80       else {                                                            \
81         if (err_ptr) *err_ptr = str;                                    \
82         if (err_len) *err_len = cnt;                                    \
83         return -1;                                                      \
84       }                                                                 \
85                                                                         \
86       if (ptr==0) break;                                                \
87       str = ptr+1;                                                      \
88     }                                                                   \
89                                                                         \
90     if (err_ptr) *err_ptr = 0;                                          \
91     if (err_len) *err_len = 0;                                          \
92     return 0;                                                           \
93   }