| 1 | /* |
| 2 | * field-path.c |
| 3 | * |
| 4 | * Babeltrace CTF IR - Field path |
| 5 | * |
| 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * Copyright 2016 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/ctf-ir/field-types.h> |
| 29 | #include <babeltrace/ctf-ir/field-path-internal.h> |
| 30 | #include <babeltrace/ctf-ir/field-path.h> |
| 31 | #include <limits.h> |
| 32 | #include <glib.h> |
| 33 | |
| 34 | static |
| 35 | void field_path_destroy(struct bt_object *obj) |
| 36 | { |
| 37 | struct bt_ctf_field_path *field_path = (struct bt_ctf_field_path *) obj; |
| 38 | |
| 39 | if (!field_path) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | if (field_path->indexes) { |
| 44 | g_array_free(field_path->indexes, TRUE); |
| 45 | } |
| 46 | g_free(field_path); |
| 47 | } |
| 48 | |
| 49 | BT_HIDDEN |
| 50 | struct bt_ctf_field_path *bt_ctf_field_path_create(void) |
| 51 | { |
| 52 | struct bt_ctf_field_path *field_path = NULL; |
| 53 | |
| 54 | field_path = g_new0(struct bt_ctf_field_path, 1); |
| 55 | if (!field_path) { |
| 56 | goto error; |
| 57 | } |
| 58 | |
| 59 | bt_object_init(field_path, field_path_destroy); |
| 60 | field_path->root = BT_CTF_SCOPE_UNKNOWN; |
| 61 | field_path->indexes = g_array_new(TRUE, FALSE, sizeof(int)); |
| 62 | if (!field_path->indexes) { |
| 63 | goto error; |
| 64 | } |
| 65 | |
| 66 | return field_path; |
| 67 | |
| 68 | error: |
| 69 | BT_PUT(field_path); |
| 70 | return NULL; |
| 71 | } |
| 72 | |
| 73 | BT_HIDDEN |
| 74 | void bt_ctf_field_path_clear(struct bt_ctf_field_path *field_path) |
| 75 | { |
| 76 | if (field_path->indexes->len > 0) { |
| 77 | g_array_remove_range(field_path->indexes, 0, |
| 78 | field_path->indexes->len); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | BT_HIDDEN |
| 83 | struct bt_ctf_field_path *bt_ctf_field_path_copy( |
| 84 | struct bt_ctf_field_path *path) |
| 85 | { |
| 86 | struct bt_ctf_field_path *new_path = bt_ctf_field_path_create(); |
| 87 | |
| 88 | if (!new_path) { |
| 89 | goto end; |
| 90 | } |
| 91 | |
| 92 | new_path->root = path->root; |
| 93 | g_array_insert_vals(new_path->indexes, 0, |
| 94 | path->indexes->data, path->indexes->len); |
| 95 | end: |
| 96 | return new_path; |
| 97 | } |
| 98 | |
| 99 | enum bt_ctf_scope bt_ctf_field_path_get_root_scope( |
| 100 | const struct bt_ctf_field_path *field_path) |
| 101 | { |
| 102 | enum bt_ctf_scope scope = BT_CTF_SCOPE_UNKNOWN; |
| 103 | |
| 104 | if (!field_path) { |
| 105 | goto end; |
| 106 | } |
| 107 | |
| 108 | scope = field_path->root; |
| 109 | |
| 110 | end: |
| 111 | return scope; |
| 112 | } |
| 113 | |
| 114 | int bt_ctf_field_path_get_index_count( |
| 115 | const struct bt_ctf_field_path *field_path) |
| 116 | { |
| 117 | int ret = -1; |
| 118 | |
| 119 | if (!field_path) { |
| 120 | goto end; |
| 121 | } |
| 122 | |
| 123 | ret = field_path->indexes->len; |
| 124 | |
| 125 | end: |
| 126 | return ret; |
| 127 | } |
| 128 | |
| 129 | int bt_ctf_field_path_get_index(const struct bt_ctf_field_path *field_path, |
| 130 | int index) |
| 131 | { |
| 132 | int ret = INT_MIN; |
| 133 | |
| 134 | if (!field_path || index < 0) { |
| 135 | goto end; |
| 136 | } |
| 137 | |
| 138 | if (index >= field_path->indexes->len) { |
| 139 | goto end; |
| 140 | } |
| 141 | |
| 142 | ret = g_array_index(field_path->indexes, int, index); |
| 143 | |
| 144 | end: |
| 145 | return ret; |
| 146 | } |