-
Notifications
You must be signed in to change notification settings - Fork 9
/
cxprofile_skv_xml.cpp
164 lines (146 loc) · 5.5 KB
/
cxprofile_skv_xml.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
153
154
155
156
157
158
159
160
161
162
163
164
#include "cxprofile_skv_xml.h"
#include "cxtinyxml.h"
using namespace std;
CxSkverXml::CxSkverXml() :
CxSkverBase(),
_dealState(0),
_dealType(0)
{
_text = _textData;
}
CxSkverXml::CxSkverXml(const std::map<std::string, std::map<std::string, std::string> > & sectionEntryValues) :
CxSkverBase(sectionEntryValues),
_dealState(0),
_dealType(0)
{
_text = _textData;
}
CxSkverXml::CxSkverXml( const std::string & sFilePath ) :
CxSkverBase(),
_dealState(0),
_dealType(0)
{
_text = _textData;
load(sFilePath);
}
CxSkverXml::~CxSkverXml()
{
}
bool CxSkverXml::loadImpl(std::map<string, std::map<string, string> > §ionEntryValues, const string &sFilePath)
{
if (skvType().empty())
{
return loadXmlNormal(sectionEntryValues, sFilePath);
}
else if (skvType() == "xml_4level")
{
map<string, string> sDataParams = CxString::splitToMap(skvParam(), '=', ',');
string sTableName = CxContainer::value(sDataParams, string("TableName"));
std::vector<std::map<string, string> > rows;
CxTinyXml::loadTable4Level(sFilePath, rows, "", sTableName);
for (size_t i = 0; i < rows.size(); ++i)
{
sectionEntryValues[CxString::toString(i)] = rows.at(i);
}
return sectionEntryValues.size();
}
return false;
}
bool CxSkverXml::saveImpl(const std::map<string, std::map<string, string> > §ionEntryValues, const string &sFilePath, bool bRenew) const
{
if (skvType().empty())
{
return saveXmlNormal(sectionEntryValues, sFilePath, bRenew);
}
else if (skvType() == "xml_4level")
{
map<string, string> sDataParams = CxString::splitToMap(skvParam(), '=', ',');
string sTableName = CxContainer::value(sDataParams, string("TableName"));
return CxTinyXml::saveTable4Level(sFilePath, CxString::sortToLines(sectionEntryValues), "", sTableName);
}
return false;
}
bool CxSkverXml::loadXmlNormal(std::map<std::string, std::map<std::string, std::string> > & sectionEntryValues, const std::string& sFilePath)
{
TiXmlDocument doc;
if(doc.LoadFile(sFilePath))
{
TiXmlHandle docHandle( &doc );
TiXmlNode* nodeLevel2 = docHandle.FirstChild("root").FirstChild("config").Node();
if(nodeLevel2)
{
sectionEntryValues.clear();
int iRowIndex = 0;
for( TiXmlElement * oElementLevel3 = nodeLevel2->FirstChildElement(); oElementLevel3; oElementLevel3 = oElementLevel3->NextSiblingElement() )
{
if (! oElementLevel3) break;
map<string, string> entryValues;
const TiXmlAttribute * oAttribute = oElementLevel3->FirstAttribute();
while (oAttribute)
{
string sKey = oAttribute->NameTStr();
string sValue = oAttribute->ValueStr();
entryValues[sKey] = sValue;
oAttribute = oAttribute->Next();
}
iRowIndex++;
string sSection = CxString::format("%06d",iRowIndex);
sectionEntryValues[sSection] = entryValues;
}
return true;
}
}
return false;
}
bool CxSkverXml::saveXmlNormal(const std::map<std::string, std::map<std::string, std::string> > & sectionEntryValues, const std::string& sFilePath, bool bRenew) const
{
FILE * pFile;
pFile = fopen (sFilePath.data(), "wb");
size_t iWroteTotal = 0;
if (pFile==NULL)
{
return false;
}
string sLine = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<root>\r\n <config>\r\n";
iWroteTotal += fwrite (const_cast<char *>(sLine.data()) , 1, sLine.size(), pFile);
vector<string> sSectionNames = sectionNames(1);
for (size_t i = 0; i < sSectionNames.size(); ++i)
{
const string & sSectionName = sSectionNames.at(i);
map<string, map<string, string> >::const_iterator it = sectionEntryValues.find(sSectionName);
if (it != sectionEntryValues.end())
{
const map<string, string> & sEntrys = it->second;
sLine = (" <prop ");
// sLine = (" < ");
for (map<string, string>::const_iterator itEntry = sEntrys.begin(); itEntry != sEntrys.end(); ++itEntry)
{
sLine += itEntry->first + "=\"" + itEntry->second + "\" ";
}
sLine += "/>\r\n";
size_t iWrote = fwrite (const_cast<char *>(sLine.data()) , 1, sLine.size(), pFile);
if (iWrote != sLine.size())
return false;
iWroteTotal += iWrote;
}
}
// for (map<string, map<string, string> >::const_iterator itSection = sectionEntryValues.begin(); itSection != sectionEntryValues.end(); ++itSection)
// {
// const map<string, string> & sEntrys = itSection->second;
// sLine = (" <prop ");
//// sLine = (" < ");
// for (map<string, string>::const_iterator itEntry = sEntrys.begin(); itEntry != sEntrys.end(); ++itEntry)
// {
// sLine += itEntry->first + "=\"" + itEntry->second + "\" ";
// }
// sLine += "/>\r\n";
// size_t iWrote = fwrite (const_cast<char *>(sLine.data()) , 1, sLine.size(), pFile);
// if (iWrote != sLine.size())
// return false;
// iWroteTotal += iWrote;
// }
sLine = " </config>\r\n</root>\r\n";
iWroteTotal += fwrite (const_cast<char *>(sLine.data()) , 1, sLine.size(), pFile);
fclose (pFile);
return iWroteTotal;
}