1 | // $Id: Gnuplot.h 36 2006-01-13 01:05:53Z jari $ |
---|
2 | |
---|
3 | #ifndef _theplu_svnstat_gnuplot_ |
---|
4 | #define _theplu_svnstat_gnuplot_ |
---|
5 | |
---|
6 | #include <cstdio> |
---|
7 | #include <list> |
---|
8 | #include <stdexcept> |
---|
9 | #include <string> |
---|
10 | #include <vector> |
---|
11 | |
---|
12 | namespace theplu { |
---|
13 | namespace svnstat { |
---|
14 | |
---|
15 | /// |
---|
16 | /// If something goes wrong in the use of the Gnuplot class, a |
---|
17 | /// GnuplotException is thrown. |
---|
18 | /// |
---|
19 | struct GnuplotException : public std::runtime_error |
---|
20 | { inline GnuplotException(const std::string &msg) : runtime_error(msg) {} }; |
---|
21 | |
---|
22 | /// |
---|
23 | /// The Gnuplot class creates a pipe to 'gnuplot' and facilitates |
---|
24 | /// communication with the binary. |
---|
25 | /// |
---|
26 | /// Temporary files are created in directory /tmp. These temporaru |
---|
27 | /// files are removed on a normal program exit. |
---|
28 | /// |
---|
29 | class Gnuplot |
---|
30 | { |
---|
31 | public: |
---|
32 | /// |
---|
33 | /// This constructor sets up the pipe to the first gnuplot |
---|
34 | /// executable found in the PATH environment variable. The PATH |
---|
35 | /// variable must exist. |
---|
36 | /// |
---|
37 | /// @throw Throws a GnuplotException if a no PATH variable is |
---|
38 | /// found, if the gnuplot binary cannot be found, or if a pipe |
---|
39 | /// cannot to the gnuplot binary cannot be established. |
---|
40 | /// |
---|
41 | Gnuplot(void); |
---|
42 | |
---|
43 | /// |
---|
44 | /// The destructor deletes all temporary files created in the |
---|
45 | /// calls to plot_xy and plot_x. If the program execution fails to |
---|
46 | /// reach this destructor the temporary files will not be removed |
---|
47 | /// from the storage area. |
---|
48 | /// |
---|
49 | ~Gnuplot(void); |
---|
50 | |
---|
51 | /// |
---|
52 | /// Send arbitrary commands to Gnuplot. |
---|
53 | /// |
---|
54 | void command(std::string); |
---|
55 | |
---|
56 | /// |
---|
57 | /// Set the \a style of the line in the subsequent plot_xy or |
---|
58 | /// plot_y calls. The setting applies until this function is |
---|
59 | /// called again. |
---|
60 | /// |
---|
61 | /// @note The \a style is not checked to be valid. |
---|
62 | /// |
---|
63 | inline void linestyle(std::string style) { linestyle_=style; } |
---|
64 | |
---|
65 | /// |
---|
66 | /// Set the \a title of the line in the subsequent plot_xy or |
---|
67 | /// plot_y calls. The setting applies until this function is |
---|
68 | /// called again. |
---|
69 | /// |
---|
70 | inline void linetitle(std::string title) { linetitle_=title; } |
---|
71 | |
---|
72 | /// |
---|
73 | /// Plot the data \a y as a function of \a x using the Gnuplot |
---|
74 | /// 'plot' command. The \a x vector can be omitted as in normal |
---|
75 | /// Gnuplot usage. |
---|
76 | /// |
---|
77 | inline void |
---|
78 | plot(const std::vector<double>& y, |
---|
79 | const std::vector<double>& x=std::vector<double>()) |
---|
80 | { plot(y,x,"plot"); } |
---|
81 | |
---|
82 | /// |
---|
83 | /// Plot the data \a y as a function of \a x using the Gnuplot |
---|
84 | /// 'replot' command. The \a x vector can be omitted as in normal |
---|
85 | /// Gnuplot usage. |
---|
86 | /// |
---|
87 | inline void |
---|
88 | replot(const std::vector<double>& y, |
---|
89 | const std::vector<double>& x=std::vector<double>()) |
---|
90 | { plot(y,x,"replot"); } |
---|
91 | |
---|
92 | private: |
---|
93 | /// |
---|
94 | /// Copy constructor, not implemented. |
---|
95 | /// |
---|
96 | Gnuplot(const Gnuplot&); |
---|
97 | |
---|
98 | void acquire_program_path(const std::string&); |
---|
99 | |
---|
100 | /// |
---|
101 | /// @param \a plotcmd must be "plot" or "replot". |
---|
102 | /// |
---|
103 | void plot(const std::vector<double>& y, const std::vector<double>& x, |
---|
104 | const std::string& plotcmd); |
---|
105 | |
---|
106 | void tokenizer(const std::string& in, std::list<std::string>& tokens, |
---|
107 | const std::string& delimiters = ":"); |
---|
108 | |
---|
109 | // need to keep track of created files since the gnuplot command |
---|
110 | // is not executed until pclose on MacOSX, shouldn't the fflush |
---|
111 | // fix this in the command() member function? |
---|
112 | std::list<std::string> tempfiles_; |
---|
113 | std::string gnuplot_binary_; |
---|
114 | std::string linestyle_; |
---|
115 | std::string linetitle_; |
---|
116 | FILE* pipe_; |
---|
117 | }; |
---|
118 | |
---|
119 | }} // end of namespace svnstat and namespace theplu |
---|
120 | |
---|
121 | #endif |
---|