Fix mistake: fuzzy matching is 5=103, not 5=102
[yaz-moved-to-github.git] / src / zoom-opt.c
1 /*
2  * Copyright (C) 1995-2005, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: zoom-opt.c,v 1.5 2005-10-17 12:28:20 mike Exp $
6  */
7 /**
8  * \file zoom-opt.c
9  * \brief Implements ZOOM options handling
10  */
11 #include <assert.h>
12 #include "zoom-p.h"
13
14 #include <yaz/xmalloc.h>
15
16 ZOOM_API(ZOOM_options)
17 ZOOM_options_create_with_parent (ZOOM_options parent)
18 {
19     return ZOOM_options_create_with_parent2(parent, 0);
20 }
21
22 ZOOM_API(ZOOM_options)
23 ZOOM_options_create (void)
24 {
25     return ZOOM_options_create_with_parent (0);
26 }
27
28
29 ZOOM_API(ZOOM_options)
30 ZOOM_options_create_with_parent2 (ZOOM_options parent1, ZOOM_options parent2)
31 {
32     ZOOM_options opt = (ZOOM_options) xmalloc (sizeof(*opt));
33
34     opt->refcount = 1;
35     opt->callback_func = 0;
36     opt->callback_handle = 0;
37     opt->entries = 0;
38     opt->parent1= parent1;
39     if (parent1)
40         (parent1->refcount)++;
41     opt->parent2= parent2;
42     if (parent2)
43         (parent2->refcount)++;
44     return opt;
45 }
46
47
48 void ZOOM_options_addref (ZOOM_options opt)
49 {
50     (opt->refcount)++;
51 }
52
53 ZOOM_API(ZOOM_options_callback)
54 ZOOM_options_set_callback (
55     ZOOM_options opt,
56     ZOOM_options_callback callback_func,
57     void *callback_handle)
58 {
59     ZOOM_options_callback callback_old;
60
61     assert (opt);
62     callback_old = opt->callback_func;
63     opt->callback_func = callback_func;
64     opt->callback_handle = callback_handle;
65     return callback_old;
66 }
67
68 ZOOM_API(void)
69 ZOOM_options_destroy (ZOOM_options opt)
70 {
71     if (!opt)
72         return;
73     (opt->refcount)--;
74     if (opt->refcount == 0)
75     {
76         struct ZOOM_options_entry *e;
77         
78         ZOOM_options_destroy (opt->parent1);
79         ZOOM_options_destroy (opt->parent2);
80         e = opt->entries;
81         while (e)
82         {
83             struct ZOOM_options_entry *e0 = e;
84             xfree (e->name);
85             xfree (e->value);
86             e = e->next;
87             xfree (e0);
88         }
89         xfree (opt);
90     }
91 }
92
93 /* PRIVATE to ZOOM_options_setl() */
94 static void
95 _set_value(struct ZOOM_options_entry **e, const char *value, int len)
96 {
97     (*e)->value = 0;
98     (*e)->len = 0;
99     if (value)
100     {
101         (*e)->value = (char *) xmalloc (len+1);
102         memcpy ((*e)->value, value, len);
103         (*e)->value[len] = '\0';
104         (*e)->len = len;
105     }
106 }
107
108 ZOOM_API(void)
109 ZOOM_options_setl (ZOOM_options opt, const char *name, const char *value,
110                    int len)
111 {
112     struct ZOOM_options_entry **e;
113
114     e = &opt->entries;
115     while (*e)
116     {
117         if (!strcmp((*e)->name, name))
118         {
119             xfree ((*e)->value);
120             _set_value(e, value, len);
121             return;
122         }
123         e = &(*e)->next;
124     }
125     *e = (struct ZOOM_options_entry *) xmalloc (sizeof(**e));
126     (*e)->name = xstrdup (name);
127     _set_value(e, value, len);
128     (*e)->next = 0;
129 }
130
131 ZOOM_API(void)
132 ZOOM_options_set (ZOOM_options opt, const char *name, const char *value)
133 {
134     ZOOM_options_setl (opt, name, value, value ? strlen(value): 0);
135 }
136
137 ZOOM_API(const char *)
138 ZOOM_options_getl (ZOOM_options opt, const char *name, int *lenp)
139 {
140     const char *v = 0;
141     if (!opt)
142         return 0;
143     if (opt->callback_func)
144         v = (*opt->callback_func)(opt->callback_handle, name);
145     if (!v)
146     {
147         struct ZOOM_options_entry *e;
148         for (e = opt->entries; e; e = e->next)
149             if (!strcmp(e->name, name))
150             {
151                 v = e->value;
152                 *lenp = e->len;
153                 break;
154             }
155     }
156     if (!v)
157         v = ZOOM_options_getl(opt->parent1, name, lenp);
158     if (!v)
159         v = ZOOM_options_getl(opt->parent2, name, lenp);
160     return v;
161 }
162
163 ZOOM_API(const char *)
164 ZOOM_options_get (ZOOM_options opt, const char *name)
165 {
166     int dummy;
167     return ZOOM_options_getl(opt, name, &dummy);
168 }
169
170 ZOOM_API(int)
171 ZOOM_options_get_bool (ZOOM_options opt, const char *name, int defa)
172 {
173     const char *v = ZOOM_options_get (opt, name);
174
175     if (!v)
176         return defa;
177     if (!strcmp (v, "1") || !strcmp(v, "T"))
178         return 1;
179     return 0;
180 }
181
182 ZOOM_API(int)
183 ZOOM_options_get_int (ZOOM_options opt, const char *name, int defa)
184 {
185     const char *v = ZOOM_options_get (opt, name);
186
187     if (!v || !*v)
188         return defa;
189     return atoi(v);
190 }
191
192 ZOOM_API(void)
193 ZOOM_options_set_int(ZOOM_options opt, const char *name, int value)
194 {
195     char s[40];
196
197     sprintf (s, "%d", value);
198     ZOOM_options_set (opt, name, s);
199 }
200 /*
201  * Local variables:
202  * c-basic-offset: 4
203  * indent-tabs-mode: nil
204  * End:
205  * vim: shiftwidth=4 tabstop=8 expandtab
206  */
207