IOSystem.hpp 10 KB

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