| 1 | /* |
| 2 | * BabelTrace - String Type Converter |
| 3 | * |
| 4 | * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <babeltrace/compiler.h> |
| 22 | #include <babeltrace/align.h> |
| 23 | #include <babeltrace/types.h> |
| 24 | |
| 25 | void string_copy(struct stream_pos *dest, const struct format *fdest, |
| 26 | struct stream_pos *src, const struct format *fsrc, |
| 27 | const struct type_class *type_class) |
| 28 | { |
| 29 | struct type_class_string *string_class = |
| 30 | container_of(type_class, struct type_class_string, p); |
| 31 | |
| 32 | if (fsrc->string_copy == fdest->string_copy) { |
| 33 | fsrc->string_copy(dest, src, string_class); |
| 34 | } else { |
| 35 | unsigned char *tmp = NULL; |
| 36 | |
| 37 | fsrc->string_read(&tmp, src, string_class); |
| 38 | fdest->string_write(dest, tmp, string_class); |
| 39 | fsrc->string_free_temp(tmp); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | void string_type_free(struct type_class_string *string_class) |
| 44 | { |
| 45 | g_free(string_class); |
| 46 | } |
| 47 | |
| 48 | static void _string_type_free(struct type_class *type_class) |
| 49 | { |
| 50 | struct type_class_string *string_class = |
| 51 | container_of(type_class, struct type_class_string, p); |
| 52 | string_type_free(string_class); |
| 53 | } |
| 54 | |
| 55 | struct type_class_string *string_type_new(const char *name) |
| 56 | { |
| 57 | struct type_class_string *string_class; |
| 58 | int ret; |
| 59 | |
| 60 | string_class = g_new(struct type_class_string, 1); |
| 61 | string_class->p.name = g_quark_from_string(name); |
| 62 | string_class->p.alignment = CHAR_BIT; |
| 63 | string_class->p.copy = string_copy; |
| 64 | string_class->p.free = _string_type_free; |
| 65 | if (string_class->p.name) { |
| 66 | ret = ctf_register_type(&string_class->p); |
| 67 | if (ret) { |
| 68 | g_free(string_class); |
| 69 | return NULL; |
| 70 | } |
| 71 | } |
| 72 | return string_class; |
| 73 | } |