7 #include <sys/select.h>
16 #ifndef WIBBLE_SYS_PIPE_H
17 #define WIBBLE_SYS_PIPE_H
22 namespace wexcept = wibble::exception;
34 Writer() : fd( -1 ), close( false ), running( false ) {}
42 wrote = ::write( fd, data.c_str(), data.length() );
44 data.erase( data.begin(), data.begin() + wrote );
48 if ( errno == EAGAIN || errno == EWOULDBLOCK )
51 throw wexcept::System(
"writing to pipe" );
68 void run(
int _fd, std::string what ) {
84 typedef std::deque< char > Buffer;
90 Pipe(
int p ) : fd( p ), _eof( false )
94 if ( fcntl( fd, F_SETFL, O_NONBLOCK ) == -1 )
95 throw wexcept::System(
"fcntl on a pipe" );
97 Pipe() : fd( -1 ), _eof( false ) {}
100 void write( std::string what ) {
101 writer.run( fd, what );
106 writer.run( fd,
"" );
114 return valid() && !eof();
124 int r = ::read( fd, _buffer, 1023 );
125 if ( r == -1 && errno != EAGAIN && errno != EWOULDBLOCK )
126 throw wexcept::System(
"reading from pipe" );
132 std::copy( _buffer, _buffer + r, std::back_inserter( buffer ) );
136 std::string nextChunk() {
137 std::string line( buffer.begin(), buffer.end() );
142 std::string nextLine() {
145 std::find( buffer.begin(), buffer.end(),
'\n' );
146 while ( nl == buffer.end() ) {
149 nl = std::find( buffer.begin(), buffer.end(),
'\n' );
151 std::string line( buffer.begin(), nl );
153 if ( nl != buffer.end() )
155 buffer.erase( buffer.begin(), nl );
165 while ( buffer.empty() && !eof() ) {
171 select( fd + 1, &fds, 0, 0, 0 );
174 std::string nextLineBlocking() {
182 return std::string( buffer.begin(), buffer.end() );