#ifndef JULES_UTILS_COMMANDBUF_H #define JULES_UTILS_COMMANDBUF_H #include #include #include #include #include #include #include namespace jules { struct CommandError : std::runtime_error { CommandError( const std::string& command0, int errno0); ~CommandError() throw() {} virtual const char* what() const throw(); std::string command; int errornumber; std::string message; }; struct CommandBuf : std::streambuf /* A std::streambuf that send text to system() whenever sync() is called. sync() will set errno to return value from system() */ { explicit CommandBuf( bool use_exceptions0); virtual int sync(); virtual int overflow( int c); virtual ~CommandBuf(); private: std::string buffer; bool use_exceptions; }; struct Command { Command(bool use_exceptions0=false); operator std::ostream& (); std::ostream& GetStream(); private: CommandBuf buffer; std::ostream stream; }; void System( const std::string& cmd); std::ostream& ccmd(); /* Returns a singleton ostream that sends text to system() whenever it is flushed. Use like: ccmd() << "ls -l" << flush; // runs `ls -l'. int i=34; ccmd() << "find "; ccmd() << ". -name "; ccmd() << "foo" << i << ".txt"; ccmd() << flush; // runs `find . -name foo34.txt'. When a command is executed, the return from system() is put into errno */ std::ostream& ccmde(); /* As ccmde(), except that errors cause an exception. */ } #endif