Commit | Line | Data |
---|---|---|
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 | 23 | static |
e19c3d69 MD |
24 | struct declaration *_string_declaration_new(struct type *type, |
25 | struct declaration_scope *parent_scope); | |
c054553d | 26 | static |
e19c3d69 | 27 | void _string_declaration_free(struct declaration *declaration); |
c054553d | 28 | |
bed864a7 MD |
29 | void 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 | 48 | static |
e19c3d69 | 49 | void _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 | 56 | struct type_string *string_type_new(const char *name) |
bed864a7 | 57 | { |
e19c3d69 | 58 | struct type_string *string_type; |
bed864a7 MD |
59 | int ret; |
60 | ||
e19c3d69 MD |
61 | string_type = g_new(struct type_string, 1); |
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 = _strin_declaration_free; | |
68 | string_type->p.ref = 1; | |
69 | if (string_type->p.name) { | |
70 | ret = register_type(&string_type->p); | |
bed864a7 | 71 | if (ret) { |
e19c3d69 | 72 | g_free(string_type); |
bed864a7 MD |
73 | return NULL; |
74 | } | |
75 | } | |
e19c3d69 | 76 | return string_type; |
bed864a7 | 77 | } |
c054553d MD |
78 | |
79 | static | |
e19c3d69 MD |
80 | struct declaration * |
81 | _string_declaration_new(struct type *type, | |
82 | struct declaration_scope *parent_scope) | |
c054553d | 83 | { |
e19c3d69 MD |
84 | struct type_string *string_type = |
85 | container_of(type, struct type_string, p); | |
86 | struct declaration_string *string; | |
c054553d | 87 | |
e19c3d69 MD |
88 | string = g_new(struct declaration_string, 1); |
89 | type_ref(&string_type->p); | |
90 | string->p.type = string_type; | |
c054553d MD |
91 | string->p.ref = 1; |
92 | string->value = NULL; | |
93 | return &string->p; | |
94 | } | |
95 | ||
96 | static | |
e19c3d69 | 97 | void _string_declaration_free(struct declaration *declaration) |
c054553d | 98 | { |
e19c3d69 MD |
99 | struct declaration_string *string = |
100 | container_of(declaration, struct declaration_string, p); | |
c054553d | 101 | |
e19c3d69 | 102 | type_unref(string->p.type); |
c054553d MD |
103 | g_free(string->value); |
104 | g_free(string); | |
105 | } |