Repository navigation
Expand file tree
/
Copy pathtoml_parser.c
More file actions
188 lines (153 loc) · 4.45 KB
/
Copy pathtoml_parser.c
File metadata and controls
188 lines (153 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// SPDX-License-Identifier: GPL-3.0-only
/*
* Copyright (c) 2023 Dengfeng Liu <liudf0716@gmail.com>
*
* TOML parser wrapper using tomlc17 library.
* Maps the xfrpc-style flat API (toml_find_array_section/toml_get) onto
* the tomlc17 tree-based API (toml_seek/toml_get).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <inttypes.h>
#include "vendor/tomlc17/tomlc17.h"
#define TOML_PARSER_INTERNAL
#include "toml_parser.h"
#include "debug.h"
/* ---- Internal document structure ---- */
struct toml_doc {
toml_result_t result;
};
/* ---- Parse a TOML file ---- */
int xfrpc_toml_parse_file(const char *path, struct toml_doc **out_doc)
{
struct toml_doc *doc = calloc(1, sizeof(struct toml_doc));
if (!doc) {
debug(LOG_ERR, "TOML: out of memory");
return -1;
}
doc->result = toml_parse_file_ex(path);
if (!doc->result.ok) {
debug(LOG_ERR, "TOML: parse error: %s", doc->result.errmsg);
free(doc);
return -1;
}
*out_doc = doc;
return 0;
}
/* ---- Free document ---- */
void xfrpc_toml_doc_free(struct toml_doc *doc)
{
if (!doc)
return;
toml_free(doc->result);
free(doc);
}
/* ---- Get root table from doc ---- */
static toml_datum_t get_root(struct toml_doc *doc)
{
return doc->result.toptab;
}
/* ---- Find array-of-tables section ---- */
void *xfrpc_toml_find_array_section(struct toml_doc *doc, const char *prefix, int index)
{
if (!doc)
return NULL;
toml_datum_t root = get_root(doc);
/* index == -1 means return the root table itself */
if (index < 0) {
/* Return a pointer to the root datum (persistent in doc) */
static __thread toml_datum_t tls_slot;
tls_slot = root;
return &tls_slot;
}
/* Look up the array by prefix */
toml_datum_t arr;
if (prefix && *prefix) {
arr = toml_get(root, prefix);
} else {
arr = root;
}
if (arr.type != TOML_ARRAY)
return NULL;
if (index >= arr.u.arr.size)
return NULL;
/* Return pointer to the element (persistent in doc) */
return &arr.u.arr.elem[index];
}
/* ---- Count array-of-tables sections ---- */
int xfrpc_toml_count_array_sections(struct toml_doc *doc, const char *prefix)
{
if (!doc || !prefix)
return 0;
toml_datum_t root = get_root(doc);
toml_datum_t arr = toml_get(root, prefix);
if (arr.type != TOML_ARRAY)
return 0;
return arr.u.arr.size;
}
/* ---- Helper: convert datum to string in doc's buffer ---- */
/* ---- Get value from section by key ---- */
const char *xfrpc_toml_get(void *sec, const char *key)
{
if (!sec || !key)
return NULL;
toml_datum_t *table = (toml_datum_t *)sec;
/* Use toml_seek for dotted key paths (e.g. "transport.tls.enable") */
toml_datum_t val = toml_seek(*table, key);
if (val.type == TOML_UNKNOWN)
return NULL;
/* Find the document that owns this datum to use its scratch buffer.
* We store a doc pointer at a known offset. Since toml_datum_t is
* embedded in the doc's result tree, we can't easily get back to doc.
* Instead, use a thread-local static buffer. */
static __thread char tls_buf[1024];
switch (val.type) {
case TOML_STRING:
return val.u.s;
case TOML_INT64:
snprintf(tls_buf, sizeof(tls_buf), "%" PRId64, val.u.int64);
return tls_buf;
case TOML_BOOLEAN:
return val.u.boolean ? "1" : "0";
case TOML_FP64:
snprintf(tls_buf, sizeof(tls_buf), "%g", val.u.fp64);
return tls_buf;
case TOML_ARRAY: {
int pos = 0;
for (int i = 0; i < val.u.arr.size && pos < (int)sizeof(tls_buf) - 4; i++) {
toml_datum_t elem = val.u.arr.elem[i];
if (i > 0)
pos += snprintf(tls_buf + pos, sizeof(tls_buf) - pos, ", ");
if (elem.type == TOML_STRING)
pos += snprintf(tls_buf + pos, sizeof(tls_buf) - pos, "%s", elem.u.s);
else if (elem.type == TOML_INT64)
pos += snprintf(tls_buf + pos, sizeof(tls_buf) - pos, "%" PRId64, elem.u.int64);
}
return tls_buf;
}
default:
return NULL;
}
}
const char *xfrpc_toml_get_table_pairs(void *sec, const char *path)
{
if (!sec || !path)
return NULL;
toml_datum_t *table = (toml_datum_t *)sec;
toml_datum_t val = toml_seek(*table, path);
if (val.type != TOML_TABLE || val.u.tab.size == 0)
return NULL;
static __thread char tls_pairs[2048];
int pos = 0;
for (int i = 0; i < val.u.tab.size && pos < (int)sizeof(tls_pairs) - 4; i++) {
if (val.u.tab.value[i].type == TOML_STRING) {
if (pos > 0)
pos += snprintf(tls_pairs + pos, sizeof(tls_pairs) - pos, ",");
pos += snprintf(tls_pairs + pos, sizeof(tls_pairs) - pos, "%s=%s",
val.u.tab.key[i], val.u.tab.value[i].u.s);
}
}
return pos > 0 ? tls_pairs : NULL;
}