| 1 | /* |
| 2 | * string.c |
| 3 | * |
| 4 | * BabelTrace - String Type Converter |
| 5 | * |
| 6 | * Copyright 2010 - 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 | void string_copy(struct stream_pos *dest, const struct format *fdest, |
| 24 | struct stream_pos *src, const struct format *fsrc, |
| 25 | const struct type_class *type_class) |
| 26 | { |
| 27 | struct type_class_string *string_class = |
| 28 | container_of(type_class, struct type_class_string, p); |
| 29 | |
| 30 | if (fsrc->string_copy == fdest->string_copy) { |
| 31 | fsrc->string_copy(dest, src, string_class); |
| 32 | } else { |
| 33 | char *tmp = NULL; |
| 34 | |
| 35 | fsrc->string_read(&tmp, src, string_class); |
| 36 | fdest->string_write(dest, tmp, string_class); |
| 37 | fsrc->string_free_temp(tmp); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | void string_type_free(struct type_class_string *string_class) |
| 42 | { |
| 43 | g_free(string_class); |
| 44 | } |
| 45 | |
| 46 | static void _string_type_free(struct type_class *type_class) |
| 47 | { |
| 48 | struct type_class_string *string_class = |
| 49 | container_of(type_class, struct type_class_string, p); |
| 50 | string_type_free(string_class); |
| 51 | } |
| 52 | |
| 53 | struct type_class_string *string_type_new(const char *name) |
| 54 | { |
| 55 | struct type_class_string *string_class; |
| 56 | int ret; |
| 57 | |
| 58 | string_class = g_new(struct type_class_string, 1); |
| 59 | string_class->p.name = g_quark_from_string(name); |
| 60 | string_class->p.alignment = CHAR_BIT; |
| 61 | string_class->p.copy = string_copy; |
| 62 | string_class->p.free = _string_type_free; |
| 63 | string_class->p.ref = 1; |
| 64 | if (string_class->p.name) { |
| 65 | ret = register_type(&string_class->p); |
| 66 | if (ret) { |
| 67 | g_free(string_class); |
| 68 | return NULL; |
| 69 | } |
| 70 | } |
| 71 | return string_class; |
| 72 | } |