2 * Unix SMB/CIFS implementation.
3 * Copyright (C) Jelmer Vernooij 2005
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 2 of the License, or
8 * (at your option) any later version.
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, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "dlinklist.h"
22 #include "param/generic.h"
24 struct param_section *param_get_section(struct param_context *ctx, const char *name)
26 struct param_section *sect;
31 for (sect = ctx->sections; sect; sect = sect->next) {
32 if (!strcasecmp_m(sect->name, name))
39 struct param *param_section_get (struct param_section *section, const char *name)
43 for (p = section->parameters; p; p = p->next) {
44 if (strcasecmp(p->name, name) == 0)
51 struct param *param_get (struct param_context *ctx, const char *section_name, const char *name)
53 struct param_section *section = param_get_section(ctx, section_name);
57 return param_section_get(section, name);
60 /* Look up parameter. If it is not found, add it */
61 static struct param *param_get_add(struct param_context *ctx, const char *section_name, const char *name)
63 struct param_section *section;
66 section = param_get_section(ctx, section_name);
68 if (section == NULL) {
69 section = talloc_zero(ctx, struct param_section);
70 section->name = talloc_strdup(section, section_name);
71 DLIST_ADD(ctx->sections, section);
74 p = param_section_get(section, name);
76 p = talloc_zero(section, struct param);
77 p->name = talloc_strdup(p, name);
78 DLIST_ADD(section->parameters, p);
84 const char *param_get_string(struct param_context *ctx, const char *section, const char *param)
86 struct param *p = param_get(ctx, section, param);
94 void param_set_string(struct param_context *ctx, const char *section, const char *param, const char *value)
96 struct param *p = param_get_add(ctx, section, param);
98 p->value = talloc_strdup(p, value);
101 const char **param_get_string_list(struct param_context *ctx, const char *section, const char *param,
102 const char *separator)
104 struct param *p = param_get(ctx, section, param);
109 if (p->list_value == NULL) {
110 p->list_value = str_list_make(ctx, p->value, separator);
113 return p->list_value;
116 void param_set_string_list(struct param_context *ctx, const char *section, const char *param, const char **list)
118 struct param *p = param_get_add(ctx, section, param);
120 p->value = str_list_join(p, list, ' ');
121 p->list_value = str_list_copy(p, list);
124 int param_get_int(struct param_context *ctx, const char *section, const char *param, int default_v)
126 const char *value = param_get_string(ctx, section, param);
129 return strtol(value, NULL, 0);
134 void param_set_int(struct param_context *ctx, const char *section, const char *param, int value)
136 struct param *p = param_get_add(ctx, section, param);
138 p->value = talloc_asprintf(p, "%d", value);
141 unsigned long param_get_ulong(struct param_context *ctx, const char *section, const char *param, unsigned long default_v)
143 const char *value = param_get_string(ctx, section, param);
146 return strtoul(value, NULL, 0);
151 void param_set_ulong(struct param_context *ctx, const char *section, const char *name, unsigned long value)
153 struct param *p = param_get_add(ctx, section, name);
155 p->value = talloc_asprintf(p, "%lu", value);
158 static BOOL param_sfunc (const char *name, void *_ctx)
160 struct param_context *ctx = _ctx;
161 struct param_section *section = param_get_section(ctx, name);
163 if (section == NULL) {
164 section = talloc_zero(ctx, struct param_section);
165 section->name = talloc_strdup(section, name);
167 DLIST_ADD(ctx->sections, section);
170 DLIST_PROMOTE(ctx->sections, section);
177 static BOOL param_pfunc (const char *name, const char *value, void *_ctx)
179 struct param_context *ctx = _ctx;
180 struct param *p = param_section_get(ctx->sections, name);
183 p = talloc_zero(ctx->sections, struct param);
184 p->name = talloc_strdup(p, name);
185 p->value = talloc_strdup(p, value);
186 DLIST_ADD(ctx->sections->parameters, p);
187 } else { /* Replace current value */
188 talloc_free(p->value);
189 p->value = talloc_strdup(p, value);
195 struct param_context *param_read(TALLOC_CTX *mem_ctx, const char *fn)
197 struct param_context *ctx = talloc_zero(mem_ctx, struct param_context);
199 ctx->sections = talloc_zero(ctx, struct param_section);
200 ctx->sections->name = talloc_strdup(ctx->sections, "global");
202 if (!pm_process( fn, param_sfunc, param_pfunc, ctx)) {
210 int param_write(FILE *file, struct param_context *ctx)
212 struct param_section *section;
220 for (section = ctx->sections; section; section = section->next) {
223 fprintf(file, "[%s]\n", section->name);
224 for (param = section->parameters; param; param = param->next) {
225 fprintf(file, "\t%s = %s\n", param->name, param->value);