Add a simple write string to file method

This commit is contained in:
Danny Robson 2012-08-15 16:03:48 +10:00
parent 5e764244ac
commit b17f7691a1
2 changed files with 24 additions and 1 deletions

22
io.cpp
View File

@ -58,7 +58,7 @@ util::slurp (const boost::filesystem::path& path) {
char *cursor = buffer.get(); char *cursor = buffer.get();
while (remaining) { while (remaining) {
ssize_t consumed = read (fd, cursor, remaining); ssize_t consumed = ::read (fd, cursor, remaining);
if (consumed == -1) if (consumed == -1)
throw errno_error(); throw errno_error();
CHECK_HARD ( consumed > 0); CHECK_HARD ( consumed > 0);
@ -71,6 +71,26 @@ util::slurp (const boost::filesystem::path& path) {
return buffer; 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<size_t> (consumed);
cursor += sign_cast<size_t> (consumed);
}
}
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
fd_ref::fd_ref (int _fd): fd_ref::fd_ref (int _fd):
fd (_fd) fd (_fd)

3
io.hpp
View File

@ -44,6 +44,9 @@ namespace util {
std::unique_ptr<char []> std::unique_ptr<char []>
slurp (const boost::filesystem::path&) mustuse; 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 /// A simple RAII wrapper for file descriptors
struct fd_ref { struct fd_ref {
public: public: