libos: Let users change the buffering mode of a File

This commit is contained in:
apio 2023-08-02 11:54:47 +02:00
parent dc35c42371
commit b17793134e
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 32 additions and 0 deletions

View File

@ -143,6 +143,19 @@ namespace os
*/
Result<void> read(Buffer& buf, usize size);
/**
* @brief Read an object from this File.
*
* @tparam T The type of the object to read.
* @param object A reference to the object in question.
* @return Result<void> Whether the operation succeeded.
*/
template <typename T> Result<void> read_typed(T& object)
{
TRY(raw_read((u8*)&object, sizeof(T)));
return {};
}
/**
* @brief Read the entire File's contents.
*
@ -178,6 +191,20 @@ namespace os
*/
void rewind();
enum BufferingMode
{
NotBuffered = _IONBF,
LineBuffered = _IOLBF,
FullyBuffered = _IOFBF,
};
/**
* @brief Change the buffering mode of this File.
*
* @param mode The buffering mode.
*/
void set_buffer(BufferingMode mode);
File(Badge<File>);
~File();

View File

@ -226,6 +226,11 @@ namespace os
fflush(m_file);
}
void File::set_buffer(BufferingMode mode)
{
setvbuf(m_file, NULL, mode, 0);
}
// FIXME: Do not allocate memory for printing.
Result<void> print_impl(SharedPtr<File> f, StringView fmt, va_list ap)
{