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