BaseImporter.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development Team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the ASSIMP team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the ASSIMP Development Team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Implementation of BaseImporter */
  35. #include "AssimpPCH.h"
  36. #include "BaseImporter.h"
  37. using namespace Assimp;
  38. // ------------------------------------------------------------------------------------------------
  39. // Constructor to be privately used by Importer
  40. BaseImporter::BaseImporter()
  41. {
  42. // nothing to do here
  43. }
  44. // ------------------------------------------------------------------------------------------------
  45. // Destructor, private as well
  46. BaseImporter::~BaseImporter()
  47. {
  48. // nothing to do here
  49. }
  50. // ------------------------------------------------------------------------------------------------
  51. // Imports the given file and returns the imported data.
  52. aiScene* BaseImporter::ReadFile( const std::string& pFile, IOSystem* pIOHandler)
  53. {
  54. // create a scene object to hold the data
  55. aiScene* scene = new aiScene();
  56. // dispatch importing
  57. try
  58. {
  59. InternReadFile( pFile, scene, pIOHandler);
  60. } catch( ImportErrorException* exception)
  61. {
  62. // extract error description
  63. mErrorText = exception->GetErrorText();
  64. DefaultLogger::get()->error(mErrorText);
  65. delete exception;
  66. // and kill the partially imported data
  67. delete scene;
  68. scene = NULL;
  69. }
  70. // return what we gathered from the import.
  71. return scene;
  72. }
  73. // ------------------------------------------------------------------------------------------------
  74. void BaseImporter::SetupProperties(const Importer* pImp)
  75. {
  76. // the default implementation does nothing
  77. }
  78. // ------------------------------------------------------------------------------------------------
  79. bool BaseImporter::SearchFileHeaderForToken(IOSystem* pIOHandler,
  80. const std::string& pFile,
  81. const char** tokens,
  82. unsigned int numTokens,
  83. unsigned int searchBytes /* = 200 */)
  84. {
  85. ai_assert(NULL != tokens && 0 != numTokens && NULL != pIOHandler && 0 != searchBytes);
  86. boost::scoped_ptr<IOStream> pStream (pIOHandler->Open(pFile));
  87. if (pStream.get() )
  88. {
  89. // read 200 characters from the file
  90. boost::scoped_ptr<char> _buffer (new char[searchBytes]);
  91. char* buffer = _buffer.get();
  92. unsigned int read = (unsigned int)pStream->Read(buffer,1,searchBytes);
  93. if (!read)return false;
  94. for (unsigned int i = 0; i < read;++i)
  95. buffer[i] = ::tolower(buffer[i]);
  96. // It is not a proper handling of unicode files here ...
  97. // ehm ... but it works in most cases.
  98. char* cur = buffer,*cur2 = buffer,*end = &buffer[read];
  99. while (cur != end)
  100. {
  101. if (*cur)*cur2++ = *cur;
  102. ++cur;
  103. }
  104. *cur2 = '\0';
  105. for (unsigned int i = 0; i < numTokens;++i)
  106. {
  107. ai_assert(NULL != tokens[i]);
  108. if (::strstr(buffer,tokens[i]))return true;
  109. }
  110. }
  111. return false;
  112. }
  113. // ------------------------------------------------------------------------------------------------
  114. // Represents an import request
  115. struct LoadRequest
  116. {
  117. LoadRequest(const std::string& _file, unsigned int _flags,const BatchLoader::PropertyMap* _map)
  118. : file (_file)
  119. , flags (_flags)
  120. , scene (NULL)
  121. , refCnt (1)
  122. , loaded (false)
  123. , map (*_map)
  124. {}
  125. const std::string file;
  126. unsigned int flags;
  127. unsigned int refCnt;
  128. aiScene* scene;
  129. bool loaded;
  130. BatchLoader::PropertyMap map;
  131. bool operator== (const std::string& f)
  132. {return file == f;}
  133. };
  134. // ------------------------------------------------------------------------------------------------
  135. // BatchLoader::pimpl data structure
  136. struct BatchData
  137. {
  138. // IO system to be used for all imports
  139. IOSystem* pIOSystem;
  140. // Importer used to load all meshes
  141. Importer* pImporter;
  142. // List of all imports
  143. std::list<LoadRequest> requests;
  144. };
  145. // ------------------------------------------------------------------------------------------------
  146. BatchLoader::BatchLoader(IOSystem* pIO)
  147. {
  148. ai_assert(NULL != pIO);
  149. pimpl = new BatchData();
  150. BatchData* data = ( BatchData* )pimpl;
  151. data->pIOSystem = pIO;
  152. data->pImporter = new Importer();
  153. }
  154. // ------------------------------------------------------------------------------------------------
  155. BatchLoader::~BatchLoader()
  156. {
  157. // delete all scenes wthat have not been polled by the user
  158. BatchData* data = ( BatchData* )pimpl;
  159. for (std::list<LoadRequest>::iterator it = data->requests.begin();
  160. it != data->requests.end(); ++it)
  161. {
  162. delete (*it).scene;
  163. }
  164. delete data->pImporter;
  165. delete data;
  166. }
  167. // ------------------------------------------------------------------------------------------------
  168. void BatchLoader::AddLoadRequest (const std::string& file,
  169. unsigned int steps /*= 0*/, const PropertyMap* map /*= NULL*/)
  170. {
  171. // no threaded implementation for the moment
  172. BatchData* data = ( BatchData* )pimpl;
  173. std::list<LoadRequest>::iterator it = std::find(data->requests.begin(),
  174. data->requests.end(), file);
  175. if (it != data->requests.end())
  176. {
  177. (*it).refCnt++;
  178. return;
  179. }
  180. data->requests.push_back(LoadRequest(file,steps,map));
  181. }
  182. // ------------------------------------------------------------------------------------------------
  183. aiScene* BatchLoader::GetImport (const std::string& file)
  184. {
  185. // no threaded implementation for the moment
  186. BatchData* data = ( BatchData* )pimpl;
  187. std::list<LoadRequest>::iterator it = std::find(data->requests.begin(),
  188. data->requests.end(), file);
  189. if (it != data->requests.end() && (*it).loaded)
  190. {
  191. aiScene* sc = (*it).scene;
  192. if (!(--(*it).refCnt))
  193. {
  194. data->requests.erase(it);
  195. }
  196. return sc;
  197. }
  198. return NULL;
  199. }
  200. // ------------------------------------------------------------------------------------------------
  201. void BatchLoader::LoadAll()
  202. {
  203. BatchData* data = ( BatchData* )pimpl;
  204. // no threaded implementation for the moment
  205. for (std::list<LoadRequest>::iterator it = data->requests.begin();
  206. it != data->requests.end(); ++it)
  207. {
  208. // force validation in debug builds
  209. unsigned int pp = (*it).flags;
  210. #ifdef _DEBUG
  211. pp |= aiProcess_ValidateDataStructure;
  212. #endif
  213. // setup config properties if necessary
  214. data->pImporter->mFloatProperties = (*it).map.floats;
  215. data->pImporter->mIntProperties = (*it).map.ints;
  216. data->pImporter->mStringProperties = (*it).map.strings;
  217. data->pImporter->ReadFile((*it).file,pp);
  218. (*it).scene = const_cast<aiScene*>(data->pImporter->GetOrphanedScene());
  219. (*it).loaded = true;
  220. }
  221. }