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