-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlhelpers.h
200 lines (174 loc) · 4.8 KB
/
xmlhelpers.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
This file is a part of the
Amagami SS PS2 Translation Project
You may feel free to use this code if you want so.
I provide no guarantee that this code is working,
useful or anything else. Use it at your own risk.
*/
#ifndef XMLHELPERS_H
#define XMLHELPERS_H
/* ------------------------------------ MACROS ------------------------------------ */
#define READ_TO_VAR(file, dest) \
{ \
if( file.read((char*)&(dest), sizeof(dest)) != sizeof(dest)) \
{ \
file.close(); \
fatalExit("Failed to read from file"); \
} \
}
#define READ_TO_VAR_NONZERO(file, dest) \
{ \
READ_TO_VAR((file), (dest)); \
assert((dest) && "Expected to be nonzero"); \
}
#define WRITE_HEADER(file, array, size) \
{ \
if( file.write((array), (size)) != (size)) \
{ \
file.close(); \
fatalExit("Failed to write into the file"); \
} \
}
#define WRITE_VAR(file, var) \
{ \
if( file.write((char*)&(var), sizeof(var)) != sizeof(var)) \
{ \
file.close(); \
fatalExit("Failed to write into the file"); \
} \
}
#define WRITE_STR(file, string) \
{ \
QByteArray _tmpArray = string.toAscii(); \
if( file.write(_tmpArray) != _tmpArray.size()) \
{ \
file.close(); \
fatalExit("Failed to write into the file"); \
} \
}
#define WRITE_BINARY_DATA(file, data) \
{ \
if( file.write(data) != data.size()) \
{ \
file.close(); \
fatalExit("Failed to write into the file"); \
} \
}
#define WRITE_STRING_WITH_LENGTH(file, string) \
{ \
quint16 _length = (quint16)string.length(); \
WRITE_VAR(file, _length); \
WRITE_STR(file, string); \
}
#define WRITE_BINARY_DATA_WITH_LENGTH(file, data) \
{ \
quint16 _length = (quint16)data.size(); \
WRITE_VAR(file, _length); \
WRITE_BINARY_DATA(file, data); \
}
#define READ_TO_BUF(file, size) \
{ \
if( file.read(buffer, (size)) != (size)) \
{ \
file.close(); \
fatalExit("Failed to read from file"); \
} \
}
#define READ_TO_STRING(file, dest, size) \
{ \
READ_TO_BUF((file), (size)); \
dest = QString::fromAscii(buffer, (size)); \
}
#define READ_TO_BYTE_ARRAY(file, dest, size) \
{ \
dest = file.read((size)); \
}
#define APPEND_ATTRIBUTE_DEC(xml, node, name, var) \
{ \
QDomAttr _tmpAttr = xml.createAttribute((name)); \
_tmpAttr.setValue(QString().sprintf("%d", (var))); \
node.setAttributeNode(_tmpAttr); \
}
#define APPEND_ATTRIBUTE_HEX(xml, node, name, var) \
{ \
QDomAttr _tmpAttr = xml.createAttribute((name)); \
_tmpAttr.setValue(QString().sprintf("0x%x", (var))); \
node.setAttributeNode(_tmpAttr); \
}
#define APPEND_ATTRIBUTE_STR(xml, node, name, var) \
{ \
QDomAttr _tmpAttr = xml.createAttribute((name)); \
_tmpAttr.setValue((var)); \
node.setAttributeNode(_tmpAttr); \
}
#define ATTRIBUTE_TO_INT(node, name, dest, type) \
{ \
QString _tmpAttrString = node.attribute((name)); \
if(_tmpAttrString.isNull()) \
{ \
fatalExit("Attribute [" name "] not found!"); \
} \
bool ok = false; \
dest = (type)_tmpAttrString.toInt(&ok, 0); \
if(!ok) \
{ \
fatalExit("Attribute [" name "] has invalid value!"); \
} \
}
#define ATTRIBUTE_TO_INT_NONZERO(node, name, dest, type) \
{ \
ATTRIBUTE_TO_INT(node, name, dest, type); \
assert((dest) && "Expected to be nonzero"); \
}
#define ATTRIBUTE_TO_UINT(node, name, dest, type) \
{ \
QString _tmpAttrString = node.attribute((name)); \
if(_tmpAttrString.isNull()) \
{ \
fatalExit("Attribute [" name "] not found!"); \
} \
bool ok = false; \
dest = (type)_tmpAttrString.toUInt(&ok, 0); \
if(!ok) \
{ \
fatalExit("Attribute [" name "] has invalid value!"); \
} \
}
#define ATTRIBUTE_TO_UINT_NONZERO(node, name, dest, type) \
{ \
ATTRIBUTE_TO_UINT(node, name, dest, type); \
assert((dest) && "Expected to be nonzero"); \
}
#define ATTRIBUTE_TO_STRING_NOT_EMPTY(node, name, var) \
{ \
var = node.attribute((name)); \
if(var.isNull()) \
{ \
fatalExit("Attribute [" name "] not found or empty!"); \
} \
}
#define GET_NODE(root, name, var) \
{ \
var = root.firstChildElement(name); \
if (var.isNull()) \
{ \
fatalExit("Cannot find <" name "> node!"); \
} \
}
#define GET_FIRST_CHILD_NODE(root, var) \
{ \
var = root.firstChildElement(); \
if (var.isNull()) \
{ \
fatalExit("Cannot find a child node!"); \
} \
}
#define GET_NODE_TEXT(node, dest) \
{ \
dest = node.text(); \
}
#define GET_NODE_BINARY_DATA(node, dest) \
{ \
dest = QByteArray::fromBase64(node.text().toAscii()); \
}
#endif // XMLHELPERS_H