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" |
|
49 |
|
50 #include "byte-swap.h" |
|
51 #include "ls-oct-ascii.h" |
|
52 #include "ls-hdf5.h" |
|
53 #include "ls-utils.h" |
4343
|
54 |
|
55 DEFINE_OCTAVE_ALLOCATOR (octave_fcn_handle); |
|
56 |
4612
|
57 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_fcn_handle, |
|
58 "function handle", |
5946
|
59 "function_handle"); |
4343
|
60 |
5663
|
61 void |
|
62 octave_fcn_handle::reload_warning (const std::string& fcn_type) const |
|
63 { |
|
64 if (warn_reload) |
|
65 { |
|
66 warn_reload = false; |
|
67 |
|
68 warning ("reloading %s functions referenced by function handles is not implemented", |
|
69 fcn_type.c_str ()); |
|
70 } |
|
71 } |
|
72 |
4924
|
73 octave_value_list |
|
74 octave_fcn_handle::subsref (const std::string& type, |
|
75 const std::list<octave_value_list>& idx, |
|
76 int nargout) |
|
77 { |
|
78 octave_value_list retval; |
|
79 |
|
80 switch (type[0]) |
|
81 { |
|
82 case '(': |
|
83 { |
|
84 octave_function *f = function_value (); |
5312
|
85 |
5663
|
86 if (f && f->time_checked () < Vlast_prompt_time) |
|
87 { |
|
88 std::string ff_nm = f->fcn_file_name (); |
|
89 |
|
90 time_t tp = f->time_parsed (); |
|
91 |
|
92 if (ff_nm.empty ()) |
|
93 { |
5775
|
94 // FIXME -- need to handle inline and |
5663
|
95 // command-line functions here. |
|
96 } |
|
97 else |
|
98 { |
|
99 if (fcn_out_of_date (f, ff_nm, tp)) |
|
100 { |
5775
|
101 // FIXME -- there is currently no way to |
5663
|
102 // parse a .m file or reload a .oct file that |
|
103 // leaves the fbi symbol table untouched. We need |
|
104 // a function that will parse the file and return |
|
105 // a pointer to the new function definition |
|
106 // without altering the symbol table. |
|
107 |
|
108 if (f->is_nested_function ()) |
|
109 reload_warning ("nested"); |
|
110 else |
|
111 reload_warning ("functions"); |
|
112 } |
|
113 } |
|
114 } |
5312
|
115 |
|
116 if (f) |
|
117 retval = f->subsref (type, idx, nargout); |
|
118 else |
|
119 error ("invalid function handle"); |
4924
|
120 } |
|
121 break; |
|
122 |
|
123 |
|
124 case '{': |
|
125 case '.': |
|
126 { |
4930
|
127 std::string typ_nm = type_name (); |
|
128 error ("%s cannot be indexed with %c", typ_nm.c_str (), type[0]); |
4924
|
129 } |
|
130 break; |
|
131 |
|
132 default: |
|
133 panic_impossible (); |
|
134 } |
|
135 |
5775
|
136 // FIXME -- perhaps there should be an |
4924
|
137 // octave_value_list::next_subsref member function? See also |
|
138 // octave_builtin::subsref. |
|
139 |
|
140 if (idx.size () > 1) |
4994
|
141 retval = retval(0).next_subsref (nargout, type, idx); |
4924
|
142 |
|
143 return retval; |
|
144 } |
|
145 |
4988
|
146 bool |
5958
|
147 octave_fcn_handle::save_ascii (std::ostream& os, bool&) |
4988
|
148 { |
|
149 os << nm << "\n"; |
4989
|
150 |
4988
|
151 if (nm == "@<anonymous>") |
|
152 { |
4989
|
153 print_raw (os, true); |
|
154 os << "\n"; |
4988
|
155 } |
|
156 |
|
157 return true; |
|
158 } |
|
159 |
|
160 bool |
|
161 octave_fcn_handle::load_ascii (std::istream& is) |
|
162 { |
|
163 is >> nm; |
4989
|
164 |
4988
|
165 if (nm == "@<anonymous>") |
|
166 { |
|
167 char c; |
5765
|
168 std::ostringstream buf; |
4988
|
169 |
4989
|
170 // Skip preceeding newline(s). |
|
171 while (is.get (c) && c == '\n') |
|
172 /* do nothing */; |
4988
|
173 |
|
174 if (is) |
|
175 { |
|
176 buf << c; |
|
177 |
|
178 // Get a line of text whitespace characters included, leaving |
4989
|
179 // newline in the stream. |
|
180 |
4988
|
181 while (is.peek () != '\n') |
|
182 { |
|
183 is.get (c); |
|
184 if (! is) |
|
185 break; |
|
186 buf << c; |
|
187 } |
|
188 } |
|
189 |
|
190 int parse_status; |
5765
|
191 octave_value anon_fcn_handle = eval_string (buf.str (), true, |
|
192 parse_status); |
4988
|
193 |
4989
|
194 if (parse_status == 0) |
|
195 { |
|
196 octave_fcn_handle *fh = anon_fcn_handle.fcn_handle_value (); |
|
197 if (fh) |
|
198 fcn = fh->fcn; |
|
199 else |
|
200 return false; |
|
201 } |
|
202 else |
|
203 return false; |
4988
|
204 } |
|
205 else |
|
206 { |
|
207 fcn = lookup_function (nm); |
|
208 if (! fcn.is_function ()) |
6225
|
209 { |
|
210 error ("function handle points to non-existent function"); |
|
211 return false; |
|
212 } |
4988
|
213 } |
|
214 |
|
215 return true; |
|
216 } |
|
217 |
|
218 bool |
|
219 octave_fcn_handle::save_binary (std::ostream& os, bool&) |
|
220 { |
5828
|
221 int32_t tmp = nm.length (); |
5760
|
222 os.write (reinterpret_cast<char *> (&tmp), 4); |
4988
|
223 os.write (nm.c_str (), nm.length ()); |
|
224 if (nm == "@<anonymous>") |
|
225 { |
5765
|
226 std::ostringstream buf; |
4988
|
227 print_raw (buf, true); |
5765
|
228 std::string stmp = buf.str (); |
4988
|
229 tmp = stmp.length (); |
5760
|
230 os.write (reinterpret_cast<char *> (&tmp), 4); |
4988
|
231 os.write (stmp.c_str (), stmp.length ()); |
|
232 } |
|
233 return true; |
|
234 } |
|
235 |
|
236 bool |
|
237 octave_fcn_handle::load_binary (std::istream& is, bool swap, |
|
238 oct_mach_info::float_format) |
|
239 { |
5828
|
240 int32_t tmp; |
5760
|
241 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
4988
|
242 return false; |
|
243 if (swap) |
|
244 swap_bytes<4> (&tmp); |
|
245 |
|
246 OCTAVE_LOCAL_BUFFER (char, ctmp1, tmp+1); |
|
247 is.read (ctmp1, tmp); |
|
248 nm = std::string (ctmp1); |
|
249 |
|
250 if (! is) |
|
251 return false; |
|
252 |
|
253 if (nm == "@<anonymous>") |
|
254 { |
5760
|
255 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
4988
|
256 return false; |
|
257 if (swap) |
|
258 swap_bytes<4> (&tmp); |
|
259 |
|
260 OCTAVE_LOCAL_BUFFER (char, ctmp2, tmp+1); |
|
261 is.read (ctmp2, tmp); |
|
262 |
|
263 int parse_status; |
|
264 octave_value anon_fcn_handle = eval_string (ctmp2, true, parse_status); |
|
265 |
4989
|
266 if (parse_status == 0) |
|
267 { |
|
268 octave_fcn_handle *fh = anon_fcn_handle.fcn_handle_value (); |
|
269 if (fh) |
|
270 fcn = fh->fcn; |
|
271 else |
|
272 return false; |
|
273 } |
|
274 else |
|
275 return false; |
4988
|
276 } |
|
277 else |
|
278 { |
|
279 fcn = lookup_function (nm); |
|
280 if (! fcn.is_function ()) |
6225
|
281 { |
|
282 error ("function handle points to non-existent function"); |
|
283 return false; |
|
284 } |
4988
|
285 } |
|
286 return true; |
|
287 } |
|
288 |
|
289 #if defined (HAVE_HDF5) |
|
290 bool |
|
291 octave_fcn_handle::save_hdf5 (hid_t loc_id, const char *name, |
|
292 bool /* save_as_floats */) |
|
293 { |
|
294 hid_t group_hid = -1; |
|
295 group_hid = H5Gcreate (loc_id, name, 0); |
|
296 if (group_hid < 0 ) return false; |
|
297 |
|
298 hid_t space_hid = -1, data_hid = -1, type_hid = -1;; |
|
299 bool retval = true; |
|
300 |
|
301 // attach the type of the variable |
|
302 type_hid = H5Tcopy (H5T_C_S1); |
|
303 H5Tset_size (type_hid, nm.length () + 1); |
|
304 if (type_hid < 0) |
|
305 { |
|
306 H5Gclose (group_hid); |
|
307 return false; |
|
308 } |
|
309 |
|
310 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, 2); |
|
311 hdims[0] = 0; |
|
312 hdims[1] = 0; |
5760
|
313 space_hid = H5Screate_simple (0 , hdims, 0); |
4988
|
314 if (space_hid < 0) |
|
315 { |
|
316 H5Tclose (type_hid); |
|
317 H5Gclose (group_hid); |
|
318 return false; |
|
319 } |
|
320 |
|
321 data_hid = H5Dcreate (group_hid, "nm", type_hid, space_hid, H5P_DEFAULT); |
|
322 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, |
5760
|
323 H5P_DEFAULT, nm.c_str ()) < 0) |
4988
|
324 { |
|
325 H5Sclose (space_hid); |
|
326 H5Tclose (type_hid); |
|
327 H5Gclose (group_hid); |
|
328 return false; |
|
329 } |
|
330 H5Dclose (data_hid); |
|
331 |
|
332 if (nm == "@<anonymous>") |
|
333 { |
5765
|
334 std::ostringstream buf; |
4988
|
335 print_raw (buf, true); |
5765
|
336 std::string stmp = buf.str (); |
4988
|
337 |
|
338 // attach the type of the variable |
|
339 H5Tset_size (type_hid, stmp.length () + 1); |
|
340 if (type_hid < 0) |
|
341 { |
|
342 H5Gclose (group_hid); |
|
343 return false; |
|
344 } |
|
345 |
|
346 data_hid = H5Dcreate (group_hid, "fcn", type_hid, space_hid, |
|
347 H5P_DEFAULT); |
|
348 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, |
5760
|
349 H5P_DEFAULT, stmp.c_str ()) < 0) |
4988
|
350 { |
|
351 H5Sclose (space_hid); |
|
352 H5Tclose (type_hid); |
|
353 H5Gclose (group_hid); |
|
354 return false; |
|
355 } |
|
356 |
|
357 H5Dclose (data_hid); |
|
358 } |
|
359 |
|
360 H5Sclose (space_hid); |
|
361 H5Tclose (type_hid); |
|
362 H5Gclose (group_hid); |
|
363 |
|
364 return retval; |
|
365 } |
|
366 |
|
367 bool |
|
368 octave_fcn_handle::load_hdf5 (hid_t loc_id, const char *name, |
|
369 bool /* have_h5giterate_bug */) |
|
370 { |
|
371 hid_t group_hid, data_hid, space_hid, type_hid, type_class_hid, st_id; |
|
372 hsize_t rank; |
|
373 int slen; |
|
374 |
|
375 group_hid = H5Gopen (loc_id, name); |
|
376 if (group_hid < 0 ) return false; |
|
377 |
|
378 data_hid = H5Dopen (group_hid, "nm"); |
|
379 |
|
380 if (data_hid < 0) |
|
381 { |
|
382 H5Gclose (group_hid); |
|
383 return false; |
|
384 } |
|
385 |
|
386 type_hid = H5Dget_type (data_hid); |
|
387 type_class_hid = H5Tget_class (type_hid); |
|
388 |
|
389 if (type_class_hid != H5T_STRING) |
|
390 { |
|
391 H5Tclose (type_hid); |
|
392 H5Dclose (data_hid); |
|
393 H5Gclose (group_hid); |
|
394 return false; |
|
395 } |
|
396 |
|
397 space_hid = H5Dget_space (data_hid); |
|
398 rank = H5Sget_simple_extent_ndims (space_hid); |
|
399 |
|
400 if (rank != 0) |
|
401 { |
|
402 H5Sclose (space_hid); |
|
403 H5Tclose (type_hid); |
|
404 H5Dclose (data_hid); |
|
405 H5Gclose (group_hid); |
|
406 return false; |
|
407 } |
|
408 |
|
409 slen = H5Tget_size (type_hid); |
|
410 if (slen < 0) |
|
411 { |
|
412 H5Sclose (space_hid); |
|
413 H5Tclose (type_hid); |
|
414 H5Dclose (data_hid); |
|
415 H5Gclose (group_hid); |
|
416 return false; |
|
417 } |
|
418 |
|
419 OCTAVE_LOCAL_BUFFER (char, nm_tmp, slen); |
|
420 |
|
421 // create datatype for (null-terminated) string to read into: |
|
422 st_id = H5Tcopy (H5T_C_S1); |
|
423 H5Tset_size (st_id, slen); |
|
424 |
5760
|
425 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, nm_tmp) < 0) |
4988
|
426 { |
|
427 H5Sclose (space_hid); |
|
428 H5Tclose (type_hid); |
|
429 H5Gclose (group_hid); |
|
430 return false; |
|
431 } |
|
432 H5Tclose (st_id); |
|
433 H5Dclose (data_hid); |
|
434 nm = nm_tmp; |
|
435 |
|
436 if (nm == "@<anonymous>") |
|
437 { |
|
438 data_hid = H5Dopen (group_hid, "fcn"); |
|
439 |
|
440 if (data_hid < 0) |
|
441 { |
|
442 H5Gclose (group_hid); |
|
443 return false; |
|
444 } |
|
445 |
|
446 type_hid = H5Dget_type (data_hid); |
|
447 type_class_hid = H5Tget_class (type_hid); |
|
448 |
|
449 if (type_class_hid != H5T_STRING) |
|
450 { |
|
451 H5Tclose (type_hid); |
|
452 H5Dclose (data_hid); |
|
453 H5Gclose (group_hid); |
|
454 return false; |
|
455 } |
|
456 |
|
457 space_hid = H5Dget_space (data_hid); |
|
458 rank = H5Sget_simple_extent_ndims (space_hid); |
|
459 |
|
460 if (rank != 0) |
|
461 { |
|
462 H5Sclose (space_hid); |
|
463 H5Tclose (type_hid); |
|
464 H5Dclose (data_hid); |
|
465 H5Gclose (group_hid); |
|
466 return false; |
|
467 } |
|
468 |
|
469 slen = H5Tget_size (type_hid); |
|
470 if (slen < 0) |
|
471 { |
|
472 H5Sclose (space_hid); |
|
473 H5Tclose (type_hid); |
|
474 H5Dclose (data_hid); |
|
475 H5Gclose (group_hid); |
|
476 return false; |
|
477 } |
|
478 |
|
479 OCTAVE_LOCAL_BUFFER (char, fcn_tmp, slen); |
|
480 |
|
481 // create datatype for (null-terminated) string to read into: |
|
482 st_id = H5Tcopy (H5T_C_S1); |
|
483 H5Tset_size (st_id, slen); |
|
484 |
5760
|
485 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, fcn_tmp) < 0) |
4988
|
486 { |
|
487 H5Sclose (space_hid); |
|
488 H5Tclose (type_hid); |
|
489 H5Gclose (group_hid); |
|
490 return false; |
|
491 } |
|
492 H5Dclose (data_hid); |
|
493 H5Tclose (st_id); |
|
494 |
|
495 int parse_status; |
|
496 octave_value anon_fcn_handle = eval_string (fcn_tmp, true, parse_status); |
|
497 |
4989
|
498 if (parse_status == 0) |
|
499 { |
|
500 octave_fcn_handle *fh = anon_fcn_handle.fcn_handle_value (); |
|
501 if (fh) |
|
502 fcn = fh->fcn; |
|
503 else |
|
504 return false; |
|
505 } |
|
506 else |
|
507 return false; |
4988
|
508 } |
|
509 else |
|
510 { |
|
511 fcn = lookup_function (nm); |
|
512 if (! fcn.is_function ()) |
6225
|
513 { |
|
514 error ("function handle points to non-existent function"); |
|
515 return false; |
|
516 } |
4988
|
517 } |
|
518 |
|
519 return true; |
|
520 } |
|
521 #endif |
|
522 |
4343
|
523 void |
|
524 octave_fcn_handle::print (std::ostream& os, bool pr_as_read_syntax) const |
|
525 { |
|
526 print_raw (os, pr_as_read_syntax); |
|
527 newline (os); |
|
528 } |
|
529 |
|
530 void |
|
531 octave_fcn_handle::print_raw (std::ostream& os, bool pr_as_read_syntax) const |
|
532 { |
4980
|
533 bool printed = false; |
|
534 |
|
535 if (nm == "@<anonymous>") |
|
536 { |
|
537 tree_print_code tpc (os); |
|
538 |
4989
|
539 // FCN is const because this member function is, so we can't |
4980
|
540 // use it to call user_function_value, so we make a copy first. |
|
541 |
|
542 octave_value ftmp = fcn; |
|
543 |
|
544 octave_user_function *f = ftmp.user_function_value (); |
|
545 |
|
546 if (f) |
|
547 { |
|
548 tree_parameter_list *p = f->parameter_list (); |
|
549 |
|
550 os << "@("; |
|
551 |
|
552 if (p) |
|
553 p->accept (tpc); |
|
554 |
|
555 os << ") "; |
|
556 |
|
557 tree_statement_list *b = f->body (); |
|
558 |
|
559 if (b) |
|
560 { |
|
561 assert (b->length () == 1); |
|
562 |
|
563 tree_statement *s = b->front (); |
|
564 |
|
565 if (s) |
|
566 { |
|
567 if (s->is_expression ()) |
|
568 { |
|
569 tree_expression *e = s->expression (); |
|
570 |
|
571 if (e) |
|
572 { |
|
573 if (e->is_assignment_expression ()) |
|
574 { |
|
575 // The parser builds an assignment to |
|
576 // __retval__, and we don't want to |
|
577 // display that part. |
|
578 |
|
579 tree_simple_assignment *tsa |
|
580 = reinterpret_cast <tree_simple_assignment *> (e); |
|
581 tree_expression *rhs = tsa->right_hand_side (); |
|
582 |
|
583 if (rhs) |
|
584 rhs->accept (tpc); |
|
585 } |
|
586 else |
|
587 e->accept (tpc); |
|
588 } |
|
589 } |
|
590 else |
|
591 { |
|
592 tree_command *c = s->command (); |
|
593 |
|
594 tpc.suspend_newline (); |
|
595 c->accept (tpc); |
|
596 tpc.resume_newline (); |
|
597 } |
|
598 } |
|
599 } |
|
600 |
|
601 printed = true; |
|
602 } |
|
603 } |
|
604 |
|
605 if (! printed) |
|
606 octave_print_internal (os, nm, pr_as_read_syntax, |
|
607 current_print_indent_level ()); |
4343
|
608 } |
|
609 |
|
610 octave_value |
|
611 make_fcn_handle (const std::string& nm) |
|
612 { |
|
613 octave_value retval; |
|
614 |
6323
|
615 octave_function *fcn = octave_call_stack::current (); |
|
616 |
6351
|
617 octave_value f = lookup_function (nm, fcn ? fcn->name () : std::string ()); |
4343
|
618 |
4930
|
619 if (f.is_function ()) |
|
620 retval = octave_value (new octave_fcn_handle (f, nm)); |
4343
|
621 else |
|
622 error ("error creating function handle \"@%s\"", nm.c_str ()); |
|
623 |
|
624 return retval; |
|
625 } |
|
626 |
4933
|
627 DEFUN (functions, args, , |
4343
|
628 "-*- texinfo -*-\n\ |
4933
|
629 @deftypefn {Built-in Function} {} functions (@var{fcn_handle})\n\ |
|
630 Return a struct containing information about the function handle\n\ |
|
631 @var{fcn_handle}.\n\ |
|
632 @end deftypefn") |
4343
|
633 { |
|
634 octave_value retval; |
|
635 |
4933
|
636 if (args.length () == 1) |
4343
|
637 { |
4933
|
638 octave_fcn_handle *fh = args(0).fcn_handle_value (); |
4343
|
639 |
|
640 if (! error_state) |
|
641 { |
4933
|
642 octave_function *fcn = fh ? fh->function_value (true) : 0; |
4343
|
643 |
4933
|
644 if (fcn) |
4930
|
645 { |
4933
|
646 Octave_map m; |
4649
|
647 |
4933
|
648 std::string fh_nm = fh->fcn_name (); |
|
649 |
4978
|
650 m.assign ("function", fh_nm); |
4343
|
651 |
4933
|
652 if (fcn->is_nested_function ()) |
|
653 m.assign ("type", "subfunction"); |
|
654 else |
|
655 m.assign ("type", "simple"); |
|
656 |
|
657 std::string nm = fcn->fcn_file_name (); |
4343
|
658 |
4933
|
659 if (nm.empty ()) |
4935
|
660 { |
|
661 if (fh_nm == "@<anonymous>") |
|
662 m.assign ("file", "none"); |
5313
|
663 else if (fcn->is_user_function ()) |
|
664 m.assign ("file", "command-line function"); |
4935
|
665 else |
|
666 m.assign ("file", "built-in function"); |
|
667 } |
4343
|
668 else |
4933
|
669 m.assign ("file", nm); |
|
670 |
|
671 retval = m; |
4343
|
672 } |
|
673 else |
4933
|
674 error ("functions: invalid function handle object"); |
4343
|
675 } |
|
676 else |
4933
|
677 error ("functions: argument must be a function handle object"); |
4343
|
678 } |
|
679 else |
5823
|
680 print_usage (); |
4343
|
681 |
|
682 return retval; |
|
683 } |
|
684 |
4933
|
685 DEFUN (func2str, args, , |
4343
|
686 "-*- texinfo -*-\n\ |
4933
|
687 @deftypefn {Built-in Function} {} func2str (@var{fcn_handle})\n\ |
|
688 Return a string containing the name of the function referenced by\n\ |
|
689 the function handle @var{fcn_handle}.\n\ |
|
690 @end deftypefn") |
4343
|
691 { |
|
692 octave_value retval; |
|
693 |
4933
|
694 if (args.length () == 1) |
4930
|
695 { |
4933
|
696 octave_fcn_handle *fh = args(0).fcn_handle_value (); |
4930
|
697 |
4933
|
698 if (! error_state && fh) |
|
699 { |
|
700 std::string fh_nm = fh->fcn_name (); |
6416
|
701 |
|
702 if (fh_nm == "@<anonymous>") |
|
703 { |
|
704 std::ostringstream buf; |
|
705 |
|
706 fh->print_raw (buf); |
|
707 |
|
708 retval = buf.str (); |
|
709 } |
|
710 else |
|
711 retval = fh_nm; |
4933
|
712 } |
4343
|
713 else |
4933
|
714 error ("func2str: expecting valid function handle as first argument"); |
4343
|
715 } |
|
716 else |
5823
|
717 print_usage (); |
4343
|
718 |
|
719 return retval; |
|
720 } |
|
721 |
4933
|
722 DEFUN (str2func, args, , |
4343
|
723 "-*- texinfo -*-\n\ |
4933
|
724 @deftypefn {Built-in Function} {} str2func (@var{fcn_name})\n\ |
|
725 Return a function handle constructed from the string @var{fcn_name}.\n\ |
|
726 @end deftypefn") |
4343
|
727 { |
|
728 octave_value retval; |
|
729 |
4933
|
730 if (args.length () == 1) |
4343
|
731 { |
4933
|
732 std::string nm = args(0).string_value (); |
4343
|
733 |
4933
|
734 if (! error_state) |
|
735 retval = make_fcn_handle (nm); |
4343
|
736 else |
4933
|
737 error ("str2func: expecting string as first argument"); |
4343
|
738 } |
|
739 else |
5823
|
740 print_usage (); |
4343
|
741 |
|
742 return retval; |
|
743 } |
|
744 |
|
745 /* |
|
746 ;;; Local Variables: *** |
|
747 ;;; mode: C++ *** |
|
748 ;;; End: *** |
|
749 */ |