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