4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews/* Copyright (c) 2008 The NetBSD Foundation, Inc.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * All rights reserved.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Redistribution and use in source and binary forms, with or without
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * modification, are permitted provided that the following conditions
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * are met:
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * 1. Redistributions of source code must retain the above copyright
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * notice, this list of conditions and the following disclaimer.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * 2. Redistributions in binary form must reproduce the above copyright
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * notice, this list of conditions and the following disclaimer in the
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * documentation and/or other materials provided with the distribution.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include "atf-c/detail/map.h"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <errno.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <stdlib.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <string.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include "atf-c/detail/sanity.h"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include "atf-c/error.h"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include "atf-c/utils.h"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* ---------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Auxiliary functions.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * --------------------------------------------------------------------- */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstruct map_entry {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt char *m_key;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt void *m_value;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt bool m_managed;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt};
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstruct map_entry *
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntnew_entry(const char *key, void *value, bool managed)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt struct map_entry *me;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt me = (struct map_entry *)malloc(sizeof(*me));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (me != NULL) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt me->m_key = strdup(key);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (me->m_key == NULL) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt free(me);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt me = NULL;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt } else {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt me->m_value = value;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt me->m_managed = managed;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return me;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* ---------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * The "atf_map_citer" type.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * --------------------------------------------------------------------- */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/*
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Getters.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntconst char *
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_citer_key(const atf_map_citer_t citer)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const struct map_entry *me = citer.m_entry;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt PRE(me != NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return me->m_key;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntconst void *
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_citer_data(const atf_map_citer_t citer)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const struct map_entry *me = citer.m_entry;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt PRE(me != NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return me->m_value;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_citer_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_citer_next(const atf_map_citer_t citer)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_map_citer_t newciter;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt newciter = citer;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt newciter.m_listiter = atf_list_citer_next(citer.m_listiter);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt newciter.m_entry = ((const struct map_entry *)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_list_citer_data(newciter.m_listiter));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return newciter;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntbool
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_equal_map_citer_map_citer(const atf_map_citer_t i1,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const atf_map_citer_t i2)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return i1.m_map == i2.m_map && i1.m_entry == i2.m_entry;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* ---------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * The "atf_map_iter" type.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * --------------------------------------------------------------------- */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/*
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Getters.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntconst char *
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_iter_key(const atf_map_iter_t iter)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const struct map_entry *me = iter.m_entry;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt PRE(me != NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return me->m_key;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntvoid *
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_iter_data(const atf_map_iter_t iter)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const struct map_entry *me = iter.m_entry;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt PRE(me != NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return me->m_value;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_iter_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_iter_next(const atf_map_iter_t iter)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_map_iter_t newiter;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt newiter = iter;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt newiter.m_listiter = atf_list_iter_next(iter.m_listiter);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt newiter.m_entry = ((struct map_entry *)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_list_iter_data(newiter.m_listiter));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return newiter;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntbool
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_equal_map_iter_map_iter(const atf_map_iter_t i1,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const atf_map_iter_t i2)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return i1.m_map == i2.m_map && i1.m_entry == i2.m_entry;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* ---------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * The "atf_map" type.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * --------------------------------------------------------------------- */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/*
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Constructors and destructors.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_error_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_init(atf_map_t *m)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return atf_list_init(&m->m_list);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_error_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_init_charpp(atf_map_t *m, const char *const *array)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_error_t err;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *const *ptr = array;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err = atf_map_init(m);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (array != NULL) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt while (!atf_is_error(err) && *ptr != NULL) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *key, *value;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt key = *ptr;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt INV(key != NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ptr++;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if ((value = *ptr) == NULL) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err = atf_libc_error(EINVAL, "List too short; no value for "
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "key '%s' provided", key); /* XXX: Not really libc_error */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt break;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ptr++;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err = atf_map_insert(m, key, strdup(value), true);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (atf_is_error(err))
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_map_fini(m);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return err;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntvoid
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_fini(atf_map_t *m)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_list_iter_t iter;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_list_for_each(iter, &m->m_list) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt struct map_entry *me = atf_list_iter_data(iter);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (me->m_managed)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt free(me->m_value);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt free(me->m_key);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt free(me);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_list_fini(&m->m_list);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/*
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Getters.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_iter_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_begin(atf_map_t *m)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_map_iter_t iter;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt iter.m_map = m;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt iter.m_listiter = atf_list_begin(&m->m_list);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt iter.m_entry = atf_list_iter_data(iter.m_listiter);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return iter;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_citer_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_begin_c(const atf_map_t *m)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_map_citer_t citer;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt citer.m_map = m;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt citer.m_listiter = atf_list_begin_c(&m->m_list);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt citer.m_entry = atf_list_citer_data(citer.m_listiter);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return citer;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_iter_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_end(atf_map_t *m)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_map_iter_t iter;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt iter.m_map = m;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt iter.m_entry = NULL;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt iter.m_listiter = atf_list_end(&m->m_list);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return iter;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_citer_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_end_c(const atf_map_t *m)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_map_citer_t iter;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt iter.m_map = m;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt iter.m_entry = NULL;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt iter.m_listiter = atf_list_end_c(&m->m_list);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return iter;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_iter_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_find(atf_map_t *m, const char *key)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_list_iter_t iter;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_list_for_each(iter, &m->m_list) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt struct map_entry *me = atf_list_iter_data(iter);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (strcmp(me->m_key, key) == 0) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_map_iter_t i;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt i.m_map = m;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt i.m_entry = me;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt i.m_listiter = iter;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return i;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return atf_map_end(m);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_citer_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_find_c(const atf_map_t *m, const char *key)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_list_citer_t iter;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_list_for_each_c(iter, &m->m_list) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const struct map_entry *me = atf_list_citer_data(iter);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (strcmp(me->m_key, key) == 0) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_map_citer_t i;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt i.m_map = m;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt i.m_entry = me;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt i.m_listiter = iter;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return i;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return atf_map_end_c(m);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntsize_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_size(const atf_map_t *m)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return atf_list_size(&m->m_list);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntchar **
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_to_charpp(const atf_map_t *l)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt char **array;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_map_citer_t iter;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt size_t i;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt array = malloc(sizeof(char *) * (atf_map_size(l) * 2 + 1));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (array == NULL)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt goto out;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt i = 0;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_map_for_each_c(iter, l) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt array[i] = strdup(atf_map_citer_key(iter));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (array[i] == NULL) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_utils_free_charpp(array);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt array = NULL;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt goto out;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt array[i + 1] = strdup((const char *)atf_map_citer_data(iter));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (array[i + 1] == NULL) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_utils_free_charpp(array);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt array = NULL;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt goto out;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt i += 2;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt array[i] = NULL;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntout:
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return array;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/*
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Modifiers.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_error_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_map_insert(atf_map_t *m, const char *key, void *value, bool managed)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt struct map_entry *me;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_error_t err;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_map_iter_t iter;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt iter = atf_map_find(m, key);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (atf_equal_map_iter_map_iter(iter, atf_map_end(m))) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt me = new_entry(key, value, managed);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (me == NULL)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err = atf_no_memory_error();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt else {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err = atf_list_append(&m->m_list, me, false);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (atf_is_error(err)) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (managed)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt free(value);
f7f4730e563a2749629fe7fef4cd9513cd2bfab7Mark Andrews free(me->m_key);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt free(me);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt } else {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt me = iter.m_entry;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (me->m_managed)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt free(me->m_value);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt INV(strcmp(me->m_key, key) == 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt me->m_value = value;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt me->m_managed = managed;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err = atf_no_error();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return err;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}