parmlist: Add more tests.
[Samba/fernandojvsilva.git] / lib / util / parmlist.c
blob6658fa7e33290039511ebaff759346711381410d
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.
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/>.
19 #include "includes.h"
20 #include "../lib/util/dlinklist.h"
21 #include "../lib/util/parmlist.h"
23 struct parmlist_entry *parmlist_get(struct parmlist *ctx, const char *name)
25 struct parmlist_entry *e;
26 for (e = ctx->entries; e; e = e->next) {
27 if (strcasecmp(e->key, name) == 0)
28 return e;
31 return NULL;
34 int parmlist_get_int(struct parmlist *ctx, const char *name, int default_v)
36 struct parmlist_entry *p = parmlist_get(ctx, name);
38 if (p != NULL)
39 return strtol(p->value, NULL, 0);
41 return default_v;
44 bool parmlist_get_bool(struct parmlist *ctx, const char *name, bool default_v)
46 struct parmlist_entry *p = parmlist_get(ctx, name);
47 bool ret;
49 if (p == NULL)
50 return default_v;
52 if (!set_boolean(p->value, &ret)) {
53 DEBUG(0,("lp_bool(%s): value is not boolean!\n", p->value));
54 return default_v;
57 return ret;
60 const char *parmlist_get_string(struct parmlist *ctx, const char *name,
61 const char *default_v)
63 struct parmlist_entry *p = parmlist_get(ctx, name);
65 if (p == NULL)
66 return default_v;
68 return p->value;
71 const char **parmlist_get_string_list(struct parmlist *ctx, const char *name,
72 const char *separator)
74 struct parmlist_entry *p = parmlist_get(ctx, name);
76 if (p == NULL)
77 return NULL;
79 return (const char **)str_list_make(ctx, p->value, separator);
82 static struct parmlist_entry *parmlist_get_add(struct parmlist *ctx, const char *name)
84 struct parmlist_entry *e = parmlist_get(ctx, name);
86 if (e != NULL)
87 return e;
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;
97 int parmlist_set_string(struct parmlist *ctx, const char *name,
98 const char *value)
100 struct parmlist_entry *e = parmlist_get_add(ctx, name);
101 if (e == NULL)
102 return -1;
104 e->value = talloc_strdup(e, value);
105 return 0;