Add Subprocess::callAndGetReturnCode()

This commit is contained in:
Sebastian Messmer 2015-11-03 13:11:09 -08:00
parent 8b585c39fe
commit 7f6dffdbd9
2 changed files with 22 additions and 6 deletions

View File

@ -5,13 +5,10 @@
using std::string;
namespace cpputils {
//TODO Exception safety
string Subprocess::call(const string &command) {
//TODO Exception safety
FILE *subprocessOutput = popen(command.c_str(), "r");
if (!subprocessOutput)
{
throw std::runtime_error("Error starting subprocess "+command);
}
FILE *subprocessOutput = _call(command);
string result;
char buffer[1024];
@ -26,4 +23,20 @@ namespace cpputils {
return result;
}
int Subprocess::callAndGetReturnCode(const string &command) {
FILE *subprocess = _call(command);
auto returncode = pclose(subprocess);
return WEXITSTATUS(returncode);
}
FILE *Subprocess::_call(const string &command) {
FILE *subprocess = popen(command.c_str(), "r");
if (!subprocess)
{
throw std::runtime_error("Error starting subprocess "+command);
}
return subprocess;
}
}

View File

@ -9,6 +9,9 @@ namespace cpputils {
class Subprocess {
public:
static std::string call(const std::string &command);
static int callAndGetReturnCode(const std::string &command);
private:
static FILE* _call(const std::string &command);
};
}