Add missing permission notice in each source file
[babeltrace.git] / types / string.c
CommitLineData
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 34static
f6625916 35struct definition *_string_definition_new(struct declaration *declaration,
05c749e5 36 struct definition_scope *parent_scope,
98df1c9f
MD
37 GQuark field_name, int index,
38 const char *root_name);
c054553d 39static
e1151715 40void _string_definition_free(struct definition *definition);
c054553d 41
c054553d 42static
f6625916 43void _string_declaration_free(struct 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
MD
50struct declaration_string *
51 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
66static
e1151715 67struct definition *
f6625916 68 _string_definition_new(struct 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);
f6625916
MD
79 declaration_ref(&string_declaration->p);
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;
98df1c9f
MD
89 string->p.path = new_definition_path(parent_scope, field_name,
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;
98df1c9f
MD
95 ret = register_field_definition(field_name, &string->p,
96 parent_scope);
97 assert(!ret);
c054553d
MD
98 return &string->p;
99}
100
101static
e1151715 102void _string_definition_free(struct definition *definition)
c054553d 103{
e1151715
MD
104 struct definition_string *string =
105 container_of(definition, struct definition_string, p);
c054553d 106
f6625916 107 declaration_unref(string->p.declaration);
c054553d
MD
108 g_free(string->value);
109 g_free(string);
110}
98b68326 111
8673030f
JD
112enum ctf_string_encoding get_string_encoding(const struct 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
da320b83 123char *get_string(const struct 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}
This page took 0.047614 seconds and 4 git commands to generate.