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