]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/private/__gnuplot_get_var__.m
update packages
[CreaPhase.git] / octave_packages / m / plot / private / __gnuplot_get_var__.m
1 ## Copyright (C) 2009-2012 Ben Abbott
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {@var{value} =} __gnuplot_get_var__ (@var{h}, @var{name}, @var{fmt})
21 ## Undocumented internal function.
22 ## @end deftypefn
23
24 ## Author: Ben Abbott <bpabbott@mac.com>
25 ## Created: 2009-02-07
26
27 function gp_var_value = __gnuplot_get_var__ (h, gp_var_name, fmt)
28
29   if (nargin == 0)
30     h = gcf ();
31   endif
32   if (nargin < 2)
33     print_usage ();
34   endif
35   if (nargin < 3)
36     fmt = '';
37   endif
38
39   if (numel (h) == 1 && isfigure (h))
40     if (isempty (get (gcf, "__plot_stream__")))
41       ostream = __gnuplot_open_stream__ (2, h);
42     else
43       ostream = get (h, "__plot_stream__");
44     endif
45   else
46     ostream = h;
47   endif
48   if (numel (ostream) < 1)
49     error ("__gnuplot_get_var__: stream to gnuplot not open");
50   elseif (ispc ())
51     if (numel (ostream) == 1)
52       error ("__gnuplot_get_var__: Need mkfifo that is not implemented under Windows");
53     endif
54     use_mkfifo = false;
55     istream = ostream(2);
56     ostream = ostream(1);
57   else
58     use_mkfifo = true;
59     ostream = ostream(1);
60   endif
61
62   if (use_mkfifo)
63     gpin_name = tmpnam ();
64
65     ## Mode: 6*8*8 ==  0600
66     [err, msg] = mkfifo (gpin_name, 6*8*8);
67
68     if (err != 0)
69       error ("__gnuplot_get_var__: Can not make fifo (%s)", msg);
70     endif
71   endif
72
73   gp_var_name = strtrim (gp_var_name);
74   n = min (strfind (gp_var_name, " "), strfind (gp_var_name, ",")) - 1;
75   if (isempty (n))
76     n = numel (gp_var_name);
77   endif
78
79   unwind_protect
80
81     ## Notes: Variables may be undefined if user closes gnuplot by "q"
82     ## or Alt-F4. Further, this abrupt close also requires the leading
83     ## "\n" on the next line.
84     if (use_mkfifo)
85       fprintf (ostream, "\nset print \"%s\";\n", gpin_name);
86       fflush (ostream);
87       [gpin, err] = fopen (gpin_name, "r");
88       if (err != 0)
89         ## Try a second time, and then give an error.
90         [gpin, err] = fopen (gpin_name, "r");
91       endif
92       if (err != 0)
93         error ("__gnuplot_get_var__: can not open fifo");
94       endif
95       gp_cmd = sprintf ("\nif (exists(\"%s\")) print %s; else print NaN\n",
96                         gp_var_name(1:n), gp_var_name);
97       fputs (ostream, gp_cmd);
98
99       ## Close output file, to force it to be flushed
100       fputs (ostream, "set print;\n");
101       fflush (ostream);
102
103       ## Now read from fifo.
104       reading = true;
105       str = {};
106       while (reading)
107         str{end+1} = fgets (gpin);
108         if (isnumeric (str{end}) && (str{end} == -1))
109           reading = false;
110           str = str(1:(end-1));
111         endif
112       endwhile
113       str = strcat (str{:});
114       fclose (gpin);
115     else
116       ## Direct gnuplot to print to <STDOUT>
117       fprintf (ostream, "set print \"-\";\n");
118       fflush (ostream);
119       gp_cmd = sprintf ("\nif (exists(\"%s\")) print \"OCTAVE: \", %s; else print NaN\n",
120                         gp_var_name(1:n), gp_var_name);
121       fputs (ostream, gp_cmd);
122       fflush (ostream);
123       ## Direct gnuplot to print to <STDERR>
124       fputs (ostream, "set print;\n");
125       fflush (ostream);
126
127       str = {};
128       while (isempty (str))
129         str = char (fread (istream)');
130         if (isempty (str))
131           sleep (0.05);
132         else
133           str = regexp (str, 'OCTAVE:.*', "match");
134           str = str{end}(8:end);
135         endif
136         fclear (istream);
137       endwhile
138     endif
139
140     ## Strip out EOLs and the continuation character "|"
141     str(str=="\n") = "";
142     str(str=="\r") = "";
143     n_continue = strfind (str, " \\ ");
144     if (! isempty (n_continue))
145       str(n_continue+1) = "";
146     endif
147
148     if (isempty (fmt))
149       gp_var_value = strtrim (str);
150     else
151       gp_var_value = sscanf (str, fmt);
152     endif
153
154   unwind_protect_cleanup
155     if (use_mkfifo)
156       unlink (gpin_name);
157     endif
158   end_unwind_protect
159
160 endfunction
161