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 |
4675
|
428 Cell c (dim_vector (n, 1)); |
|
429 |
4634
|
430 for (int k = n-1; k >=0; k--) |
4675
|
431 c(k) = field_elts(j,k); |
|
432 |
|
433 m.assign (key, c); |
4634
|
434 } |
|
435 |
|
436 tc = m; |
|
437 } |
|
438 break; |
|
439 |
|
440 case mxCHAR_CLASS: |
|
441 // handle as a numerical array to start with |
|
442 |
|
443 case mxDOUBLE_CLASS: |
|
444 case mxSINGLE_CLASS: |
|
445 case mxINT8_CLASS: |
|
446 case mxUINT8_CLASS: |
|
447 case mxINT16_CLASS: |
|
448 case mxUINT16_CLASS: |
|
449 case mxINT32_CLASS: |
|
450 case mxUINT32_CLASS: |
|
451 default: |
|
452 // handle all these numerical formats as double arrays |
|
453 |
|
454 // real data subelement |
|
455 { |
|
456 std::streampos tmp_pos; |
|
457 |
|
458 if (read_mat5_tag (is, swap, type, len)) |
|
459 { |
|
460 error ("load: reading matrix data for `%s'", retval.c_str ()); |
|
461 goto data_read_error; |
|
462 } |
|
463 |
|
464 int n = re.length (); |
|
465 tmp_pos = is.tellg (); |
|
466 read_mat5_binary_data (is, re.fortran_vec (), n, swap, |
|
467 (enum mat5_data_type) type, flt_fmt); |
|
468 |
|
469 if (! is || error_state) |
|
470 { |
|
471 error ("load: reading matrix data for `%s'", retval.c_str ()); |
|
472 goto data_read_error; |
|
473 } |
|
474 |
|
475 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
|
476 } |
|
477 |
|
478 // imaginary data subelement |
|
479 if (imag) |
|
480 { |
|
481 NDArray im (dims); |
|
482 |
|
483 if (read_mat5_tag (is, swap, type, len)) |
|
484 { |
|
485 error ("load: reading matrix data for `%s'", retval.c_str ()); |
|
486 goto data_read_error; |
|
487 } |
|
488 |
|
489 int n = im.length (); |
|
490 read_mat5_binary_data (is, im.fortran_vec (), n, swap, |
|
491 (enum mat5_data_type) type, flt_fmt); |
|
492 |
|
493 if (! is || error_state) |
|
494 { |
|
495 error ("load: reading imaginary matrix data for `%s'", |
|
496 retval.c_str ()); |
|
497 goto data_read_error; |
|
498 } |
|
499 |
|
500 ComplexNDArray ctmp (dims); |
|
501 |
|
502 for (int i = 0; i < n; i++) |
|
503 ctmp(i) = Complex (re(i), im(i)); |
|
504 |
|
505 tc = ctmp; |
|
506 } |
|
507 else |
|
508 tc = re; |
|
509 |
|
510 if (arrayclass == mxCHAR_CLASS) |
|
511 tc = tc.convert_to_str (); |
|
512 } |
|
513 |
|
514 is.seekg (pos + static_cast<std::streamoff> (element_length)); |
|
515 |
|
516 if (is.eof ()) |
|
517 is.clear (); |
|
518 |
|
519 return retval; |
|
520 |
|
521 data_read_error: |
|
522 early_read_error: |
|
523 error ("load: trouble reading binary file `%s'", filename.c_str ()); |
|
524 return std::string (); |
|
525 |
|
526 skip_ahead: |
|
527 warning ("skipping over `%s'", retval.c_str ()); |
|
528 is.seekg (pos + static_cast<std::streamoff> (element_length)); |
|
529 return read_mat5_binary_element (is, filename, swap, global, tc); |
|
530 } |
|
531 |
|
532 int |
|
533 read_mat5_binary_file_header (std::istream& is, bool& swap, bool quiet) |
|
534 { |
|
535 TWO_BYTE_INT version=0, magic=0; |
|
536 |
|
537 is.seekg (124, std::ios::beg); |
|
538 is.read (X_CAST (char *, &version), 2); |
|
539 is.read (X_CAST (char *, &magic), 2); |
|
540 |
|
541 if (magic == 0x4d49) |
|
542 swap = 0; |
|
543 else if (magic == 0x494d) |
|
544 swap = 1; |
|
545 else |
|
546 { |
|
547 if (! quiet) |
|
548 error ("load: can't read binary file"); |
|
549 return -1; |
|
550 } |
|
551 |
|
552 if (! swap) // version number is inverse swapped! |
|
553 version = ((version >> 8) & 0xff) + ((version & 0xff) << 8); |
|
554 |
|
555 if (version != 1 && !quiet) |
|
556 warning ("load: found version %d binary MAT file, " |
|
557 "but only prepared for version 1", version); |
|
558 |
|
559 return 0; |
|
560 } |
|
561 |
|
562 static int |
|
563 write_mat5_tag (std::ostream& is, int type, int bytes) |
|
564 { |
|
565 FOUR_BYTE_INT temp; |
|
566 |
|
567 if (bytes <= 4) |
|
568 temp = (bytes << 16) + type; |
|
569 else |
|
570 { |
|
571 temp = type; |
|
572 if (! is.write ((char *)&temp, 4)) |
|
573 goto data_write_error; |
|
574 temp = bytes; |
|
575 } |
|
576 |
|
577 if (! is.write ((char *)&temp, 4)) |
|
578 goto data_write_error; |
|
579 |
|
580 return 0; |
|
581 |
|
582 data_write_error: |
|
583 return 1; |
|
584 } |
|
585 |
|
586 // write out the numeric values in M to OS, |
|
587 // preceded by the appropriate tag. |
|
588 static void |
|
589 write_mat5_array (std::ostream& os, const NDArray& m, bool save_as_floats) |
|
590 { |
|
591 int nel = m.nelem (); |
|
592 double max_val, min_val; |
|
593 save_type st = LS_DOUBLE; |
|
594 mat5_data_type mst; |
|
595 int size; |
|
596 unsigned len; |
|
597 const double *data = m.data (); |
|
598 |
|
599 // Have to use copy here to avoid writing over data accessed via |
|
600 // Matrix::data(). |
|
601 |
|
602 #define MAT5_DO_WRITE(TYPE, data, count, stream) \ |
|
603 do \ |
|
604 { \ |
|
605 OCTAVE_LOCAL_BUFFER (TYPE, ptr, count); \ |
|
606 for (int i = 0; i < count; i++) \ |
|
607 ptr[i] = X_CAST (TYPE, data[i]); \ |
|
608 stream.write (X_CAST (char *, ptr), count * sizeof (TYPE)); \ |
|
609 } \ |
|
610 while (0) |
|
611 |
|
612 if (save_as_floats) |
|
613 { |
|
614 if (m.too_large_for_float ()) |
|
615 { |
|
616 warning ("save: some values too large to save as floats --"); |
|
617 warning ("save: saving as doubles instead"); |
|
618 } |
|
619 else |
|
620 st = LS_FLOAT; |
|
621 } |
|
622 |
|
623 if (m.all_integers (max_val, min_val)) |
|
624 st = get_save_type (max_val, min_val); |
|
625 |
|
626 switch (st) |
|
627 { |
|
628 default: |
|
629 case LS_DOUBLE: mst = miDOUBLE; size = 8; break; |
|
630 case LS_FLOAT: mst = miSINGLE; size = 4; break; |
|
631 case LS_U_CHAR: mst = miUINT8; size = 1; break; |
|
632 case LS_U_SHORT: mst = miUINT16; size = 2; break; |
|
633 case LS_U_INT: mst = miUINT32; size = 4; break; |
|
634 case LS_CHAR: mst = miINT8; size = 1; break; |
|
635 case LS_SHORT: mst = miINT16; size = 2; break; |
|
636 case LS_INT: mst = miINT32; size = 4; break; |
|
637 } |
|
638 |
|
639 len = nel*size; |
|
640 write_mat5_tag (os, mst, len); |
|
641 |
|
642 { |
|
643 switch (st) |
|
644 { |
|
645 case LS_U_CHAR: |
|
646 MAT5_DO_WRITE (unsigned char, data, nel, os); |
|
647 break; |
|
648 |
|
649 case LS_U_SHORT: |
|
650 MAT5_DO_WRITE (unsigned TWO_BYTE_INT, data, nel, os); |
|
651 break; |
|
652 |
|
653 case LS_U_INT: |
|
654 MAT5_DO_WRITE (unsigned FOUR_BYTE_INT, data, nel, os); |
|
655 break; |
|
656 |
|
657 // provide for 64 bit ints, even though get_save_type does |
|
658 // not yet implement them |
|
659 #ifdef EIGHT_BYTE_INT |
|
660 case LS_U_LONG: |
|
661 MAT5_DO_WRITE (unsigned EIGHT_BYTE_INT, data, nel, os); |
|
662 break; |
|
663 #endif |
|
664 |
|
665 case LS_CHAR: |
|
666 MAT5_DO_WRITE (signed char, data, nel, os); |
|
667 break; |
|
668 |
|
669 case LS_SHORT: |
|
670 MAT5_DO_WRITE (TWO_BYTE_INT, data, nel, os); |
|
671 break; |
|
672 |
|
673 case LS_INT: |
|
674 MAT5_DO_WRITE (FOUR_BYTE_INT, data, nel, os); |
|
675 break; |
|
676 |
|
677 #ifdef EIGHT_BYTE_INT |
|
678 case LS_LONG: |
|
679 MAT5_DO_WRITE (EIGHT_BYTE_INT, data, nel, os); |
|
680 break; |
|
681 #endif |
|
682 |
|
683 case LS_FLOAT: |
|
684 MAT5_DO_WRITE (float, data, nel, os); |
|
685 break; |
|
686 |
|
687 case LS_DOUBLE: // No conversion necessary. |
|
688 os.write (X_CAST (char *, data), len); |
|
689 break; |
|
690 |
|
691 default: |
|
692 (*current_liboctave_error_handler) |
|
693 ("unrecognized data format requested"); |
|
694 break; |
|
695 } |
|
696 } |
|
697 if (PAD (len) > len) |
|
698 { |
|
699 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
700 os.write (buf, PAD (len) - len); |
|
701 } |
|
702 } |
|
703 |
|
704 // Write out cell element values in the cell array to OS, preceded by |
|
705 // the appropriate tag. |
|
706 |
|
707 static bool |
|
708 write_mat5_cell_array (std::ostream& os, Cell& cell, bool mark_as_global, |
|
709 bool save_as_floats) |
|
710 { |
|
711 int nel = cell.nelem (); |
|
712 |
|
713 for (int i = 0; i < nel; i++) |
|
714 { |
|
715 octave_value ov = cell(i); |
|
716 |
|
717 if (! save_mat5_binary_element (os, ov, "", mark_as_global, |
|
718 save_as_floats)) |
|
719 return false; |
|
720 } |
|
721 |
|
722 return true; |
|
723 } |
|
724 |
|
725 // save the data from TC along with the corresponding NAME on stream |
|
726 // OS in the MatLab version 5 binary format. Return true on success. |
|
727 |
|
728 bool |
|
729 save_mat5_binary_element (std::ostream& os, |
|
730 const octave_value& tc, const std::string& name, |
|
731 bool mark_as_global, bool save_as_floats) |
|
732 { |
|
733 FOUR_BYTE_INT flags=0; |
|
734 FOUR_BYTE_INT junk=0; |
|
735 std::streampos fixup, contin; |
|
736 |
|
737 // element type and length |
|
738 fixup = os.tellp (); |
|
739 write_mat5_tag (os, miMATRIX, 99); // we don't know the real length yet |
|
740 |
|
741 // array flags subelement |
|
742 write_mat5_tag (os, miUINT32, 8); |
|
743 |
|
744 if (mark_as_global) |
|
745 flags |= 0x0400; |
|
746 |
|
747 if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
748 flags |= 0x0800; |
|
749 |
|
750 if (tc.is_string ()) |
|
751 flags |= mxCHAR_CLASS; |
|
752 else if (tc.is_real_scalar ()) |
|
753 flags |= mxDOUBLE_CLASS; |
|
754 else if (tc.is_real_matrix () || tc.is_range ()) |
|
755 flags |= mxDOUBLE_CLASS; |
|
756 else if (tc.is_complex_scalar ()) |
|
757 flags |= mxDOUBLE_CLASS; |
|
758 else if (tc.is_complex_matrix ()) |
|
759 flags |= mxDOUBLE_CLASS; |
|
760 else if (tc.is_map ()) |
|
761 flags |= mxSTRUCT_CLASS; |
|
762 else if (tc.is_cell ()) |
|
763 flags |= mxCELL_CLASS; |
|
764 else |
|
765 { |
|
766 gripe_wrong_type_arg ("save", tc, false); |
|
767 goto error_cleanup; |
|
768 } |
|
769 |
|
770 os.write ((char *)&flags, 4); |
|
771 os.write ((char *)&junk, 4); |
|
772 |
|
773 { |
|
774 dim_vector dv = tc.dims (); |
|
775 int nd = tc.ndims (); |
4638
|
776 int dim_len = 4*nd; |
4634
|
777 |
4638
|
778 write_mat5_tag (os, miINT32, dim_len); |
4634
|
779 |
|
780 for (int i = 0; i < nd; i++) |
|
781 { |
4638
|
782 FOUR_BYTE_INT n = dv(i); |
4634
|
783 os.write ((char *)&n, 4); |
|
784 } |
4638
|
785 |
|
786 if (PAD (dim_len) > dim_len) |
|
787 { |
|
788 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
789 os.write (buf, PAD (dim_len) - dim_len); |
|
790 } |
4634
|
791 } |
|
792 |
|
793 // array name subelement |
|
794 { |
|
795 int namelen = name.length (); |
|
796 |
|
797 if (namelen > 31) |
|
798 namelen = 31; // only 31 char names permitted in mat file |
|
799 |
|
800 int paddedlength = PAD (namelen); |
|
801 |
|
802 write_mat5_tag (os, miINT8, namelen); |
|
803 OCTAVE_LOCAL_BUFFER (char, paddedname, paddedlength); |
|
804 memset (paddedname, 0, paddedlength); |
|
805 strncpy (paddedname, name.c_str (), namelen); |
|
806 os.write (paddedname, paddedlength); |
|
807 } |
|
808 |
|
809 // data element |
|
810 if (tc.is_string ()) |
|
811 { |
|
812 charMatrix chm = tc.char_matrix_value (); |
|
813 int nr = chm.rows (); |
|
814 int nc = chm.cols (); |
|
815 int len = nr*nc*2; |
|
816 int paddedlength = PAD (nr*nc*2); |
|
817 |
|
818 OCTAVE_LOCAL_BUFFER (TWO_BYTE_INT, buf, nc*nr+3); |
|
819 write_mat5_tag (os, miUINT16, len); |
|
820 |
|
821 for (int i = 0; i < nr; i++) |
|
822 { |
|
823 std::string tstr = chm.row_as_string (i); |
|
824 const char *s = tstr.data (); |
|
825 |
|
826 for (int j = 0; j < nc; j++) |
|
827 buf[j*nr+i] = *s++ & 0x00FF; |
|
828 } |
|
829 os.write ((char *)buf, nr*nc*2); |
|
830 |
|
831 if (paddedlength > len) |
|
832 os.write ((char *)buf, paddedlength - len); |
|
833 } |
|
834 else if (tc.is_real_scalar () || tc.is_real_matrix () || tc.is_range ()) |
|
835 { |
|
836 NDArray m = tc.array_value (); |
|
837 |
|
838 write_mat5_array (os, m, save_as_floats); |
|
839 } |
|
840 else if (tc.is_cell ()) |
|
841 { |
|
842 Cell cell = tc.cell_value (); |
|
843 |
|
844 if (! write_mat5_cell_array (os, cell, mark_as_global, save_as_floats)) |
|
845 goto error_cleanup; |
|
846 } |
|
847 else if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
848 { |
|
849 ComplexNDArray m_cmplx = tc.complex_matrix_value (); |
|
850 |
|
851 write_mat5_array (os, ::real (m_cmplx), save_as_floats); |
|
852 write_mat5_array (os, ::imag (m_cmplx), save_as_floats); |
|
853 } |
|
854 else if (tc.is_map ()) |
|
855 { |
|
856 // an Octave structure */ |
|
857 // recursively write each element of the structure |
4675
|
858 const Octave_map m = tc.map_value (); |
4634
|
859 |
|
860 { |
|
861 char buf[32]; |
|
862 FOUR_BYTE_INT maxfieldnamelength = 32; |
|
863 int fieldcnt = 0; |
|
864 |
4675
|
865 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
4634
|
866 fieldcnt++; |
|
867 |
|
868 write_mat5_tag (os, miINT32, 4); |
|
869 os.write ((char *)&maxfieldnamelength, 4); |
|
870 write_mat5_tag (os, miINT8, fieldcnt*32); |
|
871 |
4675
|
872 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
4634
|
873 { |
|
874 // write the name of each element |
|
875 std::string tstr = m.key (i); |
|
876 memset (buf, 0, 32); |
|
877 strncpy (buf, tstr.c_str (), 31); // only 31 char names permitted |
|
878 os.write (buf, 32); |
|
879 } |
|
880 |
|
881 int len = m.numel (); |
|
882 |
4675
|
883 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
4634
|
884 { |
|
885 // write the data of each element |
|
886 Cell elts = m.contents (i); |
|
887 |
|
888 for (int j = 0; j < len; j++) |
|
889 { |
|
890 bool retval2 = save_mat5_binary_element (os, elts(j), "", |
|
891 mark_as_global, |
|
892 save_as_floats); |
|
893 if (! retval2) |
|
894 goto error_cleanup; |
|
895 } |
|
896 } |
|
897 } |
|
898 } |
|
899 else |
|
900 gripe_wrong_type_arg ("save", tc, false); |
|
901 |
|
902 contin = os.tellp (); |
|
903 os.seekp (fixup); |
|
904 write_mat5_tag (os, miMATRIX, |
|
905 static_cast<int>(contin - fixup) - 8); // the actual length |
|
906 os.seekp (contin); |
|
907 |
|
908 return true; |
|
909 |
|
910 error_cleanup: |
|
911 error ("save: error while writing `%s' to MAT file", name.c_str ()); |
|
912 |
|
913 return false; |
|
914 } |
|
915 |
|
916 /* |
|
917 ;;; Local Variables: *** |
|
918 ;;; mode: C++ *** |
|
919 ;;; End: *** |
|
920 */ |
|
921 |