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