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 |
3695
|
1310 // This variable, set in read_hdf5_data(), tells whether we are using |
|
1311 // a version of HDF5 with a buggy H5Giterate (i.e. which neglects to |
|
1312 // increment the index parameter to the next unread item). |
|
1313 static bool have_h5giterate_bug = false; |
|
1314 |
3687
|
1315 // This function is designed to be passed to H5Giterate, which calls it |
|
1316 // on each data item in an HDF5 file. For the item whose name is NAME in |
|
1317 // the group GROUP_ID, this function sets dv->tc to an Octave representation |
|
1318 // of that item. (dv must be a pointer to hdf5_callback_data.) (It also |
|
1319 // sets the other fields of dv). |
|
1320 // |
|
1321 // It returns 1 on success (in which case H5Giterate stops and returns), |
|
1322 // -1 on error, and 0 to tell H5Giterate to continue on to the next item |
|
1323 // (e.g. if NAME was a data type we don't recognize). |
|
1324 |
|
1325 static herr_t |
|
1326 hdf5_read_next_data (hid_t group_id, const char *name, void *dv) |
|
1327 { |
|
1328 hdf5_callback_data *d = (hdf5_callback_data *) dv; |
|
1329 H5G_stat_t info; |
|
1330 herr_t retval = 0; |
|
1331 bool ident_valid = valid_identifier (name); |
|
1332 char *vname; |
|
1333 |
|
1334 vname = new char[strlen (name) + 1]; |
|
1335 |
|
1336 strcpy (vname, name); |
|
1337 |
3688
|
1338 if (! ident_valid && d->import) |
3687
|
1339 { |
|
1340 // fix the identifier, replacing invalid chars with underscores |
|
1341 make_valid_identifier (vname); |
|
1342 |
|
1343 // check again (in case vname was null, empty, or some such thing): |
|
1344 ident_valid = valid_identifier (vname); |
|
1345 } |
|
1346 |
|
1347 H5Gget_objinfo (group_id, name, 1, &info); |
|
1348 |
|
1349 if (info.type == H5G_DATASET && ident_valid) |
|
1350 { |
|
1351 retval = 1; |
|
1352 |
|
1353 hid_t data_id = H5Dopen (group_id, name); |
|
1354 |
|
1355 if (data_id < 0) |
|
1356 { |
|
1357 retval = data_id; |
|
1358 |
|
1359 goto done; |
|
1360 } |
|
1361 |
|
1362 hid_t type_id = H5Dget_type (data_id); |
|
1363 |
|
1364 hid_t type_class_id = H5Tget_class (type_id); |
|
1365 |
|
1366 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS |
|
1367 if (type_class_id == H5T_INTEGER || type_class_id == H5T_FLOAT) |
|
1368 { |
|
1369 #else |
|
1370 // hdf5 doesn't (yet) support automatic float/integer conversions |
|
1371 if (type_class_id == H5T_FLOAT) |
|
1372 { |
|
1373 #endif |
|
1374 // read real matrix or scalar variable |
|
1375 |
|
1376 hid_t space_id = H5Dget_space (data_id); |
|
1377 |
|
1378 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
1379 |
|
1380 if (rank == 0) |
|
1381 { |
|
1382 // real scalar: |
|
1383 double dtmp; |
|
1384 if (H5Dread (data_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, |
|
1385 H5P_DEFAULT, (void *) &dtmp) < 0) |
|
1386 retval = -1; // error |
|
1387 else |
|
1388 d->tc = dtmp; |
|
1389 } |
|
1390 else if (rank > 0 && rank <= 2) |
|
1391 { |
|
1392 // real matrix |
|
1393 hsize_t *dims = new hsize_t[rank]; |
|
1394 hsize_t *maxdims = new hsize_t[rank]; |
|
1395 |
|
1396 H5Sget_simple_extent_dims (space_id, dims, maxdims); |
|
1397 |
|
1398 int nr, nc; // rows and columns |
|
1399 // octave uses column-major & HDF5 uses row-major |
|
1400 nc = dims[0]; |
|
1401 nr = rank > 1 ? dims[1] : 1; |
|
1402 |
|
1403 delete [] dims; |
|
1404 delete [] maxdims; |
|
1405 |
|
1406 Matrix m (nr, nc); |
|
1407 double *re = m.fortran_vec (); |
|
1408 if (H5Dread (data_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, |
|
1409 H5P_DEFAULT, (void *) re) < 0) |
|
1410 retval = -1; // error |
|
1411 else |
|
1412 d->tc = m; |
|
1413 } |
|
1414 else if (rank >= 3 && d->import) |
|
1415 { |
|
1416 hsize_t *dims = new hsize_t[rank]; |
|
1417 hsize_t *maxdims = new hsize_t[rank]; |
|
1418 |
|
1419 H5Sget_simple_extent_dims (space_id, dims, maxdims); |
|
1420 |
|
1421 hssize_t *start = new hssize_t[rank]; |
|
1422 hsize_t *count = new hsize_t[rank]; |
|
1423 |
|
1424 for (hsize_t i = 0; i < rank; ++i) |
|
1425 { |
|
1426 start[i] = 0; |
|
1427 count[i] = 1; |
|
1428 } |
|
1429 count[0] = dims[0]; |
|
1430 count[1] = dims[1]; |
|
1431 retval = hdf5_import_multidim (data_id, space_id, |
|
1432 rank, dims, rank-1, |
|
1433 start, count, |
|
1434 H5T_NATIVE_DOUBLE, d->tc); |
|
1435 |
|
1436 delete [] count; |
|
1437 delete [] start; |
|
1438 delete [] dims; |
|
1439 delete [] maxdims; |
|
1440 } |
|
1441 else |
|
1442 { |
|
1443 warning ("load: can't read %d-dim. hdf5 dataset %s", |
|
1444 rank, name); |
|
1445 retval = 0; // skip; we can't read 3+ dimensional datasets |
|
1446 } |
|
1447 |
|
1448 H5Sclose (space_id); |
|
1449 } |
|
1450 else if (type_class_id == H5T_STRING) |
|
1451 { |
|
1452 // read string variable |
|
1453 hid_t space_id = H5Dget_space (data_id); |
|
1454 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
1455 |
|
1456 if (rank == 0) |
|
1457 { |
|
1458 // a single string: |
|
1459 int slen = H5Tget_size (type_id); |
|
1460 if (slen < 0) |
|
1461 retval = -1; // error |
|
1462 else |
|
1463 { |
|
1464 char *s = new char [slen]; |
|
1465 // create datatype for (null-terminated) string |
|
1466 // to read into: |
|
1467 hid_t st_id = H5Tcopy (H5T_C_S1); |
|
1468 H5Tset_size (st_id, slen); |
|
1469 if (H5Dread (data_id, st_id, H5S_ALL, H5S_ALL, |
|
1470 H5P_DEFAULT, (void *) s) < 0) |
|
1471 { |
|
1472 delete [] s; |
|
1473 retval = -1; // error |
|
1474 } |
|
1475 else |
|
1476 d->tc = s; |
|
1477 |
|
1478 H5Tclose (st_id); |
|
1479 } |
|
1480 } |
|
1481 else if (rank == 1) |
|
1482 { |
|
1483 // string vector |
|
1484 hsize_t elements, maxdim; |
|
1485 H5Sget_simple_extent_dims (space_id, &elements, &maxdim); |
|
1486 int slen = H5Tget_size (type_id); |
|
1487 if (slen < 0) |
|
1488 retval = -1; // error |
|
1489 else |
|
1490 { |
|
1491 // hdf5 string arrays store strings of all the |
|
1492 // same physical length (I think), which is |
|
1493 // slightly wasteful, but oh well. |
|
1494 |
|
1495 char *s = new char [elements * slen]; |
|
1496 // create datatype for (null-terminated) string |
|
1497 // to read into: |
|
1498 hid_t st_id = H5Tcopy (H5T_C_S1); |
|
1499 H5Tset_size (st_id, slen); |
|
1500 |
|
1501 if (H5Dread (data_id, st_id, H5S_ALL, H5S_ALL, |
|
1502 H5P_DEFAULT, (void *) s) < 0) |
|
1503 retval = -1; // error |
|
1504 else |
|
1505 { |
|
1506 charMatrix chm (elements, slen - 1); |
|
1507 for (hsize_t i = 0; i < elements; ++i) |
|
1508 { |
|
1509 chm.insert (s + i*slen, i, 0); |
|
1510 } |
|
1511 d->tc = octave_value (chm, true); |
|
1512 } |
|
1513 |
|
1514 delete [] s; |
|
1515 |
|
1516 H5Tclose (st_id); |
|
1517 } |
|
1518 } |
|
1519 else |
|
1520 { |
|
1521 warning ("load: can't read %d-dim. hdf5 string vector %s", |
|
1522 rank, name); |
|
1523 // skip; we can't read higher-dimensional string vectors |
|
1524 retval = 0; |
|
1525 } |
|
1526 } |
|
1527 else if (type_class_id == H5T_COMPOUND) |
|
1528 { |
|
1529 // check for complex or range data: |
|
1530 |
|
1531 if (hdf5_types_compatible (type_id, d->complex_type)) |
|
1532 { |
|
1533 // read complex matrix or scalar variable |
|
1534 |
|
1535 hid_t space_id = H5Dget_space (data_id); |
|
1536 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
1537 |
|
1538 if (rank == 0) |
|
1539 { |
|
1540 // complex scalar: |
|
1541 Complex ctmp; |
|
1542 if (H5Dread (data_id, d->complex_type, H5S_ALL, |
|
1543 H5S_ALL, H5P_DEFAULT, |
|
1544 (void *) X_CAST (double *, &ctmp)) < 0) |
|
1545 retval = -1; // error |
|
1546 else |
|
1547 d->tc = ctmp; |
|
1548 } |
|
1549 else if (rank > 0 && rank <= 2) |
|
1550 { |
|
1551 // complex matrix |
|
1552 hsize_t *dims = new hsize_t[rank]; |
|
1553 hsize_t *maxdims = new hsize_t[rank]; |
|
1554 H5Sget_simple_extent_dims (space_id, dims, maxdims); |
|
1555 int nr, nc; // rows and columns |
|
1556 // octave uses column-major & HDF5 uses row-major |
|
1557 nc = dims[0]; |
|
1558 nr = rank > 1 ? dims[1] : 1; |
|
1559 delete [] dims; |
|
1560 delete [] maxdims; |
|
1561 ComplexMatrix m (nr, nc); |
|
1562 Complex *reim = m.fortran_vec (); |
|
1563 if (H5Dread (data_id, d->complex_type, H5S_ALL, |
|
1564 H5S_ALL, H5P_DEFAULT, |
|
1565 (void *) X_CAST (double *, reim)) < 0) |
|
1566 retval = -1; // error |
|
1567 else |
|
1568 d->tc = m; |
|
1569 } |
|
1570 else if (rank >= 3 && d->import) |
|
1571 { |
3691
|
1572 hsize_t *dims = new hsize_t[rank]; |
|
1573 hsize_t *maxdims = new hsize_t[rank]; |
3687
|
1574 H5Sget_simple_extent_dims (space_id, dims, maxdims); |
|
1575 hssize_t *start = new hssize_t[rank]; |
|
1576 hsize_t *count = new hsize_t[rank]; |
|
1577 for (hsize_t i = 0; i < rank; ++i) |
|
1578 { |
|
1579 start[i] = 0; |
|
1580 count[i] = 1; |
|
1581 } |
|
1582 count[0] = dims[0]; |
|
1583 count[1] = dims[1]; |
|
1584 retval = hdf5_import_multidim (data_id, space_id, |
|
1585 rank, dims, rank-1, |
|
1586 start, count, |
|
1587 d->complex_type, |
|
1588 d->tc); |
|
1589 |
|
1590 delete [] count; |
|
1591 delete [] start; |
|
1592 delete [] dims; |
|
1593 delete [] maxdims; |
|
1594 } |
|
1595 else |
|
1596 { |
|
1597 warning ("load: can't read %d-dim. hdf5 dataset %s", |
|
1598 rank, name); |
|
1599 // skip; we can't read 3+ dimensional datasets |
|
1600 retval = 0; |
|
1601 } |
|
1602 H5Sclose (space_id); |
|
1603 } |
|
1604 else if (hdf5_types_compatible (type_id, d->range_type)) |
|
1605 { |
|
1606 // read range variable: |
|
1607 hid_t space_id = H5Dget_space (data_id); |
|
1608 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
1609 |
|
1610 if (rank == 0) |
|
1611 { |
|
1612 double rangevals[3]; |
|
1613 if (H5Dread (data_id, d->range_type, H5S_ALL, H5S_ALL, |
|
1614 H5P_DEFAULT, (void *) rangevals) < 0) |
|
1615 retval = -1; // error |
|
1616 else |
|
1617 { |
|
1618 Range r (rangevals[0], rangevals[1], rangevals[2]); |
|
1619 d->tc = r; |
|
1620 } |
|
1621 } |
|
1622 else |
|
1623 { |
|
1624 warning ("load: can't read range array `%s' in hdf5 file", |
|
1625 name); |
|
1626 // skip; we can't read arrays of range variables |
|
1627 retval = 0; |
|
1628 } |
|
1629 |
|
1630 H5Sclose (space_id); |
|
1631 } |
|
1632 else |
|
1633 { |
|
1634 warning ("load: can't read `%s' (unknown compound datatype)", |
|
1635 name); |
|
1636 retval = 0; // unknown datatype; skip. |
|
1637 } |
|
1638 } |
|
1639 else |
|
1640 { |
|
1641 warning ("load: can't read `%s' (unknown datatype)", name); |
|
1642 retval = 0; // unknown datatype; skip |
|
1643 } |
|
1644 |
|
1645 H5Tclose (type_id); |
|
1646 |
|
1647 // check for OCTAVE_GLOBAL attribute: |
3688
|
1648 d->global = hdf5_check_attr (data_id, "OCTAVE_GLOBAL"); |
3687
|
1649 |
|
1650 H5Dclose (data_id); |
|
1651 } |
|
1652 else if (info.type == H5G_GROUP && ident_valid) |
|
1653 { |
|
1654 // read in group as a list or a structure |
|
1655 retval = 1; |
|
1656 |
|
1657 hid_t subgroup_id = H5Gopen (group_id, name); |
|
1658 |
|
1659 if (subgroup_id < 0) |
|
1660 { |
|
1661 retval = subgroup_id; |
|
1662 goto done; |
|
1663 } |
|
1664 |
|
1665 // an HDF5 group is treated as an octave structure by |
|
1666 // default (since that preserves name information), and an |
|
1667 // octave list otherwise. |
|
1668 |
3688
|
1669 bool is_list = hdf5_check_attr (subgroup_id, "OCTAVE_LIST"); |
3687
|
1670 |
|
1671 hdf5_callback_data dsub; |
|
1672 dsub.name = dsub.doc = (char*) NULL; |
|
1673 dsub.global = 0; |
|
1674 dsub.complex_type = d->complex_type; |
|
1675 dsub.range_type = d->range_type; |
|
1676 dsub.import = d->import; |
|
1677 |
|
1678 herr_t retval2; |
|
1679 octave_value_list lst; |
|
1680 Octave_map m; |
|
1681 int current_item = 0; |
|
1682 while ((retval2 = H5Giterate (group_id, name, ¤t_item, |
|
1683 hdf5_read_next_data, &dsub)) > 0) |
|
1684 { |
|
1685 if (is_list) |
|
1686 lst.append (dsub.tc); |
|
1687 else |
|
1688 m [dsub.name] = dsub.tc; |
|
1689 |
|
1690 if (dsub.name) |
|
1691 delete [] dsub.name; |
|
1692 |
|
1693 if (dsub.doc) |
|
1694 delete [] dsub.doc; |
|
1695 |
3695
|
1696 if (have_h5giterate_bug) |
|
1697 current_item++; // H5Giterate returned the last index processed |
3687
|
1698 } |
|
1699 |
|
1700 if (retval2 < 0) |
|
1701 retval = retval2; |
|
1702 else |
|
1703 { |
3688
|
1704 d->global = hdf5_check_attr (group_id, "OCTAVE_GLOBAL"); |
3687
|
1705 |
|
1706 if (is_list) |
|
1707 d->tc = lst; |
|
1708 else |
|
1709 d->tc = m; |
|
1710 } |
|
1711 |
|
1712 H5Gclose (subgroup_id); |
|
1713 } |
|
1714 else if (! ident_valid) |
|
1715 { |
|
1716 // should we attempt to handle invalid identifiers by converting |
|
1717 // bad characters to '_', say? |
|
1718 warning ("load: skipping invalid identifier `%s' in hdf5 file", |
|
1719 name); |
|
1720 } |
|
1721 |
|
1722 done: |
|
1723 |
|
1724 if (retval < 0) |
|
1725 error ("load: error while reading hdf5 item %s", name); |
|
1726 |
|
1727 if (retval > 0) |
|
1728 { |
|
1729 // get documentation string, if any: |
|
1730 int comment_length = H5Gget_comment (group_id, name, 0, NULL); |
|
1731 |
|
1732 if (comment_length > 1) |
|
1733 { |
|
1734 d->doc = new char[comment_length]; |
|
1735 H5Gget_comment (group_id, name, comment_length, d->doc); |
|
1736 } |
|
1737 else if (strcmp (name, vname) != 0) |
|
1738 { |
|
1739 // the name was changed by import; store the original name |
|
1740 // as the documentation string: |
|
1741 d->doc = new char [strlen (name) + 1]; |
|
1742 strcpy (d->doc, name); |
|
1743 } |
|
1744 else |
|
1745 d->doc = (char *) NULL; |
|
1746 |
|
1747 // copy name (actually, vname): |
|
1748 d->name = new char [strlen (vname) + 1]; |
|
1749 strcpy (d->name, vname); |
|
1750 } |
|
1751 |
|
1752 delete [] vname; |
|
1753 |
|
1754 return retval; |
|
1755 } |
|
1756 |
|
1757 // The following two subroutines create HDF5 representations of the way |
|
1758 // we will store Octave complex and range types (pairs and triplets of |
|
1759 // floating-point numbers, respectively). NUM_TYPE is the HDF5 numeric |
|
1760 // type to use for storage (e.g. H5T_NATIVE_DOUBLE to save as 'double'). |
|
1761 // Note that any necessary conversions are handled automatically by HDF5. |
|
1762 |
|
1763 static hid_t |
|
1764 hdf5_make_complex_type (hid_t num_type) |
|
1765 { |
|
1766 hid_t type_id = H5Tcreate (H5T_COMPOUND, sizeof (double) * 2); |
|
1767 |
|
1768 H5Tinsert (type_id, "real", 0 * sizeof (double), num_type); |
|
1769 H5Tinsert (type_id, "imag", 1 * sizeof (double), num_type); |
|
1770 |
|
1771 return type_id; |
|
1772 } |
|
1773 |
|
1774 static hid_t |
|
1775 hdf5_make_range_type (hid_t num_type) |
|
1776 { |
|
1777 hid_t type_id = H5Tcreate (H5T_COMPOUND, sizeof (double) * 3); |
|
1778 |
|
1779 H5Tinsert (type_id, "base", 0 * sizeof (double), num_type); |
|
1780 H5Tinsert (type_id, "limit", 1 * sizeof (double), num_type); |
|
1781 H5Tinsert (type_id, "increment", 2 * sizeof (double), num_type); |
|
1782 |
|
1783 return type_id; |
|
1784 } |
|
1785 |
|
1786 // Read the next Octave variable from the stream IS, which must really be |
|
1787 // an hdf5_ifstream. Return the variable value in tc, its doc string |
|
1788 // in doc, and whether it is global in global. The return value is |
|
1789 // the name of the variable, or NULL if none were found or there was |
|
1790 // and error. If import is true, we try extra hard to import "foreign" |
|
1791 // datasets (not created by Octave), although we usually do a reasonable |
|
1792 // job anyway. (c.f. load -import documentation.) |
|
1793 static char * |
|
1794 read_hdf5_data (std::istream& is, |
|
1795 const std::string& filename, bool& global, |
|
1796 octave_value& tc, char *&doc, bool import) |
|
1797 { |
|
1798 hdf5_ifstream& hs = (hdf5_ifstream&) is; |
|
1799 hdf5_callback_data d; |
|
1800 |
|
1801 d.name = (char *) NULL; |
|
1802 d.global = 0; |
|
1803 d.doc = (char *) NULL; |
|
1804 d.complex_type = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); |
|
1805 d.range_type = hdf5_make_range_type (H5T_NATIVE_DOUBLE); |
|
1806 d.import = import; |
|
1807 |
3695
|
1808 // Versions of HDF5 prior to 1.2.2 had a bug in H5Giterate where it |
|
1809 // would return the index of the last item processed instead of the |
|
1810 // next item to be processed, forcing us to increment the index manually. |
|
1811 |
|
1812 unsigned int vers_major, vers_minor, vers_release; |
|
1813 |
|
1814 H5get_libversion (&vers_major, &vers_minor, &vers_release); |
|
1815 |
|
1816 // XXX FIXME XXX -- this test looks wrong. |
|
1817 have_h5giterate_bug |
|
1818 = (vers_major < 1 |
|
1819 || (vers_major == 1 && (vers_minor < 2 |
|
1820 || (vers_minor == 2 && vers_release < 2)))); |
|
1821 |
3687
|
1822 herr_t retval = H5Giterate (hs.file_id, "/", &hs.current_item, |
|
1823 hdf5_read_next_data, &d); |
|
1824 |
3695
|
1825 if (have_h5giterate_bug) |
|
1826 { |
|
1827 // H5Giterate sets current_item to the last item processed; we want |
|
1828 // the index of the next item (for the next call to read_hdf5_data) |
|
1829 |
|
1830 hs.current_item++; |
|
1831 } |
3687
|
1832 |
|
1833 if (retval > 0) |
|
1834 { |
|
1835 global = d.global; |
|
1836 tc = d.tc; |
|
1837 doc = d.doc; |
|
1838 } |
|
1839 else |
|
1840 { |
|
1841 // an error occurred (retval < 0) or there are no more datasets |
|
1842 // print an error message if retval < 0? |
|
1843 // hdf5_read_next_data already printed one, probably. |
|
1844 } |
|
1845 |
|
1846 H5Tclose (d.complex_type); |
|
1847 H5Tclose (d.range_type); |
|
1848 |
|
1849 return d.name; |
|
1850 } |
|
1851 |
|
1852 #endif /* HAVE_HDF5 */ |
|
1853 |
3536
|
1854 static std::string |
3523
|
1855 get_mat_data_input_line (std::istream& is) |
3216
|
1856 { |
3523
|
1857 std::string retval; |
3216
|
1858 |
|
1859 bool have_data = false; |
|
1860 |
|
1861 do |
|
1862 { |
|
1863 retval = ""; |
|
1864 |
|
1865 char c; |
|
1866 while (is.get (c)) |
|
1867 { |
|
1868 if (c == '\n') |
|
1869 break; |
|
1870 |
|
1871 if (c == '%' || c == '#') |
|
1872 { |
|
1873 // skip to end of line |
|
1874 while (is.get (c) && c != '\n') |
|
1875 ; |
|
1876 |
|
1877 break; |
|
1878 } |
|
1879 |
|
1880 if (! is.eof ()) |
|
1881 { |
|
1882 if (! have_data && c != ' ' && c != '\t') |
|
1883 have_data = true; |
|
1884 |
|
1885 retval += c; |
|
1886 } |
|
1887 } |
|
1888 } |
|
1889 while (! (have_data || is.eof ())); |
|
1890 |
|
1891 return retval; |
|
1892 } |
|
1893 |
2511
|
1894 static void |
3523
|
1895 get_lines_and_columns (std::istream& is, const std::string& filename, int& nr, int& nc) |
2511
|
1896 { |
3552
|
1897 std::streampos pos = is.tellg (); |
2511
|
1898 |
|
1899 int file_line_number = 0; |
|
1900 |
|
1901 nr = 0; |
|
1902 nc = 0; |
|
1903 |
2795
|
1904 while (is && ! error_state) |
2511
|
1905 { |
3523
|
1906 std::string buf = get_mat_data_input_line (is); |
2511
|
1907 |
|
1908 file_line_number++; |
|
1909 |
2795
|
1910 size_t beg = buf.find_first_not_of (" \t"); |
|
1911 |
|
1912 int tmp_nc = 0; |
|
1913 |
|
1914 while (beg != NPOS) |
2511
|
1915 { |
2795
|
1916 tmp_nc++; |
|
1917 |
|
1918 size_t end = buf.find_first_of (" \t", beg); |
|
1919 |
|
1920 if (end != NPOS) |
|
1921 beg = buf.find_first_not_of (" \t", end); |
|
1922 else |
|
1923 break; |
|
1924 } |
|
1925 |
|
1926 if (tmp_nc > 0) |
|
1927 { |
2511
|
1928 if (nc == 0) |
|
1929 { |
|
1930 nc = tmp_nc; |
|
1931 nr++; |
|
1932 } |
|
1933 else if (nc == tmp_nc) |
|
1934 nr++; |
|
1935 else |
|
1936 error ("load: %s: inconsistent number of columns near line %d", |
|
1937 filename.c_str (), file_line_number); |
|
1938 } |
|
1939 } |
|
1940 |
|
1941 if (nr == 0 || nc == 0) |
|
1942 error ("load: file `%s' seems to be empty!", filename.c_str ()); |
|
1943 |
|
1944 is.clear (); |
3538
|
1945 is.seekg (pos, std::ios::beg); |
2511
|
1946 } |
|
1947 |
|
1948 // Extract a matrix from a file of numbers only. |
|
1949 // |
|
1950 // Comments are not allowed. The file should only have numeric values. |
|
1951 // |
|
1952 // Reads the file twice. Once to find the number of rows and columns, |
|
1953 // and once to extract the matrix. |
|
1954 // |
|
1955 // FILENAME is used for error messages. |
|
1956 // |
|
1957 // This format provides no way to tag the data as global. |
|
1958 |
|
1959 static char * |
3523
|
1960 read_mat_ascii_data (std::istream& is, const std::string& filename, |
2511
|
1961 octave_value& tc) |
|
1962 { |
|
1963 char *name = 0; |
|
1964 |
3523
|
1965 std::string varname; |
2511
|
1966 |
3202
|
1967 size_t pos = filename.rfind ('/'); |
2511
|
1968 |
|
1969 if (pos != NPOS) |
3202
|
1970 varname = filename.substr (pos+1); |
2511
|
1971 else |
|
1972 varname = filename; |
|
1973 |
3202
|
1974 pos = varname.find ('.'); |
|
1975 |
|
1976 if (pos != NPOS) |
|
1977 varname = varname.substr (0, pos); |
|
1978 |
3263
|
1979 if (valid_identifier (varname)) |
2511
|
1980 { |
|
1981 int nr = 0; |
|
1982 int nc = 0; |
|
1983 |
|
1984 get_lines_and_columns (is, filename, nr, nc); |
|
1985 |
2795
|
1986 if (! error_state && nr > 0 && nc > 0) |
2511
|
1987 { |
|
1988 Matrix tmp (nr, nc); |
|
1989 |
3216
|
1990 if (nr < 1 || nc < 1) |
3538
|
1991 is.clear (std::ios::badbit); |
3216
|
1992 else |
|
1993 { |
|
1994 double d; |
|
1995 for (int i = 0; i < nr; i++) |
|
1996 { |
3523
|
1997 std::string buf = get_mat_data_input_line (is); |
|
1998 |
|
1999 std::istrstream tmp_stream (buf.c_str ()); |
3216
|
2000 |
|
2001 for (int j = 0; j < nc; j++) |
|
2002 { |
|
2003 tmp_stream >> d; |
3323
|
2004 |
|
2005 if (tmp_stream) |
3216
|
2006 tmp.elem (i, j) = d; |
|
2007 else |
3323
|
2008 { |
|
2009 error ("load: failed to read matrix from file `%s'", |
|
2010 filename.c_str ()); |
|
2011 |
|
2012 return name; |
|
2013 } |
|
2014 |
3216
|
2015 } |
|
2016 } |
|
2017 } |
|
2018 |
2511
|
2019 if (is) |
|
2020 { |
|
2021 tc = tmp; |
|
2022 |
|
2023 name = strsave (varname.c_str ()); |
|
2024 } |
|
2025 else |
|
2026 error ("load: failed to read matrix from file `%s'", |
|
2027 filename.c_str ()); |
|
2028 } |
|
2029 else |
|
2030 error ("load: unable to extract matrix size from file `%s'", |
|
2031 filename.c_str ()); |
|
2032 } |
|
2033 else |
|
2034 error ("load: unable to convert filename `%s' to valid identifier", |
|
2035 filename.c_str ()); |
|
2036 |
|
2037 return name; |
|
2038 } |
|
2039 |
604
|
2040 // Read LEN elements of data from IS in the format specified by |
3019
|
2041 // PRECISION, placing the result in DATA. If SWAP is TRUE, swap |
604
|
2042 // the bytes of each element before copying to DATA. FLT_FMT |
|
2043 // specifies the format of the data if we are reading floating point |
|
2044 // numbers. |
|
2045 |
|
2046 static void |
3523
|
2047 read_mat_binary_data (std::istream& is, double *data, int precision, |
3019
|
2048 int len, bool swap, |
2318
|
2049 oct_mach_info::float_format flt_fmt) |
604
|
2050 { |
|
2051 switch (precision) |
|
2052 { |
|
2053 case 0: |
630
|
2054 read_doubles (is, data, LS_DOUBLE, len, swap, flt_fmt); |
604
|
2055 break; |
|
2056 |
|
2057 case 1: |
630
|
2058 read_doubles (is, data, LS_FLOAT, len, swap, flt_fmt); |
604
|
2059 break; |
|
2060 |
|
2061 case 2: |
|
2062 read_doubles (is, data, LS_INT, len, swap, flt_fmt); |
|
2063 break; |
|
2064 |
|
2065 case 3: |
|
2066 read_doubles (is, data, LS_SHORT, len, swap, flt_fmt); |
|
2067 break; |
|
2068 |
|
2069 case 4: |
|
2070 read_doubles (is, data, LS_U_SHORT, len, swap, flt_fmt); |
|
2071 break; |
|
2072 |
|
2073 case 5: |
|
2074 read_doubles (is, data, LS_U_CHAR, len, swap, flt_fmt); |
|
2075 break; |
|
2076 |
|
2077 default: |
|
2078 break; |
|
2079 } |
|
2080 } |
|
2081 |
|
2082 static int |
3523
|
2083 read_mat_file_header (std::istream& is, bool& swap, FOUR_BYTE_INT& mopt, |
604
|
2084 FOUR_BYTE_INT& nr, FOUR_BYTE_INT& nc, |
|
2085 FOUR_BYTE_INT& imag, FOUR_BYTE_INT& len, |
|
2086 int quiet = 0) |
|
2087 { |
3019
|
2088 swap = false; |
671
|
2089 |
1358
|
2090 // We expect to fail here, at the beginning of a record, so not |
|
2091 // being able to read another mopt value should not result in an |
|
2092 // error. |
911
|
2093 |
3557
|
2094 is.read (X_CAST (char *, &mopt), 4); |
604
|
2095 if (! is) |
911
|
2096 return 1; |
604
|
2097 |
3557
|
2098 if (! is.read (X_CAST (char *, &nr), 4)) |
604
|
2099 goto data_read_error; |
|
2100 |
3557
|
2101 if (! is.read (X_CAST (char *, &nc), 4)) |
604
|
2102 goto data_read_error; |
|
2103 |
3557
|
2104 if (! is.read (X_CAST (char *, &imag), 4)) |
604
|
2105 goto data_read_error; |
|
2106 |
3557
|
2107 if (! is.read (X_CAST (char *, &len), 4)) |
604
|
2108 goto data_read_error; |
|
2109 |
|
2110 // If mopt is nonzero and the byte order is swapped, mopt will be |
|
2111 // bigger than we expect, so we swap bytes. |
|
2112 // |
|
2113 // If mopt is zero, it means the file was written on a little endian |
|
2114 // machine, and we only need to swap if we are running on a big endian |
|
2115 // machine. |
|
2116 // |
|
2117 // Gag me. |
|
2118 |
2318
|
2119 if (oct_mach_info::words_big_endian () && mopt == 0) |
3019
|
2120 swap = true; |
604
|
2121 |
1358
|
2122 // mopt is signed, therefore byte swap may result in negative value. |
911
|
2123 |
|
2124 if (mopt > 9999 || mopt < 0) |
3019
|
2125 swap = true; |
604
|
2126 |
|
2127 if (swap) |
|
2128 { |
3145
|
2129 swap_4_bytes (X_CAST (char *, &mopt)); |
|
2130 swap_4_bytes (X_CAST (char *, &nr)); |
|
2131 swap_4_bytes (X_CAST (char *, &nc)); |
|
2132 swap_4_bytes (X_CAST (char *, &imag)); |
|
2133 swap_4_bytes (X_CAST (char *, &len)); |
604
|
2134 } |
|
2135 |
911
|
2136 if (mopt > 9999 || mopt < 0 || imag > 1 || imag < 0) |
604
|
2137 { |
|
2138 if (! quiet) |
|
2139 error ("load: can't read binary file"); |
|
2140 return -1; |
|
2141 } |
|
2142 |
|
2143 return 0; |
|
2144 |
|
2145 data_read_error: |
|
2146 return -1; |
|
2147 } |
|
2148 |
617
|
2149 // We don't just use a cast here, because we need to be able to detect |
|
2150 // possible errors. |
|
2151 |
2318
|
2152 static oct_mach_info::float_format |
|
2153 mopt_digit_to_float_format (int mach) |
617
|
2154 { |
2318
|
2155 oct_mach_info::float_format flt_fmt = oct_mach_info::unknown; |
619
|
2156 |
617
|
2157 switch (mach) |
|
2158 { |
|
2159 case 0: |
2318
|
2160 flt_fmt = oct_mach_info::ieee_little_endian; |
617
|
2161 break; |
|
2162 |
|
2163 case 1: |
2318
|
2164 flt_fmt = oct_mach_info::ieee_big_endian; |
617
|
2165 break; |
|
2166 |
|
2167 case 2: |
2318
|
2168 flt_fmt = oct_mach_info::vax_d; |
617
|
2169 break; |
|
2170 |
|
2171 case 3: |
2318
|
2172 flt_fmt = oct_mach_info::vax_g; |
617
|
2173 break; |
|
2174 |
|
2175 case 4: |
2318
|
2176 flt_fmt = oct_mach_info::cray; |
617
|
2177 break; |
|
2178 |
|
2179 default: |
2318
|
2180 flt_fmt = oct_mach_info::unknown; |
617
|
2181 break; |
|
2182 } |
619
|
2183 |
|
2184 return flt_fmt; |
617
|
2185 } |
619
|
2186 |
2318
|
2187 static int |
|
2188 float_format_to_mopt_digit (oct_mach_info::float_format flt_fmt) |
|
2189 { |
|
2190 int retval = -1; |
|
2191 |
|
2192 switch (flt_fmt) |
|
2193 { |
|
2194 case oct_mach_info::ieee_little_endian: |
|
2195 retval = 0; |
|
2196 break; |
|
2197 |
|
2198 case oct_mach_info::ieee_big_endian: |
|
2199 retval = 1; |
|
2200 break; |
|
2201 |
|
2202 case oct_mach_info::vax_d: |
|
2203 retval = 2; |
|
2204 break; |
|
2205 |
|
2206 case oct_mach_info::vax_g: |
|
2207 retval = 3; |
|
2208 break; |
|
2209 |
|
2210 case oct_mach_info::cray: |
|
2211 retval = 4; |
|
2212 break; |
|
2213 |
|
2214 default: |
|
2215 break; |
|
2216 } |
|
2217 |
|
2218 return retval; |
|
2219 } |
|
2220 |
604
|
2221 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
2222 // place it in TC, returning the name of the variable. |
|
2223 // |
3688
|
2224 // The data is expected to be in Matlab version 4 .mat format, though |
|
2225 // not all the features of that format are supported. |
604
|
2226 // |
|
2227 // FILENAME is used for error messages. |
|
2228 // |
|
2229 // This format provides no way to tag the data as global. |
|
2230 |
|
2231 static char * |
3523
|
2232 read_mat_binary_data (std::istream& is, const std::string& filename, |
2086
|
2233 octave_value& tc) |
604
|
2234 { |
1358
|
2235 // These are initialized here instead of closer to where they are |
|
2236 // first used to avoid errors from gcc about goto crossing |
|
2237 // initialization of variable. |
604
|
2238 |
|
2239 Matrix re; |
2318
|
2240 oct_mach_info::float_format flt_fmt = oct_mach_info::unknown; |
604
|
2241 char *name = 0; |
3019
|
2242 bool swap = false; |
|
2243 int type = 0; |
|
2244 int prec = 0; |
3136
|
2245 int order = 0; |
3019
|
2246 int mach = 0; |
|
2247 int dlen = 0; |
604
|
2248 |
|
2249 FOUR_BYTE_INT mopt, nr, nc, imag, len; |
|
2250 |
|
2251 int err = read_mat_file_header (is, swap, mopt, nr, nc, imag, len); |
|
2252 if (err) |
|
2253 { |
|
2254 if (err < 0) |
|
2255 goto data_read_error; |
|
2256 else |
|
2257 return 0; |
|
2258 } |
|
2259 |
3136
|
2260 type = mopt % 10; // Full, sparse, etc. |
|
2261 mopt /= 10; // Eliminate first digit. |
|
2262 prec = mopt % 10; // double, float, int, etc. |
|
2263 mopt /= 10; // Eliminate second digit. |
|
2264 order = mopt % 10; // Row or column major ordering. |
|
2265 mopt /= 10; // Eliminate third digit. |
|
2266 mach = mopt % 10; // IEEE, VAX, etc. |
604
|
2267 |
2318
|
2268 flt_fmt = mopt_digit_to_float_format (mach); |
|
2269 |
|
2270 if (flt_fmt == oct_mach_info::unknown) |
604
|
2271 { |
|
2272 error ("load: unrecognized binary format!"); |
|
2273 return 0; |
|
2274 } |
|
2275 |
|
2276 if (type != 0 && type != 1) |
|
2277 { |
|
2278 error ("load: can't read sparse matrices"); |
|
2279 return 0; |
|
2280 } |
|
2281 |
|
2282 if (imag && type == 1) |
|
2283 { |
|
2284 error ("load: encountered complex matrix with string flag set!"); |
|
2285 return 0; |
|
2286 } |
|
2287 |
2345
|
2288 // LEN includes the terminating character, and the file is also |
2436
|
2289 // supposed to include it, but apparently not all files do. Either |
|
2290 // way, I think this should work. |
2345
|
2291 |
2436
|
2292 name = new char [len+1]; |
3557
|
2293 if (! is.read (X_CAST (char *, name), len)) |
604
|
2294 goto data_read_error; |
2436
|
2295 name[len] = '\0'; |
604
|
2296 |
|
2297 dlen = nr * nc; |
|
2298 if (dlen < 0) |
|
2299 goto data_read_error; |
|
2300 |
3136
|
2301 if (order) |
|
2302 { |
|
2303 int tmp = nr; |
|
2304 nr = nc; |
|
2305 nc = tmp; |
|
2306 } |
|
2307 |
604
|
2308 re.resize (nr, nc); |
|
2309 |
|
2310 read_mat_binary_data (is, re.fortran_vec (), prec, dlen, swap, flt_fmt); |
|
2311 |
|
2312 if (! is || error_state) |
|
2313 { |
|
2314 error ("load: reading matrix data for `%s'", name); |
|
2315 goto data_read_error; |
|
2316 } |
|
2317 |
|
2318 if (imag) |
|
2319 { |
|
2320 Matrix im (nr, nc); |
|
2321 |
|
2322 read_mat_binary_data (is, im.fortran_vec (), prec, dlen, swap, flt_fmt); |
|
2323 |
|
2324 if (! is || error_state) |
|
2325 { |
|
2326 error ("load: reading imaginary matrix data for `%s'", name); |
|
2327 goto data_read_error; |
|
2328 } |
|
2329 |
|
2330 ComplexMatrix ctmp (nr, nc); |
|
2331 |
|
2332 for (int j = 0; j < nc; j++) |
|
2333 for (int i = 0; i < nr; i++) |
2305
|
2334 ctmp (i, j) = Complex (re (i, j), im (i, j)); |
604
|
2335 |
3136
|
2336 tc = order ? ctmp.transpose () : ctmp; |
604
|
2337 } |
|
2338 else |
3136
|
2339 tc = order ? re.transpose () : re; |
604
|
2340 |
1427
|
2341 if (type == 1) |
604
|
2342 tc = tc.convert_to_str (); |
|
2343 |
|
2344 return name; |
|
2345 |
|
2346 data_read_error: |
1755
|
2347 error ("load: trouble reading binary file `%s'", filename.c_str ()); |
604
|
2348 delete [] name; |
|
2349 return 0; |
|
2350 } |
|
2351 |
3688
|
2352 // Read COUNT elements of data from IS in the format specified by TYPE, |
|
2353 // placing the result in DATA. If SWAP is TRUE, swap the bytes of |
|
2354 // each element before copying to DATA. FLT_FMT specifies the format |
|
2355 // of the data if we are reading floating point numbers. |
|
2356 |
|
2357 static void |
|
2358 read_mat5_binary_data (std::istream& is, double *data, |
|
2359 int count, bool swap, mat5_data_type type, |
|
2360 oct_mach_info::float_format flt_fmt) |
|
2361 { |
|
2362 |
|
2363 switch (type) |
|
2364 { |
|
2365 case miINT8: |
|
2366 read_doubles (is, data, LS_CHAR, count, swap, flt_fmt); |
|
2367 break; |
|
2368 |
|
2369 case miUINT8: |
|
2370 read_doubles (is, data, LS_U_CHAR, count, swap, flt_fmt); |
|
2371 break; |
|
2372 |
|
2373 case miINT16: |
|
2374 read_doubles (is, data, LS_SHORT, count, swap, flt_fmt); |
|
2375 break; |
|
2376 |
|
2377 case miUINT16: |
|
2378 read_doubles (is, data, LS_U_SHORT, count, swap, flt_fmt); |
|
2379 break; |
|
2380 |
|
2381 case miINT32: |
|
2382 read_doubles (is, data, LS_INT, count, swap, flt_fmt); |
|
2383 break; |
|
2384 |
|
2385 case miUINT32: |
|
2386 read_doubles (is, data, LS_U_INT, count, swap, flt_fmt); |
|
2387 break; |
|
2388 |
|
2389 case miSINGLE: |
|
2390 read_doubles (is, data, LS_FLOAT, count, swap, flt_fmt); |
|
2391 break; |
|
2392 |
|
2393 case miRESERVE1: |
|
2394 break; |
|
2395 |
|
2396 case miDOUBLE: |
|
2397 read_doubles (is, data, LS_DOUBLE, count, swap, flt_fmt); |
|
2398 break; |
|
2399 |
|
2400 case miRESERVE2: |
|
2401 case miRESERVE3: |
|
2402 break; |
|
2403 |
|
2404 case miINT64: |
|
2405 #ifdef EIGHT_BYTE_INT |
|
2406 read_doubles (is, data, LS_LONG, count, swap, flt_fmt); |
|
2407 #endif |
|
2408 break; |
|
2409 |
|
2410 case miUINT64: |
|
2411 #ifdef EIGHT_BYTE_INT |
|
2412 read_doubles (is, data, LS_U_LONG, count, swap, flt_fmt); |
|
2413 #endif |
|
2414 break; |
|
2415 |
|
2416 case miMATRIX: |
|
2417 default: |
|
2418 break; |
|
2419 } |
|
2420 } |
|
2421 |
|
2422 // Read one element tag from stream IS, |
|
2423 // place the type code in TYPE and the byte count in BYTES |
|
2424 // return nonzero on error |
|
2425 static int |
|
2426 read_mat5_tag (std::istream& is, bool swap, int& type, int& bytes) |
|
2427 { |
|
2428 unsigned int upper; |
|
2429 FOUR_BYTE_INT temp; |
|
2430 |
|
2431 if (! is.read (X_CAST (char *, &temp), 4 )) |
|
2432 goto data_read_error; |
|
2433 |
|
2434 if (swap) |
|
2435 swap_4_bytes ((char *)&temp); |
|
2436 |
|
2437 upper = (temp >> 16) & 0xffff; |
|
2438 type = temp & 0xffff; |
|
2439 |
|
2440 if (upper) |
|
2441 { |
|
2442 // "compressed" format |
|
2443 bytes = upper; |
|
2444 } |
|
2445 else |
|
2446 { |
|
2447 if (! is.read (X_CAST (char *, &temp), 4 )) |
|
2448 goto data_read_error; |
|
2449 if (swap) |
|
2450 swap_4_bytes ((char *)&temp); |
|
2451 bytes = temp; |
|
2452 } |
|
2453 |
|
2454 return 0; |
|
2455 |
|
2456 data_read_error: |
|
2457 return 1; |
|
2458 } |
|
2459 |
|
2460 // Extract one data element (scalar, matrix, string, etc.) from stream |
|
2461 // IS and place it in TC, returning the name of the variable. |
|
2462 // |
|
2463 // The data is expected to be in Matlab's "Version 5" .mat format, |
|
2464 // though not all the features of that format are supported. |
|
2465 // |
|
2466 // FILENAME is used for error messages. |
|
2467 |
|
2468 static char * |
|
2469 read_mat5_binary_element (std::istream& is, const std::string& filename, |
|
2470 bool swap, bool& global, octave_value& tc) |
|
2471 { |
|
2472 // These are initialized here instead of closer to where they are |
|
2473 // first used to avoid errors from gcc about goto crossing |
|
2474 // initialization of variable. |
|
2475 |
|
2476 Matrix re; |
|
2477 oct_mach_info::float_format flt_fmt = oct_mach_info::unknown; |
|
2478 char *name = 0; |
|
2479 int type = 0; |
|
2480 bool imag; |
|
2481 bool logicalvar; |
|
2482 enum arrayclasstype arrayclass; |
|
2483 FOUR_BYTE_INT junk; |
|
2484 FOUR_BYTE_INT flags; |
|
2485 FOUR_BYTE_INT nr; |
|
2486 FOUR_BYTE_INT nc; |
|
2487 FOUR_BYTE_INT dimension_length; |
|
2488 int len; |
|
2489 int element_length; |
|
2490 std::streampos pos; |
|
2491 TWO_BYTE_INT number; |
|
2492 number = *(TWO_BYTE_INT *)"\x00\x01"; |
|
2493 |
|
2494 // MAT files always use IEEE floating point |
|
2495 if ((number == 1) ^ swap) |
|
2496 flt_fmt = oct_mach_info::ieee_big_endian; |
|
2497 else |
|
2498 flt_fmt = oct_mach_info::ieee_little_endian; |
|
2499 |
|
2500 // element type and length |
|
2501 if (read_mat5_tag (is, swap, type, element_length)) |
|
2502 return 0; // EOF |
|
2503 |
|
2504 if (type != miMATRIX) |
|
2505 { |
|
2506 error ("load: invalid element type"); |
|
2507 goto early_read_error; |
|
2508 } |
|
2509 pos = is.tellg (); |
|
2510 |
|
2511 // array flags subelement |
|
2512 if (read_mat5_tag (is, swap, type, len) || type != miUINT32 || len != 8) |
|
2513 { |
|
2514 error ("load: invalid array flags subelement"); |
|
2515 goto early_read_error; |
|
2516 } |
|
2517 |
|
2518 read_int (is, swap, flags); |
|
2519 imag = (flags & 0x0800) != 0; // has an imaginary part? |
|
2520 global = (flags & 0x0400) != 0; // global variable? |
|
2521 logicalvar = (flags & 0x0200) != 0; // we don't use this yet |
|
2522 arrayclass = (arrayclasstype)(flags & 0xff); |
|
2523 read_int (is, swap, junk); // an "undefined" entry |
|
2524 |
|
2525 // dimensions array subelement |
|
2526 { |
|
2527 std::streampos pos; |
|
2528 |
|
2529 if (read_mat5_tag (is, swap, type, dimension_length) || type != miINT32) |
|
2530 { |
|
2531 error ("load: invalid dimensions array subelement"); |
|
2532 goto early_read_error; |
|
2533 } |
|
2534 |
|
2535 pos = is.tellg (); |
|
2536 read_int (is, swap, nr); |
|
2537 read_int (is, swap, nc); |
|
2538 re.resize (nr, nc); |
|
2539 |
|
2540 // delay checking for a multidimensional array until we have read |
|
2541 // the variable name |
|
2542 is.seekg (pos + dimension_length); |
|
2543 } |
|
2544 |
|
2545 // array name subelement |
|
2546 { |
|
2547 std::streampos pos; |
|
2548 |
|
2549 if (read_mat5_tag (is, swap, type, len) || type != miINT8) |
|
2550 { |
|
2551 error ("load: invalid array name subelement"); |
|
2552 goto early_read_error; |
|
2553 } |
|
2554 |
|
2555 pos = is.tellg (); |
|
2556 name = new char[len+1]; |
|
2557 |
|
2558 if (len) // structure field subelements have |
|
2559 // zero-length array name subelements |
|
2560 { |
|
2561 if (! is.read (X_CAST (char *, name), len )) |
|
2562 goto data_read_error; |
|
2563 |
|
2564 is.seekg (pos + PAD (len)); |
|
2565 } |
|
2566 |
|
2567 name[len] = '\0'; |
|
2568 } |
|
2569 |
|
2570 if (dimension_length != 8) |
|
2571 { |
|
2572 error ("load: multidimension arrays are not implemented"); |
|
2573 goto skip_ahead; |
|
2574 } |
|
2575 |
|
2576 switch (arrayclass) |
|
2577 { |
|
2578 case mxCELL_CLASS: |
|
2579 warning ("load: cell arrays are not implemented"); |
|
2580 goto skip_ahead; |
|
2581 |
|
2582 case mxOBJECT_CLASS: |
|
2583 warning ("load: objects are not implemented"); |
|
2584 goto skip_ahead; |
|
2585 |
|
2586 case mxSPARSE_CLASS: |
|
2587 warning ("load: sparse arrays are not implemented"); |
|
2588 goto skip_ahead; |
|
2589 |
|
2590 case mxSTRUCT_CLASS: |
|
2591 { |
|
2592 Octave_map m; |
|
2593 FOUR_BYTE_INT type; |
|
2594 FOUR_BYTE_INT len; |
|
2595 FOUR_BYTE_INT field_name_length; |
|
2596 int i; |
|
2597 char *elname; |
|
2598 |
|
2599 // field name length subelement -- actually the maximum length |
|
2600 // of a field name. The Matlab docs promise this will always |
|
2601 // be 32. We read and use the actual value, on the theory |
|
2602 // that eventually someone will recognize that's a waste of |
|
2603 // space. |
|
2604 if (read_mat5_tag (is, swap, type, len) || type != miINT32) |
|
2605 { |
|
2606 error ("load: invalid field name subelement"); |
|
2607 goto data_read_error; |
|
2608 } |
|
2609 |
|
2610 if (! is.read (X_CAST (char *, &field_name_length), len )) |
|
2611 goto data_read_error; |
|
2612 |
|
2613 if (swap) |
|
2614 swap_4_bytes ((char *)&field_name_length); |
|
2615 |
|
2616 // field name subelement. The length of this subelement tells |
|
2617 // us how many fields there are. |
|
2618 if (read_mat5_tag (is, swap, type, len) || type != miINT8) |
|
2619 { |
|
2620 error ("load: invalid field name subelement"); |
|
2621 goto data_read_error; |
|
2622 } |
|
2623 |
|
2624 elname = new char[len]; |
|
2625 |
|
2626 if (! is.read (elname, len)) |
|
2627 goto data_read_error; |
|
2628 |
|
2629 // fields subelements |
|
2630 for (i=0; i < len/field_name_length; i++) |
|
2631 { |
|
2632 char *thename; |
|
2633 octave_value fieldtc; |
|
2634 thename = read_mat5_binary_element (is, filename, swap, |
|
2635 global, fieldtc); |
|
2636 m[elname + i*field_name_length] = fieldtc; |
|
2637 delete [] thename; |
|
2638 } |
|
2639 |
|
2640 delete [] elname; |
|
2641 tc = m; |
|
2642 } |
|
2643 break; |
|
2644 |
|
2645 case mxCHAR_CLASS: |
|
2646 // handle as a numerical array to start with |
|
2647 |
|
2648 case mxDOUBLE_CLASS: |
|
2649 case mxSINGLE_CLASS: |
|
2650 case mxINT8_CLASS: |
|
2651 case mxUINT8_CLASS: |
|
2652 case mxINT16_CLASS: |
|
2653 case mxUINT16_CLASS: |
|
2654 case mxINT32_CLASS: |
|
2655 case mxUINT32_CLASS: |
|
2656 default: |
|
2657 // handle all these numerical formats as double arrays |
|
2658 |
|
2659 // real data subelement |
|
2660 { |
|
2661 std::streampos pos; |
|
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 pos = is.tellg (); |
|
2670 read_mat5_binary_data (is, re.fortran_vec (), nr*nc, swap, |
|
2671 (enum mat5_data_type) type, flt_fmt); |
|
2672 |
|
2673 if (! is || error_state) |
|
2674 { |
|
2675 error ("load: reading matrix data for `%s'", name); |
|
2676 goto data_read_error; |
|
2677 } |
|
2678 |
|
2679 is.seekg (pos + PAD (len)); |
|
2680 } |
|
2681 |
|
2682 // imaginary data subelement |
|
2683 if (imag) |
|
2684 { |
|
2685 Matrix im (nr, nc); |
|
2686 |
|
2687 if (read_mat5_tag (is, swap, type, len)) |
|
2688 { |
|
2689 error ("load: reading matrix data for `%s'", name); |
|
2690 goto data_read_error; |
|
2691 } |
|
2692 |
|
2693 read_mat5_binary_data (is, im.fortran_vec (), nr*nc, swap, |
|
2694 (enum mat5_data_type) type, flt_fmt); |
|
2695 |
|
2696 if (! is || error_state) |
|
2697 { |
|
2698 error ("load: reading imaginary matrix data for `%s'", name); |
|
2699 goto data_read_error; |
|
2700 } |
|
2701 |
|
2702 ComplexMatrix ctmp (nr, nc); |
|
2703 |
|
2704 for (int j = 0; j < nc; j++) |
|
2705 for (int i = 0; i < nr; i++) |
|
2706 ctmp (i, j) = Complex (re (i, j), im (i, j)); |
|
2707 |
|
2708 tc = ctmp; |
|
2709 } |
|
2710 else |
|
2711 tc = re; |
|
2712 |
|
2713 if (arrayclass == mxCHAR_CLASS) |
|
2714 tc = tc.convert_to_str (); |
|
2715 } |
|
2716 |
|
2717 is.seekg (pos + element_length); |
|
2718 |
|
2719 return name; |
|
2720 |
|
2721 data_read_error: |
|
2722 delete [] name; |
|
2723 |
|
2724 early_read_error: |
|
2725 error ("load: trouble reading binary file `%s'", filename.c_str ()); |
|
2726 return 0; |
|
2727 |
|
2728 skip_ahead: |
|
2729 warning (" skipping over `%s'", name); |
|
2730 delete [] name; |
|
2731 is.seekg (pos + element_length); |
|
2732 return read_mat5_binary_element (is, filename, swap, global, tc); |
|
2733 } |
|
2734 |
|
2735 static int |
|
2736 read_mat5_binary_file_header (std::istream& is, bool& swap, |
|
2737 bool quiet = false) |
|
2738 { |
|
2739 TWO_BYTE_INT version=0, magic=0; |
|
2740 |
|
2741 is.seekg (124, std::ios::beg); |
|
2742 is.read (X_CAST (char *, &version), 2); |
|
2743 is.read (X_CAST (char *, &magic), 2); |
|
2744 |
|
2745 if (magic == 0x4d49) |
|
2746 swap = 0; |
|
2747 else if (magic == 0x494d) |
|
2748 swap = 1; |
|
2749 else |
|
2750 { |
|
2751 if (! quiet) |
|
2752 error ("load: can't read binary file"); |
|
2753 return -1; |
|
2754 } |
|
2755 |
|
2756 if (! swap) // version number is inverse swapped! |
|
2757 version = ((version >> 8) & 0xff) + ((version & 0xff) << 8); |
|
2758 |
|
2759 if (version != 1 && !quiet) |
|
2760 warning ("load: found version %d binary MAT file, " |
|
2761 "but only prepared for version 1", version); |
|
2762 |
|
2763 return 0; |
|
2764 } |
|
2765 |
3019
|
2766 // Return TRUE if NAME matches one of the given globbing PATTERNS. |
604
|
2767 |
3013
|
2768 static bool |
3687
|
2769 matches_patterns (const std::string_vector& patterns, int pat_idx, |
3523
|
2770 int num_pat, const std::string& name) |
604
|
2771 { |
1755
|
2772 for (int i = pat_idx; i < num_pat; i++) |
604
|
2773 { |
1792
|
2774 glob_match pattern (patterns[i]); |
3013
|
2775 |
1792
|
2776 if (pattern.match (name)) |
3013
|
2777 return true; |
604
|
2778 } |
3688
|
2779 |
3013
|
2780 return false; |
604
|
2781 } |
|
2782 |
|
2783 static int |
3523
|
2784 read_binary_file_header (std::istream& is, bool& swap, |
2318
|
2785 oct_mach_info::float_format& flt_fmt, |
3019
|
2786 bool quiet = false) |
604
|
2787 { |
3552
|
2788 const int magic_len = 10; |
|
2789 char magic[magic_len+1]; |
3557
|
2790 is.read (X_CAST (char *, magic), magic_len); |
604
|
2791 magic[magic_len] = '\0'; |
3688
|
2792 |
604
|
2793 if (strncmp (magic, "Octave-1-L", magic_len) == 0) |
2318
|
2794 swap = oct_mach_info::words_big_endian (); |
604
|
2795 else if (strncmp (magic, "Octave-1-B", magic_len) == 0) |
2318
|
2796 swap = ! oct_mach_info::words_big_endian (); |
604
|
2797 else |
|
2798 { |
|
2799 if (! quiet) |
|
2800 error ("load: can't read binary file"); |
|
2801 return -1; |
|
2802 } |
|
2803 |
|
2804 char tmp = 0; |
3557
|
2805 is.read (X_CAST (char *, &tmp), 1); |
604
|
2806 |
2318
|
2807 flt_fmt = mopt_digit_to_float_format (tmp); |
|
2808 |
|
2809 if (flt_fmt == oct_mach_info::unknown) |
604
|
2810 { |
|
2811 if (! quiet) |
|
2812 error ("load: unrecognized binary format!"); |
3688
|
2813 |
604
|
2814 return -1; |
|
2815 } |
|
2816 |
|
2817 return 0; |
|
2818 } |
|
2819 |
|
2820 static load_save_format |
3523
|
2821 get_file_format (const std::string& fname, const std::string& orig_fname) |
604
|
2822 { |
|
2823 load_save_format retval = LS_UNKNOWN; |
|
2824 |
3687
|
2825 #ifdef HAVE_HDF5 |
3688
|
2826 // check this before we open the file |
3687
|
2827 if (H5Fis_hdf5 (fname.c_str ()) > 0) |
|
2828 return LS_HDF5; |
|
2829 #endif /* HAVE_HDF5 */ |
|
2830 |
3523
|
2831 std::ifstream file (fname.c_str ()); |
604
|
2832 |
|
2833 if (! file) |
|
2834 { |
1750
|
2835 error ("load: couldn't open input file `%s'", orig_fname.c_str ()); |
604
|
2836 return retval; |
|
2837 } |
|
2838 |
2318
|
2839 oct_mach_info::float_format flt_fmt = oct_mach_info::unknown; |
604
|
2840 |
3019
|
2841 bool swap = false; |
|
2842 |
|
2843 if (read_binary_file_header (file, swap, flt_fmt, true) == 0) |
604
|
2844 retval = LS_BINARY; |
|
2845 else |
|
2846 { |
3538
|
2847 file.seekg (0, std::ios::beg); |
604
|
2848 |
|
2849 FOUR_BYTE_INT mopt, nr, nc, imag, len; |
1180
|
2850 |
|
2851 int err = read_mat_file_header (file, swap, mopt, nr, nc, imag, len, 1); |
|
2852 |
|
2853 if (! err) |
604
|
2854 retval = LS_MAT_BINARY; |
|
2855 else |
|
2856 { |
2511
|
2857 file.clear (); |
3538
|
2858 file.seekg (0, std::ios::beg); |
604
|
2859 |
3697
|
2860 err = read_mat5_binary_file_header (file, swap, true); |
3688
|
2861 |
|
2862 if (! err) |
|
2863 { |
|
2864 file.clear (); |
|
2865 file.seekg (0, std::ios::beg); |
|
2866 retval = LS_MAT5_BINARY; |
|
2867 } |
|
2868 else |
|
2869 { |
|
2870 file.clear (); |
|
2871 file.seekg (0, std::ios::beg); |
|
2872 |
|
2873 char *tmp = extract_keyword (file, "name"); |
|
2874 |
|
2875 if (tmp) |
|
2876 { |
|
2877 retval = LS_ASCII; |
|
2878 |
|
2879 delete [] tmp; |
|
2880 } |
|
2881 else |
|
2882 { |
|
2883 // Try reading the file as numbers only, determining the |
|
2884 // number of rows and columns from the data. We don't |
|
2885 // even bother to check to see if the first item in the |
|
2886 // file is a number, so that get_complete_line() can |
|
2887 // skip any comments that might appear at the top of the |
|
2888 // file. |
|
2889 |
|
2890 retval = LS_MAT_ASCII; |
|
2891 } |
2511
|
2892 } |
604
|
2893 } |
|
2894 } |
|
2895 |
|
2896 file.close (); |
|
2897 |
|
2898 if (retval == LS_UNKNOWN) |
1750
|
2899 error ("load: unable to determine file format for `%s'", |
|
2900 orig_fname.c_str ()); |
604
|
2901 |
|
2902 return retval; |
|
2903 } |
|
2904 |
2086
|
2905 static octave_value_list |
3523
|
2906 do_load (std::istream& stream, const std::string& orig_fname, bool force, |
2318
|
2907 load_save_format format, oct_mach_info::float_format flt_fmt, |
3687
|
2908 bool list_only, bool swap, bool verbose, bool import, |
|
2909 const string_vector& argv, int argv_idx, int argc, int nargout) |
604
|
2910 { |
2086
|
2911 octave_value_list retval; |
604
|
2912 |
3523
|
2913 std::ostrstream output_buf; |
604
|
2914 int count = 0; |
|
2915 for (;;) |
|
2916 { |
3019
|
2917 bool global = false; |
2086
|
2918 octave_value tc; |
604
|
2919 |
|
2920 char *name = 0; |
|
2921 char *doc = 0; |
|
2922 |
|
2923 switch (format) |
|
2924 { |
|
2925 case LS_ASCII: |
3136
|
2926 name = read_ascii_data (stream, orig_fname, global, tc, count); |
604
|
2927 break; |
|
2928 |
|
2929 case LS_BINARY: |
|
2930 name = read_binary_data (stream, swap, flt_fmt, orig_fname, |
|
2931 global, tc, doc); |
|
2932 break; |
|
2933 |
2511
|
2934 case LS_MAT_ASCII: |
|
2935 name = read_mat_ascii_data (stream, orig_fname, tc); |
|
2936 break; |
|
2937 |
604
|
2938 case LS_MAT_BINARY: |
|
2939 name = read_mat_binary_data (stream, orig_fname, tc); |
|
2940 break; |
|
2941 |
3687
|
2942 #ifdef HAVE_HDF5 |
|
2943 case LS_HDF5: |
|
2944 name = read_hdf5_data (stream, orig_fname, |
|
2945 global, tc, doc, import); |
|
2946 break; |
|
2947 #endif /* HAVE_HDF5 */ |
|
2948 |
3688
|
2949 case LS_MAT5_BINARY: |
|
2950 name = read_mat5_binary_element (stream, orig_fname, swap, |
|
2951 global, tc); |
|
2952 break; |
|
2953 |
604
|
2954 default: |
775
|
2955 gripe_unrecognized_data_fmt ("load"); |
604
|
2956 break; |
|
2957 } |
|
2958 |
867
|
2959 if (error_state || stream.eof () || ! name) |
604
|
2960 { |
867
|
2961 delete [] name; |
|
2962 delete [] doc; |
2511
|
2963 |
604
|
2964 break; |
|
2965 } |
|
2966 else if (! error_state && name) |
|
2967 { |
|
2968 if (tc.is_defined ()) |
|
2969 { |
3136
|
2970 if (format == LS_MAT_ASCII && argv_idx < argc) |
|
2971 warning ("load: loaded ASCII file `%s' -- ignoring extra args", |
3687
|
2972 orig_fname.c_str ()); |
3136
|
2973 |
|
2974 if (format == LS_MAT_ASCII |
|
2975 || argv_idx == argc |
1755
|
2976 || matches_patterns (argv, argv_idx, argc, name)) |
604
|
2977 { |
|
2978 count++; |
621
|
2979 if (list_only) |
|
2980 { |
|
2981 if (verbose) |
|
2982 { |
|
2983 if (count == 1) |
|
2984 output_buf |
|
2985 << "type rows cols name\n" |
|
2986 << "==== ==== ==== ====\n"; |
|
2987 |
3013
|
2988 output_buf |
3548
|
2989 << std::setiosflags (std::ios::left) |
|
2990 << std::setw (16) << tc.type_name () . c_str () |
|
2991 << std::setiosflags (std::ios::right) |
|
2992 << std::setw (7) << tc.rows () |
|
2993 << std::setw (7) << tc.columns () |
3013
|
2994 << " "; |
621
|
2995 } |
|
2996 output_buf << name << "\n"; |
|
2997 } |
|
2998 else |
|
2999 { |
|
3000 install_loaded_variable (force, name, tc, global, doc); |
|
3001 } |
604
|
3002 } |
2511
|
3003 |
|
3004 delete [] name; |
|
3005 delete [] doc; |
|
3006 |
|
3007 // Only attempt to read one item from a headless text file. |
|
3008 |
|
3009 if (format == LS_MAT_ASCII) |
|
3010 break; |
604
|
3011 } |
|
3012 else |
|
3013 error ("load: unable to load variable `%s'", name); |
|
3014 } |
|
3015 else |
|
3016 { |
|
3017 if (count == 0) |
|
3018 error ("load: are you sure `%s' is an Octave data file?", |
1755
|
3019 orig_fname.c_str ()); |
604
|
3020 |
867
|
3021 delete [] name; |
|
3022 delete [] doc; |
2511
|
3023 |
604
|
3024 break; |
|
3025 } |
|
3026 } |
|
3027 |
621
|
3028 if (list_only && count) |
|
3029 { |
3538
|
3030 output_buf << std::ends; |
2095
|
3031 |
|
3032 char *msg = output_buf.str (); |
|
3033 |
621
|
3034 if (nargout > 0) |
2095
|
3035 retval = msg; |
621
|
3036 else |
2095
|
3037 octave_stdout << msg; |
|
3038 |
|
3039 delete [] msg; |
621
|
3040 } |
|
3041 |
863
|
3042 return retval; |
|
3043 } |
|
3044 |
3687
|
3045 // HDF5 load/save documentation is included in the Octave manual |
|
3046 // regardless, but if HDF5 is not linked in we also include a |
|
3047 // sentence noting this, so the user understands that the features |
|
3048 // aren't available. Define a macro for this sentence: |
|
3049 |
|
3050 #ifdef HAVE_HDF5 |
|
3051 #define HAVE_HDF5_HELP_STRING "" |
|
3052 #else /* ! HAVE_HDF5 */ |
|
3053 #define HAVE_HDF5_HELP_STRING "\n\ |
|
3054 HDF5 load and save are not available, as this Octave executable was\n\ |
|
3055 not linked with the HDF5 library." |
|
3056 #endif /* ! HAVE HDF5 */ |
|
3057 |
1957
|
3058 DEFUN_TEXT (load, args, nargout, |
3372
|
3059 "-*- texinfo -*-\n\ |
|
3060 @deffn {Command} load options file v1 v2 @dots{}\n\ |
|
3061 Load the named variables from the file @var{file}. As with @code{save},\n\ |
|
3062 you may specify a list of variables and @code{load} will only extract\n\ |
|
3063 those variables with names that match. For example, to restore the\n\ |
|
3064 variables saved in the file @file{data}, use the command\n\ |
|
3065 \n\ |
|
3066 @example\n\ |
|
3067 load data\n\ |
|
3068 @end example\n\ |
863
|
3069 \n\ |
3372
|
3070 Octave will refuse to overwrite existing variables unless you use the\n\ |
|
3071 option @samp{-force}.\n\ |
|
3072 \n\ |
|
3073 If a variable that is not marked as global is loaded from a file when a\n\ |
|
3074 global symbol with the same name already exists, it is loaded in the\n\ |
|
3075 global symbol table. Also, if a variable is marked as global in a file\n\ |
|
3076 and a local symbol exists, the local symbol is moved to the global\n\ |
|
3077 symbol table and given the value from the file. Since it seems that\n\ |
|
3078 both of these cases are likely to be the result of some sort of error,\n\ |
|
3079 they will generate warnings.\n\ |
863
|
3080 \n\ |
3372
|
3081 The @code{load} command can read data stored in Octave's text and\n\ |
|
3082 binary formats, and @sc{Matlab}'s binary format. It will automatically\n\ |
|
3083 detect the type of file and do conversion from different floating point\n\ |
|
3084 formats (currently only IEEE big and little endian, though other formats\n\ |
|
3085 may added in the future).\n\ |
|
3086 \n\ |
|
3087 Valid options for @code{load} are listed in the following table.\n\ |
863
|
3088 \n\ |
3372
|
3089 @table @code\n\ |
|
3090 @item -force\n\ |
|
3091 Force variables currently in memory to be overwritten by variables with\n\ |
|
3092 the same name found in the file.\n\ |
|
3093 \n\ |
|
3094 @item -ascii\n\ |
|
3095 Force Octave to assume the file is in Octave's text format.\n\ |
|
3096 \n\ |
|
3097 @item -binary\n\ |
|
3098 Force Octave to assume the file is in Octave's binary format.\n\ |
|
3099 \n\ |
|
3100 @item -mat-binary\n\ |
|
3101 Force Octave to assume the file is in @sc{Matlab}'s binary format.\n\ |
3687
|
3102 \n\ |
3688
|
3103 @item -mat4-binary\n\ |
|
3104 Force Octave to assume the file is in the binary format written by\n\ |
|
3105 @sc{Matlab} version 4.\n\ |
|
3106 \n\ |
3687
|
3107 @item -hdf5\n\ |
|
3108 Force Octave to assume the file is in HDF5 format.\n\ |
|
3109 (HDF5 is a free, portable binary format developed by the National\n\ |
|
3110 Center for Supercomputing Applications at the University of Illinois.)\n\ |
|
3111 Note that Octave can read HDF5 files not created by itself, but may\n\ |
|
3112 skip some datasets in formats that it cannot support. In particular,\n\ |
|
3113 it will skip datasets of data types that it does not recognize, with\n\ |
|
3114 dimensionality > 2, or with names that aren't valid Octave identifiers\n\ |
|
3115 See, however, the @samp{-import} option to ameliorate this somewhat.\n" |
|
3116 |
|
3117 HAVE_HDF5_HELP_STRING |
|
3118 |
|
3119 "\n\ |
|
3120 @item -import\n\ |
|
3121 Make a stronger attempt to import foreign datasets. Currently, this means\n\ |
|
3122 that for HDF5 files, invalid characters in names are converted to @samp{_},\n\ |
|
3123 and datasets with dimensionality > 2 are imported as lists of matrices (or\n\ |
|
3124 lists of lists of matrices, or ...).\n\ |
|
3125 \n\ |
3372
|
3126 @end table\n\ |
|
3127 @end deffn") |
863
|
3128 { |
2086
|
3129 octave_value_list retval; |
863
|
3130 |
1755
|
3131 int argc = args.length () + 1; |
|
3132 |
1968
|
3133 string_vector argv = args.make_argv ("load"); |
1755
|
3134 |
|
3135 if (error_state) |
|
3136 return retval; |
863
|
3137 |
1358
|
3138 // It isn't necessary to have the default load format stored in a |
|
3139 // user preference variable since we can determine the type of file |
|
3140 // as we are reading. |
863
|
3141 |
|
3142 load_save_format format = LS_UNKNOWN; |
|
3143 |
3019
|
3144 bool force = false; |
|
3145 bool list_only = false; |
|
3146 bool verbose = false; |
3687
|
3147 bool import = false; |
863
|
3148 |
1755
|
3149 int i; |
|
3150 for (i = 1; i < argc; i++) |
863
|
3151 { |
1755
|
3152 if (argv[i] == "-force" || argv[i] == "-f") |
863
|
3153 { |
3019
|
3154 force = true; |
863
|
3155 } |
1755
|
3156 else if (argv[i] == "-list" || argv[i] == "-l") |
863
|
3157 { |
3019
|
3158 list_only = true; |
863
|
3159 } |
1755
|
3160 else if (argv[i] == "-verbose" || argv[i] == "-v") |
863
|
3161 { |
3019
|
3162 verbose = true; |
863
|
3163 } |
1755
|
3164 else if (argv[i] == "-ascii" || argv[i] == "-a") |
863
|
3165 { |
|
3166 format = LS_ASCII; |
|
3167 } |
1755
|
3168 else if (argv[i] == "-binary" || argv[i] == "-b") |
863
|
3169 { |
|
3170 format = LS_BINARY; |
|
3171 } |
1755
|
3172 else if (argv[i] == "-mat-binary" || argv[i] == "-m") |
863
|
3173 { |
3688
|
3174 format = LS_MAT5_BINARY; |
|
3175 } |
|
3176 else if (argv[i] == "-mat4-binary" || argv[i] == "-4") |
|
3177 { |
863
|
3178 format = LS_MAT_BINARY; |
|
3179 } |
3687
|
3180 else if (argv[i] == "-hdf5" || argv[i] == "-h") |
|
3181 { |
|
3182 #ifdef HAVE_HDF5 |
|
3183 format = LS_HDF5; |
|
3184 #else /* ! HAVE_HDF5 */ |
|
3185 error ("load: octave executable was not linked with HDF5 library"); |
|
3186 return retval; |
|
3187 #endif /* ! HAVE_HDF5 */ |
|
3188 } |
|
3189 else if (argv[i] == "-import" || argv[i] == "-i") |
|
3190 { |
|
3191 import = true; |
|
3192 } |
863
|
3193 else |
|
3194 break; |
|
3195 } |
|
3196 |
1755
|
3197 if (i == argc) |
863
|
3198 { |
2057
|
3199 print_usage ("load"); |
863
|
3200 return retval; |
|
3201 } |
|
3202 |
3523
|
3203 std::string orig_fname = argv[i]; |
863
|
3204 |
2318
|
3205 oct_mach_info::float_format flt_fmt = oct_mach_info::unknown; |
863
|
3206 |
3019
|
3207 bool swap = false; |
863
|
3208 |
1755
|
3209 if (argv[i] == "-") |
863
|
3210 { |
1755
|
3211 i++; |
863
|
3212 |
3687
|
3213 #ifdef HAVE_HDF5 |
|
3214 if (format == LS_HDF5) |
|
3215 error ("load: cannot read HDF5 format from stdin"); |
|
3216 else |
|
3217 #endif /* HAVE_HDF5 */ |
863
|
3218 if (format != LS_UNKNOWN) |
|
3219 { |
1358
|
3220 // XXX FIXME XXX -- if we have already seen EOF on a |
3531
|
3221 // previous call, how do we fix up the state of std::cin so |
|
3222 // that we can get additional input? I'm afraid that we |
|
3223 // can't fix this using std::cin only. |
|
3224 |
|
3225 retval = do_load (std::cin, orig_fname, force, format, flt_fmt, |
3687
|
3226 list_only, swap, verbose, import, argv, i, argc, |
863
|
3227 nargout); |
|
3228 } |
|
3229 else |
|
3230 error ("load: must specify file format if reading from stdin"); |
|
3231 } |
|
3232 else |
|
3233 { |
3523
|
3234 std::string fname = file_ops::tilde_expand (argv[i]); |
863
|
3235 |
|
3236 if (format == LS_UNKNOWN) |
|
3237 format = get_file_format (fname, orig_fname); |
|
3238 |
3687
|
3239 #ifdef HAVE_HDF5 |
|
3240 if (format == LS_HDF5) |
|
3241 { |
|
3242 i++; |
|
3243 |
|
3244 hdf5_ifstream hdf5_file (fname.c_str ()); |
|
3245 |
|
3246 if (hdf5_file.file_id >= 0) |
|
3247 { |
|
3248 retval = do_load (hdf5_file, orig_fname, force, format, |
|
3249 flt_fmt, list_only, swap, verbose, |
|
3250 import, argv, i, argc, nargout); |
|
3251 |
|
3252 hdf5_file.close (); |
|
3253 } |
|
3254 else |
|
3255 error ("load: couldn't open input file `%s'", |
|
3256 orig_fname.c_str ()); |
|
3257 } |
|
3258 else |
|
3259 #endif /* HAVE_HDF5 */ |
|
3260 // don't insert any statements here; the "else" above has to |
|
3261 // go with the "if" below!!!!! |
863
|
3262 if (format != LS_UNKNOWN) |
|
3263 { |
1755
|
3264 i++; |
863
|
3265 |
3538
|
3266 unsigned mode = std::ios::in; |
3688
|
3267 if (format == LS_BINARY || |
|
3268 format == LS_MAT_BINARY || |
|
3269 format == LS_MAT5_BINARY) |
3552
|
3270 mode |= std::ios::binary; |
863
|
3271 |
3523
|
3272 std::ifstream file (fname.c_str (), mode); |
863
|
3273 |
|
3274 if (file) |
|
3275 { |
|
3276 if (format == LS_BINARY) |
|
3277 { |
|
3278 if (read_binary_file_header (file, swap, flt_fmt) < 0) |
|
3279 { |
|
3280 file.close (); |
|
3281 return retval; |
|
3282 } |
|
3283 } |
3688
|
3284 else if (format == LS_MAT5_BINARY) |
|
3285 { |
|
3286 if (read_mat5_binary_file_header (file, swap, false) < 0) |
|
3287 { |
|
3288 file.close (); |
|
3289 return retval; |
|
3290 } |
|
3291 } |
863
|
3292 |
|
3293 retval = do_load (file, orig_fname, force, format, |
3687
|
3294 flt_fmt, list_only, swap, verbose, import, |
1755
|
3295 argv, i, argc, nargout); |
863
|
3296 file.close (); |
|
3297 } |
|
3298 else |
1755
|
3299 error ("load: couldn't open input file `%s'", |
|
3300 orig_fname.c_str ()); |
863
|
3301 } |
|
3302 } |
604
|
3303 |
|
3304 return retval; |
|
3305 } |
|
3306 |
3019
|
3307 // Return TRUE if PATTERN has any special globbing chars in it. |
|
3308 |
|
3309 static bool |
3523
|
3310 glob_pattern_p (const std::string& pattern) |
604
|
3311 { |
|
3312 int open = 0; |
|
3313 |
1755
|
3314 int len = pattern.length (); |
|
3315 |
|
3316 for (int i = 0; i < len; i++) |
604
|
3317 { |
1755
|
3318 char c = pattern[i]; |
|
3319 |
604
|
3320 switch (c) |
|
3321 { |
|
3322 case '?': |
|
3323 case '*': |
3019
|
3324 return true; |
604
|
3325 |
|
3326 case '[': // Only accept an open brace if there is a close |
|
3327 open++; // brace to match it. Bracket expressions must be |
|
3328 continue; // complete, according to Posix.2 |
|
3329 |
|
3330 case ']': |
|
3331 if (open) |
3019
|
3332 return true; |
604
|
3333 continue; |
|
3334 |
|
3335 case '\\': |
1755
|
3336 if (i == len - 1) |
3019
|
3337 return false; |
604
|
3338 |
|
3339 default: |
|
3340 continue; |
|
3341 } |
|
3342 } |
|
3343 |
3019
|
3344 return false; |
604
|
3345 } |
|
3346 |
618
|
3347 // MAX_VAL and MIN_VAL are assumed to have integral values even though |
|
3348 // they are stored in doubles. |
|
3349 |
604
|
3350 static save_type |
|
3351 get_save_type (double max_val, double min_val) |
|
3352 { |
|
3353 save_type st = LS_DOUBLE; |
|
3354 |
|
3355 if (max_val < 256 && min_val > -1) |
|
3356 st = LS_U_CHAR; |
|
3357 else if (max_val < 65536 && min_val > -1) |
|
3358 st = LS_U_SHORT; |
3131
|
3359 else if (max_val < 4294967295UL && min_val > -1) |
618
|
3360 st = LS_U_INT; |
|
3361 else if (max_val < 128 && min_val >= -128) |
|
3362 st = LS_CHAR; |
604
|
3363 else if (max_val < 32768 && min_val >= -32768) |
|
3364 st = LS_SHORT; |
3131
|
3365 else if (max_val <= 2147483647L && min_val >= -2147483647L) |
604
|
3366 st = LS_INT; |
|
3367 |
|
3368 return st; |
|
3369 } |
|
3370 |
|
3371 // Save the data from TC along with the corresponding NAME, help |
|
3372 // string DOC, and global flag MARK_AS_GLOBAL on stream OS in the |
1427
|
3373 // binary format described above for read_binary_data. |
604
|
3374 |
2799
|
3375 static bool |
3523
|
3376 save_binary_data (std::ostream& os, const octave_value& tc, |
|
3377 const std::string& name, const std::string& doc, |
3019
|
3378 bool mark_as_global, bool save_as_floats) |
604
|
3379 { |
1755
|
3380 FOUR_BYTE_INT name_len = name.length (); |
604
|
3381 |
3557
|
3382 os.write (X_CAST (char *, &name_len), 4); |
1755
|
3383 os << name; |
|
3384 |
|
3385 FOUR_BYTE_INT doc_len = doc.length (); |
604
|
3386 |
3557
|
3387 os.write (X_CAST (char *, &doc_len), 4); |
1755
|
3388 os << doc; |
604
|
3389 |
|
3390 char tmp; |
|
3391 |
|
3392 tmp = mark_as_global; |
3557
|
3393 os.write (X_CAST (char *, &tmp), 1); |
604
|
3394 |
3233
|
3395 if (tc.is_string ()) |
|
3396 { |
|
3397 tmp = 7; |
3557
|
3398 os.write (X_CAST (char *, &tmp), 1); |
3233
|
3399 FOUR_BYTE_INT nr = tc.rows (); |
3557
|
3400 os.write (X_CAST (char *, &nr), 4); |
3233
|
3401 charMatrix chm = tc.char_matrix_value (); |
|
3402 for (int i = 0; i < nr; i++) |
|
3403 { |
|
3404 FOUR_BYTE_INT len = chm.cols (); |
3557
|
3405 os.write (X_CAST (char *, &len), 4); |
3523
|
3406 std::string tstr = chm.row_as_string (i); |
3233
|
3407 const char *tmp = tstr.data (); |
3557
|
3408 os.write (X_CAST (char *, tmp), len); |
3233
|
3409 } |
|
3410 } |
|
3411 else if (tc.is_range ()) |
|
3412 { |
|
3413 tmp = 6; |
3557
|
3414 os.write (X_CAST (char *, &tmp), 1); |
3233
|
3415 tmp = (char) LS_DOUBLE; |
3557
|
3416 os.write (X_CAST (char *, &tmp), 1); |
3233
|
3417 Range r = tc.range_value (); |
|
3418 double bas = r.base (); |
|
3419 double lim = r.limit (); |
|
3420 double inc = r.inc (); |
3557
|
3421 os.write (X_CAST (char *, &bas), 8); |
|
3422 os.write (X_CAST (char *, &lim), 8); |
|
3423 os.write (X_CAST (char *, &inc), 8); |
3233
|
3424 } |
|
3425 else if (tc.is_real_scalar ()) |
604
|
3426 { |
|
3427 tmp = 1; |
3557
|
3428 os.write (X_CAST (char *, &tmp), 1); |
630
|
3429 tmp = (char) LS_DOUBLE; |
3557
|
3430 os.write (X_CAST (char *, &tmp), 1); |
604
|
3431 double tmp = tc.double_value (); |
3557
|
3432 os.write (X_CAST (char *, &tmp), 8); |
604
|
3433 } |
620
|
3434 else if (tc.is_real_matrix ()) |
604
|
3435 { |
|
3436 tmp = 2; |
3557
|
3437 os.write (X_CAST (char *, &tmp), 1); |
604
|
3438 Matrix m = tc.matrix_value (); |
|
3439 FOUR_BYTE_INT nr = m.rows (); |
|
3440 FOUR_BYTE_INT nc = m.columns (); |
3557
|
3441 os.write (X_CAST (char *, &nr), 4); |
|
3442 os.write (X_CAST (char *, &nc), 4); |
604
|
3443 int len = nr * nc; |
|
3444 save_type st = LS_DOUBLE; |
630
|
3445 if (save_as_floats) |
|
3446 { |
1963
|
3447 if (m.too_large_for_float ()) |
630
|
3448 { |
|
3449 warning ("save: some values too large to save as floats --"); |
|
3450 warning ("save: saving as doubles instead"); |
|
3451 } |
|
3452 else |
|
3453 st = LS_FLOAT; |
|
3454 } |
|
3455 else if (len > 8192) // XXX FIXME XXX -- make this configurable. |
604
|
3456 { |
|
3457 double max_val, min_val; |
1963
|
3458 if (m.all_integers (max_val, min_val)) |
604
|
3459 st = get_save_type (max_val, min_val); |
|
3460 } |
630
|
3461 const double *mtmp = m.data (); |
604
|
3462 write_doubles (os, mtmp, st, len); |
|
3463 } |
|
3464 else if (tc.is_complex_scalar ()) |
|
3465 { |
|
3466 tmp = 3; |
3557
|
3467 os.write (X_CAST (char *, &tmp), 1); |
630
|
3468 tmp = (char) LS_DOUBLE; |
3557
|
3469 os.write (X_CAST (char *, &tmp), 1); |
604
|
3470 Complex tmp = tc.complex_value (); |
3557
|
3471 os.write (X_CAST (char *, &tmp), 16); |
604
|
3472 } |
|
3473 else if (tc.is_complex_matrix ()) |
|
3474 { |
|
3475 tmp = 4; |
3557
|
3476 os.write (X_CAST (char *, &tmp), 1); |
604
|
3477 ComplexMatrix m = tc.complex_matrix_value (); |
|
3478 FOUR_BYTE_INT nr = m.rows (); |
|
3479 FOUR_BYTE_INT nc = m.columns (); |
3557
|
3480 os.write (X_CAST (char *, &nr), 4); |
|
3481 os.write (X_CAST (char *, &nc), 4); |
604
|
3482 int len = nr * nc; |
|
3483 save_type st = LS_DOUBLE; |
630
|
3484 if (save_as_floats) |
|
3485 { |
1963
|
3486 if (m.too_large_for_float ()) |
630
|
3487 { |
|
3488 warning ("save: some values too large to save as floats --"); |
|
3489 warning ("save: saving as doubles instead"); |
|
3490 } |
|
3491 else |
|
3492 st = LS_FLOAT; |
|
3493 } |
|
3494 else if (len > 4096) // XXX FIXME XXX -- make this configurable. |
604
|
3495 { |
|
3496 double max_val, min_val; |
1963
|
3497 if (m.all_integers (max_val, min_val)) |
604
|
3498 st = get_save_type (max_val, min_val); |
|
3499 } |
630
|
3500 const Complex *mtmp = m.data (); |
3145
|
3501 write_doubles (os, X_CAST (const double *, mtmp), st, 2*len); |
604
|
3502 } |
|
3503 else |
2799
|
3504 gripe_wrong_type_arg ("save", tc, false); |
|
3505 |
|
3506 return os; |
604
|
3507 } |
|
3508 |
3687
|
3509 #ifdef HAVE_HDF5 |
|
3510 |
|
3511 // Add an attribute named attr_name to loc_id (a simple scalar |
|
3512 // attribute with value 1). Return value is >= 0 on success. |
|
3513 static herr_t |
|
3514 hdf5_add_attr (hid_t loc_id, const char *attr_name) |
|
3515 { |
|
3516 herr_t retval = 0; |
|
3517 |
|
3518 hid_t as_id = H5Screate (H5S_SCALAR); |
|
3519 |
|
3520 if (as_id >= 0) |
|
3521 { |
|
3522 hid_t a_id = H5Acreate (loc_id, attr_name, |
|
3523 H5T_NATIVE_UCHAR, as_id, H5P_DEFAULT); |
|
3524 |
|
3525 if (a_id >= 0) |
|
3526 { |
|
3527 unsigned char attr_val = 1; |
|
3528 |
|
3529 retval = H5Awrite (a_id, H5T_NATIVE_UCHAR, (void*) &attr_val); |
|
3530 |
|
3531 H5Aclose (a_id); |
|
3532 } |
|
3533 else |
|
3534 retval = a_id; |
|
3535 |
|
3536 H5Sclose (as_id); |
|
3537 } |
|
3538 else |
|
3539 retval = as_id; |
|
3540 |
|
3541 return retval; |
|
3542 } |
|
3543 |
|
3544 |
|
3545 // save_type_to_hdf5 is not currently used, since hdf5 doesn't yet support |
|
3546 // automatic float<->integer conversions: |
|
3547 |
|
3548 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS |
|
3549 |
|
3550 // return the HDF5 type id corresponding to the Octave save_type |
|
3551 |
|
3552 static hid_t |
|
3553 save_type_to_hdf5 (save_type st) |
|
3554 { |
|
3555 switch (st) |
|
3556 { |
|
3557 case LS_U_CHAR: |
|
3558 return H5T_NATIVE_UCHAR; |
|
3559 |
|
3560 case LS_U_SHORT: |
|
3561 return H5T_NATIVE_USHORT; |
|
3562 |
|
3563 case LS_U_INT: |
|
3564 return H5T_NATIVE_UINT; |
|
3565 |
|
3566 case LS_CHAR: |
|
3567 return H5T_NATIVE_CHAR; |
|
3568 |
|
3569 case LS_SHORT: |
|
3570 return H5T_NATIVE_SHORT; |
|
3571 |
|
3572 case LS_INT: |
|
3573 return H5T_NATIVE_INT; |
|
3574 |
|
3575 case LS_FLOAT: |
|
3576 return H5T_NATIVE_FLOAT; |
|
3577 |
|
3578 case LS_DOUBLE: |
|
3579 default: |
|
3580 return H5T_NATIVE_DOUBLE; |
|
3581 } |
|
3582 } |
|
3583 #endif /* HAVE_HDF5_INT2FLOAT_CONVERSIONS */ |
|
3584 |
|
3585 // Add the data from TC to the HDF5 location loc_id, which could |
|
3586 // be either a file or a group within a file. Return true if |
|
3587 // successful. This function calls itself recursively for lists |
|
3588 // (stored as HDF5 groups). |
|
3589 |
|
3590 static bool |
|
3591 add_hdf5_data (hid_t loc_id, const octave_value& tc, |
|
3592 const std::string& name, const std::string& doc, |
|
3593 bool mark_as_global, bool save_as_floats) |
|
3594 { |
|
3595 hsize_t dims[3]; |
|
3596 hid_t type_id = -1, space_id = -1, data_id = -1; |
|
3597 bool data_is_group = 0; |
|
3598 bool retval = 0; |
|
3599 |
|
3600 if (tc.is_string ()) |
|
3601 { |
|
3602 int nr = tc.rows (); |
|
3603 charMatrix chm = tc.char_matrix_value (); |
|
3604 int nc = chm.cols (); |
|
3605 |
|
3606 // create datatype for (null-terminated) string to write from: |
|
3607 type_id = H5Tcopy (H5T_C_S1); H5Tset_size (type_id, nc + 1); |
|
3608 if (type_id < 0) |
|
3609 goto error_cleanup; |
|
3610 |
|
3611 dims[0] = nr; |
|
3612 space_id = H5Screate_simple (nr > 0 ? 1 : 0, dims, (hsize_t*) NULL); |
|
3613 if (space_id < 0) |
|
3614 goto error_cleanup; |
|
3615 |
|
3616 data_id = H5Dcreate (loc_id, name.c_str (), |
|
3617 type_id, space_id, H5P_DEFAULT); |
|
3618 if (data_id < 0) |
|
3619 goto error_cleanup; |
|
3620 |
|
3621 char *s = new char [nr * (nc + 1)]; |
|
3622 |
|
3623 for (int i = 0; i < nr; ++i) |
|
3624 { |
|
3625 std::string tstr = chm.row_as_string (i); |
|
3626 strcpy (s + i * (nc+1), tstr.c_str ()); |
|
3627 } |
|
3628 |
|
3629 if (H5Dwrite (data_id, type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
|
3630 (void*) s) < 0) { |
|
3631 delete [] s; |
|
3632 goto error_cleanup; |
|
3633 } |
|
3634 |
|
3635 delete [] s; |
|
3636 } |
|
3637 else if (tc.is_range ()) |
|
3638 { |
|
3639 space_id = H5Screate_simple (0, dims, (hsize_t*) NULL); |
|
3640 if (space_id < 0) |
|
3641 goto error_cleanup; |
|
3642 |
|
3643 type_id = hdf5_make_range_type (H5T_NATIVE_DOUBLE); |
|
3644 if (type_id < 0) |
|
3645 goto error_cleanup; |
|
3646 |
|
3647 data_id = H5Dcreate (loc_id, name.c_str (), |
|
3648 type_id, space_id, H5P_DEFAULT); |
|
3649 if (data_id < 0) |
|
3650 goto error_cleanup; |
|
3651 |
|
3652 Range r = tc.range_value (); |
|
3653 double range_vals[3]; |
|
3654 range_vals[0] = r.base (); |
|
3655 range_vals[1] = r.limit (); |
|
3656 range_vals[2] = r.inc (); |
|
3657 |
|
3658 if (H5Dwrite (data_id, type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
|
3659 (void*) range_vals) < 0) |
|
3660 goto error_cleanup; |
|
3661 } |
|
3662 else if (tc.is_real_scalar ()) |
|
3663 { |
|
3664 space_id = H5Screate_simple (0, dims, (hsize_t*) NULL); |
|
3665 if (space_id < 0) goto error_cleanup; |
|
3666 |
|
3667 data_id = H5Dcreate (loc_id, name.c_str (), |
|
3668 H5T_NATIVE_DOUBLE, space_id, H5P_DEFAULT); |
|
3669 if (data_id < 0) |
|
3670 goto error_cleanup; |
|
3671 |
|
3672 double tmp = tc.double_value (); |
|
3673 if (H5Dwrite (data_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, |
|
3674 H5P_DEFAULT, (void*) &tmp) < 0) |
|
3675 goto error_cleanup; |
|
3676 } |
|
3677 else if (tc.is_real_matrix ()) |
|
3678 { |
|
3679 Matrix m = tc.matrix_value (); |
|
3680 dims[1] = m.rows (); // Octave uses column-major, while |
|
3681 dims[0] = m.columns (); // HDF5 uses row-major ordering |
|
3682 |
|
3683 space_id = H5Screate_simple (dims[1] > 1 ?2:1, dims, (hsize_t*) NULL); |
|
3684 if (space_id < 0) |
|
3685 goto error_cleanup; |
|
3686 |
|
3687 hid_t save_type_id = H5T_NATIVE_DOUBLE; |
|
3688 |
|
3689 if (save_as_floats) |
|
3690 { |
|
3691 if (m.too_large_for_float ()) |
|
3692 { |
|
3693 warning ("save: some values too large to save as floats --"); |
|
3694 warning ("save: saving as doubles instead"); |
|
3695 } |
|
3696 else |
|
3697 save_type_id = H5T_NATIVE_FLOAT; |
|
3698 } |
|
3699 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS |
|
3700 // hdf5 currently doesn't support float/integer conversions |
|
3701 else |
|
3702 { |
|
3703 double max_val, min_val; |
|
3704 |
|
3705 if (m.all_integers (max_val, min_val)) |
|
3706 save_type_id |
|
3707 = save_type_to_hdf5 (get_save_type (max_val, min_val)); |
|
3708 } |
|
3709 #endif /* HAVE_HDF5_INT2FLOAT_CONVERSIONS */ |
|
3710 |
|
3711 data_id = H5Dcreate (loc_id, name.c_str (), |
|
3712 save_type_id, space_id, H5P_DEFAULT); |
|
3713 if (data_id < 0) |
|
3714 goto error_cleanup; |
|
3715 |
|
3716 double *mtmp = m.fortran_vec (); |
|
3717 if (H5Dwrite (data_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, |
|
3718 H5P_DEFAULT, (void*) mtmp) < 0) |
|
3719 goto error_cleanup; |
|
3720 } |
|
3721 else if (tc.is_complex_scalar ()) |
|
3722 { |
|
3723 space_id = H5Screate_simple (0, dims, (hsize_t*) NULL); |
|
3724 if (space_id < 0) |
|
3725 goto error_cleanup; |
|
3726 |
|
3727 type_id = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); |
|
3728 if (type_id < 0) |
|
3729 goto error_cleanup; |
|
3730 |
|
3731 data_id = H5Dcreate (loc_id, name.c_str (), |
|
3732 type_id, space_id, H5P_DEFAULT); |
|
3733 if (data_id < 0) |
|
3734 goto error_cleanup; |
|
3735 |
|
3736 Complex tmp = tc.complex_value (); |
|
3737 if (H5Dwrite (data_id, type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
|
3738 (void*) X_CAST (double*, &tmp)) < 0) |
|
3739 goto error_cleanup; |
|
3740 } |
|
3741 else if (tc.is_complex_matrix ()) |
|
3742 { |
|
3743 ComplexMatrix m = tc.complex_matrix_value (); |
|
3744 |
|
3745 dims[1] = m.rows (); // Octave uses column-major, while |
|
3746 dims[0] = m.columns (); // HDF5 uses row-major ordering |
|
3747 |
|
3748 space_id = H5Screate_simple (dims[1] > 1 ?2:1, dims, (hsize_t*) NULL); |
|
3749 if (space_id < 0) |
|
3750 goto error_cleanup; |
|
3751 |
|
3752 hid_t save_type_id = H5T_NATIVE_DOUBLE; |
|
3753 |
|
3754 if (save_as_floats) |
|
3755 { |
|
3756 if (m.too_large_for_float ()) |
|
3757 { |
|
3758 warning ("save: some values too large to save as floats --"); |
|
3759 warning ("save: saving as doubles instead"); |
|
3760 } |
|
3761 else |
|
3762 save_type_id = H5T_NATIVE_FLOAT; |
|
3763 } |
|
3764 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS |
|
3765 // hdf5 currently doesn't support float/integer conversions |
|
3766 else |
|
3767 { |
|
3768 double max_val, min_val; |
|
3769 |
|
3770 if (m.all_integers (max_val, min_val)) |
|
3771 save_type_id |
|
3772 = save_type_to_hdf5 (get_save_type (max_val, min_val)); |
|
3773 } |
|
3774 #endif /* HAVE_HDF5_INT2FLOAT_CONVERSIONS */ |
|
3775 |
|
3776 type_id = hdf5_make_complex_type (save_type_id); |
|
3777 if (type_id < 0) goto error_cleanup; |
|
3778 |
|
3779 data_id = H5Dcreate (loc_id, name.c_str (), |
|
3780 type_id, space_id, H5P_DEFAULT); |
|
3781 if (data_id < 0) |
|
3782 goto error_cleanup; |
|
3783 |
|
3784 hid_t complex_type_id = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); |
|
3785 if (complex_type_id < 0) |
|
3786 goto error_cleanup; |
|
3787 |
|
3788 Complex *mtmp = m.fortran_vec (); |
|
3789 if (H5Dwrite (data_id, complex_type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
|
3790 (void*) X_CAST (double *, mtmp)) < 0) |
|
3791 { |
|
3792 H5Tclose (complex_type_id); |
|
3793 goto error_cleanup; |
|
3794 } |
|
3795 |
|
3796 H5Tclose (complex_type_id); |
|
3797 } |
|
3798 else if (tc.is_list ()) |
|
3799 { |
|
3800 data_id = H5Gcreate (loc_id, name.c_str (), 0); |
|
3801 if (data_id < 0) |
|
3802 goto error_cleanup; |
|
3803 |
|
3804 data_is_group = 1; |
|
3805 |
|
3806 // recursively add each element of the list to this group |
|
3807 octave_value_list lst = tc.list_value (); |
|
3808 |
|
3809 for (int i = 0; i < lst.length (); ++i) |
|
3810 { |
|
3811 // should we use lst.name_tags () to label the elements? |
|
3812 char s[20]; |
|
3813 sprintf (s, "%d", i); |
|
3814 bool retval2 = add_hdf5_data (data_id, lst (i), s, "", |
|
3815 false, save_as_floats); |
|
3816 if (! retval2) |
|
3817 goto error_cleanup; |
|
3818 } |
|
3819 |
|
3820 // mark with an attribute "OCTAVE_LIST" with value 1 |
|
3821 // to distinguish from structures (also stored as HDF5 groups): |
3688
|
3822 if (hdf5_add_attr (data_id, "OCTAVE_LIST") < 0) |
3687
|
3823 goto error_cleanup; |
|
3824 } |
|
3825 else if (tc.is_map ()) |
|
3826 { |
|
3827 // an Octave structure |
|
3828 data_id = H5Gcreate (loc_id, name.c_str (), 0); |
|
3829 if (data_id < 0) |
|
3830 goto error_cleanup; |
|
3831 |
|
3832 data_is_group = 1; |
|
3833 |
|
3834 // recursively add each element of the structure to this group |
|
3835 Octave_map m = tc.map_value (); |
|
3836 Pix i = m.first (); |
|
3837 while (i) |
|
3838 { |
|
3839 bool retval2 = add_hdf5_data (data_id, |
|
3840 m.contents (i), m.key (i), "", |
|
3841 false, save_as_floats); |
|
3842 if (! retval2) |
|
3843 goto error_cleanup; |
|
3844 |
|
3845 // advance i to next element, or 0 |
|
3846 m.next (i); |
|
3847 } |
|
3848 } |
|
3849 else |
|
3850 { |
|
3851 gripe_wrong_type_arg ("save", tc, false); |
|
3852 goto error_cleanup; |
|
3853 } |
|
3854 |
|
3855 // attach doc string as comment: |
|
3856 if (doc.length () > 0 |
|
3857 && H5Gset_comment (loc_id, name.c_str (), doc.c_str ()) < 0) |
|
3858 goto error_cleanup; |
|
3859 |
|
3860 retval = 1; |
|
3861 |
|
3862 // if it's global, add an attribute "OCTAVE_GLOBAL" with value 1 |
|
3863 if (mark_as_global) |
3688
|
3864 retval = hdf5_add_attr (data_id, "OCTAVE_GLOBAL") >= 0; |
3687
|
3865 |
|
3866 error_cleanup: |
|
3867 |
|
3868 if (! retval) |
|
3869 error ("save: error while writing `%s' to hdf5 file", name.c_str ()); |
|
3870 |
|
3871 if (data_id >= 0) |
|
3872 { |
|
3873 if (data_is_group) |
|
3874 H5Gclose (data_id); |
|
3875 else |
|
3876 H5Dclose (data_id); |
|
3877 } |
|
3878 |
|
3879 if (space_id >= 0) |
|
3880 H5Sclose (space_id); |
|
3881 |
|
3882 if (type_id >= 0) |
|
3883 H5Tclose (type_id); |
|
3884 |
|
3885 return retval; |
|
3886 } |
|
3887 |
|
3888 // Write data from TC in HDF5 (binary) format to the stream OS, |
|
3889 // which must be an hdf5_ofstream, returning true on success. |
|
3890 |
|
3891 static bool |
|
3892 save_hdf5_data (std::ostream& os, const octave_value& tc, |
|
3893 const std::string& name, const std::string& doc, |
|
3894 bool mark_as_global, bool save_as_floats) |
|
3895 { |
|
3896 hdf5_ofstream& hs = (hdf5_ofstream&) os; |
|
3897 |
|
3898 return add_hdf5_data (hs.file_id, tc, name, doc, |
|
3899 mark_as_global, save_as_floats); |
|
3900 } |
|
3901 |
|
3902 #endif /* HAVE_HDF5 */ |
|
3903 |
3688
|
3904 static int |
|
3905 write_mat5_tag (std::ostream& is, int type, int bytes) |
|
3906 { |
|
3907 FOUR_BYTE_INT temp; |
|
3908 |
|
3909 if (bytes <= 4) |
|
3910 temp = (bytes << 16) + type; |
|
3911 else |
|
3912 { |
|
3913 temp = type; |
|
3914 if (! is.write ((char *)&temp, 4)) |
|
3915 goto data_write_error; |
|
3916 temp = bytes; |
|
3917 } |
|
3918 |
|
3919 if (! is.write ((char *)&temp, 4)) |
|
3920 goto data_write_error; |
|
3921 |
|
3922 return 0; |
|
3923 |
|
3924 data_write_error: |
|
3925 return 1; |
|
3926 } |
|
3927 |
|
3928 // write out the numeric values in M to OS, |
|
3929 // preceded by the appropriate tag. |
|
3930 static void |
|
3931 write_mat5_array (std::ostream& os, Matrix& m, const int save_as_floats) |
|
3932 { |
|
3933 int nr = m.rows (); |
|
3934 int nc = m.columns (); |
|
3935 double max_val, min_val; |
|
3936 save_type st = LS_DOUBLE; |
|
3937 mat5_data_type mst; |
|
3938 int size; |
|
3939 unsigned len; |
|
3940 double *data = m.fortran_vec (); |
|
3941 |
|
3942 // Have to use copy here to avoid writing over data accessed via |
|
3943 // Matrix::data(). |
|
3944 |
|
3945 #define MAT5_DO_WRITE(TYPE, data, count, stream) \ |
|
3946 do \ |
|
3947 { \ |
|
3948 TYPE *ptr = new TYPE [count]; \ |
|
3949 for (int i = 0; i < count; i++) \ |
|
3950 ptr[i] = X_CAST (TYPE, data[i]); \ |
|
3951 stream.write (X_CAST (char *, ptr), count * sizeof (TYPE)); \ |
|
3952 delete [] ptr ; \ |
|
3953 } \ |
|
3954 while (0) |
|
3955 |
|
3956 if (save_as_floats) |
|
3957 { |
|
3958 if (m.too_large_for_float ()) |
|
3959 { |
|
3960 warning ("save: some values too large to save as floats --"); |
|
3961 warning ("save: saving as doubles instead"); |
|
3962 } |
|
3963 else |
|
3964 st = LS_FLOAT; |
|
3965 } |
|
3966 |
|
3967 if (m.all_integers (max_val, min_val)) |
|
3968 st = get_save_type (max_val, min_val); |
|
3969 |
|
3970 switch (st) |
|
3971 { |
|
3972 default: |
|
3973 case LS_DOUBLE: mst = miDOUBLE; size = 8; break; |
|
3974 case LS_FLOAT: mst = miSINGLE; size = 4; break; |
|
3975 case LS_U_CHAR: mst = miUINT8; size = 1; break; |
|
3976 case LS_U_SHORT: mst = miUINT16; size = 2; break; |
|
3977 case LS_U_INT: mst = miUINT32; size = 4; break; |
|
3978 case LS_CHAR: mst = miINT8; size = 1; break; |
|
3979 case LS_SHORT: mst = miINT16; size = 2; break; |
|
3980 case LS_INT: mst = miINT32; size = 4; break; |
|
3981 } |
|
3982 |
|
3983 len = nr*nc*size; |
|
3984 write_mat5_tag (os, mst, len); |
|
3985 |
|
3986 { |
|
3987 switch (st) |
|
3988 { |
|
3989 case LS_U_CHAR: |
|
3990 MAT5_DO_WRITE (unsigned char, data, nr*nc, os); |
|
3991 break; |
|
3992 |
|
3993 case LS_U_SHORT: |
|
3994 MAT5_DO_WRITE (unsigned TWO_BYTE_INT, data, nr*nc, os); |
|
3995 break; |
|
3996 |
|
3997 case LS_U_INT: |
|
3998 MAT5_DO_WRITE (unsigned FOUR_BYTE_INT, data, nr*nc, os); |
|
3999 break; |
|
4000 |
|
4001 // provide for 64 bit ints, even though get_save_type does |
|
4002 // not yet implement them |
|
4003 #ifdef EIGHT_BYTE_INT |
|
4004 case LS_U_LONG: |
|
4005 MAT5_DO_WRITE (unsigned EIGHT_BYTE_INT, data, nr*nc, os); |
|
4006 break; |
|
4007 #endif |
|
4008 |
|
4009 case LS_CHAR: |
|
4010 MAT5_DO_WRITE (signed char, data, nr*nc, os); |
|
4011 break; |
|
4012 |
|
4013 case LS_SHORT: |
|
4014 MAT5_DO_WRITE (TWO_BYTE_INT, data, nr*nc, os); |
|
4015 break; |
|
4016 |
|
4017 case LS_INT: |
|
4018 MAT5_DO_WRITE (FOUR_BYTE_INT, data, nr*nc, os); |
|
4019 break; |
|
4020 |
|
4021 #ifdef EIGHT_BYTE_INT |
|
4022 case LS_LONG: |
|
4023 MAT5_DO_WRITE (EIGHT_BYTE_INT, data, nr*nc, os); |
|
4024 break; |
|
4025 #endif |
|
4026 |
|
4027 case LS_FLOAT: |
|
4028 MAT5_DO_WRITE (float, data, nr*nc, os); |
|
4029 break; |
|
4030 |
|
4031 case LS_DOUBLE: // No conversion necessary. |
|
4032 os.write (X_CAST (char *, data), len); |
|
4033 break; |
|
4034 |
|
4035 default: |
|
4036 (*current_liboctave_error_handler) |
|
4037 ("unrecognized data format requested"); |
|
4038 break; |
|
4039 } |
|
4040 } |
|
4041 if (PAD (len) > len) |
|
4042 { |
|
4043 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
4044 os.write (buf, PAD (len) - len); |
|
4045 } |
|
4046 } |
|
4047 |
|
4048 // save the data from TC along with the corresponding NAME on stream |
|
4049 // OS in the MatLab version 5 binary format. Return true on success. |
|
4050 |
|
4051 static bool |
|
4052 save_mat5_binary_element (std::ostream& os, |
|
4053 const octave_value& tc, const std::string& name, |
|
4054 bool mark_as_global, bool save_as_floats) |
|
4055 { |
|
4056 FOUR_BYTE_INT flags=0; |
|
4057 FOUR_BYTE_INT junk=0; |
|
4058 FOUR_BYTE_INT nr; |
|
4059 FOUR_BYTE_INT nc; |
|
4060 streampos fixup, contin; |
|
4061 |
|
4062 // element type and length |
|
4063 fixup = os.tellp (); |
|
4064 write_mat5_tag (os, miMATRIX, 99); // we don't know the real length yet |
|
4065 |
|
4066 // array flags subelement |
|
4067 write_mat5_tag (os, miUINT32, 8); |
|
4068 |
|
4069 if (mark_as_global) |
|
4070 flags |= 0x0400; |
|
4071 |
|
4072 if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
4073 flags |= 0x0800; |
|
4074 |
|
4075 if (tc.is_string ()) |
|
4076 flags |= mxCHAR_CLASS; |
|
4077 else if (tc.is_real_scalar ()) |
|
4078 flags |= mxDOUBLE_CLASS; |
|
4079 else if (tc.is_real_matrix ()) |
|
4080 flags |= mxDOUBLE_CLASS; |
|
4081 else if (tc.is_complex_scalar ()) |
|
4082 flags |= mxDOUBLE_CLASS; |
|
4083 else if (tc.is_complex_matrix ()) |
|
4084 flags |= mxDOUBLE_CLASS; |
|
4085 else if (tc.is_map ()) |
|
4086 flags |= mxSTRUCT_CLASS; |
|
4087 else |
|
4088 { |
|
4089 gripe_wrong_type_arg ("save", tc, false); |
|
4090 goto error_cleanup; |
|
4091 } |
|
4092 |
|
4093 os.write ((char *)&flags, 4); |
|
4094 os.write ((char *)&junk, 4); |
|
4095 |
|
4096 // dimensions array subelement |
|
4097 { |
|
4098 if (tc.is_string ()) |
|
4099 { |
|
4100 charMatrix chm = tc.char_matrix_value (); |
|
4101 nr = tc.rows (); |
|
4102 nc = chm.cols (); |
|
4103 } |
|
4104 else if (tc.is_real_scalar () || tc.is_complex_scalar () || tc.is_map ()) |
|
4105 { |
|
4106 nr = nc = 1; |
|
4107 } |
|
4108 else if (tc.is_real_matrix ()) |
|
4109 { |
|
4110 Matrix m = tc.matrix_value (); |
|
4111 nr = m.rows (); |
|
4112 nc = m.columns (); |
|
4113 } |
|
4114 else if (tc.is_complex_matrix ()) |
|
4115 { |
|
4116 ComplexMatrix m = tc.complex_matrix_value (); |
|
4117 nr = m.rows (); |
|
4118 nc = m.columns (); |
|
4119 } |
|
4120 |
|
4121 write_mat5_tag (os, miINT32, 8); |
|
4122 os.write ((char *)&nr, 4); |
|
4123 os.write ((char *)&nc, 4); |
|
4124 } |
|
4125 |
|
4126 // array name subelement |
|
4127 { |
|
4128 int namelen = name.length (); |
|
4129 |
|
4130 if (namelen > 31) |
|
4131 namelen = 31; // only 31 char names permitted in mat file |
|
4132 |
|
4133 int paddedlength = PAD (namelen); |
|
4134 |
|
4135 write_mat5_tag (os, miINT8, namelen); |
|
4136 char * paddedname = new char [paddedlength]; |
|
4137 memset (paddedname, 0, paddedlength); |
|
4138 strncpy (paddedname, name.c_str (), namelen); |
|
4139 os.write (paddedname, paddedlength); |
|
4140 delete [] paddedname; |
|
4141 } |
|
4142 |
|
4143 // data element |
|
4144 if (tc.is_string ()) |
|
4145 { |
|
4146 charMatrix chm = tc.char_matrix_value (); |
|
4147 int nc = chm.cols (); |
|
4148 int len = nr*nc*2; |
|
4149 int paddedlength = PAD (nr*nc*2); |
|
4150 |
|
4151 TWO_BYTE_INT *buf = new TWO_BYTE_INT[nc+3]; |
|
4152 write_mat5_tag (os, miUINT16, len); |
|
4153 |
|
4154 for (int i = 0; i < nr; i++) |
|
4155 { |
|
4156 std::string tstr = chm.row_as_string (i); |
|
4157 const char *s = tstr.data (); |
|
4158 |
|
4159 for (int j = 0; j < nc; j++) |
|
4160 buf[j] = *s++; |
|
4161 |
|
4162 os.write ((char *)buf, nc*2); |
|
4163 } |
|
4164 |
|
4165 if (paddedlength > len) |
|
4166 os.write ((char *)buf, paddedlength - len); |
|
4167 |
|
4168 delete [] buf; |
|
4169 } |
|
4170 else if (tc.is_real_scalar () || tc.is_real_matrix ()) |
|
4171 { |
|
4172 Matrix m = tc.matrix_value (); |
|
4173 |
|
4174 write_mat5_array (os, m, save_as_floats); |
|
4175 } |
|
4176 else if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
4177 { |
|
4178 ComplexMatrix m_cmplx = tc.complex_matrix_value (); |
|
4179 Matrix m = ::real (m_cmplx); |
|
4180 |
|
4181 for (int part=0; part < 2; part++) |
|
4182 { |
|
4183 // real part, then complex part |
|
4184 write_mat5_array (os, m, save_as_floats); |
|
4185 m = ::imag (m_cmplx); |
|
4186 } |
|
4187 } |
|
4188 else if (tc.is_map ()) |
|
4189 { |
|
4190 // an Octave structure */ |
|
4191 // recursively write each element of the structure |
|
4192 Octave_map m = tc.map_value (); |
|
4193 Pix i; |
|
4194 |
|
4195 { |
|
4196 char buf[32]; |
|
4197 FOUR_BYTE_INT maxfieldnamelength = 32; |
|
4198 int fieldcnt = 0; |
|
4199 |
|
4200 for (i = m.first (); i; m.next (i)) |
|
4201 fieldcnt++; |
|
4202 |
|
4203 write_mat5_tag (os, miINT32, 4); |
|
4204 os.write ((char *)&maxfieldnamelength, 4); |
|
4205 write_mat5_tag (os, miINT8, fieldcnt*32); |
|
4206 |
|
4207 for (i = m.first (); i; m.next (i)) |
|
4208 { |
|
4209 // write the name of each element |
|
4210 string tstr = m.key (i); |
|
4211 memset (buf, 0, 32); |
|
4212 strncpy (buf, tstr.c_str (), 31); // only 31 char names permitted |
|
4213 os.write (buf, 32); |
|
4214 } |
|
4215 |
|
4216 for (i = m.first (); i; m.next (i)) |
|
4217 { |
|
4218 // write the data of each element |
|
4219 bool retval2 = save_mat5_binary_element (os, m.contents (i), "", |
|
4220 mark_as_global, |
|
4221 save_as_floats); |
|
4222 |
|
4223 if (! retval2) |
|
4224 goto error_cleanup; |
|
4225 } |
|
4226 } |
|
4227 } |
|
4228 else |
|
4229 gripe_wrong_type_arg ("save", tc, false); |
|
4230 |
|
4231 contin = os.tellp (); |
|
4232 os.seekp (fixup); |
|
4233 write_mat5_tag (os, miMATRIX, contin - fixup - 8); // the actual length |
|
4234 os.seekp (contin); |
|
4235 |
|
4236 return true; |
|
4237 |
|
4238 error_cleanup: |
|
4239 error ("save: error while writing `%s' to MAT file", name.c_str ()); |
|
4240 |
|
4241 return false; |
|
4242 } |
|
4243 |
667
|
4244 // Save the data from TC along with the corresponding NAME on stream OS |
3688
|
4245 // in the MatLab version 4 binary format. |
667
|
4246 |
2799
|
4247 static bool |
3523
|
4248 save_mat_binary_data (std::ostream& os, const octave_value& tc, |
|
4249 const std::string& name) |
667
|
4250 { |
|
4251 FOUR_BYTE_INT mopt = 0; |
|
4252 |
|
4253 mopt += tc.is_string () ? 1 : 0; |
2318
|
4254 |
|
4255 oct_mach_info::float_format flt_fmt = |
|
4256 oct_mach_info::native_float_format ();; |
|
4257 |
|
4258 mopt += 1000 * float_format_to_mopt_digit (flt_fmt); |
667
|
4259 |
3557
|
4260 os.write (X_CAST (char *, &mopt), 4); |
667
|
4261 |
|
4262 FOUR_BYTE_INT nr = tc.rows (); |
3557
|
4263 os.write (X_CAST (char *, &nr), 4); |
667
|
4264 |
|
4265 FOUR_BYTE_INT nc = tc.columns (); |
3557
|
4266 os.write (X_CAST (char *, &nc), 4); |
667
|
4267 |
|
4268 int len = nr * nc; |
|
4269 |
|
4270 FOUR_BYTE_INT imag = tc.is_complex_type () ? 1 : 0; |
3557
|
4271 os.write (X_CAST (char *, &imag), 4); |
667
|
4272 |
2345
|
4273 // LEN includes the terminating character, and the file is also |
|
4274 // supposed to include it. |
|
4275 |
|
4276 FOUR_BYTE_INT name_len = name.length () + 1; |
667
|
4277 |
3557
|
4278 os.write (X_CAST (char *, &name_len), 4); |
2345
|
4279 os << name << '\0'; |
667
|
4280 |
3233
|
4281 if (tc.is_string ()) |
|
4282 { |
|
4283 unwind_protect::begin_frame ("save_mat_binary_data"); |
|
4284 unwind_protect_int (Vimplicit_str_to_num_ok); |
|
4285 Vimplicit_str_to_num_ok = true; |
|
4286 Matrix m = tc.matrix_value (); |
3557
|
4287 os.write (X_CAST (char *, m.data ()), 8 * len); |
3233
|
4288 unwind_protect::run_frame ("save_mat_binary_data"); |
|
4289 } |
|
4290 else if (tc.is_range ()) |
|
4291 { |
|
4292 Range r = tc.range_value (); |
|
4293 double base = r.base (); |
|
4294 double inc = r.inc (); |
|
4295 int nel = r.nelem (); |
|
4296 for (int i = 0; i < nel; i++) |
|
4297 { |
|
4298 double x = base + i * inc; |
3557
|
4299 os.write (X_CAST (char *, &x), 8); |
3233
|
4300 } |
|
4301 } |
|
4302 else if (tc.is_real_scalar ()) |
667
|
4303 { |
|
4304 double tmp = tc.double_value (); |
3557
|
4305 os.write (X_CAST (char *, &tmp), 8); |
667
|
4306 } |
911
|
4307 else if (tc.is_real_matrix ()) |
667
|
4308 { |
|
4309 Matrix m = tc.matrix_value (); |
3557
|
4310 os.write (X_CAST (char *, m.data ()), 8 * len); |
667
|
4311 } |
|
4312 else if (tc.is_complex_scalar ()) |
|
4313 { |
|
4314 Complex tmp = tc.complex_value (); |
3557
|
4315 os.write (X_CAST (char *, &tmp), 16); |
667
|
4316 } |
|
4317 else if (tc.is_complex_matrix ()) |
|
4318 { |
|
4319 ComplexMatrix m_cmplx = tc.complex_matrix_value (); |
3688
|
4320 Matrix m = ::real (m_cmplx); |
3557
|
4321 os.write (X_CAST (char *, m.data ()), 8 * len); |
3688
|
4322 m = ::imag (m_cmplx); |
3557
|
4323 os.write (X_CAST (char *, m.data ()), 8 * len); |
667
|
4324 } |
|
4325 else |
2799
|
4326 gripe_wrong_type_arg ("save", tc, false); |
|
4327 |
|
4328 return os; |
667
|
4329 } |
|
4330 |
620
|
4331 static void |
3523
|
4332 ascii_save_type (std::ostream& os, const char *type, bool mark_as_global) |
620
|
4333 { |
|
4334 if (mark_as_global) |
|
4335 os << "# type: global "; |
|
4336 else |
|
4337 os << "# type: "; |
|
4338 |
|
4339 os << type << "\n"; |
|
4340 } |
|
4341 |
872
|
4342 static Matrix |
|
4343 strip_infnan (const Matrix& m) |
|
4344 { |
|
4345 int nr = m.rows (); |
|
4346 int nc = m.columns (); |
|
4347 |
|
4348 Matrix retval (nr, nc); |
|
4349 |
|
4350 int k = 0; |
|
4351 for (int i = 0; i < nr; i++) |
|
4352 { |
|
4353 for (int j = 0; j < nc; j++) |
|
4354 { |
2305
|
4355 double d = m (i, j); |
872
|
4356 if (xisnan (d)) |
|
4357 goto next_row; |
|
4358 else |
2305
|
4359 retval (k, j) = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
872
|
4360 } |
|
4361 k++; |
|
4362 |
|
4363 next_row: |
|
4364 continue; |
|
4365 } |
|
4366 |
|
4367 if (k > 0) |
|
4368 retval.resize (k, nc); |
|
4369 |
|
4370 return retval; |
|
4371 } |
|
4372 |
|
4373 static ComplexMatrix |
|
4374 strip_infnan (const ComplexMatrix& m) |
|
4375 { |
|
4376 int nr = m.rows (); |
|
4377 int nc = m.columns (); |
|
4378 |
|
4379 ComplexMatrix retval (nr, nc); |
|
4380 |
|
4381 int k = 0; |
|
4382 for (int i = 0; i < nr; i++) |
|
4383 { |
|
4384 for (int j = 0; j < nc; j++) |
|
4385 { |
2305
|
4386 Complex c = m (i, j); |
872
|
4387 if (xisnan (c)) |
|
4388 goto next_row; |
|
4389 else |
|
4390 { |
|
4391 double re = real (c); |
|
4392 double im = imag (c); |
|
4393 |
|
4394 re = xisinf (re) ? (re > 0 ? OCT_RBV : -OCT_RBV) : re; |
|
4395 im = xisinf (im) ? (im > 0 ? OCT_RBV : -OCT_RBV) : im; |
|
4396 |
2305
|
4397 retval (k, j) = Complex (re, im); |
872
|
4398 } |
|
4399 } |
|
4400 k++; |
|
4401 |
|
4402 next_row: |
|
4403 continue; |
|
4404 } |
|
4405 |
|
4406 if (k > 0) |
|
4407 retval.resize (k, nc); |
|
4408 |
|
4409 return retval; |
|
4410 } |
|
4411 |
620
|
4412 // Save the data from TC along with the corresponding NAME, and global |
604
|
4413 // flag MARK_AS_GLOBAL on stream OS in the plain text format described |
1755
|
4414 // above for load_ascii_data. If NAME is empty, the name: line is not |
604
|
4415 // generated. PRECISION specifies the number of decimal digits to print. |
3019
|
4416 // If STRIP_NAN_AND_INF is TRUE, rows containing NaNs are deleted, |
872
|
4417 // and Infinite values are converted to +/-OCT_RBV (A Real Big Value, |
|
4418 // but not so big that gnuplot can't handle it when trying to compute |
|
4419 // axis ranges, etc.). |
|
4420 // |
|
4421 // Assumes ranges and strings cannot contain Inf or NaN values. |
|
4422 // |
|
4423 // Returns 1 for success and 0 for failure. |
604
|
4424 |
|
4425 // XXX FIXME XXX -- should probably write the help string here too. |
|
4426 |
2799
|
4427 bool |
3523
|
4428 save_ascii_data (std::ostream& os, const octave_value& tc, |
|
4429 const std::string& name, bool strip_nan_and_inf, |
2799
|
4430 bool mark_as_global, int precision) |
604
|
4431 { |
2799
|
4432 bool success = true; |
620
|
4433 |
604
|
4434 if (! precision) |
2194
|
4435 precision = Vsave_precision; |
604
|
4436 |
1755
|
4437 if (! name.empty ()) |
604
|
4438 os << "# name: " << name << "\n"; |
|
4439 |
|
4440 long old_precision = os.precision (); |
|
4441 os.precision (precision); |
|
4442 |
3233
|
4443 if (tc.is_string ()) |
|
4444 { |
|
4445 ascii_save_type (os, "string array", mark_as_global); |
|
4446 charMatrix chm = tc.char_matrix_value (); |
|
4447 int elements = chm.rows (); |
|
4448 os << "# elements: " << elements << "\n"; |
|
4449 for (int i = 0; i < elements; i++) |
|
4450 { |
|
4451 int len = chm.cols (); |
|
4452 os << "# length: " << len << "\n"; |
3523
|
4453 std::string tstr = chm.row_as_string (i); |
3233
|
4454 const char *tmp = tstr.data (); |
3557
|
4455 os.write (X_CAST (char *, tmp), len); |
3233
|
4456 os << "\n"; |
|
4457 } |
|
4458 } |
|
4459 else if (tc.is_range ()) |
|
4460 { |
|
4461 ascii_save_type (os, "range", mark_as_global); |
|
4462 Range tmp = tc.range_value (); |
|
4463 os << "# base, limit, increment\n" |
|
4464 << tmp.base () << " " |
|
4465 << tmp.limit () << " " |
|
4466 << tmp.inc () << "\n"; |
|
4467 } |
|
4468 else if (tc.is_real_scalar ()) |
620
|
4469 { |
|
4470 ascii_save_type (os, "scalar", mark_as_global); |
872
|
4471 |
|
4472 double d = tc.double_value (); |
|
4473 if (strip_nan_and_inf) |
|
4474 { |
|
4475 if (xisnan (d)) |
|
4476 { |
|
4477 error ("only value to plot is NaN"); |
2799
|
4478 success = false; |
872
|
4479 } |
|
4480 else |
|
4481 { |
|
4482 d = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
|
4483 os << d << "\n"; |
|
4484 } |
|
4485 } |
|
4486 else |
|
4487 os << d << "\n"; |
620
|
4488 } |
|
4489 else if (tc.is_real_matrix ()) |
|
4490 { |
|
4491 ascii_save_type (os, "matrix", mark_as_global); |
|
4492 os << "# rows: " << tc.rows () << "\n" |
872
|
4493 << "# columns: " << tc.columns () << "\n"; |
|
4494 |
|
4495 Matrix tmp = tc.matrix_value (); |
|
4496 if (strip_nan_and_inf) |
|
4497 tmp = strip_infnan (tmp); |
|
4498 |
|
4499 os << tmp; |
620
|
4500 } |
|
4501 else if (tc.is_complex_scalar ()) |
|
4502 { |
|
4503 ascii_save_type (os, "complex scalar", mark_as_global); |
872
|
4504 |
|
4505 Complex c = tc.complex_value (); |
|
4506 if (strip_nan_and_inf) |
|
4507 { |
|
4508 if (xisnan (c)) |
|
4509 { |
|
4510 error ("only value to plot is NaN"); |
2799
|
4511 success = false; |
872
|
4512 } |
|
4513 else |
|
4514 { |
|
4515 double re = real (c); |
|
4516 double im = imag (c); |
|
4517 |
|
4518 re = xisinf (re) ? (re > 0 ? OCT_RBV : -OCT_RBV) : re; |
|
4519 im = xisinf (im) ? (im > 0 ? OCT_RBV : -OCT_RBV) : im; |
|
4520 |
|
4521 c = Complex (re, im); |
|
4522 |
|
4523 os << c << "\n"; |
|
4524 } |
|
4525 } |
|
4526 else |
|
4527 os << c << "\n"; |
620
|
4528 } |
|
4529 else if (tc.is_complex_matrix ()) |
604
|
4530 { |
620
|
4531 ascii_save_type (os, "complex matrix", mark_as_global); |
|
4532 os << "# rows: " << tc.rows () << "\n" |
875
|
4533 << "# columns: " << tc.columns () << "\n"; |
|
4534 |
|
4535 ComplexMatrix tmp = tc.complex_matrix_value (); |
872
|
4536 if (strip_nan_and_inf) |
|
4537 tmp = strip_infnan (tmp); |
|
4538 |
|
4539 os << tmp; |
620
|
4540 } |
|
4541 else |
2799
|
4542 gripe_wrong_type_arg ("save", tc, false); |
604
|
4543 |
|
4544 os.precision (old_precision); |
|
4545 |
872
|
4546 return (os && success); |
604
|
4547 } |
|
4548 |
|
4549 // Save the info from sr on stream os in the format specified by fmt. |
|
4550 |
|
4551 static void |
3523
|
4552 do_save (std::ostream& os, symbol_record *sr, load_save_format fmt, |
630
|
4553 int save_as_floats) |
604
|
4554 { |
|
4555 if (! sr->is_variable ()) |
|
4556 { |
|
4557 error ("save: can only save variables, not functions"); |
|
4558 return; |
|
4559 } |
|
4560 |
3523
|
4561 std::string name = sr->name (); |
|
4562 std::string help = sr->help (); |
604
|
4563 int global = sr->is_linked_to_global (); |
2970
|
4564 |
|
4565 octave_value tc = sr->def (); |
604
|
4566 |
1755
|
4567 if (tc.is_undefined ()) |
604
|
4568 return; |
|
4569 |
|
4570 switch (fmt) |
|
4571 { |
|
4572 case LS_ASCII: |
2799
|
4573 save_ascii_data (os, tc, name, false, global); |
604
|
4574 break; |
|
4575 |
|
4576 case LS_BINARY: |
630
|
4577 save_binary_data (os, tc, name, help, global, save_as_floats); |
604
|
4578 break; |
|
4579 |
667
|
4580 case LS_MAT_BINARY: |
|
4581 save_mat_binary_data (os, tc, name); |
|
4582 break; |
|
4583 |
3687
|
4584 #ifdef HAVE_HDF5 |
|
4585 case LS_HDF5: |
|
4586 save_hdf5_data (os, tc, name, help, global, save_as_floats); |
|
4587 break; |
|
4588 #endif /* HAVE_HDF5 */ |
|
4589 |
3688
|
4590 case LS_MAT5_BINARY: |
|
4591 save_mat5_binary_element (os, tc, name, global, save_as_floats); |
|
4592 break; |
|
4593 |
604
|
4594 default: |
775
|
4595 gripe_unrecognized_data_fmt ("save"); |
604
|
4596 break; |
|
4597 } |
|
4598 } |
|
4599 |
|
4600 // Save variables with names matching PATTERN on stream OS in the |
3019
|
4601 // format specified by FMT. If SAVE_BUILTINS is TRUE, also save |
604
|
4602 // builtin variables with names that match PATTERN. |
|
4603 |
|
4604 static int |
3523
|
4605 save_vars (std::ostream& os, const std::string& pattern, bool save_builtins, |
630
|
4606 load_save_format fmt, int save_as_floats) |
604
|
4607 { |
3355
|
4608 Array<symbol_record *> vars = curr_sym_tab->glob |
|
4609 (pattern, symbol_record::USER_VARIABLE, SYMTAB_ALL_SCOPES); |
|
4610 |
|
4611 int saved = vars.length (); |
|
4612 |
|
4613 for (int i = 0; i < saved; i++) |
620
|
4614 { |
3687
|
4615 do_save (os, vars (i), fmt, save_as_floats); |
620
|
4616 |
|
4617 if (error_state) |
|
4618 break; |
|
4619 } |
604
|
4620 |
620
|
4621 if (! error_state && save_builtins) |
604
|
4622 { |
3355
|
4623 vars = global_sym_tab->glob |
|
4624 (pattern, symbol_record::BUILTIN_VARIABLE, SYMTAB_ALL_SCOPES); |
|
4625 |
|
4626 int count = vars.length (); |
604
|
4627 |
|
4628 saved += count; |
|
4629 |
3355
|
4630 for (int i = 0; i < count; i++) |
620
|
4631 { |
3687
|
4632 do_save (os, vars (i), fmt, save_as_floats); |
620
|
4633 |
|
4634 if (error_state) |
|
4635 break; |
|
4636 } |
604
|
4637 } |
|
4638 |
|
4639 return saved; |
|
4640 } |
|
4641 |
|
4642 static load_save_format |
|
4643 get_default_save_format (void) |
|
4644 { |
|
4645 load_save_format retval = LS_ASCII; |
|
4646 |
3523
|
4647 std::string fmt = Vdefault_save_format; |
1755
|
4648 |
|
4649 if (fmt == "binary") |
604
|
4650 retval = LS_BINARY; |
1755
|
4651 else if (fmt == "mat-binary" || fmt =="mat_binary") |
3688
|
4652 retval = LS_MAT5_BINARY; |
|
4653 else if (fmt == "mat4-binary" || fmt =="mat4_binary") |
911
|
4654 retval = LS_MAT_BINARY; |
3687
|
4655 #ifdef HAVE_HDF5 |
|
4656 else if (fmt == "hdf5") |
|
4657 retval = LS_HDF5; |
|
4658 #endif /* HAVE_HDF5 */ |
604
|
4659 |
|
4660 return retval; |
|
4661 } |
|
4662 |
863
|
4663 static void |
3523
|
4664 write_header (std::ostream& os, load_save_format format) |
863
|
4665 { |
3185
|
4666 switch (format) |
863
|
4667 { |
3185
|
4668 case LS_BINARY: |
|
4669 { |
|
4670 os << (oct_mach_info::words_big_endian () |
|
4671 ? "Octave-1-B" : "Octave-1-L"); |
|
4672 |
|
4673 oct_mach_info::float_format flt_fmt = |
|
4674 oct_mach_info::native_float_format (); |
|
4675 |
|
4676 char tmp = (char) float_format_to_mopt_digit (flt_fmt); |
|
4677 |
3557
|
4678 os.write (X_CAST (char *, &tmp), 1); |
3185
|
4679 } |
3688
|
4680 break; |
|
4681 |
|
4682 case LS_MAT5_BINARY: |
|
4683 { |
|
4684 char *versionmagic; |
|
4685 TWO_BYTE_INT number = *(TWO_BYTE_INT *)"\x00\x01"; |
|
4686 struct tm bdt; |
|
4687 time_t now; |
|
4688 char headertext[128]; |
|
4689 |
|
4690 time (&now); |
|
4691 bdt = *gmtime (&now); |
|
4692 memset (headertext, ' ', 124); |
|
4693 // ISO 8601 format date |
|
4694 strftime (headertext, 124, "MATLAB 5.0 MAT-file, written by Octave " |
|
4695 OCTAVE_VERSION ", %Y-%m-%d %T UTC", &bdt); |
|
4696 |
|
4697 // The first pair of bytes give the version of the MAT file |
|
4698 // format. The second pair of bytes form a magic number which |
|
4699 // signals a MAT file. MAT file data are always written in |
|
4700 // native byte order. The order of the bytes in the second |
|
4701 // pair indicates whether the file was written by a big- or |
|
4702 // little-endian machine. However, the version number is |
|
4703 // written in the *opposite* byte order from everything else! |
|
4704 if (number == 1) |
|
4705 versionmagic = "\x01\x00\x4d\x49"; // this machine is big endian |
|
4706 else |
|
4707 versionmagic = "\x00\x01\x49\x4d"; // this machine is little endian |
|
4708 |
|
4709 memcpy (headertext+124, versionmagic, 4); |
|
4710 os.write (headertext, 128); |
|
4711 } |
|
4712 |
|
4713 break; |
3185
|
4714 |
3687
|
4715 #ifdef HAVE_HDF5 |
|
4716 case LS_HDF5: |
|
4717 #endif /* HAVE_HDF5 */ |
3185
|
4718 case LS_ASCII: |
|
4719 { |
3258
|
4720 octave_gmtime now; |
3523
|
4721 std::string time_string = now.asctime (); |
3185
|
4722 time_string = time_string.substr (0, time_string.length () - 1); |
3687
|
4723 std::ostream *s = &os; |
|
4724 #ifdef HAVE_HDF5 |
|
4725 // for HDF5, write data to a string instead of to os, |
|
4726 // and then save the string as the HDF5 file's "comment" field. |
|
4727 std::ostrstream ss; |
|
4728 if (format == LS_HDF5) |
|
4729 s = &ss; |
|
4730 #endif /* HAVE_HDF5 */ |
|
4731 |
|
4732 *s << "# Created by Octave " OCTAVE_VERSION ", " |
3185
|
4733 << time_string |
|
4734 << " <" |
|
4735 << octave_env::get_user_name () |
|
4736 << "@" |
|
4737 << octave_env::get_host_name () |
3687
|
4738 << ">"; |
|
4739 |
|
4740 #ifdef HAVE_HDF5 |
|
4741 if (format != LS_HDF5) // don't append newline for HDF5 |
|
4742 #endif /* HAVE_HDF5 */ |
|
4743 *s << "\n"; |
|
4744 |
|
4745 #ifdef HAVE_HDF5 |
|
4746 if (format == LS_HDF5) |
|
4747 { |
|
4748 hdf5_ofstream& hs = (hdf5_ofstream&) os; |
|
4749 H5Gset_comment (hs.file_id, "/", ss.str ()); |
|
4750 } |
|
4751 #endif /* HAVE_HDF5 */ |
3185
|
4752 } |
|
4753 break; |
|
4754 |
|
4755 default: |
|
4756 break; |
863
|
4757 } |
|
4758 } |
|
4759 |
|
4760 static void |
3687
|
4761 save_vars (const std::string_vector& argv, int argv_idx, int argc, |
3523
|
4762 std::ostream& os, bool save_builtins, load_save_format fmt, |
3185
|
4763 bool save_as_floats, bool write_header_info) |
863
|
4764 { |
3185
|
4765 if (write_header_info) |
|
4766 write_header (os, fmt); |
863
|
4767 |
1755
|
4768 if (argv_idx == argc) |
863
|
4769 { |
|
4770 save_vars (os, "*", save_builtins, fmt, save_as_floats); |
|
4771 } |
|
4772 else |
|
4773 { |
1755
|
4774 for (int i = argv_idx; i < argc; i++) |
863
|
4775 { |
1755
|
4776 if (! save_vars (os, argv[i], save_builtins, fmt, save_as_floats)) |
863
|
4777 { |
1755
|
4778 warning ("save: no such variable `%s'", argv[i].c_str ()); |
863
|
4779 } |
|
4780 } |
|
4781 } |
|
4782 } |
|
4783 |
1380
|
4784 void |
|
4785 save_user_variables (void) |
|
4786 { |
3189
|
4787 if (Vcrash_dumps_octave_core) |
1380
|
4788 { |
3189
|
4789 // XXX FIXME XXX -- should choose better file name? |
|
4790 |
|
4791 const char *fname = "octave-core"; |
|
4792 |
|
4793 message (0, "attempting to save variables to `%s'...", fname); |
|
4794 |
|
4795 load_save_format format = get_default_save_format (); |
|
4796 |
3538
|
4797 unsigned mode = std::ios::out|std::ios::trunc; |
3688
|
4798 if (format == LS_BINARY || |
|
4799 format == LS_MAT_BINARY || |
|
4800 format == LS_MAT5_BINARY) |
3552
|
4801 mode |= std::ios::binary; |
3189
|
4802 |
3687
|
4803 #ifdef HAVE_HDF5 |
|
4804 if (format == LS_HDF5) |
3189
|
4805 { |
3687
|
4806 hdf5_ofstream file (fname); |
|
4807 |
|
4808 if (file.file_id >= 0) |
|
4809 { |
|
4810 save_vars (string_vector (), 0, 0, file, |
|
4811 false, format, false, true); |
|
4812 |
|
4813 message (0, "save to `%s' complete", fname); |
|
4814 |
|
4815 file.close (); |
|
4816 } |
|
4817 else |
|
4818 warning ("unable to open `%s' for writing...", fname); |
3189
|
4819 } |
|
4820 else |
3687
|
4821 #endif /* HAVE_HDF5 */ |
|
4822 // don't insert any commands here! The open brace below must |
|
4823 // go with the else above! |
|
4824 { |
|
4825 std::ofstream file (fname, mode); |
|
4826 |
|
4827 if (file) |
|
4828 { |
|
4829 save_vars (string_vector (), 0, 0, file, |
|
4830 false, format, false, true); |
|
4831 message (0, "save to `%s' complete", fname); |
|
4832 file.close (); |
|
4833 } |
|
4834 else |
|
4835 warning ("unable to open `%s' for writing...", fname); |
|
4836 } |
1380
|
4837 } |
|
4838 } |
|
4839 |
1957
|
4840 DEFUN_TEXT (save, args, , |
3372
|
4841 "-*- texinfo -*-\n\ |
|
4842 @deffn {Command} save options file v1 v2 @dots{}\n\ |
|
4843 Save the named variables @var{v1}, @var{v2}, @dots{} in the file\n\ |
|
4844 @var{file}. The special filename @samp{-} can be used to write the\n\ |
|
4845 output to your terminal. If no variable names are listed, Octave saves\n\ |
|
4846 all the variables in the current scope. Valid options for the\n\ |
|
4847 @code{save} command are listed in the following table. Options that\n\ |
|
4848 modify the output format override the format specified by the built-in\n\ |
|
4849 variable @code{default_save_format}.\n\ |
|
4850 \n\ |
|
4851 @table @code\n\ |
|
4852 @item -ascii\n\ |
|
4853 Save the data in Octave's text data format.\n\ |
|
4854 \n\ |
|
4855 @item -binary\n\ |
|
4856 Save the data in Octave's binary data format.\n\ |
|
4857 \n\ |
|
4858 @item -float-binary\n\ |
|
4859 Save the data in Octave's binary data format but only using single\n\ |
|
4860 precision. You should use this format only if you know that all the\n\ |
|
4861 values to be saved can be represented in single precision.\n\ |
|
4862 \n\ |
|
4863 @item -mat-binary\n\ |
|
4864 Save the data in @sc{Matlab}'s binary data format.\n\ |
|
4865 \n\ |
3688
|
4866 @item -mat4-binary\n\ |
|
4867 Save the data in the binary format written by @sc{Matlab} version 4.\n\ |
|
4868 \n\ |
3687
|
4869 @item -hdf5\n\ |
|
4870 Save the data in HDF5 format.\n\ |
|
4871 (HDF5 is a free, portable binary format developed by the National\n\ |
|
4872 Center for Supercomputing Applications at the University of Illinois.)\n" |
|
4873 |
|
4874 HAVE_HDF5_HELP_STRING |
|
4875 |
|
4876 "\n\ |
|
4877 @item -float-hdf5\n\ |
|
4878 Save the data in HDF5 format but only using single precision.\n\ |
|
4879 You should use this format only if you know that all the\n\ |
|
4880 values to be saved can be represented in single precision.\n\ |
|
4881 \n\ |
3372
|
4882 @item -save-builtins\n\ |
|
4883 Force Octave to save the values of built-in variables too. By default,\n\ |
|
4884 Octave does not save built-in variables.\n\ |
|
4885 @end table\n\ |
604
|
4886 \n\ |
3372
|
4887 The list of variables to save may include wildcard patterns containing\n\ |
|
4888 the following special characters:\n\ |
|
4889 @table @code\n\ |
|
4890 @item ?\n\ |
|
4891 Match any single character.\n\ |
|
4892 \n\ |
|
4893 @item *\n\ |
|
4894 Match zero or more characters.\n\ |
|
4895 \n\ |
|
4896 @item [ @var{list} ]\n\ |
|
4897 Match the list of characters specified by @var{list}. If the first\n\ |
|
4898 character is @code{!} or @code{^}, match all characters except those\n\ |
|
4899 specified by @var{list}. For example, the pattern @samp{[a-zA-Z]} will\n\ |
|
4900 match all lower and upper case alphabetic characters. \n\ |
|
4901 @end table\n\ |
|
4902 \n\ |
|
4903 Except when using the @sc{Matlab} binary data file format, saving global\n\ |
|
4904 variables also saves the global status of the variable, so that if it is\n\ |
|
4905 restored at a later time using @samp{load}, it will be restored as a\n\ |
|
4906 global variable.\n\ |
|
4907 \n\ |
|
4908 The command\n\ |
|
4909 \n\ |
|
4910 @example\n\ |
|
4911 save -binary data a b*\n\ |
|
4912 @end example\n\ |
|
4913 \n\ |
|
4914 @noindent\n\ |
|
4915 saves the variable @samp{a} and all variables beginning with @samp{b} to\n\ |
|
4916 the file @file{data} in Octave's binary format.\n\ |
|
4917 @end deffn") |
604
|
4918 { |
2086
|
4919 octave_value_list retval; |
604
|
4920 |
1755
|
4921 int argc = args.length () + 1; |
|
4922 |
1968
|
4923 string_vector argv = args.make_argv ("save"); |
1755
|
4924 |
|
4925 if (error_state) |
|
4926 return retval; |
604
|
4927 |
1358
|
4928 // Here is where we would get the default save format if it were |
|
4929 // stored in a user preference variable. |
604
|
4930 |
3019
|
4931 bool save_builtins = false; |
|
4932 |
|
4933 bool save_as_floats = false; |
630
|
4934 |
604
|
4935 load_save_format format = get_default_save_format (); |
|
4936 |
3185
|
4937 bool append = false; |
|
4938 |
1755
|
4939 int i; |
|
4940 for (i = 1; i < argc; i++) |
604
|
4941 { |
3185
|
4942 if (argv[i] == "-append") |
|
4943 { |
|
4944 append = true; |
|
4945 } |
3465
|
4946 else if (argv[i] == "-ascii" || argv[i] == "-a") |
604
|
4947 { |
|
4948 format = LS_ASCII; |
|
4949 } |
1755
|
4950 else if (argv[i] == "-binary" || argv[i] == "-b") |
604
|
4951 { |
|
4952 format = LS_BINARY; |
|
4953 } |
3687
|
4954 else if (argv[i] == "-hdf5" || argv[i] == "-h") |
|
4955 { |
|
4956 #ifdef HAVE_HDF5 |
|
4957 format = LS_HDF5; |
|
4958 #else /* ! HAVE_HDF5 */ |
|
4959 error ("save: octave executable was not linked with HDF5 library"); |
|
4960 return retval; |
|
4961 #endif /* ! HAVE_HDF5 */ |
|
4962 } |
1755
|
4963 else if (argv[i] == "-mat-binary" || argv[i] == "-m") |
667
|
4964 { |
3688
|
4965 format = LS_MAT5_BINARY; |
|
4966 } |
|
4967 else if (argv[i] == "-mat4-binary" || argv[i] == "-4") |
|
4968 { |
667
|
4969 format = LS_MAT_BINARY; |
|
4970 } |
1755
|
4971 else if (argv[i] == "-float-binary" || argv[i] == "-f") |
630
|
4972 { |
|
4973 format = LS_BINARY; |
3019
|
4974 save_as_floats = true; |
630
|
4975 } |
3687
|
4976 else if (argv[i] == "-float-hdf5") |
|
4977 { |
|
4978 #ifdef HAVE_HDF5 |
|
4979 format = LS_HDF5; |
|
4980 save_as_floats = true; |
|
4981 #else /* ! HAVE_HDF5 */ |
|
4982 error ("save: octave executable was not linked with HDF5 library"); |
|
4983 return retval; |
|
4984 #endif /* ! HAVE_HDF5 */ |
|
4985 } |
1755
|
4986 else if (argv[i] == "-save-builtins") |
604
|
4987 { |
3019
|
4988 save_builtins = true; |
604
|
4989 } |
|
4990 else |
|
4991 break; |
|
4992 } |
|
4993 |
2057
|
4994 if (i == argc) |
604
|
4995 { |
|
4996 print_usage ("save"); |
|
4997 return retval; |
|
4998 } |
|
4999 |
630
|
5000 if (save_as_floats && format == LS_ASCII) |
|
5001 { |
|
5002 error ("save: cannot specify both -ascii and -float-binary"); |
|
5003 return retval; |
|
5004 } |
|
5005 |
1755
|
5006 if (argv[i] == "-") |
604
|
5007 { |
1755
|
5008 i++; |
863
|
5009 |
3687
|
5010 #ifdef HAVE_HDF5 |
|
5011 if (format == LS_HDF5) |
|
5012 error ("load: cannot write HDF5 format to stdout"); |
|
5013 else |
|
5014 #endif /* HAVE_HDF5 */ |
|
5015 // don't insert any commands here! the brace below must go |
|
5016 // with the "else" above! |
|
5017 { |
|
5018 // XXX FIXME XXX -- should things intended for the screen end up |
|
5019 // in a octave_value (string)? |
|
5020 |
|
5021 save_vars (argv, i, argc, octave_stdout, save_builtins, format, |
|
5022 save_as_floats, true); |
|
5023 } |
604
|
5024 } |
1755
|
5025 |
|
5026 // Guard against things like `save a*', which are probably mistakes... |
|
5027 |
|
5028 else if (i == argc - 1 && glob_pattern_p (argv[i])) |
|
5029 { |
|
5030 print_usage ("save"); |
604
|
5031 return retval; |
|
5032 } |
|
5033 else |
|
5034 { |
3523
|
5035 std::string fname = file_ops::tilde_expand (argv[i]); |
1755
|
5036 |
|
5037 i++; |
604
|
5038 |
3538
|
5039 unsigned mode = std::ios::out; |
3688
|
5040 if (format == LS_BINARY || |
|
5041 format == LS_MAT_BINARY || |
|
5042 format == LS_MAT5_BINARY) |
3552
|
5043 mode |= std::ios::binary; |
3538
|
5044 |
|
5045 mode |= append ? std::ios::ate : std::ios::trunc; |
3185
|
5046 |
3687
|
5047 #ifdef HAVE_HDF5 |
|
5048 if (format == LS_HDF5) |
863
|
5049 { |
3687
|
5050 hdf5_ofstream hdf5_file (fname.c_str ()); |
|
5051 |
|
5052 if (hdf5_file.file_id >= 0) { |
|
5053 save_vars (argv, i, argc, hdf5_file, save_builtins, format, |
|
5054 save_as_floats, true); |
|
5055 |
|
5056 hdf5_file.close (); |
|
5057 } |
|
5058 else |
|
5059 { |
|
5060 error ("save: couldn't open output file `%s'", fname.c_str ()); |
|
5061 return retval; |
|
5062 } |
863
|
5063 } |
|
5064 else |
3687
|
5065 #endif /* HAVE_HDF5 */ |
|
5066 // don't insert any statements here! The brace below must go |
|
5067 // with the "else" above! |
604
|
5068 { |
3687
|
5069 std::ofstream file (fname.c_str (), mode); |
|
5070 |
|
5071 if (file) |
|
5072 { |
|
5073 bool write_header_info |
|
5074 = ( (file.rdbuf ())->seekoff (0, std::ios::cur) == 0); |
|
5075 |
|
5076 save_vars (argv, i, argc, file, save_builtins, format, |
|
5077 save_as_floats, write_header_info); |
|
5078 } |
|
5079 else |
|
5080 { |
|
5081 error ("save: couldn't open output file `%s'", fname.c_str ()); |
|
5082 return retval; |
|
5083 } |
604
|
5084 } |
|
5085 } |
|
5086 |
|
5087 return retval; |
|
5088 } |
|
5089 |
|
5090 // Maybe this should be a static function in tree-plot.cc? |
|
5091 |
620
|
5092 // If TC is matrix, save it on stream OS in a format useful for |
604
|
5093 // making a 3-dimensional plot with gnuplot. If PARAMETRIC is |
3019
|
5094 // TRUE, assume a parametric 3-dimensional plot will be generated. |
604
|
5095 |
2799
|
5096 bool |
3523
|
5097 save_three_d (std::ostream& os, const octave_value& tc, bool parametric) |
604
|
5098 { |
3019
|
5099 bool fail = false; |
604
|
5100 |
620
|
5101 int nr = tc.rows (); |
|
5102 int nc = tc.columns (); |
|
5103 |
|
5104 if (tc.is_real_matrix ()) |
604
|
5105 { |
|
5106 os << "# 3D data...\n" |
|
5107 << "# type: matrix\n" |
|
5108 << "# total rows: " << nr << "\n" |
|
5109 << "# total columns: " << nc << "\n"; |
|
5110 |
|
5111 if (parametric) |
|
5112 { |
|
5113 int extras = nc % 3; |
|
5114 if (extras) |
|
5115 warning ("ignoring last %d columns", extras); |
|
5116 |
620
|
5117 Matrix tmp = tc.matrix_value (); |
872
|
5118 tmp = strip_infnan (tmp); |
|
5119 nr = tmp.rows (); |
|
5120 |
604
|
5121 for (int i = 0; i < nc-extras; i += 3) |
|
5122 { |
|
5123 os << tmp.extract (0, i, nr-1, i+2); |
|
5124 if (i+3 < nc-extras) |
|
5125 os << "\n"; |
|
5126 } |
|
5127 } |
|
5128 else |
|
5129 { |
620
|
5130 Matrix tmp = tc.matrix_value (); |
872
|
5131 tmp = strip_infnan (tmp); |
|
5132 nr = tmp.rows (); |
|
5133 |
604
|
5134 for (int i = 0; i < nc; i++) |
|
5135 { |
|
5136 os << tmp.extract (0, i, nr-1, i); |
|
5137 if (i+1 < nc) |
|
5138 os << "\n"; |
|
5139 } |
|
5140 } |
620
|
5141 } |
|
5142 else |
|
5143 { |
604
|
5144 ::error ("for now, I can only save real matrices in 3D format"); |
3019
|
5145 fail = true; |
604
|
5146 } |
620
|
5147 |
|
5148 return (os && ! fail); |
604
|
5149 } |
|
5150 |
2194
|
5151 static int |
3189
|
5152 crash_dumps_octave_core (void) |
|
5153 { |
|
5154 Vcrash_dumps_octave_core = check_preference ("crash_dumps_octave_core"); |
|
5155 return 0; |
|
5156 } |
|
5157 |
|
5158 |
|
5159 static int |
2194
|
5160 default_save_format (void) |
|
5161 { |
|
5162 int status = 0; |
|
5163 |
3523
|
5164 std::string s = builtin_string_variable ("default_save_format"); |
2194
|
5165 |
|
5166 if (s.empty ()) |
|
5167 { |
|
5168 gripe_invalid_value_specified ("default_save_format"); |
|
5169 status = -1; |
|
5170 } |
|
5171 else |
|
5172 Vdefault_save_format = s; |
|
5173 |
|
5174 return status; |
|
5175 } |
|
5176 |
|
5177 static int |
|
5178 save_precision (void) |
|
5179 { |
|
5180 double val; |
|
5181 if (builtin_real_scalar_variable ("save_precision", val) |
|
5182 && ! xisnan (val)) |
|
5183 { |
|
5184 int ival = NINT (val); |
2800
|
5185 if (ival >= 0 && ival == val) |
2194
|
5186 { |
|
5187 Vsave_precision = ival; |
|
5188 return 0; |
|
5189 } |
|
5190 } |
|
5191 gripe_invalid_value_specified ("save_precision"); |
|
5192 return -1; |
|
5193 } |
|
5194 |
|
5195 void |
|
5196 symbols_of_load_save (void) |
|
5197 { |
3258
|
5198 DEFVAR (crash_dumps_octave_core, 1.0, crash_dumps_octave_core, |
3372
|
5199 "-*- texinfo -*-\n\ |
|
5200 @defvr {Built-in Variable} crash_dumps_octave_core\n\ |
|
5201 If this variable is set to a nonzero value, Octave tries to save all\n\ |
|
5202 current variables the the file \"octave-core\" if it crashes or receives a\n\ |
|
5203 hangup, terminate or similar signal. The default value is 1.\n\ |
|
5204 @end defvr"); |
3189
|
5205 |
3258
|
5206 DEFVAR (default_save_format, "ascii", default_save_format, |
3372
|
5207 "-*- texinfo -*-\n\ |
|
5208 @defvr {Built-in Variable} default_save_format\n\ |
|
5209 This variable specifies the default format for the @code{save} command.\n\ |
|
5210 It should have one of the following values: @code{\"ascii\"},\n\ |
|
5211 @code{\"binary\"}, @code{float-binary}, or @code{\"mat-binary\"}. The\n\ |
|
5212 initial default save format is Octave's text format.\n\ |
|
5213 @end defvr"); |
2194
|
5214 |
3258
|
5215 DEFVAR (save_precision, 15.0, save_precision, |
3372
|
5216 "-*- texinfo -*-\n\ |
|
5217 @defvr {Built-in Variable} save_precision\n\ |
|
5218 This variable specifies the number of digits to keep when saving data in\n\ |
|
5219 text format. The default value is 17.\n\ |
|
5220 @end defvr"); |
2194
|
5221 } |
|
5222 |
604
|
5223 /* |
|
5224 ;;; Local Variables: *** |
|
5225 ;;; mode: C++ *** |
|
5226 ;;; End: *** |
|
5227 */ |