ExternalProgram: add command output redirection to stream.
authorMichael Andres <ma@suse.de>
Thu, 23 Jul 2009 08:10:39 +0000 (10:10 +0200)
committerMichael Andres <ma@suse.de>
Thu, 23 Jul 2009 08:10:39 +0000 (10:10 +0200)
zypp/ExternalProgram.cc
zypp/ExternalProgram.h

index 8da0779..22979d0 100644 (file)
@@ -483,4 +483,13 @@ namespace zypp {
       }
     }
 
+    std::ostream & ExternalProgram::operator>>( std::ostream & out_r )
+    {
+      setBlocking( true );
+      for ( std::string line = receiveLine(); line.length(); line = receiveLine() )
+        out_r << line;
+      return out_r;
+    }
+
+
 } // namespace zypp
index 305e03f..f33a6d6 100644 (file)
@@ -168,6 +168,28 @@ namespace zypp {
        */
       static void renumber_fd (int origfd, int newfd);
 
+    public:
+
+      /**
+       * Redirect all command output to an \c ostream.
+       * Returns when the command has completed.
+       * \code
+       *   std::ostringstream s;
+       *   ExternalProgram("pwd") >> s;
+       *   SEC << s.str() << endl;
+       * \endcode
+       * \code
+       *   std::ostringstream s;
+       *   ExternalProgram prog("ls -l wrzl");
+       *   prog >> s;
+       *   if ( prog.close() == 0 )
+       *     MIL << s.str() << endl;
+       *   else
+       *     ERR << prog.execError() << endl;
+       * \endcode
+       */
+      std::ostream & operator>>( std::ostream & out_r );
+
     protected:
       int checkStatus( int );