Line data Source code
1 : #include "utils/pipestream.hpp" 2 : 3 : #include <unistd.h> 4 : 5 : #include <stdexcept> 6 : 7 : using namespace std; 8 : using namespace utils; 9 : 10 0 : pair<int, int> create_pipe() { 11 0 : int fds[2]; 12 0 : if(pipe(fds)) { 13 0 : throw runtime_error("Failed to create pipe"); 14 : } 15 0 : return pair<int, int>(fds[0], fds[1]); 16 : } 17 : 18 0 : pipestream::pipestream(): 19 0 : pipestream(create_pipe()) {} 20 : 21 0 : pipestream::pipestream(pair<int, int> fds): 22 : ifs(fds.first), 23 : ofs(fds.second), 24 : is(0), 25 : os(0), 26 : iBuf(fds.first, ios::in), 27 0 : oBuf(fds.second, ios::out) { 28 0 : is.rdbuf(&iBuf); 29 0 : os.rdbuf(&oBuf); 30 : 31 0 : is.exceptions( 32 : iostream::badbit 33 : ); 34 0 : os.exceptions( 35 : iostream::failbit | iostream::badbit 36 : ); 37 0 : } 38 : 39 0 : istream &pipestream::i() { return is; } 40 0 : ostream &pipestream::o() { return os; } 41 : 42 0 : void pipestream::closeWrite() { 43 0 : close(ofs); 44 0 : }