miniassimp

annotate 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
rev   line source
nuclear@0 1 /*
nuclear@0 2 ---------------------------------------------------------------------------
nuclear@0 3 Open Asset Import Library (assimp)
nuclear@0 4 ---------------------------------------------------------------------------
nuclear@0 5
nuclear@0 6 Copyright (c) 2006-2018, assimp team
nuclear@0 7
nuclear@0 8
nuclear@0 9
nuclear@0 10 All rights reserved.
nuclear@0 11
nuclear@0 12 Redistribution and use of this software in source and binary forms,
nuclear@0 13 with or without modification, are permitted provided that the following
nuclear@0 14 conditions are met:
nuclear@0 15
nuclear@0 16 * Redistributions of source code must retain the above
nuclear@0 17 copyright notice, this list of conditions and the
nuclear@0 18 following disclaimer.
nuclear@0 19
nuclear@0 20 * Redistributions in binary form must reproduce the above
nuclear@0 21 copyright notice, this list of conditions and the
nuclear@0 22 following disclaimer in the documentation and/or other
nuclear@0 23 materials provided with the distribution.
nuclear@0 24
nuclear@0 25 * Neither the name of the assimp team, nor the names of its
nuclear@0 26 contributors may be used to endorse or promote products
nuclear@0 27 derived from this software without specific prior
nuclear@0 28 written permission of the assimp team.
nuclear@0 29
nuclear@0 30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
nuclear@0 31 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
nuclear@0 32 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
nuclear@0 33 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
nuclear@0 34 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
nuclear@0 35 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
nuclear@0 36 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
nuclear@0 37 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
nuclear@0 38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
nuclear@0 39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
nuclear@0 40 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
nuclear@0 41 ---------------------------------------------------------------------------
nuclear@0 42 */
nuclear@0 43
nuclear@0 44 /** @file IOSystem.hpp
nuclear@0 45 * @brief File system wrapper for C++. Inherit this class to supply
nuclear@0 46 * custom file handling logic to the Import library.
nuclear@0 47 */
nuclear@0 48
nuclear@0 49 #pragma once
nuclear@0 50 #ifndef AI_IOSYSTEM_H_INC
nuclear@0 51 #define AI_IOSYSTEM_H_INC
nuclear@0 52
nuclear@0 53 #ifndef __cplusplus
nuclear@0 54 # error This header requires C++ to be used. aiFileIO.h is the \
nuclear@0 55 corresponding C interface.
nuclear@0 56 #endif
nuclear@0 57
nuclear@0 58 #include "types.h"
nuclear@0 59
nuclear@0 60 #ifdef _WIN32
nuclear@0 61 # include <direct.h>
nuclear@0 62 # include <stdlib.h>
nuclear@0 63 # include <stdio.h>
nuclear@0 64 #else
nuclear@0 65 # include <sys/stat.h>
nuclear@0 66 # include <sys/types.h>
nuclear@0 67 # include <unistd.h>
nuclear@0 68 #endif // _WIN32
nuclear@0 69
nuclear@0 70 #include <vector>
nuclear@0 71
nuclear@0 72 namespace Assimp {
nuclear@0 73
nuclear@0 74 class IOStream;
nuclear@0 75
nuclear@0 76 // ---------------------------------------------------------------------------
nuclear@0 77 /** @brief CPP-API: Interface to the file system.
nuclear@0 78 *
nuclear@0 79 * Derive an own implementation from this interface to supply custom file handling
nuclear@0 80 * to the importer library. If you implement this interface, you also want to
nuclear@0 81 * supply a custom implementation for IOStream.
nuclear@0 82 *
nuclear@0 83 * @see Importer::SetIOHandler()
nuclear@0 84 */
nuclear@0 85 class ASSIMP_API IOSystem
nuclear@0 86 #ifndef SWIG
nuclear@0 87 : public Intern::AllocateFromAssimpHeap
nuclear@0 88 #endif
nuclear@0 89 {
nuclear@0 90 public:
nuclear@0 91
nuclear@0 92 // -------------------------------------------------------------------
nuclear@0 93 /** @brief Default constructor.
nuclear@0 94 *
nuclear@0 95 * Create an instance of your derived class and assign it to an
nuclear@0 96 * #Assimp::Importer instance by calling Importer::SetIOHandler().
nuclear@0 97 */
nuclear@0 98 IOSystem() AI_NO_EXCEPT;
nuclear@0 99
nuclear@0 100 // -------------------------------------------------------------------
nuclear@0 101 /** @brief Virtual destructor.
nuclear@0 102 *
nuclear@0 103 * It is safe to be called from within DLL Assimp, we're constructed
nuclear@0 104 * on Assimp's heap.
nuclear@0 105 */
nuclear@0 106 virtual ~IOSystem();
nuclear@0 107
nuclear@0 108 // -------------------------------------------------------------------
nuclear@0 109 /** @brief For backward compatibility
nuclear@0 110 * @see Exists(const char*)
nuclear@0 111 */
nuclear@0 112 AI_FORCE_INLINE bool Exists( const std::string& pFile) const;
nuclear@0 113
nuclear@0 114 // -------------------------------------------------------------------
nuclear@0 115 /** @brief Tests for the existence of a file at the given path.
nuclear@0 116 *
nuclear@0 117 * @param pFile Path to the file
nuclear@0 118 * @return true if there is a file with this path, else false.
nuclear@0 119 */
nuclear@0 120 virtual bool Exists( const char* pFile) const = 0;
nuclear@0 121
nuclear@0 122 // -------------------------------------------------------------------
nuclear@0 123 /** @brief Returns the system specific directory separator
nuclear@0 124 * @return System specific directory separator
nuclear@0 125 */
nuclear@0 126 virtual char getOsSeparator() const = 0;
nuclear@0 127
nuclear@0 128 // -------------------------------------------------------------------
nuclear@0 129 /** @brief Open a new file with a given path.
nuclear@0 130 *
nuclear@0 131 * When the access to the file is finished, call Close() to release
nuclear@0 132 * all associated resources (or the virtual dtor of the IOStream).
nuclear@0 133 *
nuclear@0 134 * @param pFile Path to the file
nuclear@0 135 * @param pMode Desired file I/O mode. Required are: "wb", "w", "wt",
nuclear@0 136 * "rb", "r", "rt".
nuclear@0 137 *
nuclear@0 138 * @return New IOStream interface allowing the lib to access
nuclear@0 139 * the underlying file.
nuclear@0 140 * @note When implementing this class to provide custom IO handling,
nuclear@0 141 * you probably have to supply an own implementation of IOStream as well.
nuclear@0 142 */
nuclear@0 143 virtual IOStream* Open(const char* pFile,
nuclear@0 144 const char* pMode = "rb") = 0;
nuclear@0 145
nuclear@0 146 // -------------------------------------------------------------------
nuclear@0 147 /** @brief For backward compatibility
nuclear@0 148 * @see Open(const char*, const char*)
nuclear@0 149 */
nuclear@0 150 inline IOStream* Open(const std::string& pFile,
nuclear@0 151 const std::string& pMode = std::string("rb"));
nuclear@0 152
nuclear@0 153 // -------------------------------------------------------------------
nuclear@0 154 /** @brief Closes the given file and releases all resources
nuclear@0 155 * associated with it.
nuclear@0 156 * @param pFile The file instance previously created by Open().
nuclear@0 157 */
nuclear@0 158 virtual void Close( IOStream* pFile) = 0;
nuclear@0 159
nuclear@0 160 // -------------------------------------------------------------------
nuclear@0 161 /** @brief Compares two paths and check whether the point to
nuclear@0 162 * identical files.
nuclear@0 163 *
nuclear@0 164 * The dummy implementation of this virtual member performs a
nuclear@0 165 * case-insensitive comparison of the given strings. The default IO
nuclear@0 166 * system implementation uses OS mechanisms to convert relative into
nuclear@0 167 * absolute paths, so the result can be trusted.
nuclear@0 168 * @param one First file
nuclear@0 169 * @param second Second file
nuclear@0 170 * @return true if the paths point to the same file. The file needn't
nuclear@0 171 * be existing, however.
nuclear@0 172 */
nuclear@0 173 virtual bool ComparePaths (const char* one,
nuclear@0 174 const char* second) const;
nuclear@0 175
nuclear@0 176 // -------------------------------------------------------------------
nuclear@0 177 /** @brief For backward compatibility
nuclear@0 178 * @see ComparePaths(const char*, const char*)
nuclear@0 179 */
nuclear@0 180 inline bool ComparePaths (const std::string& one,
nuclear@0 181 const std::string& second) const;
nuclear@0 182
nuclear@0 183 // -------------------------------------------------------------------
nuclear@0 184 /** @brief Pushes a new directory onto the directory stack.
nuclear@0 185 * @param path Path to push onto the stack.
nuclear@0 186 * @return True, when push was successful, false if path is empty.
nuclear@0 187 */
nuclear@0 188 virtual bool PushDirectory( const std::string &path );
nuclear@0 189
nuclear@0 190 // -------------------------------------------------------------------
nuclear@0 191 /** @brief Returns the top directory from the stack.
nuclear@0 192 * @return The directory on the top of the stack.
nuclear@0 193 * Returns empty when no directory was pushed to the stack.
nuclear@0 194 */
nuclear@0 195 virtual const std::string &CurrentDirectory() const;
nuclear@0 196
nuclear@0 197 // -------------------------------------------------------------------
nuclear@0 198 /** @brief Returns the number of directories stored on the stack.
nuclear@0 199 * @return The number of directories of the stack.
nuclear@0 200 */
nuclear@0 201 virtual size_t StackSize() const;
nuclear@0 202
nuclear@0 203 // -------------------------------------------------------------------
nuclear@0 204 /** @brief Pops the top directory from the stack.
nuclear@0 205 * @return True, when a directory was on the stack. False if no
nuclear@0 206 * directory was on the stack.
nuclear@0 207 */
nuclear@0 208 virtual bool PopDirectory();
nuclear@0 209
nuclear@0 210 // -------------------------------------------------------------------
nuclear@0 211 /** @brief CReates an new directory at the given path.
nuclear@0 212 * @param path [in] The path to create.
nuclear@0 213 * @return True, when a directory was created. False if the directory
nuclear@0 214 * cannot be created.
nuclear@0 215 */
nuclear@0 216 virtual bool CreateDirectory( const std::string &path );
nuclear@0 217
nuclear@0 218 // -------------------------------------------------------------------
nuclear@0 219 /** @brief Will change the current directory to the given path.
nuclear@0 220 * @param path [in] The path to change to.
nuclear@0 221 * @return True, when the directory has changed successfully.
nuclear@0 222 */
nuclear@0 223 virtual bool ChangeDirectory( const std::string &path );
nuclear@0 224
nuclear@0 225 virtual bool DeleteFile( const std::string &file );
nuclear@0 226
nuclear@0 227 private:
nuclear@0 228 std::vector<std::string> m_pathStack;
nuclear@0 229 };
nuclear@0 230
nuclear@0 231 // ----------------------------------------------------------------------------
nuclear@0 232 AI_FORCE_INLINE
nuclear@0 233 IOSystem::IOSystem() AI_NO_EXCEPT
nuclear@0 234 : m_pathStack() {
nuclear@0 235 // empty
nuclear@0 236 }
nuclear@0 237
nuclear@0 238 // ----------------------------------------------------------------------------
nuclear@0 239 AI_FORCE_INLINE
nuclear@0 240 IOSystem::~IOSystem() {
nuclear@0 241 // empty
nuclear@0 242 }
nuclear@0 243
nuclear@0 244 // ----------------------------------------------------------------------------
nuclear@0 245 // For compatibility, the interface of some functions taking a std::string was
nuclear@0 246 // changed to const char* to avoid crashes between binary incompatible STL
nuclear@0 247 // versions. This code her is inlined, so it shouldn't cause any problems.
nuclear@0 248 // ----------------------------------------------------------------------------
nuclear@0 249
nuclear@0 250 // ----------------------------------------------------------------------------
nuclear@0 251 AI_FORCE_INLINE
nuclear@0 252 IOStream* IOSystem::Open(const std::string& pFile, const std::string& pMode) {
nuclear@0 253 // NOTE:
nuclear@0 254 // For compatibility, interface was changed to const char* to
nuclear@0 255 // avoid crashes between binary incompatible STL versions
nuclear@0 256 return Open(pFile.c_str(),pMode.c_str());
nuclear@0 257 }
nuclear@0 258
nuclear@0 259 // ----------------------------------------------------------------------------
nuclear@0 260 AI_FORCE_INLINE
nuclear@0 261 bool IOSystem::Exists( const std::string& pFile) const {
nuclear@0 262 // NOTE:
nuclear@0 263 // For compatibility, interface was changed to const char* to
nuclear@0 264 // avoid crashes between binary incompatible STL versions
nuclear@0 265 return Exists(pFile.c_str());
nuclear@0 266 }
nuclear@0 267
nuclear@0 268 // ----------------------------------------------------------------------------
nuclear@0 269 AI_FORCE_INLINE
nuclear@0 270 bool IOSystem::ComparePaths (const std::string& one, const std::string& second) const {
nuclear@0 271 // NOTE:
nuclear@0 272 // For compatibility, interface was changed to const char* to
nuclear@0 273 // avoid crashes between binary incompatible STL versions
nuclear@0 274 return ComparePaths(one.c_str(),second.c_str());
nuclear@0 275 }
nuclear@0 276
nuclear@0 277 // ----------------------------------------------------------------------------
nuclear@0 278 AI_FORCE_INLINE
nuclear@0 279 bool IOSystem::PushDirectory( const std::string &path ) {
nuclear@0 280 if ( path.empty() ) {
nuclear@0 281 return false;
nuclear@0 282 }
nuclear@0 283
nuclear@0 284 m_pathStack.push_back( path );
nuclear@0 285
nuclear@0 286 return true;
nuclear@0 287 }
nuclear@0 288
nuclear@0 289 // ----------------------------------------------------------------------------
nuclear@0 290 AI_FORCE_INLINE
nuclear@0 291 const std::string &IOSystem::CurrentDirectory() const {
nuclear@0 292 if ( m_pathStack.empty() ) {
nuclear@0 293 static const std::string Dummy("");
nuclear@0 294 return Dummy;
nuclear@0 295 }
nuclear@0 296 return m_pathStack[ m_pathStack.size()-1 ];
nuclear@0 297 }
nuclear@0 298
nuclear@0 299 // ----------------------------------------------------------------------------
nuclear@0 300 AI_FORCE_INLINE
nuclear@0 301 size_t IOSystem::StackSize() const {
nuclear@0 302 return m_pathStack.size();
nuclear@0 303 }
nuclear@0 304
nuclear@0 305 // ----------------------------------------------------------------------------
nuclear@0 306 AI_FORCE_INLINE
nuclear@0 307 bool IOSystem::PopDirectory() {
nuclear@0 308 if ( m_pathStack.empty() ) {
nuclear@0 309 return false;
nuclear@0 310 }
nuclear@0 311
nuclear@0 312 m_pathStack.pop_back();
nuclear@0 313
nuclear@0 314 return true;
nuclear@0 315 }
nuclear@0 316
nuclear@0 317 // ----------------------------------------------------------------------------
nuclear@0 318 AI_FORCE_INLINE
nuclear@0 319 bool IOSystem::CreateDirectory( const std::string &path ) {
nuclear@0 320 if ( path.empty() ) {
nuclear@0 321 return false;
nuclear@0 322 }
nuclear@0 323
nuclear@0 324 #ifdef _WIN32
nuclear@0 325 return 0 != ::_mkdir( path.c_str() );
nuclear@0 326 #else
nuclear@0 327 return 0 != ::mkdir( path.c_str(), 0777 );
nuclear@0 328 #endif // _WIN32
nuclear@0 329 }
nuclear@0 330
nuclear@0 331 // ----------------------------------------------------------------------------
nuclear@0 332 AI_FORCE_INLINE
nuclear@0 333 bool IOSystem::ChangeDirectory( const std::string &path ) {
nuclear@0 334 if ( path.empty() ) {
nuclear@0 335 return false;
nuclear@0 336 }
nuclear@0 337
nuclear@0 338 #ifdef _WIN32
nuclear@0 339 return 0 != ::_chdir( path.c_str() );
nuclear@0 340 #else
nuclear@0 341 return 0 != ::chdir( path.c_str() );
nuclear@0 342 #endif // _WIN32
nuclear@0 343 }
nuclear@0 344
nuclear@0 345
nuclear@0 346 // ----------------------------------------------------------------------------
nuclear@0 347 AI_FORCE_INLINE
nuclear@0 348 bool IOSystem::DeleteFile( const std::string &file ) {
nuclear@0 349 if ( file.empty() ) {
nuclear@0 350 return false;
nuclear@0 351 }
nuclear@0 352 const int retCode( ::remove( file.c_str() ) );
nuclear@0 353 return ( 0 == retCode );
nuclear@0 354 }
nuclear@0 355 } //!ns Assimp
nuclear@0 356
nuclear@0 357 #endif //AI_IOSYSTEM_H_INC