-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIniFile.cpp
More file actions
184 lines (172 loc) · 3.25 KB
/
Copy pathIniFile.cpp
File metadata and controls
184 lines (172 loc) · 3.25 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
#include "stdafx.h"
#include "IniFile.h"
#include <fstream>
/* 测试代码
INIFile inifile;
inifile.Create("abc.ini");
string val;
inifile.GetVar("SERVER","PORT",val);
AfxMessageBox(val.c_str());
inifile.SetVar("SERVER","PORT","test");
inifile.Save("abc.ini");
*/
INIFile::INIFile()
{
m_FileContent.erase(m_FileContent.begin(),m_FileContent.end());
m_bCreated = false;
}
INIFile::INIFile(string filename)
{
m_FileContent.erase(m_FileContent.begin(),m_FileContent.end());
m_bCreated = false;
Create(filename);
}
bool INIFile::Create(string filename)
{
ifstream ifile;
string strLine;
if (filename == "")
{
return false;
}
ifile.open(filename.c_str());
if (!ifile.is_open())
{
FILE *pfile = fopen(filename.c_str(),"wb+");
if (pfile == NULL)
{
return false;
}
fclose(pfile);
ifile.open(filename.c_str());
}
while (getline(ifile,strLine))
{
ProcessLine(strLine);
}
ifile.close();
m_bCreated = true;
return true;
}
bool INIFile::ProcessLine(string strLine)
{
string key,val,sect;
if (strLine[0] == '#')
{
//如果是注释直接返回
return false;
}
if (strLine[0] == '[')
{
sect = strLine.substr(1,strLine.find("]")-1);
SetVar(sect);
}else if (strLine.find("=") != 0)
{
string::size_type ePos = strLine.find("=");
if (ePos != std::string::npos)
{
SetVar(m_FileContent.back().SectionName, strLine.substr(0, ePos), strLine.substr(ePos + 1, strLine.size()), false);
}
}else
{
return false;
}
return true;
}
bool INIFile::SetVar(string strSection, string varName, string varvalue, bool breplace)
{
//先查找段
itVector it = m_FileContent.begin();
for (it;it!=m_FileContent.end();it++)
{
if (it->SectionName == strSection)
{
break;
}
}
//如果是加入段
if (varName == "" && varvalue == "")
{
if (it != m_FileContent.end() && !breplace)
{
return false;
}
if (it != m_FileContent.end() && breplace)
{
m_FileContent.erase(it);
}
//加入这个段
Section t;
t.SectionName = strSection;
m_FileContent.push_back(t);
}else
{
//加入变量
if (it == m_FileContent.end())
{
Section t;
t.SectionName = strSection;
m_FileContent.push_back(t);
it = m_FileContent.end()-1;
}
if (breplace)
{
it->dMap.erase(varName);
}
it->dMap.insert(make_pair(varName,varvalue));
}
return true;
}
bool INIFile::GetVar(string strSection, string varName, string default, string &varvalue)
{
//先查找段
itVector it = m_FileContent.begin();
for (it;it!=m_FileContent.end();it++)
{
if (it->SectionName == strSection)
{
break;
}
}
//如果段不存在
if (it == m_FileContent.end())
{
return false;
}
//查找字典
itmapDirectory it2 = it->dMap.find(varName);
if (it2 == it->dMap.end())
{
varvalue = default;
return false;
}
varvalue = it2->second;
return true;
}
bool INIFile::Save(string filename)
{
ofstream ofile;
string strLine;
if (filename == "")
{
return false;
}
ofile.open(filename.c_str());
if (!ofile.is_open())
{
return false;
}
itVector it = m_FileContent.begin();
for (it;it!=m_FileContent.end();it++)
{
ofile<<"["<<it->SectionName<<"]"<<endl;
itmapDirectory itd = it->dMap.begin();
for (itd;itd!=it->dMap.end();itd++)
{
if (itd->first == " " || itd->second==" ") continue;
ofile<<itd->first<<"="<<itd->second<<endl;
}
}
ofile.close();
return false;
}