/src/pupnp/ixml/src/nodeList.c
Line | Count | Source |
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 | | * \file |
35 | | */ |
36 | | |
37 | | #include "ixml.h" |
38 | | #include "ixmlparser.h" |
39 | | |
40 | | #include <assert.h> |
41 | | #include <stdlib.h> |
42 | | #include <string.h> |
43 | | |
44 | | void ixmlNodeList_init(IXML_NodeList *nList) |
45 | 0 | { |
46 | 0 | assert(nList); |
47 | |
|
48 | 0 | memset(nList, 0, sizeof(IXML_NodeList)); |
49 | 0 | } |
50 | | |
51 | | IXML_Node *ixmlNodeList_item(IXML_NodeList *nList, unsigned long index) |
52 | 0 | { |
53 | 0 | IXML_NodeList *next; |
54 | 0 | unsigned int i; |
55 | | |
56 | | /* if the list ptr is NULL */ |
57 | 0 | if (!nList) { |
58 | 0 | return NULL; |
59 | 0 | } |
60 | | /* if index is more than list length */ |
61 | 0 | if (index > ixmlNodeList_length(nList) - 1lu) { |
62 | 0 | return NULL; |
63 | 0 | } |
64 | | |
65 | 0 | next = nList; |
66 | 0 | for (i = 0u; i < index && next; ++i) { |
67 | 0 | next = next->next; |
68 | 0 | } |
69 | |
|
70 | 0 | if (!next) { |
71 | 0 | return NULL; |
72 | 0 | } |
73 | | |
74 | 0 | return next->nodeItem; |
75 | 0 | } |
76 | | |
77 | | int ixmlNodeList_addToNodeList(IXML_NodeList **nList, IXML_Node *add) |
78 | 0 | { |
79 | 0 | IXML_NodeList *traverse = NULL; |
80 | 0 | IXML_NodeList *p = NULL; |
81 | 0 | IXML_NodeList *newListItem; |
82 | |
|
83 | 0 | assert(add); |
84 | |
|
85 | 0 | if (!add) { |
86 | 0 | return IXML_FAILED; |
87 | 0 | } |
88 | | |
89 | 0 | if (!*nList) { |
90 | | /* nodelist is empty */ |
91 | 0 | *nList = (IXML_NodeList *)malloc(sizeof(IXML_NodeList)); |
92 | 0 | if (!*nList) { |
93 | 0 | return IXML_INSUFFICIENT_MEMORY; |
94 | 0 | } |
95 | | |
96 | 0 | ixmlNodeList_init(*nList); |
97 | 0 | } |
98 | | |
99 | 0 | if (!(*nList)->nodeItem) { |
100 | 0 | (*nList)->nodeItem = add; |
101 | 0 | } else { |
102 | 0 | traverse = *nList; |
103 | 0 | while (traverse) { |
104 | 0 | p = traverse; |
105 | 0 | traverse = traverse->next; |
106 | 0 | } |
107 | |
|
108 | 0 | newListItem = (IXML_NodeList *)malloc(sizeof(IXML_NodeList)); |
109 | 0 | if (!newListItem) { |
110 | 0 | return IXML_INSUFFICIENT_MEMORY; |
111 | 0 | } |
112 | 0 | p->next = newListItem; |
113 | 0 | newListItem->nodeItem = add; |
114 | 0 | newListItem->next = NULL; |
115 | 0 | } |
116 | | |
117 | 0 | return IXML_SUCCESS; |
118 | 0 | } |
119 | | |
120 | | unsigned long ixmlNodeList_length(IXML_NodeList *nList) |
121 | 0 | { |
122 | 0 | IXML_NodeList *list; |
123 | 0 | unsigned long length = 0lu; |
124 | |
|
125 | 0 | list = nList; |
126 | 0 | while (list) { |
127 | 0 | ++length; |
128 | 0 | list = list->next; |
129 | 0 | } |
130 | |
|
131 | 0 | return length; |
132 | 0 | } |
133 | | |
134 | | void ixmlNodeList_free(IXML_NodeList *nList) |
135 | 0 | { |
136 | 0 | IXML_NodeList *next; |
137 | |
|
138 | 0 | while (nList) { |
139 | 0 | next = nList->next; |
140 | 0 | free(nList); |
141 | 0 | nList = next; |
142 | 0 | } |
143 | 0 | } |