lib/util: Add back control of mmap and hash size in tdb for top level build
[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 #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
78         if (p == NULL)
79                 return NULL;
80
81         return (const char **)str_list_make(ctx, p->value, separator);
82 }
83
84 static struct parmlist_entry *parmlist_get_add(struct parmlist *ctx, const char *name)
85 {
86         struct parmlist_entry *e = parmlist_get(ctx, name);
87
88         if (e != NULL)
89                 return e;
90
91         e = talloc(ctx, struct parmlist_entry);
92         if (e == NULL)
93                 return NULL;
94         e->key = talloc_strdup(e, name);
95         DLIST_ADD(ctx->entries, e);
96         return e;
97 }
98
99 int parmlist_set_string(struct parmlist *ctx, const char *name, 
100                                                 const char *value)
101 {
102         struct parmlist_entry *e = parmlist_get_add(ctx, name);
103         if (e == NULL)
104                 return -1;
105
106         e->value = talloc_strdup(e, value);
107         return 0;
108 }