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