Petr Machata | 000e311 | 2012-01-03 17:03:39 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 28 | struct named_value |
| 29 | { |
| 30 | const char *name; |
| 31 | struct value value; |
| 32 | int own_name; |
| 33 | }; |
| 34 | |
| 35 | void |
| 36 | val_dict_init(struct value_dict *dict) |
| 37 | { |
| 38 | VECT_INIT(&dict->numbered, struct value); |
| 39 | VECT_INIT(&dict->named, struct named_value); |
| 40 | } |
| 41 | |
| 42 | static int |
Petr Machata | edce6cf | 2012-11-26 15:51:12 +0100 | [diff] [blame] | 43 | value_clone_cb(struct value *tgt, const struct value *src, void *data) |
Petr Machata | 000e311 | 2012-01-03 17:03:39 +0100 | [diff] [blame] | 44 | { |
| 45 | return value_clone(tgt, src); |
| 46 | } |
| 47 | |
| 48 | static void |
| 49 | value_dtor(struct value *val, void *data) |
| 50 | { |
| 51 | value_destroy(val); |
| 52 | } |
| 53 | |
| 54 | static int |
Petr Machata | edce6cf | 2012-11-26 15:51:12 +0100 | [diff] [blame] | 55 | named_value_clone(struct named_value *tgt, |
| 56 | const struct named_value *src, void *data) |
Petr Machata | 000e311 | 2012-01-03 17:03:39 +0100 | [diff] [blame] | 57 | { |
| 58 | tgt->name = strdup(src->name); |
| 59 | if (tgt->name == NULL) |
| 60 | return -1; |
| 61 | tgt->own_name = 1; |
| 62 | if (value_clone(&tgt->value, &src->value) < 0) { |
| 63 | free((char *)tgt->name); |
| 64 | return -1; |
| 65 | } |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static void |
| 70 | named_value_dtor(struct named_value *named, void *data) |
| 71 | { |
| 72 | if (named->own_name) |
| 73 | free((char *)named->name); |
| 74 | value_destroy(&named->value); |
| 75 | } |
| 76 | |
| 77 | int |
| 78 | val_dict_clone(struct value_dict *target, struct value_dict *source) |
| 79 | { |
| 80 | if (VECT_CLONE(&target->numbered, &source->numbered, struct value, |
| 81 | value_clone_cb, value_dtor, NULL) < 0) |
| 82 | return -1; |
| 83 | |
| 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); |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | int |
| 94 | val_dict_push_next(struct value_dict *dict, struct value *val) |
| 95 | { |
| 96 | return VECT_PUSHBACK(&dict->numbered, val); |
| 97 | } |
| 98 | |
| 99 | int |
| 100 | val_dict_push_named(struct value_dict *dict, struct value *val, |
| 101 | const char *name, int own_name) |
| 102 | { |
| 103 | if (own_name && (name = strdup(name)) == NULL) |
| 104 | return -1; |
| 105 | struct named_value element = { name, *val, own_name }; |
| 106 | if (VECT_PUSHBACK(&dict->named, &element) < 0) { |
| 107 | if (own_name) |
| 108 | free((char *)name); |
| 109 | return -1; |
| 110 | } |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | size_t |
| 115 | val_dict_count(struct value_dict *dict) |
| 116 | { |
| 117 | return vect_size(&dict->numbered); |
| 118 | } |
| 119 | |
| 120 | struct value * |
| 121 | val_dict_get_num(struct value_dict *dict, size_t num) |
| 122 | { |
| 123 | assert(num < vect_size(&dict->numbered)); |
| 124 | return VECT_ELEMENT(&dict->numbered, struct value, num); |
| 125 | } |
| 126 | |
| 127 | struct value * |
| 128 | val_dict_get_name(struct value_dict *dict, const char *name) |
| 129 | { |
| 130 | size_t i; |
| 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; |
| 136 | } |
| 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | void |
| 141 | val_dict_destroy(struct value_dict *dict) |
| 142 | { |
| 143 | if (dict == NULL) |
| 144 | return; |
| 145 | |
| 146 | VECT_DESTROY(&dict->numbered, struct value, value_dtor, NULL); |
| 147 | VECT_DESTROY(&dict->named, struct named_value, named_value_dtor, NULL); |
| 148 | } |