FBXParser.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file FBXParser.cpp
  34. * @brief Implementation of the FBX parser and the rudimentary DOM that we use
  35. */
  36. #include "AssimpPCH.h"
  37. #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
  38. #include "FBXTokenizer.h"
  39. #include "FBXParser.h"
  40. #include "FBXUtil.h"
  41. #include "ParsingUtils.h"
  42. #include "fast_atof.h"
  43. using namespace Assimp;
  44. using namespace Assimp::FBX;
  45. namespace {
  46. // ------------------------------------------------------------------------------------------------
  47. // signal parsing error, this is always unrecoverable. Throws DeadlyImportError.
  48. void ParseError(const std::string& message, TokenPtr token)
  49. {
  50. throw DeadlyImportError(token ? Util::AddTokenText("FBX-Parse",message,token) : ("FBX-Parse " + message));
  51. }
  52. }
  53. namespace Assimp {
  54. namespace FBX {
  55. // ------------------------------------------------------------------------------------------------
  56. Element::Element(const Token& key_token, Parser& parser)
  57. : key_token(key_token)
  58. {
  59. TokenPtr n = NULL;
  60. do {
  61. n = parser.AdvanceToNextToken();
  62. if(!n) {
  63. ParseError("unexpected end of file, expected closing bracket",parser.LastToken());
  64. }
  65. if (n->Type() == TokenType_DATA) {
  66. tokens.push_back(n);
  67. n = parser.AdvanceToNextToken();
  68. if(!n) {
  69. ParseError("unexpected end of file, expected bracket, comma or key",parser.LastToken());
  70. }
  71. const TokenType ty = n->Type();
  72. if (ty != TokenType_OPEN_BRACKET && ty != TokenType_CLOSE_BRACKET && ty != TokenType_COMMA && ty != TokenType_KEY) {
  73. ParseError("unexpected token; expected bracket, comma or key",n);
  74. }
  75. }
  76. if (n->Type() == TokenType_OPEN_BRACKET) {
  77. compound.reset(new Scope(parser));
  78. // current token should be a TOK_CLOSE_BRACKET
  79. n = parser.CurrentToken();
  80. ai_assert(n);
  81. if (n->Type() != TokenType_CLOSE_BRACKET) {
  82. ParseError("expected closing bracket",n);
  83. }
  84. parser.AdvanceToNextToken();
  85. return;
  86. }
  87. }
  88. while(n->Type() != TokenType_KEY && n->Type() != TokenType_CLOSE_BRACKET);
  89. }
  90. // ------------------------------------------------------------------------------------------------
  91. Element::~Element()
  92. {
  93. // no need to delete tokens, they are owned by the parser
  94. }
  95. // ------------------------------------------------------------------------------------------------
  96. Scope::Scope(Parser& parser,bool topLevel)
  97. {
  98. if(!topLevel) {
  99. TokenPtr t = parser.CurrentToken();
  100. if (t->Type() != TokenType_OPEN_BRACKET) {
  101. ParseError("expected open bracket",t);
  102. }
  103. }
  104. TokenPtr n = parser.AdvanceToNextToken();
  105. if(n == NULL) {
  106. ParseError("unexpected end of file",NULL);
  107. }
  108. // note: empty scopes are allowed
  109. while(n->Type() != TokenType_CLOSE_BRACKET) {
  110. if (n->Type() != TokenType_KEY) {
  111. ParseError("unexpected token, expected TOK_KEY",n);
  112. }
  113. const std::string& str = n->StringContents();
  114. elements.insert(ElementMap::value_type(str,new_Element(*n,parser)));
  115. // Element() should stop at the next Key token (or right after a Close token)
  116. n = parser.CurrentToken();
  117. if(n == NULL) {
  118. if (topLevel) {
  119. return;
  120. }
  121. ParseError("unexpected end of file",parser.LastToken());
  122. }
  123. }
  124. }
  125. // ------------------------------------------------------------------------------------------------
  126. Scope::~Scope()
  127. {
  128. BOOST_FOREACH(ElementMap::value_type& v, elements) {
  129. delete v.second;
  130. }
  131. }
  132. // ------------------------------------------------------------------------------------------------
  133. Parser::Parser (const TokenList& tokens)
  134. : tokens(tokens)
  135. , cursor(tokens.begin())
  136. , current()
  137. , last()
  138. {
  139. root.reset(new Scope(*this,true));
  140. }
  141. // ------------------------------------------------------------------------------------------------
  142. Parser::~Parser()
  143. {
  144. }
  145. // ------------------------------------------------------------------------------------------------
  146. TokenPtr Parser::AdvanceToNextToken()
  147. {
  148. last = current;
  149. if (cursor == tokens.end()) {
  150. current = NULL;
  151. }
  152. else {
  153. current = *cursor++;
  154. }
  155. return current;
  156. }
  157. // ------------------------------------------------------------------------------------------------
  158. TokenPtr Parser::CurrentToken() const
  159. {
  160. return current;
  161. }
  162. // ------------------------------------------------------------------------------------------------
  163. TokenPtr Parser::LastToken() const
  164. {
  165. return last;
  166. }
  167. // ------------------------------------------------------------------------------------------------
  168. uint64_t ParseTokenAsID(const Token& t, const char*& err_out)
  169. {
  170. err_out = NULL;
  171. if (t.Type() != TokenType_DATA) {
  172. err_out = "expected TOK_DATA token";
  173. return 0L;
  174. }
  175. // XXX: should use size_t here
  176. unsigned int length = static_cast<unsigned int>(t.end() - t.begin());
  177. ai_assert(length > 0);
  178. const char* out;
  179. const uint64_t id = strtoul10_64(t.begin(),&out,&length);
  180. if (out != t.end()) {
  181. err_out = "failed to parse ID";
  182. return 0L;
  183. }
  184. return id;
  185. }
  186. // ------------------------------------------------------------------------------------------------
  187. uint64_t ParseTokenAsDim(const Token& t, const char*& err_out)
  188. {
  189. // same as ID parsing, except there is a trailing asterisk
  190. err_out = NULL;
  191. if (t.Type() != TokenType_DATA) {
  192. err_out = "expected TOK_DATA token";
  193. return 0L;
  194. }
  195. if(*t.begin() != '*') {
  196. err_out = "expected asterisk before array dimension";
  197. return 0L;
  198. }
  199. // XXX: should use size_t here
  200. unsigned int length = static_cast<unsigned int>(t.end() - t.begin());
  201. if(length == 0) {
  202. err_out = "expected valid integer number after asterisk";
  203. return 0L;
  204. }
  205. const char* out;
  206. const uint64_t id = strtoul10_64(t.begin() + 1,&out,&length);
  207. if (out != t.end()) {
  208. err_out = "failed to parse ID";
  209. return 0L;
  210. }
  211. return id;
  212. }
  213. // ------------------------------------------------------------------------------------------------
  214. float ParseTokenAsFloat(const Token& t, const char*& err_out)
  215. {
  216. err_out = NULL;
  217. if (t.Type() != TokenType_DATA) {
  218. err_out = "expected TOK_DATA token";
  219. return 0.0f;
  220. }
  221. const char* inout = t.begin();
  222. float f;
  223. fast_atof(&inout);
  224. if (inout != t.end()) {
  225. err_out = "failed to parse floating point number";
  226. return 0.0f;
  227. }
  228. return f;
  229. }
  230. // ------------------------------------------------------------------------------------------------
  231. int ParseTokenAsInt(const Token& t, const char*& err_out)
  232. {
  233. err_out = NULL;
  234. if (t.Type() != TokenType_DATA) {
  235. err_out = "expected TOK_DATA token";
  236. return 0;
  237. }
  238. ai_assert(static_cast<size_t>(t.end() - t.begin()) > 0);
  239. const char* out;
  240. const int intval = strtol10(t.begin(),&out);
  241. if (out != t.end()) {
  242. err_out = "failed to parse ID";
  243. return 0;
  244. }
  245. return intval;
  246. }
  247. // ------------------------------------------------------------------------------------------------
  248. std::string ParseTokenAsString(const Token& t, const char*& err_out)
  249. {
  250. err_out = NULL;
  251. if (t.Type() != TokenType_DATA) {
  252. err_out = "expected TOK_DATA token";
  253. return "";
  254. }
  255. const size_t length = static_cast<size_t>(t.end() - t.begin());
  256. if(length < 2) {
  257. err_out = "token is too short to hold a string";
  258. return "";
  259. }
  260. const char* s = t.begin(), *e = t.end() - 1;
  261. if (*s != '\"' || *e != '\*') {
  262. err_out = "expected double quoted string";
  263. return "";
  264. }
  265. return std::string(s+1,length-2);
  266. }
  267. } // !FBX
  268. } // !Assimp
  269. #endif