comparison src/ov-struct.cc @ 4687:e95c86d48732

[project @ 2004-01-06 21:53:34 by jwe]
author jwe
date Tue, 06 Jan 2004 21:53:34 +0000
parents f6d6335c08f6
children fcab389ad291
comparison
equal deleted inserted replaced
4686:c7ae43dfdea4 4687:e95c86d48732
37 #include "oct-lvalue.h" 37 #include "oct-lvalue.h"
38 #include "ov-list.h" 38 #include "ov-list.h"
39 #include "ov-struct.h" 39 #include "ov-struct.h"
40 #include "unwind-prot.h" 40 #include "unwind-prot.h"
41 #include "variables.h" 41 #include "variables.h"
42
43 #include "byte-swap.h"
44 #include "ls-oct-ascii.h"
45 #include "ls-oct-binary.h"
46 #include "ls-hdf5.h"
47 #include "ls-utils.h"
42 48
43 DEFINE_OCTAVE_ALLOCATOR(octave_struct); 49 DEFINE_OCTAVE_ALLOCATOR(octave_struct);
44 50
45 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(octave_struct, "struct", "struct"); 51 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(octave_struct, "struct", "struct");
46 52
525 print_usage ("isfield"); 531 print_usage ("isfield");
526 532
527 return retval; 533 return retval;
528 } 534 }
529 535
536 bool
537 octave_struct::save_ascii (std::ostream& os, bool& infnan_warned,
538 bool strip_nan_and_inf)
539 {
540 Octave_map m = map_value ();
541 os << "# length: " << m.length () << "\n";
542
543 Octave_map::iterator i = m.begin ();
544 while (i != m.end ())
545 {
546 Cell val = map.contents (i);
547 octave_value tmp = (map.numel () == 1) ? val(0) :
548 octave_value (val, true);
549
550 bool b = save_ascii_data (os, tmp, m.key (i), infnan_warned,
551 strip_nan_and_inf, 0, 0);
552
553 if (! b)
554 return os;
555
556 i++;
557 }
558
559 return true;
560 }
561
562 bool
563 octave_struct::load_ascii (std::istream& is)
564 {
565 int len = 0;
566 bool success = true;
567
568 if (extract_keyword (is, "length", len) && len >= 0)
569 {
570 if (len > 0)
571 {
572 Octave_map m (map);
573
574 for (int j = 0; j < len; j++)
575 {
576 octave_value t2;
577 bool dummy;
578
579 // recurse to read cell elements
580 std::string nm
581 = read_ascii_data (is, std::string (), dummy, t2, count);
582
583 if (!is)
584 break;
585
586 m.assign (nm, t2);
587 }
588
589 if (is)
590 map = m;
591 else
592 {
593 error ("load: failed to load structure");
594 success = false;
595 }
596 }
597 else if (len == 0 )
598 map = Octave_map ();
599 else
600 panic_impossible ();
601 }
602 else {
603 error ("load: failed to extract number of elements in structure");
604 success = false;
605 }
606
607 return success;
608 }
609
610 bool
611 octave_struct::save_binary (std::ostream& os, bool& save_as_floats)
612 {
613 Octave_map m = map_value ();
614
615 FOUR_BYTE_INT len = m.length();
616 os.write (X_CAST (char *, &len), 4);
617
618 Octave_map::iterator i = m.begin ();
619 while (i != m.end ())
620 {
621 Cell val = map.contents (i);
622 octave_value tmp = (map.numel () == 1) ? val(0) :
623 octave_value (val, true);
624
625 bool b = save_binary_data (os, tmp, m.key (i), "", 0, save_as_floats);
626
627 if (! b)
628 return os;
629
630 i++;
631 }
632
633 return true;
634 }
635
636 bool
637 octave_struct::load_binary (std::istream& is, bool swap,
638 oct_mach_info::float_format fmt)
639 {
640 bool success = true;
641 FOUR_BYTE_INT len;
642 if (! is.read (X_CAST (char *, &len), 4))
643 return false;
644 if (swap)
645 swap_4_bytes (X_CAST (char *, &len));
646
647 if (len > 0)
648 {
649 Octave_map m (map);
650
651 for (int j = 0; j < len; j++)
652 {
653 octave_value t2;
654 bool dummy;
655 std::string doc;
656
657 // recurse to read cell elements
658 std::string nm = read_binary_data (is, swap, fmt, std::string (),
659 dummy, t2, doc);
660
661 if (!is)
662 break;
663
664 m.assign (nm, t2);
665 }
666
667 if (is)
668 map = m;
669 else
670 {
671 error ("load: failed to load structure");
672 success = false;
673 }
674 }
675 else if (len == 0 )
676 map = Octave_map ();
677 else
678 panic_impossible ();
679
680 return success;
681 }
682
683 #if defined (HAVE_HDF5)
684 bool
685 octave_struct::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats)
686 {
687 hid_t data_hid = -1;
688
689 data_hid = H5Gcreate (loc_id, name, 0);
690 if (data_hid < 0) return false;
691
692 // recursively add each element of the structure to this group
693 Octave_map m = map_value ();
694 Octave_map::iterator i = m.begin ();
695 while (i != m.end ())
696 {
697 Cell val = map.contents (i);
698 octave_value tmp = (map.numel () == 1) ? val(0) :
699 octave_value (val, true);
700
701 bool retval2 = add_hdf5_data (data_hid, tmp, m.key (i), "", false,
702 save_as_floats);
703
704 if (! retval2)
705 break;
706
707 i++;
708 }
709
710 H5Gclose (data_hid);
711 return true;
712 }
713
714 bool
715 octave_struct::load_hdf5 (hid_t loc_id, const char *name,
716 bool have_h5giterate_bug)
717 {
718 bool retval = false;
719
720 hdf5_callback_data dsub;
721
722 herr_t retval2;
723 Octave_map m;
724 int current_item = 0;
725 while ((retval2 = H5Giterate (loc_id, name, &current_item,
726 hdf5_read_next_data, &dsub)) > 0)
727 {
728 m.assign (dsub.name, dsub.tc);
729
730 if (have_h5giterate_bug)
731 current_item++; // H5Giterate returned the last index processed
732 }
733
734 if (retval2 >= 0)
735 {
736 map = m;
737 retval = true;
738 }
739
740 return retval;
741 }
742 #endif
743
530 /* 744 /*
531 ;;; Local Variables: *** 745 ;;; Local Variables: ***
532 ;;; mode: C++ *** 746 ;;; mode: C++ ***
533 ;;; End: *** 747 ;;; End: ***
534 */ 748 */