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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/limits.h>
#include <toml/toml.h>
#include <yyjson/yyjson.h>
#include <stellar/utils.h>
#include <stellar/scanner.h>
#include "scanner_toml.h"
static toml_table_t *toml_open(struct logger *logger, const char *toml_path)
{
FILE *fp=fopen(toml_path, "r");
if (NULL==fp)
{
STELLAR_LOG_FATAL(logger, SCANNER_MODULE_NAME, "toml_bool_get can't open config file: %s", toml_path);
return NULL;
}
char errbuf[256]={0};
toml_table_t *root=toml_parse_file(fp, errbuf, sizeof(errbuf));
fclose(fp);
return root;
}
static void toml_close(struct toml_table_t *root)
{
toml_free(root);
}
void toml_bool_get(struct logger *logger, const char *toml_path, const char *table_key, const char *key, bool *value)
{
toml_table_t *root=toml_open(logger, toml_path);
if(NULL==root)
{
return ;
}
toml_table_t *table=toml_table_in(root, table_key);
if(NULL==table)
{
STELLAR_LOG_FATAL(logger, SCANNER_MODULE_NAME, "toml_bool_get can't find key: [%s] in config file: %s", table_key, toml_path);
toml_close(root);
return ;
}
toml_datum_t val=toml_bool_in(table, key);
if(val.ok>0)
{
*value=val.u.b;
}
else
{
*value=false;
}
toml_close(root);
}
void toml_int_get(struct logger *logger, const char *toml_path, const char *table_key, const char *key, int *value)
{
toml_table_t *root=toml_open(logger, toml_path);
if(NULL==root)
{
return ;
}
toml_table_t *table=toml_table_in(root, table_key);
if(NULL==table)
{
STELLAR_LOG_FATAL(logger, SCANNER_MODULE_NAME, "toml_int_get can't find key: [%s] in config file: %s", table_key, toml_path);
toml_close(root);
return ;
}
toml_datum_t val=toml_int_in(table, key);
if(val.ok>0)
{
*value=val.u.i;
}
else
{
*value=0;
}
toml_close(root);
}
void toml_string_get(struct logger *logger, const char *toml_path, const char *table_key, const char *key, char *value, size_t value_len)
{
toml_table_t *root=toml_open(logger, toml_path);
if(NULL==root)
{
return ;
}
toml_table_t *table=toml_table_in(root, table_key);
if(NULL==table)
{
STELLAR_LOG_FATAL(logger, SCANNER_MODULE_NAME, "toml_string_get can't find key: [%s] in config file: %s", table_key, toml_path);
toml_close(root);
return ;
}
toml_datum_t val=toml_string_in(table, key);
if(val.ok>0)
{
strncpy(value, val.u.s, MIN(value_len-1, strlen(val.u.s)));
free(val.u.s);
}
else
{
strncpy(value, "", value_len);
}
toml_close(root);
}
|