blob: 3f3880af6e8e2faa3f860f6be9c7a6b7eead0e22 [file] [log] [blame]
Petr Machata000e3112012-01-03 17:03:39 +01001/*
2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
4 *
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.
9 *
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.
14 *
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
18 * 02110-1301 USA
19 */
20
21#include <assert.h>
22#include <string.h>
23#include <stdlib.h>
24
25#include "value_dict.h"
26#include "value.h"
27
28struct named_value
29{
30 const char *name;
31 struct value value;
32 int own_name;
33};
34
35void
36val_dict_init(struct value_dict *dict)
37{
38 VECT_INIT(&dict->numbered, struct value);
39 VECT_INIT(&dict->named, struct named_value);
40}
41
42static int
43value_clone_cb(struct value *tgt, struct value *src, void *data)
44{
45 return value_clone(tgt, src);
46}
47
48static void
49value_dtor(struct value *val, void *data)
50{
51 value_destroy(val);
52}
53
54static int
55named_value_clone(struct named_value *tgt, struct named_value *src, void *data)
56{
57 tgt->name = strdup(src->name);
58 if (tgt->name == NULL)
59 return -1;
60 tgt->own_name = 1;
61 if (value_clone(&tgt->value, &src->value) < 0) {
62 free((char *)tgt->name);
63 return -1;
64 }
65 return 0;
66}
67
68static void
69named_value_dtor(struct named_value *named, void *data)
70{
71 if (named->own_name)
72 free((char *)named->name);
73 value_destroy(&named->value);
74}
75
76int
77val_dict_clone(struct value_dict *target, struct value_dict *source)
78{
79 if (VECT_CLONE(&target->numbered, &source->numbered, struct value,
80 value_clone_cb, value_dtor, NULL) < 0)
81 return -1;
82
83 if (VECT_CLONE(&target->named, &source->named, struct named_value,
84 named_value_clone, named_value_dtor, NULL) < 0) {
85 VECT_DESTROY(&target->numbered, struct value, value_dtor, NULL);
86 return -1;
87 }
88
89 return 0;
90}
91
92int
93val_dict_push_next(struct value_dict *dict, struct value *val)
94{
95 return VECT_PUSHBACK(&dict->numbered, val);
96}
97
98int
99val_dict_push_named(struct value_dict *dict, struct value *val,
100 const char *name, int own_name)
101{
102 if (own_name && (name = strdup(name)) == NULL)
103 return -1;
104 struct named_value element = { name, *val, own_name };
105 if (VECT_PUSHBACK(&dict->named, &element) < 0) {
106 if (own_name)
107 free((char *)name);
108 return -1;
109 }
110 return 0;
111}
112
113size_t
114val_dict_count(struct value_dict *dict)
115{
116 return vect_size(&dict->numbered);
117}
118
119struct value *
120val_dict_get_num(struct value_dict *dict, size_t num)
121{
122 assert(num < vect_size(&dict->numbered));
123 return VECT_ELEMENT(&dict->numbered, struct value, num);
124}
125
126struct value *
127val_dict_get_name(struct value_dict *dict, const char *name)
128{
129 size_t i;
130 for (i = 0; i < vect_size(&dict->named); ++i) {
131 struct named_value *element
132 = VECT_ELEMENT(&dict->named, struct named_value, i);
133 if (strcmp(element->name, name) == 0)
134 return &element->value;
135 }
136 return NULL;
137}
138
139void
140val_dict_destroy(struct value_dict *dict)
141{
142 if (dict == NULL)
143 return;
144
145 VECT_DESTROY(&dict->numbered, struct value, value_dtor, NULL);
146 VECT_DESTROY(&dict->named, struct named_value, named_value_dtor, NULL);
147}