#include "commandbuf.h" #include "debug.h" #include namespace jules { CommandError::CommandError( const std::string& command0, int errno0) : std::runtime_error( ""), command( command0), errornumber( errno0) { std::stringstream buffer; buffer << "Command returned error " << this->errornumber << ": " << this->command; this->message = buffer.str(); } const char* CommandError::what() const throw() { return this->message.c_str(); } CommandBuf::CommandBuf( bool use_exceptions0) : use_exceptions( use_exceptions0) {} int CommandBuf::sync() { if ( this->buffer=="") return 0; debug0 << debug_PLACE << this->buffer << std::endl; errno = system( this->buffer.c_str()); if (errno && this->use_exceptions) { CommandError e(this->buffer, errno); this->buffer = ""; throw e; } this->buffer=""; return 0; } int CommandBuf::overflow( int c) { assert( c>=0 && (unsigned int) c<=UCHAR_MAX); this->buffer += static_cast< unsigned char>( c); return 0; } CommandBuf::~CommandBuf() { this->sync(); } Command::Command(bool use_exceptions0) : buffer(use_exceptions0), stream( &buffer) {} Command::operator std::ostream& () { return this->stream; } std::ostream& Command::GetStream() { return *this; } void System( const std::string& cmd) { int ret = system( cmd.c_str()); if ( ret) { std::stringstream buffer; buffer << "Command `" << cmd << "' failed with return code " << ret; throw std::runtime_error( buffer.str().c_str()); } } std::ostream& ccmd() { static Command commandstream; return commandstream.GetStream(); } std::ostream& ccmde() { static Command commandstream(true); return commandstream.GetStream(); } }