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> |
1728
|
38 #include <string> |
|
39 |
3687
|
40 #ifdef HAVE_HDF5 |
|
41 #include <hdf5.h> |
|
42 #endif |
|
43 |
1961
|
44 #include "byte-swap.h" |
|
45 #include "data-conv.h" |
2926
|
46 #include "file-ops.h" |
|
47 #include "glob-match.h" |
2890
|
48 #include "lo-mappers.h" |
4051
|
49 #include "lo-sstream.h" |
2318
|
50 #include "mach-info.h" |
3185
|
51 #include "oct-env.h" |
3258
|
52 #include "oct-time.h" |
4171
|
53 #include "quit.h" |
1755
|
54 #include "str-vec.h" |
|
55 |
4332
|
56 #include "Cell.h" |
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" |
4332
|
63 #include "ov-cell.h" |
1352
|
64 #include "pager.h" |
1750
|
65 #include "pt-exp.h" |
1352
|
66 #include "symtab.h" |
|
67 #include "sysdep.h" |
|
68 #include "unwind-prot.h" |
604
|
69 #include "utils.h" |
2371
|
70 #include "variables.h" |
3185
|
71 #include "version.h" |
3688
|
72 #include "dMatrix.h" |
|
73 |
4659
|
74 #ifdef HAVE_HDF5 |
4633
|
75 #include "ls-hdf5.h" |
4659
|
76 #endif |
4633
|
77 #include "ls-mat-ascii.h" |
|
78 #include "ls-mat4.h" |
|
79 #include "ls-mat5.h" |
|
80 #include "ls-oct-ascii.h" |
|
81 #include "ls-oct-binary.h" |
3688
|
82 |
3598
|
83 // Write octave-core file if Octave crashes or is killed by a signal. |
3189
|
84 static bool Vcrash_dumps_octave_core; |
|
85 |
4791
|
86 // The maximum amount of memory (in kilobytes) that we will attempt to |
|
87 // write to the Octave core file. |
|
88 static double Voctave_core_file_limit; |
|
89 |
|
90 // The name of the Octave core file. |
|
91 static std::string Voctave_core_file_name; |
|
92 |
3687
|
93 // The default output format. May be one of "binary", "text", |
|
94 // "mat-binary", or "hdf5". |
3523
|
95 static std::string Vdefault_save_format; |
2194
|
96 |
4791
|
97 // The output format for Octave core files. May be one of "binary", |
4788
|
98 // "text", "mat-binary", or "hdf5". |
4791
|
99 static std::string Voctave_core_file_format; |
4788
|
100 |
3709
|
101 // The format string for the comment line at the top of text-format |
|
102 // save files. Passed to strftime. Should begin with `#' and contain |
|
103 // no newline characters. |
|
104 static std::string Vsave_header_format_string; |
|
105 |
630
|
106 // XXX FIXME XXX -- shouldn't this be implemented in terms of other |
|
107 // functions that are already available? |
604
|
108 |
|
109 // Install a variable with name NAME and the value specified TC in the |
3019
|
110 // symbol table. If FORCE is TRUE, replace any existing definition |
|
111 // for NAME. If GLOBAL is TRUE, make the variable global. |
604
|
112 // |
|
113 // Assumes TC is defined. |
|
114 |
|
115 static void |
4171
|
116 install_loaded_variable (int force, const std::string& name, |
|
117 const octave_value& val, |
|
118 int global, const std::string& doc) |
604
|
119 { |
1358
|
120 // Is there already a symbol by this name? If so, what is it? |
604
|
121 |
2856
|
122 symbol_record *lsr = curr_sym_tab->lookup (name); |
604
|
123 |
3019
|
124 bool is_undefined = true; |
|
125 bool is_variable = false; |
|
126 bool is_function = false; |
|
127 bool is_global = false; |
604
|
128 |
|
129 if (lsr) |
|
130 { |
|
131 is_undefined = ! lsr->is_defined (); |
|
132 is_variable = lsr->is_variable (); |
|
133 is_function = lsr->is_function (); |
|
134 is_global = lsr->is_linked_to_global (); |
|
135 } |
|
136 |
|
137 symbol_record *sr = 0; |
|
138 |
|
139 if (global) |
|
140 { |
|
141 if (is_global || is_undefined) |
|
142 { |
|
143 if (force || is_undefined) |
|
144 { |
2856
|
145 lsr = curr_sym_tab->lookup (name, true); |
604
|
146 link_to_global_variable (lsr); |
|
147 sr = lsr; |
|
148 } |
|
149 else |
|
150 { |
4171
|
151 warning ("load: global variable name `%s' exists", |
|
152 name.c_str ()); |
604
|
153 warning ("use `load -force' to overwrite"); |
|
154 } |
|
155 } |
|
156 else if (is_function) |
|
157 { |
|
158 if (force) |
|
159 { |
2856
|
160 lsr = curr_sym_tab->lookup (name, true); |
604
|
161 link_to_global_variable (lsr); |
|
162 sr = lsr; |
|
163 } |
|
164 else |
|
165 { |
4171
|
166 warning ("load: `%s' is currently a function in this scope", |
|
167 name.c_str ()); |
604
|
168 warning ("`load -force' will load variable and hide function"); |
|
169 } |
|
170 } |
|
171 else if (is_variable) |
|
172 { |
|
173 if (force) |
|
174 { |
2856
|
175 lsr = curr_sym_tab->lookup (name, true); |
604
|
176 link_to_global_variable (lsr); |
|
177 sr = lsr; |
|
178 } |
|
179 else |
|
180 { |
4171
|
181 warning ("load: local variable name `%s' exists", |
|
182 name.c_str ()); |
604
|
183 warning ("use `load -force' to overwrite"); |
|
184 } |
|
185 } |
|
186 else |
774
|
187 error ("load: unable to load data for unknown symbol type"); |
604
|
188 } |
|
189 else |
|
190 { |
|
191 if (is_global) |
|
192 { |
|
193 if (force || is_undefined) |
|
194 { |
2856
|
195 lsr = curr_sym_tab->lookup (name, true); |
604
|
196 link_to_global_variable (lsr); |
|
197 sr = lsr; |
|
198 } |
|
199 else |
|
200 { |
4171
|
201 warning ("load: global variable name `%s' exists", |
|
202 name.c_str ()); |
604
|
203 warning ("use `load -force' to overwrite"); |
|
204 } |
|
205 } |
|
206 else if (is_function) |
|
207 { |
|
208 if (force) |
|
209 { |
2856
|
210 lsr = curr_sym_tab->lookup (name, true); |
604
|
211 link_to_global_variable (lsr); |
|
212 sr = lsr; |
|
213 } |
|
214 else |
|
215 { |
4171
|
216 warning ("load: `%s' is currently a function in this scope", |
|
217 name.c_str ()); |
604
|
218 warning ("`load -force' will load variable and hide function"); |
|
219 } |
|
220 } |
|
221 else if (is_variable || is_undefined) |
|
222 { |
|
223 if (force || is_undefined) |
|
224 { |
2856
|
225 lsr = curr_sym_tab->lookup (name, true); |
604
|
226 sr = lsr; |
|
227 } |
|
228 else |
|
229 { |
4171
|
230 warning ("load: local variable name `%s' exists", |
|
231 name.c_str ()); |
604
|
232 warning ("use `load -force' to overwrite"); |
|
233 } |
|
234 } |
|
235 else |
774
|
236 error ("load: unable to load data for unknown symbol type"); |
604
|
237 } |
|
238 |
|
239 if (sr) |
|
240 { |
2371
|
241 sr->define (val); |
4171
|
242 sr->document (doc); |
604
|
243 return; |
|
244 } |
|
245 else |
4171
|
246 error ("load: unable to load variable `%s'", name.c_str ()); |
604
|
247 |
|
248 return; |
|
249 } |
|
250 |
3019
|
251 // Return TRUE if NAME matches one of the given globbing PATTERNS. |
604
|
252 |
3013
|
253 static bool |
3769
|
254 matches_patterns (const string_vector& patterns, int pat_idx, |
3523
|
255 int num_pat, const std::string& name) |
604
|
256 { |
1755
|
257 for (int i = pat_idx; i < num_pat; i++) |
604
|
258 { |
1792
|
259 glob_match pattern (patterns[i]); |
3013
|
260 |
1792
|
261 if (pattern.match (name)) |
3013
|
262 return true; |
604
|
263 } |
3688
|
264 |
3013
|
265 return false; |
604
|
266 } |
|
267 |
4329
|
268 int |
3523
|
269 read_binary_file_header (std::istream& is, bool& swap, |
4329
|
270 oct_mach_info::float_format& flt_fmt, bool quiet) |
604
|
271 { |
3552
|
272 const int magic_len = 10; |
|
273 char magic[magic_len+1]; |
3557
|
274 is.read (X_CAST (char *, magic), magic_len); |
604
|
275 magic[magic_len] = '\0'; |
3688
|
276 |
604
|
277 if (strncmp (magic, "Octave-1-L", magic_len) == 0) |
2318
|
278 swap = oct_mach_info::words_big_endian (); |
604
|
279 else if (strncmp (magic, "Octave-1-B", magic_len) == 0) |
2318
|
280 swap = ! oct_mach_info::words_big_endian (); |
604
|
281 else |
|
282 { |
|
283 if (! quiet) |
|
284 error ("load: can't read binary file"); |
|
285 return -1; |
|
286 } |
|
287 |
|
288 char tmp = 0; |
3557
|
289 is.read (X_CAST (char *, &tmp), 1); |
604
|
290 |
2318
|
291 flt_fmt = mopt_digit_to_float_format (tmp); |
|
292 |
4574
|
293 if (flt_fmt == oct_mach_info::flt_fmt_unknown) |
604
|
294 { |
|
295 if (! quiet) |
|
296 error ("load: unrecognized binary format!"); |
3688
|
297 |
604
|
298 return -1; |
|
299 } |
|
300 |
|
301 return 0; |
|
302 } |
|
303 |
|
304 static load_save_format |
3523
|
305 get_file_format (const std::string& fname, const std::string& orig_fname) |
604
|
306 { |
|
307 load_save_format retval = LS_UNKNOWN; |
|
308 |
4844
|
309 // If the file doesn't exist do nothing |
|
310 std::ifstream file_exist (fname.c_str ()); |
|
311 if (file_exist) |
|
312 file_exist.close (); |
|
313 else |
|
314 return LS_UNKNOWN; |
|
315 |
3687
|
316 #ifdef HAVE_HDF5 |
3688
|
317 // check this before we open the file |
3687
|
318 if (H5Fis_hdf5 (fname.c_str ()) > 0) |
|
319 return LS_HDF5; |
|
320 #endif /* HAVE_HDF5 */ |
|
321 |
3523
|
322 std::ifstream file (fname.c_str ()); |
604
|
323 |
|
324 if (! file) |
|
325 { |
1750
|
326 error ("load: couldn't open input file `%s'", orig_fname.c_str ()); |
604
|
327 return retval; |
|
328 } |
|
329 |
4574
|
330 oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown; |
604
|
331 |
3019
|
332 bool swap = false; |
|
333 |
|
334 if (read_binary_file_header (file, swap, flt_fmt, true) == 0) |
604
|
335 retval = LS_BINARY; |
|
336 else |
|
337 { |
3538
|
338 file.seekg (0, std::ios::beg); |
604
|
339 |
|
340 FOUR_BYTE_INT mopt, nr, nc, imag, len; |
1180
|
341 |
|
342 int err = read_mat_file_header (file, swap, mopt, nr, nc, imag, len, 1); |
|
343 |
|
344 if (! err) |
604
|
345 retval = LS_MAT_BINARY; |
|
346 else |
|
347 { |
2511
|
348 file.clear (); |
3538
|
349 file.seekg (0, std::ios::beg); |
604
|
350 |
3697
|
351 err = read_mat5_binary_file_header (file, swap, true); |
3688
|
352 |
|
353 if (! err) |
|
354 { |
|
355 file.clear (); |
|
356 file.seekg (0, std::ios::beg); |
|
357 retval = LS_MAT5_BINARY; |
|
358 } |
|
359 else |
|
360 { |
|
361 file.clear (); |
|
362 file.seekg (0, std::ios::beg); |
|
363 |
4171
|
364 std::string tmp = extract_keyword (file, "name"); |
|
365 |
|
366 if (! tmp.empty ()) |
|
367 retval = LS_ASCII; |
3688
|
368 else |
|
369 { |
|
370 // Try reading the file as numbers only, determining the |
|
371 // number of rows and columns from the data. We don't |
|
372 // even bother to check to see if the first item in the |
|
373 // file is a number, so that get_complete_line() can |
|
374 // skip any comments that might appear at the top of the |
|
375 // file. |
|
376 |
|
377 retval = LS_MAT_ASCII; |
|
378 } |
2511
|
379 } |
604
|
380 } |
|
381 } |
|
382 |
|
383 file.close (); |
|
384 |
|
385 if (retval == LS_UNKNOWN) |
1750
|
386 error ("load: unable to determine file format for `%s'", |
|
387 orig_fname.c_str ()); |
604
|
388 |
|
389 return retval; |
|
390 } |
|
391 |
4329
|
392 octave_value |
3523
|
393 do_load (std::istream& stream, const std::string& orig_fname, bool force, |
2318
|
394 load_save_format format, oct_mach_info::float_format flt_fmt, |
4687
|
395 bool list_only, bool swap, bool verbose, |
3687
|
396 const string_vector& argv, int argv_idx, int argc, int nargout) |
604
|
397 { |
3727
|
398 octave_value retval; |
|
399 |
|
400 Octave_map retstruct; |
604
|
401 |
4051
|
402 OSSTREAM output_buf; |
|
403 |
604
|
404 int count = 0; |
4051
|
405 |
604
|
406 for (;;) |
|
407 { |
3019
|
408 bool global = false; |
2086
|
409 octave_value tc; |
604
|
410 |
4171
|
411 std::string name; |
|
412 std::string doc; |
604
|
413 |
|
414 switch (format) |
|
415 { |
|
416 case LS_ASCII: |
3136
|
417 name = read_ascii_data (stream, orig_fname, global, tc, count); |
604
|
418 break; |
|
419 |
|
420 case LS_BINARY: |
|
421 name = read_binary_data (stream, swap, flt_fmt, orig_fname, |
|
422 global, tc, doc); |
|
423 break; |
|
424 |
2511
|
425 case LS_MAT_ASCII: |
|
426 name = read_mat_ascii_data (stream, orig_fname, tc); |
|
427 break; |
|
428 |
604
|
429 case LS_MAT_BINARY: |
|
430 name = read_mat_binary_data (stream, orig_fname, tc); |
|
431 break; |
|
432 |
3687
|
433 #ifdef HAVE_HDF5 |
|
434 case LS_HDF5: |
4687
|
435 name = read_hdf5_data (stream, orig_fname, global, tc, doc); |
3687
|
436 break; |
|
437 #endif /* HAVE_HDF5 */ |
|
438 |
3688
|
439 case LS_MAT5_BINARY: |
|
440 name = read_mat5_binary_element (stream, orig_fname, swap, |
|
441 global, tc); |
|
442 break; |
|
443 |
604
|
444 default: |
775
|
445 gripe_unrecognized_data_fmt ("load"); |
604
|
446 break; |
|
447 } |
|
448 |
4171
|
449 if (error_state || stream.eof () || name.empty ()) |
|
450 break; |
|
451 else if (! error_state && ! name.empty ()) |
604
|
452 { |
|
453 if (tc.is_defined ()) |
|
454 { |
3136
|
455 if (format == LS_MAT_ASCII && argv_idx < argc) |
|
456 warning ("load: loaded ASCII file `%s' -- ignoring extra args", |
3687
|
457 orig_fname.c_str ()); |
3136
|
458 |
|
459 if (format == LS_MAT_ASCII |
|
460 || argv_idx == argc |
1755
|
461 || matches_patterns (argv, argv_idx, argc, name)) |
604
|
462 { |
|
463 count++; |
621
|
464 if (list_only) |
|
465 { |
|
466 if (verbose) |
|
467 { |
|
468 if (count == 1) |
|
469 output_buf |
|
470 << "type rows cols name\n" |
|
471 << "==== ==== ==== ====\n"; |
|
472 |
3013
|
473 output_buf |
3548
|
474 << std::setiosflags (std::ios::left) |
|
475 << std::setw (16) << tc.type_name () . c_str () |
|
476 << std::setiosflags (std::ios::right) |
|
477 << std::setw (7) << tc.rows () |
|
478 << std::setw (7) << tc.columns () |
3013
|
479 << " "; |
621
|
480 } |
|
481 output_buf << name << "\n"; |
|
482 } |
|
483 else |
|
484 { |
3727
|
485 if (nargout == 1) |
|
486 { |
|
487 if (format == LS_MAT_ASCII) |
|
488 retval = tc; |
|
489 else |
4675
|
490 retstruct.assign (name, tc); |
3727
|
491 } |
|
492 else |
|
493 install_loaded_variable (force, name, tc, global, doc); |
621
|
494 } |
604
|
495 } |
2511
|
496 |
|
497 // Only attempt to read one item from a headless text file. |
|
498 |
|
499 if (format == LS_MAT_ASCII) |
|
500 break; |
604
|
501 } |
|
502 else |
4171
|
503 error ("load: unable to load variable `%s'", name.c_str ()); |
604
|
504 } |
|
505 else |
|
506 { |
|
507 if (count == 0) |
|
508 error ("load: are you sure `%s' is an Octave data file?", |
1755
|
509 orig_fname.c_str ()); |
604
|
510 |
|
511 break; |
|
512 } |
|
513 } |
|
514 |
621
|
515 if (list_only && count) |
|
516 { |
4051
|
517 output_buf << OSSTREAM_ENDS; |
|
518 std::string msg = OSSTREAM_STR (output_buf); |
|
519 OSSTREAM_FREEZE (output_buf); |
2095
|
520 |
621
|
521 if (nargout > 0) |
2095
|
522 retval = msg; |
621
|
523 else |
2095
|
524 octave_stdout << msg; |
621
|
525 } |
3727
|
526 else if (! retstruct.empty ()) |
|
527 retval = retstruct; |
621
|
528 |
863
|
529 return retval; |
|
530 } |
|
531 |
3687
|
532 // HDF5 load/save documentation is included in the Octave manual |
|
533 // regardless, but if HDF5 is not linked in we also include a |
|
534 // sentence noting this, so the user understands that the features |
|
535 // aren't available. Define a macro for this sentence: |
|
536 |
|
537 #ifdef HAVE_HDF5 |
|
538 #define HAVE_HDF5_HELP_STRING "" |
|
539 #else /* ! HAVE_HDF5 */ |
|
540 #define HAVE_HDF5_HELP_STRING "\n\ |
|
541 HDF5 load and save are not available, as this Octave executable was\n\ |
|
542 not linked with the HDF5 library." |
|
543 #endif /* ! HAVE HDF5 */ |
|
544 |
4208
|
545 DEFCMD (load, args, nargout, |
3372
|
546 "-*- texinfo -*-\n\ |
|
547 @deffn {Command} load options file v1 v2 @dots{}\n\ |
|
548 Load the named variables from the file @var{file}. As with @code{save},\n\ |
|
549 you may specify a list of variables and @code{load} will only extract\n\ |
|
550 those variables with names that match. For example, to restore the\n\ |
|
551 variables saved in the file @file{data}, use the command\n\ |
|
552 \n\ |
|
553 @example\n\ |
|
554 load data\n\ |
|
555 @end example\n\ |
863
|
556 \n\ |
3372
|
557 Octave will refuse to overwrite existing variables unless you use the\n\ |
|
558 option @samp{-force}.\n\ |
|
559 \n\ |
|
560 If a variable that is not marked as global is loaded from a file when a\n\ |
|
561 global symbol with the same name already exists, it is loaded in the\n\ |
|
562 global symbol table. Also, if a variable is marked as global in a file\n\ |
|
563 and a local symbol exists, the local symbol is moved to the global\n\ |
|
564 symbol table and given the value from the file. Since it seems that\n\ |
|
565 both of these cases are likely to be the result of some sort of error,\n\ |
|
566 they will generate warnings.\n\ |
863
|
567 \n\ |
3727
|
568 If invoked with a single output argument, Octave returns data instead\n\ |
|
569 of inserting variables in the symbol table. If the data file contains\n\ |
|
570 only numbers (TAB- or space-delimited columns), a matrix of values is\n\ |
|
571 returned. Otherwise, @code{load} returns a structure with members\n\ |
|
572 corresponding to the names of the variables in the file.\n\ |
|
573 \n\ |
3372
|
574 The @code{load} command can read data stored in Octave's text and\n\ |
|
575 binary formats, and @sc{Matlab}'s binary format. It will automatically\n\ |
|
576 detect the type of file and do conversion from different floating point\n\ |
|
577 formats (currently only IEEE big and little endian, though other formats\n\ |
|
578 may added in the future).\n\ |
|
579 \n\ |
|
580 Valid options for @code{load} are listed in the following table.\n\ |
863
|
581 \n\ |
3372
|
582 @table @code\n\ |
|
583 @item -force\n\ |
|
584 Force variables currently in memory to be overwritten by variables with\n\ |
|
585 the same name found in the file.\n\ |
|
586 \n\ |
|
587 @item -ascii\n\ |
|
588 Force Octave to assume the file is in Octave's text format.\n\ |
|
589 \n\ |
|
590 @item -binary\n\ |
|
591 Force Octave to assume the file is in Octave's binary format.\n\ |
|
592 \n\ |
|
593 @item -mat-binary\n\ |
|
594 Force Octave to assume the file is in @sc{Matlab}'s binary format.\n\ |
3687
|
595 \n\ |
3688
|
596 @item -mat4-binary\n\ |
|
597 Force Octave to assume the file is in the binary format written by\n\ |
|
598 @sc{Matlab} version 4.\n\ |
|
599 \n\ |
3687
|
600 @item -hdf5\n\ |
|
601 Force Octave to assume the file is in HDF5 format.\n\ |
|
602 (HDF5 is a free, portable binary format developed by the National\n\ |
|
603 Center for Supercomputing Applications at the University of Illinois.)\n\ |
|
604 Note that Octave can read HDF5 files not created by itself, but may\n\ |
4687
|
605 skip some datasets in formats that it cannot support.\n" |
3687
|
606 |
|
607 HAVE_HDF5_HELP_STRING |
|
608 |
|
609 "\n\ |
|
610 @item -import\n\ |
4687
|
611 The @samp{-import} is acceptted but ignored for backward compatiability.\n\ |
|
612 Octave can now support multi-dimensional HDF data and automatically modifies\n\ |
|
613 variable names if they are invalid Octave identifiers.\n\ |
3687
|
614 \n\ |
3372
|
615 @end table\n\ |
|
616 @end deffn") |
863
|
617 { |
2086
|
618 octave_value_list retval; |
863
|
619 |
1755
|
620 int argc = args.length () + 1; |
|
621 |
1968
|
622 string_vector argv = args.make_argv ("load"); |
1755
|
623 |
|
624 if (error_state) |
|
625 return retval; |
863
|
626 |
1358
|
627 // It isn't necessary to have the default load format stored in a |
|
628 // user preference variable since we can determine the type of file |
|
629 // as we are reading. |
863
|
630 |
|
631 load_save_format format = LS_UNKNOWN; |
|
632 |
4691
|
633 bool force = true; |
3019
|
634 bool list_only = false; |
|
635 bool verbose = false; |
863
|
636 |
1755
|
637 int i; |
|
638 for (i = 1; i < argc; i++) |
863
|
639 { |
1755
|
640 if (argv[i] == "-force" || argv[i] == "-f") |
863
|
641 { |
3019
|
642 force = true; |
863
|
643 } |
1755
|
644 else if (argv[i] == "-list" || argv[i] == "-l") |
863
|
645 { |
3019
|
646 list_only = true; |
863
|
647 } |
1755
|
648 else if (argv[i] == "-verbose" || argv[i] == "-v") |
863
|
649 { |
3019
|
650 verbose = true; |
863
|
651 } |
1755
|
652 else if (argv[i] == "-ascii" || argv[i] == "-a") |
863
|
653 { |
|
654 format = LS_ASCII; |
|
655 } |
1755
|
656 else if (argv[i] == "-binary" || argv[i] == "-b") |
863
|
657 { |
|
658 format = LS_BINARY; |
|
659 } |
1755
|
660 else if (argv[i] == "-mat-binary" || argv[i] == "-m") |
863
|
661 { |
3688
|
662 format = LS_MAT5_BINARY; |
|
663 } |
3797
|
664 else if (argv[i] == "-mat4-binary" || argv[i] == "-4" || argv[i] == "-v4") |
3688
|
665 { |
863
|
666 format = LS_MAT_BINARY; |
|
667 } |
3687
|
668 else if (argv[i] == "-hdf5" || argv[i] == "-h") |
|
669 { |
|
670 #ifdef HAVE_HDF5 |
|
671 format = LS_HDF5; |
|
672 #else /* ! HAVE_HDF5 */ |
|
673 error ("load: octave executable was not linked with HDF5 library"); |
|
674 return retval; |
|
675 #endif /* ! HAVE_HDF5 */ |
|
676 } |
|
677 else if (argv[i] == "-import" || argv[i] == "-i") |
|
678 { |
4687
|
679 warning ("load: -import ignored"); |
3687
|
680 } |
863
|
681 else |
|
682 break; |
|
683 } |
|
684 |
1755
|
685 if (i == argc) |
863
|
686 { |
2057
|
687 print_usage ("load"); |
863
|
688 return retval; |
|
689 } |
|
690 |
3523
|
691 std::string orig_fname = argv[i]; |
863
|
692 |
4574
|
693 oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown; |
863
|
694 |
3019
|
695 bool swap = false; |
863
|
696 |
1755
|
697 if (argv[i] == "-") |
863
|
698 { |
1755
|
699 i++; |
863
|
700 |
3687
|
701 #ifdef HAVE_HDF5 |
|
702 if (format == LS_HDF5) |
|
703 error ("load: cannot read HDF5 format from stdin"); |
|
704 else |
|
705 #endif /* HAVE_HDF5 */ |
863
|
706 if (format != LS_UNKNOWN) |
|
707 { |
1358
|
708 // XXX FIXME XXX -- if we have already seen EOF on a |
3531
|
709 // previous call, how do we fix up the state of std::cin so |
|
710 // that we can get additional input? I'm afraid that we |
|
711 // can't fix this using std::cin only. |
|
712 |
|
713 retval = do_load (std::cin, orig_fname, force, format, flt_fmt, |
4687
|
714 list_only, swap, verbose, argv, i, argc, |
863
|
715 nargout); |
|
716 } |
|
717 else |
|
718 error ("load: must specify file format if reading from stdin"); |
|
719 } |
|
720 else |
|
721 { |
3523
|
722 std::string fname = file_ops::tilde_expand (argv[i]); |
863
|
723 |
|
724 if (format == LS_UNKNOWN) |
|
725 format = get_file_format (fname, orig_fname); |
|
726 |
3687
|
727 #ifdef HAVE_HDF5 |
|
728 if (format == LS_HDF5) |
|
729 { |
|
730 i++; |
|
731 |
4844
|
732 // If the file doesn't exist do nothing |
|
733 std::ifstream file (fname.c_str (), std::ios::in); |
|
734 if (file) |
3687
|
735 { |
4844
|
736 file.close (); |
|
737 |
|
738 hdf5_ifstream hdf5_file (fname.c_str ()); |
3687
|
739 |
4844
|
740 if (hdf5_file.file_id >= 0) |
|
741 { |
|
742 retval = do_load (hdf5_file, orig_fname, force, format, |
|
743 flt_fmt, list_only, swap, verbose, |
|
744 argv, i, argc, nargout); |
|
745 |
|
746 hdf5_file.close (); |
|
747 } |
|
748 else |
|
749 error ("load: couldn't open input file `%s'", |
|
750 orig_fname.c_str ()); |
3687
|
751 } |
|
752 } |
|
753 else |
|
754 #endif /* HAVE_HDF5 */ |
|
755 // don't insert any statements here; the "else" above has to |
|
756 // go with the "if" below!!!!! |
863
|
757 if (format != LS_UNKNOWN) |
|
758 { |
1755
|
759 i++; |
863
|
760 |
3775
|
761 std::ios::openmode mode = std::ios::in; |
4791
|
762 |
|
763 if (format == LS_BINARY |
|
764 #ifdef HAVE_HDF5 |
|
765 || format == LS_HDF5 |
|
766 #endif |
|
767 || format == LS_MAT_BINARY |
|
768 || format == LS_MAT5_BINARY) |
3552
|
769 mode |= std::ios::binary; |
863
|
770 |
3523
|
771 std::ifstream file (fname.c_str (), mode); |
863
|
772 |
|
773 if (file) |
|
774 { |
|
775 if (format == LS_BINARY) |
|
776 { |
|
777 if (read_binary_file_header (file, swap, flt_fmt) < 0) |
|
778 { |
|
779 file.close (); |
|
780 return retval; |
|
781 } |
|
782 } |
3688
|
783 else if (format == LS_MAT5_BINARY) |
|
784 { |
|
785 if (read_mat5_binary_file_header (file, swap, false) < 0) |
|
786 { |
|
787 file.close (); |
|
788 return retval; |
|
789 } |
|
790 } |
863
|
791 |
|
792 retval = do_load (file, orig_fname, force, format, |
4687
|
793 flt_fmt, list_only, swap, verbose, |
1755
|
794 argv, i, argc, nargout); |
863
|
795 file.close (); |
|
796 } |
|
797 else |
1755
|
798 error ("load: couldn't open input file `%s'", |
|
799 orig_fname.c_str ()); |
863
|
800 } |
|
801 } |
604
|
802 |
|
803 return retval; |
|
804 } |
|
805 |
3019
|
806 // Return TRUE if PATTERN has any special globbing chars in it. |
|
807 |
|
808 static bool |
3523
|
809 glob_pattern_p (const std::string& pattern) |
604
|
810 { |
|
811 int open = 0; |
|
812 |
1755
|
813 int len = pattern.length (); |
|
814 |
|
815 for (int i = 0; i < len; i++) |
604
|
816 { |
1755
|
817 char c = pattern[i]; |
|
818 |
604
|
819 switch (c) |
|
820 { |
|
821 case '?': |
|
822 case '*': |
3019
|
823 return true; |
604
|
824 |
|
825 case '[': // Only accept an open brace if there is a close |
|
826 open++; // brace to match it. Bracket expressions must be |
|
827 continue; // complete, according to Posix.2 |
|
828 |
|
829 case ']': |
|
830 if (open) |
3019
|
831 return true; |
604
|
832 continue; |
4402
|
833 |
604
|
834 case '\\': |
1755
|
835 if (i == len - 1) |
3019
|
836 return false; |
604
|
837 |
|
838 default: |
|
839 continue; |
|
840 } |
|
841 } |
|
842 |
3019
|
843 return false; |
604
|
844 } |
|
845 |
4791
|
846 static void |
|
847 do_save (std::ostream& os, const octave_value& tc, |
|
848 const std::string& name, const std::string& help, |
|
849 int global, load_save_format fmt, bool save_as_floats, |
|
850 bool& infnan_warned) |
604
|
851 { |
|
852 switch (fmt) |
|
853 { |
|
854 case LS_ASCII: |
3738
|
855 save_ascii_data (os, tc, name, infnan_warned, false, global, 0); |
604
|
856 break; |
|
857 |
|
858 case LS_BINARY: |
630
|
859 save_binary_data (os, tc, name, help, global, save_as_floats); |
604
|
860 break; |
|
861 |
667
|
862 case LS_MAT_BINARY: |
|
863 save_mat_binary_data (os, tc, name); |
|
864 break; |
|
865 |
3687
|
866 #ifdef HAVE_HDF5 |
|
867 case LS_HDF5: |
|
868 save_hdf5_data (os, tc, name, help, global, save_as_floats); |
|
869 break; |
|
870 #endif /* HAVE_HDF5 */ |
|
871 |
3688
|
872 case LS_MAT5_BINARY: |
|
873 save_mat5_binary_element (os, tc, name, global, save_as_floats); |
|
874 break; |
|
875 |
604
|
876 default: |
775
|
877 gripe_unrecognized_data_fmt ("save"); |
604
|
878 break; |
|
879 } |
|
880 } |
|
881 |
4791
|
882 // Save the info from SR on stream OS in the format specified by FMT. |
|
883 |
|
884 void |
|
885 do_save (std::ostream& os, symbol_record *sr, load_save_format fmt, |
|
886 bool save_as_floats, bool& infnan_warned) |
|
887 { |
|
888 if (! sr->is_variable ()) |
|
889 { |
|
890 error ("save: can only save variables, not functions"); |
|
891 return; |
|
892 } |
|
893 |
|
894 octave_value tc = sr->def (); |
|
895 |
|
896 if (tc.is_defined ()) |
|
897 { |
|
898 std::string name = sr->name (); |
|
899 std::string help = sr->help (); |
|
900 |
|
901 int global = sr->is_linked_to_global (); |
|
902 |
|
903 do_save (os, tc, name, help, global, fmt, save_as_floats, |
|
904 infnan_warned); |
|
905 } |
|
906 } |
|
907 |
604
|
908 // Save variables with names matching PATTERN on stream OS in the |
3019
|
909 // format specified by FMT. If SAVE_BUILTINS is TRUE, also save |
604
|
910 // builtin variables with names that match PATTERN. |
|
911 |
|
912 static int |
3523
|
913 save_vars (std::ostream& os, const std::string& pattern, bool save_builtins, |
4791
|
914 load_save_format fmt, bool save_as_floats) |
604
|
915 { |
3355
|
916 Array<symbol_record *> vars = curr_sym_tab->glob |
|
917 (pattern, symbol_record::USER_VARIABLE, SYMTAB_ALL_SCOPES); |
|
918 |
|
919 int saved = vars.length (); |
|
920 |
3738
|
921 bool infnan_warned = false; |
|
922 |
3355
|
923 for (int i = 0; i < saved; i++) |
620
|
924 { |
4791
|
925 do_save (os, vars(i), fmt, save_as_floats, infnan_warned); |
620
|
926 |
|
927 if (error_state) |
|
928 break; |
|
929 } |
604
|
930 |
620
|
931 if (! error_state && save_builtins) |
604
|
932 { |
4009
|
933 vars = fbi_sym_tab->glob |
3355
|
934 (pattern, symbol_record::BUILTIN_VARIABLE, SYMTAB_ALL_SCOPES); |
|
935 |
|
936 int count = vars.length (); |
604
|
937 |
|
938 saved += count; |
|
939 |
3355
|
940 for (int i = 0; i < count; i++) |
620
|
941 { |
4791
|
942 do_save (os, vars(i), fmt, save_as_floats, infnan_warned); |
620
|
943 |
|
944 if (error_state) |
|
945 break; |
|
946 } |
604
|
947 } |
|
948 |
|
949 return saved; |
|
950 } |
|
951 |
|
952 static load_save_format |
4788
|
953 get_save_format (const std::string& fmt, |
|
954 load_save_format fallback_format = LS_ASCII) |
604
|
955 { |
4788
|
956 load_save_format retval = fallback_format; |
1755
|
957 |
|
958 if (fmt == "binary") |
604
|
959 retval = LS_BINARY; |
1755
|
960 else if (fmt == "mat-binary" || fmt =="mat_binary") |
3688
|
961 retval = LS_MAT5_BINARY; |
|
962 else if (fmt == "mat4-binary" || fmt =="mat4_binary") |
911
|
963 retval = LS_MAT_BINARY; |
3687
|
964 #ifdef HAVE_HDF5 |
|
965 else if (fmt == "hdf5") |
|
966 retval = LS_HDF5; |
|
967 #endif /* HAVE_HDF5 */ |
604
|
968 |
|
969 return retval; |
|
970 } |
|
971 |
4329
|
972 void |
3523
|
973 write_header (std::ostream& os, load_save_format format) |
863
|
974 { |
3185
|
975 switch (format) |
863
|
976 { |
3185
|
977 case LS_BINARY: |
|
978 { |
|
979 os << (oct_mach_info::words_big_endian () |
|
980 ? "Octave-1-B" : "Octave-1-L"); |
|
981 |
|
982 oct_mach_info::float_format flt_fmt = |
|
983 oct_mach_info::native_float_format (); |
|
984 |
|
985 char tmp = (char) float_format_to_mopt_digit (flt_fmt); |
|
986 |
3557
|
987 os.write (X_CAST (char *, &tmp), 1); |
3185
|
988 } |
3688
|
989 break; |
|
990 |
|
991 case LS_MAT5_BINARY: |
|
992 { |
3775
|
993 char const * versionmagic; |
3688
|
994 TWO_BYTE_INT number = *(TWO_BYTE_INT *)"\x00\x01"; |
|
995 struct tm bdt; |
|
996 time_t now; |
|
997 char headertext[128]; |
|
998 |
|
999 time (&now); |
|
1000 bdt = *gmtime (&now); |
|
1001 memset (headertext, ' ', 124); |
|
1002 // ISO 8601 format date |
|
1003 strftime (headertext, 124, "MATLAB 5.0 MAT-file, written by Octave " |
|
1004 OCTAVE_VERSION ", %Y-%m-%d %T UTC", &bdt); |
|
1005 |
|
1006 // The first pair of bytes give the version of the MAT file |
|
1007 // format. The second pair of bytes form a magic number which |
|
1008 // signals a MAT file. MAT file data are always written in |
|
1009 // native byte order. The order of the bytes in the second |
|
1010 // pair indicates whether the file was written by a big- or |
|
1011 // little-endian machine. However, the version number is |
|
1012 // written in the *opposite* byte order from everything else! |
|
1013 if (number == 1) |
|
1014 versionmagic = "\x01\x00\x4d\x49"; // this machine is big endian |
|
1015 else |
|
1016 versionmagic = "\x00\x01\x49\x4d"; // this machine is little endian |
|
1017 |
|
1018 memcpy (headertext+124, versionmagic, 4); |
|
1019 os.write (headertext, 128); |
|
1020 } |
|
1021 |
|
1022 break; |
3185
|
1023 |
3687
|
1024 #ifdef HAVE_HDF5 |
|
1025 case LS_HDF5: |
|
1026 #endif /* HAVE_HDF5 */ |
3185
|
1027 case LS_ASCII: |
|
1028 { |
3709
|
1029 octave_localtime now; |
|
1030 |
|
1031 std::string comment_string = now.strftime (Vsave_header_format_string); |
|
1032 |
|
1033 if (! comment_string.empty ()) |
|
1034 { |
3687
|
1035 #ifdef HAVE_HDF5 |
3709
|
1036 if (format == LS_HDF5) |
|
1037 { |
|
1038 hdf5_ofstream& hs = (hdf5_ofstream&) os; |
|
1039 H5Gset_comment (hs.file_id, "/", comment_string.c_str ()); |
|
1040 } |
|
1041 else |
3687
|
1042 #endif /* HAVE_HDF5 */ |
3709
|
1043 os << comment_string << "\n"; |
3687
|
1044 } |
3185
|
1045 } |
|
1046 break; |
|
1047 |
|
1048 default: |
|
1049 break; |
863
|
1050 } |
|
1051 } |
|
1052 |
|
1053 static void |
3769
|
1054 save_vars (const string_vector& argv, int argv_idx, int argc, |
3523
|
1055 std::ostream& os, bool save_builtins, load_save_format fmt, |
3185
|
1056 bool save_as_floats, bool write_header_info) |
863
|
1057 { |
3185
|
1058 if (write_header_info) |
|
1059 write_header (os, fmt); |
863
|
1060 |
1755
|
1061 if (argv_idx == argc) |
863
|
1062 { |
|
1063 save_vars (os, "*", save_builtins, fmt, save_as_floats); |
|
1064 } |
|
1065 else |
|
1066 { |
1755
|
1067 for (int i = argv_idx; i < argc; i++) |
863
|
1068 { |
1755
|
1069 if (! save_vars (os, argv[i], save_builtins, fmt, save_as_floats)) |
863
|
1070 { |
1755
|
1071 warning ("save: no such variable `%s'", argv[i].c_str ()); |
863
|
1072 } |
|
1073 } |
|
1074 } |
|
1075 } |
|
1076 |
1380
|
1077 void |
4791
|
1078 dump_octave_core (std::ostream& os, const char *fname, load_save_format fmt) |
|
1079 { |
|
1080 write_header (os, fmt); |
|
1081 |
|
1082 Array<symbol_record *> vars = curr_sym_tab->glob |
|
1083 ("*", symbol_record::USER_VARIABLE, SYMTAB_ALL_SCOPES); |
|
1084 |
|
1085 int num_to_save = vars.length (); |
|
1086 |
|
1087 bool infnan_warned = false; |
|
1088 |
|
1089 double save_mem_size = 0; |
|
1090 |
|
1091 for (int i = 0; i < num_to_save; i++) |
|
1092 { |
|
1093 symbol_record *sr = vars(i); |
|
1094 |
|
1095 if (sr->is_variable ()) |
|
1096 { |
|
1097 octave_value tc = sr->def (); |
|
1098 |
|
1099 if (tc.is_defined ()) |
|
1100 { |
|
1101 double tc_size = tc.byte_size () / 1024; |
|
1102 |
|
1103 // XXX FIXME XXX -- maybe we should try to throw out hte |
|
1104 // largest first... |
|
1105 |
|
1106 if (Voctave_core_file_limit < 0 |
|
1107 || save_mem_size + tc_size < Voctave_core_file_limit) |
|
1108 { |
|
1109 save_mem_size += tc_size; |
|
1110 |
|
1111 std::string name = sr->name (); |
|
1112 std::string help = sr->help (); |
|
1113 |
|
1114 int global = sr->is_linked_to_global (); |
|
1115 |
|
1116 do_save (os, tc, name, help, global, fmt, false, |
|
1117 infnan_warned); |
|
1118 |
|
1119 if (error_state) |
|
1120 break; |
|
1121 } |
|
1122 } |
|
1123 } |
|
1124 } |
|
1125 |
|
1126 message (0, "save to `%s' complete", fname); |
|
1127 } |
|
1128 |
|
1129 void |
|
1130 dump_octave_core (void) |
1380
|
1131 { |
3189
|
1132 if (Vcrash_dumps_octave_core) |
1380
|
1133 { |
3189
|
1134 // XXX FIXME XXX -- should choose better file name? |
|
1135 |
4791
|
1136 const char *fname = Voctave_core_file_name.c_str (); |
3189
|
1137 |
|
1138 message (0, "attempting to save variables to `%s'...", fname); |
|
1139 |
4788
|
1140 load_save_format format |
4791
|
1141 = get_save_format (Voctave_core_file_format, LS_BINARY); |
3189
|
1142 |
3775
|
1143 std::ios::openmode mode = std::ios::out|std::ios::trunc; |
4791
|
1144 |
|
1145 if (format == LS_BINARY |
|
1146 #ifdef HAVE_HDF5 |
|
1147 || format == LS_HDF5 |
|
1148 #endif |
|
1149 || format == LS_MAT_BINARY |
|
1150 || format == LS_MAT5_BINARY) |
3552
|
1151 mode |= std::ios::binary; |
3189
|
1152 |
3687
|
1153 #ifdef HAVE_HDF5 |
|
1154 if (format == LS_HDF5) |
3189
|
1155 { |
3687
|
1156 hdf5_ofstream file (fname); |
|
1157 |
|
1158 if (file.file_id >= 0) |
|
1159 { |
4791
|
1160 dump_octave_core (file, fname, format); |
3687
|
1161 |
|
1162 file.close (); |
|
1163 } |
|
1164 else |
|
1165 warning ("unable to open `%s' for writing...", fname); |
3189
|
1166 } |
|
1167 else |
3687
|
1168 #endif /* HAVE_HDF5 */ |
|
1169 // don't insert any commands here! The open brace below must |
|
1170 // go with the else above! |
|
1171 { |
|
1172 std::ofstream file (fname, mode); |
|
1173 |
|
1174 if (file) |
|
1175 { |
4791
|
1176 dump_octave_core (file, fname, format); |
|
1177 |
3687
|
1178 file.close (); |
|
1179 } |
|
1180 else |
|
1181 warning ("unable to open `%s' for writing...", fname); |
|
1182 } |
1380
|
1183 } |
|
1184 } |
|
1185 |
4208
|
1186 DEFCMD (save, args, , |
3372
|
1187 "-*- texinfo -*-\n\ |
|
1188 @deffn {Command} save options file v1 v2 @dots{}\n\ |
|
1189 Save the named variables @var{v1}, @var{v2}, @dots{} in the file\n\ |
|
1190 @var{file}. The special filename @samp{-} can be used to write the\n\ |
|
1191 output to your terminal. If no variable names are listed, Octave saves\n\ |
|
1192 all the variables in the current scope. Valid options for the\n\ |
|
1193 @code{save} command are listed in the following table. Options that\n\ |
|
1194 modify the output format override the format specified by the built-in\n\ |
|
1195 variable @code{default_save_format}.\n\ |
|
1196 \n\ |
|
1197 @table @code\n\ |
|
1198 @item -ascii\n\ |
|
1199 Save the data in Octave's text data format.\n\ |
|
1200 \n\ |
|
1201 @item -binary\n\ |
|
1202 Save the data in Octave's binary data format.\n\ |
|
1203 \n\ |
|
1204 @item -float-binary\n\ |
|
1205 Save the data in Octave's binary data format but only using single\n\ |
|
1206 precision. You should use this format only if you know that all the\n\ |
|
1207 values to be saved can be represented in single precision.\n\ |
|
1208 \n\ |
|
1209 @item -mat-binary\n\ |
|
1210 Save the data in @sc{Matlab}'s binary data format.\n\ |
|
1211 \n\ |
3688
|
1212 @item -mat4-binary\n\ |
|
1213 Save the data in the binary format written by @sc{Matlab} version 4.\n\ |
|
1214 \n\ |
3687
|
1215 @item -hdf5\n\ |
|
1216 Save the data in HDF5 format.\n\ |
|
1217 (HDF5 is a free, portable binary format developed by the National\n\ |
|
1218 Center for Supercomputing Applications at the University of Illinois.)\n" |
|
1219 |
|
1220 HAVE_HDF5_HELP_STRING |
|
1221 |
|
1222 "\n\ |
|
1223 @item -float-hdf5\n\ |
|
1224 Save the data in HDF5 format but only using single precision.\n\ |
|
1225 You should use this format only if you know that all the\n\ |
|
1226 values to be saved can be represented in single precision.\n\ |
|
1227 \n\ |
3372
|
1228 @item -save-builtins\n\ |
|
1229 Force Octave to save the values of built-in variables too. By default,\n\ |
|
1230 Octave does not save built-in variables.\n\ |
|
1231 @end table\n\ |
604
|
1232 \n\ |
3372
|
1233 The list of variables to save may include wildcard patterns containing\n\ |
|
1234 the following special characters:\n\ |
|
1235 @table @code\n\ |
|
1236 @item ?\n\ |
|
1237 Match any single character.\n\ |
|
1238 \n\ |
|
1239 @item *\n\ |
|
1240 Match zero or more characters.\n\ |
|
1241 \n\ |
|
1242 @item [ @var{list} ]\n\ |
|
1243 Match the list of characters specified by @var{list}. If the first\n\ |
|
1244 character is @code{!} or @code{^}, match all characters except those\n\ |
|
1245 specified by @var{list}. For example, the pattern @samp{[a-zA-Z]} will\n\ |
|
1246 match all lower and upper case alphabetic characters. \n\ |
|
1247 @end table\n\ |
|
1248 \n\ |
|
1249 Except when using the @sc{Matlab} binary data file format, saving global\n\ |
|
1250 variables also saves the global status of the variable, so that if it is\n\ |
|
1251 restored at a later time using @samp{load}, it will be restored as a\n\ |
|
1252 global variable.\n\ |
|
1253 \n\ |
|
1254 The command\n\ |
|
1255 \n\ |
|
1256 @example\n\ |
|
1257 save -binary data a b*\n\ |
|
1258 @end example\n\ |
|
1259 \n\ |
|
1260 @noindent\n\ |
|
1261 saves the variable @samp{a} and all variables beginning with @samp{b} to\n\ |
|
1262 the file @file{data} in Octave's binary format.\n\ |
|
1263 @end deffn") |
604
|
1264 { |
2086
|
1265 octave_value_list retval; |
604
|
1266 |
1755
|
1267 int argc = args.length () + 1; |
|
1268 |
1968
|
1269 string_vector argv = args.make_argv ("save"); |
1755
|
1270 |
|
1271 if (error_state) |
|
1272 return retval; |
604
|
1273 |
1358
|
1274 // Here is where we would get the default save format if it were |
|
1275 // stored in a user preference variable. |
604
|
1276 |
3019
|
1277 bool save_builtins = false; |
|
1278 |
|
1279 bool save_as_floats = false; |
630
|
1280 |
4788
|
1281 load_save_format format = get_save_format (Vdefault_save_format); |
604
|
1282 |
3185
|
1283 bool append = false; |
|
1284 |
1755
|
1285 int i; |
|
1286 for (i = 1; i < argc; i++) |
604
|
1287 { |
3185
|
1288 if (argv[i] == "-append") |
|
1289 { |
|
1290 append = true; |
|
1291 } |
3465
|
1292 else if (argv[i] == "-ascii" || argv[i] == "-a") |
604
|
1293 { |
|
1294 format = LS_ASCII; |
|
1295 } |
1755
|
1296 else if (argv[i] == "-binary" || argv[i] == "-b") |
604
|
1297 { |
|
1298 format = LS_BINARY; |
|
1299 } |
3687
|
1300 else if (argv[i] == "-hdf5" || argv[i] == "-h") |
|
1301 { |
|
1302 #ifdef HAVE_HDF5 |
|
1303 format = LS_HDF5; |
|
1304 #else /* ! HAVE_HDF5 */ |
|
1305 error ("save: octave executable was not linked with HDF5 library"); |
|
1306 return retval; |
|
1307 #endif /* ! HAVE_HDF5 */ |
|
1308 } |
1755
|
1309 else if (argv[i] == "-mat-binary" || argv[i] == "-m") |
667
|
1310 { |
3688
|
1311 format = LS_MAT5_BINARY; |
|
1312 } |
3797
|
1313 else if (argv[i] == "-mat4-binary" || argv[i] == "-4" || argv[i] == "-v4") |
3688
|
1314 { |
667
|
1315 format = LS_MAT_BINARY; |
|
1316 } |
1755
|
1317 else if (argv[i] == "-float-binary" || argv[i] == "-f") |
630
|
1318 { |
|
1319 format = LS_BINARY; |
3019
|
1320 save_as_floats = true; |
630
|
1321 } |
3687
|
1322 else if (argv[i] == "-float-hdf5") |
|
1323 { |
|
1324 #ifdef HAVE_HDF5 |
|
1325 format = LS_HDF5; |
|
1326 save_as_floats = true; |
|
1327 #else /* ! HAVE_HDF5 */ |
|
1328 error ("save: octave executable was not linked with HDF5 library"); |
|
1329 return retval; |
|
1330 #endif /* ! HAVE_HDF5 */ |
|
1331 } |
1755
|
1332 else if (argv[i] == "-save-builtins") |
604
|
1333 { |
3019
|
1334 save_builtins = true; |
604
|
1335 } |
|
1336 else |
|
1337 break; |
|
1338 } |
|
1339 |
2057
|
1340 if (i == argc) |
604
|
1341 { |
|
1342 print_usage ("save"); |
|
1343 return retval; |
|
1344 } |
|
1345 |
630
|
1346 if (save_as_floats && format == LS_ASCII) |
|
1347 { |
|
1348 error ("save: cannot specify both -ascii and -float-binary"); |
|
1349 return retval; |
|
1350 } |
|
1351 |
1755
|
1352 if (argv[i] == "-") |
604
|
1353 { |
1755
|
1354 i++; |
863
|
1355 |
3687
|
1356 #ifdef HAVE_HDF5 |
|
1357 if (format == LS_HDF5) |
4687
|
1358 error ("save: cannot write HDF5 format to stdout"); |
3687
|
1359 else |
|
1360 #endif /* HAVE_HDF5 */ |
|
1361 // don't insert any commands here! the brace below must go |
|
1362 // with the "else" above! |
|
1363 { |
|
1364 // XXX FIXME XXX -- should things intended for the screen end up |
|
1365 // in a octave_value (string)? |
|
1366 |
|
1367 save_vars (argv, i, argc, octave_stdout, save_builtins, format, |
|
1368 save_as_floats, true); |
|
1369 } |
604
|
1370 } |
1755
|
1371 |
|
1372 // Guard against things like `save a*', which are probably mistakes... |
|
1373 |
|
1374 else if (i == argc - 1 && glob_pattern_p (argv[i])) |
|
1375 { |
|
1376 print_usage ("save"); |
604
|
1377 return retval; |
|
1378 } |
|
1379 else |
|
1380 { |
3523
|
1381 std::string fname = file_ops::tilde_expand (argv[i]); |
1755
|
1382 |
|
1383 i++; |
604
|
1384 |
3775
|
1385 std::ios::openmode mode = std::ios::out; |
4791
|
1386 |
|
1387 if (format == LS_BINARY |
|
1388 #ifdef HAVE_HDF5 |
|
1389 || format == LS_HDF5 |
|
1390 #endif |
|
1391 || format == LS_MAT_BINARY |
|
1392 || format == LS_MAT5_BINARY) |
3552
|
1393 mode |= std::ios::binary; |
3538
|
1394 |
|
1395 mode |= append ? std::ios::ate : std::ios::trunc; |
3185
|
1396 |
3687
|
1397 #ifdef HAVE_HDF5 |
|
1398 if (format == LS_HDF5) |
863
|
1399 { |
3687
|
1400 hdf5_ofstream hdf5_file (fname.c_str ()); |
|
1401 |
4687
|
1402 if (hdf5_file.file_id >= 0) |
|
1403 { |
|
1404 save_vars (argv, i, argc, hdf5_file, save_builtins, format, |
|
1405 save_as_floats, true); |
3687
|
1406 |
4687
|
1407 hdf5_file.close (); |
3687
|
1408 } |
|
1409 else |
|
1410 { |
|
1411 error ("save: couldn't open output file `%s'", fname.c_str ()); |
|
1412 return retval; |
|
1413 } |
863
|
1414 } |
|
1415 else |
3687
|
1416 #endif /* HAVE_HDF5 */ |
|
1417 // don't insert any statements here! The brace below must go |
|
1418 // with the "else" above! |
604
|
1419 { |
3687
|
1420 std::ofstream file (fname.c_str (), mode); |
|
1421 |
|
1422 if (file) |
|
1423 { |
|
1424 bool write_header_info |
3775
|
1425 = ((file.rdbuf ())->pubseekoff (0, std::ios::cur) |
3769
|
1426 == static_cast<std::streampos> (0)); |
3687
|
1427 |
|
1428 save_vars (argv, i, argc, file, save_builtins, format, |
|
1429 save_as_floats, write_header_info); |
|
1430 } |
|
1431 else |
|
1432 { |
|
1433 error ("save: couldn't open output file `%s'", fname.c_str ()); |
|
1434 return retval; |
|
1435 } |
604
|
1436 } |
|
1437 } |
|
1438 |
|
1439 return retval; |
|
1440 } |
|
1441 |
2194
|
1442 static int |
3189
|
1443 crash_dumps_octave_core (void) |
|
1444 { |
|
1445 Vcrash_dumps_octave_core = check_preference ("crash_dumps_octave_core"); |
4449
|
1446 |
3189
|
1447 return 0; |
|
1448 } |
|
1449 |
|
1450 static int |
2194
|
1451 default_save_format (void) |
|
1452 { |
|
1453 int status = 0; |
|
1454 |
3523
|
1455 std::string s = builtin_string_variable ("default_save_format"); |
2194
|
1456 |
|
1457 if (s.empty ()) |
|
1458 { |
|
1459 gripe_invalid_value_specified ("default_save_format"); |
|
1460 status = -1; |
|
1461 } |
|
1462 else |
|
1463 Vdefault_save_format = s; |
|
1464 |
|
1465 return status; |
|
1466 } |
|
1467 |
4788
|
1468 static int |
4791
|
1469 octave_core_file_limit (void) |
|
1470 { |
|
1471 double val; |
|
1472 |
|
1473 if (builtin_real_scalar_variable ("octave_core_file_limit", val)) |
|
1474 { |
|
1475 Voctave_core_file_limit = val; |
|
1476 return 0; |
|
1477 } |
|
1478 else |
|
1479 gripe_invalid_value_specified ("octave_core_file_limit"); |
|
1480 |
|
1481 return -1; |
|
1482 } |
|
1483 |
|
1484 static int |
|
1485 octave_core_file_name (void) |
4788
|
1486 { |
|
1487 int status = 0; |
|
1488 |
4791
|
1489 std::string s = builtin_string_variable ("octave_core_file_name"); |
4788
|
1490 |
|
1491 if (s.empty ()) |
|
1492 { |
4791
|
1493 gripe_invalid_value_specified ("octave_core_file_name"); |
4788
|
1494 status = -1; |
|
1495 } |
|
1496 else |
4791
|
1497 Voctave_core_file_name = s; |
|
1498 |
|
1499 return status; |
|
1500 } |
|
1501 |
|
1502 static int |
|
1503 octave_core_file_format (void) |
|
1504 { |
|
1505 int status = 0; |
|
1506 |
|
1507 std::string s = builtin_string_variable ("octave_core_file_format"); |
|
1508 |
|
1509 if (s.empty ()) |
|
1510 { |
|
1511 gripe_invalid_value_specified ("octave_core_file_format"); |
|
1512 status = -1; |
|
1513 } |
|
1514 else |
|
1515 Voctave_core_file_format = s; |
4788
|
1516 |
|
1517 return status; |
|
1518 } |
|
1519 |
3769
|
1520 static std::string |
3709
|
1521 default_save_header_format (void) |
|
1522 { |
|
1523 return |
4633
|
1524 std::string ("# Created by Octave " OCTAVE_VERSION |
|
1525 ", %a %b %d %H:%M:%S %Y %Z <") |
3709
|
1526 + octave_env::get_user_name () |
|
1527 + std::string ("@") |
|
1528 + octave_env::get_host_name () |
|
1529 + std::string (">"); |
|
1530 } |
|
1531 |
|
1532 static int |
|
1533 save_header_format_string (void) |
|
1534 { |
|
1535 int status = 0; |
|
1536 |
|
1537 octave_value v = builtin_any_variable ("save_header_format_string"); |
|
1538 |
|
1539 if (v.is_string ()) |
|
1540 Vsave_header_format_string = v.string_value (); |
|
1541 else |
|
1542 { |
|
1543 gripe_invalid_value_specified ("save_header_format_string"); |
|
1544 status = -1; |
|
1545 } |
|
1546 |
|
1547 return status; |
|
1548 } |
|
1549 |
2194
|
1550 void |
|
1551 symbols_of_load_save (void) |
|
1552 { |
4233
|
1553 DEFVAR (crash_dumps_octave_core, true, crash_dumps_octave_core, |
3372
|
1554 "-*- texinfo -*-\n\ |
|
1555 @defvr {Built-in Variable} crash_dumps_octave_core\n\ |
|
1556 If this variable is set to a nonzero value, Octave tries to save all\n\ |
|
1557 current variables the the file \"octave-core\" if it crashes or receives a\n\ |
|
1558 hangup, terminate or similar signal. The default value is 1.\n\ |
4791
|
1559 @seealso{octave_core_file_limit, octave_core_file_name, and octave_core_file_format}\n\ |
3372
|
1560 @end defvr"); |
3189
|
1561 |
3258
|
1562 DEFVAR (default_save_format, "ascii", default_save_format, |
3372
|
1563 "-*- texinfo -*-\n\ |
|
1564 @defvr {Built-in Variable} default_save_format\n\ |
|
1565 This variable specifies the default format for the @code{save} command.\n\ |
|
1566 It should have one of the following values: @code{\"ascii\"},\n\ |
|
1567 @code{\"binary\"}, @code{float-binary}, or @code{\"mat-binary\"}. The\n\ |
|
1568 initial default save format is Octave's text format.\n\ |
4791
|
1569 @end defvr"); |
|
1570 |
|
1571 DEFVAR (octave_core_file_limit, -1.0, octave_core_file_limit, |
|
1572 "-*- texinfo -*-\n\ |
|
1573 @defvr {Built-in Variable} octave_core_file_limit\n\ |
|
1574 The maximum amount of memory (in kilobytes) of the top-level workspace\n\ |
|
1575 that Octave will attempt to write when saving data to the\n\ |
|
1576 @var{octave_core_file_name}. If @var{octave_core_file_format} is a\n\ |
|
1577 binary format, then @var{octave_core_file_limit} will be approximately\n\ |
|
1578 the maximum size of the file. If a text file format is used, then the\n\ |
|
1579 file could be much larger than the limit.\n\ |
|
1580 The default value is -1 (unlimited)\n\ |
|
1581 @seealso{crash_dumps_octave_core, octave_core_file_name, and octave_core_file_format}\n\ |
4788
|
1582 @end defvr"); |
|
1583 |
4791
|
1584 DEFVAR (octave_core_file_name, "octave-core", octave_core_file_name, |
4788
|
1585 "-*- texinfo -*-\n\ |
4791
|
1586 @defvr {Built-in Variable} octave_core_file_name\n\ |
|
1587 The name of the file used for saving data from the top-level workspace\n\ |
|
1588 when Octave aborts. The default value is @code{\"octave-core\"}\n\ |
|
1589 @seealso{crash_dumps_octave_core, octave_core_file_name, and octave_core_file_format}\n\ |
|
1590 @end defvr"); |
|
1591 |
|
1592 DEFVAR (octave_core_file_format, "binary", octave_core_file_format, |
|
1593 "-*- texinfo -*-\n\ |
|
1594 @defvr {Built-in Variable} octave_core_file_format\n\ |
4788
|
1595 If Octave aborts, it attempts to save the contents of the top-level\n\ |
|
1596 workspace in a file using this format. The value of\n\ |
4791
|
1597 @code{octave_core_file_format} should have one of the following values:\n\ |
4788
|
1598 @code{\"ascii\"}, @code{\"binary\"}, @code{float-binary}, or\n\ |
|
1599 @code{\"mat-binary\"}. The default value is Octave's binary format.\n\ |
4791
|
1600 @seealso{crash_dumps_octave_core, octave_core_file_name, and octave_core_file_limit}\n\ |
3372
|
1601 @end defvr"); |
2194
|
1602 |
3709
|
1603 DEFVAR (save_header_format_string, default_save_header_format (), |
|
1604 save_header_format_string, |
|
1605 "-*- texinfo -*-\n\ |
|
1606 @defvr {Built-in Variable} save_header_format_string\n\ |
|
1607 This variable specifies the the format string for the comment line\n\ |
|
1608 that is written at the beginning of text-format data files saved by\n\ |
|
1609 Octave. The format string is passed to @code{strftime} and should\n\ |
|
1610 begin with the character @samp{#} and contain no newline characters.\n\ |
|
1611 If the value of @code{save_header_format_string} is the empty string,\n\ |
|
1612 the header comment is omitted from text-format data files. The\n\ |
|
1613 default value is\n\ |
|
1614 \n\ |
|
1615 @example\n\ |
4060
|
1616 \"# Created by Octave VERSION, %a %b %d %H:%M:%S %Y %Z <USER@@HOST>\"\n\ |
3709
|
1617 @end example\n\ |
|
1618 @seealso{strftime}\n\ |
|
1619 @end defvr"); |
2194
|
1620 } |
|
1621 |
604
|
1622 /* |
|
1623 ;;; Local Variables: *** |
|
1624 ;;; mode: C++ *** |
|
1625 ;;; End: *** |
|
1626 */ |