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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
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> |
|
36 #include <string> |
4726
|
37 #include <vector> |
4634
|
38 |
|
39 #include "byte-swap.h" |
|
40 #include "data-conv.h" |
|
41 #include "file-ops.h" |
|
42 #include "glob-match.h" |
|
43 #include "lo-mappers.h" |
|
44 #include "lo-sstream.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" |
|
50 |
|
51 #include "Cell.h" |
|
52 #include "defun.h" |
|
53 #include "error.h" |
|
54 #include "gripes.h" |
|
55 #include "load-save.h" |
|
56 #include "oct-obj.h" |
|
57 #include "oct-map.h" |
|
58 #include "ov-cell.h" |
|
59 #include "pager.h" |
|
60 #include "pt-exp.h" |
|
61 #include "symtab.h" |
|
62 #include "sysdep.h" |
|
63 #include "unwind-prot.h" |
|
64 #include "utils.h" |
|
65 #include "variables.h" |
|
66 #include "version.h" |
|
67 #include "dMatrix.h" |
|
68 |
|
69 #include "ls-utils.h" |
|
70 #include "ls-mat5.h" |
|
71 |
|
72 #define PAD(l) (((l)<=4)?4:(((l)+7)/8)*8) |
|
73 #define TAGLENGTH(l) ((l)<=4?4:8) |
|
74 |
|
75 enum arrayclasstype |
|
76 { |
|
77 mxCELL_CLASS=1, // cell array |
|
78 mxSTRUCT_CLASS, // structure |
|
79 mxOBJECT_CLASS, // object |
|
80 mxCHAR_CLASS, // character array |
|
81 mxSPARSE_CLASS, // sparse array |
|
82 mxDOUBLE_CLASS, // double precision array |
|
83 mxSINGLE_CLASS, // single precision floating point |
|
84 mxINT8_CLASS, // 8 bit signed integer |
|
85 mxUINT8_CLASS, // 8 bit unsigned integer |
|
86 mxINT16_CLASS, // 16 bit signed integer |
|
87 mxUINT16_CLASS, // 16 bit unsigned integer |
|
88 mxINT32_CLASS, // 32 bit signed integer |
5089
|
89 mxUINT32_CLASS, // 32 bit unsigned integer |
|
90 mxINT64_CLASS, // 64 bit signed integer |
|
91 mxUINT64_CLASS, // 64 bit unsigned integer |
|
92 mxFUNCTION_CLASS // Function handle |
4634
|
93 }; |
|
94 |
|
95 // Read COUNT elements of data from IS in the format specified by TYPE, |
|
96 // placing the result in DATA. If SWAP is TRUE, swap the bytes of |
|
97 // each element before copying to DATA. FLT_FMT specifies the format |
|
98 // of the data if we are reading floating point numbers. |
|
99 |
|
100 static void |
|
101 read_mat5_binary_data (std::istream& is, double *data, |
|
102 int count, bool swap, mat5_data_type type, |
|
103 oct_mach_info::float_format flt_fmt) |
|
104 { |
|
105 |
|
106 switch (type) |
|
107 { |
|
108 case miINT8: |
|
109 read_doubles (is, data, LS_CHAR, count, swap, flt_fmt); |
|
110 break; |
|
111 |
|
112 case miUINT8: |
|
113 read_doubles (is, data, LS_U_CHAR, count, swap, flt_fmt); |
|
114 break; |
|
115 |
|
116 case miINT16: |
|
117 read_doubles (is, data, LS_SHORT, count, swap, flt_fmt); |
|
118 break; |
|
119 |
|
120 case miUINT16: |
|
121 read_doubles (is, data, LS_U_SHORT, count, swap, flt_fmt); |
|
122 break; |
|
123 |
|
124 case miINT32: |
|
125 read_doubles (is, data, LS_INT, count, swap, flt_fmt); |
|
126 break; |
|
127 |
|
128 case miUINT32: |
|
129 read_doubles (is, data, LS_U_INT, count, swap, flt_fmt); |
|
130 break; |
|
131 |
|
132 case miSINGLE: |
|
133 read_doubles (is, data, LS_FLOAT, count, swap, flt_fmt); |
|
134 break; |
|
135 |
|
136 case miRESERVE1: |
|
137 break; |
|
138 |
|
139 case miDOUBLE: |
|
140 read_doubles (is, data, LS_DOUBLE, count, swap, flt_fmt); |
|
141 break; |
|
142 |
|
143 case miRESERVE2: |
|
144 case miRESERVE3: |
|
145 break; |
|
146 |
|
147 case miINT64: |
|
148 #ifdef EIGHT_BYTE_INT |
|
149 read_doubles (is, data, LS_LONG, count, swap, flt_fmt); |
|
150 #endif |
|
151 break; |
|
152 |
|
153 case miUINT64: |
|
154 #ifdef EIGHT_BYTE_INT |
|
155 read_doubles (is, data, LS_U_LONG, count, swap, flt_fmt); |
|
156 #endif |
|
157 break; |
|
158 |
|
159 case miMATRIX: |
|
160 default: |
|
161 break; |
|
162 } |
|
163 } |
|
164 |
5089
|
165 template <class T> |
|
166 void |
|
167 read_mat5_integer_data (std::istream& is, T &m, int count, bool swap, |
|
168 mat5_data_type type) |
|
169 { |
|
170 |
|
171 #define READ_INTEGER_DATA(TYPE, swap, data, size, len, stream) \ |
|
172 do \ |
|
173 { \ |
|
174 if (len > 0) \ |
|
175 { \ |
|
176 volatile TYPE *ptr = X_CAST (volatile TYPE *, data); \ |
|
177 stream.read (X_CAST (char *, ptr), size * len); \ |
|
178 if (swap) \ |
|
179 swap_bytes< size > (ptr, len); \ |
|
180 TYPE tmp = ptr[0]; \ |
|
181 for (int i = len - 1; i > 0; i--) \ |
|
182 data[i] = ptr[i]; \ |
|
183 data[0] = tmp; \ |
|
184 } \ |
|
185 } \ |
|
186 while (0) |
|
187 |
|
188 switch (type) |
|
189 { |
|
190 case miINT8: |
|
191 READ_INTEGER_DATA (signed char, swap, m.fortran_vec (), 1, |
|
192 count, is); |
|
193 break; |
|
194 |
|
195 case miUINT8: |
|
196 READ_INTEGER_DATA (unsigned char, swap, m.fortran_vec (), 1, |
|
197 count, is); |
|
198 break; |
|
199 |
|
200 case miINT16: |
|
201 READ_INTEGER_DATA (signed TWO_BYTE_INT, swap, m.fortran_vec (), 2, |
|
202 count, is); |
|
203 break; |
|
204 |
|
205 case miUINT16: |
|
206 READ_INTEGER_DATA (unsigned TWO_BYTE_INT, swap, m.fortran_vec (), 2, |
|
207 count, is); |
|
208 break; |
|
209 |
|
210 case miINT32: |
|
211 READ_INTEGER_DATA (signed FOUR_BYTE_INT, swap, m.fortran_vec (), 4, |
|
212 count, is); |
|
213 break; |
|
214 |
|
215 case miUINT32: |
|
216 READ_INTEGER_DATA (unsigned FOUR_BYTE_INT, swap, m.fortran_vec (), 4, |
|
217 count, is); |
|
218 break; |
|
219 |
|
220 case miSINGLE: |
|
221 case miRESERVE1: |
|
222 case miDOUBLE: |
|
223 case miRESERVE2: |
|
224 case miRESERVE3: |
|
225 break; |
|
226 |
|
227 case miINT64: |
|
228 #ifdef EIGHT_BYTE_INT |
|
229 READ_INTEGER_DATA (signed EIGHT_BYTE_INT, swap, m.fortran_vec (), 8, |
|
230 count, is); |
|
231 #endif |
|
232 break; |
|
233 |
|
234 case miUINT64: |
|
235 #ifdef EIGHT_BYTE_INT |
|
236 READ_INTEGER_DATA (unsigned EIGHT_BYTE_INT, swap, m.fortran_vec (), 8, |
|
237 count, is); |
|
238 #endif |
|
239 break; |
|
240 |
|
241 case miMATRIX: |
|
242 default: |
|
243 break; |
|
244 } |
|
245 |
|
246 #undef READ_INTEGER_DATA |
|
247 |
|
248 } |
|
249 |
|
250 template void read_mat5_integer_data (std::istream& is, int8NDArray &m, |
|
251 int count, bool swap, |
|
252 mat5_data_type type); |
|
253 template void read_mat5_integer_data (std::istream& is, int16NDArray &m, |
|
254 int count, bool swap, |
|
255 mat5_data_type type); |
|
256 template void read_mat5_integer_data (std::istream& is, int32NDArray &m, |
|
257 int count, bool swap, |
|
258 mat5_data_type type); |
|
259 template void read_mat5_integer_data (std::istream& is, int64NDArray &m, |
|
260 int count, bool swap, |
|
261 mat5_data_type type); |
|
262 template void read_mat5_integer_data (std::istream& is, uint8NDArray &m, |
|
263 int count, bool swap, |
|
264 mat5_data_type type); |
|
265 template void read_mat5_integer_data (std::istream& is, uint16NDArray &m, |
|
266 int count, bool swap, |
|
267 mat5_data_type type); |
|
268 template void read_mat5_integer_data (std::istream& is, uint32NDArray &m, |
|
269 int count, bool swap, |
|
270 mat5_data_type type); |
|
271 template void read_mat5_integer_data (std::istream& is, uint64NDArray &m, |
|
272 int count, bool swap, |
|
273 mat5_data_type type); |
|
274 |
|
275 #define OCTAVE_MAT5_INTEGER_READ(TYP) \ |
|
276 { \ |
|
277 TYP re (dims); \ |
|
278 \ |
|
279 std::streampos tmp_pos; \ |
|
280 \ |
|
281 if (read_mat5_tag (is, swap, type, len)) \ |
|
282 { \ |
|
283 error ("load: reading matrix data for `%s'", retval.c_str ()); \ |
|
284 goto data_read_error; \ |
|
285 } \ |
|
286 \ |
|
287 int n = re.length (); \ |
|
288 tmp_pos = is.tellg (); \ |
|
289 read_mat5_integer_data (is, re, n, swap, \ |
|
290 (enum mat5_data_type) type); \ |
|
291 \ |
|
292 if (! is || error_state) \ |
|
293 { \ |
|
294 error ("load: reading matrix data for `%s'", retval.c_str ()); \ |
|
295 goto data_read_error; \ |
|
296 } \ |
|
297 \ |
|
298 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); \ |
|
299 \ |
|
300 if (imag) \ |
|
301 { \ |
|
302 /* We don't handle imag integer types, convert to an array */ \ |
|
303 NDArray im (dims); \ |
|
304 \ |
|
305 if (read_mat5_tag (is, swap, type, len)) \ |
|
306 { \ |
|
307 error ("load: reading matrix data for `%s'", \ |
|
308 retval.c_str ()); \ |
|
309 goto data_read_error; \ |
|
310 } \ |
|
311 \ |
|
312 n = im.length (); \ |
|
313 read_mat5_binary_data (is, im.fortran_vec (), n, swap, \ |
|
314 (enum mat5_data_type) type, flt_fmt); \ |
|
315 \ |
|
316 if (! is || error_state) \ |
|
317 { \ |
|
318 error ("load: reading imaginary matrix data for `%s'", \ |
|
319 retval.c_str ()); \ |
|
320 goto data_read_error; \ |
|
321 } \ |
|
322 \ |
|
323 ComplexNDArray ctmp (dims); \ |
|
324 \ |
|
325 for (int i = 0; i < n; i++) \ |
|
326 ctmp(i) = Complex (double (re(i)), im(i)); \ |
|
327 \ |
|
328 tc = ctmp; \ |
|
329 } \ |
|
330 else \ |
|
331 tc = re; \ |
|
332 } |
|
333 |
4634
|
334 // Read one element tag from stream IS, |
|
335 // place the type code in TYPE and the byte count in BYTES |
|
336 // return nonzero on error |
|
337 static int |
|
338 read_mat5_tag (std::istream& is, bool swap, int& type, int& bytes) |
|
339 { |
|
340 unsigned int upper; |
|
341 FOUR_BYTE_INT temp; |
|
342 |
|
343 if (! is.read (X_CAST (char *, &temp), 4 )) |
|
344 goto data_read_error; |
|
345 |
|
346 if (swap) |
4944
|
347 swap_bytes<4> (&temp); |
4634
|
348 |
|
349 upper = (temp >> 16) & 0xffff; |
|
350 type = temp & 0xffff; |
|
351 |
|
352 if (upper) |
|
353 { |
|
354 // "compressed" format |
|
355 bytes = upper; |
|
356 } |
|
357 else |
|
358 { |
|
359 if (! is.read (X_CAST (char *, &temp), 4 )) |
|
360 goto data_read_error; |
|
361 if (swap) |
4944
|
362 swap_bytes<4> (&temp); |
4634
|
363 bytes = temp; |
|
364 } |
|
365 |
|
366 return 0; |
|
367 |
|
368 data_read_error: |
|
369 return 1; |
|
370 } |
|
371 |
4944
|
372 static void |
|
373 read_int (std::istream& is, bool swap, FOUR_BYTE_INT& val) |
|
374 { |
|
375 is.read (reinterpret_cast<char *> (&val), 4); |
|
376 |
|
377 if (swap) |
|
378 swap_bytes<4> (&val); |
|
379 } |
|
380 |
4634
|
381 // Extract one data element (scalar, matrix, string, etc.) from stream |
|
382 // IS and place it in TC, returning the name of the variable. |
|
383 // |
|
384 // The data is expected to be in Matlab's "Version 5" .mat format, |
|
385 // though not all the features of that format are supported. |
|
386 // |
|
387 // FILENAME is used for error messages. |
|
388 |
|
389 std::string |
|
390 read_mat5_binary_element (std::istream& is, const std::string& filename, |
|
391 bool swap, bool& global, octave_value& tc) |
|
392 { |
|
393 std::string retval; |
|
394 |
|
395 // These are initialized here instead of closer to where they are |
|
396 // first used to avoid errors from gcc about goto crossing |
|
397 // initialization of variable. |
|
398 |
|
399 oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown; |
|
400 int type = 0; |
|
401 bool imag; |
|
402 bool logicalvar; |
|
403 enum arrayclasstype arrayclass; |
|
404 FOUR_BYTE_INT junk; |
|
405 FOUR_BYTE_INT flags; |
|
406 dim_vector dims; |
|
407 int len; |
|
408 int element_length; |
|
409 std::streampos pos; |
|
410 TWO_BYTE_INT number; |
|
411 number = *(TWO_BYTE_INT *)"\x00\x01"; |
|
412 |
|
413 global = false; |
|
414 |
|
415 // MAT files always use IEEE floating point |
|
416 if ((number == 1) ^ swap) |
|
417 flt_fmt = oct_mach_info::flt_fmt_ieee_big_endian; |
|
418 else |
|
419 flt_fmt = oct_mach_info::flt_fmt_ieee_little_endian; |
|
420 |
|
421 // element type and length |
|
422 if (read_mat5_tag (is, swap, type, element_length)) |
|
423 return retval; // EOF |
|
424 |
|
425 if (type != miMATRIX) |
|
426 { |
|
427 error ("load: invalid element type"); |
|
428 goto early_read_error; |
|
429 } |
|
430 |
|
431 if (element_length == 0) |
|
432 { |
|
433 tc = Matrix (); |
|
434 return retval; |
|
435 } |
|
436 |
|
437 pos = is.tellg (); |
|
438 |
|
439 // array flags subelement |
|
440 if (read_mat5_tag (is, swap, type, len) || type != miUINT32 || len != 8) |
|
441 { |
|
442 error ("load: invalid array flags subelement"); |
|
443 goto early_read_error; |
|
444 } |
|
445 |
|
446 read_int (is, swap, flags); |
|
447 imag = (flags & 0x0800) != 0; // has an imaginary part? |
|
448 global = (flags & 0x0400) != 0; // global variable? |
|
449 logicalvar = (flags & 0x0200) != 0; // we don't use this yet |
|
450 arrayclass = (arrayclasstype)(flags & 0xff); |
|
451 read_int (is, swap, junk); // an "undefined" entry |
|
452 |
|
453 // dimensions array subelement |
|
454 { |
4638
|
455 FOUR_BYTE_INT dim_len; |
|
456 |
|
457 if (read_mat5_tag (is, swap, type, dim_len) || type != miINT32) |
4634
|
458 { |
|
459 error ("load: invalid dimensions array subelement"); |
|
460 goto early_read_error; |
|
461 } |
|
462 |
4638
|
463 int ndims = dim_len / 4; |
4634
|
464 dims.resize (ndims); |
|
465 for (int i = 0; i < ndims; i++) |
|
466 { |
|
467 FOUR_BYTE_INT n; |
|
468 read_int (is, swap, n); |
|
469 dims(i) = n; |
|
470 } |
|
471 |
|
472 std::streampos tmp_pos = is.tellg (); |
4638
|
473 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (dim_len) - dim_len)); |
4634
|
474 } |
|
475 |
|
476 if (read_mat5_tag (is, swap, type, len) || type != miINT8) |
|
477 { |
|
478 error ("load: invalid array name subelement"); |
|
479 goto early_read_error; |
|
480 } |
|
481 |
|
482 { |
|
483 OCTAVE_LOCAL_BUFFER (char, name, len+1); |
|
484 |
|
485 // Structure field subelements have zero-length array name subelements. |
|
486 |
|
487 std::streampos tmp_pos = is.tellg (); |
|
488 |
|
489 if (len) |
|
490 { |
|
491 if (! is.read (X_CAST (char *, name), len )) |
|
492 goto data_read_error; |
|
493 |
|
494 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
|
495 } |
|
496 |
|
497 name[len] = '\0'; |
|
498 retval = name; |
|
499 } |
|
500 |
|
501 switch (arrayclass) |
|
502 { |
|
503 case mxCELL_CLASS: |
|
504 { |
|
505 Cell cell_array (dims); |
|
506 |
|
507 int n = cell_array.length (); |
|
508 |
|
509 for (int i = 0; i < n; i++) |
|
510 { |
|
511 octave_value tc2; |
|
512 |
|
513 std::string nm |
|
514 = read_mat5_binary_element (is, filename, swap, global, tc2); |
|
515 |
|
516 if (! is || error_state) |
|
517 { |
|
518 error ("load: reading cell data for `%s'", nm.c_str ()); |
|
519 goto data_read_error; |
|
520 } |
|
521 |
|
522 cell_array(i) = tc2; |
|
523 } |
|
524 |
|
525 tc = cell_array; |
|
526 } |
|
527 break; |
|
528 |
|
529 case mxOBJECT_CLASS: |
|
530 warning ("load: objects are not implemented"); |
|
531 goto skip_ahead; |
|
532 |
|
533 case mxSPARSE_CLASS: |
|
534 warning ("load: sparse arrays are not implemented"); |
|
535 goto skip_ahead; |
|
536 |
5089
|
537 case mxFUNCTION_CLASS: |
|
538 warning ("load: function handles are not implemented"); |
|
539 goto skip_ahead; |
|
540 |
4634
|
541 case mxSTRUCT_CLASS: |
|
542 { |
|
543 Octave_map m; |
|
544 FOUR_BYTE_INT fn_type; |
|
545 FOUR_BYTE_INT fn_len; |
|
546 FOUR_BYTE_INT field_name_length; |
|
547 int i; |
|
548 |
|
549 // field name length subelement -- actually the maximum length |
|
550 // of a field name. The Matlab docs promise this will always |
|
551 // be 32. We read and use the actual value, on the theory |
|
552 // that eventually someone will recognize that's a waste of |
|
553 // space. |
|
554 if (read_mat5_tag (is, swap, fn_type, fn_len) || fn_type != miINT32) |
|
555 { |
|
556 error ("load: invalid field name subelement"); |
|
557 goto data_read_error; |
|
558 } |
|
559 |
|
560 if (! is.read (X_CAST (char *, &field_name_length), fn_len )) |
|
561 goto data_read_error; |
|
562 |
|
563 if (swap) |
4944
|
564 swap_bytes<4> (&field_name_length); |
4634
|
565 |
|
566 // field name subelement. The length of this subelement tells |
|
567 // us how many fields there are. |
|
568 if (read_mat5_tag (is, swap, fn_type, fn_len) || fn_type != miINT8) |
|
569 { |
|
570 error ("load: invalid field name subelement"); |
|
571 goto data_read_error; |
|
572 } |
|
573 |
|
574 int n_fields = fn_len/field_name_length; |
|
575 |
|
576 fn_len = PAD (fn_len); |
|
577 |
|
578 OCTAVE_LOCAL_BUFFER (char, elname, fn_len); |
|
579 |
|
580 if (! is.read (elname, fn_len)) |
|
581 goto data_read_error; |
|
582 |
|
583 int n; |
|
584 if (dims(0) == 1) |
|
585 n = dims(1); |
|
586 else if (dims(1) == 1) |
|
587 n = dims(0); |
|
588 else |
|
589 { |
|
590 error ("load: can only handle one-dimensional structure arrays"); |
|
591 goto data_read_error; |
|
592 } |
|
593 |
|
594 Cell field_elts (n_fields, n); |
|
595 |
|
596 // fields subelements |
|
597 for (int j = 0; j < n; j++) |
|
598 { |
|
599 for (i = 0; i < n_fields; i++) |
|
600 { |
|
601 octave_value fieldtc; |
|
602 read_mat5_binary_element (is, filename, swap, global, fieldtc); |
|
603 field_elts(i,j) = fieldtc; |
|
604 } |
|
605 } |
|
606 |
|
607 for (int j = n_fields-1; j >= 0; j--) |
|
608 { |
|
609 const char *key = elname + j*field_name_length; |
|
610 |
4675
|
611 Cell c (dim_vector (n, 1)); |
|
612 |
4634
|
613 for (int k = n-1; k >=0; k--) |
4675
|
614 c(k) = field_elts(j,k); |
|
615 |
|
616 m.assign (key, c); |
4634
|
617 } |
|
618 |
|
619 tc = m; |
|
620 } |
|
621 break; |
|
622 |
5089
|
623 case mxINT8_CLASS: |
|
624 OCTAVE_MAT5_INTEGER_READ (int8NDArray); |
|
625 break; |
|
626 |
|
627 case mxUINT8_CLASS: |
|
628 OCTAVE_MAT5_INTEGER_READ (uint8NDArray); |
|
629 break; |
|
630 |
|
631 case mxINT16_CLASS: |
|
632 OCTAVE_MAT5_INTEGER_READ (int16NDArray); |
|
633 break; |
|
634 |
|
635 case mxUINT16_CLASS: |
|
636 OCTAVE_MAT5_INTEGER_READ (uint16NDArray); |
|
637 break; |
|
638 |
|
639 case mxINT32_CLASS: |
|
640 OCTAVE_MAT5_INTEGER_READ (int32NDArray); |
|
641 break; |
|
642 |
|
643 case mxUINT32_CLASS: |
|
644 OCTAVE_MAT5_INTEGER_READ (uint32NDArray); |
|
645 break; |
|
646 |
|
647 case mxINT64_CLASS: |
|
648 OCTAVE_MAT5_INTEGER_READ (int64NDArray); |
|
649 break; |
|
650 |
|
651 case mxUINT64_CLASS: |
|
652 OCTAVE_MAT5_INTEGER_READ (uint64NDArray); |
|
653 break; |
|
654 |
4634
|
655 case mxCHAR_CLASS: |
|
656 // handle as a numerical array to start with |
|
657 |
|
658 case mxDOUBLE_CLASS: |
|
659 case mxSINGLE_CLASS: |
|
660 default: |
5089
|
661 { |
|
662 NDArray re (dims); |
4634
|
663 |
5089
|
664 // real data subelement |
|
665 |
4634
|
666 std::streampos tmp_pos; |
5089
|
667 |
4634
|
668 if (read_mat5_tag (is, swap, type, len)) |
|
669 { |
|
670 error ("load: reading matrix data for `%s'", retval.c_str ()); |
|
671 goto data_read_error; |
|
672 } |
|
673 |
|
674 int n = re.length (); |
|
675 tmp_pos = is.tellg (); |
|
676 read_mat5_binary_data (is, re.fortran_vec (), n, swap, |
|
677 (enum mat5_data_type) type, flt_fmt); |
|
678 |
|
679 if (! is || error_state) |
|
680 { |
|
681 error ("load: reading matrix data for `%s'", retval.c_str ()); |
|
682 goto data_read_error; |
|
683 } |
|
684 |
|
685 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
5089
|
686 |
|
687 // imaginary data subelement |
|
688 if (imag) |
|
689 { |
|
690 NDArray im (dims); |
4634
|
691 |
5089
|
692 if (read_mat5_tag (is, swap, type, len)) |
|
693 { |
|
694 error ("load: reading matrix data for `%s'", retval.c_str ()); |
|
695 goto data_read_error; |
|
696 } |
4634
|
697 |
5089
|
698 n = im.length (); |
|
699 read_mat5_binary_data (is, im.fortran_vec (), n, swap, |
|
700 (enum mat5_data_type) type, flt_fmt); |
4634
|
701 |
5089
|
702 if (! is || error_state) |
|
703 { |
|
704 error ("load: reading imaginary matrix data for `%s'", |
|
705 retval.c_str ()); |
|
706 goto data_read_error; |
|
707 } |
4634
|
708 |
5089
|
709 ComplexNDArray ctmp (dims); |
4634
|
710 |
5089
|
711 for (int i = 0; i < n; i++) |
|
712 ctmp(i) = Complex (re(i), im(i)); |
4634
|
713 |
5089
|
714 tc = ctmp; |
|
715 } |
|
716 else |
|
717 tc = re; |
4634
|
718 |
5089
|
719 if (arrayclass == mxCHAR_CLASS) |
|
720 tc = tc.convert_to_str (false, true); |
|
721 } |
4634
|
722 } |
|
723 |
|
724 is.seekg (pos + static_cast<std::streamoff> (element_length)); |
|
725 |
|
726 if (is.eof ()) |
|
727 is.clear (); |
|
728 |
|
729 return retval; |
|
730 |
|
731 data_read_error: |
|
732 early_read_error: |
|
733 error ("load: trouble reading binary file `%s'", filename.c_str ()); |
|
734 return std::string (); |
|
735 |
|
736 skip_ahead: |
|
737 warning ("skipping over `%s'", retval.c_str ()); |
|
738 is.seekg (pos + static_cast<std::streamoff> (element_length)); |
|
739 return read_mat5_binary_element (is, filename, swap, global, tc); |
|
740 } |
|
741 |
|
742 int |
|
743 read_mat5_binary_file_header (std::istream& is, bool& swap, bool quiet) |
|
744 { |
|
745 TWO_BYTE_INT version=0, magic=0; |
|
746 |
|
747 is.seekg (124, std::ios::beg); |
|
748 is.read (X_CAST (char *, &version), 2); |
|
749 is.read (X_CAST (char *, &magic), 2); |
|
750 |
|
751 if (magic == 0x4d49) |
|
752 swap = 0; |
|
753 else if (magic == 0x494d) |
|
754 swap = 1; |
|
755 else |
|
756 { |
|
757 if (! quiet) |
|
758 error ("load: can't read binary file"); |
|
759 return -1; |
|
760 } |
|
761 |
|
762 if (! swap) // version number is inverse swapped! |
|
763 version = ((version >> 8) & 0xff) + ((version & 0xff) << 8); |
|
764 |
|
765 if (version != 1 && !quiet) |
|
766 warning ("load: found version %d binary MAT file, " |
|
767 "but only prepared for version 1", version); |
|
768 |
|
769 return 0; |
|
770 } |
|
771 |
|
772 static int |
|
773 write_mat5_tag (std::ostream& is, int type, int bytes) |
|
774 { |
|
775 FOUR_BYTE_INT temp; |
|
776 |
|
777 if (bytes <= 4) |
|
778 temp = (bytes << 16) + type; |
|
779 else |
|
780 { |
|
781 temp = type; |
|
782 if (! is.write ((char *)&temp, 4)) |
|
783 goto data_write_error; |
|
784 temp = bytes; |
|
785 } |
|
786 |
|
787 if (! is.write ((char *)&temp, 4)) |
|
788 goto data_write_error; |
|
789 |
|
790 return 0; |
|
791 |
|
792 data_write_error: |
|
793 return 1; |
|
794 } |
|
795 |
|
796 // write out the numeric values in M to OS, |
|
797 // preceded by the appropriate tag. |
|
798 static void |
|
799 write_mat5_array (std::ostream& os, const NDArray& m, bool save_as_floats) |
|
800 { |
|
801 int nel = m.nelem (); |
|
802 double max_val, min_val; |
|
803 save_type st = LS_DOUBLE; |
|
804 mat5_data_type mst; |
|
805 int size; |
|
806 unsigned len; |
|
807 const double *data = m.data (); |
|
808 |
|
809 // Have to use copy here to avoid writing over data accessed via |
|
810 // Matrix::data(). |
|
811 |
|
812 #define MAT5_DO_WRITE(TYPE, data, count, stream) \ |
|
813 do \ |
|
814 { \ |
|
815 OCTAVE_LOCAL_BUFFER (TYPE, ptr, count); \ |
|
816 for (int i = 0; i < count; i++) \ |
|
817 ptr[i] = X_CAST (TYPE, data[i]); \ |
|
818 stream.write (X_CAST (char *, ptr), count * sizeof (TYPE)); \ |
|
819 } \ |
|
820 while (0) |
|
821 |
|
822 if (save_as_floats) |
|
823 { |
|
824 if (m.too_large_for_float ()) |
|
825 { |
|
826 warning ("save: some values too large to save as floats --"); |
|
827 warning ("save: saving as doubles instead"); |
|
828 } |
|
829 else |
|
830 st = LS_FLOAT; |
|
831 } |
|
832 |
|
833 if (m.all_integers (max_val, min_val)) |
|
834 st = get_save_type (max_val, min_val); |
|
835 |
|
836 switch (st) |
|
837 { |
|
838 default: |
|
839 case LS_DOUBLE: mst = miDOUBLE; size = 8; break; |
|
840 case LS_FLOAT: mst = miSINGLE; size = 4; break; |
|
841 case LS_U_CHAR: mst = miUINT8; size = 1; break; |
|
842 case LS_U_SHORT: mst = miUINT16; size = 2; break; |
|
843 case LS_U_INT: mst = miUINT32; size = 4; break; |
|
844 case LS_CHAR: mst = miINT8; size = 1; break; |
|
845 case LS_SHORT: mst = miINT16; size = 2; break; |
|
846 case LS_INT: mst = miINT32; size = 4; break; |
|
847 } |
|
848 |
|
849 len = nel*size; |
|
850 write_mat5_tag (os, mst, len); |
|
851 |
|
852 { |
|
853 switch (st) |
|
854 { |
|
855 case LS_U_CHAR: |
|
856 MAT5_DO_WRITE (unsigned char, data, nel, os); |
|
857 break; |
|
858 |
|
859 case LS_U_SHORT: |
|
860 MAT5_DO_WRITE (unsigned TWO_BYTE_INT, data, nel, os); |
|
861 break; |
|
862 |
|
863 case LS_U_INT: |
|
864 MAT5_DO_WRITE (unsigned FOUR_BYTE_INT, data, nel, os); |
|
865 break; |
|
866 |
|
867 // provide for 64 bit ints, even though get_save_type does |
|
868 // not yet implement them |
|
869 #ifdef EIGHT_BYTE_INT |
|
870 case LS_U_LONG: |
|
871 MAT5_DO_WRITE (unsigned EIGHT_BYTE_INT, data, nel, os); |
|
872 break; |
|
873 #endif |
|
874 |
|
875 case LS_CHAR: |
|
876 MAT5_DO_WRITE (signed char, data, nel, os); |
|
877 break; |
|
878 |
|
879 case LS_SHORT: |
|
880 MAT5_DO_WRITE (TWO_BYTE_INT, data, nel, os); |
|
881 break; |
|
882 |
|
883 case LS_INT: |
|
884 MAT5_DO_WRITE (FOUR_BYTE_INT, data, nel, os); |
|
885 break; |
|
886 |
|
887 #ifdef EIGHT_BYTE_INT |
|
888 case LS_LONG: |
|
889 MAT5_DO_WRITE (EIGHT_BYTE_INT, data, nel, os); |
|
890 break; |
|
891 #endif |
|
892 |
|
893 case LS_FLOAT: |
|
894 MAT5_DO_WRITE (float, data, nel, os); |
|
895 break; |
|
896 |
|
897 case LS_DOUBLE: // No conversion necessary. |
|
898 os.write (X_CAST (char *, data), len); |
|
899 break; |
|
900 |
|
901 default: |
|
902 (*current_liboctave_error_handler) |
|
903 ("unrecognized data format requested"); |
|
904 break; |
|
905 } |
|
906 } |
|
907 if (PAD (len) > len) |
|
908 { |
|
909 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
910 os.write (buf, PAD (len) - len); |
|
911 } |
|
912 } |
|
913 |
5089
|
914 template <class T> |
|
915 void |
|
916 write_mat5_integer_data (std::ostream& os, const T& m, int size) |
|
917 { |
|
918 int nel = m.nelem (); |
|
919 mat5_data_type mst; |
|
920 unsigned len; |
|
921 |
|
922 switch (size) |
|
923 { |
|
924 case 1: |
|
925 mst = miUINT8; |
|
926 break; |
|
927 case 2: |
|
928 mst = miUINT16; |
|
929 break; |
|
930 case 3: |
|
931 mst = miUINT32; |
|
932 break; |
|
933 case 4: |
|
934 mst = miUINT64; |
|
935 break; |
|
936 case -1: |
|
937 mst = miINT8; |
|
938 size = - size; |
|
939 break; |
|
940 case -2: |
|
941 mst = miINT16; |
|
942 size = - size; |
|
943 break; |
|
944 case -3: |
|
945 mst = miINT32; |
|
946 size = - size; |
|
947 break; |
|
948 case -4: |
|
949 default: |
|
950 mst = miINT64; |
|
951 size = - size; |
|
952 break; |
|
953 } |
|
954 |
|
955 len = nel*size; |
|
956 write_mat5_tag (os, mst, len); |
|
957 |
|
958 os.write (X_CAST(char *, m.data ()), len); |
|
959 |
|
960 if (PAD (len) > len) |
|
961 { |
|
962 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
963 os.write (buf, PAD (len) - len); |
|
964 } |
|
965 } |
|
966 |
|
967 template void write_mat5_integer_data (std::ostream& os, |
|
968 const int8NDArray &m, int size); |
|
969 template void write_mat5_integer_data (std::ostream& os, |
|
970 const int16NDArray &m, int size); |
|
971 template void write_mat5_integer_data (std::ostream& os, |
|
972 const int32NDArray &m, int size); |
|
973 template void write_mat5_integer_data (std::ostream& os, |
|
974 const int64NDArray &m, int size); |
|
975 template void write_mat5_integer_data (std::ostream& os, |
|
976 const uint8NDArray &m, int size); |
|
977 template void write_mat5_integer_data (std::ostream& os, |
|
978 const uint16NDArray &m, int size); |
|
979 template void write_mat5_integer_data (std::ostream& os, |
|
980 const uint32NDArray &m, int size); |
|
981 template void write_mat5_integer_data (std::ostream& os, |
|
982 const uint64NDArray &m, int size); |
|
983 |
4634
|
984 // Write out cell element values in the cell array to OS, preceded by |
|
985 // the appropriate tag. |
|
986 |
|
987 static bool |
4701
|
988 write_mat5_cell_array (std::ostream& os, const Cell& cell, |
|
989 bool mark_as_global, bool save_as_floats) |
4634
|
990 { |
|
991 int nel = cell.nelem (); |
|
992 |
|
993 for (int i = 0; i < nel; i++) |
|
994 { |
|
995 octave_value ov = cell(i); |
|
996 |
|
997 if (! save_mat5_binary_element (os, ov, "", mark_as_global, |
|
998 save_as_floats)) |
|
999 return false; |
|
1000 } |
|
1001 |
|
1002 return true; |
|
1003 } |
|
1004 |
|
1005 // save the data from TC along with the corresponding NAME on stream |
|
1006 // OS in the MatLab version 5 binary format. Return true on success. |
|
1007 |
|
1008 bool |
|
1009 save_mat5_binary_element (std::ostream& os, |
|
1010 const octave_value& tc, const std::string& name, |
|
1011 bool mark_as_global, bool save_as_floats) |
|
1012 { |
|
1013 FOUR_BYTE_INT flags=0; |
|
1014 FOUR_BYTE_INT junk=0; |
|
1015 std::streampos fixup, contin; |
5089
|
1016 std::string cname = tc.class_name (); |
4634
|
1017 |
|
1018 // element type and length |
|
1019 fixup = os.tellp (); |
|
1020 write_mat5_tag (os, miMATRIX, 99); // we don't know the real length yet |
|
1021 |
|
1022 // array flags subelement |
|
1023 write_mat5_tag (os, miUINT32, 8); |
|
1024 |
|
1025 if (mark_as_global) |
|
1026 flags |= 0x0400; |
|
1027 |
|
1028 if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
1029 flags |= 0x0800; |
|
1030 |
|
1031 if (tc.is_string ()) |
|
1032 flags |= mxCHAR_CLASS; |
5089
|
1033 else if (cname == "int8") |
|
1034 flags |= mxINT8_CLASS; |
|
1035 else if (cname == "int16") |
|
1036 flags |= mxINT16_CLASS; |
|
1037 else if (cname == "int32") |
|
1038 flags |= mxINT32_CLASS; |
|
1039 else if (cname == "int64") |
|
1040 flags |= mxINT64_CLASS; |
|
1041 else if (cname == "uint8") |
|
1042 flags |= mxUINT8_CLASS; |
|
1043 else if (cname == "uint16") |
|
1044 flags |= mxUINT16_CLASS; |
|
1045 else if (cname == "uint32") |
|
1046 flags |= mxUINT32_CLASS; |
|
1047 else if (cname == "uint64") |
|
1048 flags |= mxUINT64_CLASS; |
4634
|
1049 else if (tc.is_real_scalar ()) |
|
1050 flags |= mxDOUBLE_CLASS; |
|
1051 else if (tc.is_real_matrix () || tc.is_range ()) |
|
1052 flags |= mxDOUBLE_CLASS; |
|
1053 else if (tc.is_complex_scalar ()) |
|
1054 flags |= mxDOUBLE_CLASS; |
|
1055 else if (tc.is_complex_matrix ()) |
|
1056 flags |= mxDOUBLE_CLASS; |
|
1057 else if (tc.is_map ()) |
|
1058 flags |= mxSTRUCT_CLASS; |
|
1059 else if (tc.is_cell ()) |
|
1060 flags |= mxCELL_CLASS; |
|
1061 else |
|
1062 { |
|
1063 gripe_wrong_type_arg ("save", tc, false); |
|
1064 goto error_cleanup; |
|
1065 } |
|
1066 |
|
1067 os.write ((char *)&flags, 4); |
|
1068 os.write ((char *)&junk, 4); |
|
1069 |
|
1070 { |
|
1071 dim_vector dv = tc.dims (); |
|
1072 int nd = tc.ndims (); |
4638
|
1073 int dim_len = 4*nd; |
4634
|
1074 |
4638
|
1075 write_mat5_tag (os, miINT32, dim_len); |
4634
|
1076 |
|
1077 for (int i = 0; i < nd; i++) |
|
1078 { |
4638
|
1079 FOUR_BYTE_INT n = dv(i); |
4634
|
1080 os.write ((char *)&n, 4); |
|
1081 } |
4638
|
1082 |
|
1083 if (PAD (dim_len) > dim_len) |
|
1084 { |
|
1085 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
1086 os.write (buf, PAD (dim_len) - dim_len); |
|
1087 } |
4634
|
1088 } |
|
1089 |
|
1090 // array name subelement |
|
1091 { |
|
1092 int namelen = name.length (); |
|
1093 |
|
1094 if (namelen > 31) |
|
1095 namelen = 31; // only 31 char names permitted in mat file |
|
1096 |
|
1097 int paddedlength = PAD (namelen); |
|
1098 |
|
1099 write_mat5_tag (os, miINT8, namelen); |
|
1100 OCTAVE_LOCAL_BUFFER (char, paddedname, paddedlength); |
|
1101 memset (paddedname, 0, paddedlength); |
|
1102 strncpy (paddedname, name.c_str (), namelen); |
|
1103 os.write (paddedname, paddedlength); |
|
1104 } |
|
1105 |
|
1106 // data element |
|
1107 if (tc.is_string ()) |
|
1108 { |
|
1109 charMatrix chm = tc.char_matrix_value (); |
|
1110 int nr = chm.rows (); |
|
1111 int nc = chm.cols (); |
|
1112 int len = nr*nc*2; |
|
1113 int paddedlength = PAD (nr*nc*2); |
|
1114 |
|
1115 OCTAVE_LOCAL_BUFFER (TWO_BYTE_INT, buf, nc*nr+3); |
|
1116 write_mat5_tag (os, miUINT16, len); |
|
1117 |
|
1118 for (int i = 0; i < nr; i++) |
|
1119 { |
|
1120 std::string tstr = chm.row_as_string (i); |
|
1121 const char *s = tstr.data (); |
|
1122 |
|
1123 for (int j = 0; j < nc; j++) |
|
1124 buf[j*nr+i] = *s++ & 0x00FF; |
|
1125 } |
|
1126 os.write ((char *)buf, nr*nc*2); |
|
1127 |
|
1128 if (paddedlength > len) |
|
1129 os.write ((char *)buf, paddedlength - len); |
|
1130 } |
5089
|
1131 else if (cname == "int8") |
|
1132 { |
|
1133 int8NDArray m = tc.int8_array_value (); |
|
1134 |
|
1135 write_mat5_integer_data (os, m, -1); |
|
1136 } |
|
1137 else if (cname == "int16") |
|
1138 { |
|
1139 int16NDArray m = tc.int16_array_value (); |
|
1140 |
|
1141 write_mat5_integer_data (os, m, -2); |
|
1142 } |
|
1143 else if (cname == "int32") |
|
1144 { |
|
1145 int32NDArray m = tc.int32_array_value (); |
|
1146 |
|
1147 write_mat5_integer_data (os, m, -4); |
|
1148 } |
|
1149 else if (cname == "int64") |
|
1150 { |
|
1151 int64NDArray m = tc.int64_array_value (); |
|
1152 |
|
1153 write_mat5_integer_data (os, m, -8); |
|
1154 } |
|
1155 else if (cname == "uint8") |
|
1156 { |
|
1157 uint8NDArray m = tc.uint8_array_value (); |
|
1158 |
|
1159 write_mat5_integer_data (os, m, 1); |
|
1160 } |
|
1161 else if (cname == "uint16") |
|
1162 { |
|
1163 uint16NDArray m = tc.uint16_array_value (); |
|
1164 |
|
1165 write_mat5_integer_data (os, m, 2); |
|
1166 } |
|
1167 else if (cname == "uint32") |
|
1168 { |
|
1169 uint32NDArray m = tc.uint32_array_value (); |
|
1170 |
|
1171 write_mat5_integer_data (os, m, 4); |
|
1172 } |
|
1173 else if (cname == "uint64") |
|
1174 { |
|
1175 uint64NDArray m = tc.uint64_array_value (); |
|
1176 |
|
1177 write_mat5_integer_data (os, m, 8); |
|
1178 } |
4634
|
1179 else if (tc.is_real_scalar () || tc.is_real_matrix () || tc.is_range ()) |
|
1180 { |
|
1181 NDArray m = tc.array_value (); |
|
1182 |
|
1183 write_mat5_array (os, m, save_as_floats); |
|
1184 } |
|
1185 else if (tc.is_cell ()) |
|
1186 { |
|
1187 Cell cell = tc.cell_value (); |
|
1188 |
|
1189 if (! write_mat5_cell_array (os, cell, mark_as_global, save_as_floats)) |
|
1190 goto error_cleanup; |
|
1191 } |
|
1192 else if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
1193 { |
|
1194 ComplexNDArray m_cmplx = tc.complex_matrix_value (); |
|
1195 |
|
1196 write_mat5_array (os, ::real (m_cmplx), save_as_floats); |
|
1197 write_mat5_array (os, ::imag (m_cmplx), save_as_floats); |
|
1198 } |
|
1199 else if (tc.is_map ()) |
|
1200 { |
|
1201 // an Octave structure */ |
|
1202 // recursively write each element of the structure |
4675
|
1203 const Octave_map m = tc.map_value (); |
4634
|
1204 |
|
1205 { |
|
1206 char buf[32]; |
|
1207 FOUR_BYTE_INT maxfieldnamelength = 32; |
|
1208 int fieldcnt = 0; |
|
1209 |
4675
|
1210 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
4634
|
1211 fieldcnt++; |
|
1212 |
|
1213 write_mat5_tag (os, miINT32, 4); |
|
1214 os.write ((char *)&maxfieldnamelength, 4); |
|
1215 write_mat5_tag (os, miINT8, fieldcnt*32); |
|
1216 |
4675
|
1217 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
4634
|
1218 { |
|
1219 // write the name of each element |
|
1220 std::string tstr = m.key (i); |
|
1221 memset (buf, 0, 32); |
|
1222 strncpy (buf, tstr.c_str (), 31); // only 31 char names permitted |
|
1223 os.write (buf, 32); |
|
1224 } |
|
1225 |
|
1226 int len = m.numel (); |
|
1227 |
5058
|
1228 for (int j = 0; j < len; j++) |
4634
|
1229 { |
|
1230 // write the data of each element |
|
1231 |
5058
|
1232 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
4634
|
1233 { |
5058
|
1234 Cell elts = m.contents (i); |
|
1235 |
4634
|
1236 bool retval2 = save_mat5_binary_element (os, elts(j), "", |
|
1237 mark_as_global, |
|
1238 save_as_floats); |
|
1239 if (! retval2) |
|
1240 goto error_cleanup; |
|
1241 } |
|
1242 } |
|
1243 } |
|
1244 } |
|
1245 else |
|
1246 gripe_wrong_type_arg ("save", tc, false); |
|
1247 |
|
1248 contin = os.tellp (); |
|
1249 os.seekp (fixup); |
|
1250 write_mat5_tag (os, miMATRIX, |
|
1251 static_cast<int>(contin - fixup) - 8); // the actual length |
|
1252 os.seekp (contin); |
|
1253 |
|
1254 return true; |
|
1255 |
|
1256 error_cleanup: |
|
1257 error ("save: error while writing `%s' to MAT file", name.c_str ()); |
|
1258 |
|
1259 return false; |
|
1260 } |
|
1261 |
|
1262 /* |
|
1263 ;;; Local Variables: *** |
|
1264 ;;; mode: C++ *** |
|
1265 ;;; End: *** |
|
1266 */ |
|
1267 |