2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 #include "value_dict.h"
36 val_dict_init(struct value_dict *dict)
38 VECT_INIT(&dict->numbered, struct value);
39 VECT_INIT(&dict->named, struct named_value);
43 value_clone_cb(struct value *tgt, const struct value *src, void *data)
45 return value_clone(tgt, src);
49 value_dtor(struct value *val, void *data)
55 named_value_clone(struct named_value *tgt,
56 const struct named_value *src, void *data)
58 tgt->name = strdup(src->name);
59 if (tgt->name == NULL)
62 if (value_clone(&tgt->value, &src->value) < 0) {
63 free((char *)tgt->name);
70 named_value_dtor(struct named_value *named, void *data)
73 free((char *)named->name);
74 value_destroy(&named->value);
78 val_dict_clone(struct value_dict *target, struct value_dict *source)
80 if (VECT_CLONE(&target->numbered, &source->numbered, struct value,
81 value_clone_cb, value_dtor, NULL) < 0)
84 if (VECT_CLONE(&target->named, &source->named, struct named_value,
85 named_value_clone, named_value_dtor, NULL) < 0) {
86 VECT_DESTROY(&target->numbered, struct value, value_dtor, NULL);
94 val_dict_push_next(struct value_dict *dict, struct value *val)
96 return VECT_PUSHBACK(&dict->numbered, val);
100 val_dict_push_named(struct value_dict *dict, struct value *val,
101 const char *name, int own_name)
103 if (own_name && (name = strdup(name)) == NULL)
105 struct named_value element = { name, *val, own_name };
106 if (VECT_PUSHBACK(&dict->named, &element) < 0) {
115 val_dict_count(struct value_dict *dict)
117 return vect_size(&dict->numbered);
121 val_dict_get_num(struct value_dict *dict, size_t num)
123 assert(num < vect_size(&dict->numbered));
124 return VECT_ELEMENT(&dict->numbered, struct value, num);
128 val_dict_get_name(struct value_dict *dict, const char *name)
131 for (i = 0; i < vect_size(&dict->named); ++i) {
132 struct named_value *element
133 = VECT_ELEMENT(&dict->named, struct named_value, i);
134 if (strcmp(element->name, name) == 0)
135 return &element->value;
141 val_dict_destroy(struct value_dict *dict)
146 VECT_DESTROY(&dict->numbered, struct value, value_dtor, NULL);
147 VECT_DESTROY(&dict->named, struct named_value, named_value_dtor, NULL);