io: expose file-object variant of draining write

This commit is contained in:
Danny Robson 2017-12-26 17:24:42 +11:00
parent ce64396da4
commit ac685cfdd2

27
io.hpp
View File

@ -43,9 +43,11 @@ namespace util {
std::vector<T> slurp (FILE *); std::vector<T> slurp (FILE *);
//------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////
void write (const posix::fd&, const void *restrict data, size_t bytes); void write (const posix::fd&, const void *restrict data, size_t bytes);
//-------------------------------------------------------------------------
inline void inline void
write (const posix::fd &dst, util::view<const char*> data) write (const posix::fd &dst, util::view<const char*> data)
{ {
@ -53,6 +55,7 @@ namespace util {
} }
//-------------------------------------------------------------------------
template <typename T> template <typename T>
void write (const posix::fd &_fd, const T &data) void write (const posix::fd &_fd, const T &data)
{ {
@ -60,7 +63,27 @@ namespace util {
} }
//------------------------------------------------------------------------- ///------------------------------------------------------------------------
/// writes all data from the provided view into the file-like-object
///
/// writing will continually iterate until the entire buffer has been
/// dispatched so as to avoid issues with partial writes. will block until
/// such time as the entire buffer has written.
///
/// an exception may be thrown in the event forward progress is impossible.
/// in this event the progress may not be reported to the caller. in the
/// future an exception member variable may expose the information.
template <typename DstT, typename IteratorA, typename IteratorB>
void
write (DstT &dst, util::view<IteratorA, IteratorB> src)
{
for (auto cursor = src.begin (); cursor != src.end (); ) {
cursor = dst.write (view{cursor, src.end ()});
}
}
///////////////////////////////////////////////////////////////////////////
class indenter : public std::streambuf { class indenter : public std::streambuf {
protected: protected:
std::streambuf* m_dest; std::streambuf* m_dest;