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/values.h>
31 #define BT_CTF_ATTR_NAME_INDEX 0
32 #define BT_CTF_ATTR_VALUE_INDEX 1
35 struct bt_value
*bt_ctf_attributes_create(void)
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).
42 * Example (JSON representation):
45 * ["hostname", "eeppdesk"],
46 * ["sysname", "Linux"],
47 * ["tracer_major", 2],
51 return bt_value_array_create();
55 void bt_ctf_attributes_destroy(struct bt_value
*attr_obj
)
61 int bt_ctf_attributes_get_count(struct bt_value
*attr_obj
)
63 return bt_value_array_size(attr_obj
);
67 const char *bt_ctf_attributes_get_field_name(struct bt_value
*attr_obj
,
71 const char *ret
= NULL
;
72 struct bt_value
*attr_field_obj
= NULL
;
73 struct bt_value
*attr_field_name_obj
= NULL
;
75 if (!attr_obj
|| index
< 0) {
79 attr_field_obj
= bt_value_array_get(attr_obj
, index
);
81 if (!attr_field_obj
) {
85 attr_field_name_obj
= bt_value_array_get(attr_field_obj
,
86 BT_CTF_ATTR_NAME_INDEX
);
88 if (!attr_field_name_obj
) {
92 rc
= bt_value_string_get(attr_field_name_obj
, &ret
);
99 BT_PUT(attr_field_name_obj
);
100 BT_PUT(attr_field_obj
);
105 struct bt_value
*bt_ctf_attributes_get_field_value(struct bt_value
*attr_obj
,
108 struct bt_value
*value_obj
= NULL
;
109 struct bt_value
*attr_field_obj
= NULL
;
111 if (!attr_obj
|| index
< 0) {
115 attr_field_obj
= bt_value_array_get(attr_obj
, index
);
117 if (!attr_field_obj
) {
121 value_obj
= bt_value_array_get(attr_field_obj
,
122 BT_CTF_ATTR_VALUE_INDEX
);
125 BT_PUT(attr_field_obj
);
130 struct bt_value
*bt_ctf_attributes_get_field_by_name(
131 struct bt_value
*attr_obj
, const char *name
)
135 struct bt_value
*value_obj
= NULL
;
136 struct bt_value
*attr_field_name_obj
= NULL
;
138 attr_size
= bt_value_array_size(attr_obj
);
144 for (i
= 0; i
< attr_size
; ++i
) {
146 const char *field_name
;
148 value_obj
= bt_value_array_get(attr_obj
, i
);
154 attr_field_name_obj
= bt_value_array_get(value_obj
, 0);
156 if (!attr_field_name_obj
) {
160 ret
= bt_value_string_get(attr_field_name_obj
, &field_name
);
165 if (!strcmp(field_name
, name
)) {
166 BT_PUT(attr_field_name_obj
);
170 BT_PUT(attr_field_name_obj
);
177 BT_PUT(attr_field_name_obj
);
184 int bt_ctf_attributes_set_field_value(struct bt_value
*attr_obj
,
185 const char *name
, struct bt_value
*value_obj
)
188 struct bt_value
*attr_field_obj
= NULL
;
190 if (!attr_obj
|| !name
|| !value_obj
) {
195 attr_field_obj
= bt_ctf_attributes_get_field_by_name(attr_obj
, name
);
197 if (attr_field_obj
) {
198 ret
= bt_value_array_set(attr_field_obj
,
199 BT_CTF_ATTR_VALUE_INDEX
, value_obj
);
203 attr_field_obj
= bt_value_array_create();
205 if (!attr_field_obj
) {
210 ret
= bt_value_array_append_string(attr_field_obj
, name
);
211 ret
|= bt_value_array_append(attr_field_obj
, value_obj
);
217 ret
= bt_value_array_append(attr_obj
, attr_field_obj
);
220 BT_PUT(attr_field_obj
);
226 struct bt_value
*bt_ctf_attributes_get_field_value_by_name(
227 struct bt_value
*attr_obj
, const char *name
)
229 struct bt_value
*value_obj
= NULL
;
230 struct bt_value
*attr_field_obj
= NULL
;
232 if (!attr_obj
|| !name
) {
236 attr_field_obj
= bt_ctf_attributes_get_field_by_name(attr_obj
, name
);
238 if (!attr_field_obj
) {
242 value_obj
= bt_value_array_get(attr_field_obj
,
243 BT_CTF_ATTR_VALUE_INDEX
);
246 BT_PUT(attr_field_obj
);
252 int bt_ctf_attributes_freeze(struct bt_value
*attr_obj
)
263 count
= bt_value_array_size(attr_obj
);
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.
275 for (i
= 0; i
< count
; ++i
) {
276 struct bt_value
*obj
= NULL
;
278 obj
= bt_ctf_attributes_get_field_value(attr_obj
, i
);
285 bt_value_freeze(obj
);
This page took 0.035653 seconds and 4 git commands to generate.