source: trunk/lib/Gnuplot.cc @ 101

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

closes #28 #18 and added some lippstick to the node daughters table output

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.7 KB
RevLine 
[31]1// $Id: Gnuplot.cc 101 2006-06-22 09:00:06Z peter $
2
[84]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
[31]24#include "Gnuplot.h"
25
26#include <cstdio>
[70]27#include <string>
[31]28#include <unistd.h>
29
[73]30#include <iostream>
[31]31
[73]32
[31]33namespace theplu {
34namespace svnstat {
35
36
37  Gnuplot::Gnuplot(void)
[39]38    : linestyle_("steps"), pipe_(NULL)
[31]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
[60]78 
[101]79  int Gnuplot::command(const std::string& cmdstr)
[74]80  {
[101]81    int r = fputs(cmdstr.c_str(),pipe_);
[74]82    if (*(cmdstr.rbegin())!='\n')
83      fputc('\n',pipe_);
84    fflush(pipe_);
[101]85    return r;
[74]86  }
87
88
[31]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.