| 1 | /* |
| 2 | * attributes.c |
| 3 | * |
| 4 | * Babeltrace CTF IR - Attributes |
| 5 | * |
| 6 | * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation |
| 7 | * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com> |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | * of this software and associated documentation files (the "Software"), to deal |
| 11 | * in the Software without restriction, including without limitation the rights |
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | * copies of the Software, and to permit persons to whom the Software is |
| 14 | * furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | * SOFTWARE. |
| 26 | */ |
| 27 | |
| 28 | #include <babeltrace/babeltrace-internal.h> |
| 29 | #include <babeltrace/values.h> |
| 30 | |
| 31 | #define BT_CTF_ATTR_NAME_INDEX 0 |
| 32 | #define BT_CTF_ATTR_VALUE_INDEX 1 |
| 33 | |
| 34 | BT_HIDDEN |
| 35 | struct bt_value *bt_ctf_attributes_create(void) |
| 36 | { |
| 37 | /* |
| 38 | * Attributes: array value object of array value objects, each one |
| 39 | * containing two entries: a string value object (attributes |
| 40 | * field name), and a value object (attributes field value). |
| 41 | * |
| 42 | * Example (JSON representation): |
| 43 | * |
| 44 | * [ |
| 45 | * ["hostname", "eeppdesk"], |
| 46 | * ["sysname", "Linux"], |
| 47 | * ["tracer_major", 2], |
| 48 | * ["tracer_minor", 5] |
| 49 | * ] |
| 50 | */ |
| 51 | return bt_value_array_create(); |
| 52 | } |
| 53 | |
| 54 | BT_HIDDEN |
| 55 | void bt_ctf_attributes_destroy(struct bt_value *attr_obj) |
| 56 | { |
| 57 | bt_put(attr_obj); |
| 58 | } |
| 59 | |
| 60 | BT_HIDDEN |
| 61 | int bt_ctf_attributes_get_count(struct bt_value *attr_obj) |
| 62 | { |
| 63 | return bt_value_array_size(attr_obj); |
| 64 | } |
| 65 | |
| 66 | BT_HIDDEN |
| 67 | const char *bt_ctf_attributes_get_field_name(struct bt_value *attr_obj, |
| 68 | int index) |
| 69 | { |
| 70 | int rc; |
| 71 | const char *ret = NULL; |
| 72 | struct bt_value *attr_field_obj = NULL; |
| 73 | struct bt_value *attr_field_name_obj = NULL; |
| 74 | |
| 75 | if (!attr_obj || index < 0) { |
| 76 | goto end; |
| 77 | } |
| 78 | |
| 79 | attr_field_obj = bt_value_array_get(attr_obj, index); |
| 80 | |
| 81 | if (!attr_field_obj) { |
| 82 | goto end; |
| 83 | } |
| 84 | |
| 85 | attr_field_name_obj = bt_value_array_get(attr_field_obj, |
| 86 | BT_CTF_ATTR_NAME_INDEX); |
| 87 | |
| 88 | if (!attr_field_name_obj) { |
| 89 | goto end; |
| 90 | } |
| 91 | |
| 92 | rc = bt_value_string_get(attr_field_name_obj, &ret); |
| 93 | |
| 94 | if (rc) { |
| 95 | ret = NULL; |
| 96 | } |
| 97 | |
| 98 | end: |
| 99 | BT_PUT(attr_field_name_obj); |
| 100 | BT_PUT(attr_field_obj); |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | BT_HIDDEN |
| 105 | struct bt_value *bt_ctf_attributes_get_field_value(struct bt_value *attr_obj, |
| 106 | int index) |
| 107 | { |
| 108 | struct bt_value *value_obj = NULL; |
| 109 | struct bt_value *attr_field_obj = NULL; |
| 110 | |
| 111 | if (!attr_obj || index < 0) { |
| 112 | goto end; |
| 113 | } |
| 114 | |
| 115 | attr_field_obj = bt_value_array_get(attr_obj, index); |
| 116 | |
| 117 | if (!attr_field_obj) { |
| 118 | goto end; |
| 119 | } |
| 120 | |
| 121 | value_obj = bt_value_array_get(attr_field_obj, |
| 122 | BT_CTF_ATTR_VALUE_INDEX); |
| 123 | |
| 124 | end: |
| 125 | BT_PUT(attr_field_obj); |
| 126 | return value_obj; |
| 127 | } |
| 128 | |
| 129 | static |
| 130 | struct bt_value *bt_ctf_attributes_get_field_by_name( |
| 131 | struct bt_value *attr_obj, const char *name) |
| 132 | { |
| 133 | int i; |
| 134 | int attr_size; |
| 135 | struct bt_value *value_obj = NULL; |
| 136 | struct bt_value *attr_field_name_obj = NULL; |
| 137 | |
| 138 | attr_size = bt_value_array_size(attr_obj); |
| 139 | |
| 140 | if (attr_size < 0) { |
| 141 | goto error; |
| 142 | } |
| 143 | |
| 144 | for (i = 0; i < attr_size; ++i) { |
| 145 | int ret; |
| 146 | const char *field_name; |
| 147 | |
| 148 | value_obj = bt_value_array_get(attr_obj, i); |
| 149 | |
| 150 | if (!value_obj) { |
| 151 | goto error; |
| 152 | } |
| 153 | |
| 154 | attr_field_name_obj = bt_value_array_get(value_obj, 0); |
| 155 | |
| 156 | if (!attr_field_name_obj) { |
| 157 | goto error; |
| 158 | } |
| 159 | |
| 160 | ret = bt_value_string_get(attr_field_name_obj, &field_name); |
| 161 | if (ret) { |
| 162 | goto error; |
| 163 | } |
| 164 | |
| 165 | if (!strcmp(field_name, name)) { |
| 166 | BT_PUT(attr_field_name_obj); |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | BT_PUT(attr_field_name_obj); |
| 171 | BT_PUT(value_obj); |
| 172 | } |
| 173 | |
| 174 | return value_obj; |
| 175 | |
| 176 | error: |
| 177 | BT_PUT(attr_field_name_obj); |
| 178 | BT_PUT(value_obj); |
| 179 | |
| 180 | return value_obj; |
| 181 | } |
| 182 | |
| 183 | BT_HIDDEN |
| 184 | int bt_ctf_attributes_set_field_value(struct bt_value *attr_obj, |
| 185 | const char *name, struct bt_value *value_obj) |
| 186 | { |
| 187 | int ret = 0; |
| 188 | struct bt_value *attr_field_obj = NULL; |
| 189 | |
| 190 | if (!attr_obj || !name || !value_obj) { |
| 191 | ret = -1; |
| 192 | goto end; |
| 193 | } |
| 194 | |
| 195 | attr_field_obj = bt_ctf_attributes_get_field_by_name(attr_obj, name); |
| 196 | |
| 197 | if (attr_field_obj) { |
| 198 | ret = bt_value_array_set(attr_field_obj, |
| 199 | BT_CTF_ATTR_VALUE_INDEX, value_obj); |
| 200 | goto end; |
| 201 | } |
| 202 | |
| 203 | attr_field_obj = bt_value_array_create(); |
| 204 | |
| 205 | if (!attr_field_obj) { |
| 206 | ret = -1; |
| 207 | goto end; |
| 208 | } |
| 209 | |
| 210 | ret = bt_value_array_append_string(attr_field_obj, name); |
| 211 | ret |= bt_value_array_append(attr_field_obj, value_obj); |
| 212 | |
| 213 | if (ret) { |
| 214 | goto end; |
| 215 | } |
| 216 | |
| 217 | ret = bt_value_array_append(attr_obj, attr_field_obj); |
| 218 | |
| 219 | end: |
| 220 | BT_PUT(attr_field_obj); |
| 221 | |
| 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | BT_HIDDEN |
| 226 | struct bt_value *bt_ctf_attributes_get_field_value_by_name( |
| 227 | struct bt_value *attr_obj, const char *name) |
| 228 | { |
| 229 | struct bt_value *value_obj = NULL; |
| 230 | struct bt_value *attr_field_obj = NULL; |
| 231 | |
| 232 | if (!attr_obj || !name) { |
| 233 | goto end; |
| 234 | } |
| 235 | |
| 236 | attr_field_obj = bt_ctf_attributes_get_field_by_name(attr_obj, name); |
| 237 | |
| 238 | if (!attr_field_obj) { |
| 239 | goto end; |
| 240 | } |
| 241 | |
| 242 | value_obj = bt_value_array_get(attr_field_obj, |
| 243 | BT_CTF_ATTR_VALUE_INDEX); |
| 244 | |
| 245 | end: |
| 246 | BT_PUT(attr_field_obj); |
| 247 | |
| 248 | return value_obj; |
| 249 | } |
| 250 | |
| 251 | BT_HIDDEN |
| 252 | int bt_ctf_attributes_freeze(struct bt_value *attr_obj) |
| 253 | { |
| 254 | int i; |
| 255 | int count; |
| 256 | int ret = 0; |
| 257 | |
| 258 | if (!attr_obj) { |
| 259 | ret = -1; |
| 260 | goto end; |
| 261 | } |
| 262 | |
| 263 | count = bt_value_array_size(attr_obj); |
| 264 | |
| 265 | if (count < 0) { |
| 266 | ret = -1; |
| 267 | goto end; |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * We do not freeze the array value object itself here, since |
| 272 | * internal stuff could need to modify/add attributes. Each |
| 273 | * attribute is frozen one by one. |
| 274 | */ |
| 275 | for (i = 0; i < count; ++i) { |
| 276 | struct bt_value *obj = NULL; |
| 277 | |
| 278 | obj = bt_ctf_attributes_get_field_value(attr_obj, i); |
| 279 | |
| 280 | if (!obj) { |
| 281 | ret = -1; |
| 282 | goto end; |
| 283 | } |
| 284 | |
| 285 | bt_value_freeze(obj); |
| 286 | BT_PUT(obj); |
| 287 | } |
| 288 | |
| 289 | end: |
| 290 | return ret; |
| 291 | } |