11529dd9bb9cf001dc87ecb460199f044bcc0bd8
[babeltrace.git] / formats / ctf-text / types / enum.c
1 /*
2 * Common Trace Format
3 *
4 * Enumeration mapping strings (quarks) from/to integers.
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 */
20
21 #include <babeltrace/ctf-text/types.h>
22 #include <stdio.h>
23 #include <stdint.h>
24
25 int ctf_text_enum_write(struct stream_pos *ppos, struct definition *definition)
26 {
27 struct definition_enum *enum_definition =
28 container_of(definition, struct definition_enum, p);
29 struct definition_integer *integer_definition =
30 enum_definition->integer;
31 struct ctf_text_stream_pos *pos = ctf_text_pos(ppos);
32 GArray *qs;
33 int ret;
34 int field_nr_saved;
35
36 if (!print_field(definition))
37 return 0;
38
39 if (pos->dummy)
40 return 0;
41
42 if (pos->field_nr++ != 0)
43 fprintf(pos->fp, ",");
44 fprintf(pos->fp, " ");
45 if (pos->print_names)
46 fprintf(pos->fp, "%s = ",
47 rem_(g_quark_to_string(definition->name)));
48
49 field_nr_saved = pos->field_nr;
50 pos->field_nr = 0;
51 fprintf(pos->fp, "(");
52 pos->depth++;
53 qs = enum_definition->value;
54
55 if (qs) {
56 int i;
57
58 for (i = 0; i < qs->len; i++) {
59 GQuark q = g_array_index(qs, GQuark, i);
60 const char *str = g_quark_to_string(q);
61
62 assert(str);
63 if (pos->field_nr++ != 0)
64 fprintf(pos->fp, ",");
65 fprintf(pos->fp, " ");
66 fprintf(pos->fp, "%s", str);
67 }
68 } else {
69 fprintf(pos->fp, " <unknown>");
70 }
71
72 pos->field_nr = 0;
73 fprintf(pos->fp, " :");
74 ret = generic_rw(ppos, &integer_definition->p);
75
76 pos->depth--;
77 fprintf(pos->fp, " )");
78 pos->field_nr = field_nr_saved;
79 return ret;
80 }
This page took 0.050164 seconds and 3 git commands to generate.