Add Subprocess::callAndGetReturnCode()
This commit is contained in:
parent
8b585c39fe
commit
7f6dffdbd9
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user