1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
7 * \brief Implements ZOOM options handling
16 #include <yaz/xmalloc.h>
18 struct ZOOM_options_entry {
21 int len; /* of `value', which may contain NULs */
22 struct ZOOM_options_entry *next;
25 struct ZOOM_options_p {
27 void *callback_handle;
28 ZOOM_options_callback callback_func;
29 struct ZOOM_options_entry *entries;
34 static void set_value(struct ZOOM_options_entry **e,
35 const char *value, int len)
41 (*e)->value = (char *) xmalloc(len+1);
42 memcpy((*e)->value, value, len);
43 (*e)->value[len] = '\0';
48 static void append_entry(struct ZOOM_options_entry **e,
49 const char *name, const char *value, int len)
51 *e = (struct ZOOM_options_entry *) xmalloc(sizeof(**e));
52 (*e)->name = xstrdup(name);
53 set_value(e, value, len);
57 ZOOM_API(ZOOM_options)
58 ZOOM_options_dup(ZOOM_options src)
64 ZOOM_options dst = ZOOM_options_create();
65 struct ZOOM_options_entry *src_e = src->entries;
66 struct ZOOM_options_entry **dst_e = &dst->entries;
70 append_entry(dst_e, src_e->name, src_e->value, src_e->len);
71 dst_e = &(*dst_e)->next;
74 dst->parent1 = ZOOM_options_dup(src->parent1);
75 dst->parent2 = ZOOM_options_dup(src->parent2);
80 ZOOM_API(ZOOM_options)
81 ZOOM_options_create_with_parent(ZOOM_options parent)
83 return ZOOM_options_create_with_parent2(parent, 0);
86 ZOOM_API(ZOOM_options)
87 ZOOM_options_create(void)
89 return ZOOM_options_create_with_parent(0);
93 ZOOM_API(ZOOM_options)
94 ZOOM_options_create_with_parent2(ZOOM_options parent1,
97 ZOOM_options opt = (ZOOM_options) xmalloc(sizeof(*opt));
100 opt->callback_func = 0;
101 opt->callback_handle = 0;
103 opt->parent1= parent1;
105 (parent1->refcount)++;
106 opt->parent2= parent2;
108 (parent2->refcount)++;
113 void ZOOM_options_addref(ZOOM_options opt)
118 ZOOM_API(ZOOM_options_callback)
119 ZOOM_options_set_callback (
121 ZOOM_options_callback callback_func,
122 void *callback_handle)
124 ZOOM_options_callback callback_old;
127 callback_old = opt->callback_func;
128 opt->callback_func = callback_func;
129 opt->callback_handle = callback_handle;
134 ZOOM_options_destroy(ZOOM_options opt)
139 if (opt->refcount == 0)
141 struct ZOOM_options_entry *e;
143 ZOOM_options_destroy(opt->parent1);
144 ZOOM_options_destroy(opt->parent2);
148 struct ZOOM_options_entry *e0 = e;
160 ZOOM_options_setl(ZOOM_options opt, const char *name, const char *value,
163 struct ZOOM_options_entry **e;
168 if (!strcmp((*e)->name, name))
171 set_value(e, value, len);
176 append_entry(e, name, value, len);
180 ZOOM_options_set(ZOOM_options opt, const char *name, const char *value)
182 ZOOM_options_setl(opt, name, value, value ? strlen(value): 0);
185 ZOOM_API(const char *)
186 ZOOM_options_getl(ZOOM_options opt, const char *name, int *lenp)
191 if (opt->callback_func)
193 v = (*opt->callback_func)(opt->callback_handle, name);
199 struct ZOOM_options_entry *e;
200 for (e = opt->entries; e; e = e->next)
201 if (!strcmp(e->name, name))
209 v = ZOOM_options_getl(opt->parent1, name, lenp);
211 v = ZOOM_options_getl(opt->parent2, name, lenp);
215 ZOOM_API(const char *)
216 ZOOM_options_get(ZOOM_options opt, const char *name)
219 return ZOOM_options_getl(opt, name, &dummy);
223 ZOOM_options_get_bool(ZOOM_options opt, const char *name, int defa)
225 const char *v = ZOOM_options_get(opt, name);
229 if (!strcmp(v, "1") || !strcmp(v, "T"))
235 ZOOM_options_get_int(ZOOM_options opt, const char *name, int defa)
237 const char *v = ZOOM_options_get(opt, name);
245 ZOOM_options_set_int(ZOOM_options opt, const char *name, int value)
249 sprintf(s, "%d", value);
250 ZOOM_options_set(opt, name, s);
255 * c-file-style: "Stroustrup"
256 * indent-tabs-mode: nil
258 * vim: shiftwidth=4 tabstop=8 expandtab