No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / atf / dist / atf-c / map.c
blob6cd5b92bb83920a62c6d82b4353c478d97f9fe94
1 /*
2 * Automated Testing Framework (atf)
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <stdlib.h>
31 #include <string.h>
33 #include "atf-c/error.h"
34 #include "atf-c/map.h"
35 #include "atf-c/sanity.h"
37 /* ---------------------------------------------------------------------
38 * Auxiliary functions.
39 * --------------------------------------------------------------------- */
41 struct map_entry {
42 char *m_key;
43 void *m_value;
44 bool m_managed;
47 static
48 struct map_entry *
49 new_entry(const char *key, void *value, bool managed)
51 struct map_entry *me;
53 me = (struct map_entry *)malloc(sizeof(*me));
54 if (me != NULL) {
55 me->m_key = strdup(key);
56 if (me->m_key == NULL) {
57 free(me);
58 me = NULL;
59 } else {
60 me->m_value = value;
61 me->m_managed = managed;
65 return me;
68 /* ---------------------------------------------------------------------
69 * The "atf_map_citer" type.
70 * --------------------------------------------------------------------- */
73 * Getters.
76 const void *
77 atf_map_citer_data(const atf_map_citer_t citer)
79 const struct map_entry *me = citer.m_entry;
80 PRE(me != NULL);
81 return me->m_value;
84 bool
85 atf_equal_map_citer_map_citer(const atf_map_citer_t i1,
86 const atf_map_citer_t i2)
88 return i1.m_map == i2.m_map && i1.m_entry == i2.m_entry;
91 /* ---------------------------------------------------------------------
92 * The "atf_map_iter" type.
93 * --------------------------------------------------------------------- */
96 * Getters.
99 void *
100 atf_map_iter_data(const atf_map_iter_t iter)
102 const struct map_entry *me = iter.m_entry;
103 PRE(me != NULL);
104 return me->m_value;
107 bool
108 atf_equal_map_iter_map_iter(const atf_map_iter_t i1,
109 const atf_map_iter_t i2)
111 return i1.m_map == i2.m_map && i1.m_entry == i2.m_entry;
114 /* ---------------------------------------------------------------------
115 * The "atf_map" type.
116 * --------------------------------------------------------------------- */
119 * Constructors and destructors.
122 atf_error_t
123 atf_map_init(atf_map_t *m)
125 atf_error_t err;
127 err = atf_list_init(&m->m_list);
128 if (atf_is_error(err))
129 goto out;
131 atf_object_init(&m->m_object);
133 out:
134 return err;
137 void
138 atf_map_fini(atf_map_t *m)
140 atf_list_iter_t iter;
142 atf_list_for_each(iter, &m->m_list) {
143 struct map_entry *me = atf_list_iter_data(iter);
145 if (me->m_managed)
146 free(me->m_value);
147 free(me->m_key);
148 free(me);
150 atf_list_fini(&m->m_list);
152 atf_object_fini(&m->m_object);
156 * Getters.
159 atf_map_iter_t
160 atf_map_end(atf_map_t *m)
162 atf_map_iter_t iter;
163 iter.m_map = m;
164 iter.m_entry = NULL;
165 return iter;
168 atf_map_citer_t
169 atf_map_end_c(const atf_map_t *m)
171 atf_map_citer_t iter;
172 iter.m_map = m;
173 iter.m_entry = NULL;
174 return iter;
177 atf_map_iter_t
178 atf_map_find(atf_map_t *m, const char *key)
180 atf_list_iter_t iter;
182 atf_list_for_each(iter, &m->m_list) {
183 struct map_entry *me = atf_list_iter_data(iter);
185 if (strcmp(me->m_key, key) == 0) {
186 atf_map_iter_t i;
187 i.m_map = m;
188 i.m_entry = me;
189 return i;
193 return atf_map_end(m);
196 atf_map_citer_t
197 atf_map_find_c(const atf_map_t *m, const char *key)
199 atf_list_citer_t iter;
201 atf_list_for_each_c(iter, &m->m_list) {
202 const struct map_entry *me = atf_list_citer_data(iter);
204 if (strcmp(me->m_key, key) == 0) {
205 atf_map_citer_t i;
206 i.m_map = m;
207 i.m_entry = me;
208 return i;
212 return atf_map_end_c(m);
215 size_t
216 atf_map_size(const atf_map_t *m)
218 return atf_list_size(&m->m_list);
222 * Modifiers.
225 atf_error_t
226 atf_map_insert(atf_map_t *m, const char *key, void *value, bool managed)
228 struct map_entry *me;
229 atf_error_t err;
230 atf_map_iter_t iter;
232 iter = atf_map_find(m, key);
233 if (atf_equal_map_iter_map_iter(iter, atf_map_end(m))) {
234 me = new_entry(key, value, managed);
235 if (me == NULL)
236 err = atf_no_memory_error();
237 else {
238 err = atf_list_append(&m->m_list, me, false);
239 if (atf_is_error(err)) {
240 if (managed)
241 free(value);
242 free(me);
245 } else {
246 me = iter.m_entry;
247 if (me->m_managed)
248 free(me->m_value);
250 INV(strcmp(me->m_key, key) == 0);
251 me->m_value = value;
252 me->m_managed = managed;
254 err = atf_no_error();
257 return err;