comparison src/ls-mat5.cc @ 11586:12df7854fa7c

strip trailing whitespace from source files
author John W. Eaton <jwe@octave.org>
date Thu, 20 Jan 2011 17:24:59 -0500
parents e306683a7e97
children 4ced6b90fffb
comparison
equal deleted inserted replaced
11585:1473d0cf86d2 11586:12df7854fa7c
123 static void 123 static void
124 read_mat5_binary_data (std::istream& is, double *data, 124 read_mat5_binary_data (std::istream& is, double *data,
125 octave_idx_type count, bool swap, mat5_data_type type, 125 octave_idx_type count, bool swap, mat5_data_type type,
126 oct_mach_info::float_format flt_fmt) 126 oct_mach_info::float_format flt_fmt)
127 { 127 {
128 128
129 switch (type) 129 switch (type)
130 { 130 {
131 case miINT8: 131 case miINT8:
132 read_doubles (is, data, LS_CHAR, count, swap, flt_fmt); 132 read_doubles (is, data, LS_CHAR, count, swap, flt_fmt);
133 break; 133 break;
188 static void 188 static void
189 read_mat5_binary_data (std::istream& is, float *data, 189 read_mat5_binary_data (std::istream& is, float *data,
190 octave_idx_type count, bool swap, mat5_data_type type, 190 octave_idx_type count, bool swap, mat5_data_type type,
191 oct_mach_info::float_format flt_fmt) 191 oct_mach_info::float_format flt_fmt)
192 { 192 {
193 193
194 switch (type) 194 switch (type)
195 { 195 {
196 case miINT8: 196 case miINT8:
197 read_floats (is, data, LS_CHAR, count, swap, flt_fmt); 197 read_floats (is, data, LS_CHAR, count, swap, flt_fmt);
198 break; 198 break;
422 tc = ctmp; \ 422 tc = ctmp; \
423 } \ 423 } \
424 else \ 424 else \
425 tc = re; \ 425 tc = re; \
426 } 426 }
427 427
428 // Read one element tag from stream IS, 428 // Read one element tag from stream IS,
429 // place the type code in TYPE and the byte count in BYTES 429 // place the type code in TYPE and the byte count in BYTES
430 // return nonzero on error 430 // return nonzero on error
431 static int 431 static int
432 read_mat5_tag (std::istream& is, bool swap, int32_t& type, int32_t& bytes) 432 read_mat5_tag (std::istream& is, bool swap, int32_t& type, int32_t& bytes)
433 { 433 {
527 527
528 // We uncompress the first 8 bytes of the header to get the buffer length 528 // We uncompress the first 8 bytes of the header to get the buffer length
529 // This will fail with an error Z_MEM_ERROR 529 // This will fail with an error Z_MEM_ERROR
530 uLongf destLen = 8; 530 uLongf destLen = 8;
531 OCTAVE_LOCAL_BUFFER (unsigned int, tmp, 2); 531 OCTAVE_LOCAL_BUFFER (unsigned int, tmp, 2);
532 if (uncompress (reinterpret_cast<Bytef *> (tmp), &destLen, 532 if (uncompress (reinterpret_cast<Bytef *> (tmp), &destLen,
533 reinterpret_cast<Bytef *> (inbuf), element_length) 533 reinterpret_cast<Bytef *> (inbuf), element_length)
534 != Z_MEM_ERROR) 534 != Z_MEM_ERROR)
535 { 535 {
536 // Why should I have to initialize outbuf as I'll just overwrite!! 536 // Why should I have to initialize outbuf as I'll just overwrite!!
537 if (swap) 537 if (swap)
538 swap_bytes<4> (tmp, 2); 538 swap_bytes<4> (tmp, 2);
539 539
540 destLen = tmp[1] + 8; 540 destLen = tmp[1] + 8;
541 std::string outbuf (destLen, ' '); 541 std::string outbuf (destLen, ' ');
542 542
543 // FIXME -- find a way to avoid casting away const here! 543 // FIXME -- find a way to avoid casting away const here!
544 544
545 int err = uncompress (reinterpret_cast<Bytef *> (const_cast<char *> (outbuf.c_str ())), 545 int err = uncompress (reinterpret_cast<Bytef *> (const_cast<char *> (outbuf.c_str ())),
546 &destLen, reinterpret_cast<Bytef *> (inbuf), 546 &destLen, reinterpret_cast<Bytef *> (inbuf),
588 msg.c_str ()); 588 msg.c_str ());
589 } 589 }
590 else 590 else
591 { 591 {
592 std::istringstream gz_is (outbuf); 592 std::istringstream gz_is (outbuf);
593 retval = read_mat5_binary_element (gz_is, filename, 593 retval = read_mat5_binary_element (gz_is, filename,
594 swap, global, tc); 594 swap, global, tc);
595 } 595 }
596 } 596 }
597 else 597 else
598 error ("load: error probing size of compressed data element"); 598 error ("load: error probing size of compressed data element");
634 global = (flags & 0x0400) != 0; // global variable? 634 global = (flags & 0x0400) != 0; // global variable?
635 635
636 logicalvar = (flags & 0x0200) != 0; // boolean ? 636 logicalvar = (flags & 0x0200) != 0; // boolean ?
637 637
638 arrayclass = static_cast<arrayclasstype> (flags & 0xff); 638 arrayclass = static_cast<arrayclasstype> (flags & 0xff);
639 639
640 int32_t tmp_nzmax; 640 int32_t tmp_nzmax;
641 read_int (is, swap, tmp_nzmax); // max number of non-zero in sparse 641 read_int (is, swap, tmp_nzmax); // max number of non-zero in sparse
642 nzmax = tmp_nzmax; 642 nzmax = tmp_nzmax;
643 643
644 // dimensions array subelement 644 // dimensions array subelement
645 if (arrayclass != MAT_FILE_WORKSPACE_CLASS) 645 if (arrayclass != MAT_FILE_WORKSPACE_CLASS)
646 { 646 {
647 int32_t dim_len; 647 int32_t dim_len;
648 648
687 687
688 if (len) 688 if (len)
689 { 689 {
690 if (! is.read (name, len )) 690 if (! is.read (name, len ))
691 goto data_read_error; 691 goto data_read_error;
692 692
693 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); 693 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len)));
694 } 694 }
695 695
696 name[len] = '\0'; 696 name[len] = '\0';
697 retval = name; 697 retval = name;
751 data = sm.data (); 751 data = sm.data ();
752 } 752 }
753 753
754 // row indices 754 // row indices
755 std::streampos tmp_pos; 755 std::streampos tmp_pos;
756 756
757 if (read_mat5_tag (is, swap, type, len)) 757 if (read_mat5_tag (is, swap, type, len))
758 { 758 {
759 error ("load: reading sparse row data for `%s'", retval.c_str ()); 759 error ("load: reading sparse row data for `%s'", retval.c_str ());
760 goto data_read_error; 760 goto data_read_error;
761 } 761 }
822 822
823 // imaginary data subelement 823 // imaginary data subelement
824 if (imag) 824 if (imag)
825 { 825 {
826 NDArray im (dim_vector (static_cast<int> (nnz), 1)); 826 NDArray im (dim_vector (static_cast<int> (nnz), 1));
827 827
828 if (read_mat5_tag (is, swap, type, len)) 828 if (read_mat5_tag (is, swap, type, len))
829 { 829 {
830 error ("load: reading sparse matrix data for `%s'", retval.c_str ()); 830 error ("load: reading sparse matrix data for `%s'", retval.c_str ());
831 goto data_read_error; 831 goto data_read_error;
832 } 832 }
874 if (fpath.length() == 0) 874 if (fpath.length() == 0)
875 // We have a builtin function 875 // We have a builtin function
876 tc = make_fcn_handle (fname); 876 tc = make_fcn_handle (fname);
877 else 877 else
878 { 878 {
879 std::string mroot = 879 std::string mroot =
880 m0.contents("matlabroot")(0).string_value(); 880 m0.contents("matlabroot")(0).string_value();
881 881
882 if ((fpath.length () >= mroot.length ()) && 882 if ((fpath.length () >= mroot.length ()) &&
883 fpath.substr(0, mroot.length()) == mroot && 883 fpath.substr(0, mroot.length()) == mroot &&
884 OCTAVE_EXEC_PREFIX != mroot) 884 OCTAVE_EXEC_PREFIX != mroot)
887 // doesn't equal octave_config_info ("exec_prefix") 887 // doesn't equal octave_config_info ("exec_prefix")
888 // then the function points to a version of Octave 888 // then the function points to a version of Octave
889 // or Matlab other than the running version. In that 889 // or Matlab other than the running version. In that
890 // case we replace with the same function in the 890 // case we replace with the same function in the
891 // running version of Octave? 891 // running version of Octave?
892 892
893 // First check if just replacing matlabroot is enough 893 // First check if just replacing matlabroot is enough
894 std::string str = OCTAVE_EXEC_PREFIX + 894 std::string str = OCTAVE_EXEC_PREFIX +
895 fpath.substr (mroot.length ()); 895 fpath.substr (mroot.length ());
896 file_stat fs (str); 896 file_stat fs (str);
897 897
898 if (fs.exists ()) 898 if (fs.exists ())
899 { 899 {
900 size_t xpos 900 size_t xpos
902 902
903 std::string dir_name = str.substr (0, xpos); 903 std::string dir_name = str.substr (0, xpos);
904 904
905 octave_function *fcn 905 octave_function *fcn
906 = load_fcn_from_file (str, dir_name, "", fname); 906 = load_fcn_from_file (str, dir_name, "", fname);
907 907
908 if (fcn) 908 if (fcn)
909 { 909 {
910 octave_value tmp (fcn); 910 octave_value tmp (fcn);
911 911
912 tc = octave_value (new octave_fcn_handle (tmp, fname)); 912 tc = octave_value (new octave_fcn_handle (tmp, fname));
939 939
940 tc = octave_value (new octave_fcn_handle (tmp, fname)); 940 tc = octave_value (new octave_fcn_handle (tmp, fname));
941 } 941 }
942 else 942 else
943 { 943 {
944 warning ("load: can't find the file %s", 944 warning ("load: can't find the file %s",
945 fpath.c_str()); 945 fpath.c_str());
946 goto skip_ahead; 946 goto skip_ahead;
947 } 947 }
948 } 948 }
949 } 949 }
963 963
964 tc = octave_value (new octave_fcn_handle (tmp, fname)); 964 tc = octave_value (new octave_fcn_handle (tmp, fname));
965 } 965 }
966 else 966 else
967 { 967 {
968 warning ("load: can't find the file %s", 968 warning ("load: can't find the file %s",
969 fpath.c_str()); 969 fpath.c_str());
970 goto skip_ahead; 970 goto skip_ahead;
971 } 971 }
972 } 972 }
973 } 973 }
1001 frame.add_fcn (octave_call_stack::pop); 1001 frame.add_fcn (octave_call_stack::pop);
1002 1002
1003 if (m2.nfields() > 0) 1003 if (m2.nfields() > 0)
1004 { 1004 {
1005 octave_value tmp; 1005 octave_value tmp;
1006 1006
1007 for (Octave_map::iterator p0 = m2.begin() ; 1007 for (Octave_map::iterator p0 = m2.begin() ;
1008 p0 != m2.end(); p0++) 1008 p0 != m2.end(); p0++)
1009 { 1009 {
1010 std::string key = m2.key(p0); 1010 std::string key = m2.key(p0);
1011 octave_value val = m2.contents(p0)(0); 1011 octave_value val = m2.contents(p0)(0);
1012 1012
1013 symbol_table::varref (key, local_scope, 0) = val; 1013 symbol_table::varref (key, local_scope, 0) = val;
1014 } 1014 }
1015 } 1015 }
1016 1016
1017 int parse_status; 1017 int parse_status;
1018 octave_value anon_fcn_handle = 1018 octave_value anon_fcn_handle =
1019 eval_string (fname.substr (4), true, parse_status); 1019 eval_string (fname.substr (4), true, parse_status);
1020 1020
1021 if (parse_status == 0) 1021 if (parse_status == 0)
1022 { 1022 {
1023 octave_fcn_handle *fh = 1023 octave_fcn_handle *fh =
1024 anon_fcn_handle.fcn_handle_value (); 1024 anon_fcn_handle.fcn_handle_value ();
1025 1025
1026 if (fh) 1026 if (fh)
1027 tc = new octave_fcn_handle (fh->fcn_val (), "@<anonymous>"); 1027 tc = new octave_fcn_handle (fh->fcn_val (), "@<anonymous>");
1028 else 1028 else
1070 if (fn_len) 1070 if (fn_len)
1071 { 1071 {
1072 if (! is.read (elname, fn_len)) 1072 if (! is.read (elname, fn_len))
1073 goto data_read_error; 1073 goto data_read_error;
1074 1074
1075 is.seekg (tmp_pos + 1075 is.seekg (tmp_pos +
1076 static_cast<std::streamoff> (PAD (fn_len))); 1076 static_cast<std::streamoff> (PAD (fn_len)));
1077 } 1077 }
1078 1078
1079 elname[fn_len] = '\0'; 1079 elname[fn_len] = '\0';
1080 1080
1095 { 1095 {
1096 if (field(i) == "MCOS") 1096 if (field(i) == "MCOS")
1097 { 1097 {
1098 octave_value fieldtc; 1098 octave_value fieldtc;
1099 read_mat5_binary_element (is, filename, swap, global, 1099 read_mat5_binary_element (is, filename, swap, global,
1100 fieldtc); 1100 fieldtc);
1101 if (! is || error_state) 1101 if (! is || error_state)
1102 goto data_read_error; 1102 goto data_read_error;
1103 1103
1104 elt[i](j) = fieldtc; 1104 elt[i](j) = fieldtc;
1105 } 1105 }
1131 1131
1132 if (len) 1132 if (len)
1133 { 1133 {
1134 if (! is.read (name, len )) 1134 if (! is.read (name, len ))
1135 goto data_read_error; 1135 goto data_read_error;
1136 1136
1137 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); 1137 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len)));
1138 } 1138 }
1139 1139
1140 name[len] = '\0'; 1140 name[len] = '\0';
1141 classname = name; 1141 classname = name;
1197 { 1197 {
1198 for (octave_idx_type i = 0; i < n_fields; i++) 1198 for (octave_idx_type i = 0; i < n_fields; i++)
1199 { 1199 {
1200 octave_value fieldtc; 1200 octave_value fieldtc;
1201 read_mat5_binary_element (is, filename, swap, global, 1201 read_mat5_binary_element (is, filename, swap, global,
1202 fieldtc); 1202 fieldtc);
1203 elt[i](j) = fieldtc; 1203 elt[i](j) = fieldtc;
1204 } 1204 }
1205 } 1205 }
1206 1206
1207 for (octave_idx_type i = 0; i < n_fields; i++) 1207 for (octave_idx_type i = 0; i < n_fields; i++)
1216 { 1216 {
1217 if (classname == "inline") 1217 if (classname == "inline")
1218 { 1218 {
1219 // inline is not an object in Octave but rather an 1219 // inline is not an object in Octave but rather an
1220 // overload of a function handle. Special case. 1220 // overload of a function handle. Special case.
1221 tc = 1221 tc =
1222 new octave_fcn_inline (m.contents("expr")(0).string_value(), 1222 new octave_fcn_inline (m.contents("expr")(0).string_value(),
1223 m.contents("args")(0).string_value()); 1223 m.contents("args")(0).string_value());
1224 } 1224 }
1225 else 1225 else
1226 { 1226 {
1228 cls->reconstruct_exemplar (); 1228 cls->reconstruct_exemplar ();
1229 1229
1230 if (! cls->reconstruct_parents ()) 1230 if (! cls->reconstruct_parents ())
1231 warning ("load: unable to reconstruct object inheritance"); 1231 warning ("load: unable to reconstruct object inheritance");
1232 1232
1233 tc = cls; 1233 tc = cls;
1234 if (load_path::find_method (classname, "loadobj") != 1234 if (load_path::find_method (classname, "loadobj") !=
1235 std::string()) 1235 std::string())
1236 { 1236 {
1237 octave_value_list tmp = feval ("loadobj", tc, 1); 1237 octave_value_list tmp = feval ("loadobj", tc, 1);
1238 1238
1239 if (! error_state) 1239 if (! error_state)
1263 if (logicalvar) 1263 if (logicalvar)
1264 { 1264 {
1265 uint8NDArray in = tc.uint8_array_value (); 1265 uint8NDArray in = tc.uint8_array_value ();
1266 octave_idx_type nel = in.numel (); 1266 octave_idx_type nel = in.numel ();
1267 boolNDArray out (dims); 1267 boolNDArray out (dims);
1268 1268
1269 for (octave_idx_type i = 0; i < nel; i++) 1269 for (octave_idx_type i = 0; i < nel; i++)
1270 out (i) = in(i).bool_value (); 1270 out (i) = in(i).bool_value ();
1271 1271
1272 tc = out; 1272 tc = out;
1273 } 1273 }
1300 1300
1301 1301
1302 case MAT_FILE_SINGLE_CLASS: 1302 case MAT_FILE_SINGLE_CLASS:
1303 { 1303 {
1304 FloatNDArray re (dims); 1304 FloatNDArray re (dims);
1305 1305
1306 // real data subelement 1306 // real data subelement
1307 1307
1308 std::streampos tmp_pos; 1308 std::streampos tmp_pos;
1309 1309
1310 if (read_mat5_tag (is, swap, type, len)) 1310 if (read_mat5_tag (is, swap, type, len))
1311 { 1311 {
1312 error ("load: reading matrix data for `%s'", retval.c_str ()); 1312 error ("load: reading matrix data for `%s'", retval.c_str ());
1313 goto data_read_error; 1313 goto data_read_error;
1314 } 1314 }
1329 if (imag) 1329 if (imag)
1330 { 1330 {
1331 // imaginary data subelement 1331 // imaginary data subelement
1332 1332
1333 FloatNDArray im (dims); 1333 FloatNDArray im (dims);
1334 1334
1335 if (read_mat5_tag (is, swap, type, len)) 1335 if (read_mat5_tag (is, swap, type, len))
1336 { 1336 {
1337 error ("load: reading matrix data for `%s'", retval.c_str ()); 1337 error ("load: reading matrix data for `%s'", retval.c_str ());
1338 goto data_read_error; 1338 goto data_read_error;
1339 } 1339 }
1366 1366
1367 case MAT_FILE_DOUBLE_CLASS: 1367 case MAT_FILE_DOUBLE_CLASS:
1368 default: 1368 default:
1369 { 1369 {
1370 NDArray re (dims); 1370 NDArray re (dims);
1371 1371
1372 // real data subelement 1372 // real data subelement
1373 1373
1374 std::streampos tmp_pos; 1374 std::streampos tmp_pos;
1375 1375
1376 if (read_mat5_tag (is, swap, type, len)) 1376 if (read_mat5_tag (is, swap, type, len))
1377 { 1377 {
1378 error ("load: reading matrix data for `%s'", retval.c_str ()); 1378 error ("load: reading matrix data for `%s'", retval.c_str ());
1379 goto data_read_error; 1379 goto data_read_error;
1380 } 1380 }
1397 // Logical variables can either be MAT_FILE_UINT8_CLASS or 1397 // Logical variables can either be MAT_FILE_UINT8_CLASS or
1398 // MAT_FILE_DOUBLE_CLASS, so check if we have a logical 1398 // MAT_FILE_DOUBLE_CLASS, so check if we have a logical
1399 // variable and convert it. 1399 // variable and convert it.
1400 1400
1401 boolNDArray out (dims); 1401 boolNDArray out (dims);
1402 1402
1403 for (octave_idx_type i = 0; i < n; i++) 1403 for (octave_idx_type i = 0; i < n; i++)
1404 out (i) = static_cast<bool> (re (i)); 1404 out (i) = static_cast<bool> (re (i));
1405 1405
1406 tc = out; 1406 tc = out;
1407 } 1407 }
1408 else if (imag) 1408 else if (imag)
1409 { 1409 {
1410 // imaginary data subelement 1410 // imaginary data subelement
1411 1411
1412 NDArray im (dims); 1412 NDArray im (dims);
1413 1413
1414 if (read_mat5_tag (is, swap, type, len)) 1414 if (read_mat5_tag (is, swap, type, len))
1415 { 1415 {
1416 error ("load: reading matrix data for `%s'", retval.c_str ()); 1416 error ("load: reading matrix data for `%s'", retval.c_str ());
1417 goto data_read_error; 1417 goto data_read_error;
1418 } 1418 }
1466 { 1466 {
1467 unsigned char a = static_cast<unsigned char> (re(i)); 1467 unsigned char a = static_cast<unsigned char> (re(i));
1468 if (a > 0x7f) 1468 if (a > 0x7f)
1469 utf8_multi_byte = true; 1469 utf8_multi_byte = true;
1470 } 1470 }
1471 1471
1472 if (utf8_multi_byte) 1472 if (utf8_multi_byte)
1473 { 1473 {
1474 warning ("load: can not read multi-byte encoded UTF8 characters."); 1474 warning ("load: can not read multi-byte encoded UTF8 characters.");
1475 warning (" Replacing unreadable characters with '?'."); 1475 warning (" Replacing unreadable characters with '?'.");
1476 for (octave_idx_type i = 0; i < n; i++) 1476 for (octave_idx_type i = 0; i < n; i++)
1507 is.seekg (pos + static_cast<std::streamoff> (element_length)); 1507 is.seekg (pos + static_cast<std::streamoff> (element_length));
1508 return read_mat5_binary_element (is, filename, swap, global, tc); 1508 return read_mat5_binary_element (is, filename, swap, global, tc);
1509 } 1509 }
1510 1510
1511 int 1511 int
1512 read_mat5_binary_file_header (std::istream& is, bool& swap, bool quiet, 1512 read_mat5_binary_file_header (std::istream& is, bool& swap, bool quiet,
1513 const std::string& filename) 1513 const std::string& filename)
1514 { 1514 {
1515 int16_t version=0, magic=0; 1515 int16_t version=0, magic=0;
1516 uint64_t subsys_offset; 1516 uint64_t subsys_offset;
1517 1517
1583 } 1583 }
1584 1584
1585 return 0; 1585 return 0;
1586 } 1586 }
1587 1587
1588 static int 1588 static int
1589 write_mat5_tag (std::ostream& is, int type, octave_idx_type bytes) 1589 write_mat5_tag (std::ostream& is, int type, octave_idx_type bytes)
1590 { 1590 {
1591 int32_t temp; 1591 int32_t temp;
1592 1592
1593 if (bytes > 0 && bytes <= 4) 1593 if (bytes > 0 && bytes <= 4)
1622 } \ 1622 } \
1623 while (0) 1623 while (0)
1624 1624
1625 // write out the numeric values in M to OS, 1625 // write out the numeric values in M to OS,
1626 // preceded by the appropriate tag. 1626 // preceded by the appropriate tag.
1627 static void 1627 static void
1628 write_mat5_array (std::ostream& os, const NDArray& m, bool save_as_floats) 1628 write_mat5_array (std::ostream& os, const NDArray& m, bool save_as_floats)
1629 { 1629 {
1630 save_type st = LS_DOUBLE; 1630 save_type st = LS_DOUBLE;
1631 const double *data = m.data (); 1631 const double *data = m.data ();
1632 1632
1669 switch (st) 1669 switch (st)
1670 { 1670 {
1671 case LS_U_CHAR: 1671 case LS_U_CHAR:
1672 MAT5_DO_WRITE (uint8_t, data, nel, os); 1672 MAT5_DO_WRITE (uint8_t, data, nel, os);
1673 break; 1673 break;
1674 1674
1675 case LS_U_SHORT: 1675 case LS_U_SHORT:
1676 MAT5_DO_WRITE (uint16_t, data, nel, os); 1676 MAT5_DO_WRITE (uint16_t, data, nel, os);
1677 break; 1677 break;
1678 1678
1679 case LS_U_INT: 1679 case LS_U_INT:
1680 MAT5_DO_WRITE (uint32_t, data, nel, os); 1680 MAT5_DO_WRITE (uint32_t, data, nel, os);
1681 break; 1681 break;
1682 1682
1683 case LS_U_LONG: 1683 case LS_U_LONG:
1684 MAT5_DO_WRITE (uint64_t, data, nel, os); 1684 MAT5_DO_WRITE (uint64_t, data, nel, os);
1685 break; 1685 break;
1686 1686
1687 case LS_CHAR: 1687 case LS_CHAR:
1688 MAT5_DO_WRITE (int8_t, data, nel, os); 1688 MAT5_DO_WRITE (int8_t, data, nel, os);
1689 break; 1689 break;
1690 1690
1691 case LS_SHORT: 1691 case LS_SHORT:
1692 MAT5_DO_WRITE (int16_t, data, nel, os); 1692 MAT5_DO_WRITE (int16_t, data, nel, os);
1693 break; 1693 break;
1694 1694
1695 case LS_INT: 1695 case LS_INT:
1719 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; 1719 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00";
1720 os.write (buf, PAD (len) - len); 1720 os.write (buf, PAD (len) - len);
1721 } 1721 }
1722 } 1722 }
1723 1723
1724 static void 1724 static void
1725 write_mat5_array (std::ostream& os, const FloatNDArray& m, bool) 1725 write_mat5_array (std::ostream& os, const FloatNDArray& m, bool)
1726 { 1726 {
1727 save_type st = LS_FLOAT; 1727 save_type st = LS_FLOAT;
1728 const float *data = m.data (); 1728 const float *data = m.data ();
1729 1729
1755 switch (st) 1755 switch (st)
1756 { 1756 {
1757 case LS_U_CHAR: 1757 case LS_U_CHAR:
1758 MAT5_DO_WRITE (uint8_t, data, nel, os); 1758 MAT5_DO_WRITE (uint8_t, data, nel, os);
1759 break; 1759 break;
1760 1760
1761 case LS_U_SHORT: 1761 case LS_U_SHORT:
1762 MAT5_DO_WRITE (uint16_t, data, nel, os); 1762 MAT5_DO_WRITE (uint16_t, data, nel, os);
1763 break; 1763 break;
1764 1764
1765 case LS_U_INT: 1765 case LS_U_INT:
1766 MAT5_DO_WRITE (uint32_t, data, nel, os); 1766 MAT5_DO_WRITE (uint32_t, data, nel, os);
1767 break; 1767 break;
1768 1768
1769 case LS_U_LONG: 1769 case LS_U_LONG:
1770 MAT5_DO_WRITE (uint64_t, data, nel, os); 1770 MAT5_DO_WRITE (uint64_t, data, nel, os);
1771 break; 1771 break;
1772 1772
1773 case LS_CHAR: 1773 case LS_CHAR:
1774 MAT5_DO_WRITE (int8_t, data, nel, os); 1774 MAT5_DO_WRITE (int8_t, data, nel, os);
1775 break; 1775 break;
1776 1776
1777 case LS_SHORT: 1777 case LS_SHORT:
1778 MAT5_DO_WRITE (int16_t, data, nel, os); 1778 MAT5_DO_WRITE (int16_t, data, nel, os);
1779 break; 1779 break;
1780 1780
1781 case LS_INT: 1781 case LS_INT:
1806 os.write (buf, PAD (len) - len); 1806 os.write (buf, PAD (len) - len);
1807 } 1807 }
1808 } 1808 }
1809 1809
1810 template <class T> 1810 template <class T>
1811 void 1811 void
1812 write_mat5_integer_data (std::ostream& os, const T *m, int size, 1812 write_mat5_integer_data (std::ostream& os, const T *m, int size,
1813 octave_idx_type nel) 1813 octave_idx_type nel)
1814 { 1814 {
1815 mat5_data_type mst; 1815 mat5_data_type mst;
1816 unsigned len; 1816 unsigned len;
1891 template void 1891 template void
1892 write_mat5_integer_data (std::ostream& os, const octave_uint64 *m, 1892 write_mat5_integer_data (std::ostream& os, const octave_uint64 *m,
1893 int size, octave_idx_type nel); 1893 int size, octave_idx_type nel);
1894 1894
1895 template void 1895 template void
1896 write_mat5_integer_data (std::ostream& os, const int *m, 1896 write_mat5_integer_data (std::ostream& os, const int *m,
1897 int size, octave_idx_type nel); 1897 int size, octave_idx_type nel);
1898 1898
1899 // Write out cell element values in the cell array to OS, preceded by 1899 // Write out cell element values in the cell array to OS, preceded by
1900 // the appropriate tag. 1900 // the appropriate tag.
1901 1901
1902 static bool 1902 static bool
1903 write_mat5_cell_array (std::ostream& os, const Cell& cell, 1903 write_mat5_cell_array (std::ostream& os, const Cell& cell,
1904 bool mark_as_global, bool save_as_floats) 1904 bool mark_as_global, bool save_as_floats)
1905 { 1905 {
1906 octave_idx_type nel = cell.numel (); 1906 octave_idx_type nel = cell.numel ();
1907 1907
2089 2089
2090 return ret; 2090 return ret;
2091 } 2091 }
2092 2092
2093 int 2093 int
2094 save_mat5_element_length (const octave_value& tc, const std::string& name, 2094 save_mat5_element_length (const octave_value& tc, const std::string& name,
2095 bool save_as_floats, bool mat7_format) 2095 bool save_as_floats, bool mat7_format)
2096 { 2096 {
2097 size_t max_namelen = (mat7_format ? 63 : 31); 2097 size_t max_namelen = (mat7_format ? 63 : 31);
2098 size_t len = name.length (); 2098 size_t len = name.length ();
2099 std::string cname = tc.class_name (); 2099 std::string cname = tc.class_name ();
2101 2101
2102 if (len > 4) 2102 if (len > 4)
2103 ret += PAD (len > max_namelen ? max_namelen : len); 2103 ret += PAD (len > max_namelen ? max_namelen : len);
2104 2104
2105 ret += PAD (4 * tc.ndims ()); 2105 ret += PAD (4 * tc.ndims ());
2106 2106
2107 if (tc.is_string ()) 2107 if (tc.is_string ())
2108 { 2108 {
2109 charNDArray chm = tc.char_array_value (); 2109 charNDArray chm = tc.char_array_value ();
2110 ret += 8; 2110 ret += 8;
2111 if (chm.numel () > 2) 2111 if (chm.numel () > 2)
2184 { 2184 {
2185 Cell cell = tc.cell_value (); 2185 Cell cell = tc.cell_value ();
2186 octave_idx_type nel = cell.numel (); 2186 octave_idx_type nel = cell.numel ();
2187 2187
2188 for (int i = 0; i < nel; i++) 2188 for (int i = 0; i < nel; i++)
2189 ret += 8 + 2189 ret += 8 +
2190 save_mat5_element_length (cell (i), "", save_as_floats, mat7_format); 2190 save_mat5_element_length (cell (i), "", save_as_floats, mat7_format);
2191 } 2191 }
2192 else if (tc.is_complex_scalar () || tc.is_complex_matrix ()) 2192 else if (tc.is_complex_scalar () || tc.is_complex_matrix ())
2193 { 2193 {
2194 if (tc.is_single_type ()) 2194 if (tc.is_single_type ())
2195 { 2195 {
2196 const FloatComplexNDArray m = tc.float_complex_array_value (); 2196 const FloatComplexNDArray m = tc.float_complex_array_value ();
2197 ret += save_mat5_array_length (m.fortran_vec (), m.numel (), 2197 ret += save_mat5_array_length (m.fortran_vec (), m.numel (),
2198 save_as_floats); 2198 save_as_floats);
2199 } 2199 }
2200 else 2200 else
2201 { 2201 {
2202 const ComplexNDArray m = tc.complex_array_value (); 2202 const ComplexNDArray m = tc.complex_array_value ();
2203 ret += save_mat5_array_length (m.fortran_vec (), m.numel (), 2203 ret += save_mat5_array_length (m.fortran_vec (), m.numel (),
2204 save_as_floats); 2204 save_as_floats);
2205 } 2205 }
2206 } 2206 }
2207 else if (tc.is_map () || tc.is_inline_function () || tc.is_object ()) 2207 else if (tc.is_map () || tc.is_inline_function () || tc.is_object ())
2208 { 2208 {
2209 int fieldcnt = 0; 2209 int fieldcnt = 0;
2210 const Octave_map m = tc.map_value (); 2210 const Octave_map m = tc.map_value ();
2211 octave_idx_type nel = m.numel (); 2211 octave_idx_type nel = m.numel ();
2212 2212
2231 2231
2232 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) 2232 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++)
2233 { 2233 {
2234 const Cell elts = m.contents (i); 2234 const Cell elts = m.contents (i);
2235 2235
2236 ret += 8 + save_mat5_element_length (elts(j), "", 2236 ret += 8 + save_mat5_element_length (elts(j), "",
2237 save_as_floats, mat7_format); 2237 save_as_floats, mat7_format);
2238 } 2238 }
2239 } 2239 }
2240 } 2240 }
2241 else 2241 else
2271 2271
2272 bool 2272 bool
2273 save_mat5_binary_element (std::ostream& os, 2273 save_mat5_binary_element (std::ostream& os,
2274 const octave_value& tc, const std::string& name, 2274 const octave_value& tc, const std::string& name,
2275 bool mark_as_global, bool mat7_format, 2275 bool mark_as_global, bool mat7_format,
2276 bool save_as_floats, bool compressing) 2276 bool save_as_floats, bool compressing)
2277 { 2277 {
2278 int32_t flags = 0; 2278 int32_t flags = 0;
2279 int32_t nnz_32 = 0; 2279 int32_t nnz_32 = 0;
2280 std::string cname = tc.class_name (); 2280 std::string cname = tc.class_name ();
2281 size_t max_namelen = (mat7_format ? 63 : 31); 2281 size_t max_namelen = (mat7_format ? 63 : 31);
2339 ret = save_mat5_binary_element (buf, tc, name, mark_as_global, true, 2339 ret = save_mat5_binary_element (buf, tc, name, mark_as_global, true,
2340 save_as_floats, true); 2340 save_as_floats, true);
2341 2341
2342 if (ret) 2342 if (ret)
2343 { 2343 {
2344 // destLen must be at least 0.1% larger than source buffer 2344 // destLen must be at least 0.1% larger than source buffer
2345 // + 12 bytes. Reality is it must be larger again than that. 2345 // + 12 bytes. Reality is it must be larger again than that.
2346 std::string buf_str = buf.str (); 2346 std::string buf_str = buf.str ();
2347 uLongf srcLen = buf_str.length (); 2347 uLongf srcLen = buf_str.length ();
2348 uLongf destLen = srcLen * 101 / 100 + 12; 2348 uLongf destLen = srcLen * 101 / 100 + 12;
2349 OCTAVE_LOCAL_BUFFER (char, out_buf, destLen); 2349 OCTAVE_LOCAL_BUFFER (char, out_buf, destLen);
2350 2350
2351 if (compress (reinterpret_cast<Bytef *> (out_buf), &destLen, 2351 if (compress (reinterpret_cast<Bytef *> (out_buf), &destLen,
2352 reinterpret_cast<const Bytef *> (buf_str.c_str ()), srcLen) == Z_OK) 2352 reinterpret_cast<const Bytef *> (buf_str.c_str ()), srcLen) == Z_OK)
2353 { 2353 {
2354 write_mat5_tag (os, miCOMPRESSED, 2354 write_mat5_tag (os, miCOMPRESSED,
2355 static_cast<octave_idx_type> (destLen)); 2355 static_cast<octave_idx_type> (destLen));
2356 2356
2357 os.write (out_buf, destLen); 2357 os.write (out_buf, destLen);
2358 } 2358 }
2359 else 2359 else
2360 { 2360 {
2365 2365
2366 return ret; 2366 return ret;
2367 } 2367 }
2368 #endif 2368 #endif
2369 2369
2370 write_mat5_tag (os, miMATRIX, save_mat5_element_length 2370 write_mat5_tag (os, miMATRIX, save_mat5_element_length
2371 (tc, name, save_as_floats, mat7_format)); 2371 (tc, name, save_as_floats, mat7_format));
2372 2372
2373 // array flags subelement 2373 // array flags subelement
2374 write_mat5_tag (os, miUINT32, 8); 2374 write_mat5_tag (os, miUINT32, 8);
2375 2375
2376 if (tc.is_bool_type ()) 2376 if (tc.is_bool_type ())
2377 flags |= 0x0200; 2377 flags |= 0x0200;
2408 if (tc.is_single_type ()) 2408 if (tc.is_single_type ())
2409 flags |= MAT_FILE_SINGLE_CLASS; 2409 flags |= MAT_FILE_SINGLE_CLASS;
2410 else 2410 else
2411 flags |= MAT_FILE_DOUBLE_CLASS; 2411 flags |= MAT_FILE_DOUBLE_CLASS;
2412 } 2412 }
2413 else if (tc.is_map ()) 2413 else if (tc.is_map ())
2414 flags |= MAT_FILE_STRUCT_CLASS; 2414 flags |= MAT_FILE_STRUCT_CLASS;
2415 else if (tc.is_cell ()) 2415 else if (tc.is_cell ())
2416 flags |= MAT_FILE_CELL_CLASS; 2416 flags |= MAT_FILE_CELL_CLASS;
2417 else if (tc.is_inline_function () || tc.is_object ()) 2417 else if (tc.is_inline_function () || tc.is_object ())
2418 flags |= MAT_FILE_OBJECT_CLASS; 2418 flags |= MAT_FILE_OBJECT_CLASS;
2470 2470
2471 for (octave_idx_type i = 0; i < nel; i++) 2471 for (octave_idx_type i = 0; i < nel; i++)
2472 buf[i] = *s++ & 0x00FF; 2472 buf[i] = *s++ & 0x00FF;
2473 2473
2474 os.write (reinterpret_cast<char *> (buf), len); 2474 os.write (reinterpret_cast<char *> (buf), len);
2475 2475
2476 if (paddedlength > len) 2476 if (paddedlength > len)
2477 { 2477 {
2478 static char padbuf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; 2478 static char padbuf[9]="\x00\x00\x00\x00\x00\x00\x00\x00";
2479 os.write (padbuf, paddedlength - len); 2479 os.write (padbuf, paddedlength - len);
2480 } 2480 }
2595 Cell cell = tc.cell_value (); 2595 Cell cell = tc.cell_value ();
2596 2596
2597 if (! write_mat5_cell_array (os, cell, mark_as_global, save_as_floats)) 2597 if (! write_mat5_cell_array (os, cell, mark_as_global, save_as_floats))
2598 goto error_cleanup; 2598 goto error_cleanup;
2599 } 2599 }
2600 else if (tc.is_complex_scalar () || tc.is_complex_matrix ()) 2600 else if (tc.is_complex_scalar () || tc.is_complex_matrix ())
2601 { 2601 {
2602 if (tc.is_single_type ()) 2602 if (tc.is_single_type ())
2603 { 2603 {
2604 FloatComplexNDArray m_cmplx = tc.float_complex_array_value (); 2604 FloatComplexNDArray m_cmplx = tc.float_complex_array_value ();
2605 2605
2612 2612
2613 write_mat5_array (os, ::real (m_cmplx), save_as_floats); 2613 write_mat5_array (os, ::real (m_cmplx), save_as_floats);
2614 write_mat5_array (os, ::imag (m_cmplx), save_as_floats); 2614 write_mat5_array (os, ::imag (m_cmplx), save_as_floats);
2615 } 2615 }
2616 } 2616 }
2617 else if (tc.is_map () || tc.is_inline_function() || tc.is_object ()) 2617 else if (tc.is_map () || tc.is_inline_function() || tc.is_object ())
2618 { 2618 {
2619 if (tc.is_inline_function () || tc.is_object ()) 2619 if (tc.is_inline_function () || tc.is_object ())
2620 { 2620 {
2621 std::string classname = tc.is_object() ? tc.class_name () : "inline"; 2621 std::string classname = tc.is_object() ? tc.class_name () : "inline";
2622 size_t namelen = classname.length (); 2622 size_t namelen = classname.length ();
2690 // of the fields. 2690 // of the fields.
2691 for (octave_idx_type i = 0; i < nf; i++) 2691 for (octave_idx_type i = 0; i < nf; i++)
2692 { 2692 {
2693 bool retval2 = save_mat5_binary_element (os, elts[i][j], "", 2693 bool retval2 = save_mat5_binary_element (os, elts[i][j], "",
2694 mark_as_global, 2694 mark_as_global,
2695 false, 2695 false,
2696 save_as_floats); 2696 save_as_floats);
2697 if (! retval2) 2697 if (! retval2)
2698 goto error_cleanup; 2698 goto error_cleanup;
2699 } 2699 }
2700 } 2700 }