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