Standardize `strcmp(a, b) == 0` instead of `!strcmp(a, b)`
[babeltrace.git] / src / ctf-writer / attributes.c
1 /*
2 * attributes.c
3 *
4 * Babeltrace CTF writer - 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 #define BT_LOG_TAG "CTF-WRITER/ATTRS"
29 #include "logging.h"
30
31 #include "common/assert.h"
32 #include "common/macros.h"
33 #include "compat/string.h"
34 #include <babeltrace2-ctf-writer/object.h>
35 #include <inttypes.h>
36
37 #include "values.h"
38
39 #define BT_CTF_ATTR_NAME_INDEX 0
40 #define BT_CTF_ATTR_VALUE_INDEX 1
41
42 BT_HIDDEN
43 struct bt_ctf_private_value *bt_ctf_attributes_create(void)
44 {
45 struct bt_ctf_private_value *attr_obj;
46
47 /*
48 * Attributes: array value object of array value objects, each one
49 * containing two entries: a string value object (attributes
50 * field name), and a value object (attributes field value).
51 *
52 * Example (JSON representation):
53 *
54 * [
55 * ["hostname", "eeppdesk"],
56 * ["sysname", "Linux"],
57 * ["tracer_major", 2],
58 * ["tracer_minor", 5]
59 * ]
60 */
61 BT_LOGD_STR("Creating attributes object.");
62 attr_obj = bt_ctf_private_value_array_create();
63 if (!attr_obj) {
64 BT_LOGE_STR("Failed to create array value.");
65 } else {
66 BT_LOGD("Created attributes object: addr=%p",
67 attr_obj);
68 }
69
70 return attr_obj;
71 }
72
73 BT_HIDDEN
74 void bt_ctf_attributes_destroy(struct bt_ctf_private_value *attr_obj)
75 {
76 BT_LOGD("Destroying attributes object: addr=%p", attr_obj);
77 bt_ctf_object_put_ref(attr_obj);
78 }
79
80 BT_HIDDEN
81 int64_t bt_ctf_attributes_get_count(struct bt_ctf_private_value *attr_obj)
82 {
83 return bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj));
84 }
85
86 BT_HIDDEN
87 const char *bt_ctf_attributes_get_field_name(struct bt_ctf_private_value *attr_obj,
88 uint64_t index)
89 {
90 const char *ret = NULL;
91 struct bt_ctf_private_value *attr_field_obj = NULL;
92 struct bt_ctf_private_value *attr_field_name_obj = NULL;
93
94 if (!attr_obj) {
95 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
96 goto end;
97 }
98
99 if (index >= bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj))) {
100 BT_LOGW("Invalid parameter: index is out of bounds: "
101 "index=%" PRIu64 ", count=%" PRId64,
102 index, bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj)));
103 goto end;
104 }
105
106 attr_field_obj = bt_ctf_private_value_array_borrow_element_by_index(
107 attr_obj, index);
108 if (!attr_field_obj) {
109 BT_LOGE("Cannot get attributes object's array value's element by index: "
110 "value-addr=%p, index=%" PRIu64, attr_obj, index);
111 goto end;
112 }
113
114 attr_field_name_obj =
115 bt_ctf_private_value_array_borrow_element_by_index(
116 attr_field_obj, BT_CTF_ATTR_NAME_INDEX);
117 if (!attr_field_name_obj) {
118 BT_LOGE("Cannot get attribute array value's element by index: "
119 "value-addr=%p, index=%" PRIu64, attr_field_obj,
120 (uint64_t) BT_CTF_ATTR_NAME_INDEX);
121 goto end;
122 }
123
124 ret = bt_ctf_value_string_get(
125 bt_ctf_private_value_as_value(attr_field_name_obj));
126
127 end:
128 return ret;
129 }
130
131 BT_HIDDEN
132 struct bt_ctf_private_value *bt_ctf_attributes_borrow_field_value(struct bt_ctf_private_value *attr_obj,
133 uint64_t index)
134 {
135 struct bt_ctf_private_value *value_obj = NULL;
136 struct bt_ctf_private_value *attr_field_obj = NULL;
137
138 if (!attr_obj) {
139 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
140 goto end;
141 }
142
143 if (index >= bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj))) {
144 BT_LOGW("Invalid parameter: index is out of bounds: "
145 "index=%" PRIu64 ", count=%" PRId64,
146 index, bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj)));
147 goto end;
148 }
149
150 attr_field_obj = bt_ctf_private_value_array_borrow_element_by_index(
151 attr_obj, index);
152 if (!attr_field_obj) {
153 BT_LOGE("Cannot get attributes object's array value's element by index: "
154 "value-addr=%p, index=%" PRIu64, attr_obj, index);
155 goto end;
156 }
157
158 value_obj = bt_ctf_private_value_array_borrow_element_by_index(attr_field_obj,
159 BT_CTF_ATTR_VALUE_INDEX);
160 if (!value_obj) {
161 BT_LOGE("Cannot get attribute array value's element by index: "
162 "value-addr=%p, index=%" PRIu64, attr_field_obj,
163 (uint64_t) BT_CTF_ATTR_VALUE_INDEX);
164 }
165
166 end:
167 return value_obj;
168 }
169
170 static
171 struct bt_ctf_private_value *bt_ctf_attributes_borrow_field_by_name(
172 struct bt_ctf_private_value *attr_obj, const char *name)
173 {
174 uint64_t i;
175 int64_t attr_size;
176 struct bt_ctf_private_value *value_obj = NULL;
177 struct bt_ctf_private_value *attr_field_name_obj = NULL;
178
179 attr_size = bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj));
180 if (attr_size < 0) {
181 BT_LOGE("Cannot get array value's size: value-addr=%p",
182 attr_obj);
183 goto error;
184 }
185
186 for (i = 0; i < attr_size; ++i) {
187 const char *field_name;
188
189 value_obj = bt_ctf_private_value_array_borrow_element_by_index(attr_obj, i);
190 if (!value_obj) {
191 BT_LOGE("Cannot get attributes object's array value's element by index: "
192 "value-addr=%p, index=%" PRIu64, attr_obj, i);
193 goto error;
194 }
195
196 attr_field_name_obj = bt_ctf_private_value_array_borrow_element_by_index(value_obj,
197 BT_CTF_ATTR_NAME_INDEX);
198 if (!attr_field_name_obj) {
199 BT_LOGE("Cannot get attribute array value's element by index: "
200 "value-addr=%p, index=%" PRIu64,
201 value_obj, (int64_t) BT_CTF_ATTR_NAME_INDEX);
202 goto error;
203 }
204
205 field_name = bt_ctf_value_string_get(
206 bt_ctf_private_value_as_value(attr_field_name_obj));
207
208 if (strcmp(field_name, name) == 0) {
209 break;
210 }
211
212 value_obj = NULL;
213 }
214
215 return value_obj;
216
217 error:
218 value_obj = NULL;
219 return value_obj;
220 }
221
222 BT_HIDDEN
223 int bt_ctf_attributes_set_field_value(struct bt_ctf_private_value *attr_obj,
224 const char *name, struct bt_ctf_private_value *value_obj)
225 {
226 int ret = 0;
227 struct bt_ctf_private_value *attr_field_obj = NULL;
228
229 if (!attr_obj || !name || !value_obj) {
230 BT_LOGW("Invalid parameter: attributes object, name, or value object is NULL: "
231 "attr-value-addr=%p, name-addr=%p, value-addr=%p",
232 attr_obj, name, value_obj);
233 ret = -1;
234 goto end;
235 }
236
237 attr_field_obj = bt_ctf_attributes_borrow_field_by_name(attr_obj, name);
238 if (attr_field_obj) {
239 ret = bt_ctf_private_value_array_set_element_by_index(
240 attr_field_obj, BT_CTF_ATTR_VALUE_INDEX,
241 bt_ctf_private_value_as_value(value_obj));
242 attr_field_obj = NULL;
243 goto end;
244 }
245
246 attr_field_obj = bt_ctf_private_value_array_create();
247 if (!attr_field_obj) {
248 BT_LOGE_STR("Failed to create empty array value.");
249 ret = -1;
250 goto end;
251 }
252
253 ret = bt_ctf_private_value_array_append_string_element(attr_field_obj, name);
254 ret |= bt_ctf_private_value_array_append_element(attr_field_obj,
255 bt_ctf_private_value_as_value(value_obj));
256 if (ret) {
257 BT_LOGE("Cannot append elements to array value: addr=%p",
258 attr_field_obj);
259 goto end;
260 }
261
262 ret = bt_ctf_private_value_array_append_element(attr_obj,
263 bt_ctf_private_value_as_value(attr_field_obj));
264 if (ret) {
265 BT_LOGE("Cannot append element to array value: "
266 "array-value-addr=%p, element-value-addr=%p",
267 attr_obj, attr_field_obj);
268 }
269
270 end:
271 bt_ctf_object_put_ref(attr_field_obj);
272 return ret;
273 }
274
275 BT_HIDDEN
276 struct bt_ctf_private_value *bt_ctf_attributes_borrow_field_value_by_name(
277 struct bt_ctf_private_value *attr_obj, const char *name)
278 {
279 struct bt_ctf_private_value *value_obj = NULL;
280 struct bt_ctf_private_value *attr_field_obj = NULL;
281
282 if (!attr_obj || !name) {
283 BT_LOGW("Invalid parameter: attributes object or name is NULL: "
284 "value-addr=%p, name-addr=%p", attr_obj, name);
285 goto end;
286 }
287
288 attr_field_obj = bt_ctf_attributes_borrow_field_by_name(attr_obj, name);
289 if (!attr_field_obj) {
290 BT_LOGD("Cannot find attributes object's field by name: "
291 "value-addr=%p, name=\"%s\"", attr_obj, name);
292 goto end;
293 }
294
295 value_obj = bt_ctf_private_value_array_borrow_element_by_index(attr_field_obj,
296 BT_CTF_ATTR_VALUE_INDEX);
297 if (!value_obj) {
298 BT_LOGE("Cannot get attribute array value's element by index: "
299 "value-addr=%p, index=%" PRIu64, attr_field_obj,
300 (uint64_t) BT_CTF_ATTR_VALUE_INDEX);
301 }
302
303 end:
304 return value_obj;
305 }
306
307 BT_HIDDEN
308 int bt_ctf_attributes_freeze(struct bt_ctf_private_value *attr_obj)
309 {
310 uint64_t i;
311 int64_t count;
312 int ret = 0;
313
314 if (!attr_obj) {
315 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
316 ret = -1;
317 goto end;
318 }
319
320 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj);
321 count = bt_ctf_value_array_get_size(bt_ctf_private_value_as_value(attr_obj));
322 BT_ASSERT(count >= 0);
323
324 /*
325 * We do not freeze the array value object itself here, since
326 * internal stuff could need to modify/add attributes. Each
327 * attribute is frozen one by one.
328 */
329 for (i = 0; i < count; ++i) {
330 struct bt_ctf_private_value *obj = NULL;
331
332 obj = bt_ctf_attributes_borrow_field_value(attr_obj, i);
333 if (!obj) {
334 BT_LOGE("Cannot get attributes object's field value by index: "
335 "value-addr=%p, index=%" PRIu64,
336 attr_obj, i);
337 ret = -1;
338 goto end;
339 }
340
341 bt_ctf_value_freeze(bt_ctf_private_value_as_value(obj));
342 }
343
344 end:
345 return ret;
346 }
This page took 0.047335 seconds and 4 git commands to generate.