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