IOSystem.hpp 12 KB

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