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