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
|
#include "StdAfx.h"
class CXMLNodeMI : public IXMLNode, private boost::noncopyable
{
public:
typedef boost::shared_ptr<IXMLNode> TXMLNodePtr;
public:
explicit CXMLNodeMI(HXML hXMl, bool bDestroy = false) : m_hXML(hXMl), m_bDestroy(bDestroy)
{
assert(m_hXML);
}
virtual ~CXMLNodeMI()
{
if (m_bDestroy)
xmlDestroyNode(m_hXML);
}
virtual size_t GetChildCount()const
{
return xmlGetChildCount(m_hXML);
}
virtual TXMLNodePtr GetChildNode(size_t nIndex)const
{
HXML h = xmlGetChild(m_hXML, (int)nIndex);
if (h)
return TXMLNodePtr(new CXMLNodeMI(h));
return TXMLNodePtr();
}
virtual tstring GetText()const
{
tstring sResult;
LPCTSTR psz = xmlGetText(m_hXML);
if (psz)
sResult = psz;
return sResult;
}
virtual tstring GetName()const
{
tstring sResult;
LPCTSTR psz = xmlGetName(m_hXML);
if (psz)
sResult = psz;
return sResult;
}
virtual bool AddChild(const TXMLNodePtr& pNode)
{
CXMLNodeMI* pXML = dynamic_cast<CXMLNodeMI*>(pNode.get());
if (pXML) {
xmlAddChild2(pXML->m_hXML, m_hXML);
pXML->m_bDestroy = false;
return true;
}
return false;
}
virtual bool AddAttribute(const tstring& rsName, const tstring& rsValue)
{
xmlAddAttr(m_hXML, rsName.c_str(), rsValue.c_str());
return true;
}
virtual tstring GetAttributeValue(const tstring& rsAttrName)
{
LPCTSTR pszValue = xmlGetAttrValue(m_hXML, rsAttrName.c_str());
return ((NULL != pszValue) ? tstring(pszValue) : tstring());
}
virtual void Write(tostream& o)const
{
ptrT ss(xmlToString(m_hXML, NULL));
if (ss != NULL)
o << (char*)T2Utf(ss);
}
private:
HXML m_hXML;
bool m_bDestroy;
};
CXMLEngineMI::CXMLEngineMI()
{
}
CXMLEngineMI::~CXMLEngineMI()
{
}
IXMLNode::TXMLNodePtr CXMLEngineMI::LoadFile(const tstring& rsFileName)const
{
IXMLNode::TXMLNodePtr pResult;
FILE *stream;
if (0 == ::_tfopen_s(&stream, rsFileName.c_str(), _T("r"))) {
struct _stat st;
if (-1 != ::_fstat(::_fileno(stream), &st)) {
std::vector<char> aBuffer(st.st_size + 1);
char* pBuffer = &*(aBuffer.begin());
size_t cBytes = ::fread(pBuffer, sizeof(char), st.st_size, stream);
if (cBytes > 0 && cBytes <= static_cast<size_t>(st.st_size)) {
pBuffer[cBytes] = '\0';
int nLen = (int)cBytes;
ptrT ss(mir_utf8decodeT(pBuffer));
if (ss) {
HXML h = xmlParseString(ss, &nLen, NULL);
if (h)
pResult = IXMLNode::TXMLNodePtr(new CXMLNodeMI(h, true));
}
}
}
::fclose(stream);
}
return pResult;
}
static IXMLNode::TXMLNodePtr create_node(const tstring& rsName, const tstring& rsText, bool bIsDecl)
{
IXMLNode::TXMLNodePtr pResult;
HXML h = xmlCreateNode(rsName.c_str(), rsText.c_str(), bIsDecl);
if (h)
pResult = IXMLNode::TXMLNodePtr(new CXMLNodeMI(h, true));
return pResult;
}
bool CXMLEngineMI::SaveFile(const tstring& rsFileName, const IXMLNode::TXMLNodePtr& pNode)const
{
CXMLNodeMI* pXML = dynamic_cast<CXMLNodeMI*>(pNode.get());
if (pXML) {
tofstream file(rsFileName.c_str());
if (file.good()) {
IXMLNode::TXMLNodePtr pRoot(create_node(_T("xml"), tstring(), true));
if (pRoot) {
pRoot->AddAttribute(_T("version"), _T("1.0"));
pRoot->AddAttribute(_T("encoding"), _T("UTF-8"));
file << *pRoot;
}
if (file.good()) {
file << *pNode;
}
}
return file.good();
}
return false;
}
IXMLNode::TXMLNodePtr CXMLEngineMI::CreateNode(const tstring& rsName, const tstring& rsText)const
{
return create_node(rsName, rsText, false);
}
|