Skip to content

Commit 45736a1

Browse files
committed
Refactored popen/pclose into custom implementation and added c++ wrappers
1 parent f874e9e commit 45736a1

File tree

5 files changed

+279
-194
lines changed

5 files changed

+279
-194
lines changed

source/matplot/backend/gnuplot.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,16 @@ namespace matplot::backend {
7070
}
7171

7272
// Open the gnuplot pipe_
73+
int perr;
7374
if constexpr (windows_should_persist_by_default) {
74-
pipe_ = POPEN("gnuplot --persist", "w");
75+
perr = popen2("gnuplot --persist", "w", &pipe_);
7576
} else {
76-
pipe_ = POPEN("gnuplot", "w");
77+
perr = popen2("gnuplot", "w", &pipe_);
7778
}
7879

7980
// Check if everything is OK
80-
if (!pipe_) {
81-
std::cerr << "Opening the gnuplot pipe_ failed!" << std::endl;
81+
if (perr != 0) {
82+
std::cerr << "Opening the gnuplot pipe_ failed: " std::strerror(perr) << std::endl;
8283
std::cerr
8384
<< "Please install gnuplot 5.2.6+: http://www.gnuplot.info"
8485
<< std::endl;
@@ -97,8 +98,8 @@ namespace matplot::backend {
9798
flush_commands();
9899
run_command("exit");
99100
flush_commands();
100-
if (pipe_) {
101-
PCLOSE(pipe_);
101+
if (pipe_.file != nullptr) {
102+
pclose2(&pipe_);
102103
}
103104
}
104105

@@ -281,8 +282,7 @@ namespace matplot::backend {
281282
if constexpr (dont_let_it_close_too_fast) {
282283
last_flush_ = std::chrono::high_resolution_clock::now();
283284
}
284-
fputs("\n", pipe_);
285-
fflush(pipe_);
285+
proc_flush(&pipe_, "\n");
286286
if constexpr (trace_commands) {
287287
std::cout << "\n\n\n\n" << std::endl;
288288
}
@@ -294,19 +294,19 @@ namespace matplot::backend {
294294
}
295295

296296
void gnuplot::run_command(const std::string &command) {
297-
if (!pipe_) {
297+
if (pipe_.file != nullptr) {
298298
return;
299299
}
300-
size_t pipe_capacity = gnuplot_pipe_capacity(pipe_);
300+
size_t pipe_capacity = gnuplot_pipe_capacity(pipe_.file);
301301
if (command.size() + bytes_in_pipe_ > pipe_capacity) {
302302
flush_commands();
303303
bytes_in_pipe_ = 0;
304304
}
305305
if (!command.empty()) {
306-
fputs(command.c_str(), pipe_);
306+
proc_write(&pipe_, command);
307307
}
308-
// fputs("; ", pipe_);
309-
fputs("\n", pipe_);
308+
// proc_write(&pipe_, "; ");
309+
proc_write(&pipe_, "\n");
310310
bytes_in_pipe_ += command.size();
311311
if constexpr (trace_commands) {
312312
std::cout << command << std::endl;

source/matplot/backend/gnuplot.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <matplot/detail/config.h>
99
#include <matplot/backend/backend_interface.h>
10+
#include <matplot/util/popen.h>
1011
#include <array>
1112
#include <chrono>
1213
#include <tuple>
@@ -115,8 +116,8 @@ namespace matplot::backend {
115116
}
116117

117118
private:
118-
// Pipe to gnuplot process
119-
FILE *pipe_;
119+
// Process pipe to gnuplot
120+
ProcPipe pipe_;
120121

121122
// How many bytes we put in the pipe
122123
size_t bytes_in_pipe_{0};

source/matplot/util/common.cpp

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,8 @@ namespace matplot {
5050
iequals(str, "no");
5151
}
5252

53-
struct pipe_deleter {
54-
int operator()(FILE* pipe) const {
55-
if (int status = PCLOSE(pipe); status != -1)
56-
return status;
57-
throw std::system_error{errno, std::system_category(), "pclose"};
58-
}
59-
};
60-
6153
std::string run_and_get_output(const std::string &cmd) {
62-
std::unique_ptr<FILE, pipe_deleter> pipe(POPEN(cmd.c_str(), "r"));
63-
if (!pipe) {
64-
throw std::system_error{errno, std::system_category(), cmd};
65-
}
66-
std::array<char, 128> buffer{};
67-
std::string result;
68-
while (fgets(buffer.data(), static_cast<int>(buffer.size()),
69-
pipe.get()) != nullptr) {
70-
result += buffer.data();
71-
}
72-
return result;
54+
return shell_read(cmd);
7355
}
7456

7557
std::string escape(std::string_view label) {

0 commit comments

Comments
 (0)