1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements ZOOM options handling
12 #include <yaz/xmalloc.h>
14 static void set_value(struct ZOOM_options_entry **e,
15 const char *value, int len)
21 (*e)->value = (char *) xmalloc(len+1);
22 memcpy((*e)->value, value, len);
23 (*e)->value[len] = '\0';
28 static void append_entry(struct ZOOM_options_entry **e,
29 const char *name, const char *value, int len)
31 *e = (struct ZOOM_options_entry *) xmalloc(sizeof(**e));
32 (*e)->name = xstrdup(name);
33 set_value(e, value, len);
37 ZOOM_API(ZOOM_options)
38 ZOOM_options_dup(ZOOM_options src)
44 ZOOM_options dst = ZOOM_options_create();
45 struct ZOOM_options_entry *src_e = src->entries;
46 struct ZOOM_options_entry **dst_e = &dst->entries;
50 append_entry(dst_e, src_e->name, src_e->value, src_e->len);
51 dst_e = &(*dst_e)->next;
54 dst->parent1 = ZOOM_options_dup(src->parent1);
55 dst->parent2 = ZOOM_options_dup(src->parent2);
60 ZOOM_API(ZOOM_options)
61 ZOOM_options_create_with_parent(ZOOM_options parent)
63 return ZOOM_options_create_with_parent2(parent, 0);
66 ZOOM_API(ZOOM_options)
67 ZOOM_options_create(void)
69 return ZOOM_options_create_with_parent(0);
73 ZOOM_API(ZOOM_options)
74 ZOOM_options_create_with_parent2(ZOOM_options parent1,
77 ZOOM_options opt = (ZOOM_options) xmalloc(sizeof(*opt));
80 opt->callback_func = 0;
81 opt->callback_handle = 0;
83 opt->parent1= parent1;
85 (parent1->refcount)++;
86 opt->parent2= parent2;
88 (parent2->refcount)++;
93 void ZOOM_options_addref(ZOOM_options opt)
98 ZOOM_API(ZOOM_options_callback)
99 ZOOM_options_set_callback (
101 ZOOM_options_callback callback_func,
102 void *callback_handle)
104 ZOOM_options_callback callback_old;
107 callback_old = opt->callback_func;
108 opt->callback_func = callback_func;
109 opt->callback_handle = callback_handle;
114 ZOOM_options_destroy(ZOOM_options opt)
119 if (opt->refcount == 0)
121 struct ZOOM_options_entry *e;
123 ZOOM_options_destroy(opt->parent1);
124 ZOOM_options_destroy(opt->parent2);
128 struct ZOOM_options_entry *e0 = e;
140 ZOOM_options_setl(ZOOM_options opt, const char *name, const char *value,
143 struct ZOOM_options_entry **e;
148 if (!strcmp((*e)->name, name))
151 set_value(e, value, len);
156 append_entry(e, name, value, len);
160 ZOOM_options_set(ZOOM_options opt, const char *name, const char *value)
162 ZOOM_options_setl(opt, name, value, value ? strlen(value): 0);
165 ZOOM_API(const char *)
166 ZOOM_options_getl(ZOOM_options opt, const char *name, int *lenp)
171 if (opt->callback_func)
173 v = (*opt->callback_func)(opt->callback_handle, name);
179 struct ZOOM_options_entry *e;
180 for (e = opt->entries; e; e = e->next)
181 if (!strcmp(e->name, name))
189 v = ZOOM_options_getl(opt->parent1, name, lenp);
191 v = ZOOM_options_getl(opt->parent2, name, lenp);
195 ZOOM_API(const char *)
196 ZOOM_options_get(ZOOM_options opt, const char *name)
199 return ZOOM_options_getl(opt, name, &dummy);
203 ZOOM_options_get_bool(ZOOM_options opt, const char *name, int defa)
205 const char *v = ZOOM_options_get(opt, name);
209 if (!strcmp(v, "1") || !strcmp(v, "T"))
215 ZOOM_options_get_int(ZOOM_options opt, const char *name, int defa)
217 const char *v = ZOOM_options_get(opt, name);
225 ZOOM_options_set_int(ZOOM_options opt, const char *name, int value)
229 sprintf(s, "%d", value);
230 ZOOM_options_set(opt, name, s);
235 * c-file-style: "Stroustrup"
236 * indent-tabs-mode: nil
238 * vim: shiftwidth=4 tabstop=8 expandtab