4634
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
4634
|
21 |
|
22 */ |
|
23 |
|
24 // Author: James R. Van Zandt <jrv@vanzandt.mv.com> |
|
25 |
|
26 #ifdef HAVE_CONFIG_H |
|
27 #include <config.h> |
|
28 #endif |
|
29 |
|
30 #include <cfloat> |
|
31 #include <cstring> |
|
32 #include <cctype> |
|
33 |
|
34 #include <fstream> |
|
35 #include <iomanip> |
|
36 #include <iostream> |
|
37 #include <string> |
4726
|
38 #include <vector> |
4634
|
39 |
|
40 #include "byte-swap.h" |
|
41 #include "data-conv.h" |
|
42 #include "file-ops.h" |
|
43 #include "glob-match.h" |
|
44 #include "lo-mappers.h" |
|
45 #include "lo-sstream.h" |
|
46 #include "mach-info.h" |
|
47 #include "oct-env.h" |
|
48 #include "oct-time.h" |
|
49 #include "quit.h" |
|
50 #include "str-vec.h" |
|
51 |
|
52 #include "Cell.h" |
|
53 #include "defun.h" |
|
54 #include "error.h" |
|
55 #include "gripes.h" |
|
56 #include "load-save.h" |
|
57 #include "oct-obj.h" |
|
58 #include "oct-map.h" |
|
59 #include "ov-cell.h" |
|
60 #include "pager.h" |
|
61 #include "pt-exp.h" |
|
62 #include "symtab.h" |
|
63 #include "sysdep.h" |
|
64 #include "unwind-prot.h" |
|
65 #include "utils.h" |
|
66 #include "variables.h" |
|
67 #include "version.h" |
|
68 #include "dMatrix.h" |
|
69 |
|
70 #include "ls-utils.h" |
|
71 #include "ls-mat5.h" |
|
72 |
5269
|
73 #ifdef HAVE_ZLIB |
|
74 #include <zlib.h> |
|
75 #endif |
|
76 |
4634
|
77 #define PAD(l) (((l)<=4)?4:(((l)+7)/8)*8) |
|
78 #define TAGLENGTH(l) ((l)<=4?4:8) |
|
79 |
|
80 enum arrayclasstype |
|
81 { |
|
82 mxCELL_CLASS=1, // cell array |
|
83 mxSTRUCT_CLASS, // structure |
|
84 mxOBJECT_CLASS, // object |
|
85 mxCHAR_CLASS, // character array |
|
86 mxSPARSE_CLASS, // sparse array |
|
87 mxDOUBLE_CLASS, // double precision array |
|
88 mxSINGLE_CLASS, // single precision floating point |
|
89 mxINT8_CLASS, // 8 bit signed integer |
|
90 mxUINT8_CLASS, // 8 bit unsigned integer |
|
91 mxINT16_CLASS, // 16 bit signed integer |
|
92 mxUINT16_CLASS, // 16 bit unsigned integer |
|
93 mxINT32_CLASS, // 32 bit signed integer |
5089
|
94 mxUINT32_CLASS, // 32 bit unsigned integer |
|
95 mxINT64_CLASS, // 64 bit signed integer |
|
96 mxUINT64_CLASS, // 64 bit unsigned integer |
|
97 mxFUNCTION_CLASS // Function handle |
4634
|
98 }; |
|
99 |
|
100 // Read COUNT elements of data from IS in the format specified by TYPE, |
|
101 // placing the result in DATA. If SWAP is TRUE, swap the bytes of |
|
102 // each element before copying to DATA. FLT_FMT specifies the format |
|
103 // of the data if we are reading floating point numbers. |
|
104 |
|
105 static void |
|
106 read_mat5_binary_data (std::istream& is, double *data, |
|
107 int count, bool swap, mat5_data_type type, |
|
108 oct_mach_info::float_format flt_fmt) |
|
109 { |
|
110 |
|
111 switch (type) |
|
112 { |
|
113 case miINT8: |
|
114 read_doubles (is, data, LS_CHAR, count, swap, flt_fmt); |
|
115 break; |
|
116 |
5351
|
117 case miUTF8: |
4634
|
118 case miUINT8: |
|
119 read_doubles (is, data, LS_U_CHAR, count, swap, flt_fmt); |
|
120 break; |
|
121 |
|
122 case miINT16: |
|
123 read_doubles (is, data, LS_SHORT, count, swap, flt_fmt); |
|
124 break; |
|
125 |
|
126 case miUINT16: |
|
127 read_doubles (is, data, LS_U_SHORT, count, swap, flt_fmt); |
|
128 break; |
|
129 |
|
130 case miINT32: |
|
131 read_doubles (is, data, LS_INT, count, swap, flt_fmt); |
|
132 break; |
|
133 |
|
134 case miUINT32: |
|
135 read_doubles (is, data, LS_U_INT, count, swap, flt_fmt); |
|
136 break; |
|
137 |
|
138 case miSINGLE: |
|
139 read_doubles (is, data, LS_FLOAT, count, swap, flt_fmt); |
|
140 break; |
|
141 |
|
142 case miRESERVE1: |
|
143 break; |
|
144 |
|
145 case miDOUBLE: |
|
146 read_doubles (is, data, LS_DOUBLE, count, swap, flt_fmt); |
|
147 break; |
|
148 |
|
149 case miRESERVE2: |
|
150 case miRESERVE3: |
|
151 break; |
|
152 |
|
153 case miINT64: |
|
154 #ifdef EIGHT_BYTE_INT |
|
155 read_doubles (is, data, LS_LONG, count, swap, flt_fmt); |
|
156 #endif |
|
157 break; |
|
158 |
|
159 case miUINT64: |
|
160 #ifdef EIGHT_BYTE_INT |
|
161 read_doubles (is, data, LS_U_LONG, count, swap, flt_fmt); |
|
162 #endif |
|
163 break; |
|
164 |
|
165 case miMATRIX: |
|
166 default: |
|
167 break; |
|
168 } |
|
169 } |
|
170 |
5089
|
171 template <class T> |
|
172 void |
5164
|
173 read_mat5_integer_data (std::istream& is, T *m, int count, bool swap, |
5089
|
174 mat5_data_type type) |
|
175 { |
|
176 |
|
177 #define READ_INTEGER_DATA(TYPE, swap, data, size, len, stream) \ |
|
178 do \ |
|
179 { \ |
|
180 if (len > 0) \ |
|
181 { \ |
|
182 volatile TYPE *ptr = X_CAST (volatile TYPE *, data); \ |
|
183 stream.read (X_CAST (char *, ptr), size * len); \ |
|
184 if (swap) \ |
|
185 swap_bytes< size > (ptr, len); \ |
|
186 TYPE tmp = ptr[0]; \ |
|
187 for (int i = len - 1; i > 0; i--) \ |
|
188 data[i] = ptr[i]; \ |
|
189 data[0] = tmp; \ |
|
190 } \ |
|
191 } \ |
|
192 while (0) |
|
193 |
|
194 switch (type) |
|
195 { |
|
196 case miINT8: |
5164
|
197 READ_INTEGER_DATA (signed char, swap, m, 1, count, is); |
5089
|
198 break; |
|
199 |
|
200 case miUINT8: |
5164
|
201 READ_INTEGER_DATA (unsigned char, swap, m, 1, count, is); |
5089
|
202 break; |
|
203 |
|
204 case miINT16: |
5164
|
205 READ_INTEGER_DATA (signed TWO_BYTE_INT, swap, m, 2, count, is); |
5089
|
206 break; |
|
207 |
|
208 case miUINT16: |
5164
|
209 READ_INTEGER_DATA (unsigned TWO_BYTE_INT, swap, m, 2, count, is); |
5089
|
210 break; |
|
211 |
|
212 case miINT32: |
5164
|
213 READ_INTEGER_DATA (signed FOUR_BYTE_INT, swap, m, 4, count, is); |
5089
|
214 break; |
|
215 |
|
216 case miUINT32: |
5164
|
217 READ_INTEGER_DATA (unsigned FOUR_BYTE_INT, swap, m, 4, count, is); |
5089
|
218 break; |
|
219 |
|
220 case miSINGLE: |
|
221 case miRESERVE1: |
|
222 case miDOUBLE: |
|
223 case miRESERVE2: |
|
224 case miRESERVE3: |
|
225 break; |
|
226 |
|
227 case miINT64: |
|
228 #ifdef EIGHT_BYTE_INT |
5164
|
229 READ_INTEGER_DATA (signed EIGHT_BYTE_INT, swap, m, 8, count, is); |
5089
|
230 #endif |
|
231 break; |
|
232 |
|
233 case miUINT64: |
|
234 #ifdef EIGHT_BYTE_INT |
5164
|
235 READ_INTEGER_DATA (unsigned EIGHT_BYTE_INT, swap, m, 8, count, is); |
5089
|
236 #endif |
|
237 break; |
|
238 |
|
239 case miMATRIX: |
|
240 default: |
|
241 break; |
|
242 } |
|
243 |
|
244 #undef READ_INTEGER_DATA |
|
245 |
|
246 } |
|
247 |
5164
|
248 template void read_mat5_integer_data (std::istream& is, octave_int8 *m, |
5089
|
249 int count, bool swap, |
|
250 mat5_data_type type); |
5164
|
251 template void read_mat5_integer_data (std::istream& is, octave_int16 *m, |
5089
|
252 int count, bool swap, |
|
253 mat5_data_type type); |
5164
|
254 template void read_mat5_integer_data (std::istream& is, octave_int32 *m, |
|
255 int count, bool swap, |
|
256 mat5_data_type type); |
|
257 template void read_mat5_integer_data (std::istream& is, octave_int64 *m, |
5089
|
258 int count, bool swap, |
|
259 mat5_data_type type); |
5164
|
260 template void read_mat5_integer_data (std::istream& is, octave_uint8 *m, |
5089
|
261 int count, bool swap, |
|
262 mat5_data_type type); |
5164
|
263 template void read_mat5_integer_data (std::istream& is, octave_uint16 *m, |
5089
|
264 int count, bool swap, |
|
265 mat5_data_type type); |
5164
|
266 template void read_mat5_integer_data (std::istream& is, octave_uint32 *m, |
5089
|
267 int count, bool swap, |
|
268 mat5_data_type type); |
5164
|
269 template void read_mat5_integer_data (std::istream& is, octave_uint64 *m, |
5089
|
270 int count, bool swap, |
|
271 mat5_data_type type); |
5164
|
272 |
|
273 template void read_mat5_integer_data (std::istream& is, int *m, |
5089
|
274 int count, bool swap, |
|
275 mat5_data_type type); |
|
276 |
|
277 #define OCTAVE_MAT5_INTEGER_READ(TYP) \ |
|
278 { \ |
|
279 TYP re (dims); \ |
|
280 \ |
|
281 std::streampos tmp_pos; \ |
|
282 \ |
|
283 if (read_mat5_tag (is, swap, type, len)) \ |
|
284 { \ |
|
285 error ("load: reading matrix data for `%s'", retval.c_str ()); \ |
|
286 goto data_read_error; \ |
|
287 } \ |
|
288 \ |
|
289 int n = re.length (); \ |
|
290 tmp_pos = is.tellg (); \ |
5164
|
291 read_mat5_integer_data (is, re.fortran_vec (), n, swap, \ |
5089
|
292 (enum mat5_data_type) type); \ |
|
293 \ |
|
294 if (! is || error_state) \ |
|
295 { \ |
|
296 error ("load: reading matrix data for `%s'", retval.c_str ()); \ |
|
297 goto data_read_error; \ |
|
298 } \ |
|
299 \ |
|
300 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); \ |
|
301 \ |
|
302 if (imag) \ |
|
303 { \ |
|
304 /* We don't handle imag integer types, convert to an array */ \ |
|
305 NDArray im (dims); \ |
|
306 \ |
|
307 if (read_mat5_tag (is, swap, type, len)) \ |
|
308 { \ |
|
309 error ("load: reading matrix data for `%s'", \ |
|
310 retval.c_str ()); \ |
|
311 goto data_read_error; \ |
|
312 } \ |
|
313 \ |
|
314 n = im.length (); \ |
|
315 read_mat5_binary_data (is, im.fortran_vec (), n, swap, \ |
|
316 (enum mat5_data_type) type, flt_fmt); \ |
|
317 \ |
|
318 if (! is || error_state) \ |
|
319 { \ |
|
320 error ("load: reading imaginary matrix data for `%s'", \ |
|
321 retval.c_str ()); \ |
|
322 goto data_read_error; \ |
|
323 } \ |
|
324 \ |
|
325 ComplexNDArray ctmp (dims); \ |
|
326 \ |
|
327 for (int i = 0; i < n; i++) \ |
|
328 ctmp(i) = Complex (double (re(i)), im(i)); \ |
|
329 \ |
|
330 tc = ctmp; \ |
|
331 } \ |
|
332 else \ |
|
333 tc = re; \ |
|
334 } |
|
335 |
4634
|
336 // Read one element tag from stream IS, |
|
337 // place the type code in TYPE and the byte count in BYTES |
|
338 // return nonzero on error |
|
339 static int |
|
340 read_mat5_tag (std::istream& is, bool swap, int& type, int& bytes) |
|
341 { |
|
342 unsigned int upper; |
|
343 FOUR_BYTE_INT temp; |
|
344 |
|
345 if (! is.read (X_CAST (char *, &temp), 4 )) |
|
346 goto data_read_error; |
|
347 |
|
348 if (swap) |
4944
|
349 swap_bytes<4> (&temp); |
4634
|
350 |
|
351 upper = (temp >> 16) & 0xffff; |
|
352 type = temp & 0xffff; |
|
353 |
|
354 if (upper) |
|
355 { |
|
356 // "compressed" format |
|
357 bytes = upper; |
|
358 } |
|
359 else |
|
360 { |
|
361 if (! is.read (X_CAST (char *, &temp), 4 )) |
|
362 goto data_read_error; |
|
363 if (swap) |
4944
|
364 swap_bytes<4> (&temp); |
4634
|
365 bytes = temp; |
|
366 } |
|
367 |
|
368 return 0; |
|
369 |
|
370 data_read_error: |
|
371 return 1; |
|
372 } |
|
373 |
4944
|
374 static void |
|
375 read_int (std::istream& is, bool swap, FOUR_BYTE_INT& val) |
|
376 { |
|
377 is.read (reinterpret_cast<char *> (&val), 4); |
|
378 |
|
379 if (swap) |
|
380 swap_bytes<4> (&val); |
|
381 } |
|
382 |
4634
|
383 // Extract one data element (scalar, matrix, string, etc.) from stream |
|
384 // IS and place it in TC, returning the name of the variable. |
|
385 // |
|
386 // The data is expected to be in Matlab's "Version 5" .mat format, |
|
387 // though not all the features of that format are supported. |
|
388 // |
|
389 // FILENAME is used for error messages. |
|
390 |
|
391 std::string |
|
392 read_mat5_binary_element (std::istream& is, const std::string& filename, |
|
393 bool swap, bool& global, octave_value& tc) |
|
394 { |
|
395 std::string retval; |
|
396 |
|
397 // These are initialized here instead of closer to where they are |
|
398 // first used to avoid errors from gcc about goto crossing |
|
399 // initialization of variable. |
|
400 |
|
401 oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown; |
|
402 int type = 0; |
|
403 bool imag; |
|
404 bool logicalvar; |
|
405 enum arrayclasstype arrayclass; |
5164
|
406 FOUR_BYTE_INT nnz; |
4634
|
407 FOUR_BYTE_INT flags; |
|
408 dim_vector dims; |
|
409 int len; |
|
410 int element_length; |
|
411 std::streampos pos; |
|
412 TWO_BYTE_INT number; |
|
413 number = *(TWO_BYTE_INT *)"\x00\x01"; |
|
414 |
|
415 global = false; |
|
416 |
|
417 // MAT files always use IEEE floating point |
|
418 if ((number == 1) ^ swap) |
|
419 flt_fmt = oct_mach_info::flt_fmt_ieee_big_endian; |
|
420 else |
|
421 flt_fmt = oct_mach_info::flt_fmt_ieee_little_endian; |
|
422 |
|
423 // element type and length |
|
424 if (read_mat5_tag (is, swap, type, element_length)) |
|
425 return retval; // EOF |
|
426 |
5383
|
427 #ifdef HAVE_ZLIB |
5269
|
428 if (type == miCOMPRESSED) |
|
429 { |
|
430 // If C++ allowed us direct access to the file descriptor of an ifstream |
|
431 // in a uniform way, the code below could be vastly simplified, and |
|
432 // additional copies of the data in memory wouldn't be needed!! |
|
433 |
|
434 OCTAVE_LOCAL_BUFFER (char, inbuf, element_length); |
|
435 is.read (inbuf, element_length); |
|
436 |
|
437 // We uncompress the first 8 bytes of the header to get the buffer length |
|
438 // This will fail with an error Z_MEM_ERROR |
|
439 uLongf destLen = 8; |
|
440 OCTAVE_LOCAL_BUFFER (unsigned int, tmp, 2); |
|
441 if (uncompress (X_CAST (Bytef *, tmp), &destLen, |
|
442 X_CAST (Bytef *, inbuf), element_length) != Z_MEM_ERROR) |
|
443 { |
|
444 // Why should I have to initialize outbuf as I'll just overwrite!! |
5322
|
445 if (swap) |
|
446 swap_bytes<4> (tmp, 2); |
|
447 |
5269
|
448 destLen = tmp[1] + 8; |
|
449 std::string outbuf (destLen, ' '); |
|
450 |
|
451 int err = uncompress (X_CAST (Bytef *, outbuf.c_str ()), &destLen, |
|
452 X_CAST ( Bytef *, inbuf), element_length); |
|
453 //if (uncompress (X_CAST (Bytef *, outbuf.c_str ()), &destLen, |
|
454 // X_CAST ( Bytef *, inbuf), element_length) != Z_OK) |
|
455 |
|
456 if (err != Z_OK) |
|
457 error ("load: error uncompressing data element"); |
|
458 else |
|
459 { |
|
460 ISSTREAM gz_is (outbuf); |
|
461 retval = read_mat5_binary_element (gz_is, filename, |
|
462 swap, global, tc); |
|
463 } |
|
464 } |
|
465 else |
|
466 error ("load: error probing size of compressed data element"); |
|
467 |
|
468 return retval; |
|
469 } |
|
470 #endif |
|
471 |
4634
|
472 if (type != miMATRIX) |
|
473 { |
|
474 error ("load: invalid element type"); |
|
475 goto early_read_error; |
|
476 } |
|
477 |
|
478 if (element_length == 0) |
|
479 { |
|
480 tc = Matrix (); |
|
481 return retval; |
|
482 } |
|
483 |
|
484 pos = is.tellg (); |
|
485 |
|
486 // array flags subelement |
|
487 if (read_mat5_tag (is, swap, type, len) || type != miUINT32 || len != 8) |
|
488 { |
|
489 error ("load: invalid array flags subelement"); |
|
490 goto early_read_error; |
|
491 } |
|
492 |
|
493 read_int (is, swap, flags); |
|
494 imag = (flags & 0x0800) != 0; // has an imaginary part? |
|
495 global = (flags & 0x0400) != 0; // global variable? |
5269
|
496 logicalvar = (flags & 0x0200) != 0; // boolean ? |
4634
|
497 arrayclass = (arrayclasstype)(flags & 0xff); |
5164
|
498 read_int (is, swap, nnz); // number of non-zero in sparse |
4634
|
499 |
|
500 // dimensions array subelement |
|
501 { |
4638
|
502 FOUR_BYTE_INT dim_len; |
|
503 |
|
504 if (read_mat5_tag (is, swap, type, dim_len) || type != miINT32) |
4634
|
505 { |
|
506 error ("load: invalid dimensions array subelement"); |
|
507 goto early_read_error; |
|
508 } |
|
509 |
4638
|
510 int ndims = dim_len / 4; |
4634
|
511 dims.resize (ndims); |
|
512 for (int i = 0; i < ndims; i++) |
|
513 { |
|
514 FOUR_BYTE_INT n; |
|
515 read_int (is, swap, n); |
|
516 dims(i) = n; |
|
517 } |
|
518 |
|
519 std::streampos tmp_pos = is.tellg (); |
4638
|
520 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (dim_len) - dim_len)); |
4634
|
521 } |
|
522 |
|
523 if (read_mat5_tag (is, swap, type, len) || type != miINT8) |
|
524 { |
|
525 error ("load: invalid array name subelement"); |
|
526 goto early_read_error; |
|
527 } |
|
528 |
|
529 { |
|
530 OCTAVE_LOCAL_BUFFER (char, name, len+1); |
|
531 |
|
532 // Structure field subelements have zero-length array name subelements. |
|
533 |
|
534 std::streampos tmp_pos = is.tellg (); |
|
535 |
|
536 if (len) |
|
537 { |
|
538 if (! is.read (X_CAST (char *, name), len )) |
|
539 goto data_read_error; |
|
540 |
|
541 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
|
542 } |
|
543 |
|
544 name[len] = '\0'; |
|
545 retval = name; |
|
546 } |
|
547 |
|
548 switch (arrayclass) |
|
549 { |
|
550 case mxCELL_CLASS: |
|
551 { |
|
552 Cell cell_array (dims); |
|
553 |
|
554 int n = cell_array.length (); |
|
555 |
|
556 for (int i = 0; i < n; i++) |
|
557 { |
|
558 octave_value tc2; |
|
559 |
|
560 std::string nm |
|
561 = read_mat5_binary_element (is, filename, swap, global, tc2); |
|
562 |
|
563 if (! is || error_state) |
|
564 { |
|
565 error ("load: reading cell data for `%s'", nm.c_str ()); |
|
566 goto data_read_error; |
|
567 } |
|
568 |
|
569 cell_array(i) = tc2; |
|
570 } |
|
571 |
|
572 tc = cell_array; |
|
573 } |
|
574 break; |
|
575 |
|
576 case mxOBJECT_CLASS: |
|
577 warning ("load: objects are not implemented"); |
|
578 goto skip_ahead; |
|
579 |
|
580 case mxSPARSE_CLASS: |
5297
|
581 #if SIZEOF_INT != SIZEOF_OCTAVE_IDX_TYPE |
|
582 warning ("load: sparse objects are not implemented"); |
|
583 goto skip_ahead; |
|
584 #else |
5164
|
585 { |
|
586 int nr = dims(0); |
|
587 int nc = dims(1); |
|
588 SparseMatrix sm; |
|
589 SparseComplexMatrix scm; |
|
590 NDArray re; |
|
591 int *ridx; |
|
592 int *cidx; |
|
593 double *data; |
|
594 |
|
595 // Setup return value |
|
596 if (imag) |
|
597 { |
5275
|
598 scm = SparseComplexMatrix (static_cast<octave_idx_type> (nr), |
|
599 static_cast<octave_idx_type> (nc), |
|
600 static_cast<octave_idx_type> (nnz)); |
5164
|
601 ridx = scm.ridx (); |
|
602 cidx = scm.cidx (); |
|
603 re = NDArray (dim_vector (static_cast<int> (nnz))); |
|
604 data = re.fortran_vec (); |
|
605 } |
|
606 else |
|
607 { |
5275
|
608 sm = SparseMatrix (static_cast<octave_idx_type> (nr), |
|
609 static_cast<octave_idx_type> (nc), |
|
610 static_cast<octave_idx_type> (nnz)); |
5164
|
611 ridx = sm.ridx (); |
|
612 cidx = sm.cidx (); |
|
613 data = sm.data (); |
|
614 } |
|
615 |
|
616 // row indices |
|
617 std::streampos tmp_pos; |
|
618 |
|
619 if (read_mat5_tag (is, swap, type, len)) |
|
620 { |
|
621 error ("load: reading sparse row data for `%s'", retval.c_str ()); |
|
622 goto data_read_error; |
|
623 } |
|
624 |
|
625 tmp_pos = is.tellg (); |
|
626 |
|
627 read_mat5_integer_data (is, ridx, nnz, swap, |
|
628 (enum mat5_data_type) type); |
|
629 |
|
630 if (! is || error_state) |
|
631 { |
|
632 error ("load: reading sparse row data for `%s'", retval.c_str ()); |
|
633 goto data_read_error; |
|
634 } |
|
635 |
|
636 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
|
637 |
|
638 // col indices |
|
639 if (read_mat5_tag (is, swap, type, len)) |
|
640 { |
|
641 error ("load: reading sparse column data for `%s'", retval.c_str ()); |
|
642 goto data_read_error; |
|
643 } |
|
644 |
|
645 tmp_pos = is.tellg (); |
|
646 |
|
647 read_mat5_integer_data (is, cidx, nc + 1, swap, |
|
648 (enum mat5_data_type) type); |
|
649 |
|
650 if (! is || error_state) |
|
651 { |
|
652 error ("load: reading sparse column data for `%s'", retval.c_str ()); |
|
653 goto data_read_error; |
|
654 } |
|
655 |
|
656 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
|
657 |
|
658 // real data subelement |
|
659 if (read_mat5_tag (is, swap, type, len)) |
|
660 { |
|
661 error ("load: reading sparse matrix data for `%s'", retval.c_str ()); |
|
662 goto data_read_error; |
|
663 } |
|
664 |
|
665 tmp_pos = is.tellg (); |
|
666 read_mat5_binary_data (is, data, nnz, swap, |
|
667 (enum mat5_data_type) type, flt_fmt); |
|
668 |
|
669 if (! is || error_state) |
|
670 { |
|
671 error ("load: reading sparse matrix data for `%s'", retval.c_str ()); |
|
672 goto data_read_error; |
|
673 } |
|
674 |
|
675 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
|
676 |
|
677 // imaginary data subelement |
|
678 if (imag) |
|
679 { |
|
680 NDArray im (dim_vector (static_cast<int> (nnz))); |
|
681 |
|
682 if (read_mat5_tag (is, swap, type, len)) |
|
683 { |
|
684 error ("load: reading sparse matrix data for `%s'", retval.c_str ()); |
|
685 goto data_read_error; |
|
686 } |
|
687 |
|
688 read_mat5_binary_data (is, im.fortran_vec (), nnz, swap, |
|
689 (enum mat5_data_type) type, flt_fmt); |
|
690 |
|
691 if (! is || error_state) |
|
692 { |
|
693 error ("load: reading imaginary sparse matrix data for `%s'", |
|
694 retval.c_str ()); |
|
695 goto data_read_error; |
|
696 } |
|
697 |
|
698 for (int i = 0; i < nnz; i++) |
|
699 scm.xdata (i) = Complex (re (i), im (i)); |
|
700 |
|
701 tc = scm; |
|
702 } |
|
703 else |
|
704 tc = sm; |
|
705 } |
|
706 break; |
5297
|
707 #endif |
4634
|
708 |
5089
|
709 case mxFUNCTION_CLASS: |
|
710 warning ("load: function handles are not implemented"); |
|
711 goto skip_ahead; |
|
712 |
4634
|
713 case mxSTRUCT_CLASS: |
|
714 { |
|
715 Octave_map m; |
|
716 FOUR_BYTE_INT fn_type; |
|
717 FOUR_BYTE_INT fn_len; |
|
718 FOUR_BYTE_INT field_name_length; |
|
719 |
|
720 // field name length subelement -- actually the maximum length |
|
721 // of a field name. The Matlab docs promise this will always |
|
722 // be 32. We read and use the actual value, on the theory |
|
723 // that eventually someone will recognize that's a waste of |
|
724 // space. |
|
725 if (read_mat5_tag (is, swap, fn_type, fn_len) || fn_type != miINT32) |
|
726 { |
|
727 error ("load: invalid field name subelement"); |
|
728 goto data_read_error; |
|
729 } |
|
730 |
|
731 if (! is.read (X_CAST (char *, &field_name_length), fn_len )) |
|
732 goto data_read_error; |
|
733 |
|
734 if (swap) |
4944
|
735 swap_bytes<4> (&field_name_length); |
4634
|
736 |
|
737 // field name subelement. The length of this subelement tells |
|
738 // us how many fields there are. |
|
739 if (read_mat5_tag (is, swap, fn_type, fn_len) || fn_type != miINT8) |
|
740 { |
|
741 error ("load: invalid field name subelement"); |
|
742 goto data_read_error; |
|
743 } |
|
744 |
5336
|
745 octave_idx_type n_fields = fn_len/field_name_length; |
4634
|
746 |
|
747 fn_len = PAD (fn_len); |
|
748 |
|
749 OCTAVE_LOCAL_BUFFER (char, elname, fn_len); |
|
750 |
|
751 if (! is.read (elname, fn_len)) |
|
752 goto data_read_error; |
|
753 |
5336
|
754 std::vector<Cell> elt (n_fields); |
4634
|
755 |
5336
|
756 for (octave_idx_type i = 0; i < n_fields; i++) |
|
757 elt[i] = Cell (dims); |
|
758 |
|
759 octave_idx_type n = dims.numel (); |
4634
|
760 |
|
761 // fields subelements |
5336
|
762 for (octave_idx_type j = 0; j < n; j++) |
4634
|
763 { |
5336
|
764 for (octave_idx_type i = 0; i < n_fields; i++) |
4634
|
765 { |
|
766 octave_value fieldtc; |
|
767 read_mat5_binary_element (is, filename, swap, global, fieldtc); |
5336
|
768 elt[i](j) = fieldtc; |
4634
|
769 } |
5336
|
770 |
4634
|
771 } |
|
772 |
5336
|
773 for (octave_idx_type i = 0; i < n_fields; i++) |
4634
|
774 { |
5336
|
775 const char *key = elname + i*field_name_length; |
4634
|
776 |
5336
|
777 m.assign (key, elt[i]); |
4634
|
778 } |
|
779 |
|
780 tc = m; |
|
781 } |
|
782 break; |
|
783 |
5089
|
784 case mxINT8_CLASS: |
|
785 OCTAVE_MAT5_INTEGER_READ (int8NDArray); |
|
786 break; |
|
787 |
|
788 case mxUINT8_CLASS: |
5269
|
789 { |
|
790 OCTAVE_MAT5_INTEGER_READ (uint8NDArray); |
|
791 |
|
792 // logical variables can either be mxUINT8_CLASS or mxDOUBLE_CLASS, |
|
793 // so chek if we have a logical variable and convert it |
|
794 |
|
795 if (logicalvar) |
|
796 { |
|
797 uint8NDArray in = tc.uint8_array_value (); |
|
798 int nel = in.nelem (); |
|
799 boolNDArray out (dims); |
|
800 |
|
801 for (int i = 0; i < nel; i++) |
|
802 out (i) = static_cast<bool> (double (in (i))); |
|
803 |
|
804 tc = out; |
|
805 } |
|
806 } |
5089
|
807 break; |
|
808 |
|
809 case mxINT16_CLASS: |
|
810 OCTAVE_MAT5_INTEGER_READ (int16NDArray); |
|
811 break; |
|
812 |
|
813 case mxUINT16_CLASS: |
|
814 OCTAVE_MAT5_INTEGER_READ (uint16NDArray); |
|
815 break; |
|
816 |
|
817 case mxINT32_CLASS: |
|
818 OCTAVE_MAT5_INTEGER_READ (int32NDArray); |
|
819 break; |
|
820 |
|
821 case mxUINT32_CLASS: |
|
822 OCTAVE_MAT5_INTEGER_READ (uint32NDArray); |
|
823 break; |
|
824 |
|
825 case mxINT64_CLASS: |
|
826 OCTAVE_MAT5_INTEGER_READ (int64NDArray); |
|
827 break; |
|
828 |
|
829 case mxUINT64_CLASS: |
|
830 OCTAVE_MAT5_INTEGER_READ (uint64NDArray); |
|
831 break; |
|
832 |
4634
|
833 case mxCHAR_CLASS: |
|
834 // handle as a numerical array to start with |
|
835 |
|
836 case mxDOUBLE_CLASS: |
|
837 case mxSINGLE_CLASS: |
|
838 default: |
5089
|
839 { |
|
840 NDArray re (dims); |
4634
|
841 |
5089
|
842 // real data subelement |
|
843 |
4634
|
844 std::streampos tmp_pos; |
5089
|
845 |
4634
|
846 if (read_mat5_tag (is, swap, type, len)) |
|
847 { |
|
848 error ("load: reading matrix data for `%s'", retval.c_str ()); |
|
849 goto data_read_error; |
|
850 } |
|
851 |
|
852 int n = re.length (); |
|
853 tmp_pos = is.tellg (); |
|
854 read_mat5_binary_data (is, re.fortran_vec (), n, swap, |
|
855 (enum mat5_data_type) type, flt_fmt); |
|
856 |
|
857 if (! is || error_state) |
|
858 { |
|
859 error ("load: reading matrix data for `%s'", retval.c_str ()); |
|
860 goto data_read_error; |
|
861 } |
|
862 |
|
863 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
5089
|
864 |
5269
|
865 if (logicalvar) |
5089
|
866 { |
5269
|
867 // logical variables can either be mxUINT8_CLASS or mxDOUBLE_CLASS, |
|
868 // so chek if we have a logical variable and convert it |
|
869 |
|
870 boolNDArray out (dims); |
|
871 |
|
872 for (int i = 0; i < n; i++) |
|
873 out (i) = static_cast<bool> (re (i)); |
|
874 |
|
875 tc = out; |
|
876 } |
|
877 else if (imag) |
|
878 { |
|
879 // imaginary data subelement |
|
880 |
5089
|
881 NDArray im (dims); |
4634
|
882 |
5089
|
883 if (read_mat5_tag (is, swap, type, len)) |
|
884 { |
|
885 error ("load: reading matrix data for `%s'", retval.c_str ()); |
|
886 goto data_read_error; |
|
887 } |
4634
|
888 |
5089
|
889 n = im.length (); |
|
890 read_mat5_binary_data (is, im.fortran_vec (), n, swap, |
|
891 (enum mat5_data_type) type, flt_fmt); |
4634
|
892 |
5089
|
893 if (! is || error_state) |
|
894 { |
|
895 error ("load: reading imaginary matrix data for `%s'", |
|
896 retval.c_str ()); |
|
897 goto data_read_error; |
|
898 } |
4634
|
899 |
5089
|
900 ComplexNDArray ctmp (dims); |
4634
|
901 |
5089
|
902 for (int i = 0; i < n; i++) |
|
903 ctmp(i) = Complex (re(i), im(i)); |
4634
|
904 |
5089
|
905 tc = ctmp; |
|
906 } |
|
907 else |
5269
|
908 { |
5351
|
909 if (arrayclass == mxCHAR_CLASS) |
|
910 { |
|
911 if (type == miUTF16 || type == miUTF32) |
|
912 { |
|
913 error ("load: can not read Unicode UTF16 and UTF32 encoded characters"); |
|
914 goto data_read_error; |
|
915 } |
|
916 else if (type == miUTF8) |
|
917 { |
|
918 // Search for multi-byte encoded UTF8 characters and |
|
919 // replace with 0x3F for '?'... Give the user a warning |
4634
|
920 |
5351
|
921 bool utf8_multi_byte = false; |
|
922 for (int i = 0; i < n; i++) |
|
923 { |
5352
|
924 unsigned char a = static_cast<unsigned char> (re(i)); |
5351
|
925 if (a > 0x7f) |
|
926 utf8_multi_byte = true; |
|
927 } |
|
928 |
|
929 if (utf8_multi_byte) |
|
930 { |
|
931 warning ("load: can not read multi-byte encoded UTF8 characters."); |
|
932 warning (" Replacing unreadable characters with '?'."); |
|
933 for (int i = 0; i < n; i++) |
|
934 { |
5352
|
935 unsigned char a = static_cast<unsigned char> (re(i)); |
5351
|
936 if (a > 0x7f) |
5352
|
937 re(i) = '?'; |
5351
|
938 } |
|
939 } |
|
940 } |
|
941 tc = re; |
|
942 tc = tc.convert_to_str (false, true, '\''); |
|
943 } |
|
944 else |
|
945 tc = re; |
5269
|
946 } |
5089
|
947 } |
4634
|
948 } |
|
949 |
|
950 is.seekg (pos + static_cast<std::streamoff> (element_length)); |
|
951 |
|
952 if (is.eof ()) |
|
953 is.clear (); |
|
954 |
|
955 return retval; |
|
956 |
|
957 data_read_error: |
|
958 early_read_error: |
|
959 error ("load: trouble reading binary file `%s'", filename.c_str ()); |
|
960 return std::string (); |
|
961 |
|
962 skip_ahead: |
|
963 warning ("skipping over `%s'", retval.c_str ()); |
|
964 is.seekg (pos + static_cast<std::streamoff> (element_length)); |
|
965 return read_mat5_binary_element (is, filename, swap, global, tc); |
|
966 } |
|
967 |
|
968 int |
|
969 read_mat5_binary_file_header (std::istream& is, bool& swap, bool quiet) |
|
970 { |
|
971 TWO_BYTE_INT version=0, magic=0; |
|
972 |
|
973 is.seekg (124, std::ios::beg); |
|
974 is.read (X_CAST (char *, &version), 2); |
|
975 is.read (X_CAST (char *, &magic), 2); |
|
976 |
|
977 if (magic == 0x4d49) |
|
978 swap = 0; |
|
979 else if (magic == 0x494d) |
|
980 swap = 1; |
|
981 else |
|
982 { |
|
983 if (! quiet) |
|
984 error ("load: can't read binary file"); |
|
985 return -1; |
|
986 } |
|
987 |
|
988 if (! swap) // version number is inverse swapped! |
|
989 version = ((version >> 8) & 0xff) + ((version & 0xff) << 8); |
|
990 |
|
991 if (version != 1 && !quiet) |
|
992 warning ("load: found version %d binary MAT file, " |
|
993 "but only prepared for version 1", version); |
|
994 |
|
995 return 0; |
|
996 } |
|
997 |
|
998 static int |
|
999 write_mat5_tag (std::ostream& is, int type, int bytes) |
|
1000 { |
|
1001 FOUR_BYTE_INT temp; |
|
1002 |
|
1003 if (bytes <= 4) |
|
1004 temp = (bytes << 16) + type; |
|
1005 else |
|
1006 { |
|
1007 temp = type; |
|
1008 if (! is.write ((char *)&temp, 4)) |
|
1009 goto data_write_error; |
|
1010 temp = bytes; |
|
1011 } |
|
1012 |
|
1013 if (! is.write ((char *)&temp, 4)) |
|
1014 goto data_write_error; |
|
1015 |
|
1016 return 0; |
|
1017 |
|
1018 data_write_error: |
|
1019 return 1; |
|
1020 } |
|
1021 |
|
1022 // write out the numeric values in M to OS, |
|
1023 // preceded by the appropriate tag. |
|
1024 static void |
|
1025 write_mat5_array (std::ostream& os, const NDArray& m, bool save_as_floats) |
|
1026 { |
|
1027 int nel = m.nelem (); |
|
1028 double max_val, min_val; |
|
1029 save_type st = LS_DOUBLE; |
|
1030 mat5_data_type mst; |
|
1031 int size; |
|
1032 unsigned len; |
|
1033 const double *data = m.data (); |
|
1034 |
|
1035 // Have to use copy here to avoid writing over data accessed via |
|
1036 // Matrix::data(). |
|
1037 |
|
1038 #define MAT5_DO_WRITE(TYPE, data, count, stream) \ |
|
1039 do \ |
|
1040 { \ |
|
1041 OCTAVE_LOCAL_BUFFER (TYPE, ptr, count); \ |
|
1042 for (int i = 0; i < count; i++) \ |
|
1043 ptr[i] = X_CAST (TYPE, data[i]); \ |
|
1044 stream.write (X_CAST (char *, ptr), count * sizeof (TYPE)); \ |
|
1045 } \ |
|
1046 while (0) |
|
1047 |
|
1048 if (save_as_floats) |
|
1049 { |
|
1050 if (m.too_large_for_float ()) |
|
1051 { |
|
1052 warning ("save: some values too large to save as floats --"); |
|
1053 warning ("save: saving as doubles instead"); |
|
1054 } |
|
1055 else |
|
1056 st = LS_FLOAT; |
|
1057 } |
|
1058 |
|
1059 if (m.all_integers (max_val, min_val)) |
|
1060 st = get_save_type (max_val, min_val); |
|
1061 |
|
1062 switch (st) |
|
1063 { |
|
1064 default: |
|
1065 case LS_DOUBLE: mst = miDOUBLE; size = 8; break; |
|
1066 case LS_FLOAT: mst = miSINGLE; size = 4; break; |
|
1067 case LS_U_CHAR: mst = miUINT8; size = 1; break; |
|
1068 case LS_U_SHORT: mst = miUINT16; size = 2; break; |
|
1069 case LS_U_INT: mst = miUINT32; size = 4; break; |
|
1070 case LS_CHAR: mst = miINT8; size = 1; break; |
|
1071 case LS_SHORT: mst = miINT16; size = 2; break; |
|
1072 case LS_INT: mst = miINT32; size = 4; break; |
|
1073 } |
|
1074 |
|
1075 len = nel*size; |
|
1076 write_mat5_tag (os, mst, len); |
|
1077 |
|
1078 { |
|
1079 switch (st) |
|
1080 { |
|
1081 case LS_U_CHAR: |
|
1082 MAT5_DO_WRITE (unsigned char, data, nel, os); |
|
1083 break; |
|
1084 |
|
1085 case LS_U_SHORT: |
|
1086 MAT5_DO_WRITE (unsigned TWO_BYTE_INT, data, nel, os); |
|
1087 break; |
|
1088 |
|
1089 case LS_U_INT: |
|
1090 MAT5_DO_WRITE (unsigned FOUR_BYTE_INT, data, nel, os); |
|
1091 break; |
|
1092 |
|
1093 // provide for 64 bit ints, even though get_save_type does |
|
1094 // not yet implement them |
|
1095 #ifdef EIGHT_BYTE_INT |
|
1096 case LS_U_LONG: |
|
1097 MAT5_DO_WRITE (unsigned EIGHT_BYTE_INT, data, nel, os); |
|
1098 break; |
|
1099 #endif |
|
1100 |
|
1101 case LS_CHAR: |
|
1102 MAT5_DO_WRITE (signed char, data, nel, os); |
|
1103 break; |
|
1104 |
|
1105 case LS_SHORT: |
|
1106 MAT5_DO_WRITE (TWO_BYTE_INT, data, nel, os); |
|
1107 break; |
|
1108 |
|
1109 case LS_INT: |
|
1110 MAT5_DO_WRITE (FOUR_BYTE_INT, data, nel, os); |
|
1111 break; |
|
1112 |
|
1113 #ifdef EIGHT_BYTE_INT |
|
1114 case LS_LONG: |
|
1115 MAT5_DO_WRITE (EIGHT_BYTE_INT, data, nel, os); |
|
1116 break; |
|
1117 #endif |
|
1118 |
|
1119 case LS_FLOAT: |
|
1120 MAT5_DO_WRITE (float, data, nel, os); |
|
1121 break; |
|
1122 |
|
1123 case LS_DOUBLE: // No conversion necessary. |
|
1124 os.write (X_CAST (char *, data), len); |
|
1125 break; |
|
1126 |
|
1127 default: |
|
1128 (*current_liboctave_error_handler) |
|
1129 ("unrecognized data format requested"); |
|
1130 break; |
|
1131 } |
|
1132 } |
|
1133 if (PAD (len) > len) |
|
1134 { |
|
1135 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
1136 os.write (buf, PAD (len) - len); |
|
1137 } |
|
1138 } |
|
1139 |
5089
|
1140 template <class T> |
|
1141 void |
5164
|
1142 write_mat5_integer_data (std::ostream& os, const T *m, int size, int nel) |
5089
|
1143 { |
|
1144 mat5_data_type mst; |
|
1145 unsigned len; |
|
1146 |
|
1147 switch (size) |
|
1148 { |
|
1149 case 1: |
|
1150 mst = miUINT8; |
|
1151 break; |
|
1152 case 2: |
|
1153 mst = miUINT16; |
|
1154 break; |
5164
|
1155 case 4: |
5089
|
1156 mst = miUINT32; |
|
1157 break; |
5164
|
1158 case 8: |
5089
|
1159 mst = miUINT64; |
|
1160 break; |
|
1161 case -1: |
|
1162 mst = miINT8; |
|
1163 size = - size; |
|
1164 break; |
|
1165 case -2: |
|
1166 mst = miINT16; |
|
1167 size = - size; |
|
1168 break; |
5164
|
1169 case -4: |
5089
|
1170 mst = miINT32; |
|
1171 size = - size; |
|
1172 break; |
5164
|
1173 case -8: |
5089
|
1174 default: |
|
1175 mst = miINT64; |
|
1176 size = - size; |
|
1177 break; |
|
1178 } |
|
1179 |
|
1180 len = nel*size; |
|
1181 write_mat5_tag (os, mst, len); |
|
1182 |
5164
|
1183 os.write (X_CAST(char *, m), len); |
5089
|
1184 |
|
1185 if (PAD (len) > len) |
|
1186 { |
|
1187 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
1188 os.write (buf, PAD (len) - len); |
|
1189 } |
|
1190 } |
|
1191 |
5164
|
1192 template void write_mat5_integer_data (std::ostream& os, const octave_int8 *m, |
|
1193 int size, int nel); |
|
1194 template void write_mat5_integer_data (std::ostream& os, const octave_int16 *m, |
|
1195 int size, int nel); |
|
1196 template void write_mat5_integer_data (std::ostream& os, const octave_int32 *m, |
|
1197 int size, int nel); |
|
1198 template void write_mat5_integer_data (std::ostream& os, const octave_int64 *m, |
|
1199 int size, int nel); |
|
1200 template void write_mat5_integer_data (std::ostream& os, const octave_uint8 *m, |
|
1201 int size, int nel); |
|
1202 template void write_mat5_integer_data (std::ostream& os, const octave_uint16 *m, |
|
1203 int size, int nel); |
|
1204 template void write_mat5_integer_data (std::ostream& os, const octave_uint32 *m, |
|
1205 int size, int nel); |
|
1206 template void write_mat5_integer_data (std::ostream& os, const octave_uint64 *m, |
|
1207 int size, int nel); |
|
1208 template void write_mat5_integer_data (std::ostream& os, const int *m, |
|
1209 int size, int nel); |
5089
|
1210 |
4634
|
1211 // Write out cell element values in the cell array to OS, preceded by |
|
1212 // the appropriate tag. |
|
1213 |
|
1214 static bool |
4701
|
1215 write_mat5_cell_array (std::ostream& os, const Cell& cell, |
|
1216 bool mark_as_global, bool save_as_floats) |
4634
|
1217 { |
|
1218 int nel = cell.nelem (); |
|
1219 |
|
1220 for (int i = 0; i < nel; i++) |
|
1221 { |
|
1222 octave_value ov = cell(i); |
|
1223 |
|
1224 if (! save_mat5_binary_element (os, ov, "", mark_as_global, |
5269
|
1225 false, save_as_floats)) |
4634
|
1226 return false; |
|
1227 } |
|
1228 |
|
1229 return true; |
|
1230 } |
|
1231 |
5269
|
1232 int |
|
1233 save_mat5_array_length (const double* val, int nel, bool save_as_floats) |
|
1234 { |
|
1235 if (nel > 0) |
|
1236 { |
|
1237 int size = 8; |
|
1238 |
|
1239 if (save_as_floats) |
|
1240 { |
|
1241 bool too_large_for_float = false; |
|
1242 for (int i = 0; i < nel; i++) |
|
1243 { |
|
1244 double tmp = val [i]; |
|
1245 |
5389
|
1246 if (! (xisnan (tmp) || xisinf (tmp)) |
5388
|
1247 && fabs (tmp) > FLT_MAX) |
5269
|
1248 { |
|
1249 too_large_for_float = true; |
|
1250 break; |
|
1251 } |
|
1252 } |
|
1253 |
|
1254 if (!too_large_for_float) |
|
1255 size = 4; |
|
1256 } |
|
1257 |
|
1258 // The code below is disabled since get_save_type currently doesn't |
|
1259 // deal with integer types. This will need to be activated if get_save_type |
|
1260 // is changed. |
|
1261 |
|
1262 // double max_val = val[0]; |
|
1263 // double min_val = val[0]; |
|
1264 // bool all_integers = true; |
|
1265 // |
|
1266 // for (int i = 0; i < nel; i++) |
|
1267 // { |
|
1268 // double val = val[i]; |
|
1269 // |
|
1270 // if (val > max_val) |
|
1271 // max_val = val; |
|
1272 // |
|
1273 // if (val < min_val) |
|
1274 // min_val = val; |
|
1275 // |
|
1276 // if (D_NINT (val) != val) |
|
1277 // { |
|
1278 // all_integers = false; |
|
1279 // break; |
|
1280 // } |
|
1281 // } |
|
1282 // |
|
1283 // if (all_integers) |
|
1284 // { |
|
1285 // if (max_val < 256 && min_val > -1) |
|
1286 // size = 1; |
|
1287 // else if (max_val < 65536 && min_val > -1) |
|
1288 // size = 2; |
|
1289 // else if (max_val < 4294967295UL && min_val > -1) |
|
1290 // size = 4; |
|
1291 // else if (max_val < 128 && min_val >= -128) |
|
1292 // size = 1; |
|
1293 // else if (max_val < 32768 && min_val >= -32768) |
|
1294 // size = 2; |
|
1295 // else if (max_val <= 2147483647L && min_val >= -2147483647L) |
|
1296 // size = 4; |
|
1297 // } |
|
1298 |
|
1299 return 8 + nel * size; |
|
1300 } |
|
1301 else |
|
1302 return 8; |
|
1303 } |
|
1304 |
|
1305 int |
|
1306 save_mat5_array_length (const Complex* val, int nel, bool save_as_floats) |
|
1307 { |
|
1308 int ret; |
|
1309 |
|
1310 OCTAVE_LOCAL_BUFFER (double, tmp, nel); |
|
1311 |
|
1312 for (int i = 1; i < nel; i++) |
|
1313 tmp[i] = std::real (val[i]); |
|
1314 |
|
1315 ret = save_mat5_array_length (tmp, nel, save_as_floats); |
|
1316 |
|
1317 for (int i = 1; i < nel; i++) |
|
1318 tmp[i] = std::imag (val[i]); |
|
1319 |
|
1320 ret += save_mat5_array_length (tmp, nel, save_as_floats); |
|
1321 |
|
1322 return ret; |
|
1323 } |
|
1324 |
|
1325 int |
|
1326 save_mat5_element_length (const octave_value& tc, const std::string& name, |
|
1327 bool save_as_floats, bool mat7_format) |
|
1328 { |
|
1329 int max_namelen = (mat7_format ? 63 : 31); |
|
1330 int len = name.length (); |
|
1331 std::string cname = tc.class_name (); |
|
1332 int ret = 32; |
|
1333 |
|
1334 if (len > 4) |
|
1335 ret += PAD (len > max_namelen ? max_namelen : len); |
|
1336 |
|
1337 ret += PAD (4 * tc.ndims ()); |
|
1338 |
|
1339 if (tc.is_string ()) |
|
1340 { |
|
1341 charMatrix chm = tc.char_matrix_value (); |
5384
|
1342 ret += 8; |
|
1343 if (chm.nelem () > 1) |
|
1344 ret += PAD (2 * chm.rows () * chm.cols ()); |
5269
|
1345 } |
|
1346 else if (cname == "sparse") |
|
1347 { |
|
1348 if (tc.is_complex_type ()) |
|
1349 { |
|
1350 SparseComplexMatrix m = tc.sparse_complex_matrix_value (); |
|
1351 int nc = m.cols (); |
|
1352 int nnz = m.nnz (); |
|
1353 |
|
1354 ret += 16 + PAD (nnz * sizeof (int)) + PAD ((nc + 1) * sizeof (int)) + |
|
1355 save_mat5_array_length (m.data (), m.nelem (), save_as_floats); |
|
1356 } |
|
1357 else |
|
1358 { |
|
1359 SparseMatrix m = tc.sparse_matrix_value (); |
|
1360 int nc = m.cols (); |
|
1361 int nnz = m.nnz (); |
|
1362 |
|
1363 ret += 16 + PAD (nnz * sizeof (int)) + PAD ((nc + 1) * sizeof (int)) + |
|
1364 save_mat5_array_length (m.data (), m.nelem (), save_as_floats); |
|
1365 } |
|
1366 } |
|
1367 |
|
1368 #define INT_LEN(nel, size) \ |
|
1369 { \ |
|
1370 ret += 8; \ |
|
1371 int sz = nel * size; \ |
|
1372 if (sz > 4) \ |
|
1373 ret += PAD (sz); \ |
|
1374 } |
|
1375 |
|
1376 else if (cname == "int8") |
|
1377 INT_LEN (tc.int8_array_value ().nelem (), 1) |
|
1378 else if (cname == "int16") |
|
1379 INT_LEN (tc.int16_array_value ().nelem (), 2) |
|
1380 else if (cname == "int32") |
|
1381 INT_LEN (tc.int32_array_value ().nelem (), 4) |
|
1382 else if (cname == "int64") |
|
1383 INT_LEN (tc.int64_array_value ().nelem (), 8) |
|
1384 else if (cname == "uint8") |
|
1385 INT_LEN (tc.uint8_array_value ().nelem (), 1) |
|
1386 else if (cname == "uint16") |
|
1387 INT_LEN (tc.uint16_array_value ().nelem (), 2) |
|
1388 else if (cname == "uint32") |
|
1389 INT_LEN (tc.uint32_array_value ().nelem (), 4) |
|
1390 else if (cname == "uint64") |
|
1391 INT_LEN (tc.uint64_array_value ().nelem (), 8) |
|
1392 else if (tc.is_bool_type ()) |
|
1393 INT_LEN (tc.bool_array_value ().nelem (), 1) |
|
1394 else if (tc.is_real_scalar () || tc.is_real_matrix () || tc.is_range ()) |
|
1395 { |
|
1396 NDArray m = tc.array_value (); |
|
1397 ret += save_mat5_array_length (m.fortran_vec (), m.nelem (), |
|
1398 save_as_floats); |
|
1399 } |
|
1400 else if (tc.is_cell ()) |
|
1401 { |
|
1402 Cell cell = tc.cell_value (); |
|
1403 int nel = cell.nelem (); |
|
1404 |
|
1405 for (int i = 0; i < nel; i++) |
|
1406 ret += 8 + |
|
1407 save_mat5_element_length (cell (i), "", save_as_floats, mat7_format); |
|
1408 } |
|
1409 else if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
1410 { |
|
1411 ComplexNDArray m = tc.complex_array_value (); |
|
1412 ret += save_mat5_array_length (m.fortran_vec (), m.nelem (), |
|
1413 save_as_floats); |
|
1414 } |
|
1415 else if (tc.is_map ()) |
|
1416 { |
|
1417 int fieldcnt = 0; |
|
1418 const Octave_map m = tc.map_value (); |
|
1419 int nel = m.numel (); |
|
1420 |
|
1421 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
|
1422 fieldcnt++; |
|
1423 |
|
1424 ret += 16 + fieldcnt * (max_namelen + 1); |
|
1425 |
|
1426 |
|
1427 for (int j = 0; j < nel; j++) |
|
1428 { |
|
1429 |
|
1430 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
|
1431 { |
|
1432 Cell elts = m.contents (i); |
|
1433 |
|
1434 ret += 8 + save_mat5_element_length (elts (j), "", |
|
1435 save_as_floats, mat7_format); |
|
1436 } |
|
1437 } |
|
1438 } |
|
1439 else |
|
1440 ret = -1; |
|
1441 |
|
1442 return ret; |
|
1443 } |
|
1444 |
4634
|
1445 // save the data from TC along with the corresponding NAME on stream |
|
1446 // OS in the MatLab version 5 binary format. Return true on success. |
|
1447 |
|
1448 bool |
|
1449 save_mat5_binary_element (std::ostream& os, |
|
1450 const octave_value& tc, const std::string& name, |
5269
|
1451 bool mark_as_global, bool mat7_format, |
|
1452 bool save_as_floats, bool compressing) |
4634
|
1453 { |
|
1454 FOUR_BYTE_INT flags=0; |
5164
|
1455 FOUR_BYTE_INT nnz=0; |
4634
|
1456 std::streampos fixup, contin; |
5089
|
1457 std::string cname = tc.class_name (); |
5269
|
1458 int max_namelen = (mat7_format ? 63 : 31); |
|
1459 |
|
1460 #ifdef HAVE_ZLIB |
|
1461 if (mat7_format && !compressing) |
|
1462 { |
|
1463 bool ret = false; |
|
1464 |
|
1465 OSSTREAM buf; |
|
1466 |
|
1467 // The code seeks backwards in the stream to fix the header. Can't |
|
1468 // do this with zlib, so use a stringstream. |
|
1469 ret = save_mat5_binary_element (buf, tc, name, mark_as_global, true, |
|
1470 save_as_floats, true); |
|
1471 |
|
1472 if (ret) |
|
1473 { |
|
1474 OSSTREAM_FREEZE (buf); |
|
1475 |
5351
|
1476 // destLen must be at least 0.1% larger than source buffer |
|
1477 // + 12 bytes. Reality is it must be larger again than that. |
5269
|
1478 uLongf srcLen = OSSTREAM_STR (buf).length (); |
5351
|
1479 uLongf destLen = srcLen * 101 / 100 + 12; |
5269
|
1480 OCTAVE_LOCAL_BUFFER (char, out_buf, destLen); |
|
1481 |
|
1482 if (compress (X_CAST (Bytef *, out_buf), &destLen, |
|
1483 X_CAST (Bytef *, OSSTREAM_C_STR (buf)), srcLen) == Z_OK) |
|
1484 { |
|
1485 write_mat5_tag (os, miCOMPRESSED, X_CAST(int, destLen)); |
|
1486 os.write (out_buf, destLen); |
|
1487 } |
|
1488 else |
|
1489 { |
|
1490 error ("save: error compressing data element"); |
|
1491 ret = false; |
|
1492 } |
|
1493 } |
|
1494 |
|
1495 return ret; |
|
1496 } |
|
1497 #endif |
4634
|
1498 |
|
1499 // element type and length |
|
1500 fixup = os.tellp (); |
5269
|
1501 write_mat5_tag (os, miMATRIX, save_mat5_element_length |
|
1502 (tc, name, save_as_floats, mat7_format)); |
4634
|
1503 |
|
1504 // array flags subelement |
|
1505 write_mat5_tag (os, miUINT32, 8); |
|
1506 |
5269
|
1507 if (tc.is_bool_type ()) |
|
1508 flags |= 0x0200; |
|
1509 |
4634
|
1510 if (mark_as_global) |
|
1511 flags |= 0x0400; |
|
1512 |
|
1513 if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
1514 flags |= 0x0800; |
|
1515 |
|
1516 if (tc.is_string ()) |
|
1517 flags |= mxCHAR_CLASS; |
5089
|
1518 else if (cname == "int8") |
|
1519 flags |= mxINT8_CLASS; |
|
1520 else if (cname == "int16") |
|
1521 flags |= mxINT16_CLASS; |
|
1522 else if (cname == "int32") |
|
1523 flags |= mxINT32_CLASS; |
|
1524 else if (cname == "int64") |
|
1525 flags |= mxINT64_CLASS; |
5269
|
1526 else if (cname == "uint8" || tc.is_bool_type ()) |
5089
|
1527 flags |= mxUINT8_CLASS; |
|
1528 else if (cname == "uint16") |
|
1529 flags |= mxUINT16_CLASS; |
|
1530 else if (cname == "uint32") |
|
1531 flags |= mxUINT32_CLASS; |
|
1532 else if (cname == "uint64") |
|
1533 flags |= mxUINT64_CLASS; |
5164
|
1534 else if (cname == "sparse") |
|
1535 { |
|
1536 flags |= mxSPARSE_CLASS; |
|
1537 if (tc.is_complex_type ()) |
|
1538 { |
|
1539 SparseComplexMatrix scm = tc.sparse_complex_matrix_value (); |
|
1540 nnz = scm.nnz (); |
|
1541 } |
|
1542 else |
|
1543 { |
|
1544 SparseMatrix sm = tc.sparse_matrix_value (); |
|
1545 nnz = sm.nnz (); |
|
1546 } |
|
1547 } |
4634
|
1548 else if (tc.is_real_scalar ()) |
|
1549 flags |= mxDOUBLE_CLASS; |
|
1550 else if (tc.is_real_matrix () || tc.is_range ()) |
|
1551 flags |= mxDOUBLE_CLASS; |
|
1552 else if (tc.is_complex_scalar ()) |
|
1553 flags |= mxDOUBLE_CLASS; |
|
1554 else if (tc.is_complex_matrix ()) |
|
1555 flags |= mxDOUBLE_CLASS; |
|
1556 else if (tc.is_map ()) |
|
1557 flags |= mxSTRUCT_CLASS; |
|
1558 else if (tc.is_cell ()) |
|
1559 flags |= mxCELL_CLASS; |
|
1560 else |
|
1561 { |
|
1562 gripe_wrong_type_arg ("save", tc, false); |
|
1563 goto error_cleanup; |
|
1564 } |
|
1565 |
|
1566 os.write ((char *)&flags, 4); |
5164
|
1567 os.write ((char *)&nnz, 4); |
4634
|
1568 |
|
1569 { |
|
1570 dim_vector dv = tc.dims (); |
|
1571 int nd = tc.ndims (); |
4638
|
1572 int dim_len = 4*nd; |
4634
|
1573 |
4638
|
1574 write_mat5_tag (os, miINT32, dim_len); |
4634
|
1575 |
|
1576 for (int i = 0; i < nd; i++) |
|
1577 { |
4638
|
1578 FOUR_BYTE_INT n = dv(i); |
4634
|
1579 os.write ((char *)&n, 4); |
|
1580 } |
4638
|
1581 |
|
1582 if (PAD (dim_len) > dim_len) |
|
1583 { |
|
1584 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
1585 os.write (buf, PAD (dim_len) - dim_len); |
|
1586 } |
4634
|
1587 } |
|
1588 |
|
1589 // array name subelement |
|
1590 { |
|
1591 int namelen = name.length (); |
|
1592 |
5269
|
1593 if (namelen > max_namelen) |
|
1594 namelen = max_namelen; // only 31 or 63 char names permitted in mat file |
4634
|
1595 |
|
1596 int paddedlength = PAD (namelen); |
|
1597 |
|
1598 write_mat5_tag (os, miINT8, namelen); |
|
1599 OCTAVE_LOCAL_BUFFER (char, paddedname, paddedlength); |
|
1600 memset (paddedname, 0, paddedlength); |
|
1601 strncpy (paddedname, name.c_str (), namelen); |
|
1602 os.write (paddedname, paddedlength); |
|
1603 } |
|
1604 |
|
1605 // data element |
|
1606 if (tc.is_string ()) |
|
1607 { |
|
1608 charMatrix chm = tc.char_matrix_value (); |
|
1609 int nr = chm.rows (); |
|
1610 int nc = chm.cols (); |
|
1611 int len = nr*nc*2; |
|
1612 int paddedlength = PAD (nr*nc*2); |
|
1613 |
|
1614 OCTAVE_LOCAL_BUFFER (TWO_BYTE_INT, buf, nc*nr+3); |
|
1615 write_mat5_tag (os, miUINT16, len); |
|
1616 |
|
1617 for (int i = 0; i < nr; i++) |
|
1618 { |
|
1619 std::string tstr = chm.row_as_string (i); |
|
1620 const char *s = tstr.data (); |
|
1621 |
|
1622 for (int j = 0; j < nc; j++) |
|
1623 buf[j*nr+i] = *s++ & 0x00FF; |
|
1624 } |
|
1625 os.write ((char *)buf, nr*nc*2); |
|
1626 |
|
1627 if (paddedlength > len) |
|
1628 os.write ((char *)buf, paddedlength - len); |
|
1629 } |
5164
|
1630 else if (cname == "sparse") |
|
1631 { |
|
1632 if (tc.is_complex_type ()) |
|
1633 { |
|
1634 SparseComplexMatrix m = tc.sparse_complex_matrix_value (); |
|
1635 int nc = m.cols (); |
|
1636 |
|
1637 write_mat5_integer_data (os, m.ridx (), - sizeof(int), nnz); |
|
1638 write_mat5_integer_data (os, m.cidx (), - sizeof(int), nc + 1); |
|
1639 |
|
1640 NDArray buf (dim_vector (nnz, 1)); |
|
1641 |
|
1642 for (int i = 0; i < nnz; i++) |
5261
|
1643 buf (i) = std::real (m.data (i)); |
5164
|
1644 |
|
1645 write_mat5_array (os, buf, save_as_floats); |
|
1646 |
|
1647 for (int i = 0; i < nnz; i++) |
5261
|
1648 buf (i) = std::imag (m.data (i)); |
5164
|
1649 |
|
1650 write_mat5_array (os, buf, save_as_floats); |
|
1651 } |
|
1652 else |
|
1653 { |
|
1654 SparseMatrix m = tc.sparse_matrix_value (); |
|
1655 int nc = m.cols (); |
|
1656 |
|
1657 write_mat5_integer_data (os, m.ridx (), - sizeof(int), nnz); |
|
1658 write_mat5_integer_data (os, m.cidx (), - sizeof(int), nc + 1); |
|
1659 |
|
1660 // XXX FIXME XXX |
|
1661 // Is there a way to easily do without this buffer |
|
1662 NDArray buf (dim_vector (nnz, 1)); |
|
1663 |
|
1664 for (int i = 0; i < nnz; i++) |
|
1665 buf (i) = m.data (i); |
|
1666 |
|
1667 write_mat5_array (os, buf, save_as_floats); |
|
1668 } |
|
1669 } |
5089
|
1670 else if (cname == "int8") |
|
1671 { |
|
1672 int8NDArray m = tc.int8_array_value (); |
|
1673 |
5164
|
1674 write_mat5_integer_data (os, m.fortran_vec (), -1, m.nelem ()); |
5089
|
1675 } |
|
1676 else if (cname == "int16") |
|
1677 { |
|
1678 int16NDArray m = tc.int16_array_value (); |
|
1679 |
5164
|
1680 write_mat5_integer_data (os, m.fortran_vec (), -2, m.nelem ()); |
5089
|
1681 } |
|
1682 else if (cname == "int32") |
|
1683 { |
|
1684 int32NDArray m = tc.int32_array_value (); |
|
1685 |
5164
|
1686 write_mat5_integer_data (os, m.fortran_vec (), -4, m.nelem ()); |
5089
|
1687 } |
|
1688 else if (cname == "int64") |
|
1689 { |
|
1690 int64NDArray m = tc.int64_array_value (); |
|
1691 |
5164
|
1692 write_mat5_integer_data (os, m.fortran_vec (), -8, m.nelem ()); |
5089
|
1693 } |
|
1694 else if (cname == "uint8") |
|
1695 { |
|
1696 uint8NDArray m = tc.uint8_array_value (); |
|
1697 |
5164
|
1698 write_mat5_integer_data (os, m.fortran_vec (), 1, m.nelem ()); |
5089
|
1699 } |
|
1700 else if (cname == "uint16") |
|
1701 { |
|
1702 uint16NDArray m = tc.uint16_array_value (); |
|
1703 |
5164
|
1704 write_mat5_integer_data (os, m.fortran_vec (), 2, m.nelem ()); |
5089
|
1705 } |
|
1706 else if (cname == "uint32") |
|
1707 { |
|
1708 uint32NDArray m = tc.uint32_array_value (); |
|
1709 |
5164
|
1710 write_mat5_integer_data (os, m.fortran_vec (), 4, m.nelem ()); |
5089
|
1711 } |
|
1712 else if (cname == "uint64") |
|
1713 { |
|
1714 uint64NDArray m = tc.uint64_array_value (); |
|
1715 |
5164
|
1716 write_mat5_integer_data (os, m.fortran_vec (), 8, m.nelem ()); |
5089
|
1717 } |
5269
|
1718 else if (tc.is_bool_type ()) |
|
1719 { |
|
1720 uint8NDArray m (tc.bool_array_value ()); |
|
1721 |
|
1722 write_mat5_integer_data (os, m.fortran_vec (), 1, m.nelem ()); |
|
1723 } |
4634
|
1724 else if (tc.is_real_scalar () || tc.is_real_matrix () || tc.is_range ()) |
|
1725 { |
|
1726 NDArray m = tc.array_value (); |
|
1727 |
|
1728 write_mat5_array (os, m, save_as_floats); |
|
1729 } |
|
1730 else if (tc.is_cell ()) |
|
1731 { |
|
1732 Cell cell = tc.cell_value (); |
|
1733 |
|
1734 if (! write_mat5_cell_array (os, cell, mark_as_global, save_as_floats)) |
|
1735 goto error_cleanup; |
|
1736 } |
|
1737 else if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
1738 { |
5269
|
1739 ComplexNDArray m_cmplx = tc.complex_array_value (); |
4634
|
1740 |
|
1741 write_mat5_array (os, ::real (m_cmplx), save_as_floats); |
|
1742 write_mat5_array (os, ::imag (m_cmplx), save_as_floats); |
|
1743 } |
|
1744 else if (tc.is_map ()) |
|
1745 { |
|
1746 // an Octave structure */ |
|
1747 // recursively write each element of the structure |
4675
|
1748 const Octave_map m = tc.map_value (); |
4634
|
1749 |
|
1750 { |
5269
|
1751 char buf[64]; |
|
1752 FOUR_BYTE_INT maxfieldnamelength = max_namelen + 1; |
4634
|
1753 int fieldcnt = 0; |
|
1754 |
4675
|
1755 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
4634
|
1756 fieldcnt++; |
|
1757 |
|
1758 write_mat5_tag (os, miINT32, 4); |
|
1759 os.write ((char *)&maxfieldnamelength, 4); |
5269
|
1760 write_mat5_tag (os, miINT8, fieldcnt*maxfieldnamelength); |
4634
|
1761 |
4675
|
1762 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
4634
|
1763 { |
|
1764 // write the name of each element |
|
1765 std::string tstr = m.key (i); |
5269
|
1766 memset (buf, 0, max_namelen + 1); |
|
1767 strncpy (buf, tstr.c_str (), max_namelen); // only 31 or 63 char names permitted |
|
1768 os.write (buf, max_namelen + 1); |
4634
|
1769 } |
|
1770 |
|
1771 int len = m.numel (); |
|
1772 |
5058
|
1773 for (int j = 0; j < len; j++) |
4634
|
1774 { |
|
1775 // write the data of each element |
|
1776 |
5058
|
1777 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
4634
|
1778 { |
5058
|
1779 Cell elts = m.contents (i); |
|
1780 |
4634
|
1781 bool retval2 = save_mat5_binary_element (os, elts(j), "", |
|
1782 mark_as_global, |
5269
|
1783 false, |
4634
|
1784 save_as_floats); |
|
1785 if (! retval2) |
|
1786 goto error_cleanup; |
|
1787 } |
|
1788 } |
|
1789 } |
|
1790 } |
|
1791 else |
|
1792 gripe_wrong_type_arg ("save", tc, false); |
|
1793 |
|
1794 contin = os.tellp (); |
|
1795 |
|
1796 return true; |
|
1797 |
|
1798 error_cleanup: |
|
1799 error ("save: error while writing `%s' to MAT file", name.c_str ()); |
|
1800 |
|
1801 return false; |
|
1802 } |
|
1803 |
|
1804 /* |
|
1805 ;;; Local Variables: *** |
|
1806 ;;; mode: C++ *** |
|
1807 ;;; End: *** |
|
1808 */ |
|
1809 |