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