source: trunk/lib/Gnuplot.cc @ 791

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

removed usage of envar HOME in Gnuplot.cc

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.6 KB
Line 
1// $Id: Gnuplot.cc 743 2008-12-19 23:04:54Z 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 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 <config.h>
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    std::string tmp = GNUPLOT_PATH;
63    if (tmp=="no")
64      throw GnuplotException("no gnuplot binary available");
65    if (!access(tmp.c_str(),X_OK)) {
66      gnuplot_binary_=tmp;
67    }
68
69    if (gnuplot_binary_.empty())
70      throw GnuplotException("Cannot find '" + tmp);
71  }
72
73 
74  int Gnuplot::command(const std::string& cmdstr)
75  {
76    if (fputs(cmdstr.c_str(),pipe_)==EOF)
77      return EOF;
78    if ((*(cmdstr.rbegin())!='\n') && (fputc('\n',pipe_)==EOF))
79      return EOF;
80    return fflush(pipe_);
81  }
82
83
84  void Gnuplot::tokenizer(const std::string& in,
85                          std::list<std::string>& tokens,
86                          const std::string& delimiters)
87  {
88    std::string::size_type previous_pos=in.find_first_not_of(delimiters,0);
89    std::string::size_type position=in.find_first_of(delimiters,previous_pos);
90    while ((std::string::npos!=position) || (std::string::npos!=previous_pos)) {
91      tokens.push_back(in.substr(previous_pos, position-previous_pos));
92      previous_pos=in.find_first_not_of(delimiters,position);
93      position=in.find_first_of(delimiters,previous_pos);
94    }
95  }
96
97
98}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.