forked from viabtc/viabtc_exchange_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
me_config.c
167 lines (150 loc) · 5.28 KB
/
me_config.c
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
/*
* Description:
* History: [email protected], 2017/03/16, create
*/
# include "me_config.h"
struct settings settings;
static int load_assets(json_t *root, const char *key)
{
json_t *node = json_object_get(root, key);
if (!node || !json_is_array(node)) {
return -__LINE__;
}
settings.asset_num = json_array_size(node);
settings.assets = malloc(sizeof(struct asset) * settings.asset_num);
for (size_t i = 0; i < settings.asset_num; ++i) {
json_t *row = json_array_get(node, i);
if (!json_is_object(row))
return -__LINE__;
ERR_RET_LN(read_cfg_str(row, "name", &settings.assets[i].name, NULL));
ERR_RET_LN(read_cfg_int(row, "prec_save", &settings.assets[i].prec_save, true, 0));
ERR_RET_LN(read_cfg_int(row, "prec_show", &settings.assets[i].prec_show, false, settings.assets[i].prec_save));
if (strlen(settings.assets[i].name) > ASSET_NAME_MAX_LEN)
return -__LINE__;
}
return 0;
}
static int load_markets(json_t *root, const char *key)
{
json_t *node = json_object_get(root, key);
if (!node || !json_is_array(node)) {
return -__LINE__;
}
settings.market_num = json_array_size(node);
settings.markets = malloc(sizeof(struct market) * settings.market_num);
for (size_t i = 0; i < settings.market_num; ++i) {
json_t *row = json_array_get(node, i);
if (!json_is_object(row))
return -__LINE__;
ERR_RET_LN(read_cfg_str(row, "name", &settings.markets[i].name, NULL));
ERR_RET_LN(read_cfg_int(row, "fee_prec", &settings.markets[i].fee_prec, false, 4));
ERR_RET_LN(read_cfg_mpd(row, "min_amount", &settings.markets[i].min_amount, "0.01"));
json_t *stock = json_object_get(row, "stock");
if (!stock || !json_is_object(stock))
return -__LINE__;
ERR_RET_LN(read_cfg_str(stock, "name", &settings.markets[i].stock, NULL));
ERR_RET_LN(read_cfg_int(stock, "prec", &settings.markets[i].stock_prec, true, 0));
json_t *money = json_object_get(row, "money");
if (!money || !json_is_object(money))
return -__LINE__;
ERR_RET_LN(read_cfg_str(money, "name", &settings.markets[i].money, NULL));
ERR_RET_LN(read_cfg_int(money, "prec", &settings.markets[i].money_prec, true, 0));
}
return 0;
}
static int read_config_from_json(json_t *root)
{
int ret;
ret = read_cfg_bool(root, "debug", &settings.debug, false, false);
if (ret < 0) {
printf("read debug config fail: %d\n", ret);
return -__LINE__;
}
ret = load_cfg_process(root, "process", &settings.process);
if (ret < 0) {
printf("load process config fail: %d\n", ret);
return -__LINE__;
}
ret = load_cfg_log(root, "log", &settings.log);
if (ret < 0) {
printf("load log config fail: %d\n", ret);
return -__LINE__;
}
ret = load_cfg_alert(root, "alert", &settings.alert);
if (ret < 0) {
printf("load alert config fail: %d\n", ret);
return -__LINE__;
}
ret = load_cfg_rpc_svr(root, "svr", &settings.svr);
if (ret < 0) {
printf("load svr config fail: %d\n", ret);
return -__LINE__;
}
ret = load_cfg_cli_svr(root, "cli", &settings.cli);
if (ret < 0) {
printf("load cli config fail: %d\n", ret);
return -__LINE__;
}
ret = load_cfg_mysql(root, "db_log", &settings.db_log);
if (ret < 0) {
printf("load log db config fail: %d\n", ret);
return -__LINE__;
}
ret = load_cfg_mysql(root, "db_history", &settings.db_history);
if (ret < 0) {
printf("load history db config fail: %d\n", ret);
return -__LINE__;
}
ret = load_assets(root, "assets");
if (ret < 0) {
printf("load assets config fail: %d\n", ret);
return -__LINE__;
}
ret = load_markets(root, "markets");
if (ret < 0) {
printf("load markets config fail: %d\n", ret);
return -__LINE__;
}
ret = read_cfg_str(root, "brokers", &settings.brokers, NULL);
if (ret < 0) {
printf("load brokers fail: %d\n", ret);
return -__LINE__;
}
ret = read_cfg_int(root, "slice_interval", &settings.slice_interval, false, 86400);
if (ret < 0) {
printf("load slice_interval fail: %d", ret);
return -__LINE__;
}
ret = read_cfg_int(root, "slice_keeptime", &settings.slice_keeptime, false, 86400 * 3);
if (ret < 0) {
printf("load slice_keeptime fail: %d", ret);
return -__LINE__;
}
ret = read_cfg_int(root, "history_thread", &settings.history_thread, false, 10);
if (ret < 0) {
printf("load history_thread fail: %d", ret);
return -__LINE__;
}
ERR_RET_LN(read_cfg_real(root, "cache_timeout", &settings.cache_timeout, false, 0.45));
return 0;
}
int init_config(const char *path)
{
json_error_t error;
json_t *root = json_load_file(path, 0, &error);
if (root == NULL) {
printf("json_load_file from: %s fail: %s in line: %d\n", path, error.text, error.line);
return -__LINE__;
}
if (!json_is_object(root)) {
json_decref(root);
return -__LINE__;
}
int ret = read_config_from_json(root);
if (ret < 0) {
json_decref(root);
return ret;
}
json_decref(root);
return 0;
}