update gitignore
[babeltrace.git] / formats / ctf / types / string.c
CommitLineData
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
d11e9c49 23void ctf_string_read(struct stream_pos *ppos, struct definition *definition)
a52d7f6a 24{
d11e9c49
MD
25 struct definition_string *string_definition =
26 container_of(definition, struct definition_string, p);
27 const struct declaration_string *string_declaration =
28 string_definition->declaration;
29 struct ctf_stream_pos *pos = ctf_pos(ppos);
a52d7f6a 30 size_t len;
47e0f2e2 31 char *srcaddr;
a52d7f6a 32
d11e9c49
MD
33 ctf_align_pos(pos, string_declaration->p.alignment);
34 srcaddr = ctf_get_pos_addr(pos);
a52d7f6a 35 len = strlen(srcaddr) + 1;
d11e9c49
MD
36 if (string_definition->alloc_len < len) {
37 string_definition->value =
38 g_realloc(string_definition->value, len);
39 string_definition->alloc_len = len;
40 }
41 memcpy(string_definition->value, srcaddr, len);
42 string_definition->len = len;
43 ctf_move_pos(pos, len);
6dc2ca62 44}
a52d7f6a 45
d11e9c49
MD
46void ctf_string_write(struct stream_pos *ppos,
47 struct definition *definition)
a52d7f6a 48{
d11e9c49
MD
49 struct definition_string *string_definition =
50 container_of(definition, struct definition_string, p);
51 const struct declaration_string *string_declaration =
52 string_definition->declaration;
53 struct ctf_stream_pos *pos = ctf_pos(ppos);
a52d7f6a 54 size_t len;
47e0f2e2 55 char *destaddr;
a52d7f6a 56
d11e9c49
MD
57 ctf_align_pos(pos, string_declaration->p.alignment);
58 assert(string_definition->value != NULL);
59 len = string_definition->len;
60 if (pos->dummy)
a52d7f6a 61 goto end;
d11e9c49 62 destaddr = ctf_get_pos_addr(pos);
8563e754 63 memcpy(destaddr, string_definition->value, len);
a52d7f6a 64end:
d11e9c49 65 ctf_move_pos(pos, len);
a52d7f6a 66}
This page took 0.025643 seconds and 4 git commands to generate.