From c54ff48ac3abb62a40eb1a438da8e3e7ef139797 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Sat, 28 Jun 2025 20:40:25 +0100 Subject: posix: add tempfile() --- nihil.posix/posix.read_file.ccm | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 nihil.posix/posix.read_file.ccm (limited to 'nihil.posix/posix.read_file.ccm') diff --git a/nihil.posix/posix.read_file.ccm b/nihil.posix/posix.read_file.ccm new file mode 100644 index 0000000..c950f67 --- /dev/null +++ b/nihil.posix/posix.read_file.ccm @@ -0,0 +1,48 @@ +/* + * This source code is released into the public domain. + */ + +module; + +#include +#include +#include +#include +#include +#include + +#include +#include + +export module nihil.posix:read_file; + +import nihil.error; +import nihil.monad; +import :fd; +import :open; + +namespace nihil { + +/* + * Read the contents of a file into an output iterator. + */ +export [[nodiscard]] auto +read_file(std::filesystem::path const &filename, + std::output_iterator auto &&iter) + -> std::expected +{ + auto file = co_await open(filename, O_RDONLY); + + auto constexpr bufsize = std::size_t{1024}; + auto buffer = std::array{}; + + for (;;) { + auto read_buf = co_await(read(file, buffer)); + if (read_buf.empty()) + co_return {}; + + std::ranges::copy(read_buf, iter); + } +} + +} // namespace nihil -- cgit v1.2.3