| 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 | |
| 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 */ |
| 49 | struct bt_definition **_bt_python_field_listcaller( |
| 50 | const struct bt_ctf_event *ctf_event, |
| 51 | const struct bt_definition *scope, |
| 52 | unsigned int *len) |
| 53 | { |
| 54 | struct bt_definition **list; |
| 55 | int ret; |
| 56 | |
| 57 | ret = bt_ctf_get_field_list(ctf_event, scope, |
| 58 | (const struct bt_definition * const **)&list, len); |
| 59 | |
| 60 | if (ret < 0) /* For python to know an error occured */ |
| 61 | list = NULL; |
| 62 | |
| 63 | return list; |
| 64 | } |
| 65 | |
| 66 | struct bt_definition *_bt_python_field_one_from_list( |
| 67 | struct bt_definition **list, int index) |
| 68 | { |
| 69 | return list[index]; |
| 70 | } |
| 71 | |
| 72 | /* event_decl_list */ |
| 73 | struct bt_ctf_event_decl **_bt_python_event_decl_listcaller( |
| 74 | int handle_id, |
| 75 | struct bt_context *ctx, |
| 76 | unsigned int *len) |
| 77 | { |
| 78 | struct bt_ctf_event_decl **list; |
| 79 | int ret; |
| 80 | |
| 81 | ret = bt_ctf_get_event_decl_list(handle_id, ctx, |
| 82 | (struct bt_ctf_event_decl * const **)&list, len); |
| 83 | |
| 84 | if (ret < 0) /* For python to know an error occured */ |
| 85 | list = NULL; |
| 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, |
| 99 | enum bt_ctf_scope scope, |
| 100 | unsigned int *len) |
| 101 | { |
| 102 | struct bt_ctf_field_decl **list; |
| 103 | int ret; |
| 104 | |
| 105 | ret = bt_ctf_get_decl_fields(event_decl, scope, |
| 106 | (const struct bt_ctf_field_decl * const **)&list, len); |
| 107 | |
| 108 | if (ret < 0) /* For python to know an error occured */ |
| 109 | list = NULL; |
| 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 | } |
| 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 | } |
| 137 | |
| 138 | struct 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; |
| 150 | end: |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | const 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; |
| 165 | end: |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | struct 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 | } |
| 179 | |
| 180 | int _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; |
| 192 | end: |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | enum 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; |
| 205 | end: |
| 206 | return type_id; |
| 207 | } |