source: trunk/src/core/scew/element.c @ 1018

Last change on this file since 1018 was 1018, checked in by gah, 16 years ago

Massive changes: New directory/file layout

File size: 7.3 KB
Line 
1/**
2 *
3 * @file     element.c
4 * @author   Aleix Conchillo Flaque <aleix@member.fsf.org>
5 * @date     Mon Nov 25, 2002 00:48
6 * @brief    SCEW element type implementation
7 *
8 * $Id: element.c,v 1.2 2004/02/18 21:51:31 aleix Exp $
9 *
10 * @if copyright
11 *
12 * Copyright (C) 2002, 2003, 2004 Aleix Conchillo Flaque
13 *
14 * SCEW is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * SCEW is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
27 *
28 * @endif
29 */
30
31#include "xelement.h"
32
33#include "xerror.h"
34
35#include "str.h"
36
37#include <assert.h>
38#include <string.h>
39
40
41scew_element*
42scew_element_create(XML_Char const* name)
43{
44    scew_element* element = NULL;
45
46    assert(name != NULL);
47
48    element = (scew_element*) calloc(1, sizeof(scew_element));
49    if (element != NULL)
50    {
51        element->name = scew_strdup(name);
52        element->attributes = attribute_list_create();
53    }
54    else
55    {
56        set_last_error(scew_error_no_memory);
57    }
58
59    return element;
60}
61
62void
63scew_element_free(scew_element* element)
64{
65    scew_element* left = NULL;
66    scew_element* right = NULL;
67
68    if (element == NULL)
69    {
70        return;
71    }
72
73    left = element->left;
74    right = element->right;
75    if (left != NULL)
76    {
77        left->right = right;
78    }
79    if (right != NULL)
80    {
81        right->left = left;
82    }
83
84    free(element->name);
85    free(element->contents);
86    attribute_list_free(element->attributes);
87
88    if (element->parent != NULL)
89    {
90        if (element->parent->child == element)
91        {
92            element->parent->child = element->right;
93        }
94        if (element->parent->last_child == element)
95        {
96            element->parent->last_child = element->left;
97        }
98        element->parent->n_children--;
99    }
100
101    while (element->child != NULL)
102    {
103        scew_element_free(element->child);
104    }
105
106    free(element);
107}
108
109unsigned int
110scew_element_count(scew_element const* parent)
111{
112    assert(parent != NULL);
113
114    return parent->n_children;
115}
116
117scew_element*
118scew_element_next(scew_element const* parent, scew_element const* element)
119{
120    scew_element *next_element;
121
122    if (element == NULL)
123    {
124        if (parent == NULL)
125        {
126            return NULL;
127        }
128        next_element = parent->child;
129    }
130    else
131    {
132        next_element = element->right;
133    }
134
135    return next_element;
136}
137
138scew_element*
139scew_element_by_index(scew_element* parent, unsigned int idx)
140{
141    unsigned int i = 0;
142    scew_element* element = NULL;
143
144    assert(parent != NULL);
145    assert(idx < parent->n_children);
146
147    element = parent->child;
148    for (i = 0; (i < idx) && (element != NULL); ++i)
149    {
150        element = element->right;
151    }
152
153    return element;
154}
155
156scew_element*
157scew_element_by_name(scew_element const* parent, XML_Char const* name)
158{
159    scew_element* element = NULL;
160
161    assert(parent != NULL);
162
163    if (name == NULL)
164    {
165        return NULL;
166    }
167
168    element = scew_element_next(parent, 0);
169    while (element && scew_strcmp(element->name, name))
170    {
171        element = scew_element_next(parent, element);
172    }
173
174    return element;
175}
176
177scew_element**
178scew_element_list(scew_element const* parent, XML_Char const* name,
179                  unsigned int* count)
180{
181    unsigned int curr = 0;
182    unsigned int max = 0;
183    scew_element** list = NULL;
184    scew_element* element;
185
186    assert(parent != NULL);
187    assert(name != NULL);
188    assert(count != NULL);
189
190    element = scew_element_next(parent, 0);
191    while (element)
192    {
193        if (!scew_strcmp(element->name, name))
194        {
195            if (curr >= max)
196            {
197                max = (max + 1) * 2;
198                list = (scew_element**) realloc(list,
199                                                sizeof(scew_element*) * max);
200                if (!list)
201                {
202                    set_last_error(scew_error_no_memory);
203                    return NULL;
204                }
205            }
206            list[curr++] = element;
207        }
208        element = scew_element_next(parent, element);
209    }
210
211    *count = curr;
212
213    return list;
214}
215
216void
217scew_element_list_free(scew_element** lst)
218{
219    free(lst);
220}
221
222XML_Char const*
223scew_element_name(scew_element const* element)
224{
225    assert(element != NULL);
226
227    return element->name;
228}
229
230XML_Char const*
231scew_element_contents(scew_element const* element)
232{
233    assert(element != NULL);
234
235    return element->contents;
236}
237
238XML_Char const*
239scew_element_set_name(scew_element* element, XML_Char const* name)
240{
241    assert(element != NULL);
242    assert(name != NULL);
243
244    free(element->name);
245    element->name = scew_strdup(name);
246
247    return element->name;
248}
249
250XML_Char const*
251scew_element_set_contents(scew_element* element, XML_Char const* data)
252{
253    assert(element != NULL);
254    assert(data != NULL);
255
256    free(element->contents);
257    element->contents = scew_strdup(data);
258
259    return element->contents;
260}
261
262scew_element*
263scew_element_add(scew_element* element, XML_Char const* name)
264{
265    scew_element* new_elem = scew_element_create(name);
266
267    return scew_element_add_elem(element, new_elem);
268}
269
270scew_element*
271scew_element_add_elem(scew_element* element, scew_element* new_elem)
272{
273    scew_element* current = NULL;
274
275    assert(element != NULL);
276    assert(new_elem != NULL);
277
278    element->n_children++;
279
280    new_elem->parent = element;
281    if (element->child == NULL)
282    {
283        element->child = new_elem;
284    }
285    else
286    {
287        current = element->last_child;
288        current->right = new_elem;
289        new_elem->left = current;
290    }
291    element->last_child = new_elem;
292
293    return new_elem;
294}
295
296void
297scew_element_del(scew_element* element)
298{
299    scew_element_free(element);
300}
301
302void
303scew_element_del_by_name(scew_element* element, XML_Char const* name)
304{
305    scew_element_free(scew_element_by_name(element, name));
306}
307
308void
309scew_element_del_by_index(scew_element* element, unsigned int idx)
310{
311    scew_element_free(scew_element_by_index(element, idx));
312}
313
314void
315scew_element_list_del(scew_element* element, XML_Char const* name)
316{
317    unsigned int i = 0;
318    unsigned int count = 0;
319    scew_element** list = NULL;
320
321    if ((element == NULL) || (name == NULL))
322    {
323        return;
324    }
325
326    list = scew_element_list(element, name, &count);
327    if (list == NULL)
328    {
329        return;
330    }
331
332    for (i = 0; i < count; i++)
333    {
334        scew_element_free(list[i]);
335    }
336    scew_element_list_free(list);
337}
338
339scew_attribute*
340scew_element_add_attr(scew_element* element, scew_attribute* attribute)
341{
342    return attribute_list_add(element->attributes, attribute);
343}
344
345scew_attribute*
346scew_element_add_attr_pair(scew_element* element, XML_Char const* name,
347                           XML_Char const* value)
348{
349    scew_attribute* attribute = scew_attribute_create(name, value);
350
351    return attribute_list_add(element->attributes, attribute);
352}
353
354void
355scew_element_del_attr(scew_element* element, XML_Char const* name)
356{
357    attribute_list_del(element->attributes, name);
358}
Note: See TracBrowser for help on using the repository browser.