prop_string.c revision 1.17 1 1.17 riastrad /* $NetBSD: prop_string.c,v 1.17 2022/08/03 21:13:46 riastradh Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.14 thorpej * Copyright (c) 2006, 2020 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.13 thorpej #include "prop_object_impl.h"
33 1.1 thorpej #include <prop/prop_string.h>
34 1.1 thorpej
35 1.14 thorpej #include <sys/rbtree.h>
36 1.14 thorpej #if defined(_KERNEL) || defined(_STANDALONE)
37 1.14 thorpej #include <sys/stdarg.h>
38 1.14 thorpej #else
39 1.14 thorpej #include <stdarg.h>
40 1.14 thorpej #endif /* _KERNEL || _STANDALONE */
41 1.14 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.14 thorpej struct rb_node ps_link;
52 1.1 thorpej int ps_flags;
53 1.1 thorpej };
54 1.1 thorpej
55 1.1 thorpej #define PS_F_NOCOPY 0x01
56 1.14 thorpej #define PS_F_MUTABLE 0x02
57 1.1 thorpej
58 1.1 thorpej _PROP_POOL_INIT(_prop_string_pool, sizeof(struct _prop_string), "propstng")
59 1.1 thorpej
60 1.1 thorpej _PROP_MALLOC_DEFINE(M_PROP_STRING, "prop string",
61 1.1 thorpej "property string container object")
62 1.1 thorpej
63 1.11 thorpej static _prop_object_free_rv_t
64 1.11 thorpej _prop_string_free(prop_stack_t, prop_object_t *);
65 1.7 thorpej static bool _prop_string_externalize(
66 1.2 thorpej struct _prop_object_externalize_context *,
67 1.2 thorpej void *);
68 1.11 thorpej static _prop_object_equals_rv_t
69 1.11 thorpej _prop_string_equals(prop_object_t, prop_object_t,
70 1.11 thorpej void **, void **,
71 1.11 thorpej prop_object_t *, prop_object_t *);
72 1.2 thorpej
73 1.2 thorpej static const struct _prop_object_type _prop_object_type_string = {
74 1.2 thorpej .pot_type = PROP_TYPE_STRING,
75 1.2 thorpej .pot_free = _prop_string_free,
76 1.2 thorpej .pot_extern = _prop_string_externalize,
77 1.2 thorpej .pot_equals = _prop_string_equals,
78 1.2 thorpej };
79 1.2 thorpej
80 1.2 thorpej #define prop_object_is_string(x) \
81 1.4 thorpej ((x) != NULL && (x)->ps_obj.po_type == &_prop_object_type_string)
82 1.1 thorpej #define prop_string_contents(x) ((x)->ps_immutable ? (x)->ps_immutable : "")
83 1.1 thorpej
84 1.14 thorpej /*
85 1.14 thorpej * In order to reduce memory usage, all immutable string objects are
86 1.14 thorpej * de-duplicated.
87 1.14 thorpej */
88 1.14 thorpej
89 1.14 thorpej static int
90 1.14 thorpej /*ARGSUSED*/
91 1.14 thorpej _prop_string_rb_compare_nodes(void *ctx _PROP_ARG_UNUSED,
92 1.14 thorpej const void *n1, const void *n2)
93 1.14 thorpej {
94 1.14 thorpej const struct _prop_string * const ps1 = n1;
95 1.14 thorpej const struct _prop_string * const ps2 = n2;
96 1.14 thorpej
97 1.14 thorpej _PROP_ASSERT(ps1->ps_immutable != NULL);
98 1.14 thorpej _PROP_ASSERT(ps2->ps_immutable != NULL);
99 1.14 thorpej
100 1.14 thorpej return strcmp(ps1->ps_immutable, ps2->ps_immutable);
101 1.14 thorpej }
102 1.14 thorpej
103 1.14 thorpej static int
104 1.14 thorpej /*ARGSUSED*/
105 1.14 thorpej _prop_string_rb_compare_key(void *ctx _PROP_ARG_UNUSED,
106 1.14 thorpej const void *n, const void *v)
107 1.14 thorpej {
108 1.14 thorpej const struct _prop_string * const ps = n;
109 1.14 thorpej const char * const cp = v;
110 1.14 thorpej
111 1.14 thorpej _PROP_ASSERT(ps->ps_immutable != NULL);
112 1.14 thorpej
113 1.14 thorpej return strcmp(ps->ps_immutable, cp);
114 1.14 thorpej }
115 1.14 thorpej
116 1.14 thorpej static const rb_tree_ops_t _prop_string_rb_tree_ops = {
117 1.14 thorpej .rbto_compare_nodes = _prop_string_rb_compare_nodes,
118 1.14 thorpej .rbto_compare_key = _prop_string_rb_compare_key,
119 1.14 thorpej .rbto_node_offset = offsetof(struct _prop_string, ps_link),
120 1.14 thorpej .rbto_context = NULL
121 1.14 thorpej };
122 1.14 thorpej
123 1.14 thorpej static struct rb_tree _prop_string_tree;
124 1.14 thorpej
125 1.14 thorpej _PROP_ONCE_DECL(_prop_string_init_once)
126 1.14 thorpej _PROP_MUTEX_DECL_STATIC(_prop_string_tree_mutex)
127 1.14 thorpej
128 1.14 thorpej static int
129 1.14 thorpej _prop_string_init(void)
130 1.14 thorpej {
131 1.14 thorpej
132 1.14 thorpej _PROP_MUTEX_INIT(_prop_string_tree_mutex);
133 1.14 thorpej rb_tree_init(&_prop_string_tree,
134 1.14 thorpej &_prop_string_rb_tree_ops);
135 1.17 riastrad
136 1.14 thorpej return 0;
137 1.14 thorpej }
138 1.14 thorpej
139 1.8 joerg /* ARGSUSED */
140 1.11 thorpej static _prop_object_free_rv_t
141 1.8 joerg _prop_string_free(prop_stack_t stack, prop_object_t *obj)
142 1.1 thorpej {
143 1.8 joerg prop_string_t ps = *obj;
144 1.1 thorpej
145 1.14 thorpej if ((ps->ps_flags & PS_F_MUTABLE) == 0) {
146 1.14 thorpej _PROP_MUTEX_LOCK(_prop_string_tree_mutex);
147 1.14 thorpej /*
148 1.14 thorpej * Double-check the retain count now that we've
149 1.16 andvar * acquired the tree lock; holding this lock prevents
150 1.14 thorpej * new retains from coming in by finding it in the
151 1.14 thorpej * tree.
152 1.14 thorpej */
153 1.14 thorpej if (_PROP_ATOMIC_LOAD(&ps->ps_obj.po_refcnt) == 0)
154 1.14 thorpej rb_tree_remove_node(&_prop_string_tree, ps);
155 1.14 thorpej else
156 1.14 thorpej ps = NULL;
157 1.14 thorpej _PROP_MUTEX_UNLOCK(_prop_string_tree_mutex);
158 1.14 thorpej
159 1.14 thorpej if (ps == NULL)
160 1.14 thorpej return (_PROP_OBJECT_FREE_DONE);
161 1.14 thorpej }
162 1.14 thorpej
163 1.1 thorpej if ((ps->ps_flags & PS_F_NOCOPY) == 0 && ps->ps_mutable != NULL)
164 1.1 thorpej _PROP_FREE(ps->ps_mutable, M_PROP_STRING);
165 1.8 joerg _PROP_POOL_PUT(_prop_string_pool, ps);
166 1.8 joerg
167 1.8 joerg return (_PROP_OBJECT_FREE_DONE);
168 1.1 thorpej }
169 1.1 thorpej
170 1.7 thorpej static bool
171 1.1 thorpej _prop_string_externalize(struct _prop_object_externalize_context *ctx,
172 1.1 thorpej void *v)
173 1.1 thorpej {
174 1.1 thorpej prop_string_t ps = v;
175 1.1 thorpej
176 1.1 thorpej if (ps->ps_size == 0)
177 1.1 thorpej return (_prop_object_externalize_empty_tag(ctx, "string"));
178 1.1 thorpej
179 1.7 thorpej if (_prop_object_externalize_start_tag(ctx, "string") == false ||
180 1.1 thorpej _prop_object_externalize_append_encoded_cstring(ctx,
181 1.7 thorpej ps->ps_immutable) == false ||
182 1.7 thorpej _prop_object_externalize_end_tag(ctx, "string") == false)
183 1.7 thorpej return (false);
184 1.17 riastrad
185 1.7 thorpej return (true);
186 1.1 thorpej }
187 1.1 thorpej
188 1.9 joerg /* ARGSUSED */
189 1.11 thorpej static _prop_object_equals_rv_t
190 1.9 joerg _prop_string_equals(prop_object_t v1, prop_object_t v2,
191 1.9 joerg void **stored_pointer1, void **stored_pointer2,
192 1.9 joerg prop_object_t *next_obj1, prop_object_t *next_obj2)
193 1.2 thorpej {
194 1.2 thorpej prop_string_t str1 = v1;
195 1.2 thorpej prop_string_t str2 = v2;
196 1.2 thorpej
197 1.2 thorpej if (str1 == str2)
198 1.9 joerg return (_PROP_OBJECT_EQUALS_TRUE);
199 1.2 thorpej if (str1->ps_size != str2->ps_size)
200 1.9 joerg return (_PROP_OBJECT_EQUALS_FALSE);
201 1.9 joerg if (strcmp(prop_string_contents(str1), prop_string_contents(str2)))
202 1.9 joerg return (_PROP_OBJECT_EQUALS_FALSE);
203 1.9 joerg else
204 1.9 joerg return (_PROP_OBJECT_EQUALS_TRUE);
205 1.2 thorpej }
206 1.2 thorpej
207 1.1 thorpej static prop_string_t
208 1.14 thorpej _prop_string_alloc(int const flags)
209 1.1 thorpej {
210 1.1 thorpej prop_string_t ps;
211 1.1 thorpej
212 1.1 thorpej ps = _PROP_POOL_GET(_prop_string_pool);
213 1.1 thorpej if (ps != NULL) {
214 1.2 thorpej _prop_object_init(&ps->ps_obj, &_prop_object_type_string);
215 1.1 thorpej
216 1.1 thorpej ps->ps_mutable = NULL;
217 1.1 thorpej ps->ps_size = 0;
218 1.14 thorpej ps->ps_flags = flags;
219 1.14 thorpej }
220 1.14 thorpej
221 1.14 thorpej return (ps);
222 1.14 thorpej }
223 1.14 thorpej
224 1.14 thorpej static prop_string_t
225 1.14 thorpej _prop_string_instantiate(int const flags, const char * const str,
226 1.14 thorpej size_t const len)
227 1.14 thorpej {
228 1.14 thorpej prop_string_t ps;
229 1.14 thorpej
230 1.14 thorpej _PROP_ONCE_RUN(_prop_string_init_once, _prop_string_init);
231 1.14 thorpej
232 1.14 thorpej ps = _prop_string_alloc(flags);
233 1.14 thorpej if (ps != NULL) {
234 1.14 thorpej ps->ps_immutable = str;
235 1.14 thorpej ps->ps_size = len;
236 1.14 thorpej
237 1.14 thorpej if ((flags & PS_F_MUTABLE) == 0) {
238 1.14 thorpej prop_string_t ops;
239 1.14 thorpej
240 1.14 thorpej _PROP_MUTEX_LOCK(_prop_string_tree_mutex);
241 1.14 thorpej ops = rb_tree_insert_node(&_prop_string_tree, ps);
242 1.14 thorpej if (ops != ps) {
243 1.14 thorpej /*
244 1.14 thorpej * Equivalent string object already exist;
245 1.14 thorpej * free the new one and return a reference
246 1.14 thorpej * to the existing object.
247 1.14 thorpej */
248 1.14 thorpej prop_object_retain(ops);
249 1.14 thorpej _PROP_MUTEX_UNLOCK(_prop_string_tree_mutex);
250 1.14 thorpej _PROP_POOL_PUT(_prop_string_pool, ps);
251 1.14 thorpej ps = ops;
252 1.14 thorpej } else {
253 1.14 thorpej _PROP_MUTEX_UNLOCK(_prop_string_tree_mutex);
254 1.14 thorpej }
255 1.14 thorpej }
256 1.1 thorpej }
257 1.1 thorpej
258 1.1 thorpej return (ps);
259 1.1 thorpej }
260 1.1 thorpej
261 1.14 thorpej _PROP_DEPRECATED(prop_string_create,
262 1.14 thorpej "this program uses prop_string_create(); all functions "
263 1.14 thorpej "supporting mutable prop_strings are deprecated.")
264 1.1 thorpej prop_string_t
265 1.1 thorpej prop_string_create(void)
266 1.1 thorpej {
267 1.1 thorpej
268 1.14 thorpej return (_prop_string_alloc(PS_F_MUTABLE));
269 1.1 thorpej }
270 1.1 thorpej
271 1.14 thorpej _PROP_DEPRECATED(prop_string_create_cstring,
272 1.14 thorpej "this program uses prop_string_create_cstring(); all functions "
273 1.14 thorpej "supporting mutable prop_strings are deprecated.")
274 1.1 thorpej prop_string_t
275 1.1 thorpej prop_string_create_cstring(const char *str)
276 1.1 thorpej {
277 1.1 thorpej prop_string_t ps;
278 1.1 thorpej char *cp;
279 1.1 thorpej size_t len;
280 1.1 thorpej
281 1.14 thorpej _PROP_ASSERT(str != NULL);
282 1.14 thorpej
283 1.14 thorpej ps = _prop_string_alloc(PS_F_MUTABLE);
284 1.1 thorpej if (ps != NULL) {
285 1.1 thorpej len = strlen(str);
286 1.1 thorpej cp = _PROP_MALLOC(len + 1, M_PROP_STRING);
287 1.1 thorpej if (cp == NULL) {
288 1.8 joerg prop_object_release(ps);
289 1.1 thorpej return (NULL);
290 1.1 thorpej }
291 1.1 thorpej strcpy(cp, str);
292 1.1 thorpej ps->ps_mutable = cp;
293 1.1 thorpej ps->ps_size = len;
294 1.1 thorpej }
295 1.1 thorpej return (ps);
296 1.1 thorpej }
297 1.1 thorpej
298 1.14 thorpej _PROP_DEPRECATED(prop_string_create_cstring_nocopy,
299 1.14 thorpej "this program uses prop_string_create_cstring_nocopy(), "
300 1.14 thorpej "which is deprecated; use prop_string_create_nocopy() instead.")
301 1.14 thorpej prop_string_t
302 1.14 thorpej prop_string_create_cstring_nocopy(const char *str)
303 1.14 thorpej {
304 1.14 thorpej return prop_string_create_nocopy(str);
305 1.14 thorpej }
306 1.14 thorpej
307 1.1 thorpej /*
308 1.14 thorpej * prop_string_create_format --
309 1.14 thorpej * Create a string object using the provided format string.
310 1.1 thorpej */
311 1.14 thorpej prop_string_t __printflike(1, 2)
312 1.14 thorpej prop_string_create_format(const char *fmt, ...)
313 1.1 thorpej {
314 1.1 thorpej prop_string_t ps;
315 1.14 thorpej char *str = NULL;
316 1.14 thorpej int len;
317 1.15 christos size_t nlen;
318 1.14 thorpej va_list ap;
319 1.14 thorpej
320 1.14 thorpej _PROP_ASSERT(fmt != NULL);
321 1.14 thorpej
322 1.14 thorpej va_start(ap, fmt);
323 1.14 thorpej len = vsnprintf(NULL, 0, fmt, ap);
324 1.14 thorpej va_end(ap);
325 1.14 thorpej
326 1.14 thorpej if (len < 0)
327 1.14 thorpej return (NULL);
328 1.15 christos nlen = len + 1;
329 1.14 thorpej
330 1.15 christos str = _PROP_MALLOC(nlen, M_PROP_STRING);
331 1.14 thorpej if (str == NULL)
332 1.14 thorpej return (NULL);
333 1.14 thorpej
334 1.14 thorpej va_start(ap, fmt);
335 1.15 christos vsnprintf(str, nlen, fmt, ap);
336 1.14 thorpej va_end(ap);
337 1.14 thorpej
338 1.15 christos ps = _prop_string_instantiate(0, str, (size_t)len);
339 1.14 thorpej if (ps == NULL)
340 1.14 thorpej _PROP_FREE(str, M_PROP_STRING);
341 1.14 thorpej
342 1.1 thorpej return (ps);
343 1.1 thorpej }
344 1.1 thorpej
345 1.1 thorpej /*
346 1.14 thorpej * prop_string_create_copy --
347 1.14 thorpej * Create a string object by coping the provided constant string.
348 1.14 thorpej */
349 1.14 thorpej prop_string_t
350 1.14 thorpej prop_string_create_copy(const char *str)
351 1.14 thorpej {
352 1.14 thorpej return prop_string_create_format("%s", str);
353 1.14 thorpej }
354 1.14 thorpej
355 1.14 thorpej /*
356 1.14 thorpej * prop_string_create_nocopy --
357 1.14 thorpej * Create a string object using the provided external constant
358 1.14 thorpej * string.
359 1.14 thorpej */
360 1.14 thorpej prop_string_t
361 1.14 thorpej prop_string_create_nocopy(const char *str)
362 1.14 thorpej {
363 1.14 thorpej
364 1.14 thorpej _PROP_ASSERT(str != NULL);
365 1.14 thorpej
366 1.14 thorpej return _prop_string_instantiate(PS_F_NOCOPY, str, strlen(str));
367 1.14 thorpej }
368 1.14 thorpej
369 1.14 thorpej /*
370 1.1 thorpej * prop_string_copy --
371 1.14 thorpej * Copy a string. This reduces to a retain in the common case.
372 1.14 thorpej * Deprecated mutable string objects must be copied.
373 1.1 thorpej */
374 1.1 thorpej prop_string_t
375 1.1 thorpej prop_string_copy(prop_string_t ops)
376 1.1 thorpej {
377 1.1 thorpej prop_string_t ps;
378 1.14 thorpej char *cp;
379 1.1 thorpej
380 1.3 thorpej if (! prop_object_is_string(ops))
381 1.3 thorpej return (NULL);
382 1.1 thorpej
383 1.14 thorpej if ((ops->ps_flags & PS_F_MUTABLE) == 0) {
384 1.14 thorpej prop_object_retain(ops);
385 1.14 thorpej return (ops);
386 1.1 thorpej }
387 1.14 thorpej
388 1.14 thorpej cp = _PROP_MALLOC(ops->ps_size + 1, M_PROP_STRING);
389 1.14 thorpej if (cp == NULL)
390 1.14 thorpej return NULL;
391 1.17 riastrad
392 1.14 thorpej strcpy(cp, prop_string_contents(ops));
393 1.14 thorpej
394 1.14 thorpej ps = _prop_string_instantiate(PS_F_MUTABLE, cp, ops->ps_size);
395 1.14 thorpej if (ps == NULL)
396 1.14 thorpej _PROP_FREE(cp, M_PROP_STRING);
397 1.14 thorpej
398 1.1 thorpej return (ps);
399 1.1 thorpej }
400 1.1 thorpej
401 1.14 thorpej _PROP_DEPRECATED(prop_string_copy_mutable,
402 1.14 thorpej "this program uses prop_string_copy_mutable(); all functions "
403 1.14 thorpej "supporting mutable prop_strings are deprecated.")
404 1.1 thorpej prop_string_t
405 1.1 thorpej prop_string_copy_mutable(prop_string_t ops)
406 1.1 thorpej {
407 1.1 thorpej prop_string_t ps;
408 1.1 thorpej char *cp;
409 1.1 thorpej
410 1.3 thorpej if (! prop_object_is_string(ops))
411 1.3 thorpej return (NULL);
412 1.1 thorpej
413 1.14 thorpej cp = _PROP_MALLOC(ops->ps_size + 1, M_PROP_STRING);
414 1.14 thorpej if (cp == NULL)
415 1.14 thorpej return NULL;
416 1.17 riastrad
417 1.14 thorpej strcpy(cp, prop_string_contents(ops));
418 1.14 thorpej
419 1.14 thorpej ps = _prop_string_instantiate(PS_F_MUTABLE, cp, ops->ps_size);
420 1.14 thorpej if (ps == NULL)
421 1.14 thorpej _PROP_FREE(cp, M_PROP_STRING);
422 1.14 thorpej
423 1.1 thorpej return (ps);
424 1.1 thorpej }
425 1.1 thorpej
426 1.1 thorpej /*
427 1.1 thorpej * prop_string_size --
428 1.3 thorpej * Return the size of the string, not including the terminating NUL.
429 1.1 thorpej */
430 1.1 thorpej size_t
431 1.1 thorpej prop_string_size(prop_string_t ps)
432 1.1 thorpej {
433 1.1 thorpej
434 1.3 thorpej if (! prop_object_is_string(ps))
435 1.3 thorpej return (0);
436 1.3 thorpej
437 1.1 thorpej return (ps->ps_size);
438 1.1 thorpej }
439 1.1 thorpej
440 1.1 thorpej /*
441 1.14 thorpej * prop_string_value --
442 1.14 thorpej * Returns a pointer to the string object's value. This pointer
443 1.14 thorpej * remains valid only as long as the string object.
444 1.14 thorpej */
445 1.14 thorpej const char *
446 1.14 thorpej prop_string_value(prop_string_t ps)
447 1.14 thorpej {
448 1.14 thorpej
449 1.14 thorpej if (! prop_object_is_string(ps))
450 1.14 thorpej return (NULL);
451 1.14 thorpej
452 1.14 thorpej if ((ps->ps_flags & PS_F_MUTABLE) == 0)
453 1.14 thorpej return (ps->ps_immutable);
454 1.17 riastrad
455 1.14 thorpej return (prop_string_contents(ps));
456 1.14 thorpej }
457 1.14 thorpej
458 1.14 thorpej /*
459 1.14 thorpej * prop_string_copy_value --
460 1.14 thorpej * Copy the string object's value into the supplied buffer.
461 1.1 thorpej */
462 1.7 thorpej bool
463 1.14 thorpej prop_string_copy_value(prop_string_t ps, void *buf, size_t buflen)
464 1.14 thorpej {
465 1.14 thorpej
466 1.14 thorpej if (! prop_object_is_string(ps))
467 1.14 thorpej return (false);
468 1.14 thorpej
469 1.14 thorpej if (buf == NULL || buflen < ps->ps_size + 1)
470 1.14 thorpej return (false);
471 1.17 riastrad
472 1.14 thorpej strcpy(buf, prop_string_contents(ps));
473 1.14 thorpej
474 1.14 thorpej return (true);
475 1.14 thorpej }
476 1.14 thorpej
477 1.14 thorpej _PROP_DEPRECATED(prop_string_mutable,
478 1.14 thorpej "this program uses prop_string_mutable(); all functions "
479 1.14 thorpej "supporting mutable prop_strings are deprecated.")
480 1.14 thorpej bool
481 1.1 thorpej prop_string_mutable(prop_string_t ps)
482 1.1 thorpej {
483 1.1 thorpej
484 1.3 thorpej if (! prop_object_is_string(ps))
485 1.7 thorpej return (false);
486 1.3 thorpej
487 1.14 thorpej return ((ps->ps_flags & PS_F_MUTABLE) != 0);
488 1.1 thorpej }
489 1.1 thorpej
490 1.14 thorpej _PROP_DEPRECATED(prop_string_cstring,
491 1.14 thorpej "this program uses prop_string_cstring(), "
492 1.14 thorpej "which is deprecated; use prop_string_copy_value() instead.")
493 1.1 thorpej char *
494 1.1 thorpej prop_string_cstring(prop_string_t ps)
495 1.1 thorpej {
496 1.1 thorpej char *cp;
497 1.1 thorpej
498 1.3 thorpej if (! prop_object_is_string(ps))
499 1.3 thorpej return (NULL);
500 1.3 thorpej
501 1.1 thorpej cp = _PROP_MALLOC(ps->ps_size + 1, M_TEMP);
502 1.1 thorpej if (cp != NULL)
503 1.1 thorpej strcpy(cp, prop_string_contents(ps));
504 1.17 riastrad
505 1.1 thorpej return (cp);
506 1.1 thorpej }
507 1.1 thorpej
508 1.14 thorpej _PROP_DEPRECATED(prop_string_cstring_nocopy,
509 1.14 thorpej "this program uses prop_string_cstring_nocopy(), "
510 1.14 thorpej "which is deprecated; use prop_string_value() instead.")
511 1.1 thorpej const char *
512 1.1 thorpej prop_string_cstring_nocopy(prop_string_t ps)
513 1.1 thorpej {
514 1.1 thorpej
515 1.3 thorpej if (! prop_object_is_string(ps))
516 1.3 thorpej return (NULL);
517 1.3 thorpej
518 1.1 thorpej return (prop_string_contents(ps));
519 1.1 thorpej }
520 1.1 thorpej
521 1.14 thorpej _PROP_DEPRECATED(prop_string_append,
522 1.14 thorpej "this program uses prop_string_append(); all functions "
523 1.14 thorpej "supporting mutable prop_strings are deprecated.")
524 1.7 thorpej bool
525 1.1 thorpej prop_string_append(prop_string_t dst, prop_string_t src)
526 1.1 thorpej {
527 1.1 thorpej char *ocp, *cp;
528 1.1 thorpej size_t len;
529 1.1 thorpej
530 1.3 thorpej if (! (prop_object_is_string(dst) &&
531 1.3 thorpej prop_object_is_string(src)))
532 1.7 thorpej return (false);
533 1.1 thorpej
534 1.14 thorpej if ((dst->ps_flags & PS_F_MUTABLE) == 0)
535 1.7 thorpej return (false);
536 1.1 thorpej
537 1.1 thorpej len = dst->ps_size + src->ps_size;
538 1.1 thorpej cp = _PROP_MALLOC(len + 1, M_PROP_STRING);
539 1.1 thorpej if (cp == NULL)
540 1.7 thorpej return (false);
541 1.12 christos snprintf(cp, len + 1, "%s%s", prop_string_contents(dst),
542 1.1 thorpej prop_string_contents(src));
543 1.1 thorpej ocp = dst->ps_mutable;
544 1.1 thorpej dst->ps_mutable = cp;
545 1.1 thorpej dst->ps_size = len;
546 1.1 thorpej if (ocp != NULL)
547 1.1 thorpej _PROP_FREE(ocp, M_PROP_STRING);
548 1.17 riastrad
549 1.7 thorpej return (true);
550 1.1 thorpej }
551 1.1 thorpej
552 1.14 thorpej _PROP_DEPRECATED(prop_string_append_cstring,
553 1.14 thorpej "this program uses prop_string_append_cstring(); all functions "
554 1.14 thorpej "supporting mutable prop_strings are deprecated.")
555 1.7 thorpej bool
556 1.1 thorpej prop_string_append_cstring(prop_string_t dst, const char *src)
557 1.1 thorpej {
558 1.1 thorpej char *ocp, *cp;
559 1.1 thorpej size_t len;
560 1.1 thorpej
561 1.3 thorpej if (! prop_object_is_string(dst))
562 1.7 thorpej return (false);
563 1.3 thorpej
564 1.1 thorpej _PROP_ASSERT(src != NULL);
565 1.1 thorpej
566 1.14 thorpej if ((dst->ps_flags & PS_F_MUTABLE) == 0)
567 1.7 thorpej return (false);
568 1.14 thorpej
569 1.1 thorpej len = dst->ps_size + strlen(src);
570 1.1 thorpej cp = _PROP_MALLOC(len + 1, M_PROP_STRING);
571 1.1 thorpej if (cp == NULL)
572 1.7 thorpej return (false);
573 1.12 christos snprintf(cp, len + 1, "%s%s", prop_string_contents(dst), src);
574 1.1 thorpej ocp = dst->ps_mutable;
575 1.1 thorpej dst->ps_mutable = cp;
576 1.1 thorpej dst->ps_size = len;
577 1.1 thorpej if (ocp != NULL)
578 1.1 thorpej _PROP_FREE(ocp, M_PROP_STRING);
579 1.17 riastrad
580 1.7 thorpej return (true);
581 1.1 thorpej }
582 1.1 thorpej
583 1.1 thorpej /*
584 1.1 thorpej * prop_string_equals --
585 1.7 thorpej * Return true if two strings are equivalent.
586 1.1 thorpej */
587 1.7 thorpej bool
588 1.1 thorpej prop_string_equals(prop_string_t str1, prop_string_t str2)
589 1.1 thorpej {
590 1.9 joerg if (!prop_object_is_string(str1) || !prop_object_is_string(str2))
591 1.9 joerg return (false);
592 1.1 thorpej
593 1.9 joerg return prop_object_equals(str1, str2);
594 1.1 thorpej }
595 1.1 thorpej
596 1.1 thorpej /*
597 1.14 thorpej * prop_string_equals_string --
598 1.14 thorpej * Return true if the string object is equivalent to the specified
599 1.1 thorpej * C string.
600 1.1 thorpej */
601 1.7 thorpej bool
602 1.14 thorpej prop_string_equals_string(prop_string_t ps, const char *cp)
603 1.1 thorpej {
604 1.1 thorpej
605 1.3 thorpej if (! prop_object_is_string(ps))
606 1.7 thorpej return (false);
607 1.3 thorpej
608 1.1 thorpej return (strcmp(prop_string_contents(ps), cp) == 0);
609 1.1 thorpej }
610 1.1 thorpej
611 1.14 thorpej _PROP_DEPRECATED(prop_string_equals_cstring,
612 1.14 thorpej "this program uses prop_string_equals_cstring(), "
613 1.14 thorpej "which is deprecated; prop_string_equals_string() instead.")
614 1.14 thorpej bool
615 1.14 thorpej prop_string_equals_cstring(prop_string_t ps, const char *cp)
616 1.14 thorpej {
617 1.14 thorpej return prop_string_equals_string(ps, cp);
618 1.14 thorpej }
619 1.14 thorpej
620 1.14 thorpej /*
621 1.14 thorpej * prop_string_compare --
622 1.14 thorpej * Compare two string objects, using strcmp() semantics.
623 1.14 thorpej */
624 1.14 thorpej int
625 1.14 thorpej prop_string_compare(prop_string_t ps1, prop_string_t ps2)
626 1.14 thorpej {
627 1.14 thorpej if (!prop_object_is_string(ps1) || !prop_object_is_string(ps2))
628 1.14 thorpej return (-666); /* arbitrary */
629 1.14 thorpej
630 1.14 thorpej return (strcmp(prop_string_contents(ps1),
631 1.14 thorpej prop_string_contents(ps2)));
632 1.14 thorpej }
633 1.14 thorpej
634 1.14 thorpej /*
635 1.14 thorpej * prop_string_compare_string --
636 1.14 thorpej * Compare a string object to the specified C string, using
637 1.14 thorpej * strcmp() semantics.
638 1.14 thorpej */
639 1.14 thorpej int
640 1.14 thorpej prop_string_compare_string(prop_string_t ps, const char *cp)
641 1.14 thorpej {
642 1.14 thorpej if (!prop_object_is_string(ps))
643 1.14 thorpej return (-666); /* arbitrary */
644 1.14 thorpej
645 1.14 thorpej return (strcmp(prop_string_contents(ps), cp));
646 1.14 thorpej }
647 1.14 thorpej
648 1.1 thorpej /*
649 1.1 thorpej * _prop_string_internalize --
650 1.1 thorpej * Parse a <string>...</string> and return the object created from the
651 1.1 thorpej * external representation.
652 1.1 thorpej */
653 1.8 joerg /* ARGSUSED */
654 1.8 joerg bool
655 1.8 joerg _prop_string_internalize(prop_stack_t stack, prop_object_t *obj,
656 1.8 joerg struct _prop_object_internalize_context *ctx)
657 1.1 thorpej {
658 1.1 thorpej prop_string_t string;
659 1.1 thorpej char *str;
660 1.1 thorpej size_t len, alen;
661 1.1 thorpej
662 1.8 joerg if (ctx->poic_is_empty_element) {
663 1.8 joerg *obj = prop_string_create();
664 1.8 joerg return (true);
665 1.8 joerg }
666 1.17 riastrad
667 1.1 thorpej /* No attributes recognized here. */
668 1.1 thorpej if (ctx->poic_tagattr != NULL)
669 1.8 joerg return (true);
670 1.1 thorpej
671 1.1 thorpej /* Compute the length of the result. */
672 1.6 martin if (_prop_object_internalize_decode_string(ctx, NULL, 0, &len,
673 1.7 thorpej NULL) == false)
674 1.8 joerg return (true);
675 1.17 riastrad
676 1.1 thorpej str = _PROP_MALLOC(len + 1, M_PROP_STRING);
677 1.1 thorpej if (str == NULL)
678 1.8 joerg return (true);
679 1.17 riastrad
680 1.1 thorpej if (_prop_object_internalize_decode_string(ctx, str, len, &alen,
681 1.7 thorpej &ctx->poic_cp) == false ||
682 1.1 thorpej alen != len) {
683 1.1 thorpej _PROP_FREE(str, M_PROP_STRING);
684 1.8 joerg return (true);
685 1.1 thorpej }
686 1.1 thorpej str[len] = '\0';
687 1.1 thorpej
688 1.1 thorpej if (_prop_object_internalize_find_tag(ctx, "string",
689 1.7 thorpej _PROP_TAG_TYPE_END) == false) {
690 1.1 thorpej _PROP_FREE(str, M_PROP_STRING);
691 1.8 joerg return (true);
692 1.1 thorpej }
693 1.1 thorpej
694 1.14 thorpej string = _prop_string_instantiate(0, str, len);
695 1.14 thorpej if (string == NULL)
696 1.1 thorpej _PROP_FREE(str, M_PROP_STRING);
697 1.17 riastrad
698 1.8 joerg *obj = string;
699 1.8 joerg return (true);
700 1.1 thorpej }
701