Python bindings: return char arrays as strings in value()
[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
154const char *_bt_python_get_array_string(struct bt_definition *field)
155{
156 struct definition_array *array;
157 const char *ret = NULL;
158
159 if (!field) {
160 goto end;
161 }
162
163 array = container_of(field, struct definition_array, p);
164 ret = array->string->str;
165end:
166 return ret;
167}
168
786207e0
XH
169struct definition_sequence *_bt_python_get_sequence_from_def(
170 struct bt_definition *field)
171{
172 if (field && bt_ctf_field_type(
173 bt_ctf_get_decl_from_def(field)) == CTF_TYPE_SEQUENCE) {
174 return container_of(field, struct definition_sequence, p);
175 }
176
177 return NULL;
178}
ec8c88d7
JG
179
180int _bt_python_field_integer_get_signedness(const struct bt_ctf_field *field)
181{
182 int ret;
183
184 if (!field || field->type->declaration->id != CTF_TYPE_INTEGER) {
185 ret = -1;
186 goto end;
187 }
188
189 const struct bt_ctf_field_type_integer *type = container_of(field->type,
190 const struct bt_ctf_field_type_integer, parent);
191 ret = type->declaration.signedness;
192end:
193 return ret;
194}
195
196enum ctf_type_id _bt_python_get_field_type(const struct bt_ctf_field *field)
197{
198 enum ctf_type_id type_id = CTF_TYPE_UNKNOWN;
199
200 if (!field) {
201 goto end;
202 }
203
204 type_id = field->type->declaration->id;
205end:
206 return type_id;
207}
This page took 0.032003 seconds and 4 git commands to generate.