source: trunk/lib/Gnuplot.cc @ 60

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

plotting vs date rather than revision

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