-
Notifications
You must be signed in to change notification settings - Fork 4
/
Config.cpp
executable file
·364 lines (339 loc) · 12.2 KB
/
Config.cpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include <stdio.h>
#include "Config.h"
#include "FileUtil.h"
#include "Loger.h"
#include "MT4ServerAPI.h"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Config::Config() : m_cfg(NULL), m_cfg_total(0), m_cfg_max(0) {
m_filename[0] = 0;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Config::~Config() {
m_sync.Lock();
if (m_cfg != NULL) {
delete[] m_cfg;
m_cfg = NULL;
}
m_cfg_total = m_cfg_max = 0;
m_sync.Unlock();
}
Config& Config::Instance() {
static Config _instance;
return _instance;
}
//+------------------------------------------------------------------+
//| Load config from file |
//+------------------------------------------------------------------+
void Config::Load(const char* filename) {
char tmp[MAX_PATH], *cp, *start;
FileUtil in;
PluginCfg cfg, *buf;
if (filename == NULL) return;
//--- copy file name
m_sync.Lock();
COPY_STR(m_filename, filename);
//--- open file
if (in.Open(m_filename, GENERIC_READ, OPEN_EXISTING)) {
while (in.GetNextLine(tmp, sizeof(tmp) - 1) > 0) {
if (tmp[0] == ';') continue;
//--- omit white space
start = tmp;
while (*start == ' ') start++;
if ((cp = strchr(start, '=')) == NULL) continue;
*cp = 0;
//--- copy config name
ZeroMemory(&cfg, sizeof(cfg));
COPY_STR(cfg.name, start);
//--- skip space character
cp++;
while (*cp == ' ') cp++;
COPY_STR(cfg.value, cp);
if (cfg.name[0] == 0 || cfg.value[0] == 0) continue;
//--- add
if (m_cfg == NULL || m_cfg_total >= m_cfg_max) // place here
{
//--- reallocate memory
if ((buf = new PluginCfg[m_cfg_total + 64]) == NULL) break;
//--- copy configs to new memory
if (m_cfg != NULL) {
if (m_cfg_total > 0) memcpy(buf, m_cfg, sizeof(PluginCfg) * m_cfg_total);
delete[] m_cfg;
}
//--- replace old buffer
m_cfg = buf;
m_cfg_max = m_cfg_total + 64;
}
//--- add new config
memcpy(&m_cfg[m_cfg_total++], &cfg, sizeof(PluginCfg));
}
//--- close
in.Close();
}
//--- sort config by name
if (m_cfg != NULL && m_cfg_total > 0) qsort(m_cfg, m_cfg_total, sizeof(PluginCfg), SortByName);
m_sync.Unlock();
}
//+------------------------------------------------------------------+
//| save configs to file |
//+------------------------------------------------------------------+
void Config::Save(void) {
FileUtil out;
char tmp[512];
//--- write
m_sync.Lock();
if (m_filename[0] != 0)
if (out.Open(m_filename, GENERIC_WRITE, CREATE_ALWAYS)) {
if (m_cfg != NULL)
for (int i = 0; i < m_cfg_total; i++) {
_snprintf(tmp, sizeof(tmp) - 1, "%s=%s\n", m_cfg[i].name, m_cfg[i].value);
if (out.Write(tmp, strlen(tmp)) < 1) break;
}
//--- close file
out.Close();
}
m_sync.Unlock();
}
//+------------------------------------------------------------------+
//| Search config by name |
//+------------------------------------------------------------------+
PluginCfg* Config::Search(const char* name) {
PluginCfg* config = NULL;
if (m_cfg != NULL && m_cfg_total > 0)
config = (PluginCfg*)bsearch(name, m_cfg, m_cfg_total, sizeof(PluginCfg), SearchByName);
return (config);
}
int Config::Add(const char* name, const char* value, bool save) {
PluginCfg *config, *buf;
if (name == NULL || name[0] == 0 || value == NULL) {
return (FALSE);
}
m_sync.Lock();
if ((config = Search(name)) != NULL) {
memcpy(config->name, name, sizeof(PluginCfg::name));
memcpy(config->value, value, sizeof(PluginCfg::value));
} else {
//--- reach max size or empty
if (m_cfg == NULL || m_cfg_total >= m_cfg_max) {
//--- reallocate memory
if ((buf = new PluginCfg[m_cfg_total + 64]) == NULL) {
m_sync.Unlock();
return (FALSE);
}
//--- copy old configs
if (m_cfg != NULL) {
if (m_cfg_total > 0) {
memcpy(buf, m_cfg, sizeof(PluginCfg) * m_cfg_total);
}
delete[] m_cfg;
}
//--- replace old buf
m_cfg = buf;
m_cfg_max = m_cfg_total + 64;
}
//--- add new config
memcpy(config->name, name, sizeof(PluginCfg::name));
memcpy(config->value, value, sizeof(PluginCfg::value));
//--- sort configs by name
qsort(m_cfg, m_cfg_total, sizeof(PluginCfg), SortByName);
}
m_sync.Unlock();
if (save) {
Save();
}
return (TRUE);
}
int Config::Add(const PluginCfg* cfg) {
PluginCfg *config, *buf;
if (cfg == NULL || cfg->name[0] == 0) return (FALSE);
m_sync.Lock();
if ((config = Search(cfg->name)) != NULL)
memcpy(config, cfg, sizeof(PluginCfg));
else {
//--- reach max size or empty
if (m_cfg == NULL || m_cfg_total >= m_cfg_max) {
//--- reallocate memory
if ((buf = new PluginCfg[m_cfg_total + 64]) == NULL) {
m_sync.Unlock();
return (FALSE);
}
//--- copy old configs
if (m_cfg != NULL) {
if (m_cfg_total > 0) memcpy(buf, m_cfg, sizeof(PluginCfg) * m_cfg_total);
delete[] m_cfg;
}
//--- replace old buf
m_cfg = buf;
m_cfg_max = m_cfg_total + 64;
}
//--- add new config
memcpy(&m_cfg[m_cfg_total++], cfg, sizeof(PluginCfg));
//--- sort configs by name
qsort(m_cfg, m_cfg_total, sizeof(PluginCfg), SortByName);
}
m_sync.Unlock();
//--- save configs to file
Save();
//--- return
return (TRUE);
}
//+------------------------------------------------------------------+
//| Batch set configs |
//+------------------------------------------------------------------+
int Config::Set(const PluginCfg* values, const int total) {
if (total < 0) return (FALSE);
m_sync.Lock();
if (values != NULL && total > 0) {
//--- overflow or empty
if (m_cfg == NULL || total >= m_cfg_max) {
//--- replace old buffer with new buffer
if (m_cfg != NULL) delete[] m_cfg;
if ((m_cfg = new PluginCfg[total + 64]) == NULL) {
m_cfg_max = m_cfg_total = 0;
m_sync.Unlock();
return (FALSE);
}
//--- set max config capacity
m_cfg_max = total + 64;
}
//--- copy configs
memcpy(m_cfg, values, sizeof(PluginCfg) * total);
}
//--- expose new configs
m_cfg_total = total;
if (m_cfg != NULL && m_cfg_total > 0) qsort(m_cfg, m_cfg_total, sizeof(PluginCfg), SortByName);
m_sync.Unlock();
//--- save to file
Save();
return (TRUE);
}
//+------------------------------------------------------------------+
//| Look for config by name |
//+------------------------------------------------------------------+
int Config::Get(const char* name, PluginCfg* cfg) {
PluginCfg* config = NULL;
if (name != NULL && cfg != NULL) {
m_sync.Lock();
if ((config = Search(name)) != NULL) memcpy(cfg, config, sizeof(PluginCfg));
m_sync.Unlock();
}
return (config != NULL);
}
//+------------------------------------------------------------------+
//| Get config at index |
//+------------------------------------------------------------------+
int Config::Next(const int index, PluginCfg* cfg) {
if (cfg != NULL && index >= 0) {
m_sync.Lock();
if (m_cfg != NULL && index < m_cfg_total) {
memcpy(cfg, &m_cfg[index], sizeof(PluginCfg));
m_sync.Unlock();
return (TRUE);
}
m_sync.Unlock();
}
//--- not found
return (FALSE);
}
//+------------------------------------------------------------------+
//| Remove config by name |
//+------------------------------------------------------------------+
int Config::Delete(const char* name) {
PluginCfg* config = NULL;
if (name != NULL) {
m_sync.Lock();
if ((config = Search(name)) != NULL) {
int index = config - m_cfg;
if ((index + 1) < m_cfg_total) memmove(config, config + 1, sizeof(PluginCfg) * (m_cfg_total - index - 1));
m_cfg_total--;
}
//--- sort
if (m_cfg != NULL && m_cfg_total > 0) qsort(m_cfg, m_cfg_total, sizeof(PluginCfg), SortByName);
m_sync.Unlock();
}
return (config != NULL);
}
int Config::SortByName(const void* left, const void* right) {
return strcmp(((PluginCfg*)left)->name, ((PluginCfg*)right)->name);
}
int Config::SearchByName(const void* left, const void* right) {
return strcmp((char*)left, ((PluginCfg*)right)->name);
}
int Config::GetInteger(const char* name, int* value, const char* defvalue) {
PluginCfg* config = NULL;
if (name != NULL && value != NULL) {
m_sync.Lock();
if ((config = Search(name)) != NULL)
*value = atoi(config->value);
else if (defvalue != NULL) {
m_sync.Unlock();
//--- new config
PluginCfg cfg = {0};
COPY_STR(cfg.name, name);
COPY_STR(cfg.value, defvalue);
Add(&cfg);
//--- set default value
*value = atoi(cfg.value);
return (TRUE);
}
m_sync.Unlock();
}
//--- return
return (config != NULL);
}
int Config::GetLong(const char* name, long* value, const char* defvalue) {
PluginCfg* config = NULL;
if (name != NULL && value != NULL) {
m_sync.Lock();
if ((config = Search(name)) != NULL) {
*value = atol(config->value);
} else if (defvalue != NULL) {
m_sync.Unlock();
PluginCfg cfg = {0};
COPY_STR(cfg.name, name);
COPY_STR(cfg.value, defvalue);
Add(&cfg);
*value = atol(cfg.value);
return (TRUE);
}
m_sync.Unlock();
}
return (config != NULL);
}
int Config::GetString(const char* name, char* value, const int maxlen, const char* defvalue) {
PluginCfg* config = NULL;
if (name != NULL && value != NULL) {
m_sync.Lock();
if ((config = Search(name)) != NULL) {
strncpy(value, config->value, maxlen);
value[maxlen] = 0;
} else if (defvalue != NULL) {
m_sync.Unlock();
//--- new config
PluginCfg cfg = {0};
COPY_STR(cfg.name, name);
COPY_STR(cfg.value, defvalue);
Add(&cfg);
//--- copy value
strncpy(value, cfg.value, maxlen);
value[maxlen] = 0;
return (TRUE);
}
m_sync.Unlock();
}
//--- return
return (config != NULL);
}
bool Config::HasKey(const char* name) {
if (name != NULL) {
m_sync.Lock();
PluginCfg* config = Search(name);
m_sync.Unlock();
return config != NULL;
}
return FALSE;
}
//--- end