Debian Codegraph @

src/OVAL/adt/oval_smc.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 * Structure providing 1--N mapping, by integrating two structures:
24 * oval_string_map and oval_collection.
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30
31 #include "oval_smc_impl.h"
32 #include "oval_string_map_impl.h"
33 #include "common/util.h"
34 #include "oval_adt.h"
35
36 struct oval_smc *oval_smc_new(void)
37 {
38 return (struct oval_smc *) oval_string_map_new();
39 }
40
41 static inline struct oval_collection *_oval_smc_get_all(struct oval_smc *map, const char *key)
42 {
43 return (struct oval_collection *) oval_string_map_get_value((struct oval_string_map *)map, key);
44 }
45
46 void oval_smc_put_last(struct oval_smc *map, const char *key, void *item)
47 {
48 __attribute__nonnull__(map);
49
50 if (item != NULL) {
51 struct oval_collection *list_col = _oval_smc_get_all(map, key);
52 if (list_col == NULL) {
53 list_col = oval_collection_new();
54 oval_string_map_put((struct oval_string_map *) map, key, list_col);
55 }
56 oval_collection_add(list_col, item);
57 }
58 }
59
60 void oval_smc_put_last_if_not_exists(struct oval_smc *map, const char *key, void *item)
61 {
62 __attribute__nonnull__(map);
63
64 if (item != NULL) {
65 struct oval_collection *list_col = _oval_smc_get_all(map, key);
66 if (list_col == NULL) {
67 list_col = oval_collection_new();
68 oval_string_map_put((struct oval_string_map *) map, key, list_col);
69 }
70
71 struct oval_iterator *list_it = oval_collection_iterator(list_col);
72 bool found = false;
73 while (!found && oval_collection_iterator_has_more(list_it)) {
74 if (item == oval_collection_iterator_next(list_it)) {
75 found = true;
76 }
77 }
78 oval_collection_iterator_free(list_it);
79 if (!found) {
80 oval_collection_add(list_col, item);
81 }
82 }
83 }
84
85 struct oval_iterator *oval_smc_get_all_it(struct oval_smc *map, const char *key)
86 {
87 struct oval_collection *col = _oval_smc_get_all(map, key);
88 return (col == NULL) ? NULL : oval_collection_iterator(col);
89 }
90
91 void *oval_smc_get_last(struct oval_smc *map, const char *key)
92 {
93 struct oval_iterator *list_it = oval_smc_get_all_it(map, key);
94 if (list_it == NULL)
95 return NULL;
96 void *found = NULL;
97 while (oval_collection_iterator_has_more(list_it)) {
98 found = oval_collection_iterator_next(list_it);
99 }
100 oval_collection_iterator_free(list_it);
101 return found;
102 }
103
104 void oval_smc_free0(struct oval_smc *map)
105 {
106 if (map == NULL)
107 return;
108 oval_string_map_free((struct oval_string_map *) map, (oscap_destruct_func) oval_collection_free);
109 }
110
111 void oval_smc_free(struct oval_smc *map, oscap_destruct_func destructor)
112 {
113 if (map == NULL)
114 return;
115 struct oval_iterator *map_it = oval_string_map_values((struct oval_string_map *) map);
116 while (oval_collection_iterator_has_more(map_it)) {
117 struct oval_collection *list_col = (struct oval_collection *) oval_collection_iterator_next(map_it);
118 oval_collection_free_items(list_col, destructor);
119 }
120 oval_collection_iterator_free(map_it);
121 oval_string_map_free((struct oval_string_map *) map, NULL);
122 }
123
124 static inline struct oval_collection *_oval_collection_clone_user(struct oval_collection *oldcol, oval_smc_user_clone_func cloner, void *user_data)
125 {
126 if (oldcol == NULL || cloner == NULL)
127 return NULL;
128
129 struct oval_collection *newcol = oval_collection_new();
130 struct oval_iterator *col_it = oval_collection_iterator(oldcol);
131 while (oval_collection_iterator_has_more(col_it)) {
132 void *item = oval_collection_iterator_next(col_it);
133 void *new_item = (*cloner) (user_data, item);
134 oval_collection_add(newcol, new_item);
135 }
136 oval_collection_iterator_free(col_it);
137 return newcol;
138 }
139
140 struct oval_smc *oval_smc_clone_user(struct oval_smc *oldmap, oval_smc_user_clone_func cloner, void *user_data)
141 {
142 if (oldmap == NULL || cloner == NULL)
143 return NULL;
144
145 struct oval_smc *newmap = oval_smc_new();
146 struct oval_string_iterator *key_it = (struct oval_string_iterator *) oval_string_map_keys((struct oval_string_map *) oldmap);
147 while (oval_string_iterator_has_more(key_it)) {
148 const char *key = oval_string_iterator_next(key_it);
149 struct oval_collection *list_col = _oval_smc_get_all(oldmap, key);
150 struct oval_collection *cloned = _oval_collection_clone_user(list_col, cloner, user_data);
151 oval_string_map_put((struct oval_string_map *) newmap, key, cloned);
152 }
153 oval_string_iterator_free(key_it);
154 return newmap;
155 }