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