1 /***************************************************************************
2 inpipestream.hxx - C++ streambuffer wrapper
4 begin : Thu Dec 27 2001
5 copyright : (C) 2001 by Intra2net AG
6 email : intranator@intra2net.com
7 ***************************************************************************/
16 #include "exception.hxx"
18 // ATTENTION: A lot of mysterious STL bugs occured
19 // with a "real" buffer (buffer larger than 1 byte and up to 100 bytes)
20 // -> Keep it slow and working!
22 class inpipebuf : public std::streambuf
28 // "callback" variables for destructor to store exit status
33 inpipebuf(const std::string& command)
38 pipe = popen (command.c_str(), "r");
40 throw EXCEPTION (pipestream_error, "can't open program or permission denied");
43 setg (&buffer, &buffer, &buffer);
49 int pclose_exit = pclose (pipe);
51 if (exit_status && pclose_exit != -1) {
53 *exit_status = pclose_exit;
60 void store_exit_status(bool *_status_set, int *_exit_status)
61 { status_set = _status_set; exit_status = _exit_status; }
64 virtual int_type underflow()
67 return traits_type::to_int_type(*gptr());
69 buffer = fgetc (pipe);
76 setg (&buffer, &buffer, &buffer+sizeof(char));
78 return traits_type::to_int_type(*gptr());
82 class inpipestream : public std::istream
88 inpipestream(const std::string& command)
89 : buf(command), std::istream(&buf)
92 void store_exit_status(bool *_status_set, int *_exit_status)
93 { buf.store_exit_status(_status_set, _exit_status); }
96 class outpipebuf : public std::streambuf
101 // "callback" variables for destructor to store exit status
106 outpipebuf(const std::string& command)
111 pipe = popen (command.c_str(), "w");
113 throw EXCEPTION (pipestream_error, "can't open program or permission denied");
119 int pclose_exit = pclose (pipe);
121 if (exit_status && pclose_exit != -1) {
123 *exit_status = pclose_exit;
130 void store_exit_status(bool *_status_set, int *_exit_status)
131 { status_set = _status_set; exit_status = _exit_status; }
134 virtual int_type overflow(int_type c)
138 if (fputc(c,pipe)==EOF)
144 virtual std::streamsize xsputn(const char* s, std::streamsize num)
146 return fwrite(s,num,1,pipe);
150 class outpipestream : public std::ostream
155 outpipestream(const std::string& command)
156 : buf(command), std::ostream(&buf)
159 void store_exit_status(bool *_status_set, int *_exit_status)
160 { buf.store_exit_status(_status_set, _exit_status); }