2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: zoom-opt.c,v 1.5 2005-10-17 12:28:20 mike 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;
93 /* PRIVATE to ZOOM_options_setl() */
95 _set_value(struct ZOOM_options_entry **e, const char *value, int len)
101 (*e)->value = (char *) xmalloc (len+1);
102 memcpy ((*e)->value, value, len);
103 (*e)->value[len] = '\0';
109 ZOOM_options_setl (ZOOM_options opt, const char *name, const char *value,
112 struct ZOOM_options_entry **e;
117 if (!strcmp((*e)->name, name))
120 _set_value(e, value, len);
125 *e = (struct ZOOM_options_entry *) xmalloc (sizeof(**e));
126 (*e)->name = xstrdup (name);
127 _set_value(e, value, len);
132 ZOOM_options_set (ZOOM_options opt, const char *name, const char *value)
134 ZOOM_options_setl (opt, name, value, value ? strlen(value): 0);
137 ZOOM_API(const char *)
138 ZOOM_options_getl (ZOOM_options opt, const char *name, int *lenp)
143 if (opt->callback_func)
144 v = (*opt->callback_func)(opt->callback_handle, name);
147 struct ZOOM_options_entry *e;
148 for (e = opt->entries; e; e = e->next)
149 if (!strcmp(e->name, name))
157 v = ZOOM_options_getl(opt->parent1, name, lenp);
159 v = ZOOM_options_getl(opt->parent2, name, lenp);
163 ZOOM_API(const char *)
164 ZOOM_options_get (ZOOM_options opt, const char *name)
167 return ZOOM_options_getl(opt, name, &dummy);
171 ZOOM_options_get_bool (ZOOM_options opt, const char *name, int defa)
173 const char *v = ZOOM_options_get (opt, name);
177 if (!strcmp (v, "1") || !strcmp(v, "T"))
183 ZOOM_options_get_int (ZOOM_options opt, const char *name, int defa)
185 const char *v = ZOOM_options_get (opt, name);
193 ZOOM_options_set_int(ZOOM_options opt, const char *name, int value)
197 sprintf (s, "%d", value);
198 ZOOM_options_set (opt, name, s);
203 * indent-tabs-mode: nil
205 * vim: shiftwidth=4 tabstop=8 expandtab