diff options
Diffstat (limited to 'libbutl/fdstream.cxx')
-rw-r--r-- | libbutl/fdstream.cxx | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/libbutl/fdstream.cxx b/libbutl/fdstream.cxx index 50056a2..de91724 100644 --- a/libbutl/fdstream.cxx +++ b/libbutl/fdstream.cxx @@ -48,6 +48,7 @@ #include <new> // bad_alloc #include <limits> // numeric_limits #include <cstring> // memcpy(), memmove() +#include <iostream> // cin, cout #include <exception> // uncaught_exception[s]() #include <stdexcept> // invalid_argument #include <system_error> @@ -946,6 +947,44 @@ namespace butl : m | translate_mode (out))); } + istream& + open_file_or_stdin (path_name& pn, ifdstream& ifs) + { + assert (pn.path != nullptr); + + if (pn.path->string () != "-") + { + ifs.open (*pn.path); + return ifs; + } + else + { + cin.exceptions (ifs.exceptions ()); + if (!pn.name) + pn.name = "<stdin>"; + return cin; + } + } + + ostream& + open_file_or_stdout (path_name& pn, ofdstream& ofs) + { + assert (pn.path != nullptr); + + if (pn.path->string () != "-") + { + ofs.open (*pn.path); + return ofs; + } + else + { + cout.exceptions (ofs.exceptions ()); + if (!pn.name) + pn.name = "<stdout>"; + return cout; + } + } + // fd*() functions // |