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