admin
2024-05-17 296ffe27dd5686e6897f414677b8164ec136e639
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
#include "ConfigUtil.h"
 
#include "ConfigUtil.h"
#include <iostream>
#include <fstream>
 
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);
}
 
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);
}