source: trunk/lib/Gnuplot.h @ 111

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

Reverted Gnuplot plot to return nothing (void). fputs have different return values on linux and BSD.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.5 KB
RevLine 
[31]1// $Id: Gnuplot.h 111 2006-06-28 15:18:42Z jari $
2
[84]3/*
4  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
5
6  This file is part of svnstat, http://lev.thep.lu.se/trac/svnstat
7
8  svnstat is free software; you can redistribute it and/or modify it
9  under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  svnstat is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
[31]24#ifndef _theplu_svnstat_gnuplot_
25#define _theplu_svnstat_gnuplot_
26
[65]27#include <cassert>
[31]28#include <cstdio>
29#include <list>
30#include <stdexcept>
[73]31#include <sstream>
[31]32#include <string>
33#include <vector>
34
[63]35
[107]36#include <iostream>
37
[31]38namespace theplu {
39namespace svnstat {
40
41  ///
42  /// If something goes wrong in the use of the Gnuplot class, a
43  /// GnuplotException is thrown.
44  ///
45  struct GnuplotException : public std::runtime_error
[39]46  { inline GnuplotException(const std::string& msg) : runtime_error(msg) {} };
[31]47
48  ///
49  /// The Gnuplot class creates a pipe to 'gnuplot' and facilitates
[73]50  /// communication with the gnuplot binary.
[31]51  ///
52  class Gnuplot
53  {
54  public:
55    ///
56    /// This constructor sets up the pipe to the first gnuplot
57    /// executable found in the PATH environment variable. The PATH
58    /// variable must exist.
59    ///
60    /// @throw Throws a GnuplotException if a no PATH variable is
61    /// found, if the gnuplot binary cannot be found, or if a pipe
62    /// cannot to the gnuplot binary cannot be established.
63    ///
64    Gnuplot(void);
65
66    ///
[73]67    /// The destructor, closes the pipe to the gnuplot binary.
[31]68    ///
[73]69    virtual ~Gnuplot(void);
[31]70
71    ///
72    /// Send arbitrary commands to Gnuplot.
73    ///
[107]74    /// @return 0 if fputs, fputc, and fflush are succesful
75    ///
[111]76    void command(const std::string&);
[31]77
78    ///
[60]79    /// Set the \a style of the line in the subsequent plot or
80    /// replot calls. The setting applies until this function is
[31]81    /// called again.
82    ///
83    /// @note The \a style is not checked to be valid.
84    ///
[39]85    inline void linestyle(const std::string& style) { linestyle_=style; }
[31]86
87    ///
[60]88    /// Set the \a title of the line in the subsequent plot or
89    /// replot calls. The setting applies until this function is
[31]90    /// called again.
91    ///
[39]92    inline void linetitle(const std::string& title) { linetitle_=title; }
[31]93
94    ///
95    /// Plot the data \a y as a function of \a x using the Gnuplot
[36]96    /// 'plot' command. The \a x vector can be omitted as in normal
97    /// Gnuplot usage.
[31]98    ///
[107]99    /// @return whatever command() returns
100    ///
[73]101    template <class T1, class T2>
[111]102    void plot(const std::vector<T1>& y, const std::vector<T2>& x)
103    { plot(y,x,"plot"); }
[31]104
[73]105    template <class T1>
[111]106    void plot(const std::vector<T1>& y,
[73]107              const std::vector<T1>& x=std::vector<T1>())
[111]108    { plot(y,x,"plot"); }
[73]109
[31]110    ///
[36]111    /// Plot the data \a y as a function of \a x using the Gnuplot
112    /// 'replot' command. The \a x vector can be omitted as in normal
113    /// Gnuplot usage.
[31]114    ///
[107]115    /// @return whatever command() returns
116    ///
[73]117    template <class T1, class T2>
[111]118    void replot(const std::vector<T1>& y, const std::vector<T2>& x)
119    { plot(y,x,"replot"); }
[31]120
[73]121    template <class T1>
[111]122    void replot(const std::vector<T1>& y,
[73]123              const std::vector<T1>& x=std::vector<T1>())
[111]124    { plot(y,x,"replot"); }
[60]125
[31]126  private:
127    ///
128    /// Copy constructor, not implemented.
129    ///
130    Gnuplot(const Gnuplot&);
131
132    void acquire_program_path(const std::string&);
[36]133
134    ///
135    /// @param \a plotcmd must be "plot" or "replot".
136    ///
[60]137    template <class T1, class T2>
[111]138    void plot(const std::vector<T1>& y, const std::vector<T2>& x,
[60]139              const std::string& plotcmd)
140    {
141      assert(x.size()==y.size() || x.empty());
142      assert(plotcmd=="plot" || plotcmd=="replot");
[73]143
144      std::ostringstream cmdstring;
145      cmdstring << plotcmd << " '-' u 1:2 title '" << linetitle_ << "' with "
146                << linestyle_ << '\n';
147      for (size_t i=0; i<y.size(); ++i) {
148        if (!x.empty())
149          cmdstring << x[i] << '\t';
[60]150        else
[73]151          cmdstring << i << '\t';
152        cmdstring << y[i] << '\n';
153      }
154      cmdstring << "e\n";
[70]155
[111]156      command(cmdstring.str());
[60]157    }
[36]158
[31]159    void tokenizer(const std::string& in, std::list<std::string>& tokens,
160                   const std::string& delimiters = ":");
161
162    std::string gnuplot_binary_;
163    std::string linestyle_;
164    std::string linetitle_;
165    FILE* pipe_;
166};
167
168}} // end of namespace svnstat and namespace theplu
169
170#endif
Note: See TracBrowser for help on using the repository browser.