source: branches/0.6-stable/lib/Gnuplot.cc

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

update copyright statements

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1// $Id: Gnuplot.cc 731 2008-12-15 19:03:04Z peter $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2007 Peter Johansson
6  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
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 2 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 this program; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23  02111-1307, USA.
24*/
25
26#include "Gnuplot.h"
27
28#include <cstdio>
29#include <cstdlib>
30#include <string>
31#include <unistd.h>
32
33#include <iostream>
34
35
36namespace theplu {
37namespace svndigest {
38
39
40  Gnuplot::Gnuplot(void)
41    : linestyle_("steps"), pipe_(NULL)
42  {
43    acquire_program_path("gnuplot");
44    if (gnuplot_binary_.empty())
45      throw GnuplotException("Can't find gnuplot in your PATH");
46
47    pipe_=popen(gnuplot_binary_.c_str(),"w");
48    if (!pipe_)
49      throw GnuplotException("Could not open connection to gnuplot");
50  }
51
52
53  Gnuplot::~Gnuplot(void)
54  {
55    if (pclose(pipe_) == -1)
56      throw GnuplotException("Problem closing communication to gnuplot");
57  }
58
59
60  void Gnuplot::acquire_program_path(const std::string& progname)
61  {
62    char* env_path=getenv("PATH");
63    if (!env_path)
64      throw GnuplotException("Environment variable PATH is not set");
65
66    std::list<std::string> paths;
67    tokenizer(env_path,paths);
68    for (std::list<std::string>::const_iterator i=paths.begin();
69         i!=paths.end(); ++i) {
70      std::string tmp((*i) + '/' + progname);
71      if (!access(tmp.c_str(),X_OK)) {
72        gnuplot_binary_=tmp;
73        break;
74      }
75    }
76
77    if (gnuplot_binary_.empty())
78      throw GnuplotException("Cannot find '" + progname + "' in PATH");
79  }
80
81 
82  int Gnuplot::command(const std::string& cmdstr)
83  {
84    if (fputs(cmdstr.c_str(),pipe_)==EOF)
85      return EOF;
86    if ((*(cmdstr.rbegin())!='\n') && (fputc('\n',pipe_)==EOF))
87      return EOF;
88    return fflush(pipe_);
89  }
90
91
92  void Gnuplot::tokenizer(const std::string& in,
93                          std::list<std::string>& tokens,
94                          const std::string& delimiters)
95  {
96    std::string::size_type previous_pos=in.find_first_not_of(delimiters,0);
97    std::string::size_type position=in.find_first_of(delimiters,previous_pos);
98    while ((std::string::npos!=position) || (std::string::npos!=previous_pos)) {
99      tokens.push_back(in.substr(previous_pos, position-previous_pos));
100      previous_pos=in.find_first_not_of(delimiters,position);
101      position=in.find_first_of(delimiters,previous_pos);
102    }
103  }
104
105
106}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.