utility 2026.1.9
A comprehensive C++ utilities library tailored for the development of modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
utility::File Class Reference

The File class represents a I/O file asset. More...

#include <headers/utility/system_io/file.hpp>

Public Types

enum class  Seek { Unknown = 0 , SET = 0 , CUR = 1 , END = 2 }
 The Seek enum represents the different seek modes for file operations. More...
 

Public Member Functions

 File (const std::string &path, const std::string &content)
 Constructs a File object with its content and size.
 
 File (const std::string &path, const std::vector< std::byte > &content)
 Constructs a File object with raw binary content.
 
 ~File ()
 Destructs the File object.
 
size_t write (const void *ptr, size_t size, size_t nmemb)
 Writes data to the file.
 
size_t read (void *ptr, size_t size, size_t count)
 Reads data from the file.
 
size_t read (std::string &str, size_t size, size_t count)
 Reads data from the file.
 
int seek (long offset, Seek whence)
 Seeks to a specific position in the file.
 
size_t tell () const
 Returns the current position in the file.
 
const std::vector< std::byte > & data () const
 Returns the binary content of the file.
 
std::string content () const
 Returns the content of the file as a string view.
 
const std::string & path () const
 Returns the path of the file.
 
size_t size () const
 Returns the size of the file content.
 
void clear ()
 Clears the content of the file.
 
size_t remove (size_t count)
 Removes a number of elements from the current position.
 

Protected Attributes

std::vector< std::byte > _content
 In-memory content of the asset.
 
std::string _path
 Path to the file.
 
size_t _pos = 0
 Current read/write cursor position in the content buffer.
 

Detailed Description

The File class represents a I/O file asset.

This class provides methods to read and write binary data to and from a file. Content is stored as raw bytes so binary assets (which may contain null bytes) are handled correctly.

Definition at line 47 of file file.hpp.

Member Enumeration Documentation

◆ Seek

enum class utility::File::Seek
strong

The Seek enum represents the different seek modes for file operations.

Enumerator
SET 

‍Unknown seek mode

CUR 

‍Seek from the beginning of the file

END 

‍Seek from the current position in the file

Definition at line 55 of file file.hpp.

Constructor & Destructor Documentation

◆ File() [1/2]

utility::File::File ( const std::string &  path,
const std::string &  content 
)

Constructs a File object with its content and size.

Parameters
pathThe path to the file.
contentThe content of the file.

Definition at line 30 of file file_asset.cpp.

Here is the call graph for this function:

◆ File() [2/2]

utility::File::File ( const std::string &  path,
const std::vector< std::byte > &  content 
)

Constructs a File object with raw binary content.

Parameters
pathThe path to the file.
contentThe binary content of the file.

Definition at line 39 of file file_asset.cpp.

Member Function Documentation

◆ clear()

void utility::File::clear ( )

Clears the content of the file.

This method clears the content of the file by resetting the _content member variable to an empty string and resetting the _pos member variable to 0. After calling this method, the file will be empty and the position will be reset to the beginning of the file. This method can be used to clear the content of the file before writing new data to it or to reset the file to an empty state.

Definition at line 139 of file file_asset.cpp.

◆ content()

std::string utility::File::content ( ) const
inline

Returns the content of the file as a string view.

Convenience accessor for text-oriented consumers. For binary assets prefer data().

Returns
The content of the file interpreted as bytes.

Definition at line 140 of file file.hpp.

Here is the caller graph for this function:

◆ data()

const std::vector< std::byte > & utility::File::data ( ) const
inline

Returns the binary content of the file.

Returns
The content of the file as raw bytes.

Definition at line 128 of file file.hpp.

◆ path()

const std::string & utility::File::path ( ) const
inline

Returns the path of the file.

This method returns the path of the file as a string. The path is stored in the _path member variable and can be accessed using this method.

Returns
The path of the file.

Definition at line 154 of file file.hpp.

◆ read() [1/2]

size_t utility::File::read ( std::string &  str,
size_t  size,
size_t  count 
)

Reads data from the file.

Parameters
strThe string to read data into.
sizeThe size of each element to read.
countThe number of elements to read.
Returns
The number of elements read.

Definition at line 89 of file file_asset.cpp.

◆ read() [2/2]

size_t utility::File::read ( void *  ptr,
size_t  size,
size_t  count 
)

Reads data from the file.

Parameters
ptrThe pointer to the buffer to read data into.
sizeThe size of each element to read.
countThe number of elements to read.
Returns
The number of elements read.

Definition at line 72 of file file_asset.cpp.

◆ remove()

size_t utility::File::remove ( size_t  count)

Removes a number of elements from the current position.

Parameters
countThe number of elements to remove.
Returns
The number of elements removed.

Definition at line 145 of file file_asset.cpp.

◆ seek()

int utility::File::seek ( long  offset,
Seek  whence 
)

Seeks to a specific position in the file.

Parameters
offsetThe offset to seek to.
whenceThe seek mode (SET, CUR, END).
Returns
0 on success, -1 on failure.

Definition at line 108 of file file_asset.cpp.

◆ size()

size_t utility::File::size ( ) const
inline

Returns the size of the file content.

This method returns the size of the file content by returning the size of the _content member variable. The size is returned as a size_t value.

Returns
The size of the file content.

Definition at line 168 of file file.hpp.

◆ tell()

size_t utility::File::tell ( ) const

Returns the current position in the file.

Returns
The current position in the file.

Definition at line 134 of file file_asset.cpp.

◆ write()

size_t utility::File::write ( const void *  ptr,
size_t  size,
size_t  nmemb 
)

Writes data to the file.

Parameters
ptrThe pointer to the data to write.
sizeThe size of each element to write.
nmembThe number of elements to write.
Returns
The number of elements written.

This method appends data at the seek position in the file.

Definition at line 48 of file file_asset.cpp.

Member Data Documentation

◆ _content

std::vector<std::byte> utility::File::_content
protected

In-memory content of the asset.

Definition at line 197 of file file.hpp.

◆ _path

std::string utility::File::_path
protected

Path to the file.

Definition at line 202 of file file.hpp.

◆ _pos

size_t utility::File::_pos = 0
protected

Current read/write cursor position in the content buffer.

Definition at line 207 of file file.hpp.


The documentation for this class was generated from the following files: