source: trunk/lib/Gnuplot.h @ 31

Last change on this file since 31 was 31, checked in by Jari Häkkinen, 16 years ago

Reimplemented the gnuplot pipe implementation.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1// $Id: Gnuplot.h 31 2006-01-11 23:45:03Z 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
12namespace theplu {
13namespace 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. If \a replot is set to true, then the Gnuplot
75    /// 'replot' command is used to create the plot.
76    ///
77    void plot_xy(const std::vector<double>& x, const std::vector<double>& y,
78                 bool replot=false);
79
80    ///
81    /// Plot the data in \a y using the Gnuplot 'plot' command. If \a
82    /// replot is set to true, then the Gnuplot 'replot' command is
83    /// used to create the plot.
84    ///
85    void plot_y(const std::vector<double>& y, bool replot=false);
86
87  private:
88    ///
89    /// Copy constructor, not implemented.
90    ///
91    Gnuplot(const Gnuplot&);
92
93    void acquire_program_path(const std::string&);
94    void tokenizer(const std::string& in, std::list<std::string>& tokens,
95                   const std::string& delimiters = ":");
96
97    // need to keep track of created files since the gnuplot command
98    // is not executed until pclose on MacOSX, shouldn't the fflush
99    // fix this in the command() member function?
100    std::list<std::string> tempfiles_;
101    std::string gnuplot_binary_;
102    std::string linestyle_;
103    std::string linetitle_;
104    FILE* pipe_;
105};
106
107}} // end of namespace svnstat and namespace theplu
108
109#endif
Note: See TracBrowser for help on using the repository browser.