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" | |
22 | ||
23 | /* FILE functions | |
24 | ---------------------------------------------------- | |
25 | */ | |
26 | ||
27 | FILE *_bt_file_open(char *file_path, char *mode) | |
28 | { | |
29 | FILE *fp = stdout; | |
30 | if (file_path != NULL) | |
31 | fp = fopen(file_path, mode); | |
32 | return fp; | |
33 | } | |
34 | ||
35 | void _bt_file_close(FILE *fp) | |
36 | { | |
37 | if (fp != NULL) | |
38 | fclose(fp); | |
39 | } | |
40 | ||
41 | ||
42 | /* List-related functions | |
43 | ---------------------------------------------------- | |
44 | */ | |
45 | ||
46 | /* ctf-field-list */ | |
2c0df204 | 47 | struct bt_definition **_bt_python_field_listcaller( |
24a3136a | 48 | const struct bt_ctf_event *ctf_event, |
cebae8c3 JG |
49 | const struct bt_definition *scope, |
50 | unsigned int *len) | |
24a3136a | 51 | { |
2c0df204 | 52 | struct bt_definition **list; |
24a3136a DS |
53 | int ret; |
54 | ||
55 | ret = bt_ctf_get_field_list(ctf_event, scope, | |
cebae8c3 | 56 | (const struct bt_definition * const **)&list, len); |
24a3136a DS |
57 | |
58 | if (ret < 0) /* For python to know an error occured */ | |
59 | list = NULL; | |
24a3136a DS |
60 | |
61 | return list; | |
62 | } | |
63 | ||
2c0df204 XH |
64 | struct bt_definition *_bt_python_field_one_from_list( |
65 | struct bt_definition **list, int index) | |
24a3136a DS |
66 | { |
67 | return list[index]; | |
68 | } | |
69 | ||
70 | /* event_decl_list */ | |
71 | struct bt_ctf_event_decl **_bt_python_event_decl_listcaller( | |
cebae8c3 JG |
72 | int handle_id, |
73 | struct bt_context *ctx, | |
74 | unsigned int *len) | |
24a3136a DS |
75 | { |
76 | struct bt_ctf_event_decl **list; | |
24a3136a DS |
77 | int ret; |
78 | ||
79 | ret = bt_ctf_get_event_decl_list(handle_id, ctx, | |
cebae8c3 | 80 | (struct bt_ctf_event_decl * const **)&list, len); |
24a3136a DS |
81 | |
82 | if (ret < 0) /* For python to know an error occured */ | |
83 | list = NULL; | |
24a3136a DS |
84 | |
85 | return list; | |
86 | } | |
87 | ||
88 | struct bt_ctf_event_decl *_bt_python_decl_one_from_list( | |
89 | struct bt_ctf_event_decl **list, int index) | |
90 | { | |
91 | return list[index]; | |
92 | } | |
93 | ||
94 | /* decl_fields */ | |
95 | struct bt_ctf_field_decl **_by_python_field_decl_listcaller( | |
96 | struct bt_ctf_event_decl *event_decl, | |
cebae8c3 JG |
97 | enum bt_ctf_scope scope, |
98 | unsigned int *len) | |
24a3136a DS |
99 | { |
100 | struct bt_ctf_field_decl **list; | |
24a3136a DS |
101 | int ret; |
102 | ||
103 | ret = bt_ctf_get_decl_fields(event_decl, scope, | |
cebae8c3 | 104 | (const struct bt_ctf_field_decl * const **)&list, len); |
24a3136a DS |
105 | |
106 | if (ret < 0) /* For python to know an error occured */ | |
107 | list = NULL; | |
24a3136a DS |
108 | |
109 | return list; | |
110 | } | |
111 | ||
112 | struct bt_ctf_field_decl *_bt_python_field_decl_one_from_list( | |
113 | struct bt_ctf_field_decl **list, int index) | |
114 | { | |
115 | return list[index]; | |
116 | } | |
3c2ce778 XH |
117 | |
118 | struct definition_array *_bt_python_get_array_from_def( | |
119 | struct bt_definition *field) | |
120 | { | |
121 | const struct bt_declaration *array_decl; | |
122 | struct definition_array *array = NULL; | |
123 | ||
124 | if (!field) { | |
125 | goto end; | |
126 | } | |
127 | ||
128 | array_decl = bt_ctf_get_decl_from_def(field); | |
129 | if (bt_ctf_field_type(array_decl) == CTF_TYPE_ARRAY) { | |
130 | array = container_of(field, struct definition_array, p); | |
131 | } | |
132 | end: | |
133 | return array; | |
134 | } | |
786207e0 XH |
135 | |
136 | struct definition_sequence *_bt_python_get_sequence_from_def( | |
137 | struct bt_definition *field) | |
138 | { | |
139 | if (field && bt_ctf_field_type( | |
140 | bt_ctf_get_decl_from_def(field)) == CTF_TYPE_SEQUENCE) { | |
141 | return container_of(field, struct definition_sequence, p); | |
142 | } | |
143 | ||
144 | return NULL; | |
145 | } |