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