SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLIOMemory.cpp
Go to the documentation of this file.
1 /**
2  * \file SLIOMemory.cpp
3  * \date October 2022
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 <SLIOMemory.h>
11 
12 #ifdef SL_STORAGE_WEB
13 //-----------------------------------------------------------------------------
14 # include <unordered_map>
15 # include <vector>
16 //-----------------------------------------------------------------------------
17 std::unordered_map<std::string, std::vector<char>> memoryFiles;
18 //-----------------------------------------------------------------------------
19 bool SLIOMemory::exists(std::string path)
20 {
21  return memoryFiles.count(path);
22 }
23 //-----------------------------------------------------------------------------
24 std::vector<char>& SLIOMemory::get(std::string path)
25 {
26  return memoryFiles[path];
27 }
28 //-----------------------------------------------------------------------------
29 void SLIOMemory::set(std::string path, const std::vector<char>& data)
30 {
31  memoryFiles[path] = data;
32 }
33 //-----------------------------------------------------------------------------
34 void SLIOMemory::clear(std::string path)
35 {
36  memoryFiles.erase(path);
37 }
38 //-----------------------------------------------------------------------------
40  : _path(path)
41 {
42 }
43 //-----------------------------------------------------------------------------
44 size_t SLIOReaderMemory::read(void* buffer, size_t size)
45 {
46  std::vector<char>& memoryFile = memoryFiles[_path];
47  std::memcpy(buffer, memoryFile.data(), size);
48  return size;
49 }
50 //-----------------------------------------------------------------------------
52 {
53  return _position;
54 }
55 //-----------------------------------------------------------------------------
56 bool SLIOReaderMemory::seek(size_t offset, Origin origin)
57 {
58  std::vector<char>& memoryFile = memoryFiles[_path];
59  size_t size = (size_t)memoryFile.size();
60  size_t previousPos = _position;
61 
62  switch (origin)
63  {
64  case IOO_beg: _position = offset; break;
65  case IOO_cur: _position += offset; break;
66  case IOO_end: _position = size - 1 - offset; break;
67  }
68 
69  bool ok = _position >= 0 && _position < size;
70  if (!ok)
71  _position = previousPos;
72 
73  return ok;
74 }
75 //-----------------------------------------------------------------------------
77 {
78  return memoryFiles[_path].size();
79 }
80 //-----------------------------------------------------------------------------
82  : _path(path),
83  _position(0)
84 {
85  memoryFiles[_path].clear();
86 }
87 //-----------------------------------------------------------------------------
88 size_t SLIOWriterMemory::write(const void* buffer, size_t size)
89 {
90  std::vector<char>& memoryFile = memoryFiles[_path];
91  memoryFile.insert(memoryFile.end(), (char*)buffer, (char*)buffer + size);
92  _position += size;
93  return size;
94 }
95 //-----------------------------------------------------------------------------
97 {
98  return _position;
99 }
100 //-----------------------------------------------------------------------------
101 bool SLIOWriterMemory::seek(size_t offset, Origin origin)
102 {
103  std::vector<char>& memoryFile = memoryFiles[_path];
104  size_t size = (size_t)memoryFile.size();
105  size_t previousPos = _position;
106 
107  switch (origin)
108  {
109  case IOO_beg: _position = offset; break;
110  case IOO_cur: _position += offset; break;
111  case IOO_end: _position = size - 1 - offset; break;
112  }
113 
114  bool ok = _position >= 0 && _position < size;
115  if (!ok)
116  _position = previousPos;
117 
118  return ok;
119 }
120 //-----------------------------------------------------------------------------
122 {
123 }
124 //-----------------------------------------------------------------------------
125 #endif
std::unordered_map< std::string, std::vector< char > > memoryFiles
Definition: SLIOMemory.cpp:17
SLIOReaderMemory(std::string path)
Definition: SLIOMemory.cpp:39
size_t _position
Definition: SLIOMemory.h:38
bool seek(size_t offset, Origin origin)
Definition: SLIOMemory.cpp:56
size_t read(void *buffer, size_t size)
Definition: SLIOMemory.cpp:44
std::string _path
Definition: SLIOMemory.h:37
virtual size_t size()
Definition: SLFileStorage.h:76
bool seek(size_t offset, Origin origin)
Definition: SLIOMemory.cpp:101
SLIOWriterMemory(std::string path)
Definition: SLIOMemory.cpp:81
size_t write(const void *buffer, size_t size)
Definition: SLIOMemory.cpp:88
size_t _position
Definition: SLIOMemory.h:53
std::string _path
Definition: SLIOMemory.h:52
std::vector< char > & get(std::string path)
Definition: SLIOMemory.cpp:24
void set(std::string path, const std::vector< char > &data)
Definition: SLIOMemory.cpp:29
void clear(std::string path)
Definition: SLIOMemory.cpp:34
bool exists(std::string path)
Definition: SLIOMemory.cpp:19