| |
1 |
1 |
#include "cfg_xml.h" |
| |
2 |
2 |
#include <expat.h> |
| |
3 |
3 |
#include "exception.h" |
| |
4 |
4 |
#include <string> |
| |
5 |
5 |
#include <map> |
| |
6 |
6 |
#include <assert.h> |
| |
7 |
7 |
|
| |
8 |
8 |
BEGIN_ICPF_NAMESPACE |
| |
9 |
9 |
|
| |
10 |
10 |
|
| |
11 |
11 |
#define XML_BUFFER 65536 |
| |
12 |
12 |
|
| |
|
13 |
|
| |
|
14 |
#if defined(_WIN32) || defined(_WIN64) |
| |
|
15 |
#define ENDL _t("\r\n") |
| |
|
16 |
#else |
| |
|
17 |
#define ENDL _t("\n") |
| |
|
18 |
#endif |
| |
|
19 |
|
| |
13 |
20 |
|
| |
14 |
21 |
class xml_node; |
| |
15 |
22 |
|
| |
16 |
23 |
|
| |
17 |
24 |
typedef std::map<tstring_t, xml_node> xml_storage; |
| |
18 |
25 |
|
| |
19 |
26 |
typedef std::multimap<tstring_t, tstring_t> attr_storage; |
| |
20 |
27 |
|
| |
21 |
28 |
|
| |
22 |
29 |
|
| |
23 |
30 |
class xml_node |
| |
24 |
31 |
{ |
| |
25 |
32 |
public: |
| |
26 |
33 |
|
| |
27 |
34 |
|
| |
28 |
35 |
|
| |
29 |
36 |
xml_node() : m_mNodes(), m_mAttr(), m_pParentNode(NULL) { }; |
| |
30 |
37 |
|
| |
31 |
38 |
xml_node(xml_node* pParentNode) : m_mNodes(), m_mAttr(), m_pParentNode(pParentNode) { }; |
| |
32 |
39 |
|
|
| |
178 |
185 |
|
| |
179 |
186 |
XML_ParserFree(parser); |
| |
180 |
187 |
|
| |
181 |
188 |
|
| |
182 |
189 |
fclose(pFile); |
| |
183 |
190 |
} |
| |
184 |
191 |
|
| |
185 |
192 |
|
| |
186 |
193 |
|
| |
187 |
194 |
|
| |
188 |
195 |
|
| |
189 |
196 |
|
| |
190 |
197 |
|
| |
191 |
198 |
void xml_cfg::save(const tchar_t* pszPath) |
| |
192 |
199 |
{ |
| |
193 |
200 |
|
| |
194 |
201 |
FILE* pFile=_tfopen(pszPath, _t("wb")); |
| |
195 |
202 |
if (pFile == NULL) |
| |
196 |
203 |
THROW(icpf::exception::format(_t("Cannot open the file ") TSTRFMT _t(" for writing."), pszPath), 0, errno, 0); |
| |
197 |
204 |
|
| |
|
205 |
|
| |
|
206 |
#if defined(_UNICODE) && (defined(_WIN32) || defined(_WIN64)) |
| |
|
207 |
|
| |
|
208 |
uint_t uiBOM=0x0000feff; |
| |
|
209 |
uint_t uiCount=2; |
| |
|
210 |
#else |
| |
|
211 |
|
| |
|
212 |
uint_t uiBOM=0x00bfbbef; |
| |
|
213 |
uint_t uiCount=3; |
| |
|
214 |
#endif |
| |
|
215 |
|
| |
|
216 |
try |
| |
|
217 |
{ |
| |
|
218 |
|
| |
|
219 |
if (fwrite(&uiBOM, 1, uiCount, pFile) != uiCount) |
| |
|
220 |
THROW(_t("Cannot write the BOM to the file '") TSTRFMT _t("'"), 0, errno, 0); |
| |
|
221 |
|
| |
198 |
222 |
|
| |
199 |
223 |
save_node(pFile, m_pMainNode); |
| |
|
224 |
} |
| |
|
225 |
catch(...) |
| |
|
226 |
{ |
| |
|
227 |
fclose(pFile); |
| |
|
228 |
throw; |
| |
|
229 |
} |
| |
200 |
230 |
|
| |
201 |
231 |
|
| |
202 |
232 |
fclose(pFile); |
| |
203 |
233 |
} |
| |
204 |
234 |
|
| |
205 |
235 |
void xml_cfg::save_node(FILE* pFile, ptr_t pNodePtr) |
| |
206 |
236 |
{ |
| |
207 |
237 |
xml_node* pNode=(xml_node*)pNodePtr; |
| |
208 |
238 |
|
| |
209 |
239 |
|
| |
210 |
240 |
for (attr_storage::iterator it=pNode->m_mAttr.begin();it != pNode->m_mAttr.end();it++) |
| |
211 |
241 |
{ |
| |
212 |
|
_ftprintf(pFile, _t("<") TSTRFMT _t(" value=\"") TSTRFMT _t("\"/>\n"), (*it).first, (*it).second.c_str()); |
| |
|
242 |
const tchar_t pszFmt[]=_t("<") TSTRFMT _t(" value=\"") TSTRFMT _t("\"/>") ENDL; |
| |
|
243 |
|
| |
|
244 |
int_t iSize=_sctprintf(pszFmt, (*it).first.c_str(), (*it).second.c_str()); |
| |
|
245 |
|
| |
|
246 |
if (_ftprintf(pFile, pszFmt, (*it).first.c_str(), (*it).second.c_str()) < 0) |
| |
|
247 |
THROW(_t("Cannot write requested data to the file"), 0, errno, 0); |
| |
213 |
248 |
} |
| |
214 |
249 |
|
| |
215 |
250 |
|
| |
216 |
251 |
for (xml_storage::iterator it=pNode->m_mNodes.begin();it != pNode->m_mNodes.end();it++) |
| |
217 |
252 |
{ |
| |
218 |
253 |
|
| |
219 |
|
_ftprintf(pFile, _t("<") TSTRFMT _t(">\n"), (*it).first); |
| |
|
254 |
if (_ftprintf(pFile, _t("<") TSTRFMT _t(">") ENDL, (*it).first.c_str()) < 0) |
| |
|
255 |
THROW(_t("Cannot write requested data to the file"), 0, errno, 0); |
| |
220 |
256 |
|
| |
221 |
257 |
save_node(pFile, &(*it).second); |
| |
222 |
258 |
|
| |
223 |
|
_ftprintf(pFile, _t("</") TSTRFMT _t(">\n"), (*it).first); |
| |
|
259 |
if (_ftprintf(pFile, _t("</") TSTRFMT _t(">") ENDL, (*it).first.c_str()) < 0) |
| |
|
260 |
THROW(_t("Cannot write requested data to the file"), 0, errno, 0); |
| |
224 |
261 |
} |
| |
225 |
262 |
} |
| |
226 |
263 |
|
| |
|
264 |
void xml_cfg::fprintf_utf8(FILE* pFile, const tchar_t* pszFmt, ...) |
| |
|
265 |
{ |
| |
|
266 |
va_list va; |
| |
|
267 |
va_start(va, pszFmt); |
| |
|
268 |
|
| |
|
269 |
|
| |
|
270 |
int_t iCount=_vsctprintf(pszFmt, va); |
| |
|
271 |
tchar_t* pszFormatted=new tchar_t[iCount+1]; |
| |
|
272 |
|
| |
|
273 |
|
| |
|
274 |
va_start(va, pszFmt); |
| |
|
275 |
_vsntprintf(pszFormatted, iCount, pszFmt, va); |
| |
|
276 |
|
| |
|
277 |
|
| |
|
278 |
delete [] pszFormatted; |
| |
|
279 |
|
| |
|
280 |
va_end(va); |
| |
|
281 |
|
| |
|
282 |
} |
| |
|
283 |
|
| |
227 |
284 |
|
| |
228 |
285 |
|
| |
229 |
286 |
|
| |
230 |
287 |
|
| |
231 |
288 |
|
| |
232 |
289 |
|
| |
233 |
290 |
|
| |
234 |
291 |
|
| |
235 |
292 |
|
| |
236 |
293 |
|
| |
237 |
294 |
|
| |
238 |
295 |
|
| |
239 |
296 |
|
| |
240 |
297 |
|
| |
241 |
298 |
|
| |
242 |
299 |
ptr_t xml_cfg::find(const tchar_t* pszName) |
| |
243 |
300 |
{ |
| |
244 |
301 |
return find(m_pMainNode, pszName); |
| |
245 |
302 |
} |
| |
246 |
303 |
|