From b17793134e5e3f0663de29a8315fd3dc2701c3d4 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 2 Aug 2023 11:54:47 +0200 Subject: [PATCH] libos: Let users change the buffering mode of a File --- libos/include/os/File.h | 27 +++++++++++++++++++++++++++ libos/src/File.cpp | 5 +++++ 2 files changed, 32 insertions(+) diff --git a/libos/include/os/File.h b/libos/include/os/File.h index a017d6b3..ac27f165 100644 --- a/libos/include/os/File.h +++ b/libos/include/os/File.h @@ -143,6 +143,19 @@ namespace os */ Result 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 Whether the operation succeeded. + */ + template Result 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(); diff --git a/libos/src/File.cpp b/libos/src/File.cpp index f7a9448d..4bee3fe1 100644 --- a/libos/src/File.cpp +++ b/libos/src/File.cpp @@ -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 print_impl(SharedPtr f, StringView fmt, va_list ap) {