miniassimp
diff include/miniassimp/IOSystem.hpp @ 0:879c81d94345
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 28 Jan 2019 18:19:26 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/include/miniassimp/IOSystem.hpp Mon Jan 28 18:19:26 2019 +0200 1.3 @@ -0,0 +1,357 @@ 1.4 +/* 1.5 +--------------------------------------------------------------------------- 1.6 +Open Asset Import Library (assimp) 1.7 +--------------------------------------------------------------------------- 1.8 + 1.9 +Copyright (c) 2006-2018, assimp team 1.10 + 1.11 + 1.12 + 1.13 +All rights reserved. 1.14 + 1.15 +Redistribution and use of this software in source and binary forms, 1.16 +with or without modification, are permitted provided that the following 1.17 +conditions are met: 1.18 + 1.19 +* Redistributions of source code must retain the above 1.20 + copyright notice, this list of conditions and the 1.21 + following disclaimer. 1.22 + 1.23 +* Redistributions in binary form must reproduce the above 1.24 + copyright notice, this list of conditions and the 1.25 + following disclaimer in the documentation and/or other 1.26 + materials provided with the distribution. 1.27 + 1.28 +* Neither the name of the assimp team, nor the names of its 1.29 + contributors may be used to endorse or promote products 1.30 + derived from this software without specific prior 1.31 + written permission of the assimp team. 1.32 + 1.33 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.34 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.35 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.36 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.37 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.38 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.39 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.40 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.41 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.42 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.43 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.44 +--------------------------------------------------------------------------- 1.45 +*/ 1.46 + 1.47 +/** @file IOSystem.hpp 1.48 + * @brief File system wrapper for C++. Inherit this class to supply 1.49 + * custom file handling logic to the Import library. 1.50 +*/ 1.51 + 1.52 +#pragma once 1.53 +#ifndef AI_IOSYSTEM_H_INC 1.54 +#define AI_IOSYSTEM_H_INC 1.55 + 1.56 +#ifndef __cplusplus 1.57 +# error This header requires C++ to be used. aiFileIO.h is the \ 1.58 + corresponding C interface. 1.59 +#endif 1.60 + 1.61 +#include "types.h" 1.62 + 1.63 +#ifdef _WIN32 1.64 +# include <direct.h> 1.65 +# include <stdlib.h> 1.66 +# include <stdio.h> 1.67 +#else 1.68 +# include <sys/stat.h> 1.69 +# include <sys/types.h> 1.70 +# include <unistd.h> 1.71 +#endif // _WIN32 1.72 + 1.73 +#include <vector> 1.74 + 1.75 +namespace Assimp { 1.76 + 1.77 + class IOStream; 1.78 + 1.79 +// --------------------------------------------------------------------------- 1.80 +/** @brief CPP-API: Interface to the file system. 1.81 + * 1.82 + * Derive an own implementation from this interface to supply custom file handling 1.83 + * to the importer library. If you implement this interface, you also want to 1.84 + * supply a custom implementation for IOStream. 1.85 + * 1.86 + * @see Importer::SetIOHandler() 1.87 + */ 1.88 +class ASSIMP_API IOSystem 1.89 +#ifndef SWIG 1.90 + : public Intern::AllocateFromAssimpHeap 1.91 +#endif 1.92 +{ 1.93 +public: 1.94 + 1.95 + // ------------------------------------------------------------------- 1.96 + /** @brief Default constructor. 1.97 + * 1.98 + * Create an instance of your derived class and assign it to an 1.99 + * #Assimp::Importer instance by calling Importer::SetIOHandler(). 1.100 + */ 1.101 + IOSystem() AI_NO_EXCEPT; 1.102 + 1.103 + // ------------------------------------------------------------------- 1.104 + /** @brief Virtual destructor. 1.105 + * 1.106 + * It is safe to be called from within DLL Assimp, we're constructed 1.107 + * on Assimp's heap. 1.108 + */ 1.109 + virtual ~IOSystem(); 1.110 + 1.111 + // ------------------------------------------------------------------- 1.112 + /** @brief For backward compatibility 1.113 + * @see Exists(const char*) 1.114 + */ 1.115 + AI_FORCE_INLINE bool Exists( const std::string& pFile) const; 1.116 + 1.117 + // ------------------------------------------------------------------- 1.118 + /** @brief Tests for the existence of a file at the given path. 1.119 + * 1.120 + * @param pFile Path to the file 1.121 + * @return true if there is a file with this path, else false. 1.122 + */ 1.123 + virtual bool Exists( const char* pFile) const = 0; 1.124 + 1.125 + // ------------------------------------------------------------------- 1.126 + /** @brief Returns the system specific directory separator 1.127 + * @return System specific directory separator 1.128 + */ 1.129 + virtual char getOsSeparator() const = 0; 1.130 + 1.131 + // ------------------------------------------------------------------- 1.132 + /** @brief Open a new file with a given path. 1.133 + * 1.134 + * When the access to the file is finished, call Close() to release 1.135 + * all associated resources (or the virtual dtor of the IOStream). 1.136 + * 1.137 + * @param pFile Path to the file 1.138 + * @param pMode Desired file I/O mode. Required are: "wb", "w", "wt", 1.139 + * "rb", "r", "rt". 1.140 + * 1.141 + * @return New IOStream interface allowing the lib to access 1.142 + * the underlying file. 1.143 + * @note When implementing this class to provide custom IO handling, 1.144 + * you probably have to supply an own implementation of IOStream as well. 1.145 + */ 1.146 + virtual IOStream* Open(const char* pFile, 1.147 + const char* pMode = "rb") = 0; 1.148 + 1.149 + // ------------------------------------------------------------------- 1.150 + /** @brief For backward compatibility 1.151 + * @see Open(const char*, const char*) 1.152 + */ 1.153 + inline IOStream* Open(const std::string& pFile, 1.154 + const std::string& pMode = std::string("rb")); 1.155 + 1.156 + // ------------------------------------------------------------------- 1.157 + /** @brief Closes the given file and releases all resources 1.158 + * associated with it. 1.159 + * @param pFile The file instance previously created by Open(). 1.160 + */ 1.161 + virtual void Close( IOStream* pFile) = 0; 1.162 + 1.163 + // ------------------------------------------------------------------- 1.164 + /** @brief Compares two paths and check whether the point to 1.165 + * identical files. 1.166 + * 1.167 + * The dummy implementation of this virtual member performs a 1.168 + * case-insensitive comparison of the given strings. The default IO 1.169 + * system implementation uses OS mechanisms to convert relative into 1.170 + * absolute paths, so the result can be trusted. 1.171 + * @param one First file 1.172 + * @param second Second file 1.173 + * @return true if the paths point to the same file. The file needn't 1.174 + * be existing, however. 1.175 + */ 1.176 + virtual bool ComparePaths (const char* one, 1.177 + const char* second) const; 1.178 + 1.179 + // ------------------------------------------------------------------- 1.180 + /** @brief For backward compatibility 1.181 + * @see ComparePaths(const char*, const char*) 1.182 + */ 1.183 + inline bool ComparePaths (const std::string& one, 1.184 + const std::string& second) const; 1.185 + 1.186 + // ------------------------------------------------------------------- 1.187 + /** @brief Pushes a new directory onto the directory stack. 1.188 + * @param path Path to push onto the stack. 1.189 + * @return True, when push was successful, false if path is empty. 1.190 + */ 1.191 + virtual bool PushDirectory( const std::string &path ); 1.192 + 1.193 + // ------------------------------------------------------------------- 1.194 + /** @brief Returns the top directory from the stack. 1.195 + * @return The directory on the top of the stack. 1.196 + * Returns empty when no directory was pushed to the stack. 1.197 + */ 1.198 + virtual const std::string &CurrentDirectory() const; 1.199 + 1.200 + // ------------------------------------------------------------------- 1.201 + /** @brief Returns the number of directories stored on the stack. 1.202 + * @return The number of directories of the stack. 1.203 + */ 1.204 + virtual size_t StackSize() const; 1.205 + 1.206 + // ------------------------------------------------------------------- 1.207 + /** @brief Pops the top directory from the stack. 1.208 + * @return True, when a directory was on the stack. False if no 1.209 + * directory was on the stack. 1.210 + */ 1.211 + virtual bool PopDirectory(); 1.212 + 1.213 + // ------------------------------------------------------------------- 1.214 + /** @brief CReates an new directory at the given path. 1.215 + * @param path [in] The path to create. 1.216 + * @return True, when a directory was created. False if the directory 1.217 + * cannot be created. 1.218 + */ 1.219 + virtual bool CreateDirectory( const std::string &path ); 1.220 + 1.221 + // ------------------------------------------------------------------- 1.222 + /** @brief Will change the current directory to the given path. 1.223 + * @param path [in] The path to change to. 1.224 + * @return True, when the directory has changed successfully. 1.225 + */ 1.226 + virtual bool ChangeDirectory( const std::string &path ); 1.227 + 1.228 + virtual bool DeleteFile( const std::string &file ); 1.229 + 1.230 +private: 1.231 + std::vector<std::string> m_pathStack; 1.232 +}; 1.233 + 1.234 +// ---------------------------------------------------------------------------- 1.235 +AI_FORCE_INLINE 1.236 +IOSystem::IOSystem() AI_NO_EXCEPT 1.237 +: m_pathStack() { 1.238 + // empty 1.239 +} 1.240 + 1.241 +// ---------------------------------------------------------------------------- 1.242 +AI_FORCE_INLINE 1.243 +IOSystem::~IOSystem() { 1.244 + // empty 1.245 +} 1.246 + 1.247 +// ---------------------------------------------------------------------------- 1.248 +// For compatibility, the interface of some functions taking a std::string was 1.249 +// changed to const char* to avoid crashes between binary incompatible STL 1.250 +// versions. This code her is inlined, so it shouldn't cause any problems. 1.251 +// ---------------------------------------------------------------------------- 1.252 + 1.253 +// ---------------------------------------------------------------------------- 1.254 +AI_FORCE_INLINE 1.255 +IOStream* IOSystem::Open(const std::string& pFile, const std::string& pMode) { 1.256 + // NOTE: 1.257 + // For compatibility, interface was changed to const char* to 1.258 + // avoid crashes between binary incompatible STL versions 1.259 + return Open(pFile.c_str(),pMode.c_str()); 1.260 +} 1.261 + 1.262 +// ---------------------------------------------------------------------------- 1.263 +AI_FORCE_INLINE 1.264 +bool IOSystem::Exists( const std::string& pFile) const { 1.265 + // NOTE: 1.266 + // For compatibility, interface was changed to const char* to 1.267 + // avoid crashes between binary incompatible STL versions 1.268 + return Exists(pFile.c_str()); 1.269 +} 1.270 + 1.271 +// ---------------------------------------------------------------------------- 1.272 +AI_FORCE_INLINE 1.273 +bool IOSystem::ComparePaths (const std::string& one, const std::string& second) const { 1.274 + // NOTE: 1.275 + // For compatibility, interface was changed to const char* to 1.276 + // avoid crashes between binary incompatible STL versions 1.277 + return ComparePaths(one.c_str(),second.c_str()); 1.278 +} 1.279 + 1.280 +// ---------------------------------------------------------------------------- 1.281 +AI_FORCE_INLINE 1.282 +bool IOSystem::PushDirectory( const std::string &path ) { 1.283 + if ( path.empty() ) { 1.284 + return false; 1.285 + } 1.286 + 1.287 + m_pathStack.push_back( path ); 1.288 + 1.289 + return true; 1.290 +} 1.291 + 1.292 +// ---------------------------------------------------------------------------- 1.293 +AI_FORCE_INLINE 1.294 +const std::string &IOSystem::CurrentDirectory() const { 1.295 + if ( m_pathStack.empty() ) { 1.296 + static const std::string Dummy(""); 1.297 + return Dummy; 1.298 + } 1.299 + return m_pathStack[ m_pathStack.size()-1 ]; 1.300 +} 1.301 + 1.302 +// ---------------------------------------------------------------------------- 1.303 +AI_FORCE_INLINE 1.304 +size_t IOSystem::StackSize() const { 1.305 + return m_pathStack.size(); 1.306 +} 1.307 + 1.308 +// ---------------------------------------------------------------------------- 1.309 +AI_FORCE_INLINE 1.310 +bool IOSystem::PopDirectory() { 1.311 + if ( m_pathStack.empty() ) { 1.312 + return false; 1.313 + } 1.314 + 1.315 + m_pathStack.pop_back(); 1.316 + 1.317 + return true; 1.318 +} 1.319 + 1.320 +// ---------------------------------------------------------------------------- 1.321 +AI_FORCE_INLINE 1.322 +bool IOSystem::CreateDirectory( const std::string &path ) { 1.323 + if ( path.empty() ) { 1.324 + return false; 1.325 + } 1.326 + 1.327 +#ifdef _WIN32 1.328 + return 0 != ::_mkdir( path.c_str() ); 1.329 +#else 1.330 + return 0 != ::mkdir( path.c_str(), 0777 ); 1.331 +#endif // _WIN32 1.332 +} 1.333 + 1.334 +// ---------------------------------------------------------------------------- 1.335 +AI_FORCE_INLINE 1.336 +bool IOSystem::ChangeDirectory( const std::string &path ) { 1.337 + if ( path.empty() ) { 1.338 + return false; 1.339 + } 1.340 + 1.341 +#ifdef _WIN32 1.342 + return 0 != ::_chdir( path.c_str() ); 1.343 +#else 1.344 + return 0 != ::chdir( path.c_str() ); 1.345 +#endif // _WIN32 1.346 +} 1.347 + 1.348 + 1.349 +// ---------------------------------------------------------------------------- 1.350 +AI_FORCE_INLINE 1.351 +bool IOSystem::DeleteFile( const std::string &file ) { 1.352 + if ( file.empty() ) { 1.353 + return false; 1.354 + } 1.355 + const int retCode( ::remove( file.c_str() ) ); 1.356 + return ( 0 == retCode ); 1.357 +} 1.358 +} //!ns Assimp 1.359 + 1.360 +#endif //AI_IOSYSTEM_H_INC