SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLIOLocalStorage.cpp
Go to the documentation of this file.
1 /**
2  * \file SLIOLocalStorage.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 <SLIOLocalStorage.h>
11 
12 #ifdef SL_STORAGE_WEB
13 //-----------------------------------------------------------------------------
14 bool SLIOLocalStorage::exists(std::string path)
15 {
16  // clang-format off
17  return MAIN_THREAD_EM_ASM_INT({
18  let path = UTF8ToString($0);
19  let key = "SL:" + path;
20  return localStorage.getItem(key) !== null;
21  }, path.c_str());
22  // clang-format on
23 }
24 //-----------------------------------------------------------------------------
26  : SLIOReaderMemory(path)
27 {
28  char* data;
29  int length;
30 
31  // clang-format off
32  MAIN_THREAD_EM_ASM({
33  let path = UTF8ToString($0);
34  let key = "SL:" + path;
35  let item = localStorage.getItem(key);
36  let string = atob(item);
37 
38  // Allocate one more byte than necessary because stringToAscii includes a null terminator.
39  let buffer = _malloc(string.length + 1);
40  stringToAscii(string, buffer, string.length + 1);
41 
42  setValue($1, buffer, "i8*");
43  setValue($2, string.length, "i32");
44  }, path.c_str(), &data, &length);
45  // clang-format on
46 
47  std::vector<char> vector(data, data + length);
48  SLIOMemory::set(path, vector);
49 }
50 //-----------------------------------------------------------------------------
52 {
54 }
55 //-----------------------------------------------------------------------------
57  : SLIOWriterMemory(path)
58 {
59 }
60 //-----------------------------------------------------------------------------
62 {
63  std::vector<char>& buffer = SLIOMemory::get(_path);
64  const char* data = buffer.data();
65  size_t length = buffer.size();
66 
67  // clang-format off
68  MAIN_THREAD_EM_ASM({
69  let path = UTF8ToString($0);
70  let section = HEAPU8.subarray($1, $1 + $2);
71 
72  let array = new Uint8Array(section);
73  let binary = "";
74  for(let i = 0; i < array.byteLength; i++)
75  binary += String.fromCharCode(array[i]);
76 
77  let key = "SL:" + path;
78  localStorage.setItem(key, btoa(binary));
79  }, _path.c_str(), data, length);
80  // clang-format on
81 }
82 //-----------------------------------------------------------------------------
83 #endif
SLIOReaderLocalStorage(std::string path)
SLIOStream implementation for reading from memory.
Definition: SLIOMemory.h:28
std::string _path
Definition: SLIOMemory.h:37
SLIOWriterLocalStorage(std::string path)
SLIOStream implementation for reading to memory.
Definition: SLIOMemory.h:43
std::string _path
Definition: SLIOMemory.h:52
bool exists(std::string path)
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