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