Added support of CharacterSetandLanguageNegotiation-3
[yaz-moved-to-github.git] / zoom / zoom-opt.c
1 /*
2  * $Id: zoom-opt.c,v 1.5 2002-05-17 12:48:30 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 (void)
14 {
15     return ZOOM_options_create_with_parent (0);
16 }
17
18 ZOOM_API(ZOOM_options)
19 ZOOM_options_create_with_parent (ZOOM_options parent)
20 {
21     ZOOM_options opt = (ZOOM_options) xmalloc (sizeof(*opt));
22
23     opt->refcount = 1;
24     opt->callback_func = 0;
25     opt->callback_handle = 0;
26     opt->entries = 0;
27     opt->parent= parent;
28     if (parent)
29         (parent->refcount)++;
30     return opt;
31 }
32
33 void ZOOM_options_addref (ZOOM_options opt)
34 {
35     (opt->refcount)++;
36 }
37
38 ZOOM_API(ZOOM_options_callback)
39 ZOOM_options_set_callback (
40     ZOOM_options opt,
41     ZOOM_options_callback callback_func,
42     void *callback_handle)
43 {
44     ZOOM_options_callback callback_old;
45
46     assert (opt);
47     callback_old = opt->callback_func;
48     opt->callback_func = callback_func;
49     opt->callback_handle = callback_handle;
50     return callback_old;
51 }
52
53 ZOOM_API(void)
54 ZOOM_options_destroy (ZOOM_options opt)
55 {
56     if (!opt)
57         return;
58     (opt->refcount)--;
59     if (opt->refcount == 0)
60     {
61         struct ZOOM_options_entry *e;
62         
63         ZOOM_options_destroy (opt->parent);
64         e = opt->entries;
65         while (e)
66         {
67             struct ZOOM_options_entry *e0 = e;
68             xfree (e->name);
69             xfree (e->value);
70             e = e->next;
71             xfree (e0);
72         }
73         xfree (opt);
74     }
75 }
76
77 ZOOM_API(void)
78 ZOOM_options_set (ZOOM_options opt, const char *name, const char *value)
79 {
80     struct ZOOM_options_entry **e;
81
82     e = &opt->entries;
83     while (*e)
84     {
85         if (!strcmp((*e)->name, name))
86         {
87             xfree ((*e)->value);
88             (*e)->value = xstrdup(value);
89             return;
90         }
91         e = &(*e)->next;
92     }
93     *e = (struct ZOOM_options_entry *) xmalloc (sizeof(**e));
94     (*e)->name = xstrdup (name);
95     (*e)->value = xstrdup (value);
96     (*e)->next = 0;
97 }
98
99 ZOOM_API(const char *)
100 ZOOM_options_get (ZOOM_options opt, const char *name)
101 {
102     const char *v = 0;
103     if (!opt)
104         return 0;
105     if (opt->callback_func)
106         v = (*opt->callback_func)(opt->callback_handle, name);
107     if (!v)
108     {
109         struct ZOOM_options_entry *e;
110         for (e = opt->entries; e; e = e->next)
111             if (!strcmp(e->name, name))
112             {
113                 v = e->value;
114                 break;
115             }
116     }
117     if (!v)
118         return ZOOM_options_get(opt->parent, name);
119     return v;
120 }
121
122 ZOOM_API(int)
123 ZOOM_options_get_bool (ZOOM_options opt, const char *name, int defa)
124 {
125     const char *v = ZOOM_options_get (opt, name);
126
127     if (!v)
128         return defa;
129     if (!strcmp (v, "1") || !strcmp(v, "T"))
130         return 1;
131     return 0;
132 }
133
134 ZOOM_API(int)
135 ZOOM_options_get_int (ZOOM_options opt, const char *name, int defa)
136 {
137     const char *v = ZOOM_options_get (opt, name);
138
139     if (!v || !*v)
140         return defa;
141     return atoi(v);
142 }
143
144 ZOOM_API(void)
145 ZOOM_options_set_int(ZOOM_options opt, const char *name, int value)
146 {
147     char s[40];
148
149     sprintf (s, "%d", value);
150     ZOOM_options_set (opt, name, s);
151 }