Path Tracer
MemoryIOWrapper.h
Go to the documentation of this file.
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4 
5 Copyright (c) 2006-2020, assimp team
6 
7 
8 All rights reserved.
9 
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the
12 following conditions are met:
13 
14 * Redistributions of source code must retain the above
15  copyright notice, this list of conditions and the
16  following disclaimer.
17 
18 * Redistributions in binary form must reproduce the above
19  copyright notice, this list of conditions and the
20  following disclaimer in the documentation and/or other
21  materials provided with the distribution.
22 
23 * Neither the name of the assimp team, nor the names of its
24  contributors may be used to endorse or promote products
25  derived from this software without specific prior
26  written permission of the assimp team.
27 
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 
40 ----------------------------------------------------------------------
41 */
42 
45 #pragma once
46 #ifndef AI_MEMORYIOSTREAM_H_INC
47 #define AI_MEMORYIOSTREAM_H_INC
48 
49 #ifdef __GNUC__
50 # pragma GCC system_header
51 #endif
52 
53 #include <assimp/IOStream.hpp>
54 #include <assimp/IOSystem.hpp>
55 #include <assimp/ai_assert.h>
56 
57 #include <stdint.h>
58 
59 namespace Assimp {
60 
61 #define AI_MEMORYIO_MAGIC_FILENAME "$$$___magic___$$$"
62 #define AI_MEMORYIO_MAGIC_FILENAME_LENGTH 17
63 
64 // ----------------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------------
67 class MemoryIOStream : public IOStream {
68 public:
69  MemoryIOStream (const uint8_t* buff, size_t len, bool own = false)
70  : buffer (buff)
71  , length(len)
72  , pos((size_t)0)
73  , own(own) {
74  // empty
75  }
76 
77  ~MemoryIOStream () {
78  if(own) {
79  delete[] buffer;
80  }
81  }
82 
83  // -------------------------------------------------------------------
84  // Read from stream
85  size_t Read(void* pvBuffer, size_t pSize, size_t pCount) {
86  ai_assert(nullptr != pvBuffer);
87  ai_assert(0 != pSize);
88 
89  const size_t cnt = std::min( pCount, (length-pos) / pSize);
90  const size_t ofs = pSize * cnt;
91 
92  ::memcpy(pvBuffer,buffer+pos,ofs);
93  pos += ofs;
94 
95  return cnt;
96  }
97 
98  // -------------------------------------------------------------------
99  // Write to stream
100  size_t Write(const void* /*pvBuffer*/, size_t /*pSize*/,size_t /*pCount*/) {
101  ai_assert(false); // won't be needed
102  return 0;
103  }
104 
105  // -------------------------------------------------------------------
106  // Seek specific position
107  aiReturn Seek(size_t pOffset, aiOrigin pOrigin) {
108  if (aiOrigin_SET == pOrigin) {
109  if (pOffset > length) {
110  return AI_FAILURE;
111  }
112  pos = pOffset;
113  } else if (aiOrigin_END == pOrigin) {
114  if (pOffset > length) {
115  return AI_FAILURE;
116  }
117  pos = length-pOffset;
118  } else {
119  if (pOffset+pos > length) {
120  return AI_FAILURE;
121  }
122  pos += pOffset;
123  }
124  return AI_SUCCESS;
125  }
126 
127  // -------------------------------------------------------------------
128  // Get current seek position
129  size_t Tell() const {
130  return pos;
131  }
132 
133  // -------------------------------------------------------------------
134  // Get size of file
135  size_t FileSize() const {
136  return length;
137  }
138 
139  // -------------------------------------------------------------------
140  // Flush file contents
141  void Flush() {
142  ai_assert(false); // won't be needed
143  }
144 
145 private:
146  const uint8_t* buffer;
147  size_t length,pos;
148  bool own;
149 };
150 
151 // ---------------------------------------------------------------------------
153 class MemoryIOSystem : public IOSystem {
154 public:
156  MemoryIOSystem(const uint8_t* buff, size_t len, IOSystem* io)
157  : buffer(buff)
158  , length(len)
159  , existing_io(io)
160  , created_streams() {
161  // empty
162  }
163 
166  }
167 
168  // -------------------------------------------------------------------
170  bool Exists(const char* pFile) const override {
171  if (0 == strncmp( pFile, AI_MEMORYIO_MAGIC_FILENAME, AI_MEMORYIO_MAGIC_FILENAME_LENGTH ) ) {
172  return true;
173  }
174  return existing_io ? existing_io->Exists(pFile) : false;
175  }
176 
177  // -------------------------------------------------------------------
179  char getOsSeparator() const override {
180  return existing_io ? existing_io->getOsSeparator()
181  : '/'; // why not? it doesn't care
182  }
183 
184  // -------------------------------------------------------------------
186  IOStream* Open(const char* pFile, const char* pMode = "rb") override {
187  if ( 0 == strncmp( pFile, AI_MEMORYIO_MAGIC_FILENAME, AI_MEMORYIO_MAGIC_FILENAME_LENGTH ) ) {
188  created_streams.emplace_back(new MemoryIOStream(buffer, length));
189  return created_streams.back();
190  }
191  return existing_io ? existing_io->Open(pFile, pMode) : NULL;
192  }
193 
194  // -------------------------------------------------------------------
196  void Close( IOStream* pFile) override {
197  auto it = std::find(created_streams.begin(), created_streams.end(), pFile);
198  if (it != created_streams.end()) {
199  delete pFile;
200  created_streams.erase(it);
201  } else if (existing_io) {
202  existing_io->Close(pFile);
203  }
204  }
205 
206  // -------------------------------------------------------------------
208  bool ComparePaths(const char* one, const char* second) const override {
209  return existing_io ? existing_io->ComparePaths(one, second) : false;
210  }
211 
212  bool PushDirectory( const std::string &path ) override {
213  return existing_io ? existing_io->PushDirectory(path) : false;
214  }
215 
216  const std::string &CurrentDirectory() const override {
217  static std::string empty;
218  return existing_io ? existing_io->CurrentDirectory() : empty;
219  }
220 
221  size_t StackSize() const override {
222  return existing_io ? existing_io->StackSize() : 0;
223  }
224 
225  bool PopDirectory() override {
226  return existing_io ? existing_io->PopDirectory() : false;
227  }
228 
229  bool CreateDirectory( const std::string &path ) override {
230  return existing_io ? existing_io->CreateDirectory(path) : false;
231  }
232 
233  bool ChangeDirectory( const std::string &path ) override {
234  return existing_io ? existing_io->ChangeDirectory(path) : false;
235  }
236 
237  bool DeleteFile( const std::string &file ) override {
238  return existing_io ? existing_io->DeleteFile(file) : false;
239  }
240 
241 private:
242  const uint8_t* buffer;
243  size_t length;
244  IOSystem* existing_io;
245  std::vector<IOStream*> created_streams;
246 };
247 
248 } // end namespace Assimp
249 
250 #endif
Assimp::MemoryIOStream::Read
size_t Read(void *pvBuffer, size_t pSize, size_t pCount)
Read from the file.
Definition: MemoryIOWrapper.h:85
Assimp::MemoryIOSystem::ChangeDirectory
bool ChangeDirectory(const std::string &path) override
Will change the current directory to the given path.
Definition: MemoryIOWrapper.h:233
aiOrigin
aiOrigin
Definition: types.h:426
Assimp::MemoryIOSystem::CreateDirectory
bool CreateDirectory(const std::string &path) override
CReates an new directory at the given path.
Definition: MemoryIOWrapper.h:229
Assimp::IOSystem::ChangeDirectory
virtual bool ChangeDirectory(const std::string &path)
Will change the current directory to the given path.
Definition: IOSystem.hpp:337
aiOrigin_SET
@ aiOrigin_SET
Definition: types.h:428
Assimp::IOSystem::CreateDirectory
virtual bool CreateDirectory(const std::string &path)
CReates an new directory at the given path.
Definition: IOSystem.hpp:323
IOSystem.hpp
File system wrapper for C++. Inherit this class to supply custom file handling logic to the Import li...
Assimp::IOSystem::IOSystem
IOSystem() AI_NO_EXCEPT
Default constructor.
Definition: IOSystem.hpp:237
Assimp::MemoryIOSystem::~MemoryIOSystem
~MemoryIOSystem()
Definition: MemoryIOWrapper.h:165
Assimp::MemoryIOSystem::MemoryIOSystem
MemoryIOSystem(const uint8_t *buff, size_t len, IOSystem *io)
Definition: MemoryIOWrapper.h:156
Assimp::IOSystem::StackSize
virtual size_t StackSize() const
Returns the number of directories stored on the stack.
Definition: IOSystem.hpp:305
Assimp::MemoryIOSystem
Definition: MemoryIOWrapper.h:153
aiOrigin_END
@ aiOrigin_END
Definition: types.h:434
Assimp::MemoryIOSystem::PushDirectory
bool PushDirectory(const std::string &path) override
Pushes a new directory onto the directory stack.
Definition: MemoryIOWrapper.h:212
aiReturn
aiReturn
Definition: types.h:397
Assimp::MemoryIOSystem::ComparePaths
bool ComparePaths(const char *one, const char *second) const override
Definition: MemoryIOWrapper.h:208
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::MemoryIOStream::FileSize
size_t FileSize() const
Returns filesize Returns the filesize.
Definition: MemoryIOWrapper.h:135
Assimp::MemoryIOSystem::Open
IOStream * Open(const char *pFile, const char *pMode="rb") override
Definition: MemoryIOWrapper.h:186
Assimp::MemoryIOStream::Tell
size_t Tell() const
Get the current position of the read/write cursor.
Definition: MemoryIOWrapper.h:129
Assimp::IOSystem::PushDirectory
virtual bool PushDirectory(const std::string &path)
Pushes a new directory onto the directory stack.
Definition: IOSystem.hpp:283
Assimp::MemoryIOSystem::CurrentDirectory
const std::string & CurrentDirectory() const override
Returns the top directory from the stack.
Definition: MemoryIOWrapper.h:216
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::MemoryIOSystem::Exists
bool Exists(const char *pFile) const override
Definition: MemoryIOWrapper.h:170
Assimp::MemoryIOStream
Definition: MemoryIOWrapper.h:67
Assimp::IOSystem
CPP-API: Interface to the file system.
Definition: IOSystem.hpp:93
Assimp::MemoryIOStream::Seek
aiReturn Seek(size_t pOffset, aiOrigin pOrigin)
Set the read/write cursor of the file.
Definition: MemoryIOWrapper.h:107
Assimp::MemoryIOSystem::PopDirectory
bool PopDirectory() override
Pops the top directory from the stack.
Definition: MemoryIOWrapper.h:225
Assimp::MemoryIOStream::Flush
void Flush()
Flush the contents of the file buffer (for writers) See fflush() for more details.
Definition: MemoryIOWrapper.h:141
Assimp::MemoryIOSystem::StackSize
size_t StackSize() const override
Returns the number of directories stored on the stack.
Definition: MemoryIOWrapper.h:221
IOStream.hpp
File I/O wrappers for C++.
Assimp::MemoryIOSystem::Close
void Close(IOStream *pFile) override
Definition: MemoryIOWrapper.h:196
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::MemoryIOStream::Write
size_t Write(const void *, size_t, size_t)
Write to the file.
Definition: MemoryIOWrapper.h:100
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::MemoryIOSystem::getOsSeparator
char getOsSeparator() const override
Definition: MemoryIOWrapper.h:179
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