ir: add trace UUID getter and setter
[deliverable/babeltrace.git] / lib / ctf-ir / field-path.c
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 <stdint.h>
33 #include <glib.h>
34
35 static
36 void field_path_destroy(struct bt_object *obj)
37 {
38 struct bt_ctf_field_path *field_path = (struct bt_ctf_field_path *) obj;
39
40 if (!field_path) {
41 return;
42 }
43
44 if (field_path->indexes) {
45 g_array_free(field_path->indexes, TRUE);
46 }
47 g_free(field_path);
48 }
49
50 BT_HIDDEN
51 struct bt_ctf_field_path *bt_ctf_field_path_create(void)
52 {
53 struct bt_ctf_field_path *field_path = NULL;
54
55 field_path = g_new0(struct bt_ctf_field_path, 1);
56 if (!field_path) {
57 goto error;
58 }
59
60 bt_object_init(field_path, field_path_destroy);
61 field_path->root = BT_CTF_SCOPE_UNKNOWN;
62 field_path->indexes = g_array_new(TRUE, FALSE, sizeof(int));
63 if (!field_path->indexes) {
64 goto error;
65 }
66
67 return field_path;
68
69 error:
70 BT_PUT(field_path);
71 return NULL;
72 }
73
74 BT_HIDDEN
75 void bt_ctf_field_path_clear(struct bt_ctf_field_path *field_path)
76 {
77 if (field_path->indexes->len > 0) {
78 g_array_remove_range(field_path->indexes, 0,
79 field_path->indexes->len);
80 }
81 }
82
83 BT_HIDDEN
84 struct bt_ctf_field_path *bt_ctf_field_path_copy(
85 struct bt_ctf_field_path *path)
86 {
87 struct bt_ctf_field_path *new_path = bt_ctf_field_path_create();
88
89 if (!new_path) {
90 goto end;
91 }
92
93 new_path->root = path->root;
94 g_array_insert_vals(new_path->indexes, 0,
95 path->indexes->data, path->indexes->len);
96 end:
97 return new_path;
98 }
99
100 enum bt_ctf_scope bt_ctf_field_path_get_root_scope(
101 const struct bt_ctf_field_path *field_path)
102 {
103 enum bt_ctf_scope scope = BT_CTF_SCOPE_UNKNOWN;
104
105 if (!field_path) {
106 goto end;
107 }
108
109 scope = field_path->root;
110
111 end:
112 return scope;
113 }
114
115 int64_t bt_ctf_field_path_get_index_count(
116 const struct bt_ctf_field_path *field_path)
117 {
118 return field_path ? (int64_t) field_path->indexes->len : (int64_t) -1;
119 }
120
121 int bt_ctf_field_path_get_index(const struct bt_ctf_field_path *field_path,
122 uint64_t index)
123 {
124 int ret = INT_MIN;
125
126 if (!field_path) {
127 goto end;
128 }
129
130 if (index >= field_path->indexes->len) {
131 goto end;
132 }
133
134 ret = g_array_index(field_path->indexes, int, index);
135
136 end:
137 return ret;
138 }
This page took 0.047422 seconds and 5 git commands to generate.