summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/libcroco/cr-prop-list.c
blob: 8e42cee4f819a5f8284d9445ec398b97cba62d1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
 * This file is part of The Croco Library
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 * Author: Dodji Seketeli
 * See COPYRIGHTS file for copyrights information.
 */

#include <string.h>
#include "cr-prop-list.h"

#define PRIVATE(a_obj) (a_obj)->priv

struct _CRPropListPriv {
        CRString *prop;
        CRDeclaration *decl;
        CRPropList *next;
        CRPropList *prev;
};

static CRPropList *cr_prop_list_allocate (void);

/**
 *Default allocator of CRPropList
 *@return the newly allocated CRPropList or NULL
 *if an error arises.
 */
static CRPropList *
cr_prop_list_allocate (void)
{
        CRPropList *result = NULL;

        result = g_try_malloc (sizeof (CRPropList));
        if (!result) {
                cr_utils_trace_info ("could not allocate CRPropList");
                return NULL;
        }
        memset (result, 0, sizeof (CRPropList));
        PRIVATE (result) = g_try_malloc (sizeof (CRPropListPriv));
        if (!PRIVATE (result)) {
                cr_utils_trace_info ("could not allocate CRPropListPriv");
                g_free (result);
                return NULL;
        }
        memset (PRIVATE (result), 0, sizeof (CRPropListPriv));
        return result;
}

/****************
 *public methods
 ***************/

/**
 * cr_prop_list_append:
 *@a_this: the current instance of #CRPropList
 *@a_to_append: the property list to append
 *
 *Appends a property list to the current one.
 *
 *Returns the resulting prop list, or NULL if an error
 *occurred
 */
CRPropList *
cr_prop_list_append (CRPropList * a_this, CRPropList * a_to_append)
{
        CRPropList *cur = NULL;

        g_return_val_if_fail (a_to_append, NULL);

        if (!a_this)
                return a_to_append;

        /*go fetch the last element of the list */
        for (cur = a_this;
             cur && PRIVATE (cur) && PRIVATE (cur)->next;
             cur = PRIVATE (cur)->next) ;
        g_return_val_if_fail (cur, NULL);
        PRIVATE (cur)->next = a_to_append;
        PRIVATE (a_to_append)->prev = cur;
        return a_this;
}

/**
 * cr_prop_list_append2:
 *Appends a pair of prop/declaration to
 *the current prop list.
 *@a_this: the current instance of #CRPropList
 *@a_prop: the property to consider
 *@a_decl: the declaration to consider
 *
 *Returns the resulting property list, or NULL in case
 *of an error.
 */
CRPropList *
cr_prop_list_append2 (CRPropList * a_this,
                      CRString * a_prop, 
		      CRDeclaration * a_decl)
{
        CRPropList *list = NULL,
                *result = NULL;

        g_return_val_if_fail (a_prop && a_decl, NULL);

        list = cr_prop_list_allocate ();
        g_return_val_if_fail (list && PRIVATE (list), NULL);

        PRIVATE (list)->prop = a_prop;
        PRIVATE (list)->decl = a_decl;

        result = cr_prop_list_append (a_this, list);
        return result;
}

/**
 * cr_prop_list_prepend:
 *@a_this: the current instance of #CRPropList
 *@a_to_prepend: the new list to prepend.
 *
 *Prepends a list to the current list
 *Returns the new properties list.
 */
CRPropList *
cr_prop_list_prepend (CRPropList * a_this, CRPropList * a_to_prepend)
{
        CRPropList *cur = NULL;

        g_return_val_if_fail (a_to_prepend, NULL);

        if (!a_this)
                return a_to_prepend;

        for (cur = a_to_prepend; cur && PRIVATE (cur)->next;
             cur = PRIVATE (cur)->next) ;
        g_return_val_if_fail (cur, NULL);
        PRIVATE (cur)->next = a_this;
        PRIVATE (a_this)->prev = cur;
        return a_to_prepend;
}

/**
 * cr_prop_list_prepend2:
 *@a_this: the current instance of #CRPropList
 *@a_prop_name: property name to append
 *@a_decl: the property value to append.
 *
 *Prepends a property to a list of properties 
 *
 *Returns the new property list.
 */
CRPropList *
cr_prop_list_prepend2 (CRPropList * a_this,
                       CRString * a_prop_name, CRDeclaration * a_decl)
{
        CRPropList *list = NULL,
                *result = NULL;

        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_prop_name && a_decl, NULL);

        list = cr_prop_list_allocate ();
        g_return_val_if_fail (list, NULL);
        PRIVATE (list)->prop = a_prop_name;
        PRIVATE (list)->decl = a_decl;
        result = cr_prop_list_prepend (a_this, list);
        return result;
}

/**
 * cr_prop_list_set_prop:
 *@a_this: the current instance of #CRPropList
 *@a_prop: the property to set
 *
 *Sets the property of a CRPropList
 */
enum CRStatus
cr_prop_list_set_prop (CRPropList * a_this, CRString * a_prop)
{
        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_prop, CR_BAD_PARAM_ERROR);

        PRIVATE (a_this)->prop = a_prop;
        return CR_OK;
}

/**
 * cr_prop_list_get_prop:
 *@a_this: the current instance of #CRPropList
 *@a_prop: out parameter. The returned property
 *
 *Getter of the property associated to the current instance
 *of #CRPropList
 *
 *Returns CR_OK upon successful completion, an error code
 *otherwise.
 */
enum CRStatus
cr_prop_list_get_prop (CRPropList const * a_this, CRString ** a_prop)
{
        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_prop, CR_BAD_PARAM_ERROR);

        *a_prop = PRIVATE (a_this)->prop;
        return CR_OK;
}

/**
 * cr_prop_list_set_decl:
 * @a_this: the current instance of #CRPropList
 * @a_decl: the new property value.
 *
 * Returns CR_OK upon successful completion, an error code otherwise.
 */
enum CRStatus
cr_prop_list_set_decl (CRPropList * a_this, CRDeclaration * a_decl)
{
        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_decl, CR_BAD_PARAM_ERROR);

        PRIVATE (a_this)->decl = a_decl;
        return CR_OK;
}

/**
 * cr_prop_list_get_decl:
 * @a_this: the current instance of #CRPropList
 * @a_decl: out parameter. The property value
 *
 * Returns CR_OK upon successful completion.
 */
enum CRStatus
cr_prop_list_get_decl (CRPropList const * a_this, CRDeclaration ** a_decl)
{
        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_decl, CR_BAD_PARAM_ERROR);

        *a_decl = PRIVATE (a_this)->decl;
        return CR_OK;
}

/**
 * cr_prop_list_lookup_prop:
 *@a_this: the current instance of #CRPropList
 *@a_prop: the property to lookup
 *@a_prop_list: out parameter. The property/declaration
 *pair found (if and only if the function returned code if CR_OK)
 *
 *Lookup a given property/declaration pair
 *
 *Returns CR_OK if a prop/decl pair has been found,
 *CR_VALUE_NOT_FOUND_ERROR if not, or an error code if something
 *bad happens.
 */
enum CRStatus
cr_prop_list_lookup_prop (CRPropList * a_this,
                          CRString * a_prop, CRPropList ** a_pair)
{
        CRPropList *cur = NULL;

        g_return_val_if_fail (a_prop && a_pair, CR_BAD_PARAM_ERROR);

        if (!a_this)
                return CR_VALUE_NOT_FOUND_ERROR;

        g_return_val_if_fail (PRIVATE (a_this), CR_BAD_PARAM_ERROR);

        for (cur = a_this; cur; cur = PRIVATE (cur)->next) {
                if (PRIVATE (cur)->prop
		    && PRIVATE (cur)->prop->stryng
                    && PRIVATE (cur)->prop->stryng->str
		    && a_prop->stryng
                    && a_prop->stryng->str
                    && !strcmp (PRIVATE (cur)->prop->stryng->str, 
				a_prop->stryng->str))
                        break;
        }

        if (cur) {
                *a_pair = cur;
                return CR_OK;
        }

        return CR_VALUE_NOT_FOUND_ERROR;
}

/**
 * cr_prop_list_get_next:
 *@a_this: the current instance of CRPropList
 *
 *Gets the next prop/decl pair in the list
 *
 *Returns the next prop/declaration pair of the list, 
 *or NULL if we reached end of list (or if an error occurs)
 */
CRPropList *
cr_prop_list_get_next (CRPropList * a_this)
{
        g_return_val_if_fail (a_this && PRIVATE (a_this), NULL);

        return PRIVATE (a_this)->next;
}

/**
 * cr_prop_list_get_prev:
 *@a_this: the current instance of CRPropList
 *
 *Gets the previous prop/decl pair in the list
 *
 *Returns the previous prop/declaration pair of the list, 
 *or NULL if we reached end of list (or if an error occurs)
 */
CRPropList *
cr_prop_list_get_prev (CRPropList * a_this)
{
        g_return_val_if_fail (a_this && PRIVATE (a_this), NULL);

        return PRIVATE (a_this)->prev;
}

/**
 * cr_prop_list_unlink:
 *@a_this: the current list of prop/decl pairs
 *@a_pair: the prop/decl pair to unlink.
 *
 *Unlinks a prop/decl pair from the list
 *
 *Returns the new list or NULL in case of an error.
 */
CRPropList *
cr_prop_list_unlink (CRPropList * a_this, CRPropList * a_pair)
{
        CRPropList *prev = NULL,
                *next = NULL;

        g_return_val_if_fail (a_this && PRIVATE (a_this) && a_pair, NULL);

        /*some sanity checks */
        if (PRIVATE (a_pair)->next) {
                next = PRIVATE (a_pair)->next;
                g_return_val_if_fail (PRIVATE (next), NULL);
                g_return_val_if_fail (PRIVATE (next)->prev == a_pair, NULL);
        }
        if (PRIVATE (a_pair)->prev) {
                prev = PRIVATE (a_pair)->prev;
                g_return_val_if_fail (PRIVATE (prev), NULL);
                g_return_val_if_fail (PRIVATE (prev)->next == a_pair, NULL);
        }
        if (prev) {
                PRIVATE (prev)->next = next;
        }
        if (next) {
                PRIVATE (next)->prev = prev;
        }
        PRIVATE (a_pair)->prev = PRIVATE (a_pair)->next = NULL;
        if (a_this == a_pair) {
                if (next)
                        return next;
                return NULL;
        }
        return a_this;
}

/**
 * cr_prop_list_destroy:
 * @a_this: the current instance of #CRPropList
 */
void
cr_prop_list_destroy (CRPropList * a_this)
{
        CRPropList *tail = NULL,
                *cur = NULL;

        g_return_if_fail (a_this && PRIVATE (a_this));

        for (tail = a_this;
             tail && PRIVATE (tail) && PRIVATE (tail)->next;
             tail = cr_prop_list_get_next (tail)) ;
        g_return_if_fail (tail);

        cur = tail;

        while (cur) {
                tail = PRIVATE (cur)->prev;
                if (tail && PRIVATE (tail))
                        PRIVATE (tail)->next = NULL;
                PRIVATE (cur)->prev = NULL;
                g_free (PRIVATE (cur));
                PRIVATE (cur) = NULL;
                g_free (cur);
                cur = tail;
        }
}