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