source: trunk/lib/Gnuplot.cc @ 74

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

Improved web presentation. Y axises normalized, ticket ticket:15 resolved.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 1.9 KB
Line 
1// $Id: Gnuplot.cc 74 2006-03-07 15:46:59Z 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::command(const std::string& cmdstr)
59  {
60    fputs(cmdstr.c_str(),pipe_);
61    if (*(cmdstr.rbegin())!='\n')
62      fputc('\n',pipe_);
63    fflush(pipe_);
64  }
65
66
67  void Gnuplot::tokenizer(const std::string& in,
68                          std::list<std::string>& tokens,
69                          const std::string& delimiters)
70  {
71    std::string::size_type previous_pos=in.find_first_not_of(delimiters,0);
72    std::string::size_type position=in.find_first_of(delimiters,previous_pos);
73    while ((std::string::npos!=position) || (std::string::npos!=previous_pos)) {
74      tokens.push_back(in.substr(previous_pos, position-previous_pos));
75      previous_pos=in.find_first_not_of(delimiters,position);
76      position=in.find_first_of(delimiters,previous_pos);
77    }
78  }
79
80
81}} // end of namespace svnstat and namespace theplu
Note: See TracBrowser for help on using the repository browser.