| 1 | /* |
| 2 | * string.c |
| 3 | * |
| 4 | * BabelTrace - String Type Converter |
| 5 | * |
| 6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
| 7 | * |
| 8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@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 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | * SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | #include <babeltrace/compiler.h> |
| 30 | #include <babeltrace/align.h> |
| 31 | #include <babeltrace/format.h> |
| 32 | #include <babeltrace/types.h> |
| 33 | |
| 34 | static |
| 35 | struct bt_definition *_string_definition_new(struct bt_declaration *declaration, |
| 36 | struct definition_scope *parent_scope, |
| 37 | GQuark field_name, int index, |
| 38 | const char *root_name); |
| 39 | static |
| 40 | void _string_definition_free(struct bt_definition *definition); |
| 41 | |
| 42 | static |
| 43 | void _string_declaration_free(struct bt_declaration *declaration) |
| 44 | { |
| 45 | struct declaration_string *string_declaration = |
| 46 | container_of(declaration, struct declaration_string, p); |
| 47 | g_free(string_declaration); |
| 48 | } |
| 49 | |
| 50 | struct declaration_string * |
| 51 | bt_string_declaration_new(enum ctf_string_encoding encoding) |
| 52 | { |
| 53 | struct declaration_string *string_declaration; |
| 54 | |
| 55 | string_declaration = g_new(struct declaration_string, 1); |
| 56 | string_declaration->p.id = CTF_TYPE_STRING; |
| 57 | string_declaration->p.alignment = CHAR_BIT; |
| 58 | string_declaration->p.declaration_free = _string_declaration_free; |
| 59 | string_declaration->p.definition_new = _string_definition_new; |
| 60 | string_declaration->p.definition_free = _string_definition_free; |
| 61 | string_declaration->p.ref = 1; |
| 62 | string_declaration->encoding = encoding; |
| 63 | return string_declaration; |
| 64 | } |
| 65 | |
| 66 | static |
| 67 | struct bt_definition * |
| 68 | _string_definition_new(struct bt_declaration *declaration, |
| 69 | struct definition_scope *parent_scope, |
| 70 | GQuark field_name, int index, |
| 71 | const char *root_name) |
| 72 | { |
| 73 | struct declaration_string *string_declaration = |
| 74 | container_of(declaration, struct declaration_string, p); |
| 75 | struct definition_string *string; |
| 76 | int ret; |
| 77 | |
| 78 | string = g_new(struct definition_string, 1); |
| 79 | bt_declaration_ref(&string_declaration->p); |
| 80 | string->p.declaration = declaration; |
| 81 | string->declaration = string_declaration; |
| 82 | string->p.ref = 1; |
| 83 | /* |
| 84 | * Use INT_MAX order to ensure that all fields of the parent |
| 85 | * scope are seen as being prior to this scope. |
| 86 | */ |
| 87 | string->p.index = root_name ? INT_MAX : index; |
| 88 | string->p.name = field_name; |
| 89 | string->p.path = bt_new_definition_path(parent_scope, field_name, |
| 90 | root_name); |
| 91 | string->p.scope = NULL; |
| 92 | string->value = NULL; |
| 93 | string->len = 0; |
| 94 | string->alloc_len = 0; |
| 95 | ret = bt_register_field_definition(field_name, &string->p, |
| 96 | parent_scope); |
| 97 | assert(!ret); |
| 98 | return &string->p; |
| 99 | } |
| 100 | |
| 101 | static |
| 102 | void _string_definition_free(struct bt_definition *definition) |
| 103 | { |
| 104 | struct definition_string *string = |
| 105 | container_of(definition, struct definition_string, p); |
| 106 | |
| 107 | bt_declaration_unref(string->p.declaration); |
| 108 | g_free(string->value); |
| 109 | g_free(string); |
| 110 | } |
| 111 | |
| 112 | enum ctf_string_encoding bt_get_string_encoding(const struct bt_definition *field) |
| 113 | { |
| 114 | struct definition_string *string_definition; |
| 115 | const struct declaration_string *string_declaration; |
| 116 | |
| 117 | string_definition = container_of(field, struct definition_string, p); |
| 118 | string_declaration = string_definition->declaration; |
| 119 | |
| 120 | return string_declaration->encoding; |
| 121 | } |
| 122 | |
| 123 | char *bt_get_string(const struct bt_definition *field) |
| 124 | { |
| 125 | struct definition_string *string_definition = |
| 126 | container_of(field, struct definition_string, p); |
| 127 | |
| 128 | assert(string_definition->value != NULL); |
| 129 | |
| 130 | return string_definition->value; |
| 131 | } |