// This source code is released into the public domain. export module nihil.posix:read_file; import nihil.error; import nihil.util; 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, open_read); 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