4343
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 2003, 2004, 2005, 2006, 2007 John W. Eaton |
4343
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
4343
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
4343
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <iostream> |
5765
|
28 #include <sstream> |
5164
|
29 #include <vector> |
4343
|
30 |
7336
|
31 #include "file-ops.h" |
|
32 |
4343
|
33 #include "defun.h" |
4654
|
34 #include "error.h" |
|
35 #include "gripes.h" |
5663
|
36 #include "input.h" |
4343
|
37 #include "oct-map.h" |
|
38 #include "ov-base.h" |
|
39 #include "ov-fcn-handle.h" |
4980
|
40 #include "ov-usr-fcn.h" |
4343
|
41 #include "pr-output.h" |
4980
|
42 #include "pt-pr-code.h" |
|
43 #include "pt-misc.h" |
|
44 #include "pt-stmt.h" |
|
45 #include "pt-cmd.h" |
|
46 #include "pt-exp.h" |
|
47 #include "pt-assign.h" |
4343
|
48 #include "variables.h" |
4988
|
49 #include "parse.h" |
6625
|
50 #include "unwind-prot.h" |
|
51 #include "defaults.h" |
|
52 #include "file-stat.h" |
|
53 #include "load-path.h" |
|
54 #include "oct-env.h" |
4988
|
55 |
|
56 #include "byte-swap.h" |
|
57 #include "ls-oct-ascii.h" |
6625
|
58 #include "ls-oct-binary.h" |
4988
|
59 #include "ls-hdf5.h" |
|
60 #include "ls-utils.h" |
4343
|
61 |
|
62 DEFINE_OCTAVE_ALLOCATOR (octave_fcn_handle); |
|
63 |
4612
|
64 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_fcn_handle, |
|
65 "function handle", |
5946
|
66 "function_handle"); |
4343
|
67 |
4924
|
68 octave_value_list |
|
69 octave_fcn_handle::subsref (const std::string& type, |
|
70 const std::list<octave_value_list>& idx, |
|
71 int nargout) |
|
72 { |
|
73 octave_value_list retval; |
|
74 |
|
75 switch (type[0]) |
|
76 { |
|
77 case '(': |
|
78 { |
7336
|
79 out_of_date_check (fcn); |
5663
|
80 |
7336
|
81 if (fcn.is_defined ()) |
|
82 { |
|
83 octave_function *f = function_value (); |
5663
|
84 |
7336
|
85 if (f) |
|
86 retval = f->subsref (type, idx, nargout); |
5663
|
87 else |
7336
|
88 error ("invalid function handle"); |
5663
|
89 } |
5312
|
90 else |
|
91 error ("invalid function handle"); |
4924
|
92 } |
|
93 break; |
|
94 |
|
95 case '{': |
|
96 case '.': |
|
97 { |
4930
|
98 std::string typ_nm = type_name (); |
|
99 error ("%s cannot be indexed with %c", typ_nm.c_str (), type[0]); |
4924
|
100 } |
|
101 break; |
|
102 |
|
103 default: |
|
104 panic_impossible (); |
|
105 } |
|
106 |
5775
|
107 // FIXME -- perhaps there should be an |
4924
|
108 // octave_value_list::next_subsref member function? See also |
|
109 // octave_builtin::subsref. |
|
110 |
|
111 if (idx.size () > 1) |
4994
|
112 retval = retval(0).next_subsref (nargout, type, idx); |
4924
|
113 |
|
114 return retval; |
|
115 } |
|
116 |
4988
|
117 bool |
6625
|
118 octave_fcn_handle::set_fcn (const std::string &octaveroot, |
|
119 const std::string& fpath) |
4988
|
120 { |
6625
|
121 bool success = true; |
|
122 |
|
123 if (octaveroot.length () != 0 && |
|
124 fpath.length () >= octaveroot.length () && |
|
125 fpath.substr (0, octaveroot.length ()) == octaveroot && |
|
126 OCTAVE_EXEC_PREFIX != octaveroot) |
|
127 { |
|
128 // First check if just replacing matlabroot is enough |
|
129 std::string str = OCTAVE_EXEC_PREFIX + |
|
130 fpath.substr (octaveroot.length ()); |
|
131 file_stat fs (str); |
|
132 |
|
133 if (fs.exists ()) |
|
134 { |
7336
|
135 size_t xpos = str.find_last_of (file_ops::dir_sep_chars); |
6625
|
136 |
7336
|
137 std::string dir_name = str.substr (0, xpos); |
6625
|
138 |
7336
|
139 octave_function *xfcn |
|
140 = load_fcn_from_file (str, dir_name, "", nm); |
6625
|
141 |
7336
|
142 if (xfcn) |
|
143 { |
|
144 octave_value tmp (xfcn); |
6625
|
145 |
7336
|
146 fcn = octave_value (new octave_fcn_handle (tmp, nm)); |
6625
|
147 } |
|
148 else |
|
149 { |
|
150 error ("function handle points to non-existent function"); |
|
151 success = false; |
|
152 } |
|
153 } |
|
154 else |
|
155 { |
|
156 // Next just search for it anywhere in the system path |
|
157 string_vector names(3); |
|
158 names(0) = nm + ".oct"; |
|
159 names(1) = nm + ".mex"; |
|
160 names(2) = nm + ".m"; |
|
161 |
6626
|
162 dir_path p (load_path::system_path ()); |
6625
|
163 |
|
164 str = octave_env::make_absolute |
|
165 (p.find_first_of (names), octave_env::getcwd ()); |
|
166 |
7336
|
167 size_t xpos = str.find_last_of (file_ops::dir_sep_chars); |
6625
|
168 |
7336
|
169 std::string dir_name = str.substr (0, xpos); |
|
170 |
|
171 octave_function *xfcn = load_fcn_from_file (str, dir_name, "", nm); |
4989
|
172 |
7336
|
173 if (xfcn) |
|
174 { |
|
175 octave_value tmp (xfcn); |
6625
|
176 |
7336
|
177 fcn = octave_value (new octave_fcn_handle (tmp, nm)); |
6625
|
178 } |
|
179 else |
|
180 { |
|
181 error ("function handle points to non-existent function"); |
|
182 success = false; |
|
183 } |
|
184 } |
|
185 } |
|
186 else |
|
187 { |
|
188 if (fpath.length () > 0) |
|
189 { |
7336
|
190 size_t xpos = fpath.find_last_of (file_ops::dir_sep_chars); |
6625
|
191 |
7336
|
192 std::string dir_name = fpath.substr (0, xpos); |
|
193 |
|
194 octave_function *xfcn = load_fcn_from_file (fpath, dir_name, "", nm); |
6625
|
195 |
7336
|
196 if (xfcn) |
|
197 { |
|
198 octave_value tmp (xfcn); |
6625
|
199 |
7336
|
200 fcn = octave_value (new octave_fcn_handle (tmp, nm)); |
6625
|
201 } |
|
202 else |
|
203 { |
|
204 error ("function handle points to non-existent function"); |
|
205 success = false; |
|
206 } |
|
207 } |
|
208 else |
|
209 { |
7336
|
210 fcn = symbol_table::find_function (nm); |
|
211 |
6625
|
212 if (! fcn.is_function ()) |
|
213 { |
|
214 error ("function handle points to non-existent function"); |
|
215 success = false; |
|
216 } |
|
217 } |
|
218 } |
|
219 |
|
220 return success; |
|
221 } |
|
222 |
|
223 bool |
6974
|
224 octave_fcn_handle::save_ascii (std::ostream& os) |
6625
|
225 { |
4988
|
226 if (nm == "@<anonymous>") |
|
227 { |
6625
|
228 os << nm << "\n"; |
|
229 |
4989
|
230 print_raw (os, true); |
|
231 os << "\n"; |
6625
|
232 |
7336
|
233 if (fcn.is_undefined ()) |
6625
|
234 return false; |
|
235 |
|
236 octave_user_function *f = fcn.user_function_value (); |
|
237 |
7336
|
238 std::list<symbol_table::symbol_record> vars |
|
239 = symbol_table::all_variables (f->scope ()); |
6625
|
240 |
7336
|
241 size_t varlen = vars.size (); |
6625
|
242 |
|
243 if (varlen > 0) |
|
244 { |
|
245 os << "# length: " << varlen << "\n"; |
|
246 |
7336
|
247 for (std::list<symbol_table::symbol_record>::const_iterator p = vars.begin (); |
|
248 p != vars.end (); p++) |
6625
|
249 { |
7336
|
250 if (! save_ascii_data (os, p->varval (), p->name (), false, 0)) |
6625
|
251 return os; |
|
252 } |
|
253 } |
|
254 } |
|
255 else |
|
256 { |
|
257 os << "# octaveroot: " << OCTAVE_EXEC_PREFIX << "\n"; |
|
258 os << "# path: " << user_function_value ()-> fcn_file_name () << "\n"; |
|
259 os << nm << "\n"; |
4988
|
260 } |
|
261 |
|
262 return true; |
|
263 } |
|
264 |
|
265 bool |
|
266 octave_fcn_handle::load_ascii (std::istream& is) |
|
267 { |
6625
|
268 bool success = true; |
|
269 |
|
270 std::streampos pos = is.tellg (); |
|
271 std::string octaveroot = extract_keyword (is, "octaveroot", true); |
|
272 if (octaveroot.length() == 0) |
|
273 { |
|
274 is.seekg (pos); |
|
275 is.clear (); |
|
276 } |
|
277 pos = is.tellg (); |
|
278 std::string fpath = extract_keyword (is, "path", true); |
|
279 if (fpath.length() == 0) |
|
280 { |
|
281 is.seekg (pos); |
|
282 is.clear (); |
|
283 } |
|
284 |
4988
|
285 is >> nm; |
4989
|
286 |
4988
|
287 if (nm == "@<anonymous>") |
|
288 { |
6625
|
289 octave_idx_type len = 0; |
4988
|
290 char c; |
5765
|
291 std::ostringstream buf; |
4988
|
292 |
4989
|
293 // Skip preceeding newline(s). |
|
294 while (is.get (c) && c == '\n') |
|
295 /* do nothing */; |
4988
|
296 |
|
297 if (is) |
|
298 { |
|
299 buf << c; |
|
300 |
|
301 // Get a line of text whitespace characters included, leaving |
4989
|
302 // newline in the stream. |
|
303 |
4988
|
304 while (is.peek () != '\n') |
|
305 { |
|
306 is.get (c); |
|
307 if (! is) |
|
308 break; |
|
309 buf << c; |
|
310 } |
|
311 } |
|
312 |
6625
|
313 pos = is.tellg (); |
7336
|
314 |
|
315 symbol_table::scope_id local_scope = symbol_table::alloc_scope (); |
4988
|
316 |
6625
|
317 if (extract_keyword (is, "length", len, true) && len >= 0) |
4989
|
318 { |
6625
|
319 if (len > 0) |
|
320 { |
|
321 for (octave_idx_type i = 0; i < len; i++) |
|
322 { |
|
323 octave_value t2; |
|
324 bool dummy; |
|
325 |
|
326 std::string name |
|
327 = read_ascii_data (is, std::string (), dummy, t2, i); |
|
328 |
|
329 if (!is) |
|
330 { |
|
331 error ("load: failed to load anonymous function handle"); |
|
332 break; |
|
333 } |
|
334 |
7336
|
335 symbol_table::varref (name, local_scope) = t2; |
6625
|
336 } |
|
337 } |
4989
|
338 } |
|
339 else |
6625
|
340 { |
|
341 is.seekg (pos); |
|
342 is.clear (); |
|
343 } |
|
344 |
|
345 if (is && success) |
|
346 { |
|
347 unwind_protect::begin_frame ("anon_ascii_load"); |
|
348 |
7336
|
349 symbol_table::push_scope (local_scope); |
|
350 |
|
351 unwind_protect::add (symbol_table::pop_scope); |
6625
|
352 |
|
353 int parse_status; |
|
354 octave_value anon_fcn_handle = |
|
355 eval_string (buf.str (), true, parse_status); |
|
356 |
|
357 if (parse_status == 0) |
|
358 { |
|
359 octave_fcn_handle *fh = |
|
360 anon_fcn_handle.fcn_handle_value (); |
|
361 if (fh) |
|
362 fcn = fh->fcn; |
|
363 else |
|
364 success = false; |
|
365 } |
|
366 else |
|
367 success = false; |
|
368 |
|
369 unwind_protect::run_frame ("anon_ascii_load"); |
|
370 } |
|
371 else |
|
372 success = false; |
|
373 |
7336
|
374 symbol_table::erase_scope (local_scope); |
4988
|
375 } |
|
376 else |
6625
|
377 success = set_fcn (octaveroot, fpath); |
4988
|
378 |
6625
|
379 return success; |
4988
|
380 } |
|
381 |
6625
|
382 /* |
|
383 |
|
384 %!test |
|
385 %! a = 2; |
|
386 %! f = @(x) a + x; |
|
387 %! g = @(x) 2 * x; |
|
388 %! h = @log2; |
|
389 %! f2 = f; |
|
390 %! g2 = g; |
|
391 %! h2 = h; |
|
392 %! nm = tmpnam(); |
|
393 %! unwind_protect |
|
394 %! save ("-text", nm, "f2", "g2", "h2"); |
|
395 %! clear f2 g2 h2 |
|
396 %! load (nm); |
|
397 %! assert (f(2),f2(2)); |
|
398 %! assert (g(2),g2(2)); |
|
399 %! assert (g(3),g2(3)); |
|
400 %! unlink (nm); |
|
401 %! save ("-text", nm, "f2", "g2", "h2"); |
|
402 %! unwind_protect_cleanup |
|
403 %! unlink (nm); |
|
404 %! end_unwind_protect |
|
405 |
|
406 */ |
|
407 |
4988
|
408 bool |
6625
|
409 octave_fcn_handle::save_binary (std::ostream& os, bool& save_as_floats) |
4988
|
410 { |
|
411 if (nm == "@<anonymous>") |
|
412 { |
6625
|
413 std::ostringstream nmbuf; |
|
414 |
7336
|
415 if (fcn.is_undefined ()) |
6625
|
416 return false; |
|
417 |
|
418 octave_user_function *f = fcn.user_function_value (); |
|
419 |
7336
|
420 std::list<symbol_table::symbol_record> vars |
|
421 = symbol_table::all_variables (f->scope ()); |
6625
|
422 |
7336
|
423 size_t varlen = vars.size (); |
6625
|
424 |
|
425 if (varlen > 0) |
|
426 nmbuf << nm << " " << varlen; |
|
427 else |
|
428 nmbuf << nm; |
|
429 |
|
430 std::string buf_str = nmbuf.str(); |
|
431 int32_t tmp = buf_str.length (); |
|
432 os.write (reinterpret_cast<char *> (&tmp), 4); |
|
433 os.write (buf_str.c_str (), buf_str.length ()); |
|
434 |
5765
|
435 std::ostringstream buf; |
4988
|
436 print_raw (buf, true); |
5765
|
437 std::string stmp = buf.str (); |
4988
|
438 tmp = stmp.length (); |
5760
|
439 os.write (reinterpret_cast<char *> (&tmp), 4); |
4988
|
440 os.write (stmp.c_str (), stmp.length ()); |
6625
|
441 |
|
442 if (varlen > 0) |
|
443 { |
7336
|
444 for (std::list<symbol_table::symbol_record>::const_iterator p = vars.begin (); |
|
445 p != vars.end (); p++) |
6625
|
446 { |
7336
|
447 if (! save_binary_data (os, p->varval (), p->name (), |
6625
|
448 "", 0, save_as_floats)) |
|
449 return os; |
|
450 } |
|
451 } |
|
452 } |
|
453 else |
|
454 { |
|
455 std::ostringstream nmbuf; |
|
456 |
|
457 nmbuf << nm << "\n" << OCTAVE_EXEC_PREFIX << "\n" |
|
458 << user_function_value ()-> fcn_file_name () ; |
|
459 |
|
460 std::string buf_str = nmbuf.str (); |
|
461 int32_t tmp = buf_str.length (); |
|
462 os.write (reinterpret_cast<char *> (&tmp), 4); |
|
463 os.write (buf_str.c_str (), buf_str.length ()); |
4988
|
464 } |
7336
|
465 |
4988
|
466 return true; |
|
467 } |
|
468 |
|
469 bool |
|
470 octave_fcn_handle::load_binary (std::istream& is, bool swap, |
6625
|
471 oct_mach_info::float_format fmt) |
4988
|
472 { |
6625
|
473 bool success = true; |
7336
|
474 |
5828
|
475 int32_t tmp; |
5760
|
476 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
4988
|
477 return false; |
|
478 if (swap) |
|
479 swap_bytes<4> (&tmp); |
|
480 |
|
481 OCTAVE_LOCAL_BUFFER (char, ctmp1, tmp+1); |
|
482 is.read (ctmp1, tmp); |
|
483 nm = std::string (ctmp1); |
|
484 |
|
485 if (! is) |
|
486 return false; |
|
487 |
6625
|
488 if (nm.length() >= 12 && nm.substr (0, 12) == "@<anonymous>") |
4988
|
489 { |
6625
|
490 octave_idx_type len = 0; |
|
491 |
|
492 if (nm.length() > 12) |
|
493 { |
|
494 std::istringstream nm_is (nm.substr(12)); |
|
495 nm_is >> len; |
|
496 nm = nm.substr(0,12); |
|
497 } |
|
498 |
5760
|
499 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
4988
|
500 return false; |
|
501 if (swap) |
|
502 swap_bytes<4> (&tmp); |
|
503 |
|
504 OCTAVE_LOCAL_BUFFER (char, ctmp2, tmp+1); |
|
505 is.read (ctmp2, tmp); |
|
506 |
7336
|
507 symbol_table::scope_id local_scope = symbol_table::alloc_scope (); |
|
508 |
6625
|
509 if (len > 0) |
4989
|
510 { |
6625
|
511 for (octave_idx_type i = 0; i < len; i++) |
|
512 { |
|
513 octave_value t2; |
|
514 bool dummy; |
|
515 std::string doc; |
|
516 |
|
517 std::string name = |
|
518 read_binary_data (is, swap, fmt, std::string (), |
|
519 dummy, t2, doc); |
|
520 |
|
521 if (!is) |
|
522 { |
|
523 error ("load: failed to load anonymous function handle"); |
|
524 break; |
|
525 } |
|
526 |
7336
|
527 symbol_table::varref (name, local_scope) = t2; |
6625
|
528 } |
|
529 } |
|
530 |
|
531 if (is && success) |
|
532 { |
|
533 unwind_protect::begin_frame ("anon_binary_load"); |
|
534 |
7336
|
535 symbol_table::push_scope (local_scope); |
|
536 |
|
537 unwind_protect::add (symbol_table::pop_scope); |
6625
|
538 |
|
539 int parse_status; |
|
540 octave_value anon_fcn_handle = |
|
541 eval_string (ctmp2, true, parse_status); |
|
542 |
|
543 if (parse_status == 0) |
|
544 { |
|
545 octave_fcn_handle *fh = anon_fcn_handle.fcn_handle_value (); |
|
546 if (fh) |
|
547 fcn = fh->fcn; |
|
548 else |
|
549 success = false; |
|
550 } |
4989
|
551 else |
6625
|
552 success = false; |
|
553 |
|
554 unwind_protect::run_frame ("anon_binary_load"); |
4989
|
555 } |
6625
|
556 |
7336
|
557 symbol_table::erase_scope (local_scope); |
4988
|
558 } |
|
559 else |
|
560 { |
6625
|
561 std::string octaveroot; |
|
562 std::string fpath; |
|
563 |
|
564 if (nm.find_first_of ("\n") != NPOS) |
6225
|
565 { |
6625
|
566 size_t pos1 = nm.find_first_of ("\n"); |
|
567 size_t pos2 = nm.find_first_of ("\n", pos1 + 1); |
|
568 octaveroot = nm.substr (pos1 + 1, pos2 - pos1 - 1); |
|
569 fpath = nm.substr (pos2 + 1); |
|
570 nm = nm.substr (0, pos1); |
6225
|
571 } |
6625
|
572 |
|
573 success = set_fcn (octaveroot, fpath); |
|
574 } |
|
575 |
|
576 return success; |
4988
|
577 } |
|
578 |
6625
|
579 /* |
|
580 |
|
581 %!test |
|
582 %! a = 2; |
|
583 %! f = @(x) a + x; |
|
584 %! g = @(x) 2 * x; |
|
585 %! h = @log2; |
|
586 %! f2 = f; |
|
587 %! g2 = g; |
|
588 %! h2 = h; |
|
589 %! nm = tmpnam(); |
|
590 %! unwind_protect |
|
591 %! save ("-binary", nm, "f2", "g2", "h2"); |
|
592 %! clear f2 g2 h2 |
|
593 %! load (nm); |
|
594 %! assert (f(2),f2(2)); |
|
595 %! assert (g(2),g2(2)); |
|
596 %! assert (g(3),g2(3)); |
|
597 %! unlink (nm); |
|
598 %! save ("-binary", nm, "f2", "g2", "h2"); |
|
599 %! unwind_protect_cleanup |
|
600 %! unlink (nm); |
|
601 %! end_unwind_protect |
|
602 |
|
603 */ |
|
604 |
4988
|
605 #if defined (HAVE_HDF5) |
|
606 bool |
|
607 octave_fcn_handle::save_hdf5 (hid_t loc_id, const char *name, |
6625
|
608 bool save_as_floats) |
4988
|
609 { |
7336
|
610 bool retval = true; |
|
611 |
4988
|
612 hid_t group_hid = -1; |
|
613 group_hid = H5Gcreate (loc_id, name, 0); |
7336
|
614 if (group_hid < 0) |
|
615 return false; |
4988
|
616 |
|
617 hid_t space_hid = -1, data_hid = -1, type_hid = -1;; |
|
618 |
|
619 // attach the type of the variable |
|
620 type_hid = H5Tcopy (H5T_C_S1); |
|
621 H5Tset_size (type_hid, nm.length () + 1); |
|
622 if (type_hid < 0) |
|
623 { |
|
624 H5Gclose (group_hid); |
|
625 return false; |
|
626 } |
|
627 |
|
628 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, 2); |
|
629 hdims[0] = 0; |
|
630 hdims[1] = 0; |
5760
|
631 space_hid = H5Screate_simple (0 , hdims, 0); |
4988
|
632 if (space_hid < 0) |
|
633 { |
|
634 H5Tclose (type_hid); |
|
635 H5Gclose (group_hid); |
|
636 return false; |
|
637 } |
|
638 |
|
639 data_hid = H5Dcreate (group_hid, "nm", type_hid, space_hid, H5P_DEFAULT); |
|
640 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, |
5760
|
641 H5P_DEFAULT, nm.c_str ()) < 0) |
4988
|
642 { |
|
643 H5Sclose (space_hid); |
|
644 H5Tclose (type_hid); |
|
645 H5Gclose (group_hid); |
|
646 return false; |
|
647 } |
|
648 H5Dclose (data_hid); |
|
649 |
|
650 if (nm == "@<anonymous>") |
|
651 { |
5765
|
652 std::ostringstream buf; |
4988
|
653 print_raw (buf, true); |
5765
|
654 std::string stmp = buf.str (); |
4988
|
655 |
|
656 // attach the type of the variable |
|
657 H5Tset_size (type_hid, stmp.length () + 1); |
|
658 if (type_hid < 0) |
|
659 { |
6695
|
660 H5Sclose (space_hid); |
4988
|
661 H5Gclose (group_hid); |
|
662 return false; |
|
663 } |
|
664 |
|
665 data_hid = H5Dcreate (group_hid, "fcn", type_hid, space_hid, |
|
666 H5P_DEFAULT); |
|
667 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, |
5760
|
668 H5P_DEFAULT, stmp.c_str ()) < 0) |
4988
|
669 { |
|
670 H5Sclose (space_hid); |
|
671 H5Tclose (type_hid); |
|
672 H5Gclose (group_hid); |
|
673 return false; |
|
674 } |
|
675 |
|
676 H5Dclose (data_hid); |
6625
|
677 |
|
678 octave_user_function *f = fcn.user_function_value (); |
|
679 |
7336
|
680 std::list<symbol_table::symbol_record> vars |
|
681 = symbol_table::all_variables (f->scope ()); |
|
682 |
|
683 size_t varlen = vars.size (); |
6625
|
684 |
|
685 if (varlen > 0) |
|
686 { |
|
687 hid_t as_id = H5Screate (H5S_SCALAR); |
|
688 |
|
689 if (as_id >= 0) |
|
690 { |
|
691 hid_t a_id = H5Acreate (group_hid, "SYMBOL_TABLE", |
|
692 H5T_NATIVE_IDX, as_id, H5P_DEFAULT); |
|
693 |
|
694 if (a_id >= 0) |
|
695 { |
|
696 retval = (H5Awrite (a_id, H5T_NATIVE_IDX, &varlen) >= 0); |
|
697 |
|
698 H5Aclose (a_id); |
|
699 } |
|
700 else |
|
701 retval = false; |
|
702 |
|
703 H5Sclose (as_id); |
|
704 } |
|
705 else |
|
706 retval = false; |
|
707 |
|
708 data_hid = H5Gcreate (group_hid, "symbol table", 0); |
|
709 if (data_hid < 0) |
|
710 { |
|
711 H5Sclose (space_hid); |
|
712 H5Tclose (type_hid); |
|
713 H5Gclose (group_hid); |
|
714 return false; |
|
715 } |
|
716 |
7336
|
717 for (std::list<symbol_table::symbol_record>::const_iterator p = vars.begin (); |
|
718 p != vars.end (); p++) |
6625
|
719 { |
7336
|
720 if (! add_hdf5_data (data_hid, p->varval (), p->name (), |
6625
|
721 "", false, save_as_floats)) |
|
722 break; |
|
723 } |
|
724 H5Gclose (data_hid); |
|
725 } |
|
726 } |
|
727 else |
|
728 { |
|
729 std::string octaveroot = OCTAVE_EXEC_PREFIX; |
|
730 std::string fpath = user_function_value ()-> fcn_file_name (); |
|
731 |
|
732 H5Sclose (space_hid); |
|
733 hdims[0] = 1; |
|
734 hdims[1] = octaveroot.length (); |
|
735 space_hid = H5Screate_simple (0 , hdims, 0); |
|
736 if (space_hid < 0) |
|
737 { |
|
738 H5Tclose (type_hid); |
|
739 H5Gclose (group_hid); |
|
740 return false; |
|
741 } |
|
742 |
|
743 H5Tclose (type_hid); |
|
744 type_hid = H5Tcopy (H5T_C_S1); |
|
745 H5Tset_size (type_hid, octaveroot.length () + 1); |
|
746 |
|
747 hid_t a_id = H5Acreate (group_hid, "OCTAVEROOT", |
|
748 type_hid, space_hid, H5P_DEFAULT); |
|
749 |
|
750 if (a_id >= 0) |
|
751 { |
|
752 retval = (H5Awrite (a_id, type_hid, octaveroot.c_str ()) >= 0); |
|
753 |
|
754 H5Aclose (a_id); |
|
755 } |
|
756 else |
6695
|
757 { |
|
758 H5Sclose (space_hid); |
|
759 H5Tclose (type_hid); |
|
760 H5Gclose (group_hid); |
|
761 return false; |
|
762 } |
6625
|
763 |
|
764 H5Sclose (space_hid); |
|
765 hdims[0] = 1; |
|
766 hdims[1] = fpath.length (); |
|
767 space_hid = H5Screate_simple (0 , hdims, 0); |
|
768 if (space_hid < 0) |
|
769 { |
|
770 H5Tclose (type_hid); |
|
771 H5Gclose (group_hid); |
|
772 return false; |
|
773 } |
|
774 |
|
775 H5Tclose (type_hid); |
|
776 type_hid = H5Tcopy (H5T_C_S1); |
|
777 H5Tset_size (type_hid, fpath.length () + 1); |
|
778 |
|
779 a_id = H5Acreate (group_hid, "FILE", type_hid, space_hid, H5P_DEFAULT); |
|
780 |
|
781 if (a_id >= 0) |
|
782 { |
|
783 retval = (H5Awrite (a_id, type_hid, fpath.c_str ()) >= 0); |
|
784 |
|
785 H5Aclose (a_id); |
|
786 } |
|
787 else |
|
788 retval = false; |
4988
|
789 } |
|
790 |
|
791 H5Sclose (space_hid); |
|
792 H5Tclose (type_hid); |
|
793 H5Gclose (group_hid); |
|
794 |
|
795 return retval; |
|
796 } |
|
797 |
|
798 bool |
|
799 octave_fcn_handle::load_hdf5 (hid_t loc_id, const char *name, |
6625
|
800 bool have_h5giterate_bug) |
4988
|
801 { |
7336
|
802 bool success = true; |
|
803 |
4988
|
804 hid_t group_hid, data_hid, space_hid, type_hid, type_class_hid, st_id; |
|
805 hsize_t rank; |
|
806 int slen; |
|
807 |
|
808 group_hid = H5Gopen (loc_id, name); |
7336
|
809 if (group_hid < 0) |
|
810 return false; |
4988
|
811 |
|
812 data_hid = H5Dopen (group_hid, "nm"); |
|
813 |
|
814 if (data_hid < 0) |
|
815 { |
|
816 H5Gclose (group_hid); |
|
817 return false; |
|
818 } |
|
819 |
|
820 type_hid = H5Dget_type (data_hid); |
|
821 type_class_hid = H5Tget_class (type_hid); |
|
822 |
|
823 if (type_class_hid != H5T_STRING) |
|
824 { |
|
825 H5Tclose (type_hid); |
|
826 H5Dclose (data_hid); |
|
827 H5Gclose (group_hid); |
|
828 return false; |
|
829 } |
|
830 |
|
831 space_hid = H5Dget_space (data_hid); |
|
832 rank = H5Sget_simple_extent_ndims (space_hid); |
|
833 |
|
834 if (rank != 0) |
|
835 { |
|
836 H5Sclose (space_hid); |
|
837 H5Tclose (type_hid); |
|
838 H5Dclose (data_hid); |
|
839 H5Gclose (group_hid); |
|
840 return false; |
|
841 } |
|
842 |
|
843 slen = H5Tget_size (type_hid); |
|
844 if (slen < 0) |
|
845 { |
|
846 H5Sclose (space_hid); |
|
847 H5Tclose (type_hid); |
|
848 H5Dclose (data_hid); |
|
849 H5Gclose (group_hid); |
|
850 return false; |
|
851 } |
|
852 |
|
853 OCTAVE_LOCAL_BUFFER (char, nm_tmp, slen); |
|
854 |
|
855 // create datatype for (null-terminated) string to read into: |
|
856 st_id = H5Tcopy (H5T_C_S1); |
|
857 H5Tset_size (st_id, slen); |
|
858 |
5760
|
859 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, nm_tmp) < 0) |
4988
|
860 { |
6695
|
861 H5Tclose (st_id); |
4988
|
862 H5Sclose (space_hid); |
|
863 H5Tclose (type_hid); |
6695
|
864 H5Dclose (data_hid); |
4988
|
865 H5Gclose (group_hid); |
|
866 return false; |
|
867 } |
|
868 H5Tclose (st_id); |
|
869 H5Dclose (data_hid); |
|
870 nm = nm_tmp; |
|
871 |
|
872 if (nm == "@<anonymous>") |
|
873 { |
|
874 data_hid = H5Dopen (group_hid, "fcn"); |
|
875 |
|
876 if (data_hid < 0) |
|
877 { |
6695
|
878 H5Sclose (space_hid); |
|
879 H5Tclose (type_hid); |
4988
|
880 H5Gclose (group_hid); |
|
881 return false; |
|
882 } |
|
883 |
6695
|
884 H5Tclose (type_hid); |
4988
|
885 type_hid = H5Dget_type (data_hid); |
|
886 type_class_hid = H5Tget_class (type_hid); |
|
887 |
|
888 if (type_class_hid != H5T_STRING) |
|
889 { |
6695
|
890 H5Sclose (space_hid); |
4988
|
891 H5Tclose (type_hid); |
|
892 H5Dclose (data_hid); |
|
893 H5Gclose (group_hid); |
|
894 return false; |
|
895 } |
|
896 |
6695
|
897 H5Sclose (space_hid); |
4988
|
898 space_hid = H5Dget_space (data_hid); |
|
899 rank = H5Sget_simple_extent_ndims (space_hid); |
|
900 |
|
901 if (rank != 0) |
|
902 { |
|
903 H5Sclose (space_hid); |
|
904 H5Tclose (type_hid); |
|
905 H5Dclose (data_hid); |
|
906 H5Gclose (group_hid); |
|
907 return false; |
|
908 } |
|
909 |
|
910 slen = H5Tget_size (type_hid); |
|
911 if (slen < 0) |
|
912 { |
|
913 H5Sclose (space_hid); |
|
914 H5Tclose (type_hid); |
|
915 H5Dclose (data_hid); |
|
916 H5Gclose (group_hid); |
|
917 return false; |
|
918 } |
|
919 |
|
920 OCTAVE_LOCAL_BUFFER (char, fcn_tmp, slen); |
|
921 |
|
922 // create datatype for (null-terminated) string to read into: |
|
923 st_id = H5Tcopy (H5T_C_S1); |
|
924 H5Tset_size (st_id, slen); |
|
925 |
5760
|
926 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, fcn_tmp) < 0) |
4988
|
927 { |
6695
|
928 H5Tclose (st_id); |
4988
|
929 H5Sclose (space_hid); |
|
930 H5Tclose (type_hid); |
6695
|
931 H5Dclose (data_hid); |
4988
|
932 H5Gclose (group_hid); |
|
933 return false; |
|
934 } |
6695
|
935 H5Tclose (st_id); |
4988
|
936 H5Dclose (data_hid); |
6625
|
937 |
|
938 octave_idx_type len = 0; |
|
939 |
|
940 // we have to pull some shenanigans here to make sure |
|
941 // HDF5 doesn't print out all sorts of error messages if we |
|
942 // call H5Aopen for a non-existing attribute |
|
943 |
|
944 H5E_auto_t err_func; |
|
945 void *err_func_data; |
4988
|
946 |
6625
|
947 // turn off error reporting temporarily, but save the error |
|
948 // reporting function: |
|
949 H5Eget_auto (&err_func, &err_func_data); |
|
950 H5Eset_auto (0, 0); |
|
951 |
|
952 hid_t attr_id = H5Aopen_name (group_hid, "SYMBOL_TABLE"); |
4988
|
953 |
6625
|
954 if (attr_id >= 0) |
|
955 { |
|
956 if (H5Aread (attr_id, H5T_NATIVE_IDX, &len) < 0) |
|
957 success = false; |
|
958 |
|
959 H5Aclose (attr_id); |
|
960 } |
|
961 |
|
962 // restore error reporting: |
|
963 H5Eset_auto (err_func, err_func_data); |
|
964 |
7336
|
965 symbol_table::scope_id local_scope = symbol_table::alloc_scope (); |
|
966 |
6625
|
967 if (len > 0 && success) |
4989
|
968 { |
6625
|
969 #ifdef HAVE_H5GGET_NUM_OBJS |
|
970 hsize_t num_obj = 0; |
|
971 data_hid = H5Gopen (group_hid, "symbol table"); |
|
972 H5Gget_num_objs (data_hid, &num_obj); |
|
973 H5Gclose (data_hid); |
|
974 |
|
975 if (num_obj != static_cast<hsize_t>(len)) |
|
976 { |
|
977 error ("load: failed to load anonymous function handle"); |
|
978 success = false; |
|
979 } |
|
980 #endif |
|
981 |
|
982 if (! error_state) |
|
983 { |
|
984 hdf5_callback_data dsub; |
|
985 int current_item = 0; |
|
986 for (octave_idx_type i = 0; i < len; i++) |
|
987 { |
|
988 if (H5Giterate (group_hid, "symbol table", ¤t_item, |
|
989 hdf5_read_next_data, &dsub) <= 0) |
|
990 { |
|
991 error ("load: failed to load anonymous function handle"); |
|
992 success = false; |
|
993 break; |
|
994 } |
|
995 |
|
996 if (have_h5giterate_bug) |
|
997 current_item++; // H5Giterate returns last index processed |
|
998 |
7336
|
999 symbol_table::varref (dsub.name, local_scope) = dsub.tc; |
6625
|
1000 } |
|
1001 } |
|
1002 } |
|
1003 |
|
1004 if (success) |
|
1005 { |
|
1006 unwind_protect::begin_frame ("anon_hdf5_load"); |
|
1007 |
7336
|
1008 symbol_table::push_scope (local_scope); |
|
1009 |
|
1010 unwind_protect::add (symbol_table::pop_scope); |
6625
|
1011 |
|
1012 int parse_status; |
|
1013 octave_value anon_fcn_handle = |
|
1014 eval_string (fcn_tmp, true, parse_status); |
|
1015 |
|
1016 if (parse_status == 0) |
|
1017 { |
|
1018 octave_fcn_handle *fh = anon_fcn_handle.fcn_handle_value (); |
|
1019 if (fh) |
|
1020 fcn = fh->fcn; |
|
1021 else |
|
1022 success = false; |
|
1023 } |
4989
|
1024 else |
6625
|
1025 success = false; |
|
1026 |
|
1027 unwind_protect::run_frame ("anon_hdf5_load"); |
4989
|
1028 } |
6625
|
1029 |
7336
|
1030 symbol_table::erase_scope (local_scope); |
4988
|
1031 } |
|
1032 else |
|
1033 { |
6625
|
1034 std::string octaveroot; |
|
1035 std::string fpath; |
|
1036 |
|
1037 // we have to pull some shenanigans here to make sure |
|
1038 // HDF5 doesn't print out all sorts of error messages if we |
|
1039 // call H5Aopen for a non-existing attribute |
|
1040 |
|
1041 H5E_auto_t err_func; |
|
1042 void *err_func_data; |
|
1043 |
|
1044 // turn off error reporting temporarily, but save the error |
|
1045 // reporting function: |
|
1046 H5Eget_auto (&err_func, &err_func_data); |
|
1047 H5Eset_auto (0, 0); |
|
1048 |
|
1049 hid_t attr_id = H5Aopen_name (group_hid, "OCTAVEROOT"); |
|
1050 if (attr_id >= 0) |
6225
|
1051 { |
6695
|
1052 H5Tclose (type_hid); |
6625
|
1053 type_hid = H5Aget_type (attr_id); |
|
1054 type_class_hid = H5Tget_class (type_hid); |
|
1055 |
|
1056 if (type_class_hid != H5T_STRING) |
|
1057 success = false; |
|
1058 else |
|
1059 { |
|
1060 slen = H5Tget_size (type_hid); |
|
1061 st_id = H5Tcopy (H5T_C_S1); |
|
1062 H5Tset_size (st_id, slen); |
|
1063 OCTAVE_LOCAL_BUFFER (char, root_tmp, slen); |
|
1064 |
|
1065 if (H5Aread (attr_id, st_id, root_tmp) < 0) |
|
1066 success = false; |
|
1067 else |
|
1068 octaveroot = root_tmp; |
6695
|
1069 |
|
1070 H5Tclose (st_id); |
6625
|
1071 } |
|
1072 |
|
1073 H5Aclose (attr_id); |
6225
|
1074 } |
6625
|
1075 |
6695
|
1076 if (success) |
6625
|
1077 { |
6695
|
1078 attr_id = H5Aopen_name (group_hid, "FILE"); |
|
1079 if (attr_id >= 0) |
|
1080 { |
|
1081 H5Tclose (type_hid); |
|
1082 type_hid = H5Aget_type (attr_id); |
|
1083 type_class_hid = H5Tget_class (type_hid); |
6625
|
1084 |
6695
|
1085 if (type_class_hid != H5T_STRING) |
6625
|
1086 success = false; |
|
1087 else |
6695
|
1088 { |
|
1089 slen = H5Tget_size (type_hid); |
|
1090 st_id = H5Tcopy (H5T_C_S1); |
|
1091 H5Tset_size (st_id, slen); |
|
1092 OCTAVE_LOCAL_BUFFER (char, path_tmp, slen); |
|
1093 |
|
1094 if (H5Aread (attr_id, st_id, path_tmp) < 0) |
|
1095 success = false; |
|
1096 else |
|
1097 fpath = path_tmp; |
|
1098 |
|
1099 H5Tclose (st_id); |
|
1100 } |
|
1101 |
|
1102 H5Aclose (attr_id); |
6625
|
1103 } |
|
1104 } |
|
1105 |
|
1106 // restore error reporting: |
|
1107 H5Eset_auto (err_func, err_func_data); |
|
1108 |
|
1109 success = (success ? set_fcn (octaveroot, fpath) : success); |
4988
|
1110 } |
|
1111 |
6695
|
1112 H5Tclose (type_hid); |
|
1113 H5Sclose (space_hid); |
|
1114 H5Gclose (group_hid); |
|
1115 |
6625
|
1116 return success; |
4988
|
1117 } |
6625
|
1118 |
|
1119 /* |
|
1120 |
|
1121 %!test |
|
1122 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_HDF5"))) |
|
1123 %! a = 2; |
|
1124 %! f = @(x) a + x; |
|
1125 %! g = @(x) 2 * x; |
|
1126 %! h = @log2; |
|
1127 %! f2 = f; |
|
1128 %! g2 = g; |
|
1129 %! h2 = h; |
|
1130 %! nm = tmpnam(); |
|
1131 %! unwind_protect |
|
1132 %! save ("-hdf5", nm, "f2", "g2", "h2"); |
|
1133 %! clear f2 g2 h2 |
|
1134 %! load (nm); |
|
1135 %! assert (f(2),f2(2)); |
|
1136 %! assert (g(2),g2(2)); |
|
1137 %! assert (g(3),g2(3)); |
|
1138 %! unlink (nm); |
|
1139 %! save ("-hdf5", nm, "f2", "g2", "h2"); |
|
1140 %! unwind_protect_cleanup |
|
1141 %! unlink (nm); |
|
1142 %! end_unwind_protect |
|
1143 %! endif |
|
1144 |
|
1145 */ |
4988
|
1146 #endif |
|
1147 |
4343
|
1148 void |
|
1149 octave_fcn_handle::print (std::ostream& os, bool pr_as_read_syntax) const |
|
1150 { |
|
1151 print_raw (os, pr_as_read_syntax); |
|
1152 newline (os); |
|
1153 } |
|
1154 |
|
1155 void |
|
1156 octave_fcn_handle::print_raw (std::ostream& os, bool pr_as_read_syntax) const |
|
1157 { |
4980
|
1158 bool printed = false; |
|
1159 |
|
1160 if (nm == "@<anonymous>") |
|
1161 { |
|
1162 tree_print_code tpc (os); |
|
1163 |
4989
|
1164 // FCN is const because this member function is, so we can't |
4980
|
1165 // use it to call user_function_value, so we make a copy first. |
|
1166 |
|
1167 octave_value ftmp = fcn; |
|
1168 |
|
1169 octave_user_function *f = ftmp.user_function_value (); |
|
1170 |
|
1171 if (f) |
|
1172 { |
|
1173 tree_parameter_list *p = f->parameter_list (); |
|
1174 |
|
1175 os << "@("; |
|
1176 |
|
1177 if (p) |
|
1178 p->accept (tpc); |
|
1179 |
|
1180 os << ") "; |
|
1181 |
|
1182 tree_statement_list *b = f->body (); |
|
1183 |
|
1184 if (b) |
|
1185 { |
|
1186 assert (b->length () == 1); |
|
1187 |
|
1188 tree_statement *s = b->front (); |
|
1189 |
|
1190 if (s) |
|
1191 { |
|
1192 if (s->is_expression ()) |
|
1193 { |
|
1194 tree_expression *e = s->expression (); |
|
1195 |
|
1196 if (e) |
6657
|
1197 e->accept (tpc); |
4980
|
1198 } |
|
1199 else |
|
1200 { |
|
1201 tree_command *c = s->command (); |
|
1202 |
|
1203 tpc.suspend_newline (); |
|
1204 c->accept (tpc); |
|
1205 tpc.resume_newline (); |
|
1206 } |
|
1207 } |
|
1208 } |
|
1209 |
|
1210 printed = true; |
|
1211 } |
|
1212 } |
|
1213 |
|
1214 if (! printed) |
|
1215 octave_print_internal (os, nm, pr_as_read_syntax, |
|
1216 current_print_indent_level ()); |
4343
|
1217 } |
|
1218 |
|
1219 octave_value |
|
1220 make_fcn_handle (const std::string& nm) |
|
1221 { |
|
1222 octave_value retval; |
|
1223 |
7336
|
1224 octave_value f = symbol_table::find_function (nm); |
6481
|
1225 |
7336
|
1226 if (f.is_defined ()) |
4930
|
1227 retval = octave_value (new octave_fcn_handle (f, nm)); |
4343
|
1228 else |
|
1229 error ("error creating function handle \"@%s\"", nm.c_str ()); |
|
1230 |
|
1231 return retval; |
|
1232 } |
|
1233 |
4933
|
1234 DEFUN (functions, args, , |
4343
|
1235 "-*- texinfo -*-\n\ |
4933
|
1236 @deftypefn {Built-in Function} {} functions (@var{fcn_handle})\n\ |
|
1237 Return a struct containing information about the function handle\n\ |
|
1238 @var{fcn_handle}.\n\ |
|
1239 @end deftypefn") |
4343
|
1240 { |
|
1241 octave_value retval; |
|
1242 |
4933
|
1243 if (args.length () == 1) |
4343
|
1244 { |
4933
|
1245 octave_fcn_handle *fh = args(0).fcn_handle_value (); |
4343
|
1246 |
|
1247 if (! error_state) |
|
1248 { |
4933
|
1249 octave_function *fcn = fh ? fh->function_value (true) : 0; |
4343
|
1250 |
4933
|
1251 if (fcn) |
4930
|
1252 { |
4933
|
1253 Octave_map m; |
4649
|
1254 |
4933
|
1255 std::string fh_nm = fh->fcn_name (); |
|
1256 |
6625
|
1257 if (fh_nm == "@<anonymous>") |
|
1258 { |
|
1259 std::ostringstream buf; |
|
1260 fh->print_raw (buf); |
|
1261 m.assign ("function", buf.str ()); |
|
1262 |
|
1263 m.assign ("type", "anonymous"); |
|
1264 } |
|
1265 else |
|
1266 { |
|
1267 m.assign ("function", fh_nm); |
4343
|
1268 |
6625
|
1269 if (fcn->is_nested_function ()) |
|
1270 { |
|
1271 m.assign ("type", "subfunction"); |
|
1272 Cell parentage (dim_vector (1, 2)); |
|
1273 parentage.elem(0) = fh_nm; |
|
1274 parentage.elem(1) = fcn->parent_fcn_name (); |
|
1275 m.assign ("parentage", parentage); |
|
1276 } |
|
1277 else |
|
1278 m.assign ("type", "simple"); |
|
1279 } |
4933
|
1280 |
|
1281 std::string nm = fcn->fcn_file_name (); |
4343
|
1282 |
4933
|
1283 if (nm.empty ()) |
4935
|
1284 { |
|
1285 if (fh_nm == "@<anonymous>") |
6625
|
1286 { |
|
1287 m.assign ("file", ""); |
|
1288 |
|
1289 octave_user_function *fu = fh->user_function_value (); |
|
1290 |
7336
|
1291 std::list<symbol_table::symbol_record> vars |
|
1292 = symbol_table::all_variables (fu->scope ()); |
|
1293 |
|
1294 size_t varlen = vars.size (); |
6625
|
1295 |
|
1296 if (varlen > 0) |
|
1297 { |
|
1298 Octave_map ws; |
7336
|
1299 for (std::list<symbol_table::symbol_record>::const_iterator p = vars.begin (); |
|
1300 p != vars.end (); p++) |
6625
|
1301 { |
7336
|
1302 ws.assign (p->name (), p->varval ()); |
6625
|
1303 } |
|
1304 |
|
1305 m.assign ("workspace", ws); |
|
1306 } |
|
1307 } |
5313
|
1308 else if (fcn->is_user_function ()) |
6625
|
1309 { |
|
1310 octave_user_function *fu = fh->user_function_value (); |
|
1311 m.assign ("file", fu->fcn_file_name ()); |
|
1312 } |
4935
|
1313 else |
6625
|
1314 m.assign ("file", ""); |
4935
|
1315 } |
4343
|
1316 else |
4933
|
1317 m.assign ("file", nm); |
|
1318 |
|
1319 retval = m; |
4343
|
1320 } |
|
1321 else |
4933
|
1322 error ("functions: invalid function handle object"); |
4343
|
1323 } |
|
1324 else |
4933
|
1325 error ("functions: argument must be a function handle object"); |
4343
|
1326 } |
|
1327 else |
5823
|
1328 print_usage (); |
4343
|
1329 |
|
1330 return retval; |
|
1331 } |
|
1332 |
4933
|
1333 DEFUN (func2str, args, , |
4343
|
1334 "-*- texinfo -*-\n\ |
4933
|
1335 @deftypefn {Built-in Function} {} func2str (@var{fcn_handle})\n\ |
|
1336 Return a string containing the name of the function referenced by\n\ |
|
1337 the function handle @var{fcn_handle}.\n\ |
|
1338 @end deftypefn") |
4343
|
1339 { |
|
1340 octave_value retval; |
|
1341 |
4933
|
1342 if (args.length () == 1) |
4930
|
1343 { |
4933
|
1344 octave_fcn_handle *fh = args(0).fcn_handle_value (); |
4930
|
1345 |
4933
|
1346 if (! error_state && fh) |
|
1347 { |
|
1348 std::string fh_nm = fh->fcn_name (); |
6416
|
1349 |
|
1350 if (fh_nm == "@<anonymous>") |
|
1351 { |
|
1352 std::ostringstream buf; |
|
1353 |
|
1354 fh->print_raw (buf); |
|
1355 |
|
1356 retval = buf.str (); |
|
1357 } |
|
1358 else |
|
1359 retval = fh_nm; |
4933
|
1360 } |
4343
|
1361 else |
4933
|
1362 error ("func2str: expecting valid function handle as first argument"); |
4343
|
1363 } |
|
1364 else |
5823
|
1365 print_usage (); |
4343
|
1366 |
|
1367 return retval; |
|
1368 } |
|
1369 |
4933
|
1370 DEFUN (str2func, args, , |
4343
|
1371 "-*- texinfo -*-\n\ |
4933
|
1372 @deftypefn {Built-in Function} {} str2func (@var{fcn_name})\n\ |
|
1373 Return a function handle constructed from the string @var{fcn_name}.\n\ |
|
1374 @end deftypefn") |
4343
|
1375 { |
|
1376 octave_value retval; |
|
1377 |
4933
|
1378 if (args.length () == 1) |
4343
|
1379 { |
4933
|
1380 std::string nm = args(0).string_value (); |
4343
|
1381 |
4933
|
1382 if (! error_state) |
|
1383 retval = make_fcn_handle (nm); |
4343
|
1384 else |
4933
|
1385 error ("str2func: expecting string as first argument"); |
4343
|
1386 } |
|
1387 else |
5823
|
1388 print_usage (); |
4343
|
1389 |
|
1390 return retval; |
|
1391 } |
|
1392 |
|
1393 /* |
|
1394 ;;; Local Variables: *** |
|
1395 ;;; mode: C++ *** |
|
1396 ;;; End: *** |
|
1397 */ |