Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Xeromyces.h
Go to the documentation of this file.
1 /* Copyright (C) 2010 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  Xeromyces file-loading interface.
20  Automatically creates and caches relatively
21  efficient binary representations of XML files.
22 */
23 
24 #ifndef INCLUDED_XEROMYCES
25 #define INCLUDED_XEROMYCES
26 
27 #include "ps/Errors.h"
28 ERROR_GROUP(Xeromyces);
29 ERROR_TYPE(Xeromyces, XMLOpenFailed);
30 ERROR_TYPE(Xeromyces, XMLParseError);
31 
32 #include "XeroXMB.h"
33 
34 #include "lib/file/vfs/vfs.h"
35 
36 class WriteBuffer;
37 class MD5;
38 
39 typedef struct _xmlDoc xmlDoc;
40 typedef xmlDoc* xmlDocPtr;
41 
42 class CXeromyces : public XMBFile
43 {
44  friend class TestXeroXMB;
45 public:
46  /**
47  * Load from an XML file (with invisible XMB caching).
48  */
49  PSRETURN Load(const PIVFS& vfs, const VfsPath& filename);
50 
51  /**
52  * Load from an in-memory XML string (with no caching).
53  */
54  PSRETURN LoadString(const char* xml);
55 
56  /**
57  * Convert the given XML file into an XMB in the archive cache.
58  * Returns the XMB path in @p archiveCachePath.
59  * Returns false on error.
60  */
61  bool GenerateCachedXMB(const PIVFS& vfs, const VfsPath& sourcePath, VfsPath& archiveCachePath);
62 
63  /**
64  * Call once when initialising the program, to load libxml2.
65  * This should be run in the main thread, before any thread uses libxml2.
66  */
67  static void Startup();
68 
69  /**
70  * Call once when shutting down the program, to unload libxml2.
71  */
72  static void Terminate();
73 
74 private:
75  PSRETURN ConvertFile(const PIVFS& vfs, const VfsPath& filename, const VfsPath& xmbPath);
76 
77  bool ReadXMBFile(const PIVFS& vfs, const VfsPath& filename);
78 
79  static PSRETURN CreateXMB(const xmlDocPtr doc, WriteBuffer& writeBuffer);
80 
81  shared_ptr<u8> m_XMBBuffer;
82 };
83 
84 
85 #define _XERO_MAKE_UID2__(p,l) p ## l
86 #define _XERO_MAKE_UID1__(p,l) _XERO_MAKE_UID2__(p,l)
87 
88 #define _XERO_CHILDREN _XERO_MAKE_UID1__(_children_, __LINE__)
89 #define _XERO_I _XERO_MAKE_UID1__(_i_, __LINE__)
90 
91 #define XERO_ITER_EL(parent_element, child_element) \
92  XMBElementList _XERO_CHILDREN = parent_element.GetChildNodes(); \
93  XMBElement child_element (0); \
94  for (int _XERO_I = 0; \
95  _XERO_I < _XERO_CHILDREN.Count \
96  && (child_element = _XERO_CHILDREN.Item(_XERO_I), 1); \
97  ++_XERO_I)
98 
99 #define XERO_ITER_ATTR(parent_element, attribute) \
100  XMBAttributeList _XERO_CHILDREN = parent_element.GetAttributes(); \
101  XMBAttribute attribute; \
102  for (int _XERO_I = 0; \
103  _XERO_I < _XERO_CHILDREN.Count \
104  && (attribute = _XERO_CHILDREN.Item(_XERO_I), 1); \
105  ++_XERO_I)
106 
107 #endif // INCLUDED_XEROMYCES
PSRETURN Load(const PIVFS &vfs, const VfsPath &filename)
Load from an XML file (with invisible XMB caching).
Definition: Xeromyces.cpp:65
shared_ptr< IVFS > PIVFS
Definition: vfs.h:226
xmlDoc * xmlDocPtr
Definition: Xeromyces.h:40
tuple xml
Definition: tests.py:119
#define ERROR_TYPE(a, b)
Definition: Errors.h:96
#define ERROR_GROUP(a)
Definition: Errors.h:87
friend class TestXeroXMB
Definition: Xeromyces.h:44
bool ReadXMBFile(const PIVFS &vfs, const VfsPath &filename)
Definition: Xeromyces.cpp:148
MD5 hashing algorithm.
Definition: MD5.h:27
u32 PSRETURN
Definition: Errors.h:75
Definition: path.h:75
bool GenerateCachedXMB(const PIVFS &vfs, const VfsPath &sourcePath, VfsPath &archiveCachePath)
Convert the given XML file into an XMB in the archive cache.
Definition: Xeromyces.cpp:104
PSRETURN LoadString(const char *xml)
Load from an in-memory XML string (with no caching).
Definition: Xeromyces.cpp:167
static void Terminate()
Call once when shutting down the program, to unload libxml2.
Definition: Xeromyces.cpp:57
shared_ptr< u8 > m_XMBBuffer
Definition: Xeromyces.h:81
struct _xmlDoc xmlDoc
Definition: Xeromyces.h:39
static void Startup()
Call once when initialising the program, to load libxml2.
Definition: Xeromyces.cpp:49
PSRETURN ConvertFile(const PIVFS &vfs, const VfsPath &filename, const VfsPath &xmbPath)
Definition: Xeromyces.cpp:113
static PSRETURN CreateXMB(const xmlDocPtr doc, WriteBuffer &writeBuffer)
Definition: Xeromyces.cpp:314