4 * BabelTrace - String Type Converter
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #include <babeltrace/compiler.h>
30 #include <babeltrace/align.h>
31 #include <babeltrace/format.h>
32 #include <babeltrace/types.h>
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
);
40 void _string_definition_free(struct bt_definition
*definition
);
43 void _string_declaration_free(struct bt_declaration
*declaration
)
45 struct declaration_string
*string_declaration
=
46 container_of(declaration
, struct declaration_string
, p
);
47 g_free(string_declaration
);
50 struct declaration_string
*
51 bt_string_declaration_new(enum ctf_string_encoding encoding
)
53 struct declaration_string
*string_declaration
;
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
;
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
)
73 struct declaration_string
*string_declaration
=
74 container_of(declaration
, struct declaration_string
, p
);
75 struct definition_string
*string
;
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
;
84 * Use INT_MAX order to ensure that all fields of the parent
85 * scope are seen as being prior to this scope.
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
,
91 string
->p
.scope
= NULL
;
94 string
->alloc_len
= 0;
95 ret
= bt_register_field_definition(field_name
, &string
->p
,
102 void _string_definition_free(struct bt_definition
*definition
)
104 struct definition_string
*string
=
105 container_of(definition
, struct definition_string
, p
);
107 bt_declaration_unref(string
->p
.declaration
);
108 g_free(string
->value
);
112 enum ctf_string_encoding
bt_get_string_encoding(const struct bt_definition
*field
)
114 struct definition_string
*string_definition
;
115 const struct declaration_string
*string_declaration
;
117 string_definition
= container_of(field
, struct definition_string
, p
);
118 string_declaration
= string_definition
->declaration
;
120 return string_declaration
->encoding
;
123 char *bt_get_string(const struct bt_definition
*field
)
125 struct definition_string
*string_definition
=
126 container_of(field
, struct definition_string
, p
);
128 assert(string_definition
->value
!= NULL
);
130 return string_definition
->value
;