diff --git a/io.cpp b/io.cpp index 383d12c5..96d2ece1 100644 --- a/io.cpp +++ b/io.cpp @@ -58,7 +58,7 @@ util::slurp (const boost::filesystem::path& path) { char *cursor = buffer.get(); while (remaining) { - ssize_t consumed = read (fd, cursor, remaining); + ssize_t consumed = ::read (fd, cursor, remaining); if (consumed == -1) throw errno_error(); CHECK_HARD ( consumed > 0); @@ -71,6 +71,26 @@ util::slurp (const boost::filesystem::path& path) { return buffer; } +//---------------------------------------------------------------------------- +void +util::write (const boost::filesystem::path &path, const char *data, size_t len) { + CHECK_SOFT (len > 0); + CHECK_HARD (data); + + fd_ref fd (path); + const char *cursor = data; + size_t remaining = len; + + while (remaining) { + ssize_t consumed = ::write (fd, cursor, remaining); + if (consumed < 0) + errno_error::throw_code (); + + remaining -= sign_cast (consumed); + cursor += sign_cast (consumed); + } +} + //---------------------------------------------------------------------------- fd_ref::fd_ref (int _fd): fd (_fd) diff --git a/io.hpp b/io.hpp index 42e82fac..24c797f0 100644 --- a/io.hpp +++ b/io.hpp @@ -44,6 +44,9 @@ namespace util { std::unique_ptr slurp (const boost::filesystem::path&) mustuse; + void + write (const boost::filesystem::path &, const char *data, size_t len); + /// A simple RAII wrapper for file descriptors struct fd_ref { public: