class inpipebuf : public std::streambuf
{
- protected:
- 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");
-
- // force underflow
- setg (&buffer, &buffer, &buffer);
- }
-
- ~inpipebuf()
- {
- 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()
- {
- if (gptr() < egptr())
- return traits_type::to_int_type(*gptr());
-
- buffer = fgetc (pipe);
- if (feof (pipe))
- {
- // ERROR or EOF
- return EOF;
- }
-
- setg (&buffer, &buffer, &buffer+sizeof(char));
-
- return traits_type::to_int_type(*gptr());
- }
-};
+protected:
+ 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");
+
+ // force underflow
+ setg (&buffer, &buffer, &buffer);
+ }
+
+ ~inpipebuf()
+ {
+ 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()
+ {
+ if (gptr() < egptr())
+ return traits_type::to_int_type(*gptr());
+
+ buffer = fgetc (pipe);
+ if (feof (pipe))
+ {
+ // ERROR or EOF
+ return EOF;
+ }
+
+ setg (&buffer, &buffer, &buffer+sizeof(char));
+
+ return traits_type::to_int_type(*gptr());
+ }
+};
class inpipestream : public std::istream
{
- 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); }
+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) {
- 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 overflow(int_type c)
- {
- if (c != EOF)
- {
- if (fputc(c,pipe)==EOF)
- return EOF;
- }
- return c;
- }
-
- virtual std::streamsize xsputn(const char* s, std::streamsize num)
- {
- return fwrite(s,num,1,pipe);
- }
+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) {
+ 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 overflow(int_type c)
+ {
+ if (c != EOF)
+ {
+ if (fputc(c,pipe)==EOF)
+ return EOF;
+ }
+ return c;
+ }
+
+ virtual std::streamsize xsputn(const char* s, std::streamsize num)
+ {
+ return fwrite(s,num,1,pipe);
+ }
};
class outpipestream : public std::ostream
{
- protected:
- outpipebuf buf;
- public:
- 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); }
+protected:
+ outpipebuf buf;
+public:
+ 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