IOSystem.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2019, assimp 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 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 IOSystem.hpp
  35. * @brief File system wrapper for C++. Inherit this class to supply
  36. * custom file handling logic to the Import library.
  37. */
  38. #pragma once
  39. #ifndef AI_IOSYSTEM_H_INC
  40. #define AI_IOSYSTEM_H_INC
  41. #ifndef __cplusplus
  42. # error This header requires C++ to be used. aiFileIO.h is the \
  43. corresponding C interface.
  44. #endif
  45. #include "types.h"
  46. #ifdef _WIN32
  47. # include <direct.h>
  48. # include <stdlib.h>
  49. # include <stdio.h>
  50. #else
  51. # include <sys/stat.h>
  52. # include <sys/types.h>
  53. # include <unistd.h>
  54. #endif // _WIN32
  55. #include <vector>
  56. namespace Assimp {
  57. class IOStream;
  58. // ---------------------------------------------------------------------------
  59. /** @brief CPP-API: Interface to the file system.
  60. *
  61. * Derive an own implementation from this interface to supply custom file handling
  62. * to the importer library. If you implement this interface, you also want to
  63. * supply a custom implementation for IOStream.
  64. *
  65. * @see Importer::SetIOHandler()
  66. */
  67. class ASSIMP_API IOSystem
  68. #ifndef SWIG
  69. : public Intern::AllocateFromAssimpHeap
  70. #endif
  71. {
  72. public:
  73. // -------------------------------------------------------------------
  74. /** @brief Default constructor.
  75. *
  76. * Create an instance of your derived class and assign it to an
  77. * #Assimp::Importer instance by calling Importer::SetIOHandler().
  78. */
  79. IOSystem() AI_NO_EXCEPT;
  80. // -------------------------------------------------------------------
  81. /** @brief Virtual destructor.
  82. *
  83. * It is safe to be called from within DLL Assimp, we're constructed
  84. * on Assimp's heap.
  85. */
  86. virtual ~IOSystem();
  87. // -------------------------------------------------------------------
  88. /** @brief For backward compatibility
  89. * @see Exists(const char*)
  90. */
  91. AI_FORCE_INLINE bool Exists( const std::string& pFile) const;
  92. // -------------------------------------------------------------------
  93. /** @brief Tests for the existence of a file at the given path.
  94. *
  95. * @param pFile Path to the file
  96. * @return true if there is a file with this path, else false.
  97. */
  98. virtual bool Exists( const char* pFile) const = 0;
  99. // -------------------------------------------------------------------
  100. /** @brief Returns the system specific directory separator
  101. * @return System specific directory separator
  102. */
  103. virtual char getOsSeparator() const = 0;
  104. // -------------------------------------------------------------------
  105. /** @brief Open a new file with a given path.
  106. *
  107. * When the access to the file is finished, call Close() to release
  108. * all associated resources (or the virtual dtor of the IOStream).
  109. *
  110. * @param pFile Path to the file
  111. * @param pMode Desired file I/O mode. Required are: "wb", "w", "wt",
  112. * "rb", "r", "rt".
  113. *
  114. * @return New IOStream interface allowing the lib to access
  115. * the underlying file.
  116. * @note When implementing this class to provide custom IO handling,
  117. * you probably have to supply an own implementation of IOStream as well.
  118. */
  119. virtual IOStream* Open(const char* pFile,
  120. const char* pMode = "rb") = 0;
  121. // -------------------------------------------------------------------
  122. /** @brief For backward compatibility
  123. * @see Open(const char*, const char*)
  124. */
  125. inline IOStream* Open(const std::string& pFile,
  126. const std::string& pMode = std::string("rb"));
  127. // -------------------------------------------------------------------
  128. /** @brief Closes the given file and releases all resources
  129. * associated with it.
  130. * @param pFile The file instance previously created by Open().
  131. */
  132. virtual void Close( IOStream* pFile) = 0;
  133. // -------------------------------------------------------------------
  134. /** @brief Compares two paths and check whether the point to
  135. * identical files.
  136. *
  137. * The dummy implementation of this virtual member performs a
  138. * case-insensitive comparison of the given strings. The default IO
  139. * system implementation uses OS mechanisms to convert relative into
  140. * absolute paths, so the result can be trusted.
  141. * @param one First file
  142. * @param second Second file
  143. * @return true if the paths point to the same file. The file needn't
  144. * be existing, however.
  145. */
  146. virtual bool ComparePaths (const char* one,
  147. const char* second) const;
  148. // -------------------------------------------------------------------
  149. /** @brief For backward compatibility
  150. * @see ComparePaths(const char*, const char*)
  151. */
  152. inline bool ComparePaths (const std::string& one,
  153. const std::string& second) const;
  154. // -------------------------------------------------------------------
  155. /** @brief Pushes a new directory onto the directory stack.
  156. * @param path Path to push onto the stack.
  157. * @return True, when push was successful, false if path is empty.
  158. */
  159. virtual bool PushDirectory( const std::string &path );
  160. // -------------------------------------------------------------------
  161. /** @brief Returns the top directory from the stack.
  162. * @return The directory on the top of the stack.
  163. * Returns empty when no directory was pushed to the stack.
  164. */
  165. virtual const std::string &CurrentDirectory() const;
  166. // -------------------------------------------------------------------
  167. /** @brief Returns the number of directories stored on the stack.
  168. * @return The number of directories of the stack.
  169. */
  170. virtual size_t StackSize() const;
  171. // -------------------------------------------------------------------
  172. /** @brief Pops the top directory from the stack.
  173. * @return True, when a directory was on the stack. False if no
  174. * directory was on the stack.
  175. */
  176. virtual bool PopDirectory();
  177. // -------------------------------------------------------------------
  178. /** @brief CReates an new directory at the given path.
  179. * @param path [in] The path to create.
  180. * @return True, when a directory was created. False if the directory
  181. * cannot be created.
  182. */
  183. virtual bool CreateDirectory( const std::string &path );
  184. // -------------------------------------------------------------------
  185. /** @brief Will change the current directory to the given path.
  186. * @param path [in] The path to change to.
  187. * @return True, when the directory has changed successfully.
  188. */
  189. virtual bool ChangeDirectory( const std::string &path );
  190. virtual bool DeleteFile( const std::string &file );
  191. private:
  192. std::vector<std::string> m_pathStack;
  193. };
  194. // ----------------------------------------------------------------------------
  195. AI_FORCE_INLINE
  196. IOSystem::IOSystem() AI_NO_EXCEPT
  197. : m_pathStack() {
  198. // empty
  199. }
  200. // ----------------------------------------------------------------------------
  201. AI_FORCE_INLINE
  202. IOSystem::~IOSystem() {
  203. // empty
  204. }
  205. // ----------------------------------------------------------------------------
  206. // For compatibility, the interface of some functions taking a std::string was
  207. // changed to const char* to avoid crashes between binary incompatible STL
  208. // versions. This code her is inlined, so it shouldn't cause any problems.
  209. // ----------------------------------------------------------------------------
  210. // ----------------------------------------------------------------------------
  211. AI_FORCE_INLINE
  212. IOStream* IOSystem::Open(const std::string& pFile, const std::string& pMode) {
  213. // NOTE:
  214. // For compatibility, interface was changed to const char* to
  215. // avoid crashes between binary incompatible STL versions
  216. return Open(pFile.c_str(),pMode.c_str());
  217. }
  218. // ----------------------------------------------------------------------------
  219. AI_FORCE_INLINE
  220. bool IOSystem::Exists( const std::string& pFile) const {
  221. // NOTE:
  222. // For compatibility, interface was changed to const char* to
  223. // avoid crashes between binary incompatible STL versions
  224. return Exists(pFile.c_str());
  225. }
  226. // ----------------------------------------------------------------------------
  227. AI_FORCE_INLINE
  228. bool IOSystem::ComparePaths (const std::string& one, const std::string& second) const {
  229. // NOTE:
  230. // For compatibility, interface was changed to const char* to
  231. // avoid crashes between binary incompatible STL versions
  232. return ComparePaths(one.c_str(),second.c_str());
  233. }
  234. // ----------------------------------------------------------------------------
  235. AI_FORCE_INLINE
  236. bool IOSystem::PushDirectory( const std::string &path ) {
  237. if ( path.empty() ) {
  238. return false;
  239. }
  240. m_pathStack.push_back( path );
  241. return true;
  242. }
  243. // ----------------------------------------------------------------------------
  244. AI_FORCE_INLINE
  245. const std::string &IOSystem::CurrentDirectory() const {
  246. if ( m_pathStack.empty() ) {
  247. static const std::string Dummy("");
  248. return Dummy;
  249. }
  250. return m_pathStack[ m_pathStack.size()-1 ];
  251. }
  252. // ----------------------------------------------------------------------------
  253. AI_FORCE_INLINE
  254. size_t IOSystem::StackSize() const {
  255. return m_pathStack.size();
  256. }
  257. // ----------------------------------------------------------------------------
  258. AI_FORCE_INLINE
  259. bool IOSystem::PopDirectory() {
  260. if ( m_pathStack.empty() ) {
  261. return false;
  262. }
  263. m_pathStack.pop_back();
  264. return true;
  265. }
  266. // ----------------------------------------------------------------------------
  267. AI_FORCE_INLINE
  268. bool IOSystem::CreateDirectory( const std::string &path ) {
  269. if ( path.empty() ) {
  270. return false;
  271. }
  272. #ifdef _WIN32
  273. return 0 != ::_mkdir( path.c_str() );
  274. #else
  275. return 0 != ::mkdir( path.c_str(), 0777 );
  276. #endif // _WIN32
  277. }
  278. // ----------------------------------------------------------------------------
  279. AI_FORCE_INLINE
  280. bool IOSystem::ChangeDirectory( const std::string &path ) {
  281. if ( path.empty() ) {
  282. return false;
  283. }
  284. #ifdef _WIN32
  285. return 0 != ::_chdir( path.c_str() );
  286. #else
  287. return 0 != ::chdir( path.c_str() );
  288. #endif // _WIN32
  289. }
  290. // ----------------------------------------------------------------------------
  291. AI_FORCE_INLINE
  292. bool IOSystem::DeleteFile( const std::string &file ) {
  293. if ( file.empty() ) {
  294. return false;
  295. }
  296. const int retCode( ::remove( file.c_str() ) );
  297. return ( 0 == retCode );
  298. }
  299. } //!ns Assimp
  300. #endif //AI_IOSYSTEM_H_INC