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" |
6625
|
51 #include "file-stat.h" |
4634
|
52 |
|
53 #include "Cell.h" |
|
54 #include "defun.h" |
|
55 #include "error.h" |
|
56 #include "gripes.h" |
|
57 #include "load-save.h" |
6625
|
58 #include "load-path.h" |
4634
|
59 #include "oct-obj.h" |
|
60 #include "oct-map.h" |
|
61 #include "ov-cell.h" |
6625
|
62 #include "ov-fcn-inline.h" |
4634
|
63 #include "pager.h" |
|
64 #include "pt-exp.h" |
|
65 #include "symtab.h" |
|
66 #include "sysdep.h" |
|
67 #include "unwind-prot.h" |
|
68 #include "utils.h" |
|
69 #include "variables.h" |
|
70 #include "version.h" |
|
71 #include "dMatrix.h" |
|
72 |
|
73 #include "ls-utils.h" |
|
74 #include "ls-mat5.h" |
|
75 |
6625
|
76 #include "parse.h" |
|
77 #include "defaults.h" |
|
78 |
5269
|
79 #ifdef HAVE_ZLIB |
|
80 #include <zlib.h> |
|
81 #endif |
|
82 |
6295
|
83 #define PAD(l) (((l) > 0 && (l) <= 4) ? 4 : (((l)+7)/8)*8) |
4634
|
84 |
6625
|
85 |
|
86 // The subsystem data block |
|
87 static octave_value subsys_ov; |
|
88 |
5900
|
89 // FIXME -- the following enum values should be the same as the |
|
90 // mxClassID values in mexproto.h, but it seems they have also changed |
|
91 // over time. What is the correct way to handle this and maintain |
|
92 // backward compatibility with old MAT files? For now, use |
|
93 // "MAT_FILE_" instead of "mx" as the prefix for these names to avoid |
|
94 // conflict with the mxClassID enum in mexproto.h. |
|
95 |
4634
|
96 enum arrayclasstype |
|
97 { |
5900
|
98 MAT_FILE_CELL_CLASS=1, // cell array |
|
99 MAT_FILE_STRUCT_CLASS, // structure |
|
100 MAT_FILE_OBJECT_CLASS, // object |
|
101 MAT_FILE_CHAR_CLASS, // character array |
|
102 MAT_FILE_SPARSE_CLASS, // sparse array |
|
103 MAT_FILE_DOUBLE_CLASS, // double precision array |
|
104 MAT_FILE_SINGLE_CLASS, // single precision floating point |
|
105 MAT_FILE_INT8_CLASS, // 8 bit signed integer |
|
106 MAT_FILE_UINT8_CLASS, // 8 bit unsigned integer |
|
107 MAT_FILE_INT16_CLASS, // 16 bit signed integer |
|
108 MAT_FILE_UINT16_CLASS, // 16 bit unsigned integer |
|
109 MAT_FILE_INT32_CLASS, // 32 bit signed integer |
|
110 MAT_FILE_UINT32_CLASS, // 32 bit unsigned integer |
|
111 MAT_FILE_INT64_CLASS, // 64 bit signed integer |
|
112 MAT_FILE_UINT64_CLASS, // 64 bit unsigned integer |
6625
|
113 MAT_FILE_FUNCTION_CLASS, // Function handle |
|
114 MAT_FILE_WORKSPACE_CLASS // Workspace (undocumented) |
4634
|
115 }; |
|
116 |
|
117 // Read COUNT elements of data from IS in the format specified by TYPE, |
|
118 // placing the result in DATA. If SWAP is TRUE, swap the bytes of |
|
119 // each element before copying to DATA. FLT_FMT specifies the format |
|
120 // of the data if we are reading floating point numbers. |
|
121 |
|
122 static void |
|
123 read_mat5_binary_data (std::istream& is, double *data, |
|
124 int count, bool swap, mat5_data_type type, |
|
125 oct_mach_info::float_format flt_fmt) |
|
126 { |
|
127 |
|
128 switch (type) |
|
129 { |
|
130 case miINT8: |
|
131 read_doubles (is, data, LS_CHAR, count, swap, flt_fmt); |
|
132 break; |
|
133 |
5351
|
134 case miUTF8: |
4634
|
135 case miUINT8: |
|
136 read_doubles (is, data, LS_U_CHAR, count, swap, flt_fmt); |
|
137 break; |
|
138 |
|
139 case miINT16: |
|
140 read_doubles (is, data, LS_SHORT, count, swap, flt_fmt); |
|
141 break; |
|
142 |
|
143 case miUINT16: |
|
144 read_doubles (is, data, LS_U_SHORT, count, swap, flt_fmt); |
|
145 break; |
|
146 |
|
147 case miINT32: |
|
148 read_doubles (is, data, LS_INT, count, swap, flt_fmt); |
|
149 break; |
|
150 |
|
151 case miUINT32: |
|
152 read_doubles (is, data, LS_U_INT, count, swap, flt_fmt); |
|
153 break; |
|
154 |
|
155 case miSINGLE: |
|
156 read_doubles (is, data, LS_FLOAT, count, swap, flt_fmt); |
|
157 break; |
|
158 |
|
159 case miRESERVE1: |
|
160 break; |
|
161 |
|
162 case miDOUBLE: |
|
163 read_doubles (is, data, LS_DOUBLE, count, swap, flt_fmt); |
|
164 break; |
|
165 |
|
166 case miRESERVE2: |
|
167 case miRESERVE3: |
|
168 break; |
|
169 |
5949
|
170 // FIXME -- how are the 64-bit cases supposed to work here? |
4634
|
171 case miINT64: |
|
172 read_doubles (is, data, LS_LONG, count, swap, flt_fmt); |
|
173 break; |
|
174 |
|
175 case miUINT64: |
|
176 read_doubles (is, data, LS_U_LONG, count, swap, flt_fmt); |
|
177 break; |
|
178 |
|
179 case miMATRIX: |
|
180 default: |
|
181 break; |
|
182 } |
|
183 } |
|
184 |
5089
|
185 template <class T> |
|
186 void |
5164
|
187 read_mat5_integer_data (std::istream& is, T *m, int count, bool swap, |
5089
|
188 mat5_data_type type) |
|
189 { |
|
190 |
|
191 #define READ_INTEGER_DATA(TYPE, swap, data, size, len, stream) \ |
|
192 do \ |
|
193 { \ |
|
194 if (len > 0) \ |
|
195 { \ |
5760
|
196 OCTAVE_LOCAL_BUFFER (TYPE, ptr, len); \ |
|
197 stream.read (reinterpret_cast<char *> (ptr), size * len); \ |
5089
|
198 if (swap) \ |
|
199 swap_bytes< size > (ptr, len); \ |
5760
|
200 for (int i = 0; i < len; i++) \ |
5089
|
201 data[i] = ptr[i]; \ |
|
202 } \ |
|
203 } \ |
|
204 while (0) |
|
205 |
|
206 switch (type) |
|
207 { |
|
208 case miINT8: |
5828
|
209 READ_INTEGER_DATA (int8_t, swap, m, 1, count, is); |
5089
|
210 break; |
|
211 |
|
212 case miUINT8: |
5828
|
213 READ_INTEGER_DATA (uint8_t, swap, m, 1, count, is); |
5089
|
214 break; |
|
215 |
|
216 case miINT16: |
5828
|
217 READ_INTEGER_DATA (int16_t, swap, m, 2, count, is); |
5089
|
218 break; |
|
219 |
|
220 case miUINT16: |
5828
|
221 READ_INTEGER_DATA (uint16_t, swap, m, 2, count, is); |
5089
|
222 break; |
|
223 |
|
224 case miINT32: |
5828
|
225 READ_INTEGER_DATA (int32_t, swap, m, 4, count, is); |
5089
|
226 break; |
|
227 |
|
228 case miUINT32: |
5828
|
229 READ_INTEGER_DATA (uint32_t, swap, m, 4, count, is); |
5089
|
230 break; |
|
231 |
|
232 case miSINGLE: |
|
233 case miRESERVE1: |
|
234 case miDOUBLE: |
|
235 case miRESERVE2: |
|
236 case miRESERVE3: |
|
237 break; |
|
238 |
|
239 case miINT64: |
5828
|
240 READ_INTEGER_DATA (int64_t, swap, m, 8, count, is); |
5089
|
241 break; |
|
242 |
|
243 case miUINT64: |
5828
|
244 READ_INTEGER_DATA (uint64_t, swap, m, 8, count, is); |
5089
|
245 break; |
|
246 |
|
247 case miMATRIX: |
|
248 default: |
|
249 break; |
|
250 } |
|
251 |
|
252 #undef READ_INTEGER_DATA |
|
253 |
|
254 } |
|
255 |
5164
|
256 template void read_mat5_integer_data (std::istream& is, octave_int8 *m, |
5089
|
257 int count, bool swap, |
|
258 mat5_data_type type); |
5164
|
259 template void read_mat5_integer_data (std::istream& is, octave_int16 *m, |
5089
|
260 int count, bool swap, |
|
261 mat5_data_type type); |
5164
|
262 template void read_mat5_integer_data (std::istream& is, octave_int32 *m, |
|
263 int count, bool swap, |
|
264 mat5_data_type type); |
|
265 template void read_mat5_integer_data (std::istream& is, octave_int64 *m, |
5089
|
266 int count, bool swap, |
|
267 mat5_data_type type); |
5164
|
268 template void read_mat5_integer_data (std::istream& is, octave_uint8 *m, |
5089
|
269 int count, bool swap, |
|
270 mat5_data_type type); |
5164
|
271 template void read_mat5_integer_data (std::istream& is, octave_uint16 *m, |
5089
|
272 int count, bool swap, |
|
273 mat5_data_type type); |
5164
|
274 template void read_mat5_integer_data (std::istream& is, octave_uint32 *m, |
5089
|
275 int count, bool swap, |
|
276 mat5_data_type type); |
5164
|
277 template void read_mat5_integer_data (std::istream& is, octave_uint64 *m, |
5089
|
278 int count, bool swap, |
|
279 mat5_data_type type); |
5164
|
280 |
|
281 template void read_mat5_integer_data (std::istream& is, int *m, |
5089
|
282 int count, bool swap, |
|
283 mat5_data_type type); |
|
284 |
|
285 #define OCTAVE_MAT5_INTEGER_READ(TYP) \ |
|
286 { \ |
|
287 TYP re (dims); \ |
|
288 \ |
|
289 std::streampos tmp_pos; \ |
|
290 \ |
|
291 if (read_mat5_tag (is, swap, type, len)) \ |
|
292 { \ |
|
293 error ("load: reading matrix data for `%s'", retval.c_str ()); \ |
|
294 goto data_read_error; \ |
|
295 } \ |
|
296 \ |
|
297 int n = re.length (); \ |
|
298 tmp_pos = is.tellg (); \ |
5164
|
299 read_mat5_integer_data (is, re.fortran_vec (), n, swap, \ |
5760
|
300 static_cast<enum mat5_data_type> (type)); \ |
5089
|
301 \ |
|
302 if (! is || error_state) \ |
|
303 { \ |
|
304 error ("load: reading matrix data for `%s'", retval.c_str ()); \ |
|
305 goto data_read_error; \ |
|
306 } \ |
|
307 \ |
|
308 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); \ |
|
309 \ |
|
310 if (imag) \ |
|
311 { \ |
|
312 /* We don't handle imag integer types, convert to an array */ \ |
|
313 NDArray im (dims); \ |
|
314 \ |
|
315 if (read_mat5_tag (is, swap, type, len)) \ |
|
316 { \ |
|
317 error ("load: reading matrix data for `%s'", \ |
|
318 retval.c_str ()); \ |
|
319 goto data_read_error; \ |
|
320 } \ |
|
321 \ |
|
322 n = im.length (); \ |
|
323 read_mat5_binary_data (is, im.fortran_vec (), n, swap, \ |
5760
|
324 static_cast<enum mat5_data_type> (type), flt_fmt); \ |
5089
|
325 \ |
|
326 if (! is || error_state) \ |
|
327 { \ |
|
328 error ("load: reading imaginary matrix data for `%s'", \ |
|
329 retval.c_str ()); \ |
|
330 goto data_read_error; \ |
|
331 } \ |
|
332 \ |
|
333 ComplexNDArray ctmp (dims); \ |
|
334 \ |
|
335 for (int i = 0; i < n; i++) \ |
|
336 ctmp(i) = Complex (double (re(i)), im(i)); \ |
|
337 \ |
|
338 tc = ctmp; \ |
|
339 } \ |
|
340 else \ |
|
341 tc = re; \ |
|
342 } |
|
343 |
4634
|
344 // Read one element tag from stream IS, |
|
345 // place the type code in TYPE and the byte count in BYTES |
|
346 // return nonzero on error |
|
347 static int |
6125
|
348 read_mat5_tag (std::istream& is, bool swap, int32_t& type, int32_t& bytes) |
4634
|
349 { |
|
350 unsigned int upper; |
5828
|
351 int32_t temp; |
4634
|
352 |
5760
|
353 if (! is.read (reinterpret_cast<char *> (&temp), 4 )) |
4634
|
354 goto data_read_error; |
|
355 |
|
356 if (swap) |
4944
|
357 swap_bytes<4> (&temp); |
4634
|
358 |
|
359 upper = (temp >> 16) & 0xffff; |
|
360 type = temp & 0xffff; |
|
361 |
|
362 if (upper) |
|
363 { |
|
364 // "compressed" format |
|
365 bytes = upper; |
|
366 } |
|
367 else |
|
368 { |
5760
|
369 if (! is.read (reinterpret_cast<char *> (&temp), 4 )) |
4634
|
370 goto data_read_error; |
|
371 if (swap) |
4944
|
372 swap_bytes<4> (&temp); |
4634
|
373 bytes = temp; |
|
374 } |
|
375 |
|
376 return 0; |
|
377 |
|
378 data_read_error: |
|
379 return 1; |
|
380 } |
|
381 |
4944
|
382 static void |
5828
|
383 read_int (std::istream& is, bool swap, int32_t& val) |
4944
|
384 { |
|
385 is.read (reinterpret_cast<char *> (&val), 4); |
|
386 |
|
387 if (swap) |
|
388 swap_bytes<4> (&val); |
|
389 } |
|
390 |
4634
|
391 // Extract one data element (scalar, matrix, string, etc.) from stream |
|
392 // IS and place it in TC, returning the name of the variable. |
|
393 // |
|
394 // The data is expected to be in Matlab's "Version 5" .mat format, |
|
395 // though not all the features of that format are supported. |
|
396 // |
|
397 // FILENAME is used for error messages. |
|
398 |
|
399 std::string |
|
400 read_mat5_binary_element (std::istream& is, const std::string& filename, |
|
401 bool swap, bool& global, octave_value& tc) |
|
402 { |
|
403 std::string retval; |
|
404 |
|
405 // These are initialized here instead of closer to where they are |
|
406 // first used to avoid errors from gcc about goto crossing |
|
407 // initialization of variable. |
|
408 |
|
409 oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown; |
5941
|
410 int32_t type = 0; |
6625
|
411 std::string classname; |
|
412 bool isclass = false; |
4634
|
413 bool imag; |
|
414 bool logicalvar; |
|
415 enum arrayclasstype arrayclass; |
5828
|
416 int32_t nzmax; |
|
417 int32_t flags; |
4634
|
418 dim_vector dims; |
5941
|
419 int32_t len; |
|
420 int32_t element_length; |
4634
|
421 std::streampos pos; |
5828
|
422 int16_t number; |
6625
|
423 number = *(reinterpret_cast<const int16_t *>("\x00\x01")); |
4634
|
424 |
|
425 global = false; |
|
426 |
|
427 // MAT files always use IEEE floating point |
|
428 if ((number == 1) ^ swap) |
|
429 flt_fmt = oct_mach_info::flt_fmt_ieee_big_endian; |
|
430 else |
|
431 flt_fmt = oct_mach_info::flt_fmt_ieee_little_endian; |
|
432 |
|
433 // element type and length |
|
434 if (read_mat5_tag (is, swap, type, element_length)) |
|
435 return retval; // EOF |
|
436 |
5383
|
437 #ifdef HAVE_ZLIB |
5269
|
438 if (type == miCOMPRESSED) |
|
439 { |
|
440 // If C++ allowed us direct access to the file descriptor of an ifstream |
|
441 // in a uniform way, the code below could be vastly simplified, and |
|
442 // additional copies of the data in memory wouldn't be needed!! |
|
443 |
|
444 OCTAVE_LOCAL_BUFFER (char, inbuf, element_length); |
|
445 is.read (inbuf, element_length); |
|
446 |
|
447 // We uncompress the first 8 bytes of the header to get the buffer length |
|
448 // This will fail with an error Z_MEM_ERROR |
|
449 uLongf destLen = 8; |
|
450 OCTAVE_LOCAL_BUFFER (unsigned int, tmp, 2); |
5760
|
451 if (uncompress (reinterpret_cast<Bytef *> (tmp), &destLen, |
|
452 reinterpret_cast<Bytef *> (inbuf), element_length) |
|
453 != Z_MEM_ERROR) |
5269
|
454 { |
|
455 // Why should I have to initialize outbuf as I'll just overwrite!! |
5322
|
456 if (swap) |
|
457 swap_bytes<4> (tmp, 2); |
|
458 |
5269
|
459 destLen = tmp[1] + 8; |
|
460 std::string outbuf (destLen, ' '); |
|
461 |
5775
|
462 // FIXME -- find a way to avoid casting away const here! |
5760
|
463 |
|
464 int err = uncompress (reinterpret_cast<Bytef *> (const_cast<char *> (outbuf.c_str ())), &destLen, |
|
465 reinterpret_cast<Bytef *> (inbuf), element_length); |
5269
|
466 |
|
467 if (err != Z_OK) |
|
468 error ("load: error uncompressing data element"); |
|
469 else |
|
470 { |
5765
|
471 std::istringstream gz_is (outbuf); |
5269
|
472 retval = read_mat5_binary_element (gz_is, filename, |
|
473 swap, global, tc); |
|
474 } |
|
475 } |
|
476 else |
|
477 error ("load: error probing size of compressed data element"); |
|
478 |
|
479 return retval; |
|
480 } |
|
481 #endif |
|
482 |
4634
|
483 if (type != miMATRIX) |
|
484 { |
6625
|
485 pos = is.tellg (); |
5930
|
486 error ("load: invalid element type = %d", type); |
4634
|
487 goto early_read_error; |
|
488 } |
|
489 |
|
490 if (element_length == 0) |
|
491 { |
|
492 tc = Matrix (); |
|
493 return retval; |
|
494 } |
|
495 |
|
496 pos = is.tellg (); |
|
497 |
|
498 // array flags subelement |
|
499 if (read_mat5_tag (is, swap, type, len) || type != miUINT32 || len != 8) |
|
500 { |
|
501 error ("load: invalid array flags subelement"); |
|
502 goto early_read_error; |
|
503 } |
|
504 |
|
505 read_int (is, swap, flags); |
|
506 imag = (flags & 0x0800) != 0; // has an imaginary part? |
|
507 global = (flags & 0x0400) != 0; // global variable? |
5269
|
508 logicalvar = (flags & 0x0200) != 0; // boolean ? |
5760
|
509 arrayclass = static_cast<arrayclasstype> (flags & 0xff); |
5592
|
510 read_int (is, swap, nzmax); // max number of non-zero in sparse |
4634
|
511 |
|
512 // dimensions array subelement |
6625
|
513 if (arrayclass != MAT_FILE_WORKSPACE_CLASS) |
|
514 { |
|
515 int32_t dim_len; |
4638
|
516 |
6625
|
517 if (read_mat5_tag (is, swap, type, dim_len) || type != miINT32) |
|
518 { |
|
519 error ("load: invalid dimensions array subelement"); |
|
520 goto early_read_error; |
|
521 } |
4634
|
522 |
6625
|
523 int ndims = dim_len / 4; |
|
524 dims.resize (ndims); |
|
525 for (int i = 0; i < ndims; i++) |
|
526 { |
|
527 int32_t n; |
|
528 read_int (is, swap, n); |
|
529 dims(i) = n; |
|
530 } |
4634
|
531 |
6625
|
532 std::streampos tmp_pos = is.tellg (); |
|
533 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (dim_len) - dim_len)); |
|
534 } |
|
535 else |
|
536 { |
|
537 // Why did mathworks decide to not have dims for a workspace!!! |
|
538 dims.resize(2); |
|
539 dims(0) = 1; |
|
540 dims(1) = 1; |
|
541 } |
4634
|
542 |
|
543 if (read_mat5_tag (is, swap, type, len) || type != miINT8) |
|
544 { |
|
545 error ("load: invalid array name subelement"); |
|
546 goto early_read_error; |
|
547 } |
|
548 |
|
549 { |
|
550 OCTAVE_LOCAL_BUFFER (char, name, len+1); |
|
551 |
|
552 // Structure field subelements have zero-length array name subelements. |
|
553 |
|
554 std::streampos tmp_pos = is.tellg (); |
|
555 |
|
556 if (len) |
|
557 { |
5760
|
558 if (! is.read (name, len )) |
4634
|
559 goto data_read_error; |
|
560 |
|
561 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
|
562 } |
|
563 |
|
564 name[len] = '\0'; |
|
565 retval = name; |
|
566 } |
|
567 |
|
568 switch (arrayclass) |
|
569 { |
5900
|
570 case MAT_FILE_CELL_CLASS: |
4634
|
571 { |
|
572 Cell cell_array (dims); |
|
573 |
|
574 int n = cell_array.length (); |
|
575 |
|
576 for (int i = 0; i < n; i++) |
|
577 { |
|
578 octave_value tc2; |
|
579 |
|
580 std::string nm |
|
581 = read_mat5_binary_element (is, filename, swap, global, tc2); |
|
582 |
|
583 if (! is || error_state) |
|
584 { |
|
585 error ("load: reading cell data for `%s'", nm.c_str ()); |
|
586 goto data_read_error; |
|
587 } |
|
588 |
|
589 cell_array(i) = tc2; |
|
590 } |
|
591 |
|
592 tc = cell_array; |
|
593 } |
|
594 break; |
|
595 |
5900
|
596 case MAT_FILE_SPARSE_CLASS: |
5297
|
597 #if SIZEOF_INT != SIZEOF_OCTAVE_IDX_TYPE |
|
598 warning ("load: sparse objects are not implemented"); |
|
599 goto skip_ahead; |
|
600 #else |
5164
|
601 { |
|
602 int nr = dims(0); |
|
603 int nc = dims(1); |
|
604 SparseMatrix sm; |
|
605 SparseComplexMatrix scm; |
|
606 int *ridx; |
|
607 int *cidx; |
|
608 double *data; |
|
609 |
|
610 // Setup return value |
|
611 if (imag) |
|
612 { |
5275
|
613 scm = SparseComplexMatrix (static_cast<octave_idx_type> (nr), |
|
614 static_cast<octave_idx_type> (nc), |
5592
|
615 static_cast<octave_idx_type> (nzmax)); |
5164
|
616 ridx = scm.ridx (); |
|
617 cidx = scm.cidx (); |
5592
|
618 data = 0; |
5164
|
619 } |
|
620 else |
|
621 { |
5275
|
622 sm = SparseMatrix (static_cast<octave_idx_type> (nr), |
|
623 static_cast<octave_idx_type> (nc), |
5592
|
624 static_cast<octave_idx_type> (nzmax)); |
5164
|
625 ridx = sm.ridx (); |
|
626 cidx = sm.cidx (); |
|
627 data = sm.data (); |
|
628 } |
|
629 |
|
630 // row indices |
|
631 std::streampos tmp_pos; |
|
632 |
|
633 if (read_mat5_tag (is, swap, type, len)) |
|
634 { |
|
635 error ("load: reading sparse row data for `%s'", retval.c_str ()); |
|
636 goto data_read_error; |
|
637 } |
|
638 |
|
639 tmp_pos = is.tellg (); |
|
640 |
5592
|
641 read_mat5_integer_data (is, ridx, nzmax, swap, |
5760
|
642 static_cast<enum mat5_data_type> (type)); |
5164
|
643 |
|
644 if (! is || error_state) |
|
645 { |
|
646 error ("load: reading sparse row data for `%s'", retval.c_str ()); |
|
647 goto data_read_error; |
|
648 } |
|
649 |
|
650 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
|
651 |
|
652 // col indices |
|
653 if (read_mat5_tag (is, swap, type, len)) |
|
654 { |
|
655 error ("load: reading sparse column data for `%s'", retval.c_str ()); |
|
656 goto data_read_error; |
|
657 } |
|
658 |
|
659 tmp_pos = is.tellg (); |
|
660 |
|
661 read_mat5_integer_data (is, cidx, nc + 1, swap, |
5760
|
662 static_cast<enum mat5_data_type> (type)); |
5164
|
663 |
|
664 if (! is || error_state) |
|
665 { |
|
666 error ("load: reading sparse column data for `%s'", retval.c_str ()); |
|
667 goto data_read_error; |
|
668 } |
|
669 |
|
670 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
|
671 |
|
672 // real data subelement |
|
673 if (read_mat5_tag (is, swap, type, len)) |
|
674 { |
|
675 error ("load: reading sparse matrix data for `%s'", retval.c_str ()); |
|
676 goto data_read_error; |
|
677 } |
|
678 |
5828
|
679 int32_t nnz = cidx[nc]; |
5592
|
680 NDArray re; |
|
681 if (imag) |
|
682 { |
|
683 re = NDArray (dim_vector (static_cast<int> (nnz))); |
|
684 data = re.fortran_vec (); |
|
685 } |
|
686 |
5164
|
687 tmp_pos = is.tellg (); |
|
688 read_mat5_binary_data (is, data, nnz, swap, |
5760
|
689 static_cast<enum mat5_data_type> (type), flt_fmt); |
5164
|
690 |
|
691 if (! is || error_state) |
|
692 { |
|
693 error ("load: reading sparse matrix data for `%s'", retval.c_str ()); |
|
694 goto data_read_error; |
|
695 } |
|
696 |
|
697 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
|
698 |
|
699 // imaginary data subelement |
|
700 if (imag) |
|
701 { |
|
702 NDArray im (dim_vector (static_cast<int> (nnz))); |
|
703 |
|
704 if (read_mat5_tag (is, swap, type, len)) |
|
705 { |
|
706 error ("load: reading sparse matrix data for `%s'", retval.c_str ()); |
|
707 goto data_read_error; |
|
708 } |
|
709 |
|
710 read_mat5_binary_data (is, im.fortran_vec (), nnz, swap, |
5760
|
711 static_cast<enum mat5_data_type> (type), flt_fmt); |
5164
|
712 |
|
713 if (! is || error_state) |
|
714 { |
|
715 error ("load: reading imaginary sparse matrix data for `%s'", |
|
716 retval.c_str ()); |
|
717 goto data_read_error; |
|
718 } |
|
719 |
|
720 for (int i = 0; i < nnz; i++) |
|
721 scm.xdata (i) = Complex (re (i), im (i)); |
|
722 |
|
723 tc = scm; |
|
724 } |
|
725 else |
|
726 tc = sm; |
|
727 } |
6625
|
728 #endif |
5164
|
729 break; |
4634
|
730 |
5900
|
731 case MAT_FILE_FUNCTION_CLASS: |
6625
|
732 { |
|
733 octave_value tc2; |
|
734 std::string nm |
|
735 = read_mat5_binary_element (is, filename, swap, global, tc2); |
|
736 |
|
737 if (! is || error_state) |
|
738 goto data_read_error; |
|
739 |
|
740 // Octave can handle both "/" and "\" as a directry seperator |
|
741 // and so can ignore the seperator field of m0. I think the |
|
742 // sentinel field is also save to ignore. |
|
743 Octave_map m0 = tc2.map_value(); |
|
744 Octave_map m1 = m0.contents("function_handle")(0).map_value(); |
|
745 std::string ftype = m1.contents("type")(0).string_value(); |
|
746 std::string fname = m1.contents("function")(0).string_value(); |
|
747 std::string fpath = m1.contents("file")(0).string_value(); |
|
748 |
|
749 if (ftype == "simple" || ftype == "scopedfunction") |
|
750 { |
|
751 if (fpath.length() == 0) |
|
752 // We have a builtin function |
|
753 tc = make_fcn_handle (fname); |
|
754 else |
|
755 { |
|
756 std::string mroot = |
|
757 m0.contents("matlabroot")(0).string_value(); |
|
758 |
|
759 if ((fpath.length () >= mroot.length ()) && |
|
760 fpath.substr(0, mroot.length()) == mroot && |
|
761 OCTAVE_EXEC_PREFIX != mroot) |
|
762 { |
|
763 // If fpath starts with matlabroot, and matlabroot |
|
764 // doesn't equal octave_config_info ("exec_prefix") |
|
765 // then the function points to a version of Octave |
|
766 // or Matlab other than the running version. In that |
|
767 // case we replace with the same function in the |
|
768 // running version of Octave? |
|
769 |
|
770 // First check if just replacing matlabroot is enough |
|
771 std::string str = OCTAVE_EXEC_PREFIX + |
|
772 fpath.substr (mroot.length ()); |
|
773 file_stat fs (str); |
|
774 |
|
775 if (fs.exists ()) |
|
776 { |
|
777 symbol_record *sr = fbi_sym_tab->lookup (str, true); |
|
778 |
|
779 if (sr) |
|
780 { |
|
781 load_fcn_from_file (sr, false); |
|
782 |
|
783 tc = octave_value (new octave_fcn_handle |
|
784 (sr->def (), fname)); |
|
785 |
|
786 // The next two lines are needed to force the |
|
787 // definition of the function back to the one |
|
788 // that is on the user path. |
|
789 sr = fbi_sym_tab->lookup (fname, true); |
|
790 |
|
791 load_fcn_from_file (sr, false); |
|
792 } |
|
793 } |
|
794 else |
|
795 { |
|
796 // Next just search for it anywhere in the |
|
797 // system path |
|
798 string_vector names(3); |
|
799 names(0) = fname + ".oct"; |
|
800 names(1) = fname + ".mex"; |
|
801 names(2) = fname + ".m"; |
|
802 |
6626
|
803 dir_path p (load_path::system_path ()); |
6625
|
804 |
|
805 str = octave_env::make_absolute |
|
806 (p.find_first_of (names), octave_env::getcwd ()); |
|
807 |
|
808 symbol_record *sr = fbi_sym_tab->lookup (str, true); |
|
809 |
|
810 if (sr) |
|
811 { |
|
812 load_fcn_from_file (sr, false); |
|
813 |
|
814 tc = octave_value (new octave_fcn_handle |
|
815 (sr->def (), fname)); |
|
816 |
|
817 // The next two lines are needed to force the |
|
818 // definition of the function back to the one |
|
819 // that is on the user path. |
|
820 sr = fbi_sym_tab->lookup (fname, true); |
|
821 |
|
822 load_fcn_from_file (sr, false); |
|
823 } |
|
824 else |
|
825 { |
|
826 warning ("load: can't find the file %s", |
|
827 fpath.c_str()); |
|
828 goto skip_ahead; |
|
829 } |
|
830 } |
|
831 } |
|
832 else |
|
833 { |
|
834 symbol_record *sr = fbi_sym_tab->lookup (fpath, true); |
|
835 |
|
836 if (sr) |
|
837 { |
|
838 load_fcn_from_file (sr, false); |
|
839 |
|
840 tc = octave_value (new octave_fcn_handle (sr->def (), |
|
841 fname)); |
|
842 |
|
843 sr = fbi_sym_tab->lookup (fname, true); |
|
844 |
|
845 load_fcn_from_file (sr, false); |
|
846 } |
|
847 else |
|
848 { |
|
849 warning ("load: can't find the file %s", |
|
850 fpath.c_str()); |
|
851 goto skip_ahead; |
|
852 } |
|
853 } |
|
854 } |
|
855 } |
|
856 else if (ftype == "nested") |
|
857 { |
|
858 warning ("load: can't load nested function"); |
|
859 goto skip_ahead; |
|
860 } |
|
861 else if (ftype == "anonymous") |
|
862 { |
|
863 Octave_map m2 = m1.contents("workspace")(0).map_value(); |
|
864 uint32NDArray MCOS = m2.contents("MCOS")(0).uint32_array_value(); |
|
865 octave_idx_type off = static_cast<octave_idx_type>(double (MCOS (4))); |
|
866 m2 = subsys_ov.map_value(); |
|
867 m2 = m2.contents("MCOS")(0).map_value(); |
|
868 tc2 = m2.contents("MCOS")(0).cell_value()(1 + off).cell_value()(1); |
|
869 m2 = tc2.map_value(); |
|
870 symbol_table *local_sym_tab = 0; |
6639
|
871 if (m2.nfields() > 0) |
6625
|
872 { |
|
873 octave_value tmp; |
5089
|
874 |
6639
|
875 local_sym_tab = new symbol_table (((m2.nfields() + 1) & ~1), |
6625
|
876 "LOCAL"); |
|
877 |
|
878 for (Octave_map::iterator p0 = m2.begin() ; |
|
879 p0 != m2.end(); p0++) |
|
880 { |
|
881 std::string key = m2.key(p0); |
|
882 octave_value val = m2.contents(p0)(0); |
|
883 |
|
884 symbol_record *sr = local_sym_tab->lookup (key, true); |
|
885 |
|
886 if (sr) |
|
887 sr->define (val); |
|
888 else |
|
889 { |
|
890 error ("load: failed to load anonymous function handle"); |
|
891 goto skip_ahead; |
|
892 } |
|
893 } |
|
894 } |
|
895 |
|
896 unwind_protect::begin_frame ("anon_mat5_load"); |
|
897 unwind_protect_ptr (curr_sym_tab); |
|
898 |
|
899 if (local_sym_tab) |
|
900 curr_sym_tab = local_sym_tab; |
|
901 |
|
902 int parse_status; |
|
903 octave_value anon_fcn_handle = |
|
904 eval_string (fname.substr (4), true, parse_status); |
|
905 |
|
906 if (parse_status == 0) |
|
907 { |
|
908 octave_fcn_handle *fh = |
|
909 anon_fcn_handle.fcn_handle_value (); |
|
910 if (fh) |
|
911 tc = new octave_fcn_handle (fh->fcn_val(), "@<anonymous>"); |
|
912 else |
|
913 { |
|
914 error ("load: failed to load anonymous function handle"); |
|
915 goto skip_ahead; |
|
916 } |
|
917 } |
|
918 else |
|
919 { |
|
920 error ("load: failed to load anonymous function handle"); |
|
921 goto skip_ahead; |
|
922 } |
|
923 |
|
924 unwind_protect::run_frame ("anon_mat5_load"); |
|
925 |
|
926 if (local_sym_tab) |
|
927 delete local_sym_tab; |
|
928 } |
|
929 else |
|
930 { |
|
931 error ("load: invalid function handle type"); |
|
932 goto skip_ahead; |
|
933 } |
|
934 } |
|
935 break; |
|
936 |
|
937 case MAT_FILE_WORKSPACE_CLASS: |
|
938 { |
|
939 Octave_map m (dim_vector (1, 1)); |
|
940 int n_fields = 2; |
|
941 string_vector field (n_fields); |
|
942 |
|
943 for (int i = 0; i < n_fields; i++) |
|
944 { |
|
945 int32_t fn_type; |
|
946 int32_t fn_len; |
|
947 if (read_mat5_tag (is, swap, fn_type, fn_len) || fn_type != miINT8) |
|
948 { |
|
949 error ("load: invalid field name subelement"); |
|
950 goto data_read_error; |
|
951 } |
|
952 |
|
953 OCTAVE_LOCAL_BUFFER (char, elname, fn_len + 1); |
|
954 |
|
955 std::streampos tmp_pos = is.tellg (); |
|
956 |
|
957 if (fn_len) |
|
958 { |
|
959 if (! is.read (elname, fn_len)) |
|
960 goto data_read_error; |
|
961 |
|
962 is.seekg (tmp_pos + |
|
963 static_cast<std::streamoff> (PAD (fn_len))); |
|
964 } |
|
965 |
|
966 elname[fn_len] = '\0'; |
|
967 |
|
968 field(i) = elname; |
|
969 } |
|
970 |
|
971 std::vector<Cell> elt (n_fields); |
|
972 |
|
973 for (octave_idx_type i = 0; i < n_fields; i++) |
|
974 elt[i] = Cell (dims); |
|
975 |
|
976 octave_idx_type n = dims.numel (); |
|
977 |
|
978 // fields subelements |
|
979 for (octave_idx_type j = 0; j < n; j++) |
|
980 { |
|
981 for (octave_idx_type i = 0; i < n_fields; i++) |
|
982 { |
|
983 if (field(i) == "MCOS") |
|
984 { |
|
985 octave_value fieldtc; |
|
986 read_mat5_binary_element (is, filename, swap, global, |
|
987 fieldtc); |
|
988 if (! is || error_state) |
|
989 goto data_read_error; |
|
990 |
|
991 elt[i](j) = fieldtc; |
|
992 } |
|
993 else |
|
994 elt[i](j) = octave_value (); |
|
995 } |
|
996 } |
|
997 |
|
998 for (octave_idx_type i = 0; i < n_fields; i++) |
|
999 m.assign (field (i), elt[i]); |
|
1000 tc = m; |
|
1001 } |
|
1002 break; |
|
1003 |
|
1004 case MAT_FILE_OBJECT_CLASS: |
|
1005 { |
|
1006 isclass = true; |
|
1007 |
|
1008 if (read_mat5_tag (is, swap, type, len) || type != miINT8) |
|
1009 { |
|
1010 error ("load: invalid class name"); |
|
1011 goto skip_ahead; |
|
1012 } |
|
1013 |
|
1014 { |
|
1015 OCTAVE_LOCAL_BUFFER (char, name, len+1); |
|
1016 |
|
1017 std::streampos tmp_pos = is.tellg (); |
|
1018 |
|
1019 if (len) |
|
1020 { |
|
1021 if (! is.read (name, len )) |
|
1022 goto data_read_error; |
|
1023 |
|
1024 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
|
1025 } |
|
1026 |
|
1027 name[len] = '\0'; |
|
1028 classname = name; |
|
1029 } |
|
1030 } |
|
1031 // Fall-through |
5900
|
1032 case MAT_FILE_STRUCT_CLASS: |
4634
|
1033 { |
6292
|
1034 Octave_map m (dim_vector (1, 1)); |
5828
|
1035 int32_t fn_type; |
|
1036 int32_t fn_len; |
|
1037 int32_t field_name_length; |
4634
|
1038 |
|
1039 // field name length subelement -- actually the maximum length |
|
1040 // of a field name. The Matlab docs promise this will always |
|
1041 // be 32. We read and use the actual value, on the theory |
|
1042 // that eventually someone will recognize that's a waste of |
|
1043 // space. |
|
1044 if (read_mat5_tag (is, swap, fn_type, fn_len) || fn_type != miINT32) |
|
1045 { |
6292
|
1046 error ("load: invalid field name length subelement"); |
4634
|
1047 goto data_read_error; |
|
1048 } |
|
1049 |
5760
|
1050 if (! is.read (reinterpret_cast<char *> (&field_name_length), fn_len )) |
4634
|
1051 goto data_read_error; |
|
1052 |
|
1053 if (swap) |
4944
|
1054 swap_bytes<4> (&field_name_length); |
4634
|
1055 |
|
1056 // field name subelement. The length of this subelement tells |
|
1057 // us how many fields there are. |
|
1058 if (read_mat5_tag (is, swap, fn_type, fn_len) || fn_type != miINT8) |
|
1059 { |
|
1060 error ("load: invalid field name subelement"); |
|
1061 goto data_read_error; |
|
1062 } |
|
1063 |
5336
|
1064 octave_idx_type n_fields = fn_len/field_name_length; |
4634
|
1065 |
6292
|
1066 if (n_fields > 0) |
|
1067 { |
|
1068 fn_len = PAD (fn_len); |
4634
|
1069 |
6292
|
1070 OCTAVE_LOCAL_BUFFER (char, elname, fn_len); |
4634
|
1071 |
6292
|
1072 if (! is.read (elname, fn_len)) |
|
1073 goto data_read_error; |
4634
|
1074 |
6292
|
1075 std::vector<Cell> elt (n_fields); |
|
1076 |
|
1077 for (octave_idx_type i = 0; i < n_fields; i++) |
|
1078 elt[i] = Cell (dims); |
4634
|
1079 |
6292
|
1080 octave_idx_type n = dims.numel (); |
5336
|
1081 |
6292
|
1082 // fields subelements |
|
1083 for (octave_idx_type j = 0; j < n; j++) |
|
1084 { |
|
1085 for (octave_idx_type i = 0; i < n_fields; i++) |
|
1086 { |
|
1087 octave_value fieldtc; |
|
1088 read_mat5_binary_element (is, filename, swap, global, |
|
1089 fieldtc); |
|
1090 elt[i](j) = fieldtc; |
|
1091 } |
|
1092 } |
4634
|
1093 |
5336
|
1094 for (octave_idx_type i = 0; i < n_fields; i++) |
4634
|
1095 { |
6292
|
1096 const char *key = elname + i*field_name_length; |
5336
|
1097 |
6292
|
1098 m.assign (key, elt[i]); |
|
1099 } |
4634
|
1100 } |
|
1101 |
6625
|
1102 if (isclass) |
|
1103 { |
|
1104 if (classname == "inline") |
|
1105 { |
|
1106 // inline is not an object in Octave but rather an |
|
1107 // overload of a function handle. Special case. |
|
1108 tc = |
|
1109 new octave_fcn_inline (m.contents("expr")(0).string_value(), |
|
1110 m.contents("args")(0).string_value()); |
|
1111 } |
|
1112 else |
|
1113 { |
|
1114 warning ("load: objects are not implemented"); |
|
1115 goto skip_ahead; |
|
1116 } |
|
1117 } |
|
1118 else |
|
1119 tc = m; |
4634
|
1120 } |
|
1121 break; |
|
1122 |
5900
|
1123 case MAT_FILE_INT8_CLASS: |
5089
|
1124 OCTAVE_MAT5_INTEGER_READ (int8NDArray); |
|
1125 break; |
|
1126 |
5900
|
1127 case MAT_FILE_UINT8_CLASS: |
5269
|
1128 { |
|
1129 OCTAVE_MAT5_INTEGER_READ (uint8NDArray); |
|
1130 |
5900
|
1131 // Logical variables can either be MAT_FILE_UINT8_CLASS or |
|
1132 // MAT_FILE_DOUBLE_CLASS, so check if we have a logical |
|
1133 // variable and convert it. |
5269
|
1134 |
|
1135 if (logicalvar) |
|
1136 { |
|
1137 uint8NDArray in = tc.uint8_array_value (); |
|
1138 int nel = in.nelem (); |
|
1139 boolNDArray out (dims); |
|
1140 |
|
1141 for (int i = 0; i < nel; i++) |
|
1142 out (i) = static_cast<bool> (double (in (i))); |
|
1143 |
|
1144 tc = out; |
|
1145 } |
|
1146 } |
5089
|
1147 break; |
|
1148 |
5900
|
1149 case MAT_FILE_INT16_CLASS: |
5089
|
1150 OCTAVE_MAT5_INTEGER_READ (int16NDArray); |
|
1151 break; |
|
1152 |
5900
|
1153 case MAT_FILE_UINT16_CLASS: |
5089
|
1154 OCTAVE_MAT5_INTEGER_READ (uint16NDArray); |
|
1155 break; |
|
1156 |
5900
|
1157 case MAT_FILE_INT32_CLASS: |
5089
|
1158 OCTAVE_MAT5_INTEGER_READ (int32NDArray); |
|
1159 break; |
|
1160 |
5900
|
1161 case MAT_FILE_UINT32_CLASS: |
5089
|
1162 OCTAVE_MAT5_INTEGER_READ (uint32NDArray); |
|
1163 break; |
|
1164 |
5900
|
1165 case MAT_FILE_INT64_CLASS: |
5089
|
1166 OCTAVE_MAT5_INTEGER_READ (int64NDArray); |
|
1167 break; |
|
1168 |
5900
|
1169 case MAT_FILE_UINT64_CLASS: |
5089
|
1170 OCTAVE_MAT5_INTEGER_READ (uint64NDArray); |
|
1171 break; |
|
1172 |
5900
|
1173 case MAT_FILE_CHAR_CLASS: |
4634
|
1174 // handle as a numerical array to start with |
|
1175 |
5900
|
1176 case MAT_FILE_DOUBLE_CLASS: |
|
1177 case MAT_FILE_SINGLE_CLASS: |
4634
|
1178 default: |
5089
|
1179 { |
|
1180 NDArray re (dims); |
4634
|
1181 |
5089
|
1182 // real data subelement |
|
1183 |
4634
|
1184 std::streampos tmp_pos; |
5089
|
1185 |
4634
|
1186 if (read_mat5_tag (is, swap, type, len)) |
|
1187 { |
|
1188 error ("load: reading matrix data for `%s'", retval.c_str ()); |
|
1189 goto data_read_error; |
|
1190 } |
|
1191 |
|
1192 int n = re.length (); |
|
1193 tmp_pos = is.tellg (); |
|
1194 read_mat5_binary_data (is, re.fortran_vec (), n, swap, |
5760
|
1195 static_cast<enum mat5_data_type> (type), flt_fmt); |
4634
|
1196 |
|
1197 if (! is || error_state) |
|
1198 { |
|
1199 error ("load: reading matrix data for `%s'", retval.c_str ()); |
|
1200 goto data_read_error; |
|
1201 } |
|
1202 |
|
1203 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
5089
|
1204 |
5269
|
1205 if (logicalvar) |
5089
|
1206 { |
5900
|
1207 // Logical variables can either be MAT_FILE_UINT8_CLASS or |
|
1208 // MAT_FILE_DOUBLE_CLASS, so check if we have a logical |
|
1209 // variable and convert it. |
5269
|
1210 |
|
1211 boolNDArray out (dims); |
|
1212 |
|
1213 for (int i = 0; i < n; i++) |
|
1214 out (i) = static_cast<bool> (re (i)); |
|
1215 |
|
1216 tc = out; |
|
1217 } |
|
1218 else if (imag) |
|
1219 { |
|
1220 // imaginary data subelement |
|
1221 |
5089
|
1222 NDArray im (dims); |
4634
|
1223 |
5089
|
1224 if (read_mat5_tag (is, swap, type, len)) |
|
1225 { |
|
1226 error ("load: reading matrix data for `%s'", retval.c_str ()); |
|
1227 goto data_read_error; |
|
1228 } |
4634
|
1229 |
5089
|
1230 n = im.length (); |
|
1231 read_mat5_binary_data (is, im.fortran_vec (), n, swap, |
5760
|
1232 static_cast<enum mat5_data_type> (type), flt_fmt); |
4634
|
1233 |
5089
|
1234 if (! is || error_state) |
|
1235 { |
|
1236 error ("load: reading imaginary matrix data for `%s'", |
|
1237 retval.c_str ()); |
|
1238 goto data_read_error; |
|
1239 } |
4634
|
1240 |
5089
|
1241 ComplexNDArray ctmp (dims); |
4634
|
1242 |
5089
|
1243 for (int i = 0; i < n; i++) |
|
1244 ctmp(i) = Complex (re(i), im(i)); |
4634
|
1245 |
5089
|
1246 tc = ctmp; |
|
1247 } |
|
1248 else |
5269
|
1249 { |
5900
|
1250 if (arrayclass == MAT_FILE_CHAR_CLASS) |
5351
|
1251 { |
|
1252 if (type == miUTF16 || type == miUTF32) |
|
1253 { |
|
1254 error ("load: can not read Unicode UTF16 and UTF32 encoded characters"); |
|
1255 goto data_read_error; |
|
1256 } |
|
1257 else if (type == miUTF8) |
|
1258 { |
|
1259 // Search for multi-byte encoded UTF8 characters and |
|
1260 // replace with 0x3F for '?'... Give the user a warning |
4634
|
1261 |
5351
|
1262 bool utf8_multi_byte = false; |
|
1263 for (int i = 0; i < n; i++) |
|
1264 { |
5352
|
1265 unsigned char a = static_cast<unsigned char> (re(i)); |
5351
|
1266 if (a > 0x7f) |
|
1267 utf8_multi_byte = true; |
|
1268 } |
|
1269 |
|
1270 if (utf8_multi_byte) |
|
1271 { |
|
1272 warning ("load: can not read multi-byte encoded UTF8 characters."); |
|
1273 warning (" Replacing unreadable characters with '?'."); |
|
1274 for (int i = 0; i < n; i++) |
|
1275 { |
5352
|
1276 unsigned char a = static_cast<unsigned char> (re(i)); |
5351
|
1277 if (a > 0x7f) |
5352
|
1278 re(i) = '?'; |
5351
|
1279 } |
|
1280 } |
|
1281 } |
|
1282 tc = re; |
|
1283 tc = tc.convert_to_str (false, true, '\''); |
|
1284 } |
|
1285 else |
|
1286 tc = re; |
5269
|
1287 } |
5089
|
1288 } |
4634
|
1289 } |
|
1290 |
|
1291 is.seekg (pos + static_cast<std::streamoff> (element_length)); |
|
1292 |
|
1293 if (is.eof ()) |
|
1294 is.clear (); |
|
1295 |
|
1296 return retval; |
|
1297 |
|
1298 data_read_error: |
|
1299 early_read_error: |
|
1300 error ("load: trouble reading binary file `%s'", filename.c_str ()); |
|
1301 return std::string (); |
|
1302 |
|
1303 skip_ahead: |
|
1304 warning ("skipping over `%s'", retval.c_str ()); |
|
1305 is.seekg (pos + static_cast<std::streamoff> (element_length)); |
|
1306 return read_mat5_binary_element (is, filename, swap, global, tc); |
|
1307 } |
|
1308 |
|
1309 int |
6625
|
1310 read_mat5_binary_file_header (std::istream& is, bool& swap, bool quiet, |
|
1311 const std::string& filename) |
4634
|
1312 { |
5828
|
1313 int16_t version=0, magic=0; |
6625
|
1314 uint64_t subsys_offset; |
|
1315 |
|
1316 is.seekg (116, std::ios::beg); |
|
1317 is.read (reinterpret_cast<char *> (&subsys_offset), 8); |
4634
|
1318 |
|
1319 is.seekg (124, std::ios::beg); |
5760
|
1320 is.read (reinterpret_cast<char *> (&version), 2); |
|
1321 is.read (reinterpret_cast<char *> (&magic), 2); |
4634
|
1322 |
|
1323 if (magic == 0x4d49) |
|
1324 swap = 0; |
|
1325 else if (magic == 0x494d) |
|
1326 swap = 1; |
|
1327 else |
|
1328 { |
|
1329 if (! quiet) |
|
1330 error ("load: can't read binary file"); |
|
1331 return -1; |
|
1332 } |
|
1333 |
|
1334 if (! swap) // version number is inverse swapped! |
|
1335 version = ((version >> 8) & 0xff) + ((version & 0xff) << 8); |
|
1336 |
|
1337 if (version != 1 && !quiet) |
|
1338 warning ("load: found version %d binary MAT file, " |
|
1339 "but only prepared for version 1", version); |
|
1340 |
6625
|
1341 if (swap) |
|
1342 swap_bytes<8> (&subsys_offset, 1); |
|
1343 |
|
1344 if (subsys_offset != 0x2020202020202020ULL && subsys_offset != 0ULL) |
|
1345 { |
|
1346 // Read the subsystem data block |
|
1347 is.seekg (subsys_offset, std::ios::beg); |
|
1348 |
|
1349 octave_value tc; |
|
1350 bool global; |
|
1351 read_mat5_binary_element (is, filename, swap, global, tc); |
|
1352 |
|
1353 if (!is || error_state) |
|
1354 return -1; |
|
1355 |
|
1356 if (tc.is_uint8_type ()) |
|
1357 { |
|
1358 const uint8NDArray itmp = tc.uint8_array_value(); |
|
1359 octave_idx_type ilen = itmp.nelem (); |
|
1360 |
|
1361 // Why should I have to initialize outbuf as just overwrite |
|
1362 std::string outbuf (ilen - 7, ' '); |
|
1363 |
|
1364 // FIXME -- find a way to avoid casting away const here |
|
1365 char *ctmp = const_cast<char *> (outbuf.c_str ()); |
|
1366 for (octave_idx_type j = 8; j < ilen; j++) |
|
1367 ctmp [j - 8] = itmp (j); |
|
1368 |
|
1369 std::istringstream fh_ws (outbuf); |
|
1370 |
|
1371 read_mat5_binary_element (fh_ws, filename, swap, global, subsys_ov); |
|
1372 |
|
1373 if (error_state) |
|
1374 return -1; |
|
1375 } |
|
1376 else |
|
1377 return -1; |
|
1378 |
|
1379 // Reposition to just after the header |
|
1380 is.seekg (128, std::ios::beg); |
|
1381 } |
|
1382 |
4634
|
1383 return 0; |
|
1384 } |
|
1385 |
|
1386 static int |
|
1387 write_mat5_tag (std::ostream& is, int type, int bytes) |
|
1388 { |
5828
|
1389 int32_t temp; |
4634
|
1390 |
6292
|
1391 if (bytes > 0 && bytes <= 4) |
4634
|
1392 temp = (bytes << 16) + type; |
|
1393 else |
|
1394 { |
|
1395 temp = type; |
5760
|
1396 if (! is.write (reinterpret_cast<char *> (&temp), 4)) |
4634
|
1397 goto data_write_error; |
|
1398 temp = bytes; |
|
1399 } |
|
1400 |
5760
|
1401 if (! is.write (reinterpret_cast<char *> (&temp), 4)) |
4634
|
1402 goto data_write_error; |
|
1403 |
|
1404 return 0; |
|
1405 |
|
1406 data_write_error: |
|
1407 return 1; |
|
1408 } |
|
1409 |
|
1410 // write out the numeric values in M to OS, |
|
1411 // preceded by the appropriate tag. |
|
1412 static void |
|
1413 write_mat5_array (std::ostream& os, const NDArray& m, bool save_as_floats) |
|
1414 { |
|
1415 int nel = m.nelem (); |
|
1416 double max_val, min_val; |
|
1417 save_type st = LS_DOUBLE; |
|
1418 mat5_data_type mst; |
|
1419 int size; |
|
1420 unsigned len; |
|
1421 const double *data = m.data (); |
|
1422 |
|
1423 // Have to use copy here to avoid writing over data accessed via |
|
1424 // Matrix::data(). |
|
1425 |
5760
|
1426 #define MAT5_DO_WRITE(TYPE, data, count, stream) \ |
|
1427 do \ |
|
1428 { \ |
|
1429 OCTAVE_LOCAL_BUFFER (TYPE, ptr, count); \ |
|
1430 for (int i = 0; i < count; i++) \ |
|
1431 ptr[i] = static_cast<TYPE> (data[i]); \ |
|
1432 stream.write (reinterpret_cast<char *> (ptr), count * sizeof (TYPE)); \ |
|
1433 } \ |
4634
|
1434 while (0) |
|
1435 |
|
1436 if (save_as_floats) |
|
1437 { |
|
1438 if (m.too_large_for_float ()) |
|
1439 { |
|
1440 warning ("save: some values too large to save as floats --"); |
|
1441 warning ("save: saving as doubles instead"); |
|
1442 } |
|
1443 else |
|
1444 st = LS_FLOAT; |
|
1445 } |
|
1446 |
|
1447 if (m.all_integers (max_val, min_val)) |
|
1448 st = get_save_type (max_val, min_val); |
|
1449 |
|
1450 switch (st) |
|
1451 { |
|
1452 default: |
|
1453 case LS_DOUBLE: mst = miDOUBLE; size = 8; break; |
|
1454 case LS_FLOAT: mst = miSINGLE; size = 4; break; |
|
1455 case LS_U_CHAR: mst = miUINT8; size = 1; break; |
|
1456 case LS_U_SHORT: mst = miUINT16; size = 2; break; |
|
1457 case LS_U_INT: mst = miUINT32; size = 4; break; |
|
1458 case LS_CHAR: mst = miINT8; size = 1; break; |
|
1459 case LS_SHORT: mst = miINT16; size = 2; break; |
|
1460 case LS_INT: mst = miINT32; size = 4; break; |
|
1461 } |
|
1462 |
|
1463 len = nel*size; |
|
1464 write_mat5_tag (os, mst, len); |
|
1465 |
|
1466 { |
|
1467 switch (st) |
|
1468 { |
|
1469 case LS_U_CHAR: |
5828
|
1470 MAT5_DO_WRITE (uint8_t, data, nel, os); |
4634
|
1471 break; |
|
1472 |
|
1473 case LS_U_SHORT: |
5828
|
1474 MAT5_DO_WRITE (uint16_t, data, nel, os); |
4634
|
1475 break; |
|
1476 |
|
1477 case LS_U_INT: |
5828
|
1478 MAT5_DO_WRITE (uint32_t, data, nel, os); |
4634
|
1479 break; |
|
1480 |
|
1481 case LS_U_LONG: |
5828
|
1482 MAT5_DO_WRITE (uint64_t, data, nel, os); |
4634
|
1483 break; |
|
1484 |
|
1485 case LS_CHAR: |
5828
|
1486 MAT5_DO_WRITE (int8_t, data, nel, os); |
4634
|
1487 break; |
|
1488 |
|
1489 case LS_SHORT: |
5828
|
1490 MAT5_DO_WRITE (int16_t, data, nel, os); |
4634
|
1491 break; |
|
1492 |
|
1493 case LS_INT: |
5828
|
1494 MAT5_DO_WRITE (int32_t, data, nel, os); |
4634
|
1495 break; |
|
1496 |
|
1497 case LS_LONG: |
5828
|
1498 MAT5_DO_WRITE (int64_t, data, nel, os); |
4634
|
1499 break; |
|
1500 |
|
1501 case LS_FLOAT: |
|
1502 MAT5_DO_WRITE (float, data, nel, os); |
|
1503 break; |
|
1504 |
|
1505 case LS_DOUBLE: // No conversion necessary. |
5760
|
1506 os.write (reinterpret_cast<const char *> (data), len); |
4634
|
1507 break; |
|
1508 |
|
1509 default: |
|
1510 (*current_liboctave_error_handler) |
|
1511 ("unrecognized data format requested"); |
|
1512 break; |
|
1513 } |
|
1514 } |
|
1515 if (PAD (len) > len) |
|
1516 { |
|
1517 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
1518 os.write (buf, PAD (len) - len); |
|
1519 } |
|
1520 } |
|
1521 |
5089
|
1522 template <class T> |
|
1523 void |
5164
|
1524 write_mat5_integer_data (std::ostream& os, const T *m, int size, int nel) |
5089
|
1525 { |
|
1526 mat5_data_type mst; |
|
1527 unsigned len; |
|
1528 |
|
1529 switch (size) |
|
1530 { |
|
1531 case 1: |
|
1532 mst = miUINT8; |
|
1533 break; |
|
1534 case 2: |
|
1535 mst = miUINT16; |
|
1536 break; |
5164
|
1537 case 4: |
5089
|
1538 mst = miUINT32; |
|
1539 break; |
5164
|
1540 case 8: |
5089
|
1541 mst = miUINT64; |
|
1542 break; |
|
1543 case -1: |
|
1544 mst = miINT8; |
|
1545 size = - size; |
|
1546 break; |
|
1547 case -2: |
|
1548 mst = miINT16; |
|
1549 size = - size; |
|
1550 break; |
5164
|
1551 case -4: |
5089
|
1552 mst = miINT32; |
|
1553 size = - size; |
|
1554 break; |
5164
|
1555 case -8: |
5089
|
1556 default: |
|
1557 mst = miINT64; |
|
1558 size = - size; |
|
1559 break; |
|
1560 } |
|
1561 |
|
1562 len = nel*size; |
|
1563 write_mat5_tag (os, mst, len); |
|
1564 |
5760
|
1565 os.write (reinterpret_cast<const char *> (m), len); |
5089
|
1566 |
|
1567 if (PAD (len) > len) |
|
1568 { |
|
1569 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
1570 os.write (buf, PAD (len) - len); |
|
1571 } |
|
1572 } |
|
1573 |
5164
|
1574 template void write_mat5_integer_data (std::ostream& os, const octave_int8 *m, |
|
1575 int size, int nel); |
|
1576 template void write_mat5_integer_data (std::ostream& os, const octave_int16 *m, |
|
1577 int size, int nel); |
|
1578 template void write_mat5_integer_data (std::ostream& os, const octave_int32 *m, |
|
1579 int size, int nel); |
|
1580 template void write_mat5_integer_data (std::ostream& os, const octave_int64 *m, |
|
1581 int size, int nel); |
|
1582 template void write_mat5_integer_data (std::ostream& os, const octave_uint8 *m, |
|
1583 int size, int nel); |
|
1584 template void write_mat5_integer_data (std::ostream& os, const octave_uint16 *m, |
|
1585 int size, int nel); |
|
1586 template void write_mat5_integer_data (std::ostream& os, const octave_uint32 *m, |
|
1587 int size, int nel); |
|
1588 template void write_mat5_integer_data (std::ostream& os, const octave_uint64 *m, |
|
1589 int size, int nel); |
|
1590 template void write_mat5_integer_data (std::ostream& os, const int *m, |
|
1591 int size, int nel); |
5089
|
1592 |
4634
|
1593 // Write out cell element values in the cell array to OS, preceded by |
|
1594 // the appropriate tag. |
|
1595 |
|
1596 static bool |
4701
|
1597 write_mat5_cell_array (std::ostream& os, const Cell& cell, |
|
1598 bool mark_as_global, bool save_as_floats) |
4634
|
1599 { |
|
1600 int nel = cell.nelem (); |
|
1601 |
|
1602 for (int i = 0; i < nel; i++) |
|
1603 { |
|
1604 octave_value ov = cell(i); |
|
1605 |
|
1606 if (! save_mat5_binary_element (os, ov, "", mark_as_global, |
5269
|
1607 false, save_as_floats)) |
4634
|
1608 return false; |
|
1609 } |
|
1610 |
|
1611 return true; |
|
1612 } |
|
1613 |
5269
|
1614 int |
|
1615 save_mat5_array_length (const double* val, int nel, bool save_as_floats) |
|
1616 { |
|
1617 if (nel > 0) |
|
1618 { |
|
1619 int size = 8; |
|
1620 |
|
1621 if (save_as_floats) |
|
1622 { |
|
1623 bool too_large_for_float = false; |
|
1624 for (int i = 0; i < nel; i++) |
|
1625 { |
|
1626 double tmp = val [i]; |
|
1627 |
5389
|
1628 if (! (xisnan (tmp) || xisinf (tmp)) |
5388
|
1629 && fabs (tmp) > FLT_MAX) |
5269
|
1630 { |
|
1631 too_large_for_float = true; |
|
1632 break; |
|
1633 } |
|
1634 } |
|
1635 |
|
1636 if (!too_large_for_float) |
|
1637 size = 4; |
|
1638 } |
|
1639 |
|
1640 // The code below is disabled since get_save_type currently doesn't |
|
1641 // deal with integer types. This will need to be activated if get_save_type |
|
1642 // is changed. |
|
1643 |
|
1644 // double max_val = val[0]; |
|
1645 // double min_val = val[0]; |
|
1646 // bool all_integers = true; |
|
1647 // |
|
1648 // for (int i = 0; i < nel; i++) |
|
1649 // { |
|
1650 // double val = val[i]; |
|
1651 // |
|
1652 // if (val > max_val) |
|
1653 // max_val = val; |
|
1654 // |
|
1655 // if (val < min_val) |
|
1656 // min_val = val; |
|
1657 // |
|
1658 // if (D_NINT (val) != val) |
|
1659 // { |
|
1660 // all_integers = false; |
|
1661 // break; |
|
1662 // } |
|
1663 // } |
|
1664 // |
|
1665 // if (all_integers) |
|
1666 // { |
|
1667 // if (max_val < 256 && min_val > -1) |
|
1668 // size = 1; |
|
1669 // else if (max_val < 65536 && min_val > -1) |
|
1670 // size = 2; |
|
1671 // else if (max_val < 4294967295UL && min_val > -1) |
|
1672 // size = 4; |
|
1673 // else if (max_val < 128 && min_val >= -128) |
|
1674 // size = 1; |
|
1675 // else if (max_val < 32768 && min_val >= -32768) |
|
1676 // size = 2; |
|
1677 // else if (max_val <= 2147483647L && min_val >= -2147483647L) |
|
1678 // size = 4; |
|
1679 // } |
|
1680 |
|
1681 return 8 + nel * size; |
|
1682 } |
|
1683 else |
|
1684 return 8; |
|
1685 } |
|
1686 |
|
1687 int |
|
1688 save_mat5_array_length (const Complex* val, int nel, bool save_as_floats) |
|
1689 { |
|
1690 int ret; |
|
1691 |
|
1692 OCTAVE_LOCAL_BUFFER (double, tmp, nel); |
|
1693 |
|
1694 for (int i = 1; i < nel; i++) |
|
1695 tmp[i] = std::real (val[i]); |
|
1696 |
|
1697 ret = save_mat5_array_length (tmp, nel, save_as_floats); |
|
1698 |
|
1699 for (int i = 1; i < nel; i++) |
|
1700 tmp[i] = std::imag (val[i]); |
|
1701 |
|
1702 ret += save_mat5_array_length (tmp, nel, save_as_floats); |
|
1703 |
|
1704 return ret; |
|
1705 } |
|
1706 |
|
1707 int |
|
1708 save_mat5_element_length (const octave_value& tc, const std::string& name, |
|
1709 bool save_as_floats, bool mat7_format) |
|
1710 { |
|
1711 int max_namelen = (mat7_format ? 63 : 31); |
|
1712 int len = name.length (); |
|
1713 std::string cname = tc.class_name (); |
|
1714 int ret = 32; |
|
1715 |
|
1716 if (len > 4) |
|
1717 ret += PAD (len > max_namelen ? max_namelen : len); |
|
1718 |
|
1719 ret += PAD (4 * tc.ndims ()); |
|
1720 |
|
1721 if (tc.is_string ()) |
|
1722 { |
5933
|
1723 charNDArray chm = tc.char_array_value (); |
5384
|
1724 ret += 8; |
5933
|
1725 if (chm.nelem () > 2) |
|
1726 ret += PAD (2 * chm.nelem ()); |
5269
|
1727 } |
|
1728 else if (cname == "sparse") |
|
1729 { |
|
1730 if (tc.is_complex_type ()) |
|
1731 { |
|
1732 SparseComplexMatrix m = tc.sparse_complex_matrix_value (); |
|
1733 int nc = m.cols (); |
5604
|
1734 int nnz = m.nzmax (); |
5269
|
1735 |
|
1736 ret += 16 + PAD (nnz * sizeof (int)) + PAD ((nc + 1) * sizeof (int)) + |
|
1737 save_mat5_array_length (m.data (), m.nelem (), save_as_floats); |
|
1738 } |
|
1739 else |
|
1740 { |
|
1741 SparseMatrix m = tc.sparse_matrix_value (); |
|
1742 int nc = m.cols (); |
5604
|
1743 int nnz = m.nzmax (); |
5269
|
1744 |
|
1745 ret += 16 + PAD (nnz * sizeof (int)) + PAD ((nc + 1) * sizeof (int)) + |
|
1746 save_mat5_array_length (m.data (), m.nelem (), save_as_floats); |
|
1747 } |
|
1748 } |
|
1749 |
|
1750 #define INT_LEN(nel, size) \ |
|
1751 { \ |
|
1752 ret += 8; \ |
|
1753 int sz = nel * size; \ |
|
1754 if (sz > 4) \ |
|
1755 ret += PAD (sz); \ |
|
1756 } |
|
1757 |
|
1758 else if (cname == "int8") |
|
1759 INT_LEN (tc.int8_array_value ().nelem (), 1) |
|
1760 else if (cname == "int16") |
|
1761 INT_LEN (tc.int16_array_value ().nelem (), 2) |
|
1762 else if (cname == "int32") |
|
1763 INT_LEN (tc.int32_array_value ().nelem (), 4) |
|
1764 else if (cname == "int64") |
|
1765 INT_LEN (tc.int64_array_value ().nelem (), 8) |
|
1766 else if (cname == "uint8") |
|
1767 INT_LEN (tc.uint8_array_value ().nelem (), 1) |
|
1768 else if (cname == "uint16") |
|
1769 INT_LEN (tc.uint16_array_value ().nelem (), 2) |
|
1770 else if (cname == "uint32") |
|
1771 INT_LEN (tc.uint32_array_value ().nelem (), 4) |
|
1772 else if (cname == "uint64") |
|
1773 INT_LEN (tc.uint64_array_value ().nelem (), 8) |
|
1774 else if (tc.is_bool_type ()) |
|
1775 INT_LEN (tc.bool_array_value ().nelem (), 1) |
|
1776 else if (tc.is_real_scalar () || tc.is_real_matrix () || tc.is_range ()) |
|
1777 { |
|
1778 NDArray m = tc.array_value (); |
|
1779 ret += save_mat5_array_length (m.fortran_vec (), m.nelem (), |
|
1780 save_as_floats); |
|
1781 } |
|
1782 else if (tc.is_cell ()) |
|
1783 { |
|
1784 Cell cell = tc.cell_value (); |
|
1785 int nel = cell.nelem (); |
|
1786 |
|
1787 for (int i = 0; i < nel; i++) |
|
1788 ret += 8 + |
|
1789 save_mat5_element_length (cell (i), "", save_as_floats, mat7_format); |
|
1790 } |
|
1791 else if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
1792 { |
|
1793 ComplexNDArray m = tc.complex_array_value (); |
|
1794 ret += save_mat5_array_length (m.fortran_vec (), m.nelem (), |
|
1795 save_as_floats); |
|
1796 } |
6625
|
1797 else if (tc.is_map () || tc.is_inline_function ()) |
5269
|
1798 { |
|
1799 int fieldcnt = 0; |
|
1800 const Octave_map m = tc.map_value (); |
|
1801 int nel = m.numel (); |
|
1802 |
6625
|
1803 if (tc.is_inline_function ()) |
|
1804 // length of "inline" is 6 |
|
1805 ret += 8 + PAD (6 > max_namelen ? max_namelen : 6); |
|
1806 |
5269
|
1807 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
|
1808 fieldcnt++; |
|
1809 |
|
1810 ret += 16 + fieldcnt * (max_namelen + 1); |
|
1811 |
|
1812 |
|
1813 for (int j = 0; j < nel; j++) |
|
1814 { |
|
1815 |
|
1816 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
|
1817 { |
|
1818 Cell elts = m.contents (i); |
|
1819 |
|
1820 ret += 8 + save_mat5_element_length (elts (j), "", |
|
1821 save_as_floats, mat7_format); |
|
1822 } |
|
1823 } |
|
1824 } |
|
1825 else |
|
1826 ret = -1; |
|
1827 |
|
1828 return ret; |
|
1829 } |
|
1830 |
4634
|
1831 // save the data from TC along with the corresponding NAME on stream |
|
1832 // OS in the MatLab version 5 binary format. Return true on success. |
|
1833 |
|
1834 bool |
|
1835 save_mat5_binary_element (std::ostream& os, |
|
1836 const octave_value& tc, const std::string& name, |
5269
|
1837 bool mark_as_global, bool mat7_format, |
|
1838 bool save_as_floats, bool compressing) |
4634
|
1839 { |
5828
|
1840 int32_t flags=0; |
|
1841 int32_t nnz=0; |
4634
|
1842 std::streampos fixup, contin; |
5089
|
1843 std::string cname = tc.class_name (); |
5269
|
1844 int max_namelen = (mat7_format ? 63 : 31); |
|
1845 |
|
1846 #ifdef HAVE_ZLIB |
|
1847 if (mat7_format && !compressing) |
|
1848 { |
|
1849 bool ret = false; |
|
1850 |
5765
|
1851 std::ostringstream buf; |
5269
|
1852 |
|
1853 // The code seeks backwards in the stream to fix the header. Can't |
|
1854 // do this with zlib, so use a stringstream. |
|
1855 ret = save_mat5_binary_element (buf, tc, name, mark_as_global, true, |
|
1856 save_as_floats, true); |
|
1857 |
|
1858 if (ret) |
|
1859 { |
5351
|
1860 // destLen must be at least 0.1% larger than source buffer |
|
1861 // + 12 bytes. Reality is it must be larger again than that. |
5765
|
1862 std::string buf_str = buf.str (); |
|
1863 uLongf srcLen = buf_str.length (); |
5351
|
1864 uLongf destLen = srcLen * 101 / 100 + 12; |
5269
|
1865 OCTAVE_LOCAL_BUFFER (char, out_buf, destLen); |
|
1866 |
5760
|
1867 if (compress (reinterpret_cast<Bytef *> (out_buf), &destLen, |
5765
|
1868 reinterpret_cast<const Bytef *> (buf_str.c_str ()), srcLen) == Z_OK) |
5269
|
1869 { |
5760
|
1870 write_mat5_tag (os, miCOMPRESSED, static_cast<int> (destLen)); |
5269
|
1871 os.write (out_buf, destLen); |
|
1872 } |
|
1873 else |
|
1874 { |
|
1875 error ("save: error compressing data element"); |
|
1876 ret = false; |
|
1877 } |
|
1878 } |
|
1879 |
|
1880 return ret; |
|
1881 } |
|
1882 #endif |
4634
|
1883 |
|
1884 // element type and length |
|
1885 fixup = os.tellp (); |
5269
|
1886 write_mat5_tag (os, miMATRIX, save_mat5_element_length |
|
1887 (tc, name, save_as_floats, mat7_format)); |
4634
|
1888 |
|
1889 // array flags subelement |
|
1890 write_mat5_tag (os, miUINT32, 8); |
|
1891 |
5269
|
1892 if (tc.is_bool_type ()) |
|
1893 flags |= 0x0200; |
|
1894 |
4634
|
1895 if (mark_as_global) |
|
1896 flags |= 0x0400; |
|
1897 |
|
1898 if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
1899 flags |= 0x0800; |
|
1900 |
|
1901 if (tc.is_string ()) |
5900
|
1902 flags |= MAT_FILE_CHAR_CLASS; |
5089
|
1903 else if (cname == "int8") |
5900
|
1904 flags |= MAT_FILE_INT8_CLASS; |
5089
|
1905 else if (cname == "int16") |
5900
|
1906 flags |= MAT_FILE_INT16_CLASS; |
5089
|
1907 else if (cname == "int32") |
5900
|
1908 flags |= MAT_FILE_INT32_CLASS; |
5089
|
1909 else if (cname == "int64") |
5900
|
1910 flags |= MAT_FILE_INT64_CLASS; |
5269
|
1911 else if (cname == "uint8" || tc.is_bool_type ()) |
5900
|
1912 flags |= MAT_FILE_UINT8_CLASS; |
5089
|
1913 else if (cname == "uint16") |
5900
|
1914 flags |= MAT_FILE_UINT16_CLASS; |
5089
|
1915 else if (cname == "uint32") |
5900
|
1916 flags |= MAT_FILE_UINT32_CLASS; |
5089
|
1917 else if (cname == "uint64") |
5900
|
1918 flags |= MAT_FILE_UINT64_CLASS; |
5164
|
1919 else if (cname == "sparse") |
|
1920 { |
5900
|
1921 flags |= MAT_FILE_SPARSE_CLASS; |
5164
|
1922 if (tc.is_complex_type ()) |
|
1923 { |
|
1924 SparseComplexMatrix scm = tc.sparse_complex_matrix_value (); |
5604
|
1925 nnz = scm.nzmax (); |
5164
|
1926 } |
|
1927 else |
|
1928 { |
|
1929 SparseMatrix sm = tc.sparse_matrix_value (); |
5604
|
1930 nnz = sm.nzmax (); |
5164
|
1931 } |
|
1932 } |
4634
|
1933 else if (tc.is_real_scalar ()) |
5900
|
1934 flags |= MAT_FILE_DOUBLE_CLASS; |
4634
|
1935 else if (tc.is_real_matrix () || tc.is_range ()) |
5900
|
1936 flags |= MAT_FILE_DOUBLE_CLASS; |
4634
|
1937 else if (tc.is_complex_scalar ()) |
5900
|
1938 flags |= MAT_FILE_DOUBLE_CLASS; |
4634
|
1939 else if (tc.is_complex_matrix ()) |
5900
|
1940 flags |= MAT_FILE_DOUBLE_CLASS; |
4634
|
1941 else if (tc.is_map ()) |
5900
|
1942 flags |= MAT_FILE_STRUCT_CLASS; |
4634
|
1943 else if (tc.is_cell ()) |
5900
|
1944 flags |= MAT_FILE_CELL_CLASS; |
6625
|
1945 else if (tc.is_inline_function ()) |
|
1946 flags |= MAT_FILE_OBJECT_CLASS; |
4634
|
1947 else |
|
1948 { |
|
1949 gripe_wrong_type_arg ("save", tc, false); |
|
1950 goto error_cleanup; |
|
1951 } |
|
1952 |
5760
|
1953 os.write (reinterpret_cast<char *> (&flags), 4); |
|
1954 os.write (reinterpret_cast<char *> (&nnz), 4); |
4634
|
1955 |
|
1956 { |
|
1957 dim_vector dv = tc.dims (); |
|
1958 int nd = tc.ndims (); |
4638
|
1959 int dim_len = 4*nd; |
4634
|
1960 |
4638
|
1961 write_mat5_tag (os, miINT32, dim_len); |
4634
|
1962 |
|
1963 for (int i = 0; i < nd; i++) |
|
1964 { |
5828
|
1965 int32_t n = dv(i); |
5760
|
1966 os.write (reinterpret_cast<char *> (&n), 4); |
4634
|
1967 } |
4638
|
1968 |
|
1969 if (PAD (dim_len) > dim_len) |
|
1970 { |
|
1971 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
1972 os.write (buf, PAD (dim_len) - dim_len); |
|
1973 } |
4634
|
1974 } |
|
1975 |
|
1976 // array name subelement |
|
1977 { |
|
1978 int namelen = name.length (); |
|
1979 |
5269
|
1980 if (namelen > max_namelen) |
|
1981 namelen = max_namelen; // only 31 or 63 char names permitted in mat file |
4634
|
1982 |
|
1983 int paddedlength = PAD (namelen); |
|
1984 |
|
1985 write_mat5_tag (os, miINT8, namelen); |
|
1986 OCTAVE_LOCAL_BUFFER (char, paddedname, paddedlength); |
|
1987 memset (paddedname, 0, paddedlength); |
|
1988 strncpy (paddedname, name.c_str (), namelen); |
|
1989 os.write (paddedname, paddedlength); |
|
1990 } |
|
1991 |
|
1992 // data element |
|
1993 if (tc.is_string ()) |
|
1994 { |
5933
|
1995 charNDArray chm = tc.char_array_value (); |
|
1996 int nel = chm.nelem (); |
|
1997 int len = nel*2; |
|
1998 int paddedlength = PAD (len); |
4634
|
1999 |
5933
|
2000 OCTAVE_LOCAL_BUFFER (int16_t, buf, nel+3); |
4634
|
2001 write_mat5_tag (os, miUINT16, len); |
|
2002 |
5933
|
2003 const char *s = chm.data (); |
4634
|
2004 |
5933
|
2005 for (int i = 0; i < nel; i++) |
|
2006 buf[i] = *s++ & 0x00FF; |
|
2007 |
|
2008 os.write (reinterpret_cast<char *> (buf), len); |
4634
|
2009 |
|
2010 if (paddedlength > len) |
5933
|
2011 { |
|
2012 static char padbuf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
2013 os.write (padbuf, paddedlength - len); |
|
2014 } |
4634
|
2015 } |
5164
|
2016 else if (cname == "sparse") |
|
2017 { |
|
2018 if (tc.is_complex_type ()) |
|
2019 { |
|
2020 SparseComplexMatrix m = tc.sparse_complex_matrix_value (); |
|
2021 int nc = m.cols (); |
|
2022 |
5941
|
2023 int tmp = sizeof (int); |
|
2024 |
|
2025 write_mat5_integer_data (os, m.ridx (), -tmp, nnz); |
|
2026 write_mat5_integer_data (os, m.cidx (), -tmp, nc + 1); |
5164
|
2027 |
|
2028 NDArray buf (dim_vector (nnz, 1)); |
|
2029 |
|
2030 for (int i = 0; i < nnz; i++) |
5261
|
2031 buf (i) = std::real (m.data (i)); |
5164
|
2032 |
|
2033 write_mat5_array (os, buf, save_as_floats); |
|
2034 |
|
2035 for (int i = 0; i < nnz; i++) |
5261
|
2036 buf (i) = std::imag (m.data (i)); |
5164
|
2037 |
|
2038 write_mat5_array (os, buf, save_as_floats); |
|
2039 } |
|
2040 else |
|
2041 { |
|
2042 SparseMatrix m = tc.sparse_matrix_value (); |
|
2043 int nc = m.cols (); |
|
2044 |
5941
|
2045 int tmp = sizeof (int); |
|
2046 |
|
2047 write_mat5_integer_data (os, m.ridx (), -tmp, nnz); |
|
2048 write_mat5_integer_data (os, m.cidx (), -tmp, nc + 1); |
5164
|
2049 |
5775
|
2050 // FIXME |
5164
|
2051 // Is there a way to easily do without this buffer |
|
2052 NDArray buf (dim_vector (nnz, 1)); |
|
2053 |
|
2054 for (int i = 0; i < nnz; i++) |
|
2055 buf (i) = m.data (i); |
|
2056 |
|
2057 write_mat5_array (os, buf, save_as_floats); |
|
2058 } |
|
2059 } |
5089
|
2060 else if (cname == "int8") |
|
2061 { |
|
2062 int8NDArray m = tc.int8_array_value (); |
|
2063 |
5164
|
2064 write_mat5_integer_data (os, m.fortran_vec (), -1, m.nelem ()); |
5089
|
2065 } |
|
2066 else if (cname == "int16") |
|
2067 { |
|
2068 int16NDArray m = tc.int16_array_value (); |
|
2069 |
5164
|
2070 write_mat5_integer_data (os, m.fortran_vec (), -2, m.nelem ()); |
5089
|
2071 } |
|
2072 else if (cname == "int32") |
|
2073 { |
|
2074 int32NDArray m = tc.int32_array_value (); |
|
2075 |
5164
|
2076 write_mat5_integer_data (os, m.fortran_vec (), -4, m.nelem ()); |
5089
|
2077 } |
|
2078 else if (cname == "int64") |
|
2079 { |
|
2080 int64NDArray m = tc.int64_array_value (); |
|
2081 |
5164
|
2082 write_mat5_integer_data (os, m.fortran_vec (), -8, m.nelem ()); |
5089
|
2083 } |
|
2084 else if (cname == "uint8") |
|
2085 { |
|
2086 uint8NDArray m = tc.uint8_array_value (); |
|
2087 |
5164
|
2088 write_mat5_integer_data (os, m.fortran_vec (), 1, m.nelem ()); |
5089
|
2089 } |
|
2090 else if (cname == "uint16") |
|
2091 { |
|
2092 uint16NDArray m = tc.uint16_array_value (); |
|
2093 |
5164
|
2094 write_mat5_integer_data (os, m.fortran_vec (), 2, m.nelem ()); |
5089
|
2095 } |
|
2096 else if (cname == "uint32") |
|
2097 { |
|
2098 uint32NDArray m = tc.uint32_array_value (); |
|
2099 |
5164
|
2100 write_mat5_integer_data (os, m.fortran_vec (), 4, m.nelem ()); |
5089
|
2101 } |
|
2102 else if (cname == "uint64") |
|
2103 { |
|
2104 uint64NDArray m = tc.uint64_array_value (); |
|
2105 |
5164
|
2106 write_mat5_integer_data (os, m.fortran_vec (), 8, m.nelem ()); |
5089
|
2107 } |
5269
|
2108 else if (tc.is_bool_type ()) |
|
2109 { |
|
2110 uint8NDArray m (tc.bool_array_value ()); |
|
2111 |
|
2112 write_mat5_integer_data (os, m.fortran_vec (), 1, m.nelem ()); |
|
2113 } |
4634
|
2114 else if (tc.is_real_scalar () || tc.is_real_matrix () || tc.is_range ()) |
|
2115 { |
|
2116 NDArray m = tc.array_value (); |
|
2117 |
|
2118 write_mat5_array (os, m, save_as_floats); |
|
2119 } |
|
2120 else if (tc.is_cell ()) |
|
2121 { |
|
2122 Cell cell = tc.cell_value (); |
|
2123 |
|
2124 if (! write_mat5_cell_array (os, cell, mark_as_global, save_as_floats)) |
|
2125 goto error_cleanup; |
|
2126 } |
|
2127 else if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
2128 { |
5269
|
2129 ComplexNDArray m_cmplx = tc.complex_array_value (); |
4634
|
2130 |
|
2131 write_mat5_array (os, ::real (m_cmplx), save_as_floats); |
|
2132 write_mat5_array (os, ::imag (m_cmplx), save_as_floats); |
|
2133 } |
6625
|
2134 else if (tc.is_map () || tc.is_inline_function()) |
4634
|
2135 { |
6625
|
2136 const Octave_map m = tc.map_value (); |
|
2137 if (tc.is_inline_function ()) |
|
2138 { |
|
2139 std::string classname = "inline"; |
|
2140 int namelen = classname.length (); |
|
2141 |
|
2142 if (namelen > max_namelen) |
|
2143 namelen = max_namelen; // only 31 or 63 char names permitted |
|
2144 |
|
2145 int paddedlength = PAD (namelen); |
|
2146 |
|
2147 write_mat5_tag (os, miINT8, namelen); |
|
2148 OCTAVE_LOCAL_BUFFER (char, paddedname, paddedlength); |
|
2149 memset (paddedname, 0, paddedlength); |
|
2150 strncpy (paddedname, classname.c_str (), namelen); |
|
2151 os.write (paddedname, paddedlength); |
|
2152 } |
|
2153 |
4634
|
2154 // an Octave structure */ |
|
2155 // recursively write each element of the structure |
|
2156 { |
5269
|
2157 char buf[64]; |
5828
|
2158 int32_t maxfieldnamelength = max_namelen + 1; |
4634
|
2159 int fieldcnt = 0; |
|
2160 |
4675
|
2161 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
4634
|
2162 fieldcnt++; |
|
2163 |
|
2164 write_mat5_tag (os, miINT32, 4); |
5760
|
2165 os.write (reinterpret_cast<char *> (&maxfieldnamelength), 4); |
5269
|
2166 write_mat5_tag (os, miINT8, fieldcnt*maxfieldnamelength); |
4634
|
2167 |
4675
|
2168 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
4634
|
2169 { |
|
2170 // write the name of each element |
|
2171 std::string tstr = m.key (i); |
5269
|
2172 memset (buf, 0, max_namelen + 1); |
|
2173 strncpy (buf, tstr.c_str (), max_namelen); // only 31 or 63 char names permitted |
|
2174 os.write (buf, max_namelen + 1); |
4634
|
2175 } |
|
2176 |
|
2177 int len = m.numel (); |
|
2178 |
5058
|
2179 for (int j = 0; j < len; j++) |
4634
|
2180 { |
|
2181 // write the data of each element |
|
2182 |
5058
|
2183 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
4634
|
2184 { |
5058
|
2185 Cell elts = m.contents (i); |
|
2186 |
4634
|
2187 bool retval2 = save_mat5_binary_element (os, elts(j), "", |
|
2188 mark_as_global, |
5269
|
2189 false, |
4634
|
2190 save_as_floats); |
|
2191 if (! retval2) |
|
2192 goto error_cleanup; |
|
2193 } |
|
2194 } |
|
2195 } |
|
2196 } |
|
2197 else |
|
2198 gripe_wrong_type_arg ("save", tc, false); |
|
2199 |
|
2200 contin = os.tellp (); |
|
2201 |
|
2202 return true; |
|
2203 |
|
2204 error_cleanup: |
|
2205 error ("save: error while writing `%s' to MAT file", name.c_str ()); |
|
2206 |
|
2207 return false; |
|
2208 } |
|
2209 |
|
2210 /* |
|
2211 ;;; Local Variables: *** |
|
2212 ;;; mode: C++ *** |
|
2213 ;;; End: *** |
|
2214 */ |
|
2215 |