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