Rename: type_class, type -> type, declaration
[babeltrace.git] / formats / ctf / types / enum.c
CommitLineData
6dc2ca62
MD
1/*
2 * Common Trace Format
3 *
4 * Enumeration mapping strings (quarks) from/to integers.
5 *
c054553d 6 * Copyright 2010, 2011 - 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>
6dc2ca62
MD
20#include <stdint.h>
21#include <glib.h>
22
c054553d
MD
23/*
24 * The caller should unref the GArray.
25 */
47e0f2e2 26GArray *ctf_enum_read(struct stream_pos *pos,
e19c3d69 27 const struct type_enum *src)
448d3cc7 28{
e19c3d69 29 const struct type_integer *integer_type = src->integer_type;
448d3cc7 30
e19c3d69 31 if (!integer_type->signedness) {
448d3cc7
MD
32 uint64_t v;
33
e19c3d69 34 v = ctf_uint_read(pos, integer_type);
47e0f2e2 35 return enum_uint_to_quark_set(src, v);
448d3cc7
MD
36 } else {
37 int64_t v;
38
e19c3d69 39 v = ctf_int_read(pos, integer_type);
47e0f2e2 40 return enum_int_to_quark_set(src, v);
448d3cc7
MD
41 }
42}
43
47e0f2e2
MD
44/*
45 * Arbitrarily choose the start of the first matching range.
46 */
4c8bfb7e 47void ctf_enum_write(struct stream_pos *pos,
e19c3d69 48 const struct type_enum *dest,
47e0f2e2 49 GQuark q)
448d3cc7 50{
e19c3d69 51 const struct type_integer *integer_type = dest->integer_type;
47e0f2e2 52 GArray *array;
448d3cc7 53
47e0f2e2
MD
54 array = enum_quark_to_range_set(dest, q);
55 assert(array);
448d3cc7 56
e19c3d69 57 if (!integer_type->signedness) {
47e0f2e2 58 uint64_t v = g_array_index(array, struct enum_range, 0).start._unsigned;
e19c3d69 59 ctf_uint_write(pos, integer_type, v);
448d3cc7 60 } else {
47e0f2e2 61 int64_t v = g_array_index(array, struct enum_range, 0).start._unsigned;
e19c3d69 62 ctf_int_write(pos, integer_type, v);
448d3cc7 63 }
6dc2ca62 64}
This page took 0.026036 seconds and 4 git commands to generate.