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