Expanded tabs in all source files. Added vim/emacs local variables
[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.4 2005-06-25 15:46:07 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, 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 ZOOM_API(void)
94 ZOOM_options_setl (ZOOM_options opt, const char *name, const char *value,
95                    int len)
96 {
97     struct ZOOM_options_entry **e;
98
99     e = &opt->entries;
100     while (*e)
101     {
102         if (!strcmp((*e)->name, name))
103         {
104             xfree ((*e)->value);
105             (*e)->value = 0;
106             if (value)
107             {
108                 (*e)->value = (char *) xmalloc (len+1);
109                 memcpy ((*e)->value, value, len);
110                 (*e)->value[len] = '\0';
111             }
112             return;
113         }
114         e = &(*e)->next;
115     }
116     *e = (struct ZOOM_options_entry *) xmalloc (sizeof(**e));
117     (*e)->name = xstrdup (name);
118     (*e)->value = 0;
119     if (value)
120     {
121         (*e)->value = (char *) xmalloc (len+1);
122         memcpy ((*e)->value, value, len);
123         (*e)->value[len] = '\0';
124     }
125     (*e)->next = 0;
126 }
127
128 ZOOM_API(void)
129 ZOOM_options_set (ZOOM_options opt, const char *name, const char *value)
130 {
131     ZOOM_options_setl (opt, name, value, value ? strlen(value): 0);
132 }
133
134 ZOOM_API(const char *)
135 ZOOM_options_get (ZOOM_options opt, const char *name)
136 {
137     const char *v = 0;
138     if (!opt)
139         return 0;
140     if (opt->callback_func)
141         v = (*opt->callback_func)(opt->callback_handle, name);
142     if (!v)
143     {
144         struct ZOOM_options_entry *e;
145         for (e = opt->entries; e; e = e->next)
146             if (!strcmp(e->name, name))
147             {
148                 v = e->value;
149                 break;
150             }
151     }
152     if (!v)
153         v = ZOOM_options_get(opt->parent1, name);
154     if (!v)
155         v = ZOOM_options_get(opt->parent2, name);
156     return v;
157 }
158
159 ZOOM_API(int)
160 ZOOM_options_get_bool (ZOOM_options opt, const char *name, int defa)
161 {
162     const char *v = ZOOM_options_get (opt, name);
163
164     if (!v)
165         return defa;
166     if (!strcmp (v, "1") || !strcmp(v, "T"))
167         return 1;
168     return 0;
169 }
170
171 ZOOM_API(int)
172 ZOOM_options_get_int (ZOOM_options opt, const char *name, int defa)
173 {
174     const char *v = ZOOM_options_get (opt, name);
175
176     if (!v || !*v)
177         return defa;
178     return atoi(v);
179 }
180
181 ZOOM_API(void)
182 ZOOM_options_set_int(ZOOM_options opt, const char *name, int value)
183 {
184     char s[40];
185
186     sprintf (s, "%d", value);
187     ZOOM_options_set (opt, name, s);
188 }
189 /*
190  * Local variables:
191  * c-basic-offset: 4
192  * indent-tabs-mode: nil
193  * End:
194  * vim: shiftwidth=4 tabstop=8 expandtab
195  */
196