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

Last change on this file since 430 was 430, checked in by Peter Johansson, 16 years ago

changing lev.thep.lu.se to trac.thep.lu.se

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