2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: zoom-opt.c,v 1.9 2008-01-07 17:32:49 adam Exp $
9 * \brief Implements ZOOM options handling
14 #include <yaz/xmalloc.h>
16 static void set_value(struct ZOOM_options_entry **e,
17 const char *value, int len)
23 (*e)->value = (char *) xmalloc(len+1);
24 memcpy((*e)->value, value, len);
25 (*e)->value[len] = '\0';
30 static void append_entry(struct ZOOM_options_entry **e,
31 const char *name, const char *value, int len)
33 *e = (struct ZOOM_options_entry *) xmalloc(sizeof(**e));
34 (*e)->name = xstrdup(name);
35 set_value(e, value, len);
39 ZOOM_API(ZOOM_options)
40 ZOOM_options_dup(ZOOM_options src)
46 ZOOM_options dst = ZOOM_options_create();
47 struct ZOOM_options_entry *src_e = src->entries;
48 struct ZOOM_options_entry **dst_e = &dst->entries;
52 append_entry(dst_e, src_e->name, src_e->value, src_e->len);
53 dst_e = &(*dst_e)->next;
56 dst->parent1 = ZOOM_options_dup(src->parent1);
57 dst->parent2 = ZOOM_options_dup(src->parent2);
62 ZOOM_API(ZOOM_options)
63 ZOOM_options_create_with_parent(ZOOM_options parent)
65 return ZOOM_options_create_with_parent2(parent, 0);
68 ZOOM_API(ZOOM_options)
69 ZOOM_options_create(void)
71 return ZOOM_options_create_with_parent(0);
75 ZOOM_API(ZOOM_options)
76 ZOOM_options_create_with_parent2(ZOOM_options parent1,
79 ZOOM_options opt = (ZOOM_options) xmalloc(sizeof(*opt));
82 opt->callback_func = 0;
83 opt->callback_handle = 0;
85 opt->parent1= parent1;
87 (parent1->refcount)++;
88 opt->parent2= parent2;
90 (parent2->refcount)++;
95 void ZOOM_options_addref(ZOOM_options opt)
100 ZOOM_API(ZOOM_options_callback)
101 ZOOM_options_set_callback (
103 ZOOM_options_callback callback_func,
104 void *callback_handle)
106 ZOOM_options_callback callback_old;
109 callback_old = opt->callback_func;
110 opt->callback_func = callback_func;
111 opt->callback_handle = callback_handle;
116 ZOOM_options_destroy(ZOOM_options opt)
121 if (opt->refcount == 0)
123 struct ZOOM_options_entry *e;
125 ZOOM_options_destroy(opt->parent1);
126 ZOOM_options_destroy(opt->parent2);
130 struct ZOOM_options_entry *e0 = e;
142 ZOOM_options_setl(ZOOM_options opt, const char *name, const char *value,
145 struct ZOOM_options_entry **e;
150 if (!strcmp((*e)->name, name))
153 set_value(e, value, len);
158 append_entry(e, name, value, len);
162 ZOOM_options_set(ZOOM_options opt, const char *name, const char *value)
164 ZOOM_options_setl(opt, name, value, value ? strlen(value): 0);
167 ZOOM_API(const char *)
168 ZOOM_options_getl(ZOOM_options opt, const char *name, int *lenp)
173 if (opt->callback_func)
175 v = (*opt->callback_func)(opt->callback_handle, name);
181 struct ZOOM_options_entry *e;
182 for (e = opt->entries; e; e = e->next)
183 if (!strcmp(e->name, name))
191 v = ZOOM_options_getl(opt->parent1, name, lenp);
193 v = ZOOM_options_getl(opt->parent2, name, lenp);
197 ZOOM_API(const char *)
198 ZOOM_options_get(ZOOM_options opt, const char *name)
201 return ZOOM_options_getl(opt, name, &dummy);
205 ZOOM_options_get_bool(ZOOM_options opt, const char *name, int defa)
207 const char *v = ZOOM_options_get(opt, name);
211 if (!strcmp(v, "1") || !strcmp(v, "T"))
217 ZOOM_options_get_int(ZOOM_options opt, const char *name, int defa)
219 const char *v = ZOOM_options_get(opt, name);
227 ZOOM_options_set_int(ZOOM_options opt, const char *name, int value)
231 sprintf(s, "%d", value);
232 ZOOM_options_set(opt, name, s);
237 * indent-tabs-mode: nil
239 * vim: shiftwidth=4 tabstop=8 expandtab