4634
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 2003, 2004, 2005, 2006, 2007 John W. Eaton |
4634
|
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 |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
4634
|
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 |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
4634
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <cfloat> |
|
28 #include <cstring> |
|
29 #include <cctype> |
|
30 |
|
31 #include <fstream> |
|
32 #include <iomanip> |
|
33 #include <iostream> |
|
34 #include <string> |
4726
|
35 #include <vector> |
4634
|
36 |
|
37 #include "byte-swap.h" |
|
38 #include "data-conv.h" |
|
39 #include "file-ops.h" |
|
40 #include "glob-match.h" |
|
41 #include "lo-mappers.h" |
|
42 #include "mach-info.h" |
|
43 #include "oct-env.h" |
|
44 #include "oct-time.h" |
|
45 #include "quit.h" |
|
46 #include "str-vec.h" |
|
47 |
|
48 #include "Cell.h" |
|
49 #include "defun.h" |
|
50 #include "error.h" |
|
51 #include "gripes.h" |
|
52 #include "load-save.h" |
|
53 #include "oct-obj.h" |
|
54 #include "oct-map.h" |
|
55 #include "ov-cell.h" |
|
56 #include "pager.h" |
|
57 #include "pt-exp.h" |
|
58 #include "symtab.h" |
|
59 #include "sysdep.h" |
|
60 #include "unwind-prot.h" |
|
61 #include "utils.h" |
|
62 #include "variables.h" |
|
63 #include "version.h" |
|
64 #include "dMatrix.h" |
|
65 |
|
66 #include "ls-mat4.h" |
|
67 |
|
68 // Read LEN elements of data from IS in the format specified by |
|
69 // PRECISION, placing the result in DATA. If SWAP is TRUE, swap |
|
70 // the bytes of each element before copying to DATA. FLT_FMT |
|
71 // specifies the format of the data if we are reading floating point |
|
72 // numbers. |
|
73 |
|
74 static void |
|
75 read_mat_binary_data (std::istream& is, double *data, int precision, |
|
76 int len, bool swap, |
|
77 oct_mach_info::float_format flt_fmt) |
|
78 { |
|
79 switch (precision) |
|
80 { |
|
81 case 0: |
|
82 read_doubles (is, data, LS_DOUBLE, len, swap, flt_fmt); |
|
83 break; |
|
84 |
|
85 case 1: |
|
86 read_doubles (is, data, LS_FLOAT, len, swap, flt_fmt); |
|
87 break; |
|
88 |
|
89 case 2: |
|
90 read_doubles (is, data, LS_INT, len, swap, flt_fmt); |
|
91 break; |
|
92 |
|
93 case 3: |
|
94 read_doubles (is, data, LS_SHORT, len, swap, flt_fmt); |
|
95 break; |
|
96 |
|
97 case 4: |
|
98 read_doubles (is, data, LS_U_SHORT, len, swap, flt_fmt); |
|
99 break; |
|
100 |
|
101 case 5: |
|
102 read_doubles (is, data, LS_U_CHAR, len, swap, flt_fmt); |
|
103 break; |
|
104 |
|
105 default: |
|
106 break; |
|
107 } |
|
108 } |
|
109 |
|
110 int |
5828
|
111 read_mat_file_header (std::istream& is, bool& swap, int32_t& mopt, |
|
112 int32_t& nr, int32_t& nc, |
|
113 int32_t& imag, int32_t& len, |
4634
|
114 int quiet) |
|
115 { |
|
116 swap = false; |
|
117 |
|
118 // We expect to fail here, at the beginning of a record, so not |
|
119 // being able to read another mopt value should not result in an |
|
120 // error. |
|
121 |
5760
|
122 is.read (reinterpret_cast<char *> (&mopt), 4); |
4634
|
123 if (! is) |
|
124 return 1; |
|
125 |
5760
|
126 if (! is.read (reinterpret_cast<char *> (&nr), 4)) |
4634
|
127 goto data_read_error; |
|
128 |
5760
|
129 if (! is.read (reinterpret_cast<char *> (&nc), 4)) |
4634
|
130 goto data_read_error; |
|
131 |
5760
|
132 if (! is.read (reinterpret_cast<char *> (&imag), 4)) |
4634
|
133 goto data_read_error; |
|
134 |
5760
|
135 if (! is.read (reinterpret_cast<char *> (&len), 4)) |
4634
|
136 goto data_read_error; |
|
137 |
|
138 // If mopt is nonzero and the byte order is swapped, mopt will be |
|
139 // bigger than we expect, so we swap bytes. |
|
140 // |
|
141 // If mopt is zero, it means the file was written on a little endian |
|
142 // machine, and we only need to swap if we are running on a big endian |
|
143 // machine. |
|
144 // |
|
145 // Gag me. |
|
146 |
|
147 if (oct_mach_info::words_big_endian () && mopt == 0) |
|
148 swap = true; |
|
149 |
|
150 // mopt is signed, therefore byte swap may result in negative value. |
|
151 |
|
152 if (mopt > 9999 || mopt < 0) |
|
153 swap = true; |
|
154 |
|
155 if (swap) |
|
156 { |
4944
|
157 swap_bytes<4> (&mopt); |
|
158 swap_bytes<4> (&nr); |
|
159 swap_bytes<4> (&nc); |
|
160 swap_bytes<4> (&imag); |
|
161 swap_bytes<4> (&len); |
4634
|
162 } |
|
163 |
|
164 if (mopt > 9999 || mopt < 0 || imag > 1 || imag < 0) |
|
165 { |
|
166 if (! quiet) |
|
167 error ("load: can't read binary file"); |
|
168 return -1; |
|
169 } |
|
170 |
|
171 return 0; |
|
172 |
|
173 data_read_error: |
|
174 return -1; |
|
175 } |
|
176 |
|
177 // We don't just use a cast here, because we need to be able to detect |
|
178 // possible errors. |
|
179 |
|
180 oct_mach_info::float_format |
|
181 mopt_digit_to_float_format (int mach) |
|
182 { |
|
183 oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown; |
|
184 |
|
185 switch (mach) |
|
186 { |
|
187 case 0: |
|
188 flt_fmt = oct_mach_info::flt_fmt_ieee_little_endian; |
|
189 break; |
|
190 |
|
191 case 1: |
|
192 flt_fmt = oct_mach_info::flt_fmt_ieee_big_endian; |
|
193 break; |
|
194 |
|
195 case 2: |
|
196 flt_fmt = oct_mach_info::flt_fmt_vax_d; |
|
197 break; |
|
198 |
|
199 case 3: |
|
200 flt_fmt = oct_mach_info::flt_fmt_vax_g; |
|
201 break; |
|
202 |
|
203 case 4: |
|
204 flt_fmt = oct_mach_info::flt_fmt_cray; |
|
205 break; |
|
206 |
|
207 default: |
|
208 flt_fmt = oct_mach_info::flt_fmt_unknown; |
|
209 break; |
|
210 } |
|
211 |
|
212 return flt_fmt; |
|
213 } |
|
214 |
|
215 int |
|
216 float_format_to_mopt_digit (oct_mach_info::float_format flt_fmt) |
|
217 { |
|
218 int retval = -1; |
|
219 |
|
220 switch (flt_fmt) |
|
221 { |
|
222 case oct_mach_info::flt_fmt_ieee_little_endian: |
|
223 retval = 0; |
|
224 break; |
|
225 |
|
226 case oct_mach_info::flt_fmt_ieee_big_endian: |
|
227 retval = 1; |
|
228 break; |
|
229 |
|
230 case oct_mach_info::flt_fmt_vax_d: |
|
231 retval = 2; |
|
232 break; |
|
233 |
|
234 case oct_mach_info::flt_fmt_vax_g: |
|
235 retval = 3; |
|
236 break; |
|
237 |
|
238 case oct_mach_info::flt_fmt_cray: |
|
239 retval = 4; |
|
240 break; |
|
241 |
|
242 default: |
|
243 break; |
|
244 } |
|
245 |
|
246 return retval; |
|
247 } |
|
248 |
|
249 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
250 // place it in TC, returning the name of the variable. |
|
251 // |
|
252 // The data is expected to be in Matlab version 4 .mat format, though |
|
253 // not all the features of that format are supported. |
|
254 // |
|
255 // FILENAME is used for error messages. |
|
256 // |
|
257 // This format provides no way to tag the data as global. |
|
258 |
|
259 std::string |
|
260 read_mat_binary_data (std::istream& is, const std::string& filename, |
|
261 octave_value& tc) |
|
262 { |
|
263 std::string retval; |
|
264 |
|
265 // These are initialized here instead of closer to where they are |
|
266 // first used to avoid errors from gcc about goto crossing |
|
267 // initialization of variable. |
|
268 |
|
269 Matrix re; |
|
270 oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown; |
|
271 bool swap = false; |
|
272 int type = 0; |
|
273 int prec = 0; |
|
274 int order = 0; |
|
275 int mach = 0; |
|
276 int dlen = 0; |
|
277 |
5828
|
278 int32_t mopt, nr, nc, imag, len; |
4634
|
279 |
|
280 int err = read_mat_file_header (is, swap, mopt, nr, nc, imag, len); |
|
281 if (err) |
|
282 { |
|
283 if (err < 0) |
|
284 goto data_read_error; |
|
285 else |
|
286 return retval; |
|
287 } |
|
288 |
|
289 type = mopt % 10; // Full, sparse, etc. |
|
290 mopt /= 10; // Eliminate first digit. |
|
291 prec = mopt % 10; // double, float, int, etc. |
|
292 mopt /= 10; // Eliminate second digit. |
|
293 order = mopt % 10; // Row or column major ordering. |
|
294 mopt /= 10; // Eliminate third digit. |
|
295 mach = mopt % 10; // IEEE, VAX, etc. |
|
296 |
|
297 flt_fmt = mopt_digit_to_float_format (mach); |
|
298 |
|
299 if (flt_fmt == oct_mach_info::flt_fmt_unknown) |
|
300 { |
|
301 error ("load: unrecognized binary format!"); |
|
302 return retval; |
|
303 } |
|
304 |
|
305 if (type != 0 && type != 1) |
|
306 { |
|
307 error ("load: can't read sparse matrices"); |
|
308 return retval; |
|
309 } |
|
310 |
|
311 if (imag && type == 1) |
|
312 { |
|
313 error ("load: encountered complex matrix with string flag set!"); |
|
314 return retval; |
|
315 } |
|
316 |
|
317 // LEN includes the terminating character, and the file is also |
|
318 // supposed to include it, but apparently not all files do. Either |
|
319 // way, I think this should work. |
|
320 |
|
321 { |
|
322 OCTAVE_LOCAL_BUFFER (char, name, len+1); |
|
323 name[len] = '\0'; |
5760
|
324 if (! is.read (name, len)) |
4634
|
325 goto data_read_error; |
|
326 retval = name; |
|
327 |
|
328 dlen = nr * nc; |
|
329 if (dlen < 0) |
|
330 goto data_read_error; |
|
331 |
|
332 if (order) |
|
333 { |
5275
|
334 octave_idx_type tmp = nr; |
4634
|
335 nr = nc; |
|
336 nc = tmp; |
|
337 } |
|
338 |
|
339 re.resize (nr, nc); |
|
340 |
|
341 read_mat_binary_data (is, re.fortran_vec (), prec, dlen, swap, flt_fmt); |
|
342 |
|
343 if (! is || error_state) |
|
344 { |
|
345 error ("load: reading matrix data for `%s'", name); |
|
346 goto data_read_error; |
|
347 } |
|
348 |
|
349 if (imag) |
|
350 { |
|
351 Matrix im (nr, nc); |
|
352 |
|
353 read_mat_binary_data (is, im.fortran_vec (), prec, dlen, swap, |
|
354 flt_fmt); |
|
355 |
|
356 if (! is || error_state) |
|
357 { |
|
358 error ("load: reading imaginary matrix data for `%s'", name); |
|
359 goto data_read_error; |
|
360 } |
|
361 |
|
362 ComplexMatrix ctmp (nr, nc); |
|
363 |
5275
|
364 for (octave_idx_type j = 0; j < nc; j++) |
|
365 for (octave_idx_type i = 0; i < nr; i++) |
4634
|
366 ctmp (i, j) = Complex (re (i, j), im (i, j)); |
|
367 |
|
368 tc = order ? ctmp.transpose () : ctmp; |
|
369 } |
|
370 else |
|
371 tc = order ? re.transpose () : re; |
|
372 |
|
373 if (type == 1) |
5279
|
374 tc = tc.convert_to_str (false, true, '\''); |
4634
|
375 |
|
376 return retval; |
|
377 } |
|
378 |
|
379 data_read_error: |
|
380 error ("load: trouble reading binary file `%s'", filename.c_str ()); |
|
381 return retval; |
|
382 } |
|
383 |
|
384 // Save the data from TC along with the corresponding NAME on stream OS |
|
385 // in the MatLab version 4 binary format. |
|
386 |
|
387 bool |
|
388 save_mat_binary_data (std::ostream& os, const octave_value& tc, |
|
389 const std::string& name) |
|
390 { |
5828
|
391 int32_t mopt = 0; |
4634
|
392 |
|
393 mopt += tc.is_string () ? 1 : 0; |
|
394 |
|
395 oct_mach_info::float_format flt_fmt = |
|
396 oct_mach_info::native_float_format ();; |
|
397 |
|
398 mopt += 1000 * float_format_to_mopt_digit (flt_fmt); |
|
399 |
5760
|
400 os.write (reinterpret_cast<char *> (&mopt), 4); |
4634
|
401 |
5828
|
402 int32_t nr = tc.rows (); |
5760
|
403 os.write (reinterpret_cast<char *> (&nr), 4); |
4634
|
404 |
5828
|
405 int32_t nc = tc.columns (); |
5760
|
406 os.write (reinterpret_cast<char *> (&nc), 4); |
4634
|
407 |
5275
|
408 octave_idx_type len = nr * nc; |
4634
|
409 |
5828
|
410 int32_t imag = tc.is_complex_type () ? 1 : 0; |
5760
|
411 os.write (reinterpret_cast<char *> (&imag), 4); |
4634
|
412 |
|
413 // LEN includes the terminating character, and the file is also |
|
414 // supposed to include it. |
|
415 |
5828
|
416 int32_t name_len = name.length () + 1; |
4634
|
417 |
5760
|
418 os.write (reinterpret_cast<char *> (&name_len), 4); |
4634
|
419 os << name << '\0'; |
|
420 |
|
421 if (tc.is_string ()) |
|
422 { |
|
423 unwind_protect::begin_frame ("save_mat_binary_data"); |
|
424 |
|
425 charMatrix chm = tc.char_matrix_value (); |
|
426 |
5275
|
427 octave_idx_type nrow = chm.rows (); |
|
428 octave_idx_type ncol = chm.cols (); |
4634
|
429 |
|
430 OCTAVE_LOCAL_BUFFER (double, buf, ncol*nrow); |
|
431 |
5275
|
432 for (octave_idx_type i = 0; i < nrow; i++) |
4634
|
433 { |
|
434 std::string tstr = chm.row_as_string (i); |
|
435 const char *s = tstr.data (); |
|
436 |
5275
|
437 for (octave_idx_type j = 0; j < ncol; j++) |
4634
|
438 buf[j*nrow+i] = static_cast<double> (*s++ & 0x00FF); |
|
439 } |
5760
|
440 os.write (reinterpret_cast<char *> (buf), nrow*ncol*sizeof(double)); |
4634
|
441 |
|
442 unwind_protect::run_frame ("save_mat_binary_data"); |
|
443 } |
|
444 else if (tc.is_range ()) |
|
445 { |
|
446 Range r = tc.range_value (); |
|
447 double base = r.base (); |
|
448 double inc = r.inc (); |
5275
|
449 octave_idx_type nel = r.nelem (); |
|
450 for (octave_idx_type i = 0; i < nel; i++) |
4634
|
451 { |
|
452 double x = base + i * inc; |
5760
|
453 os.write (reinterpret_cast<char *> (&x), 8); |
4634
|
454 } |
|
455 } |
|
456 else if (tc.is_real_scalar ()) |
|
457 { |
|
458 double tmp = tc.double_value (); |
5760
|
459 os.write (reinterpret_cast<char *> (&tmp), 8); |
4634
|
460 } |
|
461 else if (tc.is_real_matrix ()) |
|
462 { |
|
463 Matrix m = tc.matrix_value (); |
5760
|
464 os.write (reinterpret_cast<const char *> (m.data ()), 8 * len); |
4634
|
465 } |
|
466 else if (tc.is_complex_scalar ()) |
|
467 { |
|
468 Complex tmp = tc.complex_value (); |
5760
|
469 os.write (reinterpret_cast<char *> (&tmp), 16); |
4634
|
470 } |
|
471 else if (tc.is_complex_matrix ()) |
|
472 { |
|
473 ComplexMatrix m_cmplx = tc.complex_matrix_value (); |
|
474 Matrix m = ::real (m_cmplx); |
5760
|
475 os.write (reinterpret_cast<const char *> (m.data ()), 8 * len); |
4634
|
476 m = ::imag (m_cmplx); |
5760
|
477 os.write (reinterpret_cast<const char *> (m.data ()), 8 * len); |
4634
|
478 } |
|
479 else |
|
480 gripe_wrong_type_arg ("save", tc, false); |
|
481 |
|
482 return os; |
|
483 } |
|
484 |
|
485 /* |
|
486 ;;; Local Variables: *** |
|
487 ;;; mode: C++ *** |
|
488 ;;; End: *** |
|
489 */ |