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

Collection of functions to open, use and close streams. More...

Functions

SLIOStreamopen (std::string path, SLIOStreamKind kind, SLIOStreamMode mode)
 Opens a file stream for I/O operations. More...
 
void close (SLIOStream *stream)
 Closes and deletes a stream. More...
 
bool exists (std::string path, SLIOStreamKind kind)
 Checks whether a given file exists. More...
 
SLIOBuffer readIntoBuffer (std::string path, SLIOStreamKind kind)
 Reads an entire file into memory. More...
 
std::string readIntoString (std::string path, SLIOStreamKind kind)
 Reads an entire file into a string. More...
 
void writeString (std::string path, SLIOStreamKind kind, const std::string &string)
 Writes a string to a file. More...
 

Detailed Description

Collection of functions to open, use and close streams.

Function Documentation

◆ close()

void SLFileStorage::close ( SLIOStream stream)

Closes and deletes a stream.

First flushes the stream to make sure that all data is written and then deletes it. Implementations of SLIOStream will then close their backing streams in the destructor.

Parameters
streamStream to close

Definition at line 106 of file SLFileStorage.cpp.

107 {
108  stream->flush();
109  delete stream;
110 }
virtual void flush()
Definition: SLFileStorage.h:77

◆ exists()

bool SLFileStorage::exists ( std::string  path,
SLIOStreamKind  kind 
)

Checks whether a given file exists.

Checks whether a file exists at the given path and if it is of the given kind. When running in a web browser, the function may need to communicate with a server to check whether the file exists, which is quite slow.

Parameters
pathPath to file
kindKind of file
Returns
True if the file exists

Definition at line 121 of file SLFileStorage.cpp.

122 {
123 #if defined(SL_STORAGE_FS)
124  return Utils::fileExists(path);
125 #elif defined(SL_STORAGE_WEB)
126  if (path == "")
127  return false;
128 
129  if (kind == IOK_shader)
130  return SLIOReaderFetch::exists(path);
131  else if (kind == IOK_config)
133  else
134  return SLIOReaderFetch::exists(path);
135 #endif
136 }
@ IOK_config
Definition: SLFileStorage.h:44
@ IOK_shader
Definition: SLFileStorage.h:42
static bool exists(std::string url)
Definition: SLIOFetch.cpp:19
bool exists(std::string path)
bool fileExists(const string &pathfilename)
Returns true if a file exists.
Definition: Utils.cpp:897

◆ open()

SLIOStream * SLFileStorage::open ( std::string  path,
SLIOStreamKind  kind,
SLIOStreamMode  mode 
)

Opens a file stream for I/O operations.

Opens a file stream and prepares it for reading or writing. After usage, the stream should be closed by calling SLFileStorage::close. The function uses the kind and the mode to determine which kind of stream it should create. For example, in a web browser, configuration files are written to local storage.

Parameters
pathPath to file
kindKind of file
modeMode to open the stream in
Returns
Opened stream ready for I/O

Definition at line 50 of file SLFileStorage.cpp.

53 {
54 #if defined(SL_STORAGE_FS)
55  if (mode == IOM_read)
56  return new SLIOReaderNative(path);
57  else if (mode == IOM_write)
58  return new SLIOWriterNative(path);
59  else
60  return nullptr;
61 #elif defined(SL_STORAGE_WEB)
62  Utils::log("I/O", "OPENING \"%s\", (%d)", path.c_str(), kind);
63 
64  if (mode == IOM_read)
65  {
66  if (kind == IOK_shader || kind == IOK_image || kind == IOK_model || kind == IOK_font)
67  {
68  // Shaders, images, models and fonts are always stored on the server.
69  return new SLIOReaderFetch(path);
70  }
71  else if (kind == IOK_config)
72  {
73  // Config files written by the application (e.g. by Dear ImGUI)
74  // are stored in the browsers local storage, other config files
75  // are stored on the server.
76 
77  if (SLIOLocalStorage::exists(path))
78  return new SLIOReaderLocalStorage(path);
79  else
80  return new SLIOReaderFetch(path);
81  }
82  }
83  else if (mode == IOM_write)
84  {
85  // Config files are written to local storage so they can be read when
86  // the website is reloaded, images (e.g. screenshots) are displayed in
87  // the browser window so the user can download them.
88 
89  if (kind == IOK_config)
90  return new SLIOWriterLocalStorage(path);
91  else if (kind == IOK_image)
92  return new SLIOWriterBrowserPopup(path);
93  }
94 
95  return nullptr;
96 #endif
97 }
@ IOM_read
Definition: SLFileStorage.h:50
@ IOM_write
Definition: SLFileStorage.h:51
@ IOK_font
Definition: SLFileStorage.h:43
@ IOK_image
Definition: SLFileStorage.h:40
@ IOK_model
Definition: SLFileStorage.h:41
static WAI::ModeOrbSlam2 * mode
Definition: WAIInterface.cpp:5
SLIOStream implementation for downloading files from a web server.
Definition: SLIOFetch.h:25
SLIOStream implementation for reading from browser local storage.
SLIOStream implementation for reading from native files.
Definition: SLIONative.h:19
SLIOStream implementation to display PNG files in a browser popup.
SLIOStream implementation for writing to browser local storage.
SLIOStream implementation for writing to native files.
Definition: SLIONative.h:33
void log(const char *tag, const char *format,...)
logs a formatted string platform independently
Definition: Utils.cpp:1103

◆ readIntoBuffer()

SLIOBuffer SLFileStorage::readIntoBuffer ( std::string  path,
SLIOStreamKind  kind 
)

Reads an entire file into memory.

Opens a stream from the path provided in read mode, allocates a buffer for its content, reads the content into the buffer and closes the stream. The buffer is owned by the caller and must be deallocated with a call to SLFileStorage::deleteBuffer after usage.

Parameters
pathPath to the file to read
kindKind of the file to read
Returns
Buffer holding the file contents and size

Definition at line 148 of file SLFileStorage.cpp.

149 {
150  SLIOStream* stream = open(path, kind, IOM_read);
151  size_t size = stream->size();
152  unsigned char* data = new unsigned char[size];
153  stream->read(data, size);
154  close(stream);
155 
156  return SLIOBuffer{data, size};
157 }
Interface for accessing external data using streams.
Definition: SLFileStorage.h:62
virtual size_t read(void *buffer, size_t size)
Definition: SLFileStorage.h:72
virtual size_t size()
Definition: SLFileStorage.h:76
void close(SLIOStream *stream)
Closes and deletes a stream.
SLIOStream * open(std::string path, SLIOStreamKind kind, SLIOStreamMode mode)
Opens a file stream for I/O operations.
Utility struct that holds a pointer and its length.
Definition: SLFileStorage.h:28

◆ readIntoString()

std::string SLFileStorage::readIntoString ( std::string  path,
SLIOStreamKind  kind 
)

Reads an entire file into a string.

Opens a stream from the path provided in read mode, allocates a strings for its content, reads the content into the string and closes the stream. Line endings are NOT converted to LF, which means that on Windows, the string may contain CRLF line endings.

Parameters
pathPath to the file to read
kindKind of the file to read
Returns
String containing the contents of the file

Definition at line 169 of file SLFileStorage.cpp.

170 {
171  SLIOStream* stream = open(path, kind, IOM_read);
172  size_t size = stream->size();
173  std::string string;
174  string.resize(size);
175  stream->read((void*)string.data(), size);
176  close(stream);
177 
178  return string;
179 }

◆ writeString()

void SLFileStorage::writeString ( std::string  path,
SLIOStreamKind  kind,
const std::string &  string 
)

Writes a string to a file.

Opens a stream to the path provided in write mode, writes the string to the file and closes the stream. Line endings are NOT converted to LF, which means that on Windows, the file may contain LF line endings after writing instead of CRLF line endings.

Parameters
pathThe path to the file to write to
kindThe kind of the file to write to
stringThe string to write to the file

Definition at line 191 of file SLFileStorage.cpp.

194 {
195  SLIOStream* stream = open(path, kind, IOM_write);
196  stream->write(string.c_str(), string.size());
197  close(stream);
198 }
virtual size_t write(const void *buffer, size_t size)
Definition: SLFileStorage.h:73