diff --git a/alib2std/src/extensions/fdstream.cpp b/alib2std/src/extensions/fdstream.cpp index b9796a6e21830bd1b06e79fd1e1594612ea28969..dee32ac7bb0a342e42f8bec0b7236664ec548285 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 17ced5796869a3bd758b071a8d4bc36766753a94..d36abc7090e145ad193f9ac83c2aa518e01fc72f 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;