prop_string.c revision 1.1 1 1.1 thorpej /* $NetBSD: prop_string.c,v 1.1 2006/04/27 20:11:27 thorpej 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_string.h>
40 1.1 thorpej #include "prop_object_impl.h"
41 1.1 thorpej
42 1.1 thorpej struct _prop_string {
43 1.1 thorpej struct _prop_object ps_obj;
44 1.1 thorpej union {
45 1.1 thorpej char * psu_mutable;
46 1.1 thorpej const char * psu_immutable;
47 1.1 thorpej } ps_un;
48 1.1 thorpej #define ps_mutable ps_un.psu_mutable
49 1.1 thorpej #define ps_immutable ps_un.psu_immutable
50 1.1 thorpej size_t ps_size; /* not including \0 */
51 1.1 thorpej int ps_flags;
52 1.1 thorpej };
53 1.1 thorpej
54 1.1 thorpej #define PS_F_NOCOPY 0x01
55 1.1 thorpej
56 1.1 thorpej _PROP_POOL_INIT(_prop_string_pool, sizeof(struct _prop_string), "propstng")
57 1.1 thorpej
58 1.1 thorpej _PROP_MALLOC_DEFINE(M_PROP_STRING, "prop string",
59 1.1 thorpej "property string container object")
60 1.1 thorpej
61 1.1 thorpej #define prop_object_is_string(x) ((x)->ps_obj.po_type == PROP_TYPE_STRING)
62 1.1 thorpej #define prop_string_contents(x) ((x)->ps_immutable ? (x)->ps_immutable : "")
63 1.1 thorpej
64 1.1 thorpej static void
65 1.1 thorpej _prop_string_free(void *v)
66 1.1 thorpej {
67 1.1 thorpej prop_string_t ps = v;
68 1.1 thorpej
69 1.1 thorpej if ((ps->ps_flags & PS_F_NOCOPY) == 0 && ps->ps_mutable != NULL)
70 1.1 thorpej _PROP_FREE(ps->ps_mutable, M_PROP_STRING);
71 1.1 thorpej _PROP_POOL_PUT(_prop_string_pool, v);
72 1.1 thorpej }
73 1.1 thorpej
74 1.1 thorpej static boolean_t
75 1.1 thorpej _prop_string_externalize(struct _prop_object_externalize_context *ctx,
76 1.1 thorpej void *v)
77 1.1 thorpej {
78 1.1 thorpej prop_string_t ps = v;
79 1.1 thorpej
80 1.1 thorpej if (ps->ps_size == 0)
81 1.1 thorpej return (_prop_object_externalize_empty_tag(ctx, "string"));
82 1.1 thorpej
83 1.1 thorpej if (_prop_object_externalize_start_tag(ctx, "string") == FALSE ||
84 1.1 thorpej _prop_object_externalize_append_encoded_cstring(ctx,
85 1.1 thorpej ps->ps_immutable) == FALSE ||
86 1.1 thorpej _prop_object_externalize_end_tag(ctx, "string") == FALSE)
87 1.1 thorpej return (FALSE);
88 1.1 thorpej
89 1.1 thorpej return (TRUE);
90 1.1 thorpej }
91 1.1 thorpej
92 1.1 thorpej static prop_string_t
93 1.1 thorpej _prop_string_alloc(void)
94 1.1 thorpej {
95 1.1 thorpej prop_string_t ps;
96 1.1 thorpej
97 1.1 thorpej ps = _PROP_POOL_GET(_prop_string_pool);
98 1.1 thorpej if (ps != NULL) {
99 1.1 thorpej _prop_object_init(&ps->ps_obj);
100 1.1 thorpej ps->ps_obj.po_type = PROP_TYPE_STRING;
101 1.1 thorpej ps->ps_obj.po_free = _prop_string_free;
102 1.1 thorpej ps->ps_obj.po_extern = _prop_string_externalize;
103 1.1 thorpej
104 1.1 thorpej ps->ps_mutable = NULL;
105 1.1 thorpej ps->ps_size = 0;
106 1.1 thorpej ps->ps_flags = 0;
107 1.1 thorpej }
108 1.1 thorpej
109 1.1 thorpej return (ps);
110 1.1 thorpej }
111 1.1 thorpej
112 1.1 thorpej /*
113 1.1 thorpej * prop_string_create --
114 1.1 thorpej * Create an empty mutable string.
115 1.1 thorpej */
116 1.1 thorpej prop_string_t
117 1.1 thorpej prop_string_create(void)
118 1.1 thorpej {
119 1.1 thorpej
120 1.1 thorpej return (_prop_string_alloc());
121 1.1 thorpej }
122 1.1 thorpej
123 1.1 thorpej /*
124 1.1 thorpej * prop_string_create_cstring --
125 1.1 thorpej * Create a string that contains a copy of the provided C string.
126 1.1 thorpej */
127 1.1 thorpej prop_string_t
128 1.1 thorpej prop_string_create_cstring(const char *str)
129 1.1 thorpej {
130 1.1 thorpej prop_string_t ps;
131 1.1 thorpej char *cp;
132 1.1 thorpej size_t len;
133 1.1 thorpej
134 1.1 thorpej ps = _prop_string_alloc();
135 1.1 thorpej if (ps != NULL) {
136 1.1 thorpej len = strlen(str);
137 1.1 thorpej cp = _PROP_MALLOC(len + 1, M_PROP_STRING);
138 1.1 thorpej if (cp == NULL) {
139 1.1 thorpej _prop_string_free(ps);
140 1.1 thorpej return (NULL);
141 1.1 thorpej }
142 1.1 thorpej strcpy(cp, str);
143 1.1 thorpej ps->ps_mutable = cp;
144 1.1 thorpej ps->ps_size = len;
145 1.1 thorpej }
146 1.1 thorpej return (ps);
147 1.1 thorpej }
148 1.1 thorpej
149 1.1 thorpej /*
150 1.1 thorpej * prop_string_create_cstring_nocopy --
151 1.1 thorpej * Create an immutable string that contains a refrence to the
152 1.1 thorpej * provided C string.
153 1.1 thorpej */
154 1.1 thorpej prop_string_t
155 1.1 thorpej prop_string_create_cstring_nocopy(const char *str)
156 1.1 thorpej {
157 1.1 thorpej prop_string_t ps;
158 1.1 thorpej
159 1.1 thorpej ps = _prop_string_alloc();
160 1.1 thorpej if (ps != NULL) {
161 1.1 thorpej ps->ps_immutable = str;
162 1.1 thorpej ps->ps_size = strlen(str);
163 1.1 thorpej ps->ps_flags |= PS_F_NOCOPY;
164 1.1 thorpej }
165 1.1 thorpej return (ps);
166 1.1 thorpej }
167 1.1 thorpej
168 1.1 thorpej /*
169 1.1 thorpej * prop_string_copy --
170 1.1 thorpej * Copy a string. If the original string is immutable, then the
171 1.1 thorpej * copy is also immutable and references the same external data.
172 1.1 thorpej */
173 1.1 thorpej prop_string_t
174 1.1 thorpej prop_string_copy(prop_string_t ops)
175 1.1 thorpej {
176 1.1 thorpej prop_string_t ps;
177 1.1 thorpej
178 1.1 thorpej _PROP_ASSERT(prop_object_is_string(ops));
179 1.1 thorpej
180 1.1 thorpej ps = _prop_string_alloc();
181 1.1 thorpej if (ps != NULL) {
182 1.1 thorpej ps->ps_size = ops->ps_size;
183 1.1 thorpej ps->ps_flags = ops->ps_flags;
184 1.1 thorpej if (ops->ps_flags & PS_F_NOCOPY)
185 1.1 thorpej ps->ps_immutable = ops->ps_immutable;
186 1.1 thorpej else {
187 1.1 thorpej char *cp = _PROP_MALLOC(ps->ps_size + 1, M_PROP_STRING);
188 1.1 thorpej if (cp == NULL) {
189 1.1 thorpej _prop_string_free(ps);
190 1.1 thorpej return (NULL);
191 1.1 thorpej }
192 1.1 thorpej strcpy(cp, prop_string_contents(ops));
193 1.1 thorpej ps->ps_mutable = cp;
194 1.1 thorpej }
195 1.1 thorpej }
196 1.1 thorpej return (ps);
197 1.1 thorpej }
198 1.1 thorpej
199 1.1 thorpej /*
200 1.1 thorpej * prop_string_copy_mutable --
201 1.1 thorpej * Copy a string, always returning a mutable copy.
202 1.1 thorpej */
203 1.1 thorpej prop_string_t
204 1.1 thorpej prop_string_copy_mutable(prop_string_t ops)
205 1.1 thorpej {
206 1.1 thorpej prop_string_t ps;
207 1.1 thorpej char *cp;
208 1.1 thorpej
209 1.1 thorpej _PROP_ASSERT(prop_object_is_string(ops));
210 1.1 thorpej
211 1.1 thorpej ps = _prop_string_alloc();
212 1.1 thorpej if (ps != NULL) {
213 1.1 thorpej ps->ps_size = ops->ps_size;
214 1.1 thorpej cp = _PROP_MALLOC(ps->ps_size + 1, M_PROP_STRING);
215 1.1 thorpej if (cp == NULL) {
216 1.1 thorpej _prop_string_free(ps);
217 1.1 thorpej return (NULL);
218 1.1 thorpej }
219 1.1 thorpej strcpy(cp, prop_string_contents(ops));
220 1.1 thorpej ps->ps_mutable = cp;
221 1.1 thorpej }
222 1.1 thorpej return (ps);
223 1.1 thorpej }
224 1.1 thorpej
225 1.1 thorpej /*
226 1.1 thorpej * prop_string_size --
227 1.1 thorpej * Return the size of the string, no including the terminating NUL.
228 1.1 thorpej */
229 1.1 thorpej size_t
230 1.1 thorpej prop_string_size(prop_string_t ps)
231 1.1 thorpej {
232 1.1 thorpej
233 1.1 thorpej _PROP_ASSERT(prop_object_is_string(ps));
234 1.1 thorpej return (ps->ps_size);
235 1.1 thorpej }
236 1.1 thorpej
237 1.1 thorpej /*
238 1.1 thorpej * prop_string_mutable --
239 1.1 thorpej * Return TRUE if the string is a mutable string.
240 1.1 thorpej */
241 1.1 thorpej boolean_t
242 1.1 thorpej prop_string_mutable(prop_string_t ps)
243 1.1 thorpej {
244 1.1 thorpej
245 1.1 thorpej _PROP_ASSERT(prop_object_is_string(ps));
246 1.1 thorpej return ((ps->ps_flags & PS_F_NOCOPY) == 0);
247 1.1 thorpej }
248 1.1 thorpej
249 1.1 thorpej /*
250 1.1 thorpej * prop_string_cstring --
251 1.1 thorpej * Return a copy of the contents of the string as a C string.
252 1.1 thorpej * The string is allocated with the M_TEMP malloc type.
253 1.1 thorpej */
254 1.1 thorpej char *
255 1.1 thorpej prop_string_cstring(prop_string_t ps)
256 1.1 thorpej {
257 1.1 thorpej char *cp;
258 1.1 thorpej
259 1.1 thorpej _PROP_ASSERT(prop_object_is_string(ps));
260 1.1 thorpej cp = _PROP_MALLOC(ps->ps_size + 1, M_TEMP);
261 1.1 thorpej if (cp != NULL)
262 1.1 thorpej strcpy(cp, prop_string_contents(ps));
263 1.1 thorpej
264 1.1 thorpej return (cp);
265 1.1 thorpej }
266 1.1 thorpej
267 1.1 thorpej /*
268 1.1 thorpej * prop_string_cstring_nocopy --
269 1.1 thorpej * Return an immutable reference to the contents of the string
270 1.1 thorpej * as a C string.
271 1.1 thorpej */
272 1.1 thorpej const char *
273 1.1 thorpej prop_string_cstring_nocopy(prop_string_t ps)
274 1.1 thorpej {
275 1.1 thorpej
276 1.1 thorpej _PROP_ASSERT(prop_object_is_string(ps));
277 1.1 thorpej return (prop_string_contents(ps));
278 1.1 thorpej }
279 1.1 thorpej
280 1.1 thorpej /*
281 1.1 thorpej * prop_string_append --
282 1.1 thorpej * Append the contents of one string to another. Returns TRUE
283 1.1 thorpej * upon success. The destination string must be mutable.
284 1.1 thorpej */
285 1.1 thorpej boolean_t
286 1.1 thorpej prop_string_append(prop_string_t dst, prop_string_t src)
287 1.1 thorpej {
288 1.1 thorpej char *ocp, *cp;
289 1.1 thorpej size_t len;
290 1.1 thorpej
291 1.1 thorpej _PROP_ASSERT(prop_object_is_string(dst));
292 1.1 thorpej _PROP_ASSERT(prop_object_is_string(src));
293 1.1 thorpej
294 1.1 thorpej if (dst->ps_flags & PS_F_NOCOPY)
295 1.1 thorpej return (FALSE);
296 1.1 thorpej
297 1.1 thorpej len = dst->ps_size + src->ps_size;
298 1.1 thorpej cp = _PROP_MALLOC(len + 1, M_PROP_STRING);
299 1.1 thorpej if (cp == NULL)
300 1.1 thorpej return (FALSE);
301 1.1 thorpej sprintf(cp, "%s%s", prop_string_contents(dst),
302 1.1 thorpej prop_string_contents(src));
303 1.1 thorpej ocp = dst->ps_mutable;
304 1.1 thorpej dst->ps_mutable = cp;
305 1.1 thorpej dst->ps_size = len;
306 1.1 thorpej if (ocp != NULL)
307 1.1 thorpej _PROP_FREE(ocp, M_PROP_STRING);
308 1.1 thorpej
309 1.1 thorpej return (TRUE);
310 1.1 thorpej }
311 1.1 thorpej
312 1.1 thorpej /*
313 1.1 thorpej * prop_string_append_cstring --
314 1.1 thorpej * Append a C string to a string. Returns TRUE upon success.
315 1.1 thorpej * The destination string must be mutable.
316 1.1 thorpej */
317 1.1 thorpej boolean_t
318 1.1 thorpej prop_string_append_cstring(prop_string_t dst, const char *src)
319 1.1 thorpej {
320 1.1 thorpej char *ocp, *cp;
321 1.1 thorpej size_t len;
322 1.1 thorpej
323 1.1 thorpej _PROP_ASSERT(src != NULL);
324 1.1 thorpej _PROP_ASSERT(prop_object_is_string(dst));
325 1.1 thorpej
326 1.1 thorpej if (dst->ps_flags & PS_F_NOCOPY)
327 1.1 thorpej return (FALSE);
328 1.1 thorpej
329 1.1 thorpej len = dst->ps_size + strlen(src);
330 1.1 thorpej cp = _PROP_MALLOC(len + 1, M_PROP_STRING);
331 1.1 thorpej if (cp == NULL)
332 1.1 thorpej return (FALSE);
333 1.1 thorpej sprintf(cp, "%s%s", prop_string_contents(dst), src);
334 1.1 thorpej ocp = dst->ps_mutable;
335 1.1 thorpej dst->ps_mutable = cp;
336 1.1 thorpej dst->ps_size = len;
337 1.1 thorpej if (ocp != NULL)
338 1.1 thorpej _PROP_FREE(ocp, M_PROP_STRING);
339 1.1 thorpej
340 1.1 thorpej return (TRUE);
341 1.1 thorpej }
342 1.1 thorpej
343 1.1 thorpej /*
344 1.1 thorpej * prop_string_equals --
345 1.1 thorpej * Return TRUE if two strings are equivalent.
346 1.1 thorpej */
347 1.1 thorpej boolean_t
348 1.1 thorpej prop_string_equals(prop_string_t str1, prop_string_t str2)
349 1.1 thorpej {
350 1.1 thorpej
351 1.1 thorpej _PROP_ASSERT(prop_object_is_string(str1));
352 1.1 thorpej _PROP_ASSERT(prop_object_is_string(str2));
353 1.1 thorpej if (str1 == str2)
354 1.1 thorpej return (TRUE);
355 1.1 thorpej if (str1->ps_size != str2->ps_size)
356 1.1 thorpej return (FALSE);
357 1.1 thorpej return (strcmp(prop_string_contents(str1),
358 1.1 thorpej prop_string_contents(str2)) == 0);
359 1.1 thorpej }
360 1.1 thorpej
361 1.1 thorpej /*
362 1.1 thorpej * prop_string_equals_cstring --
363 1.1 thorpej * Return TRUE if the string is equivalent to the specified
364 1.1 thorpej * C string.
365 1.1 thorpej */
366 1.1 thorpej boolean_t
367 1.1 thorpej prop_string_equals_cstring(prop_string_t ps, const char *cp)
368 1.1 thorpej {
369 1.1 thorpej
370 1.1 thorpej _PROP_ASSERT(prop_object_is_string(ps));
371 1.1 thorpej return (strcmp(prop_string_contents(ps), cp) == 0);
372 1.1 thorpej }
373 1.1 thorpej
374 1.1 thorpej /*
375 1.1 thorpej * _prop_string_internalize --
376 1.1 thorpej * Parse a <string>...</string> and return the object created from the
377 1.1 thorpej * external representation.
378 1.1 thorpej */
379 1.1 thorpej prop_object_t
380 1.1 thorpej _prop_string_internalize(struct _prop_object_internalize_context *ctx)
381 1.1 thorpej {
382 1.1 thorpej prop_string_t string;
383 1.1 thorpej char *str;
384 1.1 thorpej size_t len, alen;
385 1.1 thorpej
386 1.1 thorpej if (ctx->poic_is_empty_element)
387 1.1 thorpej return (prop_string_create());
388 1.1 thorpej
389 1.1 thorpej /* No attributes recognized here. */
390 1.1 thorpej if (ctx->poic_tagattr != NULL)
391 1.1 thorpej return (NULL);
392 1.1 thorpej
393 1.1 thorpej /* Compute the length of the result. */
394 1.1 thorpej if (_prop_object_internalize_decode_string(ctx, NULL, 0, &len,
395 1.1 thorpej NULL) == FALSE)
396 1.1 thorpej return (NULL);
397 1.1 thorpej
398 1.1 thorpej str = _PROP_MALLOC(len + 1, M_PROP_STRING);
399 1.1 thorpej if (str == NULL)
400 1.1 thorpej return (NULL);
401 1.1 thorpej
402 1.1 thorpej if (_prop_object_internalize_decode_string(ctx, str, len, &alen,
403 1.1 thorpej &ctx->poic_cp) == FALSE ||
404 1.1 thorpej alen != len) {
405 1.1 thorpej _PROP_FREE(str, M_PROP_STRING);
406 1.1 thorpej return (NULL);
407 1.1 thorpej }
408 1.1 thorpej str[len] = '\0';
409 1.1 thorpej
410 1.1 thorpej if (_prop_object_internalize_find_tag(ctx, "string",
411 1.1 thorpej _PROP_TAG_TYPE_END) == FALSE) {
412 1.1 thorpej _PROP_FREE(str, M_PROP_STRING);
413 1.1 thorpej return (NULL);
414 1.1 thorpej }
415 1.1 thorpej
416 1.1 thorpej string = _prop_string_alloc();
417 1.1 thorpej if (string == NULL) {
418 1.1 thorpej _PROP_FREE(str, M_PROP_STRING);
419 1.1 thorpej return (NULL);
420 1.1 thorpej }
421 1.1 thorpej
422 1.1 thorpej string->ps_mutable = str;
423 1.1 thorpej string->ps_size = len;
424 1.1 thorpej
425 1.1 thorpej return (string);
426 1.1 thorpej }
427