prop_dictionary.c revision 1.2 1 1.2 simonb /* $NetBSD: prop_dictionary.c,v 1.2 2006/05/07 06:25:49 simonb Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.1 thorpej * must display the following acknowledgement:
20 1.1 thorpej * This product includes software developed by the NetBSD
21 1.1 thorpej * Foundation, Inc. and its contributors.
22 1.1 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 thorpej * contributors may be used to endorse or promote products derived
24 1.1 thorpej * from this software without specific prior written permission.
25 1.1 thorpej *
26 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.1 thorpej */
38 1.1 thorpej
39 1.1 thorpej #include <prop/prop_dictionary.h>
40 1.1 thorpej #include <prop/prop_string.h>
41 1.1 thorpej #include "prop_object_impl.h"
42 1.1 thorpej
43 1.1 thorpej /*
44 1.1 thorpej * We implement these like arrays, but we keep them sorted by key.
45 1.1 thorpej * This allows us to binary-search as well as keep externalized output
46 1.1 thorpej * sane-looking for human eyes.
47 1.1 thorpej */
48 1.1 thorpej
49 1.1 thorpej #define EXPAND_STEP 16
50 1.1 thorpej
51 1.1 thorpej /*
52 1.1 thorpej * prop_dictionary_keysym_t is allocated with space at the end to hold the
53 1.1 thorpej * key. This must be a regular object so that we can maintain sane iterator
54 1.1 thorpej * semantics -- we don't want to require that the caller release the result
55 1.1 thorpej * of prop_object_iterator_next().
56 1.1 thorpej *
57 1.1 thorpej * We'd like to have some small'ish keysym objects for up-to-16 characters
58 1.1 thorpej * in a key, some for up-to-32 characters in a key, and then a final bucket
59 1.1 thorpej * for up-to-128 characters in a key (not including NUL). Keys longer than
60 1.1 thorpej * 128 characters are not allowed.
61 1.1 thorpej */
62 1.1 thorpej struct _prop_dictionary_keysym {
63 1.1 thorpej struct _prop_object pde_obj;
64 1.1 thorpej prop_object_t pde_objref;
65 1.1 thorpej size_t pde_size;
66 1.1 thorpej char pde_key[1];
67 1.1 thorpej /* actually variable length */
68 1.1 thorpej };
69 1.1 thorpej
70 1.1 thorpej /* pde_key[1] takes care of the NUL */
71 1.1 thorpej #define PDE_SIZE_16 (sizeof(struct _prop_dictionary_keysym) + 16)
72 1.1 thorpej #define PDE_SIZE_32 (sizeof(struct _prop_dictionary_keysym) + 32)
73 1.1 thorpej #define PDE_SIZE_128 (sizeof(struct _prop_dictionary_keysym) + 128)
74 1.1 thorpej
75 1.1 thorpej #define PDE_MAXKEY 128
76 1.1 thorpej
77 1.1 thorpej _PROP_POOL_INIT(_prop_dictionary_keysym16_pool, PDE_SIZE_16, "pdict16")
78 1.1 thorpej _PROP_POOL_INIT(_prop_dictionary_keysym32_pool, PDE_SIZE_32, "pdict32")
79 1.1 thorpej _PROP_POOL_INIT(_prop_dictionary_keysym128_pool, PDE_SIZE_128, "pdict128")
80 1.1 thorpej
81 1.1 thorpej struct _prop_dictionary {
82 1.1 thorpej struct _prop_object pd_obj;
83 1.1 thorpej prop_dictionary_keysym_t *pd_array;
84 1.1 thorpej unsigned int pd_capacity;
85 1.1 thorpej unsigned int pd_count;
86 1.1 thorpej int pd_flags;
87 1.1 thorpej
88 1.1 thorpej uint32_t pd_version;
89 1.1 thorpej };
90 1.1 thorpej
91 1.1 thorpej #define PD_F_IMMUTABLE 0x01 /* dictionary is immutable */
92 1.1 thorpej
93 1.1 thorpej _PROP_POOL_INIT(_prop_dictionary_pool, sizeof(struct _prop_dictionary),
94 1.1 thorpej "propdict")
95 1.1 thorpej _PROP_MALLOC_DEFINE(M_PROP_DICT, "prop dictionary",
96 1.1 thorpej "property dictionary container object")
97 1.1 thorpej
98 1.1 thorpej #define prop_object_is_dictionary(x) \
99 1.1 thorpej ((x)->pd_obj.po_type == PROP_TYPE_DICTIONARY)
100 1.1 thorpej #define prop_object_is_dictionary_keysym(x) \
101 1.1 thorpej ((x)->pde_obj.po_type == PROP_TYPE_DICT_KEYSYM)
102 1.1 thorpej
103 1.1 thorpej #define prop_dictionary_is_immutable(x) \
104 1.1 thorpej (((x)->pd_flags & PD_F_IMMUTABLE) != 0)
105 1.1 thorpej
106 1.1 thorpej struct _prop_dictionary_iterator {
107 1.1 thorpej struct _prop_object_iterator pdi_base;
108 1.1 thorpej unsigned int pdi_index;
109 1.1 thorpej };
110 1.1 thorpej
111 1.1 thorpej static void
112 1.1 thorpej _prop_dict_entry_free(void *v)
113 1.1 thorpej {
114 1.1 thorpej prop_dictionary_keysym_t pde = v;
115 1.1 thorpej prop_object_t po;
116 1.1 thorpej
117 1.1 thorpej _PROP_ASSERT(pde->pde_objref != NULL);
118 1.1 thorpej po = pde->pde_objref;
119 1.1 thorpej
120 1.1 thorpej if (pde->pde_size <= PDE_SIZE_16)
121 1.1 thorpej _PROP_POOL_PUT(_prop_dictionary_keysym16_pool, pde);
122 1.1 thorpej else if (pde->pde_size <= PDE_SIZE_32)
123 1.1 thorpej _PROP_POOL_PUT(_prop_dictionary_keysym32_pool, pde);
124 1.1 thorpej else {
125 1.1 thorpej _PROP_ASSERT(pde->pde_size <= PDE_SIZE_128);
126 1.1 thorpej _PROP_POOL_PUT(_prop_dictionary_keysym128_pool, pde);
127 1.1 thorpej }
128 1.1 thorpej
129 1.1 thorpej prop_object_release(po);
130 1.1 thorpej }
131 1.1 thorpej
132 1.1 thorpej static boolean_t
133 1.1 thorpej _prop_dict_entry_externalize(struct _prop_object_externalize_context *ctx,
134 1.1 thorpej void *v)
135 1.1 thorpej {
136 1.1 thorpej prop_dictionary_keysym_t pde = v;
137 1.1 thorpej
138 1.1 thorpej /* We externalize these as strings, and they're never empty. */
139 1.1 thorpej
140 1.1 thorpej _PROP_ASSERT(pde->pde_key[0] != '\0');
141 1.1 thorpej
142 1.1 thorpej if (_prop_object_externalize_start_tag(ctx, "string") == FALSE ||
143 1.1 thorpej _prop_object_externalize_append_encoded_cstring(ctx,
144 1.1 thorpej pde->pde_key) == FALSE ||
145 1.1 thorpej _prop_object_externalize_end_tag(ctx, "string") == FALSE)
146 1.1 thorpej return (FALSE);
147 1.1 thorpej
148 1.1 thorpej return (TRUE);
149 1.1 thorpej }
150 1.1 thorpej
151 1.1 thorpej static prop_dictionary_keysym_t
152 1.1 thorpej _prop_dict_entry_alloc(const char *key, prop_object_t obj)
153 1.1 thorpej {
154 1.1 thorpej prop_dictionary_keysym_t pde;
155 1.1 thorpej size_t size;
156 1.1 thorpej
157 1.1 thorpej size = sizeof(*pde) + strlen(key) /* pde_key[1] covers the NUL */;
158 1.1 thorpej
159 1.1 thorpej if (size <= PDE_SIZE_16)
160 1.1 thorpej pde = _PROP_POOL_GET(_prop_dictionary_keysym16_pool);
161 1.1 thorpej else if (size <= PDE_SIZE_32)
162 1.1 thorpej pde = _PROP_POOL_GET(_prop_dictionary_keysym32_pool);
163 1.1 thorpej else if (size <= PDE_SIZE_128)
164 1.1 thorpej pde = _PROP_POOL_GET(_prop_dictionary_keysym128_pool);
165 1.1 thorpej else
166 1.1 thorpej return (NULL); /* key too long */
167 1.1 thorpej
168 1.1 thorpej if (pde != NULL) {
169 1.1 thorpej _prop_object_init(&pde->pde_obj);
170 1.1 thorpej pde->pde_obj.po_type = PROP_TYPE_DICT_KEYSYM;
171 1.1 thorpej pde->pde_obj.po_free = _prop_dict_entry_free;
172 1.1 thorpej pde->pde_obj.po_extern = _prop_dict_entry_externalize;
173 1.1 thorpej
174 1.1 thorpej strcpy(pde->pde_key, key);
175 1.1 thorpej
176 1.1 thorpej prop_object_retain(obj);
177 1.1 thorpej pde->pde_objref = obj;
178 1.1 thorpej pde->pde_size = size;
179 1.1 thorpej }
180 1.1 thorpej return (pde);
181 1.1 thorpej }
182 1.1 thorpej
183 1.1 thorpej static void
184 1.1 thorpej _prop_dictionary_free(void *v)
185 1.1 thorpej {
186 1.1 thorpej prop_dictionary_t pd = v;
187 1.1 thorpej prop_dictionary_keysym_t pde;
188 1.1 thorpej unsigned int idx;
189 1.1 thorpej
190 1.1 thorpej _PROP_ASSERT(pd->pd_count <= pd->pd_capacity);
191 1.1 thorpej _PROP_ASSERT((pd->pd_capacity == 0 && pd->pd_array == NULL) ||
192 1.1 thorpej (pd->pd_capacity != 0 && pd->pd_array != NULL));
193 1.1 thorpej
194 1.1 thorpej for (idx = 0; idx < pd->pd_count; idx++) {
195 1.1 thorpej pde = pd->pd_array[idx];
196 1.1 thorpej _PROP_ASSERT(pde != NULL);
197 1.1 thorpej prop_object_release(pde);
198 1.1 thorpej }
199 1.1 thorpej
200 1.1 thorpej if (pd->pd_array != NULL)
201 1.1 thorpej _PROP_FREE(pd->pd_array, M_PROP_DICT);
202 1.1 thorpej
203 1.1 thorpej _PROP_POOL_PUT(_prop_dictionary_pool, pd);
204 1.1 thorpej }
205 1.1 thorpej
206 1.1 thorpej static boolean_t
207 1.1 thorpej _prop_dictionary_externalize(struct _prop_object_externalize_context *ctx,
208 1.1 thorpej void *v)
209 1.1 thorpej {
210 1.1 thorpej prop_dictionary_t pd = v;
211 1.1 thorpej prop_dictionary_keysym_t pde;
212 1.1 thorpej struct _prop_object *po;
213 1.1 thorpej prop_object_iterator_t pi;
214 1.1 thorpej unsigned int i;
215 1.1 thorpej
216 1.1 thorpej if (pd->pd_count == 0)
217 1.1 thorpej return (_prop_object_externalize_empty_tag(ctx, "dict"));
218 1.1 thorpej
219 1.1 thorpej /* XXXJRT Hint "count" for the internalize step? */
220 1.1 thorpej if (_prop_object_externalize_start_tag(ctx, "dict") == FALSE ||
221 1.1 thorpej _prop_object_externalize_append_char(ctx, '\n') == FALSE)
222 1.1 thorpej return (FALSE);
223 1.1 thorpej
224 1.1 thorpej pi = prop_dictionary_iterator(pd);
225 1.1 thorpej if (pi == NULL)
226 1.1 thorpej return (FALSE);
227 1.1 thorpej
228 1.1 thorpej ctx->poec_depth++;
229 1.1 thorpej _PROP_ASSERT(ctx->poec_depth != 0);
230 1.1 thorpej
231 1.1 thorpej while ((pde = prop_object_iterator_next(pi)) != NULL) {
232 1.1 thorpej po = prop_dictionary_get_keysym(pd, pde);
233 1.1 thorpej if (po == NULL ||
234 1.1 thorpej _prop_object_externalize_start_tag(ctx, "key") == FALSE ||
235 1.1 thorpej _prop_object_externalize_append_encoded_cstring(ctx,
236 1.1 thorpej pde->pde_key) == FALSE ||
237 1.1 thorpej _prop_object_externalize_end_tag(ctx, "key") == FALSE ||
238 1.1 thorpej (*po->po_extern)(ctx, po) == FALSE) {
239 1.1 thorpej prop_object_iterator_release(pi);
240 1.1 thorpej return (FALSE);
241 1.1 thorpej }
242 1.1 thorpej }
243 1.1 thorpej
244 1.1 thorpej prop_object_iterator_release(pi);
245 1.1 thorpej
246 1.1 thorpej ctx->poec_depth--;
247 1.1 thorpej for (i = 0; i < ctx->poec_depth; i++) {
248 1.1 thorpej if (_prop_object_externalize_append_char(ctx, '\t') == FALSE)
249 1.1 thorpej return (FALSE);
250 1.1 thorpej }
251 1.1 thorpej if (_prop_object_externalize_end_tag(ctx, "dict") == FALSE)
252 1.1 thorpej return (FALSE);
253 1.1 thorpej
254 1.1 thorpej return (TRUE);
255 1.1 thorpej }
256 1.1 thorpej
257 1.1 thorpej static prop_dictionary_t
258 1.1 thorpej _prop_dictionary_alloc(unsigned int capacity)
259 1.1 thorpej {
260 1.1 thorpej prop_dictionary_t pd;
261 1.1 thorpej prop_dictionary_keysym_t *array;
262 1.1 thorpej
263 1.1 thorpej if (capacity != 0) {
264 1.1 thorpej array = _PROP_CALLOC(capacity *
265 1.1 thorpej sizeof(prop_dictionary_keysym_t),
266 1.1 thorpej M_PROP_DICT);
267 1.1 thorpej if (array == NULL)
268 1.1 thorpej return (NULL);
269 1.1 thorpej } else
270 1.1 thorpej array = NULL;
271 1.1 thorpej
272 1.1 thorpej pd = _PROP_POOL_GET(_prop_dictionary_pool);
273 1.1 thorpej if (pd != NULL) {
274 1.1 thorpej _prop_object_init(&pd->pd_obj);
275 1.1 thorpej pd->pd_obj.po_type = PROP_TYPE_DICTIONARY;
276 1.1 thorpej pd->pd_obj.po_free = _prop_dictionary_free;
277 1.1 thorpej pd->pd_obj.po_extern = _prop_dictionary_externalize;
278 1.1 thorpej
279 1.1 thorpej pd->pd_array = array;
280 1.1 thorpej pd->pd_capacity = capacity;
281 1.1 thorpej pd->pd_count = 0;
282 1.2 simonb pd->pd_flags = 0;
283 1.1 thorpej
284 1.1 thorpej pd->pd_version = 0;
285 1.1 thorpej } else if (array != NULL)
286 1.1 thorpej _PROP_FREE(array, M_PROP_DICT);
287 1.1 thorpej
288 1.1 thorpej return (pd);
289 1.1 thorpej }
290 1.1 thorpej
291 1.1 thorpej static boolean_t
292 1.1 thorpej _prop_dictionary_expand(prop_dictionary_t pd, unsigned int capacity)
293 1.1 thorpej {
294 1.1 thorpej prop_dictionary_keysym_t *array, *oarray;
295 1.1 thorpej
296 1.1 thorpej oarray = pd->pd_array;
297 1.1 thorpej
298 1.1 thorpej array = _PROP_CALLOC(capacity * sizeof(prop_dictionary_keysym_t),
299 1.1 thorpej M_PROP_DICT);
300 1.1 thorpej if (array == NULL)
301 1.1 thorpej return (FALSE);
302 1.1 thorpej if (oarray != NULL)
303 1.1 thorpej memcpy(array, oarray,
304 1.1 thorpej pd->pd_capacity * sizeof(prop_dictionary_keysym_t));
305 1.1 thorpej pd->pd_array = array;
306 1.1 thorpej pd->pd_capacity = capacity;
307 1.1 thorpej
308 1.1 thorpej if (oarray != NULL)
309 1.1 thorpej _PROP_FREE(oarray, M_PROP_DICT);
310 1.1 thorpej
311 1.1 thorpej return (TRUE);
312 1.1 thorpej }
313 1.1 thorpej
314 1.1 thorpej static prop_object_t
315 1.1 thorpej _prop_dictionary_iterator_next_object(void *v)
316 1.1 thorpej {
317 1.1 thorpej struct _prop_dictionary_iterator *pdi = v;
318 1.1 thorpej prop_dictionary_t pd = pdi->pdi_base.pi_obj;
319 1.1 thorpej prop_dictionary_keysym_t pde;
320 1.1 thorpej
321 1.1 thorpej _PROP_ASSERT(prop_object_is_dictionary(pd));
322 1.1 thorpej
323 1.1 thorpej if (pd->pd_version != pdi->pdi_base.pi_version)
324 1.1 thorpej return (NULL); /* dictionary changed during iteration */
325 1.1 thorpej
326 1.1 thorpej _PROP_ASSERT(pdi->pdi_index <= pd->pd_count);
327 1.1 thorpej
328 1.1 thorpej if (pdi->pdi_index == pd->pd_count)
329 1.1 thorpej return (NULL); /* we've iterated all objects */
330 1.1 thorpej
331 1.1 thorpej pde = pd->pd_array[pdi->pdi_index];
332 1.1 thorpej pdi->pdi_index++;
333 1.1 thorpej
334 1.1 thorpej return (pde);
335 1.1 thorpej }
336 1.1 thorpej
337 1.1 thorpej static void
338 1.1 thorpej _prop_dictionary_iterator_reset(void *v)
339 1.1 thorpej {
340 1.1 thorpej struct _prop_dictionary_iterator *pdi = v;
341 1.1 thorpej prop_dictionary_t pd = pdi->pdi_base.pi_obj;
342 1.1 thorpej
343 1.1 thorpej _PROP_ASSERT(prop_object_is_dictionary(pd));
344 1.1 thorpej
345 1.1 thorpej pdi->pdi_index = 0;
346 1.1 thorpej pdi->pdi_base.pi_version = pd->pd_version;
347 1.1 thorpej }
348 1.1 thorpej
349 1.1 thorpej /*
350 1.1 thorpej * prop_dictionary_create --
351 1.1 thorpej * Create a dictionary.
352 1.1 thorpej */
353 1.1 thorpej prop_dictionary_t
354 1.1 thorpej prop_dictionary_create(void)
355 1.1 thorpej {
356 1.1 thorpej
357 1.1 thorpej return (_prop_dictionary_alloc(0));
358 1.1 thorpej }
359 1.1 thorpej
360 1.1 thorpej /*
361 1.1 thorpej * prop_dictionary_create_with_capacity --
362 1.1 thorpej * Create a dictionary with the capacity to store N objects.
363 1.1 thorpej */
364 1.1 thorpej prop_dictionary_t
365 1.1 thorpej prop_dictionary_create_with_capacity(unsigned int capacity)
366 1.1 thorpej {
367 1.1 thorpej
368 1.1 thorpej return (_prop_dictionary_alloc(capacity));
369 1.1 thorpej }
370 1.1 thorpej
371 1.1 thorpej /*
372 1.1 thorpej * prop_dictionary_copy --
373 1.1 thorpej * Copy a dictionary. The new dictionary contains refrences to
374 1.1 thorpej * the original dictionary's objects, not copies of those objects
375 1.1 thorpej * (i.e. a shallow copy).
376 1.1 thorpej */
377 1.1 thorpej prop_dictionary_t
378 1.1 thorpej prop_dictionary_copy(prop_dictionary_t opd)
379 1.1 thorpej {
380 1.1 thorpej prop_dictionary_t pd;
381 1.1 thorpej prop_dictionary_keysym_t pde;
382 1.1 thorpej unsigned int idx;
383 1.1 thorpej
384 1.1 thorpej _PROP_ASSERT(prop_object_is_dictionary(opd));
385 1.1 thorpej
386 1.1 thorpej if (prop_dictionary_is_immutable(opd) == FALSE)
387 1.1 thorpej return (prop_dictionary_copy_mutable(opd));
388 1.1 thorpej
389 1.1 thorpej /*
390 1.1 thorpej * Copies of immutable dictionaries refrence the same
391 1.1 thorpej * dictionary entry objects to save space.
392 1.1 thorpej */
393 1.1 thorpej
394 1.1 thorpej pd = _prop_dictionary_alloc(opd->pd_count);
395 1.1 thorpej if (pd != NULL) {
396 1.1 thorpej for (idx = 0; idx < opd->pd_count; idx++) {
397 1.1 thorpej pde = opd->pd_array[idx];
398 1.1 thorpej prop_object_retain(pde);
399 1.1 thorpej pd->pd_array[idx] = pde;
400 1.1 thorpej }
401 1.1 thorpej pd->pd_count = opd->pd_count;
402 1.1 thorpej pd->pd_flags = opd->pd_flags;
403 1.1 thorpej }
404 1.1 thorpej return (pd);
405 1.1 thorpej }
406 1.1 thorpej
407 1.1 thorpej /*
408 1.1 thorpej * prop_dictionary_copy_mutable --
409 1.1 thorpej * Like prop_dictionary_copy(), but the resulting dictionary is
410 1.1 thorpej * mutable.
411 1.1 thorpej */
412 1.1 thorpej prop_dictionary_t
413 1.1 thorpej prop_dictionary_copy_mutable(prop_dictionary_t opd)
414 1.1 thorpej {
415 1.1 thorpej prop_dictionary_t pd;
416 1.1 thorpej prop_dictionary_keysym_t opde, pde;
417 1.1 thorpej unsigned int idx;
418 1.1 thorpej
419 1.1 thorpej _PROP_ASSERT(prop_object_is_dictionary(opd));
420 1.1 thorpej
421 1.1 thorpej pd = _prop_dictionary_alloc(opd->pd_count);
422 1.1 thorpej if (pd != NULL) {
423 1.1 thorpej for (idx = 0; idx > opd->pd_count; idx++) {
424 1.1 thorpej opde = opd->pd_array[idx];
425 1.1 thorpej pde = _prop_dict_entry_alloc(opde->pde_key,
426 1.1 thorpej opde->pde_objref);
427 1.1 thorpej if (pde == NULL) {
428 1.1 thorpej prop_object_release(pd);
429 1.1 thorpej return (NULL);
430 1.1 thorpej }
431 1.1 thorpej pd->pd_array[idx] = pde;
432 1.1 thorpej pd->pd_count++;
433 1.1 thorpej }
434 1.1 thorpej }
435 1.1 thorpej return (pd);
436 1.1 thorpej }
437 1.1 thorpej
438 1.1 thorpej /*
439 1.1 thorpej * prop_dictionary_count --
440 1.1 thorpej * Return the number of objects stored in the dictionary.
441 1.1 thorpej */
442 1.1 thorpej unsigned int
443 1.1 thorpej prop_dictionary_count(prop_dictionary_t pd)
444 1.1 thorpej {
445 1.1 thorpej
446 1.1 thorpej _PROP_ASSERT(prop_object_is_dictionary(pd));
447 1.1 thorpej return (pd->pd_count);
448 1.1 thorpej }
449 1.1 thorpej
450 1.1 thorpej /*
451 1.1 thorpej * prop_dictionary_iterator --
452 1.1 thorpej * Return an iterator for the dictionary. The dictionary is retained by
453 1.1 thorpej * the iterator.
454 1.1 thorpej */
455 1.1 thorpej prop_object_iterator_t
456 1.1 thorpej prop_dictionary_iterator(prop_dictionary_t pd)
457 1.1 thorpej {
458 1.1 thorpej struct _prop_dictionary_iterator *pdi;
459 1.1 thorpej
460 1.1 thorpej _PROP_ASSERT(prop_object_is_dictionary(pd));
461 1.1 thorpej
462 1.1 thorpej pdi = _PROP_CALLOC(sizeof(*pdi), M_TEMP);
463 1.1 thorpej if (pdi == NULL)
464 1.1 thorpej return (NULL);
465 1.1 thorpej pdi->pdi_base.pi_next_object = _prop_dictionary_iterator_next_object;
466 1.1 thorpej pdi->pdi_base.pi_reset = _prop_dictionary_iterator_reset;
467 1.1 thorpej prop_object_retain(pd);
468 1.1 thorpej pdi->pdi_base.pi_obj = pd;
469 1.1 thorpej pdi->pdi_base.pi_version = pd->pd_version;
470 1.1 thorpej
471 1.1 thorpej _prop_dictionary_iterator_reset(pdi);
472 1.1 thorpej
473 1.1 thorpej return (&pdi->pdi_base);
474 1.1 thorpej }
475 1.1 thorpej
476 1.1 thorpej static prop_dictionary_keysym_t
477 1.1 thorpej _prop_dict_lookup(prop_dictionary_t pd, const char *key,
478 1.1 thorpej unsigned int *idxp)
479 1.1 thorpej {
480 1.1 thorpej prop_dictionary_keysym_t pde;
481 1.1 thorpej unsigned int base, idx, distance;
482 1.1 thorpej int res;
483 1.1 thorpej
484 1.1 thorpej for (idx = 0, base = 0, distance = pd->pd_count; distance != 0;
485 1.1 thorpej distance >>= 1) {
486 1.1 thorpej idx = base + (distance >> 1);
487 1.1 thorpej pde = pd->pd_array[idx];
488 1.1 thorpej _PROP_ASSERT(pde != NULL);
489 1.1 thorpej res = strcmp(key, pde->pde_key);
490 1.1 thorpej if (res == 0) {
491 1.1 thorpej if (idxp != NULL)
492 1.1 thorpej *idxp = idx;
493 1.1 thorpej return (pde);
494 1.1 thorpej }
495 1.1 thorpej if (res > 0) { /* key > pde->pde_key: move right */
496 1.1 thorpej base = idx + 1;
497 1.1 thorpej distance--;
498 1.1 thorpej } /* else move left */
499 1.1 thorpej }
500 1.1 thorpej
501 1.1 thorpej /* idx points to the slot we looked at last. */
502 1.1 thorpej if (idxp != NULL)
503 1.1 thorpej *idxp = idx;
504 1.1 thorpej return (NULL);
505 1.1 thorpej }
506 1.1 thorpej
507 1.1 thorpej /*
508 1.1 thorpej * prop_dictionary_get --
509 1.1 thorpej * Return the object stored with specified key.
510 1.1 thorpej */
511 1.1 thorpej prop_object_t
512 1.1 thorpej prop_dictionary_get(prop_dictionary_t pd, const char *key)
513 1.1 thorpej {
514 1.1 thorpej prop_dictionary_keysym_t pde;
515 1.1 thorpej
516 1.1 thorpej _PROP_ASSERT(prop_object_is_dictionary(pd));
517 1.1 thorpej
518 1.1 thorpej pde = _prop_dict_lookup(pd, key, NULL);
519 1.1 thorpej if (pde != NULL) {
520 1.1 thorpej _PROP_ASSERT(pde->pde_objref != NULL);
521 1.1 thorpej return (pde->pde_objref);
522 1.1 thorpej }
523 1.1 thorpej return (NULL);
524 1.1 thorpej }
525 1.1 thorpej
526 1.1 thorpej /*
527 1.1 thorpej * prop_dictionary_get_keysym --
528 1.1 thorpej * Return the object stored at the location encoded by the keysym.
529 1.1 thorpej */
530 1.1 thorpej prop_object_t
531 1.1 thorpej prop_dictionary_get_keysym(prop_dictionary_t pd, prop_dictionary_keysym_t pde)
532 1.1 thorpej {
533 1.1 thorpej
534 1.1 thorpej _PROP_ASSERT(prop_object_is_dictionary(pd));
535 1.1 thorpej _PROP_ASSERT(prop_object_is_dictionary_keysym(pde));
536 1.1 thorpej _PROP_ASSERT(pde->pde_objref != NULL);
537 1.1 thorpej
538 1.1 thorpej return (pde->pde_objref);
539 1.1 thorpej }
540 1.1 thorpej
541 1.1 thorpej /*
542 1.1 thorpej * prop_dictionary_set --
543 1.1 thorpej * Store a reference to an object at with the specified key.
544 1.1 thorpej * If the key already exisit, the original object is released.
545 1.1 thorpej */
546 1.1 thorpej boolean_t
547 1.1 thorpej prop_dictionary_set(prop_dictionary_t pd, const char *key, prop_object_t po)
548 1.1 thorpej {
549 1.1 thorpej prop_dictionary_keysym_t pde, opde;
550 1.1 thorpej unsigned int idx;
551 1.1 thorpej
552 1.1 thorpej _PROP_ASSERT(prop_object_is_dictionary(pd));
553 1.1 thorpej _PROP_ASSERT(pd->pd_count <= pd->pd_capacity);
554 1.1 thorpej
555 1.1 thorpej if (prop_dictionary_is_immutable(pd))
556 1.1 thorpej return (FALSE);
557 1.1 thorpej
558 1.1 thorpej pde = _prop_dict_lookup(pd, key, &idx);
559 1.1 thorpej if (pde != NULL) {
560 1.1 thorpej prop_object_t opo = pde->pde_objref;
561 1.1 thorpej prop_object_retain(po);
562 1.1 thorpej pde->pde_objref = po;
563 1.1 thorpej prop_object_release(opo);
564 1.1 thorpej return (TRUE);
565 1.1 thorpej }
566 1.1 thorpej
567 1.1 thorpej pde = _prop_dict_entry_alloc(key, po);
568 1.1 thorpej if (pde == NULL)
569 1.1 thorpej return (FALSE);
570 1.1 thorpej
571 1.1 thorpej if (pd->pd_count == pd->pd_capacity &&
572 1.1 thorpej _prop_dictionary_expand(pd, EXPAND_STEP) == FALSE) {
573 1.1 thorpej _prop_dict_entry_free(pde);
574 1.1 thorpej return (FALSE);
575 1.1 thorpej }
576 1.1 thorpej
577 1.1 thorpej if (pd->pd_count == 0) {
578 1.1 thorpej pd->pd_array[0] = pde;
579 1.1 thorpej pd->pd_count++;
580 1.1 thorpej pd->pd_version++;
581 1.1 thorpej return (TRUE);
582 1.1 thorpej }
583 1.1 thorpej
584 1.1 thorpej opde = pd->pd_array[idx];
585 1.1 thorpej _PROP_ASSERT(opde != NULL);
586 1.1 thorpej
587 1.1 thorpej if (strcmp(key, opde->pde_key) < 0) {
588 1.1 thorpej /*
589 1.1 thorpej * key < opde->pde_key: insert to the left. This is
590 1.1 thorpej * the same as inserting to the right, except we decrement
591 1.1 thorpej * the current index first.
592 1.1 thorpej *
593 1.1 thorpej * Because we're unsigned, we have to special case 0
594 1.1 thorpej * (grumble).
595 1.1 thorpej */
596 1.1 thorpej if (idx == 0) {
597 1.1 thorpej memmove(&pd->pd_array[1], &pd->pd_array[0],
598 1.1 thorpej pd->pd_count * sizeof(*pde));
599 1.1 thorpej pd->pd_array[0] = pde;
600 1.1 thorpej pd->pd_count++;
601 1.1 thorpej pd->pd_version++;
602 1.1 thorpej return (TRUE);
603 1.1 thorpej }
604 1.1 thorpej idx--;
605 1.1 thorpej }
606 1.1 thorpej
607 1.1 thorpej memmove(&pd->pd_array[idx + 2], &pd->pd_array[idx + 1],
608 1.1 thorpej (pd->pd_count - (idx + 1)) * sizeof(*pde));
609 1.1 thorpej pd->pd_array[idx + 1] = pde;
610 1.1 thorpej pd->pd_count++;
611 1.1 thorpej
612 1.1 thorpej pd->pd_version++;
613 1.1 thorpej
614 1.1 thorpej return (TRUE);
615 1.1 thorpej }
616 1.1 thorpej
617 1.1 thorpej /*
618 1.1 thorpej * prop_dictionary_set_keysym --
619 1.1 thorpej * Replace the object in the dictionary at the location encoded by
620 1.1 thorpej * the keysym.
621 1.1 thorpej */
622 1.1 thorpej boolean_t
623 1.1 thorpej prop_dictionary_set_keysym(prop_dictionary_t pd, prop_dictionary_keysym_t pde,
624 1.1 thorpej prop_object_t po)
625 1.1 thorpej {
626 1.1 thorpej prop_object_t opo;
627 1.1 thorpej
628 1.1 thorpej _PROP_ASSERT(prop_object_is_dictionary(pd));
629 1.1 thorpej _PROP_ASSERT(prop_object_is_dictionary_keysym(pde));
630 1.1 thorpej _PROP_ASSERT(pde->pde_objref != NULL);
631 1.1 thorpej
632 1.1 thorpej if (prop_dictionary_is_immutable(pd))
633 1.1 thorpej return (FALSE);
634 1.1 thorpej
635 1.1 thorpej opo = pde->pde_objref;
636 1.1 thorpej prop_object_retain(po);
637 1.1 thorpej pde->pde_objref = po;
638 1.1 thorpej prop_object_release(opo);
639 1.1 thorpej return (TRUE);
640 1.1 thorpej }
641 1.1 thorpej
642 1.1 thorpej static void
643 1.1 thorpej _prop_dictionary_remove(prop_dictionary_t pd, prop_dictionary_keysym_t pde,
644 1.1 thorpej unsigned int idx)
645 1.1 thorpej {
646 1.1 thorpej
647 1.1 thorpej _PROP_ASSERT(pd->pd_count != 0);
648 1.1 thorpej _PROP_ASSERT(idx < pd->pd_count);
649 1.1 thorpej _PROP_ASSERT(pd->pd_array[idx] == pde);
650 1.1 thorpej
651 1.1 thorpej idx++;
652 1.1 thorpej memmove(&pd->pd_array[idx - 1], &pd->pd_array[idx],
653 1.1 thorpej (pd->pd_count - idx) * sizeof(*pde));
654 1.1 thorpej pd->pd_count--;
655 1.1 thorpej pd->pd_version++;
656 1.1 thorpej
657 1.1 thorpej prop_object_release(pde);
658 1.1 thorpej }
659 1.1 thorpej
660 1.1 thorpej /*
661 1.1 thorpej * prop_dictionary_remove --
662 1.1 thorpej * Remove the reference to an object with the specified key from
663 1.1 thorpej * the dictionary.
664 1.1 thorpej */
665 1.1 thorpej void
666 1.1 thorpej prop_dictionary_remove(prop_dictionary_t pd, const char *key)
667 1.1 thorpej {
668 1.1 thorpej prop_dictionary_keysym_t pde;
669 1.1 thorpej unsigned int idx;
670 1.1 thorpej
671 1.1 thorpej _PROP_ASSERT(prop_object_is_dictionary(pd));
672 1.1 thorpej
673 1.1 thorpej /* XXX Should this be a _PROP_ASSERT()? */
674 1.1 thorpej if (prop_dictionary_is_immutable(pd))
675 1.1 thorpej return;
676 1.1 thorpej
677 1.1 thorpej pde = _prop_dict_lookup(pd, key, &idx);
678 1.1 thorpej /* XXX Should this be a _PROP_ASSERT()? */
679 1.1 thorpej if (pde == NULL)
680 1.1 thorpej return;
681 1.1 thorpej
682 1.1 thorpej _prop_dictionary_remove(pd, pde, idx);
683 1.1 thorpej }
684 1.1 thorpej
685 1.1 thorpej /*
686 1.1 thorpej * prop_dictionary_remove_keysym --
687 1.1 thorpej * Remove a reference to an object stored in the dictionary at the
688 1.1 thorpej * location encoded by the keysym.
689 1.1 thorpej */
690 1.1 thorpej void
691 1.1 thorpej prop_dictionary_remove_keysym(prop_dictionary_t pd,
692 1.1 thorpej prop_dictionary_keysym_t pde)
693 1.1 thorpej {
694 1.1 thorpej prop_dictionary_keysym_t opde;
695 1.1 thorpej unsigned int idx;
696 1.1 thorpej
697 1.1 thorpej _PROP_ASSERT(prop_object_is_dictionary(pd));
698 1.1 thorpej _PROP_ASSERT(prop_object_is_dictionary_keysym(pde));
699 1.1 thorpej _PROP_ASSERT(pde->pde_objref != NULL);
700 1.1 thorpej
701 1.1 thorpej /* XXX Should this be a _PROP_ASSERT()? */
702 1.1 thorpej if (prop_dictionary_is_immutable(pd))
703 1.1 thorpej return;
704 1.1 thorpej
705 1.1 thorpej opde = _prop_dict_lookup(pd, pde->pde_key, &idx);
706 1.1 thorpej _PROP_ASSERT(opde == pde);
707 1.1 thorpej
708 1.1 thorpej _prop_dictionary_remove(pd, opde, idx);
709 1.1 thorpej }
710 1.1 thorpej
711 1.1 thorpej /*
712 1.1 thorpej * prop_dictionary_keysym_cstring_nocopy --
713 1.1 thorpej * Return an immutable reference to the keysym's value.
714 1.1 thorpej */
715 1.1 thorpej const char *
716 1.1 thorpej prop_dictionary_keysym_cstring_nocopy(prop_dictionary_keysym_t pde)
717 1.1 thorpej {
718 1.1 thorpej
719 1.1 thorpej _PROP_ASSERT(prop_object_is_dictionary_keysym(pde));
720 1.1 thorpej return (pde->pde_key);
721 1.1 thorpej }
722 1.1 thorpej
723 1.1 thorpej /*
724 1.1 thorpej * prop_dictionary_externalize --
725 1.1 thorpej * Externalize a dictionary, returning a NUL-terminated buffer
726 1.1 thorpej * containing the XML-style representation. The buffer is allocated
727 1.1 thorpej * with the M_TEMP memory type.
728 1.1 thorpej */
729 1.1 thorpej char *
730 1.1 thorpej prop_dictionary_externalize(prop_dictionary_t pd)
731 1.1 thorpej {
732 1.1 thorpej struct _prop_object_externalize_context *ctx;
733 1.1 thorpej char *cp;
734 1.1 thorpej
735 1.1 thorpej ctx = _prop_object_externalize_context_alloc();
736 1.1 thorpej if (ctx == NULL)
737 1.1 thorpej return (NULL);
738 1.1 thorpej
739 1.1 thorpej if (_prop_object_externalize_start_tag(ctx, "plist") == FALSE ||
740 1.1 thorpej _prop_object_externalize_append_char(ctx, '\n') == FALSE ||
741 1.1 thorpej (*pd->pd_obj.po_extern)(ctx, pd) == FALSE ||
742 1.1 thorpej _prop_object_externalize_end_tag(ctx, "plist") == FALSE ||
743 1.1 thorpej _prop_object_externalize_append_char(ctx, '\0') == FALSE) {
744 1.1 thorpej /* We are responsible for releasing the buffer. */
745 1.1 thorpej _PROP_FREE(ctx->poec_buf, M_TEMP);
746 1.1 thorpej _prop_object_externalize_context_free(ctx);
747 1.1 thorpej return (NULL);
748 1.1 thorpej }
749 1.1 thorpej
750 1.1 thorpej cp = ctx->poec_buf;
751 1.1 thorpej _prop_object_externalize_context_free(ctx);
752 1.1 thorpej
753 1.1 thorpej return (cp);
754 1.1 thorpej }
755 1.1 thorpej
756 1.1 thorpej /*
757 1.1 thorpej * _prop_dictionary_internalize --
758 1.1 thorpej * Parse a <dict>...</dict> and return the object created from the
759 1.1 thorpej * external representation.
760 1.1 thorpej */
761 1.1 thorpej prop_object_t
762 1.1 thorpej _prop_dictionary_internalize(struct _prop_object_internalize_context *ctx)
763 1.1 thorpej {
764 1.1 thorpej prop_dictionary_t dict;
765 1.1 thorpej prop_object_t val;
766 1.1 thorpej size_t keylen;
767 1.1 thorpej char *tmpkey;
768 1.1 thorpej
769 1.1 thorpej /* We don't currently understand any attributes. */
770 1.1 thorpej if (ctx->poic_tagattr != NULL)
771 1.1 thorpej return (NULL);
772 1.1 thorpej
773 1.1 thorpej dict = prop_dictionary_create();
774 1.1 thorpej if (dict == NULL)
775 1.1 thorpej return (NULL);
776 1.1 thorpej
777 1.1 thorpej if (ctx->poic_is_empty_element)
778 1.1 thorpej return (dict);
779 1.1 thorpej
780 1.1 thorpej tmpkey = _PROP_MALLOC(PDE_MAXKEY + 1, M_TEMP);
781 1.1 thorpej if (tmpkey == NULL)
782 1.1 thorpej goto bad;
783 1.1 thorpej
784 1.1 thorpej for (;;) {
785 1.1 thorpej /* Fetch the next tag. */
786 1.1 thorpej if (_prop_object_internalize_find_tag(ctx, NULL,
787 1.1 thorpej _PROP_TAG_TYPE_EITHER) == FALSE)
788 1.1 thorpej goto bad;
789 1.1 thorpej
790 1.1 thorpej /* Check to see if this is the end of the dictionary. */
791 1.1 thorpej if (_PROP_TAG_MATCH(ctx, "dict") &&
792 1.1 thorpej ctx->poic_tag_type == _PROP_TAG_TYPE_END)
793 1.1 thorpej break;
794 1.1 thorpej
795 1.1 thorpej /* Ok, it must be a non-empty key start tag. */
796 1.1 thorpej if (!_PROP_TAG_MATCH(ctx, "key") ||
797 1.1 thorpej ctx->poic_tag_type != _PROP_TAG_TYPE_START ||
798 1.1 thorpej ctx->poic_is_empty_element)
799 1.1 thorpej goto bad;
800 1.1 thorpej
801 1.1 thorpej if (_prop_object_internalize_decode_string(ctx,
802 1.1 thorpej tmpkey, PDE_MAXKEY, &keylen,
803 1.1 thorpej &ctx->poic_cp) == FALSE)
804 1.1 thorpej goto bad;
805 1.1 thorpej
806 1.1 thorpej _PROP_ASSERT(keylen <= PDE_MAXKEY);
807 1.1 thorpej tmpkey[keylen] = '\0';
808 1.1 thorpej
809 1.1 thorpej if (_prop_object_internalize_find_tag(ctx, "key",
810 1.1 thorpej _PROP_TAG_TYPE_END) == FALSE)
811 1.1 thorpej goto bad;
812 1.1 thorpej
813 1.1 thorpej /* ..and now the beginning of the value. */
814 1.1 thorpej if (_prop_object_internalize_find_tag(ctx, NULL,
815 1.1 thorpej _PROP_TAG_TYPE_START) == FALSE)
816 1.1 thorpej goto bad;
817 1.1 thorpej
818 1.1 thorpej val = _prop_object_internalize_by_tag(ctx);
819 1.1 thorpej if (val == NULL)
820 1.1 thorpej goto bad;
821 1.1 thorpej
822 1.1 thorpej if (prop_dictionary_set(dict, tmpkey, val) == FALSE) {
823 1.1 thorpej prop_object_release(val);
824 1.1 thorpej goto bad;
825 1.1 thorpej }
826 1.1 thorpej prop_object_release(val);
827 1.1 thorpej }
828 1.1 thorpej
829 1.1 thorpej _PROP_FREE(tmpkey, M_TEMP);
830 1.1 thorpej return (dict);
831 1.1 thorpej
832 1.1 thorpej bad:
833 1.1 thorpej if (tmpkey != NULL)
834 1.1 thorpej _PROP_FREE(tmpkey, M_TEMP);
835 1.1 thorpej prop_object_release(dict);
836 1.1 thorpej return (NULL);
837 1.1 thorpej }
838 1.1 thorpej
839 1.1 thorpej /*
840 1.1 thorpej * prop_dictionary_internalize --
841 1.1 thorpej * Create a dictionary by parsing the NUL-terminated XML-style
842 1.1 thorpej * representation.
843 1.1 thorpej */
844 1.1 thorpej prop_dictionary_t
845 1.1 thorpej prop_dictionary_internalize(const char *xml)
846 1.1 thorpej {
847 1.1 thorpej prop_dictionary_t dict = NULL;
848 1.1 thorpej struct _prop_object_internalize_context *ctx;
849 1.1 thorpej
850 1.1 thorpej ctx = _prop_object_internalize_context_alloc(xml);
851 1.1 thorpej if (ctx == NULL)
852 1.1 thorpej return (NULL);
853 1.1 thorpej
854 1.1 thorpej /* We start with a <plist> tag. */
855 1.1 thorpej if (_prop_object_internalize_find_tag(ctx, "plist",
856 1.1 thorpej _PROP_TAG_TYPE_START) == FALSE)
857 1.1 thorpej goto out;
858 1.1 thorpej
859 1.1 thorpej /* Plist elements cannot be empty. */
860 1.1 thorpej if (ctx->poic_is_empty_element)
861 1.1 thorpej goto out;
862 1.1 thorpej
863 1.1 thorpej /*
864 1.1 thorpej * We don't understand any plist attributes, but Apple XML
865 1.1 thorpej * property lists often have a "version" attibute. If we
866 1.1 thorpej * see that one, we simply ignore it.
867 1.1 thorpej */
868 1.1 thorpej if (ctx->poic_tagattr != NULL &&
869 1.1 thorpej !_PROP_TAGATTR_MATCH(ctx, "version"))
870 1.1 thorpej goto out;
871 1.1 thorpej
872 1.1 thorpej /* Next we expect to see <dict>. */
873 1.1 thorpej if (_prop_object_internalize_find_tag(ctx, "dict",
874 1.1 thorpej _PROP_TAG_TYPE_START) == FALSE)
875 1.1 thorpej goto out;
876 1.1 thorpej
877 1.1 thorpej dict = _prop_dictionary_internalize(ctx);
878 1.1 thorpej if (dict == NULL)
879 1.1 thorpej goto out;
880 1.1 thorpej
881 1.1 thorpej /* We've advanced past </dict>. Now we want </plist>. */
882 1.1 thorpej if (_prop_object_internalize_find_tag(ctx, "plist",
883 1.1 thorpej _PROP_TAG_TYPE_END) == FALSE) {
884 1.1 thorpej prop_object_release(dict);
885 1.1 thorpej dict = NULL;
886 1.1 thorpej }
887 1.1 thorpej
888 1.1 thorpej out:
889 1.1 thorpej _prop_object_internalize_context_free(ctx);
890 1.1 thorpej return (dict);
891 1.1 thorpej }
892