604
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
604
|
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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
604
|
20 |
|
21 */ |
|
22 |
3911
|
23 // Author: John W. Eaton. |
|
24 // HDF5 support by Steven G. Johnson <stevenj@alum.mit.edu> |
|
25 // Matlab v5 support by James R. Van Zandt <jrv@vanzandt.mv.com> |
3687
|
26 |
604
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
604
|
29 #endif |
|
30 |
1343
|
31 #include <cfloat> |
|
32 #include <cstring> |
|
33 #include <cctype> |
|
34 |
4249
|
35 #include <fstream> |
3503
|
36 #include <iomanip> |
|
37 #include <iostream> |
4249
|
38 #include <memory> |
1728
|
39 #include <string> |
|
40 |
3687
|
41 #ifdef HAVE_HDF5 |
|
42 #include <hdf5.h> |
|
43 #endif |
|
44 |
1961
|
45 #include "byte-swap.h" |
|
46 #include "data-conv.h" |
2926
|
47 #include "file-ops.h" |
|
48 #include "glob-match.h" |
2890
|
49 #include "lo-mappers.h" |
4051
|
50 #include "lo-sstream.h" |
2318
|
51 #include "mach-info.h" |
3185
|
52 #include "oct-env.h" |
3258
|
53 #include "oct-time.h" |
4171
|
54 #include "quit.h" |
1755
|
55 #include "str-vec.h" |
|
56 |
4332
|
57 #include "Cell.h" |
1352
|
58 #include "defun.h" |
604
|
59 #include "error.h" |
777
|
60 #include "gripes.h" |
1352
|
61 #include "load-save.h" |
1750
|
62 #include "oct-obj.h" |
3687
|
63 #include "oct-map.h" |
4332
|
64 #include "ov-cell.h" |
1352
|
65 #include "pager.h" |
1750
|
66 #include "pt-exp.h" |
1352
|
67 #include "symtab.h" |
|
68 #include "sysdep.h" |
|
69 #include "unwind-prot.h" |
604
|
70 #include "utils.h" |
2371
|
71 #include "variables.h" |
3185
|
72 #include "version.h" |
3688
|
73 #include "dMatrix.h" |
|
74 |
|
75 #define PAD(l) (((l)<=4)?4:(((l)+7)/8)*8) |
|
76 #define TAGLENGTH(l) ((l)<=4?4:8) |
|
77 |
3598
|
78 // Write octave-core file if Octave crashes or is killed by a signal. |
3189
|
79 static bool Vcrash_dumps_octave_core; |
|
80 |
3687
|
81 // The default output format. May be one of "binary", "text", |
|
82 // "mat-binary", or "hdf5". |
3523
|
83 static std::string Vdefault_save_format; |
2194
|
84 |
3709
|
85 // The format string for the comment line at the top of text-format |
|
86 // save files. Passed to strftime. Should begin with `#' and contain |
|
87 // no newline characters. |
|
88 static std::string Vsave_header_format_string; |
|
89 |
2194
|
90 // The number of decimal digits to use when writing ascii data. |
|
91 static int Vsave_precision; |
|
92 |
872
|
93 // Used when converting Inf to something that gnuplot can read. |
|
94 |
|
95 #ifndef OCT_RBV |
|
96 #define OCT_RBV DBL_MAX / 100.0 |
|
97 #endif |
|
98 |
4332
|
99 #define CELL_ELT_TAG "<cell-element>" |
|
100 |
4329
|
101 enum arrayclasstype |
3688
|
102 { |
|
103 mxCELL_CLASS=1, // cell array |
|
104 mxSTRUCT_CLASS, // structure |
|
105 mxOBJECT_CLASS, // object |
|
106 mxCHAR_CLASS, // character array |
|
107 mxSPARSE_CLASS, // sparse array |
|
108 mxDOUBLE_CLASS, // double precision array |
|
109 mxSINGLE_CLASS, // single precision floating point |
|
110 mxINT8_CLASS, // 8 bit signed integer |
|
111 mxUINT8_CLASS, // 8 bit unsigned integer |
|
112 mxINT16_CLASS, // 16 bit signed integer |
|
113 mxUINT16_CLASS, // 16 bit unsigned integer |
|
114 mxINT32_CLASS, // 32 bit signed integer |
|
115 mxUINT32_CLASS // 32 bit unsigned integer |
|
116 }; |
|
117 |
|
118 enum mat5_data_type |
|
119 { |
|
120 miINT8=1, // 8 bit signed |
|
121 miUINT8, // 8 bit unsigned |
|
122 miINT16, // 16 bit signed |
|
123 miUINT16, // 16 bit unsigned |
|
124 miINT32, // 32 bit signed |
|
125 miUINT32, // 32 bit unsigned |
|
126 miSINGLE, // IEEE 754 single precision float |
|
127 miRESERVE1, |
|
128 miDOUBLE, // IEEE 754 double precision float |
|
129 miRESERVE2, |
|
130 miRESERVE3, |
|
131 miINT64, // 64 bit signed |
|
132 miUINT64, // 64 bit unsigned |
|
133 miMATRIX // MATLAB array |
|
134 }; |
|
135 |
4332
|
136 static bool |
|
137 save_mat5_binary_element (std::ostream& os, |
|
138 const octave_value& tc, const std::string& name, |
|
139 bool mark_as_global, bool save_as_floats); |
|
140 |
3687
|
141 #ifdef HAVE_HDF5 |
|
142 // this is only used for HDF5 import |
|
143 // try to convert s into a valid identifier, replacing invalid chars with "_": |
4528
|
144 static std::string |
|
145 make_valid_identifier (const std::string& nm) |
3687
|
146 { |
4528
|
147 std::string retval; |
|
148 |
|
149 size_t nm_len = nm.length (); |
|
150 |
|
151 if (nm_len > 0) |
3687
|
152 { |
4528
|
153 if (! isalpha (nm[0])) |
|
154 retval += '_'; |
|
155 |
|
156 for (size_t i = 0; i < nm_len; i++) |
|
157 { |
|
158 char c = nm[i]; |
|
159 retval += (isalnum (c) || c == '_') ? c : '_'; |
|
160 } |
3687
|
161 } |
4528
|
162 |
|
163 return retval; |
|
164 } |
|
165 |
|
166 static bool |
|
167 ident_is_all_digits (const std::string& id) |
|
168 { |
|
169 bool retval = true; |
|
170 |
|
171 size_t len = 0; |
|
172 |
|
173 for (size_t i = 0; i < len; i++) |
|
174 { |
|
175 if (! isdigit (id[i])) |
|
176 { |
|
177 retval = false; |
|
178 break; |
|
179 } |
|
180 } |
|
181 |
|
182 return retval; |
3687
|
183 } |
|
184 #endif /* HAVE_HDF5 */ |
|
185 |
630
|
186 // XXX FIXME XXX -- shouldn't this be implemented in terms of other |
|
187 // functions that are already available? |
604
|
188 |
|
189 // Install a variable with name NAME and the value specified TC in the |
3019
|
190 // symbol table. If FORCE is TRUE, replace any existing definition |
|
191 // for NAME. If GLOBAL is TRUE, make the variable global. |
604
|
192 // |
|
193 // Assumes TC is defined. |
|
194 |
|
195 static void |
4171
|
196 install_loaded_variable (int force, const std::string& name, |
|
197 const octave_value& val, |
|
198 int global, const std::string& doc) |
604
|
199 { |
1358
|
200 // Is there already a symbol by this name? If so, what is it? |
604
|
201 |
2856
|
202 symbol_record *lsr = curr_sym_tab->lookup (name); |
604
|
203 |
3019
|
204 bool is_undefined = true; |
|
205 bool is_variable = false; |
|
206 bool is_function = false; |
|
207 bool is_global = false; |
604
|
208 |
|
209 if (lsr) |
|
210 { |
|
211 is_undefined = ! lsr->is_defined (); |
|
212 is_variable = lsr->is_variable (); |
|
213 is_function = lsr->is_function (); |
|
214 is_global = lsr->is_linked_to_global (); |
|
215 } |
|
216 |
|
217 symbol_record *sr = 0; |
|
218 |
|
219 if (global) |
|
220 { |
|
221 if (is_global || is_undefined) |
|
222 { |
|
223 if (force || is_undefined) |
|
224 { |
2856
|
225 lsr = curr_sym_tab->lookup (name, true); |
604
|
226 link_to_global_variable (lsr); |
|
227 sr = lsr; |
|
228 } |
|
229 else |
|
230 { |
4171
|
231 warning ("load: global variable name `%s' exists", |
|
232 name.c_str ()); |
604
|
233 warning ("use `load -force' to overwrite"); |
|
234 } |
|
235 } |
|
236 else if (is_function) |
|
237 { |
|
238 if (force) |
|
239 { |
2856
|
240 lsr = curr_sym_tab->lookup (name, true); |
604
|
241 link_to_global_variable (lsr); |
|
242 sr = lsr; |
|
243 } |
|
244 else |
|
245 { |
4171
|
246 warning ("load: `%s' is currently a function in this scope", |
|
247 name.c_str ()); |
604
|
248 warning ("`load -force' will load variable and hide function"); |
|
249 } |
|
250 } |
|
251 else if (is_variable) |
|
252 { |
|
253 if (force) |
|
254 { |
2856
|
255 lsr = curr_sym_tab->lookup (name, true); |
604
|
256 link_to_global_variable (lsr); |
|
257 sr = lsr; |
|
258 } |
|
259 else |
|
260 { |
4171
|
261 warning ("load: local variable name `%s' exists", |
|
262 name.c_str ()); |
604
|
263 warning ("use `load -force' to overwrite"); |
|
264 } |
|
265 } |
|
266 else |
774
|
267 error ("load: unable to load data for unknown symbol type"); |
604
|
268 } |
|
269 else |
|
270 { |
|
271 if (is_global) |
|
272 { |
|
273 if (force || is_undefined) |
|
274 { |
2856
|
275 lsr = curr_sym_tab->lookup (name, true); |
604
|
276 link_to_global_variable (lsr); |
|
277 sr = lsr; |
|
278 } |
|
279 else |
|
280 { |
4171
|
281 warning ("load: global variable name `%s' exists", |
|
282 name.c_str ()); |
604
|
283 warning ("use `load -force' to overwrite"); |
|
284 } |
|
285 } |
|
286 else if (is_function) |
|
287 { |
|
288 if (force) |
|
289 { |
2856
|
290 lsr = curr_sym_tab->lookup (name, true); |
604
|
291 link_to_global_variable (lsr); |
|
292 sr = lsr; |
|
293 } |
|
294 else |
|
295 { |
4171
|
296 warning ("load: `%s' is currently a function in this scope", |
|
297 name.c_str ()); |
604
|
298 warning ("`load -force' will load variable and hide function"); |
|
299 } |
|
300 } |
|
301 else if (is_variable || is_undefined) |
|
302 { |
|
303 if (force || is_undefined) |
|
304 { |
2856
|
305 lsr = curr_sym_tab->lookup (name, true); |
604
|
306 sr = lsr; |
|
307 } |
|
308 else |
|
309 { |
4171
|
310 warning ("load: local variable name `%s' exists", |
|
311 name.c_str ()); |
604
|
312 warning ("use `load -force' to overwrite"); |
|
313 } |
|
314 } |
|
315 else |
774
|
316 error ("load: unable to load data for unknown symbol type"); |
604
|
317 } |
|
318 |
|
319 if (sr) |
|
320 { |
2371
|
321 sr->define (val); |
4171
|
322 sr->document (doc); |
604
|
323 return; |
|
324 } |
|
325 else |
4171
|
326 error ("load: unable to load variable `%s'", name.c_str ()); |
604
|
327 |
|
328 return; |
|
329 } |
|
330 |
|
331 // Functions for reading ascii data. |
|
332 |
|
333 // Skip white space and comments on stream IS. |
|
334 |
|
335 static void |
3523
|
336 skip_comments (std::istream& is) |
604
|
337 { |
|
338 char c = '\0'; |
|
339 while (is.get (c)) |
|
340 { |
|
341 if (c == ' ' || c == '\t' || c == '\n') |
|
342 ; // Skip whitespace on way to beginning of next line. |
|
343 else |
|
344 break; |
|
345 } |
|
346 |
|
347 for (;;) |
|
348 { |
3731
|
349 if (is && (c == '%' || c == '#')) |
604
|
350 while (is.get (c) && c != '\n') |
|
351 ; // Skip to beginning of next line, ignoring everything. |
|
352 else |
|
353 break; |
|
354 } |
|
355 } |
|
356 |
|
357 // Extract a KEYWORD and its value from stream IS, returning the |
|
358 // associated value in a new string. |
|
359 // |
|
360 // Input should look something like: |
|
361 // |
3731
|
362 // [%#][ \t]*keyword[ \t]*:[ \t]*string-value[ \t]*\n |
604
|
363 |
4171
|
364 static std::string |
3523
|
365 extract_keyword (std::istream& is, const char *keyword) |
604
|
366 { |
4171
|
367 std::string retval; |
604
|
368 |
|
369 char c; |
|
370 while (is.get (c)) |
|
371 { |
3731
|
372 if (c == '%' || c == '#') |
604
|
373 { |
4051
|
374 OSSTREAM buf; |
2795
|
375 |
3731
|
376 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#')) |
604
|
377 ; // Skip whitespace and comment characters. |
|
378 |
|
379 if (isalpha (c)) |
|
380 buf << c; |
|
381 |
|
382 while (is.get (c) && isalpha (c)) |
|
383 buf << c; |
|
384 |
4051
|
385 buf << OSSTREAM_ENDS; |
|
386 const char *tmp = OSSTREAM_C_STR (buf); |
|
387 OSSTREAM_FREEZE (buf); |
604
|
388 int match = (strncmp (tmp, keyword, strlen (keyword)) == 0); |
|
389 |
|
390 if (match) |
|
391 { |
4051
|
392 OSSTREAM value; |
604
|
393 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
394 ; // Skip whitespace and the colon. |
|
395 |
|
396 if (c != '\n') |
|
397 { |
|
398 value << c; |
|
399 while (is.get (c) && c != '\n') |
|
400 value << c; |
|
401 } |
4051
|
402 value << OSSTREAM_ENDS; |
4171
|
403 retval = OSSTREAM_STR (value); |
4051
|
404 OSSTREAM_FREEZE (value); |
604
|
405 break; |
|
406 } |
|
407 } |
|
408 } |
918
|
409 |
4171
|
410 int len = retval.length (); |
|
411 |
|
412 if (len > 0) |
918
|
413 { |
4171
|
414 while (len) |
918
|
415 { |
4171
|
416 char c = retval[len-1]; |
|
417 |
|
418 if (c == ' ' || c == '\t') |
|
419 len--; |
|
420 else |
|
421 { |
|
422 retval.resize (len); |
|
423 break; |
|
424 } |
918
|
425 } |
|
426 } |
|
427 |
604
|
428 return retval; |
|
429 } |
|
430 |
|
431 // Match KEYWORD on stream IS, placing the associated value in VALUE, |
3019
|
432 // returning TRUE if successful and FALSE otherwise. |
604
|
433 // |
|
434 // Input should look something like: |
|
435 // |
3731
|
436 // [%#][ \t]*keyword[ \t]*int-value.*\n |
604
|
437 |
3019
|
438 static bool |
3523
|
439 extract_keyword (std::istream& is, const char *keyword, int& value) |
604
|
440 { |
3019
|
441 bool status = false; |
604
|
442 value = 0; |
|
443 |
|
444 char c; |
|
445 while (is.get (c)) |
|
446 { |
3731
|
447 if (c == '%' || c == '#') |
604
|
448 { |
4051
|
449 OSSTREAM buf; |
2795
|
450 |
3731
|
451 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#')) |
604
|
452 ; // Skip whitespace and comment characters. |
|
453 |
|
454 if (isalpha (c)) |
|
455 buf << c; |
|
456 |
|
457 while (is.get (c) && isalpha (c)) |
|
458 buf << c; |
|
459 |
4051
|
460 buf << OSSTREAM_ENDS; |
|
461 const char *tmp = OSSTREAM_C_STR (buf); |
604
|
462 int match = (strncmp (tmp, keyword, strlen (keyword)) == 0); |
4051
|
463 OSSTREAM_FREEZE (buf); |
604
|
464 |
|
465 if (match) |
|
466 { |
|
467 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
468 ; // Skip whitespace and the colon. |
|
469 |
|
470 is.putback (c); |
|
471 if (c != '\n') |
|
472 is >> value; |
|
473 if (is) |
3019
|
474 status = true; |
604
|
475 while (is.get (c) && c != '\n') |
|
476 ; // Skip to beginning of next line; |
|
477 break; |
|
478 } |
|
479 } |
|
480 } |
|
481 return status; |
|
482 } |
|
483 |
|
484 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
485 // place it in TC, returning the name of the variable. If the value |
3019
|
486 // is tagged as global in the file, return TRUE in GLOBAL. |
604
|
487 // |
|
488 // FILENAME is used for error messages. |
|
489 // |
|
490 // The data is expected to be in the following format: |
|
491 // |
|
492 // The input file must have a header followed by some data. |
|
493 // |
|
494 // All lines in the header must begin with a `#' character. |
|
495 // |
|
496 // The header must contain a list of keyword and value pairs with the |
|
497 // keyword and value separated by a colon. |
|
498 // |
|
499 // Keywords must appear in the following order: |
|
500 // |
|
501 // # name: <name> |
|
502 // # type: <type> |
|
503 // # <info> |
|
504 // |
|
505 // Where: |
|
506 // |
|
507 // <name> : a valid identifier |
|
508 // |
|
509 // <type> : <typename> |
|
510 // | global <typename> |
|
511 // |
|
512 // <typename> : scalar |
|
513 // | complex scalar |
|
514 // | matrix |
|
515 // | complex matrix |
|
516 // | string |
|
517 // | range |
1427
|
518 // | string array |
604
|
519 // |
|
520 // <info> : <matrix info> |
|
521 // | <string info> |
1427
|
522 // | <string array info> |
604
|
523 // |
|
524 // <matrix info> : # rows: <integer> |
1427
|
525 // : # columns: <integer> |
604
|
526 // |
1427
|
527 // <string info> : # length: <integer> |
|
528 // |
|
529 // <string array info> : # elements: <integer> |
|
530 // : # length: <integer> (once before each string) |
604
|
531 // |
|
532 // Formatted ASCII data follows the header. |
|
533 // |
|
534 // Example: |
|
535 // |
|
536 // # name: foo |
|
537 // # type: matrix |
|
538 // # rows: 2 |
|
539 // # columns: 2 |
|
540 // 2 4 |
|
541 // 1 3 |
|
542 // |
1427
|
543 // Example: |
|
544 // |
|
545 // # name: foo |
|
546 // # type: string array |
|
547 // # elements: 5 |
|
548 // # length: 4 |
|
549 // this |
|
550 // # length: 2 |
|
551 // is |
|
552 // # length: 1 |
|
553 // a |
|
554 // # length: 6 |
|
555 // string |
|
556 // # length: 5 |
|
557 // array |
|
558 // |
604
|
559 // XXX FIXME XXX -- this format is fairly rigid, and doesn't allow for |
|
560 // arbitrary comments, etc. Someone should fix that. |
|
561 |
4171
|
562 // Ugh. The signature of the compare method is not standard in older |
|
563 // versions of the GNU libstdc++. Do this instead: |
|
564 |
|
565 #define SUBSTRING_COMPARE_EQ(s, pos, n, t) (s.substr (pos, n) == t) |
|
566 |
|
567 static std::string |
3523
|
568 read_ascii_data (std::istream& is, const std::string& filename, bool& global, |
3136
|
569 octave_value& tc, int count) |
604
|
570 { |
1358
|
571 // Read name for this entry or break on EOF. |
604
|
572 |
4171
|
573 std::string name = extract_keyword (is, "name"); |
|
574 |
|
575 if (name.empty ()) |
3136
|
576 { |
|
577 if (count == 0) |
4171
|
578 error ("load: empty name keyword or no data found in file `%s'", |
|
579 filename.c_str ()); |
|
580 |
|
581 return std::string (); |
3136
|
582 } |
604
|
583 |
4332
|
584 if (name == CELL_ELT_TAG) |
|
585 { |
|
586 // This is OK -- name won't be used. |
|
587 } |
|
588 else if (! valid_identifier (name)) |
604
|
589 { |
4171
|
590 error ("load: bogus identifier `%s' found in file `%s'", |
|
591 name.c_str (), filename.c_str ()); |
|
592 return std::string (); |
604
|
593 } |
|
594 |
1358
|
595 // Look for type keyword. |
604
|
596 |
4171
|
597 std::string tag = extract_keyword (is, "type"); |
|
598 |
|
599 if (! tag.empty ()) |
604
|
600 { |
4171
|
601 std::string typ; |
|
602 size_t pos = tag.rfind (' '); |
|
603 |
|
604 if (pos != NPOS) |
604
|
605 { |
4171
|
606 global = SUBSTRING_COMPARE_EQ (tag, 0, 6, "global"); |
|
607 |
|
608 typ = global ? tag.substr (7) : tag; |
604
|
609 } |
|
610 else |
4171
|
611 typ = tag; |
|
612 |
|
613 if (SUBSTRING_COMPARE_EQ (typ, 0, 6, "scalar")) |
604
|
614 { |
4130
|
615 double tmp = octave_read_double (is); |
604
|
616 if (is) |
|
617 tc = tmp; |
|
618 else |
|
619 error ("load: failed to load scalar constant"); |
|
620 } |
4171
|
621 else if (SUBSTRING_COMPARE_EQ (typ, 0, 6, "matrix")) |
604
|
622 { |
3019
|
623 int nr = 0; |
|
624 int nc = 0; |
604
|
625 |
1275
|
626 if (extract_keyword (is, "rows", nr) && nr >= 0 |
|
627 && extract_keyword (is, "columns", nc) && nc >= 0) |
604
|
628 { |
1275
|
629 if (nr > 0 && nc > 0) |
|
630 { |
|
631 Matrix tmp (nr, nc); |
|
632 is >> tmp; |
|
633 if (is) |
|
634 tc = tmp; |
|
635 else |
|
636 error ("load: failed to load matrix constant"); |
|
637 } |
|
638 else if (nr == 0 || nc == 0) |
|
639 tc = Matrix (nr, nc); |
604
|
640 else |
1275
|
641 panic_impossible (); |
604
|
642 } |
|
643 else |
|
644 error ("load: failed to extract number of rows and columns"); |
|
645 } |
4332
|
646 else if (SUBSTRING_COMPARE_EQ (typ, 0, 4, "cell")) |
|
647 { |
|
648 int nr = 0; |
|
649 int nc = 0; |
|
650 |
|
651 if (extract_keyword (is, "rows", nr) && nr >= 0 |
|
652 && extract_keyword (is, "columns", nc) && nc >= 0) |
|
653 { |
|
654 if (nr > 0 && nc > 0) |
|
655 { |
|
656 Cell tmp (nr, nc); |
|
657 |
|
658 for (int j = 0; j < nc; j++) |
|
659 { |
|
660 for (int i = 0; i < nr; i++) |
|
661 { |
|
662 octave_value t2; |
|
663 |
|
664 // recurse to read cell elements |
|
665 std::string nm |
|
666 = read_ascii_data (is, filename, global, t2, count); |
|
667 |
|
668 if (nm == CELL_ELT_TAG) |
|
669 { |
|
670 if (is) |
|
671 tmp.elem (i, j) = t2; |
|
672 } |
|
673 else |
|
674 { |
|
675 error ("load: cell array element had unexpected name"); |
|
676 goto cell_read_error; |
|
677 } |
|
678 } |
|
679 } |
|
680 |
|
681 cell_read_error: |
|
682 |
|
683 if (is) |
|
684 tc = tmp; |
|
685 else |
|
686 error ("load: failed to load cell element"); |
|
687 } |
|
688 else if (nr == 0 || nc == 0) |
|
689 tc = Cell (nr, nc); |
|
690 else |
|
691 panic_impossible (); |
|
692 } |
|
693 else |
|
694 error ("load: failed to extract number of rows and columns for cell array"); |
|
695 } |
4171
|
696 else if (SUBSTRING_COMPARE_EQ (typ, 0, 14, "complex scalar")) |
604
|
697 { |
4130
|
698 Complex tmp = octave_read_complex (is); |
604
|
699 if (is) |
|
700 tc = tmp; |
|
701 else |
|
702 error ("load: failed to load complex scalar constant"); |
|
703 } |
4171
|
704 else if (SUBSTRING_COMPARE_EQ (typ, 0, 14, "complex matrix")) |
604
|
705 { |
3019
|
706 int nr = 0; |
|
707 int nc = 0; |
604
|
708 |
|
709 if (extract_keyword (is, "rows", nr) && nr > 0 |
|
710 && extract_keyword (is, "columns", nc) && nc > 0) |
|
711 { |
|
712 ComplexMatrix tmp (nr, nc); |
|
713 is >> tmp; |
|
714 if (is) |
|
715 tc = tmp; |
|
716 else |
|
717 error ("load: failed to load complex matrix constant"); |
|
718 } |
|
719 else |
|
720 error ("load: failed to extract number of rows and columns"); |
|
721 } |
4171
|
722 else if (SUBSTRING_COMPARE_EQ (typ, 0, 12, "string array")) |
1427
|
723 { |
|
724 int elements; |
3682
|
725 if (extract_keyword (is, "elements", elements) && elements >= 0) |
1427
|
726 { |
1572
|
727 // XXX FIXME XXX -- need to be able to get max length |
|
728 // before doing anything. |
|
729 |
|
730 charMatrix chm (elements, 0); |
|
731 int max_len = 0; |
1427
|
732 for (int i = 0; i < elements; i++) |
|
733 { |
|
734 int len; |
3136
|
735 if (extract_keyword (is, "length", len) && len >= 0) |
1427
|
736 { |
4249
|
737 OCTAVE_LOCAL_BUFFER (char, tmp, len+1); |
|
738 |
3557
|
739 if (len > 0 && ! is.read (X_CAST (char *, tmp), len)) |
1427
|
740 { |
|
741 error ("load: failed to load string constant"); |
|
742 break; |
|
743 } |
|
744 else |
1572
|
745 { |
2498
|
746 tmp [len] = '\0'; |
1572
|
747 if (len > max_len) |
|
748 { |
|
749 max_len = len; |
|
750 chm.resize (elements, max_len, 0); |
|
751 } |
|
752 chm.insert (tmp, i, 0); |
|
753 } |
1427
|
754 } |
|
755 else |
|
756 error ("load: failed to extract string length for element %d", i+1); |
|
757 } |
|
758 |
|
759 if (! error_state) |
2499
|
760 tc = octave_value (chm, true); |
1427
|
761 } |
|
762 else |
|
763 error ("load: failed to extract number of string elements"); |
|
764 } |
4171
|
765 else if (SUBSTRING_COMPARE_EQ (typ, 0, 6, "string")) |
604
|
766 { |
|
767 int len; |
3682
|
768 if (extract_keyword (is, "length", len) && len >= 0) |
604
|
769 { |
4249
|
770 OCTAVE_LOCAL_BUFFER (char, tmp, len+1); |
|
771 |
3682
|
772 if (len > 0 && ! is.read (X_CAST (char *, tmp), len)) |
|
773 { |
|
774 error ("load: failed to load string constant"); |
|
775 } |
604
|
776 else |
3682
|
777 { |
|
778 tmp [len] = '\0'; |
|
779 |
|
780 if (is) |
|
781 tc = tmp; |
|
782 else |
|
783 error ("load: failed to load string constant"); |
|
784 } |
604
|
785 } |
|
786 else |
|
787 error ("load: failed to extract string length"); |
|
788 } |
4171
|
789 else if (SUBSTRING_COMPARE_EQ (typ, 0, 5, "range")) |
604
|
790 { |
3687
|
791 // # base, limit, range comment added by save (). |
1358
|
792 |
604
|
793 skip_comments (is); |
|
794 Range tmp; |
|
795 is >> tmp; |
|
796 if (is) |
|
797 tc = tmp; |
|
798 else |
|
799 error ("load: failed to load range constant"); |
|
800 } |
|
801 else |
4171
|
802 error ("load: unknown constant type `%s'", tag.c_str ()); |
604
|
803 } |
|
804 else |
|
805 error ("load: failed to extract keyword specifying value type"); |
|
806 |
|
807 if (error_state) |
|
808 { |
1755
|
809 error ("load: reading file %s", filename.c_str ()); |
4171
|
810 return std::string (); |
604
|
811 } |
|
812 |
|
813 return name; |
|
814 } |
|
815 |
|
816 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
817 // place it in TC, returning the name of the variable. If the value |
3019
|
818 // is tagged as global in the file, return TRUE in GLOBAL. If SWAP |
|
819 // is TRUE, swap bytes after reading. |
604
|
820 // |
|
821 // The data is expected to be in the following format: |
|
822 // |
867
|
823 // Header (one per file): |
|
824 // ===================== |
604
|
825 // |
867
|
826 // object type bytes |
|
827 // ------ ---- ----- |
|
828 // magic number string 10 |
604
|
829 // |
867
|
830 // float format integer 1 |
|
831 // |
604
|
832 // |
867
|
833 // Data (one set for each item): |
|
834 // ============================ |
604
|
835 // |
867
|
836 // object type bytes |
|
837 // ------ ---- ----- |
|
838 // name_length integer 4 |
604
|
839 // |
867
|
840 // name string name_length |
604
|
841 // |
867
|
842 // doc_length integer 4 |
|
843 // |
|
844 // doc string doc_length |
604
|
845 // |
867
|
846 // global flag integer 1 |
604
|
847 // |
867
|
848 // data type integer 1 |
604
|
849 // |
867
|
850 // data (one of): |
|
851 // |
|
852 // scalar: |
|
853 // data real 8 |
604
|
854 // |
867
|
855 // complex scalar: |
|
856 // data complex 16 |
|
857 // |
|
858 // matrix: |
|
859 // rows integer 4 |
|
860 // columns integer 4 |
|
861 // data real r*c*8 |
604
|
862 // |
867
|
863 // complex matrix: |
|
864 // rows integer 4 |
|
865 // columns integer 4 |
|
866 // data complex r*c*16 |
604
|
867 // |
867
|
868 // string: |
|
869 // length int 4 |
|
870 // data string length |
604
|
871 // |
867
|
872 // range: |
|
873 // base real 8 |
|
874 // limit real 8 |
|
875 // increment real 8 |
604
|
876 // |
1427
|
877 // string array |
|
878 // elements int 4 |
|
879 // |
|
880 // for each element: |
|
881 // length int 4 |
|
882 // data string length |
|
883 // |
604
|
884 // FILENAME is used for error messages. |
|
885 |
4171
|
886 static std::string |
3523
|
887 read_binary_data (std::istream& is, bool swap, |
2318
|
888 oct_mach_info::float_format fmt, |
3523
|
889 const std::string& filename, bool& global, |
4171
|
890 octave_value& tc, std::string& doc) |
604
|
891 { |
4171
|
892 std::string retval; |
|
893 |
604
|
894 char tmp = 0; |
|
895 |
3019
|
896 FOUR_BYTE_INT name_len = 0; |
|
897 FOUR_BYTE_INT doc_len = 0; |
4171
|
898 |
|
899 doc.resize (0); |
604
|
900 |
1358
|
901 // We expect to fail here, at the beginning of a record, so not |
|
902 // being able to read another name should not result in an error. |
867
|
903 |
3557
|
904 is.read (X_CAST (char *, &name_len), 4); |
604
|
905 if (! is) |
4171
|
906 return retval; |
604
|
907 if (swap) |
3145
|
908 swap_4_bytes (X_CAST (char *, &name_len)); |
604
|
909 |
4251
|
910 { |
|
911 OCTAVE_LOCAL_BUFFER (char, name, name_len+1); |
|
912 name[name_len] = '\0'; |
|
913 if (! is.read (X_CAST (char *, name), name_len)) |
|
914 goto data_read_error; |
|
915 retval = name; |
|
916 } |
604
|
917 |
3557
|
918 is.read (X_CAST (char *, &doc_len), 4); |
604
|
919 if (! is) |
|
920 goto data_read_error; |
|
921 if (swap) |
3145
|
922 swap_4_bytes (X_CAST (char *, &doc_len)); |
604
|
923 |
4251
|
924 { |
|
925 OCTAVE_LOCAL_BUFFER (char, tdoc, doc_len+1); |
|
926 tdoc[doc_len] = '\0'; |
|
927 if (! is.read (X_CAST (char *, tdoc), doc_len)) |
|
928 goto data_read_error; |
|
929 doc = tdoc; |
|
930 } |
604
|
931 |
3557
|
932 if (! is.read (X_CAST (char *, &tmp), 1)) |
604
|
933 goto data_read_error; |
|
934 global = tmp ? 1 : 0; |
|
935 |
|
936 tmp = 0; |
3557
|
937 if (! is.read (X_CAST (char *, &tmp), 1)) |
604
|
938 goto data_read_error; |
|
939 |
|
940 switch (tmp) |
|
941 { |
|
942 case 1: |
|
943 { |
3557
|
944 if (! is.read (X_CAST (char *, &tmp), 1)) |
604
|
945 goto data_read_error; |
630
|
946 double dtmp; |
3145
|
947 read_doubles (is, &dtmp, X_CAST (save_type, tmp), 1, swap, fmt); |
774
|
948 if (error_state || ! is) |
630
|
949 goto data_read_error; |
604
|
950 tc = dtmp; |
|
951 } |
|
952 break; |
|
953 |
|
954 case 2: |
|
955 { |
|
956 FOUR_BYTE_INT nr, nc; |
3557
|
957 if (! is.read (X_CAST (char *, &nr), 4)) |
604
|
958 goto data_read_error; |
|
959 if (swap) |
3145
|
960 swap_4_bytes (X_CAST (char *, &nr)); |
3557
|
961 if (! is.read (X_CAST (char *, &nc), 4)) |
604
|
962 goto data_read_error; |
|
963 if (swap) |
3145
|
964 swap_4_bytes (X_CAST (char *, &nc)); |
3557
|
965 if (! is.read (X_CAST (char *, &tmp), 1)) |
604
|
966 goto data_read_error; |
|
967 Matrix m (nr, nc); |
|
968 double *re = m.fortran_vec (); |
|
969 int len = nr * nc; |
3145
|
970 read_doubles (is, re, X_CAST (save_type, tmp), len, swap, fmt); |
774
|
971 if (error_state || ! is) |
604
|
972 goto data_read_error; |
|
973 tc = m; |
|
974 } |
|
975 break; |
|
976 |
|
977 case 3: |
|
978 { |
3557
|
979 if (! is.read (X_CAST (char *, &tmp), 1)) |
604
|
980 goto data_read_error; |
630
|
981 Complex ctmp; |
3145
|
982 read_doubles (is, X_CAST (double *, &ctmp), |
|
983 X_CAST (save_type, tmp), 2, swap, fmt); |
774
|
984 if (error_state || ! is) |
630
|
985 goto data_read_error; |
604
|
986 tc = ctmp; |
|
987 } |
|
988 break; |
|
989 |
|
990 case 4: |
|
991 { |
|
992 FOUR_BYTE_INT nr, nc; |
3557
|
993 if (! is.read (X_CAST (char *, &nr), 4)) |
604
|
994 goto data_read_error; |
|
995 if (swap) |
3145
|
996 swap_4_bytes (X_CAST (char *, &nr)); |
3557
|
997 if (! is.read (X_CAST (char *, &nc), 4)) |
604
|
998 goto data_read_error; |
|
999 if (swap) |
3145
|
1000 swap_4_bytes (X_CAST (char *, &nc)); |
3557
|
1001 if (! is.read (X_CAST (char *, &tmp), 1)) |
604
|
1002 goto data_read_error; |
|
1003 ComplexMatrix m (nr, nc); |
|
1004 Complex *im = m.fortran_vec (); |
|
1005 int len = nr * nc; |
3145
|
1006 read_doubles (is, X_CAST (double *, im), |
|
1007 X_CAST (save_type, tmp), 2*len, swap, fmt); |
774
|
1008 if (error_state || ! is) |
604
|
1009 goto data_read_error; |
|
1010 tc = m; |
|
1011 } |
|
1012 break; |
|
1013 |
|
1014 case 5: |
|
1015 { |
1427
|
1016 FOUR_BYTE_INT len; |
3557
|
1017 if (! is.read (X_CAST (char *, &len), 4)) |
604
|
1018 goto data_read_error; |
|
1019 if (swap) |
3145
|
1020 swap_4_bytes (X_CAST (char *, &len)); |
4249
|
1021 OCTAVE_LOCAL_BUFFER (char, s, len+1); |
3557
|
1022 if (! is.read (X_CAST (char *, s), len)) |
4171
|
1023 goto data_read_error; |
604
|
1024 s[len] = '\0'; |
|
1025 tc = s; |
|
1026 } |
|
1027 break; |
|
1028 |
|
1029 case 6: |
|
1030 { |
3557
|
1031 if (! is.read (X_CAST (char *, &tmp), 1)) |
630
|
1032 goto data_read_error; |
604
|
1033 double bas, lim, inc; |
3557
|
1034 if (! is.read (X_CAST (char *, &bas), 8)) |
604
|
1035 goto data_read_error; |
|
1036 if (swap) |
3145
|
1037 swap_8_bytes (X_CAST (char *, &bas)); |
3557
|
1038 if (! is.read (X_CAST (char *, &lim), 8)) |
604
|
1039 goto data_read_error; |
|
1040 if (swap) |
3145
|
1041 swap_8_bytes (X_CAST (char *, &lim)); |
3557
|
1042 if (! is.read (X_CAST (char *, &inc), 8)) |
604
|
1043 goto data_read_error; |
|
1044 if (swap) |
3145
|
1045 swap_8_bytes (X_CAST (char *, &inc)); |
604
|
1046 Range r (bas, lim, inc); |
|
1047 tc = r; |
|
1048 } |
|
1049 break; |
|
1050 |
1427
|
1051 case 7: |
|
1052 { |
|
1053 FOUR_BYTE_INT elements; |
3557
|
1054 if (! is.read (X_CAST (char *, &elements), 4)) |
1427
|
1055 goto data_read_error; |
|
1056 if (swap) |
3145
|
1057 swap_4_bytes (X_CAST (char *, &elements)); |
1572
|
1058 charMatrix chm (elements, 0); |
|
1059 int max_len = 0; |
1427
|
1060 for (int i = 0; i < elements; i++) |
|
1061 { |
|
1062 FOUR_BYTE_INT len; |
3557
|
1063 if (! is.read (X_CAST (char *, &len), 4)) |
1427
|
1064 goto data_read_error; |
|
1065 if (swap) |
3145
|
1066 swap_4_bytes (X_CAST (char *, &len)); |
4249
|
1067 OCTAVE_LOCAL_BUFFER (char, tmp, len+1); |
3557
|
1068 if (! is.read (X_CAST (char *, tmp), len)) |
4171
|
1069 goto data_read_error; |
1572
|
1070 if (len > max_len) |
|
1071 { |
|
1072 max_len = len; |
|
1073 chm.resize (elements, max_len, 0); |
|
1074 } |
2497
|
1075 tmp [len] = '\0'; |
1572
|
1076 chm.insert (tmp, i, 0); |
1427
|
1077 } |
2499
|
1078 tc = octave_value (chm, true); |
1427
|
1079 } |
|
1080 break; |
|
1081 |
604
|
1082 default: |
|
1083 data_read_error: |
1755
|
1084 error ("load: trouble reading binary file `%s'", filename.c_str ()); |
604
|
1085 break; |
|
1086 } |
|
1087 |
4171
|
1088 return retval; |
604
|
1089 } |
|
1090 |
3687
|
1091 // HDF5 input/output |
|
1092 |
|
1093 #ifdef HAVE_HDF5 |
|
1094 |
|
1095 // Define this to 1 if/when HDF5 supports automatic conversion between |
|
1096 // integer and floating-point binary data: |
|
1097 #define HAVE_HDF5_INT2FLOAT_CONVERSIONS 0 |
|
1098 |
|
1099 // first, we need to define our own dummy stream subclass, since |
|
1100 // HDF5 needs to do its own file i/o |
|
1101 |
|
1102 // hdf5_fstreambase is used for both input and output streams, modeled |
|
1103 // on the fstreambase class in <fstream.h> |
|
1104 |
|
1105 class hdf5_fstreambase : virtual public std::ios |
|
1106 { |
|
1107 public: |
|
1108 |
|
1109 // HDF5 uses an "id" to refer to an open file |
|
1110 hid_t file_id; |
|
1111 |
|
1112 // keep track of current item index in the file |
|
1113 int current_item; |
|
1114 |
|
1115 hdf5_fstreambase () { file_id = -1; } |
|
1116 |
|
1117 hdf5_fstreambase (const char *name, int mode, int prot = 0) |
|
1118 { |
|
1119 if (mode == std::ios::in) |
|
1120 file_id = H5Fopen (name, H5F_ACC_RDONLY, H5P_DEFAULT); |
|
1121 else if (mode == std::ios::out) |
|
1122 file_id = H5Fcreate (name, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); |
|
1123 |
|
1124 if (file_id < 0) |
3897
|
1125 std::ios::setstate (std::ios::badbit); |
3687
|
1126 |
|
1127 current_item = 0; |
|
1128 } |
|
1129 |
|
1130 void close () |
|
1131 { |
|
1132 if (file_id >= 0) |
|
1133 { |
|
1134 if (H5Fclose (file_id) < 0) |
3897
|
1135 std::ios::setstate (std::ios::badbit); |
3687
|
1136 file_id = -1; |
|
1137 } |
|
1138 } |
|
1139 |
|
1140 void open (const char *name, int mode, int prot = 0) |
|
1141 { |
|
1142 clear (); |
|
1143 |
|
1144 if (mode == std::ios::in) |
|
1145 file_id = H5Fopen (name, H5F_ACC_RDONLY, H5P_DEFAULT); |
|
1146 else if (mode == std::ios::out) |
|
1147 file_id = H5Fcreate (name, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); |
|
1148 |
|
1149 if (file_id < 0) |
3897
|
1150 std::ios::setstate (std::ios::badbit); |
3687
|
1151 |
|
1152 current_item = 0; |
|
1153 } |
|
1154 }; |
|
1155 |
|
1156 // input and output streams, subclassing istream and ostream |
|
1157 // so that we can pass them for stream parameters in the functions below. |
|
1158 |
|
1159 class hdf5_ifstream : public hdf5_fstreambase, public std::istream |
|
1160 { |
|
1161 public: |
|
1162 |
3897
|
1163 hdf5_ifstream () : hdf5_fstreambase (), std::istream (0) { } |
3687
|
1164 |
|
1165 hdf5_ifstream (const char *name, int mode = std::ios::in, int prot = 0) |
3897
|
1166 : hdf5_fstreambase (name, mode, prot), std::istream (0) { } |
3687
|
1167 |
|
1168 void open (const char *name, int mode = std::ios::in, int prot = 0) |
|
1169 { hdf5_fstreambase::open (name, mode, prot); } |
|
1170 }; |
|
1171 |
|
1172 class hdf5_ofstream : public hdf5_fstreambase, public std::ostream |
|
1173 { |
|
1174 public: |
|
1175 |
3897
|
1176 hdf5_ofstream () : hdf5_fstreambase (), std::ostream (0) { } |
3687
|
1177 |
|
1178 hdf5_ofstream (const char *name, int mode = std::ios::out, int prot = 0) |
3897
|
1179 : hdf5_fstreambase (name, mode, prot), std::ostream (0) { } |
3687
|
1180 |
|
1181 void open (const char *name, int mode = std::ios::out, int prot = 0) |
|
1182 { hdf5_fstreambase::open (name, mode, prot); } |
|
1183 }; |
|
1184 |
|
1185 // Given two compound types t1 and t2, determine whether they |
|
1186 // are compatible for reading/writing. This function only |
|
1187 // works for non-nested types composed of simple elements (ints, floats...), |
|
1188 // which is all we need it for |
|
1189 |
|
1190 bool |
|
1191 hdf5_types_compatible (hid_t t1, hid_t t2) |
|
1192 { |
|
1193 int n; |
|
1194 if ((n = H5Tget_nmembers (t1)) != H5Tget_nmembers (t2)) |
|
1195 return false; |
|
1196 |
|
1197 for (int i = 0; i < n; ++i) |
|
1198 { |
|
1199 hid_t mt1 = H5Tget_member_type (t1, i); |
|
1200 hid_t mt2 = H5Tget_member_type (t2, i); |
|
1201 |
|
1202 if (H5Tget_class (mt1) != H5Tget_class (mt2)) |
|
1203 return false; |
|
1204 |
|
1205 H5Tclose (mt2); |
|
1206 H5Tclose (mt1); |
|
1207 } |
|
1208 |
|
1209 return true; |
|
1210 } |
|
1211 |
|
1212 // Import a multidimensional (rank >= 3) dataset whose id is data_id, into tc. |
|
1213 // This works by calling itself recursively, building up lists of lists |
|
1214 // of lists ... of 2d matrices. rank and dims are the rank and dimensions |
|
1215 // of the dataset. type_id is the datatype to read into. If it is |
|
1216 // H5T_NATIVE_DOUBLE, we are reading a real matrix. Otherwise, type_id |
|
1217 // is assumed to be a complex type for reading a complex matrix. |
|
1218 // |
|
1219 // Upon entry, we should have curdim = rank - 1, start = an array |
|
1220 // of length rank = all zeros, and count = an array of length rank = |
|
1221 // all ones except for the first two dimensions which equal the corresponding |
|
1222 // entries in dims[]. |
|
1223 // |
|
1224 // Note that we process the dimensions in reverse order, reflecting |
|
1225 // the fact that Octave is uses column-major (Fortran-order) data while |
|
1226 // HDF5 is row-major. This means that the HDF5 file is read |
|
1227 // non-contiguously, but on the other hand means that for a 3d array |
|
1228 // we get a list of xy-plane slices, which seems nice. We could change |
|
1229 // this behavior without much trouble; what is the best thing to do? |
|
1230 // |
|
1231 // Returns a positive value upon success. |
|
1232 |
|
1233 static herr_t |
|
1234 hdf5_import_multidim (hid_t data_id, hid_t space_id, hsize_t rank, |
|
1235 const hsize_t *dims, hsize_t curdim, |
|
1236 hssize_t *start, const hsize_t *count, |
|
1237 hid_t type_id, octave_value &tc) |
|
1238 { |
|
1239 herr_t retval = 1; |
|
1240 |
|
1241 if (rank < 3 || curdim < 1 || curdim >= rank) |
|
1242 return -1; |
|
1243 |
|
1244 if (curdim == 1) |
|
1245 { |
|
1246 // import 2d dataset for 1st 2 dims directly as a matrix |
|
1247 int nr, nc; // rows and columns |
|
1248 nc = dims[0]; // octave uses column-major & HDF5 uses row-major |
|
1249 nr = dims[1]; |
|
1250 |
3956
|
1251 hid_t mem_space_id = H5Screate_simple (2, dims, 0); |
3687
|
1252 |
|
1253 if (mem_space_id < 0) |
|
1254 return -1; |
|
1255 |
|
1256 if (H5Sselect_all (mem_space_id) < 0) |
|
1257 return -1; |
|
1258 |
|
1259 if (H5Sselect_hyperslab (space_id, H5S_SELECT_SET, |
3956
|
1260 start, 0, count, 0) < 0) |
3687
|
1261 { |
|
1262 H5Sclose (mem_space_id); |
|
1263 return -1; |
|
1264 } |
|
1265 |
|
1266 if (type_id == H5T_NATIVE_DOUBLE) |
|
1267 { |
|
1268 // real matrix |
|
1269 Matrix m (nr, nc); |
|
1270 double *re = m.fortran_vec (); |
|
1271 if (H5Dread (data_id, type_id, mem_space_id, space_id, |
|
1272 H5P_DEFAULT, (void *) re) < 0) |
|
1273 retval = -1; // error |
|
1274 else |
|
1275 tc = m; |
|
1276 } |
|
1277 else |
|
1278 { |
|
1279 // assume that we are using complex numbers |
|
1280 // complex matrix |
|
1281 ComplexMatrix m (nr, nc); |
|
1282 Complex *reim = m.fortran_vec (); |
|
1283 if (H5Dread (data_id, type_id, mem_space_id, space_id, |
|
1284 H5P_DEFAULT, (void *) X_CAST (double *, reim)) < 0) |
|
1285 retval = -1; // error |
|
1286 else |
|
1287 tc = m; |
|
1288 } |
|
1289 |
|
1290 H5Sclose (mem_space_id); |
|
1291 |
|
1292 } |
|
1293 else |
|
1294 { |
|
1295 octave_value_list lst; |
|
1296 |
|
1297 for (hsize_t i = 0; i < dims[curdim]; ++i) |
|
1298 { |
|
1299 octave_value slice; |
|
1300 start[curdim] = i; |
|
1301 retval = hdf5_import_multidim (data_id, space_id, rank, |
|
1302 dims, curdim-1, start, count, |
|
1303 type_id, slice); |
|
1304 if (retval < 0) |
|
1305 break; |
|
1306 lst.append (slice); |
|
1307 } |
|
1308 |
|
1309 if (retval > 0) |
4233
|
1310 tc = octave_value (lst); |
3687
|
1311 } |
|
1312 |
|
1313 return retval; |
|
1314 } |
|
1315 |
|
1316 // Return true if loc_id has the attribute named attr_name, and false |
|
1317 // otherwise. |
|
1318 |
|
1319 bool |
|
1320 hdf5_check_attr (hid_t loc_id, const char *attr_name) |
|
1321 { |
|
1322 bool retval = false; |
|
1323 |
|
1324 // we have to pull some shenanigans here to make sure |
|
1325 // HDF5 doesn't print out all sorts of error messages if we |
|
1326 // call H5Aopen for a non-existing attribute |
|
1327 |
|
1328 H5E_auto_t err_func; |
|
1329 void *err_func_data; |
|
1330 |
|
1331 // turn off error reporting temporarily, but save the error |
|
1332 // reporting function: |
|
1333 |
|
1334 H5Eget_auto (&err_func, &err_func_data); |
3956
|
1335 H5Eset_auto (0, 0); |
3687
|
1336 |
3688
|
1337 hid_t attr_id = H5Aopen_name (loc_id, attr_name); |
3687
|
1338 |
|
1339 if (attr_id >= 0) |
|
1340 { |
|
1341 // successful |
|
1342 retval = 1; |
|
1343 H5Aclose (attr_id); |
|
1344 } |
|
1345 |
|
1346 // restore error reporting: |
|
1347 H5Eset_auto (err_func, err_func_data); |
|
1348 |
|
1349 return retval; |
|
1350 } |
|
1351 |
4335
|
1352 // The following two subroutines create HDF5 representations of the way |
|
1353 // we will store Octave complex and range types (pairs and triplets of |
|
1354 // floating-point numbers, respectively). NUM_TYPE is the HDF5 numeric |
|
1355 // type to use for storage (e.g. H5T_NATIVE_DOUBLE to save as 'double'). |
|
1356 // Note that any necessary conversions are handled automatically by HDF5. |
|
1357 |
|
1358 static hid_t |
|
1359 hdf5_make_complex_type (hid_t num_type) |
|
1360 { |
|
1361 hid_t type_id = H5Tcreate (H5T_COMPOUND, sizeof (double) * 2); |
|
1362 |
|
1363 H5Tinsert (type_id, "real", 0 * sizeof (double), num_type); |
|
1364 H5Tinsert (type_id, "imag", 1 * sizeof (double), num_type); |
|
1365 |
|
1366 return type_id; |
|
1367 } |
|
1368 |
|
1369 static hid_t |
|
1370 hdf5_make_range_type (hid_t num_type) |
|
1371 { |
|
1372 hid_t type_id = H5Tcreate (H5T_COMPOUND, sizeof (double) * 3); |
|
1373 |
|
1374 H5Tinsert (type_id, "base", 0 * sizeof (double), num_type); |
|
1375 H5Tinsert (type_id, "limit", 1 * sizeof (double), num_type); |
|
1376 H5Tinsert (type_id, "increment", 2 * sizeof (double), num_type); |
|
1377 |
|
1378 return type_id; |
|
1379 } |
|
1380 |
3687
|
1381 // Callback data structure for passing data to hdf5_read_next_data, below. |
|
1382 |
4335
|
1383 struct |
|
1384 hdf5_callback_data |
3687
|
1385 { |
4335
|
1386 hdf5_callback_data (void) |
|
1387 : name (), global (false), tc (), doc (), |
|
1388 complex_type (hdf5_make_complex_type (H5T_NATIVE_DOUBLE)), |
|
1389 range_type (hdf5_make_range_type (H5T_NATIVE_DOUBLE)), |
|
1390 import (false) { } |
|
1391 |
3687
|
1392 // the following fields are set by hdf5_read_data on successful return: |
|
1393 |
|
1394 // the name of the variable |
4335
|
1395 std::string name; |
3687
|
1396 |
|
1397 // whether it is global |
|
1398 bool global; |
|
1399 |
|
1400 // the value of the variable, in Octave form |
|
1401 octave_value tc; |
|
1402 |
|
1403 // a documentation string (NULL if none) |
4335
|
1404 std::string doc; |
3687
|
1405 |
|
1406 // the following fields are input to hdf5_read_data: |
|
1407 |
|
1408 // HDF5 rep's of complex and range type |
|
1409 hid_t complex_type, range_type; |
|
1410 |
|
1411 // whether to try extra hard to import "foreign" data |
|
1412 bool import; |
|
1413 }; |
|
1414 |
3695
|
1415 // This variable, set in read_hdf5_data(), tells whether we are using |
|
1416 // a version of HDF5 with a buggy H5Giterate (i.e. which neglects to |
|
1417 // increment the index parameter to the next unread item). |
|
1418 static bool have_h5giterate_bug = false; |
|
1419 |
3687
|
1420 // This function is designed to be passed to H5Giterate, which calls it |
|
1421 // on each data item in an HDF5 file. For the item whose name is NAME in |
|
1422 // the group GROUP_ID, this function sets dv->tc to an Octave representation |
|
1423 // of that item. (dv must be a pointer to hdf5_callback_data.) (It also |
|
1424 // sets the other fields of dv). |
|
1425 // |
|
1426 // It returns 1 on success (in which case H5Giterate stops and returns), |
|
1427 // -1 on error, and 0 to tell H5Giterate to continue on to the next item |
|
1428 // (e.g. if NAME was a data type we don't recognize). |
|
1429 |
|
1430 static herr_t |
|
1431 hdf5_read_next_data (hid_t group_id, const char *name, void *dv) |
|
1432 { |
4335
|
1433 hdf5_callback_data *d = static_cast <hdf5_callback_data *> (dv); |
|
1434 |
3687
|
1435 H5G_stat_t info; |
|
1436 herr_t retval = 0; |
|
1437 bool ident_valid = valid_identifier (name); |
4249
|
1438 |
4528
|
1439 std::string vname = name; |
|
1440 |
|
1441 // Allow identifiers as all digits so we can load lists saved by |
|
1442 // earlier versions of Octave. |
|
1443 |
|
1444 if (! ident_valid && (d->import || ident_is_all_digits (vname))) |
3687
|
1445 { |
|
1446 // fix the identifier, replacing invalid chars with underscores |
4528
|
1447 vname = make_valid_identifier (vname); |
3687
|
1448 |
|
1449 // check again (in case vname was null, empty, or some such thing): |
|
1450 ident_valid = valid_identifier (vname); |
|
1451 } |
|
1452 |
|
1453 H5Gget_objinfo (group_id, name, 1, &info); |
|
1454 |
|
1455 if (info.type == H5G_DATASET && ident_valid) |
|
1456 { |
|
1457 retval = 1; |
|
1458 |
|
1459 hid_t data_id = H5Dopen (group_id, name); |
|
1460 |
|
1461 if (data_id < 0) |
|
1462 { |
|
1463 retval = data_id; |
|
1464 |
|
1465 goto done; |
|
1466 } |
|
1467 |
|
1468 hid_t type_id = H5Dget_type (data_id); |
|
1469 |
|
1470 hid_t type_class_id = H5Tget_class (type_id); |
|
1471 |
|
1472 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS |
|
1473 if (type_class_id == H5T_INTEGER || type_class_id == H5T_FLOAT) |
|
1474 { |
|
1475 #else |
|
1476 // hdf5 doesn't (yet) support automatic float/integer conversions |
|
1477 if (type_class_id == H5T_FLOAT) |
|
1478 { |
|
1479 #endif |
|
1480 // read real matrix or scalar variable |
|
1481 |
|
1482 hid_t space_id = H5Dget_space (data_id); |
|
1483 |
|
1484 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
1485 |
|
1486 if (rank == 0) |
|
1487 { |
|
1488 // real scalar: |
|
1489 double dtmp; |
|
1490 if (H5Dread (data_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, |
|
1491 H5P_DEFAULT, (void *) &dtmp) < 0) |
|
1492 retval = -1; // error |
|
1493 else |
|
1494 d->tc = dtmp; |
|
1495 } |
|
1496 else if (rank > 0 && rank <= 2) |
|
1497 { |
|
1498 // real matrix |
4249
|
1499 OCTAVE_LOCAL_BUFFER (hsize_t, dims, rank); |
|
1500 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); |
3687
|
1501 |
|
1502 H5Sget_simple_extent_dims (space_id, dims, maxdims); |
|
1503 |
|
1504 int nr, nc; // rows and columns |
|
1505 // octave uses column-major & HDF5 uses row-major |
|
1506 nc = dims[0]; |
|
1507 nr = rank > 1 ? dims[1] : 1; |
|
1508 Matrix m (nr, nc); |
|
1509 double *re = m.fortran_vec (); |
|
1510 if (H5Dread (data_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, |
|
1511 H5P_DEFAULT, (void *) re) < 0) |
|
1512 retval = -1; // error |
|
1513 else |
|
1514 d->tc = m; |
|
1515 } |
|
1516 else if (rank >= 3 && d->import) |
|
1517 { |
4249
|
1518 OCTAVE_LOCAL_BUFFER (hsize_t, dims, rank); |
|
1519 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); |
3687
|
1520 |
|
1521 H5Sget_simple_extent_dims (space_id, dims, maxdims); |
|
1522 |
4249
|
1523 OCTAVE_LOCAL_BUFFER (hssize_t, start, rank); |
|
1524 OCTAVE_LOCAL_BUFFER (hsize_t, count, rank); |
3687
|
1525 |
|
1526 for (hsize_t i = 0; i < rank; ++i) |
|
1527 { |
|
1528 start[i] = 0; |
|
1529 count[i] = 1; |
|
1530 } |
|
1531 count[0] = dims[0]; |
|
1532 count[1] = dims[1]; |
|
1533 retval = hdf5_import_multidim (data_id, space_id, |
|
1534 rank, dims, rank-1, |
|
1535 start, count, |
|
1536 H5T_NATIVE_DOUBLE, d->tc); |
|
1537 } |
|
1538 else |
|
1539 { |
|
1540 warning ("load: can't read %d-dim. hdf5 dataset %s", |
|
1541 rank, name); |
|
1542 retval = 0; // skip; we can't read 3+ dimensional datasets |
|
1543 } |
|
1544 |
|
1545 H5Sclose (space_id); |
|
1546 } |
|
1547 else if (type_class_id == H5T_STRING) |
|
1548 { |
|
1549 // read string variable |
|
1550 hid_t space_id = H5Dget_space (data_id); |
|
1551 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
1552 |
|
1553 if (rank == 0) |
|
1554 { |
|
1555 // a single string: |
|
1556 int slen = H5Tget_size (type_id); |
|
1557 if (slen < 0) |
|
1558 retval = -1; // error |
|
1559 else |
|
1560 { |
4335
|
1561 OCTAVE_LOCAL_BUFFER (char, s, slen); |
3687
|
1562 // create datatype for (null-terminated) string |
|
1563 // to read into: |
|
1564 hid_t st_id = H5Tcopy (H5T_C_S1); |
|
1565 H5Tset_size (st_id, slen); |
|
1566 if (H5Dread (data_id, st_id, H5S_ALL, H5S_ALL, |
|
1567 H5P_DEFAULT, (void *) s) < 0) |
|
1568 { |
|
1569 retval = -1; // error |
|
1570 } |
|
1571 else |
|
1572 d->tc = s; |
|
1573 |
|
1574 H5Tclose (st_id); |
|
1575 } |
|
1576 } |
|
1577 else if (rank == 1) |
|
1578 { |
|
1579 // string vector |
|
1580 hsize_t elements, maxdim; |
|
1581 H5Sget_simple_extent_dims (space_id, &elements, &maxdim); |
|
1582 int slen = H5Tget_size (type_id); |
|
1583 if (slen < 0) |
|
1584 retval = -1; // error |
|
1585 else |
|
1586 { |
|
1587 // hdf5 string arrays store strings of all the |
|
1588 // same physical length (I think), which is |
|
1589 // slightly wasteful, but oh well. |
|
1590 |
4249
|
1591 OCTAVE_LOCAL_BUFFER (char, s, elements * slen); |
|
1592 |
3687
|
1593 // create datatype for (null-terminated) string |
|
1594 // to read into: |
|
1595 hid_t st_id = H5Tcopy (H5T_C_S1); |
|
1596 H5Tset_size (st_id, slen); |
|
1597 |
|
1598 if (H5Dread (data_id, st_id, H5S_ALL, H5S_ALL, |
|
1599 H5P_DEFAULT, (void *) s) < 0) |
|
1600 retval = -1; // error |
|
1601 else |
|
1602 { |
|
1603 charMatrix chm (elements, slen - 1); |
|
1604 for (hsize_t i = 0; i < elements; ++i) |
|
1605 { |
|
1606 chm.insert (s + i*slen, i, 0); |
|
1607 } |
|
1608 d->tc = octave_value (chm, true); |
|
1609 } |
|
1610 |
|
1611 H5Tclose (st_id); |
|
1612 } |
|
1613 } |
|
1614 else |
|
1615 { |
|
1616 warning ("load: can't read %d-dim. hdf5 string vector %s", |
|
1617 rank, name); |
|
1618 // skip; we can't read higher-dimensional string vectors |
|
1619 retval = 0; |
|
1620 } |
|
1621 } |
|
1622 else if (type_class_id == H5T_COMPOUND) |
|
1623 { |
|
1624 // check for complex or range data: |
|
1625 |
|
1626 if (hdf5_types_compatible (type_id, d->complex_type)) |
|
1627 { |
|
1628 // read complex matrix or scalar variable |
|
1629 |
|
1630 hid_t space_id = H5Dget_space (data_id); |
|
1631 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
1632 |
|
1633 if (rank == 0) |
|
1634 { |
|
1635 // complex scalar: |
|
1636 Complex ctmp; |
|
1637 if (H5Dread (data_id, d->complex_type, H5S_ALL, |
|
1638 H5S_ALL, H5P_DEFAULT, |
|
1639 (void *) X_CAST (double *, &ctmp)) < 0) |
|
1640 retval = -1; // error |
|
1641 else |
|
1642 d->tc = ctmp; |
|
1643 } |
|
1644 else if (rank > 0 && rank <= 2) |
|
1645 { |
|
1646 // complex matrix |
4249
|
1647 OCTAVE_LOCAL_BUFFER (hsize_t, dims, rank); |
|
1648 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); |
3687
|
1649 H5Sget_simple_extent_dims (space_id, dims, maxdims); |
|
1650 int nr, nc; // rows and columns |
|
1651 // octave uses column-major & HDF5 uses row-major |
|
1652 nc = dims[0]; |
|
1653 nr = rank > 1 ? dims[1] : 1; |
|
1654 ComplexMatrix m (nr, nc); |
|
1655 Complex *reim = m.fortran_vec (); |
|
1656 if (H5Dread (data_id, d->complex_type, H5S_ALL, |
|
1657 H5S_ALL, H5P_DEFAULT, |
|
1658 (void *) X_CAST (double *, reim)) < 0) |
|
1659 retval = -1; // error |
|
1660 else |
|
1661 d->tc = m; |
|
1662 } |
|
1663 else if (rank >= 3 && d->import) |
|
1664 { |
4249
|
1665 OCTAVE_LOCAL_BUFFER (hsize_t, dims, rank); |
|
1666 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); |
3687
|
1667 H5Sget_simple_extent_dims (space_id, dims, maxdims); |
4249
|
1668 OCTAVE_LOCAL_BUFFER (hssize_t, start, rank); |
|
1669 OCTAVE_LOCAL_BUFFER (hsize_t, count, rank); |
3687
|
1670 for (hsize_t i = 0; i < rank; ++i) |
|
1671 { |
|
1672 start[i] = 0; |
|
1673 count[i] = 1; |
|
1674 } |
|
1675 count[0] = dims[0]; |
|
1676 count[1] = dims[1]; |
|
1677 retval = hdf5_import_multidim (data_id, space_id, |
|
1678 rank, dims, rank-1, |
|
1679 start, count, |
|
1680 d->complex_type, |
|
1681 d->tc); |
|
1682 } |
|
1683 else |
|
1684 { |
|
1685 warning ("load: can't read %d-dim. hdf5 dataset %s", |
|
1686 rank, name); |
|
1687 // skip; we can't read 3+ dimensional datasets |
|
1688 retval = 0; |
|
1689 } |
|
1690 H5Sclose (space_id); |
|
1691 } |
|
1692 else if (hdf5_types_compatible (type_id, d->range_type)) |
|
1693 { |
|
1694 // read range variable: |
|
1695 hid_t space_id = H5Dget_space (data_id); |
|
1696 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
1697 |
|
1698 if (rank == 0) |
|
1699 { |
|
1700 double rangevals[3]; |
|
1701 if (H5Dread (data_id, d->range_type, H5S_ALL, H5S_ALL, |
|
1702 H5P_DEFAULT, (void *) rangevals) < 0) |
|
1703 retval = -1; // error |
|
1704 else |
|
1705 { |
|
1706 Range r (rangevals[0], rangevals[1], rangevals[2]); |
|
1707 d->tc = r; |
|
1708 } |
|
1709 } |
|
1710 else |
|
1711 { |
|
1712 warning ("load: can't read range array `%s' in hdf5 file", |
|
1713 name); |
|
1714 // skip; we can't read arrays of range variables |
|
1715 retval = 0; |
|
1716 } |
|
1717 |
|
1718 H5Sclose (space_id); |
|
1719 } |
|
1720 else |
|
1721 { |
|
1722 warning ("load: can't read `%s' (unknown compound datatype)", |
|
1723 name); |
|
1724 retval = 0; // unknown datatype; skip. |
|
1725 } |
|
1726 } |
|
1727 else |
|
1728 { |
|
1729 warning ("load: can't read `%s' (unknown datatype)", name); |
|
1730 retval = 0; // unknown datatype; skip |
|
1731 } |
|
1732 |
|
1733 H5Tclose (type_id); |
|
1734 |
|
1735 // check for OCTAVE_GLOBAL attribute: |
3688
|
1736 d->global = hdf5_check_attr (data_id, "OCTAVE_GLOBAL"); |
3687
|
1737 |
|
1738 H5Dclose (data_id); |
|
1739 } |
|
1740 else if (info.type == H5G_GROUP && ident_valid) |
|
1741 { |
|
1742 // read in group as a list or a structure |
|
1743 retval = 1; |
|
1744 |
|
1745 hid_t subgroup_id = H5Gopen (group_id, name); |
|
1746 |
|
1747 if (subgroup_id < 0) |
|
1748 { |
|
1749 retval = subgroup_id; |
|
1750 goto done; |
|
1751 } |
|
1752 |
|
1753 // an HDF5 group is treated as an octave structure by |
|
1754 // default (since that preserves name information), and an |
|
1755 // octave list otherwise. |
|
1756 |
3688
|
1757 bool is_list = hdf5_check_attr (subgroup_id, "OCTAVE_LIST"); |
3687
|
1758 |
|
1759 hdf5_callback_data dsub; |
4335
|
1760 |
3687
|
1761 dsub.complex_type = d->complex_type; |
|
1762 dsub.range_type = d->range_type; |
|
1763 dsub.import = d->import; |
|
1764 |
|
1765 herr_t retval2; |
|
1766 octave_value_list lst; |
|
1767 Octave_map m; |
|
1768 int current_item = 0; |
|
1769 while ((retval2 = H5Giterate (group_id, name, ¤t_item, |
|
1770 hdf5_read_next_data, &dsub)) > 0) |
|
1771 { |
|
1772 if (is_list) |
|
1773 lst.append (dsub.tc); |
|
1774 else |
4372
|
1775 { |
|
1776 octave_value ov = dsub.tc; |
|
1777 |
|
1778 if (ov.is_list ()) |
|
1779 m [dsub.name] = ov.list_value (); |
|
1780 else |
|
1781 m [dsub.name] = ov; |
|
1782 } |
3687
|
1783 |
3695
|
1784 if (have_h5giterate_bug) |
|
1785 current_item++; // H5Giterate returned the last index processed |
3687
|
1786 } |
|
1787 |
|
1788 if (retval2 < 0) |
|
1789 retval = retval2; |
|
1790 else |
|
1791 { |
3688
|
1792 d->global = hdf5_check_attr (group_id, "OCTAVE_GLOBAL"); |
3687
|
1793 |
|
1794 if (is_list) |
4233
|
1795 d->tc = octave_value (lst); |
3687
|
1796 else |
|
1797 d->tc = m; |
|
1798 } |
|
1799 |
|
1800 H5Gclose (subgroup_id); |
|
1801 } |
|
1802 else if (! ident_valid) |
|
1803 { |
|
1804 // should we attempt to handle invalid identifiers by converting |
|
1805 // bad characters to '_', say? |
|
1806 warning ("load: skipping invalid identifier `%s' in hdf5 file", |
|
1807 name); |
|
1808 } |
|
1809 |
|
1810 done: |
|
1811 |
|
1812 if (retval < 0) |
|
1813 error ("load: error while reading hdf5 item %s", name); |
|
1814 |
|
1815 if (retval > 0) |
|
1816 { |
|
1817 // get documentation string, if any: |
3956
|
1818 int comment_length = H5Gget_comment (group_id, name, 0, 0); |
3687
|
1819 |
|
1820 if (comment_length > 1) |
|
1821 { |
4335
|
1822 OCTAVE_LOCAL_BUFFER (char, tdoc, comment_length); |
|
1823 H5Gget_comment (group_id, name, comment_length, tdoc); |
|
1824 d->doc = tdoc; |
3687
|
1825 } |
4528
|
1826 else if (vname != name) |
3687
|
1827 { |
|
1828 // the name was changed by import; store the original name |
|
1829 // as the documentation string: |
4335
|
1830 d->doc = name; |
3687
|
1831 } |
|
1832 |
|
1833 // copy name (actually, vname): |
4335
|
1834 d->name = vname; |
3687
|
1835 } |
|
1836 |
|
1837 return retval; |
|
1838 } |
|
1839 |
|
1840 // Read the next Octave variable from the stream IS, which must really be |
|
1841 // an hdf5_ifstream. Return the variable value in tc, its doc string |
|
1842 // in doc, and whether it is global in global. The return value is |
|
1843 // the name of the variable, or NULL if none were found or there was |
|
1844 // and error. If import is true, we try extra hard to import "foreign" |
|
1845 // datasets (not created by Octave), although we usually do a reasonable |
|
1846 // job anyway. (c.f. load -import documentation.) |
4171
|
1847 static std::string |
3687
|
1848 read_hdf5_data (std::istream& is, |
|
1849 const std::string& filename, bool& global, |
4171
|
1850 octave_value& tc, std::string& doc, bool import) |
3687
|
1851 { |
4171
|
1852 std::string retval; |
|
1853 |
|
1854 doc.resize (0); |
|
1855 |
3687
|
1856 hdf5_ifstream& hs = (hdf5_ifstream&) is; |
|
1857 hdf5_callback_data d; |
|
1858 |
|
1859 d.import = import; |
|
1860 |
3695
|
1861 // Versions of HDF5 prior to 1.2.2 had a bug in H5Giterate where it |
|
1862 // would return the index of the last item processed instead of the |
|
1863 // next item to be processed, forcing us to increment the index manually. |
|
1864 |
|
1865 unsigned int vers_major, vers_minor, vers_release; |
|
1866 |
|
1867 H5get_libversion (&vers_major, &vers_minor, &vers_release); |
|
1868 |
|
1869 // XXX FIXME XXX -- this test looks wrong. |
|
1870 have_h5giterate_bug |
|
1871 = (vers_major < 1 |
|
1872 || (vers_major == 1 && (vers_minor < 2 |
|
1873 || (vers_minor == 2 && vers_release < 2)))); |
|
1874 |
4171
|
1875 herr_t H5Giterate_retval = H5Giterate (hs.file_id, "/", &hs.current_item, |
|
1876 hdf5_read_next_data, &d); |
3687
|
1877 |
3695
|
1878 if (have_h5giterate_bug) |
|
1879 { |
|
1880 // H5Giterate sets current_item to the last item processed; we want |
|
1881 // the index of the next item (for the next call to read_hdf5_data) |
|
1882 |
|
1883 hs.current_item++; |
|
1884 } |
3687
|
1885 |
4171
|
1886 if (H5Giterate_retval > 0) |
3687
|
1887 { |
|
1888 global = d.global; |
|
1889 tc = d.tc; |
4335
|
1890 doc = d.doc; |
3687
|
1891 } |
|
1892 else |
|
1893 { |
4171
|
1894 // an error occurred (H5Giterate_retval < 0) or there are no |
|
1895 // more datasets print an error message if retval < 0? |
3687
|
1896 // hdf5_read_next_data already printed one, probably. |
|
1897 } |
|
1898 |
|
1899 H5Tclose (d.complex_type); |
|
1900 H5Tclose (d.range_type); |
|
1901 |
4335
|
1902 if (! d.name.empty ()) |
4171
|
1903 retval = d.name; |
|
1904 |
|
1905 return retval; |
3687
|
1906 } |
|
1907 |
|
1908 #endif /* HAVE_HDF5 */ |
|
1909 |
3536
|
1910 static std::string |
3523
|
1911 get_mat_data_input_line (std::istream& is) |
3216
|
1912 { |
3523
|
1913 std::string retval; |
3216
|
1914 |
|
1915 bool have_data = false; |
|
1916 |
|
1917 do |
|
1918 { |
|
1919 retval = ""; |
|
1920 |
|
1921 char c; |
|
1922 while (is.get (c)) |
|
1923 { |
4304
|
1924 if (c == '\n' || c == '\r') |
3216
|
1925 break; |
|
1926 |
|
1927 if (c == '%' || c == '#') |
|
1928 { |
|
1929 // skip to end of line |
4304
|
1930 while (is.get (c)) |
|
1931 if (c == '\n' || c == '\r') |
|
1932 break; |
3216
|
1933 |
|
1934 break; |
|
1935 } |
|
1936 |
|
1937 if (! is.eof ()) |
|
1938 { |
|
1939 if (! have_data && c != ' ' && c != '\t') |
|
1940 have_data = true; |
|
1941 |
|
1942 retval += c; |
|
1943 } |
|
1944 } |
|
1945 } |
|
1946 while (! (have_data || is.eof ())); |
|
1947 |
|
1948 return retval; |
|
1949 } |
|
1950 |
2511
|
1951 static void |
3523
|
1952 get_lines_and_columns (std::istream& is, const std::string& filename, int& nr, int& nc) |
2511
|
1953 { |
3552
|
1954 std::streampos pos = is.tellg (); |
2511
|
1955 |
|
1956 int file_line_number = 0; |
|
1957 |
|
1958 nr = 0; |
|
1959 nc = 0; |
|
1960 |
2795
|
1961 while (is && ! error_state) |
2511
|
1962 { |
4171
|
1963 OCTAVE_QUIT; |
|
1964 |
3523
|
1965 std::string buf = get_mat_data_input_line (is); |
2511
|
1966 |
|
1967 file_line_number++; |
|
1968 |
4231
|
1969 size_t beg = buf.find_first_not_of (", \t"); |
2795
|
1970 |
3901
|
1971 // If we see a CR as the last character in the buffer, we had a |
|
1972 // CRLF pair as the line separator. Any other CR in the text |
|
1973 // will not be considered as whitespace. |
|
1974 |
|
1975 if (beg != NPOS && buf[beg] == '\r' && beg == buf.length () - 1) |
|
1976 { |
|
1977 // We had a blank line ending with a CRLF. Handle it the |
|
1978 // same as an empty line. |
|
1979 beg = NPOS; |
|
1980 } |
|
1981 |
2795
|
1982 int tmp_nc = 0; |
|
1983 |
|
1984 while (beg != NPOS) |
2511
|
1985 { |
2795
|
1986 tmp_nc++; |
|
1987 |
4231
|
1988 size_t end = buf.find_first_of (", \t", beg); |
2795
|
1989 |
|
1990 if (end != NPOS) |
3901
|
1991 { |
4231
|
1992 beg = buf.find_first_not_of (", \t", end); |
3901
|
1993 |
|
1994 if (buf[beg] == '\r' && beg == buf.length () - 1) |
|
1995 { |
|
1996 // We had a line with trailing spaces and |
|
1997 // ending with a CRLF, so this should look like EOL, |
|
1998 // not a new colum. |
|
1999 break; |
|
2000 } |
|
2001 } |
2795
|
2002 else |
|
2003 break; |
|
2004 } |
|
2005 |
|
2006 if (tmp_nc > 0) |
|
2007 { |
2511
|
2008 if (nc == 0) |
|
2009 { |
|
2010 nc = tmp_nc; |
|
2011 nr++; |
|
2012 } |
|
2013 else if (nc == tmp_nc) |
|
2014 nr++; |
|
2015 else |
|
2016 error ("load: %s: inconsistent number of columns near line %d", |
|
2017 filename.c_str (), file_line_number); |
|
2018 } |
|
2019 } |
|
2020 |
|
2021 if (nr == 0 || nc == 0) |
|
2022 error ("load: file `%s' seems to be empty!", filename.c_str ()); |
|
2023 |
|
2024 is.clear (); |
3538
|
2025 is.seekg (pos, std::ios::beg); |
2511
|
2026 } |
|
2027 |
|
2028 // Extract a matrix from a file of numbers only. |
|
2029 // |
|
2030 // Comments are not allowed. The file should only have numeric values. |
|
2031 // |
|
2032 // Reads the file twice. Once to find the number of rows and columns, |
|
2033 // and once to extract the matrix. |
|
2034 // |
|
2035 // FILENAME is used for error messages. |
|
2036 // |
|
2037 // This format provides no way to tag the data as global. |
|
2038 |
4171
|
2039 static std::string |
3523
|
2040 read_mat_ascii_data (std::istream& is, const std::string& filename, |
2511
|
2041 octave_value& tc) |
|
2042 { |
4171
|
2043 std::string retval; |
2511
|
2044 |
3523
|
2045 std::string varname; |
2511
|
2046 |
3202
|
2047 size_t pos = filename.rfind ('/'); |
2511
|
2048 |
|
2049 if (pos != NPOS) |
3202
|
2050 varname = filename.substr (pos+1); |
2511
|
2051 else |
|
2052 varname = filename; |
|
2053 |
3202
|
2054 pos = varname.find ('.'); |
|
2055 |
|
2056 if (pos != NPOS) |
|
2057 varname = varname.substr (0, pos); |
|
2058 |
4232
|
2059 size_t len = varname.length (); |
|
2060 for (size_t i = 0; i < len; i++) |
|
2061 { |
|
2062 char c = varname[i]; |
4247
|
2063 if (! (isalnum (c) || c == '_')) |
4232
|
2064 varname[i] = '_'; |
|
2065 } |
|
2066 |
4247
|
2067 if (! isalpha (varname[0])) |
4232
|
2068 varname.insert (0, "X"); |
|
2069 |
3263
|
2070 if (valid_identifier (varname)) |
2511
|
2071 { |
|
2072 int nr = 0; |
|
2073 int nc = 0; |
|
2074 |
4231
|
2075 int total_count = 0; |
|
2076 |
2511
|
2077 get_lines_and_columns (is, filename, nr, nc); |
|
2078 |
4171
|
2079 OCTAVE_QUIT; |
|
2080 |
2795
|
2081 if (! error_state && nr > 0 && nc > 0) |
2511
|
2082 { |
|
2083 Matrix tmp (nr, nc); |
|
2084 |
3216
|
2085 if (nr < 1 || nc < 1) |
3538
|
2086 is.clear (std::ios::badbit); |
3216
|
2087 else |
|
2088 { |
|
2089 double d; |
|
2090 for (int i = 0; i < nr; i++) |
|
2091 { |
3523
|
2092 std::string buf = get_mat_data_input_line (is); |
|
2093 |
4051
|
2094 #ifdef HAVE_SSTREAM |
|
2095 std::istringstream tmp_stream (buf); |
|
2096 #else |
3523
|
2097 std::istrstream tmp_stream (buf.c_str ()); |
4051
|
2098 #endif |
3216
|
2099 |
|
2100 for (int j = 0; j < nc; j++) |
|
2101 { |
4171
|
2102 OCTAVE_QUIT; |
|
2103 |
4130
|
2104 d = octave_read_double (tmp_stream); |
3323
|
2105 |
4231
|
2106 if (tmp_stream || tmp_stream.eof ()) |
|
2107 { |
|
2108 tmp.elem (i, j) = d; |
|
2109 total_count++; |
|
2110 |
|
2111 // Skip whitespace and commas. |
|
2112 char c; |
|
2113 while (1) |
|
2114 { |
|
2115 tmp_stream >> c; |
|
2116 |
|
2117 if (! tmp_stream) |
|
2118 break; |
|
2119 |
|
2120 if (! (c == ' ' || c == '\t' || c == ',')) |
|
2121 { |
|
2122 tmp_stream.putback (c); |
|
2123 break; |
|
2124 } |
|
2125 } |
|
2126 |
|
2127 if (tmp_stream.eof ()) |
|
2128 break; |
|
2129 } |
3216
|
2130 else |
3323
|
2131 { |
|
2132 error ("load: failed to read matrix from file `%s'", |
|
2133 filename.c_str ()); |
|
2134 |
4171
|
2135 return retval; |
3323
|
2136 } |
|
2137 |
3216
|
2138 } |
|
2139 } |
|
2140 } |
|
2141 |
4231
|
2142 if (is || is.eof ()) |
2511
|
2143 { |
4231
|
2144 // XXX FIXME XXX -- not sure this is best, but it works. |
|
2145 |
|
2146 if (is.eof ()) |
|
2147 is.clear (); |
|
2148 |
|
2149 int expected = nr * nc; |
|
2150 |
|
2151 if (expected == total_count) |
|
2152 { |
|
2153 tc = tmp; |
|
2154 retval = varname; |
|
2155 } |
|
2156 else |
|
2157 error ("load: expected %d elements, found %d", |
|
2158 expected, total_count); |
2511
|
2159 } |
|
2160 else |
|
2161 error ("load: failed to read matrix from file `%s'", |
|
2162 filename.c_str ()); |
|
2163 } |
|
2164 else |
|
2165 error ("load: unable to extract matrix size from file `%s'", |
|
2166 filename.c_str ()); |
|
2167 } |
|
2168 else |
|
2169 error ("load: unable to convert filename `%s' to valid identifier", |
|
2170 filename.c_str ()); |
|
2171 |
4171
|
2172 return retval; |
2511
|
2173 } |
|
2174 |
604
|
2175 // Read LEN elements of data from IS in the format specified by |
3019
|
2176 // PRECISION, placing the result in DATA. If SWAP is TRUE, swap |
604
|
2177 // the bytes of each element before copying to DATA. FLT_FMT |
|
2178 // specifies the format of the data if we are reading floating point |
|
2179 // numbers. |
|
2180 |
|
2181 static void |
3523
|
2182 read_mat_binary_data (std::istream& is, double *data, int precision, |
3019
|
2183 int len, bool swap, |
2318
|
2184 oct_mach_info::float_format flt_fmt) |
604
|
2185 { |
|
2186 switch (precision) |
|
2187 { |
|
2188 case 0: |
630
|
2189 read_doubles (is, data, LS_DOUBLE, len, swap, flt_fmt); |
604
|
2190 break; |
|
2191 |
|
2192 case 1: |
630
|
2193 read_doubles (is, data, LS_FLOAT, len, swap, flt_fmt); |
604
|
2194 break; |
|
2195 |
|
2196 case 2: |
|
2197 read_doubles (is, data, LS_INT, len, swap, flt_fmt); |
|
2198 break; |
|
2199 |
|
2200 case 3: |
|
2201 read_doubles (is, data, LS_SHORT, len, swap, flt_fmt); |
|
2202 break; |
|
2203 |
|
2204 case 4: |
|
2205 read_doubles (is, data, LS_U_SHORT, len, swap, flt_fmt); |
|
2206 break; |
|
2207 |
|
2208 case 5: |
|
2209 read_doubles (is, data, LS_U_CHAR, len, swap, flt_fmt); |
|
2210 break; |
|
2211 |
|
2212 default: |
|
2213 break; |
|
2214 } |
|
2215 } |
|
2216 |
|
2217 static int |
3523
|
2218 read_mat_file_header (std::istream& is, bool& swap, FOUR_BYTE_INT& mopt, |
604
|
2219 FOUR_BYTE_INT& nr, FOUR_BYTE_INT& nc, |
|
2220 FOUR_BYTE_INT& imag, FOUR_BYTE_INT& len, |
|
2221 int quiet = 0) |
|
2222 { |
3019
|
2223 swap = false; |
671
|
2224 |
1358
|
2225 // We expect to fail here, at the beginning of a record, so not |
|
2226 // being able to read another mopt value should not result in an |
|
2227 // error. |
911
|
2228 |
3557
|
2229 is.read (X_CAST (char *, &mopt), 4); |
604
|
2230 if (! is) |
911
|
2231 return 1; |
604
|
2232 |
3557
|
2233 if (! is.read (X_CAST (char *, &nr), 4)) |
604
|
2234 goto data_read_error; |
|
2235 |
3557
|
2236 if (! is.read (X_CAST (char *, &nc), 4)) |
604
|
2237 goto data_read_error; |
|
2238 |
3557
|
2239 if (! is.read (X_CAST (char *, &imag), 4)) |
604
|
2240 goto data_read_error; |
|
2241 |
3557
|
2242 if (! is.read (X_CAST (char *, &len), 4)) |
604
|
2243 goto data_read_error; |
|
2244 |
|
2245 // If mopt is nonzero and the byte order is swapped, mopt will be |
|
2246 // bigger than we expect, so we swap bytes. |
|
2247 // |
|
2248 // If mopt is zero, it means the file was written on a little endian |
|
2249 // machine, and we only need to swap if we are running on a big endian |
|
2250 // machine. |
|
2251 // |
|
2252 // Gag me. |
|
2253 |
2318
|
2254 if (oct_mach_info::words_big_endian () && mopt == 0) |
3019
|
2255 swap = true; |
604
|
2256 |
1358
|
2257 // mopt is signed, therefore byte swap may result in negative value. |
911
|
2258 |
|
2259 if (mopt > 9999 || mopt < 0) |
3019
|
2260 swap = true; |
604
|
2261 |
|
2262 if (swap) |
|
2263 { |
3145
|
2264 swap_4_bytes (X_CAST (char *, &mopt)); |
|
2265 swap_4_bytes (X_CAST (char *, &nr)); |
|
2266 swap_4_bytes (X_CAST (char *, &nc)); |
|
2267 swap_4_bytes (X_CAST (char *, &imag)); |
|
2268 swap_4_bytes (X_CAST (char *, &len)); |
604
|
2269 } |
|
2270 |
911
|
2271 if (mopt > 9999 || mopt < 0 || imag > 1 || imag < 0) |
604
|
2272 { |
|
2273 if (! quiet) |
|
2274 error ("load: can't read binary file"); |
|
2275 return -1; |
|
2276 } |
|
2277 |
|
2278 return 0; |
|
2279 |
|
2280 data_read_error: |
|
2281 return -1; |
|
2282 } |
|
2283 |
617
|
2284 // We don't just use a cast here, because we need to be able to detect |
|
2285 // possible errors. |
|
2286 |
2318
|
2287 static oct_mach_info::float_format |
|
2288 mopt_digit_to_float_format (int mach) |
617
|
2289 { |
4574
|
2290 oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown; |
619
|
2291 |
617
|
2292 switch (mach) |
|
2293 { |
|
2294 case 0: |
4574
|
2295 flt_fmt = oct_mach_info::flt_fmt_ieee_little_endian; |
617
|
2296 break; |
|
2297 |
|
2298 case 1: |
4574
|
2299 flt_fmt = oct_mach_info::flt_fmt_ieee_big_endian; |
617
|
2300 break; |
|
2301 |
|
2302 case 2: |
4574
|
2303 flt_fmt = oct_mach_info::flt_fmt_vax_d; |
617
|
2304 break; |
|
2305 |
|
2306 case 3: |
4574
|
2307 flt_fmt = oct_mach_info::flt_fmt_vax_g; |
617
|
2308 break; |
|
2309 |
|
2310 case 4: |
4574
|
2311 flt_fmt = oct_mach_info::flt_fmt_cray; |
617
|
2312 break; |
|
2313 |
|
2314 default: |
4574
|
2315 flt_fmt = oct_mach_info::flt_fmt_unknown; |
617
|
2316 break; |
|
2317 } |
619
|
2318 |
|
2319 return flt_fmt; |
617
|
2320 } |
619
|
2321 |
2318
|
2322 static int |
|
2323 float_format_to_mopt_digit (oct_mach_info::float_format flt_fmt) |
|
2324 { |
|
2325 int retval = -1; |
|
2326 |
|
2327 switch (flt_fmt) |
|
2328 { |
4574
|
2329 case oct_mach_info::flt_fmt_ieee_little_endian: |
2318
|
2330 retval = 0; |
|
2331 break; |
|
2332 |
4574
|
2333 case oct_mach_info::flt_fmt_ieee_big_endian: |
2318
|
2334 retval = 1; |
|
2335 break; |
|
2336 |
4574
|
2337 case oct_mach_info::flt_fmt_vax_d: |
2318
|
2338 retval = 2; |
|
2339 break; |
|
2340 |
4574
|
2341 case oct_mach_info::flt_fmt_vax_g: |
2318
|
2342 retval = 3; |
|
2343 break; |
|
2344 |
4574
|
2345 case oct_mach_info::flt_fmt_cray: |
2318
|
2346 retval = 4; |
|
2347 break; |
|
2348 |
|
2349 default: |
|
2350 break; |
|
2351 } |
|
2352 |
|
2353 return retval; |
|
2354 } |
|
2355 |
604
|
2356 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
2357 // place it in TC, returning the name of the variable. |
|
2358 // |
3688
|
2359 // The data is expected to be in Matlab version 4 .mat format, though |
|
2360 // not all the features of that format are supported. |
604
|
2361 // |
|
2362 // FILENAME is used for error messages. |
|
2363 // |
|
2364 // This format provides no way to tag the data as global. |
|
2365 |
4171
|
2366 static std::string |
3523
|
2367 read_mat_binary_data (std::istream& is, const std::string& filename, |
2086
|
2368 octave_value& tc) |
604
|
2369 { |
4171
|
2370 std::string retval; |
|
2371 |
1358
|
2372 // These are initialized here instead of closer to where they are |
|
2373 // first used to avoid errors from gcc about goto crossing |
|
2374 // initialization of variable. |
604
|
2375 |
|
2376 Matrix re; |
4574
|
2377 oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown; |
3019
|
2378 bool swap = false; |
|
2379 int type = 0; |
|
2380 int prec = 0; |
3136
|
2381 int order = 0; |
3019
|
2382 int mach = 0; |
|
2383 int dlen = 0; |
604
|
2384 |
|
2385 FOUR_BYTE_INT mopt, nr, nc, imag, len; |
|
2386 |
|
2387 int err = read_mat_file_header (is, swap, mopt, nr, nc, imag, len); |
|
2388 if (err) |
|
2389 { |
|
2390 if (err < 0) |
|
2391 goto data_read_error; |
|
2392 else |
4171
|
2393 return retval; |
604
|
2394 } |
|
2395 |
3136
|
2396 type = mopt % 10; // Full, sparse, etc. |
|
2397 mopt /= 10; // Eliminate first digit. |
|
2398 prec = mopt % 10; // double, float, int, etc. |
|
2399 mopt /= 10; // Eliminate second digit. |
|
2400 order = mopt % 10; // Row or column major ordering. |
|
2401 mopt /= 10; // Eliminate third digit. |
|
2402 mach = mopt % 10; // IEEE, VAX, etc. |
604
|
2403 |
2318
|
2404 flt_fmt = mopt_digit_to_float_format (mach); |
|
2405 |
4574
|
2406 if (flt_fmt == oct_mach_info::flt_fmt_unknown) |
604
|
2407 { |
|
2408 error ("load: unrecognized binary format!"); |
4171
|
2409 return retval; |
604
|
2410 } |
|
2411 |
|
2412 if (type != 0 && type != 1) |
|
2413 { |
|
2414 error ("load: can't read sparse matrices"); |
4171
|
2415 return retval; |
604
|
2416 } |
|
2417 |
|
2418 if (imag && type == 1) |
|
2419 { |
|
2420 error ("load: encountered complex matrix with string flag set!"); |
4171
|
2421 return retval; |
604
|
2422 } |
|
2423 |
2345
|
2424 // LEN includes the terminating character, and the file is also |
2436
|
2425 // supposed to include it, but apparently not all files do. Either |
|
2426 // way, I think this should work. |
2345
|
2427 |
4251
|
2428 { |
|
2429 OCTAVE_LOCAL_BUFFER (char, name, len+1); |
|
2430 name[len] = '\0'; |
|
2431 if (! is.read (X_CAST (char *, name), len)) |
|
2432 goto data_read_error; |
|
2433 retval = name; |
|
2434 |
|
2435 dlen = nr * nc; |
|
2436 if (dlen < 0) |
604
|
2437 goto data_read_error; |
4251
|
2438 |
|
2439 if (order) |
|
2440 { |
|
2441 int tmp = nr; |
|
2442 nr = nc; |
|
2443 nc = tmp; |
|
2444 } |
|
2445 |
|
2446 re.resize (nr, nc); |
|
2447 |
|
2448 read_mat_binary_data (is, re.fortran_vec (), prec, dlen, swap, flt_fmt); |
604
|
2449 |
|
2450 if (! is || error_state) |
|
2451 { |
4251
|
2452 error ("load: reading matrix data for `%s'", name); |
604
|
2453 goto data_read_error; |
|
2454 } |
|
2455 |
4251
|
2456 if (imag) |
|
2457 { |
|
2458 Matrix im (nr, nc); |
|
2459 |
|
2460 read_mat_binary_data (is, im.fortran_vec (), prec, dlen, swap, |
|
2461 flt_fmt); |
|
2462 |
|
2463 if (! is || error_state) |
|
2464 { |
|
2465 error ("load: reading imaginary matrix data for `%s'", name); |
|
2466 goto data_read_error; |
|
2467 } |
|
2468 |
|
2469 ComplexMatrix ctmp (nr, nc); |
|
2470 |
|
2471 for (int j = 0; j < nc; j++) |
|
2472 for (int i = 0; i < nr; i++) |
|
2473 ctmp (i, j) = Complex (re (i, j), im (i, j)); |
|
2474 |
|
2475 tc = order ? ctmp.transpose () : ctmp; |
|
2476 } |
|
2477 else |
|
2478 tc = order ? re.transpose () : re; |
|
2479 |
|
2480 if (type == 1) |
|
2481 tc = tc.convert_to_str (); |
|
2482 |
|
2483 return retval; |
604
|
2484 } |
|
2485 |
|
2486 data_read_error: |
1755
|
2487 error ("load: trouble reading binary file `%s'", filename.c_str ()); |
4171
|
2488 return retval; |
604
|
2489 } |
|
2490 |
3688
|
2491 // Read COUNT elements of data from IS in the format specified by TYPE, |
|
2492 // placing the result in DATA. If SWAP is TRUE, swap the bytes of |
|
2493 // each element before copying to DATA. FLT_FMT specifies the format |
|
2494 // of the data if we are reading floating point numbers. |
|
2495 |
|
2496 static void |
|
2497 read_mat5_binary_data (std::istream& is, double *data, |
|
2498 int count, bool swap, mat5_data_type type, |
|
2499 oct_mach_info::float_format flt_fmt) |
|
2500 { |
|
2501 |
|
2502 switch (type) |
|
2503 { |
|
2504 case miINT8: |
|
2505 read_doubles (is, data, LS_CHAR, count, swap, flt_fmt); |
|
2506 break; |
|
2507 |
|
2508 case miUINT8: |
|
2509 read_doubles (is, data, LS_U_CHAR, count, swap, flt_fmt); |
|
2510 break; |
|
2511 |
|
2512 case miINT16: |
|
2513 read_doubles (is, data, LS_SHORT, count, swap, flt_fmt); |
|
2514 break; |
|
2515 |
|
2516 case miUINT16: |
|
2517 read_doubles (is, data, LS_U_SHORT, count, swap, flt_fmt); |
|
2518 break; |
|
2519 |
|
2520 case miINT32: |
|
2521 read_doubles (is, data, LS_INT, count, swap, flt_fmt); |
|
2522 break; |
|
2523 |
|
2524 case miUINT32: |
|
2525 read_doubles (is, data, LS_U_INT, count, swap, flt_fmt); |
|
2526 break; |
|
2527 |
|
2528 case miSINGLE: |
|
2529 read_doubles (is, data, LS_FLOAT, count, swap, flt_fmt); |
|
2530 break; |
|
2531 |
|
2532 case miRESERVE1: |
|
2533 break; |
|
2534 |
|
2535 case miDOUBLE: |
|
2536 read_doubles (is, data, LS_DOUBLE, count, swap, flt_fmt); |
|
2537 break; |
|
2538 |
|
2539 case miRESERVE2: |
|
2540 case miRESERVE3: |
|
2541 break; |
|
2542 |
|
2543 case miINT64: |
|
2544 #ifdef EIGHT_BYTE_INT |
|
2545 read_doubles (is, data, LS_LONG, count, swap, flt_fmt); |
|
2546 #endif |
|
2547 break; |
|
2548 |
|
2549 case miUINT64: |
|
2550 #ifdef EIGHT_BYTE_INT |
|
2551 read_doubles (is, data, LS_U_LONG, count, swap, flt_fmt); |
|
2552 #endif |
|
2553 break; |
|
2554 |
|
2555 case miMATRIX: |
|
2556 default: |
|
2557 break; |
|
2558 } |
|
2559 } |
|
2560 |
|
2561 // Read one element tag from stream IS, |
|
2562 // place the type code in TYPE and the byte count in BYTES |
|
2563 // return nonzero on error |
|
2564 static int |
|
2565 read_mat5_tag (std::istream& is, bool swap, int& type, int& bytes) |
|
2566 { |
|
2567 unsigned int upper; |
|
2568 FOUR_BYTE_INT temp; |
|
2569 |
|
2570 if (! is.read (X_CAST (char *, &temp), 4 )) |
|
2571 goto data_read_error; |
|
2572 |
|
2573 if (swap) |
|
2574 swap_4_bytes ((char *)&temp); |
|
2575 |
|
2576 upper = (temp >> 16) & 0xffff; |
|
2577 type = temp & 0xffff; |
|
2578 |
|
2579 if (upper) |
|
2580 { |
|
2581 // "compressed" format |
|
2582 bytes = upper; |
|
2583 } |
|
2584 else |
|
2585 { |
|
2586 if (! is.read (X_CAST (char *, &temp), 4 )) |
|
2587 goto data_read_error; |
|
2588 if (swap) |
|
2589 swap_4_bytes ((char *)&temp); |
|
2590 bytes = temp; |
|
2591 } |
|
2592 |
|
2593 return 0; |
|
2594 |
|
2595 data_read_error: |
|
2596 return 1; |
|
2597 } |
|
2598 |
|
2599 // Extract one data element (scalar, matrix, string, etc.) from stream |
|
2600 // IS and place it in TC, returning the name of the variable. |
|
2601 // |
|
2602 // The data is expected to be in Matlab's "Version 5" .mat format, |
|
2603 // though not all the features of that format are supported. |
|
2604 // |
|
2605 // FILENAME is used for error messages. |
|
2606 |
4171
|
2607 static std::string |
3688
|
2608 read_mat5_binary_element (std::istream& is, const std::string& filename, |
|
2609 bool swap, bool& global, octave_value& tc) |
|
2610 { |
4171
|
2611 std::string retval; |
|
2612 |
3688
|
2613 // These are initialized here instead of closer to where they are |
|
2614 // first used to avoid errors from gcc about goto crossing |
|
2615 // initialization of variable. |
|
2616 |
|
2617 Matrix re; |
4574
|
2618 oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown; |
3688
|
2619 int type = 0; |
|
2620 bool imag; |
|
2621 bool logicalvar; |
|
2622 enum arrayclasstype arrayclass; |
|
2623 FOUR_BYTE_INT junk; |
|
2624 FOUR_BYTE_INT flags; |
|
2625 FOUR_BYTE_INT nr; |
|
2626 FOUR_BYTE_INT nc; |
|
2627 FOUR_BYTE_INT dimension_length; |
|
2628 int len; |
|
2629 int element_length; |
|
2630 std::streampos pos; |
|
2631 TWO_BYTE_INT number; |
|
2632 number = *(TWO_BYTE_INT *)"\x00\x01"; |
|
2633 |
4520
|
2634 global = false; |
|
2635 |
3688
|
2636 // MAT files always use IEEE floating point |
|
2637 if ((number == 1) ^ swap) |
4574
|
2638 flt_fmt = oct_mach_info::flt_fmt_ieee_big_endian; |
3688
|
2639 else |
4574
|
2640 flt_fmt = oct_mach_info::flt_fmt_ieee_little_endian; |
3688
|
2641 |
|
2642 // element type and length |
|
2643 if (read_mat5_tag (is, swap, type, element_length)) |
4171
|
2644 return retval; // EOF |
3688
|
2645 |
|
2646 if (type != miMATRIX) |
|
2647 { |
|
2648 error ("load: invalid element type"); |
|
2649 goto early_read_error; |
|
2650 } |
4520
|
2651 |
|
2652 if (element_length == 0) |
|
2653 { |
|
2654 tc = Matrix (); |
|
2655 return retval; |
|
2656 } |
|
2657 |
3688
|
2658 pos = is.tellg (); |
|
2659 |
|
2660 // array flags subelement |
|
2661 if (read_mat5_tag (is, swap, type, len) || type != miUINT32 || len != 8) |
|
2662 { |
|
2663 error ("load: invalid array flags subelement"); |
|
2664 goto early_read_error; |
|
2665 } |
|
2666 |
|
2667 read_int (is, swap, flags); |
|
2668 imag = (flags & 0x0800) != 0; // has an imaginary part? |
|
2669 global = (flags & 0x0400) != 0; // global variable? |
|
2670 logicalvar = (flags & 0x0200) != 0; // we don't use this yet |
|
2671 arrayclass = (arrayclasstype)(flags & 0xff); |
|
2672 read_int (is, swap, junk); // an "undefined" entry |
|
2673 |
|
2674 // dimensions array subelement |
|
2675 { |
|
2676 std::streampos pos; |
|
2677 |
|
2678 if (read_mat5_tag (is, swap, type, dimension_length) || type != miINT32) |
|
2679 { |
|
2680 error ("load: invalid dimensions array subelement"); |
|
2681 goto early_read_error; |
|
2682 } |
|
2683 |
|
2684 pos = is.tellg (); |
|
2685 read_int (is, swap, nr); |
|
2686 read_int (is, swap, nc); |
|
2687 re.resize (nr, nc); |
|
2688 |
|
2689 // delay checking for a multidimensional array until we have read |
|
2690 // the variable name |
3812
|
2691 is.seekg (pos + static_cast<std::streamoff> (dimension_length)); |
3688
|
2692 } |
|
2693 |
|
2694 // array name subelement |
|
2695 { |
|
2696 std::streampos pos; |
|
2697 |
|
2698 if (read_mat5_tag (is, swap, type, len) || type != miINT8) |
|
2699 { |
|
2700 error ("load: invalid array name subelement"); |
|
2701 goto early_read_error; |
|
2702 } |
|
2703 |
|
2704 pos = is.tellg (); |
4249
|
2705 OCTAVE_LOCAL_BUFFER (char, name, len+1); |
3688
|
2706 |
|
2707 if (len) // structure field subelements have |
|
2708 // zero-length array name subelements |
|
2709 { |
|
2710 if (! is.read (X_CAST (char *, name), len )) |
|
2711 goto data_read_error; |
|
2712 |
3812
|
2713 is.seekg (pos + static_cast<std::streamoff> (PAD (len))); |
3688
|
2714 } |
|
2715 |
|
2716 name[len] = '\0'; |
4171
|
2717 retval = name; |
4467
|
2718 } |
3688
|
2719 |
|
2720 if (dimension_length != 8) |
|
2721 { |
|
2722 error ("load: multidimension arrays are not implemented"); |
|
2723 goto skip_ahead; |
|
2724 } |
|
2725 |
|
2726 switch (arrayclass) |
|
2727 { |
|
2728 case mxCELL_CLASS: |
4332
|
2729 { |
|
2730 Cell cell_array (nr, nc); |
|
2731 |
|
2732 for (int j = 0; j < nc; j++) |
|
2733 { |
|
2734 for (int i = 0; i < nr; i++) |
|
2735 { |
|
2736 octave_value tc2; |
|
2737 |
|
2738 std::string nm |
|
2739 = read_mat5_binary_element (is, filename, swap, global, tc2); |
|
2740 |
|
2741 if (! is || error_state) |
|
2742 { |
|
2743 error ("load: reading cell data for `%s'", nm.c_str ()); |
|
2744 goto data_read_error; |
|
2745 } |
|
2746 |
|
2747 cell_array.elem (i, j) = tc2; |
|
2748 } |
|
2749 } |
|
2750 |
|
2751 tc = cell_array; |
|
2752 } |
|
2753 break; |
3688
|
2754 |
|
2755 case mxOBJECT_CLASS: |
|
2756 warning ("load: objects are not implemented"); |
|
2757 goto skip_ahead; |
|
2758 |
|
2759 case mxSPARSE_CLASS: |
|
2760 warning ("load: sparse arrays are not implemented"); |
|
2761 goto skip_ahead; |
|
2762 |
|
2763 case mxSTRUCT_CLASS: |
|
2764 { |
|
2765 Octave_map m; |
|
2766 FOUR_BYTE_INT type; |
|
2767 FOUR_BYTE_INT len; |
|
2768 FOUR_BYTE_INT field_name_length; |
|
2769 int i; |
|
2770 |
|
2771 // field name length subelement -- actually the maximum length |
|
2772 // of a field name. The Matlab docs promise this will always |
|
2773 // be 32. We read and use the actual value, on the theory |
|
2774 // that eventually someone will recognize that's a waste of |
|
2775 // space. |
|
2776 if (read_mat5_tag (is, swap, type, len) || type != miINT32) |
|
2777 { |
|
2778 error ("load: invalid field name subelement"); |
|
2779 goto data_read_error; |
|
2780 } |
|
2781 |
|
2782 if (! is.read (X_CAST (char *, &field_name_length), len )) |
|
2783 goto data_read_error; |
|
2784 |
|
2785 if (swap) |
|
2786 swap_4_bytes ((char *)&field_name_length); |
|
2787 |
|
2788 // field name subelement. The length of this subelement tells |
|
2789 // us how many fields there are. |
|
2790 if (read_mat5_tag (is, swap, type, len) || type != miINT8) |
|
2791 { |
|
2792 error ("load: invalid field name subelement"); |
|
2793 goto data_read_error; |
|
2794 } |
|
2795 |
4362
|
2796 int n_fields = len/field_name_length; |
|
2797 |
|
2798 len = PAD (len); |
|
2799 |
4249
|
2800 OCTAVE_LOCAL_BUFFER (char, elname, len); |
3688
|
2801 |
|
2802 if (! is.read (elname, len)) |
|
2803 goto data_read_error; |
|
2804 |
4362
|
2805 int n; |
|
2806 if (nr == 1) |
|
2807 n = nc; |
|
2808 else if (nc == 1) |
|
2809 n = nr; |
|
2810 else |
3688
|
2811 { |
4362
|
2812 error ("load: can only handle one-dimensional structure arrays"); |
|
2813 goto data_read_error; |
|
2814 } |
|
2815 |
4467
|
2816 Cell field_elts (n_fields, n); |
4362
|
2817 |
|
2818 // fields subelements |
4467
|
2819 for (int j = 0; j < n; j++) |
4362
|
2820 { |
4467
|
2821 for (i = 0; i < n_fields; i++) |
4362
|
2822 { |
|
2823 octave_value fieldtc; |
|
2824 read_mat5_binary_element (is, filename, swap, global, fieldtc); |
4467
|
2825 field_elts(i,j) = fieldtc; |
4362
|
2826 } |
4467
|
2827 } |
|
2828 |
|
2829 for (int i = n_fields-1; i >= 0; i--) |
|
2830 { |
|
2831 const char *key = elname + i*field_name_length; |
|
2832 |
|
2833 for (int j = n-1; j >=0; j--) |
|
2834 m[key](j) = field_elts(i,j); |
3688
|
2835 } |
|
2836 |
|
2837 tc = m; |
|
2838 } |
|
2839 break; |
|
2840 |
|
2841 case mxCHAR_CLASS: |
|
2842 // handle as a numerical array to start with |
|
2843 |
|
2844 case mxDOUBLE_CLASS: |
|
2845 case mxSINGLE_CLASS: |
|
2846 case mxINT8_CLASS: |
|
2847 case mxUINT8_CLASS: |
|
2848 case mxINT16_CLASS: |
|
2849 case mxUINT16_CLASS: |
|
2850 case mxINT32_CLASS: |
|
2851 case mxUINT32_CLASS: |
|
2852 default: |
|
2853 // handle all these numerical formats as double arrays |
|
2854 |
|
2855 // real data subelement |
|
2856 { |
|
2857 std::streampos pos; |
|
2858 |
|
2859 if (read_mat5_tag (is, swap, type, len)) |
|
2860 { |
4171
|
2861 error ("load: reading matrix data for `%s'", retval.c_str ()); |
3688
|
2862 goto data_read_error; |
|
2863 } |
|
2864 |
|
2865 pos = is.tellg (); |
|
2866 read_mat5_binary_data (is, re.fortran_vec (), nr*nc, swap, |
|
2867 (enum mat5_data_type) type, flt_fmt); |
|
2868 |
|
2869 if (! is || error_state) |
|
2870 { |
4171
|
2871 error ("load: reading matrix data for `%s'", retval.c_str ()); |
3688
|
2872 goto data_read_error; |
|
2873 } |
|
2874 |
3812
|
2875 is.seekg (pos + static_cast<std::streamoff> (PAD (len))); |
3688
|
2876 } |
|
2877 |
|
2878 // imaginary data subelement |
|
2879 if (imag) |
|
2880 { |
|
2881 Matrix im (nr, nc); |
|
2882 |
|
2883 if (read_mat5_tag (is, swap, type, len)) |
|
2884 { |
4171
|
2885 error ("load: reading matrix data for `%s'", retval.c_str ()); |
3688
|
2886 goto data_read_error; |
|
2887 } |
|
2888 |
|
2889 read_mat5_binary_data (is, im.fortran_vec (), nr*nc, swap, |
|
2890 (enum mat5_data_type) type, flt_fmt); |
|
2891 |
|
2892 if (! is || error_state) |
|
2893 { |
4171
|
2894 error ("load: reading imaginary matrix data for `%s'", |
|
2895 retval.c_str ()); |
3688
|
2896 goto data_read_error; |
|
2897 } |
|
2898 |
|
2899 ComplexMatrix ctmp (nr, nc); |
|
2900 |
|
2901 for (int j = 0; j < nc; j++) |
|
2902 for (int i = 0; i < nr; i++) |
|
2903 ctmp (i, j) = Complex (re (i, j), im (i, j)); |
|
2904 |
|
2905 tc = ctmp; |
|
2906 } |
|
2907 else |
|
2908 tc = re; |
|
2909 |
|
2910 if (arrayclass == mxCHAR_CLASS) |
|
2911 tc = tc.convert_to_str (); |
|
2912 } |
|
2913 |
3812
|
2914 is.seekg (pos + static_cast<std::streamoff> (element_length)); |
3688
|
2915 |
4362
|
2916 if (is.eof ()) |
|
2917 is.clear (); |
|
2918 |
4171
|
2919 return retval; |
3688
|
2920 |
|
2921 data_read_error: |
|
2922 early_read_error: |
|
2923 error ("load: trouble reading binary file `%s'", filename.c_str ()); |
4171
|
2924 return std::string (); |
3688
|
2925 |
|
2926 skip_ahead: |
4171
|
2927 warning ("skipping over `%s'", retval.c_str ()); |
3812
|
2928 is.seekg (pos + static_cast<std::streamoff> (element_length)); |
3688
|
2929 return read_mat5_binary_element (is, filename, swap, global, tc); |
|
2930 } |
|
2931 |
|
2932 static int |
|
2933 read_mat5_binary_file_header (std::istream& is, bool& swap, |
|
2934 bool quiet = false) |
|
2935 { |
|
2936 TWO_BYTE_INT version=0, magic=0; |
|
2937 |
|
2938 is.seekg (124, std::ios::beg); |
|
2939 is.read (X_CAST (char *, &version), 2); |
|
2940 is.read (X_CAST (char *, &magic), 2); |
|
2941 |
|
2942 if (magic == 0x4d49) |
|
2943 swap = 0; |
|
2944 else if (magic == 0x494d) |
|
2945 swap = 1; |
|
2946 else |
|
2947 { |
|
2948 if (! quiet) |
|
2949 error ("load: can't read binary file"); |
|
2950 return -1; |
|
2951 } |
|
2952 |
|
2953 if (! swap) // version number is inverse swapped! |
|
2954 version = ((version >> 8) & 0xff) + ((version & 0xff) << 8); |
|
2955 |
|
2956 if (version != 1 && !quiet) |
|
2957 warning ("load: found version %d binary MAT file, " |
|
2958 "but only prepared for version 1", version); |
|
2959 |
|
2960 return 0; |
|
2961 } |
|
2962 |
3019
|
2963 // Return TRUE if NAME matches one of the given globbing PATTERNS. |
604
|
2964 |
3013
|
2965 static bool |
3769
|
2966 matches_patterns (const string_vector& patterns, int pat_idx, |
3523
|
2967 int num_pat, const std::string& name) |
604
|
2968 { |
1755
|
2969 for (int i = pat_idx; i < num_pat; i++) |
604
|
2970 { |
1792
|
2971 glob_match pattern (patterns[i]); |
3013
|
2972 |
1792
|
2973 if (pattern.match (name)) |
3013
|
2974 return true; |
604
|
2975 } |
3688
|
2976 |
3013
|
2977 return false; |
604
|
2978 } |
|
2979 |
4329
|
2980 int |
3523
|
2981 read_binary_file_header (std::istream& is, bool& swap, |
4329
|
2982 oct_mach_info::float_format& flt_fmt, bool quiet) |
604
|
2983 { |
3552
|
2984 const int magic_len = 10; |
|
2985 char magic[magic_len+1]; |
3557
|
2986 is.read (X_CAST (char *, magic), magic_len); |
604
|
2987 magic[magic_len] = '\0'; |
3688
|
2988 |
604
|
2989 if (strncmp (magic, "Octave-1-L", magic_len) == 0) |
2318
|
2990 swap = oct_mach_info::words_big_endian (); |
604
|
2991 else if (strncmp (magic, "Octave-1-B", magic_len) == 0) |
2318
|
2992 swap = ! oct_mach_info::words_big_endian (); |
604
|
2993 else |
|
2994 { |
|
2995 if (! quiet) |
|
2996 error ("load: can't read binary file"); |
|
2997 return -1; |
|
2998 } |
|
2999 |
|
3000 char tmp = 0; |
3557
|
3001 is.read (X_CAST (char *, &tmp), 1); |
604
|
3002 |
2318
|
3003 flt_fmt = mopt_digit_to_float_format (tmp); |
|
3004 |
4574
|
3005 if (flt_fmt == oct_mach_info::flt_fmt_unknown) |
604
|
3006 { |
|
3007 if (! quiet) |
|
3008 error ("load: unrecognized binary format!"); |
3688
|
3009 |
604
|
3010 return -1; |
|
3011 } |
|
3012 |
|
3013 return 0; |
|
3014 } |
|
3015 |
|
3016 static load_save_format |
3523
|
3017 get_file_format (const std::string& fname, const std::string& orig_fname) |
604
|
3018 { |
|
3019 load_save_format retval = LS_UNKNOWN; |
|
3020 |
3687
|
3021 #ifdef HAVE_HDF5 |
3688
|
3022 // check this before we open the file |
3687
|
3023 if (H5Fis_hdf5 (fname.c_str ()) > 0) |
|
3024 return LS_HDF5; |
|
3025 #endif /* HAVE_HDF5 */ |
|
3026 |
3523
|
3027 std::ifstream file (fname.c_str ()); |
604
|
3028 |
|
3029 if (! file) |
|
3030 { |
1750
|
3031 error ("load: couldn't open input file `%s'", orig_fname.c_str ()); |
604
|
3032 return retval; |
|
3033 } |
|
3034 |
4574
|
3035 oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown; |
604
|
3036 |
3019
|
3037 bool swap = false; |
|
3038 |
|
3039 if (read_binary_file_header (file, swap, flt_fmt, true) == 0) |
604
|
3040 retval = LS_BINARY; |
|
3041 else |
|
3042 { |
3538
|
3043 file.seekg (0, std::ios::beg); |
604
|
3044 |
|
3045 FOUR_BYTE_INT mopt, nr, nc, imag, len; |
1180
|
3046 |
|
3047 int err = read_mat_file_header (file, swap, mopt, nr, nc, imag, len, 1); |
|
3048 |
|
3049 if (! err) |
604
|
3050 retval = LS_MAT_BINARY; |
|
3051 else |
|
3052 { |
2511
|
3053 file.clear (); |
3538
|
3054 file.seekg (0, std::ios::beg); |
604
|
3055 |
3697
|
3056 err = read_mat5_binary_file_header (file, swap, true); |
3688
|
3057 |
|
3058 if (! err) |
|
3059 { |
|
3060 file.clear (); |
|
3061 file.seekg (0, std::ios::beg); |
|
3062 retval = LS_MAT5_BINARY; |
|
3063 } |
|
3064 else |
|
3065 { |
|
3066 file.clear (); |
|
3067 file.seekg (0, std::ios::beg); |
|
3068 |
4171
|
3069 std::string tmp = extract_keyword (file, "name"); |
|
3070 |
|
3071 if (! tmp.empty ()) |
|
3072 retval = LS_ASCII; |
3688
|
3073 else |
|
3074 { |
|
3075 // Try reading the file as numbers only, determining the |
|
3076 // number of rows and columns from the data. We don't |
|
3077 // even bother to check to see if the first item in the |
|
3078 // file is a number, so that get_complete_line() can |
|
3079 // skip any comments that might appear at the top of the |
|
3080 // file. |
|
3081 |
|
3082 retval = LS_MAT_ASCII; |
|
3083 } |
2511
|
3084 } |
604
|
3085 } |
|
3086 } |
|
3087 |
|
3088 file.close (); |
|
3089 |
|
3090 if (retval == LS_UNKNOWN) |
1750
|
3091 error ("load: unable to determine file format for `%s'", |
|
3092 orig_fname.c_str ()); |
604
|
3093 |
|
3094 return retval; |
|
3095 } |
|
3096 |
4329
|
3097 octave_value |
3523
|
3098 do_load (std::istream& stream, const std::string& orig_fname, bool force, |
2318
|
3099 load_save_format format, oct_mach_info::float_format flt_fmt, |
3687
|
3100 bool list_only, bool swap, bool verbose, bool import, |
|
3101 const string_vector& argv, int argv_idx, int argc, int nargout) |
604
|
3102 { |
3727
|
3103 octave_value retval; |
|
3104 |
|
3105 Octave_map retstruct; |
604
|
3106 |
4051
|
3107 OSSTREAM output_buf; |
|
3108 |
604
|
3109 int count = 0; |
4051
|
3110 |
604
|
3111 for (;;) |
|
3112 { |
3019
|
3113 bool global = false; |
2086
|
3114 octave_value tc; |
604
|
3115 |
4171
|
3116 std::string name; |
|
3117 std::string doc; |
604
|
3118 |
|
3119 switch (format) |
|
3120 { |
|
3121 case LS_ASCII: |
3136
|
3122 name = read_ascii_data (stream, orig_fname, global, tc, count); |
604
|
3123 break; |
|
3124 |
|
3125 case LS_BINARY: |
|
3126 name = read_binary_data (stream, swap, flt_fmt, orig_fname, |
|
3127 global, tc, doc); |
|
3128 break; |
|
3129 |
2511
|
3130 case LS_MAT_ASCII: |
|
3131 name = read_mat_ascii_data (stream, orig_fname, tc); |
|
3132 break; |
|
3133 |
604
|
3134 case LS_MAT_BINARY: |
|
3135 name = read_mat_binary_data (stream, orig_fname, tc); |
|
3136 break; |
|
3137 |
3687
|
3138 #ifdef HAVE_HDF5 |
|
3139 case LS_HDF5: |
|
3140 name = read_hdf5_data (stream, orig_fname, |
|
3141 global, tc, doc, import); |
|
3142 break; |
|
3143 #endif /* HAVE_HDF5 */ |
|
3144 |
3688
|
3145 case LS_MAT5_BINARY: |
|
3146 name = read_mat5_binary_element (stream, orig_fname, swap, |
|
3147 global, tc); |
|
3148 break; |
|
3149 |
604
|
3150 default: |
775
|
3151 gripe_unrecognized_data_fmt ("load"); |
604
|
3152 break; |
|
3153 } |
|
3154 |
4171
|
3155 if (error_state || stream.eof () || name.empty ()) |
|
3156 break; |
|
3157 else if (! error_state && ! name.empty ()) |
604
|
3158 { |
|
3159 if (tc.is_defined ()) |
|
3160 { |
3136
|
3161 if (format == LS_MAT_ASCII && argv_idx < argc) |
|
3162 warning ("load: loaded ASCII file `%s' -- ignoring extra args", |
3687
|
3163 orig_fname.c_str ()); |
3136
|
3164 |
|
3165 if (format == LS_MAT_ASCII |
|
3166 || argv_idx == argc |
1755
|
3167 || matches_patterns (argv, argv_idx, argc, name)) |
604
|
3168 { |
|
3169 count++; |
621
|
3170 if (list_only) |
|
3171 { |
|
3172 if (verbose) |
|
3173 { |
|
3174 if (count == 1) |
|
3175 output_buf |
|
3176 << "type rows cols name\n" |
|
3177 << "==== ==== ==== ====\n"; |
|
3178 |
3013
|
3179 output_buf |
3548
|
3180 << std::setiosflags (std::ios::left) |
|
3181 << std::setw (16) << tc.type_name () . c_str () |
|
3182 << std::setiosflags (std::ios::right) |
|
3183 << std::setw (7) << tc.rows () |
|
3184 << std::setw (7) << tc.columns () |
3013
|
3185 << " "; |
621
|
3186 } |
|
3187 output_buf << name << "\n"; |
|
3188 } |
|
3189 else |
|
3190 { |
3727
|
3191 if (nargout == 1) |
|
3192 { |
|
3193 if (format == LS_MAT_ASCII) |
|
3194 retval = tc; |
|
3195 else |
|
3196 retstruct[name] = tc; |
|
3197 } |
|
3198 else |
|
3199 install_loaded_variable (force, name, tc, global, doc); |
621
|
3200 } |
604
|
3201 } |
2511
|
3202 |
|
3203 // Only attempt to read one item from a headless text file. |
|
3204 |
|
3205 if (format == LS_MAT_ASCII) |
|
3206 break; |
604
|
3207 } |
|
3208 else |
4171
|
3209 error ("load: unable to load variable `%s'", name.c_str ()); |
604
|
3210 } |
|
3211 else |
|
3212 { |
|
3213 if (count == 0) |
|
3214 error ("load: are you sure `%s' is an Octave data file?", |
1755
|
3215 orig_fname.c_str ()); |
604
|
3216 |
|
3217 break; |
|
3218 } |
|
3219 } |
|
3220 |
621
|
3221 if (list_only && count) |
|
3222 { |
4051
|
3223 output_buf << OSSTREAM_ENDS; |
|
3224 std::string msg = OSSTREAM_STR (output_buf); |
|
3225 OSSTREAM_FREEZE (output_buf); |
2095
|
3226 |
621
|
3227 if (nargout > 0) |
2095
|
3228 retval = msg; |
621
|
3229 else |
2095
|
3230 octave_stdout << msg; |
621
|
3231 } |
3727
|
3232 else if (! retstruct.empty ()) |
|
3233 retval = retstruct; |
621
|
3234 |
863
|
3235 return retval; |
|
3236 } |
|
3237 |
3687
|
3238 // HDF5 load/save documentation is included in the Octave manual |
|
3239 // regardless, but if HDF5 is not linked in we also include a |
|
3240 // sentence noting this, so the user understands that the features |
|
3241 // aren't available. Define a macro for this sentence: |
|
3242 |
|
3243 #ifdef HAVE_HDF5 |
|
3244 #define HAVE_HDF5_HELP_STRING "" |
|
3245 #else /* ! HAVE_HDF5 */ |
|
3246 #define HAVE_HDF5_HELP_STRING "\n\ |
|
3247 HDF5 load and save are not available, as this Octave executable was\n\ |
|
3248 not linked with the HDF5 library." |
|
3249 #endif /* ! HAVE HDF5 */ |
|
3250 |
4208
|
3251 DEFCMD (load, args, nargout, |
3372
|
3252 "-*- texinfo -*-\n\ |
|
3253 @deffn {Command} load options file v1 v2 @dots{}\n\ |
|
3254 Load the named variables from the file @var{file}. As with @code{save},\n\ |
|
3255 you may specify a list of variables and @code{load} will only extract\n\ |
|
3256 those variables with names that match. For example, to restore the\n\ |
|
3257 variables saved in the file @file{data}, use the command\n\ |
|
3258 \n\ |
|
3259 @example\n\ |
|
3260 load data\n\ |
|
3261 @end example\n\ |
863
|
3262 \n\ |
3372
|
3263 Octave will refuse to overwrite existing variables unless you use the\n\ |
|
3264 option @samp{-force}.\n\ |
|
3265 \n\ |
|
3266 If a variable that is not marked as global is loaded from a file when a\n\ |
|
3267 global symbol with the same name already exists, it is loaded in the\n\ |
|
3268 global symbol table. Also, if a variable is marked as global in a file\n\ |
|
3269 and a local symbol exists, the local symbol is moved to the global\n\ |
|
3270 symbol table and given the value from the file. Since it seems that\n\ |
|
3271 both of these cases are likely to be the result of some sort of error,\n\ |
|
3272 they will generate warnings.\n\ |
863
|
3273 \n\ |
3727
|
3274 If invoked with a single output argument, Octave returns data instead\n\ |
|
3275 of inserting variables in the symbol table. If the data file contains\n\ |
|
3276 only numbers (TAB- or space-delimited columns), a matrix of values is\n\ |
|
3277 returned. Otherwise, @code{load} returns a structure with members\n\ |
|
3278 corresponding to the names of the variables in the file.\n\ |
|
3279 \n\ |
3372
|
3280 The @code{load} command can read data stored in Octave's text and\n\ |
|
3281 binary formats, and @sc{Matlab}'s binary format. It will automatically\n\ |
|
3282 detect the type of file and do conversion from different floating point\n\ |
|
3283 formats (currently only IEEE big and little endian, though other formats\n\ |
|
3284 may added in the future).\n\ |
|
3285 \n\ |
|
3286 Valid options for @code{load} are listed in the following table.\n\ |
863
|
3287 \n\ |
3372
|
3288 @table @code\n\ |
|
3289 @item -force\n\ |
|
3290 Force variables currently in memory to be overwritten by variables with\n\ |
|
3291 the same name found in the file.\n\ |
|
3292 \n\ |
|
3293 @item -ascii\n\ |
|
3294 Force Octave to assume the file is in Octave's text format.\n\ |
|
3295 \n\ |
|
3296 @item -binary\n\ |
|
3297 Force Octave to assume the file is in Octave's binary format.\n\ |
|
3298 \n\ |
|
3299 @item -mat-binary\n\ |
|
3300 Force Octave to assume the file is in @sc{Matlab}'s binary format.\n\ |
3687
|
3301 \n\ |
3688
|
3302 @item -mat4-binary\n\ |
|
3303 Force Octave to assume the file is in the binary format written by\n\ |
|
3304 @sc{Matlab} version 4.\n\ |
|
3305 \n\ |
3687
|
3306 @item -hdf5\n\ |
|
3307 Force Octave to assume the file is in HDF5 format.\n\ |
|
3308 (HDF5 is a free, portable binary format developed by the National\n\ |
|
3309 Center for Supercomputing Applications at the University of Illinois.)\n\ |
|
3310 Note that Octave can read HDF5 files not created by itself, but may\n\ |
|
3311 skip some datasets in formats that it cannot support. In particular,\n\ |
|
3312 it will skip datasets of data types that it does not recognize, with\n\ |
|
3313 dimensionality > 2, or with names that aren't valid Octave identifiers\n\ |
|
3314 See, however, the @samp{-import} option to ameliorate this somewhat.\n" |
|
3315 |
|
3316 HAVE_HDF5_HELP_STRING |
|
3317 |
|
3318 "\n\ |
|
3319 @item -import\n\ |
|
3320 Make a stronger attempt to import foreign datasets. Currently, this means\n\ |
|
3321 that for HDF5 files, invalid characters in names are converted to @samp{_},\n\ |
|
3322 and datasets with dimensionality > 2 are imported as lists of matrices (or\n\ |
|
3323 lists of lists of matrices, or ...).\n\ |
|
3324 \n\ |
3372
|
3325 @end table\n\ |
|
3326 @end deffn") |
863
|
3327 { |
2086
|
3328 octave_value_list retval; |
863
|
3329 |
1755
|
3330 int argc = args.length () + 1; |
|
3331 |
1968
|
3332 string_vector argv = args.make_argv ("load"); |
1755
|
3333 |
|
3334 if (error_state) |
|
3335 return retval; |
863
|
3336 |
1358
|
3337 // It isn't necessary to have the default load format stored in a |
|
3338 // user preference variable since we can determine the type of file |
|
3339 // as we are reading. |
863
|
3340 |
|
3341 load_save_format format = LS_UNKNOWN; |
|
3342 |
3019
|
3343 bool force = false; |
|
3344 bool list_only = false; |
|
3345 bool verbose = false; |
3687
|
3346 bool import = false; |
863
|
3347 |
1755
|
3348 int i; |
|
3349 for (i = 1; i < argc; i++) |
863
|
3350 { |
1755
|
3351 if (argv[i] == "-force" || argv[i] == "-f") |
863
|
3352 { |
3019
|
3353 force = true; |
863
|
3354 } |
1755
|
3355 else if (argv[i] == "-list" || argv[i] == "-l") |
863
|
3356 { |
3019
|
3357 list_only = true; |
863
|
3358 } |
1755
|
3359 else if (argv[i] == "-verbose" || argv[i] == "-v") |
863
|
3360 { |
3019
|
3361 verbose = true; |
863
|
3362 } |
1755
|
3363 else if (argv[i] == "-ascii" || argv[i] == "-a") |
863
|
3364 { |
|
3365 format = LS_ASCII; |
|
3366 } |
1755
|
3367 else if (argv[i] == "-binary" || argv[i] == "-b") |
863
|
3368 { |
|
3369 format = LS_BINARY; |
|
3370 } |
1755
|
3371 else if (argv[i] == "-mat-binary" || argv[i] == "-m") |
863
|
3372 { |
3688
|
3373 format = LS_MAT5_BINARY; |
|
3374 } |
3797
|
3375 else if (argv[i] == "-mat4-binary" || argv[i] == "-4" || argv[i] == "-v4") |
3688
|
3376 { |
863
|
3377 format = LS_MAT_BINARY; |
|
3378 } |
3687
|
3379 else if (argv[i] == "-hdf5" || argv[i] == "-h") |
|
3380 { |
|
3381 #ifdef HAVE_HDF5 |
|
3382 format = LS_HDF5; |
|
3383 #else /* ! HAVE_HDF5 */ |
|
3384 error ("load: octave executable was not linked with HDF5 library"); |
|
3385 return retval; |
|
3386 #endif /* ! HAVE_HDF5 */ |
|
3387 } |
|
3388 else if (argv[i] == "-import" || argv[i] == "-i") |
|
3389 { |
|
3390 import = true; |
|
3391 } |
863
|
3392 else |
|
3393 break; |
|
3394 } |
|
3395 |
1755
|
3396 if (i == argc) |
863
|
3397 { |
2057
|
3398 print_usage ("load"); |
863
|
3399 return retval; |
|
3400 } |
|
3401 |
3523
|
3402 std::string orig_fname = argv[i]; |
863
|
3403 |
4574
|
3404 oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown; |
863
|
3405 |
3019
|
3406 bool swap = false; |
863
|
3407 |
1755
|
3408 if (argv[i] == "-") |
863
|
3409 { |
1755
|
3410 i++; |
863
|
3411 |
3687
|
3412 #ifdef HAVE_HDF5 |
|
3413 if (format == LS_HDF5) |
|
3414 error ("load: cannot read HDF5 format from stdin"); |
|
3415 else |
|
3416 #endif /* HAVE_HDF5 */ |
863
|
3417 if (format != LS_UNKNOWN) |
|
3418 { |
1358
|
3419 // XXX FIXME XXX -- if we have already seen EOF on a |
3531
|
3420 // previous call, how do we fix up the state of std::cin so |
|
3421 // that we can get additional input? I'm afraid that we |
|
3422 // can't fix this using std::cin only. |
|
3423 |
|
3424 retval = do_load (std::cin, orig_fname, force, format, flt_fmt, |
3687
|
3425 list_only, swap, verbose, import, argv, i, argc, |
863
|
3426 nargout); |
|
3427 } |
|
3428 else |
|
3429 error ("load: must specify file format if reading from stdin"); |
|
3430 } |
|
3431 else |
|
3432 { |
3523
|
3433 std::string fname = file_ops::tilde_expand (argv[i]); |
863
|
3434 |
|
3435 if (format == LS_UNKNOWN) |
|
3436 format = get_file_format (fname, orig_fname); |
|
3437 |
3687
|
3438 #ifdef HAVE_HDF5 |
|
3439 if (format == LS_HDF5) |
|
3440 { |
|
3441 i++; |
|
3442 |
|
3443 hdf5_ifstream hdf5_file (fname.c_str ()); |
|
3444 |
|
3445 if (hdf5_file.file_id >= 0) |
|
3446 { |
|
3447 retval = do_load (hdf5_file, orig_fname, force, format, |
|
3448 flt_fmt, list_only, swap, verbose, |
|
3449 import, argv, i, argc, nargout); |
|
3450 |
|
3451 hdf5_file.close (); |
|
3452 } |
|
3453 else |
|
3454 error ("load: couldn't open input file `%s'", |
|
3455 orig_fname.c_str ()); |
|
3456 } |
|
3457 else |
|
3458 #endif /* HAVE_HDF5 */ |
|
3459 // don't insert any statements here; the "else" above has to |
|
3460 // go with the "if" below!!!!! |
863
|
3461 if (format != LS_UNKNOWN) |
|
3462 { |
1755
|
3463 i++; |
863
|
3464 |
3775
|
3465 std::ios::openmode mode = std::ios::in; |
3688
|
3466 if (format == LS_BINARY || |
|
3467 format == LS_MAT_BINARY || |
|
3468 format == LS_MAT5_BINARY) |
3552
|
3469 mode |= std::ios::binary; |
863
|
3470 |
3523
|
3471 std::ifstream file (fname.c_str (), mode); |
863
|
3472 |
|
3473 if (file) |
|
3474 { |
|
3475 if (format == LS_BINARY) |
|
3476 { |
|
3477 if (read_binary_file_header (file, swap, flt_fmt) < 0) |
|
3478 { |
|
3479 file.close (); |
|
3480 return retval; |
|
3481 } |
|
3482 } |
3688
|
3483 else if (format == LS_MAT5_BINARY) |
|
3484 { |
|
3485 if (read_mat5_binary_file_header (file, swap, false) < 0) |
|
3486 { |
|
3487 file.close (); |
|
3488 return retval; |
|
3489 } |
|
3490 } |
863
|
3491 |
|
3492 retval = do_load (file, orig_fname, force, format, |
3687
|
3493 flt_fmt, list_only, swap, verbose, import, |
1755
|
3494 argv, i, argc, nargout); |
863
|
3495 file.close (); |
|
3496 } |
|
3497 else |
1755
|
3498 error ("load: couldn't open input file `%s'", |
|
3499 orig_fname.c_str ()); |
863
|
3500 } |
|
3501 } |
604
|
3502 |
|
3503 return retval; |
|
3504 } |
|
3505 |
3019
|
3506 // Return TRUE if PATTERN has any special globbing chars in it. |
|
3507 |
|
3508 static bool |
3523
|
3509 glob_pattern_p (const std::string& pattern) |
604
|
3510 { |
|
3511 int open = 0; |
|
3512 |
1755
|
3513 int len = pattern.length (); |
|
3514 |
|
3515 for (int i = 0; i < len; i++) |
604
|
3516 { |
1755
|
3517 char c = pattern[i]; |
|
3518 |
604
|
3519 switch (c) |
|
3520 { |
|
3521 case '?': |
|
3522 case '*': |
3019
|
3523 return true; |
604
|
3524 |
|
3525 case '[': // Only accept an open brace if there is a close |
|
3526 open++; // brace to match it. Bracket expressions must be |
|
3527 continue; // complete, according to Posix.2 |
|
3528 |
|
3529 case ']': |
|
3530 if (open) |
3019
|
3531 return true; |
604
|
3532 continue; |
4402
|
3533 |
604
|
3534 case '\\': |
1755
|
3535 if (i == len - 1) |
3019
|
3536 return false; |
604
|
3537 |
|
3538 default: |
|
3539 continue; |
|
3540 } |
|
3541 } |
|
3542 |
3019
|
3543 return false; |
604
|
3544 } |
|
3545 |
618
|
3546 // MAX_VAL and MIN_VAL are assumed to have integral values even though |
|
3547 // they are stored in doubles. |
|
3548 |
604
|
3549 static save_type |
|
3550 get_save_type (double max_val, double min_val) |
|
3551 { |
|
3552 save_type st = LS_DOUBLE; |
|
3553 |
4402
|
3554 // Matlab doesn't seem to load the UINT32 type correctly, so let's |
|
3555 // avoid it (and the other unsigned types, even though they may not |
4427
|
3556 // have the same problem. And apparently, there are problems with |
|
3557 // other smaller types as well. If we avoid them all, then maybe we |
|
3558 // will avoid problems. Unfortunately, we won't be able to save |
|
3559 // space... |
4402
|
3560 |
|
3561 // if (max_val < 256 && min_val > -1) |
|
3562 // st = LS_U_CHAR; |
|
3563 // else if (max_val < 65536 && min_val > -1) |
|
3564 // st = LS_U_SHORT; |
|
3565 // else if (max_val < 4294967295UL && min_val > -1) |
|
3566 // st = LS_U_INT; |
4427
|
3567 // else if (max_val < 128 && min_val >= -128) |
|
3568 // st = LS_CHAR; |
|
3569 // else if (max_val < 32768 && min_val >= -32768) |
|
3570 // st = LS_SHORT; |
|
3571 // else if (max_val <= 2147483647L && min_val >= -2147483647L) |
|
3572 // st = LS_INT; |
604
|
3573 |
|
3574 return st; |
|
3575 } |
|
3576 |
|
3577 // Save the data from TC along with the corresponding NAME, help |
|
3578 // string DOC, and global flag MARK_AS_GLOBAL on stream OS in the |
1427
|
3579 // binary format described above for read_binary_data. |
604
|
3580 |
2799
|
3581 static bool |
3523
|
3582 save_binary_data (std::ostream& os, const octave_value& tc, |
|
3583 const std::string& name, const std::string& doc, |
3019
|
3584 bool mark_as_global, bool save_as_floats) |
604
|
3585 { |
1755
|
3586 FOUR_BYTE_INT name_len = name.length (); |
604
|
3587 |
3557
|
3588 os.write (X_CAST (char *, &name_len), 4); |
1755
|
3589 os << name; |
|
3590 |
|
3591 FOUR_BYTE_INT doc_len = doc.length (); |
604
|
3592 |
3557
|
3593 os.write (X_CAST (char *, &doc_len), 4); |
1755
|
3594 os << doc; |
604
|
3595 |
|
3596 char tmp; |
|
3597 |
|
3598 tmp = mark_as_global; |
3557
|
3599 os.write (X_CAST (char *, &tmp), 1); |
604
|
3600 |
3233
|
3601 if (tc.is_string ()) |
|
3602 { |
|
3603 tmp = 7; |
3557
|
3604 os.write (X_CAST (char *, &tmp), 1); |
3233
|
3605 FOUR_BYTE_INT nr = tc.rows (); |
3557
|
3606 os.write (X_CAST (char *, &nr), 4); |
3233
|
3607 charMatrix chm = tc.char_matrix_value (); |
|
3608 for (int i = 0; i < nr; i++) |
|
3609 { |
|
3610 FOUR_BYTE_INT len = chm.cols (); |
3557
|
3611 os.write (X_CAST (char *, &len), 4); |
3523
|
3612 std::string tstr = chm.row_as_string (i); |
3233
|
3613 const char *tmp = tstr.data (); |
3557
|
3614 os.write (X_CAST (char *, tmp), len); |
3233
|
3615 } |
|
3616 } |
|
3617 else if (tc.is_range ()) |
|
3618 { |
|
3619 tmp = 6; |
3557
|
3620 os.write (X_CAST (char *, &tmp), 1); |
3233
|
3621 tmp = (char) LS_DOUBLE; |
3557
|
3622 os.write (X_CAST (char *, &tmp), 1); |
3233
|
3623 Range r = tc.range_value (); |
|
3624 double bas = r.base (); |
|
3625 double lim = r.limit (); |
|
3626 double inc = r.inc (); |
3557
|
3627 os.write (X_CAST (char *, &bas), 8); |
|
3628 os.write (X_CAST (char *, &lim), 8); |
|
3629 os.write (X_CAST (char *, &inc), 8); |
3233
|
3630 } |
|
3631 else if (tc.is_real_scalar ()) |
604
|
3632 { |
|
3633 tmp = 1; |
3557
|
3634 os.write (X_CAST (char *, &tmp), 1); |
630
|
3635 tmp = (char) LS_DOUBLE; |
3557
|
3636 os.write (X_CAST (char *, &tmp), 1); |
604
|
3637 double tmp = tc.double_value (); |
3557
|
3638 os.write (X_CAST (char *, &tmp), 8); |
604
|
3639 } |
620
|
3640 else if (tc.is_real_matrix ()) |
604
|
3641 { |
|
3642 tmp = 2; |
3557
|
3643 os.write (X_CAST (char *, &tmp), 1); |
604
|
3644 Matrix m = tc.matrix_value (); |
|
3645 FOUR_BYTE_INT nr = m.rows (); |
|
3646 FOUR_BYTE_INT nc = m.columns (); |
3557
|
3647 os.write (X_CAST (char *, &nr), 4); |
|
3648 os.write (X_CAST (char *, &nc), 4); |
604
|
3649 int len = nr * nc; |
|
3650 save_type st = LS_DOUBLE; |
630
|
3651 if (save_as_floats) |
|
3652 { |
1963
|
3653 if (m.too_large_for_float ()) |
630
|
3654 { |
|
3655 warning ("save: some values too large to save as floats --"); |
|
3656 warning ("save: saving as doubles instead"); |
|
3657 } |
|
3658 else |
|
3659 st = LS_FLOAT; |
|
3660 } |
|
3661 else if (len > 8192) // XXX FIXME XXX -- make this configurable. |
604
|
3662 { |
|
3663 double max_val, min_val; |
1963
|
3664 if (m.all_integers (max_val, min_val)) |
604
|
3665 st = get_save_type (max_val, min_val); |
|
3666 } |
630
|
3667 const double *mtmp = m.data (); |
604
|
3668 write_doubles (os, mtmp, st, len); |
|
3669 } |
|
3670 else if (tc.is_complex_scalar ()) |
|
3671 { |
|
3672 tmp = 3; |
3557
|
3673 os.write (X_CAST (char *, &tmp), 1); |
630
|
3674 tmp = (char) LS_DOUBLE; |
3557
|
3675 os.write (X_CAST (char *, &tmp), 1); |
604
|
3676 Complex tmp = tc.complex_value (); |
3557
|
3677 os.write (X_CAST (char *, &tmp), 16); |
604
|
3678 } |
|
3679 else if (tc.is_complex_matrix ()) |
|
3680 { |
|
3681 tmp = 4; |
3557
|
3682 os.write (X_CAST (char *, &tmp), 1); |
604
|
3683 ComplexMatrix m = tc.complex_matrix_value (); |
|
3684 FOUR_BYTE_INT nr = m.rows (); |
|
3685 FOUR_BYTE_INT nc = m.columns (); |
3557
|
3686 os.write (X_CAST (char *, &nr), 4); |
|
3687 os.write (X_CAST (char *, &nc), 4); |
604
|
3688 int len = nr * nc; |
|
3689 save_type st = LS_DOUBLE; |
630
|
3690 if (save_as_floats) |
|
3691 { |
1963
|
3692 if (m.too_large_for_float ()) |
630
|
3693 { |
|
3694 warning ("save: some values too large to save as floats --"); |
|
3695 warning ("save: saving as doubles instead"); |
|
3696 } |
|
3697 else |
|
3698 st = LS_FLOAT; |
|
3699 } |
|
3700 else if (len > 4096) // XXX FIXME XXX -- make this configurable. |
604
|
3701 { |
|
3702 double max_val, min_val; |
1963
|
3703 if (m.all_integers (max_val, min_val)) |
604
|
3704 st = get_save_type (max_val, min_val); |
|
3705 } |
630
|
3706 const Complex *mtmp = m.data (); |
3145
|
3707 write_doubles (os, X_CAST (const double *, mtmp), st, 2*len); |
604
|
3708 } |
|
3709 else |
2799
|
3710 gripe_wrong_type_arg ("save", tc, false); |
|
3711 |
|
3712 return os; |
604
|
3713 } |
|
3714 |
3687
|
3715 #ifdef HAVE_HDF5 |
|
3716 |
|
3717 // Add an attribute named attr_name to loc_id (a simple scalar |
|
3718 // attribute with value 1). Return value is >= 0 on success. |
|
3719 static herr_t |
|
3720 hdf5_add_attr (hid_t loc_id, const char *attr_name) |
|
3721 { |
|
3722 herr_t retval = 0; |
|
3723 |
|
3724 hid_t as_id = H5Screate (H5S_SCALAR); |
|
3725 |
|
3726 if (as_id >= 0) |
|
3727 { |
|
3728 hid_t a_id = H5Acreate (loc_id, attr_name, |
|
3729 H5T_NATIVE_UCHAR, as_id, H5P_DEFAULT); |
|
3730 |
|
3731 if (a_id >= 0) |
|
3732 { |
|
3733 unsigned char attr_val = 1; |
|
3734 |
|
3735 retval = H5Awrite (a_id, H5T_NATIVE_UCHAR, (void*) &attr_val); |
|
3736 |
|
3737 H5Aclose (a_id); |
|
3738 } |
|
3739 else |
|
3740 retval = a_id; |
|
3741 |
|
3742 H5Sclose (as_id); |
|
3743 } |
|
3744 else |
|
3745 retval = as_id; |
|
3746 |
|
3747 return retval; |
|
3748 } |
|
3749 |
|
3750 |
|
3751 // save_type_to_hdf5 is not currently used, since hdf5 doesn't yet support |
|
3752 // automatic float<->integer conversions: |
|
3753 |
|
3754 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS |
|
3755 |
|
3756 // return the HDF5 type id corresponding to the Octave save_type |
|
3757 |
|
3758 static hid_t |
|
3759 save_type_to_hdf5 (save_type st) |
|
3760 { |
|
3761 switch (st) |
|
3762 { |
|
3763 case LS_U_CHAR: |
|
3764 return H5T_NATIVE_UCHAR; |
|
3765 |
|
3766 case LS_U_SHORT: |
|
3767 return H5T_NATIVE_USHORT; |
|
3768 |
|
3769 case LS_U_INT: |
|
3770 return H5T_NATIVE_UINT; |
|
3771 |
|
3772 case LS_CHAR: |
|
3773 return H5T_NATIVE_CHAR; |
|
3774 |
|
3775 case LS_SHORT: |
|
3776 return H5T_NATIVE_SHORT; |
|
3777 |
|
3778 case LS_INT: |
|
3779 return H5T_NATIVE_INT; |
|
3780 |
|
3781 case LS_FLOAT: |
|
3782 return H5T_NATIVE_FLOAT; |
|
3783 |
|
3784 case LS_DOUBLE: |
|
3785 default: |
|
3786 return H5T_NATIVE_DOUBLE; |
|
3787 } |
|
3788 } |
|
3789 #endif /* HAVE_HDF5_INT2FLOAT_CONVERSIONS */ |
|
3790 |
|
3791 // Add the data from TC to the HDF5 location loc_id, which could |
|
3792 // be either a file or a group within a file. Return true if |
|
3793 // successful. This function calls itself recursively for lists |
|
3794 // (stored as HDF5 groups). |
|
3795 |
|
3796 static bool |
|
3797 add_hdf5_data (hid_t loc_id, const octave_value& tc, |
|
3798 const std::string& name, const std::string& doc, |
|
3799 bool mark_as_global, bool save_as_floats) |
|
3800 { |
|
3801 hsize_t dims[3]; |
|
3802 hid_t type_id = -1, space_id = -1, data_id = -1; |
|
3803 bool data_is_group = 0; |
|
3804 bool retval = 0; |
|
3805 |
|
3806 if (tc.is_string ()) |
|
3807 { |
|
3808 int nr = tc.rows (); |
|
3809 charMatrix chm = tc.char_matrix_value (); |
|
3810 int nc = chm.cols (); |
|
3811 |
|
3812 // create datatype for (null-terminated) string to write from: |
|
3813 type_id = H5Tcopy (H5T_C_S1); H5Tset_size (type_id, nc + 1); |
|
3814 if (type_id < 0) |
|
3815 goto error_cleanup; |
|
3816 |
|
3817 dims[0] = nr; |
3956
|
3818 space_id = H5Screate_simple (nr > 0 ? 1 : 0, dims, (hsize_t*) 0); |
3687
|
3819 if (space_id < 0) |
|
3820 goto error_cleanup; |
|
3821 |
|
3822 data_id = H5Dcreate (loc_id, name.c_str (), |
|
3823 type_id, space_id, H5P_DEFAULT); |
|
3824 if (data_id < 0) |
|
3825 goto error_cleanup; |
|
3826 |
4249
|
3827 OCTAVE_LOCAL_BUFFER (char, s, nr * (nc + 1)); |
3687
|
3828 |
|
3829 for (int i = 0; i < nr; ++i) |
|
3830 { |
|
3831 std::string tstr = chm.row_as_string (i); |
|
3832 strcpy (s + i * (nc+1), tstr.c_str ()); |
|
3833 } |
|
3834 |
|
3835 if (H5Dwrite (data_id, type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
|
3836 (void*) s) < 0) { |
|
3837 goto error_cleanup; |
|
3838 } |
|
3839 } |
|
3840 else if (tc.is_range ()) |
|
3841 { |
3956
|
3842 space_id = H5Screate_simple (0, dims, (hsize_t*) 0); |
3687
|
3843 if (space_id < 0) |
|
3844 goto error_cleanup; |
|
3845 |
|
3846 type_id = hdf5_make_range_type (H5T_NATIVE_DOUBLE); |
|
3847 if (type_id < 0) |
|
3848 goto error_cleanup; |
|
3849 |
|
3850 data_id = H5Dcreate (loc_id, name.c_str (), |
|
3851 type_id, space_id, H5P_DEFAULT); |
|
3852 if (data_id < 0) |
|
3853 goto error_cleanup; |
|
3854 |
|
3855 Range r = tc.range_value (); |
|
3856 double range_vals[3]; |
|
3857 range_vals[0] = r.base (); |
|
3858 range_vals[1] = r.limit (); |
|
3859 range_vals[2] = r.inc (); |
|
3860 |
|
3861 if (H5Dwrite (data_id, type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
|
3862 (void*) range_vals) < 0) |
|
3863 goto error_cleanup; |
|
3864 } |
|
3865 else if (tc.is_real_scalar ()) |
|
3866 { |
3956
|
3867 space_id = H5Screate_simple (0, dims, (hsize_t*) 0); |
3687
|
3868 if (space_id < 0) goto error_cleanup; |
|
3869 |
|
3870 data_id = H5Dcreate (loc_id, name.c_str (), |
|
3871 H5T_NATIVE_DOUBLE, space_id, H5P_DEFAULT); |
|
3872 if (data_id < 0) |
|
3873 goto error_cleanup; |
|
3874 |
|
3875 double tmp = tc.double_value (); |
|
3876 if (H5Dwrite (data_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, |
|
3877 H5P_DEFAULT, (void*) &tmp) < 0) |
|
3878 goto error_cleanup; |
|
3879 } |
|
3880 else if (tc.is_real_matrix ()) |
|
3881 { |
|
3882 Matrix m = tc.matrix_value (); |
|
3883 dims[1] = m.rows (); // Octave uses column-major, while |
|
3884 dims[0] = m.columns (); // HDF5 uses row-major ordering |
|
3885 |
3956
|
3886 space_id = H5Screate_simple (dims[1] > 1 ?2:1, dims, (hsize_t*) 0); |
3687
|
3887 if (space_id < 0) |
|
3888 goto error_cleanup; |
|
3889 |
|
3890 hid_t save_type_id = H5T_NATIVE_DOUBLE; |
|
3891 |
|
3892 if (save_as_floats) |
|
3893 { |
|
3894 if (m.too_large_for_float ()) |
|
3895 { |
|
3896 warning ("save: some values too large to save as floats --"); |
|
3897 warning ("save: saving as doubles instead"); |
|
3898 } |
|
3899 else |
|
3900 save_type_id = H5T_NATIVE_FLOAT; |
|
3901 } |
|
3902 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS |
|
3903 // hdf5 currently doesn't support float/integer conversions |
|
3904 else |
|
3905 { |
|
3906 double max_val, min_val; |
|
3907 |
|
3908 if (m.all_integers (max_val, min_val)) |
|
3909 save_type_id |
|
3910 = save_type_to_hdf5 (get_save_type (max_val, min_val)); |
|
3911 } |
|
3912 #endif /* HAVE_HDF5_INT2FLOAT_CONVERSIONS */ |
|
3913 |
|
3914 data_id = H5Dcreate (loc_id, name.c_str (), |
|
3915 save_type_id, space_id, H5P_DEFAULT); |
|
3916 if (data_id < 0) |
|
3917 goto error_cleanup; |
|
3918 |
|
3919 double *mtmp = m.fortran_vec (); |
|
3920 if (H5Dwrite (data_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, |
|
3921 H5P_DEFAULT, (void*) mtmp) < 0) |
|
3922 goto error_cleanup; |
|
3923 } |
|
3924 else if (tc.is_complex_scalar ()) |
|
3925 { |
3956
|
3926 space_id = H5Screate_simple (0, dims, (hsize_t*) 0); |
3687
|
3927 if (space_id < 0) |
|
3928 goto error_cleanup; |
|
3929 |
|
3930 type_id = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); |
|
3931 if (type_id < 0) |
|
3932 goto error_cleanup; |
|
3933 |
|
3934 data_id = H5Dcreate (loc_id, name.c_str (), |
|
3935 type_id, space_id, H5P_DEFAULT); |
|
3936 if (data_id < 0) |
|
3937 goto error_cleanup; |
|
3938 |
|
3939 Complex tmp = tc.complex_value (); |
|
3940 if (H5Dwrite (data_id, type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
|
3941 (void*) X_CAST (double*, &tmp)) < 0) |
|
3942 goto error_cleanup; |
|
3943 } |
|
3944 else if (tc.is_complex_matrix ()) |
|
3945 { |
|
3946 ComplexMatrix m = tc.complex_matrix_value (); |
|
3947 |
|
3948 dims[1] = m.rows (); // Octave uses column-major, while |
|
3949 dims[0] = m.columns (); // HDF5 uses row-major ordering |
|
3950 |
3956
|
3951 space_id = H5Screate_simple (dims[1] > 1 ?2:1, dims, (hsize_t*) 0); |
3687
|
3952 if (space_id < 0) |
|
3953 goto error_cleanup; |
|
3954 |
|
3955 hid_t save_type_id = H5T_NATIVE_DOUBLE; |
|
3956 |
|
3957 if (save_as_floats) |
|
3958 { |
|
3959 if (m.too_large_for_float ()) |
|
3960 { |
|
3961 warning ("save: some values too large to save as floats --"); |
|
3962 warning ("save: saving as doubles instead"); |
|
3963 } |
|
3964 else |
|
3965 save_type_id = H5T_NATIVE_FLOAT; |
|
3966 } |
|
3967 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS |
|
3968 // hdf5 currently doesn't support float/integer conversions |
|
3969 else |
|
3970 { |
|
3971 double max_val, min_val; |
|
3972 |
|
3973 if (m.all_integers (max_val, min_val)) |
|
3974 save_type_id |
|
3975 = save_type_to_hdf5 (get_save_type (max_val, min_val)); |
|
3976 } |
|
3977 #endif /* HAVE_HDF5_INT2FLOAT_CONVERSIONS */ |
|
3978 |
|
3979 type_id = hdf5_make_complex_type (save_type_id); |
|
3980 if (type_id < 0) goto error_cleanup; |
|
3981 |
|
3982 data_id = H5Dcreate (loc_id, name.c_str (), |
|
3983 type_id, space_id, H5P_DEFAULT); |
|
3984 if (data_id < 0) |
|
3985 goto error_cleanup; |
|
3986 |
|
3987 hid_t complex_type_id = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); |
|
3988 if (complex_type_id < 0) |
|
3989 goto error_cleanup; |
|
3990 |
|
3991 Complex *mtmp = m.fortran_vec (); |
|
3992 if (H5Dwrite (data_id, complex_type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
|
3993 (void*) X_CAST (double *, mtmp)) < 0) |
|
3994 { |
|
3995 H5Tclose (complex_type_id); |
|
3996 goto error_cleanup; |
|
3997 } |
|
3998 |
|
3999 H5Tclose (complex_type_id); |
|
4000 } |
|
4001 else if (tc.is_list ()) |
|
4002 { |
|
4003 data_id = H5Gcreate (loc_id, name.c_str (), 0); |
|
4004 if (data_id < 0) |
|
4005 goto error_cleanup; |
|
4006 |
|
4007 data_is_group = 1; |
|
4008 |
|
4009 // recursively add each element of the list to this group |
|
4010 octave_value_list lst = tc.list_value (); |
|
4011 |
|
4012 for (int i = 0; i < lst.length (); ++i) |
|
4013 { |
|
4014 // should we use lst.name_tags () to label the elements? |
|
4015 char s[20]; |
4372
|
4016 sprintf (s, "_%d", i); |
3687
|
4017 bool retval2 = add_hdf5_data (data_id, lst (i), s, "", |
|
4018 false, save_as_floats); |
|
4019 if (! retval2) |
|
4020 goto error_cleanup; |
|
4021 } |
|
4022 |
|
4023 // mark with an attribute "OCTAVE_LIST" with value 1 |
|
4024 // to distinguish from structures (also stored as HDF5 groups): |
3688
|
4025 if (hdf5_add_attr (data_id, "OCTAVE_LIST") < 0) |
3687
|
4026 goto error_cleanup; |
|
4027 } |
|
4028 else if (tc.is_map ()) |
|
4029 { |
|
4030 // an Octave structure |
|
4031 data_id = H5Gcreate (loc_id, name.c_str (), 0); |
|
4032 if (data_id < 0) |
|
4033 goto error_cleanup; |
|
4034 |
|
4035 data_is_group = 1; |
|
4036 |
|
4037 // recursively add each element of the structure to this group |
|
4038 Octave_map m = tc.map_value (); |
4219
|
4039 Octave_map::iterator i = m.begin (); |
|
4040 while (i != m.end ()) |
3687
|
4041 { |
4233
|
4042 // XXX FIXME XXX -- if the length of the structure array is |
|
4043 // 1, should we really create a list object? |
|
4044 bool retval2 = add_hdf5_data (data_id, octave_value (m.contents (i)), |
|
4045 m.key (i), "", false, save_as_floats); |
3687
|
4046 if (! retval2) |
|
4047 goto error_cleanup; |
|
4048 |
4219
|
4049 i++; |
3687
|
4050 } |
|
4051 } |
|
4052 else |
|
4053 { |
|
4054 gripe_wrong_type_arg ("save", tc, false); |
|
4055 goto error_cleanup; |
|
4056 } |
|
4057 |
|
4058 // attach doc string as comment: |
|
4059 if (doc.length () > 0 |
|
4060 && H5Gset_comment (loc_id, name.c_str (), doc.c_str ()) < 0) |
|
4061 goto error_cleanup; |
|
4062 |
|
4063 retval = 1; |
|
4064 |
|
4065 // if it's global, add an attribute "OCTAVE_GLOBAL" with value 1 |
|
4066 if (mark_as_global) |
3688
|
4067 retval = hdf5_add_attr (data_id, "OCTAVE_GLOBAL") >= 0; |
3687
|
4068 |
|
4069 error_cleanup: |
|
4070 |
|
4071 if (! retval) |
|
4072 error ("save: error while writing `%s' to hdf5 file", name.c_str ()); |
|
4073 |
|
4074 if (data_id >= 0) |
|
4075 { |
|
4076 if (data_is_group) |
|
4077 H5Gclose (data_id); |
|
4078 else |
|
4079 H5Dclose (data_id); |
|
4080 } |
|
4081 |
|
4082 if (space_id >= 0) |
|
4083 H5Sclose (space_id); |
|
4084 |
|
4085 if (type_id >= 0) |
|
4086 H5Tclose (type_id); |
|
4087 |
|
4088 return retval; |
|
4089 } |
|
4090 |
|
4091 // Write data from TC in HDF5 (binary) format to the stream OS, |
|
4092 // which must be an hdf5_ofstream, returning true on success. |
|
4093 |
|
4094 static bool |
|
4095 save_hdf5_data (std::ostream& os, const octave_value& tc, |
|
4096 const std::string& name, const std::string& doc, |
|
4097 bool mark_as_global, bool save_as_floats) |
|
4098 { |
|
4099 hdf5_ofstream& hs = (hdf5_ofstream&) os; |
|
4100 |
|
4101 return add_hdf5_data (hs.file_id, tc, name, doc, |
|
4102 mark_as_global, save_as_floats); |
|
4103 } |
|
4104 |
|
4105 #endif /* HAVE_HDF5 */ |
|
4106 |
3688
|
4107 static int |
|
4108 write_mat5_tag (std::ostream& is, int type, int bytes) |
|
4109 { |
|
4110 FOUR_BYTE_INT temp; |
|
4111 |
|
4112 if (bytes <= 4) |
|
4113 temp = (bytes << 16) + type; |
|
4114 else |
|
4115 { |
|
4116 temp = type; |
|
4117 if (! is.write ((char *)&temp, 4)) |
|
4118 goto data_write_error; |
|
4119 temp = bytes; |
|
4120 } |
|
4121 |
|
4122 if (! is.write ((char *)&temp, 4)) |
|
4123 goto data_write_error; |
|
4124 |
|
4125 return 0; |
|
4126 |
|
4127 data_write_error: |
|
4128 return 1; |
|
4129 } |
|
4130 |
|
4131 // write out the numeric values in M to OS, |
|
4132 // preceded by the appropriate tag. |
|
4133 static void |
|
4134 write_mat5_array (std::ostream& os, Matrix& m, const int save_as_floats) |
|
4135 { |
|
4136 int nr = m.rows (); |
|
4137 int nc = m.columns (); |
|
4138 double max_val, min_val; |
|
4139 save_type st = LS_DOUBLE; |
|
4140 mat5_data_type mst; |
|
4141 int size; |
|
4142 unsigned len; |
|
4143 double *data = m.fortran_vec (); |
|
4144 |
|
4145 // Have to use copy here to avoid writing over data accessed via |
|
4146 // Matrix::data(). |
|
4147 |
|
4148 #define MAT5_DO_WRITE(TYPE, data, count, stream) \ |
|
4149 do \ |
|
4150 { \ |
4382
|
4151 OCTAVE_LOCAL_BUFFER (TYPE, ptr, count); \ |
3688
|
4152 for (int i = 0; i < count; i++) \ |
|
4153 ptr[i] = X_CAST (TYPE, data[i]); \ |
|
4154 stream.write (X_CAST (char *, ptr), count * sizeof (TYPE)); \ |
|
4155 } \ |
|
4156 while (0) |
|
4157 |
|
4158 if (save_as_floats) |
|
4159 { |
|
4160 if (m.too_large_for_float ()) |
|
4161 { |
|
4162 warning ("save: some values too large to save as floats --"); |
|
4163 warning ("save: saving as doubles instead"); |
|
4164 } |
|
4165 else |
|
4166 st = LS_FLOAT; |
|
4167 } |
|
4168 |
|
4169 if (m.all_integers (max_val, min_val)) |
|
4170 st = get_save_type (max_val, min_val); |
|
4171 |
|
4172 switch (st) |
|
4173 { |
|
4174 default: |
|
4175 case LS_DOUBLE: mst = miDOUBLE; size = 8; break; |
|
4176 case LS_FLOAT: mst = miSINGLE; size = 4; break; |
|
4177 case LS_U_CHAR: mst = miUINT8; size = 1; break; |
|
4178 case LS_U_SHORT: mst = miUINT16; size = 2; break; |
|
4179 case LS_U_INT: mst = miUINT32; size = 4; break; |
|
4180 case LS_CHAR: mst = miINT8; size = 1; break; |
|
4181 case LS_SHORT: mst = miINT16; size = 2; break; |
|
4182 case LS_INT: mst = miINT32; size = 4; break; |
|
4183 } |
|
4184 |
|
4185 len = nr*nc*size; |
|
4186 write_mat5_tag (os, mst, len); |
|
4187 |
|
4188 { |
|
4189 switch (st) |
|
4190 { |
|
4191 case LS_U_CHAR: |
|
4192 MAT5_DO_WRITE (unsigned char, data, nr*nc, os); |
|
4193 break; |
|
4194 |
|
4195 case LS_U_SHORT: |
|
4196 MAT5_DO_WRITE (unsigned TWO_BYTE_INT, data, nr*nc, os); |
|
4197 break; |
|
4198 |
|
4199 case LS_U_INT: |
|
4200 MAT5_DO_WRITE (unsigned FOUR_BYTE_INT, data, nr*nc, os); |
|
4201 break; |
|
4202 |
|
4203 // provide for 64 bit ints, even though get_save_type does |
|
4204 // not yet implement them |
|
4205 #ifdef EIGHT_BYTE_INT |
|
4206 case LS_U_LONG: |
|
4207 MAT5_DO_WRITE (unsigned EIGHT_BYTE_INT, data, nr*nc, os); |
|
4208 break; |
|
4209 #endif |
|
4210 |
|
4211 case LS_CHAR: |
|
4212 MAT5_DO_WRITE (signed char, data, nr*nc, os); |
|
4213 break; |
|
4214 |
|
4215 case LS_SHORT: |
|
4216 MAT5_DO_WRITE (TWO_BYTE_INT, data, nr*nc, os); |
|
4217 break; |
|
4218 |
|
4219 case LS_INT: |
|
4220 MAT5_DO_WRITE (FOUR_BYTE_INT, data, nr*nc, os); |
|
4221 break; |
|
4222 |
|
4223 #ifdef EIGHT_BYTE_INT |
|
4224 case LS_LONG: |
|
4225 MAT5_DO_WRITE (EIGHT_BYTE_INT, data, nr*nc, os); |
|
4226 break; |
|
4227 #endif |
|
4228 |
|
4229 case LS_FLOAT: |
|
4230 MAT5_DO_WRITE (float, data, nr*nc, os); |
|
4231 break; |
|
4232 |
|
4233 case LS_DOUBLE: // No conversion necessary. |
|
4234 os.write (X_CAST (char *, data), len); |
|
4235 break; |
|
4236 |
|
4237 default: |
|
4238 (*current_liboctave_error_handler) |
|
4239 ("unrecognized data format requested"); |
|
4240 break; |
|
4241 } |
|
4242 } |
|
4243 if (PAD (len) > len) |
|
4244 { |
|
4245 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
4246 os.write (buf, PAD (len) - len); |
|
4247 } |
|
4248 } |
|
4249 |
4332
|
4250 // Write out cell element values in the cell array to OS, preceded by |
|
4251 // the appropriate tag. |
|
4252 |
|
4253 static bool |
|
4254 write_mat5_cell_array (std::ostream& os, Cell& cell, bool mark_as_global, |
|
4255 const int save_as_floats) |
|
4256 { |
|
4257 int nr = cell.rows (); |
|
4258 int nc = cell.columns (); |
|
4259 |
|
4260 for (int j = 0; j < nc; j++) |
|
4261 { |
|
4262 for (int i = 0; i < nr; i++) |
|
4263 { |
|
4264 octave_value ov = cell.elem (i, j); |
|
4265 |
|
4266 if (! save_mat5_binary_element (os, ov, "", mark_as_global, |
|
4267 save_as_floats)) |
|
4268 return false; |
|
4269 } |
|
4270 } |
|
4271 |
|
4272 return true; |
|
4273 } |
|
4274 |
3688
|
4275 // save the data from TC along with the corresponding NAME on stream |
|
4276 // OS in the MatLab version 5 binary format. Return true on success. |
|
4277 |
|
4278 static bool |
|
4279 save_mat5_binary_element (std::ostream& os, |
|
4280 const octave_value& tc, const std::string& name, |
|
4281 bool mark_as_global, bool save_as_floats) |
|
4282 { |
|
4283 FOUR_BYTE_INT flags=0; |
|
4284 FOUR_BYTE_INT junk=0; |
|
4285 FOUR_BYTE_INT nr; |
|
4286 FOUR_BYTE_INT nc; |
3769
|
4287 std::streampos fixup, contin; |
3688
|
4288 |
|
4289 // element type and length |
|
4290 fixup = os.tellp (); |
|
4291 write_mat5_tag (os, miMATRIX, 99); // we don't know the real length yet |
|
4292 |
|
4293 // array flags subelement |
|
4294 write_mat5_tag (os, miUINT32, 8); |
|
4295 |
|
4296 if (mark_as_global) |
|
4297 flags |= 0x0400; |
|
4298 |
|
4299 if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
4300 flags |= 0x0800; |
|
4301 |
|
4302 if (tc.is_string ()) |
|
4303 flags |= mxCHAR_CLASS; |
|
4304 else if (tc.is_real_scalar ()) |
|
4305 flags |= mxDOUBLE_CLASS; |
3866
|
4306 else if (tc.is_real_matrix () || tc.is_range ()) |
3688
|
4307 flags |= mxDOUBLE_CLASS; |
|
4308 else if (tc.is_complex_scalar ()) |
|
4309 flags |= mxDOUBLE_CLASS; |
|
4310 else if (tc.is_complex_matrix ()) |
|
4311 flags |= mxDOUBLE_CLASS; |
|
4312 else if (tc.is_map ()) |
|
4313 flags |= mxSTRUCT_CLASS; |
4332
|
4314 else if (tc.is_cell ()) |
|
4315 flags |= mxCELL_CLASS; |
3688
|
4316 else |
|
4317 { |
|
4318 gripe_wrong_type_arg ("save", tc, false); |
|
4319 goto error_cleanup; |
|
4320 } |
|
4321 |
|
4322 os.write ((char *)&flags, 4); |
|
4323 os.write ((char *)&junk, 4); |
|
4324 |
|
4325 // dimensions array subelement |
4362
|
4326 nr = tc.rows (); |
|
4327 nc = tc.columns (); |
3866
|
4328 |
|
4329 write_mat5_tag (os, miINT32, 8); |
|
4330 os.write ((char *)&nr, 4); |
|
4331 os.write ((char *)&nc, 4); |
3688
|
4332 |
|
4333 // array name subelement |
|
4334 { |
|
4335 int namelen = name.length (); |
|
4336 |
|
4337 if (namelen > 31) |
|
4338 namelen = 31; // only 31 char names permitted in mat file |
|
4339 |
|
4340 int paddedlength = PAD (namelen); |
|
4341 |
|
4342 write_mat5_tag (os, miINT8, namelen); |
4249
|
4343 OCTAVE_LOCAL_BUFFER (char, paddedname, paddedlength); |
3688
|
4344 memset (paddedname, 0, paddedlength); |
|
4345 strncpy (paddedname, name.c_str (), namelen); |
|
4346 os.write (paddedname, paddedlength); |
|
4347 } |
|
4348 |
|
4349 // data element |
|
4350 if (tc.is_string ()) |
|
4351 { |
|
4352 charMatrix chm = tc.char_matrix_value (); |
|
4353 int nc = chm.cols (); |
|
4354 int len = nr*nc*2; |
|
4355 int paddedlength = PAD (nr*nc*2); |
|
4356 |
4249
|
4357 OCTAVE_LOCAL_BUFFER (TWO_BYTE_INT, buf, nc*nr+3); |
3688
|
4358 write_mat5_tag (os, miUINT16, len); |
|
4359 |
|
4360 for (int i = 0; i < nr; i++) |
|
4361 { |
|
4362 std::string tstr = chm.row_as_string (i); |
|
4363 const char *s = tstr.data (); |
|
4364 |
|
4365 for (int j = 0; j < nc; j++) |
4420
|
4366 buf[j*nr+i] = *s++ & 0x00FF; |
3688
|
4367 } |
3895
|
4368 os.write ((char *)buf, nr*nc*2); |
3688
|
4369 |
|
4370 if (paddedlength > len) |
|
4371 os.write ((char *)buf, paddedlength - len); |
|
4372 } |
3866
|
4373 else if (tc.is_real_scalar () || tc.is_real_matrix () || tc.is_range ()) |
3688
|
4374 { |
|
4375 Matrix m = tc.matrix_value (); |
|
4376 |
|
4377 write_mat5_array (os, m, save_as_floats); |
|
4378 } |
4332
|
4379 else if (tc.is_cell ()) |
|
4380 { |
|
4381 Cell cell = tc.cell_value (); |
|
4382 |
|
4383 if (! write_mat5_cell_array (os, cell, mark_as_global, save_as_floats)) |
|
4384 goto error_cleanup; |
|
4385 } |
3688
|
4386 else if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
4387 { |
|
4388 ComplexMatrix m_cmplx = tc.complex_matrix_value (); |
|
4389 Matrix m = ::real (m_cmplx); |
|
4390 |
|
4391 for (int part=0; part < 2; part++) |
|
4392 { |
|
4393 // real part, then complex part |
|
4394 write_mat5_array (os, m, save_as_floats); |
|
4395 m = ::imag (m_cmplx); |
|
4396 } |
|
4397 } |
|
4398 else if (tc.is_map ()) |
|
4399 { |
|
4400 // an Octave structure */ |
|
4401 // recursively write each element of the structure |
|
4402 Octave_map m = tc.map_value (); |
|
4403 |
|
4404 { |
|
4405 char buf[32]; |
|
4406 FOUR_BYTE_INT maxfieldnamelength = 32; |
|
4407 int fieldcnt = 0; |
|
4408 |
4219
|
4409 for (Octave_map::iterator i = m.begin (); i != m.end (); i++) |
3688
|
4410 fieldcnt++; |
|
4411 |
|
4412 write_mat5_tag (os, miINT32, 4); |
|
4413 os.write ((char *)&maxfieldnamelength, 4); |
|
4414 write_mat5_tag (os, miINT8, fieldcnt*32); |
4219
|
4415 |
|
4416 for (Octave_map::iterator i = m.begin (); i != m.end (); i++) |
3688
|
4417 { |
|
4418 // write the name of each element |
3769
|
4419 std::string tstr = m.key (i); |
3688
|
4420 memset (buf, 0, 32); |
|
4421 strncpy (buf, tstr.c_str (), 31); // only 31 char names permitted |
|
4422 os.write (buf, 32); |
|
4423 } |
|
4424 |
4561
|
4425 int len = m.numel (); |
4362
|
4426 |
4219
|
4427 for (Octave_map::iterator i = m.begin (); i != m.end (); i++) |
3688
|
4428 { |
|
4429 // write the data of each element |
4513
|
4430 Cell elts = m.contents (i); |
4362
|
4431 |
|
4432 for (int j = 0; j < len; j++) |
|
4433 { |
|
4434 bool retval2 = save_mat5_binary_element (os, elts(j), "", |
|
4435 mark_as_global, |
|
4436 save_as_floats); |
|
4437 if (! retval2) |
|
4438 goto error_cleanup; |
|
4439 } |
3688
|
4440 } |
|
4441 } |
|
4442 } |
|
4443 else |
|
4444 gripe_wrong_type_arg ("save", tc, false); |
|
4445 |
|
4446 contin = os.tellp (); |
|
4447 os.seekp (fixup); |
3841
|
4448 write_mat5_tag (os, miMATRIX, |
|
4449 static_cast<int>(contin - fixup) - 8); // the actual length |
3688
|
4450 os.seekp (contin); |
|
4451 |
|
4452 return true; |
|
4453 |
|
4454 error_cleanup: |
|
4455 error ("save: error while writing `%s' to MAT file", name.c_str ()); |
|
4456 |
|
4457 return false; |
|
4458 } |
|
4459 |
667
|
4460 // Save the data from TC along with the corresponding NAME on stream OS |
3688
|
4461 // in the MatLab version 4 binary format. |
667
|
4462 |
2799
|
4463 static bool |
3523
|
4464 save_mat_binary_data (std::ostream& os, const octave_value& tc, |
|
4465 const std::string& name) |
667
|
4466 { |
|
4467 FOUR_BYTE_INT mopt = 0; |
|
4468 |
|
4469 mopt += tc.is_string () ? 1 : 0; |
2318
|
4470 |
|
4471 oct_mach_info::float_format flt_fmt = |
|
4472 oct_mach_info::native_float_format ();; |
|
4473 |
|
4474 mopt += 1000 * float_format_to_mopt_digit (flt_fmt); |
667
|
4475 |
3557
|
4476 os.write (X_CAST (char *, &mopt), 4); |
667
|
4477 |
|
4478 FOUR_BYTE_INT nr = tc.rows (); |
3557
|
4479 os.write (X_CAST (char *, &nr), 4); |
667
|
4480 |
|
4481 FOUR_BYTE_INT nc = tc.columns (); |
3557
|
4482 os.write (X_CAST (char *, &nc), 4); |
667
|
4483 |
|
4484 int len = nr * nc; |
|
4485 |
|
4486 FOUR_BYTE_INT imag = tc.is_complex_type () ? 1 : 0; |
3557
|
4487 os.write (X_CAST (char *, &imag), 4); |
667
|
4488 |
2345
|
4489 // LEN includes the terminating character, and the file is also |
|
4490 // supposed to include it. |
|
4491 |
|
4492 FOUR_BYTE_INT name_len = name.length () + 1; |
667
|
4493 |
3557
|
4494 os.write (X_CAST (char *, &name_len), 4); |
2345
|
4495 os << name << '\0'; |
667
|
4496 |
3233
|
4497 if (tc.is_string ()) |
|
4498 { |
|
4499 unwind_protect::begin_frame ("save_mat_binary_data"); |
4420
|
4500 |
|
4501 charMatrix chm = tc.char_matrix_value (); |
|
4502 |
|
4503 int nr = chm.rows (); |
|
4504 int nc = chm.cols (); |
|
4505 |
|
4506 OCTAVE_LOCAL_BUFFER (double, buf, nc*nr); |
|
4507 |
|
4508 for (int i = 0; i < nr; i++) |
|
4509 { |
|
4510 std::string tstr = chm.row_as_string (i); |
|
4511 const char *s = tstr.data (); |
|
4512 |
|
4513 for (int j = 0; j < nc; j++) |
|
4514 buf[j*nr+i] = static_cast<double> (*s++ & 0x00FF); |
|
4515 } |
|
4516 os.write ((char *)buf, nr*nc*sizeof(double)); |
|
4517 |
3233
|
4518 unwind_protect::run_frame ("save_mat_binary_data"); |
|
4519 } |
|
4520 else if (tc.is_range ()) |
|
4521 { |
|
4522 Range r = tc.range_value (); |
|
4523 double base = r.base (); |
|
4524 double inc = r.inc (); |
|
4525 int nel = r.nelem (); |
|
4526 for (int i = 0; i < nel; i++) |
|
4527 { |
|
4528 double x = base + i * inc; |
3557
|
4529 os.write (X_CAST (char *, &x), 8); |
3233
|
4530 } |
|
4531 } |
|
4532 else if (tc.is_real_scalar ()) |
667
|
4533 { |
|
4534 double tmp = tc.double_value (); |
3557
|
4535 os.write (X_CAST (char *, &tmp), 8); |
667
|
4536 } |
911
|
4537 else if (tc.is_real_matrix ()) |
667
|
4538 { |
|
4539 Matrix m = tc.matrix_value (); |
3557
|
4540 os.write (X_CAST (char *, m.data ()), 8 * len); |
667
|
4541 } |
|
4542 else if (tc.is_complex_scalar ()) |
|
4543 { |
|
4544 Complex tmp = tc.complex_value (); |
3557
|
4545 os.write (X_CAST (char *, &tmp), 16); |
667
|
4546 } |
|
4547 else if (tc.is_complex_matrix ()) |
|
4548 { |
|
4549 ComplexMatrix m_cmplx = tc.complex_matrix_value (); |
3688
|
4550 Matrix m = ::real (m_cmplx); |
3557
|
4551 os.write (X_CAST (char *, m.data ()), 8 * len); |
3688
|
4552 m = ::imag (m_cmplx); |
3557
|
4553 os.write (X_CAST (char *, m.data ()), 8 * len); |
667
|
4554 } |
|
4555 else |
2799
|
4556 gripe_wrong_type_arg ("save", tc, false); |
|
4557 |
|
4558 return os; |
667
|
4559 } |
|
4560 |
620
|
4561 static void |
3523
|
4562 ascii_save_type (std::ostream& os, const char *type, bool mark_as_global) |
620
|
4563 { |
|
4564 if (mark_as_global) |
4060
|
4565 os << "# type: global "; |
620
|
4566 else |
4060
|
4567 os << "# type: "; |
620
|
4568 |
|
4569 os << type << "\n"; |
|
4570 } |
|
4571 |
872
|
4572 static Matrix |
|
4573 strip_infnan (const Matrix& m) |
|
4574 { |
|
4575 int nr = m.rows (); |
|
4576 int nc = m.columns (); |
|
4577 |
|
4578 Matrix retval (nr, nc); |
|
4579 |
|
4580 int k = 0; |
|
4581 for (int i = 0; i < nr; i++) |
|
4582 { |
|
4583 for (int j = 0; j < nc; j++) |
|
4584 { |
2305
|
4585 double d = m (i, j); |
872
|
4586 if (xisnan (d)) |
|
4587 goto next_row; |
|
4588 else |
2305
|
4589 retval (k, j) = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
872
|
4590 } |
|
4591 k++; |
|
4592 |
|
4593 next_row: |
|
4594 continue; |
|
4595 } |
|
4596 |
|
4597 if (k > 0) |
|
4598 retval.resize (k, nc); |
|
4599 |
|
4600 return retval; |
|
4601 } |
|
4602 |
|
4603 static ComplexMatrix |
|
4604 strip_infnan (const ComplexMatrix& m) |
|
4605 { |
|
4606 int nr = m.rows (); |
|
4607 int nc = m.columns (); |
|
4608 |
|
4609 ComplexMatrix retval (nr, nc); |
|
4610 |
|
4611 int k = 0; |
|
4612 for (int i = 0; i < nr; i++) |
|
4613 { |
|
4614 for (int j = 0; j < nc; j++) |
|
4615 { |
2305
|
4616 Complex c = m (i, j); |
872
|
4617 if (xisnan (c)) |
|
4618 goto next_row; |
|
4619 else |
|
4620 { |
|
4621 double re = real (c); |
|
4622 double im = imag (c); |
|
4623 |
|
4624 re = xisinf (re) ? (re > 0 ? OCT_RBV : -OCT_RBV) : re; |
|
4625 im = xisinf (im) ? (im > 0 ? OCT_RBV : -OCT_RBV) : im; |
|
4626 |
2305
|
4627 retval (k, j) = Complex (re, im); |
872
|
4628 } |
|
4629 } |
|
4630 k++; |
|
4631 |
|
4632 next_row: |
|
4633 continue; |
|
4634 } |
|
4635 |
|
4636 if (k > 0) |
|
4637 retval.resize (k, nc); |
|
4638 |
|
4639 return retval; |
|
4640 } |
|
4641 |
620
|
4642 // Save the data from TC along with the corresponding NAME, and global |
604
|
4643 // flag MARK_AS_GLOBAL on stream OS in the plain text format described |
1755
|
4644 // above for load_ascii_data. If NAME is empty, the name: line is not |
604
|
4645 // generated. PRECISION specifies the number of decimal digits to print. |
3019
|
4646 // If STRIP_NAN_AND_INF is TRUE, rows containing NaNs are deleted, |
872
|
4647 // and Infinite values are converted to +/-OCT_RBV (A Real Big Value, |
|
4648 // but not so big that gnuplot can't handle it when trying to compute |
|
4649 // axis ranges, etc.). |
|
4650 // |
|
4651 // Assumes ranges and strings cannot contain Inf or NaN values. |
|
4652 // |
|
4653 // Returns 1 for success and 0 for failure. |
604
|
4654 |
|
4655 // XXX FIXME XXX -- should probably write the help string here too. |
|
4656 |
3738
|
4657 static bool |
4401
|
4658 save_ascii_data (std::ostream& os, const octave_value& val_arg, |
3738
|
4659 const std::string& name, bool& infnan_warned, |
|
4660 bool strip_nan_and_inf, bool mark_as_global, |
|
4661 int precision) |
604
|
4662 { |
2799
|
4663 bool success = true; |
620
|
4664 |
604
|
4665 if (! precision) |
2194
|
4666 precision = Vsave_precision; |
604
|
4667 |
1755
|
4668 if (! name.empty ()) |
4060
|
4669 os << "# name: " << name << "\n"; |
604
|
4670 |
|
4671 long old_precision = os.precision (); |
|
4672 os.precision (precision); |
|
4673 |
4401
|
4674 octave_value val = val_arg; |
|
4675 |
|
4676 if (val.is_range ()) |
|
4677 { |
|
4678 Range r = val.range_value (); |
|
4679 double base = r.base (); |
|
4680 double limit = r.limit (); |
|
4681 double inc = r.inc (); |
|
4682 if (! (NINT (base) == base |
|
4683 && NINT (limit) == limit |
|
4684 && NINT (inc) == inc)) |
|
4685 val = val.matrix_value (); |
|
4686 } |
|
4687 |
|
4688 if (val.is_string ()) |
3233
|
4689 { |
|
4690 ascii_save_type (os, "string array", mark_as_global); |
4401
|
4691 charMatrix chm = val.char_matrix_value (); |
3233
|
4692 int elements = chm.rows (); |
4060
|
4693 os << "# elements: " << elements << "\n"; |
3233
|
4694 for (int i = 0; i < elements; i++) |
|
4695 { |
3841
|
4696 unsigned len = chm.cols (); |
4060
|
4697 os << "# length: " << len << "\n"; |
3836
|
4698 std::string tstr = chm.row_as_string (i, false, true); |
3233
|
4699 const char *tmp = tstr.data (); |
3836
|
4700 if (tstr.length () > len) |
|
4701 panic_impossible (); |
3557
|
4702 os.write (X_CAST (char *, tmp), len); |
3233
|
4703 os << "\n"; |
|
4704 } |
|
4705 } |
4401
|
4706 else if (val.is_range ()) |
3233
|
4707 { |
|
4708 ascii_save_type (os, "range", mark_as_global); |
4401
|
4709 Range tmp = val.range_value (); |
4130
|
4710 os << "# base, limit, increment\n"; |
|
4711 octave_write_double (os, tmp.base ()); |
|
4712 os << " "; |
|
4713 octave_write_double (os, tmp.limit ()); |
|
4714 os << " "; |
|
4715 octave_write_double (os, tmp.inc ()); |
|
4716 os << "\n"; |
3233
|
4717 } |
4401
|
4718 else if (val.is_real_scalar ()) |
620
|
4719 { |
|
4720 ascii_save_type (os, "scalar", mark_as_global); |
872
|
4721 |
4401
|
4722 double d = val.double_value (); |
3738
|
4723 |
872
|
4724 if (strip_nan_and_inf) |
|
4725 { |
|
4726 if (xisnan (d)) |
|
4727 { |
|
4728 error ("only value to plot is NaN"); |
2799
|
4729 success = false; |
872
|
4730 } |
|
4731 else |
|
4732 { |
|
4733 d = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
4130
|
4734 octave_write_double (os, d); |
|
4735 os << "\n"; |
872
|
4736 } |
|
4737 } |
|
4738 else |
3738
|
4739 { |
|
4740 if (! infnan_warned && (xisnan (d) || xisinf (d))) |
|
4741 { |
|
4742 warning ("save: Inf or NaN values may not be reloadable"); |
|
4743 infnan_warned = true; |
|
4744 } |
|
4745 |
4130
|
4746 octave_write_double (os, d); |
|
4747 os << "\n"; |
3738
|
4748 } |
620
|
4749 } |
4401
|
4750 else if (val.is_real_matrix ()) |
620
|
4751 { |
|
4752 ascii_save_type (os, "matrix", mark_as_global); |
3738
|
4753 |
4401
|
4754 os << "# rows: " << val.rows () << "\n" |
|
4755 << "# columns: " << val.columns () << "\n"; |
|
4756 |
|
4757 Matrix tmp = val.matrix_value (); |
3738
|
4758 |
872
|
4759 if (strip_nan_and_inf) |
|
4760 tmp = strip_infnan (tmp); |
3738
|
4761 else if (! infnan_warned && tmp.any_element_is_inf_or_nan ()) |
|
4762 { |
|
4763 warning ("save: Inf or NaN values may not be reloadable"); |
|
4764 infnan_warned = true; |
|
4765 } |
872
|
4766 |
|
4767 os << tmp; |
620
|
4768 } |
4401
|
4769 else if (val.is_cell ()) |
4332
|
4770 { |
|
4771 ascii_save_type (os, "cell", mark_as_global); |
|
4772 |
4401
|
4773 os << "# rows: " << val.rows () << "\n" |
|
4774 << "# columns: " << val.columns () << "\n"; |
|
4775 |
|
4776 Cell tmp = val.cell_value (); |
4332
|
4777 |
|
4778 for (int j = 0; j < tmp.cols (); j++) |
|
4779 { |
|
4780 for (int i = 0; i < tmp.rows (); i++) |
|
4781 { |
|
4782 octave_value o_val = tmp.elem (i, j); |
|
4783 |
|
4784 // Recurse to print sub-value. |
|
4785 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, |
|
4786 infnan_warned, strip_nan_and_inf, |
|
4787 mark_as_global, 0); |
|
4788 |
|
4789 if (! b) |
|
4790 return os; |
|
4791 } |
|
4792 |
|
4793 os << "\n"; |
|
4794 } |
|
4795 } |
4401
|
4796 else if (val.is_complex_scalar ()) |
620
|
4797 { |
|
4798 ascii_save_type (os, "complex scalar", mark_as_global); |
872
|
4799 |
4401
|
4800 Complex c = val.complex_value (); |
3738
|
4801 |
872
|
4802 if (strip_nan_and_inf) |
|
4803 { |
|
4804 if (xisnan (c)) |
|
4805 { |
|
4806 error ("only value to plot is NaN"); |
2799
|
4807 success = false; |
872
|
4808 } |
|
4809 else |
|
4810 { |
|
4811 double re = real (c); |
|
4812 double im = imag (c); |
|
4813 |
|
4814 re = xisinf (re) ? (re > 0 ? OCT_RBV : -OCT_RBV) : re; |
|
4815 im = xisinf (im) ? (im > 0 ? OCT_RBV : -OCT_RBV) : im; |
|
4816 |
|
4817 c = Complex (re, im); |
|
4818 |
4130
|
4819 octave_write_complex (os, c); |
|
4820 os << "\n"; |
872
|
4821 } |
|
4822 } |
|
4823 else |
3738
|
4824 { |
|
4825 if (! infnan_warned && (xisnan (c) || xisinf (c))) |
|
4826 { |
|
4827 warning ("save: Inf or NaN values may not be reloadable"); |
|
4828 infnan_warned = true; |
|
4829 } |
|
4830 |
4130
|
4831 octave_write_complex (os, c); |
|
4832 os << "\n"; |
3738
|
4833 } |
620
|
4834 } |
4401
|
4835 else if (val.is_complex_matrix ()) |
604
|
4836 { |
620
|
4837 ascii_save_type (os, "complex matrix", mark_as_global); |
3738
|
4838 |
4401
|
4839 os << "# rows: " << val.rows () << "\n" |
|
4840 << "# columns: " << val.columns () << "\n"; |
|
4841 |
|
4842 ComplexMatrix tmp = val.complex_matrix_value (); |
3738
|
4843 |
872
|
4844 if (strip_nan_and_inf) |
|
4845 tmp = strip_infnan (tmp); |
3738
|
4846 else if (! infnan_warned && tmp.any_element_is_inf_or_nan ()) |
|
4847 { |
|
4848 warning ("save: Inf or NaN values may not be reloadable"); |
|
4849 infnan_warned = true; |
|
4850 } |
872
|
4851 |
|
4852 os << tmp; |
620
|
4853 } |
|
4854 else |
4401
|
4855 gripe_wrong_type_arg ("save", val, false); |
604
|
4856 |
|
4857 os.precision (old_precision); |
|
4858 |
872
|
4859 return (os && success); |
604
|
4860 } |
|
4861 |
3738
|
4862 bool |
|
4863 save_ascii_data_for_plotting (std::ostream& os, const octave_value& t, |
|
4864 const std::string& name) |
|
4865 { |
|
4866 bool infnan_warned = true; |
|
4867 |
3769
|
4868 return save_ascii_data (os, t, name, infnan_warned, true, false, 0); |
3738
|
4869 } |
|
4870 |
604
|
4871 // Save the info from sr on stream os in the format specified by fmt. |
|
4872 |
4329
|
4873 void |
3523
|
4874 do_save (std::ostream& os, symbol_record *sr, load_save_format fmt, |
3738
|
4875 int save_as_floats, bool& infnan_warned) |
604
|
4876 { |
|
4877 if (! sr->is_variable ()) |
|
4878 { |
|
4879 error ("save: can only save variables, not functions"); |
|
4880 return; |
|
4881 } |
|
4882 |
3523
|
4883 std::string name = sr->name (); |
|
4884 std::string help = sr->help (); |
604
|
4885 int global = sr->is_linked_to_global (); |
2970
|
4886 |
|
4887 octave_value tc = sr->def (); |
604
|
4888 |
1755
|
4889 if (tc.is_undefined ()) |
604
|
4890 return; |
|
4891 |
|
4892 switch (fmt) |
|
4893 { |
|
4894 case LS_ASCII: |
3738
|
4895 save_ascii_data (os, tc, name, infnan_warned, false, global, 0); |
604
|
4896 break; |
|
4897 |
|
4898 case LS_BINARY: |
630
|
4899 save_binary_data (os, tc, name, help, global, save_as_floats); |
604
|
4900 break; |
|
4901 |
667
|
4902 case LS_MAT_BINARY: |
|
4903 save_mat_binary_data (os, tc, name); |
|
4904 break; |
|
4905 |
3687
|
4906 #ifdef HAVE_HDF5 |
|
4907 case LS_HDF5: |
|
4908 save_hdf5_data (os, tc, name, help, global, save_as_floats); |
|
4909 break; |
|
4910 #endif /* HAVE_HDF5 */ |
|
4911 |
3688
|
4912 case LS_MAT5_BINARY: |
|
4913 save_mat5_binary_element (os, tc, name, global, save_as_floats); |
|
4914 break; |
|
4915 |
604
|
4916 default: |
775
|
4917 gripe_unrecognized_data_fmt ("save"); |
604
|
4918 break; |
|
4919 } |
|
4920 } |
|
4921 |
|
4922 // Save variables with names matching PATTERN on stream OS in the |
3019
|
4923 // format specified by FMT. If SAVE_BUILTINS is TRUE, also save |
604
|
4924 // builtin variables with names that match PATTERN. |
|
4925 |
|
4926 static int |
3523
|
4927 save_vars (std::ostream& os, const std::string& pattern, bool save_builtins, |
630
|
4928 load_save_format fmt, int save_as_floats) |
604
|
4929 { |
3355
|
4930 Array<symbol_record *> vars = curr_sym_tab->glob |
|
4931 (pattern, symbol_record::USER_VARIABLE, SYMTAB_ALL_SCOPES); |
|
4932 |
|
4933 int saved = vars.length (); |
|
4934 |
3738
|
4935 bool infnan_warned = false; |
|
4936 |
3355
|
4937 for (int i = 0; i < saved; i++) |
620
|
4938 { |
3738
|
4939 do_save (os, vars (i), fmt, save_as_floats, infnan_warned); |
620
|
4940 |
|
4941 if (error_state) |
|
4942 break; |
|
4943 } |
604
|
4944 |
620
|
4945 if (! error_state && save_builtins) |
604
|
4946 { |
4009
|
4947 vars = fbi_sym_tab->glob |
3355
|
4948 (pattern, symbol_record::BUILTIN_VARIABLE, SYMTAB_ALL_SCOPES); |
|
4949 |
|
4950 int count = vars.length (); |
604
|
4951 |
|
4952 saved += count; |
|
4953 |
3355
|
4954 for (int i = 0; i < count; i++) |
620
|
4955 { |
3738
|
4956 do_save (os, vars (i), fmt, save_as_floats, infnan_warned); |
620
|
4957 |
|
4958 if (error_state) |
|
4959 break; |
|
4960 } |
604
|
4961 } |
|
4962 |
|
4963 return saved; |
|
4964 } |
|
4965 |
|
4966 static load_save_format |
|
4967 get_default_save_format (void) |
|
4968 { |
|
4969 load_save_format retval = LS_ASCII; |
|
4970 |
3523
|
4971 std::string fmt = Vdefault_save_format; |
1755
|
4972 |
|
4973 if (fmt == "binary") |
604
|
4974 retval = LS_BINARY; |
1755
|
4975 else if (fmt == "mat-binary" || fmt =="mat_binary") |
3688
|
4976 retval = LS_MAT5_BINARY; |
|
4977 else if (fmt == "mat4-binary" || fmt =="mat4_binary") |
911
|
4978 retval = LS_MAT_BINARY; |
3687
|
4979 #ifdef HAVE_HDF5 |
|
4980 else if (fmt == "hdf5") |
|
4981 retval = LS_HDF5; |
|
4982 #endif /* HAVE_HDF5 */ |
604
|
4983 |
|
4984 return retval; |
|
4985 } |
|
4986 |
4329
|
4987 void |
3523
|
4988 write_header (std::ostream& os, load_save_format format) |
863
|
4989 { |
3185
|
4990 switch (format) |
863
|
4991 { |
3185
|
4992 case LS_BINARY: |
|
4993 { |
|
4994 os << (oct_mach_info::words_big_endian () |
|
4995 ? "Octave-1-B" : "Octave-1-L"); |
|
4996 |
|
4997 oct_mach_info::float_format flt_fmt = |
|
4998 oct_mach_info::native_float_format (); |
|
4999 |
|
5000 char tmp = (char) float_format_to_mopt_digit (flt_fmt); |
|
5001 |
3557
|
5002 os.write (X_CAST (char *, &tmp), 1); |
3185
|
5003 } |
3688
|
5004 break; |
|
5005 |
|
5006 case LS_MAT5_BINARY: |
|
5007 { |
3775
|
5008 char const * versionmagic; |
3688
|
5009 TWO_BYTE_INT number = *(TWO_BYTE_INT *)"\x00\x01"; |
|
5010 struct tm bdt; |
|
5011 time_t now; |
|
5012 char headertext[128]; |
|
5013 |
|
5014 time (&now); |
|
5015 bdt = *gmtime (&now); |
|
5016 memset (headertext, ' ', 124); |
|
5017 // ISO 8601 format date |
|
5018 strftime (headertext, 124, "MATLAB 5.0 MAT-file, written by Octave " |
|
5019 OCTAVE_VERSION ", %Y-%m-%d %T UTC", &bdt); |
|
5020 |
|
5021 // The first pair of bytes give the version of the MAT file |
|
5022 // format. The second pair of bytes form a magic number which |
|
5023 // signals a MAT file. MAT file data are always written in |
|
5024 // native byte order. The order of the bytes in the second |
|
5025 // pair indicates whether the file was written by a big- or |
|
5026 // little-endian machine. However, the version number is |
|
5027 // written in the *opposite* byte order from everything else! |
|
5028 if (number == 1) |
|
5029 versionmagic = "\x01\x00\x4d\x49"; // this machine is big endian |
|
5030 else |
|
5031 versionmagic = "\x00\x01\x49\x4d"; // this machine is little endian |
|
5032 |
|
5033 memcpy (headertext+124, versionmagic, 4); |
|
5034 os.write (headertext, 128); |
|
5035 } |
|
5036 |
|
5037 break; |
3185
|
5038 |
3687
|
5039 #ifdef HAVE_HDF5 |
|
5040 case LS_HDF5: |
|
5041 #endif /* HAVE_HDF5 */ |
3185
|
5042 case LS_ASCII: |
|
5043 { |
3709
|
5044 octave_localtime now; |
|
5045 |
|
5046 std::string comment_string = now.strftime (Vsave_header_format_string); |
|
5047 |
|
5048 if (! comment_string.empty ()) |
|
5049 { |
3687
|
5050 #ifdef HAVE_HDF5 |
3709
|
5051 if (format == LS_HDF5) |
|
5052 { |
|
5053 hdf5_ofstream& hs = (hdf5_ofstream&) os; |
|
5054 H5Gset_comment (hs.file_id, "/", comment_string.c_str ()); |
|
5055 } |
|
5056 else |
3687
|
5057 #endif /* HAVE_HDF5 */ |
3709
|
5058 os << comment_string << "\n"; |
3687
|
5059 } |
3185
|
5060 } |
|
5061 break; |
|
5062 |
|
5063 default: |
|
5064 break; |
863
|
5065 } |
|
5066 } |
|
5067 |
|
5068 static void |
3769
|
5069 save_vars (const string_vector& argv, int argv_idx, int argc, |
3523
|
5070 std::ostream& os, bool save_builtins, load_save_format fmt, |
3185
|
5071 bool save_as_floats, bool write_header_info) |
863
|
5072 { |
3185
|
5073 if (write_header_info) |
|
5074 write_header (os, fmt); |
863
|
5075 |
1755
|
5076 if (argv_idx == argc) |
863
|
5077 { |
|
5078 save_vars (os, "*", save_builtins, fmt, save_as_floats); |
|
5079 } |
|
5080 else |
|
5081 { |
1755
|
5082 for (int i = argv_idx; i < argc; i++) |
863
|
5083 { |
1755
|
5084 if (! save_vars (os, argv[i], save_builtins, fmt, save_as_floats)) |
863
|
5085 { |
1755
|
5086 warning ("save: no such variable `%s'", argv[i].c_str ()); |
863
|
5087 } |
|
5088 } |
|
5089 } |
|
5090 } |
|
5091 |
1380
|
5092 void |
|
5093 save_user_variables (void) |
|
5094 { |
3189
|
5095 if (Vcrash_dumps_octave_core) |
1380
|
5096 { |
3189
|
5097 // XXX FIXME XXX -- should choose better file name? |
|
5098 |
|
5099 const char *fname = "octave-core"; |
|
5100 |
|
5101 message (0, "attempting to save variables to `%s'...", fname); |
|
5102 |
|
5103 load_save_format format = get_default_save_format (); |
|
5104 |
3775
|
5105 std::ios::openmode mode = std::ios::out|std::ios::trunc; |
3688
|
5106 if (format == LS_BINARY || |
|
5107 format == LS_MAT_BINARY || |
|
5108 format == LS_MAT5_BINARY) |
3552
|
5109 mode |= std::ios::binary; |
3189
|
5110 |
3687
|
5111 #ifdef HAVE_HDF5 |
|
5112 if (format == LS_HDF5) |
3189
|
5113 { |
3687
|
5114 hdf5_ofstream file (fname); |
|
5115 |
|
5116 if (file.file_id >= 0) |
|
5117 { |
|
5118 save_vars (string_vector (), 0, 0, file, |
|
5119 false, format, false, true); |
|
5120 |
|
5121 message (0, "save to `%s' complete", fname); |
|
5122 |
|
5123 file.close (); |
|
5124 } |
|
5125 else |
|
5126 warning ("unable to open `%s' for writing...", fname); |
3189
|
5127 } |
|
5128 else |
3687
|
5129 #endif /* HAVE_HDF5 */ |
|
5130 // don't insert any commands here! The open brace below must |
|
5131 // go with the else above! |
|
5132 { |
|
5133 std::ofstream file (fname, mode); |
|
5134 |
|
5135 if (file) |
|
5136 { |
|
5137 save_vars (string_vector (), 0, 0, file, |
|
5138 false, format, false, true); |
|
5139 message (0, "save to `%s' complete", fname); |
|
5140 file.close (); |
|
5141 } |
|
5142 else |
|
5143 warning ("unable to open `%s' for writing...", fname); |
|
5144 } |
1380
|
5145 } |
|
5146 } |
|
5147 |
4208
|
5148 DEFCMD (save, args, , |
3372
|
5149 "-*- texinfo -*-\n\ |
|
5150 @deffn {Command} save options file v1 v2 @dots{}\n\ |
|
5151 Save the named variables @var{v1}, @var{v2}, @dots{} in the file\n\ |
|
5152 @var{file}. The special filename @samp{-} can be used to write the\n\ |
|
5153 output to your terminal. If no variable names are listed, Octave saves\n\ |
|
5154 all the variables in the current scope. Valid options for the\n\ |
|
5155 @code{save} command are listed in the following table. Options that\n\ |
|
5156 modify the output format override the format specified by the built-in\n\ |
|
5157 variable @code{default_save_format}.\n\ |
|
5158 \n\ |
|
5159 @table @code\n\ |
|
5160 @item -ascii\n\ |
|
5161 Save the data in Octave's text data format.\n\ |
|
5162 \n\ |
|
5163 @item -binary\n\ |
|
5164 Save the data in Octave's binary data format.\n\ |
|
5165 \n\ |
|
5166 @item -float-binary\n\ |
|
5167 Save the data in Octave's binary data format but only using single\n\ |
|
5168 precision. You should use this format only if you know that all the\n\ |
|
5169 values to be saved can be represented in single precision.\n\ |
|
5170 \n\ |
|
5171 @item -mat-binary\n\ |
|
5172 Save the data in @sc{Matlab}'s binary data format.\n\ |
|
5173 \n\ |
3688
|
5174 @item -mat4-binary\n\ |
|
5175 Save the data in the binary format written by @sc{Matlab} version 4.\n\ |
|
5176 \n\ |
3687
|
5177 @item -hdf5\n\ |
|
5178 Save the data in HDF5 format.\n\ |
|
5179 (HDF5 is a free, portable binary format developed by the National\n\ |
|
5180 Center for Supercomputing Applications at the University of Illinois.)\n" |
|
5181 |
|
5182 HAVE_HDF5_HELP_STRING |
|
5183 |
|
5184 "\n\ |
|
5185 @item -float-hdf5\n\ |
|
5186 Save the data in HDF5 format but only using single precision.\n\ |
|
5187 You should use this format only if you know that all the\n\ |
|
5188 values to be saved can be represented in single precision.\n\ |
|
5189 \n\ |
3372
|
5190 @item -save-builtins\n\ |
|
5191 Force Octave to save the values of built-in variables too. By default,\n\ |
|
5192 Octave does not save built-in variables.\n\ |
|
5193 @end table\n\ |
604
|
5194 \n\ |
3372
|
5195 The list of variables to save may include wildcard patterns containing\n\ |
|
5196 the following special characters:\n\ |
|
5197 @table @code\n\ |
|
5198 @item ?\n\ |
|
5199 Match any single character.\n\ |
|
5200 \n\ |
|
5201 @item *\n\ |
|
5202 Match zero or more characters.\n\ |
|
5203 \n\ |
|
5204 @item [ @var{list} ]\n\ |
|
5205 Match the list of characters specified by @var{list}. If the first\n\ |
|
5206 character is @code{!} or @code{^}, match all characters except those\n\ |
|
5207 specified by @var{list}. For example, the pattern @samp{[a-zA-Z]} will\n\ |
|
5208 match all lower and upper case alphabetic characters. \n\ |
|
5209 @end table\n\ |
|
5210 \n\ |
|
5211 Except when using the @sc{Matlab} binary data file format, saving global\n\ |
|
5212 variables also saves the global status of the variable, so that if it is\n\ |
|
5213 restored at a later time using @samp{load}, it will be restored as a\n\ |
|
5214 global variable.\n\ |
|
5215 \n\ |
|
5216 The command\n\ |
|
5217 \n\ |
|
5218 @example\n\ |
|
5219 save -binary data a b*\n\ |
|
5220 @end example\n\ |
|
5221 \n\ |
|
5222 @noindent\n\ |
|
5223 saves the variable @samp{a} and all variables beginning with @samp{b} to\n\ |
|
5224 the file @file{data} in Octave's binary format.\n\ |
|
5225 @end deffn") |
604
|
5226 { |
2086
|
5227 octave_value_list retval; |
604
|
5228 |
1755
|
5229 int argc = args.length () + 1; |
|
5230 |
1968
|
5231 string_vector argv = args.make_argv ("save"); |
1755
|
5232 |
|
5233 if (error_state) |
|
5234 return retval; |
604
|
5235 |
1358
|
5236 // Here is where we would get the default save format if it were |
|
5237 // stored in a user preference variable. |
604
|
5238 |
3019
|
5239 bool save_builtins = false; |
|
5240 |
|
5241 bool save_as_floats = false; |
630
|
5242 |
604
|
5243 load_save_format format = get_default_save_format (); |
|
5244 |
3185
|
5245 bool append = false; |
|
5246 |
1755
|
5247 int i; |
|
5248 for (i = 1; i < argc; i++) |
604
|
5249 { |
3185
|
5250 if (argv[i] == "-append") |
|
5251 { |
|
5252 append = true; |
|
5253 } |
3465
|
5254 else if (argv[i] == "-ascii" || argv[i] == "-a") |
604
|
5255 { |
|
5256 format = LS_ASCII; |
|
5257 } |
1755
|
5258 else if (argv[i] == "-binary" || argv[i] == "-b") |
604
|
5259 { |
|
5260 format = LS_BINARY; |
|
5261 } |
3687
|
5262 else if (argv[i] == "-hdf5" || argv[i] == "-h") |
|
5263 { |
|
5264 #ifdef HAVE_HDF5 |
|
5265 format = LS_HDF5; |
|
5266 #else /* ! HAVE_HDF5 */ |
|
5267 error ("save: octave executable was not linked with HDF5 library"); |
|
5268 return retval; |
|
5269 #endif /* ! HAVE_HDF5 */ |
|
5270 } |
1755
|
5271 else if (argv[i] == "-mat-binary" || argv[i] == "-m") |
667
|
5272 { |
3688
|
5273 format = LS_MAT5_BINARY; |
|
5274 } |
3797
|
5275 else if (argv[i] == "-mat4-binary" || argv[i] == "-4" || argv[i] == "-v4") |
3688
|
5276 { |
667
|
5277 format = LS_MAT_BINARY; |
|
5278 } |
1755
|
5279 else if (argv[i] == "-float-binary" || argv[i] == "-f") |
630
|
5280 { |
|
5281 format = LS_BINARY; |
3019
|
5282 save_as_floats = true; |
630
|
5283 } |
3687
|
5284 else if (argv[i] == "-float-hdf5") |
|
5285 { |
|
5286 #ifdef HAVE_HDF5 |
|
5287 format = LS_HDF5; |
|
5288 save_as_floats = true; |
|
5289 #else /* ! HAVE_HDF5 */ |
|
5290 error ("save: octave executable was not linked with HDF5 library"); |
|
5291 return retval; |
|
5292 #endif /* ! HAVE_HDF5 */ |
|
5293 } |
1755
|
5294 else if (argv[i] == "-save-builtins") |
604
|
5295 { |
3019
|
5296 save_builtins = true; |
604
|
5297 } |
|
5298 else |
|
5299 break; |
|
5300 } |
|
5301 |
2057
|
5302 if (i == argc) |
604
|
5303 { |
|
5304 print_usage ("save"); |
|
5305 return retval; |
|
5306 } |
|
5307 |
630
|
5308 if (save_as_floats && format == LS_ASCII) |
|
5309 { |
|
5310 error ("save: cannot specify both -ascii and -float-binary"); |
|
5311 return retval; |
|
5312 } |
|
5313 |
1755
|
5314 if (argv[i] == "-") |
604
|
5315 { |
1755
|
5316 i++; |
863
|
5317 |
3687
|
5318 #ifdef HAVE_HDF5 |
|
5319 if (format == LS_HDF5) |
|
5320 error ("load: cannot write HDF5 format to stdout"); |
|
5321 else |
|
5322 #endif /* HAVE_HDF5 */ |
|
5323 // don't insert any commands here! the brace below must go |
|
5324 // with the "else" above! |
|
5325 { |
|
5326 // XXX FIXME XXX -- should things intended for the screen end up |
|
5327 // in a octave_value (string)? |
|
5328 |
|
5329 save_vars (argv, i, argc, octave_stdout, save_builtins, format, |
|
5330 save_as_floats, true); |
|
5331 } |
604
|
5332 } |
1755
|
5333 |
|
5334 // Guard against things like `save a*', which are probably mistakes... |
|
5335 |
|
5336 else if (i == argc - 1 && glob_pattern_p (argv[i])) |
|
5337 { |
|
5338 print_usage ("save"); |
604
|
5339 return retval; |
|
5340 } |
|
5341 else |
|
5342 { |
3523
|
5343 std::string fname = file_ops::tilde_expand (argv[i]); |
1755
|
5344 |
|
5345 i++; |
604
|
5346 |
3775
|
5347 std::ios::openmode mode = std::ios::out; |
3688
|
5348 if (format == LS_BINARY || |
|
5349 format == LS_MAT_BINARY || |
|
5350 format == LS_MAT5_BINARY) |
3552
|
5351 mode |= std::ios::binary; |
3538
|
5352 |
|
5353 mode |= append ? std::ios::ate : std::ios::trunc; |
3185
|
5354 |
3687
|
5355 #ifdef HAVE_HDF5 |
|
5356 if (format == LS_HDF5) |
863
|
5357 { |
3687
|
5358 hdf5_ofstream hdf5_file (fname.c_str ()); |
|
5359 |
|
5360 if (hdf5_file.file_id >= 0) { |
|
5361 save_vars (argv, i, argc, hdf5_file, save_builtins, format, |
|
5362 save_as_floats, true); |
|
5363 |
|
5364 hdf5_file.close (); |
|
5365 } |
|
5366 else |
|
5367 { |
|
5368 error ("save: couldn't open output file `%s'", fname.c_str ()); |
|
5369 return retval; |
|
5370 } |
863
|
5371 } |
|
5372 else |
3687
|
5373 #endif /* HAVE_HDF5 */ |
|
5374 // don't insert any statements here! The brace below must go |
|
5375 // with the "else" above! |
604
|
5376 { |
3687
|
5377 std::ofstream file (fname.c_str (), mode); |
|
5378 |
|
5379 if (file) |
|
5380 { |
|
5381 bool write_header_info |
3775
|
5382 = ((file.rdbuf ())->pubseekoff (0, std::ios::cur) |
3769
|
5383 == static_cast<std::streampos> (0)); |
3687
|
5384 |
|
5385 save_vars (argv, i, argc, file, save_builtins, format, |
|
5386 save_as_floats, write_header_info); |
|
5387 } |
|
5388 else |
|
5389 { |
|
5390 error ("save: couldn't open output file `%s'", fname.c_str ()); |
|
5391 return retval; |
|
5392 } |
604
|
5393 } |
|
5394 } |
|
5395 |
|
5396 return retval; |
|
5397 } |
|
5398 |
|
5399 // Maybe this should be a static function in tree-plot.cc? |
|
5400 |
620
|
5401 // If TC is matrix, save it on stream OS in a format useful for |
604
|
5402 // making a 3-dimensional plot with gnuplot. If PARAMETRIC is |
3019
|
5403 // TRUE, assume a parametric 3-dimensional plot will be generated. |
604
|
5404 |
2799
|
5405 bool |
3523
|
5406 save_three_d (std::ostream& os, const octave_value& tc, bool parametric) |
604
|
5407 { |
3019
|
5408 bool fail = false; |
604
|
5409 |
620
|
5410 int nr = tc.rows (); |
|
5411 int nc = tc.columns (); |
|
5412 |
|
5413 if (tc.is_real_matrix ()) |
604
|
5414 { |
4060
|
5415 os << "# 3D data...\n" |
|
5416 << "# type: matrix\n" |
|
5417 << "# total rows: " << nr << "\n" |
|
5418 << "# total columns: " << nc << "\n"; |
604
|
5419 |
|
5420 if (parametric) |
|
5421 { |
|
5422 int extras = nc % 3; |
|
5423 if (extras) |
|
5424 warning ("ignoring last %d columns", extras); |
|
5425 |
620
|
5426 Matrix tmp = tc.matrix_value (); |
872
|
5427 tmp = strip_infnan (tmp); |
|
5428 nr = tmp.rows (); |
|
5429 |
604
|
5430 for (int i = 0; i < nc-extras; i += 3) |
|
5431 { |
|
5432 os << tmp.extract (0, i, nr-1, i+2); |
|
5433 if (i+3 < nc-extras) |
|
5434 os << "\n"; |
|
5435 } |
|
5436 } |
|
5437 else |
|
5438 { |
620
|
5439 Matrix tmp = tc.matrix_value (); |
872
|
5440 tmp = strip_infnan (tmp); |
|
5441 nr = tmp.rows (); |
|
5442 |
604
|
5443 for (int i = 0; i < nc; i++) |
|
5444 { |
|
5445 os << tmp.extract (0, i, nr-1, i); |
|
5446 if (i+1 < nc) |
|
5447 os << "\n"; |
|
5448 } |
|
5449 } |
620
|
5450 } |
|
5451 else |
|
5452 { |
604
|
5453 ::error ("for now, I can only save real matrices in 3D format"); |
3019
|
5454 fail = true; |
604
|
5455 } |
620
|
5456 |
|
5457 return (os && ! fail); |
604
|
5458 } |
|
5459 |
2194
|
5460 static int |
3189
|
5461 crash_dumps_octave_core (void) |
|
5462 { |
|
5463 Vcrash_dumps_octave_core = check_preference ("crash_dumps_octave_core"); |
4449
|
5464 |
3189
|
5465 return 0; |
|
5466 } |
|
5467 |
|
5468 static int |
2194
|
5469 default_save_format (void) |
|
5470 { |
|
5471 int status = 0; |
|
5472 |
3523
|
5473 std::string s = builtin_string_variable ("default_save_format"); |
2194
|
5474 |
|
5475 if (s.empty ()) |
|
5476 { |
|
5477 gripe_invalid_value_specified ("default_save_format"); |
|
5478 status = -1; |
|
5479 } |
|
5480 else |
|
5481 Vdefault_save_format = s; |
|
5482 |
|
5483 return status; |
|
5484 } |
|
5485 |
3769
|
5486 static std::string |
3709
|
5487 default_save_header_format (void) |
|
5488 { |
|
5489 return |
4060
|
5490 std::string ("# Created by Octave " OCTAVE_VERSION ", %a %b %d %H:%M:%S %Y %Z <") |
3709
|
5491 + octave_env::get_user_name () |
|
5492 + std::string ("@") |
|
5493 + octave_env::get_host_name () |
|
5494 + std::string (">"); |
|
5495 } |
|
5496 |
|
5497 static int |
|
5498 save_header_format_string (void) |
|
5499 { |
|
5500 int status = 0; |
|
5501 |
|
5502 octave_value v = builtin_any_variable ("save_header_format_string"); |
|
5503 |
|
5504 if (v.is_string ()) |
|
5505 Vsave_header_format_string = v.string_value (); |
|
5506 else |
|
5507 { |
|
5508 gripe_invalid_value_specified ("save_header_format_string"); |
|
5509 status = -1; |
|
5510 } |
|
5511 |
|
5512 return status; |
|
5513 } |
|
5514 |
2194
|
5515 static int |
|
5516 save_precision (void) |
|
5517 { |
|
5518 double val; |
|
5519 if (builtin_real_scalar_variable ("save_precision", val) |
|
5520 && ! xisnan (val)) |
|
5521 { |
|
5522 int ival = NINT (val); |
2800
|
5523 if (ival >= 0 && ival == val) |
2194
|
5524 { |
|
5525 Vsave_precision = ival; |
|
5526 return 0; |
|
5527 } |
|
5528 } |
|
5529 gripe_invalid_value_specified ("save_precision"); |
|
5530 return -1; |
|
5531 } |
|
5532 |
|
5533 void |
|
5534 symbols_of_load_save (void) |
|
5535 { |
4233
|
5536 DEFVAR (crash_dumps_octave_core, true, crash_dumps_octave_core, |
3372
|
5537 "-*- texinfo -*-\n\ |
|
5538 @defvr {Built-in Variable} crash_dumps_octave_core\n\ |
|
5539 If this variable is set to a nonzero value, Octave tries to save all\n\ |
|
5540 current variables the the file \"octave-core\" if it crashes or receives a\n\ |
|
5541 hangup, terminate or similar signal. The default value is 1.\n\ |
|
5542 @end defvr"); |
3189
|
5543 |
3258
|
5544 DEFVAR (default_save_format, "ascii", default_save_format, |
3372
|
5545 "-*- texinfo -*-\n\ |
|
5546 @defvr {Built-in Variable} default_save_format\n\ |
|
5547 This variable specifies the default format for the @code{save} command.\n\ |
|
5548 It should have one of the following values: @code{\"ascii\"},\n\ |
|
5549 @code{\"binary\"}, @code{float-binary}, or @code{\"mat-binary\"}. The\n\ |
|
5550 initial default save format is Octave's text format.\n\ |
|
5551 @end defvr"); |
2194
|
5552 |
3709
|
5553 DEFVAR (save_header_format_string, default_save_header_format (), |
|
5554 save_header_format_string, |
|
5555 "-*- texinfo -*-\n\ |
|
5556 @defvr {Built-in Variable} save_header_format_string\n\ |
|
5557 This variable specifies the the format string for the comment line\n\ |
|
5558 that is written at the beginning of text-format data files saved by\n\ |
|
5559 Octave. The format string is passed to @code{strftime} and should\n\ |
|
5560 begin with the character @samp{#} and contain no newline characters.\n\ |
|
5561 If the value of @code{save_header_format_string} is the empty string,\n\ |
|
5562 the header comment is omitted from text-format data files. The\n\ |
|
5563 default value is\n\ |
|
5564 \n\ |
|
5565 @example\n\ |
4060
|
5566 \"# Created by Octave VERSION, %a %b %d %H:%M:%S %Y %Z <USER@@HOST>\"\n\ |
3709
|
5567 @end example\n\ |
|
5568 @seealso{strftime}\n\ |
|
5569 @end defvr"); |
|
5570 |
3258
|
5571 DEFVAR (save_precision, 15.0, save_precision, |
3372
|
5572 "-*- texinfo -*-\n\ |
|
5573 @defvr {Built-in Variable} save_precision\n\ |
|
5574 This variable specifies the number of digits to keep when saving data in\n\ |
|
5575 text format. The default value is 17.\n\ |
|
5576 @end defvr"); |
2194
|
5577 } |
|
5578 |
604
|
5579 /* |
|
5580 ;;; Local Variables: *** |
|
5581 ;;; mode: C++ *** |
|
5582 ;;; End: *** |
|
5583 */ |