admin
2024-10-18 8ea6d363df77de2dca288397da8d4f9c3d3a5c4d
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
#include "common/pch.h"
 
#include "ConfigUtil.h"
#include <iostream>
#include <fstream>
#include "../common/JsonUtil.h"
 
bool fileExists(const std::string& filename) {
    std::ifstream file(filename);
    return file.good(); // Èç¹ûÎļþ³É¹¦´ò¿ª£¬Ôò·µ»Ø true
}
 
void ConfigUtil::readConfig(libconfig::Config& config)
{
 
    string strConfPath = "config.cfg";
    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("config.cfg");
}
 
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);
}
 
 
MyPoint ConfigUtil::getWindowPos()
{
 
    // Ò³ÂëλÖÃ
    try {
        string data = readStringConfig("window_pos");
        rapidjson::Document root = JsonUtil::parseUTF8(data);
        return     POINT({ root[0].GetInt(), root[1].GetInt() });
    }
    catch (...) {
 
    }
    return POINT({ 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);
}