libos: Add File::seek() and File::tell()
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
cea1b030ff
commit
4d5feb0f3b
@ -37,6 +37,13 @@ namespace os
|
|||||||
ReadAppend = O_RDWR | O_APPEND, // Open the file read-write, and append to it.
|
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.
|
* @brief Create a new File object from a file path, opening the file.
|
||||||
*
|
*
|
||||||
@ -191,6 +198,22 @@ namespace os
|
|||||||
*/
|
*/
|
||||||
void rewind();
|
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.
|
* @brief Buffering modes for a File.
|
||||||
*/
|
*/
|
||||||
|
@ -227,6 +227,20 @@ namespace os
|
|||||||
fflush(m_file);
|
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)
|
void File::set_buffer(BufferingMode mode)
|
||||||
{
|
{
|
||||||
setvbuf(m_file, NULL, mode, 0);
|
setvbuf(m_file, NULL, mode, 0);
|
||||||
|
Loading…
Reference in New Issue
Block a user