tdb_store: check returns for 0, not -1.
[ira/wip.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 struct parmlist_entry *parmlist_get(struct parmlist *ctx, const char *name)
24 {
25         struct parmlist_entry *e;
26         for (e = ctx->entries; e; e = e->next) {
27                 if (strcasecmp(e->key, name) == 0)
28                         return e;
29         }
30
31         return NULL;
32 }
33
34 int parmlist_get_int(struct parmlist *ctx, const char *name, int default_v)
35 {
36         struct parmlist_entry *p = parmlist_get(ctx, name);
37
38         if (p != NULL)
39                 return strtol(p->value, NULL, 0); 
40
41         return default_v;
42 }
43
44 bool parmlist_get_bool(struct parmlist *ctx, const char *name, bool default_v)
45 {
46         struct parmlist_entry *p = parmlist_get(ctx, name);
47         bool ret;
48
49         if (p == NULL)
50                 return default_v;
51
52         if (!set_boolean(p->value, &ret)) {
53                 DEBUG(0,("lp_bool(%s): value is not boolean!\n", p->value));
54                 return default_v;
55         }
56
57         return ret;
58 }
59
60 const char *parmlist_get_string(struct parmlist *ctx, const char *name, 
61                                                                 const char *default_v)
62 {
63         struct parmlist_entry *p = parmlist_get(ctx, name);
64
65         if (p == NULL)
66                 return default_v;
67
68         return p->value;
69 }
70
71 const char **parmlist_get_string_list(struct parmlist *ctx, const char *name, 
72                                                                           const char *separator)
73 {
74         struct parmlist_entry *p = parmlist_get(ctx, name);
75
76         if (p == NULL)
77                 return NULL;
78
79         return (const char **)str_list_make(ctx, p->value, separator);
80 }
81
82 static struct parmlist_entry *parmlist_get_add(struct parmlist *ctx, const char *name)
83 {
84         struct parmlist_entry *e = parmlist_get(ctx, name);
85
86         if (e != NULL)
87                 return e;
88
89         e = talloc(ctx, struct parmlist_entry);
90         if (e == NULL)
91                 return NULL;
92         e->key = talloc_strdup(e, name);
93         DLIST_ADD(ctx->entries, e);
94         return e;
95 }
96
97 int parmlist_set_string(struct parmlist *ctx, const char *name, 
98                                                 const char *value)
99 {
100         struct parmlist_entry *e = parmlist_get_add(ctx, name);
101         if (e == NULL)
102                 return -1;
103
104         e->value = talloc_strdup(e, value);
105         return 0;
106 }