Commit | Line | Data |
---|---|---|
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 | |
32 | #define BT_CTF_ATTR_VALUE_INDEX 1 | |
33 | ||
34 | BT_HIDDEN | |
dac5c838 | 35 | struct 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 | ||
54 | BT_HIDDEN | |
dac5c838 | 55 | void bt_ctf_attributes_destroy(struct bt_value *attr_obj) |
44e0a4f5 | 56 | { |
dac5c838 | 57 | bt_value_put(attr_obj); |
44e0a4f5 JG |
58 | } |
59 | ||
60 | BT_HIDDEN | |
dac5c838 | 61 | int 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 | ||
66 | BT_HIDDEN | |
dac5c838 | 67 | const 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 | ||
98 | end: | |
dac5c838 PP |
99 | BT_VALUE_PUT(attr_field_name_obj); |
100 | BT_VALUE_PUT(attr_field_obj); | |
44e0a4f5 JG |
101 | |
102 | return ret; | |
103 | } | |
104 | ||
105 | BT_HIDDEN | |
dac5c838 | 106 | struct bt_value *bt_ctf_attributes_get_field_value(struct bt_value *attr_obj, |
44e0a4f5 JG |
107 | int index) |
108 | { | |
dac5c838 PP |
109 | struct bt_value *value_obj = NULL; |
110 | struct bt_value *attr_field_obj = NULL; | |
44e0a4f5 JG |
111 | |
112 | if (!attr_obj || index < 0) { | |
113 | goto end; | |
114 | } | |
115 | ||
dac5c838 | 116 | attr_field_obj = bt_value_array_get(attr_obj, index); |
44e0a4f5 JG |
117 | |
118 | if (!attr_field_obj) { | |
119 | goto end; | |
120 | } | |
121 | ||
dac5c838 | 122 | value_obj = bt_value_array_get(attr_field_obj, |
44e0a4f5 JG |
123 | BT_CTF_ATTR_VALUE_INDEX); |
124 | ||
125 | end: | |
dac5c838 | 126 | BT_VALUE_PUT(attr_field_obj); |
44e0a4f5 JG |
127 | |
128 | return value_obj; | |
129 | } | |
130 | ||
131 | static | |
dac5c838 PP |
132 | struct bt_value *bt_ctf_attributes_get_field_by_name( |
133 | struct bt_value *attr_obj, const char *name) | |
44e0a4f5 JG |
134 | { |
135 | int i; | |
136 | int attr_size; | |
dac5c838 PP |
137 | struct bt_value *value_obj = NULL; |
138 | struct bt_value *attr_field_name_obj = NULL; | |
44e0a4f5 | 139 | |
dac5c838 | 140 | attr_size = bt_value_array_size(attr_obj); |
44e0a4f5 JG |
141 | |
142 | if (attr_size < 0) { | |
143 | goto error; | |
144 | } | |
145 | ||
146 | for (i = 0; i < attr_size; ++i) { | |
147 | int ret; | |
148 | const char *field_name; | |
149 | ||
dac5c838 | 150 | value_obj = bt_value_array_get(attr_obj, i); |
44e0a4f5 JG |
151 | |
152 | if (!value_obj) { | |
153 | goto error; | |
154 | } | |
155 | ||
dac5c838 | 156 | attr_field_name_obj = bt_value_array_get(value_obj, 0); |
44e0a4f5 JG |
157 | |
158 | if (!attr_field_name_obj) { | |
159 | goto error; | |
160 | } | |
161 | ||
dac5c838 | 162 | ret = bt_value_string_get(attr_field_name_obj, &field_name); |
44e0a4f5 JG |
163 | if (ret) { |
164 | goto error; | |
165 | } | |
166 | ||
167 | if (!strcmp(field_name, name)) { | |
dac5c838 | 168 | BT_VALUE_PUT(attr_field_name_obj); |
44e0a4f5 JG |
169 | break; |
170 | } | |
171 | ||
dac5c838 PP |
172 | BT_VALUE_PUT(attr_field_name_obj); |
173 | BT_VALUE_PUT(value_obj); | |
44e0a4f5 JG |
174 | } |
175 | ||
176 | return value_obj; | |
177 | ||
178 | error: | |
dac5c838 PP |
179 | BT_VALUE_PUT(attr_field_name_obj); |
180 | BT_VALUE_PUT(value_obj); | |
44e0a4f5 JG |
181 | |
182 | return value_obj; | |
183 | } | |
184 | ||
185 | BT_HIDDEN | |
dac5c838 PP |
186 | int bt_ctf_attributes_set_field_value(struct bt_value *attr_obj, |
187 | const char *name, struct bt_value *value_obj) | |
44e0a4f5 JG |
188 | { |
189 | int ret = 0; | |
dac5c838 | 190 | struct bt_value *attr_field_obj = NULL; |
44e0a4f5 JG |
191 | |
192 | if (!attr_obj || !name || !value_obj) { | |
193 | ret = -1; | |
194 | goto end; | |
195 | } | |
196 | ||
197 | attr_field_obj = bt_ctf_attributes_get_field_by_name(attr_obj, name); | |
198 | ||
199 | if (attr_field_obj) { | |
dac5c838 | 200 | ret = bt_value_array_set(attr_field_obj, |
44e0a4f5 JG |
201 | BT_CTF_ATTR_VALUE_INDEX, value_obj); |
202 | goto end; | |
203 | } | |
204 | ||
dac5c838 | 205 | attr_field_obj = bt_value_array_create(); |
44e0a4f5 JG |
206 | |
207 | if (!attr_field_obj) { | |
208 | ret = -1; | |
209 | goto end; | |
210 | } | |
211 | ||
dac5c838 PP |
212 | ret = bt_value_array_append_string(attr_field_obj, name); |
213 | ret |= bt_value_array_append(attr_field_obj, value_obj); | |
44e0a4f5 JG |
214 | |
215 | if (ret) { | |
216 | goto end; | |
217 | } | |
218 | ||
dac5c838 | 219 | ret = bt_value_array_append(attr_obj, attr_field_obj); |
44e0a4f5 JG |
220 | |
221 | end: | |
dac5c838 | 222 | BT_VALUE_PUT(attr_field_obj); |
44e0a4f5 JG |
223 | |
224 | return ret; | |
225 | } | |
226 | ||
227 | BT_HIDDEN | |
dac5c838 PP |
228 | struct bt_value *bt_ctf_attributes_get_field_value_by_name( |
229 | struct bt_value *attr_obj, const char *name) | |
44e0a4f5 | 230 | { |
dac5c838 PP |
231 | struct bt_value *value_obj = NULL; |
232 | struct bt_value *attr_field_obj = NULL; | |
44e0a4f5 JG |
233 | |
234 | if (!attr_obj || !name) { | |
235 | goto end; | |
236 | } | |
237 | ||
238 | attr_field_obj = bt_ctf_attributes_get_field_by_name(attr_obj, name); | |
239 | ||
240 | if (!attr_field_obj) { | |
241 | goto end; | |
242 | } | |
243 | ||
dac5c838 | 244 | value_obj = bt_value_array_get(attr_field_obj, |
44e0a4f5 JG |
245 | BT_CTF_ATTR_VALUE_INDEX); |
246 | ||
247 | end: | |
dac5c838 | 248 | BT_VALUE_PUT(attr_field_obj); |
44e0a4f5 JG |
249 | |
250 | return value_obj; | |
251 | } | |
252 | ||
253 | BT_HIDDEN | |
dac5c838 | 254 | int bt_ctf_attributes_freeze(struct bt_value *attr_obj) |
44e0a4f5 JG |
255 | { |
256 | int i; | |
257 | int count; | |
258 | int ret = 0; | |
259 | ||
260 | if (!attr_obj) { | |
261 | ret = -1; | |
262 | goto end; | |
263 | } | |
264 | ||
dac5c838 | 265 | count = bt_value_array_size(attr_obj); |
44e0a4f5 JG |
266 | |
267 | if (count < 0) { | |
268 | ret = -1; | |
269 | goto end; | |
270 | } | |
271 | ||
272 | /* | |
dac5c838 PP |
273 | * We do not freeze the array value object itself here, since |
274 | * internal stuff could need to modify/add attributes. Each | |
275 | * attribute is frozen one by one. | |
44e0a4f5 JG |
276 | */ |
277 | for (i = 0; i < count; ++i) { | |
dac5c838 | 278 | struct bt_value *obj = NULL; |
44e0a4f5 JG |
279 | |
280 | obj = bt_ctf_attributes_get_field_value(attr_obj, i); | |
281 | ||
282 | if (!obj) { | |
283 | ret = -1; | |
284 | goto end; | |
285 | } | |
286 | ||
dac5c838 PP |
287 | bt_value_freeze(obj); |
288 | BT_VALUE_PUT(obj); | |
44e0a4f5 JG |
289 | } |
290 | ||
291 | end: | |
292 | ||
293 | return ret; | |
294 | } |