aboutsummaryrefslogtreecommitdiff
path: root/libbutl/fdstream.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2019-11-07 11:11:45 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2019-11-07 11:11:45 +0200
commit23d4deb111bfff9c282c20989573c4b0775e4c16 (patch)
tree9763c7e2cbce36581673eb69b652f67e2f7cfa6f /libbutl/fdstream.cxx
parentb2ac741e2b806f4565beeef34b12c95b6b3fdd41 (diff)
Add path_name struct, open_file_or{stdin,stdout}() functions
Diffstat (limited to 'libbutl/fdstream.cxx')
-rw-r--r--libbutl/fdstream.cxx39
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
//