Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 09:30:08

0001 /*
0002 ---------------------------------------------------------------------------
0003 Open Asset Import Library (assimp)
0004 ---------------------------------------------------------------------------
0005 
0006 Copyright (c) 2006-2024, assimp team
0007 
0008 All rights reserved.
0009 
0010 Redistribution and use of this software in source and binary forms,
0011 with or without modification, are permitted provided that the following
0012 conditions are met:
0013 
0014 * Redistributions of source code must retain the above
0015   copyright notice, this list of conditions and the
0016   following disclaimer.
0017 
0018 * Redistributions in binary form must reproduce the above
0019   copyright notice, this list of conditions and the
0020   following disclaimer in the documentation and/or other
0021   materials provided with the distribution.
0022 
0023 * Neither the name of the assimp team, nor the names of its
0024   contributors may be used to endorse or promote products
0025   derived from this software without specific prior
0026   written permission of the assimp team.
0027 
0028 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0029 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0030 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0031 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0032 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0033 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0034 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0035 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0036 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0037 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0038 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0039 ---------------------------------------------------------------------------
0040 */
0041 
0042 /** @file IOSystem.hpp
0043  *  @brief File system wrapper for C++. Inherit this class to supply
0044  *  custom file handling logic to the Import library.
0045 */
0046 
0047 #pragma once
0048 #ifndef AI_IOSYSTEM_H_INC
0049 #define AI_IOSYSTEM_H_INC
0050 
0051 #ifdef __GNUC__
0052 #   pragma GCC system_header
0053 #endif
0054 
0055 #ifndef __cplusplus
0056 #   error This header requires C++ to be used. aiFileIO.h is the \
0057     corresponding C interface.
0058 #endif
0059 
0060 #include "types.h"
0061 
0062 #ifdef _WIN32
0063 #   include <direct.h>
0064 #   include <cstdlib>
0065 #   include <cstdio>
0066 #else
0067 #   include <sys/stat.h>
0068 #   include <sys/types.h>
0069 #   include <unistd.h>
0070 #endif // _WIN32
0071 
0072 #include <vector>
0073 
0074 namespace Assimp    {
0075 
0076 class IOStream;
0077 
0078 // ---------------------------------------------------------------------------
0079 /** @brief CPP-API: Interface to the file system.
0080  *
0081  *  Derive an own implementation from this interface to supply custom file handling
0082  *  to the importer library. If you implement this interface, you also want to
0083  *  supply a custom implementation for IOStream.
0084  *
0085  *  @see Importer::SetIOHandler()
0086  */
0087 class ASSIMP_API IOSystem
0088 #ifndef SWIG
0089     : public Intern::AllocateFromAssimpHeap
0090 #endif
0091 {
0092 public:
0093 
0094     // -------------------------------------------------------------------
0095     /** @brief Default constructor.
0096      *
0097      *  Create an instance of your derived class and assign it to an
0098      *  #Assimp::Importer instance by calling Importer::SetIOHandler().
0099      */
0100     IOSystem() AI_NO_EXCEPT = default;
0101 
0102     // -------------------------------------------------------------------
0103     /** @brief Virtual destructor.
0104      *
0105      *  It is safe to be called from within DLL Assimp, we're constructed
0106      *  on Assimp's heap.
0107      */
0108     virtual ~IOSystem() = default;
0109 
0110     // -------------------------------------------------------------------
0111     /** @brief For backward compatibility
0112      *  @see Exists(const char*)
0113      */
0114     AI_FORCE_INLINE bool Exists( const std::string& pFile) const;
0115 
0116     // -------------------------------------------------------------------
0117     /** @brief Tests for the existence of a file at the given path.
0118      *
0119      * @param pFile Path to the file
0120      * @return true if there is a file with this path, else false.
0121      */
0122     virtual bool Exists( const char* pFile) const = 0;
0123 
0124     // -------------------------------------------------------------------
0125     /** @brief Returns the system specific directory separator
0126      *  @return System specific directory separator
0127      */
0128     virtual char getOsSeparator() const = 0;
0129 
0130     // -------------------------------------------------------------------
0131     /** @brief Open a new file with a given path.
0132      *
0133      *  When the access to the file is finished, call Close() to release
0134      *  all associated resources (or the virtual dtor of the IOStream).
0135      *
0136      *  @param pFile Path to the file
0137      *  @param pMode Desired file I/O mode. Required are: "wb", "w", "wt",
0138      *         "rb", "r", "rt".
0139      *
0140      *  @return New IOStream interface allowing the lib to access
0141      *         the underlying file.
0142      *  @note When implementing this class to provide custom IO handling,
0143      *  you probably have to supply an own implementation of IOStream as well.
0144      */
0145     virtual IOStream* Open(const char* pFile,
0146         const char* pMode = "rb") = 0;
0147 
0148     // -------------------------------------------------------------------
0149     /** @brief For backward compatibility
0150      *  @see Open(const char*, const char*)
0151      */
0152     inline IOStream* Open(const std::string& pFile,
0153         const std::string& pMode = std::string("rb"));
0154 
0155     // -------------------------------------------------------------------
0156     /** @brief Closes the given file and releases all resources
0157      *    associated with it.
0158      *  @param pFile The file instance previously created by Open().
0159      */
0160     virtual void Close( IOStream* pFile) = 0;
0161 
0162     // -------------------------------------------------------------------
0163     /** @brief Compares two paths and check whether the point to
0164      *         identical files.
0165      *
0166      * The dummy implementation of this virtual member performs a
0167      * case-insensitive comparison of the given strings. The default IO
0168      * system implementation uses OS mechanisms to convert relative into
0169      * absolute paths, so the result can be trusted.
0170      * @param one First file
0171      * @param second Second file
0172      * @return true if the paths point to the same file. The file needn't
0173      *   be existing, however.
0174      */
0175     virtual bool ComparePaths (const char* one,
0176         const char* second) const;
0177 
0178     // -------------------------------------------------------------------
0179     /** @brief For backward compatibility
0180      *  @see ComparePaths(const char*, const char*)
0181      */
0182     inline bool ComparePaths (const std::string& one,
0183         const std::string& second) const;
0184 
0185     // -------------------------------------------------------------------
0186     /** @brief Pushes a new directory onto the directory stack.
0187      *  @param path Path to push onto the stack.
0188      *  @return True, when push was successful, false if path is empty.
0189      */
0190     virtual bool PushDirectory( const std::string &path );
0191 
0192     // -------------------------------------------------------------------
0193     /** @brief Returns the top directory from the stack.
0194      *  @return The directory on the top of the stack.
0195      *          Returns empty when no directory was pushed to the stack.
0196      */
0197     virtual const std::string &CurrentDirectory() const;
0198 
0199     // -------------------------------------------------------------------
0200     /** @brief Returns the number of directories stored on the stack.
0201      *  @return The number of directories of the stack.
0202      */
0203     virtual size_t StackSize() const;
0204 
0205     // -------------------------------------------------------------------
0206     /** @brief Pops the top directory from the stack.
0207      *  @return True, when a directory was on the stack. False if no
0208      *          directory was on the stack.
0209      */
0210     virtual bool PopDirectory();
0211 
0212     // -------------------------------------------------------------------
0213     /** @brief CReates an new directory at the given path.
0214      *  @param  path    [in] The path to create.
0215      *  @return True, when a directory was created. False if the directory
0216      *           cannot be created.
0217      */
0218     virtual bool CreateDirectory( const std::string &path );
0219 
0220     // -------------------------------------------------------------------
0221     /** @brief Will change the current directory to the given path.
0222      *  @param path     [in] The path to change to.
0223      *  @return True, when the directory has changed successfully.
0224      */
0225     virtual bool ChangeDirectory( const std::string &path );
0226 
0227     // -------------------------------------------------------------------
0228     /**
0229      *  @brief  Will delete the given file.
0230      *  @param file     [in] The filename
0231      *  @return true, if the file wase deleted, false if not.
0232      */
0233     virtual bool DeleteFile(const std::string &file);
0234 
0235 private:
0236     std::vector<std::string> m_pathStack;
0237 };
0238 
0239 // ----------------------------------------------------------------------------
0240 // For compatibility, the interface of some functions taking a std::string was
0241 // changed to const char* to avoid crashes between binary incompatible STL
0242 // versions. This code her is inlined,  so it shouldn't cause any problems.
0243 // ----------------------------------------------------------------------------
0244 
0245 // ----------------------------------------------------------------------------
0246 AI_FORCE_INLINE IOStream* IOSystem::Open(const std::string& pFile, const std::string& pMode) {
0247     // NOTE:
0248     // For compatibility, interface was changed to const char* to
0249     // avoid crashes between binary incompatible STL versions
0250     return Open(pFile.c_str(),pMode.c_str());
0251 }
0252 
0253 // ----------------------------------------------------------------------------
0254 AI_FORCE_INLINE bool IOSystem::Exists( const std::string& pFile) const {
0255     // NOTE:
0256     // For compatibility, interface was changed to const char* to
0257     // avoid crashes between binary incompatible STL versions
0258     return Exists(pFile.c_str());
0259 }
0260 
0261 // ----------------------------------------------------------------------------
0262 AI_FORCE_INLINE bool IOSystem::ComparePaths(const std::string& one, const std::string& second) const {
0263     // NOTE:
0264     // For compatibility, interface was changed to const char* to
0265     // avoid crashes between binary incompatible STL versions
0266     return ComparePaths(one.c_str(),second.c_str());
0267 }
0268 
0269 // ----------------------------------------------------------------------------
0270 AI_FORCE_INLINE bool IOSystem::PushDirectory( const std::string &path ) {
0271     if ( path.empty() ) {
0272         return false;
0273     }
0274 
0275     m_pathStack.push_back( path );
0276 
0277     return true;
0278 }
0279 
0280 // ----------------------------------------------------------------------------
0281 AI_FORCE_INLINE size_t IOSystem::StackSize() const {
0282     return m_pathStack.size();
0283 }
0284 
0285 // ----------------------------------------------------------------------------
0286 AI_FORCE_INLINE bool IOSystem::PopDirectory() {
0287     if ( m_pathStack.empty() ) {
0288         return false;
0289     }
0290 
0291     m_pathStack.pop_back();
0292 
0293     return true;
0294 }
0295 
0296 // ----------------------------------------------------------------------------
0297 AI_FORCE_INLINE bool IOSystem::CreateDirectory( const std::string &path ) {
0298     if ( path.empty() ) {
0299         return false;
0300     }
0301 
0302 #ifdef _WIN32
0303     return 0 != ::_mkdir( path.c_str() );
0304 #else
0305     return 0 != ::mkdir( path.c_str(), 0777 );
0306 #endif // _WIN32
0307 }
0308 
0309 // ----------------------------------------------------------------------------
0310 AI_FORCE_INLINE bool IOSystem::ChangeDirectory( const std::string &path ) {
0311     if ( path.empty() ) {
0312         return false;
0313     }
0314 
0315 #ifdef _WIN32
0316     return 0 != ::_chdir( path.c_str() );
0317 #else
0318     return 0 != ::chdir( path.c_str() );
0319 #endif // _WIN32
0320 }
0321 
0322 
0323 // ----------------------------------------------------------------------------
0324 AI_FORCE_INLINE bool IOSystem::DeleteFile( const std::string &file ) {
0325     if ( file.empty() ) {
0326         return false;
0327     }
0328     const int retCode( ::remove( file.c_str() ) );
0329     return ( 0 == retCode );
0330 }
0331 } //!ns Assimp
0332 
0333 #endif //AI_IOSYSTEM_H_INC