2 * $Id: zoom-opt.c,v 1.1 2002-09-16 18:45:14 adam Exp $
4 * ZOOM layer for C, options handling
7 #include <yaz/xmalloc.h>
12 ZOOM_API(ZOOM_options)
13 ZOOM_options_create_with_parent (ZOOM_options parent)
15 return ZOOM_options_create_with_parent2(parent, 0);
18 ZOOM_API(ZOOM_options)
19 ZOOM_options_create (void)
21 return ZOOM_options_create_with_parent (0);
25 ZOOM_API(ZOOM_options)
26 ZOOM_options_create_with_parent2 (ZOOM_options parent1, ZOOM_options parent2)
28 ZOOM_options opt = (ZOOM_options) xmalloc (sizeof(*opt));
31 opt->callback_func = 0;
32 opt->callback_handle = 0;
34 opt->parent1= parent1;
36 (parent1->refcount)++;
37 opt->parent2= parent2;
39 (parent2->refcount)++;
44 void ZOOM_options_addref (ZOOM_options opt)
49 ZOOM_API(ZOOM_options_callback)
50 ZOOM_options_set_callback (
52 ZOOM_options_callback callback_func,
53 void *callback_handle)
55 ZOOM_options_callback callback_old;
58 callback_old = opt->callback_func;
59 opt->callback_func = callback_func;
60 opt->callback_handle = callback_handle;
65 ZOOM_options_destroy (ZOOM_options opt)
70 if (opt->refcount == 0)
72 struct ZOOM_options_entry *e;
74 ZOOM_options_destroy (opt->parent1);
75 ZOOM_options_destroy (opt->parent2);
79 struct ZOOM_options_entry *e0 = e;
90 ZOOM_options_setl (ZOOM_options opt, const char *name, const char *value,
93 struct ZOOM_options_entry **e;
98 if (!strcmp((*e)->name, name))
104 (*e)->value = xmalloc (len+1);
105 memcpy ((*e)->value, value, len);
106 (*e)->value[len] = '\0';
112 *e = (struct ZOOM_options_entry *) xmalloc (sizeof(**e));
113 (*e)->name = xstrdup (name);
117 (*e)->value = xmalloc (len+1);
118 memcpy ((*e)->value, value, len);
119 (*e)->value[len] = '\0';
125 ZOOM_options_set (ZOOM_options opt, const char *name, const char *value)
127 ZOOM_options_setl (opt, name, value, value ? strlen(value): 0);
130 ZOOM_API(const char *)
131 ZOOM_options_get (ZOOM_options opt, const char *name)
136 if (opt->callback_func)
137 v = (*opt->callback_func)(opt->callback_handle, name);
140 struct ZOOM_options_entry *e;
141 for (e = opt->entries; e; e = e->next)
142 if (!strcmp(e->name, name))
149 v = ZOOM_options_get(opt->parent1, name);
151 v = ZOOM_options_get(opt->parent2, name);
156 ZOOM_options_get_bool (ZOOM_options opt, const char *name, int defa)
158 const char *v = ZOOM_options_get (opt, name);
162 if (!strcmp (v, "1") || !strcmp(v, "T"))
168 ZOOM_options_get_int (ZOOM_options opt, const char *name, int defa)
170 const char *v = ZOOM_options_get (opt, name);
178 ZOOM_options_set_int(ZOOM_options opt, const char *name, int value)
182 sprintf (s, "%d", value);
183 ZOOM_options_set (opt, name, s);