Debian Codegraph @

src/OVAL/adt/oval_smc_iterator.c

raw symbols SCIP
1 /*
2 * Copyright 2013 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Šimon Lukašík
21 *
22 *
23 *
24 * This module implements a simple iterator iterating through SMC structure.
25 * That is StringMap that points to the oval_collection objects.
26 *
27 * This module implements only sub set of iterator functionality for iterating
28 * through map/list structure. Limits: It supposes that list (secondary_col) is
29 * never empty.
30 */
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #include "oval_smc_iterator_impl.h"
37 #include "oval_string_map_impl.h"
38 #include "oval_smc_impl.h"
39
40 struct oval_smc_iterator {
41 struct oval_collection *primary_col; ///< list of lists
42 struct oval_iterator *primary_it; ///< iterating through primary_col
43 struct oval_iterator *secondary_it; ///< iterating through a single item of primary_col
44 };
45
46 struct oval_smc_iterator *oval_smc_iterator_new(struct oval_smc *mapping)
47 {
48 if (mapping == NULL)
49 return NULL;
50
51 struct oval_smc_iterator *it = calloc(1, sizeof(struct oval_smc_iterator));
52
53 it->primary_col = oval_string_map_collect_values((struct oval_string_map *) mapping, NULL);
54 it->primary_it = oval_collection_iterator(it->primary_col);
55 it->secondary_it = NULL;
56 return it;
57 }
58
59 void oval_smc_iterator_free(struct oval_smc_iterator *it)
60 {
61 if (it == NULL)
62 return;
63 oval_collection_free(it->primary_col);
64 oval_collection_iterator_free(it->primary_it);
65 oval_collection_iterator_free(it->secondary_it);
66 free(it);
67 }
68
69 bool oval_smc_iterator_has_more(struct oval_smc_iterator *it)
70 {
71 return it != NULL &&
72 (oval_collection_iterator_has_more(it->primary_it) ||
73 (it->secondary_it &&
74 oval_collection_iterator_has_more(it->secondary_it)));
75 }
76
77 void *oval_smc_iterator_next(struct oval_smc_iterator *it)
78 {
79 if (it == NULL)
80 return NULL;
81 if (it->secondary_it == NULL || !oval_collection_iterator_has_more(it->secondary_it)) {
82 // Rewind primary and find new secondary
83 if (it->secondary_it != NULL) {
84 oval_collection_iterator_free(it->secondary_it);
85 it->secondary_it = NULL;
86 }
87 if (!oval_collection_iterator_has_more(it->primary_it)) {
88 assert(false);
89 return NULL;
90 }
91 struct oval_collection *secondary_col = (struct oval_collection *) oval_collection_iterator_next(it->primary_it);
92 it->secondary_it = oval_collection_iterator(secondary_col);
93 }
94 assert(oval_collection_iterator_has_more(it->secondary_it));
95 return oval_collection_iterator_next(it->secondary_it);
96 }