Commit | Line | Data |
---|---|---|
bed864a7 | 1 | /* |
ccd7e1c8 | 2 | * string.c |
bed864a7 | 3 | * |
ccd7e1c8 | 4 | * BabelTrace - String Type Converter |
bed864a7 | 5 | * |
64fa3fec MD |
6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
7 | * | |
8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
bed864a7 | 9 | * |
ccd7e1c8 MD |
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: | |
bed864a7 | 16 | * |
ccd7e1c8 MD |
17 | * The above copyright notice and this permission notice shall be included in |
18 | * all copies or substantial portions of the Software. | |
c462e188 MD |
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. | |
bed864a7 MD |
27 | */ |
28 | ||
29 | #include <babeltrace/compiler.h> | |
30 | #include <babeltrace/align.h> | |
4c8bfb7e | 31 | #include <babeltrace/format.h> |
3122e6f0 | 32 | #include <babeltrace/types.h> |
bed864a7 | 33 | |
c054553d | 34 | static |
ecc54f11 | 35 | struct bt_definition *_string_definition_new(struct bt_declaration *declaration, |
05c749e5 | 36 | struct definition_scope *parent_scope, |
98df1c9f MD |
37 | GQuark field_name, int index, |
38 | const char *root_name); | |
c054553d | 39 | static |
0d69b916 | 40 | void _string_definition_free(struct bt_definition *definition); |
c054553d | 41 | |
c054553d | 42 | static |
ecc54f11 | 43 | void _string_declaration_free(struct bt_declaration *declaration) |
bed864a7 | 44 | { |
f6625916 MD |
45 | struct declaration_string *string_declaration = |
46 | container_of(declaration, struct declaration_string, p); | |
47 | g_free(string_declaration); | |
bed864a7 MD |
48 | } |
49 | ||
ab4cf058 | 50 | struct declaration_string * |
ebdb2383 | 51 | bt_string_declaration_new(enum ctf_string_encoding encoding) |
bed864a7 | 52 | { |
f6625916 | 53 | struct declaration_string *string_declaration; |
bed864a7 | 54 | |
f6625916 MD |
55 | string_declaration = g_new(struct declaration_string, 1); |
56 | string_declaration->p.id = CTF_TYPE_STRING; | |
f6625916 | 57 | string_declaration->p.alignment = CHAR_BIT; |
f6625916 MD |
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; | |
ab4cf058 | 62 | string_declaration->encoding = encoding; |
f6625916 | 63 | return string_declaration; |
bed864a7 | 64 | } |
c054553d MD |
65 | |
66 | static | |
0d69b916 | 67 | struct bt_definition * |
ecc54f11 | 68 | _string_definition_new(struct bt_declaration *declaration, |
05c749e5 | 69 | struct definition_scope *parent_scope, |
98df1c9f MD |
70 | GQuark field_name, int index, |
71 | const char *root_name) | |
c054553d | 72 | { |
f6625916 MD |
73 | struct declaration_string *string_declaration = |
74 | container_of(declaration, struct declaration_string, p); | |
e1151715 | 75 | struct definition_string *string; |
98df1c9f | 76 | int ret; |
c054553d | 77 | |
e1151715 | 78 | string = g_new(struct definition_string, 1); |
e6b4b4f4 | 79 | bt_declaration_ref(&string_declaration->p); |
f6625916 MD |
80 | string->p.declaration = declaration; |
81 | string->declaration = string_declaration; | |
c054553d | 82 | string->p.ref = 1; |
98df1c9f MD |
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; | |
b1a2f580 | 88 | string->p.name = field_name; |
2b77e6a6 | 89 | string->p.path = bt_new_definition_path(parent_scope, field_name, |
98df1c9f | 90 | root_name); |
a35173fe | 91 | string->p.scope = NULL; |
c054553d | 92 | string->value = NULL; |
d11e9c49 MD |
93 | string->len = 0; |
94 | string->alloc_len = 0; | |
2b77e6a6 | 95 | ret = bt_register_field_definition(field_name, &string->p, |
98df1c9f MD |
96 | parent_scope); |
97 | assert(!ret); | |
c054553d MD |
98 | return &string->p; |
99 | } | |
100 | ||
101 | static | |
0d69b916 | 102 | void _string_definition_free(struct bt_definition *definition) |
c054553d | 103 | { |
e1151715 MD |
104 | struct definition_string *string = |
105 | container_of(definition, struct definition_string, p); | |
c054553d | 106 | |
e6b4b4f4 | 107 | bt_declaration_unref(string->p.declaration); |
c054553d MD |
108 | g_free(string->value); |
109 | g_free(string); | |
110 | } | |
98b68326 | 111 | |
0d69b916 | 112 | enum ctf_string_encoding bt_get_string_encoding(const struct bt_definition *field) |
8673030f JD |
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 | ||
0d69b916 | 123 | char *bt_get_string(const struct bt_definition *field) |
98b68326 JD |
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 | } |