Debian Codegraph @

src/OVAL/adt/oval_collection.c

raw symbols SCIP
1 /**
2 * @file oval_collection.c
3 * \brief Open Vulnerability and Assessment Language
4 *
5 * See more details at http://oval.mitre.org/
6 */
7
8 /*
9 * Copyright 2009--2013 Red Hat Inc., Durham, North Carolina.
10 * All Rights Reserved.
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 *
26 * Authors:
27 * "David Niemoller" <[email protected]>
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33
34 #include <stdlib.h>
35 #include <stdio.h>
36
37 #include "oval_adt.h"
38 #include "oval_collection_impl.h"
39 #include "common/util.h"
40
41 /***************************************************************************/
42 /* Variable definitions
43 * */
44
45 typedef struct _oval_collection_item_frame {
46 struct _oval_collection_item_frame *next;
47 void *item;
48 } _oval_collection_item_frame_t;
49
50 typedef struct oval_collection {
51 struct _oval_collection_item_frame *item_collection_frame;
52 } oval_collection_t;
53
54 typedef struct oval_iterator {
55 struct _oval_collection_item_frame *item_iterator_frame;
56 } oval_iterator_t;
57
58 /* End of variable definitions
59 * */
60 /***************************************************************************/
61
62 struct oval_collection *oval_collection_new()
63 {
64 struct oval_collection *collection = (struct oval_collection *)malloc(sizeof(oval_collection_t));
65 if (collection == NULL)
66 return NULL;
67
68 collection->item_collection_frame = NULL;
69 return collection;
70 }
71
72 void oval_collection_free(struct oval_collection *collection)
73 {
74 oval_collection_free_items(collection, NULL);
75 }
76
77 void oval_collection_free_items(struct oval_collection *collection, oscap_destruct_func free_func)
78 {
79 if (collection) {
80 struct _oval_collection_item_frame *frame = collection->item_collection_frame;
81 while (frame != NULL) {
82 if (free_func != NULL) {
83 void *item = frame->item;
84 if (item)
85 (*free_func) (item);
86 frame->item = NULL;
87 }
88 struct _oval_collection_item_frame *temp = frame;
89 frame = frame->next;
90 temp->next = NULL;
91 free(temp);
92 }
93 free(collection);
94 }
95 }
96
97 int oval_collection_is_empty(struct oval_collection *collection)
98 {
99 __attribute__nonnull__(collection);
100 return collection->item_collection_frame == NULL;
101 }
102
103 void oval_collection_add(struct oval_collection *collection, void *item)
104 {
105 __attribute__nonnull__(collection);
106
107 struct _oval_collection_item_frame *next = malloc(sizeof(_oval_collection_item_frame_t));
108 if (next == NULL)
109 return;
110
111 next->next = collection->item_collection_frame;
112 collection->item_collection_frame = next;
113 next->item = item;
114 }
115
116 struct oval_iterator *oval_collection_iterator(struct oval_collection *collection)
117 {
118 __attribute__nonnull__(collection);
119
120 struct oval_iterator *iterator = (struct oval_iterator *)malloc(sizeof(oval_iterator_t));
121 if (iterator == NULL)
122 return NULL;
123
124 iterator->item_iterator_frame = NULL;
125 struct _oval_collection_item_frame *collection_frame = collection->item_collection_frame;
126
127 while (collection_frame != NULL) {
128 struct _oval_collection_item_frame *iterator_frame =
129 (struct _oval_collection_item_frame *)malloc(sizeof(_oval_collection_item_frame_t));
130
131 iterator_frame->next = iterator->item_iterator_frame;
132 iterator_frame->item = collection_frame->item;
133 iterator->item_iterator_frame = iterator_frame;
134 collection_frame = collection_frame->next;
135 }
136 return iterator;
137 }
138
139 bool oval_collection_iterator_has_more(struct oval_iterator * iterator)
140 {
141 __attribute__nonnull__(iterator);
142
143 return iterator->item_iterator_frame != NULL;
144 }
145
146 int oval_collection_iterator_remaining(struct oval_iterator *iterator)
147 {
148 __attribute__nonnull__(iterator);
149
150 int remaining;
151 struct _oval_collection_item_frame *next = iterator->item_iterator_frame;
152
153 for (remaining = 0; next; remaining++)
154 next = next->next;
155
156 return remaining;
157 }
158
159 void *oval_collection_iterator_next(struct oval_iterator *iterator)
160 {
161 __attribute__nonnull__(iterator);
162
163 struct _oval_collection_item_frame *oc_next = iterator->item_iterator_frame;
164 void *next;
165
166 if (oc_next == NULL) {
167 next = NULL;
168 } else {
169 next = oc_next->item;
170 iterator->item_iterator_frame = oc_next->next;
171 oc_next->item = NULL;
172 oc_next->next = NULL;
173 free(oc_next);
174 }
175 return next;
176 }
177
178 void oval_collection_iterator_free(struct oval_iterator *iterator)
179 {
180 if (iterator) { //NOOP if iterator is NULL
181 while (iterator->item_iterator_frame) {
182 struct _oval_collection_item_frame *oc_this;
183 oc_this = iterator->item_iterator_frame;
184 iterator->item_iterator_frame = oc_this->next;
185 oc_this->item = NULL;
186 oc_this->next = NULL;
187 free(oc_this);
188 }
189 iterator->item_iterator_frame = NULL;
190 free(iterator);
191 }
192 }
193
194 struct oval_iterator *oval_collection_iterator_new()
195 {
196 struct oval_iterator *iterator = (struct oval_iterator *)malloc(sizeof(oval_iterator_t));
197 if (iterator == NULL)
198 return NULL;
199
200 iterator->item_iterator_frame = NULL;
201 return iterator;
202 }
203
204 void oval_collection_iterator_add(struct oval_iterator *iterator, void *item)
205 {
206 __attribute__nonnull__(iterator);
207
208 struct _oval_collection_item_frame *newframe =
209 (struct _oval_collection_item_frame *)malloc(sizeof(_oval_collection_item_frame_t));
210 if (newframe == NULL) /* We don't have any information that error occurred ! */
211 return;
212
213 newframe->next = iterator->item_iterator_frame;
214 newframe->item = item;
215 iterator->item_iterator_frame = newframe;
216 }
217
218 bool oval_string_iterator_has_more(struct oval_string_iterator * iterator)
219 {
220 return oval_collection_iterator_has_more((struct oval_iterator *)iterator);
221 }
222
223 int oval_string_iterator_remaining(struct oval_string_iterator *iterator)
224 {
225 return oval_collection_iterator_remaining((struct oval_iterator *)iterator);
226 }
227
228 char *oval_string_iterator_next(struct oval_string_iterator *iterator)
229 {
230 return (char *)oval_collection_iterator_next((struct oval_iterator *)iterator);
231 }
232
233 void oval_string_iterator_free(struct oval_string_iterator *iterator)
234 {
235 oval_collection_iterator_free((struct oval_iterator *)iterator);
236 }