libutil: Add separate utility code for dealing with settings as a
[metze/samba/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 const char *parmlist_get_string(struct parmlist *ctx, const char *name, const char *default_v)
45 {
46         struct parmlist_entry *p = parmlist_get(ctx, name);
47
48         if (p == NULL)
49                 return default_v;
50
51         return p->value;
52 }
53
54 const char **parmlist_get_string_list(struct parmlist *ctx, const char *name, const char *separator)
55 {
56         struct parmlist_entry *p = parmlist_get(ctx, name);
57
58         if (p == NULL)
59                 return NULL;
60
61         return (const char **)str_list_make(ctx, p->value, separator);
62 }