| 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.name = g_quark_from_string(name); |
| 62 | string_type->p.alignment = CHAR_BIT; |
| 63 | string_type->p.copy = string_copy; |
| 64 | string_type->p.type_free = _string_type_free; |
| 65 | string_type->p.declaration_new = _string_declaration_new; |
| 66 | string_type->p.declaration_free = _string_declaration_free; |
| 67 | string_type->p.ref = 1; |
| 68 | return string_type; |
| 69 | } |
| 70 | |
| 71 | static |
| 72 | struct declaration * |
| 73 | _string_declaration_new(struct type *type, |
| 74 | struct declaration_scope *parent_scope) |
| 75 | { |
| 76 | struct type_string *string_type = |
| 77 | container_of(type, struct type_string, p); |
| 78 | struct declaration_string *string; |
| 79 | |
| 80 | string = g_new(struct declaration_string, 1); |
| 81 | type_ref(&string_type->p); |
| 82 | string->p.type = type; |
| 83 | string->type = string_type; |
| 84 | string->p.ref = 1; |
| 85 | string->value = NULL; |
| 86 | return &string->p; |
| 87 | } |
| 88 | |
| 89 | static |
| 90 | void _string_declaration_free(struct declaration *declaration) |
| 91 | { |
| 92 | struct declaration_string *string = |
| 93 | container_of(declaration, struct declaration_string, p); |
| 94 | |
| 95 | type_unref(string->p.type); |
| 96 | g_free(string->value); |
| 97 | g_free(string); |
| 98 | } |