SLProject 4.0.000
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...
 
void deleteBuffer (SLIOBuffer &buffer)
 Deallocates the data of a buffer returned by SLFileStorage::readIntoBuffer. 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

◆ deleteBuffer()

void SLFileStorage::deleteBuffer ( SLIOBuffer buffer)

Deallocates the data of a buffer returned by SLFileStorage::readIntoBuffer.

Deallocates the data owned by an SLIOBuffer. This function should be called as soon as the memory of readIntoBuffer is not used anymore.

Parameters
bufferBuffer to delete

◆ 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

◆ 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

◆ 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

◆ 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

◆ 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