FileIO

High-level utilities for loading files and opening native file dialogs.

Overview

Typical use cases:

  • Loading a file from disk:

  • Opening a file dialog to select a file:

  • Setting the working directory to the executable’s directory:

Header

<RaeptorCogs/IO/FileIO.hpp>

Metadata

Author

Estorc

Version

v1.0

Copyright

Copyright (c) 2025 Estorc MIT License.

Types

Type Definitions

Type

Description

RaeptorCogs::FileCallback

Callback function type for file dialog operations.

RaeptorCogs::FileData

Raw file data type.

RaeptorCogs::FileDialogFilter

File dialog filter type.

RaeptorCogs::FileDialogFilters

File dialog filter collection.

using RaeptorCogs::FileCallback = std::function<void(const RaeptorCogs::FileData&, const std::string&)>

Callback function type for file dialog operations.

This function is called when a file is selected in the file dialog. It receives the file data and the file name as parameters.

RaeptorCogs::OpenFileDialog([](const RaeptorCogs::FileData& data, const std::string& name) {
    // Handle the selected file data
});
using RaeptorCogs::FileData = std::vector<unsigned char>

Raw file data type.

Represents the contents of a file as a vector of unsigned char.

RaeptorCogs::FileData data = RaeptorCogs::LoadFile("example.txt");
using RaeptorCogs::FileDialogFilter = std::array<std::string, 2>

File dialog filter type.

Each filter is represented as an array of two strings: the first string is the filter name (e.g., “Image Files”), and the second string is the filter pattern (e.g., “*.png;*.jpg”). A collection of such filters is represented as a vector.

RaeptorCogs::FileDialogFilter filter = {"Image Files", "*.png;*.jpg;*.jpeg"};
using RaeptorCogs::FileDialogFilters = std::vector<FileDialogFilter>

File dialog filter collection.

Represents a vector of file dialog filters.

RaeptorCogs::FileDialogFilters filters = {
 {"Image Files", "*.png;*.jpg;*.jpeg"},
 {"Text Files", "*.txt;*.md"},
};

Functions

Functions

Function

Description

RaeptorCogs::LoadFile(const std::filesystem::path &filename)

Loads a file from disk into memory.

RaeptorCogs::LocalizeWorkingDirectory()

Sets the working directory to the executable’s directory.

RaeptorCogs::OpenFileDialog(FileCallback callback, const FileDialogFilters &filters={})

Opens a file dialog to select a file.

FileData RaeptorCogs::LoadFile(const std::filesystem::path &filename)

Loads a file from disk into memory.

Loads the specified file and returns its contents as a FileData object.

Note

The returned FileData is null-terminated, making it suitable for text files.

Parameters:

filename – The path to the file to load.

Returns:

The contents of the file as a FileData object.

bool RaeptorCogs::LocalizeWorkingDirectory()

Sets the working directory to the executable’s directory.

Sets the current working directory to the directory where the executable is located.

Returns:

true if the working directory was successfully set, false otherwise.

void RaeptorCogs::OpenFileDialog(FileCallback callback, const FileDialogFilters &filters = {})

Opens a file dialog to select a file.

Opens a file dialog allowing the user to select a file. Once a file is selected, the provided callback function is invoked with the file’s data and name.

Parameters:
  • callback – The function to call with the selected file’s data and name.

  • filters – Optional filters to apply to the file dialog.