source: trunk/lib/Gnuplot.cc @ 693

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

Fixes #339. Change to GPLv3.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1// $Id: Gnuplot.cc 693 2008-09-11 20:42:56Z jari $
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 3 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 svndigest. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "Gnuplot.h"
24
25#include <cstdio>
26#include <string>
27#include <unistd.h>
28
29#include <iostream>
30
31
32namespace theplu {
33namespace svndigest {
34
35
36  Gnuplot::Gnuplot(void)
37    : linestyle_("steps"), pipe_(NULL)
38  {
39    acquire_program_path("gnuplot");
40    if (gnuplot_binary_.empty())
41      throw GnuplotException("Can't find gnuplot in your PATH");
42
43    pipe_=popen(gnuplot_binary_.c_str(),"w");
44    if (!pipe_)
45      throw GnuplotException("Could not open connection to gnuplot");
46  }
47
48
49  Gnuplot::~Gnuplot(void)
50  {
51    if (pclose(pipe_) == -1)
52      throw GnuplotException("Problem closing communication to gnuplot");
53  }
54
55
56  void Gnuplot::acquire_program_path(const std::string& progname)
57  {
58    char* env_path=getenv("PATH");
59    if (!env_path)
60      throw GnuplotException("Environment variable PATH is not set");
61
62    std::list<std::string> paths;
63    tokenizer(env_path,paths);
64    for (std::list<std::string>::const_iterator i=paths.begin();
65         i!=paths.end(); ++i) {
66      std::string tmp((*i) + '/' + progname);
67      if (!access(tmp.c_str(),X_OK)) {
68        gnuplot_binary_=tmp;
69        break;
70      }
71    }
72
73    if (gnuplot_binary_.empty())
74      throw GnuplotException("Cannot find '" + progname + "' in PATH");
75  }
76
77 
78  int Gnuplot::command(const std::string& cmdstr)
79  {
80    if (fputs(cmdstr.c_str(),pipe_)==EOF)
81      return EOF;
82    if ((*(cmdstr.rbegin())!='\n') && (fputc('\n',pipe_)==EOF))
83      return EOF;
84    return fflush(pipe_);
85  }
86
87
88  void Gnuplot::tokenizer(const std::string& in,
89                          std::list<std::string>& tokens,
90                          const std::string& delimiters)
91  {
92    std::string::size_type previous_pos=in.find_first_not_of(delimiters,0);
93    std::string::size_type position=in.find_first_of(delimiters,previous_pos);
94    while ((std::string::npos!=position) || (std::string::npos!=previous_pos)) {
95      tokens.push_back(in.substr(previous_pos, position-previous_pos));
96      previous_pos=in.find_first_not_of(delimiters,position);
97      position=in.find_first_of(delimiters,previous_pos);
98    }
99  }
100
101
102}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.