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