IOSystem.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, 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.h
  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. namespace Assimp {
  46. class IOStream;
  47. // ---------------------------------------------------------------------------
  48. /** @brief CPP-API: Interface to the file system.
  49. *
  50. * Derive an own implementation from this interface to supply custom file handling
  51. * to the importer library. If you implement this interface, you also want to
  52. * supply a custom implementation for IOStream.
  53. *
  54. * @see Importer::SetIOHandler() */
  55. class ASSIMP_API IOSystem : public Intern::AllocateFromAssimpHeap
  56. {
  57. public:
  58. // -------------------------------------------------------------------
  59. /** @brief Default constructor.
  60. *
  61. * Create an instance of your derived class and assign it to an
  62. * #Assimp::Importer instance by calling Importer::SetIOHandler().
  63. */
  64. IOSystem();
  65. // -------------------------------------------------------------------
  66. /** @brief Virtual destructor.
  67. *
  68. * It is safe to be called from within DLL Assimp, we're constructed
  69. * on Assimp's heap.
  70. */
  71. virtual ~IOSystem();
  72. public:
  73. // -------------------------------------------------------------------
  74. /** @brief For backward compatibility
  75. * @see Exists(const char*)
  76. */
  77. AI_FORCE_INLINE bool Exists( const std::string& pFile) const;
  78. // -------------------------------------------------------------------
  79. /** @brief Tests for the existence of a file at the given path.
  80. *
  81. * @param pFile Path to the file
  82. * @return true if there is a file with this path, else false.
  83. */
  84. virtual bool Exists( const char* pFile) const = 0;
  85. // -------------------------------------------------------------------
  86. /** @brief Returns the system specific directory separator
  87. * @return System specific directory separator
  88. */
  89. virtual char getOsSeparator() const = 0;
  90. // -------------------------------------------------------------------
  91. /** @brief Open a new file with a given path.
  92. *
  93. * When the access to the file is finished, call Close() to release
  94. * all associated resources (or the virtual dtor of the IOStream).
  95. *
  96. * @param pFile Path to the file
  97. * @param pMode Desired file I/O mode. Required are: "wb", "w", "wt",
  98. * "rb", "r", "rt".
  99. *
  100. * @return New IOStream interface allowing the lib to access
  101. * the underlying file.
  102. * @note When implementing this class to provide custom IO handling,
  103. * you probably have to supply an own implementation of IOStream as well.
  104. */
  105. virtual IOStream* Open(const char* pFile,
  106. const char* pMode = "rb") = 0;
  107. // -------------------------------------------------------------------
  108. /** @brief For backward compatibility
  109. * @see Open(const char*, const char*)
  110. */
  111. inline IOStream* Open(const std::string& pFile,
  112. const std::string& pMode = std::string("rb"));
  113. // -------------------------------------------------------------------
  114. /** @brief Closes the given file and releases all resources
  115. * associated with it.
  116. * @param pFile The file instance previously created by Open().
  117. */
  118. virtual void Close( IOStream* pFile) = 0;
  119. // -------------------------------------------------------------------
  120. /** @brief Compares two paths and check whether the point to
  121. * identical files.
  122. *
  123. * The dummy implementation of this virtual member performs a
  124. * case-insensitive comparison of the given strings. The default IO
  125. * system implementation uses OS mechanisms to convert relative into
  126. * absolute paths, so the result can be trusted.
  127. * @param one First file
  128. * @param second Second file
  129. * @return true if the paths point to the same file. The file needn't
  130. * be existing, however.
  131. */
  132. virtual bool ComparePaths (const char* one,
  133. const char* second) const;
  134. // -------------------------------------------------------------------
  135. /** @brief For backward compatibility
  136. * @see ComparePaths(const char*, const char*)
  137. */
  138. inline bool ComparePaths (const std::string& one,
  139. const std::string& second) const;
  140. };
  141. // ----------------------------------------------------------------------------
  142. AI_FORCE_INLINE IOSystem::IOSystem()
  143. {
  144. // empty
  145. }
  146. // ----------------------------------------------------------------------------
  147. AI_FORCE_INLINE IOSystem::~IOSystem()
  148. {
  149. // empty
  150. }
  151. // ----------------------------------------------------------------------------
  152. // For compatibility, the interface of some functions taking a std::string was
  153. // changed to const char* to avoid crashes between binary incompatible STL
  154. // versions. This code her is inlined, so it shouldn't cause any problems.
  155. // ----------------------------------------------------------------------------
  156. // ----------------------------------------------------------------------------
  157. AI_FORCE_INLINE IOStream* IOSystem::Open(const std::string& pFile,
  158. const std::string& pMode)
  159. {
  160. // NOTE:
  161. // For compatibility, interface was changed to const char* to
  162. // avoid crashes between binary incompatible STL versions
  163. return Open(pFile.c_str(),pMode.c_str());
  164. }
  165. // ----------------------------------------------------------------------------
  166. AI_FORCE_INLINE bool IOSystem::Exists( const std::string& pFile) const
  167. {
  168. // NOTE:
  169. // For compatibility, interface was changed to const char* to
  170. // avoid crashes between binary incompatible STL versions
  171. return Exists(pFile.c_str());
  172. }
  173. // ----------------------------------------------------------------------------
  174. inline bool IOSystem::ComparePaths (const std::string& one,
  175. const std::string& second) const
  176. {
  177. // NOTE:
  178. // For compatibility, interface was changed to const char* to
  179. // avoid crashes between binary incompatible STL versions
  180. return ComparePaths(one.c_str(),second.c_str());
  181. }
  182. // ----------------------------------------------------------------------------
  183. } //!ns Assimp
  184. #endif //AI_IOSYSTEM_H_INC