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 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 <pdf-hash-helper.h>
29 :
30 :
31 : static void text_dispose_fn (const void *elt);
32 : static void time_dispose_fn (const void *elt);
33 : static void list_dispose_fn (const void *elt);
34 : static void hash_dispose_fn (const void *elt);
35 : static void stm_dispose_fn (const void *elt);
36 :
37 :
38 : pdf_status_t
39 : pdf_hash_add_text (pdf_hash_t table, const char *key, const pdf_text_t *elt)
40 1 : {
41 1 : return (pdf_hash_add(table,key,elt,text_dispose_fn));
42 : }
43 :
44 : pdf_status_t
45 : pdf_hash_get_text (pdf_hash_t table, const char *key, pdf_text_t *elt)
46 0 : {
47 0 : return pdf_hash_get (table, key, (const void **) &elt);
48 : }
49 :
50 : pdf_status_t
51 : pdf_hash_add_time (pdf_hash_t table, const char *key, const pdf_time_t *elt)
52 1 : {
53 1 : return (pdf_hash_add(table,key,elt,time_dispose_fn));
54 : }
55 :
56 : pdf_status_t
57 : pdf_hash_get_time (pdf_hash_t table, const char *key, pdf_time_t *elt)
58 0 : {
59 0 : return pdf_hash_get (table, key, (const void **) &elt);
60 : }
61 :
62 :
63 : pdf_status_t
64 : pdf_hash_add_list (pdf_hash_t table, const char *key, const pdf_list_t *elt)
65 1 : {
66 1 : return (pdf_hash_add(table,key,elt,list_dispose_fn));
67 : }
68 :
69 : pdf_status_t
70 : pdf_hash_get_list (pdf_hash_t table, const char *key, pdf_list_t *elt)
71 0 : {
72 0 : return pdf_hash_get (table, key, (const void **) &elt);
73 : }
74 :
75 : pdf_status_t
76 : pdf_hash_add_hash (pdf_hash_t table, const char *key, const pdf_hash_t *elt)
77 1 : {
78 1 : return (pdf_hash_add(table,key,elt,hash_dispose_fn));
79 : }
80 :
81 : pdf_status_t
82 : pdf_hash_get_hash (pdf_hash_t table, const char *key, pdf_hash_t *elt)
83 0 : {
84 0 : return pdf_hash_get (table, key, (const void **) &elt);
85 : }
86 :
87 :
88 : pdf_status_t
89 : pdf_hash_add_stm (pdf_hash_t table, const char *key, const pdf_stm_t *elt)
90 1 : {
91 1 : return (pdf_hash_add(table,key,elt,stm_dispose_fn));
92 : }
93 :
94 : pdf_status_t
95 : pdf_hash_get_stm (pdf_hash_t table, const char *key, pdf_stm_t *elt)
96 0 : {
97 0 : return pdf_hash_get (table, key, (const void **) &elt);
98 : }
99 :
100 : pdf_status_t
101 : pdf_hash_add_bool (pdf_hash_t table, const char *key, const pdf_bool_t elt)
102 0 : {
103 : pdf_bool_t *vbool;
104 :
105 0 : vbool = pdf_alloc (sizeof(pdf_bool_t));
106 0 : if (vbool == NULL)
107 : {
108 0 : return PDF_ERROR;
109 : }
110 :
111 0 : *vbool = elt;
112 :
113 0 : return (pdf_hash_add (table, key, vbool, pdf_dealloc));
114 : }
115 :
116 : pdf_status_t
117 : pdf_hash_get_bool (pdf_hash_t table, const char *key, pdf_bool_t *elt)
118 0 : {
119 : pdf_status_t ret;
120 : pdf_bool_t *vbool;
121 :
122 0 : ret = pdf_hash_get (table, key, (const void **) &vbool);
123 0 : if (ret == PDF_OK)
124 : {
125 0 : *elt = *vbool;
126 : }
127 :
128 0 : return ret;
129 : }
130 :
131 : pdf_status_t
132 : pdf_hash_add_size (pdf_hash_t table, const char *key, const pdf_size_t elt)
133 0 : {
134 : pdf_size_t *size;
135 :
136 0 : size = pdf_alloc (sizeof(pdf_size_t));
137 0 : if (size == NULL)
138 : {
139 0 : return PDF_ERROR;
140 : }
141 :
142 0 : *size = elt;
143 :
144 0 : return (pdf_hash_add (table, key, size, pdf_dealloc));
145 : }
146 :
147 : pdf_status_t
148 : pdf_hash_get_size (pdf_hash_t table, const char *key, pdf_size_t *elt)
149 5 : {
150 : pdf_status_t ret;
151 : pdf_size_t *size;
152 5 : ret = pdf_hash_get (table, key, (const void **) &size);
153 5 : if (ret == PDF_OK)
154 : {
155 5 : *elt = *size;
156 : }
157 :
158 5 : return ret;
159 : }
160 :
161 : pdf_status_t
162 : pdf_hash_add_string (pdf_hash_t table, const char *key, const pdf_char_t *elt)
163 0 : {
164 : pdf_char_t *string;
165 : pdf_size_t string_size;
166 :
167 0 : string_size = strlen ((char *) elt) + 1;
168 0 : string = pdf_alloc (string_size);
169 0 : if (string == NULL)
170 : {
171 0 : return PDF_ERROR;
172 : }
173 :
174 0 : string = (pdf_char_t *) strncpy ((char *) string, (char *) elt, string_size);
175 :
176 0 : return (pdf_hash_add (table, key, string, pdf_dealloc));
177 : }
178 :
179 : pdf_status_t
180 : pdf_hash_get_string (pdf_hash_t table, const char *key, pdf_char_t **elt)
181 5 : {
182 5 : return pdf_hash_get (table, key, (const void **) elt);
183 : }
184 :
185 : static void
186 : text_dispose_fn (const void *elt)
187 1 : {
188 1 : pdf_text_t *destroyed = (pdf_text_t*) elt;
189 1 : pdf_text_destroy(*destroyed);
190 1 : }
191 :
192 : static void
193 : time_dispose_fn (const void *elt)
194 1 : {
195 1 : pdf_time_t * destroyed = (pdf_time_t*) elt;
196 1 : pdf_time_destroy(*destroyed);
197 1 : }
198 :
199 : static void
200 : list_dispose_fn (const void *elt)
201 1 : {
202 1 : pdf_list_t * destroyed = (pdf_list_t*) elt;
203 1 : pdf_list_destroy(*destroyed);
204 1 : }
205 :
206 : static void
207 : hash_dispose_fn (const void *elt)
208 1 : {
209 1 : pdf_hash_destroy (*((pdf_hash_t*)elt));
210 1 : }
211 :
212 : static void
213 : stm_dispose_fn (const void *elt)
214 1 : {
215 : pdf_size_t flushed_bytes;
216 :
217 1 : pdf_stm_t * stm = (pdf_stm_t*) elt;
218 1 : pdf_stm_flush (*stm, PDF_TRUE, &flushed_bytes);
219 1 : pdf_stm_destroy (*stm);
220 1 : }
221 :
222 :
223 : /* End of pdf-hash-helper.c */
|