34 #ifndef GNSS_SDR_GNUPLOT_I_H 35 #define GNSS_SDR_GNUPLOT_I_H 37 #include <gflags/gflags.h> 50 DEFINE_bool(show_plots,
true,
"Show plots on screen. Disable for non-interactive testing.");
52 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) 56 #define GP_MAX_TMP_FILES 27 // 27 temporary files it's Microsoft restriction 58 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__) 61 #define GP_MAX_TMP_FILES 1024 63 #error unsupported or unknown operating system 71 explicit GnuplotException(
const std::string &msg) : std::runtime_error(msg) {}
93 std::vector<std::string> tmpfile_list;
98 static int tmpfile_num;
100 static std::string m_sGNUPlotFileName;
102 static std::string m_sGNUPlotPath;
104 static std::string terminal_std;
124 std::string create_tmpfile(std::ofstream &tmp);
133 static bool get_program_path();
143 bool file_available(
const std::string &filename);
153 static bool file_exists(
const std::string &filename,
int mode = 0);
164 static bool set_GNUPlotPath(
const std::string &path);
174 static void set_terminal_std(
const std::string &type);
181 explicit Gnuplot(
const std::string &style =
"points");
184 Gnuplot(
const std::vector<double> &x,
185 const std::string &title =
"",
186 const std::string &style =
"points",
187 const std::string &labelx =
"x",
188 const std::string &labely =
"y");
191 Gnuplot(
const std::vector<double> &x,
192 const std::vector<double> &y,
193 const std::string &title =
"",
194 const std::string &style =
"points",
195 const std::string &labelx =
"x",
196 const std::string &labely =
"y");
199 Gnuplot(
const std::vector<double> &x,
200 const std::vector<double> &y,
201 const std::vector<double> &z,
202 const std::string &title =
"",
203 const std::string &style =
"points",
204 const std::string &labelx =
"x",
205 const std::string &labely =
"y",
206 const std::string &labelz =
"z");
215 Gnuplot &cmd(
const std::string &cmdstr);
241 Gnuplot &savetops(
const std::string &filename =
"gnuplot_output");
244 Gnuplot &savetopdf(
const std::string &filename =
"gnuplot_output",
unsigned int font_size = 12);
252 Gnuplot &set_style(
const std::string &stylestr =
"points");
258 Gnuplot &set_smooth(
const std::string &stylestr =
"csplines");
275 Gnuplot &set_pointsize(
const double pointsize = 1.0);
297 inline Gnuplot &set_multiplot(
int rows,
int cols)
299 cmd(
"set multiplot layout " + std::to_string(rows) +
"," + std::to_string(cols));
310 inline Gnuplot &unset_multiplot()
312 cmd(
"unset multiplot");
317 Gnuplot &set_samples(
const int samples = 100);
319 Gnuplot &set_isosamples(
const int isolines = 10);
341 inline Gnuplot &unset_hidden3d()
343 cmd(
"unset hidden3d");
349 Gnuplot &set_contour(
const std::string &position =
"base");
357 inline Gnuplot &unset_contour()
359 cmd(
"unset contour");
384 inline Gnuplot &unset_surface()
386 cmd(
"unset surface");
393 Gnuplot &set_legend(
const std::string &position =
"default");
416 inline Gnuplot &set_title(
const std::string &title =
"")
419 cmdstr =
"set title \"";
441 Gnuplot &set_ylabel(
const std::string &label =
"x");
443 Gnuplot &set_xlabel(
const std::string &label =
"y");
445 Gnuplot &set_zlabel(
const std::string &label =
"z");
448 Gnuplot &set_xrange(
const double iFrom,
const double iTo);
450 Gnuplot &set_yrange(
const double iFrom,
const double iTo);
452 Gnuplot &set_zrange(
const double iFrom,
const double iTo);
460 inline Gnuplot &set_xautoscale()
462 cmd(
"set xrange restore");
463 cmd(
"set autoscale x");
474 inline Gnuplot &set_yautoscale()
476 cmd(
"set yrange restore");
477 cmd(
"set autoscale y");
488 inline Gnuplot &set_zautoscale()
490 cmd(
"set zrange restore");
491 cmd(
"set autoscale z");
496 Gnuplot &set_xlogscale(
const double base = 10);
498 Gnuplot &set_ylogscale(
const double base = 10);
500 Gnuplot &set_zlogscale(
const double base = 10);
509 inline Gnuplot &unset_xlogscale()
511 cmd(
"unset logscale x");
522 inline Gnuplot &unset_ylogscale()
524 cmd(
"unset logscale y");
535 inline Gnuplot &unset_zlogscale()
537 cmd(
"unset logscale z");
542 Gnuplot &set_cbrange(
const double iFrom,
const double iTo);
549 Gnuplot &plotfile_x(
const std::string &filename,
550 const unsigned int column = 1,
551 const std::string &title =
"");
554 template <
typename X>
555 Gnuplot &plot_x(
const X &x,
const std::string &title =
"");
559 Gnuplot &plotfile_xy(
const std::string &filename,
560 const unsigned int column_x = 1,
561 const unsigned int column_y = 2,
562 const std::string &title =
"",
563 const unsigned int decimate = 1);
565 template <
typename X,
typename Y>
566 Gnuplot &plot_xy(
const X &x,
const Y &y,
567 const std::string &title =
"",
568 const unsigned int decimate = 1);
572 Gnuplot &plotfile_xy_err(
const std::string &filename,
573 const unsigned int column_x = 1,
574 const unsigned int column_y = 2,
575 const unsigned int column_dy = 3,
576 const std::string &title =
"");
578 template <
typename X,
typename Y,
typename E>
579 Gnuplot &plot_xy_err(
const X &x,
const Y &y,
const E &dy,
580 const std::string &title =
"");
582 template <
typename X,
typename Y,
typename E>
583 Gnuplot &plot_grid3d(
const X &x,
const Y &y,
const E &mag,
584 const std::string &title =
"");
588 Gnuplot &plotfile_xyz(
const std::string &filename,
589 const unsigned int column_x = 1,
590 const unsigned int column_y = 2,
591 const unsigned int column_z = 3,
592 const std::string &title =
"");
594 template <
typename X,
typename Y,
typename Z>
598 const std::string &title =
"");
601 Gnuplot &plot_slope(
const double a,
603 const std::string &title =
"");
615 Gnuplot &plot_equation(
const std::string &equation,
const std::string &title =
"");
619 Gnuplot &plot_equation3d(
const std::string &equation,
const std::string &title =
"");
622 Gnuplot &plot_image(
const unsigned char *ucPicBuf,
623 const unsigned int iWidth,
624 const unsigned int iHeight,
625 const std::string &title =
"");
628 Gnuplot &plot_circle(
double east,
double north,
double radius,
const std::string &label =
"");
655 void remove_tmpfiles();
665 inline bool is_valid() {
return (valid); };
673 int Gnuplot::tmpfile_num = 0;
675 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) 676 std::string Gnuplot::m_sGNUPlotFileName =
"pgnuplot.exe";
677 std::string Gnuplot::m_sGNUPlotPath =
"C:/program files/gnuplot/bin/";
678 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__) 679 std::string Gnuplot::m_sGNUPlotFileName =
"gnuplot";
680 std::string Gnuplot::m_sGNUPlotPath =
"/usr/local/bin/";
683 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) 684 std::string Gnuplot::terminal_std =
"windows";
685 #elif (defined(unix) || defined(__unix) || defined(__unix__)) && !defined(__APPLE__) 686 std::string Gnuplot::terminal_std =
"x11";
687 #elif defined(__APPLE__) 688 std::string Gnuplot::terminal_std =
"aqua";
696 : gnucmd(nullptr), valid(false), two_dim(false), nplots(0)
709 const std::string &title,
710 const std::string &style,
711 const std::string &labelx,
712 const std::string &labely)
713 : gnucmd(nullptr), valid(false), two_dim(false), nplots(0)
730 const std::vector<double> &y,
731 const std::string &title,
732 const std::string &style,
733 const std::string &labelx,
734 const std::string &labely)
735 : gnucmd(nullptr), valid(false), two_dim(false), nplots(0)
743 plot_xy(x, y, title);
752 const std::vector<double> &y,
753 const std::vector<double> &z,
754 const std::string &title,
755 const std::string &style,
756 const std::string &labelx,
757 const std::string &labely,
758 const std::string &labelz)
759 : gnucmd(nullptr), valid(false), two_dim(false), nplots(0)
768 plot_xyz(x, y, z, title);
776 template <
typename X>
777 Gnuplot &Gnuplot::plot_x(
const X &x,
const std::string &title)
786 std::string name = create_tmpfile(tmp);
795 for (
unsigned int i = 0; i < x.size(); i++)
803 plotfile_x(name, 1, title);
813 template <
typename X,
typename Y>
814 Gnuplot &Gnuplot::plot_xy(
const X &x,
const Y &y,
const std::string &title,
const unsigned int decimate)
816 if (x.empty() || y.empty())
822 if (x.size() != y.size())
829 std::string name = create_tmpfile(tmp);
838 for (
unsigned int i = 0; i < x.size(); i++)
840 tmp << x[i] <<
" " << y[i] <<
'\n';
846 plotfile_xy(name, 1, 2, title, decimate);
856 template <
typename X,
typename Y,
typename E>
857 Gnuplot &Gnuplot::plot_xy_err(
const X &x,
860 const std::string &title)
862 if (x.empty() || y.empty() || dy.empty())
868 if (x.size() != y.size() || y.size() != dy.size())
875 std::string name = create_tmpfile(tmp);
884 for (
unsigned int i = 0; i < x.size(); i++)
886 tmp << x[i] <<
" " << y[i] <<
" " << dy[i] <<
'\n';
893 plotfile_xy_err(name, 1, 2, 3, title);
903 template <
typename X,
typename Y,
typename E>
904 Gnuplot &Gnuplot::plot_grid3d(
const X &x,
907 const std::string &title)
909 if (x.empty() || y.empty())
915 std::string name = create_tmpfile(tmp);
924 for (
unsigned int i = 0; i < x.size(); i++)
926 for (
unsigned int k = 0; k < y.size(); k++)
928 tmp << static_cast<float>(x.at(i)) <<
" " << static_cast<float>(y.at(k)) <<
" " << mag.at(i).at(k) <<
'\n';
935 std::ostringstream cmdstr;
936 cmdstr <<
"set ticslevel 0\n";
937 cmdstr <<
"set hidden3d\n";
938 cmdstr <<
"unset colorbox\n";
939 cmdstr <<
"set border 5\n";
940 cmdstr <<
"unset ztics\n";
942 cmdstr <<
" splot \"" << name <<
"\" u 1:2:3";
946 cmdstr <<
" notitle with " << pstyle <<
" palette";
950 cmdstr <<
" title \"" << title <<
"\" with " << pstyle <<
" palette";
966 template <
typename X,
typename Y,
typename Z>
967 Gnuplot &Gnuplot::plot_xyz(
const X &x,
970 const std::string &title)
972 if (x.empty() || y.empty() || z.empty())
978 if (x.size() != y.size() || x.size() != z.size())
985 std::string name = create_tmpfile(tmp);
994 for (
unsigned int i = 0; i < x.size(); i++)
996 tmp << x[i] <<
" " << y[i] <<
" " << z[i] <<
'\n';
1002 plotfile_xyz(name, 1, 2, 3, title);
1013 inline bool Gnuplot::set_GNUPlotPath(
const std::string &path)
1015 std::string tmp = path +
"/" + Gnuplot::m_sGNUPlotFileName;
1017 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) 1018 if (Gnuplot::file_exists(tmp, 0))
1019 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__) 1020 if (Gnuplot::file_exists(tmp, 1))
1023 Gnuplot::m_sGNUPlotPath = path;
1027 Gnuplot::m_sGNUPlotPath.clear();
1037 inline void Gnuplot::set_terminal_std(
const std::string &type)
1039 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__) 1040 if (type.find(
"x11") != std::string::npos && std::getenv(
"DISPLAY") ==
nullptr)
1046 Gnuplot::terminal_std = type;
1055 template <
typename Container>
1056 void stringtok(Container &container,
1057 std::string
const &in,
1058 const char *
const delimiters =
" \t\n")
1060 const std::string::size_type len = in.length();
1061 std::string::size_type i = 0;
1066 i = in.find_first_not_of(delimiters, i);
1068 if (i == std::string::npos)
1074 std::string::size_type j = in.find_first_of(delimiters, i);
1077 if (j == std::string::npos)
1079 container.push_back(in.substr(i));
1083 container.push_back(in.substr(i, j - i));
1101 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) 1102 if (_pclose(gnucmd) == -1)
1104 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__) 1105 if (pclose(gnucmd) == -1)
1109 std::cout <<
"Gnuplot window left open.\n";
1117 inline Gnuplot &Gnuplot::reset_plot()
1129 inline Gnuplot &Gnuplot::reset_all()
1146 inline Gnuplot &Gnuplot::set_style(
const std::string &stylestr)
1148 if (stylestr.find(
"lines") == std::string::npos &&
1149 stylestr.find(
"points") == std::string::npos &&
1150 stylestr.find(
"linespoints") == std::string::npos &&
1151 stylestr.find(
"impulses") == std::string::npos &&
1152 stylestr.find(
"dots") == std::string::npos &&
1153 stylestr.find(
"steps") == std::string::npos &&
1154 stylestr.find(
"fsteps") == std::string::npos &&
1155 stylestr.find(
"histeps") == std::string::npos &&
1156 stylestr.find(
"boxes") == std::string::npos &&
1157 stylestr.find(
"filledcurves") == std::string::npos &&
1158 stylestr.find(
"histograms") == std::string::npos)
1177 pstyle = std::string(
"points");
1191 inline Gnuplot &Gnuplot::set_smooth(
const std::string &stylestr)
1193 if (stylestr.find(
"unique") == std::string::npos &&
1194 stylestr.find(
"frequency") == std::string::npos &&
1195 stylestr.find(
"csplines") == std::string::npos &&
1196 stylestr.find(
"acsplines") == std::string::npos &&
1197 stylestr.find(
"bezier") == std::string::npos &&
1198 stylestr.find(
"sbezier") == std::string::npos)
1215 inline Gnuplot &Gnuplot::disablescreen()
1218 cmd(
"set terminal unknown");
1226 inline Gnuplot &Gnuplot::showonscreen()
1228 std::string persist(
" persist");
1233 cmd(
"set terminal " + Gnuplot::terminal_std + persist);
1243 inline Gnuplot &Gnuplot::savetopdf(
const std::string &filename,
unsigned int font_size)
1245 std::ostringstream cmdstr;
1246 cmdstr <<
"set term pdfcairo enhanced color font \"Times-New-Roman," + std::to_string(font_size) +
"\"\n";
1247 cmdstr <<
"set output \"" << filename <<
".pdf\"\n";
1259 inline Gnuplot &Gnuplot::savetops(
const std::string &filename)
1261 std::ostringstream cmdstr;
1262 cmdstr <<
"set term postscript landscape enhanced color dashed \"Times-Roman\" 18\n";
1263 cmdstr <<
"set output \"" << filename <<
".ps\"\n";
1275 inline Gnuplot &Gnuplot::set_legend(
const std::string &position)
1277 std::ostringstream cmdstr;
1278 cmdstr <<
"set key " << position;
1290 inline Gnuplot &Gnuplot::set_xlogscale(
const double base)
1292 std::ostringstream cmdstr;
1294 cmdstr <<
"set logscale x " << base;
1305 inline Gnuplot &Gnuplot::set_ylogscale(
const double base)
1307 std::ostringstream cmdstr;
1309 cmdstr <<
"set logscale y " << base;
1320 inline Gnuplot &Gnuplot::set_zlogscale(
const double base)
1322 std::ostringstream cmdstr;
1324 cmdstr <<
"set logscale z " << base;
1335 inline Gnuplot &Gnuplot::set_pointsize(
const double pointsize)
1337 std::ostringstream cmdstr;
1338 cmdstr <<
"set pointsize " << pointsize;
1349 inline Gnuplot &Gnuplot::set_samples(
const int samples)
1351 std::ostringstream cmdstr;
1352 cmdstr <<
"set samples " << samples;
1363 inline Gnuplot &Gnuplot::set_isosamples(
const int isolines)
1365 std::ostringstream cmdstr;
1366 cmdstr <<
"set isosamples " << isolines;
1377 inline Gnuplot &Gnuplot::set_contour(
const std::string &position)
1379 if (position.find(
"base") == std::string::npos &&
1380 position.find(
"surface") == std::string::npos &&
1381 position.find(
"both") == std::string::npos)
1383 cmd(
"set contour base");
1387 cmd(
"set contour " + position);
1399 inline Gnuplot &Gnuplot::set_xlabel(
const std::string &label)
1401 std::ostringstream cmdstr;
1403 cmdstr <<
"set xlabel \"" << label <<
"\"";
1413 inline Gnuplot &Gnuplot::set_ylabel(
const std::string &label)
1415 std::ostringstream cmdstr;
1417 cmdstr <<
"set ylabel \"" << label <<
"\"";
1427 inline Gnuplot &Gnuplot::set_zlabel(
const std::string &label)
1429 std::ostringstream cmdstr;
1431 cmdstr <<
"set zlabel \"" << label <<
"\"";
1443 inline Gnuplot &Gnuplot::set_xrange(
const double iFrom,
1446 std::ostringstream cmdstr;
1448 cmdstr <<
"set xrange[" << iFrom <<
":" << iTo <<
"]";
1458 inline Gnuplot &Gnuplot::set_yrange(
const double iFrom,
1461 std::ostringstream cmdstr;
1463 cmdstr <<
"set yrange[" << iFrom <<
":" << iTo <<
"]";
1473 inline Gnuplot &Gnuplot::set_zrange(
const double iFrom,
1476 std::ostringstream cmdstr;
1478 cmdstr <<
"set zrange[" << iFrom <<
":" << iTo <<
"]";
1489 inline Gnuplot &Gnuplot::set_cbrange(
const double iFrom,
1492 std::ostringstream cmdstr;
1494 cmdstr <<
"set cbrange[" << iFrom <<
":" << iTo <<
"]";
1506 inline Gnuplot &Gnuplot::plot_slope(
const double a,
1508 const std::string &title)
1510 std::ostringstream cmdstr;
1514 if (nplots > 0 && two_dim ==
true)
1516 cmdstr <<
"replot ";
1523 cmdstr << a <<
" * x + " << b <<
" title \"";
1527 cmdstr <<
"f(x) = " << a <<
" * x + " << b;
1534 cmdstr <<
"\" with " << pstyle;
1549 inline Gnuplot &Gnuplot::plot_equation(
const std::string &equation,
1550 const std::string &title)
1552 std::ostringstream cmdstr;
1556 if (nplots > 0 && two_dim ==
true)
1558 cmdstr <<
"replot ";
1565 cmdstr << equation <<
" title \"";
1569 cmdstr <<
"f(x) = " << equation;
1576 cmdstr <<
"\" with " << pstyle;
1591 inline Gnuplot &Gnuplot::plot_equation3d(
const std::string &equation,
1592 const std::string &title)
1594 std::ostringstream cmdstr;
1598 if (nplots > 0 && two_dim ==
false)
1600 cmdstr <<
"replot ";
1607 cmdstr << equation <<
" title \"";
1611 cmdstr <<
"f(x,y) = " << equation;
1618 cmdstr <<
"\" with " << pstyle;
1633 inline Gnuplot &Gnuplot::plotfile_x(
const std::string &filename,
1634 const unsigned int column,
1635 const std::string &title)
1640 file_available(filename);
1642 std::ostringstream cmdstr;
1646 if (nplots > 0 && two_dim ==
true)
1648 cmdstr <<
"replot ";
1655 cmdstr <<
"\"" << filename <<
"\" using " << column;
1659 cmdstr <<
" notitle ";
1663 cmdstr <<
" title \"" << title <<
"\" ";
1668 cmdstr <<
"with " << pstyle;
1672 cmdstr <<
"smooth " << smooth;
1688 inline Gnuplot &Gnuplot::plotfile_xy(
const std::string &filename,
1689 const unsigned int column_x,
1690 const unsigned int column_y,
1691 const std::string &title,
1692 const unsigned int decimate)
1697 file_available(filename);
1699 std::ostringstream cmdstr;
1703 if (nplots > 0 && two_dim ==
true)
1705 cmdstr <<
"replot ";
1712 cmdstr <<
"\"" << filename <<
"\" using " << column_x <<
":" << column_y <<
" every " << std::to_string(decimate);
1716 cmdstr <<
" notitle ";
1720 cmdstr <<
" title \"" << title <<
"\" ";
1725 cmdstr <<
"with " << pstyle;
1729 cmdstr <<
"smooth " << smooth;
1745 inline Gnuplot &Gnuplot::plotfile_xy_err(
const std::string &filename,
1746 const unsigned int column_x,
1747 const unsigned int column_y,
1748 const unsigned int column_dy,
1749 const std::string &title)
1754 file_available(filename);
1756 std::ostringstream cmdstr;
1760 if (nplots > 0 && two_dim ==
true)
1762 cmdstr <<
"replot ";
1769 cmdstr <<
"\"" << filename <<
"\" using " 1770 << column_x <<
":" << column_y <<
":" << column_dy
1771 <<
" with errorbars ";
1775 cmdstr <<
" notitle ";
1779 cmdstr <<
" title \"" << title <<
"\" ";
1795 inline Gnuplot &Gnuplot::plotfile_xyz(
const std::string &filename,
1796 const unsigned int column_x,
1797 const unsigned int column_y,
1798 const unsigned int column_z,
1799 const std::string &title)
1804 file_available(filename);
1806 std::ostringstream cmdstr;
1810 if (nplots > 0 && two_dim ==
false)
1812 cmdstr <<
"replot ";
1819 cmdstr <<
"\"" << filename <<
"\" using " << column_x <<
":" << column_y
1824 cmdstr <<
" notitle with " << pstyle;
1828 cmdstr <<
" title \"" << title <<
"\" with " << pstyle;
1844 inline Gnuplot &Gnuplot::plot_image(
const unsigned char *ucPicBuf,
1845 const unsigned int iWidth,
1846 const unsigned int iHeight,
1847 const std::string &title)
1850 std::string name = create_tmpfile(tmp);
1860 for (
unsigned int iRow = 0; iRow < iHeight; iRow++)
1862 for (
unsigned int iColumn = 0; iColumn < iWidth; iColumn++)
1864 tmp << iColumn <<
" " << iRow <<
" " 1865 <<
static_cast<float>(ucPicBuf[iIndex++]) <<
'\n';
1872 std::ostringstream cmdstr;
1876 if (nplots > 0 && two_dim ==
true)
1878 cmdstr <<
"replot ";
1887 cmdstr <<
"\"" << name <<
"\" with image";
1891 cmdstr <<
"\"" << name <<
"\" title \"" << title <<
"\" with image";
1903 inline Gnuplot &Gnuplot::plot_circle(
double east,
double north,
double radius,
const std::string &label)
1905 std::ostringstream cmdstr;
1909 cmdstr <<
"set object circle at " + std::to_string(east) +
"," + std::to_string(north) +
" size " +
1910 std::to_string(radius) +
" back\n";
1914 double east_label = (std::cos(M_PI / 3.0) * radius) * 1.1 + east;
1915 double north_label = (std::sin(M_PI / 3.0) * radius) * 1.1 + north;
1916 cmdstr <<
"set label \"" + label +
"\" at first " + std::to_string(east_label) +
1917 ", " + std::to_string(north_label) +
" norotate back nopoint offset 0,0\n";
1921 cmdstr <<
"replot ";
1941 inline Gnuplot &Gnuplot::cmd(
const std::string &cmdstr)
1953 fputs((cmdstr +
"\n").c_str(), gnucmd);
1962 if (cmdstr.find(
"replot") != std::string::npos)
1966 if (cmdstr.find(
"splot") != std::string::npos)
1971 else if (cmdstr.find(
"plot") != std::string::npos)
1985 inline void Gnuplot::init()
1991 #if (defined(unix) || defined(__unix) || defined(__unix__)) && !defined(__APPLE__) 1992 if (std::getenv(
"DISPLAY") ==
nullptr)
2000 if (!Gnuplot::get_program_path())
2009 std::string tmp = Gnuplot::m_sGNUPlotPath +
"/" +
2010 Gnuplot::m_sGNUPlotFileName;
2017 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) 2018 gnucmd = _popen(tmp.c_str(),
"w");
2019 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__) 2020 gnucmd = popen(tmp.c_str(),
"w");
2047 inline bool Gnuplot::get_program_path()
2052 std::string tmp = Gnuplot::m_sGNUPlotPath +
"/" +
2053 Gnuplot::m_sGNUPlotFileName;
2055 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) 2056 if (Gnuplot::file_exists(tmp, 0))
2057 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__) 2058 if (Gnuplot::file_exists(tmp, 1))
2069 path = std::getenv(
"PATH");
2070 std::stringstream s;
2071 if (path !=
nullptr)
2079 std::string path_str;
2082 std::list<std::string> ls;
2085 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) 2086 stringtok(ls, path_str,
";");
2087 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__) 2088 stringtok(ls, path_str,
":");
2092 for (std::list<std::string>::const_iterator i = ls.begin();
2095 tmp = (*i) +
"/" + Gnuplot::m_sGNUPlotFileName;
2096 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) 2097 if (Gnuplot::file_exists(tmp, 0))
2098 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
2099 if (Gnuplot::file_exists(tmp, 1))
2102 Gnuplot::m_sGNUPlotPath = *i;
2107 tmp =
"Can't find gnuplot neither in PATH nor in \"" +
2108 Gnuplot::m_sGNUPlotPath +
"\"";
2109 Gnuplot::m_sGNUPlotPath =
"";
2118 inline bool Gnuplot::file_exists(
const std::string &filename,
int mode)
2120 if (mode < 0 || mode > 7)
2122 throw std::runtime_error(
2123 "In function \"Gnuplot::file_exists\": mode\ 2124 has to be an integer between 0 and 7");
2138 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) 2139 if (_access(filename.c_str(), mode) == 0)
2140 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__) 2141 if (access(filename.c_str(), mode) == 0)
2150 inline bool Gnuplot::file_available(
const std::string &filename)
2152 std::ostringstream except;
2153 if (Gnuplot::file_exists(filename, 0))
2155 if (!(Gnuplot::file_exists(filename, 4)))
2157 except <<
"No read permission for File \"" << filename <<
"\"";
2164 except <<
"File \"" << filename <<
"\" does not exist";
2176 inline std::string Gnuplot::create_tmpfile(std::ofstream &tmp)
2178 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) 2179 char name[] =
"gnuplotiXXXXXX";
2180 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__) 2181 char name[] =
"/tmp/gnuplotiXXXXXX";
2187 if (Gnuplot::tmpfile_num == GP_MAX_TMP_FILES - 1)
2189 std::ostringstream except;
2190 except <<
"Maximum number of temporary files reached (" 2191 << GP_MAX_TMP_FILES <<
"): cannot open more files\n";
2210 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) 2211 if (_mktemp(name) == NULL)
2212 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
2213 mode_t mask = umask(S_IXUSR | S_IRWXG | S_IRWXO);
2214 if (mkstemp(name) == -1)
2217 std::ostringstream except;
2218 except <<
"Cannot create temporary file \"" << name <<
"\"";
2219 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__) 2224 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__) 2230 std::ostringstream except;
2231 except <<
"Cannot create temporary file \"" << name <<
"\"";
2238 tmpfile_list.emplace_back(name);
2239 Gnuplot::tmpfile_num++;
2245 inline void Gnuplot::remove_tmpfiles()
2247 if (!(tmpfile_list).empty())
2249 for (
auto &i : tmpfile_list)
2251 if (
remove(i.c_str()) != 0)
2253 std::cout <<
"Problem closing files\n";
2257 Gnuplot::tmpfile_num -= tmpfile_list.size();
Gnuplot(const std::string &style="points")
set a style during construction
Gnuplot & unset_title()
Clears the title of a gnuplot session.
Gnuplot & replot(void)
replot repeats the last plot or splot command.
Gnuplot & operator<<(const std::string &cmdstr)
Sends a command to an active gnuplot session, identical to cmd()