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