IOSystem.h 8.0 KB

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