Fix (Python bindings): Return a string if a sequence's element are chars
[babeltrace.git] / bindings / python / python-complements.c
CommitLineData
24a3136a
DS
1/*
2 * python-complements.c
3 *
4 * Babeltrace Python module complements, required for Python bindings
5 *
6 * Copyright 2012 EfficiOS Inc.
7 *
8 * Author: Danny Serres <danny.serres@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 */
20
21#include "python-complements.h"
ec8c88d7
JG
22#include <babeltrace/ctf-writer/event-types-internal.h>
23#include <babeltrace/ctf-writer/event-fields-internal.h>
24a3136a
DS
24
25/* FILE functions
26 ----------------------------------------------------
27*/
28
29FILE *_bt_file_open(char *file_path, char *mode)
30{
31 FILE *fp = stdout;
32 if (file_path != NULL)
33 fp = fopen(file_path, mode);
34 return fp;
35}
36
37void _bt_file_close(FILE *fp)
38{
39 if (fp != NULL)
40 fclose(fp);
41}
42
43
44/* List-related functions
45 ----------------------------------------------------
46*/
47
48/* ctf-field-list */
2c0df204 49struct bt_definition **_bt_python_field_listcaller(
24a3136a 50 const struct bt_ctf_event *ctf_event,
cebae8c3
JG
51 const struct bt_definition *scope,
52 unsigned int *len)
24a3136a 53{
2c0df204 54 struct bt_definition **list;
24a3136a
DS
55 int ret;
56
57 ret = bt_ctf_get_field_list(ctf_event, scope,
cebae8c3 58 (const struct bt_definition * const **)&list, len);
24a3136a
DS
59
60 if (ret < 0) /* For python to know an error occured */
61 list = NULL;
24a3136a
DS
62
63 return list;
64}
65
2c0df204
XH
66struct bt_definition *_bt_python_field_one_from_list(
67 struct bt_definition **list, int index)
24a3136a
DS
68{
69 return list[index];
70}
71
72/* event_decl_list */
73struct bt_ctf_event_decl **_bt_python_event_decl_listcaller(
cebae8c3
JG
74 int handle_id,
75 struct bt_context *ctx,
76 unsigned int *len)
24a3136a
DS
77{
78 struct bt_ctf_event_decl **list;
24a3136a
DS
79 int ret;
80
81 ret = bt_ctf_get_event_decl_list(handle_id, ctx,
cebae8c3 82 (struct bt_ctf_event_decl * const **)&list, len);
24a3136a
DS
83
84 if (ret < 0) /* For python to know an error occured */
85 list = NULL;
24a3136a
DS
86
87 return list;
88}
89
90struct bt_ctf_event_decl *_bt_python_decl_one_from_list(
91 struct bt_ctf_event_decl **list, int index)
92{
93 return list[index];
94}
95
96/* decl_fields */
97struct bt_ctf_field_decl **_by_python_field_decl_listcaller(
98 struct bt_ctf_event_decl *event_decl,
cebae8c3
JG
99 enum bt_ctf_scope scope,
100 unsigned int *len)
24a3136a
DS
101{
102 struct bt_ctf_field_decl **list;
24a3136a
DS
103 int ret;
104
105 ret = bt_ctf_get_decl_fields(event_decl, scope,
cebae8c3 106 (const struct bt_ctf_field_decl * const **)&list, len);
24a3136a
DS
107
108 if (ret < 0) /* For python to know an error occured */
109 list = NULL;
24a3136a
DS
110
111 return list;
112}
113
114struct bt_ctf_field_decl *_bt_python_field_decl_one_from_list(
115 struct bt_ctf_field_decl **list, int index)
116{
117 return list[index];
118}
3c2ce778
XH
119
120struct definition_array *_bt_python_get_array_from_def(
121 struct bt_definition *field)
122{
123 const struct bt_declaration *array_decl;
124 struct definition_array *array = NULL;
125
126 if (!field) {
127 goto end;
128 }
129
130 array_decl = bt_ctf_get_decl_from_def(field);
131 if (bt_ctf_field_type(array_decl) == CTF_TYPE_ARRAY) {
132 array = container_of(field, struct definition_array, p);
133 }
134end:
135 return array;
136}
786207e0 137
5792eb34
JG
138struct bt_declaration *_bt_python_get_array_element_declaration(
139 struct bt_declaration *field)
140{
141 struct declaration_array *array_decl;
142 struct bt_declaration *ret = NULL;
143
144 if (!field) {
145 goto end;
146 }
147
148 array_decl = container_of(field, struct declaration_array, p);
149 ret = array_decl->elem;
150end:
151 return ret;
152}
153
e11b8d3a
JG
154struct bt_declaration *_bt_python_get_sequence_element_declaration(
155 struct bt_declaration *field)
156{
157 struct declaration_sequence *sequence_decl;
158 struct bt_declaration *ret = NULL;
159
160 if (!field) {
161 goto end;
162 }
163
164 sequence_decl = container_of(field, struct declaration_sequence, p);
165 ret = sequence_decl->elem;
166end:
167 return ret;
168}
169
5792eb34
JG
170const char *_bt_python_get_array_string(struct bt_definition *field)
171{
172 struct definition_array *array;
173 const char *ret = NULL;
174
175 if (!field) {
176 goto end;
177 }
178
179 array = container_of(field, struct definition_array, p);
180 ret = array->string->str;
181end:
182 return ret;
183}
184
e11b8d3a
JG
185const char *_bt_python_get_sequence_string(struct bt_definition *field)
186{
187 struct definition_sequence *sequence;
188 const char *ret = NULL;
189
190 if (!field) {
191 goto end;
192 }
193
194 sequence = container_of(field, struct definition_sequence, p);
195 ret = sequence->string->str;
196end:
197 return ret;
198}
199
786207e0
XH
200struct definition_sequence *_bt_python_get_sequence_from_def(
201 struct bt_definition *field)
202{
203 if (field && bt_ctf_field_type(
204 bt_ctf_get_decl_from_def(field)) == CTF_TYPE_SEQUENCE) {
205 return container_of(field, struct definition_sequence, p);
206 }
207
208 return NULL;
209}
ec8c88d7
JG
210
211int _bt_python_field_integer_get_signedness(const struct bt_ctf_field *field)
212{
213 int ret;
214
215 if (!field || field->type->declaration->id != CTF_TYPE_INTEGER) {
216 ret = -1;
217 goto end;
218 }
219
220 const struct bt_ctf_field_type_integer *type = container_of(field->type,
221 const struct bt_ctf_field_type_integer, parent);
222 ret = type->declaration.signedness;
223end:
224 return ret;
225}
226
227enum ctf_type_id _bt_python_get_field_type(const struct bt_ctf_field *field)
228{
229 enum ctf_type_id type_id = CTF_TYPE_UNKNOWN;
230
231 if (!field) {
232 goto end;
233 }
234
235 type_id = field->type->declaration->id;
236end:
237 return type_id;
238}
This page took 0.031203 seconds and 4 git commands to generate.