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