source: trunk/lib/Gnuplot.cc @ 73

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

Divided Gnuplot class into a basic gnuplot communication class
and a front end class specialized for svnstat.
Added option to plot stats against time or revision.
Removed gnuplot usage of temporary files.

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