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 MD |
23 | static |
24 | struct type_string *_string_type_new(struct type_class *type_class, | |
25 | struct declaration_scope *parent_scope); | |
26 | static | |
27 | void _string_type_free(struct type *type); | |
28 | ||
bed864a7 MD |
29 | void string_copy(struct stream_pos *dest, const struct format *fdest, |
30 | struct stream_pos *src, const struct format *fsrc, | |
c054553d | 31 | struct type *type) |
bed864a7 | 32 | { |
c054553d MD |
33 | struct type_string *string = container_of(type, struct type_string, p); |
34 | struct type_class_string *string_class = string->_class; | |
bed864a7 MD |
35 | |
36 | if (fsrc->string_copy == fdest->string_copy) { | |
37 | fsrc->string_copy(dest, src, string_class); | |
38 | } else { | |
47e0f2e2 | 39 | char *tmp = NULL; |
a52d7f6a MD |
40 | |
41 | fsrc->string_read(&tmp, src, string_class); | |
42 | fdest->string_write(dest, tmp, string_class); | |
43 | fsrc->string_free_temp(tmp); | |
bed864a7 MD |
44 | } |
45 | } | |
46 | ||
c054553d MD |
47 | static |
48 | void _string_type_class_free(struct type_class_string *string_class) | |
bed864a7 MD |
49 | { |
50 | struct type_class_string *string_class = | |
51 | container_of(type_class, struct type_class_string, p); | |
c054553d | 52 | g_free(string_class); |
bed864a7 MD |
53 | } |
54 | ||
c054553d MD |
55 | struct type_class_string * |
56 | string_type_class_new(const char *name) | |
bed864a7 MD |
57 | { |
58 | struct type_class_string *string_class; | |
59 | int ret; | |
60 | ||
61 | string_class = g_new(struct type_class_string, 1); | |
62 | string_class->p.name = g_quark_from_string(name); | |
63 | string_class->p.alignment = CHAR_BIT; | |
64 | string_class->p.copy = string_copy; | |
c054553d MD |
65 | string_class->p.class_free = _string_type_class_free; |
66 | string_class->p.type_new = _string_type_new; | |
67 | string_class->p.type_free = _string_type_free; | |
4c8bfb7e | 68 | string_class->p.ref = 1; |
bed864a7 | 69 | if (string_class->p.name) { |
be85c1c7 | 70 | ret = register_type(&string_class->p); |
bed864a7 MD |
71 | if (ret) { |
72 | g_free(string_class); | |
73 | return NULL; | |
74 | } | |
75 | } | |
76 | return string_class; | |
77 | } | |
c054553d MD |
78 | |
79 | static | |
80 | struct type_string *_string_type_new(struct type_class *type_class, | |
81 | struct declaration_scope *parent_scope) | |
82 | { | |
83 | struct type_class_string *string_class = | |
84 | container_of(type_class, struct type_class_string, p); | |
85 | struct type_string *string; | |
86 | ||
87 | string = g_new(struct type_string, 1); | |
88 | type_class_ref(&string_class->p); | |
89 | string->p._class = string_class; | |
90 | string->p.ref = 1; | |
91 | string->value = NULL; | |
92 | return &string->p; | |
93 | } | |
94 | ||
95 | static | |
96 | void _string_type_free(struct type *type) | |
97 | { | |
98 | struct type_string *string = | |
99 | container_of(type, struct type_string, p); | |
100 | ||
101 | type_class_unref(string->p._class); | |
102 | g_free(string->value); | |
103 | g_free(string); | |
104 | } |