SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLAssimpIOSystem.cpp
Go to the documentation of this file.
1 /**
2  * \file SLAssimpIOStream.cpp
3  * \date May 2024
4  * \authors Marino von Wattenwyl
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8 */
9 
10 #include <SLAssimpIOSystem.h>
11 
12 //-----------------------------------------------------------------------------
14  : _stream(stream)
15 {
16 }
17 //-----------------------------------------------------------------------------
18 size_t SLAssimpIOStream::Read(void* pvBuffer, size_t pSize, size_t pCount)
19 {
20  char* dest = (char*)pvBuffer;
21  size_t totalSize = pCount * pSize;
22  size_t sizeRead = _stream->read(dest, totalSize);
23  return sizeRead / pSize;
24 }
25 //-----------------------------------------------------------------------------
26 size_t SLAssimpIOStream::Write(const void* pvBuffer, size_t pSize, size_t pCount)
27 {
28  const char* dest = (const char*)pvBuffer;
29  size_t totalSize = pCount * pSize;
30  return _stream->write(dest, totalSize);
31 }
32 //-----------------------------------------------------------------------------
33 aiReturn SLAssimpIOStream::Seek(size_t pOffset, aiOrigin pOrigin)
34 {
36  if (pOrigin == aiOrigin_SET)
37  streamOrigin = SLIOStream::IOO_beg;
38  else if (pOrigin == aiOrigin_CUR)
39  streamOrigin = SLIOStream::IOO_cur;
40  else if (pOrigin == aiOrigin_END)
41  streamOrigin = SLIOStream::IOO_end;
42 
43  bool successful = _stream->seek(pOffset, streamOrigin);
44  return successful ? aiReturn_SUCCESS : aiReturn_FAILURE;
45 }
46 //-----------------------------------------------------------------------------
47 size_t SLAssimpIOStream::Tell() const
48 {
49  return _stream->tell();
50 }
51 //-----------------------------------------------------------------------------
53 {
54  return _stream->size();
55 }
56 //-----------------------------------------------------------------------------
58 {
59  _stream->flush();
60 }
61 //-----------------------------------------------------------------------------
62 bool SLAssimpIOSystem::Exists(const char* pFile) const
63 {
64  return SLFileStorage::exists(pFile, IOK_model);
65 }
66 //-----------------------------------------------------------------------------
68 {
69  return '/';
70 }
71 //-----------------------------------------------------------------------------
72 Assimp::IOStream* SLAssimpIOSystem::Open(const char* pFile, const char* pMode)
73 {
74  // Assimp requires the modes "wb", "w", "wt", "rb", "r" and "rt".
75  // The second character is ignored because SLIOStreams are always binary.
76  // Therefor we only need to check the first character to determine
77  // whether we are reading or writing.
78  SLIOStreamMode streamMode = IOM_read;
79  if (pMode[0] == 'r')
80  streamMode = IOM_read;
81  else if (pMode[0] == 'w')
82  streamMode = IOM_write;
83 
84  SLIOStream* stream = SLFileStorage::open(pFile, IOK_model, streamMode);
85  return new SLAssimpIOStream(stream);
86 }
87 //-----------------------------------------------------------------------------
88 void SLAssimpIOSystem::Close(Assimp::IOStream* pFile)
89 {
90  SLAssimpIOStream* stream = dynamic_cast<SLAssimpIOStream*>(pFile);
91  SLFileStorage::close(stream->stream());
92  delete stream;
93 }
94 //-----------------------------------------------------------------------------
SLIOStreamMode
Enum of stream opening modes.
Definition: SLFileStorage.h:49
@ IOM_read
Definition: SLFileStorage.h:50
@ IOM_write
Definition: SLFileStorage.h:51
@ IOK_model
Definition: SLFileStorage.h:41
void Flush() override
aiReturn Seek(size_t pOffset, aiOrigin pOrigin) override
size_t FileSize() const override
SLAssimpIOStream(SLIOStream *stream)
SLIOStream * stream()
size_t Read(void *pvBuffer, size_t pSize, size_t pCount) override
size_t Write(const void *pvBuffer, size_t pSize, size_t pCount) override
SLIOStream * _stream
size_t Tell() const override
Assimp::IOStream * Open(const char *pFile, const char *pMode) override
char getOsSeparator() const override
bool Exists(const char *pFile) const override
void Close(Assimp::IOStream *pFile) override
Interface for accessing external data using streams.
Definition: SLFileStorage.h:62
virtual void flush()
Definition: SLFileStorage.h:77
virtual bool seek(size_t offset, Origin origin)
Definition: SLFileStorage.h:75
virtual size_t write(const void *buffer, size_t size)
Definition: SLFileStorage.h:73
virtual size_t read(void *buffer, size_t size)
Definition: SLFileStorage.h:72
virtual size_t size()
Definition: SLFileStorage.h:76
virtual size_t tell()
Definition: SLFileStorage.h:74
void close(SLIOStream *stream)
Closes and deletes a stream.
bool exists(std::string path, SLIOStreamKind kind)
Checks whether a given file exists.
SLIOStream * open(std::string path, SLIOStreamKind kind, SLIOStreamMode mode)
Opens a file stream for I/O operations.