admin
2024-10-25 0a678d4e8368730e60e17aafbb2f0d055888402a
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
365
366
367
368
369
370
371
372
373
#include "common/pch.h"
 
#include "ConfigUtil.h"
#include <iostream>
#include <fstream>
#include "../common/JsonUtil.h"
#include "../common/TimeUtil.h"
 
 
bool fileExists(const std::string& filename) {
    std::ifstream file(filename);
    return file.good(); // Èç¹ûÎļþ³É¹¦´ò¿ª£¬Ôò·µ»Ø true
}
 
string ConfigUtil::getConfigPath()
{
    wchar_t* appDataPath = nullptr;
    CString path;
    // »ñÈ¡APPDataĿ¼·¾¶
    if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &appDataPath))) {
        std::wcout << L"APPDataĿ¼·¾¶: " << appDataPath << std::endl;
        path.Append(appDataPath);
        CoTaskMemFree(appDataPath);  // ÊÍ·ÅÄÚ´æ
    }
    else {
        std::wcerr << L"ÎÞ·¨»ñÈ¡APPDataĿ¼·¾¶" << std::endl;
        return "config.cfg";
    }
    path.Append(L"\\Ðü¸¡¶¢ÅÌ");
    // ÅжÏÎļþ¼ÐÊÇ·ñ´æÔÚ
    DWORD fileAttributes = GetFileAttributesW(path);
    if (fileAttributes == INVALID_FILE_ATTRIBUTES) {
        CreateDirectoryW(path, NULL);
    }
    string fpath = StringUtil::cstring2String(path);
    fpath.append("\\config.cfg");
    return  fpath.c_str();
}
 
void ConfigUtil::readConfig(libconfig::Config& config)
{
 
    string strConfPath = getConfigPath();
    if (!fileExists(strConfPath)) {
        std::ofstream file(strConfPath);
        file.is_open();
    }
 
    //½â¶ÁÅäÖÃÎļþ
    try {
        config.readFile(strConfPath.c_str());
    }
    catch (const libconfig::FileIOException& fioex) {
        std::cerr << "I/O exception while reading the file." << std::endl;
    }
    catch (const libconfig::ParseException& pex) {
        std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
            << " - " << pex.getError() << std::endl;
    }
}
 
void ConfigUtil::writeConfig(libconfig::Config& config)
{
    config.writeFile(getConfigPath().c_str());
}
 
int ConfigUtil::readIntConfig(const char* key)
{
 
    libconfig::Config mConfig;
    readConfig(mConfig);
    libconfig::Setting& root = mConfig.getRoot();
    if (root.exists(key)) {
        int val;
        root.lookupValue(key, val);
        return val;
    }
    throw string("ÉÐδ»ñÈ¡µ½ÄÚÈÝ");
}
 
string ConfigUtil::readStringConfig(const char* key)
{
    libconfig::Config mConfig;
    readConfig(mConfig);
    libconfig::Setting& root = mConfig.getRoot();
    if (root.exists(key)) {
        //string val;
        const char* val;
        root.lookupValue(key, val);
        return string(val);
    }
    throw string("ÉÐδ»ñÈ¡µ½ÄÚÈÝ");
}
 
 
 
void ConfigUtil::setIntConfig(const char* key, int val) {
    libconfig::Config mConfig;
    readConfig(mConfig);
    libconfig::Setting& root = mConfig.getRoot();
    if (root.exists(key)) {
        root[key] = val;
    }
    else {
        root.add(key, libconfig::Setting::TypeInt) = val;
    }
    writeConfig(mConfig);
}
void ConfigUtil::setStringConfig(const char* key, string val) {
    libconfig::Config mConfig;
    readConfig(mConfig);
    libconfig::Setting& root = mConfig.getRoot();
    if (root.exists(key)) {
        root[key] = val.c_str();
    }
    else {
        root.add(key, libconfig::Setting::TypeString) = val.c_str();
    }
    writeConfig(mConfig);
}
 
list<string> ConfigUtil::getKeys()
{
    libconfig::Config mConfig;
    readConfig(mConfig);
    libconfig::Setting& root = mConfig.getRoot();
    list<string> keys;
    for (auto iter = root.begin(); iter != root.end(); ++iter) {
        keys.push_back(iter->getName());
    }
    return keys;
}
 
void ConfigUtil::delKey(string key)
{
    libconfig::Config mConfig;
    readConfig(mConfig);
    libconfig::Setting& root = mConfig.getRoot();
    root.remove(key.c_str());
    writeConfig(mConfig);
}
 
bool ConfigUtil::isTradeRefresh()
{
    try {
        return    readIntConfig("trade_refresh") > 0;
    }
    catch (...) {
 
    }
 
    return false;
}
 
bool ConfigUtil::isGroupRefresh()
{
    try {
        return    readIntConfig("group_refresh") > 0;
    }
    catch (...) {
 
    }
    return false;
}
 
bool ConfigUtil::isAutoFocus()
{
    try {
        return    readIntConfig("auto_focus") > 0;
    }
    catch (...) {
 
    }
    return false;
}
 
bool ConfigUtil::isTradeQuickKey()
{
    try {
        return    readIntConfig("trade_quick_key") > 0;
    }
    catch (...) {
 
    }
    return false;
}
 
int* ConfigUtil::getSellRuleDialogShowPos()
{
 
    int* pos = (int*)malloc(sizeof(int) * 2);
    pos[0] = 0;
    pos[1] = 0;
    try {
        string str = readStringConfig("sell_rule_show_pos");
        // ·Ö¸ô×Ö·û´®
        int split_index = str.find(",");
        int x = stoi(str.substr(0, split_index));
        int y = stoi(str.substr(split_index + 1, str.length() - (split_index + 1)));
        pos[0] = x;
        pos[1] = y;
    }
    catch (...) {
 
    }
 
    return pos;
}
 
void ConfigUtil::setTradeRefresh(bool enable)
{
    setIntConfig("trade_refresh", enable ? 1 : 0);
}
void ConfigUtil::setGroupRefresh(bool enable)
{
    setIntConfig("group_refresh", enable ? 1 : 0);
}
void ConfigUtil::setAutoFocus(bool enable)
{
    setIntConfig("auto_focus", enable ? 1 : 0);
}
void ConfigUtil::setTradeQuickKey(bool enable)
{
    setIntConfig("trade_quick_key", enable ? 1 : 0);
}
 
void ConfigUtil::setSellRuleDialogShowPos(int x, int y)
{
    string pos = "";
    pos.append(to_string(x)).append(",").append(to_string(y));
    setStringConfig("sell_rule_show_pos", pos);
}
 
MyPoint ConfigUtil::getWindowPos()
{
 
    // Ò³ÂëλÖÃ
    try {
        string data = readStringConfig("window_pos");
        rapidjson::Document root = JsonUtil::parseUTF8(data);
        return     MyPoint({ root[0].GetInt(), root[1].GetInt() });
    }
    catch (...) {
 
    }
    return MyPoint({ 0,0 });
 
 
 
}
 
void ConfigUtil::setWindowPos(int x, int y)
{
    string st = "[";
    st.append(std::to_string(x)).append(",").append(std::to_string(y)).append("]");
    setStringConfig("window_pos", st);
}
 
int ConfigUtil::getThsAutoRefreshTimeSpace()
{
    try {
        return    readIntConfig("ths_auto_refresh_time_space");
    }
    catch (...) {
 
    }
    return 0;
}
 
void ConfigUtil::setThsAutoRefreshTimeSpace(int ms)
{
    setIntConfig("ths_auto_refresh_time_space", ms);
}
 
list<int> ConfigUtil::getVolumesSetting()
{
    try {
        list<int> volumeList;
        string result = readStringConfig("volume_settings");
        auto doc = JsonUtil::parseUTF8(result);
        auto array = doc.GetArray();
        for (int i = 0; i < array.Size(); i++) {
            volumeList.push_back(array[i].GetInt());
        }
        return volumeList;
    }
    catch (...) {
 
    }
    return list<int>();
}
 
void ConfigUtil::setVolumesSetting(list<int> volumes)
{
    string st = "[";
    int index = 0;
    for (list<int>::iterator e = volumes.begin(); e != volumes.end(); ++e) {
        index++;
        st.append(to_string(*e));
        if (index != volumes.size()) {
            st.append(",");
        }
    }
    st.append("]");
    setStringConfig("volume_settings", st);
}
 
 
void ConfigUtil::setCodeNames(map<string, CString> codeNameMap)
{
    // ½«mapתΪjson
    rapidjson::StringBuffer buf;
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buf);
    writer.StartObject();
    for (map<string, CString>::iterator el = codeNameMap.begin(); el != codeNameMap.end(); el++) {
        string code = (*el).first;
        CString name = (*el).second;
        writer.Key(code.c_str());
        writer.String(StringUtil::cstring2String(name).c_str());
    }
    writer.EndObject();
    const char* json_content = buf.GetString();
    string key = "code_name_map-";
    key.append(TimeUtil::getNowTime("%Y%m%d"));
    setStringConfig(key.c_str(), json_content);
    // É¾³ý¹ýÆÚµÄ´úÂëÊý¾Ý
    list<string> kyes = getKeys();
    for (auto e = kyes.begin(); e != kyes.end(); ++e) {
        string k = *e;
        if (!k._Equal(key) && k.find("code_name_map-") == 0) {
            delKey(k);
        }
    }
}
 
map<string, CString> ConfigUtil::getCodeNames()
{
    map<string, CString> m;
    try {
        string key = "code_name_map-";
        key.append(TimeUtil::getNowTime("%Y%m%d"));
        string result = readStringConfig(key.c_str());
        if (!result.empty()) {
            auto doc = JsonUtil::parseUTF8(result);
            auto root = doc.GetObjectW();
            for (auto e = root.MemberBegin(); e != root.MemberEnd(); ++e) {
                string code = (*e).name.GetString();
                string name = (*e).value.GetString();
                m[code] = CString(name.c_str());
            }
        }
    }
    catch (...) {
    }
    return m;
}
 
void ConfigUtil::setBuyMoney(int money)
{
    setIntConfig("buy_money", money);
}
 
 
int ConfigUtil::getBuyMoney()
{
    try {
        return readIntConfig("buy_money");
    }
    catch (...) {
        // Ä¬ÈÏ2w
        return 20000;
    }
}