2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: zoom-opt.c,v 1.3 2005-01-15 19:47:15 adam Exp $
9 * \brief Implements ZOOM options handling
14 #include <yaz/xmalloc.h>
16 ZOOM_API(ZOOM_options)
17 ZOOM_options_create_with_parent (ZOOM_options parent)
19 return ZOOM_options_create_with_parent2(parent, 0);
22 ZOOM_API(ZOOM_options)
23 ZOOM_options_create (void)
25 return ZOOM_options_create_with_parent (0);
29 ZOOM_API(ZOOM_options)
30 ZOOM_options_create_with_parent2 (ZOOM_options parent1, ZOOM_options parent2)
32 ZOOM_options opt = (ZOOM_options) xmalloc (sizeof(*opt));
35 opt->callback_func = 0;
36 opt->callback_handle = 0;
38 opt->parent1= parent1;
40 (parent1->refcount)++;
41 opt->parent2= parent2;
43 (parent2->refcount)++;
48 void ZOOM_options_addref (ZOOM_options opt)
53 ZOOM_API(ZOOM_options_callback)
54 ZOOM_options_set_callback (
56 ZOOM_options_callback callback_func,
57 void *callback_handle)
59 ZOOM_options_callback callback_old;
62 callback_old = opt->callback_func;
63 opt->callback_func = callback_func;
64 opt->callback_handle = callback_handle;
69 ZOOM_options_destroy (ZOOM_options opt)
74 if (opt->refcount == 0)
76 struct ZOOM_options_entry *e;
78 ZOOM_options_destroy (opt->parent1);
79 ZOOM_options_destroy (opt->parent2);
83 struct ZOOM_options_entry *e0 = e;
94 ZOOM_options_setl (ZOOM_options opt, const char *name, const char *value,
97 struct ZOOM_options_entry **e;
102 if (!strcmp((*e)->name, name))
108 (*e)->value = (char *) xmalloc (len+1);
109 memcpy ((*e)->value, value, len);
110 (*e)->value[len] = '\0';
116 *e = (struct ZOOM_options_entry *) xmalloc (sizeof(**e));
117 (*e)->name = xstrdup (name);
121 (*e)->value = (char *) xmalloc (len+1);
122 memcpy ((*e)->value, value, len);
123 (*e)->value[len] = '\0';
129 ZOOM_options_set (ZOOM_options opt, const char *name, const char *value)
131 ZOOM_options_setl (opt, name, value, value ? strlen(value): 0);
134 ZOOM_API(const char *)
135 ZOOM_options_get (ZOOM_options opt, const char *name)
140 if (opt->callback_func)
141 v = (*opt->callback_func)(opt->callback_handle, name);
144 struct ZOOM_options_entry *e;
145 for (e = opt->entries; e; e = e->next)
146 if (!strcmp(e->name, name))
153 v = ZOOM_options_get(opt->parent1, name);
155 v = ZOOM_options_get(opt->parent2, name);
160 ZOOM_options_get_bool (ZOOM_options opt, const char *name, int defa)
162 const char *v = ZOOM_options_get (opt, name);
166 if (!strcmp (v, "1") || !strcmp(v, "T"))
172 ZOOM_options_get_int (ZOOM_options opt, const char *name, int defa)
174 const char *v = ZOOM_options_get (opt, name);
182 ZOOM_options_set_int(ZOOM_options opt, const char *name, int value)
186 sprintf (s, "%d", value);
187 ZOOM_options_set (opt, name, s);