Rename "type" to "declaration"
[babeltrace.git] / types / string.c
CommitLineData
bed864a7 1/*
ccd7e1c8 2 * string.c
bed864a7 3 *
ccd7e1c8 4 * BabelTrace - String Type Converter
bed864a7 5 *
c054553d 6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
bed864a7 7 *
ccd7e1c8
MD
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
bed864a7 14 *
ccd7e1c8
MD
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
bed864a7
MD
17 */
18
19#include <babeltrace/compiler.h>
20#include <babeltrace/align.h>
4c8bfb7e 21#include <babeltrace/format.h>
bed864a7 22
c054553d 23static
f6625916 24struct definition *_string_definition_new(struct declaration *declaration,
e1151715 25 struct definition_scope *parent_scope);
c054553d 26static
e1151715 27void _string_definition_free(struct definition *definition);
c054553d 28
bed864a7
MD
29void string_copy(struct stream_pos *dest, const struct format *fdest,
30 struct stream_pos *src, const struct format *fsrc,
e1151715 31 struct definition *definition)
bed864a7 32{
e1151715
MD
33 struct definition_string *string =
34 container_of(definition, struct definition_string, p);
f6625916 35 struct declaration_string *string_declaration = string->declaration;
bed864a7
MD
36
37 if (fsrc->string_copy == fdest->string_copy) {
f6625916 38 fsrc->string_copy(dest, src, string_declaration);
bed864a7 39 } else {
47e0f2e2 40 char *tmp = NULL;
a52d7f6a 41
f6625916
MD
42 fsrc->string_read(&tmp, src, string_declaration);
43 fdest->string_write(dest, tmp, string_declaration);
a52d7f6a 44 fsrc->string_free_temp(tmp);
bed864a7
MD
45 }
46}
47
c054553d 48static
f6625916 49void _string_declaration_free(struct declaration *declaration)
bed864a7 50{
f6625916
MD
51 struct declaration_string *string_declaration =
52 container_of(declaration, struct declaration_string, p);
53 g_free(string_declaration);
bed864a7
MD
54}
55
f6625916 56struct declaration_string *string_declaration_new(const char *name)
bed864a7 57{
f6625916 58 struct declaration_string *string_declaration;
bed864a7 59
f6625916
MD
60 string_declaration = g_new(struct declaration_string, 1);
61 string_declaration->p.id = CTF_TYPE_STRING;
62 string_declaration->p.name = g_quark_from_string(name);
63 string_declaration->p.alignment = CHAR_BIT;
64 string_declaration->p.copy = string_copy;
65 string_declaration->p.declaration_free = _string_declaration_free;
66 string_declaration->p.definition_new = _string_definition_new;
67 string_declaration->p.definition_free = _string_definition_free;
68 string_declaration->p.ref = 1;
69 return string_declaration;
bed864a7 70}
c054553d
MD
71
72static
e1151715 73struct definition *
f6625916 74 _string_definition_new(struct declaration *declaration,
e1151715 75 struct definition_scope *parent_scope)
c054553d 76{
f6625916
MD
77 struct declaration_string *string_declaration =
78 container_of(declaration, struct declaration_string, p);
e1151715 79 struct definition_string *string;
c054553d 80
e1151715 81 string = g_new(struct definition_string, 1);
f6625916
MD
82 declaration_ref(&string_declaration->p);
83 string->p.declaration = declaration;
84 string->declaration = string_declaration;
c054553d
MD
85 string->p.ref = 1;
86 string->value = NULL;
87 return &string->p;
88}
89
90static
e1151715 91void _string_definition_free(struct definition *definition)
c054553d 92{
e1151715
MD
93 struct definition_string *string =
94 container_of(definition, struct definition_string, p);
c054553d 95
f6625916 96 declaration_unref(string->p.declaration);
c054553d
MD
97 g_free(string->value);
98 g_free(string);
99}
This page took 0.027169 seconds and 4 git commands to generate.