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