2004-08-25 08:22:19 +00:00
|
|
|
/** @file
|
|
|
|
|
|
|
|
$Log$
|
2004-08-25 08:35:09 +00:00
|
|
|
Revision 1.4 2004/08/25 08:35:09 marc
|
|
|
|
change in file header
|
|
|
|
|
2004-08-25 08:22:19 +00:00
|
|
|
Revision 1.3 2004/08/25 08:22:19 marc
|
|
|
|
added file header
|
|
|
|
|
|
|
|
|
2004-08-25 08:35:09 +00:00
|
|
|
@copy © Copyright Marc Wäckerlin $Date$
|
|
|
|
@author Last Change: $Author$
|
|
|
|
@date Last Change: $Date$
|
|
|
|
@version $Id$
|
|
|
|
@licence see file COPYING or license.html
|
2004-08-25 08:22:19 +00:00
|
|
|
*/
|
2004-04-21 06:39:20 +00:00
|
|
|
#ifndef __MRW_UNISTD_HPP__
|
|
|
|
#define __MRW_UNISTD_HPP__
|
|
|
|
|
|
|
|
#include <unistd.h> // pipe, close
|
|
|
|
#include <errno.h> // errno
|
|
|
|
|
|
|
|
namespace mrw {
|
|
|
|
/** @addtogroup AutoTools */
|
|
|
|
//@{
|
|
|
|
|
2004-04-23 16:03:29 +00:00
|
|
|
/** @brief class that implements an unnamed UNIX pipe
|
|
|
|
@pre #include <mrw/unistd.hpp>
|
|
|
|
|
|
|
|
Implements a UNIX pipe that is automatically closed in
|
2004-04-21 06:39:20 +00:00
|
|
|
destructor and offers some facilities. */
|
2004-08-25 08:22:19 +00:00
|
|
|
class Pipe {
|
2004-04-21 06:39:20 +00:00
|
|
|
private:
|
|
|
|
/// the filedescriptor, [0] to read and [1] to write
|
|
|
|
int _fd[2];
|
|
|
|
int _lastError;
|
|
|
|
public:
|
|
|
|
/// creates a unix pipe
|
2004-08-25 08:22:19 +00:00
|
|
|
Pipe(): _lastError(-1) {
|
2004-04-21 06:39:20 +00:00
|
|
|
_fd[0] = -1;
|
|
|
|
_fd[1] = -1;
|
|
|
|
if (::pipe(_fd)==-1)
|
|
|
|
{
|
|
|
|
_lastError=errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// destructor closes pipe if still open
|
2004-08-25 08:22:19 +00:00
|
|
|
~Pipe() {
|
2004-04-21 06:39:20 +00:00
|
|
|
close();
|
|
|
|
}
|
|
|
|
/// closes pipe if open
|
|
|
|
void close() {
|
|
|
|
close_in();
|
|
|
|
close_out();
|
|
|
|
}
|
|
|
|
/// closes input pipe if open
|
|
|
|
void close_in() {
|
|
|
|
if (_fd[0]!=-1) while (::close(_fd[0])==-1) if (errno!=EINTR) {
|
|
|
|
_lastError = errno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_fd[0] = -1;
|
|
|
|
}
|
|
|
|
/// closes output pipe if open
|
|
|
|
void close_out() {
|
|
|
|
if (_fd[1]!=-1) while (::close(_fd[1])==-1) if (errno!=EINTR) {
|
|
|
|
_lastError = errno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_fd[1] = -1;
|
|
|
|
}
|
|
|
|
/// @return true if no error occured
|
|
|
|
operator bool() {
|
|
|
|
return _lastError == -1;
|
|
|
|
}
|
|
|
|
/// @return last error code, -1 if no error
|
|
|
|
int error() {
|
|
|
|
return _lastError;
|
|
|
|
}
|
|
|
|
/// connect output stream to @c stdout
|
|
|
|
void connect_cout() {
|
|
|
|
while (::dup2(_fd[1], 1)==-1) if (errno!=EINTR) {
|
|
|
|
_lastError = errno;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// connect output stream to @c stderr
|
|
|
|
void connect_cerr() {
|
|
|
|
while (::dup2(_fd[1], 2)==-1) if (errno!=EINTR) {
|
|
|
|
_lastError = errno;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// get an input stream
|
|
|
|
/** @return stream to read from
|
|
|
|
@note invalid after destruction or @c close or @c close_in */
|
|
|
|
int istream() {
|
|
|
|
return _fd[0];
|
|
|
|
}
|
|
|
|
/// get an output stream
|
|
|
|
/** @return stream to write to
|
|
|
|
@note invalid after destruction or @c close or @c close_out */
|
|
|
|
int ostream() {
|
|
|
|
return _fd[1];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//@}
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|