Skip to content

File include/brisk/core/IO.hpp


OpenFileMode enum

enum class OpenFileMode

OpenFileModeEnum class representing file opening modes.

ReadExisting enumerator (OpenFileMode::ReadExisting)

Opens a file for reading.

The file must exist; if it does not, the open fails.

ReadWriteExisting enumerator (OpenFileMode::ReadWriteExisting)

Opens a file for reading and writing.

The file must exist.

RewriteOrCreate enumerator (OpenFileMode::RewriteOrCreate)

Opens a file for writing.

If the file exists, it is truncated. If it does not exist, a new file is created.

ReadRewriteOrCreate enumerator (OpenFileMode::ReadRewriteOrCreate)

Opens a file for reading and writing.

If the file exists, it is truncated; if not, a new file is created.

AppendOrCreate enumerator (OpenFileMode::AppendOrCreate)

Opens a file for appending.

If the file exists, data is appended at the end. If it does not exist, a new file is created.

r enumerator (OpenFileMode::r)

Alias for ReadExisting.

r_plus enumerator (OpenFileMode::r_plus)

Alias for ReadWriteExisting.

w enumerator (OpenFileMode::w)

Alias for RewriteOrCreate.

w_plus enumerator (OpenFileMode::w_plus)

Alias for ReadRewriteOrCreate.

a enumerator (OpenFileMode::a)

Alias for AppendOrCreate.


openFile function

expected<RC<Stream>, IOError> openFile(
    const fs::path &filePath,
    OpenFileMode mode = OpenFileMode::ReadWriteExisting)

Opens a file with the specified path and mode.

This function attempts to open a file using the provided path and file mode. It returns an expected object containing either a reference-counted stream on success or an I/O error on failure.
Param filePath The path to the file to be opened.
Param mode The mode in which to open the file (default is OpenFileMode::ReadWriteExisting).
Returns An expected object containing a reference-counted stream or an I/O error.


openFileForReading function

expected<RC<Stream>, IOError>
openFileForReading(const fs::path &filePath)

Opens a file for reading.

This function opens the specified file for reading. It returns an expected object containing either a reference-counted stream on success or an I/O error on failure.
Param filePath The path to the file to be opened for reading.
Returns An expected object containing a reference-counted stream or an I/O error.


openFileForWriting function

expected<RC<Stream>, IOError>
openFileForWriting(const fs::path &filePath,
                   bool appending = false)

Opens a file for writing.

This function opens the specified file for writing. If the appending parameter is set to true, data will be appended to the end of the file. It returns an expected object containing either a reference-counted stream on success or an I/O error on failure.
Param filePath The path to the file to be opened for writing.
Param appending A flag indicating whether to append data to the end of the file (default is false).
Returns An expected object containing a reference-counted stream or an I/O error.


openFileForAppending function

inline expected<RC<Stream>, IOError>
openFileForAppending(const fs::path &filePath)

Opens a file for appending.

This function is a convenience wrapper that opens the specified file for writing in append mode. It returns an expected object containing either a reference-counted stream on success or an I/O error on failure.
Param filePath The path to the file to be opened for appending.
Returns An expected object containing a reference-counted stream or an I/O error.


openFile function

RC<Stream> openFile(std::FILE *file, bool owns = false)

Opens a file using a native file pointer.

This function opens a file given a native FILE* pointer. It allows specifying whether the function should take ownership of the file pointer. It returns a reference-counted stream.
Param file A pointer to the native file to be opened.
Param owns A flag indicating whether to take ownership of the file pointer (default is false).
Returns A reference-counted stream.


stdoutStream function

RC<Stream> stdoutStream()

Retrieves the standard output stream.

This function returns a reference-counted stream that points to the standard output.
Returns A reference-counted stream for standard output.


stderrStream function

RC<Stream> stderrStream()

Retrieves the standard error stream.

This function returns a reference-counted stream that points to the standard error output.
Returns A reference-counted stream for standard error.


stdinStream function

RC<Stream> stdinStream()

Retrieves the standard input stream.

This function returns a reference-counted stream that points to the standard input.
Returns A reference-counted stream for standard input.


fopen_native function

expected<std::FILE *, IOError>
fopen_native(const fs::path &file_name, OpenFileMode mode)

Opens a file using the fopen function. This function handles file name encoding across all platforms.

This function wraps the fopen call and returns an expected object containing either a file pointer or an I/O error.
Param file_name The path to the file to be opened.
Param mode The mode in which to open the file.
Returns An expected object containing a file pointer or an I/O error.


readBytes function

expected<bytes, IOError>
readBytes(const fs::path &file_name)

Reads the entire file as a vector of bytes.

This function reads all the contents of the specified file and returns them as a vector of bytes. It handles any I/O errors that may occur during the read operation, returning an expected object that contains either the byte vector or an I/O error.
Param file_name The path to the file to be read.
Returns An expected object containing a vector of bytes if the read operation is successful, or an I/O error if it fails.


readUtf8 function

expected<string, IOError>
readUtf8(const fs::path &file_name, bool removeBOM = true)

Reads the entire file as a UTF-8 encoded string.

This function reads a UTF-8 encoded string from the specified file. It can optionally remove the Byte Order Mark (BOM). It returns an expected object containing either the string or an I/O error.
Param file_name The path to the file to be read.
Param removeBOM A flag indicating whether to remove the BOM (default is true).
Returns An expected object containing the UTF-8 string or an I/O error.


readJson function

expected<Json, IOError> readJson(const fs::path &file_name)

Reads a JSON object from a file.

This function reads a JSON object from the specified file and returns an expected object containing either the JSON object or an I/O error.
Param file_name The path to the file to be read.
Returns An expected object containing the JSON object or an I/O error.


readMsgpack function

expected<Json, IOError>
readMsgpack(const fs::path &file_name)

Reads a JSON object in MessagePack format from a file.

This function reads a JSON object in MessagePack format from the specified file and returns an expected object containing either the JSON object or an I/O error.
Param file_name The path to the file to be read.
Returns An expected object containing the MessagePack object or an I/O error.


readLines function

expected<u8strings, IOError>
readLines(const fs::path &file_name)

Reads the entire file as a vector of UTF8-encoded strings.

This function reads lines from the specified file and returns an expected object containing either a vector of strings (each representing a line) or an I/O error.
Param file_name The path to the file to be read.
Returns An expected object containing a vector of lines or an I/O error.


writeBytes function

status<IOError> writeBytes(const fs::path &file_name,
                           const bytes_view &b)

Writes a byte span to a file.

This function writes bytes to the specified file and returns a status indicating success or an I/O error.
Param file_name The path to the file to be written.
Param b The bytes to write to the file.
Returns A status indicating success or an I/O error.


writeUtf8 function

status<IOError> writeUtf8(const fs::path &file_name,
                          string_view str,
                          bool useBOM = false)

Writes a UTF-8 encoded string to a file.

This function writes a UTF-8 encoded string to the specified file. It can optionally include a Byte Order Mark (BOM) at the beginning of the file.
Param file_name The path to the file to be written.
Param str The UTF-8 string to write to the file.
Param useBOM A flag indicating whether to include a BOM (default is false).
Returns A status indicating success or an I/O error.


writeJson function

status<IOError> writeJson(const fs::path &file_name,
                          const Json &j, int indent = 0)

Writes a JSON object to a file.

This function writes a JSON object to the specified file, optionally formatting it with the specified indentation level. If the indentation level is negative, tabs will be used instead of spaces for formatting.
Param file_name The path to the file to be written.
Param j The JSON object to write to the file.
Param indent The number of spaces to use for indentation (default is 0).
Returns A status indicating success or an I/O error.


writeMsgpack function

status<IOError> writeMsgpack(const fs::path &file_name,
                             const Json &j)

Writes a MessagePack object to a file.

This function writes a MessagePack object to the specified file.
Param file_name The path to the file to be written.
Param j The JSON object to write as MessagePack to the file.
Returns A status indicating success or an I/O error.


writeFromReader function

optional<uintmax_t> writeFromReader(RC<Stream> dest,
                                    RC<Stream> src,
                                    size_t bufSize = 65536)

Writes data from a reader stream to a destination stream.

This function reads data from the source stream and writes it to the destination stream, returning the number of bytes written or the nullopt value if an error occurs.
Param dest The destination stream to write to.
Param src The source stream to read from.
Param bufSize The buffer size to use for reading (default is 65536 bytes).
Returns An optional indicating the number of bytes written, or an empty optional if an error occurs.


DefaultFolder enum

enum class DefaultFolder

DefaultFolderEnum class representing default folder types.

This enum defines various types of default folders that can be accessed in the file system. These folders are commonly used for storing user-related data.

Documents enumerator (DefaultFolder::Documents)

Represents the Documents folder.

Pictures enumerator (DefaultFolder::Pictures)

Represents the Pictures folder.

Music enumerator (DefaultFolder::Music)

Represents the Music folder.

UserData enumerator (DefaultFolder::UserData)

Represents the User Data folder.

SystemData enumerator (DefaultFolder::SystemData)

Represents the System Data folder.

Home enumerator (DefaultFolder::Home)

Represents the Home folder.


defaultFolder function

fs::path defaultFolder(DefaultFolder folder)

Returns the path to a specified default folder.

This function takes a DefaultFolder enumeration value and returns the corresponding file system path to that folder.
Param folder The default folder to retrieve.
Returns A fs::path representing the path to the specified default folder.


fontFolders function

std::vector<fs::path> fontFolders()

Retrieves the paths of available font folders.

This function returns a vector containing the paths of directories where fonts are stored. It can be used to locate font files on the system.
Returns A vector of fs::path objects representing the font folder paths.


executablePath function

fs::path executablePath()

Retrieves the path to the executable file.

This function returns the file system path of the currently running executable.
Returns A fs::path representing the path to the executable file.


executableOrBundlePath function

fs::path executableOrBundlePath()

Retrieves the path to the executable or bundle.

This function returns the file system path of the current executable or its associated bundle, if applicable. This is useful for obtaining the location of the application package on certain platforms.
Returns A fs::path representing the path to the executable or bundle.


uniqueFileName function

fs::path uniqueFileName(std::string_view base,
                        std::string_view numbered,
                        int i = 1)

Generates a unique file name based on a base name and a numbering pattern.

This function checks if a file with the specified base name exists. If it does, it appends an incrementing number to the numbered pattern until a unique file name is found that does not already exist.

The numbered pattern is formatted using the given integer i to create the file name. The function ensures that the resulting file name is unique by incrementing i until a non-existing name is generated.
Param base The base name of the file to check for existence ("screenshot.png").
Param numbered A format string representing the naming pattern, which can include placeholders for the incrementing number ("screenshot ({}).png").
Param i The starting number to be used in the naming pattern.
Returns A fs::path representing a unique file name based on the input parameters.


tempFilePath function

fs::path tempFilePath(std::string pattern)

Generates a temporary file path based on a specified pattern.

This function creates a path for a temporary file by replacing placeholders in the given pattern. It replaces ? with a random character from the set of lowercase letters and digits, and * with 16 random characters from the same set.

The function is thread-safe, using a mutex to ensure that the random number generator is accessed in a synchronized manner.
Param pattern A string representing the desired pattern for the temporary file name. The pattern can include ? and * as placeholders for random characters.
Returns A fs::path representing the full path to the generated temporary file.


findDirNextToExe function

optional<fs::path>
findDirNextToExe(std::string_view dirName)

Finds a directory next to the executable.

This function searches for a directory specified by dirName adjacent to the current executable's path. It traverses up the directory hierarchy until it finds the specified directory or reaches the root of the file system.
Param dirName A string view representing the name of the directory to find.
Returns An optional containing the path to the found directory if it exists; otherwise, std::nullopt is returned if the directory is not found.


Auto-generated from sources, Revision , https://github.com/brisklib/brisk/blob//include/brisk/