1 /* $NetBSD: prop_object_impl.h,v 1.27 2008/08/03 04:00:12 thorpej Exp $ */
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #ifndef _PROPLIB_PROP_OBJECT_IMPL_H_
33 #define _PROPLIB_PROP_OBJECT_IMPL_H_
37 #include "prop_stack.h"
39 struct _prop_object_externalize_context
{
40 char * poec_buf
; /* string buffer */
41 size_t poec_capacity
; /* capacity of buffer */
42 size_t poec_len
; /* current length of string */
43 unsigned int poec_depth
; /* nesting depth */
46 bool _prop_object_externalize_start_tag(
47 struct _prop_object_externalize_context
*,
49 bool _prop_object_externalize_end_tag(
50 struct _prop_object_externalize_context
*,
52 bool _prop_object_externalize_empty_tag(
53 struct _prop_object_externalize_context
*,
55 bool _prop_object_externalize_append_cstring(
56 struct _prop_object_externalize_context
*,
58 bool _prop_object_externalize_append_encoded_cstring(
59 struct _prop_object_externalize_context
*,
61 bool _prop_object_externalize_append_char(
62 struct _prop_object_externalize_context
*,
64 bool _prop_object_externalize_header(
65 struct _prop_object_externalize_context
*);
66 bool _prop_object_externalize_footer(
67 struct _prop_object_externalize_context
*);
69 struct _prop_object_externalize_context
*
70 _prop_object_externalize_context_alloc(void);
71 void _prop_object_externalize_context_free(
72 struct _prop_object_externalize_context
*);
75 _PROP_TAG_TYPE_START
, /* e.g. <dict> */
76 _PROP_TAG_TYPE_END
, /* e.g. </dict> */
80 struct _prop_object_internalize_context
{
84 const char *poic_tag_start
;
86 const char *poic_tagname
;
87 size_t poic_tagname_len
;
88 const char *poic_tagattr
;
89 size_t poic_tagattr_len
;
90 const char *poic_tagattrval
;
91 size_t poic_tagattrval_len
;
93 bool poic_is_empty_element
;
94 _prop_tag_type_t poic_tag_type
;
98 _PROP_OBJECT_FREE_DONE
,
99 _PROP_OBJECT_FREE_RECURSE
,
100 _PROP_OBJECT_FREE_FAILED
101 } _prop_object_free_rv_t
;
104 _PROP_OBJECT_EQUALS_FALSE
,
105 _PROP_OBJECT_EQUALS_TRUE
,
106 _PROP_OBJECT_EQUALS_RECURSE
107 } _prop_object_equals_rv_t
;
109 #define _PROP_EOF(c) ((c) == '\0')
110 #define _PROP_ISSPACE(c) \
111 ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' || \
114 #define _PROP_TAG_MATCH(ctx, t) \
115 _prop_object_internalize_match((ctx)->poic_tagname, \
116 (ctx)->poic_tagname_len, \
119 #define _PROP_TAGATTR_MATCH(ctx, a) \
120 _prop_object_internalize_match((ctx)->poic_tagattr, \
121 (ctx)->poic_tagattr_len, \
124 #define _PROP_TAGATTRVAL_MATCH(ctx, a) \
125 _prop_object_internalize_match((ctx)->poic_tagattrval, \
126 (ctx)->poic_tagattrval_len,\
129 bool _prop_object_internalize_find_tag(
130 struct _prop_object_internalize_context
*,
131 const char *, _prop_tag_type_t
);
132 bool _prop_object_internalize_match(const char *, size_t,
133 const char *, size_t);
134 prop_object_t
_prop_object_internalize_by_tag(
135 struct _prop_object_internalize_context
*);
136 bool _prop_object_internalize_decode_string(
137 struct _prop_object_internalize_context
*,
138 char *, size_t, size_t *, const char **);
139 prop_object_t
_prop_generic_internalize(const char *, const char *);
141 struct _prop_object_internalize_context
*
142 _prop_object_internalize_context_alloc(const char *);
143 void _prop_object_internalize_context_free(
144 struct _prop_object_internalize_context
*);
146 bool _prop_object_externalize_write_file(const char *,
147 const char *, size_t);
149 struct _prop_object_internalize_mapped_file
{
151 size_t poimf_mapsize
;
154 struct _prop_object_internalize_mapped_file
*
155 _prop_object_internalize_map_file(const char *);
156 void _prop_object_internalize_unmap_file(
157 struct _prop_object_internalize_mapped_file
*);
159 typedef bool (*prop_object_internalizer_t
)(prop_stack_t
, prop_object_t
*,
160 struct _prop_object_internalize_context
*);
161 typedef bool (*prop_object_internalizer_continue_t
)(prop_stack_t
,
163 struct _prop_object_internalize_context
*,
164 void *, prop_object_t
);
166 /* These are here because they're required by shared code. */
167 bool _prop_array_internalize(prop_stack_t
, prop_object_t
*,
168 struct _prop_object_internalize_context
*);
169 bool _prop_bool_internalize(prop_stack_t
, prop_object_t
*,
170 struct _prop_object_internalize_context
*);
171 bool _prop_data_internalize(prop_stack_t
, prop_object_t
*,
172 struct _prop_object_internalize_context
*);
173 bool _prop_dictionary_internalize(prop_stack_t
, prop_object_t
*,
174 struct _prop_object_internalize_context
*);
175 bool _prop_number_internalize(prop_stack_t
, prop_object_t
*,
176 struct _prop_object_internalize_context
*);
177 bool _prop_string_internalize(prop_stack_t
, prop_object_t
*,
178 struct _prop_object_internalize_context
*);
180 struct _prop_object_type
{
183 /* func to free object */
184 _prop_object_free_rv_t
185 (*pot_free
)(prop_stack_t
, prop_object_t
*);
187 * func to free the child returned by pot_free with stack == NULL.
189 * Must be implemented if pot_free can return anything other than
190 * _PROP_OBJECT_FREE_DONE.
192 void (*pot_emergency_free
)(prop_object_t
);
193 /* func to externalize object */
194 bool (*pot_extern
)(struct _prop_object_externalize_context
*,
196 /* func to test quality */
197 _prop_object_equals_rv_t
198 (*pot_equals
)(prop_object_t
, prop_object_t
,
200 prop_object_t
*, prop_object_t
*);
202 * func to finish equality iteration.
204 * Must be implemented if pot_equals can return
205 * _PROP_OBJECT_EQUALS_RECURSE
207 void (*pot_equals_finish
)(prop_object_t
, prop_object_t
);
210 struct _prop_object
{
211 const struct _prop_object_type
*po_type
;/* type descriptor */
212 uint32_t po_refcnt
; /* reference count */
215 void _prop_object_init(struct _prop_object
*,
216 const struct _prop_object_type
*);
217 void _prop_object_fini(struct _prop_object
*);
219 struct _prop_object_iterator
{
220 prop_object_t (*pi_next_object
)(void *);
221 void (*pi_reset
)(void *);
222 prop_object_t pi_obj
;
227 * proplib in user space...
236 #define _PROP_ASSERT(x) /*LINTED*/assert(x)
238 #define _PROP_MALLOC(s, t) malloc((s))
239 #define _PROP_CALLOC(s, t) calloc(1, (s))
240 #define _PROP_REALLOC(v, s, t) realloc((v), (s))
241 #define _PROP_FREE(v, t) free((v))
243 #define _PROP_POOL_GET(p) malloc((p))
244 #define _PROP_POOL_PUT(p, v) free((v))
246 #define _PROP_POOL_INIT(p, s, d) static const size_t p = s;
248 #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */
251 * Use pthread mutexes everywhere else.
254 #define _PROP_MUTEX_DECL_STATIC(x) \
255 static pthread_mutex_t x = PTHREAD_MUTEX_INITIALIZER;
256 #define _PROP_MUTEX_LOCK(x) pthread_mutex_lock(&(x))
257 #define _PROP_MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
259 #define _PROP_RWLOCK_DECL(x) pthread_rwlock_t x ;
260 #define _PROP_RWLOCK_INIT(x) pthread_rwlock_init(&(x), NULL)
261 #define _PROP_RWLOCK_RDLOCK(x) pthread_rwlock_rdlock(&(x))
262 #define _PROP_RWLOCK_WRLOCK(x) pthread_rwlock_wrlock(&(x))
263 #define _PROP_RWLOCK_UNLOCK(x) pthread_rwlock_unlock(&(x))
264 #define _PROP_RWLOCK_DESTROY(x) pthread_rwlock_destroy(&(x))
269 #define _PROP_ARG_UNUSED /* delete */
271 #endif /* _PROPLIB_PROP_OBJECT_IMPL_H_ */