Helper function to extract string from fields
[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.
bed864a7
MD
19 */
20
21#include <babeltrace/compiler.h>
22#include <babeltrace/align.h>
4c8bfb7e 23#include <babeltrace/format.h>
bed864a7 24
c054553d 25static
f6625916 26struct definition *_string_definition_new(struct declaration *declaration,
05c749e5 27 struct definition_scope *parent_scope,
98df1c9f
MD
28 GQuark field_name, int index,
29 const char *root_name);
c054553d 30static
e1151715 31void _string_definition_free(struct definition *definition);
c054553d 32
c054553d 33static
f6625916 34void _string_declaration_free(struct declaration *declaration)
bed864a7 35{
f6625916
MD
36 struct declaration_string *string_declaration =
37 container_of(declaration, struct declaration_string, p);
38 g_free(string_declaration);
bed864a7
MD
39}
40
ab4cf058
MD
41struct declaration_string *
42 string_declaration_new(enum ctf_string_encoding encoding)
bed864a7 43{
f6625916 44 struct declaration_string *string_declaration;
bed864a7 45
f6625916
MD
46 string_declaration = g_new(struct declaration_string, 1);
47 string_declaration->p.id = CTF_TYPE_STRING;
f6625916 48 string_declaration->p.alignment = CHAR_BIT;
f6625916
MD
49 string_declaration->p.declaration_free = _string_declaration_free;
50 string_declaration->p.definition_new = _string_definition_new;
51 string_declaration->p.definition_free = _string_definition_free;
52 string_declaration->p.ref = 1;
ab4cf058 53 string_declaration->encoding = encoding;
f6625916 54 return string_declaration;
bed864a7 55}
c054553d
MD
56
57static
e1151715 58struct definition *
f6625916 59 _string_definition_new(struct declaration *declaration,
05c749e5 60 struct definition_scope *parent_scope,
98df1c9f
MD
61 GQuark field_name, int index,
62 const char *root_name)
c054553d 63{
f6625916
MD
64 struct declaration_string *string_declaration =
65 container_of(declaration, struct declaration_string, p);
e1151715 66 struct definition_string *string;
98df1c9f 67 int ret;
c054553d 68
e1151715 69 string = g_new(struct definition_string, 1);
f6625916
MD
70 declaration_ref(&string_declaration->p);
71 string->p.declaration = declaration;
72 string->declaration = string_declaration;
c054553d 73 string->p.ref = 1;
98df1c9f
MD
74 /*
75 * Use INT_MAX order to ensure that all fields of the parent
76 * scope are seen as being prior to this scope.
77 */
78 string->p.index = root_name ? INT_MAX : index;
b1a2f580 79 string->p.name = field_name;
98df1c9f
MD
80 string->p.path = new_definition_path(parent_scope, field_name,
81 root_name);
a35173fe 82 string->p.scope = NULL;
c054553d 83 string->value = NULL;
d11e9c49
MD
84 string->len = 0;
85 string->alloc_len = 0;
98df1c9f
MD
86 ret = register_field_definition(field_name, &string->p,
87 parent_scope);
88 assert(!ret);
c054553d
MD
89 return &string->p;
90}
91
92static
e1151715 93void _string_definition_free(struct definition *definition)
c054553d 94{
e1151715
MD
95 struct definition_string *string =
96 container_of(definition, struct definition_string, p);
c054553d 97
f6625916 98 declaration_unref(string->p.declaration);
c054553d
MD
99 g_free(string->value);
100 g_free(string);
101}
98b68326
JD
102
103char *get_string(struct definition *field)
104{
105 struct definition_string *string_definition =
106 container_of(field, struct definition_string, p);
107
108 assert(string_definition->value != NULL);
109
110 return string_definition->value;
111}
This page took 0.029864 seconds and 4 git commands to generate.