SRW, CQL, 2003
[yaz-moved-to-github.git] / zutil / zoom-opt.c
1 /*
2  * $Id: zoom-opt.c,v 1.3 2003-01-06 08:20:29 adam Exp $
3  *
4  * ZOOM layer for C, options handling
5  */
6 #include <assert.h>
7 #include "zoom-p.h"
8
9 #include <yaz/xmalloc.h>
10
11 ZOOM_API(ZOOM_options)
12 ZOOM_options_create_with_parent (ZOOM_options parent)
13 {
14     return ZOOM_options_create_with_parent2(parent, 0);
15 }
16
17 ZOOM_API(ZOOM_options)
18 ZOOM_options_create (void)
19 {
20     return ZOOM_options_create_with_parent (0);
21 }
22
23
24 ZOOM_API(ZOOM_options)
25 ZOOM_options_create_with_parent2 (ZOOM_options parent1, ZOOM_options parent2)
26 {
27     ZOOM_options opt = (ZOOM_options) xmalloc (sizeof(*opt));
28
29     opt->refcount = 1;
30     opt->callback_func = 0;
31     opt->callback_handle = 0;
32     opt->entries = 0;
33     opt->parent1= parent1;
34     if (parent1)
35         (parent1->refcount)++;
36     opt->parent2= parent2;
37     if (parent2)
38         (parent2->refcount)++;
39     return opt;
40 }
41
42
43 void ZOOM_options_addref (ZOOM_options opt)
44 {
45     (opt->refcount)++;
46 }
47
48 ZOOM_API(ZOOM_options_callback)
49 ZOOM_options_set_callback (
50     ZOOM_options opt,
51     ZOOM_options_callback callback_func,
52     void *callback_handle)
53 {
54     ZOOM_options_callback callback_old;
55
56     assert (opt);
57     callback_old = opt->callback_func;
58     opt->callback_func = callback_func;
59     opt->callback_handle = callback_handle;
60     return callback_old;
61 }
62
63 ZOOM_API(void)
64 ZOOM_options_destroy (ZOOM_options opt)
65 {
66     if (!opt)
67         return;
68     (opt->refcount)--;
69     if (opt->refcount == 0)
70     {
71         struct ZOOM_options_entry *e;
72         
73         ZOOM_options_destroy (opt->parent1);
74         ZOOM_options_destroy (opt->parent2);
75         e = opt->entries;
76         while (e)
77         {
78             struct ZOOM_options_entry *e0 = e;
79             xfree (e->name);
80             xfree (e->value);
81             e = e->next;
82             xfree (e0);
83         }
84         xfree (opt);
85     }
86 }
87
88 ZOOM_API(void)
89 ZOOM_options_setl (ZOOM_options opt, const char *name, const char *value,
90                    int len)
91 {
92     struct ZOOM_options_entry **e;
93
94     e = &opt->entries;
95     while (*e)
96     {
97         if (!strcmp((*e)->name, name))
98         {
99             xfree ((*e)->value);
100             (*e)->value = 0;
101             if (value)
102             {
103                 (*e)->value = (char *) xmalloc (len+1);
104                 memcpy ((*e)->value, value, len);
105                 (*e)->value[len] = '\0';
106             }
107             return;
108         }
109         e = &(*e)->next;
110     }
111     *e = (struct ZOOM_options_entry *) xmalloc (sizeof(**e));
112     (*e)->name = xstrdup (name);
113     (*e)->value = 0;
114     if (value)
115     {
116         (*e)->value = (char *) xmalloc (len+1);
117         memcpy ((*e)->value, value, len);
118         (*e)->value[len] = '\0';
119     }
120     (*e)->next = 0;
121 }
122
123 ZOOM_API(void)
124 ZOOM_options_set (ZOOM_options opt, const char *name, const char *value)
125 {
126     ZOOM_options_setl (opt, name, value, value ? strlen(value): 0);
127 }
128
129 ZOOM_API(const char *)
130 ZOOM_options_get (ZOOM_options opt, const char *name)
131 {
132     const char *v = 0;
133     if (!opt)
134         return 0;
135     if (opt->callback_func)
136         v = (*opt->callback_func)(opt->callback_handle, name);
137     if (!v)
138     {
139         struct ZOOM_options_entry *e;
140         for (e = opt->entries; e; e = e->next)
141             if (!strcmp(e->name, name))
142             {
143                 v = e->value;
144                 break;
145             }
146     }
147     if (!v)
148         v = ZOOM_options_get(opt->parent1, name);
149     if (!v)
150         v = ZOOM_options_get(opt->parent2, name);
151     return v;
152 }
153
154 ZOOM_API(int)
155 ZOOM_options_get_bool (ZOOM_options opt, const char *name, int defa)
156 {
157     const char *v = ZOOM_options_get (opt, name);
158
159     if (!v)
160         return defa;
161     if (!strcmp (v, "1") || !strcmp(v, "T"))
162         return 1;
163     return 0;
164 }
165
166 ZOOM_API(int)
167 ZOOM_options_get_int (ZOOM_options opt, const char *name, int defa)
168 {
169     const char *v = ZOOM_options_get (opt, name);
170
171     if (!v || !*v)
172         return defa;
173     return atoi(v);
174 }
175
176 ZOOM_API(void)
177 ZOOM_options_set_int(ZOOM_options opt, const char *name, int value)
178 {
179     char s[40];
180
181     sprintf (s, "%d", value);
182     ZOOM_options_set (opt, name, s);
183 }