libos: Add File::seek() and File::tell()
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-10-24 20:02:09 +02:00
parent cea1b030ff
commit 4d5feb0f3b
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 37 additions and 0 deletions

View File

@ -37,6 +37,13 @@ namespace os
ReadAppend = O_RDWR | O_APPEND, // Open the file read-write, and append to it.
};
enum class SeekMode
{
Beginning = SEEK_SET, // Seek from the beginning of the file.
Current = SEEK_CUR, // Seek from the current file position.
End = SEEK_END, // Seek from the end of the file.
};
/**
* @brief Create a new File object from a file path, opening the file.
*
@ -191,6 +198,22 @@ namespace os
*/
void rewind();
/**
* @brief Change the file pointer's position.
*
* @param offset The number of bytes to move the file pointer (can be negative, to move backwards).
* @param mode Where to seek from.
* @return Result<void> Whether the operation succeeded.
*/
Result<void> seek(off_t offset, SeekMode mode);
/**
* @brief Read the file pointer's current position.
*
* @return Result<off_t> An error, or the file pointer's position.
*/
Result<off_t> tell();
/**
* @brief Buffering modes for a File.
*/

View File

@ -227,6 +227,20 @@ namespace os
fflush(m_file);
}
Result<void> File::seek(off_t offset, SeekMode mode)
{
int rc = fseek(m_file, offset, (int)mode);
if (rc < 0) return err(errno);
return {};
}
Result<off_t> File::tell()
{
off_t off = ftell(m_file);
if (off < 0) return err(errno);
return off;
}
void File::set_buffer(BufferingMode mode)
{
setvbuf(m_file, NULL, mode, 0);