1/*
2Open Asset Import Library (assimp)
3----------------------------------------------------------------------
4
5Copyright (c) 2006-2017, assimp team
6
7All rights reserved.
8
9Redistribution and use of this software in source and binary forms,
10with or without modification, are permitted provided that the
11following conditions are met:
12
13* Redistributions of source code must retain the above
14 copyright notice, this list of conditions and the
15 following disclaimer.
16
17* Redistributions in binary form must reproduce the above
18 copyright notice, this list of conditions and the
19 following disclaimer in the documentation and/or other
20 materials provided with the distribution.
21
22* Neither the name of the assimp team, nor the names of its
23 contributors may be used to endorse or promote products
24 derived from this software without specific prior
25 written permission of the assimp team.
26
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39----------------------------------------------------------------------
40*/
41
42/** @file FBXParser.h
43 * @brief FBX parsing code
44 */
45#ifndef INCLUDED_AI_FBX_PARSER_H
46#define INCLUDED_AI_FBX_PARSER_H
47
48#include <stdint.h>
49#include <map>
50#include <memory>
51#include "LogAux.h"
52#include "fast_atof.h"
53
54#include "FBXCompileConfig.h"
55#include "FBXTokenizer.h"
56
57namespace Assimp {
58namespace FBX {
59
60class Scope;
61class Parser;
62class Element;
63
64// XXX should use C++11's unique_ptr - but assimp's need to keep working with 03
65typedef std::vector< Scope* > ScopeList;
66typedef std::fbx_unordered_multimap< std::string, Element* > ElementMap;
67
68typedef std::pair<ElementMap::const_iterator,ElementMap::const_iterator> ElementCollection;
69
70# define new_Scope new Scope
71# define new_Element new Element
72
73
74/** FBX data entity that consists of a key:value tuple.
75 *
76 * Example:
77 * @verbatim
78 * AnimationCurve: 23, "AnimCurve::", "" {
79 * [..]
80 * }
81 * @endverbatim
82 *
83 * As can be seen in this sample, elements can contain nested #Scope
84 * as their trailing member. **/
85class Element
86{
87public:
88 Element(const Token& key_token, Parser& parser);
89 ~Element();
90
91 const Scope* Compound() const {
92 return compound.get();
93 }
94
95 const Token& KeyToken() const {
96 return key_token;
97 }
98
99 const TokenList& Tokens() const {
100 return tokens;
101 }
102
103private:
104 const Token& key_token;
105 TokenList tokens;
106 std::unique_ptr<Scope> compound;
107};
108
109/** FBX data entity that consists of a 'scope', a collection
110 * of not necessarily unique #Element instances.
111 *
112 * Example:
113 * @verbatim
114 * GlobalSettings: {
115 * Version: 1000
116 * Properties70:
117 * [...]
118 * }
119 * @endverbatim */
120class Scope
121{
122public:
123 Scope(Parser& parser, bool topLevel = false);
124 ~Scope();
125
126 const Element* operator[] (const std::string& index) const {
127 ElementMap::const_iterator it = elements.find(index);
128 return it == elements.end() ? NULL : (*it).second;
129 }
130
131 const Element* FindElementCaseInsensitive(const std::string& elementName) const {
132 const char* elementNameCStr = elementName.c_str();
133 for (auto element = elements.begin(); element != elements.end(); ++element)
134 {
135 if (!ASSIMP_strincmp(element->first.c_str(), elementNameCStr, MAXLEN)) {
136 return element->second;
137 }
138 }
139 return NULL;
140 }
141
142 ElementCollection GetCollection(const std::string& index) const {
143 return elements.equal_range(index);
144 }
145
146 const ElementMap& Elements() const {
147 return elements;
148 }
149
150private:
151 ElementMap elements;
152};
153
154/** FBX parsing class, takes a list of input tokens and generates a hierarchy
155 * of nested #Scope instances, representing the fbx DOM.*/
156class Parser
157{
158public:
159 /** Parse given a token list. Does not take ownership of the tokens -
160 * the objects must persist during the entire parser lifetime */
161 Parser (const TokenList& tokens,bool is_binary);
162 ~Parser();
163
164 const Scope& GetRootScope() const {
165 return *root.get();
166 }
167
168 bool IsBinary() const {
169 return is_binary;
170 }
171
172private:
173 friend class Scope;
174 friend class Element;
175
176 TokenPtr AdvanceToNextToken();
177
178 TokenPtr LastToken() const;
179 TokenPtr CurrentToken() const;
180
181
182private:
183 const TokenList& tokens;
184
185 TokenPtr last, current;
186 TokenList::const_iterator cursor;
187 std::unique_ptr<Scope> root;
188
189 const bool is_binary;
190};
191
192
193/* token parsing - this happens when building the DOM out of the parse-tree*/
194uint64_t ParseTokenAsID(const Token& t, const char*& err_out);
195size_t ParseTokenAsDim(const Token& t, const char*& err_out);
196
197float ParseTokenAsFloat(const Token& t, const char*& err_out);
198int ParseTokenAsInt(const Token& t, const char*& err_out);
199int64_t ParseTokenAsInt64(const Token& t, const char*& err_out);
200std::string ParseTokenAsString(const Token& t, const char*& err_out);
201
202
203/* wrapper around ParseTokenAsXXX() with DOMError handling */
204uint64_t ParseTokenAsID(const Token& t);
205size_t ParseTokenAsDim(const Token& t);
206float ParseTokenAsFloat(const Token& t);
207int ParseTokenAsInt(const Token& t);
208int64_t ParseTokenAsInt64(const Token& t);
209std::string ParseTokenAsString(const Token& t);
210
211/* read data arrays */
212void ParseVectorDataArray(std::vector<aiVector3D>& out, const Element& el);
213void ParseVectorDataArray(std::vector<aiColor4D>& out, const Element& el);
214void ParseVectorDataArray(std::vector<aiVector2D>& out, const Element& el);
215void ParseVectorDataArray(std::vector<int>& out, const Element& el);
216void ParseVectorDataArray(std::vector<float>& out, const Element& el);
217void ParseVectorDataArray(std::vector<unsigned int>& out, const Element& el);
218void ParseVectorDataArray(std::vector<uint64_t>& out, const Element& e);
219void ParseVectorDataArray(std::vector<int64_t>& out, const Element& el);
220
221// extract a required element from a scope, abort if the element cannot be found
222const Element& GetRequiredElement(const Scope& sc, const std::string& index, const Element* element = NULL);
223
224// extract required compound scope
225const Scope& GetRequiredScope(const Element& el);
226// get token at a particular index
227const Token& GetRequiredToken(const Element& el, unsigned int index);
228
229// read a 4x4 matrix from an array of 16 floats
230aiMatrix4x4 ReadMatrix(const Element& element);
231
232} // ! FBX
233} // ! Assimp
234
235#endif // ! INCLUDED_AI_FBX_PARSER_H
236