-
Notifications
You must be signed in to change notification settings - Fork 1
/
xmlsd_document.c
155 lines (134 loc) · 3.27 KB
/
xmlsd_document.c
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
/*
* Copyright (c) 2010 Marco Peereboom <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "xmlsd.h"
#include "xmlsd_internal.h"
#include <stdlib.h>
#include <string.h>
/*
* Allocate a new xmlsd_document into `xdp'.
*
* Returns an error code on failure, in which case xdp is undefined.
*/
int
xmlsd_doc_alloc(struct xmlsd_document **xdp)
{
struct xmlsd_document *xd;
xd = calloc(1, sizeof(*xd));
if (xd == NULL)
return (XMLSD_ERR_RESOURCE);
xd->root = NULL;
*xdp = xd;
return (XMLSD_ERR_SUCCES);
}
/*
* Empty out and free all entries in `xd' but leave the document itself
* allocated.
*/
void
xmlsd_doc_clear(struct xmlsd_document *xd)
{
struct xmlsd_element *xe;
if (xd == NULL)
return;
/* Recursively empty tree */
while ((xe = xmlsd_doc_get_first_elem(xd)) != NULL) {
xmlsd_doc_remove_elem(xd, xe);
}
}
/*
* Free the document ``xd'' and all its entries.
*/
void
xmlsd_doc_free(struct xmlsd_document *xd)
{
if (xd == NULL)
return;
xmlsd_doc_clear(xd);
free (xd);
}
/*
* Add an element with name `name' to `xd' with parent `xe'.
* If xe is NULL then add element at the top level.
*/
struct xmlsd_element *
xmlsd_doc_add_elem(struct xmlsd_document *xd, struct xmlsd_element *xe,
const char *name)
{
struct xmlsd_element *nxe = NULL;
if (xd == NULL || name == NULL || (strlen(name) == 0))
goto fail;
/* XXX xmlsd_elem_alloc(parent, name)? */
nxe = calloc(1, sizeof *nxe);
if (nxe == NULL)
goto fail;
nxe->name = strdup(name);
if (nxe->name == NULL)
goto fail;
TAILQ_INIT(&nxe->attr_list);
TAILQ_INIT(&nxe->children);
nxe->depth = xe ? xe->depth + 1 : 0;
nxe->parent = xe;
if (xe)
TAILQ_INSERT_TAIL(&xe->children, nxe, entry);
else {
if (xd->root != NULL)
goto fail;
xd->root = nxe;
}
return nxe;
fail:
if (nxe) {
if (nxe->name)
free(nxe->name);
if (nxe)
free(nxe);
}
return NULL;
}
/*
* Remove elem and its children from xd.
*/
void
xmlsd_doc_remove_elem(struct xmlsd_document *xd, struct xmlsd_element *xe)
{
struct xmlsd_element *xc;
if (xe == NULL || xd == NULL || xd->root == NULL)
return;
while ((xc = xmlsd_elem_get_first_child(xe)) != NULL)
xmlsd_doc_remove_elem(xd, xc);
if (xe->parent)
TAILQ_REMOVE(&xe->parent->children, xe, entry);
else {
xd->root = NULL;
}
xmlsd_elem_free(xe);
}
/*
* Return boolean whether or not xd is an empty document.
*/
int
xmlsd_doc_is_empty(struct xmlsd_document *xd)
{
return (xd->root == NULL);
}
/*
* Get the first top-level element in xd NULL if it is empty.
*/
struct xmlsd_element *
xmlsd_doc_get_root(struct xmlsd_document *xd)
{
return (xd->root);
}