source: trunk/lib/Gnuplot.cc @ 111

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

Reverted Gnuplot plot to return nothing (void). fputs have different return values on linux and BSD.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1// $Id: Gnuplot.cc 111 2006-06-28 15:18:42Z jari $
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  void Gnuplot::command(const std::string& cmdstr)
80  {
81    fputs(cmdstr.c_str(),pipe_);
82    if (*(cmdstr.rbegin())!='\n')
83      fputc('\n',pipe_);
84    fflush(pipe_);
85  }
86
87
88  void Gnuplot::tokenizer(const std::string& in,
89                          std::list<std::string>& tokens,
90                          const std::string& delimiters)
91  {
92    std::string::size_type previous_pos=in.find_first_not_of(delimiters,0);
93    std::string::size_type position=in.find_first_of(delimiters,previous_pos);
94    while ((std::string::npos!=position) || (std::string::npos!=previous_pos)) {
95      tokens.push_back(in.substr(previous_pos, position-previous_pos));
96      previous_pos=in.find_first_not_of(delimiters,position);
97      position=in.find_first_of(delimiters,previous_pos);
98    }
99  }
100
101
102}} // end of namespace svnstat and namespace theplu
Note: See TracBrowser for help on using the repository browser.