1 /* Copyright (c) 2007-2009 by Adalin B.V.
2 * Copyright (c) 2007-2009 by Erik Hofman
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of (any of) the copyrightholder(s) nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
36 # define PRINT(a, b, c) { \
37 size_t l1 = (b), l2 = (c); \
41 if (l1 < l2) len = l1; \
43 printf("(%i) '", len); \
44 for (q=0; q<len; q++) printf("%c", s[q]); \
46 } else printf("Length (%u) seems too large at line %i\n",len, __LINE__); \
47 } else printf("NULL pointer at line %i\n", __LINE__); \
51 #if !defined(XML_USE_NODECACHE)
53 cacheNodeGet(void *id, const char *node)
60 #define NODE_BLOCKSIZE 16
75 __xmlNodeGetFromCache(void **nc, const char *start, size_t *len,
76 char **element, size_t *elementlen , size_t *nodenum)
78 struct _xml_node *cache;
79 size_t num = *nodenum;
80 char *name = *element;
85 cache = (struct _xml_node *)*nc;
88 assert((cache->first_free > num) || (cache->first_free == 0));
90 if (cache->first_free == 0) /* leaf node */
93 *len = cache->data_len;
94 *element = cache->name;
95 *elementlen = cache->name_len;
98 else if (*name == '*')
100 struct _xml_node *node = cache->node[num];
103 *len = node->data_len;
104 *element = node->name;
105 *elementlen = node->name_len;
106 *nodenum = cache->first_free;
110 size_t namelen = *elementlen;
113 for (i=0; i<cache->first_free; i++)
115 struct _xml_node *node = cache->node[i];
119 if ((node->name_len == namelen) &&
120 (!memcmp(node->name, name, namelen)))
126 *element = node->name;
127 *elementlen = node->name_len;
128 *len = node->data_len;
129 *nodenum = cache->first_free;
144 return calloc(1, sizeof(struct _xml_node));
148 cacheInitLevel(void *nc)
150 struct _xml_node *cache = (struct _xml_node *)nc;
154 cache->node = calloc(NODE_BLOCKSIZE, sizeof(struct _xml_node *));
155 cache->no_nodes = NODE_BLOCKSIZE;
161 struct _xml_node *cache = (struct _xml_node *)nc;
162 struct _xml_node **node;
167 node = (struct _xml_node **)cache->node;
168 while(i < cache->first_free)
170 cacheFree(node[i++]);
178 cacheNodeGet(void *id)
180 struct _xml_id *xid = (struct _xml_id *)id;
181 struct _xml_node *cache = 0;
191 struct _root_id *rid = (struct _root_id *)xid;
199 cacheNodeNew(void *nc)
201 struct _xml_node *cache = (struct _xml_node *)nc;
202 struct _xml_node *rv = 0;
207 i = cache->first_free;
208 if (i == cache->no_nodes)
210 size_t size, no_nodes;
213 no_nodes = cache->no_nodes + NODE_BLOCKSIZE;
214 size = no_nodes * sizeof(struct _xml_node *);
215 p = realloc(cache->node, size);
219 cache->no_nodes = no_nodes;
222 rv = calloc(1, sizeof(struct _xml_node));
223 if (rv) rv->parent = cache;
231 cacheDataSet(void *n, char *name, size_t namelen, char *data, size_t datalen)
233 struct _xml_node *node = (struct _xml_node *)n;
234 size_t len = datalen;
239 assert(namelen != 0);
243 node->name_len = namelen;
245 node->data_len = datalen;