From 23d4deb111bfff9c282c20989573c4b0775e4c16 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 7 Nov 2019 11:11:45 +0200 Subject: Add path_name struct, open_file_or{stdin,stdout}() functions --- libbutl/fdstream.cxx | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'libbutl/fdstream.cxx') 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 // bad_alloc #include // numeric_limits #include // memcpy(), memmove() +#include // cin, cout #include // uncaught_exception[s]() #include // invalid_argument #include @@ -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 = ""; + 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 = ""; + return cout; + } + } + // fd*() functions // -- cgit v1.1