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 <memory> |
|
37 #include <string> |
|
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 |
|
89 mxUINT32_CLASS // 32 bit unsigned integer |
|
90 }; |
|
91 |
|
92 // Read COUNT elements of data from IS in the format specified by TYPE, |
|
93 // placing the result in DATA. If SWAP is TRUE, swap the bytes of |
|
94 // each element before copying to DATA. FLT_FMT specifies the format |
|
95 // of the data if we are reading floating point numbers. |
|
96 |
|
97 static void |
|
98 read_mat5_binary_data (std::istream& is, double *data, |
|
99 int count, bool swap, mat5_data_type type, |
|
100 oct_mach_info::float_format flt_fmt) |
|
101 { |
|
102 |
|
103 switch (type) |
|
104 { |
|
105 case miINT8: |
|
106 read_doubles (is, data, LS_CHAR, count, swap, flt_fmt); |
|
107 break; |
|
108 |
|
109 case miUINT8: |
|
110 read_doubles (is, data, LS_U_CHAR, count, swap, flt_fmt); |
|
111 break; |
|
112 |
|
113 case miINT16: |
|
114 read_doubles (is, data, LS_SHORT, count, swap, flt_fmt); |
|
115 break; |
|
116 |
|
117 case miUINT16: |
|
118 read_doubles (is, data, LS_U_SHORT, count, swap, flt_fmt); |
|
119 break; |
|
120 |
|
121 case miINT32: |
|
122 read_doubles (is, data, LS_INT, count, swap, flt_fmt); |
|
123 break; |
|
124 |
|
125 case miUINT32: |
|
126 read_doubles (is, data, LS_U_INT, count, swap, flt_fmt); |
|
127 break; |
|
128 |
|
129 case miSINGLE: |
|
130 read_doubles (is, data, LS_FLOAT, count, swap, flt_fmt); |
|
131 break; |
|
132 |
|
133 case miRESERVE1: |
|
134 break; |
|
135 |
|
136 case miDOUBLE: |
|
137 read_doubles (is, data, LS_DOUBLE, count, swap, flt_fmt); |
|
138 break; |
|
139 |
|
140 case miRESERVE2: |
|
141 case miRESERVE3: |
|
142 break; |
|
143 |
|
144 case miINT64: |
|
145 #ifdef EIGHT_BYTE_INT |
|
146 read_doubles (is, data, LS_LONG, count, swap, flt_fmt); |
|
147 #endif |
|
148 break; |
|
149 |
|
150 case miUINT64: |
|
151 #ifdef EIGHT_BYTE_INT |
|
152 read_doubles (is, data, LS_U_LONG, count, swap, flt_fmt); |
|
153 #endif |
|
154 break; |
|
155 |
|
156 case miMATRIX: |
|
157 default: |
|
158 break; |
|
159 } |
|
160 } |
|
161 |
|
162 // Read one element tag from stream IS, |
|
163 // place the type code in TYPE and the byte count in BYTES |
|
164 // return nonzero on error |
|
165 static int |
|
166 read_mat5_tag (std::istream& is, bool swap, int& type, int& bytes) |
|
167 { |
|
168 unsigned int upper; |
|
169 FOUR_BYTE_INT temp; |
|
170 |
|
171 if (! is.read (X_CAST (char *, &temp), 4 )) |
|
172 goto data_read_error; |
|
173 |
|
174 if (swap) |
|
175 swap_4_bytes ((char *)&temp); |
|
176 |
|
177 upper = (temp >> 16) & 0xffff; |
|
178 type = temp & 0xffff; |
|
179 |
|
180 if (upper) |
|
181 { |
|
182 // "compressed" format |
|
183 bytes = upper; |
|
184 } |
|
185 else |
|
186 { |
|
187 if (! is.read (X_CAST (char *, &temp), 4 )) |
|
188 goto data_read_error; |
|
189 if (swap) |
|
190 swap_4_bytes ((char *)&temp); |
|
191 bytes = temp; |
|
192 } |
|
193 |
|
194 return 0; |
|
195 |
|
196 data_read_error: |
|
197 return 1; |
|
198 } |
|
199 |
|
200 // Extract one data element (scalar, matrix, string, etc.) from stream |
|
201 // IS and place it in TC, returning the name of the variable. |
|
202 // |
|
203 // The data is expected to be in Matlab's "Version 5" .mat format, |
|
204 // though not all the features of that format are supported. |
|
205 // |
|
206 // FILENAME is used for error messages. |
|
207 |
|
208 std::string |
|
209 read_mat5_binary_element (std::istream& is, const std::string& filename, |
|
210 bool swap, bool& global, octave_value& tc) |
|
211 { |
|
212 std::string retval; |
|
213 |
|
214 // These are initialized here instead of closer to where they are |
|
215 // first used to avoid errors from gcc about goto crossing |
|
216 // initialization of variable. |
|
217 |
|
218 NDArray re; |
|
219 oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown; |
|
220 int type = 0; |
|
221 bool imag; |
|
222 bool logicalvar; |
|
223 enum arrayclasstype arrayclass; |
|
224 FOUR_BYTE_INT junk; |
|
225 FOUR_BYTE_INT flags; |
|
226 FOUR_BYTE_INT dimension_length; |
|
227 dim_vector dims; |
|
228 int len; |
|
229 int element_length; |
|
230 std::streampos pos; |
|
231 TWO_BYTE_INT number; |
|
232 number = *(TWO_BYTE_INT *)"\x00\x01"; |
|
233 |
|
234 global = false; |
|
235 |
|
236 // MAT files always use IEEE floating point |
|
237 if ((number == 1) ^ swap) |
|
238 flt_fmt = oct_mach_info::flt_fmt_ieee_big_endian; |
|
239 else |
|
240 flt_fmt = oct_mach_info::flt_fmt_ieee_little_endian; |
|
241 |
|
242 // element type and length |
|
243 if (read_mat5_tag (is, swap, type, element_length)) |
|
244 return retval; // EOF |
|
245 |
|
246 if (type != miMATRIX) |
|
247 { |
|
248 error ("load: invalid element type"); |
|
249 goto early_read_error; |
|
250 } |
|
251 |
|
252 if (element_length == 0) |
|
253 { |
|
254 tc = Matrix (); |
|
255 return retval; |
|
256 } |
|
257 |
|
258 pos = is.tellg (); |
|
259 |
|
260 // array flags subelement |
|
261 if (read_mat5_tag (is, swap, type, len) || type != miUINT32 || len != 8) |
|
262 { |
|
263 error ("load: invalid array flags subelement"); |
|
264 goto early_read_error; |
|
265 } |
|
266 |
|
267 read_int (is, swap, flags); |
|
268 imag = (flags & 0x0800) != 0; // has an imaginary part? |
|
269 global = (flags & 0x0400) != 0; // global variable? |
|
270 logicalvar = (flags & 0x0200) != 0; // we don't use this yet |
|
271 arrayclass = (arrayclasstype)(flags & 0xff); |
|
272 read_int (is, swap, junk); // an "undefined" entry |
|
273 |
|
274 // dimensions array subelement |
|
275 { |
|
276 if (read_mat5_tag (is, swap, type, dimension_length) || type != miINT32) |
|
277 { |
|
278 error ("load: invalid dimensions array subelement"); |
|
279 goto early_read_error; |
|
280 } |
|
281 |
|
282 int ndims = dimension_length / 4; |
|
283 dims.resize (ndims); |
|
284 for (int i = 0; i < ndims; i++) |
|
285 { |
|
286 FOUR_BYTE_INT n; |
|
287 read_int (is, swap, n); |
|
288 dims(i) = n; |
|
289 } |
|
290 re.resize (dims); |
|
291 |
|
292 std::streampos tmp_pos = is.tellg (); |
|
293 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (dimension_length) - dimension_length)); |
|
294 } |
|
295 |
|
296 if (read_mat5_tag (is, swap, type, len) || type != miINT8) |
|
297 { |
|
298 error ("load: invalid array name subelement"); |
|
299 goto early_read_error; |
|
300 } |
|
301 |
|
302 { |
|
303 OCTAVE_LOCAL_BUFFER (char, name, len+1); |
|
304 |
|
305 // Structure field subelements have zero-length array name subelements. |
|
306 |
|
307 std::streampos tmp_pos = is.tellg (); |
|
308 |
|
309 if (len) |
|
310 { |
|
311 if (! is.read (X_CAST (char *, name), len )) |
|
312 goto data_read_error; |
|
313 |
|
314 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
|
315 } |
|
316 |
|
317 name[len] = '\0'; |
|
318 retval = name; |
|
319 } |
|
320 |
|
321 switch (arrayclass) |
|
322 { |
|
323 case mxCELL_CLASS: |
|
324 { |
|
325 Cell cell_array (dims); |
|
326 |
|
327 int n = cell_array.length (); |
|
328 |
|
329 for (int i = 0; i < n; i++) |
|
330 { |
|
331 octave_value tc2; |
|
332 |
|
333 std::string nm |
|
334 = read_mat5_binary_element (is, filename, swap, global, tc2); |
|
335 |
|
336 if (! is || error_state) |
|
337 { |
|
338 error ("load: reading cell data for `%s'", nm.c_str ()); |
|
339 goto data_read_error; |
|
340 } |
|
341 |
|
342 cell_array(i) = tc2; |
|
343 } |
|
344 |
|
345 tc = cell_array; |
|
346 } |
|
347 break; |
|
348 |
|
349 case mxOBJECT_CLASS: |
|
350 warning ("load: objects are not implemented"); |
|
351 goto skip_ahead; |
|
352 |
|
353 case mxSPARSE_CLASS: |
|
354 warning ("load: sparse arrays are not implemented"); |
|
355 goto skip_ahead; |
|
356 |
|
357 case mxSTRUCT_CLASS: |
|
358 { |
|
359 Octave_map m; |
|
360 FOUR_BYTE_INT fn_type; |
|
361 FOUR_BYTE_INT fn_len; |
|
362 FOUR_BYTE_INT field_name_length; |
|
363 int i; |
|
364 |
|
365 // field name length subelement -- actually the maximum length |
|
366 // of a field name. The Matlab docs promise this will always |
|
367 // be 32. We read and use the actual value, on the theory |
|
368 // that eventually someone will recognize that's a waste of |
|
369 // space. |
|
370 if (read_mat5_tag (is, swap, fn_type, fn_len) || fn_type != miINT32) |
|
371 { |
|
372 error ("load: invalid field name subelement"); |
|
373 goto data_read_error; |
|
374 } |
|
375 |
|
376 if (! is.read (X_CAST (char *, &field_name_length), fn_len )) |
|
377 goto data_read_error; |
|
378 |
|
379 if (swap) |
|
380 swap_4_bytes ((char *)&field_name_length); |
|
381 |
|
382 // field name subelement. The length of this subelement tells |
|
383 // us how many fields there are. |
|
384 if (read_mat5_tag (is, swap, fn_type, fn_len) || fn_type != miINT8) |
|
385 { |
|
386 error ("load: invalid field name subelement"); |
|
387 goto data_read_error; |
|
388 } |
|
389 |
|
390 int n_fields = fn_len/field_name_length; |
|
391 |
|
392 fn_len = PAD (fn_len); |
|
393 |
|
394 OCTAVE_LOCAL_BUFFER (char, elname, fn_len); |
|
395 |
|
396 if (! is.read (elname, fn_len)) |
|
397 goto data_read_error; |
|
398 |
|
399 int n; |
|
400 if (dims(0) == 1) |
|
401 n = dims(1); |
|
402 else if (dims(1) == 1) |
|
403 n = dims(0); |
|
404 else |
|
405 { |
|
406 error ("load: can only handle one-dimensional structure arrays"); |
|
407 goto data_read_error; |
|
408 } |
|
409 |
|
410 Cell field_elts (n_fields, n); |
|
411 |
|
412 // fields subelements |
|
413 for (int j = 0; j < n; j++) |
|
414 { |
|
415 for (i = 0; i < n_fields; i++) |
|
416 { |
|
417 octave_value fieldtc; |
|
418 read_mat5_binary_element (is, filename, swap, global, fieldtc); |
|
419 field_elts(i,j) = fieldtc; |
|
420 } |
|
421 } |
|
422 |
|
423 for (int j = n_fields-1; j >= 0; j--) |
|
424 { |
|
425 const char *key = elname + j*field_name_length; |
|
426 |
|
427 for (int k = n-1; k >=0; k--) |
|
428 m[key](k) = field_elts(j,k); |
|
429 } |
|
430 |
|
431 tc = m; |
|
432 } |
|
433 break; |
|
434 |
|
435 case mxCHAR_CLASS: |
|
436 // handle as a numerical array to start with |
|
437 |
|
438 case mxDOUBLE_CLASS: |
|
439 case mxSINGLE_CLASS: |
|
440 case mxINT8_CLASS: |
|
441 case mxUINT8_CLASS: |
|
442 case mxINT16_CLASS: |
|
443 case mxUINT16_CLASS: |
|
444 case mxINT32_CLASS: |
|
445 case mxUINT32_CLASS: |
|
446 default: |
|
447 // handle all these numerical formats as double arrays |
|
448 |
|
449 // real data subelement |
|
450 { |
|
451 std::streampos tmp_pos; |
|
452 |
|
453 if (read_mat5_tag (is, swap, type, len)) |
|
454 { |
|
455 error ("load: reading matrix data for `%s'", retval.c_str ()); |
|
456 goto data_read_error; |
|
457 } |
|
458 |
|
459 int n = re.length (); |
|
460 tmp_pos = is.tellg (); |
|
461 read_mat5_binary_data (is, re.fortran_vec (), n, swap, |
|
462 (enum mat5_data_type) type, flt_fmt); |
|
463 |
|
464 if (! is || error_state) |
|
465 { |
|
466 error ("load: reading matrix data for `%s'", retval.c_str ()); |
|
467 goto data_read_error; |
|
468 } |
|
469 |
|
470 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
|
471 } |
|
472 |
|
473 // imaginary data subelement |
|
474 if (imag) |
|
475 { |
|
476 NDArray im (dims); |
|
477 |
|
478 if (read_mat5_tag (is, swap, type, len)) |
|
479 { |
|
480 error ("load: reading matrix data for `%s'", retval.c_str ()); |
|
481 goto data_read_error; |
|
482 } |
|
483 |
|
484 int n = im.length (); |
|
485 read_mat5_binary_data (is, im.fortran_vec (), n, swap, |
|
486 (enum mat5_data_type) type, flt_fmt); |
|
487 |
|
488 if (! is || error_state) |
|
489 { |
|
490 error ("load: reading imaginary matrix data for `%s'", |
|
491 retval.c_str ()); |
|
492 goto data_read_error; |
|
493 } |
|
494 |
|
495 ComplexNDArray ctmp (dims); |
|
496 |
|
497 for (int i = 0; i < n; i++) |
|
498 ctmp(i) = Complex (re(i), im(i)); |
|
499 |
|
500 tc = ctmp; |
|
501 } |
|
502 else |
|
503 tc = re; |
|
504 |
|
505 if (arrayclass == mxCHAR_CLASS) |
|
506 tc = tc.convert_to_str (); |
|
507 } |
|
508 |
|
509 is.seekg (pos + static_cast<std::streamoff> (element_length)); |
|
510 |
|
511 if (is.eof ()) |
|
512 is.clear (); |
|
513 |
|
514 return retval; |
|
515 |
|
516 data_read_error: |
|
517 early_read_error: |
|
518 error ("load: trouble reading binary file `%s'", filename.c_str ()); |
|
519 return std::string (); |
|
520 |
|
521 skip_ahead: |
|
522 warning ("skipping over `%s'", retval.c_str ()); |
|
523 is.seekg (pos + static_cast<std::streamoff> (element_length)); |
|
524 return read_mat5_binary_element (is, filename, swap, global, tc); |
|
525 } |
|
526 |
|
527 int |
|
528 read_mat5_binary_file_header (std::istream& is, bool& swap, bool quiet) |
|
529 { |
|
530 TWO_BYTE_INT version=0, magic=0; |
|
531 |
|
532 is.seekg (124, std::ios::beg); |
|
533 is.read (X_CAST (char *, &version), 2); |
|
534 is.read (X_CAST (char *, &magic), 2); |
|
535 |
|
536 if (magic == 0x4d49) |
|
537 swap = 0; |
|
538 else if (magic == 0x494d) |
|
539 swap = 1; |
|
540 else |
|
541 { |
|
542 if (! quiet) |
|
543 error ("load: can't read binary file"); |
|
544 return -1; |
|
545 } |
|
546 |
|
547 if (! swap) // version number is inverse swapped! |
|
548 version = ((version >> 8) & 0xff) + ((version & 0xff) << 8); |
|
549 |
|
550 if (version != 1 && !quiet) |
|
551 warning ("load: found version %d binary MAT file, " |
|
552 "but only prepared for version 1", version); |
|
553 |
|
554 return 0; |
|
555 } |
|
556 |
|
557 static int |
|
558 write_mat5_tag (std::ostream& is, int type, int bytes) |
|
559 { |
|
560 FOUR_BYTE_INT temp; |
|
561 |
|
562 if (bytes <= 4) |
|
563 temp = (bytes << 16) + type; |
|
564 else |
|
565 { |
|
566 temp = type; |
|
567 if (! is.write ((char *)&temp, 4)) |
|
568 goto data_write_error; |
|
569 temp = bytes; |
|
570 } |
|
571 |
|
572 if (! is.write ((char *)&temp, 4)) |
|
573 goto data_write_error; |
|
574 |
|
575 return 0; |
|
576 |
|
577 data_write_error: |
|
578 return 1; |
|
579 } |
|
580 |
|
581 // write out the numeric values in M to OS, |
|
582 // preceded by the appropriate tag. |
|
583 static void |
|
584 write_mat5_array (std::ostream& os, const NDArray& m, bool save_as_floats) |
|
585 { |
|
586 int nel = m.nelem (); |
|
587 double max_val, min_val; |
|
588 save_type st = LS_DOUBLE; |
|
589 mat5_data_type mst; |
|
590 int size; |
|
591 unsigned len; |
|
592 const double *data = m.data (); |
|
593 |
|
594 // Have to use copy here to avoid writing over data accessed via |
|
595 // Matrix::data(). |
|
596 |
|
597 #define MAT5_DO_WRITE(TYPE, data, count, stream) \ |
|
598 do \ |
|
599 { \ |
|
600 OCTAVE_LOCAL_BUFFER (TYPE, ptr, count); \ |
|
601 for (int i = 0; i < count; i++) \ |
|
602 ptr[i] = X_CAST (TYPE, data[i]); \ |
|
603 stream.write (X_CAST (char *, ptr), count * sizeof (TYPE)); \ |
|
604 } \ |
|
605 while (0) |
|
606 |
|
607 if (save_as_floats) |
|
608 { |
|
609 if (m.too_large_for_float ()) |
|
610 { |
|
611 warning ("save: some values too large to save as floats --"); |
|
612 warning ("save: saving as doubles instead"); |
|
613 } |
|
614 else |
|
615 st = LS_FLOAT; |
|
616 } |
|
617 |
|
618 if (m.all_integers (max_val, min_val)) |
|
619 st = get_save_type (max_val, min_val); |
|
620 |
|
621 switch (st) |
|
622 { |
|
623 default: |
|
624 case LS_DOUBLE: mst = miDOUBLE; size = 8; break; |
|
625 case LS_FLOAT: mst = miSINGLE; size = 4; break; |
|
626 case LS_U_CHAR: mst = miUINT8; size = 1; break; |
|
627 case LS_U_SHORT: mst = miUINT16; size = 2; break; |
|
628 case LS_U_INT: mst = miUINT32; size = 4; break; |
|
629 case LS_CHAR: mst = miINT8; size = 1; break; |
|
630 case LS_SHORT: mst = miINT16; size = 2; break; |
|
631 case LS_INT: mst = miINT32; size = 4; break; |
|
632 } |
|
633 |
|
634 len = nel*size; |
|
635 write_mat5_tag (os, mst, len); |
|
636 |
|
637 { |
|
638 switch (st) |
|
639 { |
|
640 case LS_U_CHAR: |
|
641 MAT5_DO_WRITE (unsigned char, data, nel, os); |
|
642 break; |
|
643 |
|
644 case LS_U_SHORT: |
|
645 MAT5_DO_WRITE (unsigned TWO_BYTE_INT, data, nel, os); |
|
646 break; |
|
647 |
|
648 case LS_U_INT: |
|
649 MAT5_DO_WRITE (unsigned FOUR_BYTE_INT, data, nel, os); |
|
650 break; |
|
651 |
|
652 // provide for 64 bit ints, even though get_save_type does |
|
653 // not yet implement them |
|
654 #ifdef EIGHT_BYTE_INT |
|
655 case LS_U_LONG: |
|
656 MAT5_DO_WRITE (unsigned EIGHT_BYTE_INT, data, nel, os); |
|
657 break; |
|
658 #endif |
|
659 |
|
660 case LS_CHAR: |
|
661 MAT5_DO_WRITE (signed char, data, nel, os); |
|
662 break; |
|
663 |
|
664 case LS_SHORT: |
|
665 MAT5_DO_WRITE (TWO_BYTE_INT, data, nel, os); |
|
666 break; |
|
667 |
|
668 case LS_INT: |
|
669 MAT5_DO_WRITE (FOUR_BYTE_INT, data, nel, os); |
|
670 break; |
|
671 |
|
672 #ifdef EIGHT_BYTE_INT |
|
673 case LS_LONG: |
|
674 MAT5_DO_WRITE (EIGHT_BYTE_INT, data, nel, os); |
|
675 break; |
|
676 #endif |
|
677 |
|
678 case LS_FLOAT: |
|
679 MAT5_DO_WRITE (float, data, nel, os); |
|
680 break; |
|
681 |
|
682 case LS_DOUBLE: // No conversion necessary. |
|
683 os.write (X_CAST (char *, data), len); |
|
684 break; |
|
685 |
|
686 default: |
|
687 (*current_liboctave_error_handler) |
|
688 ("unrecognized data format requested"); |
|
689 break; |
|
690 } |
|
691 } |
|
692 if (PAD (len) > len) |
|
693 { |
|
694 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
695 os.write (buf, PAD (len) - len); |
|
696 } |
|
697 } |
|
698 |
|
699 // Write out cell element values in the cell array to OS, preceded by |
|
700 // the appropriate tag. |
|
701 |
|
702 static bool |
|
703 write_mat5_cell_array (std::ostream& os, Cell& cell, bool mark_as_global, |
|
704 bool save_as_floats) |
|
705 { |
|
706 int nel = cell.nelem (); |
|
707 |
|
708 for (int i = 0; i < nel; i++) |
|
709 { |
|
710 octave_value ov = cell(i); |
|
711 |
|
712 if (! save_mat5_binary_element (os, ov, "", mark_as_global, |
|
713 save_as_floats)) |
|
714 return false; |
|
715 } |
|
716 |
|
717 return true; |
|
718 } |
|
719 |
|
720 // save the data from TC along with the corresponding NAME on stream |
|
721 // OS in the MatLab version 5 binary format. Return true on success. |
|
722 |
|
723 bool |
|
724 save_mat5_binary_element (std::ostream& os, |
|
725 const octave_value& tc, const std::string& name, |
|
726 bool mark_as_global, bool save_as_floats) |
|
727 { |
|
728 FOUR_BYTE_INT flags=0; |
|
729 FOUR_BYTE_INT junk=0; |
|
730 std::streampos fixup, contin; |
|
731 |
|
732 // element type and length |
|
733 fixup = os.tellp (); |
|
734 write_mat5_tag (os, miMATRIX, 99); // we don't know the real length yet |
|
735 |
|
736 // array flags subelement |
|
737 write_mat5_tag (os, miUINT32, 8); |
|
738 |
|
739 if (mark_as_global) |
|
740 flags |= 0x0400; |
|
741 |
|
742 if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
743 flags |= 0x0800; |
|
744 |
|
745 if (tc.is_string ()) |
|
746 flags |= mxCHAR_CLASS; |
|
747 else if (tc.is_real_scalar ()) |
|
748 flags |= mxDOUBLE_CLASS; |
|
749 else if (tc.is_real_matrix () || tc.is_range ()) |
|
750 flags |= mxDOUBLE_CLASS; |
|
751 else if (tc.is_complex_scalar ()) |
|
752 flags |= mxDOUBLE_CLASS; |
|
753 else if (tc.is_complex_matrix ()) |
|
754 flags |= mxDOUBLE_CLASS; |
|
755 else if (tc.is_map ()) |
|
756 flags |= mxSTRUCT_CLASS; |
|
757 else if (tc.is_cell ()) |
|
758 flags |= mxCELL_CLASS; |
|
759 else |
|
760 { |
|
761 gripe_wrong_type_arg ("save", tc, false); |
|
762 goto error_cleanup; |
|
763 } |
|
764 |
|
765 os.write ((char *)&flags, 4); |
|
766 os.write ((char *)&junk, 4); |
|
767 |
|
768 { |
|
769 dim_vector dv = tc.dims (); |
|
770 int nd = tc.ndims (); |
|
771 |
|
772 write_mat5_tag (os, miINT32, 8); |
|
773 |
|
774 for (int i = 0; i < nd; i++) |
|
775 { |
|
776 int n = dv(i); |
|
777 os.write ((char *)&n, 4); |
|
778 } |
|
779 } |
|
780 |
|
781 // array name subelement |
|
782 { |
|
783 int namelen = name.length (); |
|
784 |
|
785 if (namelen > 31) |
|
786 namelen = 31; // only 31 char names permitted in mat file |
|
787 |
|
788 int paddedlength = PAD (namelen); |
|
789 |
|
790 write_mat5_tag (os, miINT8, namelen); |
|
791 OCTAVE_LOCAL_BUFFER (char, paddedname, paddedlength); |
|
792 memset (paddedname, 0, paddedlength); |
|
793 strncpy (paddedname, name.c_str (), namelen); |
|
794 os.write (paddedname, paddedlength); |
|
795 } |
|
796 |
|
797 // data element |
|
798 if (tc.is_string ()) |
|
799 { |
|
800 charMatrix chm = tc.char_matrix_value (); |
|
801 int nr = chm.rows (); |
|
802 int nc = chm.cols (); |
|
803 int len = nr*nc*2; |
|
804 int paddedlength = PAD (nr*nc*2); |
|
805 |
|
806 OCTAVE_LOCAL_BUFFER (TWO_BYTE_INT, buf, nc*nr+3); |
|
807 write_mat5_tag (os, miUINT16, len); |
|
808 |
|
809 for (int i = 0; i < nr; i++) |
|
810 { |
|
811 std::string tstr = chm.row_as_string (i); |
|
812 const char *s = tstr.data (); |
|
813 |
|
814 for (int j = 0; j < nc; j++) |
|
815 buf[j*nr+i] = *s++ & 0x00FF; |
|
816 } |
|
817 os.write ((char *)buf, nr*nc*2); |
|
818 |
|
819 if (paddedlength > len) |
|
820 os.write ((char *)buf, paddedlength - len); |
|
821 } |
|
822 else if (tc.is_real_scalar () || tc.is_real_matrix () || tc.is_range ()) |
|
823 { |
|
824 NDArray m = tc.array_value (); |
|
825 |
|
826 write_mat5_array (os, m, save_as_floats); |
|
827 } |
|
828 else if (tc.is_cell ()) |
|
829 { |
|
830 Cell cell = tc.cell_value (); |
|
831 |
|
832 if (! write_mat5_cell_array (os, cell, mark_as_global, save_as_floats)) |
|
833 goto error_cleanup; |
|
834 } |
|
835 else if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
836 { |
|
837 ComplexNDArray m_cmplx = tc.complex_matrix_value (); |
|
838 |
|
839 write_mat5_array (os, ::real (m_cmplx), save_as_floats); |
|
840 write_mat5_array (os, ::imag (m_cmplx), save_as_floats); |
|
841 } |
|
842 else if (tc.is_map ()) |
|
843 { |
|
844 // an Octave structure */ |
|
845 // recursively write each element of the structure |
|
846 Octave_map m = tc.map_value (); |
|
847 |
|
848 { |
|
849 char buf[32]; |
|
850 FOUR_BYTE_INT maxfieldnamelength = 32; |
|
851 int fieldcnt = 0; |
|
852 |
|
853 for (Octave_map::iterator i = m.begin (); i != m.end (); i++) |
|
854 fieldcnt++; |
|
855 |
|
856 write_mat5_tag (os, miINT32, 4); |
|
857 os.write ((char *)&maxfieldnamelength, 4); |
|
858 write_mat5_tag (os, miINT8, fieldcnt*32); |
|
859 |
|
860 for (Octave_map::iterator i = m.begin (); i != m.end (); i++) |
|
861 { |
|
862 // write the name of each element |
|
863 std::string tstr = m.key (i); |
|
864 memset (buf, 0, 32); |
|
865 strncpy (buf, tstr.c_str (), 31); // only 31 char names permitted |
|
866 os.write (buf, 32); |
|
867 } |
|
868 |
|
869 int len = m.numel (); |
|
870 |
|
871 for (Octave_map::iterator i = m.begin (); i != m.end (); i++) |
|
872 { |
|
873 // write the data of each element |
|
874 Cell elts = m.contents (i); |
|
875 |
|
876 for (int j = 0; j < len; j++) |
|
877 { |
|
878 bool retval2 = save_mat5_binary_element (os, elts(j), "", |
|
879 mark_as_global, |
|
880 save_as_floats); |
|
881 if (! retval2) |
|
882 goto error_cleanup; |
|
883 } |
|
884 } |
|
885 } |
|
886 } |
|
887 else |
|
888 gripe_wrong_type_arg ("save", tc, false); |
|
889 |
|
890 contin = os.tellp (); |
|
891 os.seekp (fixup); |
|
892 write_mat5_tag (os, miMATRIX, |
|
893 static_cast<int>(contin - fixup) - 8); // the actual length |
|
894 os.seekp (contin); |
|
895 |
|
896 return true; |
|
897 |
|
898 error_cleanup: |
|
899 error ("save: error while writing `%s' to MAT file", name.c_str ()); |
|
900 |
|
901 return false; |
|
902 } |
|
903 |
|
904 /* |
|
905 ;;; Local Variables: *** |
|
906 ;;; mode: C++ *** |
|
907 ;;; End: *** |
|
908 */ |
|
909 |