00001 #ifndef DVUTIL_PSTREAM_H 00002 #define DVUTIL_PSTREAM_H 00003 // $Id: pstream.h,v 1.5 2003/09/19 18:08:43 dvermeir Exp $ 00004 00005 #include <string> 00006 #include <iostream> 00007 #include <stdexcept> 00008 00009 namespace Dv { 00010 namespace Util { 00011 00012 /** A wrapper that makes a stream of popen(command,"r"). */ 00013 class pstream: public std::iostream { 00014 public: 00015 /** Constructor. 00016 * Create an input or output stream for data produced by a command. If mode is 00017 * "r" (read), reading the stream reads output from the command. 00018 * If mode is "w" (write), writing the stream sends the data 00019 * to the command's standard input. 00020 * @param command see popen(3) 00021 * @param mode read ("r") or write ("w"), see popen(3). 00022 * @exception std::runtime_error if anything goes wrong. 00023 * @warning E.g. for a stream opened in read mode, the default 00024 * standard ouput of the command is still the standard output of the 00025 * parent process. 00026 */ 00027 pstream(const std::string& command, const std::string& mode) throw (std::runtime_error); 00028 00029 /** Close stream. 00030 * Wait for command to finish, then return its exit status. 00031 * After a close, the stream is unusable. 00032 * @return exit status of command as returned by main(). 00033 * @warning A negative exit status gets converted to a 00034 * positive number, i.e. -1 becomes 255. 00035 * @exception std::runtime_error if command did not exit normally or any other problem. 00036 */ 00037 size_t close() throw (std::runtime_error); 00038 00039 /** Destructor. 00040 * @exception std::runtime_error if anything goes wrong. 00041 */ 00042 ~pstream() throw (std::runtime_error); 00043 00044 /* @return command producing data for this stream */ 00045 const std::string& command() const { return command_; } 00046 /* @return mode of this stream */ 00047 const std::string& mode() const { return mode_; } 00048 private: 00049 void* file_; // opaque stdio FILE* 00050 const std::string command_; 00051 const std::string mode_; 00052 }; 00053 }} 00054 #endif
dvutil-0.13.15 | [30 December, 2004] |