Rename "type" to "declaration"
[babeltrace.git] / types / string.c
1 /*
2 * string.c
3 *
4 * BabelTrace - String Type Converter
5 *
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19 #include <babeltrace/compiler.h>
20 #include <babeltrace/align.h>
21 #include <babeltrace/format.h>
22
23 static
24 struct definition *_string_definition_new(struct declaration *declaration,
25 struct definition_scope *parent_scope);
26 static
27 void _string_definition_free(struct definition *definition);
28
29 void string_copy(struct stream_pos *dest, const struct format *fdest,
30 struct stream_pos *src, const struct format *fsrc,
31 struct definition *definition)
32 {
33 struct definition_string *string =
34 container_of(definition, struct definition_string, p);
35 struct declaration_string *string_declaration = string->declaration;
36
37 if (fsrc->string_copy == fdest->string_copy) {
38 fsrc->string_copy(dest, src, string_declaration);
39 } else {
40 char *tmp = NULL;
41
42 fsrc->string_read(&tmp, src, string_declaration);
43 fdest->string_write(dest, tmp, string_declaration);
44 fsrc->string_free_temp(tmp);
45 }
46 }
47
48 static
49 void _string_declaration_free(struct declaration *declaration)
50 {
51 struct declaration_string *string_declaration =
52 container_of(declaration, struct declaration_string, p);
53 g_free(string_declaration);
54 }
55
56 struct declaration_string *string_declaration_new(const char *name)
57 {
58 struct declaration_string *string_declaration;
59
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;
70 }
71
72 static
73 struct definition *
74 _string_definition_new(struct declaration *declaration,
75 struct definition_scope *parent_scope)
76 {
77 struct declaration_string *string_declaration =
78 container_of(declaration, struct declaration_string, p);
79 struct definition_string *string;
80
81 string = g_new(struct definition_string, 1);
82 declaration_ref(&string_declaration->p);
83 string->p.declaration = declaration;
84 string->declaration = string_declaration;
85 string->p.ref = 1;
86 string->value = NULL;
87 return &string->p;
88 }
89
90 static
91 void _string_definition_free(struct definition *definition)
92 {
93 struct definition_string *string =
94 container_of(definition, struct definition_string, p);
95
96 declaration_unref(string->p.declaration);
97 g_free(string->value);
98 g_free(string);
99 }
This page took 0.031533 seconds and 5 git commands to generate.