Skip to content

Commit 6bf6b47

Browse files
committed
Use only the file buffer in stream adapter.
1 parent 664699f commit 6bf6b47

5 files changed

Lines changed: 38 additions & 38 deletions

File tree

assimpjs/example/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ static File GetFile (const std::string& filePath)
1111
return File ();
1212
}
1313
size_t fileSize = stream->FileSize ();
14-
std::vector<std::uint8_t> content (fileSize);
14+
Buffer content (fileSize);
1515
stream->Read (&content[0], 1, fileSize);
1616
return File (filePath, content);
1717
}

assimpjs/src/fileio.cpp

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,63 +11,62 @@ static char GetOsSeparator ()
1111
#endif
1212
}
1313

14-
15-
FileIOStreamAdapter::FileIOStreamAdapter (const File& file) :
16-
file (file),
14+
BufferIOStreamAdapter::BufferIOStreamAdapter (const Buffer& buffer) :
15+
buffer (buffer),
1716
position (0)
1817
{
1918
}
2019

21-
FileIOStreamAdapter::~FileIOStreamAdapter ()
20+
BufferIOStreamAdapter::~BufferIOStreamAdapter ()
2221
{
2322
}
2423

25-
size_t FileIOStreamAdapter::Read (void* pvBuffer, size_t pSize, size_t pCount)
24+
size_t BufferIOStreamAdapter::Read (void* pvBuffer, size_t pSize, size_t pCount)
2625
{
2726
size_t remainingElemCount = (size_t) (std::floor ((FileSize () - position) / pSize));
2827
size_t readableElemCount = std::min (remainingElemCount, pCount);
2928
if (readableElemCount == 0) {
3029
return 0;
3130
}
32-
memcpy (pvBuffer, &file.content[position], readableElemCount * pSize);
31+
memcpy (pvBuffer, &buffer[position], readableElemCount * pSize);
3332
position += readableElemCount * pSize;
3433
return readableElemCount;
3534
}
3635

37-
size_t FileIOStreamAdapter::Write (const void* pvBuffer, size_t pSize, size_t pCount)
36+
size_t BufferIOStreamAdapter::Write (const void* pvBuffer, size_t pSize, size_t pCount)
3837
{
3938
throw std::logic_error ("not implemented");
4039
}
4140

42-
aiReturn FileIOStreamAdapter::Seek (size_t pOffset, aiOrigin pOrigin)
41+
aiReturn BufferIOStreamAdapter::Seek (size_t pOffset, aiOrigin pOrigin)
4342
{
4443
switch (pOrigin) {
45-
case aiOrigin_SET:
46-
position = pOffset;
47-
break;
48-
case aiOrigin_CUR:
49-
position += pOffset;
50-
break;
51-
case aiOrigin_END:
52-
position = file.content.size () - pOffset;
53-
break;
54-
default:
55-
break;
44+
case aiOrigin_SET:
45+
position = pOffset;
46+
break;
47+
case aiOrigin_CUR:
48+
position += pOffset;
49+
break;
50+
case aiOrigin_END:
51+
position = buffer.size () - pOffset;
52+
break;
53+
default:
54+
break;
5655
}
5756
return aiReturn::aiReturn_SUCCESS;
5857
}
5958

60-
size_t FileIOStreamAdapter::Tell () const
59+
size_t BufferIOStreamAdapter::Tell () const
6160
{
6261
return position;
6362
}
6463

65-
size_t FileIOStreamAdapter::FileSize () const
64+
size_t BufferIOStreamAdapter::FileSize () const
6665
{
67-
return file.content.size ();
66+
return buffer.size ();
6867
}
6968

70-
void FileIOStreamAdapter::Flush ()
69+
void BufferIOStreamAdapter::Flush ()
7170
{
7271

7372
}
@@ -93,7 +92,7 @@ Assimp::IOStream* FileListIOSystemAdapter::Open (const char* pFile, const char*
9392
if (foundFile == nullptr) {
9493
return nullptr;
9594
}
96-
return new FileIOStreamAdapter (*foundFile);
95+
return new BufferIOStreamAdapter (foundFile->content);
9796
}
9897

9998
void FileListIOSystemAdapter::Close (Assimp::IOStream* pFile)

assimpjs/src/fileio.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
#include "filelist.hpp"
88

9-
class FileIOStreamAdapter : public Assimp::IOStream
9+
class BufferIOStreamAdapter : public Assimp::IOStream
1010
{
1111
public:
12-
FileIOStreamAdapter (const File& file);
13-
virtual ~FileIOStreamAdapter ();
12+
BufferIOStreamAdapter (const Buffer& buffer);
13+
virtual ~BufferIOStreamAdapter ();
1414

1515
virtual size_t Read (void* pvBuffer, size_t pSize, size_t pCount) override;
1616
virtual size_t Write (const void* pvBuffer, size_t pSize, size_t pCount) override;
@@ -19,11 +19,10 @@ class FileIOStreamAdapter : public Assimp::IOStream
1919
virtual size_t Tell () const override;
2020

2121
virtual size_t FileSize () const override;
22-
2322
virtual void Flush () override;
2423

2524
private:
26-
const File& file;
25+
const Buffer& buffer;
2726
size_t position;
2827
};
2928

assimpjs/src/filelist.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ File::File () :
2828
{
2929
}
3030

31-
File::File (const std::string& path, const std::vector<std::uint8_t>& content) :
31+
File::File (const std::string& path, const Buffer& content) :
3232
path (path),
3333
content (content)
3434
{
@@ -44,7 +44,7 @@ FileList::FileList () :
4444
{
4545
}
4646

47-
void FileList::AddFile (const std::string& path, const std::vector<std::uint8_t>& content)
47+
void FileList::AddFile (const std::string& path, const Buffer& content)
4848
{
4949
files.push_back (File (path, content));
5050
}
@@ -75,7 +75,7 @@ const File* FileList::GetFile (const std::string& path) const
7575

7676
void FileList::AddFileEmscripten (const std::string& path, const emscripten::val& content)
7777
{
78-
std::vector<std::uint8_t> contentArr = emscripten::vecFromJSArray<std::uint8_t> (content);
78+
Buffer contentArr = emscripten::vecFromJSArray<std::uint8_t> (content);
7979
AddFile (path, contentArr);
8080
}
8181
#endif

assimpjs/src/filelist.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,26 @@
88
#include <vector>
99
#include <string>
1010

11+
using Buffer = std::vector<std::uint8_t>;
12+
1113
class File
1214
{
1315
public:
1416
File ();
15-
File (const std::string& path, const std::vector<std::uint8_t>& content);
17+
File (const std::string& path, const Buffer& content);
1618

17-
bool IsValid () const;
19+
bool IsValid () const;
1820

19-
std::string path;
20-
std::vector<std::uint8_t> content;
21+
std::string path;
22+
Buffer content;
2123
};
2224

2325
class FileList
2426
{
2527
public:
2628
FileList ();
2729

28-
void AddFile (const std::string& path, const std::vector<std::uint8_t>& content);
30+
void AddFile (const std::string& path, const Buffer& content);
2931

3032
size_t FileCount () const;
3133
const File* GetFile (size_t index) const;

0 commit comments

Comments
 (0)