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