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