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.
21 #include <babeltrace/compiler.h>
22 #include <babeltrace/align.h>
23 #include <babeltrace/format.h>
24 #include <babeltrace/types.h>
27 struct definition
*_string_definition_new(struct declaration
*declaration
,
28 struct definition_scope
*parent_scope
,
29 GQuark field_name
, int index
,
30 const char *root_name
);
32 void _string_definition_free(struct definition
*definition
);
35 void _string_declaration_free(struct declaration
*declaration
)
37 struct declaration_string
*string_declaration
=
38 container_of(declaration
, struct declaration_string
, p
);
39 g_free(string_declaration
);
42 struct declaration_string
*
43 string_declaration_new(enum ctf_string_encoding encoding
)
45 struct declaration_string
*string_declaration
;
47 string_declaration
= g_new(struct declaration_string
, 1);
48 string_declaration
->p
.id
= CTF_TYPE_STRING
;
49 string_declaration
->p
.alignment
= CHAR_BIT
;
50 string_declaration
->p
.declaration_free
= _string_declaration_free
;
51 string_declaration
->p
.definition_new
= _string_definition_new
;
52 string_declaration
->p
.definition_free
= _string_definition_free
;
53 string_declaration
->p
.ref
= 1;
54 string_declaration
->encoding
= encoding
;
55 return string_declaration
;
60 _string_definition_new(struct declaration
*declaration
,
61 struct definition_scope
*parent_scope
,
62 GQuark field_name
, int index
,
63 const char *root_name
)
65 struct declaration_string
*string_declaration
=
66 container_of(declaration
, struct declaration_string
, p
);
67 struct definition_string
*string
;
70 string
= g_new(struct definition_string
, 1);
71 declaration_ref(&string_declaration
->p
);
72 string
->p
.declaration
= declaration
;
73 string
->declaration
= string_declaration
;
76 * Use INT_MAX order to ensure that all fields of the parent
77 * scope are seen as being prior to this scope.
79 string
->p
.index
= root_name
? INT_MAX
: index
;
80 string
->p
.name
= field_name
;
81 string
->p
.path
= new_definition_path(parent_scope
, field_name
,
83 string
->p
.scope
= NULL
;
86 string
->alloc_len
= 0;
87 ret
= register_field_definition(field_name
, &string
->p
,
94 void _string_definition_free(struct definition
*definition
)
96 struct definition_string
*string
=
97 container_of(definition
, struct definition_string
, p
);
99 declaration_unref(string
->p
.declaration
);
100 g_free(string
->value
);
104 enum ctf_string_encoding
get_string_encoding(const struct definition
*field
)
106 struct definition_string
*string_definition
;
107 const struct declaration_string
*string_declaration
;
109 string_definition
= container_of(field
, struct definition_string
, p
);
110 string_declaration
= string_definition
->declaration
;
112 return string_declaration
->encoding
;
115 char *get_string(const struct definition
*field
)
117 struct definition_string
*string_definition
=
118 container_of(field
, struct definition_string
, p
);
120 assert(string_definition
->value
!= NULL
);
122 return string_definition
->value
;