Repository navigation
Expand file tree
/
Copy pathgnuplot_i.cc
More file actions
1145 lines (938 loc) · 30.3 KB
/
Copy pathgnuplot_i.cc
File metadata and controls
1145 lines (938 loc) · 30.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* gnuplot_i.cc
*
* Created on: 09/ott/2009
* Author: mallo
*/
#include "gnuplot_i.hpp"
//------------------------------------------------------------------------------
//
// initialize static data
//
int Gnuplot::tmpfile_num = 0;
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
std::string Gnuplot::m_sGNUPlotFileName = "pgnuplot.exe";
std::string Gnuplot::m_sGNUPlotPath = "C:/program files/gnuplot/bin/";
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
std::string Gnuplot::m_sGNUPlotFileName = "gnuplot";
std::string Gnuplot::m_sGNUPlotPath = "/usr/local/bin/";
#endif
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
std::string Gnuplot::terminal_std = "windows";
#elif ( defined(unix) || defined(__unix) || defined(__unix__) ) && !defined(__APPLE__)
std::string Gnuplot::terminal_std = "x11";
#elif defined(__APPLE__)
std::string Gnuplot::terminal_std = "aqua";
#endif
//------------------------------------------------------------------------------
//
// define static member function: set Gnuplot path manual
// for windows: path with slash '/' not backslash '\'
//
bool Gnuplot::set_GNUPlotPath(const std::string &path)
{
std::string tmp = path + "/" + Gnuplot::m_sGNUPlotFileName;
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
if ( Gnuplot::file_exists(tmp,0) ) // check existence
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
if ( Gnuplot::file_exists(tmp,1) ) // check existence and execution permission
#endif
{
Gnuplot::m_sGNUPlotPath = path;
return true;
}
else
{
Gnuplot::m_sGNUPlotPath.clear();
return false;
}
}
//------------------------------------------------------------------------------
//
// define static member function: set standart terminal, used by showonscreen
// defaults: Windows - win, Linux - x11, Mac - aqua
//
void Gnuplot::set_terminal_std(const std::string &type)
{
#if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
if (type.find("x11") != std::string::npos && getenv("DISPLAY") == NULL)
{
throw GnuplotException("Can't find DISPLAY variable");
}
#endif
Gnuplot::terminal_std = type;
return;
}
//------------------------------------------------------------------------------
//
// A string tokenizer taken from http://www.sunsite.ualberta.ca/Documentation/
// /Gnu/libstdc++-2.90.8/html/21_strings/stringtok_std_h.txt
//
template <typename Container>
void stringtok (Container &container,
std::string const &in,
const char * const delimiters = " \t\n")
{
const std::string::size_type len = in.length();
std::string::size_type i = 0;
while ( i < len )
{
// eat leading whitespace
i = in.find_first_not_of (delimiters, i);
if (i == std::string::npos)
return; // nothing left but white space
// find the end of the token
std::string::size_type j = in.find_first_of (delimiters, i);
// push token
if (j == std::string::npos)
{
container.push_back (in.substr(i));
return;
}
else
container.push_back (in.substr(i, j-i));
// set up for next loop
i = j + 1;
}
return;
}
//------------------------------------------------------------------------------
//
// Destructor: needed to delete temporary files
//
Gnuplot::~Gnuplot()
{
// remove_tmpfiles();
// A stream opened by popen() should be closed by pclose()
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
if (_pclose(gnucmd) == -1)
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
if (pclose(gnucmd) == -1)
#endif
throw GnuplotException("Problem closing communication to gnuplot");
}
//------------------------------------------------------------------------------
//
// Resets a gnuplot session (next plot will erase previous ones)
//
Gnuplot& Gnuplot::reset_plot()
{
// remove_tmpfiles();
nplots = 0;
return *this;
}
//------------------------------------------------------------------------------
//
// resets a gnuplot session and sets all varibles to default
//
Gnuplot& Gnuplot::reset_all()
{
// remove_tmpfiles();
nplots = 0;
cmd("reset");
cmd("clear");
pstyle = "points";
smooth = "";
showonscreen();
return *this;
}
//------------------------------------------------------------------------------
//
// Change the plotting style of a gnuplot session
//
Gnuplot& Gnuplot::set_style(const std::string &stylestr)
{
if (stylestr.find("lines") == std::string::npos &&
stylestr.find("points") == std::string::npos &&
stylestr.find("linespoints") == std::string::npos &&
stylestr.find("impulses") == std::string::npos &&
stylestr.find("dots") == std::string::npos &&
stylestr.find("steps") == std::string::npos &&
stylestr.find("fsteps") == std::string::npos &&
stylestr.find("histeps") == std::string::npos &&
stylestr.find("boxes") == std::string::npos && // 1-4 columns of data are required
stylestr.find("filledcurves") == std::string::npos &&
stylestr.find("histograms") == std::string::npos ) //only for one data column
// stylestr.find("labels") == std::string::npos && // 3 columns of data are required
// stylestr.find("xerrorbars") == std::string::npos && // 3-4 columns of data are required
// stylestr.find("xerrorlines") == std::string::npos && // 3-4 columns of data are required
// stylestr.find("errorbars") == std::string::npos && // 3-4 columns of data are required
// stylestr.find("errorlines") == std::string::npos && // 3-4 columns of data are required
// stylestr.find("yerrorbars") == std::string::npos && // 3-4 columns of data are required
// stylestr.find("yerrorlines") == std::string::npos && // 3-4 columns of data are required
// stylestr.find("boxerrorbars") == std::string::npos && // 3-5 columns of data are required
// stylestr.find("xyerrorbars") == std::string::npos && // 4,6,7 columns of data are required
// stylestr.find("xyerrorlines") == std::string::npos && // 4,6,7 columns of data are required
// stylestr.find("boxxyerrorbars") == std::string::npos && // 4,6,7 columns of data are required
// stylestr.find("financebars") == std::string::npos && // 5 columns of data are required
// stylestr.find("candlesticks") == std::string::npos && // 5 columns of data are required
// stylestr.find("vectors") == std::string::npos &&
// stylestr.find("image") == std::string::npos &&
// stylestr.find("rgbimage") == std::string::npos &&
// stylestr.find("pm3d") == std::string::npos )
{
pstyle = std::string("points");
}
else
{
pstyle = stylestr;
}
return *this;
}
//------------------------------------------------------------------------------
//
// smooth: interpolation and approximation of data
//
Gnuplot& Gnuplot::set_smooth(const std::string &stylestr)
{
if (stylestr.find("unique") == std::string::npos &&
stylestr.find("frequency") == std::string::npos &&
stylestr.find("csplines") == std::string::npos &&
stylestr.find("acsplines") == std::string::npos &&
stylestr.find("bezier") == std::string::npos &&
stylestr.find("sbezier") == std::string::npos )
{
smooth = "";
}
else
{
smooth = stylestr;
}
return *this;
}
//------------------------------------------------------------------------------
//
// sets terminal type to windows / x11
//
Gnuplot& Gnuplot::showonscreen()
{
cmd("set output");
cmd("set terminal " + Gnuplot::terminal_std);
return *this;
}
//------------------------------------------------------------------------------
//
// saves a gnuplot session to a postscript file
//
Gnuplot& Gnuplot::savetops(const std::string &filename)
{
cmd("set terminal postscript color");
std::ostringstream cmdstr;
cmdstr << "set output \"" << filename << ".ps\"";
cmd(cmdstr.str());
return *this;
}
//------------------------------------------------------------------------------
//
// Switches legend on
//
Gnuplot& Gnuplot::set_legend(const std::string &position)
{
std::ostringstream cmdstr;
cmdstr << "set key " << position;
cmd(cmdstr.str());
return *this;
}
//------------------------------------------------------------------------------
//
// turns on log scaling for the x axis
//
Gnuplot& Gnuplot::set_xlogscale(const double base)
{
std::ostringstream cmdstr;
cmdstr << "set logscale x " << base;
cmd(cmdstr.str());
return *this;
}
//------------------------------------------------------------------------------
//
// turns on log scaling for the y axis
//
Gnuplot& Gnuplot::set_ylogscale(const double base)
{
std::ostringstream cmdstr;
cmdstr << "set logscale y " << base;
cmd(cmdstr.str());
return *this;
}
//------------------------------------------------------------------------------
//
// turns on log scaling for the z axis
//
Gnuplot& Gnuplot::set_zlogscale(const double base)
{
std::ostringstream cmdstr;
cmdstr << "set logscale z " << base;
cmd(cmdstr.str());
return *this;
}
//------------------------------------------------------------------------------
//
// scales the size of the points used in plots
//
Gnuplot& Gnuplot::set_pointsize(const double pointsize)
{
std::ostringstream cmdstr;
cmdstr << "set pointsize " << pointsize;
cmd(cmdstr.str());
return *this;
}
//------------------------------------------------------------------------------
//
// set isoline density (grid) for plotting functions as surfaces
//
Gnuplot& Gnuplot::set_samples(const int samples)
{
std::ostringstream cmdstr;
cmdstr << "set samples " << samples;
cmd(cmdstr.str());
return *this;
}
//------------------------------------------------------------------------------
//
// set isoline density (grid) for plotting functions as surfaces
//
Gnuplot& Gnuplot::set_isosamples(const int isolines)
{
std::ostringstream cmdstr;
cmdstr << "set isosamples " << isolines;
cmd(cmdstr.str());
return *this;
}
//------------------------------------------------------------------------------
//
// enables contour drawing for surfaces set contour {base | surface | both}
//
Gnuplot& Gnuplot::set_contour(const std::string &position)
{
if (position.find("base") == std::string::npos &&
position.find("surface") == std::string::npos &&
position.find("both") == std::string::npos )
{
cmd("set contour base");
}
else
{
cmd("set contour " + position);
}
return *this;
}
//------------------------------------------------------------------------------
//
// set labels
//
// set the xlabel
Gnuplot& Gnuplot::set_xlabel(const std::string &label)
{
std::ostringstream cmdstr;
cmdstr << "set xlabel \"" << label << "\"";
cmd(cmdstr.str());
return *this;
}
//------------------------------------------------------------------------------
// set the ylabel
//
Gnuplot& Gnuplot::set_ylabel(const std::string &label)
{
std::ostringstream cmdstr;
cmdstr << "set ylabel \"" << label << "\"";
cmd(cmdstr.str());
return *this;
}
//------------------------------------------------------------------------------
// set the zlabel
//
Gnuplot& Gnuplot::set_zlabel(const std::string &label)
{
std::ostringstream cmdstr;
cmdstr << "set zlabel \"" << label << "\"";
cmd(cmdstr.str());
return *this;
}
//------------------------------------------------------------------------------
//
// set range
//
// set the xrange
Gnuplot& Gnuplot::set_xrange(const double iFrom,
const double iTo)
{
std::ostringstream cmdstr;
cmdstr << "set xrange[" << iFrom << ":" << iTo << "]";
cmd(cmdstr.str());
return *this;
}
//------------------------------------------------------------------------------
// set the yrange
//
Gnuplot& Gnuplot::set_yrange(const double iFrom,
const double iTo)
{
std::ostringstream cmdstr;
cmdstr << "set yrange[" << iFrom << ":" << iTo << "]";
cmd(cmdstr.str());
return *this;
}
//------------------------------------------------------------------------------
// set the zrange
//
Gnuplot& Gnuplot::set_zrange(const double iFrom,
const double iTo)
{
std::ostringstream cmdstr;
cmdstr << "set zrange[" << iFrom << ":" << iTo << "]";
cmd(cmdstr.str());
return *this;
}
//------------------------------------------------------------------------------
//
// set the palette range
//
Gnuplot& Gnuplot::set_cbrange(const double iFrom,
const double iTo)
{
std::ostringstream cmdstr;
cmdstr << "set cbrange[" << iFrom << ":" << iTo << "]";
cmd(cmdstr.str());
return *this;
}
//------------------------------------------------------------------------------
//
// Plots a linear equation y=ax+b (where you supply the
// slope a and intercept b)
//
Gnuplot& Gnuplot::plot_slope(const double a,
const double b,
const std::string &title)
{
std::ostringstream cmdstr;
//
// command to be sent to gnuplot
//
if (nplots > 0 && two_dim == true)
cmdstr << "replot ";
else
cmdstr << "plot ";
cmdstr << a << " * x + " << b << " title \"";
if (title == "")
cmdstr << "f(x) = " << a << " * x + " << b;
else
cmdstr << title;
cmdstr << "\" with " << pstyle;
//
// Do the actual plot
//
cmd(cmdstr.str());
return *this;
}
//------------------------------------------------------------------------------
//
// Plot an equation supplied as a std::string y=f(x) (only f(x) expected)
//
Gnuplot& Gnuplot::plot_equation(const std::string &equation,
const std::string &title)
{
std::ostringstream cmdstr;
//
// command to be sent to gnuplot
//
if (nplots > 0 && two_dim == true)
cmdstr << "replot ";
else
cmdstr << "plot ";
cmdstr << equation << " title \"";
if (title == "")
cmdstr << "f(x) = " << equation;
else
cmdstr << title;
cmdstr << "\" with " << pstyle;
//
// Do the actual plot
//
cmd(cmdstr.str());
return *this;
}
//------------------------------------------------------------------------------
//
// plot an equation supplied as a std::string y=(x)
//
Gnuplot& Gnuplot::plot_equation3d(const std::string &equation,
const std::string &title)
{
std::ostringstream cmdstr;
//
// command to be sent to gnuplot
//
if (nplots > 0 && two_dim == false)
cmdstr << "replot ";
else
cmdstr << "splot ";
cmdstr << equation << " title \"";
if (title == "")
cmdstr << "f(x,y) = " << equation;
else
cmdstr << title;
cmdstr << "\" with " << pstyle;
//
// Do the actual plot
//
cmd(cmdstr.str());
return *this;
}
//------------------------------------------------------------------------------
//
// Plots a 2d graph from a list of doubles (x) saved in a file
//
Gnuplot& Gnuplot::plotfile_x(const std::string &filename,
const unsigned int column,
const std::string &title)
{
//
// check if file exists
//
file_available(filename);
std::ostringstream cmdstr;
//
// command to be sent to gnuplot
//
if (nplots > 0 && two_dim == true)
cmdstr << "replot ";
else
cmdstr << "plot ";
cmdstr << "\"" << filename << "\" using " << column;
if (title == "")
cmdstr << " notitle ";
else
cmdstr << " title \"" << title << "\" ";
if(smooth == "")
cmdstr << "with " << pstyle;
else
cmdstr << "smooth " << smooth;
//
// Do the actual plot
//
cmd(cmdstr.str()); //nplots++; two_dim = true; already in cmd();
return *this;
}
//------------------------------------------------------------------------------
//
// Plots a 2d graph from a list of doubles (x y) saved in a file
//
Gnuplot& Gnuplot::plotfile_xy(const std::string &filename,
const unsigned int column_x,
const unsigned int column_y,
const std::string &title)
{
//
// check if file exists
//
file_available(filename);
std::ostringstream cmdstr;
//
// command to be sent to gnuplot
//
if (nplots > 0 && two_dim == true)
cmdstr << "replot ";
else
cmdstr << "plot ";
cmdstr << "\"" << filename << "\" using " << column_x << ":" << column_y;
if (title == "")
cmdstr << " notitle ";
else
cmdstr << " title \"" << title << "\" ";
if(smooth == "")
cmdstr << "with " << pstyle;
else
cmdstr << "smooth " << smooth;
//
// Do the actual plot
//
cmd(cmdstr.str());
return *this;
}
//------------------------------------------------------------------------------
//
// Plots a 2d graph with errorbars from a list of doubles (x y dy) in a file
//
Gnuplot& Gnuplot::plotfile_xy_err(const std::string &filename,
const unsigned int column_x,
const unsigned int column_y,
const unsigned int column_dy,
const std::string &title)
{
//
// check if file exists
//
file_available(filename);
std::ostringstream cmdstr;
//
// command to be sent to gnuplot
//
if (nplots > 0 && two_dim == true)
cmdstr << "replot ";
else
cmdstr << "plot ";
cmdstr << "\"" << filename << "\" using "
<< column_x << ":" << column_y << ":" << column_dy
<< " with errorbars ";
if (title == "")
cmdstr << " notitle ";
else
cmdstr << " title \"" << title << "\" ";
//
// Do the actual plot
//
cmd(cmdstr.str());
return *this;
}
//------------------------------------------------------------------------------
//
// Plots a 3d graph from a list of doubles (x y z) saved in a file
//
Gnuplot& Gnuplot::plotfile_xyz(const std::string &filename,
const unsigned int column_x,
const unsigned int column_y,
const unsigned int column_z,
const std::string &title)
{
//
// check if file exists
//
file_available(filename);
std::ostringstream cmdstr;
//
// command to be sent to gnuplot
//
if (nplots > 0 && two_dim == false)
cmdstr << "replot ";
else
cmdstr << "splot ";
cmdstr << "\"" << filename << "\" using " << column_x << ":" << column_y
<< ":" << column_z;
if (title == "")
cmdstr << " notitle with " << pstyle;
else
cmdstr << " title \"" << title << "\" with " << pstyle;
//
// Do the actual plot
//
cmd(cmdstr.str());
return *this;
}
//------------------------------------------------------------------------------
//
/// * note that this function is not valid for versions of GNUPlot below 4.2
//
Gnuplot& Gnuplot::plot_image(const unsigned char * ucPicBuf,
const unsigned int iWidth,
const unsigned int iHeight,
const std::string &title)
{
std::ofstream tmp;
std::string name = create_tmpfile(tmp);
if (name == "")
return *this;
//
// write the data to file
//
int iIndex = 0;
for(unsigned int iRow = 0; iRow < iHeight; iRow++)
{
for(unsigned int iColumn = 0; iColumn < iWidth; iColumn++)
{
tmp << iColumn << " " << iRow << " "
<< static_cast<float>(ucPicBuf[iIndex++]) << std::endl;
}
}
tmp.flush();
tmp.close();
std::ostringstream cmdstr;
//
// command to be sent to gnuplot
//
if (nplots > 0 && two_dim == true)
cmdstr << "replot ";
else
cmdstr << "plot ";
if (title == "")
cmdstr << "\"" << name << "\" with image";
else
cmdstr << "\"" << name << "\" title \"" << title << "\" with image";
//
// Do the actual plot
//
cmd(cmdstr.str());
return *this;
}
//------------------------------------------------------------------------------
//
// Sends a command to an active gnuplot session
//
Gnuplot& Gnuplot::cmd(const std::string &cmdstr)
{
if( !(valid) )
{
return *this;
}
// int fputs ( const char * str, FILE * stream );
// writes the string str to the stream.
// The function begins copying from the address specified (str) until it
// reaches the terminating null character ('\0'). This final
// null-character is not copied to the stream.
fputs( (cmdstr+"\n").c_str(), gnucmd );
// int fflush ( FILE * stream );
// If the given stream was open for writing and the last i/o operation was
// an output operation, any unwritten data in the output buffer is written
// to the file. If the argument is a null pointer, all open files are
// flushed. The stream remains open after this call.
fflush(gnucmd);
if( cmdstr.find("replot") != std::string::npos )
{
return *this;
}
else if( cmdstr.find("splot") != std::string::npos )
{
two_dim = false;
nplots++;
}
else if( cmdstr.find("plot") != std::string::npos )
{
two_dim = true;
nplots++;
}
return *this;
}
//------------------------------------------------------------------------------
//
// Opens up a gnuplot session, ready to receive commands
//
void Gnuplot::init()
{
// char * getenv ( const char * name ); get value of environment variable
// Retrieves a C string containing the value of the environment variable
// whose name is specified as argument. If the requested variable is not
// part of the environment list, the function returns a NULL pointer.
#if ( defined(unix) || defined(__unix) || defined(__unix__) ) && !defined(__APPLE__)
if (getenv("DISPLAY") == NULL)
{
valid = false;
throw GnuplotException("Can't find DISPLAY variable");
}
#endif
// if gnuplot not available
if (!Gnuplot::get_program_path())
{
valid = false;
throw GnuplotException("Can't find gnuplot");
}
//
// open pipe
//
std::string tmp = Gnuplot::m_sGNUPlotPath + "/" +
Gnuplot::m_sGNUPlotFileName;
// FILE *popen(const char *command, const char *mode);
// The popen() function shall execute the command specified by the string
// command, create a pipe between the calling program and the executed
// command, and return a pointer to a stream that can be used to either read
// from or write to the pipe.
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
gnucmd = _popen(tmp.c_str(),"w");
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
gnucmd = popen(tmp.c_str(),"w");
#endif
// popen() shall return a pointer to an open stream that can be used to read
// or write to the pipe. Otherwise, it shall return a null pointer and may
// set errno to indicate the error.
if (!gnucmd)
{
valid = false;
throw GnuplotException("Couldn't open connection to gnuplot");
}
nplots = 0;
valid = true;
smooth = "";
//set terminal type
showonscreen();
return;
}
//------------------------------------------------------------------------------
//
// Find out if a command lives in m_sGNUPlotPath or in PATH
//
bool Gnuplot::get_program_path()
{
//
// first look in m_sGNUPlotPath for Gnuplot
//
std::string tmp = Gnuplot::m_sGNUPlotPath + "/" +
Gnuplot::m_sGNUPlotFileName;
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
if ( Gnuplot::file_exists(tmp,0) ) // check existence
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
if ( Gnuplot::file_exists(tmp,1) ) // check existence and execution permission
#endif
{
return true;
}
//
// second look in PATH for Gnuplot
//
char *path;
// Retrieves a C string containing the value of environment variable PATH
path = getenv("PATH");
if (path == NULL)
{
throw GnuplotException("Path is not set");
return false;
}
else
{
std::list<std::string> ls;
//split path (one long string) into list ls of strings
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
stringtok(ls,path,";");
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
stringtok(ls,path,":");
#endif
// scan list for Gnuplot program files
for (std::list<std::string>::const_iterator i = ls.begin();
i != ls.end(); ++i)
{
tmp = (*i) + "/" + Gnuplot::m_sGNUPlotFileName;
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
if ( Gnuplot::file_exists(tmp,0) ) // check existence
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
if ( Gnuplot::file_exists(tmp,1) ) // check existence and execution permission
#endif
{
Gnuplot::m_sGNUPlotPath = *i; // set m_sGNUPlotPath
return true;
}
}
tmp = "Can't find gnuplot neither in PATH nor in \"" +
Gnuplot::m_sGNUPlotPath + "\"";
throw GnuplotException(tmp);