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