diff --git a/source/matplot/util/common.cpp b/source/matplot/util/common.cpp index 557a3e5b..2aa2c4f0 100644 --- a/source/matplot/util/common.cpp +++ b/source/matplot/util/common.cpp @@ -50,11 +50,18 @@ namespace matplot { iequals(str, "no"); } + struct pipe_deleter { + int operator()(FILE* pipe) const { + if (int status = PCLOSE(pipe); status != -1) + return status; + throw std::system_error{errno, std::system_category(), "pclose"}; + } + }; + std::string run_and_get_output(const std::string &cmd) { - std::unique_ptr pipe(POPEN(cmd.c_str(), "r"), - PCLOSE); + std::unique_ptr pipe(POPEN(cmd.c_str(), "r")); if (!pipe) { - throw std::runtime_error("popen() failed!"); + throw std::system_error{errno, std::system_category(), cmd}; } std::array buffer{}; std::string result; @@ -363,7 +370,7 @@ namespace matplot { std::string fileread(const std::string &filename) { std::ifstream t(filename); if (!t) { - throw std::runtime_error("Cannot open the file " + filename); + throw std::system_error(errno, std::system_category(), filename); } std::string str((std::istreambuf_iterator(t)), std::istreambuf_iterator());