char buffer;
FILE *pipe;
+ // "callback" variables for destructor to store exit status
+ bool *status_set;
+ int *exit_status;
+
public:
inpipebuf(const std::string& command)
{
+ status_set = NULL;
+ exit_status = NULL;
+
pipe = popen (command.c_str(), "r");
if (pipe == NULL)
throw EXCEPTION (pipestream_error, "can't open program or permission denied");
~inpipebuf()
{
- if (pipe != NULL)
- pclose (pipe);
-
- pipe = NULL;
+ if (pipe != NULL) {
+ int pclose_exit = pclose (pipe);
+
+ if (exit_status && pclose_exit != -1) {
+ *status_set = true;
+ *exit_status = pclose_exit;
+ }
+
+ pipe = NULL;
+ }
}
+ void store_exit_status(bool *_status_set, int *_exit_status)
+ { status_set = _status_set; exit_status = _exit_status; }
+
protected:
virtual int_type underflow()
{
{
protected:
inpipebuf buf;
+
public:
inpipestream(const std::string& command)
: buf(command), std::istream(&buf)
{}
+
+ void store_exit_status(bool *_status_set, int *_exit_status)
+ { buf.store_exit_status(_status_set, _exit_status); }
};
class outpipebuf : public std::streambuf
protected:
FILE *pipe;
+ // "callback" variables for destructor to store exit status
+ bool *status_set;
+ int *exit_status;
+
public:
outpipebuf(const std::string& command)
{
+ status_set = NULL;
+ exit_status = NULL;
+
pipe = popen (command.c_str(), "w");
if (pipe == NULL)
throw EXCEPTION (pipestream_error, "can't open program or permission denied");
~outpipebuf()
{
- if (pipe != NULL)
- pclose (pipe);
+ if (pipe != NULL) {
+ int pclose_exit = pclose (pipe);
+
+ if (exit_status && pclose_exit != -1) {
+ *status_set = true;
+ *exit_status = pclose_exit;
+ }
- pipe = NULL;
+ pipe = NULL;
+ }
}
+ void store_exit_status(bool *_status_set, int *_exit_status)
+ { status_set = _status_set; exit_status = _exit_status; }
+
protected:
virtual int_type overflow(int_type c)
{
outpipestream(const std::string& command)
: buf(command), std::ostream(&buf)
{}
+
+ void store_exit_status(bool *_status_set, int *_exit_status)
+ { buf.store_exit_status(_status_set, _exit_status); }
};
#endif