SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLFileStorage.h
Go to the documentation of this file.
1 /**
2  * \file SLFileStorage.h
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 #ifndef SLPROJECT_SLFILESTORAGE_H
11 #define SLPROJECT_SLFILESTORAGE_H
12 
13 #include <Utils.h>
14 #include <string>
15 
16 #ifndef __EMSCRIPTEN__
17 # define SL_STORAGE_FS
18 # include <fstream>
19 #else
20 # define SL_STORAGE_WEB
21 # include <emscripten.h>
22 # include <unordered_map>
23 #endif
24 
25 //-----------------------------------------------------------------------------
26 //! Utility struct that holds a pointer and its length
27 struct SLIOBuffer
28 {
29  unsigned char* data;
30  size_t size;
31 
32  SLIOBuffer copy();
33  void deallocate();
34 };
35 //-----------------------------------------------------------------------------
36 //! Enum of file kinds
38 {
45 };
46 //-----------------------------------------------------------------------------
47 //! Enum of stream opening modes
49 {
51  IOM_write
52 };
53 //-----------------------------------------------------------------------------
54 //! Interface for accessing external data using streams
55 /*!
56  * SLIOStream provides an interface to access files which may be stored in the
57  * native file system, on a remote server, in memory, etc. Streams should not
58  * be instantiated by users of the class, but by SLFileStorage::open, which
59  * selects the appropriate stream implementation for a given kind and mode.
60  */
62 {
63 public:
64  enum Origin
65  {
68  IOO_end
69  };
70 
71  virtual ~SLIOStream() = default;
72  virtual size_t read(void* buffer, size_t size) { return 0; }
73  virtual size_t write(const void* buffer, size_t size) { return 0; }
74  virtual size_t tell() { return 0; }
75  virtual bool seek(size_t offset, Origin origin) { return false; }
76  virtual size_t size() { return 0; }
77  virtual void flush() {}
78 };
79 //-----------------------------------------------------------------------------
80 //! Collection of functions to open, use and close streams
81 namespace SLFileStorage
82 {
83 SLIOStream* open(std::string path, SLIOStreamKind kind, SLIOStreamMode mode);
84 void close(SLIOStream* stream);
85 bool exists(std::string path, SLIOStreamKind kind);
86 SLIOBuffer readIntoBuffer(std::string path, SLIOStreamKind kind);
87 std::string readIntoString(std::string path, SLIOStreamKind kind);
88 void writeString(std::string path, SLIOStreamKind kind, const std::string& string);
89 }
90 //-----------------------------------------------------------------------------
91 #endif
SLIOStreamMode
Enum of stream opening modes.
Definition: SLFileStorage.h:49
@ IOM_read
Definition: SLFileStorage.h:50
@ IOM_write
Definition: SLFileStorage.h:51
SLIOStreamKind
Enum of file kinds.
Definition: SLFileStorage.h:38
@ IOK_font
Definition: SLFileStorage.h:43
@ IOK_config
Definition: SLFileStorage.h:44
@ IOK_image
Definition: SLFileStorage.h:40
@ IOK_shader
Definition: SLFileStorage.h:42
@ IOK_generic
Definition: SLFileStorage.h:39
@ IOK_model
Definition: SLFileStorage.h:41
static WAI::ModeOrbSlam2 * mode
Definition: WAIInterface.cpp:5
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 ~SLIOStream()=default
virtual size_t size()
Definition: SLFileStorage.h:76
virtual size_t tell()
Definition: SLFileStorage.h:74
Collection of functions to open, use and close streams.
Definition: SLFileStorage.h:82
void close(SLIOStream *stream)
Closes and deletes a stream.
SLIOBuffer readIntoBuffer(std::string path, SLIOStreamKind kind)
Reads an entire file into memory.
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.
std::string readIntoString(std::string path, SLIOStreamKind kind)
Reads an entire file into a string.
void writeString(std::string path, SLIOStreamKind kind, const std::string &string)
Writes a string to a file.
Utility struct that holds a pointer and its length.
Definition: SLFileStorage.h:28
SLIOBuffer copy()
Creates a copy of the data in the buffer.
void deallocate()
Deallocates the data owned by the buffer.
size_t size
Definition: SLFileStorage.h:30
unsigned char * data
Definition: SLFileStorage.h:29