Line data Source code
1 : /*
2 : *
3 : * c2ru.c
4 : *
5 : * $Source: /Users/ekr/tmp/nrappkit-dump/nrappkit/src/registry/c2ru.c,v $
6 : * $Revision: 1.3 $
7 : * $Date: 2007/06/26 22:37:50 $
8 : *
9 : * c2r utility methods
10 : *
11 : *
12 : * Copyright (C) 2005, Network Resonance, Inc.
13 : * Copyright (C) 2006, Network Resonance, Inc.
14 : * All Rights Reserved
15 : *
16 : * Redistribution and use in source and binary forms, with or without
17 : * modification, are permitted provided that the following conditions
18 : * are met:
19 : *
20 : * 1. Redistributions of source code must retain the above copyright
21 : * notice, this list of conditions and the following disclaimer.
22 : * 2. Redistributions in binary form must reproduce the above copyright
23 : * notice, this list of conditions and the following disclaimer in the
24 : * documentation and/or other materials provided with the distribution.
25 : * 3. Neither the name of Network Resonance, Inc. nor the name of any
26 : * contributors to this software may be used to endorse or promote
27 : * products derived from this software without specific prior written
28 : * permission.
29 : *
30 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
31 : * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 : * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
34 : * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 : * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 : * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 : * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 : * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 : * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 : * POSSIBILITY OF SUCH DAMAGE.
41 : *
42 : *
43 : */
44 :
45 : #include <sys/queue.h>
46 : #include <string.h>
47 : #include <registry.h>
48 : #include "nr_common.h"
49 : #include <r_errors.h>
50 : #include <r_macros.h>
51 : #include <ctype.h>
52 : #include "c2ru.h"
53 :
54 :
55 : #define NRGET(func, type, get) \
56 : int \
57 : func(NR_registry parent, char *child, type **out) \
58 : { \
59 : int r, _status; \
60 : NR_registry registry; \
61 : type tmp; \
62 : \
63 : if ((r = nr_c2ru_make_registry(parent, child, registry))) \
64 : ABORT(r); \
65 : \
66 : if ((r = get(registry, &tmp))) { \
67 : if (r != R_NOT_FOUND) \
68 : ABORT(r); \
69 : *out = 0; \
70 : } \
71 : else { \
72 : *out = RCALLOC(sizeof(tmp)); \
73 : if (*out == 0) \
74 : ABORT(R_NO_MEMORY); \
75 : **out = tmp; \
76 : } \
77 : \
78 : _status = 0; \
79 : abort: \
80 : return (_status); \
81 : }
82 :
83 : int
84 0 : nr_c2ru_get_char(NR_registry parent, char *child, char **out)
85 : {
86 : int r, _status;
87 : NR_registry registry;
88 : char tmp;
89 :
90 0 : if ((r = nr_c2ru_make_registry(parent, child, registry)))
91 0 : ABORT(r);
92 :
93 0 : if ((r = NR_reg_get_char(registry, &tmp))) {
94 0 : if (r != R_NOT_FOUND)
95 0 : ABORT(r);
96 0 : *out = 0;
97 : }
98 : else {
99 0 : *out = RCALLOC(sizeof(tmp));
100 0 : if (*out == 0)
101 0 : ABORT(R_NO_MEMORY);
102 0 : **out = tmp;
103 : }
104 :
105 0 : _status = 0;
106 : abort:
107 0 : return (_status);
108 : }
109 0 : NRGET(nr_c2ru_get_uchar, UCHAR, NR_reg_get_uchar)
110 0 : NRGET(nr_c2ru_get_int2, INT2, NR_reg_get_int2)
111 0 : NRGET(nr_c2ru_get_uint2, UINT2, NR_reg_get_uint2)
112 0 : NRGET(nr_c2ru_get_int4, INT4, NR_reg_get_int4)
113 0 : NRGET(nr_c2ru_get_uint4, UINT4, NR_reg_get_uint4)
114 0 : NRGET(nr_c2ru_get_int8, INT8, NR_reg_get_int8)
115 0 : NRGET(nr_c2ru_get_uint8, UINT8, NR_reg_get_uint8)
116 0 : NRGET(nr_c2ru_get_double, double, NR_reg_get_double)
117 0 : NRGET(nr_c2ru_get_string, char*, NR_reg_alloc_string)
118 0 : NRGET(nr_c2ru_get_data, Data, NR_reg_alloc_data)
119 :
120 :
121 : #define NRSET(func, type, set) \
122 : int \
123 : func(NR_registry parent, char *child, type *in) \
124 : { \
125 : int r, _status; \
126 : NR_registry registry; \
127 : \
128 : if (in == 0) \
129 : return 0; \
130 : \
131 : if ((r = nr_c2ru_make_registry(parent, child, registry))) \
132 : ABORT(r); \
133 : \
134 : if ((r = set(registry, *in))) \
135 : ABORT(r); \
136 : \
137 : _status = 0; \
138 : abort: \
139 : return (_status); \
140 : }
141 :
142 0 : NRSET(nr_c2ru_set_char, char, NR_reg_set_char)
143 0 : NRSET(nr_c2ru_set_uchar, UCHAR, NR_reg_set_uchar)
144 0 : NRSET(nr_c2ru_set_int2, INT2, NR_reg_set_int2)
145 0 : NRSET(nr_c2ru_set_uint2, UINT2, NR_reg_set_uint2)
146 0 : NRSET(nr_c2ru_set_int4, INT4, NR_reg_set_int4)
147 0 : NRSET(nr_c2ru_set_uint4, UINT4, NR_reg_set_uint4)
148 0 : NRSET(nr_c2ru_set_int8, INT8, NR_reg_set_int8)
149 0 : NRSET(nr_c2ru_set_uint8, UINT8, NR_reg_set_uint8)
150 0 : NRSET(nr_c2ru_set_double, double, NR_reg_set_double)
151 0 : NRSET(nr_c2ru_set_string, char*, NR_reg_set_string)
152 :
153 : int
154 0 : nr_c2ru_set_data(NR_registry parent, char *child, Data *in)
155 : {
156 : int r, _status;
157 : NR_registry registry;
158 :
159 0 : if (in == 0)
160 0 : return 0;
161 :
162 0 : if ((r = nr_c2ru_make_registry(parent, child, registry)))
163 0 : ABORT(r);
164 :
165 0 : if ((r = NR_reg_set_bytes(registry, in->data, in->len)))
166 0 : ABORT(r);
167 :
168 0 : _status = 0;
169 : abort:
170 0 : return (_status);
171 : }
172 :
173 : #define NRFREE(func, type) \
174 : int \
175 : func(type *in) \
176 : { \
177 : if (in) \
178 : RFREE(in); \
179 : return 0; \
180 : }
181 :
182 0 : NRFREE(nr_c2ru_free_char, char)
183 0 : NRFREE(nr_c2ru_free_uchar, UCHAR)
184 0 : NRFREE(nr_c2ru_free_int2, INT2)
185 0 : NRFREE(nr_c2ru_free_uint2, UINT2)
186 0 : NRFREE(nr_c2ru_free_int4, INT4)
187 0 : NRFREE(nr_c2ru_free_uint4, UINT4)
188 0 : NRFREE(nr_c2ru_free_int8, INT8)
189 0 : NRFREE(nr_c2ru_free_uint8, UINT8)
190 0 : NRFREE(nr_c2ru_free_double, double)
191 :
192 :
193 : int
194 0 : nr_c2ru_free_string(char **in)
195 : {
196 0 : if (*in)
197 0 : RFREE(*in);
198 0 : if (in)
199 0 : RFREE(in);
200 0 : return 0;
201 : }
202 :
203 : int
204 0 : nr_c2ru_free_data(Data *in)
205 : {
206 : int r, _status;
207 :
208 0 : if (in) {
209 0 : if ((r=r_data_destroy(&in)))
210 0 : ABORT(r);
211 : }
212 :
213 0 : _status = 0;
214 : abort:
215 0 : return (_status);
216 : }
217 :
218 : int
219 0 : nr_c2ru_get_children(NR_registry parent, char *child, void *ptr, size_t size, int (*get)(NR_registry, void*))
220 : {
221 : int r, _status;
222 : NR_registry registry;
223 : unsigned int count;
224 : int i;
225 : NR_registry name;
226 : struct entry { TAILQ_ENTRY(entry) entries; } *entry;
227 0 : TAILQ_HEAD(, entry) *tailq = (void*)ptr;
228 :
229 0 : TAILQ_INIT(tailq);
230 :
231 0 : if ((r=nr_c2ru_make_registry(parent, child, registry)))
232 0 : ABORT(r);
233 :
234 0 : if ((r=NR_reg_get_child_count(registry, &count))) {
235 0 : if (r != R_NOT_FOUND)
236 0 : ABORT(r);
237 : }
238 : else {
239 0 : for (i = 0; i < count; ++i) {
240 0 : if ((r=NR_reg_get_child_registry(registry, i, name))) {
241 : /* ignore R_NOT_FOUND errors */
242 0 : if (r == R_NOT_FOUND)
243 0 : continue;
244 : else
245 0 : ABORT(r);
246 : }
247 :
248 0 : if ((r=get(name, &entry))) {
249 : /* ignore R_NOT_FOUND errors */
250 0 : if (r == R_NOT_FOUND)
251 0 : continue;
252 : else
253 0 : ABORT(r);
254 : }
255 :
256 0 : TAILQ_INSERT_TAIL(tailq, entry, entries);
257 : }
258 : }
259 :
260 0 : _status = 0;
261 : abort:
262 0 : return (_status);
263 : }
264 :
265 : int
266 0 : nr_c2ru_set_children(NR_registry parent, char *child, void *ptr, int (*set)(NR_registry, void*), int (*label)(NR_registry, void*, char[NR_REG_MAX_NR_REGISTRY_LEN]))
267 : {
268 : int r, _status;
269 : NR_registry registry;
270 : int i;
271 : NR_registry name;
272 : char buffer[NR_REG_MAX_NR_REGISTRY_LEN];
273 : struct entry { TAILQ_ENTRY(entry) entries; } *entry;
274 0 : TAILQ_HEAD(, entry) *tailq = (void*)ptr;
275 :
276 0 : if ((r=nr_c2ru_make_registry(parent, child, registry)))
277 0 : ABORT(r);
278 :
279 0 : (void)NR_reg_del(registry);
280 :
281 0 : i = 0;
282 0 : TAILQ_FOREACH(entry, tailq, entries) {
283 0 : if (label == 0 || (r=label(registry, entry, buffer))) {
284 0 : snprintf(buffer, sizeof(buffer), "%d", i);
285 : }
286 0 : if ((r=nr_c2ru_make_registry(registry, buffer, name)))
287 0 : ABORT(r);
288 :
289 0 : if ((r=set(name, entry)))
290 0 : ABORT(r);
291 :
292 0 : ++i;
293 : }
294 :
295 0 : _status = 0;
296 : abort:
297 0 : return (_status);
298 : }
299 :
300 : int
301 0 : nr_c2ru_free_children(void *ptr, int (*free)(void*))
302 : {
303 : struct entry { TAILQ_ENTRY(entry) entries; } *entry;
304 0 : TAILQ_HEAD(, entry) *tailq = (void*)ptr;
305 :
306 0 : while (! TAILQ_EMPTY(tailq)) {
307 0 : entry = TAILQ_FIRST(tailq);
308 0 : TAILQ_REMOVE(tailq, entry, entries);
309 0 : (void)free(entry);
310 : }
311 :
312 0 : return 0;
313 : }
314 :
315 : /* requires parent already in legal form */
316 : int
317 0 : nr_c2ru_make_registry(NR_registry parent, char *child, NR_registry out)
318 : {
319 0 : return NR_reg_make_registry(parent, child, out);
320 : }
|