source: trunk/lib/Gnuplot.cc @ 70

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

Fixed too many files open problem.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.5 KB
Line 
1// $Id: Gnuplot.cc 70 2006-02-22 07:40:36Z jari $
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    // Peter is this working, really?
73    //command(std::string("set xdata time"));
74    //command("set format x '" + format + "'");
75    plot(y,date_,"replot"); 
76  }
77
78
79  void Gnuplot::tokenizer(const std::string& in,
80                          std::list<std::string>& tokens,
81                          const std::string& delimiters)
82  {
83    std::string::size_type previous_pos=in.find_first_not_of(delimiters,0);
84    std::string::size_type position=in.find_first_of(delimiters,previous_pos);
85    while ((std::string::npos!=position) || (std::string::npos!=previous_pos)) {
86      tokens.push_back(in.substr(previous_pos, position-previous_pos));
87      previous_pos=in.find_first_not_of(delimiters,position);
88      position=in.find_first_of(delimiters,previous_pos);
89    }
90  }
91
92
93  void Gnuplot::command(std::string cmdstr)
94  {
95    if (*(cmdstr.rbegin())!='\n')
96      cmdstr+='\n';
97    fputs(cmdstr.c_str(),pipe_);
98    fflush(pipe_);
99  }
100
101
102}} // end of namespace svnstat and namespace theplu
Note: See TracBrowser for help on using the repository browser.