Path Tracer
BlobIOSystem.h
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 
47 #ifndef AI_BLOBIOSYSTEM_H_INCLUDED
48 #define AI_BLOBIOSYSTEM_H_INCLUDED
49 
50 #include <assimp/cexport.h>
51 #include <stdint.h>
52 #include <assimp/DefaultLogger.hpp>
53 #include <assimp/IOStream.hpp>
54 #include <assimp/IOSystem.hpp>
55 #include <set>
56 #include <vector>
57 
58 namespace Assimp {
59 class BlobIOSystem;
60 
61 // --------------------------------------------------------------------------------------------
63 // --------------------------------------------------------------------------------------------
64 class BlobIOStream : public IOStream {
65 public:
66  BlobIOStream(BlobIOSystem *creator, const std::string &file, size_t initial = 4096) :
67  buffer(),
68  cur_size(),
69  file_size(),
70  cursor(),
71  initial(initial),
72  file(file),
73  creator(creator) {
74  // empty
75  }
76 
77  virtual ~BlobIOStream();
78 
79 public:
80  // -------------------------------------------------------------------
81  aiExportDataBlob *GetBlob() {
82  aiExportDataBlob *blob = new aiExportDataBlob();
83  blob->size = file_size;
84  blob->data = buffer;
85 
86  buffer = nullptr;
87 
88  return blob;
89  }
90 
91  // -------------------------------------------------------------------
92  virtual size_t Read(void *,
93  size_t,
94  size_t) {
95  return 0;
96  }
97 
98  // -------------------------------------------------------------------
99  virtual size_t Write(const void *pvBuffer,
100  size_t pSize,
101  size_t pCount) {
102  pSize *= pCount;
103  if (cursor + pSize > cur_size) {
104  Grow(cursor + pSize);
105  }
106 
107  memcpy(buffer + cursor, pvBuffer, pSize);
108  cursor += pSize;
109 
110  file_size = std::max(file_size, cursor);
111  return pCount;
112  }
113 
114  // -------------------------------------------------------------------
115  virtual aiReturn Seek(size_t pOffset,
116  aiOrigin pOrigin) {
117  switch (pOrigin) {
118  case aiOrigin_CUR:
119  cursor += pOffset;
120  break;
121 
122  case aiOrigin_END:
123  cursor = file_size - pOffset;
124  break;
125 
126  case aiOrigin_SET:
127  cursor = pOffset;
128  break;
129 
130  default:
131  return AI_FAILURE;
132  }
133 
134  if (cursor > file_size) {
135  Grow(cursor);
136  }
137 
138  file_size = std::max(cursor, file_size);
139  return AI_SUCCESS;
140  }
141 
142  // -------------------------------------------------------------------
143  virtual size_t Tell() const {
144  return cursor;
145  }
146 
147  // -------------------------------------------------------------------
148  virtual size_t FileSize() const {
149  return file_size;
150  }
151 
152  // -------------------------------------------------------------------
153  virtual void Flush() {
154  // ignore
155  }
156 
157 private:
158  // -------------------------------------------------------------------
159  void Grow(size_t need = 0) {
160  // 1.5 and phi are very heap-friendly growth factors (the first
161  // allows for frequent re-use of heap blocks, the second
162  // forms a fibonacci sequence with similar characteristics -
163  // since this heavily depends on the heap implementation
164  // and other factors as well, i'll just go with 1.5 since
165  // it is quicker to compute).
166  size_t new_size = std::max(initial, std::max(need, cur_size + (cur_size >> 1)));
167 
168  const uint8_t *const old = buffer;
169  buffer = new uint8_t[new_size];
170 
171  if (old) {
172  memcpy(buffer, old, cur_size);
173  delete[] old;
174  }
175 
176  cur_size = new_size;
177  }
178 
179 private:
180  uint8_t *buffer;
181  size_t cur_size, file_size, cursor, initial;
182 
183  const std::string file;
184  BlobIOSystem *const creator;
185 };
186 
187 #define AI_BLOBIO_MAGIC "$blobfile"
188 
189 // --------------------------------------------------------------------------------------------
191 // --------------------------------------------------------------------------------------------
192 class BlobIOSystem : public IOSystem {
193 
194  friend class BlobIOStream;
195  typedef std::pair<std::string, aiExportDataBlob *> BlobEntry;
196 
197 public:
198  BlobIOSystem() {
199  }
200 
201  virtual ~BlobIOSystem() {
202  for (BlobEntry &blobby : blobs) {
203  delete blobby.second;
204  }
205  }
206 
207 public:
208  // -------------------------------------------------------------------
209  const char *GetMagicFileName() const {
210  return AI_BLOBIO_MAGIC;
211  }
212 
213  // -------------------------------------------------------------------
214  aiExportDataBlob *GetBlobChain() {
215  // one must be the master
216  aiExportDataBlob *master = nullptr, *cur;
217  for (const BlobEntry &blobby : blobs) {
218  if (blobby.first == AI_BLOBIO_MAGIC) {
219  master = blobby.second;
220  break;
221  }
222  }
223  if (!master) {
224  ASSIMP_LOG_ERROR("BlobIOSystem: no data written or master file was not closed properly.");
225  return nullptr;
226  }
227 
228  master->name.Set("");
229 
230  cur = master;
231  for (const BlobEntry &blobby : blobs) {
232  if (blobby.second == master) {
233  continue;
234  }
235 
236  cur->next = blobby.second;
237  cur = cur->next;
238 
239  // extract the file extension from the file written
240  const std::string::size_type s = blobby.first.find_first_of('.');
241  cur->name.Set(s == std::string::npos ? blobby.first : blobby.first.substr(s + 1));
242  }
243 
244  // give up blob ownership
245  blobs.clear();
246  return master;
247  }
248 
249 public:
250  // -------------------------------------------------------------------
251  virtual bool Exists(const char *pFile) const {
252  return created.find(std::string(pFile)) != created.end();
253  }
254 
255  // -------------------------------------------------------------------
256  virtual char getOsSeparator() const {
257  return '/';
258  }
259 
260  // -------------------------------------------------------------------
261  virtual IOStream *Open(const char *pFile,
262  const char *pMode) {
263  if (pMode[0] != 'w') {
264  return nullptr;
265  }
266 
267  created.insert(std::string(pFile));
268  return new BlobIOStream(this, std::string(pFile));
269  }
270 
271  // -------------------------------------------------------------------
272  virtual void Close(IOStream *pFile) {
273  delete pFile;
274  }
275 
276 private:
277  // -------------------------------------------------------------------
278  void OnDestruct(const std::string &filename, BlobIOStream *child) {
279  // we don't know in which the files are closed, so we
280  // can't reliably say that the first must be the master
281  // file ...
282  blobs.push_back(BlobEntry(filename, child->GetBlob()));
283  }
284 
285 private:
286  std::set<std::string> created;
287  std::vector<BlobEntry> blobs;
288 };
289 
290 // --------------------------------------------------------------------------------------------
291 BlobIOStream ::~BlobIOStream() {
292  creator->OnDestruct(file, this);
293  delete[] buffer;
294 }
295 
296 } // namespace Assimp
297 
298 #endif
aiExportDataBlob
Definition: cexport.h:200
aiOrigin
aiOrigin
Definition: types.h:426
Assimp::BlobIOStream::Flush
virtual void Flush()
Flush the contents of the file buffer (for writers) See fflush() for more details.
Definition: BlobIOSystem.h:153
Assimp::BlobIOStream::Tell
virtual size_t Tell() const
Get the current position of the read/write cursor.
Definition: BlobIOSystem.h:143
aiOrigin_SET
@ aiOrigin_SET
Definition: types.h:428
aiExportDataBlob::name
C_STRUCT aiString name
Definition: cexport.h:219
Assimp::BlobIOStream::Read
virtual size_t Read(void *, size_t, size_t)
Read from the file.
Definition: BlobIOSystem.h:92
IOSystem.hpp
File system wrapper for C++. Inherit this class to supply custom file handling logic to the Import li...
aiOrigin_END
@ aiOrigin_END
Definition: types.h:434
Assimp::BlobIOSystem::getOsSeparator
virtual char getOsSeparator() const
Returns the system specific directory separator.
Definition: BlobIOSystem.h:256
Assimp::BlobIOSystem::Open
virtual IOStream * Open(const char *pFile, const char *pMode)
Open a new file with a given path.
Definition: BlobIOSystem.h:261
aiReturn
aiReturn
Definition: types.h:397
aiExportDataBlob::data
void * data
The data.
Definition: cexport.h:205
Assimp::BlobIOSystem::Close
virtual void Close(IOStream *pFile)
Closes the given file and releases all resources associated with it.
Definition: BlobIOSystem.h:272
Assimp::BlobIOStream::Write
virtual size_t Write(const void *pvBuffer, size_t pSize, size_t pCount)
Write to the file.
Definition: BlobIOSystem.h:99
Assimp::BlobIOStream::Seek
virtual aiReturn Seek(size_t pOffset, aiOrigin pOrigin)
Set the read/write cursor of the file.
Definition: BlobIOSystem.h:115
Assimp::BlobIOStream
Definition: BlobIOSystem.h:64
cexport.h
Defines the C-API for the Assimp export interface.
aiExportDataBlob::next
C_STRUCT aiExportDataBlob * next
Definition: cexport.h:222
Assimp::BlobIOSystem
Definition: BlobIOSystem.h:192
Assimp::IOSystem
CPP-API: Interface to the file system.
Definition: IOSystem.hpp:93
IOStream.hpp
File I/O wrappers for C++.
Assimp
Definition: ai_assert.h:50
aiExportDataBlob::size
size_t size
Size of the data in bytes.
Definition: cexport.h:202
Assimp::BlobIOSystem::Exists
virtual bool Exists(const char *pFile) const
Tests for the existence of a file at the given path.
Definition: BlobIOSystem.h:251
Assimp::BlobIOStream::FileSize
virtual size_t FileSize() const
Returns filesize Returns the filesize.
Definition: BlobIOSystem.h:148
Assimp::IOStream
CPP-API: Class to handle file I/O for C++.
Definition: IOStream.hpp:75
DefaultLogger.hpp
aiOrigin_CUR
@ aiOrigin_CUR
Definition: types.h:431