source: trunk/lib/Gnuplot.cc @ 107

Last change on this file since 107 was 107, checked in by Peter Johansson, 15 years ago

Gnuplot functions returning int and utilizing that in the test. However the warning sent from Gnuplot ref #37 is not caught since there is no problem with the pipe there.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1// $Id: Gnuplot.cc 107 2006-06-28 09:12:38Z peter $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
5
6  This file is part of svnstat, http://lev.thep.lu.se/trac/svnstat
7
8  svnstat 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  svnstat 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 svnstat {
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'nt 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");  // is there a need to free this memory?
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    std::cout << cmdstr << std::endl;
82    int r = fputs(cmdstr.c_str(),pipe_);
83    if (!r && *(cmdstr.rbegin())!='\n')
84      r=fputc('\n',pipe_);
85    return (r ? r : 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 svnstat and namespace theplu
Note: See TracBrowser for help on using the repository browser.