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