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: |
5297
|
576 #if SIZEOF_INT != SIZEOF_OCTAVE_IDX_TYPE |
|
577 warning ("load: sparse objects are not implemented"); |
|
578 goto skip_ahead; |
|
579 #else |
5164
|
580 { |
|
581 int nr = dims(0); |
|
582 int nc = dims(1); |
|
583 SparseMatrix sm; |
|
584 SparseComplexMatrix scm; |
|
585 NDArray re; |
|
586 int *ridx; |
|
587 int *cidx; |
|
588 double *data; |
|
589 |
|
590 // Setup return value |
|
591 if (imag) |
|
592 { |
5275
|
593 scm = SparseComplexMatrix (static_cast<octave_idx_type> (nr), |
|
594 static_cast<octave_idx_type> (nc), |
|
595 static_cast<octave_idx_type> (nnz)); |
5164
|
596 ridx = scm.ridx (); |
|
597 cidx = scm.cidx (); |
|
598 re = NDArray (dim_vector (static_cast<int> (nnz))); |
|
599 data = re.fortran_vec (); |
|
600 } |
|
601 else |
|
602 { |
5275
|
603 sm = SparseMatrix (static_cast<octave_idx_type> (nr), |
|
604 static_cast<octave_idx_type> (nc), |
|
605 static_cast<octave_idx_type> (nnz)); |
5164
|
606 ridx = sm.ridx (); |
|
607 cidx = sm.cidx (); |
|
608 data = sm.data (); |
|
609 } |
|
610 |
|
611 // row indices |
|
612 std::streampos tmp_pos; |
|
613 |
|
614 if (read_mat5_tag (is, swap, type, len)) |
|
615 { |
|
616 error ("load: reading sparse row data for `%s'", retval.c_str ()); |
|
617 goto data_read_error; |
|
618 } |
|
619 |
|
620 tmp_pos = is.tellg (); |
|
621 |
|
622 read_mat5_integer_data (is, ridx, nnz, swap, |
|
623 (enum mat5_data_type) type); |
|
624 |
|
625 if (! is || error_state) |
|
626 { |
|
627 error ("load: reading sparse row data for `%s'", retval.c_str ()); |
|
628 goto data_read_error; |
|
629 } |
|
630 |
|
631 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
|
632 |
|
633 // col indices |
|
634 if (read_mat5_tag (is, swap, type, len)) |
|
635 { |
|
636 error ("load: reading sparse column data for `%s'", retval.c_str ()); |
|
637 goto data_read_error; |
|
638 } |
|
639 |
|
640 tmp_pos = is.tellg (); |
|
641 |
|
642 read_mat5_integer_data (is, cidx, nc + 1, swap, |
|
643 (enum mat5_data_type) type); |
|
644 |
|
645 if (! is || error_state) |
|
646 { |
|
647 error ("load: reading sparse column data for `%s'", retval.c_str ()); |
|
648 goto data_read_error; |
|
649 } |
|
650 |
|
651 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
|
652 |
|
653 // real data subelement |
|
654 if (read_mat5_tag (is, swap, type, len)) |
|
655 { |
|
656 error ("load: reading sparse matrix data for `%s'", retval.c_str ()); |
|
657 goto data_read_error; |
|
658 } |
|
659 |
|
660 tmp_pos = is.tellg (); |
|
661 read_mat5_binary_data (is, data, nnz, swap, |
|
662 (enum mat5_data_type) type, flt_fmt); |
|
663 |
|
664 if (! is || error_state) |
|
665 { |
|
666 error ("load: reading sparse matrix data for `%s'", retval.c_str ()); |
|
667 goto data_read_error; |
|
668 } |
|
669 |
|
670 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
|
671 |
|
672 // imaginary data subelement |
|
673 if (imag) |
|
674 { |
|
675 NDArray im (dim_vector (static_cast<int> (nnz))); |
|
676 |
|
677 if (read_mat5_tag (is, swap, type, len)) |
|
678 { |
|
679 error ("load: reading sparse matrix data for `%s'", retval.c_str ()); |
|
680 goto data_read_error; |
|
681 } |
|
682 |
|
683 read_mat5_binary_data (is, im.fortran_vec (), nnz, swap, |
|
684 (enum mat5_data_type) type, flt_fmt); |
|
685 |
|
686 if (! is || error_state) |
|
687 { |
|
688 error ("load: reading imaginary sparse matrix data for `%s'", |
|
689 retval.c_str ()); |
|
690 goto data_read_error; |
|
691 } |
|
692 |
|
693 for (int i = 0; i < nnz; i++) |
|
694 scm.xdata (i) = Complex (re (i), im (i)); |
|
695 |
|
696 tc = scm; |
|
697 } |
|
698 else |
|
699 tc = sm; |
|
700 } |
|
701 break; |
5297
|
702 #endif |
4634
|
703 |
5089
|
704 case mxFUNCTION_CLASS: |
|
705 warning ("load: function handles are not implemented"); |
|
706 goto skip_ahead; |
|
707 |
4634
|
708 case mxSTRUCT_CLASS: |
|
709 { |
|
710 Octave_map m; |
|
711 FOUR_BYTE_INT fn_type; |
|
712 FOUR_BYTE_INT fn_len; |
|
713 FOUR_BYTE_INT field_name_length; |
|
714 int i; |
|
715 |
|
716 // field name length subelement -- actually the maximum length |
|
717 // of a field name. The Matlab docs promise this will always |
|
718 // be 32. We read and use the actual value, on the theory |
|
719 // that eventually someone will recognize that's a waste of |
|
720 // space. |
|
721 if (read_mat5_tag (is, swap, fn_type, fn_len) || fn_type != miINT32) |
|
722 { |
|
723 error ("load: invalid field name subelement"); |
|
724 goto data_read_error; |
|
725 } |
|
726 |
|
727 if (! is.read (X_CAST (char *, &field_name_length), fn_len )) |
|
728 goto data_read_error; |
|
729 |
|
730 if (swap) |
4944
|
731 swap_bytes<4> (&field_name_length); |
4634
|
732 |
|
733 // field name subelement. The length of this subelement tells |
|
734 // us how many fields there are. |
|
735 if (read_mat5_tag (is, swap, fn_type, fn_len) || fn_type != miINT8) |
|
736 { |
|
737 error ("load: invalid field name subelement"); |
|
738 goto data_read_error; |
|
739 } |
|
740 |
|
741 int n_fields = fn_len/field_name_length; |
|
742 |
|
743 fn_len = PAD (fn_len); |
|
744 |
|
745 OCTAVE_LOCAL_BUFFER (char, elname, fn_len); |
|
746 |
|
747 if (! is.read (elname, fn_len)) |
|
748 goto data_read_error; |
|
749 |
|
750 int n; |
|
751 if (dims(0) == 1) |
|
752 n = dims(1); |
|
753 else if (dims(1) == 1) |
|
754 n = dims(0); |
|
755 else |
|
756 { |
|
757 error ("load: can only handle one-dimensional structure arrays"); |
|
758 goto data_read_error; |
|
759 } |
|
760 |
|
761 Cell field_elts (n_fields, n); |
|
762 |
|
763 // fields subelements |
|
764 for (int j = 0; j < n; j++) |
|
765 { |
|
766 for (i = 0; i < n_fields; i++) |
|
767 { |
|
768 octave_value fieldtc; |
|
769 read_mat5_binary_element (is, filename, swap, global, fieldtc); |
|
770 field_elts(i,j) = fieldtc; |
|
771 } |
|
772 } |
|
773 |
|
774 for (int j = n_fields-1; j >= 0; j--) |
|
775 { |
|
776 const char *key = elname + j*field_name_length; |
|
777 |
4675
|
778 Cell c (dim_vector (n, 1)); |
|
779 |
4634
|
780 for (int k = n-1; k >=0; k--) |
4675
|
781 c(k) = field_elts(j,k); |
|
782 |
|
783 m.assign (key, c); |
4634
|
784 } |
|
785 |
|
786 tc = m; |
|
787 } |
|
788 break; |
|
789 |
5089
|
790 case mxINT8_CLASS: |
|
791 OCTAVE_MAT5_INTEGER_READ (int8NDArray); |
|
792 break; |
|
793 |
|
794 case mxUINT8_CLASS: |
5269
|
795 { |
|
796 OCTAVE_MAT5_INTEGER_READ (uint8NDArray); |
|
797 |
|
798 // logical variables can either be mxUINT8_CLASS or mxDOUBLE_CLASS, |
|
799 // so chek if we have a logical variable and convert it |
|
800 |
|
801 if (logicalvar) |
|
802 { |
|
803 uint8NDArray in = tc.uint8_array_value (); |
|
804 int nel = in.nelem (); |
|
805 boolNDArray out (dims); |
|
806 |
|
807 for (int i = 0; i < nel; i++) |
|
808 out (i) = static_cast<bool> (double (in (i))); |
|
809 |
|
810 tc = out; |
|
811 } |
|
812 } |
5089
|
813 break; |
|
814 |
|
815 case mxINT16_CLASS: |
|
816 OCTAVE_MAT5_INTEGER_READ (int16NDArray); |
|
817 break; |
|
818 |
|
819 case mxUINT16_CLASS: |
|
820 OCTAVE_MAT5_INTEGER_READ (uint16NDArray); |
|
821 break; |
|
822 |
|
823 case mxINT32_CLASS: |
|
824 OCTAVE_MAT5_INTEGER_READ (int32NDArray); |
|
825 break; |
|
826 |
|
827 case mxUINT32_CLASS: |
|
828 OCTAVE_MAT5_INTEGER_READ (uint32NDArray); |
|
829 break; |
|
830 |
|
831 case mxINT64_CLASS: |
|
832 OCTAVE_MAT5_INTEGER_READ (int64NDArray); |
|
833 break; |
|
834 |
|
835 case mxUINT64_CLASS: |
|
836 OCTAVE_MAT5_INTEGER_READ (uint64NDArray); |
|
837 break; |
|
838 |
4634
|
839 case mxCHAR_CLASS: |
|
840 // handle as a numerical array to start with |
|
841 |
|
842 case mxDOUBLE_CLASS: |
|
843 case mxSINGLE_CLASS: |
|
844 default: |
5089
|
845 { |
|
846 NDArray re (dims); |
4634
|
847 |
5089
|
848 // real data subelement |
|
849 |
4634
|
850 std::streampos tmp_pos; |
5089
|
851 |
4634
|
852 if (read_mat5_tag (is, swap, type, len)) |
|
853 { |
|
854 error ("load: reading matrix data for `%s'", retval.c_str ()); |
|
855 goto data_read_error; |
|
856 } |
|
857 |
|
858 int n = re.length (); |
|
859 tmp_pos = is.tellg (); |
|
860 read_mat5_binary_data (is, re.fortran_vec (), n, swap, |
|
861 (enum mat5_data_type) type, flt_fmt); |
|
862 |
|
863 if (! is || error_state) |
|
864 { |
|
865 error ("load: reading matrix data for `%s'", retval.c_str ()); |
|
866 goto data_read_error; |
|
867 } |
|
868 |
|
869 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
5089
|
870 |
5269
|
871 if (logicalvar) |
5089
|
872 { |
5269
|
873 // logical variables can either be mxUINT8_CLASS or mxDOUBLE_CLASS, |
|
874 // so chek if we have a logical variable and convert it |
|
875 |
|
876 boolNDArray out (dims); |
|
877 |
|
878 for (int i = 0; i < n; i++) |
|
879 out (i) = static_cast<bool> (re (i)); |
|
880 |
|
881 tc = out; |
|
882 } |
|
883 else if (imag) |
|
884 { |
|
885 // imaginary data subelement |
|
886 |
5089
|
887 NDArray im (dims); |
4634
|
888 |
5089
|
889 if (read_mat5_tag (is, swap, type, len)) |
|
890 { |
|
891 error ("load: reading matrix data for `%s'", retval.c_str ()); |
|
892 goto data_read_error; |
|
893 } |
4634
|
894 |
5089
|
895 n = im.length (); |
|
896 read_mat5_binary_data (is, im.fortran_vec (), n, swap, |
|
897 (enum mat5_data_type) type, flt_fmt); |
4634
|
898 |
5089
|
899 if (! is || error_state) |
|
900 { |
|
901 error ("load: reading imaginary matrix data for `%s'", |
|
902 retval.c_str ()); |
|
903 goto data_read_error; |
|
904 } |
4634
|
905 |
5089
|
906 ComplexNDArray ctmp (dims); |
4634
|
907 |
5089
|
908 for (int i = 0; i < n; i++) |
|
909 ctmp(i) = Complex (re(i), im(i)); |
4634
|
910 |
5089
|
911 tc = ctmp; |
|
912 } |
|
913 else |
5269
|
914 { |
|
915 tc = re; |
4634
|
916 |
5269
|
917 if (arrayclass == mxCHAR_CLASS) |
5279
|
918 tc = tc.convert_to_str (false, true, '\''); |
5269
|
919 } |
5089
|
920 } |
4634
|
921 } |
|
922 |
|
923 is.seekg (pos + static_cast<std::streamoff> (element_length)); |
|
924 |
|
925 if (is.eof ()) |
|
926 is.clear (); |
|
927 |
|
928 return retval; |
|
929 |
|
930 data_read_error: |
|
931 early_read_error: |
|
932 error ("load: trouble reading binary file `%s'", filename.c_str ()); |
|
933 return std::string (); |
|
934 |
|
935 skip_ahead: |
|
936 warning ("skipping over `%s'", retval.c_str ()); |
|
937 is.seekg (pos + static_cast<std::streamoff> (element_length)); |
|
938 return read_mat5_binary_element (is, filename, swap, global, tc); |
|
939 } |
|
940 |
|
941 int |
|
942 read_mat5_binary_file_header (std::istream& is, bool& swap, bool quiet) |
|
943 { |
|
944 TWO_BYTE_INT version=0, magic=0; |
|
945 |
|
946 is.seekg (124, std::ios::beg); |
|
947 is.read (X_CAST (char *, &version), 2); |
|
948 is.read (X_CAST (char *, &magic), 2); |
|
949 |
|
950 if (magic == 0x4d49) |
|
951 swap = 0; |
|
952 else if (magic == 0x494d) |
|
953 swap = 1; |
|
954 else |
|
955 { |
|
956 if (! quiet) |
|
957 error ("load: can't read binary file"); |
|
958 return -1; |
|
959 } |
|
960 |
|
961 if (! swap) // version number is inverse swapped! |
|
962 version = ((version >> 8) & 0xff) + ((version & 0xff) << 8); |
|
963 |
|
964 if (version != 1 && !quiet) |
|
965 warning ("load: found version %d binary MAT file, " |
|
966 "but only prepared for version 1", version); |
|
967 |
|
968 return 0; |
|
969 } |
|
970 |
|
971 static int |
|
972 write_mat5_tag (std::ostream& is, int type, int bytes) |
|
973 { |
|
974 FOUR_BYTE_INT temp; |
|
975 |
|
976 if (bytes <= 4) |
|
977 temp = (bytes << 16) + type; |
|
978 else |
|
979 { |
|
980 temp = type; |
|
981 if (! is.write ((char *)&temp, 4)) |
|
982 goto data_write_error; |
|
983 temp = bytes; |
|
984 } |
|
985 |
|
986 if (! is.write ((char *)&temp, 4)) |
|
987 goto data_write_error; |
|
988 |
|
989 return 0; |
|
990 |
|
991 data_write_error: |
|
992 return 1; |
|
993 } |
|
994 |
|
995 // write out the numeric values in M to OS, |
|
996 // preceded by the appropriate tag. |
|
997 static void |
|
998 write_mat5_array (std::ostream& os, const NDArray& m, bool save_as_floats) |
|
999 { |
|
1000 int nel = m.nelem (); |
|
1001 double max_val, min_val; |
|
1002 save_type st = LS_DOUBLE; |
|
1003 mat5_data_type mst; |
|
1004 int size; |
|
1005 unsigned len; |
|
1006 const double *data = m.data (); |
|
1007 |
|
1008 // Have to use copy here to avoid writing over data accessed via |
|
1009 // Matrix::data(). |
|
1010 |
|
1011 #define MAT5_DO_WRITE(TYPE, data, count, stream) \ |
|
1012 do \ |
|
1013 { \ |
|
1014 OCTAVE_LOCAL_BUFFER (TYPE, ptr, count); \ |
|
1015 for (int i = 0; i < count; i++) \ |
|
1016 ptr[i] = X_CAST (TYPE, data[i]); \ |
|
1017 stream.write (X_CAST (char *, ptr), count * sizeof (TYPE)); \ |
|
1018 } \ |
|
1019 while (0) |
|
1020 |
|
1021 if (save_as_floats) |
|
1022 { |
|
1023 if (m.too_large_for_float ()) |
|
1024 { |
|
1025 warning ("save: some values too large to save as floats --"); |
|
1026 warning ("save: saving as doubles instead"); |
|
1027 } |
|
1028 else |
|
1029 st = LS_FLOAT; |
|
1030 } |
|
1031 |
|
1032 if (m.all_integers (max_val, min_val)) |
|
1033 st = get_save_type (max_val, min_val); |
|
1034 |
|
1035 switch (st) |
|
1036 { |
|
1037 default: |
|
1038 case LS_DOUBLE: mst = miDOUBLE; size = 8; break; |
|
1039 case LS_FLOAT: mst = miSINGLE; size = 4; break; |
|
1040 case LS_U_CHAR: mst = miUINT8; size = 1; break; |
|
1041 case LS_U_SHORT: mst = miUINT16; size = 2; break; |
|
1042 case LS_U_INT: mst = miUINT32; size = 4; break; |
|
1043 case LS_CHAR: mst = miINT8; size = 1; break; |
|
1044 case LS_SHORT: mst = miINT16; size = 2; break; |
|
1045 case LS_INT: mst = miINT32; size = 4; break; |
|
1046 } |
|
1047 |
|
1048 len = nel*size; |
|
1049 write_mat5_tag (os, mst, len); |
|
1050 |
|
1051 { |
|
1052 switch (st) |
|
1053 { |
|
1054 case LS_U_CHAR: |
|
1055 MAT5_DO_WRITE (unsigned char, data, nel, os); |
|
1056 break; |
|
1057 |
|
1058 case LS_U_SHORT: |
|
1059 MAT5_DO_WRITE (unsigned TWO_BYTE_INT, data, nel, os); |
|
1060 break; |
|
1061 |
|
1062 case LS_U_INT: |
|
1063 MAT5_DO_WRITE (unsigned FOUR_BYTE_INT, data, nel, os); |
|
1064 break; |
|
1065 |
|
1066 // provide for 64 bit ints, even though get_save_type does |
|
1067 // not yet implement them |
|
1068 #ifdef EIGHT_BYTE_INT |
|
1069 case LS_U_LONG: |
|
1070 MAT5_DO_WRITE (unsigned EIGHT_BYTE_INT, data, nel, os); |
|
1071 break; |
|
1072 #endif |
|
1073 |
|
1074 case LS_CHAR: |
|
1075 MAT5_DO_WRITE (signed char, data, nel, os); |
|
1076 break; |
|
1077 |
|
1078 case LS_SHORT: |
|
1079 MAT5_DO_WRITE (TWO_BYTE_INT, data, nel, os); |
|
1080 break; |
|
1081 |
|
1082 case LS_INT: |
|
1083 MAT5_DO_WRITE (FOUR_BYTE_INT, data, nel, os); |
|
1084 break; |
|
1085 |
|
1086 #ifdef EIGHT_BYTE_INT |
|
1087 case LS_LONG: |
|
1088 MAT5_DO_WRITE (EIGHT_BYTE_INT, data, nel, os); |
|
1089 break; |
|
1090 #endif |
|
1091 |
|
1092 case LS_FLOAT: |
|
1093 MAT5_DO_WRITE (float, data, nel, os); |
|
1094 break; |
|
1095 |
|
1096 case LS_DOUBLE: // No conversion necessary. |
|
1097 os.write (X_CAST (char *, data), len); |
|
1098 break; |
|
1099 |
|
1100 default: |
|
1101 (*current_liboctave_error_handler) |
|
1102 ("unrecognized data format requested"); |
|
1103 break; |
|
1104 } |
|
1105 } |
|
1106 if (PAD (len) > len) |
|
1107 { |
|
1108 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
1109 os.write (buf, PAD (len) - len); |
|
1110 } |
|
1111 } |
|
1112 |
5089
|
1113 template <class T> |
|
1114 void |
5164
|
1115 write_mat5_integer_data (std::ostream& os, const T *m, int size, int nel) |
5089
|
1116 { |
|
1117 mat5_data_type mst; |
|
1118 unsigned len; |
|
1119 |
|
1120 switch (size) |
|
1121 { |
|
1122 case 1: |
|
1123 mst = miUINT8; |
|
1124 break; |
|
1125 case 2: |
|
1126 mst = miUINT16; |
|
1127 break; |
5164
|
1128 case 4: |
5089
|
1129 mst = miUINT32; |
|
1130 break; |
5164
|
1131 case 8: |
5089
|
1132 mst = miUINT64; |
|
1133 break; |
|
1134 case -1: |
|
1135 mst = miINT8; |
|
1136 size = - size; |
|
1137 break; |
|
1138 case -2: |
|
1139 mst = miINT16; |
|
1140 size = - size; |
|
1141 break; |
5164
|
1142 case -4: |
5089
|
1143 mst = miINT32; |
|
1144 size = - size; |
|
1145 break; |
5164
|
1146 case -8: |
5089
|
1147 default: |
|
1148 mst = miINT64; |
|
1149 size = - size; |
|
1150 break; |
|
1151 } |
|
1152 |
|
1153 len = nel*size; |
|
1154 write_mat5_tag (os, mst, len); |
|
1155 |
5164
|
1156 os.write (X_CAST(char *, m), len); |
5089
|
1157 |
|
1158 if (PAD (len) > len) |
|
1159 { |
|
1160 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
1161 os.write (buf, PAD (len) - len); |
|
1162 } |
|
1163 } |
|
1164 |
5164
|
1165 template void write_mat5_integer_data (std::ostream& os, const octave_int8 *m, |
|
1166 int size, int nel); |
|
1167 template void write_mat5_integer_data (std::ostream& os, const octave_int16 *m, |
|
1168 int size, int nel); |
|
1169 template void write_mat5_integer_data (std::ostream& os, const octave_int32 *m, |
|
1170 int size, int nel); |
|
1171 template void write_mat5_integer_data (std::ostream& os, const octave_int64 *m, |
|
1172 int size, int nel); |
|
1173 template void write_mat5_integer_data (std::ostream& os, const octave_uint8 *m, |
|
1174 int size, int nel); |
|
1175 template void write_mat5_integer_data (std::ostream& os, const octave_uint16 *m, |
|
1176 int size, int nel); |
|
1177 template void write_mat5_integer_data (std::ostream& os, const octave_uint32 *m, |
|
1178 int size, int nel); |
|
1179 template void write_mat5_integer_data (std::ostream& os, const octave_uint64 *m, |
|
1180 int size, int nel); |
|
1181 template void write_mat5_integer_data (std::ostream& os, const int *m, |
|
1182 int size, int nel); |
5089
|
1183 |
4634
|
1184 // Write out cell element values in the cell array to OS, preceded by |
|
1185 // the appropriate tag. |
|
1186 |
|
1187 static bool |
4701
|
1188 write_mat5_cell_array (std::ostream& os, const Cell& cell, |
|
1189 bool mark_as_global, bool save_as_floats) |
4634
|
1190 { |
|
1191 int nel = cell.nelem (); |
|
1192 |
|
1193 for (int i = 0; i < nel; i++) |
|
1194 { |
|
1195 octave_value ov = cell(i); |
|
1196 |
|
1197 if (! save_mat5_binary_element (os, ov, "", mark_as_global, |
5269
|
1198 false, save_as_floats)) |
4634
|
1199 return false; |
|
1200 } |
|
1201 |
|
1202 return true; |
|
1203 } |
|
1204 |
5269
|
1205 int |
|
1206 save_mat5_array_length (const double* val, int nel, bool save_as_floats) |
|
1207 { |
|
1208 if (nel > 0) |
|
1209 { |
|
1210 int size = 8; |
|
1211 |
|
1212 if (save_as_floats) |
|
1213 { |
|
1214 bool too_large_for_float = false; |
|
1215 for (int i = 0; i < nel; i++) |
|
1216 { |
|
1217 double tmp = val [i]; |
|
1218 |
|
1219 if (tmp > FLT_MAX || tmp < FLT_MIN) |
|
1220 { |
|
1221 too_large_for_float = true; |
|
1222 break; |
|
1223 } |
|
1224 } |
|
1225 |
|
1226 if (!too_large_for_float) |
|
1227 size = 4; |
|
1228 } |
|
1229 |
|
1230 // The code below is disabled since get_save_type currently doesn't |
|
1231 // deal with integer types. This will need to be activated if get_save_type |
|
1232 // is changed. |
|
1233 |
|
1234 // double max_val = val[0]; |
|
1235 // double min_val = val[0]; |
|
1236 // bool all_integers = true; |
|
1237 // |
|
1238 // for (int i = 0; i < nel; i++) |
|
1239 // { |
|
1240 // double val = val[i]; |
|
1241 // |
|
1242 // if (val > max_val) |
|
1243 // max_val = val; |
|
1244 // |
|
1245 // if (val < min_val) |
|
1246 // min_val = val; |
|
1247 // |
|
1248 // if (D_NINT (val) != val) |
|
1249 // { |
|
1250 // all_integers = false; |
|
1251 // break; |
|
1252 // } |
|
1253 // } |
|
1254 // |
|
1255 // if (all_integers) |
|
1256 // { |
|
1257 // if (max_val < 256 && min_val > -1) |
|
1258 // size = 1; |
|
1259 // else if (max_val < 65536 && min_val > -1) |
|
1260 // size = 2; |
|
1261 // else if (max_val < 4294967295UL && min_val > -1) |
|
1262 // size = 4; |
|
1263 // else if (max_val < 128 && min_val >= -128) |
|
1264 // size = 1; |
|
1265 // else if (max_val < 32768 && min_val >= -32768) |
|
1266 // size = 2; |
|
1267 // else if (max_val <= 2147483647L && min_val >= -2147483647L) |
|
1268 // size = 4; |
|
1269 // } |
|
1270 |
|
1271 return 8 + nel * size; |
|
1272 } |
|
1273 else |
|
1274 return 8; |
|
1275 } |
|
1276 |
|
1277 int |
|
1278 save_mat5_array_length (const Complex* val, int nel, bool save_as_floats) |
|
1279 { |
|
1280 int ret; |
|
1281 |
|
1282 OCTAVE_LOCAL_BUFFER (double, tmp, nel); |
|
1283 |
|
1284 for (int i = 1; i < nel; i++) |
|
1285 tmp[i] = std::real (val[i]); |
|
1286 |
|
1287 ret = save_mat5_array_length (tmp, nel, save_as_floats); |
|
1288 |
|
1289 for (int i = 1; i < nel; i++) |
|
1290 tmp[i] = std::imag (val[i]); |
|
1291 |
|
1292 ret += save_mat5_array_length (tmp, nel, save_as_floats); |
|
1293 |
|
1294 return ret; |
|
1295 } |
|
1296 |
|
1297 int |
|
1298 save_mat5_element_length (const octave_value& tc, const std::string& name, |
|
1299 bool save_as_floats, bool mat7_format) |
|
1300 { |
|
1301 int max_namelen = (mat7_format ? 63 : 31); |
|
1302 int len = name.length (); |
|
1303 std::string cname = tc.class_name (); |
|
1304 int ret = 32; |
|
1305 |
|
1306 if (len > 4) |
|
1307 ret += PAD (len > max_namelen ? max_namelen : len); |
|
1308 |
|
1309 ret += PAD (4 * tc.ndims ()); |
|
1310 |
|
1311 if (tc.is_string ()) |
|
1312 { |
|
1313 charMatrix chm = tc.char_matrix_value (); |
|
1314 ret += 8 + PAD (2 * chm.rows () * chm.cols ()); |
|
1315 } |
|
1316 else if (cname == "sparse") |
|
1317 { |
|
1318 if (tc.is_complex_type ()) |
|
1319 { |
|
1320 SparseComplexMatrix m = tc.sparse_complex_matrix_value (); |
|
1321 int nc = m.cols (); |
|
1322 int nnz = m.nnz (); |
|
1323 |
|
1324 ret += 16 + PAD (nnz * sizeof (int)) + PAD ((nc + 1) * sizeof (int)) + |
|
1325 save_mat5_array_length (m.data (), m.nelem (), save_as_floats); |
|
1326 } |
|
1327 else |
|
1328 { |
|
1329 SparseMatrix m = tc.sparse_matrix_value (); |
|
1330 int nc = m.cols (); |
|
1331 int nnz = m.nnz (); |
|
1332 |
|
1333 ret += 16 + PAD (nnz * sizeof (int)) + PAD ((nc + 1) * sizeof (int)) + |
|
1334 save_mat5_array_length (m.data (), m.nelem (), save_as_floats); |
|
1335 } |
|
1336 } |
|
1337 |
|
1338 #define INT_LEN(nel, size) \ |
|
1339 { \ |
|
1340 ret += 8; \ |
|
1341 int sz = nel * size; \ |
|
1342 if (sz > 4) \ |
|
1343 ret += PAD (sz); \ |
|
1344 } |
|
1345 |
|
1346 else if (cname == "int8") |
|
1347 INT_LEN (tc.int8_array_value ().nelem (), 1) |
|
1348 else if (cname == "int16") |
|
1349 INT_LEN (tc.int16_array_value ().nelem (), 2) |
|
1350 else if (cname == "int32") |
|
1351 INT_LEN (tc.int32_array_value ().nelem (), 4) |
|
1352 else if (cname == "int64") |
|
1353 INT_LEN (tc.int64_array_value ().nelem (), 8) |
|
1354 else if (cname == "uint8") |
|
1355 INT_LEN (tc.uint8_array_value ().nelem (), 1) |
|
1356 else if (cname == "uint16") |
|
1357 INT_LEN (tc.uint16_array_value ().nelem (), 2) |
|
1358 else if (cname == "uint32") |
|
1359 INT_LEN (tc.uint32_array_value ().nelem (), 4) |
|
1360 else if (cname == "uint64") |
|
1361 INT_LEN (tc.uint64_array_value ().nelem (), 8) |
|
1362 else if (tc.is_bool_type ()) |
|
1363 INT_LEN (tc.bool_array_value ().nelem (), 1) |
|
1364 else if (tc.is_real_scalar () || tc.is_real_matrix () || tc.is_range ()) |
|
1365 { |
|
1366 NDArray m = tc.array_value (); |
|
1367 ret += save_mat5_array_length (m.fortran_vec (), m.nelem (), |
|
1368 save_as_floats); |
|
1369 } |
|
1370 else if (tc.is_cell ()) |
|
1371 { |
|
1372 Cell cell = tc.cell_value (); |
|
1373 int nel = cell.nelem (); |
|
1374 |
|
1375 for (int i = 0; i < nel; i++) |
|
1376 ret += 8 + |
|
1377 save_mat5_element_length (cell (i), "", save_as_floats, mat7_format); |
|
1378 } |
|
1379 else if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
1380 { |
|
1381 ComplexNDArray m = tc.complex_array_value (); |
|
1382 ret += save_mat5_array_length (m.fortran_vec (), m.nelem (), |
|
1383 save_as_floats); |
|
1384 } |
|
1385 else if (tc.is_map ()) |
|
1386 { |
|
1387 int fieldcnt = 0; |
|
1388 const Octave_map m = tc.map_value (); |
|
1389 int nel = m.numel (); |
|
1390 |
|
1391 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
|
1392 fieldcnt++; |
|
1393 |
|
1394 ret += 16 + fieldcnt * (max_namelen + 1); |
|
1395 |
|
1396 |
|
1397 for (int j = 0; j < nel; j++) |
|
1398 { |
|
1399 |
|
1400 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
|
1401 { |
|
1402 Cell elts = m.contents (i); |
|
1403 |
|
1404 ret += 8 + save_mat5_element_length (elts (j), "", |
|
1405 save_as_floats, mat7_format); |
|
1406 } |
|
1407 } |
|
1408 } |
|
1409 else |
|
1410 ret = -1; |
|
1411 |
|
1412 return ret; |
|
1413 } |
|
1414 |
4634
|
1415 // save the data from TC along with the corresponding NAME on stream |
|
1416 // OS in the MatLab version 5 binary format. Return true on success. |
|
1417 |
|
1418 bool |
|
1419 save_mat5_binary_element (std::ostream& os, |
|
1420 const octave_value& tc, const std::string& name, |
5269
|
1421 bool mark_as_global, bool mat7_format, |
|
1422 bool save_as_floats, bool compressing) |
4634
|
1423 { |
|
1424 FOUR_BYTE_INT flags=0; |
5164
|
1425 FOUR_BYTE_INT nnz=0; |
4634
|
1426 std::streampos fixup, contin; |
5089
|
1427 std::string cname = tc.class_name (); |
5269
|
1428 int max_namelen = (mat7_format ? 63 : 31); |
|
1429 |
|
1430 #ifdef HAVE_ZLIB |
|
1431 if (mat7_format && !compressing) |
|
1432 { |
|
1433 bool ret = false; |
|
1434 |
|
1435 OSSTREAM buf; |
|
1436 |
|
1437 // The code seeks backwards in the stream to fix the header. Can't |
|
1438 // do this with zlib, so use a stringstream. |
|
1439 ret = save_mat5_binary_element (buf, tc, name, mark_as_global, true, |
|
1440 save_as_floats, true); |
|
1441 |
|
1442 if (ret) |
|
1443 { |
|
1444 OSSTREAM_FREEZE (buf); |
|
1445 |
|
1446 // destLen must be 0.1% larger than source buffer + 12 bytes |
|
1447 uLongf srcLen = OSSTREAM_STR (buf).length (); |
|
1448 uLongf destLen = srcLen * 1001 / 1000 + 12; |
|
1449 OCTAVE_LOCAL_BUFFER (char, out_buf, destLen); |
|
1450 |
|
1451 if (compress (X_CAST (Bytef *, out_buf), &destLen, |
|
1452 X_CAST (Bytef *, OSSTREAM_C_STR (buf)), srcLen) == Z_OK) |
|
1453 { |
|
1454 write_mat5_tag (os, miCOMPRESSED, X_CAST(int, destLen)); |
|
1455 os.write (out_buf, destLen); |
|
1456 } |
|
1457 else |
|
1458 { |
|
1459 error ("save: error compressing data element"); |
|
1460 ret = false; |
|
1461 } |
|
1462 } |
|
1463 |
|
1464 return ret; |
|
1465 } |
|
1466 #endif |
4634
|
1467 |
|
1468 // element type and length |
|
1469 fixup = os.tellp (); |
5269
|
1470 write_mat5_tag (os, miMATRIX, save_mat5_element_length |
|
1471 (tc, name, save_as_floats, mat7_format)); |
4634
|
1472 |
|
1473 // array flags subelement |
|
1474 write_mat5_tag (os, miUINT32, 8); |
|
1475 |
5269
|
1476 if (tc.is_bool_type ()) |
|
1477 flags |= 0x0200; |
|
1478 |
4634
|
1479 if (mark_as_global) |
|
1480 flags |= 0x0400; |
|
1481 |
|
1482 if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
1483 flags |= 0x0800; |
|
1484 |
|
1485 if (tc.is_string ()) |
|
1486 flags |= mxCHAR_CLASS; |
5089
|
1487 else if (cname == "int8") |
|
1488 flags |= mxINT8_CLASS; |
|
1489 else if (cname == "int16") |
|
1490 flags |= mxINT16_CLASS; |
|
1491 else if (cname == "int32") |
|
1492 flags |= mxINT32_CLASS; |
|
1493 else if (cname == "int64") |
|
1494 flags |= mxINT64_CLASS; |
5269
|
1495 else if (cname == "uint8" || tc.is_bool_type ()) |
5089
|
1496 flags |= mxUINT8_CLASS; |
|
1497 else if (cname == "uint16") |
|
1498 flags |= mxUINT16_CLASS; |
|
1499 else if (cname == "uint32") |
|
1500 flags |= mxUINT32_CLASS; |
|
1501 else if (cname == "uint64") |
|
1502 flags |= mxUINT64_CLASS; |
5164
|
1503 else if (cname == "sparse") |
|
1504 { |
|
1505 flags |= mxSPARSE_CLASS; |
|
1506 if (tc.is_complex_type ()) |
|
1507 { |
|
1508 SparseComplexMatrix scm = tc.sparse_complex_matrix_value (); |
|
1509 nnz = scm.nnz (); |
|
1510 } |
|
1511 else |
|
1512 { |
|
1513 SparseMatrix sm = tc.sparse_matrix_value (); |
|
1514 nnz = sm.nnz (); |
|
1515 } |
|
1516 } |
4634
|
1517 else if (tc.is_real_scalar ()) |
|
1518 flags |= mxDOUBLE_CLASS; |
|
1519 else if (tc.is_real_matrix () || tc.is_range ()) |
|
1520 flags |= mxDOUBLE_CLASS; |
|
1521 else if (tc.is_complex_scalar ()) |
|
1522 flags |= mxDOUBLE_CLASS; |
|
1523 else if (tc.is_complex_matrix ()) |
|
1524 flags |= mxDOUBLE_CLASS; |
|
1525 else if (tc.is_map ()) |
|
1526 flags |= mxSTRUCT_CLASS; |
|
1527 else if (tc.is_cell ()) |
|
1528 flags |= mxCELL_CLASS; |
|
1529 else |
|
1530 { |
|
1531 gripe_wrong_type_arg ("save", tc, false); |
|
1532 goto error_cleanup; |
|
1533 } |
|
1534 |
|
1535 os.write ((char *)&flags, 4); |
5164
|
1536 os.write ((char *)&nnz, 4); |
4634
|
1537 |
|
1538 { |
|
1539 dim_vector dv = tc.dims (); |
|
1540 int nd = tc.ndims (); |
4638
|
1541 int dim_len = 4*nd; |
4634
|
1542 |
4638
|
1543 write_mat5_tag (os, miINT32, dim_len); |
4634
|
1544 |
|
1545 for (int i = 0; i < nd; i++) |
|
1546 { |
4638
|
1547 FOUR_BYTE_INT n = dv(i); |
4634
|
1548 os.write ((char *)&n, 4); |
|
1549 } |
4638
|
1550 |
|
1551 if (PAD (dim_len) > dim_len) |
|
1552 { |
|
1553 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
1554 os.write (buf, PAD (dim_len) - dim_len); |
|
1555 } |
4634
|
1556 } |
|
1557 |
|
1558 // array name subelement |
|
1559 { |
|
1560 int namelen = name.length (); |
|
1561 |
5269
|
1562 if (namelen > max_namelen) |
|
1563 namelen = max_namelen; // only 31 or 63 char names permitted in mat file |
4634
|
1564 |
|
1565 int paddedlength = PAD (namelen); |
|
1566 |
|
1567 write_mat5_tag (os, miINT8, namelen); |
|
1568 OCTAVE_LOCAL_BUFFER (char, paddedname, paddedlength); |
|
1569 memset (paddedname, 0, paddedlength); |
|
1570 strncpy (paddedname, name.c_str (), namelen); |
|
1571 os.write (paddedname, paddedlength); |
|
1572 } |
|
1573 |
|
1574 // data element |
|
1575 if (tc.is_string ()) |
|
1576 { |
|
1577 charMatrix chm = tc.char_matrix_value (); |
|
1578 int nr = chm.rows (); |
|
1579 int nc = chm.cols (); |
|
1580 int len = nr*nc*2; |
|
1581 int paddedlength = PAD (nr*nc*2); |
|
1582 |
|
1583 OCTAVE_LOCAL_BUFFER (TWO_BYTE_INT, buf, nc*nr+3); |
|
1584 write_mat5_tag (os, miUINT16, len); |
|
1585 |
|
1586 for (int i = 0; i < nr; i++) |
|
1587 { |
|
1588 std::string tstr = chm.row_as_string (i); |
|
1589 const char *s = tstr.data (); |
|
1590 |
|
1591 for (int j = 0; j < nc; j++) |
|
1592 buf[j*nr+i] = *s++ & 0x00FF; |
|
1593 } |
|
1594 os.write ((char *)buf, nr*nc*2); |
|
1595 |
|
1596 if (paddedlength > len) |
|
1597 os.write ((char *)buf, paddedlength - len); |
|
1598 } |
5164
|
1599 else if (cname == "sparse") |
|
1600 { |
|
1601 if (tc.is_complex_type ()) |
|
1602 { |
|
1603 SparseComplexMatrix m = tc.sparse_complex_matrix_value (); |
|
1604 int nc = m.cols (); |
|
1605 |
|
1606 write_mat5_integer_data (os, m.ridx (), - sizeof(int), nnz); |
|
1607 write_mat5_integer_data (os, m.cidx (), - sizeof(int), nc + 1); |
|
1608 |
|
1609 NDArray buf (dim_vector (nnz, 1)); |
|
1610 |
|
1611 for (int i = 0; i < nnz; i++) |
5261
|
1612 buf (i) = std::real (m.data (i)); |
5164
|
1613 |
|
1614 write_mat5_array (os, buf, save_as_floats); |
|
1615 |
|
1616 for (int i = 0; i < nnz; i++) |
5261
|
1617 buf (i) = std::imag (m.data (i)); |
5164
|
1618 |
|
1619 write_mat5_array (os, buf, save_as_floats); |
|
1620 } |
|
1621 else |
|
1622 { |
|
1623 SparseMatrix m = tc.sparse_matrix_value (); |
|
1624 int nc = m.cols (); |
|
1625 |
|
1626 write_mat5_integer_data (os, m.ridx (), - sizeof(int), nnz); |
|
1627 write_mat5_integer_data (os, m.cidx (), - sizeof(int), nc + 1); |
|
1628 |
|
1629 // XXX FIXME XXX |
|
1630 // Is there a way to easily do without this buffer |
|
1631 NDArray buf (dim_vector (nnz, 1)); |
|
1632 |
|
1633 for (int i = 0; i < nnz; i++) |
|
1634 buf (i) = m.data (i); |
|
1635 |
|
1636 write_mat5_array (os, buf, save_as_floats); |
|
1637 } |
|
1638 } |
5089
|
1639 else if (cname == "int8") |
|
1640 { |
|
1641 int8NDArray m = tc.int8_array_value (); |
|
1642 |
5164
|
1643 write_mat5_integer_data (os, m.fortran_vec (), -1, m.nelem ()); |
5089
|
1644 } |
|
1645 else if (cname == "int16") |
|
1646 { |
|
1647 int16NDArray m = tc.int16_array_value (); |
|
1648 |
5164
|
1649 write_mat5_integer_data (os, m.fortran_vec (), -2, m.nelem ()); |
5089
|
1650 } |
|
1651 else if (cname == "int32") |
|
1652 { |
|
1653 int32NDArray m = tc.int32_array_value (); |
|
1654 |
5164
|
1655 write_mat5_integer_data (os, m.fortran_vec (), -4, m.nelem ()); |
5089
|
1656 } |
|
1657 else if (cname == "int64") |
|
1658 { |
|
1659 int64NDArray m = tc.int64_array_value (); |
|
1660 |
5164
|
1661 write_mat5_integer_data (os, m.fortran_vec (), -8, m.nelem ()); |
5089
|
1662 } |
|
1663 else if (cname == "uint8") |
|
1664 { |
|
1665 uint8NDArray m = tc.uint8_array_value (); |
|
1666 |
5164
|
1667 write_mat5_integer_data (os, m.fortran_vec (), 1, m.nelem ()); |
5089
|
1668 } |
|
1669 else if (cname == "uint16") |
|
1670 { |
|
1671 uint16NDArray m = tc.uint16_array_value (); |
|
1672 |
5164
|
1673 write_mat5_integer_data (os, m.fortran_vec (), 2, m.nelem ()); |
5089
|
1674 } |
|
1675 else if (cname == "uint32") |
|
1676 { |
|
1677 uint32NDArray m = tc.uint32_array_value (); |
|
1678 |
5164
|
1679 write_mat5_integer_data (os, m.fortran_vec (), 4, m.nelem ()); |
5089
|
1680 } |
|
1681 else if (cname == "uint64") |
|
1682 { |
|
1683 uint64NDArray m = tc.uint64_array_value (); |
|
1684 |
5164
|
1685 write_mat5_integer_data (os, m.fortran_vec (), 8, m.nelem ()); |
5089
|
1686 } |
5269
|
1687 else if (tc.is_bool_type ()) |
|
1688 { |
|
1689 uint8NDArray m (tc.bool_array_value ()); |
|
1690 |
|
1691 write_mat5_integer_data (os, m.fortran_vec (), 1, m.nelem ()); |
|
1692 } |
4634
|
1693 else if (tc.is_real_scalar () || tc.is_real_matrix () || tc.is_range ()) |
|
1694 { |
|
1695 NDArray m = tc.array_value (); |
|
1696 |
|
1697 write_mat5_array (os, m, save_as_floats); |
|
1698 } |
|
1699 else if (tc.is_cell ()) |
|
1700 { |
|
1701 Cell cell = tc.cell_value (); |
|
1702 |
|
1703 if (! write_mat5_cell_array (os, cell, mark_as_global, save_as_floats)) |
|
1704 goto error_cleanup; |
|
1705 } |
|
1706 else if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
1707 { |
5269
|
1708 ComplexNDArray m_cmplx = tc.complex_array_value (); |
4634
|
1709 |
|
1710 write_mat5_array (os, ::real (m_cmplx), save_as_floats); |
|
1711 write_mat5_array (os, ::imag (m_cmplx), save_as_floats); |
|
1712 } |
|
1713 else if (tc.is_map ()) |
|
1714 { |
|
1715 // an Octave structure */ |
|
1716 // recursively write each element of the structure |
4675
|
1717 const Octave_map m = tc.map_value (); |
4634
|
1718 |
|
1719 { |
5269
|
1720 char buf[64]; |
|
1721 FOUR_BYTE_INT maxfieldnamelength = max_namelen + 1; |
4634
|
1722 int fieldcnt = 0; |
|
1723 |
4675
|
1724 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
4634
|
1725 fieldcnt++; |
|
1726 |
|
1727 write_mat5_tag (os, miINT32, 4); |
|
1728 os.write ((char *)&maxfieldnamelength, 4); |
5269
|
1729 write_mat5_tag (os, miINT8, fieldcnt*maxfieldnamelength); |
4634
|
1730 |
4675
|
1731 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
4634
|
1732 { |
|
1733 // write the name of each element |
|
1734 std::string tstr = m.key (i); |
5269
|
1735 memset (buf, 0, max_namelen + 1); |
|
1736 strncpy (buf, tstr.c_str (), max_namelen); // only 31 or 63 char names permitted |
|
1737 os.write (buf, max_namelen + 1); |
4634
|
1738 } |
|
1739 |
|
1740 int len = m.numel (); |
|
1741 |
5058
|
1742 for (int j = 0; j < len; j++) |
4634
|
1743 { |
|
1744 // write the data of each element |
|
1745 |
5058
|
1746 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
4634
|
1747 { |
5058
|
1748 Cell elts = m.contents (i); |
|
1749 |
4634
|
1750 bool retval2 = save_mat5_binary_element (os, elts(j), "", |
|
1751 mark_as_global, |
5269
|
1752 false, |
4634
|
1753 save_as_floats); |
|
1754 if (! retval2) |
|
1755 goto error_cleanup; |
|
1756 } |
|
1757 } |
|
1758 } |
|
1759 } |
|
1760 else |
|
1761 gripe_wrong_type_arg ("save", tc, false); |
|
1762 |
|
1763 contin = os.tellp (); |
|
1764 |
|
1765 return true; |
|
1766 |
|
1767 error_cleanup: |
|
1768 error ("save: error while writing `%s' to MAT file", name.c_str ()); |
|
1769 |
|
1770 return false; |
|
1771 } |
|
1772 |
|
1773 /* |
|
1774 ;;; Local Variables: *** |
|
1775 ;;; mode: C++ *** |
|
1776 ;;; End: *** |
|
1777 */ |
|
1778 |