ui, libi2ncommon: (tomj) proper MAC address error handling, ability to get exit statu...
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Thu, 2 Nov 2006 19:57:18 +0000 (19:57 +0000)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Thu, 2 Nov 2006 19:57:18 +0000 (19:57 +0000)
    ATTENTION: i18n translation

src/pipestream.hxx

index de59760..991e65c 100644 (file)
@@ -25,9 +25,16 @@ class inpipebuf : public std::streambuf
    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");
@@ -38,12 +45,21 @@ class inpipebuf : public std::streambuf
 
    ~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()
    {
@@ -67,10 +83,14 @@ 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); }
 };
 
 class outpipebuf : public std::streambuf
@@ -78,9 +98,16 @@ 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");
@@ -88,12 +115,21 @@ class outpipebuf : public std::streambuf
 
    ~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)
    {
@@ -119,6 +155,9 @@ class outpipestream : public std::ostream
       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