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.
24 #include "cr-prop-list.h"
26 #define PRIVATE(a_obj) (a_obj)->priv
28 struct _CRPropListPriv {
35 static CRPropList *cr_prop_list_allocate (void);
38 *Default allocator of CRPropList
39 *@return the newly allocated CRPropList or NULL
43 cr_prop_list_allocate (void)
45 CRPropList *result = NULL;
47 result = g_try_malloc (sizeof (CRPropList));
49 cr_utils_trace_info ("could not allocate CRPropList");
52 memset (result, 0, sizeof (CRPropList));
53 PRIVATE (result) = g_try_malloc (sizeof (CRPropListPriv));
55 cr_utils_trace_info ("could not allocate CRPropListPriv");
59 memset (PRIVATE (result), 0, sizeof (CRPropListPriv));
68 * cr_prop_list_append:
69 *@a_this: the current instance of #CRPropList
70 *@a_to_append: the property list to append
72 *Appends a property list to the current one.
74 *Returns the resulting prop list, or NULL if an error
78 cr_prop_list_append (CRPropList * a_this, CRPropList * a_to_append)
80 CRPropList *cur = NULL;
82 g_return_val_if_fail (a_to_append, NULL);
87 /*go fetch the last element of the list */
89 cur && PRIVATE (cur) && PRIVATE (cur)->next;
90 cur = PRIVATE (cur)->next) ;
91 g_return_val_if_fail (cur, NULL);
92 PRIVATE (cur)->next = a_to_append;
93 PRIVATE (a_to_append)->prev = cur;
98 * cr_prop_list_append2:
99 *Appends a pair of prop/declaration to
100 *the current prop list.
101 *@a_this: the current instance of #CRPropList
102 *@a_prop: the property to consider
103 *@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 * 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.
223 * Returns CR_OK upon successful completion, an error code otherwise.
226 cr_prop_list_set_decl (CRPropList * a_this, CRDeclaration * a_decl)
228 g_return_val_if_fail (a_this && PRIVATE (a_this)
229 && a_decl, CR_BAD_PARAM_ERROR);
231 PRIVATE (a_this)->decl = a_decl;
236 * cr_prop_list_get_decl:
237 * @a_this: the current instance of #CRPropList
238 * @a_decl: out parameter. The property value
239 * Returns CR_OK upon successful completion.
242 cr_prop_list_get_decl (CRPropList * a_this, CRDeclaration ** a_decl)
244 g_return_val_if_fail (a_this && PRIVATE (a_this)
245 && a_decl, CR_BAD_PARAM_ERROR);
247 *a_decl = PRIVATE (a_this)->decl;
252 * cr_prop_list_lookup_prop:
253 *@a_this: the current instance of #CRPropList
254 *@a_prop: the property to lookup
255 *@a_prop_list: out parameter. The property/declaration
256 *pair found (if and only if the function returned code if CR_OK)
258 *Lookup a given property/declaration pair
260 *Returns CR_OK if a prop/decl pair has been found,
261 *CR_VALUE_NOT_FOUND_ERROR if not, or an error code if something
265 cr_prop_list_lookup_prop (CRPropList * a_this,
266 CRString * a_prop, CRPropList ** a_pair)
268 CRPropList *cur = NULL;
270 g_return_val_if_fail (a_prop && a_pair, CR_BAD_PARAM_ERROR);
273 return CR_VALUE_NOT_FOUND_ERROR;
275 g_return_val_if_fail (PRIVATE (a_this), CR_BAD_PARAM_ERROR);
277 for (cur = a_this; cur; cur = PRIVATE (cur)->next) {
278 if (PRIVATE (cur)->prop
279 && PRIVATE (cur)->prop->stryng
280 && PRIVATE (cur)->prop->stryng->str
282 && a_prop->stryng->str
283 && !strcmp (PRIVATE (cur)->prop->stryng->str,
284 a_prop->stryng->str))
293 return CR_VALUE_NOT_FOUND_ERROR;
297 * cr_prop_list_get_next:
298 *@a_this: the current instance of CRPropList
300 *Gets the next prop/decl pair in the list
302 *Returns the next prop/declaration pair of the list,
303 *or NULL if we reached end of list (or if an error occurs)
306 cr_prop_list_get_next (CRPropList * a_this)
308 g_return_val_if_fail (a_this && PRIVATE (a_this), NULL);
310 return PRIVATE (a_this)->next;
314 * cr_prop_list_get_prev:
315 *@a_this: the current instance of CRPropList
317 *Gets the previous prop/decl pair in the list
319 *Returns the previous prop/declaration pair of the list,
320 *or NULL if we reached end of list (or if an error occurs)
323 cr_prop_list_get_prev (CRPropList * a_this)
325 g_return_val_if_fail (a_this && PRIVATE (a_this), NULL);
327 return PRIVATE (a_this)->prev;
331 * cr_prop_list_unlink:
332 *@a_this: the current list of prop/decl pairs
333 *@a_pair: the prop/decl pair to unlink.
335 *Unlinks a prop/decl pair from the list
337 *Returns the new list or NULL in case of an error.
340 cr_prop_list_unlink (CRPropList * a_this, CRPropList * a_pair)
342 CRPropList *prev = NULL,
345 g_return_val_if_fail (a_this && PRIVATE (a_this) && a_pair, NULL);
347 /*some sanity checks */
348 if (PRIVATE (a_pair)->next) {
349 next = PRIVATE (a_pair)->next;
350 g_return_val_if_fail (PRIVATE (next), NULL);
351 g_return_val_if_fail (PRIVATE (next)->prev == a_pair, NULL);
353 if (PRIVATE (a_pair)->prev) {
354 prev = PRIVATE (a_pair)->prev;
355 g_return_val_if_fail (PRIVATE (prev), NULL);
356 g_return_val_if_fail (PRIVATE (prev)->next == a_pair, NULL);
359 PRIVATE (prev)->next = next;
362 PRIVATE (next)->prev = prev;
364 PRIVATE (a_pair)->prev = PRIVATE (a_pair)->next = NULL;
365 if (a_this == a_pair) {
374 * cr_prop_list_detroy:
375 * @a_this: the current instance of #CRPropList
378 cr_prop_list_destroy (CRPropList * a_this)
380 CRPropList *tail = NULL,
383 g_return_if_fail (a_this && PRIVATE (a_this));
386 tail && PRIVATE (tail) && PRIVATE (tail)->next;
387 tail = cr_prop_list_get_next (tail)) ;
388 g_return_if_fail (tail);
393 tail = PRIVATE (cur)->prev;
394 if (tail && PRIVATE (tail))
395 PRIVATE (tail)->next = NULL;
396 PRIVATE (cur)->prev = NULL;
397 g_free (PRIVATE (cur));
398 PRIVATE (cur) = NULL;