Unify reference counting using a common bt_object base
[babeltrace.git] / formats / ctf / ir / attributes.c
CommitLineData
44e0a4f5
JG
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>
dac5c838 29#include <babeltrace/values.h>
44e0a4f5
JG
30
31#define BT_CTF_ATTR_NAME_INDEX 0
83509119 32#define BT_CTF_ATTR_VALUE_INDEX 1
44e0a4f5
JG
33
34BT_HIDDEN
dac5c838 35struct bt_value *bt_ctf_attributes_create(void)
44e0a4f5
JG
36{
37 /*
dac5c838
PP
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).
44e0a4f5
JG
41 *
42 * Example (JSON representation):
43 *
44 * [
45 * ["hostname", "eeppdesk"],
46 * ["sysname", "Linux"],
47 * ["tracer_major", 2],
48 * ["tracer_minor", 5]
49 * ]
50 */
dac5c838 51 return bt_value_array_create();
44e0a4f5
JG
52}
53
54BT_HIDDEN
dac5c838 55void bt_ctf_attributes_destroy(struct bt_value *attr_obj)
44e0a4f5 56{
83509119 57 bt_put(attr_obj);
44e0a4f5
JG
58}
59
60BT_HIDDEN
dac5c838 61int bt_ctf_attributes_get_count(struct bt_value *attr_obj)
44e0a4f5 62{
dac5c838 63 return bt_value_array_size(attr_obj);
44e0a4f5
JG
64}
65
66BT_HIDDEN
dac5c838 67const char *bt_ctf_attributes_get_field_name(struct bt_value *attr_obj,
44e0a4f5
JG
68 int index)
69{
70 int rc;
71 const char *ret = NULL;
dac5c838
PP
72 struct bt_value *attr_field_obj = NULL;
73 struct bt_value *attr_field_name_obj = NULL;
44e0a4f5
JG
74
75 if (!attr_obj || index < 0) {
76 goto end;
77 }
78
dac5c838 79 attr_field_obj = bt_value_array_get(attr_obj, index);
44e0a4f5
JG
80
81 if (!attr_field_obj) {
82 goto end;
83 }
84
dac5c838 85 attr_field_name_obj = bt_value_array_get(attr_field_obj,
44e0a4f5
JG
86 BT_CTF_ATTR_NAME_INDEX);
87
88 if (!attr_field_name_obj) {
89 goto end;
90 }
91
dac5c838 92 rc = bt_value_string_get(attr_field_name_obj, &ret);
44e0a4f5
JG
93
94 if (rc) {
95 ret = NULL;
96 }
97
98end:
83509119
JG
99 BT_PUT(attr_field_name_obj);
100 BT_PUT(attr_field_obj);
44e0a4f5
JG
101 return ret;
102}
103
104BT_HIDDEN
dac5c838 105struct bt_value *bt_ctf_attributes_get_field_value(struct bt_value *attr_obj,
44e0a4f5
JG
106 int index)
107{
dac5c838
PP
108 struct bt_value *value_obj = NULL;
109 struct bt_value *attr_field_obj = NULL;
44e0a4f5
JG
110
111 if (!attr_obj || index < 0) {
112 goto end;
113 }
114
dac5c838 115 attr_field_obj = bt_value_array_get(attr_obj, index);
44e0a4f5
JG
116
117 if (!attr_field_obj) {
118 goto end;
119 }
120
dac5c838 121 value_obj = bt_value_array_get(attr_field_obj,
44e0a4f5
JG
122 BT_CTF_ATTR_VALUE_INDEX);
123
124end:
83509119 125 BT_PUT(attr_field_obj);
44e0a4f5
JG
126 return value_obj;
127}
128
129static
dac5c838
PP
130struct bt_value *bt_ctf_attributes_get_field_by_name(
131 struct bt_value *attr_obj, const char *name)
44e0a4f5
JG
132{
133 int i;
134 int attr_size;
dac5c838
PP
135 struct bt_value *value_obj = NULL;
136 struct bt_value *attr_field_name_obj = NULL;
44e0a4f5 137
dac5c838 138 attr_size = bt_value_array_size(attr_obj);
44e0a4f5
JG
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
dac5c838 148 value_obj = bt_value_array_get(attr_obj, i);
44e0a4f5
JG
149
150 if (!value_obj) {
151 goto error;
152 }
153
dac5c838 154 attr_field_name_obj = bt_value_array_get(value_obj, 0);
44e0a4f5
JG
155
156 if (!attr_field_name_obj) {
157 goto error;
158 }
159
dac5c838 160 ret = bt_value_string_get(attr_field_name_obj, &field_name);
44e0a4f5
JG
161 if (ret) {
162 goto error;
163 }
164
165 if (!strcmp(field_name, name)) {
83509119 166 BT_PUT(attr_field_name_obj);
44e0a4f5
JG
167 break;
168 }
169
83509119
JG
170 BT_PUT(attr_field_name_obj);
171 BT_PUT(value_obj);
44e0a4f5
JG
172 }
173
174 return value_obj;
175
176error:
83509119
JG
177 BT_PUT(attr_field_name_obj);
178 BT_PUT(value_obj);
44e0a4f5
JG
179
180 return value_obj;
181}
182
183BT_HIDDEN
dac5c838
PP
184int bt_ctf_attributes_set_field_value(struct bt_value *attr_obj,
185 const char *name, struct bt_value *value_obj)
44e0a4f5
JG
186{
187 int ret = 0;
dac5c838 188 struct bt_value *attr_field_obj = NULL;
44e0a4f5
JG
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) {
dac5c838 198 ret = bt_value_array_set(attr_field_obj,
44e0a4f5
JG
199 BT_CTF_ATTR_VALUE_INDEX, value_obj);
200 goto end;
201 }
202
dac5c838 203 attr_field_obj = bt_value_array_create();
44e0a4f5
JG
204
205 if (!attr_field_obj) {
206 ret = -1;
207 goto end;
208 }
209
dac5c838
PP
210 ret = bt_value_array_append_string(attr_field_obj, name);
211 ret |= bt_value_array_append(attr_field_obj, value_obj);
44e0a4f5
JG
212
213 if (ret) {
214 goto end;
215 }
216
dac5c838 217 ret = bt_value_array_append(attr_obj, attr_field_obj);
44e0a4f5
JG
218
219end:
83509119 220 BT_PUT(attr_field_obj);
44e0a4f5
JG
221
222 return ret;
223}
224
225BT_HIDDEN
dac5c838
PP
226struct bt_value *bt_ctf_attributes_get_field_value_by_name(
227 struct bt_value *attr_obj, const char *name)
44e0a4f5 228{
dac5c838
PP
229 struct bt_value *value_obj = NULL;
230 struct bt_value *attr_field_obj = NULL;
44e0a4f5
JG
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
dac5c838 242 value_obj = bt_value_array_get(attr_field_obj,
44e0a4f5
JG
243 BT_CTF_ATTR_VALUE_INDEX);
244
245end:
83509119 246 BT_PUT(attr_field_obj);
44e0a4f5
JG
247
248 return value_obj;
249}
250
251BT_HIDDEN
dac5c838 252int bt_ctf_attributes_freeze(struct bt_value *attr_obj)
44e0a4f5
JG
253{
254 int i;
255 int count;
256 int ret = 0;
257
258 if (!attr_obj) {
259 ret = -1;
260 goto end;
261 }
262
dac5c838 263 count = bt_value_array_size(attr_obj);
44e0a4f5
JG
264
265 if (count < 0) {
266 ret = -1;
267 goto end;
268 }
269
270 /*
dac5c838
PP
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.
44e0a4f5
JG
274 */
275 for (i = 0; i < count; ++i) {
dac5c838 276 struct bt_value *obj = NULL;
44e0a4f5
JG
277
278 obj = bt_ctf_attributes_get_field_value(attr_obj, i);
279
280 if (!obj) {
281 ret = -1;
282 goto end;
283 }
284
dac5c838 285 bt_value_freeze(obj);
83509119 286 BT_PUT(obj);
44e0a4f5
JG
287 }
288
289end:
44e0a4f5
JG
290 return ret;
291}
This page took 0.035839 seconds and 4 git commands to generate.