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