SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLIOReaderLocalStorage Class Reference

SLIOStream implementation for reading from browser local storage. More...

#include <SLIOLocalStorage.h>

Inheritance diagram for SLIOReaderLocalStorage:
[legend]

Public Member Functions

 SLIOReaderLocalStorage (std::string path)
 
 ~SLIOReaderLocalStorage ()
 
- Public Member Functions inherited from SLIOReaderMemory
 SLIOReaderMemory (std::string path)
 
size_t read (void *buffer, size_t size)
 
size_t tell ()
 
bool seek (size_t offset, Origin origin)
 
size_t size ()
 
- Public Member Functions inherited from SLIOStream
virtual ~SLIOStream ()=default
 
virtual size_t write (const void *buffer, size_t size)
 
virtual void flush ()
 

Additional Inherited Members

- Public Types inherited from SLIOStream
enum  Origin { IOO_beg , IOO_cur , IOO_end }
 
- Protected Attributes inherited from SLIOReaderMemory
std::string _path
 
size_t _position
 

Detailed Description

SLIOStream implementation for reading from browser local storage.

The constructor loads the file from local storage into memory using the JavaScript API provided by the web browser.

Definition at line 29 of file SLIOLocalStorage.h.

Constructor & Destructor Documentation

◆ SLIOReaderLocalStorage()

SLIOReaderLocalStorage::SLIOReaderLocalStorage ( std::string  path)

Definition at line 25 of file SLIOLocalStorage.cpp.

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 }
SLIOReaderMemory(std::string path)
Definition: SLIOMemory.cpp:39
void set(std::string path, const std::vector< char > &data)
Definition: SLIOMemory.cpp:29

◆ ~SLIOReaderLocalStorage()

SLIOReaderLocalStorage::~SLIOReaderLocalStorage ( )

Definition at line 51 of file SLIOLocalStorage.cpp.

52 {
54 }
std::string _path
Definition: SLIOMemory.h:37
void clear(std::string path)
Definition: SLIOMemory.cpp:34

The documentation for this class was generated from the following files: