Commit | Line | Data |
---|---|---|
16ca5ff0 | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
16ca5ff0 PP |
3 | * |
4 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
5 | * | |
0235b0db | 6 | * Babeltrace CTF writer - Utilities |
16ca5ff0 PP |
7 | */ |
8 | ||
350ad6c1 | 9 | #define BT_LOG_TAG "CTF-WRITER/UTILS" |
67d2ce02 | 10 | #include "logging.h" |
16ca5ff0 | 11 | |
16ca5ff0 PP |
12 | #include <glib.h> |
13 | #include <stdlib.h> | |
14 | ||
217cf9d3 PP |
15 | #include <babeltrace2-ctf-writer/utils.h> |
16 | #include <babeltrace2-ctf-writer/object.h> | |
578e048b MJ |
17 | |
18 | #include "common/assert.h" | |
19 | ||
20 | #include "clock-class.h" | |
21 | #include "field-types.h" | |
22 | ||
16ca5ff0 PP |
23 | static |
24 | const char * const reserved_keywords_str[] = {"align", "callsite", | |
25 | "const", "char", "clock", "double", "enum", "env", "event", | |
26 | "floating_point", "float", "integer", "int", "long", "short", "signed", | |
27 | "stream", "string", "struct", "trace", "typealias", "typedef", | |
dc0e9f45 | 28 | "unsigned", "variant", "void", "_Bool", "_Complex", "_Imaginary"}; |
16ca5ff0 PP |
29 | |
30 | static GHashTable *reserved_keywords_set; | |
31 | static int init_done; | |
32 | ||
33 | static | |
34 | void try_init_reserved_keywords(void) | |
35 | { | |
36 | size_t i; | |
37 | const size_t reserved_keywords_count = | |
38 | sizeof(reserved_keywords_str) / sizeof(char *); | |
39 | ||
40 | if (reserved_keywords_set) { | |
41 | return; | |
42 | } | |
43 | ||
44 | reserved_keywords_set = g_hash_table_new(g_direct_hash, g_direct_equal); | |
98b15851 | 45 | BT_ASSERT_DBG(reserved_keywords_set); |
16ca5ff0 PP |
46 | |
47 | for (i = 0; i < reserved_keywords_count; i++) { | |
48 | gpointer quark = GINT_TO_POINTER(g_quark_from_string( | |
49 | reserved_keywords_str[i])); | |
50 | ||
51 | g_hash_table_insert(reserved_keywords_set, quark, quark); | |
52 | } | |
53 | ||
54 | init_done = 1; | |
55 | } | |
56 | ||
57 | static __attribute__((destructor)) | |
58 | void trace_finalize(void) | |
59 | { | |
60 | if (reserved_keywords_set) { | |
61 | g_hash_table_destroy(reserved_keywords_set); | |
62 | } | |
63 | } | |
64 | ||
1353b066 | 65 | BT_EXPORT |
00409097 | 66 | bt_ctf_bool bt_ctf_identifier_is_valid(const char *identifier) |
16ca5ff0 | 67 | { |
00409097 | 68 | bt_ctf_bool is_valid = BT_CTF_TRUE; |
16ca5ff0 PP |
69 | char *string = NULL; |
70 | char *save_ptr, *token; | |
71 | ||
72 | if (!identifier) { | |
ef267d12 | 73 | BT_LOGT_STR("Invalid parameter: input string is NULL."); |
00409097 | 74 | is_valid = BT_CTF_FALSE; |
16ca5ff0 PP |
75 | goto end; |
76 | } | |
77 | ||
78 | try_init_reserved_keywords(); | |
79 | ||
80 | if (identifier[0] == '\0') { | |
00409097 | 81 | is_valid = BT_CTF_FALSE; |
16ca5ff0 PP |
82 | goto end; |
83 | } | |
84 | ||
85 | string = strdup(identifier); | |
86 | if (!string) { | |
87 | BT_LOGE("strdup() failed."); | |
00409097 | 88 | is_valid = BT_CTF_FALSE; |
16ca5ff0 PP |
89 | goto end; |
90 | } | |
91 | ||
92 | token = strtok_r(string, " ", &save_ptr); | |
93 | while (token) { | |
94 | if (g_hash_table_lookup_extended(reserved_keywords_set, | |
95 | GINT_TO_POINTER(g_quark_from_string(token)), | |
96 | NULL, NULL)) { | |
00409097 | 97 | is_valid = BT_CTF_FALSE; |
16ca5ff0 PP |
98 | goto end; |
99 | } | |
100 | ||
101 | token = strtok_r(NULL, " ", &save_ptr); | |
102 | } | |
103 | end: | |
104 | free(string); | |
105 | return is_valid; | |
106 | } |