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