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 |
5164
|
167 read_mat5_integer_data (std::istream& is, T *m, int count, bool swap, |
5089
|
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: |
5164
|
191 READ_INTEGER_DATA (signed char, swap, m, 1, count, is); |
5089
|
192 break; |
|
193 |
|
194 case miUINT8: |
5164
|
195 READ_INTEGER_DATA (unsigned char, swap, m, 1, count, is); |
5089
|
196 break; |
|
197 |
|
198 case miINT16: |
5164
|
199 READ_INTEGER_DATA (signed TWO_BYTE_INT, swap, m, 2, count, is); |
5089
|
200 break; |
|
201 |
|
202 case miUINT16: |
5164
|
203 READ_INTEGER_DATA (unsigned TWO_BYTE_INT, swap, m, 2, count, is); |
5089
|
204 break; |
|
205 |
|
206 case miINT32: |
5164
|
207 READ_INTEGER_DATA (signed FOUR_BYTE_INT, swap, m, 4, count, is); |
5089
|
208 break; |
|
209 |
|
210 case miUINT32: |
5164
|
211 READ_INTEGER_DATA (unsigned FOUR_BYTE_INT, swap, m, 4, count, is); |
5089
|
212 break; |
|
213 |
|
214 case miSINGLE: |
|
215 case miRESERVE1: |
|
216 case miDOUBLE: |
|
217 case miRESERVE2: |
|
218 case miRESERVE3: |
|
219 break; |
|
220 |
|
221 case miINT64: |
|
222 #ifdef EIGHT_BYTE_INT |
5164
|
223 READ_INTEGER_DATA (signed EIGHT_BYTE_INT, swap, m, 8, count, is); |
5089
|
224 #endif |
|
225 break; |
|
226 |
|
227 case miUINT64: |
|
228 #ifdef EIGHT_BYTE_INT |
5164
|
229 READ_INTEGER_DATA (unsigned EIGHT_BYTE_INT, swap, m, 8, count, is); |
5089
|
230 #endif |
|
231 break; |
|
232 |
|
233 case miMATRIX: |
|
234 default: |
|
235 break; |
|
236 } |
|
237 |
|
238 #undef READ_INTEGER_DATA |
|
239 |
|
240 } |
|
241 |
5164
|
242 template void read_mat5_integer_data (std::istream& is, octave_int8 *m, |
5089
|
243 int count, bool swap, |
|
244 mat5_data_type type); |
5164
|
245 template void read_mat5_integer_data (std::istream& is, octave_int16 *m, |
5089
|
246 int count, bool swap, |
|
247 mat5_data_type type); |
5164
|
248 template void read_mat5_integer_data (std::istream& is, octave_int32 *m, |
|
249 int count, bool swap, |
|
250 mat5_data_type type); |
|
251 template void read_mat5_integer_data (std::istream& is, octave_int64 *m, |
5089
|
252 int count, bool swap, |
|
253 mat5_data_type type); |
5164
|
254 template void read_mat5_integer_data (std::istream& is, octave_uint8 *m, |
5089
|
255 int count, bool swap, |
|
256 mat5_data_type type); |
5164
|
257 template void read_mat5_integer_data (std::istream& is, octave_uint16 *m, |
5089
|
258 int count, bool swap, |
|
259 mat5_data_type type); |
5164
|
260 template void read_mat5_integer_data (std::istream& is, octave_uint32 *m, |
5089
|
261 int count, bool swap, |
|
262 mat5_data_type type); |
5164
|
263 template void read_mat5_integer_data (std::istream& is, octave_uint64 *m, |
5089
|
264 int count, bool swap, |
|
265 mat5_data_type type); |
5164
|
266 |
|
267 template void read_mat5_integer_data (std::istream& is, int *m, |
5089
|
268 int count, bool swap, |
|
269 mat5_data_type type); |
|
270 |
|
271 #define OCTAVE_MAT5_INTEGER_READ(TYP) \ |
|
272 { \ |
|
273 TYP re (dims); \ |
|
274 \ |
|
275 std::streampos tmp_pos; \ |
|
276 \ |
|
277 if (read_mat5_tag (is, swap, type, len)) \ |
|
278 { \ |
|
279 error ("load: reading matrix data for `%s'", retval.c_str ()); \ |
|
280 goto data_read_error; \ |
|
281 } \ |
|
282 \ |
|
283 int n = re.length (); \ |
|
284 tmp_pos = is.tellg (); \ |
5164
|
285 read_mat5_integer_data (is, re.fortran_vec (), n, swap, \ |
5089
|
286 (enum mat5_data_type) type); \ |
|
287 \ |
|
288 if (! is || error_state) \ |
|
289 { \ |
|
290 error ("load: reading matrix data for `%s'", retval.c_str ()); \ |
|
291 goto data_read_error; \ |
|
292 } \ |
|
293 \ |
|
294 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); \ |
|
295 \ |
|
296 if (imag) \ |
|
297 { \ |
|
298 /* We don't handle imag integer types, convert to an array */ \ |
|
299 NDArray im (dims); \ |
|
300 \ |
|
301 if (read_mat5_tag (is, swap, type, len)) \ |
|
302 { \ |
|
303 error ("load: reading matrix data for `%s'", \ |
|
304 retval.c_str ()); \ |
|
305 goto data_read_error; \ |
|
306 } \ |
|
307 \ |
|
308 n = im.length (); \ |
|
309 read_mat5_binary_data (is, im.fortran_vec (), n, swap, \ |
|
310 (enum mat5_data_type) type, flt_fmt); \ |
|
311 \ |
|
312 if (! is || error_state) \ |
|
313 { \ |
|
314 error ("load: reading imaginary matrix data for `%s'", \ |
|
315 retval.c_str ()); \ |
|
316 goto data_read_error; \ |
|
317 } \ |
|
318 \ |
|
319 ComplexNDArray ctmp (dims); \ |
|
320 \ |
|
321 for (int i = 0; i < n; i++) \ |
|
322 ctmp(i) = Complex (double (re(i)), im(i)); \ |
|
323 \ |
|
324 tc = ctmp; \ |
|
325 } \ |
|
326 else \ |
|
327 tc = re; \ |
|
328 } |
|
329 |
4634
|
330 // Read one element tag from stream IS, |
|
331 // place the type code in TYPE and the byte count in BYTES |
|
332 // return nonzero on error |
|
333 static int |
|
334 read_mat5_tag (std::istream& is, bool swap, int& type, int& bytes) |
|
335 { |
|
336 unsigned int upper; |
|
337 FOUR_BYTE_INT temp; |
|
338 |
|
339 if (! is.read (X_CAST (char *, &temp), 4 )) |
|
340 goto data_read_error; |
|
341 |
|
342 if (swap) |
4944
|
343 swap_bytes<4> (&temp); |
4634
|
344 |
|
345 upper = (temp >> 16) & 0xffff; |
|
346 type = temp & 0xffff; |
|
347 |
|
348 if (upper) |
|
349 { |
|
350 // "compressed" format |
|
351 bytes = upper; |
|
352 } |
|
353 else |
|
354 { |
|
355 if (! is.read (X_CAST (char *, &temp), 4 )) |
|
356 goto data_read_error; |
|
357 if (swap) |
4944
|
358 swap_bytes<4> (&temp); |
4634
|
359 bytes = temp; |
|
360 } |
|
361 |
|
362 return 0; |
|
363 |
|
364 data_read_error: |
|
365 return 1; |
|
366 } |
|
367 |
4944
|
368 static void |
|
369 read_int (std::istream& is, bool swap, FOUR_BYTE_INT& val) |
|
370 { |
|
371 is.read (reinterpret_cast<char *> (&val), 4); |
|
372 |
|
373 if (swap) |
|
374 swap_bytes<4> (&val); |
|
375 } |
|
376 |
4634
|
377 // Extract one data element (scalar, matrix, string, etc.) from stream |
|
378 // IS and place it in TC, returning the name of the variable. |
|
379 // |
|
380 // The data is expected to be in Matlab's "Version 5" .mat format, |
|
381 // though not all the features of that format are supported. |
|
382 // |
|
383 // FILENAME is used for error messages. |
|
384 |
|
385 std::string |
|
386 read_mat5_binary_element (std::istream& is, const std::string& filename, |
|
387 bool swap, bool& global, octave_value& tc) |
|
388 { |
|
389 std::string retval; |
|
390 |
|
391 // These are initialized here instead of closer to where they are |
|
392 // first used to avoid errors from gcc about goto crossing |
|
393 // initialization of variable. |
|
394 |
|
395 oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown; |
|
396 int type = 0; |
|
397 bool imag; |
|
398 bool logicalvar; |
|
399 enum arrayclasstype arrayclass; |
5164
|
400 FOUR_BYTE_INT nnz; |
4634
|
401 FOUR_BYTE_INT flags; |
|
402 dim_vector dims; |
|
403 int len; |
|
404 int element_length; |
|
405 std::streampos pos; |
|
406 TWO_BYTE_INT number; |
|
407 number = *(TWO_BYTE_INT *)"\x00\x01"; |
|
408 |
|
409 global = false; |
|
410 |
|
411 // MAT files always use IEEE floating point |
|
412 if ((number == 1) ^ swap) |
|
413 flt_fmt = oct_mach_info::flt_fmt_ieee_big_endian; |
|
414 else |
|
415 flt_fmt = oct_mach_info::flt_fmt_ieee_little_endian; |
|
416 |
|
417 // element type and length |
|
418 if (read_mat5_tag (is, swap, type, element_length)) |
|
419 return retval; // EOF |
|
420 |
|
421 if (type != miMATRIX) |
|
422 { |
|
423 error ("load: invalid element type"); |
|
424 goto early_read_error; |
|
425 } |
|
426 |
|
427 if (element_length == 0) |
|
428 { |
|
429 tc = Matrix (); |
|
430 return retval; |
|
431 } |
|
432 |
|
433 pos = is.tellg (); |
|
434 |
|
435 // array flags subelement |
|
436 if (read_mat5_tag (is, swap, type, len) || type != miUINT32 || len != 8) |
|
437 { |
|
438 error ("load: invalid array flags subelement"); |
|
439 goto early_read_error; |
|
440 } |
|
441 |
|
442 read_int (is, swap, flags); |
|
443 imag = (flags & 0x0800) != 0; // has an imaginary part? |
|
444 global = (flags & 0x0400) != 0; // global variable? |
|
445 logicalvar = (flags & 0x0200) != 0; // we don't use this yet |
|
446 arrayclass = (arrayclasstype)(flags & 0xff); |
5164
|
447 read_int (is, swap, nnz); // number of non-zero in sparse |
4634
|
448 |
|
449 // dimensions array subelement |
|
450 { |
4638
|
451 FOUR_BYTE_INT dim_len; |
|
452 |
|
453 if (read_mat5_tag (is, swap, type, dim_len) || type != miINT32) |
4634
|
454 { |
|
455 error ("load: invalid dimensions array subelement"); |
|
456 goto early_read_error; |
|
457 } |
|
458 |
4638
|
459 int ndims = dim_len / 4; |
4634
|
460 dims.resize (ndims); |
|
461 for (int i = 0; i < ndims; i++) |
|
462 { |
|
463 FOUR_BYTE_INT n; |
|
464 read_int (is, swap, n); |
|
465 dims(i) = n; |
|
466 } |
|
467 |
|
468 std::streampos tmp_pos = is.tellg (); |
4638
|
469 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (dim_len) - dim_len)); |
4634
|
470 } |
|
471 |
|
472 if (read_mat5_tag (is, swap, type, len) || type != miINT8) |
|
473 { |
|
474 error ("load: invalid array name subelement"); |
|
475 goto early_read_error; |
|
476 } |
|
477 |
|
478 { |
|
479 OCTAVE_LOCAL_BUFFER (char, name, len+1); |
|
480 |
|
481 // Structure field subelements have zero-length array name subelements. |
|
482 |
|
483 std::streampos tmp_pos = is.tellg (); |
|
484 |
|
485 if (len) |
|
486 { |
|
487 if (! is.read (X_CAST (char *, name), len )) |
|
488 goto data_read_error; |
|
489 |
|
490 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
|
491 } |
|
492 |
|
493 name[len] = '\0'; |
|
494 retval = name; |
|
495 } |
|
496 |
|
497 switch (arrayclass) |
|
498 { |
|
499 case mxCELL_CLASS: |
|
500 { |
|
501 Cell cell_array (dims); |
|
502 |
|
503 int n = cell_array.length (); |
|
504 |
|
505 for (int i = 0; i < n; i++) |
|
506 { |
|
507 octave_value tc2; |
|
508 |
|
509 std::string nm |
|
510 = read_mat5_binary_element (is, filename, swap, global, tc2); |
|
511 |
|
512 if (! is || error_state) |
|
513 { |
|
514 error ("load: reading cell data for `%s'", nm.c_str ()); |
|
515 goto data_read_error; |
|
516 } |
|
517 |
|
518 cell_array(i) = tc2; |
|
519 } |
|
520 |
|
521 tc = cell_array; |
|
522 } |
|
523 break; |
|
524 |
|
525 case mxOBJECT_CLASS: |
|
526 warning ("load: objects are not implemented"); |
|
527 goto skip_ahead; |
|
528 |
|
529 case mxSPARSE_CLASS: |
5164
|
530 { |
|
531 int nr = dims(0); |
|
532 int nc = dims(1); |
|
533 SparseMatrix sm; |
|
534 SparseComplexMatrix scm; |
|
535 NDArray re; |
|
536 int *ridx; |
|
537 int *cidx; |
|
538 double *data; |
|
539 |
|
540 // Setup return value |
|
541 if (imag) |
|
542 { |
|
543 scm = SparseComplexMatrix (nr, nc, nnz); |
|
544 ridx = scm.ridx (); |
|
545 cidx = scm.cidx (); |
|
546 re = NDArray (dim_vector (static_cast<int> (nnz))); |
|
547 data = re.fortran_vec (); |
|
548 } |
|
549 else |
|
550 { |
|
551 sm = SparseMatrix (nr, nc, nnz); |
|
552 ridx = sm.ridx (); |
|
553 cidx = sm.cidx (); |
|
554 data = sm.data (); |
|
555 } |
|
556 |
|
557 // row indices |
|
558 std::streampos tmp_pos; |
|
559 |
|
560 if (read_mat5_tag (is, swap, type, len)) |
|
561 { |
|
562 error ("load: reading sparse row data for `%s'", retval.c_str ()); |
|
563 goto data_read_error; |
|
564 } |
|
565 |
|
566 tmp_pos = is.tellg (); |
|
567 |
|
568 read_mat5_integer_data (is, ridx, nnz, swap, |
|
569 (enum mat5_data_type) type); |
|
570 |
|
571 if (! is || error_state) |
|
572 { |
|
573 error ("load: reading sparse row data for `%s'", retval.c_str ()); |
|
574 goto data_read_error; |
|
575 } |
|
576 |
|
577 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
|
578 |
|
579 // col indices |
|
580 if (read_mat5_tag (is, swap, type, len)) |
|
581 { |
|
582 error ("load: reading sparse column data for `%s'", retval.c_str ()); |
|
583 goto data_read_error; |
|
584 } |
|
585 |
|
586 tmp_pos = is.tellg (); |
|
587 |
|
588 read_mat5_integer_data (is, cidx, nc + 1, swap, |
|
589 (enum mat5_data_type) type); |
|
590 |
|
591 if (! is || error_state) |
|
592 { |
|
593 error ("load: reading sparse column data for `%s'", retval.c_str ()); |
|
594 goto data_read_error; |
|
595 } |
|
596 |
|
597 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
|
598 |
|
599 // real data subelement |
|
600 if (read_mat5_tag (is, swap, type, len)) |
|
601 { |
|
602 error ("load: reading sparse matrix data for `%s'", retval.c_str ()); |
|
603 goto data_read_error; |
|
604 } |
|
605 |
|
606 tmp_pos = is.tellg (); |
|
607 read_mat5_binary_data (is, data, nnz, swap, |
|
608 (enum mat5_data_type) type, flt_fmt); |
|
609 |
|
610 if (! is || error_state) |
|
611 { |
|
612 error ("load: reading sparse matrix data for `%s'", retval.c_str ()); |
|
613 goto data_read_error; |
|
614 } |
|
615 |
|
616 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
|
617 |
|
618 // imaginary data subelement |
|
619 if (imag) |
|
620 { |
|
621 NDArray im (dim_vector (static_cast<int> (nnz))); |
|
622 |
|
623 if (read_mat5_tag (is, swap, type, len)) |
|
624 { |
|
625 error ("load: reading sparse matrix data for `%s'", retval.c_str ()); |
|
626 goto data_read_error; |
|
627 } |
|
628 |
|
629 read_mat5_binary_data (is, im.fortran_vec (), nnz, swap, |
|
630 (enum mat5_data_type) type, flt_fmt); |
|
631 |
|
632 if (! is || error_state) |
|
633 { |
|
634 error ("load: reading imaginary sparse matrix data for `%s'", |
|
635 retval.c_str ()); |
|
636 goto data_read_error; |
|
637 } |
|
638 |
|
639 for (int i = 0; i < nnz; i++) |
|
640 scm.xdata (i) = Complex (re (i), im (i)); |
|
641 |
|
642 tc = scm; |
|
643 } |
|
644 else |
|
645 tc = sm; |
|
646 } |
|
647 break; |
4634
|
648 |
5089
|
649 case mxFUNCTION_CLASS: |
|
650 warning ("load: function handles are not implemented"); |
|
651 goto skip_ahead; |
|
652 |
4634
|
653 case mxSTRUCT_CLASS: |
|
654 { |
|
655 Octave_map m; |
|
656 FOUR_BYTE_INT fn_type; |
|
657 FOUR_BYTE_INT fn_len; |
|
658 FOUR_BYTE_INT field_name_length; |
|
659 int i; |
|
660 |
|
661 // field name length subelement -- actually the maximum length |
|
662 // of a field name. The Matlab docs promise this will always |
|
663 // be 32. We read and use the actual value, on the theory |
|
664 // that eventually someone will recognize that's a waste of |
|
665 // space. |
|
666 if (read_mat5_tag (is, swap, fn_type, fn_len) || fn_type != miINT32) |
|
667 { |
|
668 error ("load: invalid field name subelement"); |
|
669 goto data_read_error; |
|
670 } |
|
671 |
|
672 if (! is.read (X_CAST (char *, &field_name_length), fn_len )) |
|
673 goto data_read_error; |
|
674 |
|
675 if (swap) |
4944
|
676 swap_bytes<4> (&field_name_length); |
4634
|
677 |
|
678 // field name subelement. The length of this subelement tells |
|
679 // us how many fields there are. |
|
680 if (read_mat5_tag (is, swap, fn_type, fn_len) || fn_type != miINT8) |
|
681 { |
|
682 error ("load: invalid field name subelement"); |
|
683 goto data_read_error; |
|
684 } |
|
685 |
|
686 int n_fields = fn_len/field_name_length; |
|
687 |
|
688 fn_len = PAD (fn_len); |
|
689 |
|
690 OCTAVE_LOCAL_BUFFER (char, elname, fn_len); |
|
691 |
|
692 if (! is.read (elname, fn_len)) |
|
693 goto data_read_error; |
|
694 |
|
695 int n; |
|
696 if (dims(0) == 1) |
|
697 n = dims(1); |
|
698 else if (dims(1) == 1) |
|
699 n = dims(0); |
|
700 else |
|
701 { |
|
702 error ("load: can only handle one-dimensional structure arrays"); |
|
703 goto data_read_error; |
|
704 } |
|
705 |
|
706 Cell field_elts (n_fields, n); |
|
707 |
|
708 // fields subelements |
|
709 for (int j = 0; j < n; j++) |
|
710 { |
|
711 for (i = 0; i < n_fields; i++) |
|
712 { |
|
713 octave_value fieldtc; |
|
714 read_mat5_binary_element (is, filename, swap, global, fieldtc); |
|
715 field_elts(i,j) = fieldtc; |
|
716 } |
|
717 } |
|
718 |
|
719 for (int j = n_fields-1; j >= 0; j--) |
|
720 { |
|
721 const char *key = elname + j*field_name_length; |
|
722 |
4675
|
723 Cell c (dim_vector (n, 1)); |
|
724 |
4634
|
725 for (int k = n-1; k >=0; k--) |
4675
|
726 c(k) = field_elts(j,k); |
|
727 |
|
728 m.assign (key, c); |
4634
|
729 } |
|
730 |
|
731 tc = m; |
|
732 } |
|
733 break; |
|
734 |
5089
|
735 case mxINT8_CLASS: |
|
736 OCTAVE_MAT5_INTEGER_READ (int8NDArray); |
|
737 break; |
|
738 |
|
739 case mxUINT8_CLASS: |
|
740 OCTAVE_MAT5_INTEGER_READ (uint8NDArray); |
|
741 break; |
|
742 |
|
743 case mxINT16_CLASS: |
|
744 OCTAVE_MAT5_INTEGER_READ (int16NDArray); |
|
745 break; |
|
746 |
|
747 case mxUINT16_CLASS: |
|
748 OCTAVE_MAT5_INTEGER_READ (uint16NDArray); |
|
749 break; |
|
750 |
|
751 case mxINT32_CLASS: |
|
752 OCTAVE_MAT5_INTEGER_READ (int32NDArray); |
|
753 break; |
|
754 |
|
755 case mxUINT32_CLASS: |
|
756 OCTAVE_MAT5_INTEGER_READ (uint32NDArray); |
|
757 break; |
|
758 |
|
759 case mxINT64_CLASS: |
|
760 OCTAVE_MAT5_INTEGER_READ (int64NDArray); |
|
761 break; |
|
762 |
|
763 case mxUINT64_CLASS: |
|
764 OCTAVE_MAT5_INTEGER_READ (uint64NDArray); |
|
765 break; |
|
766 |
4634
|
767 case mxCHAR_CLASS: |
|
768 // handle as a numerical array to start with |
|
769 |
|
770 case mxDOUBLE_CLASS: |
|
771 case mxSINGLE_CLASS: |
|
772 default: |
5089
|
773 { |
|
774 NDArray re (dims); |
4634
|
775 |
5089
|
776 // real data subelement |
|
777 |
4634
|
778 std::streampos tmp_pos; |
5089
|
779 |
4634
|
780 if (read_mat5_tag (is, swap, type, len)) |
|
781 { |
|
782 error ("load: reading matrix data for `%s'", retval.c_str ()); |
|
783 goto data_read_error; |
|
784 } |
|
785 |
|
786 int n = re.length (); |
|
787 tmp_pos = is.tellg (); |
|
788 read_mat5_binary_data (is, re.fortran_vec (), n, swap, |
|
789 (enum mat5_data_type) type, flt_fmt); |
|
790 |
|
791 if (! is || error_state) |
|
792 { |
|
793 error ("load: reading matrix data for `%s'", retval.c_str ()); |
|
794 goto data_read_error; |
|
795 } |
|
796 |
|
797 is.seekg (tmp_pos + static_cast<std::streamoff> (PAD (len))); |
5089
|
798 |
|
799 // imaginary data subelement |
|
800 if (imag) |
|
801 { |
|
802 NDArray im (dims); |
4634
|
803 |
5089
|
804 if (read_mat5_tag (is, swap, type, len)) |
|
805 { |
|
806 error ("load: reading matrix data for `%s'", retval.c_str ()); |
|
807 goto data_read_error; |
|
808 } |
4634
|
809 |
5089
|
810 n = im.length (); |
|
811 read_mat5_binary_data (is, im.fortran_vec (), n, swap, |
|
812 (enum mat5_data_type) type, flt_fmt); |
4634
|
813 |
5089
|
814 if (! is || error_state) |
|
815 { |
|
816 error ("load: reading imaginary matrix data for `%s'", |
|
817 retval.c_str ()); |
|
818 goto data_read_error; |
|
819 } |
4634
|
820 |
5089
|
821 ComplexNDArray ctmp (dims); |
4634
|
822 |
5089
|
823 for (int i = 0; i < n; i++) |
|
824 ctmp(i) = Complex (re(i), im(i)); |
4634
|
825 |
5089
|
826 tc = ctmp; |
|
827 } |
|
828 else |
|
829 tc = re; |
4634
|
830 |
5089
|
831 if (arrayclass == mxCHAR_CLASS) |
|
832 tc = tc.convert_to_str (false, true); |
|
833 } |
4634
|
834 } |
|
835 |
|
836 is.seekg (pos + static_cast<std::streamoff> (element_length)); |
|
837 |
|
838 if (is.eof ()) |
|
839 is.clear (); |
|
840 |
|
841 return retval; |
|
842 |
|
843 data_read_error: |
|
844 early_read_error: |
|
845 error ("load: trouble reading binary file `%s'", filename.c_str ()); |
|
846 return std::string (); |
|
847 |
|
848 skip_ahead: |
|
849 warning ("skipping over `%s'", retval.c_str ()); |
|
850 is.seekg (pos + static_cast<std::streamoff> (element_length)); |
|
851 return read_mat5_binary_element (is, filename, swap, global, tc); |
|
852 } |
|
853 |
|
854 int |
|
855 read_mat5_binary_file_header (std::istream& is, bool& swap, bool quiet) |
|
856 { |
|
857 TWO_BYTE_INT version=0, magic=0; |
|
858 |
|
859 is.seekg (124, std::ios::beg); |
|
860 is.read (X_CAST (char *, &version), 2); |
|
861 is.read (X_CAST (char *, &magic), 2); |
|
862 |
|
863 if (magic == 0x4d49) |
|
864 swap = 0; |
|
865 else if (magic == 0x494d) |
|
866 swap = 1; |
|
867 else |
|
868 { |
|
869 if (! quiet) |
|
870 error ("load: can't read binary file"); |
|
871 return -1; |
|
872 } |
|
873 |
|
874 if (! swap) // version number is inverse swapped! |
|
875 version = ((version >> 8) & 0xff) + ((version & 0xff) << 8); |
|
876 |
|
877 if (version != 1 && !quiet) |
|
878 warning ("load: found version %d binary MAT file, " |
|
879 "but only prepared for version 1", version); |
|
880 |
|
881 return 0; |
|
882 } |
|
883 |
|
884 static int |
|
885 write_mat5_tag (std::ostream& is, int type, int bytes) |
|
886 { |
|
887 FOUR_BYTE_INT temp; |
|
888 |
|
889 if (bytes <= 4) |
|
890 temp = (bytes << 16) + type; |
|
891 else |
|
892 { |
|
893 temp = type; |
|
894 if (! is.write ((char *)&temp, 4)) |
|
895 goto data_write_error; |
|
896 temp = bytes; |
|
897 } |
|
898 |
|
899 if (! is.write ((char *)&temp, 4)) |
|
900 goto data_write_error; |
|
901 |
|
902 return 0; |
|
903 |
|
904 data_write_error: |
|
905 return 1; |
|
906 } |
|
907 |
|
908 // write out the numeric values in M to OS, |
|
909 // preceded by the appropriate tag. |
|
910 static void |
|
911 write_mat5_array (std::ostream& os, const NDArray& m, bool save_as_floats) |
|
912 { |
|
913 int nel = m.nelem (); |
|
914 double max_val, min_val; |
|
915 save_type st = LS_DOUBLE; |
|
916 mat5_data_type mst; |
|
917 int size; |
|
918 unsigned len; |
|
919 const double *data = m.data (); |
|
920 |
|
921 // Have to use copy here to avoid writing over data accessed via |
|
922 // Matrix::data(). |
|
923 |
|
924 #define MAT5_DO_WRITE(TYPE, data, count, stream) \ |
|
925 do \ |
|
926 { \ |
|
927 OCTAVE_LOCAL_BUFFER (TYPE, ptr, count); \ |
|
928 for (int i = 0; i < count; i++) \ |
|
929 ptr[i] = X_CAST (TYPE, data[i]); \ |
|
930 stream.write (X_CAST (char *, ptr), count * sizeof (TYPE)); \ |
|
931 } \ |
|
932 while (0) |
|
933 |
|
934 if (save_as_floats) |
|
935 { |
|
936 if (m.too_large_for_float ()) |
|
937 { |
|
938 warning ("save: some values too large to save as floats --"); |
|
939 warning ("save: saving as doubles instead"); |
|
940 } |
|
941 else |
|
942 st = LS_FLOAT; |
|
943 } |
|
944 |
|
945 if (m.all_integers (max_val, min_val)) |
|
946 st = get_save_type (max_val, min_val); |
|
947 |
|
948 switch (st) |
|
949 { |
|
950 default: |
|
951 case LS_DOUBLE: mst = miDOUBLE; size = 8; break; |
|
952 case LS_FLOAT: mst = miSINGLE; size = 4; break; |
|
953 case LS_U_CHAR: mst = miUINT8; size = 1; break; |
|
954 case LS_U_SHORT: mst = miUINT16; size = 2; break; |
|
955 case LS_U_INT: mst = miUINT32; size = 4; break; |
|
956 case LS_CHAR: mst = miINT8; size = 1; break; |
|
957 case LS_SHORT: mst = miINT16; size = 2; break; |
|
958 case LS_INT: mst = miINT32; size = 4; break; |
|
959 } |
|
960 |
|
961 len = nel*size; |
|
962 write_mat5_tag (os, mst, len); |
|
963 |
|
964 { |
|
965 switch (st) |
|
966 { |
|
967 case LS_U_CHAR: |
|
968 MAT5_DO_WRITE (unsigned char, data, nel, os); |
|
969 break; |
|
970 |
|
971 case LS_U_SHORT: |
|
972 MAT5_DO_WRITE (unsigned TWO_BYTE_INT, data, nel, os); |
|
973 break; |
|
974 |
|
975 case LS_U_INT: |
|
976 MAT5_DO_WRITE (unsigned FOUR_BYTE_INT, data, nel, os); |
|
977 break; |
|
978 |
|
979 // provide for 64 bit ints, even though get_save_type does |
|
980 // not yet implement them |
|
981 #ifdef EIGHT_BYTE_INT |
|
982 case LS_U_LONG: |
|
983 MAT5_DO_WRITE (unsigned EIGHT_BYTE_INT, data, nel, os); |
|
984 break; |
|
985 #endif |
|
986 |
|
987 case LS_CHAR: |
|
988 MAT5_DO_WRITE (signed char, data, nel, os); |
|
989 break; |
|
990 |
|
991 case LS_SHORT: |
|
992 MAT5_DO_WRITE (TWO_BYTE_INT, data, nel, os); |
|
993 break; |
|
994 |
|
995 case LS_INT: |
|
996 MAT5_DO_WRITE (FOUR_BYTE_INT, data, nel, os); |
|
997 break; |
|
998 |
|
999 #ifdef EIGHT_BYTE_INT |
|
1000 case LS_LONG: |
|
1001 MAT5_DO_WRITE (EIGHT_BYTE_INT, data, nel, os); |
|
1002 break; |
|
1003 #endif |
|
1004 |
|
1005 case LS_FLOAT: |
|
1006 MAT5_DO_WRITE (float, data, nel, os); |
|
1007 break; |
|
1008 |
|
1009 case LS_DOUBLE: // No conversion necessary. |
|
1010 os.write (X_CAST (char *, data), len); |
|
1011 break; |
|
1012 |
|
1013 default: |
|
1014 (*current_liboctave_error_handler) |
|
1015 ("unrecognized data format requested"); |
|
1016 break; |
|
1017 } |
|
1018 } |
|
1019 if (PAD (len) > len) |
|
1020 { |
|
1021 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
1022 os.write (buf, PAD (len) - len); |
|
1023 } |
|
1024 } |
|
1025 |
5089
|
1026 template <class T> |
|
1027 void |
5164
|
1028 write_mat5_integer_data (std::ostream& os, const T *m, int size, int nel) |
5089
|
1029 { |
|
1030 mat5_data_type mst; |
|
1031 unsigned len; |
|
1032 |
|
1033 switch (size) |
|
1034 { |
|
1035 case 1: |
|
1036 mst = miUINT8; |
|
1037 break; |
|
1038 case 2: |
|
1039 mst = miUINT16; |
|
1040 break; |
5164
|
1041 case 4: |
5089
|
1042 mst = miUINT32; |
|
1043 break; |
5164
|
1044 case 8: |
5089
|
1045 mst = miUINT64; |
|
1046 break; |
|
1047 case -1: |
|
1048 mst = miINT8; |
|
1049 size = - size; |
|
1050 break; |
|
1051 case -2: |
|
1052 mst = miINT16; |
|
1053 size = - size; |
|
1054 break; |
5164
|
1055 case -4: |
5089
|
1056 mst = miINT32; |
|
1057 size = - size; |
|
1058 break; |
5164
|
1059 case -8: |
5089
|
1060 default: |
|
1061 mst = miINT64; |
|
1062 size = - size; |
|
1063 break; |
|
1064 } |
|
1065 |
|
1066 len = nel*size; |
|
1067 write_mat5_tag (os, mst, len); |
|
1068 |
5164
|
1069 os.write (X_CAST(char *, m), len); |
5089
|
1070 |
|
1071 if (PAD (len) > len) |
|
1072 { |
|
1073 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
1074 os.write (buf, PAD (len) - len); |
|
1075 } |
|
1076 } |
|
1077 |
5164
|
1078 template void write_mat5_integer_data (std::ostream& os, const octave_int8 *m, |
|
1079 int size, int nel); |
|
1080 template void write_mat5_integer_data (std::ostream& os, const octave_int16 *m, |
|
1081 int size, int nel); |
|
1082 template void write_mat5_integer_data (std::ostream& os, const octave_int32 *m, |
|
1083 int size, int nel); |
|
1084 template void write_mat5_integer_data (std::ostream& os, const octave_int64 *m, |
|
1085 int size, int nel); |
|
1086 template void write_mat5_integer_data (std::ostream& os, const octave_uint8 *m, |
|
1087 int size, int nel); |
|
1088 template void write_mat5_integer_data (std::ostream& os, const octave_uint16 *m, |
|
1089 int size, int nel); |
|
1090 template void write_mat5_integer_data (std::ostream& os, const octave_uint32 *m, |
|
1091 int size, int nel); |
|
1092 template void write_mat5_integer_data (std::ostream& os, const octave_uint64 *m, |
|
1093 int size, int nel); |
|
1094 template void write_mat5_integer_data (std::ostream& os, const int *m, |
|
1095 int size, int nel); |
5089
|
1096 |
4634
|
1097 // Write out cell element values in the cell array to OS, preceded by |
|
1098 // the appropriate tag. |
|
1099 |
|
1100 static bool |
4701
|
1101 write_mat5_cell_array (std::ostream& os, const Cell& cell, |
|
1102 bool mark_as_global, bool save_as_floats) |
4634
|
1103 { |
|
1104 int nel = cell.nelem (); |
|
1105 |
|
1106 for (int i = 0; i < nel; i++) |
|
1107 { |
|
1108 octave_value ov = cell(i); |
|
1109 |
|
1110 if (! save_mat5_binary_element (os, ov, "", mark_as_global, |
|
1111 save_as_floats)) |
|
1112 return false; |
|
1113 } |
|
1114 |
|
1115 return true; |
|
1116 } |
|
1117 |
|
1118 // save the data from TC along with the corresponding NAME on stream |
|
1119 // OS in the MatLab version 5 binary format. Return true on success. |
|
1120 |
|
1121 bool |
|
1122 save_mat5_binary_element (std::ostream& os, |
|
1123 const octave_value& tc, const std::string& name, |
|
1124 bool mark_as_global, bool save_as_floats) |
|
1125 { |
|
1126 FOUR_BYTE_INT flags=0; |
5164
|
1127 FOUR_BYTE_INT nnz=0; |
4634
|
1128 std::streampos fixup, contin; |
5089
|
1129 std::string cname = tc.class_name (); |
4634
|
1130 |
|
1131 // element type and length |
|
1132 fixup = os.tellp (); |
|
1133 write_mat5_tag (os, miMATRIX, 99); // we don't know the real length yet |
|
1134 |
|
1135 // array flags subelement |
|
1136 write_mat5_tag (os, miUINT32, 8); |
|
1137 |
|
1138 if (mark_as_global) |
|
1139 flags |= 0x0400; |
|
1140 |
|
1141 if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
1142 flags |= 0x0800; |
|
1143 |
|
1144 if (tc.is_string ()) |
|
1145 flags |= mxCHAR_CLASS; |
5089
|
1146 else if (cname == "int8") |
|
1147 flags |= mxINT8_CLASS; |
|
1148 else if (cname == "int16") |
|
1149 flags |= mxINT16_CLASS; |
|
1150 else if (cname == "int32") |
|
1151 flags |= mxINT32_CLASS; |
|
1152 else if (cname == "int64") |
|
1153 flags |= mxINT64_CLASS; |
|
1154 else if (cname == "uint8") |
|
1155 flags |= mxUINT8_CLASS; |
|
1156 else if (cname == "uint16") |
|
1157 flags |= mxUINT16_CLASS; |
|
1158 else if (cname == "uint32") |
|
1159 flags |= mxUINT32_CLASS; |
|
1160 else if (cname == "uint64") |
|
1161 flags |= mxUINT64_CLASS; |
5164
|
1162 else if (cname == "sparse") |
|
1163 { |
|
1164 flags |= mxSPARSE_CLASS; |
|
1165 if (tc.is_complex_type ()) |
|
1166 { |
|
1167 SparseComplexMatrix scm = tc.sparse_complex_matrix_value (); |
|
1168 nnz = scm.nnz (); |
|
1169 } |
|
1170 else |
|
1171 { |
|
1172 SparseMatrix sm = tc.sparse_matrix_value (); |
|
1173 nnz = sm.nnz (); |
|
1174 } |
|
1175 } |
4634
|
1176 else if (tc.is_real_scalar ()) |
|
1177 flags |= mxDOUBLE_CLASS; |
|
1178 else if (tc.is_real_matrix () || tc.is_range ()) |
|
1179 flags |= mxDOUBLE_CLASS; |
|
1180 else if (tc.is_complex_scalar ()) |
|
1181 flags |= mxDOUBLE_CLASS; |
|
1182 else if (tc.is_complex_matrix ()) |
|
1183 flags |= mxDOUBLE_CLASS; |
|
1184 else if (tc.is_map ()) |
|
1185 flags |= mxSTRUCT_CLASS; |
|
1186 else if (tc.is_cell ()) |
|
1187 flags |= mxCELL_CLASS; |
|
1188 else |
|
1189 { |
|
1190 gripe_wrong_type_arg ("save", tc, false); |
|
1191 goto error_cleanup; |
|
1192 } |
|
1193 |
|
1194 os.write ((char *)&flags, 4); |
5164
|
1195 os.write ((char *)&nnz, 4); |
4634
|
1196 |
|
1197 { |
|
1198 dim_vector dv = tc.dims (); |
|
1199 int nd = tc.ndims (); |
4638
|
1200 int dim_len = 4*nd; |
4634
|
1201 |
4638
|
1202 write_mat5_tag (os, miINT32, dim_len); |
4634
|
1203 |
|
1204 for (int i = 0; i < nd; i++) |
|
1205 { |
4638
|
1206 FOUR_BYTE_INT n = dv(i); |
4634
|
1207 os.write ((char *)&n, 4); |
|
1208 } |
4638
|
1209 |
|
1210 if (PAD (dim_len) > dim_len) |
|
1211 { |
|
1212 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
1213 os.write (buf, PAD (dim_len) - dim_len); |
|
1214 } |
4634
|
1215 } |
|
1216 |
|
1217 // array name subelement |
|
1218 { |
|
1219 int namelen = name.length (); |
|
1220 |
|
1221 if (namelen > 31) |
|
1222 namelen = 31; // only 31 char names permitted in mat file |
|
1223 |
|
1224 int paddedlength = PAD (namelen); |
|
1225 |
|
1226 write_mat5_tag (os, miINT8, namelen); |
|
1227 OCTAVE_LOCAL_BUFFER (char, paddedname, paddedlength); |
|
1228 memset (paddedname, 0, paddedlength); |
|
1229 strncpy (paddedname, name.c_str (), namelen); |
|
1230 os.write (paddedname, paddedlength); |
|
1231 } |
|
1232 |
|
1233 // data element |
|
1234 if (tc.is_string ()) |
|
1235 { |
|
1236 charMatrix chm = tc.char_matrix_value (); |
|
1237 int nr = chm.rows (); |
|
1238 int nc = chm.cols (); |
|
1239 int len = nr*nc*2; |
|
1240 int paddedlength = PAD (nr*nc*2); |
|
1241 |
|
1242 OCTAVE_LOCAL_BUFFER (TWO_BYTE_INT, buf, nc*nr+3); |
|
1243 write_mat5_tag (os, miUINT16, len); |
|
1244 |
|
1245 for (int i = 0; i < nr; i++) |
|
1246 { |
|
1247 std::string tstr = chm.row_as_string (i); |
|
1248 const char *s = tstr.data (); |
|
1249 |
|
1250 for (int j = 0; j < nc; j++) |
|
1251 buf[j*nr+i] = *s++ & 0x00FF; |
|
1252 } |
|
1253 os.write ((char *)buf, nr*nc*2); |
|
1254 |
|
1255 if (paddedlength > len) |
|
1256 os.write ((char *)buf, paddedlength - len); |
|
1257 } |
5164
|
1258 else if (cname == "sparse") |
|
1259 { |
|
1260 if (tc.is_complex_type ()) |
|
1261 { |
|
1262 SparseComplexMatrix m = tc.sparse_complex_matrix_value (); |
|
1263 int nc = m.cols (); |
|
1264 |
|
1265 write_mat5_integer_data (os, m.ridx (), - sizeof(int), nnz); |
|
1266 write_mat5_integer_data (os, m.cidx (), - sizeof(int), nc + 1); |
|
1267 |
|
1268 NDArray buf (dim_vector (nnz, 1)); |
|
1269 |
|
1270 for (int i = 0; i < nnz; i++) |
5261
|
1271 buf (i) = std::real (m.data (i)); |
5164
|
1272 |
|
1273 write_mat5_array (os, buf, save_as_floats); |
|
1274 |
|
1275 for (int i = 0; i < nnz; i++) |
5261
|
1276 buf (i) = std::imag (m.data (i)); |
5164
|
1277 |
|
1278 write_mat5_array (os, buf, save_as_floats); |
|
1279 } |
|
1280 else |
|
1281 { |
|
1282 SparseMatrix m = tc.sparse_matrix_value (); |
|
1283 int nc = m.cols (); |
|
1284 |
|
1285 write_mat5_integer_data (os, m.ridx (), - sizeof(int), nnz); |
|
1286 write_mat5_integer_data (os, m.cidx (), - sizeof(int), nc + 1); |
|
1287 |
|
1288 // XXX FIXME XXX |
|
1289 // Is there a way to easily do without this buffer |
|
1290 NDArray buf (dim_vector (nnz, 1)); |
|
1291 |
|
1292 for (int i = 0; i < nnz; i++) |
|
1293 buf (i) = m.data (i); |
|
1294 |
|
1295 write_mat5_array (os, buf, save_as_floats); |
|
1296 } |
|
1297 } |
5089
|
1298 else if (cname == "int8") |
|
1299 { |
|
1300 int8NDArray m = tc.int8_array_value (); |
|
1301 |
5164
|
1302 write_mat5_integer_data (os, m.fortran_vec (), -1, m.nelem ()); |
5089
|
1303 } |
|
1304 else if (cname == "int16") |
|
1305 { |
|
1306 int16NDArray m = tc.int16_array_value (); |
|
1307 |
5164
|
1308 write_mat5_integer_data (os, m.fortran_vec (), -2, m.nelem ()); |
5089
|
1309 } |
|
1310 else if (cname == "int32") |
|
1311 { |
|
1312 int32NDArray m = tc.int32_array_value (); |
|
1313 |
5164
|
1314 write_mat5_integer_data (os, m.fortran_vec (), -4, m.nelem ()); |
5089
|
1315 } |
|
1316 else if (cname == "int64") |
|
1317 { |
|
1318 int64NDArray m = tc.int64_array_value (); |
|
1319 |
5164
|
1320 write_mat5_integer_data (os, m.fortran_vec (), -8, m.nelem ()); |
5089
|
1321 } |
|
1322 else if (cname == "uint8") |
|
1323 { |
|
1324 uint8NDArray m = tc.uint8_array_value (); |
|
1325 |
5164
|
1326 write_mat5_integer_data (os, m.fortran_vec (), 1, m.nelem ()); |
5089
|
1327 } |
|
1328 else if (cname == "uint16") |
|
1329 { |
|
1330 uint16NDArray m = tc.uint16_array_value (); |
|
1331 |
5164
|
1332 write_mat5_integer_data (os, m.fortran_vec (), 2, m.nelem ()); |
5089
|
1333 } |
|
1334 else if (cname == "uint32") |
|
1335 { |
|
1336 uint32NDArray m = tc.uint32_array_value (); |
|
1337 |
5164
|
1338 write_mat5_integer_data (os, m.fortran_vec (), 4, m.nelem ()); |
5089
|
1339 } |
|
1340 else if (cname == "uint64") |
|
1341 { |
|
1342 uint64NDArray m = tc.uint64_array_value (); |
|
1343 |
5164
|
1344 write_mat5_integer_data (os, m.fortran_vec (), 8, m.nelem ()); |
5089
|
1345 } |
4634
|
1346 else if (tc.is_real_scalar () || tc.is_real_matrix () || tc.is_range ()) |
|
1347 { |
|
1348 NDArray m = tc.array_value (); |
|
1349 |
|
1350 write_mat5_array (os, m, save_as_floats); |
|
1351 } |
|
1352 else if (tc.is_cell ()) |
|
1353 { |
|
1354 Cell cell = tc.cell_value (); |
|
1355 |
|
1356 if (! write_mat5_cell_array (os, cell, mark_as_global, save_as_floats)) |
|
1357 goto error_cleanup; |
|
1358 } |
|
1359 else if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
1360 { |
|
1361 ComplexNDArray m_cmplx = tc.complex_matrix_value (); |
|
1362 |
|
1363 write_mat5_array (os, ::real (m_cmplx), save_as_floats); |
|
1364 write_mat5_array (os, ::imag (m_cmplx), save_as_floats); |
|
1365 } |
|
1366 else if (tc.is_map ()) |
|
1367 { |
|
1368 // an Octave structure */ |
|
1369 // recursively write each element of the structure |
4675
|
1370 const Octave_map m = tc.map_value (); |
4634
|
1371 |
|
1372 { |
|
1373 char buf[32]; |
|
1374 FOUR_BYTE_INT maxfieldnamelength = 32; |
|
1375 int fieldcnt = 0; |
|
1376 |
4675
|
1377 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
4634
|
1378 fieldcnt++; |
|
1379 |
|
1380 write_mat5_tag (os, miINT32, 4); |
|
1381 os.write ((char *)&maxfieldnamelength, 4); |
|
1382 write_mat5_tag (os, miINT8, fieldcnt*32); |
|
1383 |
4675
|
1384 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
4634
|
1385 { |
|
1386 // write the name of each element |
|
1387 std::string tstr = m.key (i); |
|
1388 memset (buf, 0, 32); |
|
1389 strncpy (buf, tstr.c_str (), 31); // only 31 char names permitted |
|
1390 os.write (buf, 32); |
|
1391 } |
|
1392 |
|
1393 int len = m.numel (); |
|
1394 |
5058
|
1395 for (int j = 0; j < len; j++) |
4634
|
1396 { |
|
1397 // write the data of each element |
|
1398 |
5058
|
1399 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) |
4634
|
1400 { |
5058
|
1401 Cell elts = m.contents (i); |
|
1402 |
4634
|
1403 bool retval2 = save_mat5_binary_element (os, elts(j), "", |
|
1404 mark_as_global, |
|
1405 save_as_floats); |
|
1406 if (! retval2) |
|
1407 goto error_cleanup; |
|
1408 } |
|
1409 } |
|
1410 } |
|
1411 } |
|
1412 else |
|
1413 gripe_wrong_type_arg ("save", tc, false); |
|
1414 |
|
1415 contin = os.tellp (); |
|
1416 os.seekp (fixup); |
|
1417 write_mat5_tag (os, miMATRIX, |
|
1418 static_cast<int>(contin - fixup) - 8); // the actual length |
|
1419 os.seekp (contin); |
|
1420 |
|
1421 return true; |
|
1422 |
|
1423 error_cleanup: |
|
1424 error ("save: error while writing `%s' to MAT file", name.c_str ()); |
|
1425 |
|
1426 return false; |
|
1427 } |
|
1428 |
|
1429 /* |
|
1430 ;;; Local Variables: *** |
|
1431 ;;; mode: C++ *** |
|
1432 ;;; End: *** |
|
1433 */ |
|
1434 |