Start packet mmap work
[babeltrace.git] / types / string.c
CommitLineData
bed864a7 1/*
ccd7e1c8 2 * string.c
bed864a7 3 *
ccd7e1c8 4 * BabelTrace - String Type Converter
bed864a7 5 *
c054553d 6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
bed864a7 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:
bed864a7 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.
bed864a7
MD
17 */
18
19#include <babeltrace/compiler.h>
20#include <babeltrace/align.h>
4c8bfb7e 21#include <babeltrace/format.h>
bed864a7 22
c054553d 23static
f6625916 24struct definition *_string_definition_new(struct declaration *declaration,
05c749e5
MD
25 struct definition_scope *parent_scope,
26 GQuark field_name, int index);
c054553d 27static
e1151715 28void _string_definition_free(struct definition *definition);
c054553d 29
bed864a7
MD
30void string_copy(struct stream_pos *dest, const struct format *fdest,
31 struct stream_pos *src, const struct format *fsrc,
e1151715 32 struct definition *definition)
bed864a7 33{
e1151715
MD
34 struct definition_string *string =
35 container_of(definition, struct definition_string, p);
f6625916 36 struct declaration_string *string_declaration = string->declaration;
bed864a7 37
0f980a35 38 if (fdest && (fsrc->string_copy == fdest->string_copy)) {
f6625916 39 fsrc->string_copy(dest, src, string_declaration);
bed864a7 40 } else {
47e0f2e2 41 char *tmp = NULL;
a52d7f6a 42
f6625916 43 fsrc->string_read(&tmp, src, string_declaration);
0f980a35
MD
44 if (fdest)
45 fdest->string_write(dest, tmp, string_declaration);
a52d7f6a 46 fsrc->string_free_temp(tmp);
bed864a7
MD
47 }
48}
49
c054553d 50static
f6625916 51void _string_declaration_free(struct declaration *declaration)
bed864a7 52{
f6625916
MD
53 struct declaration_string *string_declaration =
54 container_of(declaration, struct declaration_string, p);
55 g_free(string_declaration);
bed864a7
MD
56}
57
ab4cf058
MD
58struct declaration_string *
59 string_declaration_new(enum ctf_string_encoding encoding)
bed864a7 60{
f6625916 61 struct declaration_string *string_declaration;
bed864a7 62
f6625916
MD
63 string_declaration = g_new(struct declaration_string, 1);
64 string_declaration->p.id = CTF_TYPE_STRING;
f6625916
MD
65 string_declaration->p.alignment = CHAR_BIT;
66 string_declaration->p.copy = string_copy;
67 string_declaration->p.declaration_free = _string_declaration_free;
68 string_declaration->p.definition_new = _string_definition_new;
69 string_declaration->p.definition_free = _string_definition_free;
70 string_declaration->p.ref = 1;
ab4cf058 71 string_declaration->encoding = encoding;
f6625916 72 return string_declaration;
bed864a7 73}
c054553d
MD
74
75static
e1151715 76struct definition *
f6625916 77 _string_definition_new(struct declaration *declaration,
05c749e5
MD
78 struct definition_scope *parent_scope,
79 GQuark field_name, int index)
c054553d 80{
f6625916
MD
81 struct declaration_string *string_declaration =
82 container_of(declaration, struct declaration_string, p);
e1151715 83 struct definition_string *string;
c054553d 84
e1151715 85 string = g_new(struct definition_string, 1);
f6625916
MD
86 declaration_ref(&string_declaration->p);
87 string->p.declaration = declaration;
88 string->declaration = string_declaration;
c054553d 89 string->p.ref = 1;
05c749e5 90 string->p.index = index;
c054553d
MD
91 string->value = NULL;
92 return &string->p;
93}
94
95static
e1151715 96void _string_definition_free(struct definition *definition)
c054553d 97{
e1151715
MD
98 struct definition_string *string =
99 container_of(definition, struct definition_string, p);
c054553d 100
f6625916 101 declaration_unref(string->p.declaration);
c054553d
MD
102 g_free(string->value);
103 g_free(string);
104}
This page took 0.027491 seconds and 4 git commands to generate.