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