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 |
|
1744 m [dsub.name] = dsub.tc; |
|
1745 |
3695
|
1746 if (have_h5giterate_bug) |
|
1747 current_item++; // H5Giterate returned the last index processed |
3687
|
1748 } |
|
1749 |
|
1750 if (retval2 < 0) |
|
1751 retval = retval2; |
|
1752 else |
|
1753 { |
3688
|
1754 d->global = hdf5_check_attr (group_id, "OCTAVE_GLOBAL"); |
3687
|
1755 |
|
1756 if (is_list) |
4233
|
1757 d->tc = octave_value (lst); |
3687
|
1758 else |
|
1759 d->tc = m; |
|
1760 } |
|
1761 |
|
1762 H5Gclose (subgroup_id); |
|
1763 } |
|
1764 else if (! ident_valid) |
|
1765 { |
|
1766 // should we attempt to handle invalid identifiers by converting |
|
1767 // bad characters to '_', say? |
|
1768 warning ("load: skipping invalid identifier `%s' in hdf5 file", |
|
1769 name); |
|
1770 } |
|
1771 |
|
1772 done: |
|
1773 |
|
1774 if (retval < 0) |
|
1775 error ("load: error while reading hdf5 item %s", name); |
|
1776 |
|
1777 if (retval > 0) |
|
1778 { |
|
1779 // get documentation string, if any: |
3956
|
1780 int comment_length = H5Gget_comment (group_id, name, 0, 0); |
3687
|
1781 |
|
1782 if (comment_length > 1) |
|
1783 { |
4335
|
1784 OCTAVE_LOCAL_BUFFER (char, tdoc, comment_length); |
|
1785 H5Gget_comment (group_id, name, comment_length, tdoc); |
|
1786 d->doc = tdoc; |
3687
|
1787 } |
|
1788 else if (strcmp (name, vname) != 0) |
|
1789 { |
|
1790 // the name was changed by import; store the original name |
|
1791 // as the documentation string: |
4335
|
1792 d->doc = name; |
3687
|
1793 } |
|
1794 |
|
1795 // copy name (actually, vname): |
4335
|
1796 d->name = vname; |
3687
|
1797 } |
|
1798 |
|
1799 return retval; |
|
1800 } |
|
1801 |
|
1802 // Read the next Octave variable from the stream IS, which must really be |
|
1803 // an hdf5_ifstream. Return the variable value in tc, its doc string |
|
1804 // in doc, and whether it is global in global. The return value is |
|
1805 // the name of the variable, or NULL if none were found or there was |
|
1806 // and error. If import is true, we try extra hard to import "foreign" |
|
1807 // datasets (not created by Octave), although we usually do a reasonable |
|
1808 // job anyway. (c.f. load -import documentation.) |
4171
|
1809 static std::string |
3687
|
1810 read_hdf5_data (std::istream& is, |
|
1811 const std::string& filename, bool& global, |
4171
|
1812 octave_value& tc, std::string& doc, bool import) |
3687
|
1813 { |
4171
|
1814 std::string retval; |
|
1815 |
|
1816 doc.resize (0); |
|
1817 |
3687
|
1818 hdf5_ifstream& hs = (hdf5_ifstream&) is; |
|
1819 hdf5_callback_data d; |
|
1820 |
|
1821 d.import = import; |
|
1822 |
3695
|
1823 // Versions of HDF5 prior to 1.2.2 had a bug in H5Giterate where it |
|
1824 // would return the index of the last item processed instead of the |
|
1825 // next item to be processed, forcing us to increment the index manually. |
|
1826 |
|
1827 unsigned int vers_major, vers_minor, vers_release; |
|
1828 |
|
1829 H5get_libversion (&vers_major, &vers_minor, &vers_release); |
|
1830 |
|
1831 // XXX FIXME XXX -- this test looks wrong. |
|
1832 have_h5giterate_bug |
|
1833 = (vers_major < 1 |
|
1834 || (vers_major == 1 && (vers_minor < 2 |
|
1835 || (vers_minor == 2 && vers_release < 2)))); |
|
1836 |
4171
|
1837 herr_t H5Giterate_retval = H5Giterate (hs.file_id, "/", &hs.current_item, |
|
1838 hdf5_read_next_data, &d); |
3687
|
1839 |
3695
|
1840 if (have_h5giterate_bug) |
|
1841 { |
|
1842 // H5Giterate sets current_item to the last item processed; we want |
|
1843 // the index of the next item (for the next call to read_hdf5_data) |
|
1844 |
|
1845 hs.current_item++; |
|
1846 } |
3687
|
1847 |
4171
|
1848 if (H5Giterate_retval > 0) |
3687
|
1849 { |
|
1850 global = d.global; |
|
1851 tc = d.tc; |
4335
|
1852 doc = d.doc; |
3687
|
1853 } |
|
1854 else |
|
1855 { |
4171
|
1856 // an error occurred (H5Giterate_retval < 0) or there are no |
|
1857 // more datasets print an error message if retval < 0? |
3687
|
1858 // hdf5_read_next_data already printed one, probably. |
|
1859 } |
|
1860 |
|
1861 H5Tclose (d.complex_type); |
|
1862 H5Tclose (d.range_type); |
|
1863 |
4335
|
1864 if (! d.name.empty ()) |
4171
|
1865 retval = d.name; |
|
1866 |
|
1867 return retval; |
3687
|
1868 } |
|
1869 |
|
1870 #endif /* HAVE_HDF5 */ |
|
1871 |
3536
|
1872 static std::string |
3523
|
1873 get_mat_data_input_line (std::istream& is) |
3216
|
1874 { |
3523
|
1875 std::string retval; |
3216
|
1876 |
|
1877 bool have_data = false; |
|
1878 |
|
1879 do |
|
1880 { |
|
1881 retval = ""; |
|
1882 |
|
1883 char c; |
|
1884 while (is.get (c)) |
|
1885 { |
4304
|
1886 if (c == '\n' || c == '\r') |
3216
|
1887 break; |
|
1888 |
|
1889 if (c == '%' || c == '#') |
|
1890 { |
|
1891 // skip to end of line |
4304
|
1892 while (is.get (c)) |
|
1893 if (c == '\n' || c == '\r') |
|
1894 break; |
3216
|
1895 |
|
1896 break; |
|
1897 } |
|
1898 |
|
1899 if (! is.eof ()) |
|
1900 { |
|
1901 if (! have_data && c != ' ' && c != '\t') |
|
1902 have_data = true; |
|
1903 |
|
1904 retval += c; |
|
1905 } |
|
1906 } |
|
1907 } |
|
1908 while (! (have_data || is.eof ())); |
|
1909 |
|
1910 return retval; |
|
1911 } |
|
1912 |
2511
|
1913 static void |
3523
|
1914 get_lines_and_columns (std::istream& is, const std::string& filename, int& nr, int& nc) |
2511
|
1915 { |
3552
|
1916 std::streampos pos = is.tellg (); |
2511
|
1917 |
|
1918 int file_line_number = 0; |
|
1919 |
|
1920 nr = 0; |
|
1921 nc = 0; |
|
1922 |
2795
|
1923 while (is && ! error_state) |
2511
|
1924 { |
4171
|
1925 OCTAVE_QUIT; |
|
1926 |
3523
|
1927 std::string buf = get_mat_data_input_line (is); |
2511
|
1928 |
|
1929 file_line_number++; |
|
1930 |
4231
|
1931 size_t beg = buf.find_first_not_of (", \t"); |
2795
|
1932 |
3901
|
1933 // If we see a CR as the last character in the buffer, we had a |
|
1934 // CRLF pair as the line separator. Any other CR in the text |
|
1935 // will not be considered as whitespace. |
|
1936 |
|
1937 if (beg != NPOS && buf[beg] == '\r' && beg == buf.length () - 1) |
|
1938 { |
|
1939 // We had a blank line ending with a CRLF. Handle it the |
|
1940 // same as an empty line. |
|
1941 beg = NPOS; |
|
1942 } |
|
1943 |
2795
|
1944 int tmp_nc = 0; |
|
1945 |
|
1946 while (beg != NPOS) |
2511
|
1947 { |
2795
|
1948 tmp_nc++; |
|
1949 |
4231
|
1950 size_t end = buf.find_first_of (", \t", beg); |
2795
|
1951 |
|
1952 if (end != NPOS) |
3901
|
1953 { |
4231
|
1954 beg = buf.find_first_not_of (", \t", end); |
3901
|
1955 |
|
1956 if (buf[beg] == '\r' && beg == buf.length () - 1) |
|
1957 { |
|
1958 // We had a line with trailing spaces and |
|
1959 // ending with a CRLF, so this should look like EOL, |
|
1960 // not a new colum. |
|
1961 break; |
|
1962 } |
|
1963 } |
2795
|
1964 else |
|
1965 break; |
|
1966 } |
|
1967 |
|
1968 if (tmp_nc > 0) |
|
1969 { |
2511
|
1970 if (nc == 0) |
|
1971 { |
|
1972 nc = tmp_nc; |
|
1973 nr++; |
|
1974 } |
|
1975 else if (nc == tmp_nc) |
|
1976 nr++; |
|
1977 else |
|
1978 error ("load: %s: inconsistent number of columns near line %d", |
|
1979 filename.c_str (), file_line_number); |
|
1980 } |
|
1981 } |
|
1982 |
|
1983 if (nr == 0 || nc == 0) |
|
1984 error ("load: file `%s' seems to be empty!", filename.c_str ()); |
|
1985 |
|
1986 is.clear (); |
3538
|
1987 is.seekg (pos, std::ios::beg); |
2511
|
1988 } |
|
1989 |
|
1990 // Extract a matrix from a file of numbers only. |
|
1991 // |
|
1992 // Comments are not allowed. The file should only have numeric values. |
|
1993 // |
|
1994 // Reads the file twice. Once to find the number of rows and columns, |
|
1995 // and once to extract the matrix. |
|
1996 // |
|
1997 // FILENAME is used for error messages. |
|
1998 // |
|
1999 // This format provides no way to tag the data as global. |
|
2000 |
4171
|
2001 static std::string |
3523
|
2002 read_mat_ascii_data (std::istream& is, const std::string& filename, |
2511
|
2003 octave_value& tc) |
|
2004 { |
4171
|
2005 std::string retval; |
2511
|
2006 |
3523
|
2007 std::string varname; |
2511
|
2008 |
3202
|
2009 size_t pos = filename.rfind ('/'); |
2511
|
2010 |
|
2011 if (pos != NPOS) |
3202
|
2012 varname = filename.substr (pos+1); |
2511
|
2013 else |
|
2014 varname = filename; |
|
2015 |
3202
|
2016 pos = varname.find ('.'); |
|
2017 |
|
2018 if (pos != NPOS) |
|
2019 varname = varname.substr (0, pos); |
|
2020 |
4232
|
2021 size_t len = varname.length (); |
|
2022 for (size_t i = 0; i < len; i++) |
|
2023 { |
|
2024 char c = varname[i]; |
4247
|
2025 if (! (isalnum (c) || c == '_')) |
4232
|
2026 varname[i] = '_'; |
|
2027 } |
|
2028 |
4247
|
2029 if (! isalpha (varname[0])) |
4232
|
2030 varname.insert (0, "X"); |
|
2031 |
3263
|
2032 if (valid_identifier (varname)) |
2511
|
2033 { |
|
2034 int nr = 0; |
|
2035 int nc = 0; |
|
2036 |
4231
|
2037 int total_count = 0; |
|
2038 |
2511
|
2039 get_lines_and_columns (is, filename, nr, nc); |
|
2040 |
4171
|
2041 OCTAVE_QUIT; |
|
2042 |
2795
|
2043 if (! error_state && nr > 0 && nc > 0) |
2511
|
2044 { |
|
2045 Matrix tmp (nr, nc); |
|
2046 |
3216
|
2047 if (nr < 1 || nc < 1) |
3538
|
2048 is.clear (std::ios::badbit); |
3216
|
2049 else |
|
2050 { |
|
2051 double d; |
|
2052 for (int i = 0; i < nr; i++) |
|
2053 { |
3523
|
2054 std::string buf = get_mat_data_input_line (is); |
|
2055 |
4051
|
2056 #ifdef HAVE_SSTREAM |
|
2057 std::istringstream tmp_stream (buf); |
|
2058 #else |
3523
|
2059 std::istrstream tmp_stream (buf.c_str ()); |
4051
|
2060 #endif |
3216
|
2061 |
|
2062 for (int j = 0; j < nc; j++) |
|
2063 { |
4171
|
2064 OCTAVE_QUIT; |
|
2065 |
4130
|
2066 d = octave_read_double (tmp_stream); |
3323
|
2067 |
4231
|
2068 if (tmp_stream || tmp_stream.eof ()) |
|
2069 { |
|
2070 tmp.elem (i, j) = d; |
|
2071 total_count++; |
|
2072 |
|
2073 // Skip whitespace and commas. |
|
2074 char c; |
|
2075 while (1) |
|
2076 { |
|
2077 tmp_stream >> c; |
|
2078 |
|
2079 if (! tmp_stream) |
|
2080 break; |
|
2081 |
|
2082 if (! (c == ' ' || c == '\t' || c == ',')) |
|
2083 { |
|
2084 tmp_stream.putback (c); |
|
2085 break; |
|
2086 } |
|
2087 } |
|
2088 |
|
2089 if (tmp_stream.eof ()) |
|
2090 break; |
|
2091 } |
3216
|
2092 else |
3323
|
2093 { |
|
2094 error ("load: failed to read matrix from file `%s'", |
|
2095 filename.c_str ()); |
|
2096 |
4171
|
2097 return retval; |
3323
|
2098 } |
|
2099 |
3216
|
2100 } |
|
2101 } |
|
2102 } |
|
2103 |
4231
|
2104 if (is || is.eof ()) |
2511
|
2105 { |
4231
|
2106 // XXX FIXME XXX -- not sure this is best, but it works. |
|
2107 |
|
2108 if (is.eof ()) |
|
2109 is.clear (); |
|
2110 |
|
2111 int expected = nr * nc; |
|
2112 |
|
2113 if (expected == total_count) |
|
2114 { |
|
2115 tc = tmp; |
|
2116 retval = varname; |
|
2117 } |
|
2118 else |
|
2119 error ("load: expected %d elements, found %d", |
|
2120 expected, total_count); |
2511
|
2121 } |
|
2122 else |
|
2123 error ("load: failed to read matrix from file `%s'", |
|
2124 filename.c_str ()); |
|
2125 } |
|
2126 else |
|
2127 error ("load: unable to extract matrix size from file `%s'", |
|
2128 filename.c_str ()); |
|
2129 } |
|
2130 else |
|
2131 error ("load: unable to convert filename `%s' to valid identifier", |
|
2132 filename.c_str ()); |
|
2133 |
4171
|
2134 return retval; |
2511
|
2135 } |
|
2136 |
604
|
2137 // Read LEN elements of data from IS in the format specified by |
3019
|
2138 // PRECISION, placing the result in DATA. If SWAP is TRUE, swap |
604
|
2139 // the bytes of each element before copying to DATA. FLT_FMT |
|
2140 // specifies the format of the data if we are reading floating point |
|
2141 // numbers. |
|
2142 |
|
2143 static void |
3523
|
2144 read_mat_binary_data (std::istream& is, double *data, int precision, |
3019
|
2145 int len, bool swap, |
2318
|
2146 oct_mach_info::float_format flt_fmt) |
604
|
2147 { |
|
2148 switch (precision) |
|
2149 { |
|
2150 case 0: |
630
|
2151 read_doubles (is, data, LS_DOUBLE, len, swap, flt_fmt); |
604
|
2152 break; |
|
2153 |
|
2154 case 1: |
630
|
2155 read_doubles (is, data, LS_FLOAT, len, swap, flt_fmt); |
604
|
2156 break; |
|
2157 |
|
2158 case 2: |
|
2159 read_doubles (is, data, LS_INT, len, swap, flt_fmt); |
|
2160 break; |
|
2161 |
|
2162 case 3: |
|
2163 read_doubles (is, data, LS_SHORT, len, swap, flt_fmt); |
|
2164 break; |
|
2165 |
|
2166 case 4: |
|
2167 read_doubles (is, data, LS_U_SHORT, len, swap, flt_fmt); |
|
2168 break; |
|
2169 |
|
2170 case 5: |
|
2171 read_doubles (is, data, LS_U_CHAR, len, swap, flt_fmt); |
|
2172 break; |
|
2173 |
|
2174 default: |
|
2175 break; |
|
2176 } |
|
2177 } |
|
2178 |
|
2179 static int |
3523
|
2180 read_mat_file_header (std::istream& is, bool& swap, FOUR_BYTE_INT& mopt, |
604
|
2181 FOUR_BYTE_INT& nr, FOUR_BYTE_INT& nc, |
|
2182 FOUR_BYTE_INT& imag, FOUR_BYTE_INT& len, |
|
2183 int quiet = 0) |
|
2184 { |
3019
|
2185 swap = false; |
671
|
2186 |
1358
|
2187 // We expect to fail here, at the beginning of a record, so not |
|
2188 // being able to read another mopt value should not result in an |
|
2189 // error. |
911
|
2190 |
3557
|
2191 is.read (X_CAST (char *, &mopt), 4); |
604
|
2192 if (! is) |
911
|
2193 return 1; |
604
|
2194 |
3557
|
2195 if (! is.read (X_CAST (char *, &nr), 4)) |
604
|
2196 goto data_read_error; |
|
2197 |
3557
|
2198 if (! is.read (X_CAST (char *, &nc), 4)) |
604
|
2199 goto data_read_error; |
|
2200 |
3557
|
2201 if (! is.read (X_CAST (char *, &imag), 4)) |
604
|
2202 goto data_read_error; |
|
2203 |
3557
|
2204 if (! is.read (X_CAST (char *, &len), 4)) |
604
|
2205 goto data_read_error; |
|
2206 |
|
2207 // If mopt is nonzero and the byte order is swapped, mopt will be |
|
2208 // bigger than we expect, so we swap bytes. |
|
2209 // |
|
2210 // If mopt is zero, it means the file was written on a little endian |
|
2211 // machine, and we only need to swap if we are running on a big endian |
|
2212 // machine. |
|
2213 // |
|
2214 // Gag me. |
|
2215 |
2318
|
2216 if (oct_mach_info::words_big_endian () && mopt == 0) |
3019
|
2217 swap = true; |
604
|
2218 |
1358
|
2219 // mopt is signed, therefore byte swap may result in negative value. |
911
|
2220 |
|
2221 if (mopt > 9999 || mopt < 0) |
3019
|
2222 swap = true; |
604
|
2223 |
|
2224 if (swap) |
|
2225 { |
3145
|
2226 swap_4_bytes (X_CAST (char *, &mopt)); |
|
2227 swap_4_bytes (X_CAST (char *, &nr)); |
|
2228 swap_4_bytes (X_CAST (char *, &nc)); |
|
2229 swap_4_bytes (X_CAST (char *, &imag)); |
|
2230 swap_4_bytes (X_CAST (char *, &len)); |
604
|
2231 } |
|
2232 |
911
|
2233 if (mopt > 9999 || mopt < 0 || imag > 1 || imag < 0) |
604
|
2234 { |
|
2235 if (! quiet) |
|
2236 error ("load: can't read binary file"); |
|
2237 return -1; |
|
2238 } |
|
2239 |
|
2240 return 0; |
|
2241 |
|
2242 data_read_error: |
|
2243 return -1; |
|
2244 } |
|
2245 |
617
|
2246 // We don't just use a cast here, because we need to be able to detect |
|
2247 // possible errors. |
|
2248 |
2318
|
2249 static oct_mach_info::float_format |
|
2250 mopt_digit_to_float_format (int mach) |
617
|
2251 { |
2318
|
2252 oct_mach_info::float_format flt_fmt = oct_mach_info::unknown; |
619
|
2253 |
617
|
2254 switch (mach) |
|
2255 { |
|
2256 case 0: |
2318
|
2257 flt_fmt = oct_mach_info::ieee_little_endian; |
617
|
2258 break; |
|
2259 |
|
2260 case 1: |
2318
|
2261 flt_fmt = oct_mach_info::ieee_big_endian; |
617
|
2262 break; |
|
2263 |
|
2264 case 2: |
2318
|
2265 flt_fmt = oct_mach_info::vax_d; |
617
|
2266 break; |
|
2267 |
|
2268 case 3: |
2318
|
2269 flt_fmt = oct_mach_info::vax_g; |
617
|
2270 break; |
|
2271 |
|
2272 case 4: |
2318
|
2273 flt_fmt = oct_mach_info::cray; |
617
|
2274 break; |
|
2275 |
|
2276 default: |
2318
|
2277 flt_fmt = oct_mach_info::unknown; |
617
|
2278 break; |
|
2279 } |
619
|
2280 |
|
2281 return flt_fmt; |
617
|
2282 } |
619
|
2283 |
2318
|
2284 static int |
|
2285 float_format_to_mopt_digit (oct_mach_info::float_format flt_fmt) |
|
2286 { |
|
2287 int retval = -1; |
|
2288 |
|
2289 switch (flt_fmt) |
|
2290 { |
|
2291 case oct_mach_info::ieee_little_endian: |
|
2292 retval = 0; |
|
2293 break; |
|
2294 |
|
2295 case oct_mach_info::ieee_big_endian: |
|
2296 retval = 1; |
|
2297 break; |
|
2298 |
|
2299 case oct_mach_info::vax_d: |
|
2300 retval = 2; |
|
2301 break; |
|
2302 |
|
2303 case oct_mach_info::vax_g: |
|
2304 retval = 3; |
|
2305 break; |
|
2306 |
|
2307 case oct_mach_info::cray: |
|
2308 retval = 4; |
|
2309 break; |
|
2310 |
|
2311 default: |
|
2312 break; |
|
2313 } |
|
2314 |
|
2315 return retval; |
|
2316 } |
|
2317 |
604
|
2318 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
2319 // place it in TC, returning the name of the variable. |
|
2320 // |
3688
|
2321 // The data is expected to be in Matlab version 4 .mat format, though |
|
2322 // not all the features of that format are supported. |
604
|
2323 // |
|
2324 // FILENAME is used for error messages. |
|
2325 // |
|
2326 // This format provides no way to tag the data as global. |
|
2327 |
4171
|
2328 static std::string |
3523
|
2329 read_mat_binary_data (std::istream& is, const std::string& filename, |
2086
|
2330 octave_value& tc) |
604
|
2331 { |
4171
|
2332 std::string retval; |
|
2333 |
1358
|
2334 // These are initialized here instead of closer to where they are |
|
2335 // first used to avoid errors from gcc about goto crossing |
|
2336 // initialization of variable. |
604
|
2337 |
|
2338 Matrix re; |
2318
|
2339 oct_mach_info::float_format flt_fmt = oct_mach_info::unknown; |
3019
|
2340 bool swap = false; |
|
2341 int type = 0; |
|
2342 int prec = 0; |
3136
|
2343 int order = 0; |
3019
|
2344 int mach = 0; |
|
2345 int dlen = 0; |
604
|
2346 |
|
2347 FOUR_BYTE_INT mopt, nr, nc, imag, len; |
|
2348 |
|
2349 int err = read_mat_file_header (is, swap, mopt, nr, nc, imag, len); |
|
2350 if (err) |
|
2351 { |
|
2352 if (err < 0) |
|
2353 goto data_read_error; |
|
2354 else |
4171
|
2355 return retval; |
604
|
2356 } |
|
2357 |
3136
|
2358 type = mopt % 10; // Full, sparse, etc. |
|
2359 mopt /= 10; // Eliminate first digit. |
|
2360 prec = mopt % 10; // double, float, int, etc. |
|
2361 mopt /= 10; // Eliminate second digit. |
|
2362 order = mopt % 10; // Row or column major ordering. |
|
2363 mopt /= 10; // Eliminate third digit. |
|
2364 mach = mopt % 10; // IEEE, VAX, etc. |
604
|
2365 |
2318
|
2366 flt_fmt = mopt_digit_to_float_format (mach); |
|
2367 |
|
2368 if (flt_fmt == oct_mach_info::unknown) |
604
|
2369 { |
|
2370 error ("load: unrecognized binary format!"); |
4171
|
2371 return retval; |
604
|
2372 } |
|
2373 |
|
2374 if (type != 0 && type != 1) |
|
2375 { |
|
2376 error ("load: can't read sparse matrices"); |
4171
|
2377 return retval; |
604
|
2378 } |
|
2379 |
|
2380 if (imag && type == 1) |
|
2381 { |
|
2382 error ("load: encountered complex matrix with string flag set!"); |
4171
|
2383 return retval; |
604
|
2384 } |
|
2385 |
2345
|
2386 // LEN includes the terminating character, and the file is also |
2436
|
2387 // supposed to include it, but apparently not all files do. Either |
|
2388 // way, I think this should work. |
2345
|
2389 |
4251
|
2390 { |
|
2391 OCTAVE_LOCAL_BUFFER (char, name, len+1); |
|
2392 name[len] = '\0'; |
|
2393 if (! is.read (X_CAST (char *, name), len)) |
|
2394 goto data_read_error; |
|
2395 retval = name; |
|
2396 |
|
2397 dlen = nr * nc; |
|
2398 if (dlen < 0) |
604
|
2399 goto data_read_error; |
4251
|
2400 |
|
2401 if (order) |
|
2402 { |
|
2403 int tmp = nr; |
|
2404 nr = nc; |
|
2405 nc = tmp; |
|
2406 } |
|
2407 |
|
2408 re.resize (nr, nc); |
|
2409 |
|
2410 read_mat_binary_data (is, re.fortran_vec (), prec, dlen, swap, flt_fmt); |
604
|
2411 |
|
2412 if (! is || error_state) |
|
2413 { |
4251
|
2414 error ("load: reading matrix data for `%s'", name); |
604
|
2415 goto data_read_error; |
|
2416 } |
|
2417 |
4251
|
2418 if (imag) |
|
2419 { |
|
2420 Matrix im (nr, nc); |
|
2421 |
|
2422 read_mat_binary_data (is, im.fortran_vec (), prec, dlen, swap, |
|
2423 flt_fmt); |
|
2424 |
|
2425 if (! is || error_state) |
|
2426 { |
|
2427 error ("load: reading imaginary matrix data for `%s'", name); |
|
2428 goto data_read_error; |
|
2429 } |
|
2430 |
|
2431 ComplexMatrix ctmp (nr, nc); |
|
2432 |
|
2433 for (int j = 0; j < nc; j++) |
|
2434 for (int i = 0; i < nr; i++) |
|
2435 ctmp (i, j) = Complex (re (i, j), im (i, j)); |
|
2436 |
|
2437 tc = order ? ctmp.transpose () : ctmp; |
|
2438 } |
|
2439 else |
|
2440 tc = order ? re.transpose () : re; |
|
2441 |
|
2442 if (type == 1) |
|
2443 tc = tc.convert_to_str (); |
|
2444 |
|
2445 return retval; |
604
|
2446 } |
|
2447 |
|
2448 data_read_error: |
1755
|
2449 error ("load: trouble reading binary file `%s'", filename.c_str ()); |
4171
|
2450 return retval; |
604
|
2451 } |
|
2452 |
3688
|
2453 // Read COUNT elements of data from IS in the format specified by TYPE, |
|
2454 // placing the result in DATA. If SWAP is TRUE, swap the bytes of |
|
2455 // each element before copying to DATA. FLT_FMT specifies the format |
|
2456 // of the data if we are reading floating point numbers. |
|
2457 |
|
2458 static void |
|
2459 read_mat5_binary_data (std::istream& is, double *data, |
|
2460 int count, bool swap, mat5_data_type type, |
|
2461 oct_mach_info::float_format flt_fmt) |
|
2462 { |
|
2463 |
|
2464 switch (type) |
|
2465 { |
|
2466 case miINT8: |
|
2467 read_doubles (is, data, LS_CHAR, count, swap, flt_fmt); |
|
2468 break; |
|
2469 |
|
2470 case miUINT8: |
|
2471 read_doubles (is, data, LS_U_CHAR, count, swap, flt_fmt); |
|
2472 break; |
|
2473 |
|
2474 case miINT16: |
|
2475 read_doubles (is, data, LS_SHORT, count, swap, flt_fmt); |
|
2476 break; |
|
2477 |
|
2478 case miUINT16: |
|
2479 read_doubles (is, data, LS_U_SHORT, count, swap, flt_fmt); |
|
2480 break; |
|
2481 |
|
2482 case miINT32: |
|
2483 read_doubles (is, data, LS_INT, count, swap, flt_fmt); |
|
2484 break; |
|
2485 |
|
2486 case miUINT32: |
|
2487 read_doubles (is, data, LS_U_INT, count, swap, flt_fmt); |
|
2488 break; |
|
2489 |
|
2490 case miSINGLE: |
|
2491 read_doubles (is, data, LS_FLOAT, count, swap, flt_fmt); |
|
2492 break; |
|
2493 |
|
2494 case miRESERVE1: |
|
2495 break; |
|
2496 |
|
2497 case miDOUBLE: |
|
2498 read_doubles (is, data, LS_DOUBLE, count, swap, flt_fmt); |
|
2499 break; |
|
2500 |
|
2501 case miRESERVE2: |
|
2502 case miRESERVE3: |
|
2503 break; |
|
2504 |
|
2505 case miINT64: |
|
2506 #ifdef EIGHT_BYTE_INT |
|
2507 read_doubles (is, data, LS_LONG, count, swap, flt_fmt); |
|
2508 #endif |
|
2509 break; |
|
2510 |
|
2511 case miUINT64: |
|
2512 #ifdef EIGHT_BYTE_INT |
|
2513 read_doubles (is, data, LS_U_LONG, count, swap, flt_fmt); |
|
2514 #endif |
|
2515 break; |
|
2516 |
|
2517 case miMATRIX: |
|
2518 default: |
|
2519 break; |
|
2520 } |
|
2521 } |
|
2522 |
|
2523 // Read one element tag from stream IS, |
|
2524 // place the type code in TYPE and the byte count in BYTES |
|
2525 // return nonzero on error |
|
2526 static int |
|
2527 read_mat5_tag (std::istream& is, bool swap, int& type, int& bytes) |
|
2528 { |
|
2529 unsigned int upper; |
|
2530 FOUR_BYTE_INT temp; |
|
2531 |
|
2532 if (! is.read (X_CAST (char *, &temp), 4 )) |
|
2533 goto data_read_error; |
|
2534 |
|
2535 if (swap) |
|
2536 swap_4_bytes ((char *)&temp); |
|
2537 |
|
2538 upper = (temp >> 16) & 0xffff; |
|
2539 type = temp & 0xffff; |
|
2540 |
|
2541 if (upper) |
|
2542 { |
|
2543 // "compressed" format |
|
2544 bytes = upper; |
|
2545 } |
|
2546 else |
|
2547 { |
|
2548 if (! is.read (X_CAST (char *, &temp), 4 )) |
|
2549 goto data_read_error; |
|
2550 if (swap) |
|
2551 swap_4_bytes ((char *)&temp); |
|
2552 bytes = temp; |
|
2553 } |
|
2554 |
|
2555 return 0; |
|
2556 |
|
2557 data_read_error: |
|
2558 return 1; |
|
2559 } |
|
2560 |
|
2561 // Extract one data element (scalar, matrix, string, etc.) from stream |
|
2562 // IS and place it in TC, returning the name of the variable. |
|
2563 // |
|
2564 // The data is expected to be in Matlab's "Version 5" .mat format, |
|
2565 // though not all the features of that format are supported. |
|
2566 // |
|
2567 // FILENAME is used for error messages. |
|
2568 |
4171
|
2569 static std::string |
3688
|
2570 read_mat5_binary_element (std::istream& is, const std::string& filename, |
|
2571 bool swap, bool& global, octave_value& tc) |
|
2572 { |
4171
|
2573 std::string retval; |
|
2574 |
3688
|
2575 // These are initialized here instead of closer to where they are |
|
2576 // first used to avoid errors from gcc about goto crossing |
|
2577 // initialization of variable. |
|
2578 |
|
2579 Matrix re; |
|
2580 oct_mach_info::float_format flt_fmt = oct_mach_info::unknown; |
|
2581 int type = 0; |
|
2582 bool imag; |
|
2583 bool logicalvar; |
|
2584 enum arrayclasstype arrayclass; |
|
2585 FOUR_BYTE_INT junk; |
|
2586 FOUR_BYTE_INT flags; |
|
2587 FOUR_BYTE_INT nr; |
|
2588 FOUR_BYTE_INT nc; |
|
2589 FOUR_BYTE_INT dimension_length; |
|
2590 int len; |
|
2591 int element_length; |
|
2592 std::streampos pos; |
|
2593 TWO_BYTE_INT number; |
|
2594 number = *(TWO_BYTE_INT *)"\x00\x01"; |
|
2595 |
|
2596 // MAT files always use IEEE floating point |
|
2597 if ((number == 1) ^ swap) |
|
2598 flt_fmt = oct_mach_info::ieee_big_endian; |
|
2599 else |
|
2600 flt_fmt = oct_mach_info::ieee_little_endian; |
|
2601 |
|
2602 // element type and length |
|
2603 if (read_mat5_tag (is, swap, type, element_length)) |
4171
|
2604 return retval; // EOF |
3688
|
2605 |
|
2606 if (type != miMATRIX) |
|
2607 { |
|
2608 error ("load: invalid element type"); |
|
2609 goto early_read_error; |
|
2610 } |
|
2611 pos = is.tellg (); |
|
2612 |
|
2613 // array flags subelement |
|
2614 if (read_mat5_tag (is, swap, type, len) || type != miUINT32 || len != 8) |
|
2615 { |
|
2616 error ("load: invalid array flags subelement"); |
|
2617 goto early_read_error; |
|
2618 } |
|
2619 |
|
2620 read_int (is, swap, flags); |
|
2621 imag = (flags & 0x0800) != 0; // has an imaginary part? |
|
2622 global = (flags & 0x0400) != 0; // global variable? |
|
2623 logicalvar = (flags & 0x0200) != 0; // we don't use this yet |
|
2624 arrayclass = (arrayclasstype)(flags & 0xff); |
|
2625 read_int (is, swap, junk); // an "undefined" entry |
|
2626 |
|
2627 // dimensions array subelement |
|
2628 { |
|
2629 std::streampos pos; |
|
2630 |
|
2631 if (read_mat5_tag (is, swap, type, dimension_length) || type != miINT32) |
|
2632 { |
|
2633 error ("load: invalid dimensions array subelement"); |
|
2634 goto early_read_error; |
|
2635 } |
|
2636 |
|
2637 pos = is.tellg (); |
|
2638 read_int (is, swap, nr); |
|
2639 read_int (is, swap, nc); |
|
2640 re.resize (nr, nc); |
|
2641 |
|
2642 // delay checking for a multidimensional array until we have read |
|
2643 // the variable name |
3812
|
2644 is.seekg (pos + static_cast<std::streamoff> (dimension_length)); |
3688
|
2645 } |
|
2646 |
|
2647 // array name subelement |
|
2648 { |
|
2649 std::streampos pos; |
|
2650 |
|
2651 if (read_mat5_tag (is, swap, type, len) || type != miINT8) |
|
2652 { |
|
2653 error ("load: invalid array name subelement"); |
|
2654 goto early_read_error; |
|
2655 } |
|
2656 |
|
2657 pos = is.tellg (); |
4249
|
2658 OCTAVE_LOCAL_BUFFER (char, name, len+1); |
3688
|
2659 |
|
2660 if (len) // structure field subelements have |
|
2661 // zero-length array name subelements |
|
2662 { |
|
2663 if (! is.read (X_CAST (char *, name), len )) |
|
2664 goto data_read_error; |
|
2665 |
3812
|
2666 is.seekg (pos + static_cast<std::streamoff> (PAD (len))); |
3688
|
2667 } |
|
2668 |
|
2669 name[len] = '\0'; |
4171
|
2670 retval = name; |
3688
|
2671 } |
|
2672 |
|
2673 if (dimension_length != 8) |
|
2674 { |
|
2675 error ("load: multidimension arrays are not implemented"); |
|
2676 goto skip_ahead; |
|
2677 } |
|
2678 |
|
2679 switch (arrayclass) |
|
2680 { |
|
2681 case mxCELL_CLASS: |
4332
|
2682 { |
|
2683 Cell cell_array (nr, nc); |
|
2684 |
|
2685 for (int j = 0; j < nc; j++) |
|
2686 { |
|
2687 for (int i = 0; i < nr; i++) |
|
2688 { |
|
2689 octave_value tc2; |
|
2690 |
|
2691 std::string nm |
|
2692 = read_mat5_binary_element (is, filename, swap, global, tc2); |
|
2693 |
|
2694 if (! is || error_state) |
|
2695 { |
|
2696 error ("load: reading cell data for `%s'", nm.c_str ()); |
|
2697 goto data_read_error; |
|
2698 } |
|
2699 |
|
2700 cell_array.elem (i, j) = tc2; |
|
2701 } |
|
2702 } |
|
2703 |
|
2704 tc = cell_array; |
|
2705 } |
|
2706 break; |
3688
|
2707 |
|
2708 case mxOBJECT_CLASS: |
|
2709 warning ("load: objects are not implemented"); |
|
2710 goto skip_ahead; |
|
2711 |
|
2712 case mxSPARSE_CLASS: |
|
2713 warning ("load: sparse arrays are not implemented"); |
|
2714 goto skip_ahead; |
|
2715 |
|
2716 case mxSTRUCT_CLASS: |
|
2717 { |
|
2718 Octave_map m; |
|
2719 FOUR_BYTE_INT type; |
|
2720 FOUR_BYTE_INT len; |
|
2721 FOUR_BYTE_INT field_name_length; |
|
2722 int i; |
|
2723 |
|
2724 // field name length subelement -- actually the maximum length |
|
2725 // of a field name. The Matlab docs promise this will always |
|
2726 // be 32. We read and use the actual value, on the theory |
|
2727 // that eventually someone will recognize that's a waste of |
|
2728 // space. |
|
2729 if (read_mat5_tag (is, swap, type, len) || type != miINT32) |
|
2730 { |
|
2731 error ("load: invalid field name subelement"); |
|
2732 goto data_read_error; |
|
2733 } |
|
2734 |
|
2735 if (! is.read (X_CAST (char *, &field_name_length), len )) |
|
2736 goto data_read_error; |
|
2737 |
|
2738 if (swap) |
|
2739 swap_4_bytes ((char *)&field_name_length); |
|
2740 |
|
2741 // field name subelement. The length of this subelement tells |
|
2742 // us how many fields there are. |
|
2743 if (read_mat5_tag (is, swap, type, len) || type != miINT8) |
|
2744 { |
|
2745 error ("load: invalid field name subelement"); |
|
2746 goto data_read_error; |
|
2747 } |
|
2748 |
4362
|
2749 int n_fields = len/field_name_length; |
|
2750 |
|
2751 len = PAD (len); |
|
2752 |
4249
|
2753 OCTAVE_LOCAL_BUFFER (char, elname, len); |
3688
|
2754 |
|
2755 if (! is.read (elname, len)) |
|
2756 goto data_read_error; |
|
2757 |
4362
|
2758 int n; |
|
2759 if (nr == 1) |
|
2760 n = nc; |
|
2761 else if (nc == 1) |
|
2762 n = nr; |
|
2763 else |
3688
|
2764 { |
4362
|
2765 error ("load: can only handle one-dimensional structure arrays"); |
|
2766 goto data_read_error; |
|
2767 } |
|
2768 |
|
2769 octave_value_list field_elts (n, Matrix ()); |
|
2770 |
|
2771 // fields subelements |
|
2772 for (i = 0; i < n_fields; i++) |
|
2773 { |
|
2774 for (int j = 0; j < n; j++) |
|
2775 { |
|
2776 octave_value fieldtc; |
|
2777 read_mat5_binary_element (is, filename, swap, global, fieldtc); |
|
2778 field_elts(j) = fieldtc; |
|
2779 } |
|
2780 |
|
2781 m.assign (elname + i*field_name_length, field_elts); |
3688
|
2782 } |
|
2783 |
|
2784 tc = m; |
|
2785 } |
|
2786 break; |
|
2787 |
|
2788 case mxCHAR_CLASS: |
|
2789 // handle as a numerical array to start with |
|
2790 |
|
2791 case mxDOUBLE_CLASS: |
|
2792 case mxSINGLE_CLASS: |
|
2793 case mxINT8_CLASS: |
|
2794 case mxUINT8_CLASS: |
|
2795 case mxINT16_CLASS: |
|
2796 case mxUINT16_CLASS: |
|
2797 case mxINT32_CLASS: |
|
2798 case mxUINT32_CLASS: |
|
2799 default: |
|
2800 // handle all these numerical formats as double arrays |
|
2801 |
|
2802 // real data subelement |
|
2803 { |
|
2804 std::streampos pos; |
|
2805 |
|
2806 if (read_mat5_tag (is, swap, type, len)) |
|
2807 { |
4171
|
2808 error ("load: reading matrix data for `%s'", retval.c_str ()); |
3688
|
2809 goto data_read_error; |
|
2810 } |
|
2811 |
|
2812 pos = is.tellg (); |
|
2813 read_mat5_binary_data (is, re.fortran_vec (), nr*nc, swap, |
|
2814 (enum mat5_data_type) type, flt_fmt); |
|
2815 |
|
2816 if (! is || error_state) |
|
2817 { |
4171
|
2818 error ("load: reading matrix data for `%s'", retval.c_str ()); |
3688
|
2819 goto data_read_error; |
|
2820 } |
|
2821 |
3812
|
2822 is.seekg (pos + static_cast<std::streamoff> (PAD (len))); |
3688
|
2823 } |
|
2824 |
|
2825 // imaginary data subelement |
|
2826 if (imag) |
|
2827 { |
|
2828 Matrix im (nr, nc); |
|
2829 |
|
2830 if (read_mat5_tag (is, swap, type, len)) |
|
2831 { |
4171
|
2832 error ("load: reading matrix data for `%s'", retval.c_str ()); |
3688
|
2833 goto data_read_error; |
|
2834 } |
|
2835 |
|
2836 read_mat5_binary_data (is, im.fortran_vec (), nr*nc, swap, |
|
2837 (enum mat5_data_type) type, flt_fmt); |
|
2838 |
|
2839 if (! is || error_state) |
|
2840 { |
4171
|
2841 error ("load: reading imaginary matrix data for `%s'", |
|
2842 retval.c_str ()); |
3688
|
2843 goto data_read_error; |
|
2844 } |
|
2845 |
|
2846 ComplexMatrix ctmp (nr, nc); |
|
2847 |
|
2848 for (int j = 0; j < nc; j++) |
|
2849 for (int i = 0; i < nr; i++) |
|
2850 ctmp (i, j) = Complex (re (i, j), im (i, j)); |
|
2851 |
|
2852 tc = ctmp; |
|
2853 } |
|
2854 else |
|
2855 tc = re; |
|
2856 |
|
2857 if (arrayclass == mxCHAR_CLASS) |
|
2858 tc = tc.convert_to_str (); |
|
2859 } |
|
2860 |
3812
|
2861 is.seekg (pos + static_cast<std::streamoff> (element_length)); |
3688
|
2862 |
4362
|
2863 if (is.eof ()) |
|
2864 is.clear (); |
|
2865 |
4171
|
2866 return retval; |
3688
|
2867 |
|
2868 data_read_error: |
|
2869 early_read_error: |
|
2870 error ("load: trouble reading binary file `%s'", filename.c_str ()); |
4171
|
2871 return std::string (); |
3688
|
2872 |
|
2873 skip_ahead: |
4171
|
2874 warning ("skipping over `%s'", retval.c_str ()); |
3812
|
2875 is.seekg (pos + static_cast<std::streamoff> (element_length)); |
3688
|
2876 return read_mat5_binary_element (is, filename, swap, global, tc); |
|
2877 } |
|
2878 |
|
2879 static int |
|
2880 read_mat5_binary_file_header (std::istream& is, bool& swap, |
|
2881 bool quiet = false) |
|
2882 { |
|
2883 TWO_BYTE_INT version=0, magic=0; |
|
2884 |
|
2885 is.seekg (124, std::ios::beg); |
|
2886 is.read (X_CAST (char *, &version), 2); |
|
2887 is.read (X_CAST (char *, &magic), 2); |
|
2888 |
|
2889 if (magic == 0x4d49) |
|
2890 swap = 0; |
|
2891 else if (magic == 0x494d) |
|
2892 swap = 1; |
|
2893 else |
|
2894 { |
|
2895 if (! quiet) |
|
2896 error ("load: can't read binary file"); |
|
2897 return -1; |
|
2898 } |
|
2899 |
|
2900 if (! swap) // version number is inverse swapped! |
|
2901 version = ((version >> 8) & 0xff) + ((version & 0xff) << 8); |
|
2902 |
|
2903 if (version != 1 && !quiet) |
|
2904 warning ("load: found version %d binary MAT file, " |
|
2905 "but only prepared for version 1", version); |
|
2906 |
|
2907 return 0; |
|
2908 } |
|
2909 |
3019
|
2910 // Return TRUE if NAME matches one of the given globbing PATTERNS. |
604
|
2911 |
3013
|
2912 static bool |
3769
|
2913 matches_patterns (const string_vector& patterns, int pat_idx, |
3523
|
2914 int num_pat, const std::string& name) |
604
|
2915 { |
1755
|
2916 for (int i = pat_idx; i < num_pat; i++) |
604
|
2917 { |
1792
|
2918 glob_match pattern (patterns[i]); |
3013
|
2919 |
1792
|
2920 if (pattern.match (name)) |
3013
|
2921 return true; |
604
|
2922 } |
3688
|
2923 |
3013
|
2924 return false; |
604
|
2925 } |
|
2926 |
4329
|
2927 int |
3523
|
2928 read_binary_file_header (std::istream& is, bool& swap, |
4329
|
2929 oct_mach_info::float_format& flt_fmt, bool quiet) |
604
|
2930 { |
3552
|
2931 const int magic_len = 10; |
|
2932 char magic[magic_len+1]; |
3557
|
2933 is.read (X_CAST (char *, magic), magic_len); |
604
|
2934 magic[magic_len] = '\0'; |
3688
|
2935 |
604
|
2936 if (strncmp (magic, "Octave-1-L", magic_len) == 0) |
2318
|
2937 swap = oct_mach_info::words_big_endian (); |
604
|
2938 else if (strncmp (magic, "Octave-1-B", magic_len) == 0) |
2318
|
2939 swap = ! oct_mach_info::words_big_endian (); |
604
|
2940 else |
|
2941 { |
|
2942 if (! quiet) |
|
2943 error ("load: can't read binary file"); |
|
2944 return -1; |
|
2945 } |
|
2946 |
|
2947 char tmp = 0; |
3557
|
2948 is.read (X_CAST (char *, &tmp), 1); |
604
|
2949 |
2318
|
2950 flt_fmt = mopt_digit_to_float_format (tmp); |
|
2951 |
|
2952 if (flt_fmt == oct_mach_info::unknown) |
604
|
2953 { |
|
2954 if (! quiet) |
|
2955 error ("load: unrecognized binary format!"); |
3688
|
2956 |
604
|
2957 return -1; |
|
2958 } |
|
2959 |
|
2960 return 0; |
|
2961 } |
|
2962 |
|
2963 static load_save_format |
3523
|
2964 get_file_format (const std::string& fname, const std::string& orig_fname) |
604
|
2965 { |
|
2966 load_save_format retval = LS_UNKNOWN; |
|
2967 |
3687
|
2968 #ifdef HAVE_HDF5 |
3688
|
2969 // check this before we open the file |
3687
|
2970 if (H5Fis_hdf5 (fname.c_str ()) > 0) |
|
2971 return LS_HDF5; |
|
2972 #endif /* HAVE_HDF5 */ |
|
2973 |
3523
|
2974 std::ifstream file (fname.c_str ()); |
604
|
2975 |
|
2976 if (! file) |
|
2977 { |
1750
|
2978 error ("load: couldn't open input file `%s'", orig_fname.c_str ()); |
604
|
2979 return retval; |
|
2980 } |
|
2981 |
2318
|
2982 oct_mach_info::float_format flt_fmt = oct_mach_info::unknown; |
604
|
2983 |
3019
|
2984 bool swap = false; |
|
2985 |
|
2986 if (read_binary_file_header (file, swap, flt_fmt, true) == 0) |
604
|
2987 retval = LS_BINARY; |
|
2988 else |
|
2989 { |
3538
|
2990 file.seekg (0, std::ios::beg); |
604
|
2991 |
|
2992 FOUR_BYTE_INT mopt, nr, nc, imag, len; |
1180
|
2993 |
|
2994 int err = read_mat_file_header (file, swap, mopt, nr, nc, imag, len, 1); |
|
2995 |
|
2996 if (! err) |
604
|
2997 retval = LS_MAT_BINARY; |
|
2998 else |
|
2999 { |
2511
|
3000 file.clear (); |
3538
|
3001 file.seekg (0, std::ios::beg); |
604
|
3002 |
3697
|
3003 err = read_mat5_binary_file_header (file, swap, true); |
3688
|
3004 |
|
3005 if (! err) |
|
3006 { |
|
3007 file.clear (); |
|
3008 file.seekg (0, std::ios::beg); |
|
3009 retval = LS_MAT5_BINARY; |
|
3010 } |
|
3011 else |
|
3012 { |
|
3013 file.clear (); |
|
3014 file.seekg (0, std::ios::beg); |
|
3015 |
4171
|
3016 std::string tmp = extract_keyword (file, "name"); |
|
3017 |
|
3018 if (! tmp.empty ()) |
|
3019 retval = LS_ASCII; |
3688
|
3020 else |
|
3021 { |
|
3022 // Try reading the file as numbers only, determining the |
|
3023 // number of rows and columns from the data. We don't |
|
3024 // even bother to check to see if the first item in the |
|
3025 // file is a number, so that get_complete_line() can |
|
3026 // skip any comments that might appear at the top of the |
|
3027 // file. |
|
3028 |
|
3029 retval = LS_MAT_ASCII; |
|
3030 } |
2511
|
3031 } |
604
|
3032 } |
|
3033 } |
|
3034 |
|
3035 file.close (); |
|
3036 |
|
3037 if (retval == LS_UNKNOWN) |
1750
|
3038 error ("load: unable to determine file format for `%s'", |
|
3039 orig_fname.c_str ()); |
604
|
3040 |
|
3041 return retval; |
|
3042 } |
|
3043 |
4329
|
3044 octave_value |
3523
|
3045 do_load (std::istream& stream, const std::string& orig_fname, bool force, |
2318
|
3046 load_save_format format, oct_mach_info::float_format flt_fmt, |
3687
|
3047 bool list_only, bool swap, bool verbose, bool import, |
|
3048 const string_vector& argv, int argv_idx, int argc, int nargout) |
604
|
3049 { |
3727
|
3050 octave_value retval; |
|
3051 |
|
3052 Octave_map retstruct; |
604
|
3053 |
4051
|
3054 OSSTREAM output_buf; |
|
3055 |
604
|
3056 int count = 0; |
4051
|
3057 |
604
|
3058 for (;;) |
|
3059 { |
3019
|
3060 bool global = false; |
2086
|
3061 octave_value tc; |
604
|
3062 |
4171
|
3063 std::string name; |
|
3064 std::string doc; |
604
|
3065 |
|
3066 switch (format) |
|
3067 { |
|
3068 case LS_ASCII: |
3136
|
3069 name = read_ascii_data (stream, orig_fname, global, tc, count); |
604
|
3070 break; |
|
3071 |
|
3072 case LS_BINARY: |
|
3073 name = read_binary_data (stream, swap, flt_fmt, orig_fname, |
|
3074 global, tc, doc); |
|
3075 break; |
|
3076 |
2511
|
3077 case LS_MAT_ASCII: |
|
3078 name = read_mat_ascii_data (stream, orig_fname, tc); |
|
3079 break; |
|
3080 |
604
|
3081 case LS_MAT_BINARY: |
|
3082 name = read_mat_binary_data (stream, orig_fname, tc); |
|
3083 break; |
|
3084 |
3687
|
3085 #ifdef HAVE_HDF5 |
|
3086 case LS_HDF5: |
|
3087 name = read_hdf5_data (stream, orig_fname, |
|
3088 global, tc, doc, import); |
|
3089 break; |
|
3090 #endif /* HAVE_HDF5 */ |
|
3091 |
3688
|
3092 case LS_MAT5_BINARY: |
|
3093 name = read_mat5_binary_element (stream, orig_fname, swap, |
|
3094 global, tc); |
|
3095 break; |
|
3096 |
604
|
3097 default: |
775
|
3098 gripe_unrecognized_data_fmt ("load"); |
604
|
3099 break; |
|
3100 } |
|
3101 |
4171
|
3102 if (error_state || stream.eof () || name.empty ()) |
|
3103 break; |
|
3104 else if (! error_state && ! name.empty ()) |
604
|
3105 { |
|
3106 if (tc.is_defined ()) |
|
3107 { |
3136
|
3108 if (format == LS_MAT_ASCII && argv_idx < argc) |
|
3109 warning ("load: loaded ASCII file `%s' -- ignoring extra args", |
3687
|
3110 orig_fname.c_str ()); |
3136
|
3111 |
|
3112 if (format == LS_MAT_ASCII |
|
3113 || argv_idx == argc |
1755
|
3114 || matches_patterns (argv, argv_idx, argc, name)) |
604
|
3115 { |
|
3116 count++; |
621
|
3117 if (list_only) |
|
3118 { |
|
3119 if (verbose) |
|
3120 { |
|
3121 if (count == 1) |
|
3122 output_buf |
|
3123 << "type rows cols name\n" |
|
3124 << "==== ==== ==== ====\n"; |
|
3125 |
3013
|
3126 output_buf |
3548
|
3127 << std::setiosflags (std::ios::left) |
|
3128 << std::setw (16) << tc.type_name () . c_str () |
|
3129 << std::setiosflags (std::ios::right) |
|
3130 << std::setw (7) << tc.rows () |
|
3131 << std::setw (7) << tc.columns () |
3013
|
3132 << " "; |
621
|
3133 } |
|
3134 output_buf << name << "\n"; |
|
3135 } |
|
3136 else |
|
3137 { |
3727
|
3138 if (nargout == 1) |
|
3139 { |
|
3140 if (format == LS_MAT_ASCII) |
|
3141 retval = tc; |
|
3142 else |
|
3143 retstruct[name] = tc; |
|
3144 } |
|
3145 else |
|
3146 install_loaded_variable (force, name, tc, global, doc); |
621
|
3147 } |
604
|
3148 } |
2511
|
3149 |
|
3150 // Only attempt to read one item from a headless text file. |
|
3151 |
|
3152 if (format == LS_MAT_ASCII) |
|
3153 break; |
604
|
3154 } |
|
3155 else |
4171
|
3156 error ("load: unable to load variable `%s'", name.c_str ()); |
604
|
3157 } |
|
3158 else |
|
3159 { |
|
3160 if (count == 0) |
|
3161 error ("load: are you sure `%s' is an Octave data file?", |
1755
|
3162 orig_fname.c_str ()); |
604
|
3163 |
|
3164 break; |
|
3165 } |
|
3166 } |
|
3167 |
621
|
3168 if (list_only && count) |
|
3169 { |
4051
|
3170 output_buf << OSSTREAM_ENDS; |
|
3171 std::string msg = OSSTREAM_STR (output_buf); |
|
3172 OSSTREAM_FREEZE (output_buf); |
2095
|
3173 |
621
|
3174 if (nargout > 0) |
2095
|
3175 retval = msg; |
621
|
3176 else |
2095
|
3177 octave_stdout << msg; |
621
|
3178 } |
3727
|
3179 else if (! retstruct.empty ()) |
|
3180 retval = retstruct; |
621
|
3181 |
863
|
3182 return retval; |
|
3183 } |
|
3184 |
3687
|
3185 // HDF5 load/save documentation is included in the Octave manual |
|
3186 // regardless, but if HDF5 is not linked in we also include a |
|
3187 // sentence noting this, so the user understands that the features |
|
3188 // aren't available. Define a macro for this sentence: |
|
3189 |
|
3190 #ifdef HAVE_HDF5 |
|
3191 #define HAVE_HDF5_HELP_STRING "" |
|
3192 #else /* ! HAVE_HDF5 */ |
|
3193 #define HAVE_HDF5_HELP_STRING "\n\ |
|
3194 HDF5 load and save are not available, as this Octave executable was\n\ |
|
3195 not linked with the HDF5 library." |
|
3196 #endif /* ! HAVE HDF5 */ |
|
3197 |
4208
|
3198 DEFCMD (load, args, nargout, |
3372
|
3199 "-*- texinfo -*-\n\ |
|
3200 @deffn {Command} load options file v1 v2 @dots{}\n\ |
|
3201 Load the named variables from the file @var{file}. As with @code{save},\n\ |
|
3202 you may specify a list of variables and @code{load} will only extract\n\ |
|
3203 those variables with names that match. For example, to restore the\n\ |
|
3204 variables saved in the file @file{data}, use the command\n\ |
|
3205 \n\ |
|
3206 @example\n\ |
|
3207 load data\n\ |
|
3208 @end example\n\ |
863
|
3209 \n\ |
3372
|
3210 Octave will refuse to overwrite existing variables unless you use the\n\ |
|
3211 option @samp{-force}.\n\ |
|
3212 \n\ |
|
3213 If a variable that is not marked as global is loaded from a file when a\n\ |
|
3214 global symbol with the same name already exists, it is loaded in the\n\ |
|
3215 global symbol table. Also, if a variable is marked as global in a file\n\ |
|
3216 and a local symbol exists, the local symbol is moved to the global\n\ |
|
3217 symbol table and given the value from the file. Since it seems that\n\ |
|
3218 both of these cases are likely to be the result of some sort of error,\n\ |
|
3219 they will generate warnings.\n\ |
863
|
3220 \n\ |
3727
|
3221 If invoked with a single output argument, Octave returns data instead\n\ |
|
3222 of inserting variables in the symbol table. If the data file contains\n\ |
|
3223 only numbers (TAB- or space-delimited columns), a matrix of values is\n\ |
|
3224 returned. Otherwise, @code{load} returns a structure with members\n\ |
|
3225 corresponding to the names of the variables in the file.\n\ |
|
3226 \n\ |
3372
|
3227 The @code{load} command can read data stored in Octave's text and\n\ |
|
3228 binary formats, and @sc{Matlab}'s binary format. It will automatically\n\ |
|
3229 detect the type of file and do conversion from different floating point\n\ |
|
3230 formats (currently only IEEE big and little endian, though other formats\n\ |
|
3231 may added in the future).\n\ |
|
3232 \n\ |
|
3233 Valid options for @code{load} are listed in the following table.\n\ |
863
|
3234 \n\ |
3372
|
3235 @table @code\n\ |
|
3236 @item -force\n\ |
|
3237 Force variables currently in memory to be overwritten by variables with\n\ |
|
3238 the same name found in the file.\n\ |
|
3239 \n\ |
|
3240 @item -ascii\n\ |
|
3241 Force Octave to assume the file is in Octave's text format.\n\ |
|
3242 \n\ |
|
3243 @item -binary\n\ |
|
3244 Force Octave to assume the file is in Octave's binary format.\n\ |
|
3245 \n\ |
|
3246 @item -mat-binary\n\ |
|
3247 Force Octave to assume the file is in @sc{Matlab}'s binary format.\n\ |
3687
|
3248 \n\ |
3688
|
3249 @item -mat4-binary\n\ |
|
3250 Force Octave to assume the file is in the binary format written by\n\ |
|
3251 @sc{Matlab} version 4.\n\ |
|
3252 \n\ |
3687
|
3253 @item -hdf5\n\ |
|
3254 Force Octave to assume the file is in HDF5 format.\n\ |
|
3255 (HDF5 is a free, portable binary format developed by the National\n\ |
|
3256 Center for Supercomputing Applications at the University of Illinois.)\n\ |
|
3257 Note that Octave can read HDF5 files not created by itself, but may\n\ |
|
3258 skip some datasets in formats that it cannot support. In particular,\n\ |
|
3259 it will skip datasets of data types that it does not recognize, with\n\ |
|
3260 dimensionality > 2, or with names that aren't valid Octave identifiers\n\ |
|
3261 See, however, the @samp{-import} option to ameliorate this somewhat.\n" |
|
3262 |
|
3263 HAVE_HDF5_HELP_STRING |
|
3264 |
|
3265 "\n\ |
|
3266 @item -import\n\ |
|
3267 Make a stronger attempt to import foreign datasets. Currently, this means\n\ |
|
3268 that for HDF5 files, invalid characters in names are converted to @samp{_},\n\ |
|
3269 and datasets with dimensionality > 2 are imported as lists of matrices (or\n\ |
|
3270 lists of lists of matrices, or ...).\n\ |
|
3271 \n\ |
3372
|
3272 @end table\n\ |
|
3273 @end deffn") |
863
|
3274 { |
2086
|
3275 octave_value_list retval; |
863
|
3276 |
1755
|
3277 int argc = args.length () + 1; |
|
3278 |
1968
|
3279 string_vector argv = args.make_argv ("load"); |
1755
|
3280 |
|
3281 if (error_state) |
|
3282 return retval; |
863
|
3283 |
1358
|
3284 // It isn't necessary to have the default load format stored in a |
|
3285 // user preference variable since we can determine the type of file |
|
3286 // as we are reading. |
863
|
3287 |
|
3288 load_save_format format = LS_UNKNOWN; |
|
3289 |
3019
|
3290 bool force = false; |
|
3291 bool list_only = false; |
|
3292 bool verbose = false; |
3687
|
3293 bool import = false; |
863
|
3294 |
1755
|
3295 int i; |
|
3296 for (i = 1; i < argc; i++) |
863
|
3297 { |
1755
|
3298 if (argv[i] == "-force" || argv[i] == "-f") |
863
|
3299 { |
3019
|
3300 force = true; |
863
|
3301 } |
1755
|
3302 else if (argv[i] == "-list" || argv[i] == "-l") |
863
|
3303 { |
3019
|
3304 list_only = true; |
863
|
3305 } |
1755
|
3306 else if (argv[i] == "-verbose" || argv[i] == "-v") |
863
|
3307 { |
3019
|
3308 verbose = true; |
863
|
3309 } |
1755
|
3310 else if (argv[i] == "-ascii" || argv[i] == "-a") |
863
|
3311 { |
|
3312 format = LS_ASCII; |
|
3313 } |
1755
|
3314 else if (argv[i] == "-binary" || argv[i] == "-b") |
863
|
3315 { |
|
3316 format = LS_BINARY; |
|
3317 } |
1755
|
3318 else if (argv[i] == "-mat-binary" || argv[i] == "-m") |
863
|
3319 { |
3688
|
3320 format = LS_MAT5_BINARY; |
|
3321 } |
3797
|
3322 else if (argv[i] == "-mat4-binary" || argv[i] == "-4" || argv[i] == "-v4") |
3688
|
3323 { |
863
|
3324 format = LS_MAT_BINARY; |
|
3325 } |
3687
|
3326 else if (argv[i] == "-hdf5" || argv[i] == "-h") |
|
3327 { |
|
3328 #ifdef HAVE_HDF5 |
|
3329 format = LS_HDF5; |
|
3330 #else /* ! HAVE_HDF5 */ |
|
3331 error ("load: octave executable was not linked with HDF5 library"); |
|
3332 return retval; |
|
3333 #endif /* ! HAVE_HDF5 */ |
|
3334 } |
|
3335 else if (argv[i] == "-import" || argv[i] == "-i") |
|
3336 { |
|
3337 import = true; |
|
3338 } |
863
|
3339 else |
|
3340 break; |
|
3341 } |
|
3342 |
1755
|
3343 if (i == argc) |
863
|
3344 { |
2057
|
3345 print_usage ("load"); |
863
|
3346 return retval; |
|
3347 } |
|
3348 |
3523
|
3349 std::string orig_fname = argv[i]; |
863
|
3350 |
2318
|
3351 oct_mach_info::float_format flt_fmt = oct_mach_info::unknown; |
863
|
3352 |
3019
|
3353 bool swap = false; |
863
|
3354 |
1755
|
3355 if (argv[i] == "-") |
863
|
3356 { |
1755
|
3357 i++; |
863
|
3358 |
3687
|
3359 #ifdef HAVE_HDF5 |
|
3360 if (format == LS_HDF5) |
|
3361 error ("load: cannot read HDF5 format from stdin"); |
|
3362 else |
|
3363 #endif /* HAVE_HDF5 */ |
863
|
3364 if (format != LS_UNKNOWN) |
|
3365 { |
1358
|
3366 // XXX FIXME XXX -- if we have already seen EOF on a |
3531
|
3367 // previous call, how do we fix up the state of std::cin so |
|
3368 // that we can get additional input? I'm afraid that we |
|
3369 // can't fix this using std::cin only. |
|
3370 |
|
3371 retval = do_load (std::cin, orig_fname, force, format, flt_fmt, |
3687
|
3372 list_only, swap, verbose, import, argv, i, argc, |
863
|
3373 nargout); |
|
3374 } |
|
3375 else |
|
3376 error ("load: must specify file format if reading from stdin"); |
|
3377 } |
|
3378 else |
|
3379 { |
3523
|
3380 std::string fname = file_ops::tilde_expand (argv[i]); |
863
|
3381 |
|
3382 if (format == LS_UNKNOWN) |
|
3383 format = get_file_format (fname, orig_fname); |
|
3384 |
3687
|
3385 #ifdef HAVE_HDF5 |
|
3386 if (format == LS_HDF5) |
|
3387 { |
|
3388 i++; |
|
3389 |
|
3390 hdf5_ifstream hdf5_file (fname.c_str ()); |
|
3391 |
|
3392 if (hdf5_file.file_id >= 0) |
|
3393 { |
|
3394 retval = do_load (hdf5_file, orig_fname, force, format, |
|
3395 flt_fmt, list_only, swap, verbose, |
|
3396 import, argv, i, argc, nargout); |
|
3397 |
|
3398 hdf5_file.close (); |
|
3399 } |
|
3400 else |
|
3401 error ("load: couldn't open input file `%s'", |
|
3402 orig_fname.c_str ()); |
|
3403 } |
|
3404 else |
|
3405 #endif /* HAVE_HDF5 */ |
|
3406 // don't insert any statements here; the "else" above has to |
|
3407 // go with the "if" below!!!!! |
863
|
3408 if (format != LS_UNKNOWN) |
|
3409 { |
1755
|
3410 i++; |
863
|
3411 |
3775
|
3412 std::ios::openmode mode = std::ios::in; |
3688
|
3413 if (format == LS_BINARY || |
|
3414 format == LS_MAT_BINARY || |
|
3415 format == LS_MAT5_BINARY) |
3552
|
3416 mode |= std::ios::binary; |
863
|
3417 |
3523
|
3418 std::ifstream file (fname.c_str (), mode); |
863
|
3419 |
|
3420 if (file) |
|
3421 { |
|
3422 if (format == LS_BINARY) |
|
3423 { |
|
3424 if (read_binary_file_header (file, swap, flt_fmt) < 0) |
|
3425 { |
|
3426 file.close (); |
|
3427 return retval; |
|
3428 } |
|
3429 } |
3688
|
3430 else if (format == LS_MAT5_BINARY) |
|
3431 { |
|
3432 if (read_mat5_binary_file_header (file, swap, false) < 0) |
|
3433 { |
|
3434 file.close (); |
|
3435 return retval; |
|
3436 } |
|
3437 } |
863
|
3438 |
|
3439 retval = do_load (file, orig_fname, force, format, |
3687
|
3440 flt_fmt, list_only, swap, verbose, import, |
1755
|
3441 argv, i, argc, nargout); |
863
|
3442 file.close (); |
|
3443 } |
|
3444 else |
1755
|
3445 error ("load: couldn't open input file `%s'", |
|
3446 orig_fname.c_str ()); |
863
|
3447 } |
|
3448 } |
604
|
3449 |
|
3450 return retval; |
|
3451 } |
|
3452 |
3019
|
3453 // Return TRUE if PATTERN has any special globbing chars in it. |
|
3454 |
|
3455 static bool |
3523
|
3456 glob_pattern_p (const std::string& pattern) |
604
|
3457 { |
|
3458 int open = 0; |
|
3459 |
1755
|
3460 int len = pattern.length (); |
|
3461 |
|
3462 for (int i = 0; i < len; i++) |
604
|
3463 { |
1755
|
3464 char c = pattern[i]; |
|
3465 |
604
|
3466 switch (c) |
|
3467 { |
|
3468 case '?': |
|
3469 case '*': |
3019
|
3470 return true; |
604
|
3471 |
|
3472 case '[': // Only accept an open brace if there is a close |
|
3473 open++; // brace to match it. Bracket expressions must be |
|
3474 continue; // complete, according to Posix.2 |
|
3475 |
|
3476 case ']': |
|
3477 if (open) |
3019
|
3478 return true; |
604
|
3479 continue; |
|
3480 |
|
3481 case '\\': |
1755
|
3482 if (i == len - 1) |
3019
|
3483 return false; |
604
|
3484 |
|
3485 default: |
|
3486 continue; |
|
3487 } |
|
3488 } |
|
3489 |
3019
|
3490 return false; |
604
|
3491 } |
|
3492 |
618
|
3493 // MAX_VAL and MIN_VAL are assumed to have integral values even though |
|
3494 // they are stored in doubles. |
|
3495 |
604
|
3496 static save_type |
|
3497 get_save_type (double max_val, double min_val) |
|
3498 { |
|
3499 save_type st = LS_DOUBLE; |
|
3500 |
|
3501 if (max_val < 256 && min_val > -1) |
|
3502 st = LS_U_CHAR; |
|
3503 else if (max_val < 65536 && min_val > -1) |
|
3504 st = LS_U_SHORT; |
3131
|
3505 else if (max_val < 4294967295UL && min_val > -1) |
618
|
3506 st = LS_U_INT; |
|
3507 else if (max_val < 128 && min_val >= -128) |
|
3508 st = LS_CHAR; |
604
|
3509 else if (max_val < 32768 && min_val >= -32768) |
|
3510 st = LS_SHORT; |
3131
|
3511 else if (max_val <= 2147483647L && min_val >= -2147483647L) |
604
|
3512 st = LS_INT; |
|
3513 |
|
3514 return st; |
|
3515 } |
|
3516 |
|
3517 // Save the data from TC along with the corresponding NAME, help |
|
3518 // string DOC, and global flag MARK_AS_GLOBAL on stream OS in the |
1427
|
3519 // binary format described above for read_binary_data. |
604
|
3520 |
2799
|
3521 static bool |
3523
|
3522 save_binary_data (std::ostream& os, const octave_value& tc, |
|
3523 const std::string& name, const std::string& doc, |
3019
|
3524 bool mark_as_global, bool save_as_floats) |
604
|
3525 { |
1755
|
3526 FOUR_BYTE_INT name_len = name.length (); |
604
|
3527 |
3557
|
3528 os.write (X_CAST (char *, &name_len), 4); |
1755
|
3529 os << name; |
|
3530 |
|
3531 FOUR_BYTE_INT doc_len = doc.length (); |
604
|
3532 |
3557
|
3533 os.write (X_CAST (char *, &doc_len), 4); |
1755
|
3534 os << doc; |
604
|
3535 |
|
3536 char tmp; |
|
3537 |
|
3538 tmp = mark_as_global; |
3557
|
3539 os.write (X_CAST (char *, &tmp), 1); |
604
|
3540 |
3233
|
3541 if (tc.is_string ()) |
|
3542 { |
|
3543 tmp = 7; |
3557
|
3544 os.write (X_CAST (char *, &tmp), 1); |
3233
|
3545 FOUR_BYTE_INT nr = tc.rows (); |
3557
|
3546 os.write (X_CAST (char *, &nr), 4); |
3233
|
3547 charMatrix chm = tc.char_matrix_value (); |
|
3548 for (int i = 0; i < nr; i++) |
|
3549 { |
|
3550 FOUR_BYTE_INT len = chm.cols (); |
3557
|
3551 os.write (X_CAST (char *, &len), 4); |
3523
|
3552 std::string tstr = chm.row_as_string (i); |
3233
|
3553 const char *tmp = tstr.data (); |
3557
|
3554 os.write (X_CAST (char *, tmp), len); |
3233
|
3555 } |
|
3556 } |
|
3557 else if (tc.is_range ()) |
|
3558 { |
|
3559 tmp = 6; |
3557
|
3560 os.write (X_CAST (char *, &tmp), 1); |
3233
|
3561 tmp = (char) LS_DOUBLE; |
3557
|
3562 os.write (X_CAST (char *, &tmp), 1); |
3233
|
3563 Range r = tc.range_value (); |
|
3564 double bas = r.base (); |
|
3565 double lim = r.limit (); |
|
3566 double inc = r.inc (); |
3557
|
3567 os.write (X_CAST (char *, &bas), 8); |
|
3568 os.write (X_CAST (char *, &lim), 8); |
|
3569 os.write (X_CAST (char *, &inc), 8); |
3233
|
3570 } |
|
3571 else if (tc.is_real_scalar ()) |
604
|
3572 { |
|
3573 tmp = 1; |
3557
|
3574 os.write (X_CAST (char *, &tmp), 1); |
630
|
3575 tmp = (char) LS_DOUBLE; |
3557
|
3576 os.write (X_CAST (char *, &tmp), 1); |
604
|
3577 double tmp = tc.double_value (); |
3557
|
3578 os.write (X_CAST (char *, &tmp), 8); |
604
|
3579 } |
620
|
3580 else if (tc.is_real_matrix ()) |
604
|
3581 { |
|
3582 tmp = 2; |
3557
|
3583 os.write (X_CAST (char *, &tmp), 1); |
604
|
3584 Matrix m = tc.matrix_value (); |
|
3585 FOUR_BYTE_INT nr = m.rows (); |
|
3586 FOUR_BYTE_INT nc = m.columns (); |
3557
|
3587 os.write (X_CAST (char *, &nr), 4); |
|
3588 os.write (X_CAST (char *, &nc), 4); |
604
|
3589 int len = nr * nc; |
|
3590 save_type st = LS_DOUBLE; |
630
|
3591 if (save_as_floats) |
|
3592 { |
1963
|
3593 if (m.too_large_for_float ()) |
630
|
3594 { |
|
3595 warning ("save: some values too large to save as floats --"); |
|
3596 warning ("save: saving as doubles instead"); |
|
3597 } |
|
3598 else |
|
3599 st = LS_FLOAT; |
|
3600 } |
|
3601 else if (len > 8192) // XXX FIXME XXX -- make this configurable. |
604
|
3602 { |
|
3603 double max_val, min_val; |
1963
|
3604 if (m.all_integers (max_val, min_val)) |
604
|
3605 st = get_save_type (max_val, min_val); |
|
3606 } |
630
|
3607 const double *mtmp = m.data (); |
604
|
3608 write_doubles (os, mtmp, st, len); |
|
3609 } |
|
3610 else if (tc.is_complex_scalar ()) |
|
3611 { |
|
3612 tmp = 3; |
3557
|
3613 os.write (X_CAST (char *, &tmp), 1); |
630
|
3614 tmp = (char) LS_DOUBLE; |
3557
|
3615 os.write (X_CAST (char *, &tmp), 1); |
604
|
3616 Complex tmp = tc.complex_value (); |
3557
|
3617 os.write (X_CAST (char *, &tmp), 16); |
604
|
3618 } |
|
3619 else if (tc.is_complex_matrix ()) |
|
3620 { |
|
3621 tmp = 4; |
3557
|
3622 os.write (X_CAST (char *, &tmp), 1); |
604
|
3623 ComplexMatrix m = tc.complex_matrix_value (); |
|
3624 FOUR_BYTE_INT nr = m.rows (); |
|
3625 FOUR_BYTE_INT nc = m.columns (); |
3557
|
3626 os.write (X_CAST (char *, &nr), 4); |
|
3627 os.write (X_CAST (char *, &nc), 4); |
604
|
3628 int len = nr * nc; |
|
3629 save_type st = LS_DOUBLE; |
630
|
3630 if (save_as_floats) |
|
3631 { |
1963
|
3632 if (m.too_large_for_float ()) |
630
|
3633 { |
|
3634 warning ("save: some values too large to save as floats --"); |
|
3635 warning ("save: saving as doubles instead"); |
|
3636 } |
|
3637 else |
|
3638 st = LS_FLOAT; |
|
3639 } |
|
3640 else if (len > 4096) // XXX FIXME XXX -- make this configurable. |
604
|
3641 { |
|
3642 double max_val, min_val; |
1963
|
3643 if (m.all_integers (max_val, min_val)) |
604
|
3644 st = get_save_type (max_val, min_val); |
|
3645 } |
630
|
3646 const Complex *mtmp = m.data (); |
3145
|
3647 write_doubles (os, X_CAST (const double *, mtmp), st, 2*len); |
604
|
3648 } |
|
3649 else |
2799
|
3650 gripe_wrong_type_arg ("save", tc, false); |
|
3651 |
|
3652 return os; |
604
|
3653 } |
|
3654 |
3687
|
3655 #ifdef HAVE_HDF5 |
|
3656 |
|
3657 // Add an attribute named attr_name to loc_id (a simple scalar |
|
3658 // attribute with value 1). Return value is >= 0 on success. |
|
3659 static herr_t |
|
3660 hdf5_add_attr (hid_t loc_id, const char *attr_name) |
|
3661 { |
|
3662 herr_t retval = 0; |
|
3663 |
|
3664 hid_t as_id = H5Screate (H5S_SCALAR); |
|
3665 |
|
3666 if (as_id >= 0) |
|
3667 { |
|
3668 hid_t a_id = H5Acreate (loc_id, attr_name, |
|
3669 H5T_NATIVE_UCHAR, as_id, H5P_DEFAULT); |
|
3670 |
|
3671 if (a_id >= 0) |
|
3672 { |
|
3673 unsigned char attr_val = 1; |
|
3674 |
|
3675 retval = H5Awrite (a_id, H5T_NATIVE_UCHAR, (void*) &attr_val); |
|
3676 |
|
3677 H5Aclose (a_id); |
|
3678 } |
|
3679 else |
|
3680 retval = a_id; |
|
3681 |
|
3682 H5Sclose (as_id); |
|
3683 } |
|
3684 else |
|
3685 retval = as_id; |
|
3686 |
|
3687 return retval; |
|
3688 } |
|
3689 |
|
3690 |
|
3691 // save_type_to_hdf5 is not currently used, since hdf5 doesn't yet support |
|
3692 // automatic float<->integer conversions: |
|
3693 |
|
3694 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS |
|
3695 |
|
3696 // return the HDF5 type id corresponding to the Octave save_type |
|
3697 |
|
3698 static hid_t |
|
3699 save_type_to_hdf5 (save_type st) |
|
3700 { |
|
3701 switch (st) |
|
3702 { |
|
3703 case LS_U_CHAR: |
|
3704 return H5T_NATIVE_UCHAR; |
|
3705 |
|
3706 case LS_U_SHORT: |
|
3707 return H5T_NATIVE_USHORT; |
|
3708 |
|
3709 case LS_U_INT: |
|
3710 return H5T_NATIVE_UINT; |
|
3711 |
|
3712 case LS_CHAR: |
|
3713 return H5T_NATIVE_CHAR; |
|
3714 |
|
3715 case LS_SHORT: |
|
3716 return H5T_NATIVE_SHORT; |
|
3717 |
|
3718 case LS_INT: |
|
3719 return H5T_NATIVE_INT; |
|
3720 |
|
3721 case LS_FLOAT: |
|
3722 return H5T_NATIVE_FLOAT; |
|
3723 |
|
3724 case LS_DOUBLE: |
|
3725 default: |
|
3726 return H5T_NATIVE_DOUBLE; |
|
3727 } |
|
3728 } |
|
3729 #endif /* HAVE_HDF5_INT2FLOAT_CONVERSIONS */ |
|
3730 |
|
3731 // Add the data from TC to the HDF5 location loc_id, which could |
|
3732 // be either a file or a group within a file. Return true if |
|
3733 // successful. This function calls itself recursively for lists |
|
3734 // (stored as HDF5 groups). |
|
3735 |
|
3736 static bool |
|
3737 add_hdf5_data (hid_t loc_id, const octave_value& tc, |
|
3738 const std::string& name, const std::string& doc, |
|
3739 bool mark_as_global, bool save_as_floats) |
|
3740 { |
|
3741 hsize_t dims[3]; |
|
3742 hid_t type_id = -1, space_id = -1, data_id = -1; |
|
3743 bool data_is_group = 0; |
|
3744 bool retval = 0; |
|
3745 |
|
3746 if (tc.is_string ()) |
|
3747 { |
|
3748 int nr = tc.rows (); |
|
3749 charMatrix chm = tc.char_matrix_value (); |
|
3750 int nc = chm.cols (); |
|
3751 |
|
3752 // create datatype for (null-terminated) string to write from: |
|
3753 type_id = H5Tcopy (H5T_C_S1); H5Tset_size (type_id, nc + 1); |
|
3754 if (type_id < 0) |
|
3755 goto error_cleanup; |
|
3756 |
|
3757 dims[0] = nr; |
3956
|
3758 space_id = H5Screate_simple (nr > 0 ? 1 : 0, dims, (hsize_t*) 0); |
3687
|
3759 if (space_id < 0) |
|
3760 goto error_cleanup; |
|
3761 |
|
3762 data_id = H5Dcreate (loc_id, name.c_str (), |
|
3763 type_id, space_id, H5P_DEFAULT); |
|
3764 if (data_id < 0) |
|
3765 goto error_cleanup; |
|
3766 |
4249
|
3767 OCTAVE_LOCAL_BUFFER (char, s, nr * (nc + 1)); |
3687
|
3768 |
|
3769 for (int i = 0; i < nr; ++i) |
|
3770 { |
|
3771 std::string tstr = chm.row_as_string (i); |
|
3772 strcpy (s + i * (nc+1), tstr.c_str ()); |
|
3773 } |
|
3774 |
|
3775 if (H5Dwrite (data_id, type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
|
3776 (void*) s) < 0) { |
|
3777 goto error_cleanup; |
|
3778 } |
|
3779 } |
|
3780 else if (tc.is_range ()) |
|
3781 { |
3956
|
3782 space_id = H5Screate_simple (0, dims, (hsize_t*) 0); |
3687
|
3783 if (space_id < 0) |
|
3784 goto error_cleanup; |
|
3785 |
|
3786 type_id = hdf5_make_range_type (H5T_NATIVE_DOUBLE); |
|
3787 if (type_id < 0) |
|
3788 goto error_cleanup; |
|
3789 |
|
3790 data_id = H5Dcreate (loc_id, name.c_str (), |
|
3791 type_id, space_id, H5P_DEFAULT); |
|
3792 if (data_id < 0) |
|
3793 goto error_cleanup; |
|
3794 |
|
3795 Range r = tc.range_value (); |
|
3796 double range_vals[3]; |
|
3797 range_vals[0] = r.base (); |
|
3798 range_vals[1] = r.limit (); |
|
3799 range_vals[2] = r.inc (); |
|
3800 |
|
3801 if (H5Dwrite (data_id, type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
|
3802 (void*) range_vals) < 0) |
|
3803 goto error_cleanup; |
|
3804 } |
|
3805 else if (tc.is_real_scalar ()) |
|
3806 { |
3956
|
3807 space_id = H5Screate_simple (0, dims, (hsize_t*) 0); |
3687
|
3808 if (space_id < 0) goto error_cleanup; |
|
3809 |
|
3810 data_id = H5Dcreate (loc_id, name.c_str (), |
|
3811 H5T_NATIVE_DOUBLE, space_id, H5P_DEFAULT); |
|
3812 if (data_id < 0) |
|
3813 goto error_cleanup; |
|
3814 |
|
3815 double tmp = tc.double_value (); |
|
3816 if (H5Dwrite (data_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, |
|
3817 H5P_DEFAULT, (void*) &tmp) < 0) |
|
3818 goto error_cleanup; |
|
3819 } |
|
3820 else if (tc.is_real_matrix ()) |
|
3821 { |
|
3822 Matrix m = tc.matrix_value (); |
|
3823 dims[1] = m.rows (); // Octave uses column-major, while |
|
3824 dims[0] = m.columns (); // HDF5 uses row-major ordering |
|
3825 |
3956
|
3826 space_id = H5Screate_simple (dims[1] > 1 ?2:1, dims, (hsize_t*) 0); |
3687
|
3827 if (space_id < 0) |
|
3828 goto error_cleanup; |
|
3829 |
|
3830 hid_t save_type_id = H5T_NATIVE_DOUBLE; |
|
3831 |
|
3832 if (save_as_floats) |
|
3833 { |
|
3834 if (m.too_large_for_float ()) |
|
3835 { |
|
3836 warning ("save: some values too large to save as floats --"); |
|
3837 warning ("save: saving as doubles instead"); |
|
3838 } |
|
3839 else |
|
3840 save_type_id = H5T_NATIVE_FLOAT; |
|
3841 } |
|
3842 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS |
|
3843 // hdf5 currently doesn't support float/integer conversions |
|
3844 else |
|
3845 { |
|
3846 double max_val, min_val; |
|
3847 |
|
3848 if (m.all_integers (max_val, min_val)) |
|
3849 save_type_id |
|
3850 = save_type_to_hdf5 (get_save_type (max_val, min_val)); |
|
3851 } |
|
3852 #endif /* HAVE_HDF5_INT2FLOAT_CONVERSIONS */ |
|
3853 |
|
3854 data_id = H5Dcreate (loc_id, name.c_str (), |
|
3855 save_type_id, space_id, H5P_DEFAULT); |
|
3856 if (data_id < 0) |
|
3857 goto error_cleanup; |
|
3858 |
|
3859 double *mtmp = m.fortran_vec (); |
|
3860 if (H5Dwrite (data_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, |
|
3861 H5P_DEFAULT, (void*) mtmp) < 0) |
|
3862 goto error_cleanup; |
|
3863 } |
|
3864 else if (tc.is_complex_scalar ()) |
|
3865 { |
3956
|
3866 space_id = H5Screate_simple (0, dims, (hsize_t*) 0); |
3687
|
3867 if (space_id < 0) |
|
3868 goto error_cleanup; |
|
3869 |
|
3870 type_id = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); |
|
3871 if (type_id < 0) |
|
3872 goto error_cleanup; |
|
3873 |
|
3874 data_id = H5Dcreate (loc_id, name.c_str (), |
|
3875 type_id, space_id, H5P_DEFAULT); |
|
3876 if (data_id < 0) |
|
3877 goto error_cleanup; |
|
3878 |
|
3879 Complex tmp = tc.complex_value (); |
|
3880 if (H5Dwrite (data_id, type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
|
3881 (void*) X_CAST (double*, &tmp)) < 0) |
|
3882 goto error_cleanup; |
|
3883 } |
|
3884 else if (tc.is_complex_matrix ()) |
|
3885 { |
|
3886 ComplexMatrix m = tc.complex_matrix_value (); |
|
3887 |
|
3888 dims[1] = m.rows (); // Octave uses column-major, while |
|
3889 dims[0] = m.columns (); // HDF5 uses row-major ordering |
|
3890 |
3956
|
3891 space_id = H5Screate_simple (dims[1] > 1 ?2:1, dims, (hsize_t*) 0); |
3687
|
3892 if (space_id < 0) |
|
3893 goto error_cleanup; |
|
3894 |
|
3895 hid_t save_type_id = H5T_NATIVE_DOUBLE; |
|
3896 |
|
3897 if (save_as_floats) |
|
3898 { |
|
3899 if (m.too_large_for_float ()) |
|
3900 { |
|
3901 warning ("save: some values too large to save as floats --"); |
|
3902 warning ("save: saving as doubles instead"); |
|
3903 } |
|
3904 else |
|
3905 save_type_id = H5T_NATIVE_FLOAT; |
|
3906 } |
|
3907 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS |
|
3908 // hdf5 currently doesn't support float/integer conversions |
|
3909 else |
|
3910 { |
|
3911 double max_val, min_val; |
|
3912 |
|
3913 if (m.all_integers (max_val, min_val)) |
|
3914 save_type_id |
|
3915 = save_type_to_hdf5 (get_save_type (max_val, min_val)); |
|
3916 } |
|
3917 #endif /* HAVE_HDF5_INT2FLOAT_CONVERSIONS */ |
|
3918 |
|
3919 type_id = hdf5_make_complex_type (save_type_id); |
|
3920 if (type_id < 0) goto error_cleanup; |
|
3921 |
|
3922 data_id = H5Dcreate (loc_id, name.c_str (), |
|
3923 type_id, space_id, H5P_DEFAULT); |
|
3924 if (data_id < 0) |
|
3925 goto error_cleanup; |
|
3926 |
|
3927 hid_t complex_type_id = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); |
|
3928 if (complex_type_id < 0) |
|
3929 goto error_cleanup; |
|
3930 |
|
3931 Complex *mtmp = m.fortran_vec (); |
|
3932 if (H5Dwrite (data_id, complex_type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
|
3933 (void*) X_CAST (double *, mtmp)) < 0) |
|
3934 { |
|
3935 H5Tclose (complex_type_id); |
|
3936 goto error_cleanup; |
|
3937 } |
|
3938 |
|
3939 H5Tclose (complex_type_id); |
|
3940 } |
|
3941 else if (tc.is_list ()) |
|
3942 { |
|
3943 data_id = H5Gcreate (loc_id, name.c_str (), 0); |
|
3944 if (data_id < 0) |
|
3945 goto error_cleanup; |
|
3946 |
|
3947 data_is_group = 1; |
|
3948 |
|
3949 // recursively add each element of the list to this group |
|
3950 octave_value_list lst = tc.list_value (); |
|
3951 |
|
3952 for (int i = 0; i < lst.length (); ++i) |
|
3953 { |
|
3954 // should we use lst.name_tags () to label the elements? |
|
3955 char s[20]; |
|
3956 sprintf (s, "%d", i); |
|
3957 bool retval2 = add_hdf5_data (data_id, lst (i), s, "", |
|
3958 false, save_as_floats); |
|
3959 if (! retval2) |
|
3960 goto error_cleanup; |
|
3961 } |
|
3962 |
|
3963 // mark with an attribute "OCTAVE_LIST" with value 1 |
|
3964 // to distinguish from structures (also stored as HDF5 groups): |
3688
|
3965 if (hdf5_add_attr (data_id, "OCTAVE_LIST") < 0) |
3687
|
3966 goto error_cleanup; |
|
3967 } |
|
3968 else if (tc.is_map ()) |
|
3969 { |
|
3970 // an Octave structure |
|
3971 data_id = H5Gcreate (loc_id, name.c_str (), 0); |
|
3972 if (data_id < 0) |
|
3973 goto error_cleanup; |
|
3974 |
|
3975 data_is_group = 1; |
|
3976 |
|
3977 // recursively add each element of the structure to this group |
|
3978 Octave_map m = tc.map_value (); |
4219
|
3979 Octave_map::iterator i = m.begin (); |
|
3980 while (i != m.end ()) |
3687
|
3981 { |
4233
|
3982 // XXX FIXME XXX -- if the length of the structure array is |
|
3983 // 1, should we really create a list object? |
|
3984 bool retval2 = add_hdf5_data (data_id, octave_value (m.contents (i)), |
|
3985 m.key (i), "", false, save_as_floats); |
3687
|
3986 if (! retval2) |
|
3987 goto error_cleanup; |
|
3988 |
4219
|
3989 i++; |
3687
|
3990 } |
|
3991 } |
|
3992 else |
|
3993 { |
|
3994 gripe_wrong_type_arg ("save", tc, false); |
|
3995 goto error_cleanup; |
|
3996 } |
|
3997 |
|
3998 // attach doc string as comment: |
|
3999 if (doc.length () > 0 |
|
4000 && H5Gset_comment (loc_id, name.c_str (), doc.c_str ()) < 0) |
|
4001 goto error_cleanup; |
|
4002 |
|
4003 retval = 1; |
|
4004 |
|
4005 // if it's global, add an attribute "OCTAVE_GLOBAL" with value 1 |
|
4006 if (mark_as_global) |
3688
|
4007 retval = hdf5_add_attr (data_id, "OCTAVE_GLOBAL") >= 0; |
3687
|
4008 |
|
4009 error_cleanup: |
|
4010 |
|
4011 if (! retval) |
|
4012 error ("save: error while writing `%s' to hdf5 file", name.c_str ()); |
|
4013 |
|
4014 if (data_id >= 0) |
|
4015 { |
|
4016 if (data_is_group) |
|
4017 H5Gclose (data_id); |
|
4018 else |
|
4019 H5Dclose (data_id); |
|
4020 } |
|
4021 |
|
4022 if (space_id >= 0) |
|
4023 H5Sclose (space_id); |
|
4024 |
|
4025 if (type_id >= 0) |
|
4026 H5Tclose (type_id); |
|
4027 |
|
4028 return retval; |
|
4029 } |
|
4030 |
|
4031 // Write data from TC in HDF5 (binary) format to the stream OS, |
|
4032 // which must be an hdf5_ofstream, returning true on success. |
|
4033 |
|
4034 static bool |
|
4035 save_hdf5_data (std::ostream& os, const octave_value& tc, |
|
4036 const std::string& name, const std::string& doc, |
|
4037 bool mark_as_global, bool save_as_floats) |
|
4038 { |
|
4039 hdf5_ofstream& hs = (hdf5_ofstream&) os; |
|
4040 |
|
4041 return add_hdf5_data (hs.file_id, tc, name, doc, |
|
4042 mark_as_global, save_as_floats); |
|
4043 } |
|
4044 |
|
4045 #endif /* HAVE_HDF5 */ |
|
4046 |
3688
|
4047 static int |
|
4048 write_mat5_tag (std::ostream& is, int type, int bytes) |
|
4049 { |
|
4050 FOUR_BYTE_INT temp; |
|
4051 |
|
4052 if (bytes <= 4) |
|
4053 temp = (bytes << 16) + type; |
|
4054 else |
|
4055 { |
|
4056 temp = type; |
|
4057 if (! is.write ((char *)&temp, 4)) |
|
4058 goto data_write_error; |
|
4059 temp = bytes; |
|
4060 } |
|
4061 |
|
4062 if (! is.write ((char *)&temp, 4)) |
|
4063 goto data_write_error; |
|
4064 |
|
4065 return 0; |
|
4066 |
|
4067 data_write_error: |
|
4068 return 1; |
|
4069 } |
|
4070 |
|
4071 // write out the numeric values in M to OS, |
|
4072 // preceded by the appropriate tag. |
|
4073 static void |
|
4074 write_mat5_array (std::ostream& os, Matrix& m, const int save_as_floats) |
|
4075 { |
|
4076 int nr = m.rows (); |
|
4077 int nc = m.columns (); |
|
4078 double max_val, min_val; |
|
4079 save_type st = LS_DOUBLE; |
|
4080 mat5_data_type mst; |
|
4081 int size; |
|
4082 unsigned len; |
|
4083 double *data = m.fortran_vec (); |
|
4084 |
|
4085 // Have to use copy here to avoid writing over data accessed via |
|
4086 // Matrix::data(). |
|
4087 |
|
4088 #define MAT5_DO_WRITE(TYPE, data, count, stream) \ |
|
4089 do \ |
|
4090 { \ |
4249
|
4091 OCTAVE_LOCAL_BUFFER (TYPE, ptr, count); \ |
3688
|
4092 for (int i = 0; i < count; i++) \ |
|
4093 ptr[i] = X_CAST (TYPE, data[i]); \ |
|
4094 stream.write (X_CAST (char *, ptr), count * sizeof (TYPE)); \ |
|
4095 } \ |
|
4096 while (0) |
|
4097 |
|
4098 if (save_as_floats) |
|
4099 { |
|
4100 if (m.too_large_for_float ()) |
|
4101 { |
|
4102 warning ("save: some values too large to save as floats --"); |
|
4103 warning ("save: saving as doubles instead"); |
|
4104 } |
|
4105 else |
|
4106 st = LS_FLOAT; |
|
4107 } |
|
4108 |
|
4109 if (m.all_integers (max_val, min_val)) |
|
4110 st = get_save_type (max_val, min_val); |
|
4111 |
|
4112 switch (st) |
|
4113 { |
|
4114 default: |
|
4115 case LS_DOUBLE: mst = miDOUBLE; size = 8; break; |
|
4116 case LS_FLOAT: mst = miSINGLE; size = 4; break; |
|
4117 case LS_U_CHAR: mst = miUINT8; size = 1; break; |
|
4118 case LS_U_SHORT: mst = miUINT16; size = 2; break; |
|
4119 case LS_U_INT: mst = miUINT32; size = 4; break; |
|
4120 case LS_CHAR: mst = miINT8; size = 1; break; |
|
4121 case LS_SHORT: mst = miINT16; size = 2; break; |
|
4122 case LS_INT: mst = miINT32; size = 4; break; |
|
4123 } |
|
4124 |
|
4125 len = nr*nc*size; |
|
4126 write_mat5_tag (os, mst, len); |
|
4127 |
|
4128 { |
|
4129 switch (st) |
|
4130 { |
|
4131 case LS_U_CHAR: |
|
4132 MAT5_DO_WRITE (unsigned char, data, nr*nc, os); |
|
4133 break; |
|
4134 |
|
4135 case LS_U_SHORT: |
|
4136 MAT5_DO_WRITE (unsigned TWO_BYTE_INT, data, nr*nc, os); |
|
4137 break; |
|
4138 |
|
4139 case LS_U_INT: |
|
4140 MAT5_DO_WRITE (unsigned FOUR_BYTE_INT, data, nr*nc, os); |
|
4141 break; |
|
4142 |
|
4143 // provide for 64 bit ints, even though get_save_type does |
|
4144 // not yet implement them |
|
4145 #ifdef EIGHT_BYTE_INT |
|
4146 case LS_U_LONG: |
|
4147 MAT5_DO_WRITE (unsigned EIGHT_BYTE_INT, data, nr*nc, os); |
|
4148 break; |
|
4149 #endif |
|
4150 |
|
4151 case LS_CHAR: |
|
4152 MAT5_DO_WRITE (signed char, data, nr*nc, os); |
|
4153 break; |
|
4154 |
|
4155 case LS_SHORT: |
|
4156 MAT5_DO_WRITE (TWO_BYTE_INT, data, nr*nc, os); |
|
4157 break; |
|
4158 |
|
4159 case LS_INT: |
|
4160 MAT5_DO_WRITE (FOUR_BYTE_INT, data, nr*nc, os); |
|
4161 break; |
|
4162 |
|
4163 #ifdef EIGHT_BYTE_INT |
|
4164 case LS_LONG: |
|
4165 MAT5_DO_WRITE (EIGHT_BYTE_INT, data, nr*nc, os); |
|
4166 break; |
|
4167 #endif |
|
4168 |
|
4169 case LS_FLOAT: |
|
4170 MAT5_DO_WRITE (float, data, nr*nc, os); |
|
4171 break; |
|
4172 |
|
4173 case LS_DOUBLE: // No conversion necessary. |
|
4174 os.write (X_CAST (char *, data), len); |
|
4175 break; |
|
4176 |
|
4177 default: |
|
4178 (*current_liboctave_error_handler) |
|
4179 ("unrecognized data format requested"); |
|
4180 break; |
|
4181 } |
|
4182 } |
|
4183 if (PAD (len) > len) |
|
4184 { |
|
4185 static char buf[9]="\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
4186 os.write (buf, PAD (len) - len); |
|
4187 } |
|
4188 } |
|
4189 |
4332
|
4190 // Write out cell element values in the cell array to OS, preceded by |
|
4191 // the appropriate tag. |
|
4192 |
|
4193 static bool |
|
4194 write_mat5_cell_array (std::ostream& os, Cell& cell, bool mark_as_global, |
|
4195 const int save_as_floats) |
|
4196 { |
|
4197 int nr = cell.rows (); |
|
4198 int nc = cell.columns (); |
|
4199 |
|
4200 for (int j = 0; j < nc; j++) |
|
4201 { |
|
4202 for (int i = 0; i < nr; i++) |
|
4203 { |
|
4204 octave_value ov = cell.elem (i, j); |
|
4205 |
|
4206 if (! save_mat5_binary_element (os, ov, "", mark_as_global, |
|
4207 save_as_floats)) |
|
4208 return false; |
|
4209 } |
|
4210 } |
|
4211 |
|
4212 return true; |
|
4213 } |
|
4214 |
3688
|
4215 // save the data from TC along with the corresponding NAME on stream |
|
4216 // OS in the MatLab version 5 binary format. Return true on success. |
|
4217 |
|
4218 static bool |
|
4219 save_mat5_binary_element (std::ostream& os, |
|
4220 const octave_value& tc, const std::string& name, |
|
4221 bool mark_as_global, bool save_as_floats) |
|
4222 { |
|
4223 FOUR_BYTE_INT flags=0; |
|
4224 FOUR_BYTE_INT junk=0; |
|
4225 FOUR_BYTE_INT nr; |
|
4226 FOUR_BYTE_INT nc; |
3769
|
4227 std::streampos fixup, contin; |
3688
|
4228 |
|
4229 // element type and length |
|
4230 fixup = os.tellp (); |
|
4231 write_mat5_tag (os, miMATRIX, 99); // we don't know the real length yet |
|
4232 |
|
4233 // array flags subelement |
|
4234 write_mat5_tag (os, miUINT32, 8); |
|
4235 |
|
4236 if (mark_as_global) |
|
4237 flags |= 0x0400; |
|
4238 |
|
4239 if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
4240 flags |= 0x0800; |
|
4241 |
|
4242 if (tc.is_string ()) |
|
4243 flags |= mxCHAR_CLASS; |
|
4244 else if (tc.is_real_scalar ()) |
|
4245 flags |= mxDOUBLE_CLASS; |
3866
|
4246 else if (tc.is_real_matrix () || tc.is_range ()) |
3688
|
4247 flags |= mxDOUBLE_CLASS; |
|
4248 else if (tc.is_complex_scalar ()) |
|
4249 flags |= mxDOUBLE_CLASS; |
|
4250 else if (tc.is_complex_matrix ()) |
|
4251 flags |= mxDOUBLE_CLASS; |
|
4252 else if (tc.is_map ()) |
|
4253 flags |= mxSTRUCT_CLASS; |
4332
|
4254 else if (tc.is_cell ()) |
|
4255 flags |= mxCELL_CLASS; |
3688
|
4256 else |
|
4257 { |
|
4258 gripe_wrong_type_arg ("save", tc, false); |
|
4259 goto error_cleanup; |
|
4260 } |
|
4261 |
|
4262 os.write ((char *)&flags, 4); |
|
4263 os.write ((char *)&junk, 4); |
|
4264 |
|
4265 // dimensions array subelement |
4362
|
4266 nr = tc.rows (); |
|
4267 nc = tc.columns (); |
3866
|
4268 |
|
4269 write_mat5_tag (os, miINT32, 8); |
|
4270 os.write ((char *)&nr, 4); |
|
4271 os.write ((char *)&nc, 4); |
3688
|
4272 |
|
4273 // array name subelement |
|
4274 { |
|
4275 int namelen = name.length (); |
|
4276 |
|
4277 if (namelen > 31) |
|
4278 namelen = 31; // only 31 char names permitted in mat file |
|
4279 |
|
4280 int paddedlength = PAD (namelen); |
|
4281 |
|
4282 write_mat5_tag (os, miINT8, namelen); |
4249
|
4283 OCTAVE_LOCAL_BUFFER (char, paddedname, paddedlength); |
3688
|
4284 memset (paddedname, 0, paddedlength); |
|
4285 strncpy (paddedname, name.c_str (), namelen); |
|
4286 os.write (paddedname, paddedlength); |
|
4287 } |
|
4288 |
|
4289 // data element |
|
4290 if (tc.is_string ()) |
|
4291 { |
|
4292 charMatrix chm = tc.char_matrix_value (); |
|
4293 int nc = chm.cols (); |
|
4294 int len = nr*nc*2; |
|
4295 int paddedlength = PAD (nr*nc*2); |
|
4296 |
4249
|
4297 OCTAVE_LOCAL_BUFFER (TWO_BYTE_INT, buf, nc*nr+3); |
3688
|
4298 write_mat5_tag (os, miUINT16, len); |
|
4299 |
|
4300 for (int i = 0; i < nr; i++) |
|
4301 { |
|
4302 std::string tstr = chm.row_as_string (i); |
|
4303 const char *s = tstr.data (); |
|
4304 |
|
4305 for (int j = 0; j < nc; j++) |
3895
|
4306 buf[j*nr+i] = *s++; |
3688
|
4307 } |
3895
|
4308 os.write ((char *)buf, nr*nc*2); |
3688
|
4309 |
|
4310 if (paddedlength > len) |
|
4311 os.write ((char *)buf, paddedlength - len); |
|
4312 } |
3866
|
4313 else if (tc.is_real_scalar () || tc.is_real_matrix () || tc.is_range ()) |
3688
|
4314 { |
|
4315 Matrix m = tc.matrix_value (); |
|
4316 |
|
4317 write_mat5_array (os, m, save_as_floats); |
|
4318 } |
4332
|
4319 else if (tc.is_cell ()) |
|
4320 { |
|
4321 Cell cell = tc.cell_value (); |
|
4322 |
|
4323 if (! write_mat5_cell_array (os, cell, mark_as_global, save_as_floats)) |
|
4324 goto error_cleanup; |
|
4325 } |
3688
|
4326 else if (tc.is_complex_scalar () || tc.is_complex_matrix ()) |
|
4327 { |
|
4328 ComplexMatrix m_cmplx = tc.complex_matrix_value (); |
|
4329 Matrix m = ::real (m_cmplx); |
|
4330 |
|
4331 for (int part=0; part < 2; part++) |
|
4332 { |
|
4333 // real part, then complex part |
|
4334 write_mat5_array (os, m, save_as_floats); |
|
4335 m = ::imag (m_cmplx); |
|
4336 } |
|
4337 } |
|
4338 else if (tc.is_map ()) |
|
4339 { |
|
4340 // an Octave structure */ |
|
4341 // recursively write each element of the structure |
|
4342 Octave_map m = tc.map_value (); |
|
4343 |
|
4344 { |
|
4345 char buf[32]; |
|
4346 FOUR_BYTE_INT maxfieldnamelength = 32; |
|
4347 int fieldcnt = 0; |
|
4348 |
4219
|
4349 for (Octave_map::iterator i = m.begin (); i != m.end (); i++) |
3688
|
4350 fieldcnt++; |
|
4351 |
|
4352 write_mat5_tag (os, miINT32, 4); |
|
4353 os.write ((char *)&maxfieldnamelength, 4); |
|
4354 write_mat5_tag (os, miINT8, fieldcnt*32); |
4219
|
4355 |
|
4356 for (Octave_map::iterator i = m.begin (); i != m.end (); i++) |
3688
|
4357 { |
|
4358 // write the name of each element |
3769
|
4359 std::string tstr = m.key (i); |
3688
|
4360 memset (buf, 0, 32); |
|
4361 strncpy (buf, tstr.c_str (), 31); // only 31 char names permitted |
|
4362 os.write (buf, 32); |
|
4363 } |
|
4364 |
4362
|
4365 int len = m.array_length (); |
|
4366 |
4219
|
4367 for (Octave_map::iterator i = m.begin (); i != m.end (); i++) |
3688
|
4368 { |
|
4369 // write the data of each element |
4362
|
4370 octave_value_list elts = m.contents (i); |
|
4371 |
|
4372 for (int j = 0; j < len; j++) |
|
4373 { |
|
4374 bool retval2 = save_mat5_binary_element (os, elts(j), "", |
|
4375 mark_as_global, |
|
4376 save_as_floats); |
|
4377 if (! retval2) |
|
4378 goto error_cleanup; |
|
4379 } |
3688
|
4380 } |
|
4381 } |
|
4382 } |
|
4383 else |
|
4384 gripe_wrong_type_arg ("save", tc, false); |
|
4385 |
|
4386 contin = os.tellp (); |
|
4387 os.seekp (fixup); |
3841
|
4388 write_mat5_tag (os, miMATRIX, |
|
4389 static_cast<int>(contin - fixup) - 8); // the actual length |
3688
|
4390 os.seekp (contin); |
|
4391 |
|
4392 return true; |
|
4393 |
|
4394 error_cleanup: |
|
4395 error ("save: error while writing `%s' to MAT file", name.c_str ()); |
|
4396 |
|
4397 return false; |
|
4398 } |
|
4399 |
667
|
4400 // Save the data from TC along with the corresponding NAME on stream OS |
3688
|
4401 // in the MatLab version 4 binary format. |
667
|
4402 |
2799
|
4403 static bool |
3523
|
4404 save_mat_binary_data (std::ostream& os, const octave_value& tc, |
|
4405 const std::string& name) |
667
|
4406 { |
|
4407 FOUR_BYTE_INT mopt = 0; |
|
4408 |
|
4409 mopt += tc.is_string () ? 1 : 0; |
2318
|
4410 |
|
4411 oct_mach_info::float_format flt_fmt = |
|
4412 oct_mach_info::native_float_format ();; |
|
4413 |
|
4414 mopt += 1000 * float_format_to_mopt_digit (flt_fmt); |
667
|
4415 |
3557
|
4416 os.write (X_CAST (char *, &mopt), 4); |
667
|
4417 |
|
4418 FOUR_BYTE_INT nr = tc.rows (); |
3557
|
4419 os.write (X_CAST (char *, &nr), 4); |
667
|
4420 |
|
4421 FOUR_BYTE_INT nc = tc.columns (); |
3557
|
4422 os.write (X_CAST (char *, &nc), 4); |
667
|
4423 |
|
4424 int len = nr * nc; |
|
4425 |
|
4426 FOUR_BYTE_INT imag = tc.is_complex_type () ? 1 : 0; |
3557
|
4427 os.write (X_CAST (char *, &imag), 4); |
667
|
4428 |
2345
|
4429 // LEN includes the terminating character, and the file is also |
|
4430 // supposed to include it. |
|
4431 |
|
4432 FOUR_BYTE_INT name_len = name.length () + 1; |
667
|
4433 |
3557
|
4434 os.write (X_CAST (char *, &name_len), 4); |
2345
|
4435 os << name << '\0'; |
667
|
4436 |
3233
|
4437 if (tc.is_string ()) |
|
4438 { |
|
4439 unwind_protect::begin_frame ("save_mat_binary_data"); |
|
4440 unwind_protect_int (Vimplicit_str_to_num_ok); |
|
4441 Vimplicit_str_to_num_ok = true; |
|
4442 Matrix m = tc.matrix_value (); |
3557
|
4443 os.write (X_CAST (char *, m.data ()), 8 * len); |
3233
|
4444 unwind_protect::run_frame ("save_mat_binary_data"); |
|
4445 } |
|
4446 else if (tc.is_range ()) |
|
4447 { |
|
4448 Range r = tc.range_value (); |
|
4449 double base = r.base (); |
|
4450 double inc = r.inc (); |
|
4451 int nel = r.nelem (); |
|
4452 for (int i = 0; i < nel; i++) |
|
4453 { |
|
4454 double x = base + i * inc; |
3557
|
4455 os.write (X_CAST (char *, &x), 8); |
3233
|
4456 } |
|
4457 } |
|
4458 else if (tc.is_real_scalar ()) |
667
|
4459 { |
|
4460 double tmp = tc.double_value (); |
3557
|
4461 os.write (X_CAST (char *, &tmp), 8); |
667
|
4462 } |
911
|
4463 else if (tc.is_real_matrix ()) |
667
|
4464 { |
|
4465 Matrix m = tc.matrix_value (); |
3557
|
4466 os.write (X_CAST (char *, m.data ()), 8 * len); |
667
|
4467 } |
|
4468 else if (tc.is_complex_scalar ()) |
|
4469 { |
|
4470 Complex tmp = tc.complex_value (); |
3557
|
4471 os.write (X_CAST (char *, &tmp), 16); |
667
|
4472 } |
|
4473 else if (tc.is_complex_matrix ()) |
|
4474 { |
|
4475 ComplexMatrix m_cmplx = tc.complex_matrix_value (); |
3688
|
4476 Matrix m = ::real (m_cmplx); |
3557
|
4477 os.write (X_CAST (char *, m.data ()), 8 * len); |
3688
|
4478 m = ::imag (m_cmplx); |
3557
|
4479 os.write (X_CAST (char *, m.data ()), 8 * len); |
667
|
4480 } |
|
4481 else |
2799
|
4482 gripe_wrong_type_arg ("save", tc, false); |
|
4483 |
|
4484 return os; |
667
|
4485 } |
|
4486 |
620
|
4487 static void |
3523
|
4488 ascii_save_type (std::ostream& os, const char *type, bool mark_as_global) |
620
|
4489 { |
|
4490 if (mark_as_global) |
4060
|
4491 os << "# type: global "; |
620
|
4492 else |
4060
|
4493 os << "# type: "; |
620
|
4494 |
|
4495 os << type << "\n"; |
|
4496 } |
|
4497 |
872
|
4498 static Matrix |
|
4499 strip_infnan (const Matrix& m) |
|
4500 { |
|
4501 int nr = m.rows (); |
|
4502 int nc = m.columns (); |
|
4503 |
|
4504 Matrix retval (nr, nc); |
|
4505 |
|
4506 int k = 0; |
|
4507 for (int i = 0; i < nr; i++) |
|
4508 { |
|
4509 for (int j = 0; j < nc; j++) |
|
4510 { |
2305
|
4511 double d = m (i, j); |
872
|
4512 if (xisnan (d)) |
|
4513 goto next_row; |
|
4514 else |
2305
|
4515 retval (k, j) = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
872
|
4516 } |
|
4517 k++; |
|
4518 |
|
4519 next_row: |
|
4520 continue; |
|
4521 } |
|
4522 |
|
4523 if (k > 0) |
|
4524 retval.resize (k, nc); |
|
4525 |
|
4526 return retval; |
|
4527 } |
|
4528 |
|
4529 static ComplexMatrix |
|
4530 strip_infnan (const ComplexMatrix& m) |
|
4531 { |
|
4532 int nr = m.rows (); |
|
4533 int nc = m.columns (); |
|
4534 |
|
4535 ComplexMatrix retval (nr, nc); |
|
4536 |
|
4537 int k = 0; |
|
4538 for (int i = 0; i < nr; i++) |
|
4539 { |
|
4540 for (int j = 0; j < nc; j++) |
|
4541 { |
2305
|
4542 Complex c = m (i, j); |
872
|
4543 if (xisnan (c)) |
|
4544 goto next_row; |
|
4545 else |
|
4546 { |
|
4547 double re = real (c); |
|
4548 double im = imag (c); |
|
4549 |
|
4550 re = xisinf (re) ? (re > 0 ? OCT_RBV : -OCT_RBV) : re; |
|
4551 im = xisinf (im) ? (im > 0 ? OCT_RBV : -OCT_RBV) : im; |
|
4552 |
2305
|
4553 retval (k, j) = Complex (re, im); |
872
|
4554 } |
|
4555 } |
|
4556 k++; |
|
4557 |
|
4558 next_row: |
|
4559 continue; |
|
4560 } |
|
4561 |
|
4562 if (k > 0) |
|
4563 retval.resize (k, nc); |
|
4564 |
|
4565 return retval; |
|
4566 } |
|
4567 |
620
|
4568 // Save the data from TC along with the corresponding NAME, and global |
604
|
4569 // flag MARK_AS_GLOBAL on stream OS in the plain text format described |
1755
|
4570 // above for load_ascii_data. If NAME is empty, the name: line is not |
604
|
4571 // generated. PRECISION specifies the number of decimal digits to print. |
3019
|
4572 // If STRIP_NAN_AND_INF is TRUE, rows containing NaNs are deleted, |
872
|
4573 // and Infinite values are converted to +/-OCT_RBV (A Real Big Value, |
|
4574 // but not so big that gnuplot can't handle it when trying to compute |
|
4575 // axis ranges, etc.). |
|
4576 // |
|
4577 // Assumes ranges and strings cannot contain Inf or NaN values. |
|
4578 // |
|
4579 // Returns 1 for success and 0 for failure. |
604
|
4580 |
|
4581 // XXX FIXME XXX -- should probably write the help string here too. |
|
4582 |
3738
|
4583 static bool |
3523
|
4584 save_ascii_data (std::ostream& os, const octave_value& tc, |
3738
|
4585 const std::string& name, bool& infnan_warned, |
|
4586 bool strip_nan_and_inf, bool mark_as_global, |
|
4587 int precision) |
604
|
4588 { |
2799
|
4589 bool success = true; |
620
|
4590 |
604
|
4591 if (! precision) |
2194
|
4592 precision = Vsave_precision; |
604
|
4593 |
1755
|
4594 if (! name.empty ()) |
4060
|
4595 os << "# name: " << name << "\n"; |
604
|
4596 |
|
4597 long old_precision = os.precision (); |
|
4598 os.precision (precision); |
|
4599 |
3233
|
4600 if (tc.is_string ()) |
|
4601 { |
|
4602 ascii_save_type (os, "string array", mark_as_global); |
|
4603 charMatrix chm = tc.char_matrix_value (); |
|
4604 int elements = chm.rows (); |
4060
|
4605 os << "# elements: " << elements << "\n"; |
3233
|
4606 for (int i = 0; i < elements; i++) |
|
4607 { |
3841
|
4608 unsigned len = chm.cols (); |
4060
|
4609 os << "# length: " << len << "\n"; |
3836
|
4610 std::string tstr = chm.row_as_string (i, false, true); |
3233
|
4611 const char *tmp = tstr.data (); |
3836
|
4612 if (tstr.length () > len) |
|
4613 panic_impossible (); |
3557
|
4614 os.write (X_CAST (char *, tmp), len); |
3233
|
4615 os << "\n"; |
|
4616 } |
|
4617 } |
|
4618 else if (tc.is_range ()) |
|
4619 { |
|
4620 ascii_save_type (os, "range", mark_as_global); |
|
4621 Range tmp = tc.range_value (); |
4130
|
4622 os << "# base, limit, increment\n"; |
|
4623 octave_write_double (os, tmp.base ()); |
|
4624 os << " "; |
|
4625 octave_write_double (os, tmp.limit ()); |
|
4626 os << " "; |
|
4627 octave_write_double (os, tmp.inc ()); |
|
4628 os << "\n"; |
3233
|
4629 } |
|
4630 else if (tc.is_real_scalar ()) |
620
|
4631 { |
|
4632 ascii_save_type (os, "scalar", mark_as_global); |
872
|
4633 |
|
4634 double d = tc.double_value (); |
3738
|
4635 |
872
|
4636 if (strip_nan_and_inf) |
|
4637 { |
|
4638 if (xisnan (d)) |
|
4639 { |
|
4640 error ("only value to plot is NaN"); |
2799
|
4641 success = false; |
872
|
4642 } |
|
4643 else |
|
4644 { |
|
4645 d = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
4130
|
4646 octave_write_double (os, d); |
|
4647 os << "\n"; |
872
|
4648 } |
|
4649 } |
|
4650 else |
3738
|
4651 { |
|
4652 if (! infnan_warned && (xisnan (d) || xisinf (d))) |
|
4653 { |
|
4654 warning ("save: Inf or NaN values may not be reloadable"); |
|
4655 infnan_warned = true; |
|
4656 } |
|
4657 |
4130
|
4658 octave_write_double (os, d); |
|
4659 os << "\n"; |
3738
|
4660 } |
620
|
4661 } |
|
4662 else if (tc.is_real_matrix ()) |
|
4663 { |
|
4664 ascii_save_type (os, "matrix", mark_as_global); |
3738
|
4665 |
4060
|
4666 os << "# rows: " << tc.rows () << "\n" |
|
4667 << "# columns: " << tc.columns () << "\n"; |
872
|
4668 |
|
4669 Matrix tmp = tc.matrix_value (); |
3738
|
4670 |
872
|
4671 if (strip_nan_and_inf) |
|
4672 tmp = strip_infnan (tmp); |
3738
|
4673 else if (! infnan_warned && tmp.any_element_is_inf_or_nan ()) |
|
4674 { |
|
4675 warning ("save: Inf or NaN values may not be reloadable"); |
|
4676 infnan_warned = true; |
|
4677 } |
872
|
4678 |
|
4679 os << tmp; |
620
|
4680 } |
4332
|
4681 else if (tc.is_cell ()) |
|
4682 { |
|
4683 ascii_save_type (os, "cell", mark_as_global); |
|
4684 |
|
4685 os << "# rows: " << tc.rows () << "\n" |
|
4686 << "# columns: " << tc.columns () << "\n"; |
|
4687 |
|
4688 Cell tmp = tc.cell_value (); |
|
4689 |
|
4690 for (int j = 0; j < tmp.cols (); j++) |
|
4691 { |
|
4692 for (int i = 0; i < tmp.rows (); i++) |
|
4693 { |
|
4694 octave_value o_val = tmp.elem (i, j); |
|
4695 |
|
4696 // Recurse to print sub-value. |
|
4697 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, |
|
4698 infnan_warned, strip_nan_and_inf, |
|
4699 mark_as_global, 0); |
|
4700 |
|
4701 if (! b) |
|
4702 return os; |
|
4703 } |
|
4704 |
|
4705 os << "\n"; |
|
4706 } |
|
4707 } |
620
|
4708 else if (tc.is_complex_scalar ()) |
|
4709 { |
|
4710 ascii_save_type (os, "complex scalar", mark_as_global); |
872
|
4711 |
|
4712 Complex c = tc.complex_value (); |
3738
|
4713 |
872
|
4714 if (strip_nan_and_inf) |
|
4715 { |
|
4716 if (xisnan (c)) |
|
4717 { |
|
4718 error ("only value to plot is NaN"); |
2799
|
4719 success = false; |
872
|
4720 } |
|
4721 else |
|
4722 { |
|
4723 double re = real (c); |
|
4724 double im = imag (c); |
|
4725 |
|
4726 re = xisinf (re) ? (re > 0 ? OCT_RBV : -OCT_RBV) : re; |
|
4727 im = xisinf (im) ? (im > 0 ? OCT_RBV : -OCT_RBV) : im; |
|
4728 |
|
4729 c = Complex (re, im); |
|
4730 |
4130
|
4731 octave_write_complex (os, c); |
|
4732 os << "\n"; |
872
|
4733 } |
|
4734 } |
|
4735 else |
3738
|
4736 { |
|
4737 if (! infnan_warned && (xisnan (c) || xisinf (c))) |
|
4738 { |
|
4739 warning ("save: Inf or NaN values may not be reloadable"); |
|
4740 infnan_warned = true; |
|
4741 } |
|
4742 |
4130
|
4743 octave_write_complex (os, c); |
|
4744 os << "\n"; |
3738
|
4745 } |
620
|
4746 } |
|
4747 else if (tc.is_complex_matrix ()) |
604
|
4748 { |
620
|
4749 ascii_save_type (os, "complex matrix", mark_as_global); |
3738
|
4750 |
4060
|
4751 os << "# rows: " << tc.rows () << "\n" |
|
4752 << "# columns: " << tc.columns () << "\n"; |
875
|
4753 |
|
4754 ComplexMatrix tmp = tc.complex_matrix_value (); |
3738
|
4755 |
872
|
4756 if (strip_nan_and_inf) |
|
4757 tmp = strip_infnan (tmp); |
3738
|
4758 else if (! infnan_warned && tmp.any_element_is_inf_or_nan ()) |
|
4759 { |
|
4760 warning ("save: Inf or NaN values may not be reloadable"); |
|
4761 infnan_warned = true; |
|
4762 } |
872
|
4763 |
|
4764 os << tmp; |
620
|
4765 } |
|
4766 else |
2799
|
4767 gripe_wrong_type_arg ("save", tc, false); |
604
|
4768 |
|
4769 os.precision (old_precision); |
|
4770 |
872
|
4771 return (os && success); |
604
|
4772 } |
|
4773 |
3738
|
4774 bool |
|
4775 save_ascii_data_for_plotting (std::ostream& os, const octave_value& t, |
|
4776 const std::string& name) |
|
4777 { |
|
4778 bool infnan_warned = true; |
|
4779 |
3769
|
4780 return save_ascii_data (os, t, name, infnan_warned, true, false, 0); |
3738
|
4781 } |
|
4782 |
604
|
4783 // Save the info from sr on stream os in the format specified by fmt. |
|
4784 |
4329
|
4785 void |
3523
|
4786 do_save (std::ostream& os, symbol_record *sr, load_save_format fmt, |
3738
|
4787 int save_as_floats, bool& infnan_warned) |
604
|
4788 { |
|
4789 if (! sr->is_variable ()) |
|
4790 { |
|
4791 error ("save: can only save variables, not functions"); |
|
4792 return; |
|
4793 } |
|
4794 |
3523
|
4795 std::string name = sr->name (); |
|
4796 std::string help = sr->help (); |
604
|
4797 int global = sr->is_linked_to_global (); |
2970
|
4798 |
|
4799 octave_value tc = sr->def (); |
604
|
4800 |
1755
|
4801 if (tc.is_undefined ()) |
604
|
4802 return; |
|
4803 |
|
4804 switch (fmt) |
|
4805 { |
|
4806 case LS_ASCII: |
3738
|
4807 save_ascii_data (os, tc, name, infnan_warned, false, global, 0); |
604
|
4808 break; |
|
4809 |
|
4810 case LS_BINARY: |
630
|
4811 save_binary_data (os, tc, name, help, global, save_as_floats); |
604
|
4812 break; |
|
4813 |
667
|
4814 case LS_MAT_BINARY: |
|
4815 save_mat_binary_data (os, tc, name); |
|
4816 break; |
|
4817 |
3687
|
4818 #ifdef HAVE_HDF5 |
|
4819 case LS_HDF5: |
|
4820 save_hdf5_data (os, tc, name, help, global, save_as_floats); |
|
4821 break; |
|
4822 #endif /* HAVE_HDF5 */ |
|
4823 |
3688
|
4824 case LS_MAT5_BINARY: |
|
4825 save_mat5_binary_element (os, tc, name, global, save_as_floats); |
|
4826 break; |
|
4827 |
604
|
4828 default: |
775
|
4829 gripe_unrecognized_data_fmt ("save"); |
604
|
4830 break; |
|
4831 } |
|
4832 } |
|
4833 |
|
4834 // Save variables with names matching PATTERN on stream OS in the |
3019
|
4835 // format specified by FMT. If SAVE_BUILTINS is TRUE, also save |
604
|
4836 // builtin variables with names that match PATTERN. |
|
4837 |
|
4838 static int |
3523
|
4839 save_vars (std::ostream& os, const std::string& pattern, bool save_builtins, |
630
|
4840 load_save_format fmt, int save_as_floats) |
604
|
4841 { |
3355
|
4842 Array<symbol_record *> vars = curr_sym_tab->glob |
|
4843 (pattern, symbol_record::USER_VARIABLE, SYMTAB_ALL_SCOPES); |
|
4844 |
|
4845 int saved = vars.length (); |
|
4846 |
3738
|
4847 bool infnan_warned = false; |
|
4848 |
3355
|
4849 for (int i = 0; i < saved; i++) |
620
|
4850 { |
3738
|
4851 do_save (os, vars (i), fmt, save_as_floats, infnan_warned); |
620
|
4852 |
|
4853 if (error_state) |
|
4854 break; |
|
4855 } |
604
|
4856 |
620
|
4857 if (! error_state && save_builtins) |
604
|
4858 { |
4009
|
4859 vars = fbi_sym_tab->glob |
3355
|
4860 (pattern, symbol_record::BUILTIN_VARIABLE, SYMTAB_ALL_SCOPES); |
|
4861 |
|
4862 int count = vars.length (); |
604
|
4863 |
|
4864 saved += count; |
|
4865 |
3355
|
4866 for (int i = 0; i < count; i++) |
620
|
4867 { |
3738
|
4868 do_save (os, vars (i), fmt, save_as_floats, infnan_warned); |
620
|
4869 |
|
4870 if (error_state) |
|
4871 break; |
|
4872 } |
604
|
4873 } |
|
4874 |
|
4875 return saved; |
|
4876 } |
|
4877 |
|
4878 static load_save_format |
|
4879 get_default_save_format (void) |
|
4880 { |
|
4881 load_save_format retval = LS_ASCII; |
|
4882 |
3523
|
4883 std::string fmt = Vdefault_save_format; |
1755
|
4884 |
|
4885 if (fmt == "binary") |
604
|
4886 retval = LS_BINARY; |
1755
|
4887 else if (fmt == "mat-binary" || fmt =="mat_binary") |
3688
|
4888 retval = LS_MAT5_BINARY; |
|
4889 else if (fmt == "mat4-binary" || fmt =="mat4_binary") |
911
|
4890 retval = LS_MAT_BINARY; |
3687
|
4891 #ifdef HAVE_HDF5 |
|
4892 else if (fmt == "hdf5") |
|
4893 retval = LS_HDF5; |
|
4894 #endif /* HAVE_HDF5 */ |
604
|
4895 |
|
4896 return retval; |
|
4897 } |
|
4898 |
4329
|
4899 void |
3523
|
4900 write_header (std::ostream& os, load_save_format format) |
863
|
4901 { |
3185
|
4902 switch (format) |
863
|
4903 { |
3185
|
4904 case LS_BINARY: |
|
4905 { |
|
4906 os << (oct_mach_info::words_big_endian () |
|
4907 ? "Octave-1-B" : "Octave-1-L"); |
|
4908 |
|
4909 oct_mach_info::float_format flt_fmt = |
|
4910 oct_mach_info::native_float_format (); |
|
4911 |
|
4912 char tmp = (char) float_format_to_mopt_digit (flt_fmt); |
|
4913 |
3557
|
4914 os.write (X_CAST (char *, &tmp), 1); |
3185
|
4915 } |
3688
|
4916 break; |
|
4917 |
|
4918 case LS_MAT5_BINARY: |
|
4919 { |
3775
|
4920 char const * versionmagic; |
3688
|
4921 TWO_BYTE_INT number = *(TWO_BYTE_INT *)"\x00\x01"; |
|
4922 struct tm bdt; |
|
4923 time_t now; |
|
4924 char headertext[128]; |
|
4925 |
|
4926 time (&now); |
|
4927 bdt = *gmtime (&now); |
|
4928 memset (headertext, ' ', 124); |
|
4929 // ISO 8601 format date |
|
4930 strftime (headertext, 124, "MATLAB 5.0 MAT-file, written by Octave " |
|
4931 OCTAVE_VERSION ", %Y-%m-%d %T UTC", &bdt); |
|
4932 |
|
4933 // The first pair of bytes give the version of the MAT file |
|
4934 // format. The second pair of bytes form a magic number which |
|
4935 // signals a MAT file. MAT file data are always written in |
|
4936 // native byte order. The order of the bytes in the second |
|
4937 // pair indicates whether the file was written by a big- or |
|
4938 // little-endian machine. However, the version number is |
|
4939 // written in the *opposite* byte order from everything else! |
|
4940 if (number == 1) |
|
4941 versionmagic = "\x01\x00\x4d\x49"; // this machine is big endian |
|
4942 else |
|
4943 versionmagic = "\x00\x01\x49\x4d"; // this machine is little endian |
|
4944 |
|
4945 memcpy (headertext+124, versionmagic, 4); |
|
4946 os.write (headertext, 128); |
|
4947 } |
|
4948 |
|
4949 break; |
3185
|
4950 |
3687
|
4951 #ifdef HAVE_HDF5 |
|
4952 case LS_HDF5: |
|
4953 #endif /* HAVE_HDF5 */ |
3185
|
4954 case LS_ASCII: |
|
4955 { |
3709
|
4956 octave_localtime now; |
|
4957 |
|
4958 std::string comment_string = now.strftime (Vsave_header_format_string); |
|
4959 |
|
4960 if (! comment_string.empty ()) |
|
4961 { |
3687
|
4962 #ifdef HAVE_HDF5 |
3709
|
4963 if (format == LS_HDF5) |
|
4964 { |
|
4965 hdf5_ofstream& hs = (hdf5_ofstream&) os; |
|
4966 H5Gset_comment (hs.file_id, "/", comment_string.c_str ()); |
|
4967 } |
|
4968 else |
3687
|
4969 #endif /* HAVE_HDF5 */ |
3709
|
4970 os << comment_string << "\n"; |
3687
|
4971 } |
3185
|
4972 } |
|
4973 break; |
|
4974 |
|
4975 default: |
|
4976 break; |
863
|
4977 } |
|
4978 } |
|
4979 |
|
4980 static void |
3769
|
4981 save_vars (const string_vector& argv, int argv_idx, int argc, |
3523
|
4982 std::ostream& os, bool save_builtins, load_save_format fmt, |
3185
|
4983 bool save_as_floats, bool write_header_info) |
863
|
4984 { |
3185
|
4985 if (write_header_info) |
|
4986 write_header (os, fmt); |
863
|
4987 |
1755
|
4988 if (argv_idx == argc) |
863
|
4989 { |
|
4990 save_vars (os, "*", save_builtins, fmt, save_as_floats); |
|
4991 } |
|
4992 else |
|
4993 { |
1755
|
4994 for (int i = argv_idx; i < argc; i++) |
863
|
4995 { |
1755
|
4996 if (! save_vars (os, argv[i], save_builtins, fmt, save_as_floats)) |
863
|
4997 { |
1755
|
4998 warning ("save: no such variable `%s'", argv[i].c_str ()); |
863
|
4999 } |
|
5000 } |
|
5001 } |
|
5002 } |
|
5003 |
1380
|
5004 void |
|
5005 save_user_variables (void) |
|
5006 { |
3189
|
5007 if (Vcrash_dumps_octave_core) |
1380
|
5008 { |
3189
|
5009 // XXX FIXME XXX -- should choose better file name? |
|
5010 |
|
5011 const char *fname = "octave-core"; |
|
5012 |
|
5013 message (0, "attempting to save variables to `%s'...", fname); |
|
5014 |
|
5015 load_save_format format = get_default_save_format (); |
|
5016 |
3775
|
5017 std::ios::openmode mode = std::ios::out|std::ios::trunc; |
3688
|
5018 if (format == LS_BINARY || |
|
5019 format == LS_MAT_BINARY || |
|
5020 format == LS_MAT5_BINARY) |
3552
|
5021 mode |= std::ios::binary; |
3189
|
5022 |
3687
|
5023 #ifdef HAVE_HDF5 |
|
5024 if (format == LS_HDF5) |
3189
|
5025 { |
3687
|
5026 hdf5_ofstream file (fname); |
|
5027 |
|
5028 if (file.file_id >= 0) |
|
5029 { |
|
5030 save_vars (string_vector (), 0, 0, file, |
|
5031 false, format, false, true); |
|
5032 |
|
5033 message (0, "save to `%s' complete", fname); |
|
5034 |
|
5035 file.close (); |
|
5036 } |
|
5037 else |
|
5038 warning ("unable to open `%s' for writing...", fname); |
3189
|
5039 } |
|
5040 else |
3687
|
5041 #endif /* HAVE_HDF5 */ |
|
5042 // don't insert any commands here! The open brace below must |
|
5043 // go with the else above! |
|
5044 { |
|
5045 std::ofstream file (fname, mode); |
|
5046 |
|
5047 if (file) |
|
5048 { |
|
5049 save_vars (string_vector (), 0, 0, file, |
|
5050 false, format, false, true); |
|
5051 message (0, "save to `%s' complete", fname); |
|
5052 file.close (); |
|
5053 } |
|
5054 else |
|
5055 warning ("unable to open `%s' for writing...", fname); |
|
5056 } |
1380
|
5057 } |
|
5058 } |
|
5059 |
4208
|
5060 DEFCMD (save, args, , |
3372
|
5061 "-*- texinfo -*-\n\ |
|
5062 @deffn {Command} save options file v1 v2 @dots{}\n\ |
|
5063 Save the named variables @var{v1}, @var{v2}, @dots{} in the file\n\ |
|
5064 @var{file}. The special filename @samp{-} can be used to write the\n\ |
|
5065 output to your terminal. If no variable names are listed, Octave saves\n\ |
|
5066 all the variables in the current scope. Valid options for the\n\ |
|
5067 @code{save} command are listed in the following table. Options that\n\ |
|
5068 modify the output format override the format specified by the built-in\n\ |
|
5069 variable @code{default_save_format}.\n\ |
|
5070 \n\ |
|
5071 @table @code\n\ |
|
5072 @item -ascii\n\ |
|
5073 Save the data in Octave's text data format.\n\ |
|
5074 \n\ |
|
5075 @item -binary\n\ |
|
5076 Save the data in Octave's binary data format.\n\ |
|
5077 \n\ |
|
5078 @item -float-binary\n\ |
|
5079 Save the data in Octave's binary data format but only using single\n\ |
|
5080 precision. You should use this format only if you know that all the\n\ |
|
5081 values to be saved can be represented in single precision.\n\ |
|
5082 \n\ |
|
5083 @item -mat-binary\n\ |
|
5084 Save the data in @sc{Matlab}'s binary data format.\n\ |
|
5085 \n\ |
3688
|
5086 @item -mat4-binary\n\ |
|
5087 Save the data in the binary format written by @sc{Matlab} version 4.\n\ |
|
5088 \n\ |
3687
|
5089 @item -hdf5\n\ |
|
5090 Save the data in HDF5 format.\n\ |
|
5091 (HDF5 is a free, portable binary format developed by the National\n\ |
|
5092 Center for Supercomputing Applications at the University of Illinois.)\n" |
|
5093 |
|
5094 HAVE_HDF5_HELP_STRING |
|
5095 |
|
5096 "\n\ |
|
5097 @item -float-hdf5\n\ |
|
5098 Save the data in HDF5 format but only using single precision.\n\ |
|
5099 You should use this format only if you know that all the\n\ |
|
5100 values to be saved can be represented in single precision.\n\ |
|
5101 \n\ |
3372
|
5102 @item -save-builtins\n\ |
|
5103 Force Octave to save the values of built-in variables too. By default,\n\ |
|
5104 Octave does not save built-in variables.\n\ |
|
5105 @end table\n\ |
604
|
5106 \n\ |
3372
|
5107 The list of variables to save may include wildcard patterns containing\n\ |
|
5108 the following special characters:\n\ |
|
5109 @table @code\n\ |
|
5110 @item ?\n\ |
|
5111 Match any single character.\n\ |
|
5112 \n\ |
|
5113 @item *\n\ |
|
5114 Match zero or more characters.\n\ |
|
5115 \n\ |
|
5116 @item [ @var{list} ]\n\ |
|
5117 Match the list of characters specified by @var{list}. If the first\n\ |
|
5118 character is @code{!} or @code{^}, match all characters except those\n\ |
|
5119 specified by @var{list}. For example, the pattern @samp{[a-zA-Z]} will\n\ |
|
5120 match all lower and upper case alphabetic characters. \n\ |
|
5121 @end table\n\ |
|
5122 \n\ |
|
5123 Except when using the @sc{Matlab} binary data file format, saving global\n\ |
|
5124 variables also saves the global status of the variable, so that if it is\n\ |
|
5125 restored at a later time using @samp{load}, it will be restored as a\n\ |
|
5126 global variable.\n\ |
|
5127 \n\ |
|
5128 The command\n\ |
|
5129 \n\ |
|
5130 @example\n\ |
|
5131 save -binary data a b*\n\ |
|
5132 @end example\n\ |
|
5133 \n\ |
|
5134 @noindent\n\ |
|
5135 saves the variable @samp{a} and all variables beginning with @samp{b} to\n\ |
|
5136 the file @file{data} in Octave's binary format.\n\ |
|
5137 @end deffn") |
604
|
5138 { |
2086
|
5139 octave_value_list retval; |
604
|
5140 |
1755
|
5141 int argc = args.length () + 1; |
|
5142 |
1968
|
5143 string_vector argv = args.make_argv ("save"); |
1755
|
5144 |
|
5145 if (error_state) |
|
5146 return retval; |
604
|
5147 |
1358
|
5148 // Here is where we would get the default save format if it were |
|
5149 // stored in a user preference variable. |
604
|
5150 |
3019
|
5151 bool save_builtins = false; |
|
5152 |
|
5153 bool save_as_floats = false; |
630
|
5154 |
604
|
5155 load_save_format format = get_default_save_format (); |
|
5156 |
3185
|
5157 bool append = false; |
|
5158 |
1755
|
5159 int i; |
|
5160 for (i = 1; i < argc; i++) |
604
|
5161 { |
3185
|
5162 if (argv[i] == "-append") |
|
5163 { |
|
5164 append = true; |
|
5165 } |
3465
|
5166 else if (argv[i] == "-ascii" || argv[i] == "-a") |
604
|
5167 { |
|
5168 format = LS_ASCII; |
|
5169 } |
1755
|
5170 else if (argv[i] == "-binary" || argv[i] == "-b") |
604
|
5171 { |
|
5172 format = LS_BINARY; |
|
5173 } |
3687
|
5174 else if (argv[i] == "-hdf5" || argv[i] == "-h") |
|
5175 { |
|
5176 #ifdef HAVE_HDF5 |
|
5177 format = LS_HDF5; |
|
5178 #else /* ! HAVE_HDF5 */ |
|
5179 error ("save: octave executable was not linked with HDF5 library"); |
|
5180 return retval; |
|
5181 #endif /* ! HAVE_HDF5 */ |
|
5182 } |
1755
|
5183 else if (argv[i] == "-mat-binary" || argv[i] == "-m") |
667
|
5184 { |
3688
|
5185 format = LS_MAT5_BINARY; |
|
5186 } |
3797
|
5187 else if (argv[i] == "-mat4-binary" || argv[i] == "-4" || argv[i] == "-v4") |
3688
|
5188 { |
667
|
5189 format = LS_MAT_BINARY; |
|
5190 } |
1755
|
5191 else if (argv[i] == "-float-binary" || argv[i] == "-f") |
630
|
5192 { |
|
5193 format = LS_BINARY; |
3019
|
5194 save_as_floats = true; |
630
|
5195 } |
3687
|
5196 else if (argv[i] == "-float-hdf5") |
|
5197 { |
|
5198 #ifdef HAVE_HDF5 |
|
5199 format = LS_HDF5; |
|
5200 save_as_floats = true; |
|
5201 #else /* ! HAVE_HDF5 */ |
|
5202 error ("save: octave executable was not linked with HDF5 library"); |
|
5203 return retval; |
|
5204 #endif /* ! HAVE_HDF5 */ |
|
5205 } |
1755
|
5206 else if (argv[i] == "-save-builtins") |
604
|
5207 { |
3019
|
5208 save_builtins = true; |
604
|
5209 } |
|
5210 else |
|
5211 break; |
|
5212 } |
|
5213 |
2057
|
5214 if (i == argc) |
604
|
5215 { |
|
5216 print_usage ("save"); |
|
5217 return retval; |
|
5218 } |
|
5219 |
630
|
5220 if (save_as_floats && format == LS_ASCII) |
|
5221 { |
|
5222 error ("save: cannot specify both -ascii and -float-binary"); |
|
5223 return retval; |
|
5224 } |
|
5225 |
1755
|
5226 if (argv[i] == "-") |
604
|
5227 { |
1755
|
5228 i++; |
863
|
5229 |
3687
|
5230 #ifdef HAVE_HDF5 |
|
5231 if (format == LS_HDF5) |
|
5232 error ("load: cannot write HDF5 format to stdout"); |
|
5233 else |
|
5234 #endif /* HAVE_HDF5 */ |
|
5235 // don't insert any commands here! the brace below must go |
|
5236 // with the "else" above! |
|
5237 { |
|
5238 // XXX FIXME XXX -- should things intended for the screen end up |
|
5239 // in a octave_value (string)? |
|
5240 |
|
5241 save_vars (argv, i, argc, octave_stdout, save_builtins, format, |
|
5242 save_as_floats, true); |
|
5243 } |
604
|
5244 } |
1755
|
5245 |
|
5246 // Guard against things like `save a*', which are probably mistakes... |
|
5247 |
|
5248 else if (i == argc - 1 && glob_pattern_p (argv[i])) |
|
5249 { |
|
5250 print_usage ("save"); |
604
|
5251 return retval; |
|
5252 } |
|
5253 else |
|
5254 { |
3523
|
5255 std::string fname = file_ops::tilde_expand (argv[i]); |
1755
|
5256 |
|
5257 i++; |
604
|
5258 |
3775
|
5259 std::ios::openmode mode = std::ios::out; |
3688
|
5260 if (format == LS_BINARY || |
|
5261 format == LS_MAT_BINARY || |
|
5262 format == LS_MAT5_BINARY) |
3552
|
5263 mode |= std::ios::binary; |
3538
|
5264 |
|
5265 mode |= append ? std::ios::ate : std::ios::trunc; |
3185
|
5266 |
3687
|
5267 #ifdef HAVE_HDF5 |
|
5268 if (format == LS_HDF5) |
863
|
5269 { |
3687
|
5270 hdf5_ofstream hdf5_file (fname.c_str ()); |
|
5271 |
|
5272 if (hdf5_file.file_id >= 0) { |
|
5273 save_vars (argv, i, argc, hdf5_file, save_builtins, format, |
|
5274 save_as_floats, true); |
|
5275 |
|
5276 hdf5_file.close (); |
|
5277 } |
|
5278 else |
|
5279 { |
|
5280 error ("save: couldn't open output file `%s'", fname.c_str ()); |
|
5281 return retval; |
|
5282 } |
863
|
5283 } |
|
5284 else |
3687
|
5285 #endif /* HAVE_HDF5 */ |
|
5286 // don't insert any statements here! The brace below must go |
|
5287 // with the "else" above! |
604
|
5288 { |
3687
|
5289 std::ofstream file (fname.c_str (), mode); |
|
5290 |
|
5291 if (file) |
|
5292 { |
|
5293 bool write_header_info |
3775
|
5294 = ((file.rdbuf ())->pubseekoff (0, std::ios::cur) |
3769
|
5295 == static_cast<std::streampos> (0)); |
3687
|
5296 |
|
5297 save_vars (argv, i, argc, file, save_builtins, format, |
|
5298 save_as_floats, write_header_info); |
|
5299 } |
|
5300 else |
|
5301 { |
|
5302 error ("save: couldn't open output file `%s'", fname.c_str ()); |
|
5303 return retval; |
|
5304 } |
604
|
5305 } |
|
5306 } |
|
5307 |
|
5308 return retval; |
|
5309 } |
|
5310 |
|
5311 // Maybe this should be a static function in tree-plot.cc? |
|
5312 |
620
|
5313 // If TC is matrix, save it on stream OS in a format useful for |
604
|
5314 // making a 3-dimensional plot with gnuplot. If PARAMETRIC is |
3019
|
5315 // TRUE, assume a parametric 3-dimensional plot will be generated. |
604
|
5316 |
2799
|
5317 bool |
3523
|
5318 save_three_d (std::ostream& os, const octave_value& tc, bool parametric) |
604
|
5319 { |
3019
|
5320 bool fail = false; |
604
|
5321 |
620
|
5322 int nr = tc.rows (); |
|
5323 int nc = tc.columns (); |
|
5324 |
|
5325 if (tc.is_real_matrix ()) |
604
|
5326 { |
4060
|
5327 os << "# 3D data...\n" |
|
5328 << "# type: matrix\n" |
|
5329 << "# total rows: " << nr << "\n" |
|
5330 << "# total columns: " << nc << "\n"; |
604
|
5331 |
|
5332 if (parametric) |
|
5333 { |
|
5334 int extras = nc % 3; |
|
5335 if (extras) |
|
5336 warning ("ignoring last %d columns", extras); |
|
5337 |
620
|
5338 Matrix tmp = tc.matrix_value (); |
872
|
5339 tmp = strip_infnan (tmp); |
|
5340 nr = tmp.rows (); |
|
5341 |
604
|
5342 for (int i = 0; i < nc-extras; i += 3) |
|
5343 { |
|
5344 os << tmp.extract (0, i, nr-1, i+2); |
|
5345 if (i+3 < nc-extras) |
|
5346 os << "\n"; |
|
5347 } |
|
5348 } |
|
5349 else |
|
5350 { |
620
|
5351 Matrix tmp = tc.matrix_value (); |
872
|
5352 tmp = strip_infnan (tmp); |
|
5353 nr = tmp.rows (); |
|
5354 |
604
|
5355 for (int i = 0; i < nc; i++) |
|
5356 { |
|
5357 os << tmp.extract (0, i, nr-1, i); |
|
5358 if (i+1 < nc) |
|
5359 os << "\n"; |
|
5360 } |
|
5361 } |
620
|
5362 } |
|
5363 else |
|
5364 { |
604
|
5365 ::error ("for now, I can only save real matrices in 3D format"); |
3019
|
5366 fail = true; |
604
|
5367 } |
620
|
5368 |
|
5369 return (os && ! fail); |
604
|
5370 } |
|
5371 |
2194
|
5372 static int |
3189
|
5373 crash_dumps_octave_core (void) |
|
5374 { |
|
5375 Vcrash_dumps_octave_core = check_preference ("crash_dumps_octave_core"); |
|
5376 return 0; |
|
5377 } |
|
5378 |
|
5379 |
|
5380 static int |
2194
|
5381 default_save_format (void) |
|
5382 { |
|
5383 int status = 0; |
|
5384 |
3523
|
5385 std::string s = builtin_string_variable ("default_save_format"); |
2194
|
5386 |
|
5387 if (s.empty ()) |
|
5388 { |
|
5389 gripe_invalid_value_specified ("default_save_format"); |
|
5390 status = -1; |
|
5391 } |
|
5392 else |
|
5393 Vdefault_save_format = s; |
|
5394 |
|
5395 return status; |
|
5396 } |
|
5397 |
3769
|
5398 static std::string |
3709
|
5399 default_save_header_format (void) |
|
5400 { |
|
5401 return |
4060
|
5402 std::string ("# Created by Octave " OCTAVE_VERSION ", %a %b %d %H:%M:%S %Y %Z <") |
3709
|
5403 + octave_env::get_user_name () |
|
5404 + std::string ("@") |
|
5405 + octave_env::get_host_name () |
|
5406 + std::string (">"); |
|
5407 } |
|
5408 |
|
5409 static int |
|
5410 save_header_format_string (void) |
|
5411 { |
|
5412 int status = 0; |
|
5413 |
|
5414 octave_value v = builtin_any_variable ("save_header_format_string"); |
|
5415 |
|
5416 if (v.is_string ()) |
|
5417 Vsave_header_format_string = v.string_value (); |
|
5418 else |
|
5419 { |
|
5420 gripe_invalid_value_specified ("save_header_format_string"); |
|
5421 status = -1; |
|
5422 } |
|
5423 |
|
5424 return status; |
|
5425 } |
|
5426 |
2194
|
5427 static int |
|
5428 save_precision (void) |
|
5429 { |
|
5430 double val; |
|
5431 if (builtin_real_scalar_variable ("save_precision", val) |
|
5432 && ! xisnan (val)) |
|
5433 { |
|
5434 int ival = NINT (val); |
2800
|
5435 if (ival >= 0 && ival == val) |
2194
|
5436 { |
|
5437 Vsave_precision = ival; |
|
5438 return 0; |
|
5439 } |
|
5440 } |
|
5441 gripe_invalid_value_specified ("save_precision"); |
|
5442 return -1; |
|
5443 } |
|
5444 |
|
5445 void |
|
5446 symbols_of_load_save (void) |
|
5447 { |
4233
|
5448 DEFVAR (crash_dumps_octave_core, true, crash_dumps_octave_core, |
3372
|
5449 "-*- texinfo -*-\n\ |
|
5450 @defvr {Built-in Variable} crash_dumps_octave_core\n\ |
|
5451 If this variable is set to a nonzero value, Octave tries to save all\n\ |
|
5452 current variables the the file \"octave-core\" if it crashes or receives a\n\ |
|
5453 hangup, terminate or similar signal. The default value is 1.\n\ |
|
5454 @end defvr"); |
3189
|
5455 |
3258
|
5456 DEFVAR (default_save_format, "ascii", default_save_format, |
3372
|
5457 "-*- texinfo -*-\n\ |
|
5458 @defvr {Built-in Variable} default_save_format\n\ |
|
5459 This variable specifies the default format for the @code{save} command.\n\ |
|
5460 It should have one of the following values: @code{\"ascii\"},\n\ |
|
5461 @code{\"binary\"}, @code{float-binary}, or @code{\"mat-binary\"}. The\n\ |
|
5462 initial default save format is Octave's text format.\n\ |
|
5463 @end defvr"); |
2194
|
5464 |
3709
|
5465 DEFVAR (save_header_format_string, default_save_header_format (), |
|
5466 save_header_format_string, |
|
5467 "-*- texinfo -*-\n\ |
|
5468 @defvr {Built-in Variable} save_header_format_string\n\ |
|
5469 This variable specifies the the format string for the comment line\n\ |
|
5470 that is written at the beginning of text-format data files saved by\n\ |
|
5471 Octave. The format string is passed to @code{strftime} and should\n\ |
|
5472 begin with the character @samp{#} and contain no newline characters.\n\ |
|
5473 If the value of @code{save_header_format_string} is the empty string,\n\ |
|
5474 the header comment is omitted from text-format data files. The\n\ |
|
5475 default value is\n\ |
|
5476 \n\ |
|
5477 @example\n\ |
4060
|
5478 \"# Created by Octave VERSION, %a %b %d %H:%M:%S %Y %Z <USER@@HOST>\"\n\ |
3709
|
5479 @end example\n\ |
|
5480 @seealso{strftime}\n\ |
|
5481 @end defvr"); |
|
5482 |
3258
|
5483 DEFVAR (save_precision, 15.0, save_precision, |
3372
|
5484 "-*- texinfo -*-\n\ |
|
5485 @defvr {Built-in Variable} save_precision\n\ |
|
5486 This variable specifies the number of digits to keep when saving data in\n\ |
|
5487 text format. The default value is 17.\n\ |
|
5488 @end defvr"); |
2194
|
5489 } |
|
5490 |
604
|
5491 /* |
|
5492 ;;; Local Variables: *** |
|
5493 ;;; mode: C++ *** |
|
5494 ;;; End: *** |
|
5495 */ |