1 | // $Id: Gnuplot.cc 63 2006-01-19 23:07:36Z peter $ |
---|
2 | |
---|
3 | #include "Gnuplot.h" |
---|
4 | |
---|
5 | #include <cstdio> |
---|
6 | #include <unistd.h> |
---|
7 | #include <string> |
---|
8 | |
---|
9 | #include <iostream> |
---|
10 | |
---|
11 | namespace theplu { |
---|
12 | namespace svnstat { |
---|
13 | |
---|
14 | |
---|
15 | Gnuplot::Gnuplot(void) |
---|
16 | : linestyle_("steps"), pipe_(NULL) |
---|
17 | { |
---|
18 | acquire_program_path("gnuplot"); |
---|
19 | if (gnuplot_binary_.empty()) |
---|
20 | throw GnuplotException("Can't find gnuplot in your PATH"); |
---|
21 | |
---|
22 | pipe_=popen(gnuplot_binary_.c_str(),"w"); |
---|
23 | if (!pipe_) |
---|
24 | throw GnuplotException("Could'nt open connection to gnuplot"); |
---|
25 | } |
---|
26 | |
---|
27 | |
---|
28 | Gnuplot::~Gnuplot(void) |
---|
29 | { |
---|
30 | if (pclose(pipe_) == -1) |
---|
31 | throw GnuplotException("Problem closing communication to gnuplot"); |
---|
32 | for (std::list<std::string>::iterator i=tempfiles_.begin(); |
---|
33 | i!=tempfiles_.end(); i++) |
---|
34 | remove(i->c_str()); |
---|
35 | } |
---|
36 | |
---|
37 | |
---|
38 | void Gnuplot::acquire_program_path(const std::string& progname) |
---|
39 | { |
---|
40 | char* env_path=getenv("PATH"); // is there a need to free this memory? |
---|
41 | if (!env_path) |
---|
42 | throw GnuplotException("Environment variable PATH is not set"); |
---|
43 | |
---|
44 | std::list<std::string> paths; |
---|
45 | tokenizer(env_path,paths); |
---|
46 | for (std::list<std::string>::const_iterator i=paths.begin(); |
---|
47 | i!=paths.end(); ++i) { |
---|
48 | std::string tmp((*i) + '/' + progname); |
---|
49 | if (!access(tmp.c_str(),X_OK)) { |
---|
50 | gnuplot_binary_=tmp; |
---|
51 | break; |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | if (gnuplot_binary_.empty()) |
---|
56 | throw GnuplotException("Cannot find '" + progname + "' in PATH"); |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | void Gnuplot::date_plot(const std::vector<double>& y, |
---|
61 | const std::string& format) |
---|
62 | { |
---|
63 | command(std::string("set xdata time")); |
---|
64 | command("set timefmt '" + date_input_format_ + "'"); |
---|
65 | command("set format x '" + format + "'"); |
---|
66 | assert(date_[0].size()); |
---|
67 | assert(y.back()); |
---|
68 | plot(y,date_,"plot"); |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | void Gnuplot::date_replot(const std::vector<double>& y, |
---|
73 | const std::string& format) |
---|
74 | { |
---|
75 | // Peter is this working, really? |
---|
76 | //command(std::string("set xdata time")); |
---|
77 | //command("set format x '" + format + "'"); |
---|
78 | plot(y,date_,"replot"); |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | void Gnuplot::tokenizer(const std::string& in, |
---|
83 | std::list<std::string>& tokens, |
---|
84 | const std::string& delimiters) |
---|
85 | { |
---|
86 | std::string::size_type previous_pos=in.find_first_not_of(delimiters,0); |
---|
87 | std::string::size_type position=in.find_first_of(delimiters,previous_pos); |
---|
88 | while ((std::string::npos!=position) || (std::string::npos!=previous_pos)) { |
---|
89 | tokens.push_back(in.substr(previous_pos, position-previous_pos)); |
---|
90 | previous_pos=in.find_first_not_of(delimiters,position); |
---|
91 | position=in.find_first_of(delimiters,previous_pos); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | void Gnuplot::command(std::string cmdstr) |
---|
97 | { |
---|
98 | if (*(cmdstr.rbegin())!='\n') |
---|
99 | cmdstr+='\n'; |
---|
100 | fputs(cmdstr.c_str(),pipe_); |
---|
101 | fflush(pipe_); |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | }} // end of namespace svnstat and namespace theplu |
---|