BaseImporter.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. struct LoadRequest
  115. {
  116. LoadRequest(const std::string& _file, unsigned int _flags)
  117. : file (_file)
  118. , flags (_flags)
  119. , scene (NULL)
  120. , refCnt (1)
  121. , loaded (false)
  122. {}
  123. const std::string file;
  124. unsigned int flags;
  125. unsigned int refCnt;
  126. aiScene* scene;
  127. bool loaded;
  128. bool operator== (const std::string& f)
  129. {return file == f;}
  130. };
  131. struct BatchData
  132. {
  133. // IO system to be used for all imports
  134. IOSystem* pIOSystem;
  135. // Importer used to load all meshes
  136. Importer* pImporter;
  137. // List of all imports
  138. std::list<LoadRequest> requests;
  139. };
  140. // ------------------------------------------------------------------------------------------------
  141. BatchLoader::BatchLoader(IOSystem* pIO)
  142. {
  143. ai_assert(NULL != pIO);
  144. pimpl = new BatchData();
  145. BatchData* data = ( BatchData* )pimpl;
  146. data->pIOSystem = pIO;
  147. data->pImporter = new Importer();
  148. }
  149. // ------------------------------------------------------------------------------------------------
  150. BatchLoader::~BatchLoader()
  151. {
  152. // delete all scenes wthat have not been polled by the user
  153. BatchData* data = ( BatchData* )pimpl;
  154. for (std::list<LoadRequest>::iterator it = data->requests.begin();
  155. it != data->requests.end(); ++it)
  156. {
  157. delete (*it).scene;
  158. }
  159. delete data->pImporter;
  160. delete data;
  161. }
  162. // ------------------------------------------------------------------------------------------------
  163. void BatchLoader::AddLoadRequest (const std::string& file)
  164. {
  165. // no threaded implementation for the moment
  166. BatchData* data = ( BatchData* )pimpl;
  167. std::list<LoadRequest>::iterator it = std::find(data->requests.begin(),
  168. data->requests.end(), file);
  169. if (it != data->requests.end())
  170. {
  171. (*it).refCnt++;
  172. return;
  173. }
  174. data->requests.push_back(LoadRequest(file,0));
  175. }
  176. // ------------------------------------------------------------------------------------------------
  177. aiScene* BatchLoader::GetImport (const std::string& file)
  178. {
  179. // no threaded implementation for the moment
  180. BatchData* data = ( BatchData* )pimpl;
  181. std::list<LoadRequest>::iterator it = std::find(data->requests.begin(),
  182. data->requests.end(), file);
  183. if (it != data->requests.end() && (*it).loaded)
  184. {
  185. aiScene* sc = (*it).scene;
  186. if (!(--(*it).refCnt))
  187. {
  188. data->requests.erase(it);
  189. }
  190. return sc;
  191. }
  192. return NULL;
  193. }
  194. // ------------------------------------------------------------------------------------------------
  195. void BatchLoader::LoadAll()
  196. {
  197. BatchData* data = ( BatchData* )pimpl;
  198. // no threaded implementation for the moment
  199. for (std::list<LoadRequest>::iterator it = data->requests.begin();
  200. it != data->requests.end(); ++it)
  201. {
  202. // no postprocessing for the moment except validation
  203. // in debug builds
  204. unsigned int pp = 0;
  205. #ifdef _DEBUG
  206. pp |= aiProcess_ValidateDataStructure;
  207. #endif
  208. data->pImporter->ReadFile((*it).file,pp);
  209. (*it).scene = const_cast<aiScene*>(data->pImporter->GetOrphanedScene());
  210. (*it).loaded = true;
  211. }
  212. }