Skip to content

Commit

Permalink
refactor(pipe): attach error code to exceptions via std::system_error
Browse files Browse the repository at this point in the history
  • Loading branch information
mikucionisaau authored Sep 4, 2024
1 parent 2a4eec4 commit 4ab073b
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions source/matplot/util/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<FILE, int (*)(FILE *)> pipe(POPEN(cmd.c_str(), "r"),
PCLOSE);
std::unique_ptr<FILE, pipe_deleter> 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<char, 128> buffer{};
std::string result;
Expand Down Expand Up @@ -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<char>(t)),
std::istreambuf_iterator<char>());
Expand Down

0 comments on commit 4ab073b

Please sign in to comment.