Path Tracer
IOSystem.hpp
Go to the documentation of this file.
1 /*
2 ---------------------------------------------------------------------------
3 Open Asset Import Library (assimp)
4 ---------------------------------------------------------------------------
5 
6 Copyright (c) 2006-2020, assimp team
7 
8 
9 
10 All rights reserved.
11 
12 Redistribution and use of this software in source and binary forms,
13 with or without modification, are permitted provided that the following
14 conditions are met:
15 
16 * Redistributions of source code must retain the above
17  copyright notice, this list of conditions and the
18  following disclaimer.
19 
20 * Redistributions in binary form must reproduce the above
21  copyright notice, this list of conditions and the
22  following disclaimer in the documentation and/or other
23  materials provided with the distribution.
24 
25 * Neither the name of the assimp team, nor the names of its
26  contributors may be used to endorse or promote products
27  derived from this software without specific prior
28  written permission of the assimp team.
29 
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 ---------------------------------------------------------------------------
42 */
43 
49 #pragma once
50 #ifndef AI_IOSYSTEM_H_INC
51 #define AI_IOSYSTEM_H_INC
52 
53 #ifdef __GNUC__
54 # pragma GCC system_header
55 #endif
56 
57 #ifndef __cplusplus
58 # error This header requires C++ to be used. aiFileIO.h is the \
59  corresponding C interface.
60 #endif
61 
62 #include "types.h"
63 
64 #ifdef _WIN32
65 # include <direct.h>
66 # include <stdlib.h>
67 # include <stdio.h>
68 #else
69 # include <sys/stat.h>
70 # include <sys/types.h>
71 # include <unistd.h>
72 #endif // _WIN32
73 
74 #include <vector>
75 
76 namespace Assimp {
77 
78  class IOStream;
79 
80 // ---------------------------------------------------------------------------
89 class ASSIMP_API IOSystem
90 #ifndef SWIG
91  : public Intern::AllocateFromAssimpHeap
92 #endif
93 {
94 public:
95 
96  // -------------------------------------------------------------------
102  IOSystem() AI_NO_EXCEPT;
103 
104  // -------------------------------------------------------------------
110  virtual ~IOSystem();
111 
112  // -------------------------------------------------------------------
116  AI_FORCE_INLINE bool Exists( const std::string& pFile) const;
117 
118  // -------------------------------------------------------------------
124  virtual bool Exists( const char* pFile) const = 0;
125 
126  // -------------------------------------------------------------------
130  virtual char getOsSeparator() const = 0;
131 
132  // -------------------------------------------------------------------
147  virtual IOStream* Open(const char* pFile,
148  const char* pMode = "rb") = 0;
149 
150  // -------------------------------------------------------------------
154  inline IOStream* Open(const std::string& pFile,
155  const std::string& pMode = std::string("rb"));
156 
157  // -------------------------------------------------------------------
162  virtual void Close( IOStream* pFile) = 0;
163 
164  // -------------------------------------------------------------------
177  virtual bool ComparePaths (const char* one,
178  const char* second) const;
179 
180  // -------------------------------------------------------------------
184  inline bool ComparePaths (const std::string& one,
185  const std::string& second) const;
186 
187  // -------------------------------------------------------------------
192  virtual bool PushDirectory( const std::string &path );
193 
194  // -------------------------------------------------------------------
199  virtual const std::string &CurrentDirectory() const;
200 
201  // -------------------------------------------------------------------
205  virtual size_t StackSize() const;
206 
207  // -------------------------------------------------------------------
212  virtual bool PopDirectory();
213 
214  // -------------------------------------------------------------------
220  virtual bool CreateDirectory( const std::string &path );
221 
222  // -------------------------------------------------------------------
227  virtual bool ChangeDirectory( const std::string &path );
228 
229  virtual bool DeleteFile( const std::string &file );
230 
231 private:
232  std::vector<std::string> m_pathStack;
233 };
234 
235 // ----------------------------------------------------------------------------
236 AI_FORCE_INLINE
237 IOSystem::IOSystem() AI_NO_EXCEPT
238 : m_pathStack() {
239  // empty
240 }
241 
242 // ----------------------------------------------------------------------------
243 AI_FORCE_INLINE
245  // empty
246 }
247 
248 // ----------------------------------------------------------------------------
249 // For compatibility, the interface of some functions taking a std::string was
250 // changed to const char* to avoid crashes between binary incompatible STL
251 // versions. This code her is inlined, so it shouldn't cause any problems.
252 // ----------------------------------------------------------------------------
253 
254 // ----------------------------------------------------------------------------
255 AI_FORCE_INLINE
256 IOStream* IOSystem::Open(const std::string& pFile, const std::string& pMode) {
257  // NOTE:
258  // For compatibility, interface was changed to const char* to
259  // avoid crashes between binary incompatible STL versions
260  return Open(pFile.c_str(),pMode.c_str());
261 }
262 
263 // ----------------------------------------------------------------------------
264 AI_FORCE_INLINE
265 bool IOSystem::Exists( const std::string& pFile) const {
266  // NOTE:
267  // For compatibility, interface was changed to const char* to
268  // avoid crashes between binary incompatible STL versions
269  return Exists(pFile.c_str());
270 }
271 
272 // ----------------------------------------------------------------------------
273 AI_FORCE_INLINE
274 bool IOSystem::ComparePaths (const std::string& one, const std::string& second) const {
275  // NOTE:
276  // For compatibility, interface was changed to const char* to
277  // avoid crashes between binary incompatible STL versions
278  return ComparePaths(one.c_str(),second.c_str());
279 }
280 
281 // ----------------------------------------------------------------------------
282 AI_FORCE_INLINE
283 bool IOSystem::PushDirectory( const std::string &path ) {
284  if ( path.empty() ) {
285  return false;
286  }
287 
288  m_pathStack.push_back( path );
289 
290  return true;
291 }
292 
293 // ----------------------------------------------------------------------------
294 AI_FORCE_INLINE
295 const std::string &IOSystem::CurrentDirectory() const {
296  if ( m_pathStack.empty() ) {
297  static const std::string Dummy("");
298  return Dummy;
299  }
300  return m_pathStack[ m_pathStack.size()-1 ];
301 }
302 
303 // ----------------------------------------------------------------------------
304 AI_FORCE_INLINE
305 size_t IOSystem::StackSize() const {
306  return m_pathStack.size();
307 }
308 
309 // ----------------------------------------------------------------------------
310 AI_FORCE_INLINE
312  if ( m_pathStack.empty() ) {
313  return false;
314  }
315 
316  m_pathStack.pop_back();
317 
318  return true;
319 }
320 
321 // ----------------------------------------------------------------------------
322 AI_FORCE_INLINE
323 bool IOSystem::CreateDirectory( const std::string &path ) {
324  if ( path.empty() ) {
325  return false;
326  }
327 
328 #ifdef _WIN32
329  return 0 != ::_mkdir( path.c_str() );
330 #else
331  return 0 != ::mkdir( path.c_str(), 0777 );
332 #endif // _WIN32
333 }
334 
335 // ----------------------------------------------------------------------------
336 AI_FORCE_INLINE
337 bool IOSystem::ChangeDirectory( const std::string &path ) {
338  if ( path.empty() ) {
339  return false;
340  }
341 
342 #ifdef _WIN32
343  return 0 != ::_chdir( path.c_str() );
344 #else
345  return 0 != ::chdir( path.c_str() );
346 #endif // _WIN32
347 }
348 
349 
350 // ----------------------------------------------------------------------------
351 AI_FORCE_INLINE
352 bool IOSystem::DeleteFile( const std::string &file ) {
353  if ( file.empty() ) {
354  return false;
355  }
356  const int retCode( ::remove( file.c_str() ) );
357  return ( 0 == retCode );
358 }
359 }
360 
361 #endif //AI_IOSYSTEM_H_INC
types.h
Assimp::IOSystem::ChangeDirectory
virtual bool ChangeDirectory(const std::string &path)
Will change the current directory to the given path.
Definition: IOSystem.hpp:337
Assimp::IOSystem::CreateDirectory
virtual bool CreateDirectory(const std::string &path)
CReates an new directory at the given path.
Definition: IOSystem.hpp:323
Assimp::IOSystem::IOSystem
IOSystem() AI_NO_EXCEPT
Default constructor.
Definition: IOSystem.hpp:237
Assimp::IOSystem::StackSize
virtual size_t StackSize() const
Returns the number of directories stored on the stack.
Definition: IOSystem.hpp:305
Assimp::IOSystem::ComparePaths
virtual bool ComparePaths(const char *one, const char *second) const
Compares two paths and check whether the point to identical files.
Assimp::IOSystem::~IOSystem
virtual ~IOSystem()
Virtual destructor.
Definition: IOSystem.hpp:244
Assimp::IOSystem::Exists
virtual bool Exists(const char *pFile) const =0
Tests for the existence of a file at the given path.
Assimp::IOSystem::PushDirectory
virtual bool PushDirectory(const std::string &path)
Pushes a new directory onto the directory stack.
Definition: IOSystem.hpp:283
Assimp::IOSystem::getOsSeparator
virtual char getOsSeparator() const =0
Returns the system specific directory separator.
Assimp::IOSystem::PopDirectory
virtual bool PopDirectory()
Pops the top directory from the stack.
Definition: IOSystem.hpp:311
Assimp::IOSystem
CPP-API: Interface to the file system.
Definition: IOSystem.hpp:93
Assimp::IOSystem::CurrentDirectory
virtual const std::string & CurrentDirectory() const
Returns the top directory from the stack.
Definition: IOSystem.hpp:295
Assimp
Definition: ai_assert.h:50
Assimp::IOSystem::Close
virtual void Close(IOStream *pFile)=0
Closes the given file and releases all resources associated with it.
Assimp::IOSystem::Open
virtual IOStream * Open(const char *pFile, const char *pMode="rb")=0
Open a new file with a given path.
Assimp::IOSystem::Exists
AI_FORCE_INLINE bool Exists(const std::string &pFile) const
For backward compatibility.
Definition: IOSystem.hpp:265
Assimp::IOStream
CPP-API: Class to handle file I/O for C++.
Definition: IOStream.hpp:75