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