| 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 | #include <babeltrace/ctf-writer/event-types-internal.h> |
| 23 | #include <babeltrace/ctf-writer/event-fields-internal.h> |
| 24 | #include <babeltrace/iterator.h> |
| 25 | #include <glib.h> |
| 26 | |
| 27 | /* FILE functions |
| 28 | ---------------------------------------------------- |
| 29 | */ |
| 30 | |
| 31 | FILE *_bt_file_open(char *file_path, char *mode) |
| 32 | { |
| 33 | FILE *fp = stdout; |
| 34 | if (file_path != NULL) |
| 35 | fp = fopen(file_path, mode); |
| 36 | return fp; |
| 37 | } |
| 38 | |
| 39 | void _bt_file_close(FILE *fp) |
| 40 | { |
| 41 | if (fp != NULL) |
| 42 | fclose(fp); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | /* List-related functions |
| 47 | ---------------------------------------------------- |
| 48 | */ |
| 49 | |
| 50 | /* ctf-field-list */ |
| 51 | struct bt_definition **_bt_python_field_listcaller( |
| 52 | const struct bt_ctf_event *ctf_event, |
| 53 | const struct bt_definition *scope, |
| 54 | unsigned int *len) |
| 55 | { |
| 56 | struct bt_definition **list; |
| 57 | int ret; |
| 58 | |
| 59 | ret = bt_ctf_get_field_list(ctf_event, scope, |
| 60 | (const struct bt_definition * const **)&list, len); |
| 61 | |
| 62 | if (ret < 0) /* For python to know an error occured */ |
| 63 | list = NULL; |
| 64 | |
| 65 | return list; |
| 66 | } |
| 67 | |
| 68 | struct bt_definition *_bt_python_field_one_from_list( |
| 69 | struct bt_definition **list, int index) |
| 70 | { |
| 71 | return list[index]; |
| 72 | } |
| 73 | |
| 74 | /* event_decl_list */ |
| 75 | struct bt_ctf_event_decl **_bt_python_event_decl_listcaller( |
| 76 | int handle_id, |
| 77 | struct bt_context *ctx, |
| 78 | unsigned int *len) |
| 79 | { |
| 80 | struct bt_ctf_event_decl **list; |
| 81 | int ret; |
| 82 | |
| 83 | ret = bt_ctf_get_event_decl_list(handle_id, ctx, |
| 84 | (struct bt_ctf_event_decl * const **)&list, len); |
| 85 | |
| 86 | if (ret < 0) /* For python to know an error occured */ |
| 87 | list = NULL; |
| 88 | |
| 89 | return list; |
| 90 | } |
| 91 | |
| 92 | struct bt_ctf_event_decl *_bt_python_decl_one_from_list( |
| 93 | struct bt_ctf_event_decl **list, int index) |
| 94 | { |
| 95 | return list[index]; |
| 96 | } |
| 97 | |
| 98 | /* decl_fields */ |
| 99 | struct bt_ctf_field_decl **_by_python_field_decl_listcaller( |
| 100 | struct bt_ctf_event_decl *event_decl, |
| 101 | enum bt_ctf_scope scope, |
| 102 | unsigned int *len) |
| 103 | { |
| 104 | struct bt_ctf_field_decl **list; |
| 105 | int ret; |
| 106 | |
| 107 | ret = bt_ctf_get_decl_fields(event_decl, scope, |
| 108 | (const struct bt_ctf_field_decl * const **)&list, len); |
| 109 | |
| 110 | if (ret < 0) /* For python to know an error occured */ |
| 111 | list = NULL; |
| 112 | |
| 113 | return list; |
| 114 | } |
| 115 | |
| 116 | struct bt_ctf_field_decl *_bt_python_field_decl_one_from_list( |
| 117 | struct bt_ctf_field_decl **list, int index) |
| 118 | { |
| 119 | return list[index]; |
| 120 | } |
| 121 | |
| 122 | struct definition_array *_bt_python_get_array_from_def( |
| 123 | struct bt_definition *field) |
| 124 | { |
| 125 | const struct bt_declaration *array_decl; |
| 126 | struct definition_array *array = NULL; |
| 127 | |
| 128 | if (!field) { |
| 129 | goto end; |
| 130 | } |
| 131 | |
| 132 | array_decl = bt_ctf_get_decl_from_def(field); |
| 133 | if (bt_ctf_field_type(array_decl) == CTF_TYPE_ARRAY) { |
| 134 | array = container_of(field, struct definition_array, p); |
| 135 | } |
| 136 | end: |
| 137 | return array; |
| 138 | } |
| 139 | |
| 140 | struct bt_declaration *_bt_python_get_array_element_declaration( |
| 141 | struct bt_declaration *field) |
| 142 | { |
| 143 | struct declaration_array *array_decl; |
| 144 | struct bt_declaration *ret = NULL; |
| 145 | |
| 146 | if (!field) { |
| 147 | goto end; |
| 148 | } |
| 149 | |
| 150 | array_decl = container_of(field, struct declaration_array, p); |
| 151 | ret = array_decl->elem; |
| 152 | end: |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | struct bt_declaration *_bt_python_get_sequence_element_declaration( |
| 157 | struct bt_declaration *field) |
| 158 | { |
| 159 | struct declaration_sequence *sequence_decl; |
| 160 | struct bt_declaration *ret = NULL; |
| 161 | |
| 162 | if (!field) { |
| 163 | goto end; |
| 164 | } |
| 165 | |
| 166 | sequence_decl = container_of(field, struct declaration_sequence, p); |
| 167 | ret = sequence_decl->elem; |
| 168 | end: |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | const char *_bt_python_get_array_string(struct bt_definition *field) |
| 173 | { |
| 174 | struct definition_array *array; |
| 175 | const char *ret = NULL; |
| 176 | |
| 177 | if (!field) { |
| 178 | goto end; |
| 179 | } |
| 180 | |
| 181 | array = container_of(field, struct definition_array, p); |
| 182 | ret = array->string->str; |
| 183 | end: |
| 184 | return ret; |
| 185 | } |
| 186 | |
| 187 | const char *_bt_python_get_sequence_string(struct bt_definition *field) |
| 188 | { |
| 189 | struct definition_sequence *sequence; |
| 190 | const char *ret = NULL; |
| 191 | |
| 192 | if (!field) { |
| 193 | goto end; |
| 194 | } |
| 195 | |
| 196 | sequence = container_of(field, struct definition_sequence, p); |
| 197 | ret = sequence->string->str; |
| 198 | end: |
| 199 | return ret; |
| 200 | } |
| 201 | |
| 202 | struct definition_sequence *_bt_python_get_sequence_from_def( |
| 203 | struct bt_definition *field) |
| 204 | { |
| 205 | if (field && bt_ctf_field_type( |
| 206 | bt_ctf_get_decl_from_def(field)) == CTF_TYPE_SEQUENCE) { |
| 207 | return container_of(field, struct definition_sequence, p); |
| 208 | } |
| 209 | |
| 210 | return NULL; |
| 211 | } |
| 212 | |
| 213 | int _bt_python_field_integer_get_signedness(const struct bt_ctf_field *field) |
| 214 | { |
| 215 | int ret; |
| 216 | |
| 217 | if (!field || field->type->declaration->id != CTF_TYPE_INTEGER) { |
| 218 | ret = -1; |
| 219 | goto end; |
| 220 | } |
| 221 | |
| 222 | const struct bt_ctf_field_type_integer *type = container_of(field->type, |
| 223 | const struct bt_ctf_field_type_integer, parent); |
| 224 | ret = type->declaration.signedness; |
| 225 | end: |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | enum ctf_type_id _bt_python_get_field_type(const struct bt_ctf_field *field) |
| 230 | { |
| 231 | enum ctf_type_id type_id = CTF_TYPE_UNKNOWN; |
| 232 | |
| 233 | if (!field) { |
| 234 | goto end; |
| 235 | } |
| 236 | |
| 237 | type_id = field->type->declaration->id; |
| 238 | end: |
| 239 | return type_id; |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * Python 3.5 changes the StopIteration exception clearing behaviour which |
| 244 | * erroneously marks swig clean-up function as having failed. This explicit |
| 245 | * allocation function is intended as a work-around so SWIG doesn't manage |
| 246 | * the lifetime of a "temporary" object by itself. |
| 247 | */ |
| 248 | struct bt_iter_pos *_bt_python_create_iter_pos(void) |
| 249 | { |
| 250 | return g_new0(struct bt_iter_pos, 1); |
| 251 | } |