File#
Fully qualified name: carb::filesystem::File
Defined in carb/filesystem/detail/IFileSystem2.h
-
class File#
Class representing an open file.
Public Functions
-
virtual void release() noexcept = 0#
Closes and destroys *this.
Call this function when you are done with
*this.- Post:
*this is destroyed and must not be accessed anymore
-
virtual omni::expected<void, ErrorCode> close() noexcept = 0#
Closes the File.
Any further operation on the file will result in an error result.
- Possible Error values
carb::ErrorCode
InvalidOperationThe file is already closedOther errors based on underlying platform calls.
- Returns:
voidif successful, or a carb::ErrorCode indicating the outcome of the request.
-
virtual omni::expected<size_t, ErrorCode> getSize() noexcept = 0#
Gets the total size of the file.
- Possible Error values
carb::ErrorCode
InvalidOperationThe file is already closedOther errors based on underlying platform calls.
Note
Writable files will execute a flush() as part of this request.
- Returns:
A
size_tindicating the total size of the file in bytes if successful, or a carb::ErrorCode if an error occurs.
-
virtual omni::expected<FileTime, ErrorCode> getModTime() noexcept = 0#
Gets the time of last modification to the file.
Note
Writable files will execute a flush() as part of this request.
- Returns:
A FileTime indicating the time this file was last modified if successful, or an carb::ErrorCode if an error occurs.
- virtual omni::expected<FileTime, ErrorCode> getCreateTime(
Gets the time of creation of the file.
- Returns:
A FileTime indicating the time this file was created if successful, or a carb::ErrorCode if an error occurs.
- virtual omni::expected<size_t, ErrorCode> readChunk(
- void *buffer,
- size_t bytes,
Reads a chunk of binary data from the file.
- Parameters:
buffer – A memory buffer to read the binary data into, must be at least
bytesin size.bytes – Number of bytes to read from the file into
buffer.
- Returns:
A
size_tcorresponding to the number of bytes read if successful, or a carb::ErrorCode if an error occurs.
- virtual omni::expected<size_t, ErrorCode> writeChunk(
- const void *buffer,
- size_t bytes,
Writes a chunk of binary data to the file.
- Parameters:
buffer – A memory buffer to write to the file.
bytes – The number of bytes from
bufferto write to the file.
- Returns:
A
size_tcorresponding to the number of bytes successfully written to the file, or an carb::ErrorCode if an error occurs. Note that the number of bytes written may be less than requested due to various reasons (i.e. disk full).
- virtual omni::expected<omni::string, ErrorCode> readLine(
Reads a line of character data from a text file (including the
\ror\nline-ending character(s) if any).- Possible Error values
carb::ErrorCode
EndOfFile- File is at EOF (end of file)carb::ErrorCode
Fail- A generic error was reported while readingOther errors may be reported based on the underlying platform calls
Note
This function considers a solo
\nto be a line ending, as well as\r\n. A\rby itself is not considered a line ending.Warning
This function includes the line endings, unlike the version 1.x API.
- Returns:
An
omni::stringcontaining the line read from the file, if successful. The string may be empty if an empty line was read. The string may have line-ending characters at the end. If an error or end-of-file event occurs, a carb::ErrorCode is returned instead.
- virtual omni::expected<void, ErrorCode> readLine( ) noexcept = 0#
Reads a line of character data from a text file (including the
\ror\nline-ending character(s) if any).- Possible Error values
carb::ErrorCode
EndOfFile- File is at EOF (end of file)carb::ErrorCode
Fail- A generic error was reported while readingOther errors may be reported based on the underlying platform calls
Note
This function considers a solo
\nto be a line ending, as well as\r\n. A\rby itself is not considered a line ending.Note
This variation of
readLineallows reading into an existing omni::string without always allocating a new one.Warning
This function includes the line endings, unlike the version 1.x API.
- Parameters:
out – An omni::string that will receive the line read from the file, overwriting the previous string. If an error occurs, this string is in a valid but undefined state. The string may have line-ending characters at the end. The line may be empty if read at the end of the file.
- Returns:
voidif a line was read. If an error or end-of-file event occurs, a carb::ErrorCode is returned instead.
- virtual omni::expected<void, ErrorCode> writeLine(
- cpp::string_view line,
Writes a line to the file.
- Parameters:
line – The string data to write to the file. A
\nwill be appended to the string written to the file. If the file was opened in non-binary mode on Windows, a\r\nwill be written instead.- Returns:
voidif a line was written. If an error occurs a carb::ErrorCode is returned instead.
-
virtual omni::expected<void, ErrorCode> flush() noexcept = 0#
Flushes any unwritten data to the file.
- Returns:
voidif the flush was successful, or a carb::ErrorCode representing the failure.
- virtual omni::expected<size_t, ErrorCode> getPosition(
Retrieves the current file pointer position.
Similar to
tell().Note
The offset returned may be beyond the end of the existing file if bytes have not been written to the file or it has not been flushed.
- Returns:
A
size_trepresenting the file pointer position in bytes from the beginning of the file, or an error indicating why the file pointer could not be read.
- virtual omni::expected<void, ErrorCode> setPosition(
- int64_t offset,
- FileWhence whence,
Sets the new file pointer position.
- Parameters:
offset – the new position for the file pointer relative to the location specified by
whence. This value may only be negative ifwhenceis notFileWhence::eBegin, but the absolute position of the file may not be negative. This may specify a position beyond the current end of the file, but the file is not changed until data is written at that offset and flushed.whence – the relative location to base
offsetfrom.
- Returns:
voidif set successfully, or a carb::ErrorCode indicating why the file pointer could not be set.
-
virtual omni::expected<void, ErrorCode> truncate() noexcept = 0#
Truncates the file at the current position.
The file position is set with setPosition. If the position is beyond the current end of the file, the file is filled with zero bytes. If the file is being shortened, all data in the file at and beyond the current file pointer will be removed.
- Returns:
voidif the truncation was successful, or a carb::ErrorCode indicating why the operation could not be completed if an error occurs.
-
virtual FileStatus getStatus() const noexcept = 0#
Retrieves the file status: whether at EOF, an error occurred, or no error state is recorded.
- Returns:
A FileStatus indicating the current status of the file.
-
inline bool isEof() const noexcept#
Returns whether the file position pointer is at the end-of-file position.
Similar to C’s
feof()function. Same asgetStatus() == FileStatus::eEof.- Returns:
trueif the file pointer is at end-of-file position.falseotherwise.
-
inline bool isError() const noexcept#
Returns whether the file is in error state.
Similar to C’s
ferror()function. Same asgetStatus() == FileStatus::eError.- Returns:
trueif the file is in error state.falseotherwise.
- virtual omni::expected<void, ErrorCode> getFileInfo(
- FileInfo &info,
Fills a FileInfo structure with information about the file.
- Parameters:
info – A reference to a structure to fill with information about the file. If an error occurs this struct is in a valid but undefined state.
- Returns:
voidif file info was successfully read, otherwise a carb::ErrorCode value indicating why the operation could not be completed (infois in a valid but undefined state).
- inline omni::expected<void, ErrorCode> resetPositionBegin(
Helper function to move the file position pointer to the beginning of the file.
Effectively
return setPosition(0, FileWhence::eBegin);. See setPosition for more information.- Returns:
The result from setPosition.
-
inline omni::expected<void, ErrorCode> resetPositionEnd() noexcept#
Helper function to move the file position pointer to the end of the file.
Effectively
return setPosition(0, FileWhence::eEnd);. See setPosition for more information.- Returns:
The result from setPosition.
-
virtual void release() noexcept = 0#