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