lib/util: Protect time_basic.h against multiple inclusion
[kai/samba-autobuild/.git] / lib / util / parmlist.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Copyright (C) Jelmer Vernooij                       2009
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 #include "includes.h"
20 #include "../lib/util/dlinklist.h"
21 #include "../lib/util/parmlist.h"
22
23 #undef strcasecmp
24
25 struct parmlist_entry *parmlist_get(struct parmlist *ctx, const char *name)
26 {
27         struct parmlist_entry *e;
28         for (e = ctx->entries; e; e = e->next) {
29                 if (strcasecmp(e->key, name) == 0)
30                         return e;
31         }
32
33         return NULL;
34 }
35
36 int parmlist_get_int(struct parmlist *ctx, const char *name, int default_v)
37 {
38         struct parmlist_entry *p = parmlist_get(ctx, name);
39
40         if (p != NULL)
41                 return strtol(p->value, NULL, 0); 
42
43         return default_v;
44 }
45
46 bool parmlist_get_bool(struct parmlist *ctx, const char *name, bool default_v)
47 {
48         struct parmlist_entry *p = parmlist_get(ctx, name);
49         bool ret;
50
51         if (p == NULL)
52                 return default_v;
53
54         if (!set_boolean(p->value, &ret)) {
55                 DEBUG(0,("lp_bool(%s): value is not boolean!\n", p->value));
56                 return default_v;
57         }
58
59         return ret;
60 }
61
62 const char *parmlist_get_string(struct parmlist *ctx, const char *name, 
63                                                                 const char *default_v)
64 {
65         struct parmlist_entry *p = parmlist_get(ctx, name);
66
67         if (p == NULL)
68                 return default_v;
69
70         return p->value;
71 }
72
73 const char **parmlist_get_string_list(struct parmlist *ctx, const char *name, 
74                                                                           const char *separator)
75 {
76         struct parmlist_entry *p = parmlist_get(ctx, name);
77         char **l;
78
79         if (p == NULL) {
80                 return NULL;
81         }
82
83         l = str_list_make(ctx, p->value, separator);
84         return discard_const_p(const char *, l);
85 }
86
87 static struct parmlist_entry *parmlist_get_add(struct parmlist *ctx, const char *name)
88 {
89         struct parmlist_entry *e = parmlist_get(ctx, name);
90
91         if (e != NULL)
92                 return e;
93
94         e = talloc(ctx, struct parmlist_entry);
95         if (e == NULL)
96                 return NULL;
97         e->key = talloc_strdup(e, name);
98         DLIST_ADD(ctx->entries, e);
99         return e;
100 }
101
102 int parmlist_set_string(struct parmlist *ctx, const char *name, 
103                                                 const char *value)
104 {
105         struct parmlist_entry *e = parmlist_get_add(ctx, name);
106         if (e == NULL)
107                 return -1;
108
109         e->value = talloc_strdup(e, value);
110         return 0;
111 }