File: python-complements.c

package info (click to toggle)
babeltrace 1.2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,636 kB
  • ctags: 3,016
  • sloc: ansic: 24,798; sh: 11,915; yacc: 2,318; makefile: 377; lex: 142
file content (238 lines) | stat: -rw-r--r-- 5,344 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * python-complements.c
 *
 * Babeltrace Python module complements, required for Python bindings
 *
 * Copyright 2012 EfficiOS Inc.
 *
 * Author: Danny Serres <danny.serres@efficios.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */

#include "python-complements.h"
#include <babeltrace/ctf-writer/event-types-internal.h>
#include <babeltrace/ctf-writer/event-fields-internal.h>

/* FILE functions
   ----------------------------------------------------
*/

FILE *_bt_file_open(char *file_path, char *mode)
{
	FILE *fp = stdout;
	if (file_path != NULL)
		fp = fopen(file_path, mode);
	return fp;
}

void _bt_file_close(FILE *fp)
{
	if (fp != NULL)
		fclose(fp);
}


/* List-related functions
   ----------------------------------------------------
*/

/* ctf-field-list */
struct bt_definition **_bt_python_field_listcaller(
		const struct bt_ctf_event *ctf_event,
		const struct bt_definition *scope,
		unsigned int *len)
{
	struct bt_definition **list;
	int ret;

	ret = bt_ctf_get_field_list(ctf_event, scope,
		(const struct bt_definition * const **)&list, len);

	if (ret < 0)	/* For python to know an error occured */
		list = NULL;

	return list;
}

struct bt_definition *_bt_python_field_one_from_list(
		struct bt_definition **list, int index)
{
	return list[index];
}

/* event_decl_list */
struct bt_ctf_event_decl **_bt_python_event_decl_listcaller(
		int handle_id,
		struct bt_context *ctx,
		unsigned int *len)
{
	struct bt_ctf_event_decl **list;
	int ret;

	ret = bt_ctf_get_event_decl_list(handle_id, ctx,
		(struct bt_ctf_event_decl * const **)&list, len);

	if (ret < 0)	/* For python to know an error occured */
		list = NULL;

	return list;
}

struct bt_ctf_event_decl *_bt_python_decl_one_from_list(
		struct bt_ctf_event_decl **list, int index)
{
	return list[index];
}

/* decl_fields */
struct bt_ctf_field_decl **_by_python_field_decl_listcaller(
		struct bt_ctf_event_decl *event_decl,
		enum bt_ctf_scope scope,
		unsigned int *len)
{
	struct bt_ctf_field_decl **list;
	int ret;

	ret = bt_ctf_get_decl_fields(event_decl, scope,
		(const struct bt_ctf_field_decl * const **)&list, len);

	if (ret < 0)	/* For python to know an error occured */
		list = NULL;

	return list;
}

struct bt_ctf_field_decl *_bt_python_field_decl_one_from_list(
		struct bt_ctf_field_decl **list, int index)
{
	return list[index];
}

struct definition_array *_bt_python_get_array_from_def(
		struct bt_definition *field)
{
	const struct bt_declaration *array_decl;
	struct definition_array *array = NULL;

	if (!field) {
		goto end;
	}

	array_decl = bt_ctf_get_decl_from_def(field);
	if (bt_ctf_field_type(array_decl) == CTF_TYPE_ARRAY) {
		array = container_of(field, struct definition_array, p);
	}
end:
	return array;
}

struct bt_declaration *_bt_python_get_array_element_declaration(
		struct bt_declaration *field)
{
	struct declaration_array *array_decl;
	struct bt_declaration *ret = NULL;

	if (!field) {
		goto end;
	}

	array_decl = container_of(field, struct declaration_array, p);
	ret = array_decl->elem;
end:
	return ret;
}

struct bt_declaration *_bt_python_get_sequence_element_declaration(
		struct bt_declaration *field)
{
	struct declaration_sequence *sequence_decl;
	struct bt_declaration *ret = NULL;

	if (!field) {
		goto end;
	}

	sequence_decl = container_of(field, struct declaration_sequence, p);
	ret = sequence_decl->elem;
end:
	return ret;
}

const char *_bt_python_get_array_string(struct bt_definition *field)
{
	struct definition_array *array;
	const char *ret = NULL;

	if (!field) {
		goto end;
	}

	array = container_of(field, struct definition_array, p);
	ret = array->string->str;
end:
	return ret;
}

const char *_bt_python_get_sequence_string(struct bt_definition *field)
{
	struct definition_sequence *sequence;
	const char *ret = NULL;

	if (!field) {
		goto end;
	}

	sequence = container_of(field, struct definition_sequence, p);
	ret = sequence->string->str;
end:
	return ret;
}

struct definition_sequence *_bt_python_get_sequence_from_def(
		struct bt_definition *field)
{
	if (field && bt_ctf_field_type(
		bt_ctf_get_decl_from_def(field)) == CTF_TYPE_SEQUENCE) {
		return container_of(field, struct definition_sequence, p);
	}

	return NULL;
}

int _bt_python_field_integer_get_signedness(const struct bt_ctf_field *field)
{
	int ret;

	if (!field || field->type->declaration->id != CTF_TYPE_INTEGER) {
		ret = -1;
		goto end;
	}

	const struct bt_ctf_field_type_integer *type = container_of(field->type,
		const struct bt_ctf_field_type_integer, parent);
	ret = type->declaration.signedness;
end:
	return ret;
}

enum ctf_type_id _bt_python_get_field_type(const struct bt_ctf_field *field)
{
	enum ctf_type_id type_id = CTF_TYPE_UNKNOWN;

	if (!field) {
		goto end;
	}

	type_id = field->type->declaration->id;
end:
	return type_id;
}