Skip to content

Commit 2db423f

Browse files
committed
Don't depend on internals of FILE*
closes alandefreitas#4
1 parent bb13948 commit 2db423f

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

source/matplot/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ target_link_libraries(matplot PUBLIC nodesoup cimg)
8989
# https://cmake.org/cmake/help/v3.14/manual/cmake-compile-features.7.html#requiring-language-standards
9090
target_compile_features(matplot PUBLIC cxx_std_17)
9191

92+
include(CheckSymbolExists)
93+
94+
# Some hack to not depend on FILE* internals
95+
# https://github.com/alandefreitas/matplotplusplus/issues/4
96+
check_symbol_exists(__fbufsize "stdio_ext.h" HAVE_FBUFSIZE)
97+
if (HAVE_FBUFSIZE)
98+
target_compile_definitions(matplot PRIVATE MATPLOT_HAS_FBUFSIZE)
99+
endif()
100+
92101
if (BUILD_FOR_DOCUMENTATION_IMAGES)
93102
message("Building matplot for documentation images. wait() commands will be ignored. ~figure will save the files.")
94103
target_compile_definitions(matplot PUBLIC MATPLOT_BUILD_FOR_DOCUMENTATION_IMAGES)

source/matplot/backend/gnuplot.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@
1010
#include <matplot/util/popen.h>
1111
#include <matplot/util/common.h>
1212

13+
#ifdef MATPLOT_HAS_FBUFSIZE
14+
15+
#include <stdio_ext.h>
16+
17+
static size_t gnuplot_pipe_capacity(FILE *f) {
18+
size_t sz = __fbufsize(f);
19+
return sz != 0 ? sz : matplot::backend::gnuplot::pipe_capacity_worst_case;
20+
}
21+
22+
#else
23+
24+
static size_t gnuplot_pipe_capacity(FILE *) {
25+
return matplot::backend::gnuplot::pipe_capacity_worst_case;
26+
}
27+
28+
#endif // MATPLOT_HAS_FBUFSIZE
29+
1330
namespace matplot::backend {
1431
bool gnuplot::consumes_gnuplot_commands() {
1532
return true;
@@ -220,7 +237,7 @@ namespace matplot::backend {
220237
if (!pipe_) {
221238
return;
222239
}
223-
size_t pipe_capacity = (pipe_->_bf._base != nullptr) ? pipe_->_bf._size : pipe_capacity_worst_case;
240+
size_t pipe_capacity = gnuplot_pipe_capacity(pipe_);
224241
if (command.size() + bytes_in_pipe_ > pipe_capacity) {
225242
flush_commands();
226243
bytes_in_pipe_ = 0;

source/matplot/backend/gnuplot.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ namespace matplot::backend {
7777
static constexpr bool trace_commands = false;
7878
#endif
7979

80-
static constexpr size_t pipe_capacity_worst_case = 4096;
80+
static constexpr size_t pipe_capacity_worst_case = BUFSIZ;
8181
/// File formats for figures and properties of terminals
8282
static constexpr std::array<std::pair<std::string_view,std::string_view>,33> extension_terminal() {
8383
return std::array<std::pair<std::string_view,std::string_view>,33>

0 commit comments

Comments
 (0)