-
Notifications
You must be signed in to change notification settings - Fork 1
/
gplot-easy.h
3518 lines (2916 loc) · 92.8 KB
/
gplot-easy.h
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
//
// gplot-easy.h
//
// Header/Source file of the library 'gplot'
// Made for the Easy Setup
//
// Created by João Gutemberg Barbosa de Farias Filho
// See https://github.com/gutofarias/gplot
//
#ifndef gnuplot_cpp_gnuplot_h
#define gnuplot_cpp_gnuplot_h
#include <iostream>
#include <math.h>
#include <string.h>
#include <sstream>
#include <fstream>
#include <unistd.h>
#include <list>
using namespace std;
/* -----------------------------------------------------------------------------------------*/
/* -----------------------------------------------------------------------------------------*/
/* -----------------------------------------------------------------------------------------*/
const int TERM_EPS = 1;
const int TERM_X11 = 2;
const int TERM_AQUA = 3;
const int TERM_WXT = 4;
const int TERM_QT = 5;
const int TERM_WINDOWS = 6;
const int TERM_PDFLATEX_EPS = 7;
const int TERM_PDFLATEX_CAIRO = 8;
//BEGIN USER CONFIGURATION
//CHOOSE THE DEFAULT TERMINAL TO RUN THE SCRIPTS
const int TERM_DEFAULT = TERM_EPS;
//CHOOSE THE DEFAULT FONT AND FONT SIZE
const string FONT_DEFAULT = "Verdana";//"Helvetica";
const int FONT_SIZE_DEFAULT = 10;
//CHOOSE THE DEFAULT FONT SIZE AND GRAPH SIZE WHEN USING THE PDFLATEX TERMINALS
const int FONT_SIZE_DEFAULT_PDFLATEX = 10;
const double GRAPH_SIZE_DEFAULT_PDFLATEX[2] = {16.0, 11.0}; //{width_in_cm, height_in_cm}
//ENTER NAME (COMMAND TO RUN) OF THE PROGRAM TO RUN THE LATEX OUTPUT AND GENERATE A PDF FILE WHEN USING PDFLATEX TERMINAL
const string LATEX2PDFCOMPILER = "pdflatex";
//IF THE NAME DOES NOT WORK, SET THE BOOLEAN TO TRUE AND TRY THE ABSOLUTE PATH OF OF PDFLATEX, EPSTOPDF AND GHOSTSCRIPT(GS) (GS IS NOT NEEDED ON WINDOWS)
const bool PDFLATEX_MANUAL_PATH_BOOL = true;
const string LATEX2PDFCOMPILER_PATH = "/opt/local/bin/pdflatex"; //terminal: "type -a pdflatex", cmd: "where pdflatex"
//const string LATEX2PDFCOMPILER_PATH = "C:\\Program Files\\MiKTeX 2.9\\miktex\\bin\\pdflatex.exe"; //example on windows
const string EPSTOPDF_PATH = "/opt/local/bin/epstopdf"; //terminal: "type -a epstopdf", cmd: "where epstopdf"
//const string EPSTOPDF_PATH = "C:\\Program Files\\MiKTeX 2.9\\miktex\\bin\\epstopdf.exe"; //example on windows
const string GHOSTSCRIPT_PATH = "/usr/local/bin/gs"; //terminal: "type -a gs", will not be used in Windows
const bool DEBUG_MANUAL_PATH = false; //Use this if the program says "OK" but do not generate the pdf file. If true it is going to show all the shell outputs
//OBS.: REMEMBER TO USE '\\' and '\"' WHEN SETTING AN ABSOLUTE PATH FOR WINDOWS
//ADD A CUSTOM GNUPLOT ABSOLUTE PATH IF THE LIBRARY IS NOT ABLE TO FIND IT AUTOMATICALLY
//SET THE VARIABLE CUSTOM_GNUPLOT_PATH_BOOL TO TRUE TO USE THE CUSTOM PATH
const bool CUSTOM_GNUPLOT_PATH_BOOL = false;
const string CUSTOM_GNUPLOT_PATH = "/usr/local/bin/gnuplo2t";//"/opt/local/bin/gnuplot";
//const string CUSTOM_GNUPLOT_PATH = "C:\\Program Files\\gnuplot\\bin\\gnuplot.exe";
//OBS.: REMEMBER TO USE '\\' and '\"' WHEN SETTING AN ABSOLUTE PATH FOR WINDOWS, EX.:
//const string CUSTOM_GNUPLOT_PATH = "\"C:\\Program Files\\gnuplot\\bin\\gnuplot.exe\"";
//CONFIGURING XCODE (MAC) TO WORK WITH THE LIBRARY
//SETTING THE WORKING DIRECTORY
/*
FIRST WE RECOMMEND YOU TO CHANGE THE WORKING DIRECTORY FOR ONE THAT YOU DESIRE, E.G. THE SAME FOLDER THAT CONTAINS THE main.cpp
TO DO THIS GO IN:
PRODUCT > SCHEME > EDIT SCHEME > RUN > OPTIONS
THEN ACTIVATE AND CHOOSE A CUSTOM WORKING DIRECTORY
*/
//END USER CONFIGURATION
//CODE TO TEST IF THE LIBRARY CAN FIND THE GNUPLOT EXECUTABLE
/*
#include <iostream>
#include <math.h>
#include "gnuplot.h"
using namespace std;
int main(int argc, const char * argv[]) {
const int n = 100;
double x[n];
double y[n];
for(int i=0; i<n; i++)
{
x[i] = 0.1 * i;
y[i] = sin(x[i]);
}
Gnuplot2D plot1 (2) ;
plot1.curve[0].set_plot("exported_vectors.txt", x, y, n);
plot1.curve[1].set_plot("exported_vectors.txt", x, y, n, 3);
plot1.set_term_eps();
return 0;
}
*/
//IF YOU CAN FIND AN IMAGE NAMED gnuplot2d_1.eps WHICH HAS A SINE WAVE PLOTTED, THE LIBRARY WAS ABLE TO FIND THE GNUPLOT EXECUTABLE.
/* -----------------------------------------------------------------------------------------*/
/* -----------------------------------------------------------------------------------------*/
/* -----------------------------------------------------------------------------------------*/
//DO NOT CHANGE ANYTHING FROM NOW ON
//DO NOT CHANGE ANYTHING FROM NOW ON
//DO NOT CHANGE ANYTHING FROM NOW ON
//CONSTANTS
const string PLOT_FILENAME_DEFAULT = "You need to set the plot!";
//OPERATIONAL SYSTEM DEFINITION AND GNUPLOT PATH
#define WINDOWS_OS defined(_WIN32)||defined(WIN32)||defined(_WIN64)
#define UNIX_OS defined(__APPLE__)||defined(__unix)||defined(__linux)||defined(__posix)
#if WINDOWS_OS
#include <process.h>
#include <Windows.h>
//#define popen _popen
//#define pclose _pclose
const bool WINDOWS = true;
#else
#include <sys/mman.h>
#include <sys/wait.h>
const bool WINDOWS = false;
#endif
#ifdef UNIX_OS
const bool UNIX = true;
#else
const bool UNIX = false;
#endif
//FUNCTION TO FIND GNUPLOT PATH
#if WINDOWS_OS
const int GNUPLOT_PATH_NOT_FOUND = 0;
const int GNUPLOT_PATH_CUSTOM = 1;
const int GNUPLOT_PATH_WINDOWSX86 = 2;
const int GNUPLOT_PATH_WINDOWSX64 = 3;
int GNUPLOT_PATH_CODE = GNUPLOT_PATH_NOT_FOUND;
string GNUPLOT_PATH;
void test_path (void) //FOR WINDOWS
{
static bool first = true;
static bool CUSTOM_GNUPLOT_PATH_BOOL2 = true;
if(first)
{
first = false;
if(CUSTOM_GNUPLOT_PATH_BOOL && CUSTOM_GNUPLOT_PATH_BOOL2)
{
string test_string = "if exist \"" + CUSTOM_GNUPLOT_PATH + "\" (echo sim) else (echo nao)";
FILE* test = _popen(test_string.c_str(),"r");
char resp[4];
string str;
fgets(resp, 4, test);
str = resp;
_pclose(test);
if (str=="sim")
{
GNUPLOT_PATH_CODE = GNUPLOT_PATH_CUSTOM;//true;
GNUPLOT_PATH = CUSTOM_GNUPLOT_PATH;
}
else
{
first = true;
CUSTOM_GNUPLOT_PATH_BOOL2 = false;
cout << "CUSTOM GNUPLOT PATH NOT FOUND. TRYING AGAIN WITH THE REGULAR PATH" << endl;
test_path();
}
}
else
{
FILE* test = _popen("if exist \"C:\\Program Files\\gnuplot\\bin\\gnuplot.exe\" (echo sim) else (echo nao)","r");
char resp[4];
string str;
fgets(resp, 4, test);
str = resp;
_pclose(test);
//cout << str << endl;
if (str=="sim")
{
//PATH_BOOL = true;
GNUPLOT_PATH_CODE = GNUPLOT_PATH_WINDOWSX64;
GNUPLOT_PATH = "C:\\Program Files\\gnuplot\\bin\\gnuplot.exe";
}
else
{
test = _popen("if exist \"C:\\Program Files (x86)\\gnuplot\\bin\\gnuplot.exe\" (echo sim) else (echo nao)","r");
fgets(resp, 4, test);
str = resp;
_pclose(test);
//cout << str << endl;
if(str=="sim")
{
//PATH_BOOL = true;
GNUPLOT_PATH_CODE = GNUPLOT_PATH_WINDOWSX86;
GNUPLOT_PATH = "C:\\Program Files (x86)\\gnuplot\\bin\\gnuplot.exe";
}
else
{
cout << "ERROR: GNUPLOT PATH NOT FOUND" << endl;
//PATH_BOOL = true;
GNUPLOT_PATH_CODE = GNUPLOT_PATH_NOT_FOUND;
GNUPLOT_PATH = "C:\\Program Files\\gnuplot\\bin\\gnuplot.exe";
}
}
}
}
}
#else
const int GNUPLOT_PATH_NOT_FOUND = 0;
const int GNUPLOT_PATH_CUSTOM = 1;
const int GNUPLOT_PATH_JUST_GNUPLOT = 2;
const int GNUPLOT_PATH_OPT_LOCAL_BIN_GNUPLOT = 3;
int GNUPLOT_PATH_CODE = GNUPLOT_PATH_NOT_FOUND;
//bool PATH_BOOL=false;
string GNUPLOT_PATH;
void test_path (void) //FOR MAC AND LINUX
{
static bool first = true;
static bool CUSTOM_GNUPLOT_PATH_BOOL2 = true;
if(first)
{
first = false;
if(CUSTOM_GNUPLOT_PATH_BOOL && CUSTOM_GNUPLOT_PATH_BOOL2)
{
//string test_string = "type " + CUSTOM_GNUPLOT_PATH + " 2>&1>/dev/null && echo sim || echo nao";
string test_string = "type " + CUSTOM_GNUPLOT_PATH + " 2>&1 && echo sim || echo nao";
FILE* test = popen(test_string.c_str(), "r");
char resp[200];
string str = "";
bool teste_str = false;
while(fgets(resp, sizeof(resp), test)!=NULL && teste_str == false)
{
str = resp;
if(str.find("sim")!=string::npos || str.find("nao")!=string::npos)
{
teste_str = true;
}
}
pclose(test);
if (str.find("sim")!=string::npos)
{
//PATH_BOOL = true;
GNUPLOT_PATH_CODE = GNUPLOT_PATH_CUSTOM;
GNUPLOT_PATH = CUSTOM_GNUPLOT_PATH;
}
else
{
first = true;
CUSTOM_GNUPLOT_PATH_BOOL2 = false;
cout << "CUSTOM GNUPLOT PATH NOT FOUND. TRYING AGAIN WITH THE REGULAR PATH" << endl;
test_path();
}
}
else
{
//FILE* test = popen("type gnuplot 2>&1>/dev/null && echo \"sim\" || echo \"nao\"", "r");
FILE* test = popen("type gnuplot 2>&1 && echo \"sim\" || echo \"nao\"", "r");
char resp[200];
string str = "";
bool teste_str = false;
while(fgets(resp, sizeof(resp), test)!=NULL && teste_str == false)
{
str = resp;
if(str.find("sim")!=string::npos || str.find("nao")!=string::npos)
{
teste_str = true;
}
}
pclose(test);
if (str.find("sim")!=string::npos)
{
//PATH_BOOL = true;
GNUPLOT_PATH_CODE = GNUPLOT_PATH_JUST_GNUPLOT;
GNUPLOT_PATH = "gnuplot";
}
else
{
//test = popen("type /opt/local/bin/gnuplot 2>&1>/dev/null && echo sim || echo nao", "r");
test = popen("type /opt/local/bin/gnuplot 2>&1 && echo sim || echo nao", "r");
teste_str = false;
while(fgets(resp, sizeof(resp), test)!=NULL && teste_str == false)
{
str = resp;
if(str.find("sim")!=string::npos || str.find("nao")!=string::npos)
{
teste_str = true;
}
}
pclose(test);
if (str.find("sim")!=string::npos)
{
//PATH_BOOL = true;
GNUPLOT_PATH_CODE = GNUPLOT_PATH_OPT_LOCAL_BIN_GNUPLOT;
GNUPLOT_PATH = "/opt/local/bin/gnuplot";
}
else
{
cout << "ERROR: GNUPLOT PATH NOT FOUND" << endl;
//PATH_BOOL = false;
GNUPLOT_PATH_CODE = GNUPLOT_PATH_NOT_FOUND;
GNUPLOT_PATH = "gnuplot";
}
}
}
}
}
#endif
//FUNCTION THAT EXECUTES THE SCRIPT (PLOT THE GRAPH) IN CASE IT WAS NOT PLOTTED BY USING THE Gnuplot2D::plot_graph() or Gnuplot3D::plot_graph() FUNCTIONS. THIS FUNCTION IS EXECUTED WHEN THE Gnuplot2D OBJECT or Gnuplot3D OBJECT DIES.
void execute_script_gnuplot (string scriptname, int& pid)
{
#if WINDOWS_OS
spawnl( P_NOWAIT, GNUPLOT_PATH.c_str(), "gnuplot","-persist",scriptname.c_str(), NULL);
#else
if ((pid=fork())==0) //if(fork()==0)
{
//if(!PATH_BOOL)
if(GNUPLOT_PATH_CODE == GNUPLOT_PATH_JUST_GNUPLOT)
{
execlp(GNUPLOT_PATH.c_str(),"gnuplot","-persist",scriptname.c_str(),NULL);//FOR MAC AND LINUX IN MOST OF THE CASES, RUNNING JUST THE COMMAND "gnuplot" WITHOUT AND ABSOLUTE PATH BEING NEEDED
}
else
{
execl(GNUPLOT_PATH.c_str(),"gnuplot","-persist",scriptname.c_str(), NULL); //FOR MAC WHEN GNUPLOT IS INSTALED THROUGH MACPORTS, OR WHEN RUNNING AN ABSOLUTE PATH TO GNUPLOT
}
//IF IT FAILS TO EXECUTE
cout << "PLOT " << scriptname << " FAILS TO EXECUTE" << endl;
_exit(1);
}
//int status;
//while (-1 == waitpid(pid, &status, 0))
//{}
//cout << "teste" << endl;
//pid = -100;
#endif
}
/*
void print_line_once (void)
{
static bool printed = false;
if(!printed){ cout << endl; printed = true;}
}
*/
std::string ReplaceAll(std::string str, const std::string& from, const std::string& to) {
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
}
return str;
}
//FUNCTION THAT EXECUTES THE LATEX TO PDF CONVERSION IN THE PDFLATEX TERMINAL
void pdflatex_exec (string output_filename, int pid=-100)
{
int status;
if(pid != -100)
{
#if WINDOWS_OS
#else
while (-1 == waitpid(pid, &status, 0))
{}
#endif
}
#if WINDOWS_OS
else{Sleep(1000);}
#else
else{sleep(1);}
#endif
unsigned long p = output_filename.rfind(".");
if (p>0)
{
output_filename = output_filename.substr(0,p);
}
#if WINDOWS_OS
output_filename = ReplaceAll(output_filename,"/", "\\");
#endif
bool folder_name_bool = false;
string folder_name = "";
p = output_filename.rfind("/");
unsigned long p2 = output_filename.rfind("\\");
if(p != string::npos)
{
folder_name_bool = true;
folder_name = output_filename.substr(0,p+1);
}
else if(p2 != string::npos)
{
folder_name_bool = true;
folder_name = output_filename.substr(0,p2+1);
}
string scriptex = output_filename + ".tex";
#if WINDOWS_OS
string deletefiles = "del " + output_filename + ".log " + output_filename + ".aux " + output_filename + "-inc.eps " + output_filename + "-inc-eps-converted-to.pdf " + scriptex + " 2>&1";
#else
string deletefiles = "rm " + output_filename + ".log " + output_filename + ".aux " + output_filename + "-inc.eps " + output_filename + "-inc-eps-converted-to.pdf " + scriptex + " 2>&1";
#endif
//print_line_once();
cout << "Generating " << output_filename << ".pdf ... ";
bool ERROR_BOOL = false;
char buff[512];
char error_char = '!';
if(PDFLATEX_MANUAL_PATH_BOOL)
{
#if WINDOWS_OS
string LATEX2PDFCOMPILER_PATH2 = "\"" + LATEX2PDFCOMPILER_PATH + "\"";
string EPSTOPDF_PATH2 = "\"" + EPSTOPDF_PATH + "\"";
string compare;
string LATEX2PDFCOMPILER_PATH_compare = "'" + LATEX2PDFCOMPILER_PATH2 + "'";
string EPSTOPDF_PATH_compare = "'" + EPSTOPDF_PATH2 + "'";
#endif
#if WINDOWS_OS
string pdflatex_manual_str = EPSTOPDF_PATH2 + " --outfile=" + output_filename + "-inc-eps-converted-to.pdf " + output_filename + "-inc.eps" + " 2>&1";
#else
string pdflatex_manual_str = EPSTOPDF_PATH + " --gscmd=" + GHOSTSCRIPT_PATH + " --outfile=" + output_filename + "-inc-eps-converted-to.pdf " + output_filename + "-inc.eps" + " 2>&1";
#endif
FILE* pdflatex_manual;
#if WINDOWS_OS
pdflatex_manual = _popen(pdflatex_manual_str.c_str(), "r");
#else
pdflatex_manual = popen(pdflatex_manual_str.c_str(), "r");
#endif
while(fgets(buff, sizeof(buff), pdflatex_manual)!=NULL)
{
if(DEBUG_MANUAL_PATH)
{cout << buff << endl;}
#if WINDOWS_OS
compare = string(buff).substr(0,EPSTOPDF_PATH_compare.length());
if(buff[0] == error_char || EPSTOPDF_PATH_compare == compare)
#else
if(buff[0] == error_char || (buff[0] == 's' && buff[1] == 'h'))
#endif
{
if(!ERROR_BOOL)
{
cout << endl;
ERROR_BOOL = true;
}
cout << "ERROR EPSTOPDF: ";
cout << buff;
}
#if WINDOWS_OS
else if ( buff[0] == '\'')
{
if(!ERROR_BOOL)
{
cout << endl;
ERROR_BOOL = true;
}
cout << "ERROR EPSTOPDF: " << EPSTOPDF_PATH << " NOT FOUND!" << endl;
}
#endif
}
#if WINDOWS_OS
_pclose(pdflatex_manual);
#else
pclose(pdflatex_manual);
#endif
string pdflatex_str;
#if WINDOWS_OS
if(folder_name_bool)
{
pdflatex_str = LATEX2PDFCOMPILER_PATH2 + " --interaction=nonstopmode --output-directory=" + folder_name + " " + scriptex + " 2>&1";
}
else
{
pdflatex_str = LATEX2PDFCOMPILER_PATH2 + " --interaction=nonstopmode " + scriptex + " 2>&1";
}
#else
if(folder_name_bool)
{
pdflatex_str = LATEX2PDFCOMPILER_PATH + " --interaction=nonstopmode --output-directory=" + folder_name + " " + scriptex + " 2>&1";
}
else
{
pdflatex_str = LATEX2PDFCOMPILER_PATH + " --interaction=nonstopmode " + scriptex + " 2>&1";
}
#endif
FILE* comp_tex;
#if WINDOWS_OS
comp_tex = _popen(pdflatex_str.c_str(), "r");
#else
comp_tex = popen(pdflatex_str.c_str(), "r");
#endif
while(fgets(buff, sizeof(buff), comp_tex)!=NULL)
{
if(DEBUG_MANUAL_PATH)
{cout << buff << endl;}
#if WINDOWS_OS
compare = string(buff).substr(0,LATEX2PDFCOMPILER_PATH_compare.length());
if(buff[0] == error_char || LATEX2PDFCOMPILER_PATH_compare == compare)
#else
if(buff[0] == error_char || (buff[0] == 's' && buff[1] == 'h'))
#endif
{
if(!ERROR_BOOL)
{
cout << endl;
ERROR_BOOL = true;
}
cout << "ERROR PDFLATEX: ";
cout << buff;
}
#if WINDOWS_OS
else if ( buff[0] == '\'')
{
if(!ERROR_BOOL)
{
cout << endl;
ERROR_BOOL = true;
}
cout << "ERROR PDFLATEX: " << LATEX2PDFCOMPILER_PATH << " NOT FOUND!" << endl;
}
#endif
}
#if WINDOWS_OS
_pclose(comp_tex);
#else
pclose(comp_tex);
#endif
}
else
{
#if WINDOWS_OS
string LATEX2PDFCOMPILER2 = "\"" + LATEX2PDFCOMPILER + "\"";
string compare;
string LATEX2PDFCOMPILER_compare = "'" + LATEX2PDFCOMPILER2 + "'";
#endif
string pdflatex_str;
#if WINDOWS_OS
if(folder_name_bool)
{
pdflatex_str = LATEX2PDFCOMPILER2 + " --interaction=nonstopmode --output-directory=" + folder_name + " " + scriptex + " 2>&1";
}
else
{
pdflatex_str = LATEX2PDFCOMPILER2 + " --interaction=nonstopmode " + scriptex + " 2>&1";
}
#else
if(folder_name_bool)
{
pdflatex_str = LATEX2PDFCOMPILER + " --interaction=nonstopmode --output-directory=" + folder_name + " " + scriptex + " 2>&1";
}
else
{
pdflatex_str = LATEX2PDFCOMPILER + " --interaction=nonstopmode " + scriptex + " 2>&1";
}
#endif
FILE* comp_tex;
#if WINDOWS_OS
comp_tex = _popen(pdflatex_str.c_str(), "r");
#else
comp_tex = popen(pdflatex_str.c_str(), "r");
#endif
while(fgets(buff, sizeof(buff), comp_tex)!=NULL)
{
if(DEBUG_MANUAL_PATH)
{cout << buff << endl;}
#if WINDOWS_OS
compare = string(buff).substr(0,LATEX2PDFCOMPILER_compare.length());
if(buff[0] == error_char || LATEX2PDFCOMPILER_compare == compare)
#else
if(buff[0] == error_char || (buff[0] == 's' && buff[1] == 'h'))
#endif
{
if(!ERROR_BOOL)
{
cout << endl;
ERROR_BOOL = true;
}
cout << "ERROR PDFLATEX: ";
cout << buff;
}
#if WINDOWS_OS
else if ( buff[0] == '\'')
{
if(!ERROR_BOOL)
{
cout << endl;
ERROR_BOOL = true;
}
cout << "ERROR PDFLATEX: " << LATEX2PDFCOMPILER << " NOT FOUND!" << endl;
}
#endif
}
#if WINDOWS_OS
_pclose(comp_tex);
#else
pclose(comp_tex);
#endif
//cout << endl << pdflatex_str << endl;
}
FILE* delete_files = popen(deletefiles.c_str(), "r");
#if WINDOWS_OS
_pclose(delete_files);
#else
pclose(delete_files);
#endif
if(!ERROR_BOOL)
{
cout << "OK!" << endl;
}
else
{
cout << "Failed to generate " << output_filename << ".pdf!\n\n";
}
//cout << deletefiles << endl;
}
//#if WINDOWS_OS
//#else
void exec_bundle_files (string *files_copy, int n_files_copy, string *files_move, int n_files_move, string folder_path)
{
list<string> files_copy_list(files_copy, files_copy + n_files_copy);
files_copy_list.sort();
files_copy_list.unique();
list<string> files_move_list(files_move, files_move + n_files_move);
files_move_list.sort();
files_move_list.unique();
//cout << endl << endl;
unsigned long p = folder_path.rfind("/");
unsigned long p2 = folder_path.rfind("\\");
if(p == folder_path.length()-1)
{
folder_path = folder_path.substr(0,p);
}
else if(p2 == folder_path.length()-1)
{
folder_path = folder_path.substr(0,p2);
}
string new_folder = "mkdir " + folder_path + " 2>&1";
FILE* commands;
#if WINDOWS_OS
commands = _popen(new_folder.c_str(), "r");
#else
commands = popen(new_folder.c_str(), "r");
#endif
#if WINDOWS_OS
_pclose(commands);
#else
pclose(commands);
#endif
#if WINDOWS_OS
string copy_files = "";
#else
string copy_files = "cp -p ";
#endif
for (list<string>::iterator it=files_copy_list.begin(); it!=files_copy_list.end(); ++it)
{
if((*it) == PLOT_FILENAME_DEFAULT)
{continue;}
#if WINDOWS_OS
copy_files = "copy /Y " + ReplaceAll((*it),"/", "\\") + " " + ReplaceAll(folder_path, "/", "\\");
commands = _popen(copy_files.c_str(), "r");
_pclose(commands);
#else
copy_files += (*it) + " ";
#endif
}
#if WINDOWS_OS
#else
copy_files += folder_path;
commands = popen(copy_files.c_str(), "r");
pclose(commands);
#endif
#if WINDOWS_OS
string move_files = "";
#else
string move_files = "mv ";
#endif
for (list<string>::iterator it=files_move_list.begin(); it!=files_move_list.end(); ++it)
{
#if WINDOWS_OS
move_files = "move /Y " + ReplaceAll((*it),"/", "\\") + " " + ReplaceAll(folder_path, "/", "\\");
commands = _popen(move_files.c_str(), "r");
_pclose(commands);
#else
move_files += (*it) + " ";
#endif
}
move_files += folder_path;
#if WINDOWS_OS
#else
commands = popen(move_files.c_str(), "r");
pclose(commands);
#endif
}
//#endif
/* -----------------------------------------------------------------------------------------*/
/* -----------------------------------------------------------------------------------------*/
/* -----------------------------------------------------------------------------------------*/
//FUNCTION TO CONVERT A VARIABLE OUTPUT STRINGSTREAM TO A STRING
template <typename T> string conv_string(T var)
{
stringstream ss;
ss << var;
return ss.str();
}
//CLASS PLOTBASE
class plotbase
{
protected:
string _filename;
int _columns[6];
bool _title_bool;
string _title;
bool _lines;
bool _points;
int _color_gnuplot;
bool _color_rgb_bool;
string _color_rgb;
string end_of_plot (void);
bool _parametric;
int _type_or_style;
double _size_width;
bool _palette;
int _palette_column;
bool _pm3d;
bool _dashtype_string_bool;
int _dashtype_int;
string _dashtype_string;
bool _vectors;
double _vector_headsize[2];
bool _vector_headfilled;
void set_parametric (void);
plotbase(int color_gnu = 0); //protected constructor
void set_color_rgb_or_option (string opt);
bool _plotstring_custom_bool;
string _plotstring_custom;
public:
void set_filename (string filename);
void set_columns (int col1, int col2, int col3=0, int col4=0, int col5=0, int col6=0);
//void set_one_column (int column_index, int column_value);
void set_color_gnuplot(int color_gnuplot);
void set_color_rgb (string color_rgb);
void set_title (string title);
void set_with_points (double point_size=1.0, int point_type=1);
void set_with_lines (double line_width=1.0, int line_style=1);
void set_with_vectors(double line_width=1.0, int line_style=1);
void set_vector_headsize(double length, double angle = 20.0);
void set_vector_headfilled(bool filled);
//void set_with_lines (double line_width, string dash_pattern);
//void set_with_lines_dashed (double line_width = 1.0, int dash_pattern = 1);
//void set_with_lines_dashed (double line_width, string dash_pattern);
void set_dashtype (int dash_pattern);
void set_dashtype (string custom_dash_pattern);
void set_type_or_style (int point_type_or_line_style);
void set_size_or_width (double point_size_or_line_width);
void set_palette (int function_column);
void set_pm3d (int function_column);
void set_plot_custom(string plotstring_custom);
bool get_parametric (void);
string get_filename (void);
};
/* -----------------------------------------------------------------------------------------*/
/* -----------------------------------------------------------------------------------------*/
/* -----------------------------------------------------------------------------------------*/
//CLASS PLOTSTRING2D_SINGLE
class plotstring2d_single : public plotbase {
private:
bool _straight_line_bool;
string _straight_line;
plotstring2d_single(void); //private constructor
string plotstring (void);
bool _x2y2bool;
int _xyaxes[2];
public:
friend class gnup2D; //allows gnup2D to access this class private data (access its constructor)
void set_plot(string filename, int col1, int col2, int color_gnuplot=0);
void set_plot(string filename, int col1, int col2, int col3, int col4, int color_gnuplot=0); //for ploting vectors
void set_plot(string filename, int col1, int col2, string color_rgb);
void set_plot(string filename, int col1, int col2, int col3, int col4, string color_rgb); //for ploting vectors
void set_plot(string filename, double* x, double* y, long long number_of_points, int color_gnuplot=0);
void set_plot(string filename, double* x, double* y, long long number_of_points, string color_rgb);
void set_plot_straight_line(double coef_y, double coef_x, double b, int color_gnuplot=0); //coefs of y=x+b
void set_plot_straight_line(double coef_y, double coef_x, double b, string color_rgb); //coefs of y=x+b
void set_xyaxes(int xaxis, int yaxis);
};
/* -----------------------------------------------------------------------------------------*/
/* -----------------------------------------------------------------------------------------*/
/* -----------------------------------------------------------------------------------------*/
//CLASS PLOTSTRING3D_SINGLE
class plotstring3d_single : public plotbase {
private:
bool _plane_bool;
string _plane_str;
plotstring3d_single(void); //private constructor
string plotstring (void);
public:
friend class gnup3D; //allows gnup3D to access this class private data (access its constructor)
void set_plot(string filename, int col1, int col2, int col3, int color_gnup=0);
void set_plot(string filename, int col1, int col2, int col3, int col4, int col5, int col6, int color_gnup=0); //for ploting vectors
void set_plot(string filename, int col1, int col2, int col3, string color_rgb);
void set_plot(string filename, int col1, int col2, int col3, int col4, int col5, int col6, string color_rgb); //for ploting vectors
void set_plot(string filename, double* x, double* y, double* z, long long number_of_points, int color_gnup=0);
void set_plot(string filename, double* x, double* y, double* z, long long number_of_points, string color_rgb);
void set_plot_plane(double x, double y, double z, double a, int color_gnup=0);
void set_plot_plane(double x, double y, double z, double a, string color_rgb);
};
/* -----------------------------------------------------------------------------------------*/
/* -----------------------------------------------------------------------------------------*/
/* -----------------------------------------------------------------------------------------*/
//CLASS HEADER_GNUPLOT
class header_gnuplot {
private:
static int _cont_plots;
protected:
string _terminal;
double _linewidth;
string _font;
int _font_size;
bool _font_size_changed;
int _term_code;
double _graph_size_pdflatex[2];
void set_terminal (int term1);
string terminal_gnuplot (void);
bool _added_latex_packages;
string _latex_packages;
string _output_filename;
header_gnuplot(void); //protected constructor
string header (void);
void set_term_code (int terminal1);
public:
void set_terminal_custom (string terminal);
void set_term_aqua (void);
void set_term_x11 (void);
void set_term_eps (void);
void set_term_windows (void);
void set_term_wxt (void);
void set_term_qt (void);
void set_term_pdflatex_eps(void);
void set_term_pdflatex_cairo(void);
void set_output_filename (string output_filename);
void set_font (string font);
void set_font_size (int font_size);
void set_linewidth (double linewidth);
void set_graph_size_pdflatex (double widht_in_cm, double height_in_cm);
int get_terminal (void);
void add_latex_package (string latex_package);
};
int header_gnuplot::_cont_plots = 0;
/* -----------------------------------------------------------------------------------------*/
/* -----------------------------------------------------------------------------------------*/
/* -----------------------------------------------------------------------------------------*/
//CLASS CONTROL_GNUPLOT
class control_gnuplot {
protected:
FILE* _file;
bool _file_init;
control_gnuplot (void); //protected constructor
~control_gnuplot (void); //protected destructor
void send_command (string& script);
void send_command_replot (void);
void send_command_plot (string scrp);