Debian Codegraph @

libgdome/gdomecore/gdome-xml-xmlmemory.c

raw
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
2 /* gdome-xml-xmlmemory.c
3 *
4 * Copyright (C) 2001 Paolo Casarini <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 /* NOTE
22
23 This file contains some functions that work directly on the gnome-xml
24 tree structure. So if something change we'll have to work only on this
25 module. Many of functions implemented here are a copy of functions in
26 libxml2 tree.c file. Differences should be only that in gdome_ ones we
27 set Gdome_xml_Node->n to NULL before calling xmlFree function.
28
29 */
30
31 #include "gdome.h"
32 #include <libxml/tree.h>
33 #include <libxml/parser.h>
34 #include "gdome-xml-node.h"
35 #include "gdome-treegc.h"
36
37 static const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
38 static const xmlChar xmlStringTextNoenc[] =
39 { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 };
40 static const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
41
42 void gdome_xmlFreeProp (xmlAttrPtr cur);
43 void gdome_xmlFreePropList (xmlAttrPtr cur);
44 void gdome_xmlFreeNode (xmlNodePtr cur);
45 void gdome_xmlFreeNodeList (xmlNodePtr cur);
46
47 /**
48 * gdome_xmlFreeSubtree:
49 * @cur: the root node of the subtree to free
50 *
51 * Free a node, this is a recursive behaviour, all the children are freed too.
52 * This function also set to NULL the n field in the Gdome_xml_Node structure.
53 */
54 void
55 gdome_xmlFreeSubtree (xmlNode *root) {
56 g_return_if_fail (root != NULL);
57
58 if (root->type == XML_ATTRIBUTE_NODE)
59 gdome_xmlFreeProp ((xmlAttrPtr)root);
60 else
61 gdome_xmlFreeNode ((xmlNodePtr)root);
62 }
63
64 /**
65 * gdome_xmlFreeProp:
66 * @cur: an attribute
67 *
68 * Free one attribute, all the content is freed too
69 */
70 void
71 gdome_xmlFreeProp(xmlAttrPtr cur) {
72 if (cur == NULL)
73 return;
74
75 /* Check for ID removal -> leading to invalid references ! */
76 if ((cur->parent != NULL) && (cur->parent->doc != NULL) &&
77 ((cur->parent->doc->intSubset != NULL) ||
78 (cur->parent->doc->extSubset != NULL))) {
79 if (xmlIsID(cur->parent->doc, cur->parent, cur))
80 xmlRemoveID(cur->parent->doc, cur);
81 }
82 if (cur->name != NULL) xmlFree((char *) cur->name);
83 if (cur->children != NULL) gdome_xmlFreeNodeList(cur->children);
84 if (cur->_private != NULL) gdome_treegc_invalidateNode(cur->_private);
85 xmlFree(cur);
86 }
87
88 /**
89 * gdome_xmlFreePropList:
90 * @cur: the first property in the list
91 *
92 * Free a property and all its siblings, all the children are freed too.
93 */
94 void
95 gdome_xmlFreePropList(xmlAttrPtr cur) {
96 xmlAttrPtr next;
97 if (cur == NULL)
98 return;
99
100 while (cur != NULL) {
101 next = cur->next;
102 gdome_xmlFreeProp(cur);
103 cur = next;
104 }
105 }
106
107 /**
108 * gdome_xmlFreeNodeList:
109 * @cur: the first node in the list
110 *
111 * Free a node and all its siblings, this is a recursive behaviour, all
112 * the children are freed too.
113 */
114 void
115 gdome_xmlFreeNodeList(xmlNodePtr cur) {
116 xmlNodePtr next;
117 if (cur == NULL)
118 return;
119
120 while (cur != NULL) {
121 next = cur->next;
122 /* unroll to speed up freeing the document */
123 if (cur->type != XML_DTD_NODE) {
124 if ((cur->children != NULL) &&
125 (cur->type != XML_ENTITY_REF_NODE))
126 gdome_xmlFreeNodeList(cur->children);
127 if (cur->properties != NULL)
128 gdome_xmlFreePropList(cur->properties);
129 if ((cur->type != XML_ELEMENT_NODE) &&
130 (cur->type != XML_XINCLUDE_START) &&
131 (cur->type != XML_XINCLUDE_END) &&
132 (cur->type != XML_ENTITY_REF_NODE)) {
133 if (cur->content != NULL) xmlFree(cur->content);
134 }
135 if (((cur->type == XML_ELEMENT_NODE) ||
136 (cur->type == XML_XINCLUDE_START) ||
137 (cur->type == XML_XINCLUDE_END)) &&
138 (cur->nsDef != NULL))
139 xmlFreeNsList(cur->nsDef);
140
141 /*
142 * When a node is a text node or a comment, it uses a global static
143 * variable for the name of the node.
144 *
145 * The xmlStrEqual comparisons need to be done when (happened with
146 * XML::libXML and XML::libXSLT) the library is included twice
147 * statically in the binary and a tree allocated by one occurent
148 * of the lib gets freed by the other occurence, in this case
149 * the string addresses compare are not sufficient.
150 */
151 if ((cur->name != NULL) &&
152 (cur->name != xmlStringText) &&
153 (cur->name != xmlStringTextNoenc) &&
154 (cur->name != xmlStringComment)) {
155 if (cur->type == XML_TEXT_NODE) {
156 if ((!xmlStrEqual(cur->name, xmlStringText)) &&
157 (!xmlStrEqual(cur->name, xmlStringTextNoenc)))
158 xmlFree((char *) cur->name);
159 } else if (cur->type == XML_COMMENT_NODE) {
160 if (!xmlStrEqual(cur->name, xmlStringComment))
161 xmlFree((char *) cur->name);
162 } else
163 xmlFree((char *) cur->name);
164 }
165 /* TODO : derecursivate this function */
166 if (cur->_private != NULL) gdome_treegc_invalidateNode(cur->_private);
167 xmlFree(cur);
168 }
169 cur = next;
170 }
171 }
172
173 /**
174 * gdome_xmlFreeNode:
175 * @cur: the node
176 *
177 * Free a node, this is a recursive behaviour, all the children are freed too.
178 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
179 */
180 void
181 gdome_xmlFreeNode(xmlNodePtr cur) {
182 if (cur == NULL)
183 return;
184
185 /* use xmlFreeDtd for DTD nodes */
186 if (cur->type == XML_DTD_NODE)
187 return;
188 if ((cur->children != NULL) &&
189 (cur->type != XML_ENTITY_REF_NODE))
190 gdome_xmlFreeNodeList(cur->children);
191 if (cur->properties != NULL)
192 gdome_xmlFreePropList(cur->properties);
193 if ((cur->type != XML_ELEMENT_NODE) &&
194 (cur->content != NULL) &&
195 (cur->type != XML_ENTITY_REF_NODE) &&
196 (cur->type != XML_XINCLUDE_END) &&
197 (cur->type != XML_XINCLUDE_START)) {
198 xmlFree(cur->content);
199 }
200
201 /*
202 * When a node is a text node or a comment, it uses a global static
203 * variable for the name of the node.
204 *
205 * The xmlStrEqual comparisons need to be done when (happened with
206 * XML::libXML and XML::libXSLT) the library is included twice statically
207 * in the binary and a tree allocated by one occurent of the lib gets
208 * freed by the other occurence, in this case the string addresses compare
209 * are not sufficient.
210 */
211 if ((cur->name != NULL) &&
212 (cur->name != xmlStringText) &&
213 (cur->name != xmlStringTextNoenc) &&
214 (cur->name != xmlStringComment)) {
215 if (cur->type == XML_TEXT_NODE) {
216 if ((!xmlStrEqual(cur->name, xmlStringText)) &&
217 (!xmlStrEqual(cur->name, xmlStringTextNoenc)))
218 xmlFree((char *) cur->name);
219 } else if (cur->type == XML_COMMENT_NODE) {
220 if (!xmlStrEqual(cur->name, xmlStringComment))
221 xmlFree((char *) cur->name);
222 } else
223 xmlFree((char *) cur->name);
224 }
225
226 if (cur->nsDef != NULL) xmlFreeNsList(cur->nsDef);
227 if (cur->_private != NULL) gdome_treegc_invalidateNode(cur->_private);
228 xmlFree(cur);
229 }
230