2 * Automated Testing Framework (atf)
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
33 #include "atf-c/error.h"
34 #include "atf-c/map.h"
35 #include "atf-c/sanity.h"
37 /* ---------------------------------------------------------------------
38 * Auxiliary functions.
39 * --------------------------------------------------------------------- */
49 new_entry(const char *key
, void *value
, bool managed
)
53 me
= (struct map_entry
*)malloc(sizeof(*me
));
55 me
->m_key
= strdup(key
);
56 if (me
->m_key
== NULL
) {
61 me
->m_managed
= managed
;
68 /* ---------------------------------------------------------------------
69 * The "atf_map_citer" type.
70 * --------------------------------------------------------------------- */
77 atf_map_citer_data(const atf_map_citer_t citer
)
79 const struct map_entry
*me
= citer
.m_entry
;
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 * --------------------------------------------------------------------- */
100 atf_map_iter_data(const atf_map_iter_t iter
)
102 const struct map_entry
*me
= iter
.m_entry
;
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.
123 atf_map_init(atf_map_t
*m
)
127 err
= atf_list_init(&m
->m_list
);
128 if (atf_is_error(err
))
131 atf_object_init(&m
->m_object
);
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
);
150 atf_list_fini(&m
->m_list
);
152 atf_object_fini(&m
->m_object
);
160 atf_map_end(atf_map_t
*m
)
169 atf_map_end_c(const atf_map_t
*m
)
171 atf_map_citer_t iter
;
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) {
193 return atf_map_end(m
);
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) {
212 return atf_map_end_c(m
);
216 atf_map_size(const atf_map_t
*m
)
218 return atf_list_size(&m
->m_list
);
226 atf_map_insert(atf_map_t
*m
, const char *key
, void *value
, bool managed
)
228 struct map_entry
*me
;
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
);
236 err
= atf_no_memory_error();
238 err
= atf_list_append(&m
->m_list
, me
, false);
239 if (atf_is_error(err
)) {
250 INV(strcmp(me
->m_key
, key
) == 0);
252 me
->m_managed
= managed
;
254 err
= atf_no_error();