1 /**************************************************************************
2  *
3  * Copyright (c) 2000-2003 Intel Corporation
4  * All rights reserved.
5  * Copyright (c) 2012 France Telecom All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * - Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  * - Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  * - Neither name of Intel Corporation nor the names of its contributors
16  * may be used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  **************************************************************************/
32 
33 
34 /*!
35  * \file
36  */
37 
38 
39 #include "ixmlparser.h"
40 
41 
42 #include <assert.h>
43 #include <stdlib.h> /* for free(), malloc() */
44 #include <string.h>
45 
46 
47 /*!
48  * \brief Return the item number of a item in NamedNodeMap.
49  */
ixmlNamedNodeMap_getItemNumber(IXML_NamedNodeMap * nnMap,const char * name)50 static unsigned long ixmlNamedNodeMap_getItemNumber(
51 	/*! [in] The named node map to process. */
52 	IXML_NamedNodeMap *nnMap,
53 	/*! [in] The name of the item to find. */
54 	const char *name)
55 {
56 	IXML_Node *tempNode;
57 	unsigned long returnItemNo = 0lu;
58 
59 	assert(nnMap != NULL && name != NULL);
60 	if (nnMap == NULL || name == NULL) {
61 		return (unsigned long)IXML_INVALID_ITEM_NUMBER;
62 	}
63 
64 	tempNode = nnMap->nodeItem;
65 	while (tempNode != NULL) {
66 		if (strcmp(name, tempNode->nodeName) == 0) {
67 			return returnItemNo;
68 		}
69 		tempNode = tempNode->nextSibling;
70 		returnItemNo++;
71 	}
72 
73 	return (unsigned long)IXML_INVALID_ITEM_NUMBER;
74 }
75 
76 
ixmlNamedNodeMap_init(IXML_NamedNodeMap * nnMap)77 void ixmlNamedNodeMap_init(IXML_NamedNodeMap *nnMap)
78 {
79 	assert(nnMap != NULL);
80 
81 	memset(nnMap, 0, sizeof (IXML_NamedNodeMap));
82 }
83 
84 
ixmlNamedNodeMap_getNamedItem(IXML_NamedNodeMap * nnMap,const DOMString name)85 IXML_Node *ixmlNamedNodeMap_getNamedItem(
86 	IXML_NamedNodeMap *nnMap,
87 	const DOMString name)
88 {
89 	unsigned long index;
90 
91 	if (nnMap == NULL || name == NULL) {
92 		return NULL;
93 	}
94 
95 	index = ixmlNamedNodeMap_getItemNumber(nnMap, name);
96 	if (index == (unsigned long)IXML_INVALID_ITEM_NUMBER) {
97 		return NULL;
98 	} else {
99 		return ixmlNamedNodeMap_item(nnMap, index);
100 	}
101 }
102 
103 
ixmlNamedNodeMap_item(IXML_NamedNodeMap * nnMap,unsigned long index)104 IXML_Node *ixmlNamedNodeMap_item(
105 	/* IN */ IXML_NamedNodeMap *nnMap,
106 	/* IN */ unsigned long index )
107 {
108 	IXML_Node *tempNode;
109 	unsigned int i;
110 
111 	if (nnMap == NULL) {
112 		return NULL;
113 	}
114 
115 	if (index > ixmlNamedNodeMap_getLength(nnMap) - 1lu) {
116 		return NULL;
117 	}
118 
119 	tempNode = nnMap->nodeItem;
120 	for (i = 0u; i < index && tempNode != NULL; ++i) {
121 		tempNode = tempNode->nextSibling;
122 	}
123 
124 	return tempNode;
125 }
126 
127 
ixmlNamedNodeMap_getLength(IXML_NamedNodeMap * nnMap)128 unsigned long ixmlNamedNodeMap_getLength(IXML_NamedNodeMap *nnMap)
129 {
130 	IXML_Node *tempNode;
131 	unsigned long length = 0lu;
132 
133 	if (nnMap != NULL) {
134 		tempNode = nnMap->nodeItem;
135 		for (length = 0lu; tempNode != NULL; ++length) {
136 			tempNode = tempNode->nextSibling;
137 		}
138 	}
139 
140 	return length;
141 }
142 
143 
ixmlNamedNodeMap_free(IXML_NamedNodeMap * nnMap)144 void ixmlNamedNodeMap_free(IXML_NamedNodeMap *nnMap)
145 {
146 	IXML_NamedNodeMap *pNext;
147 
148 	while (nnMap != NULL) {
149 		pNext = nnMap->next;
150 		free(nnMap);
151 		nnMap = pNext;
152 	}
153 }
154 
155 
ixmlNamedNodeMap_addToNamedNodeMap(IXML_NamedNodeMap ** nnMap,IXML_Node * add)156 int ixmlNamedNodeMap_addToNamedNodeMap(
157 	IXML_NamedNodeMap **nnMap,
158 	IXML_Node *add)
159 {
160 	IXML_NamedNodeMap *traverse = NULL;
161 	IXML_NamedNodeMap *p = NULL;
162 	IXML_NamedNodeMap *newItem = NULL;
163 
164 	if(add == NULL) {
165 		return IXML_SUCCESS;
166 	}
167 
168 	if (*nnMap == NULL) {
169 		/* nodelist is empty */
170 		*nnMap = (IXML_NamedNodeMap *)malloc(sizeof (IXML_NamedNodeMap));
171 		if (*nnMap == NULL) {
172 			return IXML_INSUFFICIENT_MEMORY;
173 		}
174 		ixmlNamedNodeMap_init(*nnMap);
175 	}
176 	if ((*nnMap)->nodeItem == NULL) {
177 		(*nnMap)->nodeItem = add;
178 	} else {
179 		traverse = *nnMap;
180 		p = traverse;
181 		while (traverse != NULL) {
182 			p = traverse;
183 			traverse = traverse->next;
184 		}
185 		newItem = (IXML_NamedNodeMap *)malloc(sizeof (IXML_NamedNodeMap));
186 		if (newItem == NULL) {
187 			return IXML_INSUFFICIENT_MEMORY;
188 		}
189 		p->next = newItem;
190 		newItem->nodeItem = add;
191 		newItem->next = NULL;
192 	}
193 
194 	return IXML_SUCCESS;
195 }
196 
197