Old Z39.50 codecs gone. Added ZOOM. WRBUF MARC display util.
[yaz-moved-to-github.git] / zoom / zoom-opt.c
1 /*
2  * $Id: zoom-opt.c,v 1.1 2001-10-23 21:00:20 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 Z3950_options Z3950_options_create (void)
13 {
14     return Z3950_options_create_with_parent (0);
15 }
16
17 Z3950_options Z3950_options_create_with_parent (Z3950_options parent)
18 {
19     Z3950_options opt = xmalloc (sizeof(*opt));
20
21     opt->refcount = 1;
22     opt->callback_func = 0;
23     opt->callback_handle = 0;
24     opt->entries = 0;
25     opt->parent= parent;
26     if (parent)
27         (parent->refcount)++;
28     return opt;
29 }
30
31 void Z3950_options_addref (Z3950_options opt)
32 {
33     (opt->refcount)++;
34 }
35
36 Z3950_options_callback Z3950_options_set_callback (
37     Z3950_options opt,
38     Z3950_options_callback callback_func,
39     void *callback_handle)
40 {
41     Z3950_options_callback callback_old;
42
43     assert (opt);
44     callback_old = opt->callback_func;
45     opt->callback_func = callback_func;
46     opt->callback_handle = callback_handle;
47     return callback_old;
48 }
49
50 void Z3950_options_destroy (Z3950_options opt)
51 {
52     if (!opt)
53         return;
54     (opt->refcount)--;
55     if (opt->refcount == 0)
56     {
57         struct Z3950_options_entry *e;
58         
59         Z3950_options_destroy (opt->parent);
60         e = opt->entries;
61         while (e)
62         {
63             struct Z3950_options_entry *e0 = e;
64             xfree (e->name);
65             xfree (e->value);
66             e = e->next;
67             xfree (e0);
68         }
69         xfree (opt);
70     }
71 }
72
73 void Z3950_options_set (Z3950_options opt, const char *name, const char *value)
74 {
75     struct Z3950_options_entry **e;
76
77     e = &opt->entries;
78     while (*e)
79     {
80         if (!strcmp((*e)->name, name))
81         {
82             xfree ((*e)->value);
83             (*e)->value = xstrdup(value);
84             return;
85         }
86         e = &(*e)->next;
87     }
88     *e = xmalloc (sizeof(**e));
89     (*e)->name = xstrdup (name);
90     (*e)->value = xstrdup (value);
91     (*e)->next = 0;
92 }
93
94 const char *Z3950_options_get (Z3950_options opt, const char *name)
95 {
96     const char *v = 0;
97     if (!opt)
98         return 0;
99     if (opt->callback_func)
100         v = (*opt->callback_func)(opt->callback_handle, name);
101     if (!v)
102     {
103         struct Z3950_options_entry *e;
104         for (e = opt->entries; e; e = e->next)
105             if (!strcmp(e->name, name))
106             {
107                 v = e->value;
108                 break;
109             }
110     }
111     if (!v)
112         return Z3950_options_get(opt->parent, name);
113     return v;
114 }
115
116 int Z3950_options_get_bool (Z3950_options opt, const char *name, int defa)
117 {
118     const char *v = Z3950_options_get (opt, name);
119
120     if (!v)
121         return defa;
122     if (!strcmp (v, "1") || !strcmp(v, "T"))
123         return 1;
124     return 0;
125 }
126
127 int Z3950_options_get_int (Z3950_options opt, const char *name, int defa)
128 {
129     const char *v = Z3950_options_get (opt, name);
130
131     if (!v || !*v)
132         return defa;
133     return atoi(v);
134 }