prop_string.c revision 1.9.6.1 1 1.9.6.1 yamt /* $NetBSD: prop_string.c,v 1.9.6.1 2008/05/18 12:28:47 yamt 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 *
19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.1 thorpej #include <prop/prop_string.h>
33 1.1 thorpej #include "prop_object_impl.h"
34 1.1 thorpej
35 1.1 thorpej struct _prop_string {
36 1.1 thorpej struct _prop_object ps_obj;
37 1.1 thorpej union {
38 1.1 thorpej char * psu_mutable;
39 1.1 thorpej const char * psu_immutable;
40 1.1 thorpej } ps_un;
41 1.1 thorpej #define ps_mutable ps_un.psu_mutable
42 1.1 thorpej #define ps_immutable ps_un.psu_immutable
43 1.1 thorpej size_t ps_size; /* not including \0 */
44 1.1 thorpej int ps_flags;
45 1.1 thorpej };
46 1.1 thorpej
47 1.1 thorpej #define PS_F_NOCOPY 0x01
48 1.1 thorpej
49 1.1 thorpej _PROP_POOL_INIT(_prop_string_pool, sizeof(struct _prop_string), "propstng")
50 1.1 thorpej
51 1.1 thorpej _PROP_MALLOC_DEFINE(M_PROP_STRING, "prop string",
52 1.1 thorpej "property string container object")
53 1.1 thorpej
54 1.8 joerg static int _prop_string_free(prop_stack_t, prop_object_t *);
55 1.7 thorpej static bool _prop_string_externalize(
56 1.2 thorpej struct _prop_object_externalize_context *,
57 1.2 thorpej void *);
58 1.9 joerg static bool _prop_string_equals(prop_object_t, prop_object_t,
59 1.9 joerg void **, void **,
60 1.9 joerg prop_object_t *, prop_object_t *);
61 1.2 thorpej
62 1.2 thorpej static const struct _prop_object_type _prop_object_type_string = {
63 1.2 thorpej .pot_type = PROP_TYPE_STRING,
64 1.2 thorpej .pot_free = _prop_string_free,
65 1.2 thorpej .pot_extern = _prop_string_externalize,
66 1.2 thorpej .pot_equals = _prop_string_equals,
67 1.2 thorpej };
68 1.2 thorpej
69 1.2 thorpej #define prop_object_is_string(x) \
70 1.4 thorpej ((x) != NULL && (x)->ps_obj.po_type == &_prop_object_type_string)
71 1.1 thorpej #define prop_string_contents(x) ((x)->ps_immutable ? (x)->ps_immutable : "")
72 1.1 thorpej
73 1.8 joerg /* ARGSUSED */
74 1.8 joerg static int
75 1.8 joerg _prop_string_free(prop_stack_t stack, prop_object_t *obj)
76 1.1 thorpej {
77 1.8 joerg prop_string_t ps = *obj;
78 1.1 thorpej
79 1.1 thorpej if ((ps->ps_flags & PS_F_NOCOPY) == 0 && ps->ps_mutable != NULL)
80 1.1 thorpej _PROP_FREE(ps->ps_mutable, M_PROP_STRING);
81 1.8 joerg _PROP_POOL_PUT(_prop_string_pool, ps);
82 1.8 joerg
83 1.8 joerg return (_PROP_OBJECT_FREE_DONE);
84 1.1 thorpej }
85 1.1 thorpej
86 1.7 thorpej static bool
87 1.1 thorpej _prop_string_externalize(struct _prop_object_externalize_context *ctx,
88 1.1 thorpej void *v)
89 1.1 thorpej {
90 1.1 thorpej prop_string_t ps = v;
91 1.1 thorpej
92 1.1 thorpej if (ps->ps_size == 0)
93 1.1 thorpej return (_prop_object_externalize_empty_tag(ctx, "string"));
94 1.1 thorpej
95 1.7 thorpej if (_prop_object_externalize_start_tag(ctx, "string") == false ||
96 1.1 thorpej _prop_object_externalize_append_encoded_cstring(ctx,
97 1.7 thorpej ps->ps_immutable) == false ||
98 1.7 thorpej _prop_object_externalize_end_tag(ctx, "string") == false)
99 1.7 thorpej return (false);
100 1.1 thorpej
101 1.7 thorpej return (true);
102 1.1 thorpej }
103 1.1 thorpej
104 1.9 joerg /* ARGSUSED */
105 1.7 thorpej static bool
106 1.9 joerg _prop_string_equals(prop_object_t v1, prop_object_t v2,
107 1.9 joerg void **stored_pointer1, void **stored_pointer2,
108 1.9 joerg prop_object_t *next_obj1, prop_object_t *next_obj2)
109 1.2 thorpej {
110 1.2 thorpej prop_string_t str1 = v1;
111 1.2 thorpej prop_string_t str2 = v2;
112 1.2 thorpej
113 1.2 thorpej if (str1 == str2)
114 1.9 joerg return (_PROP_OBJECT_EQUALS_TRUE);
115 1.2 thorpej if (str1->ps_size != str2->ps_size)
116 1.9 joerg return (_PROP_OBJECT_EQUALS_FALSE);
117 1.9 joerg if (strcmp(prop_string_contents(str1), prop_string_contents(str2)))
118 1.9 joerg return (_PROP_OBJECT_EQUALS_FALSE);
119 1.9 joerg else
120 1.9 joerg return (_PROP_OBJECT_EQUALS_TRUE);
121 1.2 thorpej }
122 1.2 thorpej
123 1.1 thorpej static prop_string_t
124 1.1 thorpej _prop_string_alloc(void)
125 1.1 thorpej {
126 1.1 thorpej prop_string_t ps;
127 1.1 thorpej
128 1.1 thorpej ps = _PROP_POOL_GET(_prop_string_pool);
129 1.1 thorpej if (ps != NULL) {
130 1.2 thorpej _prop_object_init(&ps->ps_obj, &_prop_object_type_string);
131 1.1 thorpej
132 1.1 thorpej ps->ps_mutable = NULL;
133 1.1 thorpej ps->ps_size = 0;
134 1.1 thorpej ps->ps_flags = 0;
135 1.1 thorpej }
136 1.1 thorpej
137 1.1 thorpej return (ps);
138 1.1 thorpej }
139 1.1 thorpej
140 1.1 thorpej /*
141 1.1 thorpej * prop_string_create --
142 1.1 thorpej * Create an empty mutable string.
143 1.1 thorpej */
144 1.1 thorpej prop_string_t
145 1.1 thorpej prop_string_create(void)
146 1.1 thorpej {
147 1.1 thorpej
148 1.1 thorpej return (_prop_string_alloc());
149 1.1 thorpej }
150 1.1 thorpej
151 1.1 thorpej /*
152 1.1 thorpej * prop_string_create_cstring --
153 1.1 thorpej * Create a string that contains a copy of the provided C string.
154 1.1 thorpej */
155 1.1 thorpej prop_string_t
156 1.1 thorpej prop_string_create_cstring(const char *str)
157 1.1 thorpej {
158 1.1 thorpej prop_string_t ps;
159 1.1 thorpej char *cp;
160 1.1 thorpej size_t len;
161 1.1 thorpej
162 1.1 thorpej ps = _prop_string_alloc();
163 1.1 thorpej if (ps != NULL) {
164 1.1 thorpej len = strlen(str);
165 1.1 thorpej cp = _PROP_MALLOC(len + 1, M_PROP_STRING);
166 1.1 thorpej if (cp == NULL) {
167 1.8 joerg prop_object_release(ps);
168 1.1 thorpej return (NULL);
169 1.1 thorpej }
170 1.1 thorpej strcpy(cp, str);
171 1.1 thorpej ps->ps_mutable = cp;
172 1.1 thorpej ps->ps_size = len;
173 1.1 thorpej }
174 1.1 thorpej return (ps);
175 1.1 thorpej }
176 1.1 thorpej
177 1.1 thorpej /*
178 1.1 thorpej * prop_string_create_cstring_nocopy --
179 1.1 thorpej * Create an immutable string that contains a refrence to the
180 1.1 thorpej * provided C string.
181 1.1 thorpej */
182 1.1 thorpej prop_string_t
183 1.1 thorpej prop_string_create_cstring_nocopy(const char *str)
184 1.1 thorpej {
185 1.1 thorpej prop_string_t ps;
186 1.1 thorpej
187 1.1 thorpej ps = _prop_string_alloc();
188 1.1 thorpej if (ps != NULL) {
189 1.1 thorpej ps->ps_immutable = str;
190 1.1 thorpej ps->ps_size = strlen(str);
191 1.1 thorpej ps->ps_flags |= PS_F_NOCOPY;
192 1.1 thorpej }
193 1.1 thorpej return (ps);
194 1.1 thorpej }
195 1.1 thorpej
196 1.1 thorpej /*
197 1.1 thorpej * prop_string_copy --
198 1.1 thorpej * Copy a string. If the original string is immutable, then the
199 1.1 thorpej * copy is also immutable and references the same external data.
200 1.1 thorpej */
201 1.1 thorpej prop_string_t
202 1.1 thorpej prop_string_copy(prop_string_t ops)
203 1.1 thorpej {
204 1.1 thorpej prop_string_t ps;
205 1.1 thorpej
206 1.3 thorpej if (! prop_object_is_string(ops))
207 1.3 thorpej return (NULL);
208 1.1 thorpej
209 1.1 thorpej ps = _prop_string_alloc();
210 1.1 thorpej if (ps != NULL) {
211 1.1 thorpej ps->ps_size = ops->ps_size;
212 1.1 thorpej ps->ps_flags = ops->ps_flags;
213 1.1 thorpej if (ops->ps_flags & PS_F_NOCOPY)
214 1.1 thorpej ps->ps_immutable = ops->ps_immutable;
215 1.1 thorpej else {
216 1.1 thorpej char *cp = _PROP_MALLOC(ps->ps_size + 1, M_PROP_STRING);
217 1.1 thorpej if (cp == NULL) {
218 1.8 joerg prop_object_release(ps);
219 1.1 thorpej return (NULL);
220 1.1 thorpej }
221 1.1 thorpej strcpy(cp, prop_string_contents(ops));
222 1.1 thorpej ps->ps_mutable = cp;
223 1.1 thorpej }
224 1.1 thorpej }
225 1.1 thorpej return (ps);
226 1.1 thorpej }
227 1.1 thorpej
228 1.1 thorpej /*
229 1.1 thorpej * prop_string_copy_mutable --
230 1.1 thorpej * Copy a string, always returning a mutable copy.
231 1.1 thorpej */
232 1.1 thorpej prop_string_t
233 1.1 thorpej prop_string_copy_mutable(prop_string_t ops)
234 1.1 thorpej {
235 1.1 thorpej prop_string_t ps;
236 1.1 thorpej char *cp;
237 1.1 thorpej
238 1.3 thorpej if (! prop_object_is_string(ops))
239 1.3 thorpej return (NULL);
240 1.1 thorpej
241 1.1 thorpej ps = _prop_string_alloc();
242 1.1 thorpej if (ps != NULL) {
243 1.1 thorpej ps->ps_size = ops->ps_size;
244 1.1 thorpej cp = _PROP_MALLOC(ps->ps_size + 1, M_PROP_STRING);
245 1.1 thorpej if (cp == NULL) {
246 1.8 joerg prop_object_release(ps);
247 1.1 thorpej return (NULL);
248 1.1 thorpej }
249 1.1 thorpej strcpy(cp, prop_string_contents(ops));
250 1.1 thorpej ps->ps_mutable = cp;
251 1.1 thorpej }
252 1.1 thorpej return (ps);
253 1.1 thorpej }
254 1.1 thorpej
255 1.1 thorpej /*
256 1.1 thorpej * prop_string_size --
257 1.3 thorpej * Return the size of the string, not including the terminating NUL.
258 1.1 thorpej */
259 1.1 thorpej size_t
260 1.1 thorpej prop_string_size(prop_string_t ps)
261 1.1 thorpej {
262 1.1 thorpej
263 1.3 thorpej if (! prop_object_is_string(ps))
264 1.3 thorpej return (0);
265 1.3 thorpej
266 1.1 thorpej return (ps->ps_size);
267 1.1 thorpej }
268 1.1 thorpej
269 1.1 thorpej /*
270 1.1 thorpej * prop_string_mutable --
271 1.7 thorpej * Return true if the string is a mutable string.
272 1.1 thorpej */
273 1.7 thorpej bool
274 1.1 thorpej prop_string_mutable(prop_string_t ps)
275 1.1 thorpej {
276 1.1 thorpej
277 1.3 thorpej if (! prop_object_is_string(ps))
278 1.7 thorpej return (false);
279 1.3 thorpej
280 1.1 thorpej return ((ps->ps_flags & PS_F_NOCOPY) == 0);
281 1.1 thorpej }
282 1.1 thorpej
283 1.1 thorpej /*
284 1.1 thorpej * prop_string_cstring --
285 1.1 thorpej * Return a copy of the contents of the string as a C string.
286 1.1 thorpej * The string is allocated with the M_TEMP malloc type.
287 1.1 thorpej */
288 1.1 thorpej char *
289 1.1 thorpej prop_string_cstring(prop_string_t ps)
290 1.1 thorpej {
291 1.1 thorpej char *cp;
292 1.1 thorpej
293 1.3 thorpej if (! prop_object_is_string(ps))
294 1.3 thorpej return (NULL);
295 1.3 thorpej
296 1.1 thorpej cp = _PROP_MALLOC(ps->ps_size + 1, M_TEMP);
297 1.1 thorpej if (cp != NULL)
298 1.1 thorpej strcpy(cp, prop_string_contents(ps));
299 1.1 thorpej
300 1.1 thorpej return (cp);
301 1.1 thorpej }
302 1.1 thorpej
303 1.1 thorpej /*
304 1.1 thorpej * prop_string_cstring_nocopy --
305 1.1 thorpej * Return an immutable reference to the contents of the string
306 1.1 thorpej * as a C string.
307 1.1 thorpej */
308 1.1 thorpej const char *
309 1.1 thorpej prop_string_cstring_nocopy(prop_string_t ps)
310 1.1 thorpej {
311 1.1 thorpej
312 1.3 thorpej if (! prop_object_is_string(ps))
313 1.3 thorpej return (NULL);
314 1.3 thorpej
315 1.1 thorpej return (prop_string_contents(ps));
316 1.1 thorpej }
317 1.1 thorpej
318 1.1 thorpej /*
319 1.1 thorpej * prop_string_append --
320 1.7 thorpej * Append the contents of one string to another. Returns true
321 1.1 thorpej * upon success. The destination string must be mutable.
322 1.1 thorpej */
323 1.7 thorpej bool
324 1.1 thorpej prop_string_append(prop_string_t dst, prop_string_t src)
325 1.1 thorpej {
326 1.1 thorpej char *ocp, *cp;
327 1.1 thorpej size_t len;
328 1.1 thorpej
329 1.3 thorpej if (! (prop_object_is_string(dst) &&
330 1.3 thorpej prop_object_is_string(src)))
331 1.7 thorpej return (false);
332 1.1 thorpej
333 1.1 thorpej if (dst->ps_flags & PS_F_NOCOPY)
334 1.7 thorpej return (false);
335 1.1 thorpej
336 1.1 thorpej len = dst->ps_size + src->ps_size;
337 1.1 thorpej cp = _PROP_MALLOC(len + 1, M_PROP_STRING);
338 1.1 thorpej if (cp == NULL)
339 1.7 thorpej return (false);
340 1.1 thorpej sprintf(cp, "%s%s", prop_string_contents(dst),
341 1.1 thorpej prop_string_contents(src));
342 1.1 thorpej ocp = dst->ps_mutable;
343 1.1 thorpej dst->ps_mutable = cp;
344 1.1 thorpej dst->ps_size = len;
345 1.1 thorpej if (ocp != NULL)
346 1.1 thorpej _PROP_FREE(ocp, M_PROP_STRING);
347 1.1 thorpej
348 1.7 thorpej return (true);
349 1.1 thorpej }
350 1.1 thorpej
351 1.1 thorpej /*
352 1.1 thorpej * prop_string_append_cstring --
353 1.7 thorpej * Append a C string to a string. Returns true upon success.
354 1.1 thorpej * The destination string must be mutable.
355 1.1 thorpej */
356 1.7 thorpej bool
357 1.1 thorpej prop_string_append_cstring(prop_string_t dst, const char *src)
358 1.1 thorpej {
359 1.1 thorpej char *ocp, *cp;
360 1.1 thorpej size_t len;
361 1.1 thorpej
362 1.3 thorpej if (! prop_object_is_string(dst))
363 1.7 thorpej return (false);
364 1.3 thorpej
365 1.1 thorpej _PROP_ASSERT(src != NULL);
366 1.1 thorpej
367 1.1 thorpej if (dst->ps_flags & PS_F_NOCOPY)
368 1.7 thorpej return (false);
369 1.1 thorpej
370 1.1 thorpej len = dst->ps_size + strlen(src);
371 1.1 thorpej cp = _PROP_MALLOC(len + 1, M_PROP_STRING);
372 1.1 thorpej if (cp == NULL)
373 1.7 thorpej return (false);
374 1.1 thorpej sprintf(cp, "%s%s", prop_string_contents(dst), src);
375 1.1 thorpej ocp = dst->ps_mutable;
376 1.1 thorpej dst->ps_mutable = cp;
377 1.1 thorpej dst->ps_size = len;
378 1.1 thorpej if (ocp != NULL)
379 1.1 thorpej _PROP_FREE(ocp, M_PROP_STRING);
380 1.1 thorpej
381 1.7 thorpej return (true);
382 1.1 thorpej }
383 1.1 thorpej
384 1.1 thorpej /*
385 1.1 thorpej * prop_string_equals --
386 1.7 thorpej * Return true if two strings are equivalent.
387 1.1 thorpej */
388 1.7 thorpej bool
389 1.1 thorpej prop_string_equals(prop_string_t str1, prop_string_t str2)
390 1.1 thorpej {
391 1.9 joerg if (!prop_object_is_string(str1) || !prop_object_is_string(str2))
392 1.9 joerg return (false);
393 1.1 thorpej
394 1.9 joerg return prop_object_equals(str1, str2);
395 1.1 thorpej }
396 1.1 thorpej
397 1.1 thorpej /*
398 1.1 thorpej * prop_string_equals_cstring --
399 1.7 thorpej * Return true if the string is equivalent to the specified
400 1.1 thorpej * C string.
401 1.1 thorpej */
402 1.7 thorpej bool
403 1.1 thorpej prop_string_equals_cstring(prop_string_t ps, const char *cp)
404 1.1 thorpej {
405 1.1 thorpej
406 1.3 thorpej if (! prop_object_is_string(ps))
407 1.7 thorpej return (false);
408 1.3 thorpej
409 1.1 thorpej return (strcmp(prop_string_contents(ps), cp) == 0);
410 1.1 thorpej }
411 1.1 thorpej
412 1.1 thorpej /*
413 1.1 thorpej * _prop_string_internalize --
414 1.1 thorpej * Parse a <string>...</string> and return the object created from the
415 1.1 thorpej * external representation.
416 1.1 thorpej */
417 1.8 joerg /* ARGSUSED */
418 1.8 joerg bool
419 1.8 joerg _prop_string_internalize(prop_stack_t stack, prop_object_t *obj,
420 1.8 joerg struct _prop_object_internalize_context *ctx)
421 1.1 thorpej {
422 1.1 thorpej prop_string_t string;
423 1.1 thorpej char *str;
424 1.1 thorpej size_t len, alen;
425 1.1 thorpej
426 1.8 joerg if (ctx->poic_is_empty_element) {
427 1.8 joerg *obj = prop_string_create();
428 1.8 joerg return (true);
429 1.8 joerg }
430 1.1 thorpej
431 1.1 thorpej /* No attributes recognized here. */
432 1.1 thorpej if (ctx->poic_tagattr != NULL)
433 1.8 joerg return (true);
434 1.1 thorpej
435 1.1 thorpej /* Compute the length of the result. */
436 1.6 martin if (_prop_object_internalize_decode_string(ctx, NULL, 0, &len,
437 1.7 thorpej NULL) == false)
438 1.8 joerg return (true);
439 1.1 thorpej
440 1.1 thorpej str = _PROP_MALLOC(len + 1, M_PROP_STRING);
441 1.1 thorpej if (str == NULL)
442 1.8 joerg return (true);
443 1.1 thorpej
444 1.1 thorpej if (_prop_object_internalize_decode_string(ctx, str, len, &alen,
445 1.7 thorpej &ctx->poic_cp) == false ||
446 1.1 thorpej alen != len) {
447 1.1 thorpej _PROP_FREE(str, M_PROP_STRING);
448 1.8 joerg return (true);
449 1.1 thorpej }
450 1.1 thorpej str[len] = '\0';
451 1.1 thorpej
452 1.1 thorpej if (_prop_object_internalize_find_tag(ctx, "string",
453 1.7 thorpej _PROP_TAG_TYPE_END) == false) {
454 1.1 thorpej _PROP_FREE(str, M_PROP_STRING);
455 1.8 joerg return (true);
456 1.1 thorpej }
457 1.1 thorpej
458 1.1 thorpej string = _prop_string_alloc();
459 1.1 thorpej if (string == NULL) {
460 1.1 thorpej _PROP_FREE(str, M_PROP_STRING);
461 1.8 joerg return (true);
462 1.1 thorpej }
463 1.1 thorpej
464 1.1 thorpej string->ps_mutable = str;
465 1.1 thorpej string->ps_size = len;
466 1.8 joerg *obj = string;
467 1.1 thorpej
468 1.8 joerg return (true);
469 1.1 thorpej }
470