2 * $Id: zoom-opt.c,v 1.3 2003-01-06 08:20:29 adam Exp $
4 * ZOOM layer for C, options handling
9 #include <yaz/xmalloc.h>
11 ZOOM_API(ZOOM_options)
12 ZOOM_options_create_with_parent (ZOOM_options parent)
14 return ZOOM_options_create_with_parent2(parent, 0);
17 ZOOM_API(ZOOM_options)
18 ZOOM_options_create (void)
20 return ZOOM_options_create_with_parent (0);
24 ZOOM_API(ZOOM_options)
25 ZOOM_options_create_with_parent2 (ZOOM_options parent1, ZOOM_options parent2)
27 ZOOM_options opt = (ZOOM_options) xmalloc (sizeof(*opt));
30 opt->callback_func = 0;
31 opt->callback_handle = 0;
33 opt->parent1= parent1;
35 (parent1->refcount)++;
36 opt->parent2= parent2;
38 (parent2->refcount)++;
43 void ZOOM_options_addref (ZOOM_options opt)
48 ZOOM_API(ZOOM_options_callback)
49 ZOOM_options_set_callback (
51 ZOOM_options_callback callback_func,
52 void *callback_handle)
54 ZOOM_options_callback callback_old;
57 callback_old = opt->callback_func;
58 opt->callback_func = callback_func;
59 opt->callback_handle = callback_handle;
64 ZOOM_options_destroy (ZOOM_options opt)
69 if (opt->refcount == 0)
71 struct ZOOM_options_entry *e;
73 ZOOM_options_destroy (opt->parent1);
74 ZOOM_options_destroy (opt->parent2);
78 struct ZOOM_options_entry *e0 = e;
89 ZOOM_options_setl (ZOOM_options opt, const char *name, const char *value,
92 struct ZOOM_options_entry **e;
97 if (!strcmp((*e)->name, name))
103 (*e)->value = (char *) xmalloc (len+1);
104 memcpy ((*e)->value, value, len);
105 (*e)->value[len] = '\0';
111 *e = (struct ZOOM_options_entry *) xmalloc (sizeof(**e));
112 (*e)->name = xstrdup (name);
116 (*e)->value = (char *) xmalloc (len+1);
117 memcpy ((*e)->value, value, len);
118 (*e)->value[len] = '\0';
124 ZOOM_options_set (ZOOM_options opt, const char *name, const char *value)
126 ZOOM_options_setl (opt, name, value, value ? strlen(value): 0);
129 ZOOM_API(const char *)
130 ZOOM_options_get (ZOOM_options opt, const char *name)
135 if (opt->callback_func)
136 v = (*opt->callback_func)(opt->callback_handle, name);
139 struct ZOOM_options_entry *e;
140 for (e = opt->entries; e; e = e->next)
141 if (!strcmp(e->name, name))
148 v = ZOOM_options_get(opt->parent1, name);
150 v = ZOOM_options_get(opt->parent2, name);
155 ZOOM_options_get_bool (ZOOM_options opt, const char *name, int defa)
157 const char *v = ZOOM_options_get (opt, name);
161 if (!strcmp (v, "1") || !strcmp(v, "T"))
167 ZOOM_options_get_int (ZOOM_options opt, const char *name, int defa)
169 const char *v = ZOOM_options_get (opt, name);
177 ZOOM_options_set_int(ZOOM_options opt, const char *name, int value)
181 sprintf (s, "%d", value);
182 ZOOM_options_set (opt, name, s);