-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLXIni.cpp
109 lines (94 loc) · 3.17 KB
/
LXIni.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
//
// Ini.cpp
// FileUtil
//
// Created by duoyi on 2017/6/29.
// Copyright © 2017年 lixu. All rights reserved.
//
#include <fstream>
#include <iostream>
#include "LXIni.h"
#include "LXFileUtil.h"
namespace LX_FU {
namespace INI{
IniConfig::IniConfig()
{
};
IniConfig::IniConfig(const std::string& configName)
{
loadFromFile(configName);
}
IniConfig::~IniConfig()
{
}
bool IniConfig::IniConfig::hasConf(const std::string& confName){
if (m_mapIni.find(confName) != m_mapIni.end()) {
return true;
}
return false;
};
std::string IniConfig::IniConfig::getConf(const std::string& confName){
auto it = m_mapIni.find(confName);
if (it != m_mapIni.end()) {
return it->second;
}
return "";
};
void IniConfig::IniConfig::setConf(const std::string& key, const std::string& value){
auto it = m_mapIni.find(key);
if (it != m_mapIni.end()) {
m_mapIni[key] = value;
return;
}
m_mapIni.emplace(key, value);
};
bool IniConfig::saveToFile(const std::string& configName){
bool success = LX_FU::makeDirectories(configName);
if (success)
{
std::ofstream of(LX_FU::getAbsolutePath(configName), std::ios::out);
if (of) {
for (auto it : m_mapIni) {
of.write(it.first.c_str(), it.first.size());
of.write("=", 1);
of.write(it.second.c_str(), it.second.size());
of.write("\n", 1);
}
of.close();
return true;
}
std::cout << "Error::saveConfigToFile: cannot open file with name -- " << getAbsolutePath(configName) << std::endl;
return false;
}
std::cout << "Error::saveConfigToFile: cannot create dir with name -- " << getAbsolutePath(configName) << std::endl;
};
bool IniConfig::loadFromFile(const std::string& configName){
std::ifstream config(LX_FU::getAbsolutePath(configName), std::ios::in);
char buffer[32];
if (config) {
while (!config.eof()) {
config.getline(buffer, 32);
std::string tmp(buffer);
if (*tmp.begin() == '#') {
continue;
}
unsigned long pos = tmp.find_first_of('=');
if (pos > 0) {
std::string key = tmp.substr(0, pos);
std::string value = tmp.substr(pos + 1, tmp.size());
m_mapIni.emplace(key, value);
}
}
config.close();
return true;
}
return false;
}
IniConfig readIniConfigFromFile(const std::string& configName){
return IniConfig(configName);
}
bool saveIniConfigToFile(IniConfig& iniConfig, const std::string& configName){
return iniConfig.saveToFile(configName);
}
}
}