-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathIniProcessor.cpp
152 lines (129 loc) · 2.71 KB
/
IniProcessor.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
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
/*
* IniProcessor class implementation file
* Author: Sun Junwen
* Version: 1.2.5
*/
#include <cstdlib>
#include <string>
#include <istream>
#include <map>
#include <stack>
#include "IniProcessor.h"
using namespace std;
IniProcessor::IniMap IniProcessor::GetInfo(istream& in, bool bProcSection, bool bRefresh)
{
if(!bRefresh && m_iniMap.size() > 0)
return m_iniMap;
m_iniMap.clear();
string line;
bool bSection = false;
string sectionName;
//IniValue sectionMap;
IniValue::StrMap sectionMap;
while(true)
{
if(!getline(in, line)) // eof
{
if(bProcSection && bSection)
{
AddSection(sectionName, sectionMap);
}
break;
}
line = strtrim(line);
if(line.length() <= 1 ||
line[0] == ';' ||
line[0] == '#')
continue; // ×¢ÊÍÐÐ
if(bProcSection &&
line[0] == '[' &&
line[line.length() - 1] == ']')
{
if(bSection)
{
AddSection(sectionName, sectionMap);
}
bSection = true;
sectionMap.clear();
sectionName = line.length() - 2 > 0 ?
line.substr(1, line.length() - 2) : "";
sectionName = strtrim(sectionName);
}
size_t eqPos = line.find("=");
if(eqPos != string::npos)
{
string key = line.substr(0, eqPos);
key = strtrim(key);
string value = eqPos + 1 >= line.length() ?
"" : line.substr(eqPos + 1);
value = strtrim(value);
if(bProcSection && bSection)
{
sectionMap[key] = value;
}
else
{
m_iniMap[key] = IniValue(value);
}
}
}
return m_iniMap;
}
string IniProcessor::ToString(IniProcessor::IniMap map) const
{
typedef pair<const string, IniValue> MapEntry;
string ret("");
stack<MapEntry> sectionStack;
IniMap::const_iterator iniItr = map.begin();
string line;
string key;
IniValue value;
for(; iniItr != map.end(); ++iniItr)
{
key = (*iniItr).first;
value = (*iniItr).second;
if(value.IsStrValue())
{
line = key;
line.append("=");
line.append(value.GetStrValue());
line.append("\n");
ret.append(line);
}
else
{
sectionStack.push(*iniItr);
}
}
while(!sectionStack.empty())
{
key = sectionStack.top().first;
value = sectionStack.top().second;
line = "\n[";
line.append(key);
line.append("]\n");
ret.append(line);
line = value.ToString();
ret.append(line);
sectionStack.pop();
}
return ret;
}
void IniProcessor::AddSection(const string& sectionName, const IniValue::StrMap& sectionMap)
{
IniValue iniValue = m_iniMap[sectionName];// = sectionMap;
if(iniValue.IsStrValue())
{
iniValue.SetMode(false);
iniValue.SetMapValue(sectionMap);
}
else
{
IniValue::StrMap::const_iterator itr = sectionMap.begin();
for(; itr != sectionMap.end(); ++itr)
{
iniValue.Put((*itr).first, (*itr).second);
}
}
m_iniMap[sectionName] = iniValue;
}