source: trunk/lib/Gnuplot.cc @ 687

Last change on this file since 687 was 687, checked in by Peter Johansson, 13 years ago

Merged patch release 0.6.6 to trunk. Delta 0.6.6 - 0.6.5

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1// $Id: Gnuplot.cc 687 2008-08-04 19:37:10Z peter $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2007 Peter Johansson
6
7  This file is part of svndigest, http://dev.thep.lu.se/svndigest
8
9  svndigest is free software; you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13
14  svndigest is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  02111-1307, USA.
23*/
24
25#include "Gnuplot.h"
26
27#include <cstdio>
28#include <string>
29#include <unistd.h>
30
31#include <iostream>
32
33
34namespace theplu {
35namespace svndigest {
36
37
38  Gnuplot::Gnuplot(void)
39    : linestyle_("steps"), pipe_(NULL)
40  {
41    acquire_program_path("gnuplot");
42    if (gnuplot_binary_.empty())
43      throw GnuplotException("Can't find gnuplot in your PATH");
44
45    pipe_=popen(gnuplot_binary_.c_str(),"w");
46    if (!pipe_)
47      throw GnuplotException("Could not open connection to gnuplot");
48  }
49
50
51  Gnuplot::~Gnuplot(void)
52  {
53    if (pclose(pipe_) == -1)
54      throw GnuplotException("Problem closing communication to gnuplot");
55  }
56
57
58  void Gnuplot::acquire_program_path(const std::string& progname)
59  {
60    char* env_path=getenv("PATH");
61    if (!env_path)
62      throw GnuplotException("Environment variable PATH is not set");
63
64    std::list<std::string> paths;
65    tokenizer(env_path,paths);
66    for (std::list<std::string>::const_iterator i=paths.begin();
67         i!=paths.end(); ++i) {
68      std::string tmp((*i) + '/' + progname);
69      if (!access(tmp.c_str(),X_OK)) {
70        gnuplot_binary_=tmp;
71        break;
72      }
73    }
74
75    if (gnuplot_binary_.empty())
76      throw GnuplotException("Cannot find '" + progname + "' in PATH");
77  }
78
79 
80  int Gnuplot::command(const std::string& cmdstr)
81  {
82    if (fputs(cmdstr.c_str(),pipe_)==EOF)
83      return EOF;
84    if ((*(cmdstr.rbegin())!='\n') && (fputc('\n',pipe_)==EOF))
85      return EOF;
86    return fflush(pipe_);
87  }
88
89
90  void Gnuplot::tokenizer(const std::string& in,
91                          std::list<std::string>& tokens,
92                          const std::string& delimiters)
93  {
94    std::string::size_type previous_pos=in.find_first_not_of(delimiters,0);
95    std::string::size_type position=in.find_first_of(delimiters,previous_pos);
96    while ((std::string::npos!=position) || (std::string::npos!=previous_pos)) {
97      tokens.push_back(in.substr(previous_pos, position-previous_pos));
98      previous_pos=in.find_first_not_of(delimiters,position);
99      position=in.find_first_of(delimiters,previous_pos);
100    }
101  }
102
103
104}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.