Add struct/variant/enum named type to type scope
[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
e19c3d69
MD
24struct declaration *_string_declaration_new(struct type *type,
25 struct declaration_scope *parent_scope);
c054553d 26static
e19c3d69 27void _string_declaration_free(struct declaration *declaration);
c054553d 28
bed864a7
MD
29void string_copy(struct stream_pos *dest, const struct format *fdest,
30 struct stream_pos *src, const struct format *fsrc,
e19c3d69 31 struct declaration *declaration)
bed864a7 32{
e19c3d69
MD
33 struct declaration_string *string =
34 container_of(declaration, struct declaration_string, p);
35 struct type_string *string_type = string->type;
bed864a7
MD
36
37 if (fsrc->string_copy == fdest->string_copy) {
e19c3d69 38 fsrc->string_copy(dest, src, string_type);
bed864a7 39 } else {
47e0f2e2 40 char *tmp = NULL;
a52d7f6a 41
e19c3d69
MD
42 fsrc->string_read(&tmp, src, string_type);
43 fdest->string_write(dest, tmp, string_type);
a52d7f6a 44 fsrc->string_free_temp(tmp);
bed864a7
MD
45 }
46}
47
c054553d 48static
e19c3d69 49void _string_type_free(struct type *type)
bed864a7 50{
e19c3d69
MD
51 struct type_string *string_type =
52 container_of(type, struct type_string, p);
53 g_free(string_type);
bed864a7
MD
54}
55
e19c3d69 56struct type_string *string_type_new(const char *name)
bed864a7 57{
e19c3d69 58 struct type_string *string_type;
bed864a7 59
e19c3d69 60 string_type = g_new(struct type_string, 1);
05628561 61 string_type->p.id = CTF_TYPE_STRING;
e19c3d69
MD
62 string_type->p.name = g_quark_from_string(name);
63 string_type->p.alignment = CHAR_BIT;
64 string_type->p.copy = string_copy;
65 string_type->p.type_free = _string_type_free;
66 string_type->p.declaration_new = _string_declaration_new;
6ee5115e 67 string_type->p.declaration_free = _string_declaration_free;
e19c3d69 68 string_type->p.ref = 1;
e19c3d69 69 return string_type;
bed864a7 70}
c054553d
MD
71
72static
e19c3d69
MD
73struct declaration *
74 _string_declaration_new(struct type *type,
75 struct declaration_scope *parent_scope)
c054553d 76{
e19c3d69
MD
77 struct type_string *string_type =
78 container_of(type, struct type_string, p);
79 struct declaration_string *string;
c054553d 80
e19c3d69
MD
81 string = g_new(struct declaration_string, 1);
82 type_ref(&string_type->p);
6ee5115e
MD
83 string->p.type = type;
84 string->type = string_type;
c054553d
MD
85 string->p.ref = 1;
86 string->value = NULL;
87 return &string->p;
88}
89
90static
e19c3d69 91void _string_declaration_free(struct declaration *declaration)
c054553d 92{
e19c3d69
MD
93 struct declaration_string *string =
94 container_of(declaration, struct declaration_string, p);
c054553d 95
e19c3d69 96 type_unref(string->p.type);
c054553d
MD
97 g_free(string->value);
98 g_free(string);
99}
This page took 0.027721 seconds and 4 git commands to generate.