4 * Babeltrace CTF IR - Attributes
6 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
7 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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
28 #include <babeltrace/babeltrace-internal.h>
29 #include <babeltrace/objects.h>
31 #define BT_CTF_ATTR_NAME_INDEX 0
32 #define BT_CTF_ATTR_VALUE_INDEX 1
35 struct bt_object
*bt_ctf_attributes_create(void)
38 * Attributes: array object of array objects, each one
39 * containing two entries: a string object (attributes field
40 * name), and an object (attributes field value).
42 * Example (JSON representation):
45 * ["hostname", "eeppdesk"],
46 * ["sysname", "Linux"],
47 * ["tracer_major", 2],
51 return bt_object_array_create();
55 void bt_ctf_attributes_destroy(struct bt_object
*attr_obj
)
57 bt_object_put(attr_obj
);
61 int bt_ctf_attributes_get_count(struct bt_object
*attr_obj
)
63 return bt_object_array_size(attr_obj
);
67 const char *bt_ctf_attributes_get_field_name(struct bt_object
*attr_obj
,
71 const char *ret
= NULL
;
72 struct bt_object
*attr_field_obj
= NULL
;
73 struct bt_object
*attr_field_name_obj
= NULL
;
75 if (!attr_obj
|| index
< 0) {
79 attr_field_obj
= bt_object_array_get(attr_obj
, index
);
81 if (!attr_field_obj
) {
85 attr_field_name_obj
= bt_object_array_get(attr_field_obj
,
86 BT_CTF_ATTR_NAME_INDEX
);
88 if (!attr_field_name_obj
) {
92 rc
= bt_object_string_get(attr_field_name_obj
, &ret
);
99 BT_OBJECT_PUT(attr_field_name_obj
);
100 BT_OBJECT_PUT(attr_field_obj
);
106 struct bt_object
*bt_ctf_attributes_get_field_value(struct bt_object
*attr_obj
,
109 struct bt_object
*value_obj
= NULL
;
110 struct bt_object
*attr_field_obj
= NULL
;
112 if (!attr_obj
|| index
< 0) {
116 attr_field_obj
= bt_object_array_get(attr_obj
, index
);
118 if (!attr_field_obj
) {
122 value_obj
= bt_object_array_get(attr_field_obj
,
123 BT_CTF_ATTR_VALUE_INDEX
);
126 BT_OBJECT_PUT(attr_field_obj
);
132 struct bt_object
*bt_ctf_attributes_get_field_by_name(
133 struct bt_object
*attr_obj
, const char *name
)
137 struct bt_object
*value_obj
= NULL
;
138 struct bt_object
*attr_field_name_obj
= NULL
;
140 attr_size
= bt_object_array_size(attr_obj
);
146 for (i
= 0; i
< attr_size
; ++i
) {
148 const char *field_name
;
150 value_obj
= bt_object_array_get(attr_obj
, i
);
156 attr_field_name_obj
= bt_object_array_get(value_obj
, 0);
158 if (!attr_field_name_obj
) {
162 ret
= bt_object_string_get(attr_field_name_obj
, &field_name
);
167 if (!strcmp(field_name
, name
)) {
168 BT_OBJECT_PUT(attr_field_name_obj
);
172 BT_OBJECT_PUT(attr_field_name_obj
);
173 BT_OBJECT_PUT(value_obj
);
179 BT_OBJECT_PUT(attr_field_name_obj
);
180 BT_OBJECT_PUT(value_obj
);
186 int bt_ctf_attributes_set_field_value(struct bt_object
*attr_obj
,
187 const char *name
, struct bt_object
*value_obj
)
190 struct bt_object
*attr_field_obj
= NULL
;
192 if (!attr_obj
|| !name
|| !value_obj
) {
197 attr_field_obj
= bt_ctf_attributes_get_field_by_name(attr_obj
, name
);
199 if (attr_field_obj
) {
200 ret
= bt_object_array_set(attr_field_obj
,
201 BT_CTF_ATTR_VALUE_INDEX
, value_obj
);
205 attr_field_obj
= bt_object_array_create();
207 if (!attr_field_obj
) {
212 ret
= bt_object_array_append_string(attr_field_obj
, name
);
213 ret
|= bt_object_array_append(attr_field_obj
, value_obj
);
219 ret
= bt_object_array_append(attr_obj
, attr_field_obj
);
222 BT_OBJECT_PUT(attr_field_obj
);
228 struct bt_object
*bt_ctf_attributes_get_field_value_by_name(
229 struct bt_object
*attr_obj
, const char *name
)
231 struct bt_object
*value_obj
= NULL
;
232 struct bt_object
*attr_field_obj
= NULL
;
234 if (!attr_obj
|| !name
) {
238 attr_field_obj
= bt_ctf_attributes_get_field_by_name(attr_obj
, name
);
240 if (!attr_field_obj
) {
244 value_obj
= bt_object_array_get(attr_field_obj
,
245 BT_CTF_ATTR_VALUE_INDEX
);
248 BT_OBJECT_PUT(attr_field_obj
);
254 int bt_ctf_attributes_freeze(struct bt_object
*attr_obj
)
265 count
= bt_object_array_size(attr_obj
);
273 * We do not freeze the array itself here, since internal
274 * stuff could need to modify/add attributes. Each attribute
275 * is frozen one by one.
277 for (i
= 0; i
< count
; ++i
) {
278 struct bt_object
*obj
= NULL
;
280 obj
= bt_ctf_attributes_get_field_value(attr_obj
, i
);
287 bt_object_freeze(obj
);
This page took 0.043745 seconds and 4 git commands to generate.