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