1 | // |
2 | // Redistribution and use in source and binary forms, with or without |
3 | // modification, are permitted provided that the following conditions |
4 | // are met: |
5 | // * Redistributions of source code must retain the above copyright |
6 | // notice, this list of conditions and the following disclaimer. |
7 | // * Redistributions in binary form must reproduce the above copyright |
8 | // notice, this list of conditions and the following disclaimer in the |
9 | // documentation and/or other materials provided with the distribution. |
10 | // * Neither the name of NVIDIA CORPORATION nor the names of its |
11 | // contributors may be used to endorse or promote products derived |
12 | // from this software without specific prior written permission. |
13 | // |
14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
15 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
18 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | // |
26 | // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved. |
27 | // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. |
28 | // Copyright (c) 2001-2004 NovodeX AG. All rights reserved. |
29 | |
30 | #ifndef PSFASTXML_PSFASTXML_H |
31 | #define PSFASTXML_PSFASTXML_H |
32 | |
33 | #include "foundation/PxSimpleTypes.h" // defines basic data types; modify for your platform as needed. |
34 | #include "foundation/PxIO.h" |
35 | #include "foundation/PxAssert.h" |
36 | #include "PsAllocator.h" |
37 | |
38 | namespace physx |
39 | { |
40 | namespace shdfnd |
41 | { |
42 | |
43 | class FastXml |
44 | { |
45 | PX_NOCOPY(FastXml) |
46 | |
47 | public: |
48 | class AttributePairs |
49 | { |
50 | int argc; |
51 | const char** argv; |
52 | |
53 | public: |
54 | AttributePairs() : argc(0), argv(NULL) |
55 | { |
56 | } |
57 | AttributePairs(int c, const char** v) : argc(c), argv(v) |
58 | { |
59 | } |
60 | |
61 | PX_INLINE int getNbAttr() const |
62 | { |
63 | return argc / 2; |
64 | } |
65 | |
66 | const char* getKey(uint32_t index) const |
67 | { |
68 | PX_ASSERT((index * 2) < uint32_t(argc)); |
69 | return argv[index * 2]; |
70 | } |
71 | |
72 | const char* getValue(uint32_t index) const |
73 | { |
74 | PX_ASSERT((index * 2 + 1) < uint32_t(argc)); |
75 | return argv[index * 2 + 1]; |
76 | } |
77 | |
78 | const char* get(const char* attr) const |
79 | { |
80 | int32_t count = argc / 2; |
81 | for(int32_t i = 0; i < count; ++i) |
82 | { |
83 | const char* key = argv[i * 2], *value = argv[i * 2 + 1]; |
84 | if(strcmp(str1: key, str2: attr) == 0) |
85 | return value; |
86 | } |
87 | |
88 | return NULL; |
89 | } |
90 | }; |
91 | |
92 | /*** |
93 | * Callbacks to the user with the contents of the XML file properly digested. |
94 | */ |
95 | class Callback |
96 | { |
97 | public: |
98 | virtual ~Callback() |
99 | { |
100 | } |
101 | virtual bool (const char* ) = 0; // encountered a comment in the XML |
102 | |
103 | // 'element' is the name of the element that is being closed. |
104 | // depth is the recursion depth of this element. |
105 | // Return true to continue processing the XML file. |
106 | // Return false to stop processing the XML file; leaves the read pointer of the stream right after this close |
107 | // tag. |
108 | // The bool 'isError' indicates whether processing was stopped due to an error, or intentionally canceled early. |
109 | virtual bool processClose(const char* element, uint32_t depth, bool& isError) = 0; // process the 'close' |
110 | // indicator for a previously |
111 | // encountered element |
112 | |
113 | // return true to continue processing the XML document, false to skip. |
114 | virtual bool processElement(const char* elementName, // name of the element |
115 | const char* elementData, // element data, null if none |
116 | const AttributePairs& attr, // attributes |
117 | int32_t lineno) = 0; // line number in the source XML file |
118 | |
119 | // process the XML declaration header |
120 | virtual bool processXmlDeclaration(const AttributePairs&, // attributes |
121 | const char* /*elementData*/, int32_t /*lineno*/) |
122 | { |
123 | return true; |
124 | } |
125 | |
126 | virtual bool processDoctype(const char* /*rootElement*/, // Root element tag |
127 | const char* /*type*/, // SYSTEM or PUBLIC |
128 | const char* /*fpi*/, // Formal Public Identifier |
129 | const char* /*uri*/) // Path to schema file |
130 | { |
131 | return true; |
132 | } |
133 | |
134 | virtual void* allocate(uint32_t size) |
135 | { |
136 | return getAllocator().allocate(size, typeName: "FastXml" , __FILE__, __LINE__); |
137 | } |
138 | |
139 | virtual void deallocate(void* ptr) |
140 | { |
141 | getAllocator().deallocate(ptr); |
142 | } |
143 | }; |
144 | |
145 | virtual bool processXml(PxInputData& buff, bool streamFromMemory = false) = 0; |
146 | |
147 | virtual const char* getError(int32_t& lineno) = 0; // report the reason for a parsing error, and the line number |
148 | // where it occurred. |
149 | |
150 | FastXml() |
151 | { |
152 | } |
153 | |
154 | virtual void release(void) = 0; |
155 | |
156 | protected: |
157 | virtual ~FastXml() |
158 | { |
159 | } |
160 | }; |
161 | |
162 | FastXml* createFastXml(FastXml::Callback* iface); |
163 | |
164 | } // shdfnd |
165 | } // physx |
166 | |
167 | #endif // PSFASTXML_PSFASTXML_H |
168 | |