Branch data Line data Source code
1 : : /* -*- mode: C -*-
2 : : *
3 : : * File: pdf-hash-helper.c
4 : : * Date: Thu Jul 24 21:05:05 2008
5 : : *
6 : : * GNU PDF Library - Hash helper functions
7 : : *
8 : : */
9 : :
10 : : /* Copyright (C) 2008-2011 Free Software Foundation, Inc. */
11 : :
12 : : /* This program is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU General Public License as published by
14 : : * the Free Software Foundation, either version 3 of the License, or
15 : : * (at your option) any later version.
16 : : *
17 : : * This program is distributed in the hope that it will be useful,
18 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 : : * GNU General Public License for more details.
21 : : *
22 : : * You should have received a copy of the GNU General Public License
23 : : * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 : : */
25 : :
26 : : #include <config.h>
27 : :
28 : : #include <string.h>
29 : :
30 : : #include <pdf-hash-helper.h>
31 : :
32 : : /* Hash helpers to add/get texts */
33 : :
34 : : pdf_bool_t
35 : 1 : pdf_hash_add_text (pdf_hash_t *table,
36 : : const pdf_char_t *key,
37 : : const pdf_text_t *value,
38 : : pdf_error_t **error)
39 : : {
40 : 1 : return pdf_hash_add (table,
41 : : key,
42 : : value,
43 : : (pdf_hash_value_dispose_fn_t) pdf_text_destroy,
44 : : error);
45 : : }
46 : :
47 : : pdf_bool_t
48 : 0 : pdf_hash_add_duplicated_text (pdf_hash_t *table,
49 : : const pdf_char_t *key,
50 : : const pdf_text_t *value,
51 : : pdf_error_t **error)
52 : : {
53 : : pdf_text_t *dup;
54 : :
55 : 0 : dup = pdf_text_dup (value, error);
56 [ # # ]: 0 : if (!dup)
57 : 0 : return PDF_FALSE;
58 : :
59 [ # # ]: 0 : if (!pdf_hash_add (table,
60 : : key,
61 : : dup,
62 : : (pdf_hash_value_dispose_fn_t) pdf_text_destroy,
63 : : error))
64 : : {
65 : 0 : pdf_text_destroy (dup);
66 : 0 : return PDF_FALSE;
67 : : }
68 : 0 : return PDF_TRUE;
69 : : }
70 : :
71 : : const pdf_text_t *
72 : 0 : pdf_hash_get_text (const pdf_hash_t *table,
73 : : const pdf_char_t *key)
74 : : {
75 : 0 : return (const pdf_text_t *) pdf_hash_get_value (table, key);
76 : : }
77 : :
78 : : /* Hash helpers to add/get times */
79 : :
80 : : pdf_bool_t
81 : 0 : pdf_hash_add_time (pdf_hash_t *table,
82 : : const pdf_char_t *key,
83 : : const pdf_time_t *value,
84 : : pdf_error_t **error)
85 : : {
86 : 0 : return pdf_hash_add (table,
87 : : key,
88 : : value,
89 : : (pdf_hash_value_dispose_fn_t) pdf_time_destroy,
90 : : error);
91 : : }
92 : :
93 : : pdf_bool_t
94 : 1 : pdf_hash_add_duplicated_time (pdf_hash_t *table,
95 : : const pdf_char_t *key,
96 : : const pdf_time_t *value,
97 : : pdf_error_t **error)
98 : : {
99 : : pdf_time_t *dup;
100 : :
101 : 1 : dup = pdf_time_dup (value, error);
102 [ - + ]: 1 : if (!dup)
103 : 0 : return PDF_FALSE;
104 : :
105 [ - + ]: 1 : if (!pdf_hash_add (table,
106 : : key,
107 : : dup,
108 : : (pdf_hash_value_dispose_fn_t) pdf_time_destroy,
109 : : error))
110 : : {
111 : 0 : pdf_time_destroy (dup);
112 : 0 : return PDF_FALSE;
113 : : }
114 : 1 : return PDF_TRUE;
115 : : }
116 : :
117 : : pdf_bool_t
118 : 0 : pdf_hash_add_static_time (pdf_hash_t *table,
119 : : const pdf_char_t *key,
120 : : const pdf_time_t *value,
121 : : pdf_error_t **error)
122 : : {
123 : 0 : return pdf_hash_add (table,
124 : : key,
125 : : value,
126 : : NULL,
127 : : error);
128 : : }
129 : :
130 : : const pdf_time_t *
131 : 0 : pdf_hash_get_time (const pdf_hash_t *table,
132 : : const pdf_char_t *key)
133 : : {
134 : 0 : return (const pdf_time_t *) pdf_hash_get_value (table, key);
135 : : }
136 : :
137 : : /* Hash helpers to add/get lists */
138 : :
139 : : pdf_bool_t
140 : 1 : pdf_hash_add_list (pdf_hash_t *table,
141 : : const pdf_char_t *key,
142 : : const pdf_list_t *value,
143 : : pdf_error_t **error)
144 : : {
145 : 1 : return pdf_hash_add (table,
146 : : key,
147 : : value,
148 : : (pdf_hash_value_dispose_fn_t) pdf_list_destroy,
149 : : error);
150 : : }
151 : :
152 : : const pdf_list_t *
153 : 0 : pdf_hash_get_list (const pdf_hash_t *table,
154 : : const pdf_char_t *key)
155 : : {
156 : 0 : return (const pdf_list_t *) pdf_hash_get_value (table, key);
157 : : }
158 : :
159 : : /* Hash helpers to add/get hashes */
160 : :
161 : : pdf_bool_t
162 : 1 : pdf_hash_add_hash (pdf_hash_t *table,
163 : : const pdf_char_t *key,
164 : : const pdf_hash_t *value,
165 : : pdf_error_t **error)
166 : : {
167 : 1 : return pdf_hash_add (table,
168 : : key,
169 : : value,
170 : : (pdf_hash_value_dispose_fn_t) pdf_hash_destroy,
171 : : error);
172 : : }
173 : :
174 : : const pdf_hash_t *
175 : 0 : pdf_hash_get_hash (const pdf_hash_t *table,
176 : : const pdf_char_t *key)
177 : : {
178 : 0 : return (const pdf_hash_t *) pdf_hash_get_value (table, key);
179 : : }
180 : :
181 : : /* Hash helpers to add/get stream */
182 : :
183 : : pdf_bool_t
184 : 1 : pdf_hash_add_stm (pdf_hash_t *table,
185 : : const pdf_char_t *key,
186 : : const pdf_stm_t *value,
187 : : pdf_error_t **error)
188 : : {
189 : 1 : return pdf_hash_add (table,
190 : : key,
191 : : value,
192 : : (pdf_hash_value_dispose_fn_t) pdf_stm_destroy,
193 : : error);
194 : : }
195 : :
196 : : const pdf_stm_t *
197 : 0 : pdf_hash_get_stm (const pdf_hash_t *table,
198 : : const pdf_char_t *key)
199 : : {
200 : 0 : return (const pdf_stm_t *) pdf_hash_get_value (table, key);
201 : : }
202 : :
203 : : /* Hash helpers to add/get booleans */
204 : :
205 : : pdf_bool_t
206 : 0 : pdf_hash_add_bool (pdf_hash_t *table,
207 : : const pdf_char_t *key,
208 : : const pdf_bool_t value,
209 : : pdf_error_t **error)
210 : : {
211 : 0 : return pdf_hash_add (table,
212 : : key,
213 : : (void *)value,
214 : : NULL,
215 : : error);
216 : : }
217 : :
218 : : pdf_bool_t
219 : 0 : pdf_hash_get_bool (const pdf_hash_t *table,
220 : : const pdf_char_t *key)
221 : : {
222 : 0 : return (pdf_bool_t) pdf_hash_get_value (table, key);
223 : : }
224 : :
225 : : /* Hash helpers to add/get integer */
226 : :
227 : : pdf_bool_t
228 : 0 : pdf_hash_add_i32 (pdf_hash_t *table,
229 : : const pdf_char_t *key,
230 : : const pdf_i32_t value,
231 : : pdf_error_t **error)
232 : : {
233 : 0 : return pdf_hash_add (table,
234 : : key,
235 : 0 : (void *)((long)value),
236 : : NULL,
237 : : error);
238 : : }
239 : :
240 : : pdf_i32_t
241 : 0 : pdf_hash_get_i32 (pdf_hash_t *table,
242 : : const pdf_char_t *key)
243 : : {
244 : 0 : return (pdf_i32_t) ((long) pdf_hash_get_value (table, key));
245 : : }
246 : :
247 : : pdf_bool_t
248 : 0 : pdf_hash_add_u32 (pdf_hash_t *table,
249 : : const pdf_char_t *key,
250 : : const pdf_u32_t value,
251 : : pdf_error_t **error)
252 : : {
253 : 0 : return pdf_hash_add (table,
254 : : key,
255 : 0 : (void *) ((unsigned long)value),
256 : : NULL,
257 : : error);
258 : : }
259 : :
260 : : pdf_u32_t
261 : 0 : pdf_hash_get_u32 (pdf_hash_t *table,
262 : : const pdf_char_t *key)
263 : : {
264 : 0 : return (pdf_u32_t) ((unsigned long)pdf_hash_get_value (table, key));
265 : : }
266 : :
267 : : /* Hash helpers to add/get sizes */
268 : :
269 : : pdf_bool_t
270 : 40 : pdf_hash_add_size (pdf_hash_t *table,
271 : : const pdf_char_t *key,
272 : : const pdf_size_t value,
273 : : pdf_error_t **error)
274 : : {
275 : 40 : return pdf_hash_add (table,
276 : : key,
277 : : (void *)value,
278 : : NULL,
279 : : error);
280 : : }
281 : :
282 : : pdf_size_t
283 : 40 : pdf_hash_get_size (const pdf_hash_t *table,
284 : : const pdf_char_t *key)
285 : : {
286 : 40 : return (pdf_size_t) pdf_hash_get_value (table, key);
287 : : }
288 : :
289 : : /* Hash helpers to add/get strings */
290 : :
291 : : pdf_bool_t
292 : 0 : pdf_hash_add_string (pdf_hash_t *table,
293 : : const pdf_char_t *key,
294 : : const pdf_char_t *value,
295 : : pdf_error_t **error)
296 : : {
297 : 0 : return pdf_hash_add (table,
298 : : key,
299 : : value,
300 : : pdf_dealloc,
301 : : error);
302 : : }
303 : :
304 : : pdf_bool_t
305 : 0 : pdf_hash_add_duplicated_string (pdf_hash_t *table,
306 : : const pdf_char_t *key,
307 : : const pdf_char_t *value,
308 : : pdf_error_t **error)
309 : : {
310 : : pdf_char_t *dup;
311 : : pdf_size_t len;
312 : :
313 : 0 : len = strlen (value) + 1;
314 : 0 : dup = pdf_alloc (len);
315 [ # # ]: 0 : if (!dup)
316 : : {
317 : 0 : pdf_set_error (error,
318 : : PDF_EDOMAIN_BASE_HASH,
319 : : PDF_ENOMEM,
320 : : "not enough memory: couldn't duplicate string");
321 : 0 : return PDF_FALSE;
322 : : }
323 : 0 : memcpy (dup, value, len);
324 : :
325 : 0 : return pdf_hash_add (table,
326 : : key,
327 : : dup,
328 : : pdf_dealloc,
329 : : error);
330 : : }
331 : :
332 : : pdf_bool_t
333 : 0 : pdf_hash_add_static_string (pdf_hash_t *table,
334 : : const pdf_char_t *key,
335 : : const pdf_char_t *value,
336 : : pdf_error_t **error)
337 : : {
338 : 0 : return pdf_hash_add (table,
339 : : key,
340 : : (void *)value,
341 : : NULL,
342 : : error);
343 : : }
344 : :
345 : : const pdf_char_t *
346 : 0 : pdf_hash_get_string (const pdf_hash_t *table,
347 : : const pdf_char_t *key)
348 : : {
349 : 0 : return (pdf_char_t *) pdf_hash_get_value (table, key);
350 : : }
351 : :
352 : : /* End of pdf-hash-helper.c */
|