aboutsummaryrefslogtreecommitdiff
path: root/libbutl/fdstream.ixx
diff options
context:
space:
mode:
Diffstat (limited to 'libbutl/fdstream.ixx')
-rw-r--r--libbutl/fdstream.ixx266
1 files changed, 266 insertions, 0 deletions
diff --git a/libbutl/fdstream.ixx b/libbutl/fdstream.ixx
new file mode 100644
index 0000000..a877699
--- /dev/null
+++ b/libbutl/fdstream.ixx
@@ -0,0 +1,266 @@
+// file : libbutl/fdstream.ixx -*- C++ -*-
+// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
+// license : MIT; see accompanying LICENSE file
+
+#include <cassert>
+
+namespace butl
+{
+ // auto_fd
+ //
+ inline void auto_fd::
+ reset (int fd) noexcept
+ {
+ if (fd_ >= 0)
+ fdclose (fd_); // Don't check for an error as not much we can do here.
+
+ fd_ = fd;
+ }
+
+ inline auto_fd& auto_fd::
+ operator= (auto_fd&& fd) noexcept
+ {
+ reset (fd.release ());
+ return *this;
+ }
+
+ inline auto_fd::
+ ~auto_fd () noexcept
+ {
+ reset ();
+ }
+
+ // fdbuf
+ //
+ inline auto_fd fdbuf::
+ release ()
+ {
+ return std::move (fd_);
+ }
+
+ // ifdstream
+ //
+ inline ifdstream::
+ ifdstream (auto_fd&& fd, iostate e)
+ : fdstream_base (std::move (fd)), std::istream (&buf_)
+ {
+ assert (e & badbit);
+ exceptions (e);
+ }
+
+ inline ifdstream::
+ ifdstream (iostate e)
+ : ifdstream (auto_fd (), e) // Delegate.
+ {
+ }
+
+ inline ifdstream::
+ ifdstream (auto_fd&& fd, fdstream_mode m, iostate e)
+ : fdstream_base (std::move (fd), m),
+ std::istream (&buf_),
+ skip_ ((m & fdstream_mode::skip) == fdstream_mode::skip)
+ {
+ assert (e & badbit);
+ exceptions (e);
+ }
+
+ inline ifdstream::
+ ifdstream (const std::string& f, openmode m, iostate e)
+ : ifdstream (f.c_str (), m, e) // Delegate.
+ {
+ }
+
+ inline ifdstream::
+ ifdstream (const path& f, openmode m, iostate e)
+ : ifdstream (f.string (), m, e) // Delegate.
+ {
+ }
+
+ inline ifdstream::
+ ifdstream (const std::string& f, fdopen_mode m, iostate e)
+ : ifdstream (f.c_str (), m, e) // Delegate.
+ {
+ }
+
+ inline ifdstream::
+ ifdstream (const path& f, fdopen_mode m, iostate e)
+ : ifdstream (f.string (), m, e) // Delegate.
+ {
+ }
+
+ inline void ifdstream::
+ open (const std::string& f, openmode m)
+ {
+ open (f.c_str (), m);
+ }
+
+ inline void ifdstream::
+ open (const path& f, openmode m)
+ {
+ open (f.string (), m);
+ }
+
+ inline void ifdstream::
+ open (const std::string& f, fdopen_mode m)
+ {
+ open (f.c_str (), m);
+ }
+
+ inline void ifdstream::
+ open (const path& f, fdopen_mode m)
+ {
+ open (f.string (), m);
+ }
+
+ inline auto_fd ifdstream::
+ release ()
+ {
+ return buf_.release ();
+ }
+
+ // ofdstream
+ //
+ inline ofdstream::
+ ofdstream (auto_fd&& fd, iostate e)
+ : fdstream_base (std::move (fd)), std::ostream (&buf_)
+ {
+ assert (e & badbit);
+ exceptions (e);
+ }
+
+ inline ofdstream::
+ ofdstream (iostate e)
+ : ofdstream (auto_fd (), e) // Delegate.
+ {
+ }
+
+ inline ofdstream::
+ ofdstream (auto_fd&& fd, fdstream_mode m, iostate e)
+ : fdstream_base (std::move (fd), m), std::ostream (&buf_)
+ {
+ assert (e & badbit);
+ exceptions (e);
+ }
+
+ inline ofdstream::
+ ofdstream (const std::string& f, openmode m, iostate e)
+ : ofdstream (f.c_str (), m, e) // Delegate.
+ {
+ }
+
+ inline ofdstream::
+ ofdstream (const path& f, openmode m, iostate e)
+ : ofdstream (f.string (), m, e) // Delegate.
+ {
+ }
+
+ inline ofdstream::
+ ofdstream (const std::string& f, fdopen_mode m, iostate e)
+ : ofdstream (f.c_str (), m, e) // Delegate.
+ {
+ }
+
+ inline ofdstream::
+ ofdstream (const path& f, fdopen_mode m, iostate e)
+ : ofdstream (f.string (), m, e) // Delegate.
+ {
+ }
+
+ inline void ofdstream::
+ open (const std::string& f, openmode m)
+ {
+ open (f.c_str (), m);
+ }
+
+ inline void ofdstream::
+ open (const path& f, openmode m)
+ {
+ open (f.string (), m);
+ }
+
+ inline void ofdstream::
+ open (const std::string& f, fdopen_mode m)
+ {
+ open (f.c_str (), m);
+ }
+
+ inline void ofdstream::
+ open (const path& f, fdopen_mode m)
+ {
+ open (f.string (), m);
+ }
+
+ inline auto_fd ofdstream::
+ release ()
+ {
+ if (is_open ())
+ flush ();
+
+ return buf_.release ();
+ }
+
+ // fdopen()
+ //
+ inline auto_fd
+ fdopen (const std::string& f, fdopen_mode m, permissions p)
+ {
+ return fdopen (f.c_str (), m, p);
+ }
+
+ inline auto_fd
+ fdopen (const path& f, fdopen_mode m, permissions p)
+ {
+ return fdopen (f.string (), m, p);
+ }
+
+ // fdstream_mode
+ //
+ inline fdstream_mode
+ operator& (fdstream_mode x, fdstream_mode y)
+ {
+ return x &= y;
+ }
+
+ inline fdstream_mode
+ operator| (fdstream_mode x, fdstream_mode y)
+ {
+ return x |= y;
+ }
+
+ inline fdstream_mode
+ operator&= (fdstream_mode& x, fdstream_mode y)
+ {
+ return x = static_cast<fdstream_mode> (
+ static_cast<std::uint16_t> (x) &
+ static_cast<std::uint16_t> (y));
+ }
+
+ inline fdstream_mode
+ operator|= (fdstream_mode& x, fdstream_mode y)
+ {
+ return x = static_cast<fdstream_mode> (
+ static_cast<std::uint16_t> (x) |
+ static_cast<std::uint16_t> (y));
+ }
+
+ // fdopen_mode
+ //
+ inline fdopen_mode operator& (fdopen_mode x, fdopen_mode y) {return x &= y;}
+ inline fdopen_mode operator| (fdopen_mode x, fdopen_mode y) {return x |= y;}
+
+ inline fdopen_mode
+ operator&= (fdopen_mode& x, fdopen_mode y)
+ {
+ return x = static_cast<fdopen_mode> (
+ static_cast<std::uint16_t> (x) &
+ static_cast<std::uint16_t> (y));
+ }
+
+ inline fdopen_mode
+ operator|= (fdopen_mode& x, fdopen_mode y)
+ {
+ return x = static_cast<fdopen_mode> (
+ static_cast<std::uint16_t> (x) |
+ static_cast<std::uint16_t> (y));
+ }
+}