Try normal method (uncompressed) if Z_DATA_ERROR is returned.
[portableproplib.git] / src / prop_dictionary_util.c
bloba26443e4bdcb37d65dc56dd05140b9a618a8f19d
1 /* $NetBSD: prop_dictionary_util.c,v 1.3 2008/04/28 20:22:53 martin Exp $ */
3 /*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
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.
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.
33 * Utility routines to make it more convenient to work with values
34 * stored in dictionaries.
36 * Note: There is no special magic going on here. We use the standard
37 * proplib(3) APIs to do all of this work. Any application could do
38 * exactly what we're doing here.
41 #include <prop/proplib.h>
42 #include "prop_object_impl.h" /* only to hide kernel vs. not-kernel */
44 bool
45 prop_dictionary_get_bool(prop_dictionary_t dict,
46 const char *key,
47 bool *valp)
49 prop_bool_t b;
51 b = prop_dictionary_get(dict, key);
52 if (prop_object_type(b) != PROP_TYPE_BOOL)
53 return (false);
55 *valp = prop_bool_true(b);
57 return (true);
60 bool
61 prop_dictionary_set_bool(prop_dictionary_t dict,
62 const char *key,
63 bool val)
65 prop_bool_t b;
66 int rv;
68 b = prop_bool_create(val);
69 if (b == NULL)
70 return (false);
71 rv = prop_dictionary_set(dict, key, b);
72 prop_object_release(b);
74 return (rv);
77 #define TEMPLATE(size) \
78 bool \
79 prop_dictionary_get_int ## size (prop_dictionary_t dict, \
80 const char *key, \
81 int ## size ## _t *valp) \
82 { \
83 prop_number_t num; \
85 num = prop_dictionary_get(dict, key); \
86 if (prop_object_type(num) != PROP_TYPE_NUMBER) \
87 return (false); \
89 if (prop_number_unsigned(num) && \
90 prop_number_unsigned_integer_value(num) > \
91 /*CONSTCOND*/((size) == 8 ? INT8_MAX : \
92 (size) == 16 ? INT16_MAX : \
93 (size) == 32 ? INT32_MAX : INT64_MAX)) { \
94 return (false); \
95 } \
97 if (prop_number_size(num) > (size)) \
98 return (false); \
100 *valp = (int ## size ## _t) prop_number_integer_value(num); \
102 return (true); \
105 bool \
106 prop_dictionary_get_uint ## size (prop_dictionary_t dict, \
107 const char *key, \
108 uint ## size ## _t *valp) \
110 prop_number_t num; \
112 num = prop_dictionary_get(dict, key); \
113 if (prop_object_type(num) != PROP_TYPE_NUMBER) \
114 return (false); \
116 if (prop_number_unsigned(num) == false && \
117 prop_number_integer_value(num) < 0) { \
118 return (false); \
121 if (prop_number_size(num) > (size)) \
122 return (false); \
124 *valp = (uint ## size ## _t) \
125 prop_number_unsigned_integer_value(num); \
127 return (true); \
130 bool \
131 prop_dictionary_set_int ## size (prop_dictionary_t dict, \
132 const char *key, \
133 int ## size ## _t val) \
135 prop_number_t num; \
136 int rv; \
138 num = prop_number_create_integer((int64_t) val); \
139 if (num == NULL) \
140 return (false); \
141 rv = prop_dictionary_set(dict, key, num); \
142 prop_object_release(num); \
144 return (rv); \
147 bool \
148 prop_dictionary_set_uint ## size (prop_dictionary_t dict, \
149 const char *key, \
150 uint ## size ## _t val) \
152 prop_number_t num; \
153 int rv; \
155 num = prop_number_create_unsigned_integer((uint64_t) val); \
156 if (num == NULL) \
157 return (false); \
158 rv = prop_dictionary_set(dict, key, num); \
159 prop_object_release(num); \
161 return (rv); \
164 TEMPLATE(8)
165 TEMPLATE(16)
166 TEMPLATE(32)
167 TEMPLATE(64)
169 #undef TEMPLATE
171 #define TEMPLATE(variant, qualifier) \
172 bool \
173 prop_dictionary_get_cstring ## variant (prop_dictionary_t dict, \
174 const char *key, \
175 qualifier char **cpp) \
177 prop_string_t str; \
179 str = prop_dictionary_get(dict, key); \
180 if (prop_object_type(str) != PROP_TYPE_STRING) \
181 return (false); \
183 *cpp = prop_string_cstring ## variant (str); \
185 return (*cpp == NULL ? false : true); \
188 bool \
189 prop_dictionary_set_cstring ## variant (prop_dictionary_t dict, \
190 const char *key, \
191 const char *cp) \
193 prop_string_t str; \
194 int rv; \
196 str = prop_string_create_cstring ## variant (cp); \
197 if (str == NULL) \
198 return (false); \
199 rv = prop_dictionary_set(dict, key, str); \
200 prop_object_release(str); \
202 return (rv); \
205 TEMPLATE(,)
206 TEMPLATE(_nocopy,const)
208 #undef TEMPLATE