From 40dd54a5014571e30fddaaae9e207c1d429b54fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Radovan=20=C4=8Cerven=C3=BD?= <radovan.cerveny@gmail.com> Date: Sun, 14 Feb 2016 20:11:01 +0100 Subject: [PATCH] changed fdstream documentation, added FAIL_FD constant, fallback file descriptor is now optional --- alib2std/src/extensions/fdstream.cpp | 4 +++- alib2std/src/extensions/fdstream.hpp | 10 +++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/alib2std/src/extensions/fdstream.cpp b/alib2std/src/extensions/fdstream.cpp index b9796a6e21..dee32ac7bb 100644 --- a/alib2std/src/extensions/fdstream.cpp +++ b/alib2std/src/extensions/fdstream.cpp @@ -6,6 +6,8 @@ namespace std { +const int FAIL_FD = -1; + fdstreambuf::fdstreambuf ( int fd ) : fd ( fd ) { setp ( buff.begin ( ), buff.end ( ) - 1 ); } @@ -61,7 +63,7 @@ bool fdaccessor::is_redirected ( ) const { return redirected; } -ofdstream::ofdstream ( int fd, int fallback_fd ) : fda ( fd, fallback_fd ), fdbuf ( fda.get_fd ( ) ) { +ofdstream::ofdstream ( int fd, int fallback_fd = FAIL_FD) : fda ( fd, fallback_fd ), fdbuf ( fda.get_fd ( ) ) { this->init ( & fdbuf ); if ( fda.get_fd ( ) == -1 ) diff --git a/alib2std/src/extensions/fdstream.hpp b/alib2std/src/extensions/fdstream.hpp index 17ced57968..d36abc7090 100644 --- a/alib2std/src/extensions/fdstream.hpp +++ b/alib2std/src/extensions/fdstream.hpp @@ -4,12 +4,14 @@ /* * ofdstream - * ofdstream takes a file descriptor (fd) by constructor to use for output - * if the file descriptor is not valid for any reason during construction of the ofdstream, ofdstream will set its state to ios::fail - * if the file descriptor is valid, it is used for successive buffered output + * ofdstream takes a primary file descriptor (fd) and fallback file descriptor by constructor to use for output + * if the primary fd is not valid for any reason, ofdstream will try to use fallback fd, if even fallback fd is not valid, ofdstream sets its state to ios::fail + * if the primary fd (or fallback fd) is valid , it is used for successive buffered output * * supplied file descriptor has to be already open in order for ofdstream to work * ofdstream does not close the file descriptor and does not change internal flags of the file descriptor during the lifetime of ofdstream and during destruction of ofdstream + * + * typical use is to use as primary fd some arbitrary fd (5) and for fallback fd some standard fd (2) if we wish to see the output */ #ifndef FDSTREAM_HPP_ @@ -22,6 +24,8 @@ namespace std { +extern const int FAIL_FD; + class fdstreambuf : public streambuf { static const size_t buff_sz = 512; -- GitLab