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