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