Commit | Line | Data |
---|---|---|
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 | ||
29 | FILE *_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 | ||
37 | void _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 | 49 | struct 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 |
66 | struct 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 */ | |
73 | struct 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 | ||
90 | struct 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 */ | |
97 | struct 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 | ||
114 | struct 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 | |
120 | struct 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 | } | |
134 | end: | |
135 | return array; | |
136 | } | |
786207e0 XH |
137 | |
138 | struct definition_sequence *_bt_python_get_sequence_from_def( | |
139 | struct bt_definition *field) | |
140 | { | |
141 | if (field && bt_ctf_field_type( | |
142 | bt_ctf_get_decl_from_def(field)) == CTF_TYPE_SEQUENCE) { | |
143 | return container_of(field, struct definition_sequence, p); | |
144 | } | |
145 | ||
146 | return NULL; | |
147 | } | |
ec8c88d7 JG |
148 | |
149 | int _bt_python_field_integer_get_signedness(const struct bt_ctf_field *field) | |
150 | { | |
151 | int ret; | |
152 | ||
153 | if (!field || field->type->declaration->id != CTF_TYPE_INTEGER) { | |
154 | ret = -1; | |
155 | goto end; | |
156 | } | |
157 | ||
158 | const struct bt_ctf_field_type_integer *type = container_of(field->type, | |
159 | const struct bt_ctf_field_type_integer, parent); | |
160 | ret = type->declaration.signedness; | |
161 | end: | |
162 | return ret; | |
163 | } | |
164 | ||
165 | enum ctf_type_id _bt_python_get_field_type(const struct bt_ctf_field *field) | |
166 | { | |
167 | enum ctf_type_id type_id = CTF_TYPE_UNKNOWN; | |
168 | ||
169 | if (!field) { | |
170 | goto end; | |
171 | } | |
172 | ||
173 | type_id = field->type->declaration->id; | |
174 | end: | |
175 | return type_id; | |
176 | } |