Commit | Line | Data |
---|---|---|
6dc2ca62 MD |
1 | /* |
2 | * Common Trace Format | |
3 | * | |
4 | * Strings read/write functions. | |
5 | * | |
ccd7e1c8 | 6 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
6dc2ca62 | 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: | |
de0ba614 | 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. | |
6dc2ca62 MD |
17 | */ |
18 | ||
d79865b9 | 19 | #include <babeltrace/ctf/types.h> |
a3dbc794 | 20 | #include <limits.h> /* C99 limits */ |
6dc2ca62 MD |
21 | #include <string.h> |
22 | ||
46322b33 | 23 | void ctf_string_copy(struct stream_pos *pdest, struct stream_pos *psrc, |
f6625916 | 24 | const struct declaration_string *string_declaration) |
6dc2ca62 | 25 | { |
46322b33 MD |
26 | struct ctf_stream_pos *dest = ctf_pos(pdest); |
27 | struct ctf_stream_pos *src = ctf_pos(psrc); | |
bed864a7 | 28 | size_t len; |
47e0f2e2 | 29 | char *destaddr, *srcaddr; |
6dc2ca62 | 30 | |
46322b33 MD |
31 | ctf_align_pos(src, string_declaration->p.alignment); |
32 | srcaddr = ctf_get_pos_addr(src); | |
bed864a7 | 33 | len = strlen(srcaddr) + 1; |
a52d7f6a | 34 | if (dest->dummy) |
6dc2ca62 | 35 | goto end; |
46322b33 MD |
36 | ctf_align_pos(dest, string_declaration->p.alignment); |
37 | destaddr = ctf_get_pos_addr(dest); | |
a52d7f6a MD |
38 | strcpy(destaddr, srcaddr); |
39 | end: | |
46322b33 MD |
40 | ctf_move_pos(dest, len); |
41 | ctf_move_pos(src, len); | |
a52d7f6a MD |
42 | } |
43 | ||
46322b33 | 44 | void ctf_string_read(char **dest, struct stream_pos *psrc, |
f6625916 | 45 | const struct declaration_string *string_declaration) |
a52d7f6a | 46 | { |
46322b33 | 47 | struct ctf_stream_pos *src = ctf_pos(psrc); |
a52d7f6a | 48 | size_t len; |
47e0f2e2 | 49 | char *srcaddr; |
a52d7f6a | 50 | |
46322b33 MD |
51 | ctf_align_pos(src, string_declaration->p.alignment); |
52 | srcaddr = ctf_get_pos_addr(src); | |
a52d7f6a | 53 | len = strlen(srcaddr) + 1; |
a52d7f6a | 54 | *dest = g_realloc(*dest, len); |
4c8bfb7e | 55 | strcpy(*dest, srcaddr); |
46322b33 | 56 | ctf_move_pos(src, len); |
6dc2ca62 | 57 | } |
a52d7f6a | 58 | |
46322b33 | 59 | void ctf_string_write(struct stream_pos *pdest, const char *src, |
f6625916 | 60 | const struct declaration_string *string_declaration) |
a52d7f6a | 61 | { |
46322b33 | 62 | struct ctf_stream_pos *dest = ctf_pos(pdest); |
a52d7f6a | 63 | size_t len; |
47e0f2e2 | 64 | char *destaddr; |
a52d7f6a | 65 | |
46322b33 | 66 | ctf_align_pos(dest, string_declaration->p.alignment); |
a52d7f6a MD |
67 | len = strlen(src) + 1; |
68 | if (dest->dummy) | |
69 | goto end; | |
46322b33 | 70 | destaddr = ctf_get_pos_addr(dest); |
a52d7f6a MD |
71 | strcpy(destaddr, src); |
72 | end: | |
46322b33 | 73 | ctf_move_pos(dest, len); |
a52d7f6a MD |
74 | } |
75 | ||
47e0f2e2 | 76 | void ctf_string_free_temp(char *string) |
a52d7f6a MD |
77 | { |
78 | g_free(string); | |
79 | } |