1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements ZOOM options handling
12 #include <yaz/xmalloc.h>
14 struct ZOOM_options_entry {
17 int len; /* of `value', which may contain NULs */
18 struct ZOOM_options_entry *next;
21 struct ZOOM_options_p {
23 void *callback_handle;
24 ZOOM_options_callback callback_func;
25 struct ZOOM_options_entry *entries;
30 static void set_value(struct ZOOM_options_entry **e,
31 const char *value, int len)
37 (*e)->value = (char *) xmalloc(len+1);
38 memcpy((*e)->value, value, len);
39 (*e)->value[len] = '\0';
44 static void append_entry(struct ZOOM_options_entry **e,
45 const char *name, const char *value, int len)
47 *e = (struct ZOOM_options_entry *) xmalloc(sizeof(**e));
48 (*e)->name = xstrdup(name);
49 set_value(e, value, len);
53 ZOOM_API(ZOOM_options)
54 ZOOM_options_dup(ZOOM_options src)
60 ZOOM_options dst = ZOOM_options_create();
61 struct ZOOM_options_entry *src_e = src->entries;
62 struct ZOOM_options_entry **dst_e = &dst->entries;
66 append_entry(dst_e, src_e->name, src_e->value, src_e->len);
67 dst_e = &(*dst_e)->next;
70 dst->parent1 = ZOOM_options_dup(src->parent1);
71 dst->parent2 = ZOOM_options_dup(src->parent2);
76 ZOOM_API(ZOOM_options)
77 ZOOM_options_create_with_parent(ZOOM_options parent)
79 return ZOOM_options_create_with_parent2(parent, 0);
82 ZOOM_API(ZOOM_options)
83 ZOOM_options_create(void)
85 return ZOOM_options_create_with_parent(0);
89 ZOOM_API(ZOOM_options)
90 ZOOM_options_create_with_parent2(ZOOM_options parent1,
93 ZOOM_options opt = (ZOOM_options) xmalloc(sizeof(*opt));
96 opt->callback_func = 0;
97 opt->callback_handle = 0;
99 opt->parent1= parent1;
101 (parent1->refcount)++;
102 opt->parent2= parent2;
104 (parent2->refcount)++;
109 void ZOOM_options_addref(ZOOM_options opt)
114 ZOOM_API(ZOOM_options_callback)
115 ZOOM_options_set_callback (
117 ZOOM_options_callback callback_func,
118 void *callback_handle)
120 ZOOM_options_callback callback_old;
123 callback_old = opt->callback_func;
124 opt->callback_func = callback_func;
125 opt->callback_handle = callback_handle;
130 ZOOM_options_destroy(ZOOM_options opt)
135 if (opt->refcount == 0)
137 struct ZOOM_options_entry *e;
139 ZOOM_options_destroy(opt->parent1);
140 ZOOM_options_destroy(opt->parent2);
144 struct ZOOM_options_entry *e0 = e;
156 ZOOM_options_setl(ZOOM_options opt, const char *name, const char *value,
159 struct ZOOM_options_entry **e;
164 if (!strcmp((*e)->name, name))
167 set_value(e, value, len);
172 append_entry(e, name, value, len);
176 ZOOM_options_set(ZOOM_options opt, const char *name, const char *value)
178 ZOOM_options_setl(opt, name, value, value ? strlen(value): 0);
181 ZOOM_API(const char *)
182 ZOOM_options_getl(ZOOM_options opt, const char *name, int *lenp)
187 if (opt->callback_func)
189 v = (*opt->callback_func)(opt->callback_handle, name);
195 struct ZOOM_options_entry *e;
196 for (e = opt->entries; e; e = e->next)
197 if (!strcmp(e->name, name))
205 v = ZOOM_options_getl(opt->parent1, name, lenp);
207 v = ZOOM_options_getl(opt->parent2, name, lenp);
211 ZOOM_API(const char *)
212 ZOOM_options_get(ZOOM_options opt, const char *name)
215 return ZOOM_options_getl(opt, name, &dummy);
219 ZOOM_options_get_bool(ZOOM_options opt, const char *name, int defa)
221 const char *v = ZOOM_options_get(opt, name);
225 if (!strcmp(v, "1") || !strcmp(v, "T"))
231 ZOOM_options_get_int(ZOOM_options opt, const char *name, int defa)
233 const char *v = ZOOM_options_get(opt, name);
241 ZOOM_options_set_int(ZOOM_options opt, const char *name, int value)
245 sprintf(s, "%d", value);
246 ZOOM_options_set(opt, name, s);
251 * c-file-style: "Stroustrup"
252 * indent-tabs-mode: nil
254 * vim: shiftwidth=4 tabstop=8 expandtab