-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmyini.cpp
More file actions
212 lines (194 loc) · 7.06 KB
/
Copy pathcmyini.cpp
File metadata and controls
212 lines (194 loc) · 7.06 KB
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
#include "cmyini.h"
#define INIDEBUG
CMyINI::CMyINI()
{
}
CMyINI::~CMyINI()
{
}
//************************************************************************
// 函数名称: TrimString
// 访问权限: public
// 创建日期: 2017/01/05
// 创 建 人:
// 函数说明: 去除空格
// 函数参数: string & str 输入的字符串
// 返 回 值: std::string & 结果字符串
//************************************************************************
string &TrimString(string &str)
{
string::size_type pos = 0;
while (str.npos != (pos = str.find(" ")))
str = str.replace(pos, pos + 1, "");
return str;
}
//************************************************************************
// 函数名称: ReadINI
// 访问权限: public
// 创建日期: 2017/01/05
// 创 建 人:
// 函数说明: 读取INI文件,并将其保存到map结构中
// 函数参数: string path INI文件的路径
// 返 回 值: int
//************************************************************************
int CMyINI::ReadINI(string path)
{
ifstream in_conf_file(path.c_str());
if (!in_conf_file) return 0;
string str_line = "";
string str_root = "";
vector<ININode> vec_ini;
while (getline(in_conf_file, str_line))
{
string::size_type left_pos = 0;
string::size_type right_pos = 0;
string::size_type equal_div_pos = 0;
string str_key = "";
string str_value = "";
if ((str_line.npos != (left_pos = str_line.find("["))) && (str_line.npos != (right_pos = str_line.find("]"))))
{
//cout << str_line.substr(left_pos+1, right_pos-1) << endl;
str_root = str_line.substr(left_pos + 1, right_pos - 1);
}
if (str_line.npos != (equal_div_pos = str_line.find("=")))
{
str_key = str_line.substr(0, equal_div_pos);
str_value = str_line.substr(equal_div_pos + 1, str_line.size() - 1);
str_key = TrimString(str_key);
str_value = TrimString(str_value);
//cout << str_key << "=" << str_value << endl;
}
if ((!str_root.empty()) && (!str_key.empty()) && (!str_value.empty()))
{
ININode ini_node(str_root, str_key, str_value);
vec_ini.push_back(ini_node);
//cout << vec_ini.size() << endl;
}
}
in_conf_file.close();
in_conf_file.clear();
//vector convert to map
map<string, string> map_tmp;
for (vector<ININode>::iterator itr = vec_ini.begin(); itr != vec_ini.end(); ++itr)
{
map_tmp.insert(pair<string, string>(itr->root, ""));
} //提取出根节点
for (map<string, string>::iterator itr = map_tmp.begin(); itr != map_tmp.end(); ++itr)
{
#ifdef INIDEBUG
//cout << "根节点: " << itr->first << endl;
#endif //INIDEBUG
SubNode sn;
for (vector<ININode>::iterator sub_itr = vec_ini.begin(); sub_itr != vec_ini.end(); ++sub_itr)
{
if (sub_itr->root == itr->first)
{
#ifdef INIDEBUG
//cout << "键值对: " << sub_itr->key << "=" << sub_itr->value << endl;
#endif //INIDEBUG
sn.InsertElement(sub_itr->key, sub_itr->value);
}
}
map_ini.insert(pair<string, SubNode>(itr->first, sn));
}
return 1;
}
//************************************************************************
// 函数名称: GetValue
// 访问权限: public
// 创建日期: 2017/01/05
// 创 建 人:
// 函数说明: 根据给出的根结点和键值查找配置项的值
// 函数参数: string root 配置项的根结点
// 函数参数: string key 配置项的键
// 返 回 值: std::string 配置项的值
//************************************************************************
string CMyINI::GetValue(string root, string key)
{
map<string, SubNode>::iterator itr = map_ini.find(root);
map<string, string>::iterator sub_itr = itr->second.sub_node.find(key);
if (!(sub_itr->second).empty())
return sub_itr->second;
return "";
}
//************************************************************************
// 函数名称: WriteINI
// 访问权限: public
// 创建日期: 2017/01/05
// 创 建 人:
// 函数说明: 保存XML的信息到文件中
// 函数参数: string path INI文件的保存路径
// 返 回 值: int
//************************************************************************
int CMyINI::WriteINI(string path)
{
ofstream out_conf_file(path.c_str());
if (!out_conf_file)
return -1;
//cout << map_ini.size() << endl;
for (map<string, SubNode>::iterator itr = map_ini.begin(); itr != map_ini.end(); ++itr)
{
//cout << itr->first << endl;
out_conf_file << "[" << itr->first << "]" << endl;
for (map<string, string>::iterator sub_itr = itr->second.sub_node.begin(); sub_itr != itr->second.sub_node.end(); ++sub_itr)
{
//cout << sub_itr->first << "=" << sub_itr->second << endl;
out_conf_file << sub_itr->first << "=" << sub_itr->second << endl;
}
}
out_conf_file.close();
out_conf_file.clear();
return 1;
}
//************************************************************************
// 函数名称: SetValue
// 访问权限: public
// 创建日期: 2017/01/05
// 创 建 人:
// 函数说明: 设置配置项的值
// 函数参数: string root 配置项的根节点
// 函数参数: string key 配置项的键
// 函数参数: string value 配置项的值
// 返 回 值: std::vector<ININode>::size_type
//************************************************************************
vector<ININode>::size_type CMyINI::SetValue(string root, string key, string value)
{
map<string, SubNode>::iterator itr = map_ini.find(root); //查找
if (map_ini.end() != itr)
{
//itr->second.sub_node.insert(pair<string, string>(key, value));
itr->second.sub_node[key] = value;
} //根节点已经存在了,更新值
else
{
SubNode sn;
sn.InsertElement(key, value);
map_ini.insert(pair<string, SubNode>(root, sn));
} //根节点不存在,添加值
return map_ini.size();
}
//************************************************************************
// 函数名称: Travel
// 访问权限: public
// 创建日期: 2017/01/05
// 创 建 人:
// 函数说明: 遍历打印INI文件
// 返 回 值: void
//************************************************************************
/*void CMyINI::Travel()
{
for (map<string, SubNode>::iterator itr = this->map_ini.begin(); itr!= this->map_ini.end(); ++itr)
{
//root
cout << "[" << itr->first << "]" << endl;
for (map<string, string>::iterator itr1=itr->second.sub_node.begin(); itr1!=itr->second.sub_node.end();
++itr1)
{
cout << " " << itr1->first << " = " << itr1->second << endl;
}
}
}*/
/*
版权声明:本文为CSDN博主「m_buddy」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m_buddy/article/details/54097131
*/