forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXmlFile.h
169 lines (144 loc) · 4.09 KB
/
XmlFile.h
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
/* XmlFile - Simple XML reading and writing. */
#ifndef XML_FILE_H
#define XML_FILE_H
#include <map>
#include <unordered_map>
struct DateTime;
class RageFileBasic;
struct lua_State;
class XNodeValue
{
public:
virtual ~XNodeValue() { }
virtual XNodeValue *Copy() const = 0;
virtual void GetValue( std::string &out ) const = 0;
virtual void GetValue( int &out ) const = 0;
virtual void GetValue( float &out ) const = 0;
virtual void GetValue( bool &out ) const = 0;
virtual void GetValue( unsigned &out ) const = 0;
virtual void PushValue( lua_State *L ) const = 0;
template<typename T>
T GetValue() const { T val; GetValue(val); return val; }
virtual void SetValue( std::string const &v ) = 0;
virtual void SetValue( int v ) = 0;
virtual void SetValue( float v ) = 0;
virtual void SetValue( unsigned v ) = 0;
virtual void SetValueFromStack( lua_State *L ) = 0;
};
class XNodeStringValue: public XNodeValue
{
public:
std::string m_sValue;
XNodeValue *Copy() const { return new XNodeStringValue( *this ); }
void GetValue( std::string &out ) const;
void GetValue( int &out ) const;
void GetValue( float &out ) const;
void GetValue( bool &out ) const;
void GetValue( unsigned &out ) const;
void PushValue( lua_State *L ) const;
void SetValue( std::string const &v );
void SetValue( int v );
void SetValue( float v );
void SetValue( unsigned v );
void SetValueFromStack( lua_State *L );
};
typedef std::map<std::string,XNodeValue*> XAttrs;
class XNode;
typedef std::vector<XNode*> XNodes;
class XNode
{
private:
XNodes m_childs; // child nodes
std::unordered_multimap<std::string, XNode*> m_children_by_name;
public:
std::string m_sName;
XAttrs m_attrs; // attributes
void SetName( std::string const &sName ) { m_sName = sName; }
std::string const GetName() const { return m_sName; }
static std::string const TEXT_ATTRIBUTE;
template <typename T>
void GetTextValue( T &out ) const { GetAttrValue(TEXT_ATTRIBUTE, out); }
// in own attribute list
const XNodeValue *GetAttr( const std::string &sAttrName ) const;
XNodeValue *GetAttr( const std::string &sAttrName );
template <typename T>
bool GetAttrValue( std::string const &sName, T &out ) const
{
const XNodeValue *pAttr=GetAttr(sName);
if(pAttr==nullptr)
{
return false;
}
pAttr->GetValue(out);
return true;
}
bool PushAttrValue( lua_State *L, std::string const &sName ) const;
bool ChildrenEmpty() const { return m_childs.empty(); }
XNodes::iterator begin()
{
return m_childs.begin();
}
XNodes::const_iterator begin() const
{
return m_childs.begin();
}
XNodes::iterator end()
{
return m_childs.end();
}
XNodes::const_iterator end() const
{
return m_childs.end();
}
// in one level child nodes
const XNode *GetChild( std::string const &sName ) const;
XNode *GetChild( std::string const &sName );
template <typename T>
bool GetChildValue( std::string const &sName, T &out ) const
{
const XNode *pChild=GetChild(sName);
if(pChild==nullptr)
{
return false;
}
pChild->GetTextValue(out);
return true;
}
bool PushChildValue( lua_State *L, std::string const &sName ) const;
// modify DOM
template <typename T>
XNode *AppendChild( std::string const &sName, T value )
{
XNode *p=AppendChild(sName);
p->AppendAttr(XNode::TEXT_ATTRIBUTE, value);
return p;
}
XNode *AppendChild( std::string const &sName )
{
XNode *p=new XNode(sName);
return AppendChild(p);
}
XNode *AppendChild( XNode *node );
bool RemoveChild( XNode *node, bool bDelete = true );
void RemoveChildFromByName(XNode *node);
void RenameChildInByName(XNode* node);
XNodeValue *AppendAttrFrom( std::string const &sName, XNodeValue *pValue, bool bOverwrite = true );
XNodeValue *AppendAttr( std::string const &sName );
template <typename T>
XNodeValue *AppendAttr( std::string const &sName, T value )
{
XNodeValue *pVal = AppendAttr( sName );
pVal->SetValue( value );
return pVal;
}
bool RemoveAttr( std::string const &sName );
XNode();
explicit XNode( std::string const &sName );
XNode( const XNode &cpy );
~XNode() { Free(); }
void Clear();
private:
void Free();
XNode &operator=( const XNode &cpy ); // don't use
};
#endif