source: trunk/lib/Gnuplot.cc @ 71

Last change on this file since 71 was 71, checked in by Peter Johansson, 16 years ago

class goes to typename, hi hi

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.4 KB
Line 
1// $Id: Gnuplot.cc 71 2006-02-23 09:25:41Z peter $
2
3#include "Gnuplot.h"
4
5#include <cstdio>
6#include <string>
7#include <unistd.h>
8
9
10namespace theplu {
11namespace svnstat {
12
13
14  Gnuplot::Gnuplot(void)
15    : linestyle_("steps"), pipe_(NULL)
16  {
17    acquire_program_path("gnuplot");
18    if (gnuplot_binary_.empty())
19      throw GnuplotException("Can't find gnuplot in your PATH");
20
21    pipe_=popen(gnuplot_binary_.c_str(),"w");
22    if (!pipe_)
23      throw GnuplotException("Could'nt open connection to gnuplot");
24  }
25
26
27  Gnuplot::~Gnuplot(void)
28  {
29    if (pclose(pipe_) == -1)
30      throw GnuplotException("Problem closing communication to gnuplot");
31    for (std::list<std::string>::iterator i=tempfiles_.begin();
32         i!=tempfiles_.end(); i++)
33      remove(i->c_str());
34  }
35
36
37  void Gnuplot::acquire_program_path(const std::string& progname)
38  {
39    char* env_path=getenv("PATH");  // is there a need to free this memory?
40    if (!env_path)
41      throw GnuplotException("Environment variable PATH is not set");
42
43    std::list<std::string> paths;
44    tokenizer(env_path,paths);
45    for (std::list<std::string>::const_iterator i=paths.begin();
46         i!=paths.end(); ++i) {
47      std::string tmp((*i) + '/' + progname);
48      if (!access(tmp.c_str(),X_OK)) {
49        gnuplot_binary_=tmp;
50        break;
51      }
52    }
53
54    if (gnuplot_binary_.empty())
55      throw GnuplotException("Cannot find '" + progname + "' in PATH");
56  }
57
58 
59  void Gnuplot::date_plot(const std::vector<double>& y, 
60                          const std::string& format)
61  { 
62    command(std::string("set xdata time")); 
63    command("set timefmt '" + date_input_format_ + "'");
64    command("set format x '" + format + "'");
65    plot(y,date_,"plot"); 
66  }
67
68
69  void Gnuplot::date_replot(const std::vector<double>& y, 
70                            const std::string& format)
71  { 
72    plot(y,date_,"replot"); 
73  }
74
75
76  void Gnuplot::tokenizer(const std::string& in,
77                          std::list<std::string>& tokens,
78                          const std::string& delimiters)
79  {
80    std::string::size_type previous_pos=in.find_first_not_of(delimiters,0);
81    std::string::size_type position=in.find_first_of(delimiters,previous_pos);
82    while ((std::string::npos!=position) || (std::string::npos!=previous_pos)) {
83      tokens.push_back(in.substr(previous_pos, position-previous_pos));
84      previous_pos=in.find_first_not_of(delimiters,position);
85      position=in.find_first_of(delimiters,previous_pos);
86    }
87  }
88
89
90  void Gnuplot::command(std::string cmdstr)
91  {
92    if (*(cmdstr.rbegin())!='\n')
93      cmdstr+='\n';
94    fputs(cmdstr.c_str(),pipe_);
95    fflush(pipe_);
96  }
97
98
99}} // end of namespace svnstat and namespace theplu
Note: See TracBrowser for help on using the repository browser.