2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: zoom-opt.c,v 1.7 2007-05-04 17:16:21 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,
33 ZOOM_options opt = (ZOOM_options) xmalloc(sizeof(*opt));
36 opt->callback_func = 0;
37 opt->callback_handle = 0;
39 opt->parent1= parent1;
41 (parent1->refcount)++;
42 opt->parent2= parent2;
44 (parent2->refcount)++;
49 void ZOOM_options_addref(ZOOM_options opt)
54 ZOOM_API(ZOOM_options_callback)
55 ZOOM_options_set_callback (
57 ZOOM_options_callback callback_func,
58 void *callback_handle)
60 ZOOM_options_callback callback_old;
63 callback_old = opt->callback_func;
64 opt->callback_func = callback_func;
65 opt->callback_handle = callback_handle;
70 ZOOM_options_destroy(ZOOM_options opt)
75 if (opt->refcount == 0)
77 struct ZOOM_options_entry *e;
79 ZOOM_options_destroy(opt->parent1);
80 ZOOM_options_destroy(opt->parent2);
84 struct ZOOM_options_entry *e0 = e;
94 /* PRIVATE to ZOOM_options_setl() */
96 _set_value(struct ZOOM_options_entry **e, const char *value, int len)
102 (*e)->value = (char *) xmalloc(len+1);
103 memcpy((*e)->value, value, len);
104 (*e)->value[len] = '\0';
110 ZOOM_options_setl(ZOOM_options opt, const char *name, const char *value,
113 struct ZOOM_options_entry **e;
118 if (!strcmp((*e)->name, name))
121 _set_value(e, value, len);
126 *e = (struct ZOOM_options_entry *) xmalloc(sizeof(**e));
127 (*e)->name = xstrdup(name);
128 _set_value(e, value, len);
133 ZOOM_options_set(ZOOM_options opt, const char *name, const char *value)
135 ZOOM_options_setl(opt, name, value, value ? strlen(value): 0);
138 ZOOM_API(const char *)
139 ZOOM_options_getl(ZOOM_options opt, const char *name, int *lenp)
144 if (opt->callback_func)
145 v = (*opt->callback_func)(opt->callback_handle, name);
148 struct ZOOM_options_entry *e;
149 for (e = opt->entries; e; e = e->next)
150 if (!strcmp(e->name, name))
158 v = ZOOM_options_getl(opt->parent1, name, lenp);
160 v = ZOOM_options_getl(opt->parent2, name, lenp);
164 ZOOM_API(const char *)
165 ZOOM_options_get(ZOOM_options opt, const char *name)
168 return ZOOM_options_getl(opt, name, &dummy);
172 ZOOM_options_get_bool(ZOOM_options opt, const char *name, int defa)
174 const char *v = ZOOM_options_get(opt, name);
178 if (!strcmp(v, "1") || !strcmp(v, "T"))
184 ZOOM_options_get_int(ZOOM_options opt, const char *name, int defa)
186 const char *v = ZOOM_options_get(opt, name);
194 ZOOM_options_set_int(ZOOM_options opt, const char *name, int value)
198 sprintf(s, "%d", value);
199 ZOOM_options_set(opt, name, s);
204 * indent-tabs-mode: nil
206 * vim: shiftwidth=4 tabstop=8 expandtab