Move to kernel style SPDX license identifiers
[babeltrace.git] / src / ctf-writer / utils.c
CommitLineData
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
23static
24const 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",
28 "unsigned", "variant", "void" "_Bool", "_Complex", "_Imaginary"};
29
30static GHashTable *reserved_keywords_set;
31static int init_done;
32
33static
34void 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
57static __attribute__((destructor))
58void trace_finalize(void)
59{
60 if (reserved_keywords_set) {
61 g_hash_table_destroy(reserved_keywords_set);
62 }
63}
64
00409097 65bt_ctf_bool bt_ctf_identifier_is_valid(const char *identifier)
16ca5ff0 66{
00409097 67 bt_ctf_bool is_valid = BT_CTF_TRUE;
16ca5ff0
PP
68 char *string = NULL;
69 char *save_ptr, *token;
70
71 if (!identifier) {
ef267d12 72 BT_LOGT_STR("Invalid parameter: input string is NULL.");
00409097 73 is_valid = BT_CTF_FALSE;
16ca5ff0
PP
74 goto end;
75 }
76
77 try_init_reserved_keywords();
78
79 if (identifier[0] == '\0') {
00409097 80 is_valid = BT_CTF_FALSE;
16ca5ff0
PP
81 goto end;
82 }
83
84 string = strdup(identifier);
85 if (!string) {
86 BT_LOGE("strdup() failed.");
00409097 87 is_valid = BT_CTF_FALSE;
16ca5ff0
PP
88 goto end;
89 }
90
91 token = strtok_r(string, " ", &save_ptr);
92 while (token) {
93 if (g_hash_table_lookup_extended(reserved_keywords_set,
94 GINT_TO_POINTER(g_quark_from_string(token)),
95 NULL, NULL)) {
00409097 96 is_valid = BT_CTF_FALSE;
16ca5ff0
PP
97 goto end;
98 }
99
100 token = strtok_r(NULL, " ", &save_ptr);
101 }
102end:
103 free(string);
104 return is_valid;
105}
This page took 0.062454 seconds and 4 git commands to generate.