comparison src/ov-fcn-handle.cc @ 10261:a4fb4675accb

make printing of handles more Matlab-compatible
author Jaroslav Hajek <highegg@gmail.com>
date Wed, 03 Feb 2010 13:35:41 +0100
parents 2d47356a7a1a
children 57a59eae83cc
comparison
equal deleted inserted replaced
10260:14d5fee02b3b 10261:a4fb4675accb
66 66
67 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_fcn_handle, 67 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_fcn_handle,
68 "function handle", 68 "function handle",
69 "function_handle"); 69 "function_handle");
70 70
71 const std::string octave_fcn_handle::anonymous ("@<anonymous>");
72
71 octave_fcn_handle::octave_fcn_handle (const octave_value& f, 73 octave_fcn_handle::octave_fcn_handle (const octave_value& f,
72 const std::string& n) 74 const std::string& n)
73 : fcn (f), nm (n) 75 : fcn (f), nm (n)
74 { 76 {
75 octave_user_function *uf = fcn.user_function_value (true); 77 octave_user_function *uf = fcn.user_function_value (true);
76 78
77 if (uf) 79 if (uf && nm != anonymous)
78 symbol_table::cache_name (uf->scope (), nm); 80 symbol_table::cache_name (uf->scope (), nm);
79 } 81 }
80 82
81 octave_value_list 83 octave_value_list
82 octave_fcn_handle::subsref (const std::string& type, 84 octave_fcn_handle::subsref (const std::string& type,
282 } 284 }
283 285
284 bool 286 bool
285 octave_fcn_handle::save_ascii (std::ostream& os) 287 octave_fcn_handle::save_ascii (std::ostream& os)
286 { 288 {
287 if (nm == "@<anonymous>") 289 if (nm == anonymous)
288 { 290 {
289 os << nm << "\n"; 291 os << nm << "\n";
290 292
291 print_raw (os, true); 293 print_raw (os, true);
292 os << "\n"; 294 os << "\n";
347 is.clear (); 349 is.clear ();
348 } 350 }
349 351
350 is >> nm; 352 is >> nm;
351 353
352 if (nm == "@<anonymous>") 354 if (nm == anonymous)
353 { 355 {
354 skip_preceeding_newline (is); 356 skip_preceeding_newline (is);
355 357
356 std::string buf; 358 std::string buf;
357 359
445 } 447 }
446 448
447 bool 449 bool
448 octave_fcn_handle::save_binary (std::ostream& os, bool& save_as_floats) 450 octave_fcn_handle::save_binary (std::ostream& os, bool& save_as_floats)
449 { 451 {
450 if (nm == "@<anonymous>") 452 if (nm == anonymous)
451 { 453 {
452 std::ostringstream nmbuf; 454 std::ostringstream nmbuf;
453 455
454 if (fcn.is_undefined ()) 456 if (fcn.is_undefined ())
455 return false; 457 return false;
524 nm = std::string (ctmp1); 526 nm = std::string (ctmp1);
525 527
526 if (! is) 528 if (! is)
527 return false; 529 return false;
528 530
529 if (nm.length() >= 12 && nm.substr (0, 12) == "@<anonymous>") 531 size_t anl = anonymous.length ();
532
533 if (nm.length() >= anl && nm.substr (0, anl) == anonymous)
530 { 534 {
531 octave_idx_type len = 0; 535 octave_idx_type len = 0;
532 536
533 if (nm.length() > 12) 537 if (nm.length() > anl)
534 { 538 {
535 std::istringstream nm_is (nm.substr(12)); 539 std::istringstream nm_is (nm.substr (anl));
536 nm_is >> len; 540 nm_is >> len;
537 nm = nm.substr(0,12); 541 nm = nm.substr (0, anl);
538 } 542 }
539 543
540 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) 544 if (! is.read (reinterpret_cast<char *> (&tmp), 4))
541 return false; 545 return false;
542 if (swap) 546 if (swap)
677 H5Gclose (group_hid); 681 H5Gclose (group_hid);
678 return false; 682 return false;
679 } 683 }
680 H5Dclose (data_hid); 684 H5Dclose (data_hid);
681 685
682 if (nm == "@<anonymous>") 686 if (nm == anonymous)
683 { 687 {
684 std::ostringstream buf; 688 std::ostringstream buf;
685 print_raw (buf, true); 689 print_raw (buf, true);
686 std::string stmp = buf.str (); 690 std::string stmp = buf.str ();
687 691
932 } 936 }
933 H5Tclose (st_id); 937 H5Tclose (st_id);
934 H5Dclose (data_hid); 938 H5Dclose (data_hid);
935 nm = nm_tmp; 939 nm = nm_tmp;
936 940
937 if (nm == "@<anonymous>") 941 if (nm == anonymous)
938 { 942 {
939 #if HAVE_HDF5_18 943 #if HAVE_HDF5_18
940 data_hid = H5Dopen (group_hid, "fcn", H5P_DEFAULT); 944 data_hid = H5Dopen (group_hid, "fcn", H5P_DEFAULT);
941 #else 945 #else
942 data_hid = H5Dopen (group_hid, "fcn"); 946 data_hid = H5Dopen (group_hid, "fcn");
1262 void 1266 void
1263 octave_fcn_handle::print_raw (std::ostream& os, bool pr_as_read_syntax) const 1267 octave_fcn_handle::print_raw (std::ostream& os, bool pr_as_read_syntax) const
1264 { 1268 {
1265 bool printed = false; 1269 bool printed = false;
1266 1270
1267 if (nm == "@<anonymous>") 1271 if (nm == anonymous)
1268 { 1272 {
1269 tree_print_code tpc (os); 1273 tree_print_code tpc (os);
1270 1274
1271 // FCN is const because this member function is, so we can't 1275 // FCN is const because this member function is, so we can't
1272 // use it to call user_function_value, so we make a copy first. 1276 // use it to call user_function_value, so we make a copy first.
1317 printed = true; 1321 printed = true;
1318 } 1322 }
1319 } 1323 }
1320 1324
1321 if (! printed) 1325 if (! printed)
1322 octave_print_internal (os, nm, pr_as_read_syntax, 1326 octave_print_internal (os, "@" + nm, pr_as_read_syntax,
1323 current_print_indent_level ()); 1327 current_print_indent_level ());
1324 } 1328 }
1325 1329
1326 octave_value 1330 octave_value
1327 make_fcn_handle (const std::string& nm, bool local_funcs) 1331 make_fcn_handle (const std::string& nm, bool local_funcs)
1561 { 1565 {
1562 Octave_map m; 1566 Octave_map m;
1563 1567
1564 std::string fh_nm = fh->fcn_name (); 1568 std::string fh_nm = fh->fcn_name ();
1565 1569
1566 if (fh_nm == "@<anonymous>") 1570 if (fh_nm == octave_fcn_handle::anonymous)
1567 { 1571 {
1568 std::ostringstream buf; 1572 std::ostringstream buf;
1569 fh->print_raw (buf); 1573 fh->print_raw (buf);
1570 m.assign ("function", buf.str ()); 1574 m.assign ("function", buf.str ());
1571 1575
1591 m.assign ("type", "simple"); 1595 m.assign ("type", "simple");
1592 } 1596 }
1593 1597
1594 std::string nm = fcn->fcn_file_name (); 1598 std::string nm = fcn->fcn_file_name ();
1595 1599
1596 if (fh_nm == "@<anonymous>") 1600 if (fh_nm == octave_fcn_handle::anonymous)
1597 { 1601 {
1598 m.assign ("file", nm); 1602 m.assign ("file", nm);
1599 1603
1600 octave_user_function *fu = fh->user_function_value (); 1604 octave_user_function *fu = fh->user_function_value ();
1601 1605
1653 1657
1654 if (! error_state && fh) 1658 if (! error_state && fh)
1655 { 1659 {
1656 std::string fh_nm = fh->fcn_name (); 1660 std::string fh_nm = fh->fcn_name ();
1657 1661
1658 if (fh_nm == "@<anonymous>") 1662 if (fh_nm == octave_fcn_handle::anonymous)
1659 { 1663 {
1660 std::ostringstream buf; 1664 std::ostringstream buf;
1661 1665
1662 fh->print_raw (buf); 1666 fh->print_raw (buf);
1663 1667