Line data Source code
1 : /* Copyright (C) 2021 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 : ERROR_TYPE(Xeromyces, XMLValidationFailed);
32 :
33 : #include "ps/XMB/XMBData.h"
34 : #include "ps/XMB/XMBStorage.h"
35 :
36 : #include "lib/file/vfs/vfs.h"
37 :
38 : class RelaxNGValidator;
39 :
40 992 : class CXeromyces : public XMBData
41 : {
42 : friend class TestXMBData;
43 : friend class XMBData;
44 : public:
45 : /**
46 : * Load from an XML file (with invisible XMB caching).
47 : */
48 : PSRETURN Load(const PIVFS& vfs, const VfsPath& filename, const std::string& validatorName = "");
49 :
50 : /**
51 : * Load from an in-memory XML string (with no caching).
52 : */
53 : PSRETURN LoadString(const char* xml, const std::string& validatorName = "");
54 :
55 : /**
56 : * Convert the given XML file into an XMB in the archive cache.
57 : * Returns the XMB path in @p archiveCachePath.
58 : * Returns false on error.
59 : */
60 : bool GenerateCachedXMB(const PIVFS& vfs, const VfsPath& sourcePath, VfsPath& archiveCachePath, const std::string& validatorName = "");
61 :
62 : /**
63 : * Call once when initialising the program, to load libxml2.
64 : * This should be run in the main thread, before any thread uses libxml2.
65 : */
66 : static void Startup();
67 :
68 : /**
69 : * Call once when shutting down the program, to unload libxml2.
70 : */
71 : static void Terminate();
72 :
73 : static bool AddValidator(const PIVFS& vfs, const std::string& name, const VfsPath& grammarPath);
74 :
75 : static bool ValidateEncoded(const std::string& name, const std::string& filename, const std::string& document);
76 :
77 : private:
78 : static RelaxNGValidator& GetValidator(const std::string& name);
79 :
80 : PSRETURN ConvertFile(const PIVFS& vfs, const VfsPath& filename, const VfsPath& xmbPath, const std::string& validatorName);
81 :
82 : XMBStorage m_Data;
83 : };
84 :
85 :
86 : #define _XERO_MAKE_UID2__(p,l) p ## l
87 : #define _XERO_MAKE_UID1__(p,l) _XERO_MAKE_UID2__(p,l)
88 :
89 : #define _XERO_CHILDREN _XERO_MAKE_UID1__(_children_, __LINE__)
90 : #define _XERO_I _XERO_MAKE_UID1__(_i_, __LINE__)
91 :
92 : #define XERO_ITER_EL(parent_element, child_element) \
93 : for (XMBElement child_element : parent_element.GetChildNodes())
94 :
95 : #define XERO_ITER_ATTR(parent_element, attribute) \
96 : for (XMBAttribute attribute : parent_element.GetAttributes())
97 :
98 : #endif // INCLUDED_XEROMYCES
|