source: trunk/lib/Gnuplot.cc @ 727

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

Changes needed to cleanly compile on g++ 4.3 on SuSE 11 x64.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1// $Id: Gnuplot.cc 727 2008-12-11 15:02:38Z jari $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2007 Peter Johansson
6  Copyright (C) 2008 Jari Häkkinen
7
8  This file is part of svndigest, http://dev.thep.lu.se/svndigest
9
10  svndigest is free software; you can redistribute it and/or modify it
11  under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 3 of the License, or
13  (at your option) any later version.
14
15  svndigest is distributed in the hope that it will be useful, but
16  WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
22*/
23
24#include "Gnuplot.h"
25
26#include <cstdio>
27#include <cstdlib>
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.