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