2 * This file is part of The Croco Library
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2.1 of the GNU Lesser General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * Author: Dodji Seketeli
19 * See COPYRIGHTS file for copyrights information.
23 #include "cr-prop-list.h"
25 #define PRIVATE(a_obj) (a_obj)->priv
27 struct _CRPropListPriv {
34 static CRPropList *cr_prop_list_allocate (void);
37 *Default allocator of CRPropList
38 *@return the newly allocated CRPropList or NULL
42 cr_prop_list_allocate (void)
44 CRPropList *result = NULL;
46 result = g_try_malloc (sizeof (CRPropList));
48 cr_utils_trace_info ("could not allocate CRPropList");
51 memset (result, 0, sizeof (CRPropList));
52 PRIVATE (result) = g_try_malloc (sizeof (CRPropListPriv));
54 cr_utils_trace_info ("could not allocate CRPropListPriv");
58 memset (PRIVATE (result), 0, sizeof (CRPropListPriv));
67 * cr_prop_list_append:
68 *@a_this: the current instance of #CRPropList
69 *@a_to_append: the property list to append
71 *Appends a property list to the current one.
73 *Returns the resulting prop list, or NULL if an error
77 cr_prop_list_append (CRPropList * a_this, CRPropList * a_to_append)
79 CRPropList *cur = NULL;
81 g_return_val_if_fail (a_to_append, NULL);
86 /*go fetch the last element of the list */
88 cur && PRIVATE (cur) && PRIVATE (cur)->next;
89 cur = PRIVATE (cur)->next) ;
90 g_return_val_if_fail (cur, NULL);
91 PRIVATE (cur)->next = a_to_append;
92 PRIVATE (a_to_append)->prev = cur;
97 * cr_prop_list_append2:
98 *Appends a pair of prop/declaration to
99 *the current prop list.
100 *@a_this: the current instance of #CRPropList
101 *@a_prop: the property to consider
102 *@a_decl: the declaration to consider
104 *Returns the resulting property list, or NULL in case
108 cr_prop_list_append2 (CRPropList * a_this,
110 CRDeclaration * a_decl)
112 CRPropList *list = NULL,
115 g_return_val_if_fail (a_prop && a_decl, NULL);
117 list = cr_prop_list_allocate ();
118 g_return_val_if_fail (list && PRIVATE (list), NULL);
120 PRIVATE (list)->prop = a_prop;
121 PRIVATE (list)->decl = a_decl;
123 result = cr_prop_list_append (a_this, list);
128 * cr_prop_list_prepend:
129 *@a_this: the current instance of #CRPropList
130 *@a_to_prepend: the new list to prepend.
132 *Prepends a list to the current list
133 *Returns the new properties list.
136 cr_prop_list_prepend (CRPropList * a_this, CRPropList * a_to_prepend)
138 CRPropList *cur = NULL;
140 g_return_val_if_fail (a_to_prepend, NULL);
145 for (cur = a_to_prepend; cur && PRIVATE (cur)->next;
146 cur = PRIVATE (cur)->next) ;
147 g_return_val_if_fail (cur, NULL);
148 PRIVATE (cur)->next = a_this;
149 PRIVATE (a_this)->prev = cur;
154 * cr_prop_list_prepend2:
155 *@a_this: the current instance of #CRPropList
156 *@a_prop_name: property name to append
157 *@a_decl: the property value to append.
159 *Prepends a propertie to a list of properties
161 *Returns the new property list.
164 cr_prop_list_prepend2 (CRPropList * a_this,
165 CRString * a_prop_name, CRDeclaration * a_decl)
167 CRPropList *list = NULL,
170 g_return_val_if_fail (a_this && PRIVATE (a_this)
171 && a_prop_name && a_decl, NULL);
173 list = cr_prop_list_allocate ();
174 g_return_val_if_fail (list, NULL);
175 PRIVATE (list)->prop = a_prop_name;
176 PRIVATE (list)->decl = a_decl;
177 result = cr_prop_list_prepend (a_this, list);
182 * cr_prop_list_set_prop:
183 *@a_this: the current instance of #CRPropList
184 *@a_prop: the property to set
186 *Sets the property of a CRPropList
189 cr_prop_list_set_prop (CRPropList * a_this, CRString * a_prop)
191 g_return_val_if_fail (a_this && PRIVATE (a_this)
192 && a_prop, CR_BAD_PARAM_ERROR);
194 PRIVATE (a_this)->prop = a_prop;
199 * cr_prop_list_get_prop:
200 *@a_this: the current instance of #CRPropList
201 *@a_prop: out parameter. The returned property
203 *Getter of the property associated to the current instance
206 *Returns CR_OK upon successful completion, an error code
210 cr_prop_list_get_prop (CRPropList const * a_this, CRString ** a_prop)
212 g_return_val_if_fail (a_this && PRIVATE (a_this)
213 && a_prop, CR_BAD_PARAM_ERROR);
215 *a_prop = PRIVATE (a_this)->prop;
220 * cr_prop_list_set_decl:
221 * @a_this: the current instance of #CRPropList
222 * @a_decl: the new property value.
224 * Returns CR_OK upon successful completion, an error code otherwise.
227 cr_prop_list_set_decl (CRPropList * a_this, CRDeclaration * a_decl)
229 g_return_val_if_fail (a_this && PRIVATE (a_this)
230 && a_decl, CR_BAD_PARAM_ERROR);
232 PRIVATE (a_this)->decl = a_decl;
237 * cr_prop_list_get_decl:
238 * @a_this: the current instance of #CRPropList
239 * @a_decl: out parameter. The property value
241 * Returns CR_OK upon successful completion.
244 cr_prop_list_get_decl (CRPropList const * a_this, CRDeclaration ** a_decl)
246 g_return_val_if_fail (a_this && PRIVATE (a_this)
247 && a_decl, CR_BAD_PARAM_ERROR);
249 *a_decl = PRIVATE (a_this)->decl;
254 * cr_prop_list_lookup_prop:
255 *@a_this: the current instance of #CRPropList
256 *@a_prop: the property to lookup
257 *@a_prop_list: out parameter. The property/declaration
258 *pair found (if and only if the function returned code if CR_OK)
260 *Lookup a given property/declaration pair
262 *Returns CR_OK if a prop/decl pair has been found,
263 *CR_VALUE_NOT_FOUND_ERROR if not, or an error code if something
267 cr_prop_list_lookup_prop (CRPropList * a_this,
268 CRString * a_prop, CRPropList ** a_pair)
270 CRPropList *cur = NULL;
272 g_return_val_if_fail (a_prop && a_pair, CR_BAD_PARAM_ERROR);
275 return CR_VALUE_NOT_FOUND_ERROR;
277 g_return_val_if_fail (PRIVATE (a_this), CR_BAD_PARAM_ERROR);
279 for (cur = a_this; cur; cur = PRIVATE (cur)->next) {
280 if (PRIVATE (cur)->prop
281 && PRIVATE (cur)->prop->stryng
282 && PRIVATE (cur)->prop->stryng->str
284 && a_prop->stryng->str
285 && !strcmp (PRIVATE (cur)->prop->stryng->str,
286 a_prop->stryng->str))
295 return CR_VALUE_NOT_FOUND_ERROR;
299 * cr_prop_list_get_next:
300 *@a_this: the current instance of CRPropList
302 *Gets the next prop/decl pair in the list
304 *Returns the next prop/declaration pair of the list,
305 *or NULL if we reached end of list (or if an error occurs)
308 cr_prop_list_get_next (CRPropList * a_this)
310 g_return_val_if_fail (a_this && PRIVATE (a_this), NULL);
312 return PRIVATE (a_this)->next;
316 * cr_prop_list_get_prev:
317 *@a_this: the current instance of CRPropList
319 *Gets the previous prop/decl pair in the list
321 *Returns the previous prop/declaration pair of the list,
322 *or NULL if we reached end of list (or if an error occurs)
325 cr_prop_list_get_prev (CRPropList * a_this)
327 g_return_val_if_fail (a_this && PRIVATE (a_this), NULL);
329 return PRIVATE (a_this)->prev;
333 * cr_prop_list_unlink:
334 *@a_this: the current list of prop/decl pairs
335 *@a_pair: the prop/decl pair to unlink.
337 *Unlinks a prop/decl pair from the list
339 *Returns the new list or NULL in case of an error.
342 cr_prop_list_unlink (CRPropList * a_this, CRPropList * a_pair)
344 CRPropList *prev = NULL,
347 g_return_val_if_fail (a_this && PRIVATE (a_this) && a_pair, NULL);
349 /*some sanity checks */
350 if (PRIVATE (a_pair)->next) {
351 next = PRIVATE (a_pair)->next;
352 g_return_val_if_fail (PRIVATE (next), NULL);
353 g_return_val_if_fail (PRIVATE (next)->prev == a_pair, NULL);
355 if (PRIVATE (a_pair)->prev) {
356 prev = PRIVATE (a_pair)->prev;
357 g_return_val_if_fail (PRIVATE (prev), NULL);
358 g_return_val_if_fail (PRIVATE (prev)->next == a_pair, NULL);
361 PRIVATE (prev)->next = next;
364 PRIVATE (next)->prev = prev;
366 PRIVATE (a_pair)->prev = PRIVATE (a_pair)->next = NULL;
367 if (a_this == a_pair) {
376 * cr_prop_list_destroy:
377 * @a_this: the current instance of #CRPropList
380 cr_prop_list_destroy (CRPropList * a_this)
382 CRPropList *tail = NULL,
385 g_return_if_fail (a_this && PRIVATE (a_this));
388 tail && PRIVATE (tail) && PRIVATE (tail)->next;
389 tail = cr_prop_list_get_next (tail)) ;
390 g_return_if_fail (tail);
395 tail = PRIVATE (cur)->prev;
396 if (tail && PRIVATE (tail))
397 PRIVATE (tail)->next = NULL;
398 PRIVATE (cur)->prev = NULL;
399 g_free (PRIVATE (cur));
400 PRIVATE (cur) = NULL;