aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2017-07-24 14:07:40 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2017-07-24 14:07:40 +0200
commit21acf9701d5f26ccc8c76775b0a3e1616e3b4ddd (patch)
tree9c9d821ea45412891f7716d7998eeae84d2a877e
parent9efe8d1ed5ee210ae644b7118f1d674a2de5f3e8 (diff)
Add std*_fd() functions
-rw-r--r--libbutl/fdstream.cxx36
-rw-r--r--libbutl/fdstream.hxx10
-rw-r--r--libbutl/fdstream.ixx20
3 files changed, 48 insertions, 18 deletions
diff --git a/libbutl/fdstream.cxx b/libbutl/fdstream.cxx
index daa344f..7681c88 100644
--- a/libbutl/fdstream.cxx
+++ b/libbutl/fdstream.cxx
@@ -881,22 +881,22 @@ namespace butl
: fdstream_mode::blocking);
}
- fdstream_mode
- stdin_fdmode (fdstream_mode m)
+ int
+ stdin_fd ()
{
- return fdmode (STDIN_FILENO, m);
+ return STDIN_FILENO;
}
- fdstream_mode
- stdout_fdmode (fdstream_mode m)
+ int
+ stdout_fd ()
{
- return fdmode (STDOUT_FILENO, m);
+ return STDOUT_FILENO;
}
- fdstream_mode
- stderr_fdmode (fdstream_mode m)
+ int
+ stderr_fd ()
{
- return fdmode (STDERR_FILENO, m);
+ return STDERR_FILENO;
}
fdpipe
@@ -1064,34 +1064,34 @@ namespace butl
: fdstream_mode::text);
}
- fdstream_mode
- stdin_fdmode (fdstream_mode m)
+ int
+ stdin_fd ()
{
int fd (_fileno (stdin));
if (fd == -1)
throw_ios_failure (errno);
- return fdmode (fd, m);
+ return fd;
}
- fdstream_mode
- stdout_fdmode (fdstream_mode m)
+ int
+ stdout_fd ()
{
int fd (_fileno (stdout));
if (fd == -1)
throw_ios_failure (errno);
- return fdmode (fd, m);
+ return fd;
}
- fdstream_mode
- stderr_fdmode (fdstream_mode m)
+ int
+ stderr_fd ()
{
int fd (_fileno (stderr));
if (fd == -1)
throw_ios_failure (errno);
- return fdmode (fd, m);
+ return fd;
}
fdpipe
diff --git a/libbutl/fdstream.hxx b/libbutl/fdstream.hxx
index 752f8ab..6673fed 100644
--- a/libbutl/fdstream.hxx
+++ b/libbutl/fdstream.hxx
@@ -595,6 +595,16 @@ namespace butl
LIBBUTL_SYMEXPORT fdstream_mode
fdmode (int, fdstream_mode);
+ // Portable functions for obtaining file descriptors of standard streams.
+ // Note that you normally wouldn't want to close them using fddup() to
+ // convert them to auto_fd, for example:
+ //
+ // ifdstream is (fddup (stdin_fd ()));
+ //
+ LIBBUTL_SYMEXPORT int stdin_fd ();
+ LIBBUTL_SYMEXPORT int stdout_fd ();
+ LIBBUTL_SYMEXPORT int stderr_fd ();
+
// Convenience functions for setting the translation mode for standard
// streams.
//
diff --git a/libbutl/fdstream.ixx b/libbutl/fdstream.ixx
index 5181903..212ec98 100644
--- a/libbutl/fdstream.ixx
+++ b/libbutl/fdstream.ixx
@@ -286,4 +286,24 @@ namespace butl
static_cast<std::uint16_t> (x) |
static_cast<std::uint16_t> (y));
}
+
+ // std*_fdmode()
+ //
+ inline fdstream_mode
+ stdin_fdmode (fdstream_mode m)
+ {
+ return fdmode (stdin_fd (), m);
+ }
+
+ inline fdstream_mode
+ stdout_fdmode (fdstream_mode m)
+ {
+ return fdmode (stdout_fd (), m);
+ }
+
+ inline fdstream_mode
+ stderr_fdmode (fdstream_mode m)
+ {
+ return fdmode (stderr_fd (), m);
+ }
}