Mercurial > octave-nkf
annotate src/load-save.cc @ 8418:679c22082ac7
handle -double option for save
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 24 Dec 2008 13:06:26 -0500 |
parents | 25bc2d31e1bf |
children | 2e777f5135a3 |
rev | line source |
---|---|
6763 | 1 /* |
604 | 2 |
7017 | 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, |
4 2003, 2004, 2005, 2006, 2007 John W. Eaton | |
604 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
604 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
604 | 21 |
22 */ | |
23 | |
3911 | 24 // Author: John W. Eaton. |
25 // HDF5 support by Steven G. Johnson <stevenj@alum.mit.edu> | |
26 // Matlab v5 support by James R. Van Zandt <jrv@vanzandt.mv.com> | |
3687 | 27 |
604 | 28 #ifdef HAVE_CONFIG_H |
1192 | 29 #include <config.h> |
604 | 30 #endif |
31 | |
1343 | 32 #include <cfloat> |
33 #include <cstring> | |
34 #include <cctype> | |
35 | |
4249 | 36 #include <fstream> |
3503 | 37 #include <iomanip> |
38 #include <iostream> | |
5765 | 39 #include <sstream> |
1728 | 40 #include <string> |
41 | |
3687 | 42 #ifdef HAVE_HDF5 |
43 #include <hdf5.h> | |
44 #endif | |
45 | |
1961 | 46 #include "byte-swap.h" |
47 #include "data-conv.h" | |
2926 | 48 #include "file-ops.h" |
6159 | 49 #include "file-stat.h" |
2926 | 50 #include "glob-match.h" |
2890 | 51 #include "lo-mappers.h" |
2318 | 52 #include "mach-info.h" |
3185 | 53 #include "oct-env.h" |
3258 | 54 #include "oct-time.h" |
4171 | 55 #include "quit.h" |
1755 | 56 #include "str-vec.h" |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8110
diff
changeset
|
57 #include "oct-locbuf.h" |
1755 | 58 |
4332 | 59 #include "Cell.h" |
1352 | 60 #include "defun.h" |
604 | 61 #include "error.h" |
777 | 62 #include "gripes.h" |
6159 | 63 #include "load-path.h" |
1352 | 64 #include "load-save.h" |
1750 | 65 #include "oct-obj.h" |
3687 | 66 #include "oct-map.h" |
4332 | 67 #include "ov-cell.h" |
1352 | 68 #include "pager.h" |
1750 | 69 #include "pt-exp.h" |
1352 | 70 #include "symtab.h" |
71 #include "sysdep.h" | |
72 #include "unwind-prot.h" | |
604 | 73 #include "utils.h" |
2371 | 74 #include "variables.h" |
3185 | 75 #include "version.h" |
3688 | 76 #include "dMatrix.h" |
77 | |
4659 | 78 #ifdef HAVE_HDF5 |
4633 | 79 #include "ls-hdf5.h" |
4659 | 80 #endif |
4633 | 81 #include "ls-mat-ascii.h" |
82 #include "ls-mat4.h" | |
83 #include "ls-mat5.h" | |
84 #include "ls-oct-ascii.h" | |
85 #include "ls-oct-binary.h" | |
3688 | 86 |
5269 | 87 #ifdef HAVE_ZLIB |
88 #include "zfstream.h" | |
89 #endif | |
90 | |
3598 | 91 // Write octave-core file if Octave crashes or is killed by a signal. |
5794 | 92 static bool Vcrash_dumps_octave_core = true; |
3189 | 93 |
4791 | 94 // The maximum amount of memory (in kilobytes) that we will attempt to |
95 // write to the Octave core file. | |
5794 | 96 static double Voctave_core_file_limit = -1.0; |
4791 | 97 |
98 // The name of the Octave core file. | |
5794 | 99 static std::string Voctave_core_file_name = "octave-core"; |
4791 | 100 |
3687 | 101 // The default output format. May be one of "binary", "text", |
102 // "mat-binary", or "hdf5". | |
5794 | 103 static std::string Vdefault_save_options = "-text"; |
2194 | 104 |
5284 | 105 // The output format for Octave core files. |
5794 | 106 static std::string Voctave_core_file_options = "-binary"; |
107 | |
108 static std::string | |
109 default_save_header_format (void) | |
110 { | |
111 return | |
112 std::string ("# Created by Octave " OCTAVE_VERSION | |
113 ", %a %b %d %H:%M:%S %Y %Z <") | |
114 + octave_env::get_user_name () | |
115 + std::string ("@") | |
116 + octave_env::get_host_name () | |
117 + std::string (">"); | |
118 } | |
4788 | 119 |
3709 | 120 // The format string for the comment line at the top of text-format |
121 // save files. Passed to strftime. Should begin with `#' and contain | |
122 // no newline characters. | |
5794 | 123 static std::string Vsave_header_format_string = default_save_header_format (); |
3709 | 124 |
5369 | 125 static void |
126 gripe_file_open (const std::string& fcn, const std::string& file) | |
127 { | |
128 if (fcn == "load") | |
129 error ("%s: unable to open input file `%s'", fcn.c_str (), file.c_str ()); | |
130 else if (fcn == "save") | |
131 error ("%s: unable to open output file `%s'", fcn.c_str (), file.c_str ()); | |
132 else | |
133 error ("%s: unable to open file `%s'", fcn.c_str (), file.c_str ()); | |
134 } | |
135 | |
7336 | 136 // Install a variable with name NAME and the value VAL in the |
137 // symbol table. If GLOBAL is TRUE, make the variable global. | |
604 | 138 |
139 static void | |
7336 | 140 install_loaded_variable (const std::string& name, |
4171 | 141 const octave_value& val, |
7336 | 142 bool global, const std::string& /*doc*/) |
604 | 143 { |
144 if (global) | |
145 { | |
7336 | 146 symbol_table::clear (name); |
147 symbol_table::mark_global (name); | |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7718
diff
changeset
|
148 symbol_table::global_varref (name) = val; |
604 | 149 } |
150 else | |
7336 | 151 symbol_table::varref (name) = val; |
604 | 152 } |
153 | |
3019 | 154 // Return TRUE if NAME matches one of the given globbing PATTERNS. |
604 | 155 |
3013 | 156 static bool |
3769 | 157 matches_patterns (const string_vector& patterns, int pat_idx, |
3523 | 158 int num_pat, const std::string& name) |
604 | 159 { |
1755 | 160 for (int i = pat_idx; i < num_pat; i++) |
604 | 161 { |
1792 | 162 glob_match pattern (patterns[i]); |
3013 | 163 |
1792 | 164 if (pattern.match (name)) |
3013 | 165 return true; |
604 | 166 } |
3688 | 167 |
3013 | 168 return false; |
604 | 169 } |
170 | |
4329 | 171 int |
3523 | 172 read_binary_file_header (std::istream& is, bool& swap, |
4329 | 173 oct_mach_info::float_format& flt_fmt, bool quiet) |
604 | 174 { |
3552 | 175 const int magic_len = 10; |
176 char magic[magic_len+1]; | |
5760 | 177 is.read (magic, magic_len); |
604 | 178 magic[magic_len] = '\0'; |
3688 | 179 |
604 | 180 if (strncmp (magic, "Octave-1-L", magic_len) == 0) |
2318 | 181 swap = oct_mach_info::words_big_endian (); |
604 | 182 else if (strncmp (magic, "Octave-1-B", magic_len) == 0) |
2318 | 183 swap = ! oct_mach_info::words_big_endian (); |
604 | 184 else |
185 { | |
186 if (! quiet) | |
5369 | 187 error ("load: unable to read read binary file"); |
604 | 188 return -1; |
189 } | |
190 | |
191 char tmp = 0; | |
5760 | 192 is.read (&tmp, 1); |
604 | 193 |
2318 | 194 flt_fmt = mopt_digit_to_float_format (tmp); |
195 | |
4574 | 196 if (flt_fmt == oct_mach_info::flt_fmt_unknown) |
604 | 197 { |
198 if (! quiet) | |
199 error ("load: unrecognized binary format!"); | |
3688 | 200 |
604 | 201 return -1; |
202 } | |
203 | |
204 return 0; | |
205 } | |
206 | |
5269 | 207 #ifdef HAVE_ZLIB |
208 static bool | |
209 check_gzip_magic (const std::string& fname) | |
210 { | |
211 bool retval = false; | |
212 std::ifstream file (fname.c_str ()); | |
213 OCTAVE_LOCAL_BUFFER (unsigned char, magic, 2); | |
214 | |
5760 | 215 if (file.read (reinterpret_cast<char *> (magic), 2) && magic[0] == 0x1f && |
5269 | 216 magic[1] == 0x8b) |
217 retval = true; | |
218 | |
219 file.close (); | |
220 return retval; | |
221 } | |
222 #endif | |
223 | |
604 | 224 static load_save_format |
6625 | 225 get_file_format (std::istream& file, const std::string& filename) |
604 | 226 { |
227 load_save_format retval = LS_UNKNOWN; | |
228 | |
4574 | 229 oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown; |
604 | 230 |
3019 | 231 bool swap = false; |
232 | |
233 if (read_binary_file_header (file, swap, flt_fmt, true) == 0) | |
604 | 234 retval = LS_BINARY; |
235 else | |
236 { | |
6202 | 237 file.clear (); |
3538 | 238 file.seekg (0, std::ios::beg); |
604 | 239 |
5828 | 240 int32_t mopt, nr, nc, imag, len; |
1180 | 241 |
242 int err = read_mat_file_header (file, swap, mopt, nr, nc, imag, len, 1); | |
243 | |
244 if (! err) | |
604 | 245 retval = LS_MAT_BINARY; |
246 else | |
247 { | |
2511 | 248 file.clear (); |
3538 | 249 file.seekg (0, std::ios::beg); |
604 | 250 |
6625 | 251 err = read_mat5_binary_file_header (file, swap, true, filename); |
3688 | 252 |
253 if (! err) | |
254 { | |
255 file.clear (); | |
256 file.seekg (0, std::ios::beg); | |
257 retval = LS_MAT5_BINARY; | |
258 } | |
259 else | |
260 { | |
261 file.clear (); | |
262 file.seekg (0, std::ios::beg); | |
263 | |
4171 | 264 std::string tmp = extract_keyword (file, "name"); |
265 | |
266 if (! tmp.empty ()) | |
267 retval = LS_ASCII; | |
2511 | 268 } |
604 | 269 } |
270 } | |
271 | |
5269 | 272 return retval; |
273 } | |
5977 | 274 |
5269 | 275 static load_save_format |
276 get_file_format (const std::string& fname, const std::string& orig_fname, | |
277 bool &use_zlib) | |
278 { | |
279 load_save_format retval = LS_UNKNOWN; | |
280 | |
281 #ifdef HAVE_HDF5 | |
282 // check this before we open the file | |
283 if (H5Fis_hdf5 (fname.c_str ()) > 0) | |
284 return LS_HDF5; | |
285 #endif /* HAVE_HDF5 */ | |
604 | 286 |
5269 | 287 std::ifstream file (fname.c_str ()); |
288 use_zlib = false; | |
289 | |
290 if (file) | |
291 { | |
6625 | 292 retval = get_file_format (file, orig_fname); |
5269 | 293 file.close (); |
5977 | 294 |
5383 | 295 #ifdef HAVE_ZLIB |
5269 | 296 if (retval == LS_UNKNOWN && check_gzip_magic (fname)) |
297 { | |
298 gzifstream gzfile (fname.c_str ()); | |
299 use_zlib = true; | |
300 | |
301 if (gzfile) | |
302 { | |
6625 | 303 retval = get_file_format (gzfile, orig_fname); |
5269 | 304 gzfile.close (); |
305 } | |
306 } | |
5977 | 307 #endif |
5269 | 308 |
309 if (retval == LS_UNKNOWN) | |
310 { | |
311 // Try reading the file as numbers only, determining the | |
312 // number of rows and columns from the data. We don't | |
313 // even bother to check to see if the first item in the | |
314 // file is a number, so that get_complete_line() can | |
315 // skip any comments that might appear at the top of the | |
316 // file. | |
317 | |
318 retval = LS_MAT_ASCII; | |
319 } | |
320 } | |
321 else | |
5369 | 322 gripe_file_open ("load", orig_fname); |
604 | 323 |
324 return retval; | |
325 } | |
326 | |
4329 | 327 octave_value |
7336 | 328 do_load (std::istream& stream, const std::string& orig_fname, |
2318 | 329 load_save_format format, oct_mach_info::float_format flt_fmt, |
4687 | 330 bool list_only, bool swap, bool verbose, |
3687 | 331 const string_vector& argv, int argv_idx, int argc, int nargout) |
604 | 332 { |
3727 | 333 octave_value retval; |
334 | |
335 Octave_map retstruct; | |
604 | 336 |
5765 | 337 std::ostringstream output_buf; |
7779
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
338 std::list<std::string> symbol_names; |
4051 | 339 |
5754 | 340 octave_idx_type count = 0; |
4051 | 341 |
604 | 342 for (;;) |
343 { | |
3019 | 344 bool global = false; |
2086 | 345 octave_value tc; |
604 | 346 |
4171 | 347 std::string name; |
348 std::string doc; | |
604 | 349 |
350 switch (format) | |
351 { | |
352 case LS_ASCII: | |
3136 | 353 name = read_ascii_data (stream, orig_fname, global, tc, count); |
604 | 354 break; |
355 | |
356 case LS_BINARY: | |
357 name = read_binary_data (stream, swap, flt_fmt, orig_fname, | |
358 global, tc, doc); | |
359 break; | |
360 | |
2511 | 361 case LS_MAT_ASCII: |
362 name = read_mat_ascii_data (stream, orig_fname, tc); | |
363 break; | |
364 | |
604 | 365 case LS_MAT_BINARY: |
366 name = read_mat_binary_data (stream, orig_fname, tc); | |
367 break; | |
368 | |
3687 | 369 #ifdef HAVE_HDF5 |
370 case LS_HDF5: | |
4687 | 371 name = read_hdf5_data (stream, orig_fname, global, tc, doc); |
3687 | 372 break; |
373 #endif /* HAVE_HDF5 */ | |
374 | |
3688 | 375 case LS_MAT5_BINARY: |
5269 | 376 case LS_MAT7_BINARY: |
3688 | 377 name = read_mat5_binary_element (stream, orig_fname, swap, |
378 global, tc); | |
379 break; | |
380 | |
604 | 381 default: |
775 | 382 gripe_unrecognized_data_fmt ("load"); |
604 | 383 break; |
384 } | |
385 | |
4171 | 386 if (error_state || stream.eof () || name.empty ()) |
387 break; | |
388 else if (! error_state && ! name.empty ()) | |
604 | 389 { |
390 if (tc.is_defined ()) | |
391 { | |
3136 | 392 if (format == LS_MAT_ASCII && argv_idx < argc) |
393 warning ("load: loaded ASCII file `%s' -- ignoring extra args", | |
3687 | 394 orig_fname.c_str ()); |
3136 | 395 |
396 if (format == LS_MAT_ASCII | |
397 || argv_idx == argc | |
1755 | 398 || matches_patterns (argv, argv_idx, argc, name)) |
604 | 399 { |
400 count++; | |
621 | 401 if (list_only) |
402 { | |
403 if (verbose) | |
404 { | |
405 if (count == 1) | |
406 output_buf | |
407 << "type rows cols name\n" | |
408 << "==== ==== ==== ====\n"; | |
409 | |
3013 | 410 output_buf |
3548 | 411 << std::setiosflags (std::ios::left) |
412 << std::setw (16) << tc.type_name () . c_str () | |
413 << std::setiosflags (std::ios::right) | |
414 << std::setw (7) << tc.rows () | |
415 << std::setw (7) << tc.columns () | |
7779
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
416 << " " << name << "\n"; |
621 | 417 } |
7779
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
418 else |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
419 symbol_names.push_back (name); |
621 | 420 } |
421 else | |
422 { | |
3727 | 423 if (nargout == 1) |
424 { | |
425 if (format == LS_MAT_ASCII) | |
426 retval = tc; | |
427 else | |
4675 | 428 retstruct.assign (name, tc); |
3727 | 429 } |
430 else | |
7336 | 431 install_loaded_variable (name, tc, global, doc); |
621 | 432 } |
604 | 433 } |
2511 | 434 |
435 // Only attempt to read one item from a headless text file. | |
436 | |
437 if (format == LS_MAT_ASCII) | |
438 break; | |
604 | 439 } |
440 else | |
4171 | 441 error ("load: unable to load variable `%s'", name.c_str ()); |
604 | 442 } |
443 else | |
444 { | |
445 if (count == 0) | |
446 error ("load: are you sure `%s' is an Octave data file?", | |
1755 | 447 orig_fname.c_str ()); |
604 | 448 |
449 break; | |
450 } | |
451 } | |
452 | |
621 | 453 if (list_only && count) |
454 { | |
7779
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
455 if (verbose) |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
456 { |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
457 std::string msg = output_buf.str (); |
2095 | 458 |
7779
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
459 if (nargout > 0) |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
460 retval = msg; |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
461 else |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
462 octave_stdout << msg; |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
463 } |
621 | 464 else |
7779
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
465 { |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
466 if (nargout > 0) |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
467 retval = Cell (string_vector (symbol_names)); |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
468 else |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
469 { |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
470 string_vector names (symbol_names); |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
471 |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
472 names.list_in_columns (octave_stdout); |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
473 |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
474 octave_stdout << "\n"; |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
475 } |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
476 } |
621 | 477 } |
6639 | 478 else if (retstruct.nfields () != 0) |
3727 | 479 retval = retstruct; |
621 | 480 |
863 | 481 return retval; |
482 } | |
483 | |
6159 | 484 std::string |
485 find_file_to_load (const std::string& name, const std::string& orig_name) | |
486 { | |
487 std::string fname = name; | |
488 | |
6838 | 489 if (! (octave_env::absolute_pathname (fname) |
490 || octave_env::rooted_relative_pathname (fname))) | |
6159 | 491 { |
492 file_stat fs (fname); | |
493 | |
6584 | 494 if (! (fs.exists () && fs.is_reg ())) |
6159 | 495 { |
496 std::string tmp = octave_env::make_absolute | |
497 (load_path::find_file (fname), octave_env::getcwd ()); | |
498 | |
499 if (! tmp.empty ()) | |
500 { | |
501 warning_with_id ("Octave:load-file-in-path", | |
502 "load: file found in load path"); | |
503 fname = tmp; | |
504 } | |
505 } | |
506 } | |
507 | |
6838 | 508 size_t dot_pos = fname.rfind ("."); |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7819
diff
changeset
|
509 size_t sep_pos = fname.find_last_of (file_ops::dir_sep_chars ()); |
6838 | 510 |
8021 | 511 if (dot_pos == std::string::npos |
512 || (sep_pos != std::string::npos && dot_pos < sep_pos)) | |
6159 | 513 { |
6838 | 514 // Either no '.' in name or no '.' appears after last directory |
515 // separator. | |
516 | |
6159 | 517 file_stat fs (fname); |
518 | |
6584 | 519 if (! (fs.exists () && fs.is_reg ())) |
6159 | 520 fname = find_file_to_load (fname + ".mat", orig_name); |
521 } | |
522 else | |
523 { | |
524 file_stat fs (fname); | |
525 | |
6584 | 526 if (! (fs.exists () && fs.is_reg ())) |
6159 | 527 { |
528 fname = ""; | |
529 | |
530 error ("load: unable to find file %s", orig_name.c_str ()); | |
531 } | |
532 } | |
533 | |
534 return fname; | |
535 } | |
536 | |
537 | |
3687 | 538 // HDF5 load/save documentation is included in the Octave manual |
539 // regardless, but if HDF5 is not linked in we also include a | |
540 // sentence noting this, so the user understands that the features | |
541 // aren't available. Define a macro for this sentence: | |
542 | |
543 #ifdef HAVE_HDF5 | |
544 #define HAVE_HDF5_HELP_STRING "" | |
545 #else /* ! HAVE_HDF5 */ | |
546 #define HAVE_HDF5_HELP_STRING "\n\ | |
547 HDF5 load and save are not available, as this Octave executable was\n\ | |
548 not linked with the HDF5 library." | |
549 #endif /* ! HAVE HDF5 */ | |
550 | |
4208 | 551 DEFCMD (load, args, nargout, |
3372 | 552 "-*- texinfo -*-\n\ |
553 @deffn {Command} load options file v1 v2 @dots{}\n\ | |
7247 | 554 Load the named variables @var{v1}, @var{v2}, @dots{}, from the file\n\ |
555 @var{file}. As with @code{save}, you may specify a list of variables\n\ | |
7251 | 556 and @code{load} will only extract those variables with names that\n\ |
7247 | 557 match. For example, to restore the variables saved in the file\n\ |
558 @file{data}, use the command\n\ | |
3372 | 559 \n\ |
560 @example\n\ | |
561 load data\n\ | |
562 @end example\n\ | |
863 | 563 \n\ |
5665 | 564 If load is invoked using the functional form\n\ |
565 \n\ | |
566 @example\n\ | |
7247 | 567 load (\"-option1\", @dots{}, \"file\", \"v1\", @dots{})\n\ |
5665 | 568 @end example\n\ |
569 \n\ | |
570 @noindent\n\ | |
571 then the @var{options}, @var{file}, and variable name arguments\n\ | |
572 (@var{v1}, @dots{}) must be specified as character strings.\n\ | |
573 \n\ | |
3372 | 574 If a variable that is not marked as global is loaded from a file when a\n\ |
575 global symbol with the same name already exists, it is loaded in the\n\ | |
576 global symbol table. Also, if a variable is marked as global in a file\n\ | |
577 and a local symbol exists, the local symbol is moved to the global\n\ | |
578 symbol table and given the value from the file. Since it seems that\n\ | |
579 both of these cases are likely to be the result of some sort of error,\n\ | |
580 they will generate warnings.\n\ | |
863 | 581 \n\ |
3727 | 582 If invoked with a single output argument, Octave returns data instead\n\ |
583 of inserting variables in the symbol table. If the data file contains\n\ | |
584 only numbers (TAB- or space-delimited columns), a matrix of values is\n\ | |
585 returned. Otherwise, @code{load} returns a structure with members\n\ | |
586 corresponding to the names of the variables in the file.\n\ | |
587 \n\ | |
3372 | 588 The @code{load} command can read data stored in Octave's text and\n\ |
8110
fdc7c91835ab
Document load's ability to handle gzip-compressed files.
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8034
diff
changeset
|
589 binary formats, and @sc{Matlab}'s binary format. If compiled with zlib\n\ |
fdc7c91835ab
Document load's ability to handle gzip-compressed files.
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8034
diff
changeset
|
590 support, it can load gzip-compressed files as well. It will automatically\n\ |
3372 | 591 detect the type of file and do conversion from different floating point\n\ |
592 formats (currently only IEEE big and little endian, though other formats\n\ | |
593 may added in the future).\n\ | |
594 \n\ | |
595 Valid options for @code{load} are listed in the following table.\n\ | |
863 | 596 \n\ |
3372 | 597 @table @code\n\ |
598 @item -force\n\ | |
4884 | 599 The @samp{-force} option is accepted but ignored for backward\n\ |
6159 | 600 compatibility. Octave now overwrites variables currently in memory with\n\ |
4884 | 601 the same name as those found in the file.\n\ |
3372 | 602 \n\ |
603 @item -ascii\n\ | |
5938 | 604 Force Octave to assume the file contains columns of numbers in text format\n\ |
605 without any header or other information. Data in the file will be loaded\n\ | |
606 as a single numeric matrix with the name of the variable derived from the\n\ | |
607 name of the file.\n\ | |
5197 | 608 \n\ |
3372 | 609 @item -binary\n\ |
610 Force Octave to assume the file is in Octave's binary format.\n\ | |
611 \n\ | |
4884 | 612 @item -mat\n\ |
613 @itemx -mat-binary\n\ | |
5269 | 614 @itemx -6\n\ |
615 @itemx -v6\n\ | |
616 @itemx -7\n\ | |
617 @itemx -v7\n\ | |
618 Force Octave to assume the file is in @sc{Matlab}'s version 6 or 7 binary\n\ | |
5256 | 619 format.\n\ |
3687 | 620 \n\ |
5256 | 621 @item -V4\n\ |
622 @itemx -v4\n\ | |
623 @itemx -4\n\ | |
624 @itemx -mat4-binary\n\ | |
3688 | 625 Force Octave to assume the file is in the binary format written by\n\ |
626 @sc{Matlab} version 4.\n\ | |
627 \n\ | |
3687 | 628 @item -hdf5\n\ |
629 Force Octave to assume the file is in HDF5 format.\n\ | |
630 (HDF5 is a free, portable binary format developed by the National\n\ | |
631 Center for Supercomputing Applications at the University of Illinois.)\n\ | |
632 Note that Octave can read HDF5 files not created by itself, but may\n\ | |
4687 | 633 skip some datasets in formats that it cannot support.\n" |
3687 | 634 |
635 HAVE_HDF5_HELP_STRING | |
636 | |
637 "\n\ | |
638 @item -import\n\ | |
6159 | 639 The @samp{-import} is accepted but ignored for backward compatibility.\n\ |
4884 | 640 Octave can now support multi-dimensional HDF data and automatically\n\ |
641 modifies variable names if they are invalid Octave identifiers.\n\ | |
3687 | 642 \n\ |
5198 | 643 @item -text\n\ |
5197 | 644 Force Octave to assume the file is in Octave's text format.\n\ |
3372 | 645 @end table\n\ |
646 @end deffn") | |
863 | 647 { |
2086 | 648 octave_value_list retval; |
863 | 649 |
1755 | 650 int argc = args.length () + 1; |
651 | |
1968 | 652 string_vector argv = args.make_argv ("load"); |
1755 | 653 |
654 if (error_state) | |
655 return retval; | |
863 | 656 |
1358 | 657 // It isn't necessary to have the default load format stored in a |
658 // user preference variable since we can determine the type of file | |
659 // as we are reading. | |
863 | 660 |
661 load_save_format format = LS_UNKNOWN; | |
662 | |
3019 | 663 bool list_only = false; |
664 bool verbose = false; | |
863 | 665 |
1755 | 666 int i; |
667 for (i = 1; i < argc; i++) | |
863 | 668 { |
1755 | 669 if (argv[i] == "-force" || argv[i] == "-f") |
863 | 670 { |
4884 | 671 // Silently ignore this |
672 // warning ("load: -force ignored"); | |
863 | 673 } |
1755 | 674 else if (argv[i] == "-list" || argv[i] == "-l") |
863 | 675 { |
3019 | 676 list_only = true; |
863 | 677 } |
1755 | 678 else if (argv[i] == "-verbose" || argv[i] == "-v") |
863 | 679 { |
3019 | 680 verbose = true; |
863 | 681 } |
1755 | 682 else if (argv[i] == "-ascii" || argv[i] == "-a") |
863 | 683 { |
5938 | 684 format = LS_MAT_ASCII; |
863 | 685 } |
1755 | 686 else if (argv[i] == "-binary" || argv[i] == "-b") |
863 | 687 { |
688 format = LS_BINARY; | |
689 } | |
5269 | 690 else if (argv[i] == "-mat-binary" || argv[i] == "-mat" || argv[i] == "-m" |
691 || argv[i] == "-6" || argv[i] == "-v6") | |
863 | 692 { |
3688 | 693 format = LS_MAT5_BINARY; |
694 } | |
7819
b7e8ea6a5143
trivial fix option in Fload
Jaroslav Hajek <highegg@gmail.com>
parents:
7779
diff
changeset
|
695 else if (argv[i] == "-7" || argv[i] == "-v7") |
5269 | 696 { |
697 format = LS_MAT7_BINARY; | |
698 } | |
5256 | 699 else if (argv[i] == "-mat4-binary" || argv[i] == "-V4" |
700 || argv[i] == "-v4" || argv[i] == "-4") | |
3688 | 701 { |
863 | 702 format = LS_MAT_BINARY; |
703 } | |
3687 | 704 else if (argv[i] == "-hdf5" || argv[i] == "-h") |
705 { | |
706 #ifdef HAVE_HDF5 | |
707 format = LS_HDF5; | |
708 #else /* ! HAVE_HDF5 */ | |
709 error ("load: octave executable was not linked with HDF5 library"); | |
710 return retval; | |
711 #endif /* ! HAVE_HDF5 */ | |
712 } | |
713 else if (argv[i] == "-import" || argv[i] == "-i") | |
714 { | |
4687 | 715 warning ("load: -import ignored"); |
3687 | 716 } |
5197 | 717 else if (argv[i] == "-text" || argv[i] == "-t") |
718 { | |
719 format = LS_ASCII; | |
720 } | |
863 | 721 else |
722 break; | |
723 } | |
724 | |
1755 | 725 if (i == argc) |
863 | 726 { |
5823 | 727 print_usage (); |
863 | 728 return retval; |
729 } | |
730 | |
3523 | 731 std::string orig_fname = argv[i]; |
863 | 732 |
4574 | 733 oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown; |
863 | 734 |
3019 | 735 bool swap = false; |
863 | 736 |
1755 | 737 if (argv[i] == "-") |
863 | 738 { |
1755 | 739 i++; |
863 | 740 |
3687 | 741 #ifdef HAVE_HDF5 |
742 if (format == LS_HDF5) | |
743 error ("load: cannot read HDF5 format from stdin"); | |
744 else | |
745 #endif /* HAVE_HDF5 */ | |
863 | 746 if (format != LS_UNKNOWN) |
747 { | |
5775 | 748 // FIXME -- if we have already seen EOF on a |
3531 | 749 // previous call, how do we fix up the state of std::cin so |
750 // that we can get additional input? I'm afraid that we | |
751 // can't fix this using std::cin only. | |
752 | |
7336 | 753 retval = do_load (std::cin, orig_fname, format, flt_fmt, |
4687 | 754 list_only, swap, verbose, argv, i, argc, |
863 | 755 nargout); |
756 } | |
757 else | |
758 error ("load: must specify file format if reading from stdin"); | |
759 } | |
760 else | |
761 { | |
3523 | 762 std::string fname = file_ops::tilde_expand (argv[i]); |
6159 | 763 |
764 fname = find_file_to_load (fname, orig_fname); | |
863 | 765 |
6159 | 766 if (error_state) |
767 return retval; | |
768 | |
769 bool use_zlib = false; | |
5089 | 770 |
863 | 771 if (format == LS_UNKNOWN) |
5269 | 772 format = get_file_format (fname, orig_fname, use_zlib); |
863 | 773 |
3687 | 774 #ifdef HAVE_HDF5 |
775 if (format == LS_HDF5) | |
776 { | |
777 i++; | |
778 | |
5089 | 779 hdf5_ifstream hdf5_file (fname.c_str ()); |
3687 | 780 |
5089 | 781 if (hdf5_file.file_id >= 0) |
782 { | |
7336 | 783 retval = do_load (hdf5_file, orig_fname, format, |
5089 | 784 flt_fmt, list_only, swap, verbose, |
785 argv, i, argc, nargout); | |
4844 | 786 |
5089 | 787 hdf5_file.close (); |
3687 | 788 } |
4845 | 789 else |
5369 | 790 gripe_file_open ("load", orig_fname); |
3687 | 791 } |
792 else | |
793 #endif /* HAVE_HDF5 */ | |
794 // don't insert any statements here; the "else" above has to | |
795 // go with the "if" below!!!!! | |
863 | 796 if (format != LS_UNKNOWN) |
797 { | |
1755 | 798 i++; |
863 | 799 |
3775 | 800 std::ios::openmode mode = std::ios::in; |
4791 | 801 |
802 if (format == LS_BINARY | |
803 #ifdef HAVE_HDF5 | |
804 || format == LS_HDF5 | |
805 #endif | |
806 || format == LS_MAT_BINARY | |
5269 | 807 || format == LS_MAT5_BINARY |
808 || format == LS_MAT7_BINARY) | |
3552 | 809 mode |= std::ios::binary; |
863 | 810 |
5269 | 811 #ifdef HAVE_ZLIB |
812 if (use_zlib) | |
813 { | |
814 gzifstream file (fname.c_str (), mode); | |
863 | 815 |
5269 | 816 if (file) |
863 | 817 { |
5269 | 818 if (format == LS_BINARY) |
819 { | |
820 if (read_binary_file_header (file, swap, flt_fmt) < 0) | |
821 { | |
822 if (file) file.close (); | |
823 return retval; | |
824 } | |
825 } | |
826 else if (format == LS_MAT5_BINARY | |
827 || format == LS_MAT7_BINARY) | |
863 | 828 { |
6625 | 829 if (read_mat5_binary_file_header (file, swap, false, orig_fname) < 0) |
5269 | 830 { |
831 if (file) file.close (); | |
832 return retval; | |
833 } | |
863 | 834 } |
5269 | 835 |
7336 | 836 retval = do_load (file, orig_fname, format, |
5269 | 837 flt_fmt, list_only, swap, verbose, |
838 argv, i, argc, nargout); | |
839 | |
840 file.close (); | |
863 | 841 } |
5269 | 842 else |
5369 | 843 gripe_file_open ("load", orig_fname); |
863 | 844 } |
845 else | |
5269 | 846 #endif |
847 { | |
848 std::ifstream file (fname.c_str (), mode); | |
849 | |
850 if (file) | |
851 { | |
852 if (format == LS_BINARY) | |
853 { | |
854 if (read_binary_file_header (file, swap, flt_fmt) < 0) | |
855 { | |
856 if (file) file.close (); | |
857 return retval; | |
858 } | |
859 } | |
860 else if (format == LS_MAT5_BINARY | |
861 || format == LS_MAT7_BINARY) | |
862 { | |
6625 | 863 if (read_mat5_binary_file_header (file, swap, false, orig_fname) < 0) |
5269 | 864 { |
865 if (file) file.close (); | |
866 return retval; | |
867 } | |
868 } | |
869 | |
7336 | 870 retval = do_load (file, orig_fname, format, |
5269 | 871 flt_fmt, list_only, swap, verbose, |
872 argv, i, argc, nargout); | |
873 | |
874 file.close (); | |
875 } | |
876 else | |
5369 | 877 error ("load: unable open input file `%s'", |
5269 | 878 orig_fname.c_str ()); |
879 } | |
863 | 880 } |
881 } | |
5269 | 882 |
604 | 883 return retval; |
884 } | |
885 | |
3019 | 886 // Return TRUE if PATTERN has any special globbing chars in it. |
887 | |
888 static bool | |
3523 | 889 glob_pattern_p (const std::string& pattern) |
604 | 890 { |
891 int open = 0; | |
892 | |
1755 | 893 int len = pattern.length (); |
894 | |
895 for (int i = 0; i < len; i++) | |
604 | 896 { |
1755 | 897 char c = pattern[i]; |
898 | |
604 | 899 switch (c) |
900 { | |
901 case '?': | |
902 case '*': | |
3019 | 903 return true; |
604 | 904 |
905 case '[': // Only accept an open brace if there is a close | |
906 open++; // brace to match it. Bracket expressions must be | |
907 continue; // complete, according to Posix.2 | |
908 | |
909 case ']': | |
910 if (open) | |
3019 | 911 return true; |
604 | 912 continue; |
4402 | 913 |
604 | 914 case '\\': |
1755 | 915 if (i == len - 1) |
3019 | 916 return false; |
604 | 917 |
918 default: | |
919 continue; | |
920 } | |
921 } | |
922 | |
3019 | 923 return false; |
604 | 924 } |
925 | |
4791 | 926 static void |
927 do_save (std::ostream& os, const octave_value& tc, | |
928 const std::string& name, const std::string& help, | |
7336 | 929 bool global, load_save_format fmt, bool save_as_floats) |
604 | 930 { |
931 switch (fmt) | |
932 { | |
933 case LS_ASCII: | |
6974 | 934 save_ascii_data (os, tc, name, global, 0); |
604 | 935 break; |
936 | |
937 case LS_BINARY: | |
630 | 938 save_binary_data (os, tc, name, help, global, save_as_floats); |
604 | 939 break; |
940 | |
5938 | 941 case LS_MAT_ASCII: |
942 case LS_MAT_ASCII_LONG: | |
943 if (! save_mat_ascii_data (os, tc, fmt == LS_MAT_ASCII ? 8 : 16)) | |
944 warning ("save: unable to save %s in ASCII format", name.c_str ()); | |
945 break; | |
946 | |
667 | 947 case LS_MAT_BINARY: |
948 save_mat_binary_data (os, tc, name); | |
949 break; | |
950 | |
3687 | 951 #ifdef HAVE_HDF5 |
952 case LS_HDF5: | |
953 save_hdf5_data (os, tc, name, help, global, save_as_floats); | |
954 break; | |
955 #endif /* HAVE_HDF5 */ | |
956 | |
3688 | 957 case LS_MAT5_BINARY: |
5269 | 958 save_mat5_binary_element (os, tc, name, global, false, save_as_floats); |
959 break; | |
960 | |
961 case LS_MAT7_BINARY: | |
962 save_mat5_binary_element (os, tc, name, global, true, save_as_floats); | |
3688 | 963 break; |
964 | |
604 | 965 default: |
775 | 966 gripe_unrecognized_data_fmt ("save"); |
604 | 967 break; |
968 } | |
969 } | |
970 | |
4791 | 971 // Save the info from SR on stream OS in the format specified by FMT. |
972 | |
973 void | |
7336 | 974 do_save (std::ostream& os, const symbol_table::symbol_record& sr, |
975 load_save_format fmt, bool save_as_floats) | |
4791 | 976 { |
7336 | 977 octave_value val = sr.varval (); |
4791 | 978 |
7336 | 979 if (val.is_defined ()) |
4791 | 980 { |
7336 | 981 std::string name = sr.name (); |
982 std::string help; | |
983 bool global = sr.is_global (); | |
4791 | 984 |
7336 | 985 do_save (os, val, name, help, global, fmt, save_as_floats); |
4791 | 986 } |
987 } | |
988 | |
7635
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
989 // save fields of a scalar structure STR matching PATTERN on stream OS |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
990 // in the format specified by FMT. |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
991 |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
992 static size_t |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
993 save_fields (std::ostream& os, const Octave_map& m, |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
994 const std::string& pattern, |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
995 load_save_format fmt, bool save_as_floats) |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
996 { |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
997 glob_match pat (pattern); |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
998 |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
999 size_t saved = 0; |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1000 |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1001 for (Octave_map::const_iterator p = m.begin (); p != m.end (); p++) |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1002 { |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1003 std::string empty_str; |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1004 |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1005 if (pat.match(p->first)) |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1006 { |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1007 do_save (os, p->second(0), p->first, empty_str, |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1008 0, fmt, save_as_floats); |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1009 |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1010 saved++; |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1011 } |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1012 } |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1013 |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1014 return saved; |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1015 } |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1016 |
604 | 1017 // Save variables with names matching PATTERN on stream OS in the |
5794 | 1018 // format specified by FMT. |
604 | 1019 |
7336 | 1020 static size_t |
5794 | 1021 save_vars (std::ostream& os, const std::string& pattern, |
4791 | 1022 load_save_format fmt, bool save_as_floats) |
604 | 1023 { |
7336 | 1024 std::list<symbol_table::symbol_record> vars = symbol_table::glob (pattern); |
1025 | |
1026 size_t saved = 0; | |
3355 | 1027 |
7336 | 1028 typedef std::list<symbol_table::symbol_record>::const_iterator const_vars_iterator; |
3355 | 1029 |
7336 | 1030 for (const_vars_iterator p = vars.begin (); p != vars.end (); p++) |
620 | 1031 { |
7336 | 1032 do_save (os, *p, fmt, save_as_floats); |
620 | 1033 |
1034 if (error_state) | |
1035 break; | |
7336 | 1036 |
1037 saved++; | |
620 | 1038 } |
604 | 1039 |
1040 return saved; | |
1041 } | |
1042 | |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1043 static string_vector |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1044 parse_save_options (const string_vector &argv, |
5284 | 1045 load_save_format &format, bool &append, |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1046 bool &save_as_floats, bool &use_zlib) |
604 | 1047 { |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1048 string_vector retval; |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1049 int argc = argv.length (); |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1050 |
8418
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1051 bool do_double = false; |
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1052 |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1053 for (int i = 0; i < argc; i++) |
5284 | 1054 { |
1055 if (argv[i] == "-append") | |
1056 { | |
1057 append = true; | |
1058 } | |
1059 else if (argv[i] == "-ascii" || argv[i] == "-a") | |
1060 { | |
5938 | 1061 format = LS_MAT_ASCII; |
5284 | 1062 } |
8418
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1063 else if (argv[i] == "-double") |
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1064 { |
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1065 do_double = true; |
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1066 } |
5284 | 1067 else if (argv[i] == "-text" || argv[i] == "-t") |
1068 { | |
1069 format = LS_ASCII; | |
1070 } | |
1071 else if (argv[i] == "-binary" || argv[i] == "-b") | |
1072 { | |
1073 format = LS_BINARY; | |
1074 } | |
1075 else if (argv[i] == "-hdf5" || argv[i] == "-h") | |
1076 { | |
3687 | 1077 #ifdef HAVE_HDF5 |
5284 | 1078 format = LS_HDF5; |
1079 #else /* ! HAVE_HDF5 */ | |
1080 error ("save: octave executable was not linked with HDF5 library"); | |
1081 #endif /* ! HAVE_HDF5 */ | |
1082 } | |
1083 else if (argv[i] == "-mat-binary" || argv[i] == "-mat" | |
1084 || argv[i] == "-m" || argv[i] == "-6" || argv[i] == "-v6" | |
1085 || argv[i] == "-V6") | |
1086 { | |
1087 format = LS_MAT5_BINARY; | |
1088 } | |
1089 #ifdef HAVE_ZLIB | |
1090 else if (argv[i] == "-mat7-binary" || argv[i] == "-7" | |
1091 || argv[i] == "-v7" || argv[i] == "-V7") | |
1092 { | |
1093 format = LS_MAT7_BINARY; | |
1094 } | |
1095 #endif | |
1096 else if (argv[i] == "-mat4-binary" || argv[i] == "-V4" | |
1097 || argv[i] == "-v4" || argv[i] == "-4") | |
1098 { | |
1099 format = LS_MAT_BINARY; | |
1100 } | |
1101 else if (argv[i] == "-float-binary" || argv[i] == "-f") | |
1102 { | |
1103 format = LS_BINARY; | |
1104 save_as_floats = true; | |
1105 } | |
1106 else if (argv[i] == "-float-hdf5") | |
1107 { | |
1108 #ifdef HAVE_HDF5 | |
1109 format = LS_HDF5; | |
1110 save_as_floats = true; | |
1111 #else /* ! HAVE_HDF5 */ | |
1112 error ("save: octave executable was not linked with HDF5 library"); | |
1113 #endif /* ! HAVE_HDF5 */ | |
1114 } | |
1115 #ifdef HAVE_ZLIB | |
1116 else if (argv[i] == "-zip" || argv[i] == "-z") | |
1117 { | |
1118 use_zlib = true; | |
1119 } | |
1120 #endif | |
1121 else | |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1122 retval.append (argv[i]); |
5284 | 1123 } |
1124 | |
8418
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1125 if (do_double) |
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1126 { |
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1127 if (format == LS_MAT_ASCII) |
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1128 format = LS_MAT_ASCII_LONG; |
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1129 else |
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1130 warning ("save: \"-double\" option only has an effect with \"-ascii\""); |
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1131 } |
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1132 |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1133 return retval; |
5284 | 1134 } |
1135 | |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1136 static string_vector |
5284 | 1137 parse_save_options (const std::string &arg, load_save_format &format, |
1138 bool &append, bool &save_as_floats, | |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1139 bool &use_zlib) |
5284 | 1140 { |
5765 | 1141 std::istringstream is (arg); |
5284 | 1142 std::string str; |
1143 string_vector argv; | |
1144 | |
5765 | 1145 while (! is.eof ()) |
5284 | 1146 { |
1147 is >> str; | |
1148 argv.append (str); | |
1149 } | |
1150 | |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1151 return parse_save_options (argv, format, append, save_as_floats, |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1152 use_zlib); |
604 | 1153 } |
1154 | |
4329 | 1155 void |
3523 | 1156 write_header (std::ostream& os, load_save_format format) |
863 | 1157 { |
3185 | 1158 switch (format) |
863 | 1159 { |
3185 | 1160 case LS_BINARY: |
1161 { | |
1162 os << (oct_mach_info::words_big_endian () | |
1163 ? "Octave-1-B" : "Octave-1-L"); | |
1164 | |
1165 oct_mach_info::float_format flt_fmt = | |
1166 oct_mach_info::native_float_format (); | |
1167 | |
5760 | 1168 char tmp = static_cast<char> (float_format_to_mopt_digit (flt_fmt)); |
3185 | 1169 |
5760 | 1170 os.write (&tmp, 1); |
3185 | 1171 } |
3688 | 1172 break; |
1173 | |
1174 case LS_MAT5_BINARY: | |
5269 | 1175 case LS_MAT7_BINARY: |
3688 | 1176 { |
3775 | 1177 char const * versionmagic; |
6959 | 1178 int16_t number = *(reinterpret_cast<const int16_t *>("\x00\x01")); |
3688 | 1179 struct tm bdt; |
1180 time_t now; | |
1181 char headertext[128]; | |
1182 | |
1183 time (&now); | |
1184 bdt = *gmtime (&now); | |
1185 memset (headertext, ' ', 124); | |
1186 // ISO 8601 format date | |
1187 strftime (headertext, 124, "MATLAB 5.0 MAT-file, written by Octave " | |
5760 | 1188 OCTAVE_VERSION ", %Y-%m-%d %T UTC", &bdt); |
3688 | 1189 |
1190 // The first pair of bytes give the version of the MAT file | |
1191 // format. The second pair of bytes form a magic number which | |
1192 // signals a MAT file. MAT file data are always written in | |
1193 // native byte order. The order of the bytes in the second | |
1194 // pair indicates whether the file was written by a big- or | |
1195 // little-endian machine. However, the version number is | |
1196 // written in the *opposite* byte order from everything else! | |
1197 if (number == 1) | |
1198 versionmagic = "\x01\x00\x4d\x49"; // this machine is big endian | |
1199 else | |
1200 versionmagic = "\x00\x01\x49\x4d"; // this machine is little endian | |
1201 | |
1202 memcpy (headertext+124, versionmagic, 4); | |
1203 os.write (headertext, 128); | |
1204 } | |
1205 | |
1206 break; | |
3185 | 1207 |
3687 | 1208 #ifdef HAVE_HDF5 |
1209 case LS_HDF5: | |
1210 #endif /* HAVE_HDF5 */ | |
3185 | 1211 case LS_ASCII: |
1212 { | |
3709 | 1213 octave_localtime now; |
1214 | |
1215 std::string comment_string = now.strftime (Vsave_header_format_string); | |
1216 | |
1217 if (! comment_string.empty ()) | |
1218 { | |
3687 | 1219 #ifdef HAVE_HDF5 |
3709 | 1220 if (format == LS_HDF5) |
1221 { | |
5760 | 1222 hdf5_ofstream& hs = dynamic_cast<hdf5_ofstream&> (os); |
3709 | 1223 H5Gset_comment (hs.file_id, "/", comment_string.c_str ()); |
1224 } | |
1225 else | |
3687 | 1226 #endif /* HAVE_HDF5 */ |
3709 | 1227 os << comment_string << "\n"; |
3687 | 1228 } |
3185 | 1229 } |
1230 break; | |
1231 | |
1232 default: | |
1233 break; | |
863 | 1234 } |
1235 } | |
1236 | |
1237 static void | |
3769 | 1238 save_vars (const string_vector& argv, int argv_idx, int argc, |
5794 | 1239 std::ostream& os, load_save_format fmt, |
3185 | 1240 bool save_as_floats, bool write_header_info) |
863 | 1241 { |
3185 | 1242 if (write_header_info) |
1243 write_header (os, fmt); | |
863 | 1244 |
1755 | 1245 if (argv_idx == argc) |
863 | 1246 { |
5794 | 1247 save_vars (os, "*", fmt, save_as_floats); |
863 | 1248 } |
7635
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1249 else if (argv[argv_idx] == "-struct") |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1250 { |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1251 if (++argv_idx >= argc) |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1252 { |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1253 error ("save: missing struct name"); |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1254 return; |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1255 } |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1256 |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1257 std::string struct_name = argv[argv_idx]; |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1258 |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1259 if (! symbol_table::is_variable (struct_name)) |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1260 { |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1261 error ("save: no such variable: `%s'", struct_name.c_str ()); |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1262 return; |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1263 } |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1264 |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1265 octave_value struct_var = symbol_table::varref (struct_name); |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1266 |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1267 if (! struct_var.is_map () || struct_var.numel () != 1) |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1268 { |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1269 error ("save: `%s' is not a scalar structure", |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1270 struct_name.c_str ()); |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1271 return; |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1272 } |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1273 Octave_map struct_var_map = struct_var.map_value (); |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1274 |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1275 ++argv_idx; |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1276 |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1277 if (argv_idx < argc) |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1278 { |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1279 for (int i = argv_idx; i < argc; i++) |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1280 { |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1281 if (! save_fields (os, struct_var_map, argv[i], fmt, |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1282 save_as_floats)) |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1283 { |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1284 warning ("save: no such field `%s.%s'", |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1285 struct_name.c_str (), argv[i].c_str ()); |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1286 } |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1287 } |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1288 } |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1289 else |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1290 save_fields (os, struct_var_map, "*", fmt, save_as_floats); |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1291 } |
863 | 1292 else |
1293 { | |
1755 | 1294 for (int i = argv_idx; i < argc; i++) |
863 | 1295 { |
5794 | 1296 if (! save_vars (os, argv[i], fmt, save_as_floats)) |
7635
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1297 warning ("save: no such variable `%s'", argv[i].c_str ()); |
863 | 1298 } |
1299 } | |
1300 } | |
1301 | |
5284 | 1302 static void |
1303 dump_octave_core (std::ostream& os, const char *fname, load_save_format fmt, | |
1304 bool save_as_floats) | |
4791 | 1305 { |
1306 write_header (os, fmt); | |
1307 | |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
1308 std::list<symbol_table::symbol_record> vars |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
1309 = symbol_table::all_variables (symbol_table::top_scope (), 0); |
4791 | 1310 |
1311 double save_mem_size = 0; | |
1312 | |
7336 | 1313 typedef std::list<symbol_table::symbol_record>::const_iterator const_vars_iterator; |
1314 | |
1315 for (const_vars_iterator p = vars.begin (); p != vars.end (); p++) | |
4791 | 1316 { |
7336 | 1317 octave_value val = p->varval (); |
4791 | 1318 |
7336 | 1319 if (val.is_defined ()) |
4791 | 1320 { |
7336 | 1321 std::string name = p->name (); |
1322 std::string help; | |
1323 bool global = p->is_global (); | |
4791 | 1324 |
7336 | 1325 double val_size = val.byte_size () / 1024; |
4791 | 1326 |
7336 | 1327 // FIXME -- maybe we should try to throw out the largest first... |
4791 | 1328 |
7336 | 1329 if (Voctave_core_file_limit < 0 |
1330 || save_mem_size + val_size < Voctave_core_file_limit) | |
1331 { | |
1332 save_mem_size += val_size; | |
4791 | 1333 |
7336 | 1334 do_save (os, val, name, help, global, fmt, save_as_floats); |
1335 | |
1336 if (error_state) | |
1337 break; | |
4791 | 1338 } |
1339 } | |
1340 } | |
1341 | |
1342 message (0, "save to `%s' complete", fname); | |
1343 } | |
1344 | |
1345 void | |
1346 dump_octave_core (void) | |
1380 | 1347 { |
3189 | 1348 if (Vcrash_dumps_octave_core) |
1380 | 1349 { |
5775 | 1350 // FIXME -- should choose better file name? |
3189 | 1351 |
4791 | 1352 const char *fname = Voctave_core_file_name.c_str (); |
3189 | 1353 |
1354 message (0, "attempting to save variables to `%s'...", fname); | |
1355 | |
5284 | 1356 load_save_format format = LS_BINARY; |
1357 | |
1358 bool save_as_floats = false; | |
1359 | |
1360 bool append = false; | |
3189 | 1361 |
5284 | 1362 bool use_zlib = false; |
1363 | |
1364 parse_save_options (Voctave_core_file_options, format, append, | |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1365 save_as_floats, use_zlib); |
5284 | 1366 |
1367 std::ios::openmode mode = std::ios::out; | |
4791 | 1368 |
6625 | 1369 // Matlab v7 files are always compressed |
1370 if (format == LS_MAT7_BINARY) | |
1371 use_zlib = false; | |
1372 | |
4791 | 1373 if (format == LS_BINARY |
1374 #ifdef HAVE_HDF5 | |
1375 || format == LS_HDF5 | |
1376 #endif | |
1377 || format == LS_MAT_BINARY | |
5269 | 1378 || format == LS_MAT5_BINARY |
1379 || format == LS_MAT7_BINARY) | |
3552 | 1380 mode |= std::ios::binary; |
3189 | 1381 |
5284 | 1382 mode |= append ? std::ios::ate : std::ios::trunc; |
1383 | |
3687 | 1384 #ifdef HAVE_HDF5 |
1385 if (format == LS_HDF5) | |
3189 | 1386 { |
6760 | 1387 hdf5_ofstream file (fname, mode); |
3687 | 1388 |
1389 if (file.file_id >= 0) | |
1390 { | |
5284 | 1391 dump_octave_core (file, fname, format, save_as_floats); |
3687 | 1392 |
1393 file.close (); | |
1394 } | |
1395 else | |
1396 warning ("unable to open `%s' for writing...", fname); | |
3189 | 1397 } |
1398 else | |
3687 | 1399 #endif /* HAVE_HDF5 */ |
1400 // don't insert any commands here! The open brace below must | |
1401 // go with the else above! | |
1402 { | |
5284 | 1403 #ifdef HAVE_ZLIB |
1404 if (use_zlib) | |
3687 | 1405 { |
5284 | 1406 gzofstream file (fname, mode); |
4791 | 1407 |
5284 | 1408 if (file) |
1409 { | |
1410 dump_octave_core (file, fname, format, save_as_floats); | |
1411 | |
1412 file.close (); | |
1413 } | |
1414 else | |
1415 warning ("unable to open `%s' for writing...", fname); | |
3687 | 1416 } |
1417 else | |
5284 | 1418 #endif |
1419 { | |
1420 std::ofstream file (fname, mode); | |
1421 | |
1422 if (file) | |
1423 { | |
1424 dump_octave_core (file, fname, format, save_as_floats); | |
1425 | |
1426 file.close (); | |
1427 } | |
1428 else | |
1429 warning ("unable to open `%s' for writing...", fname); | |
1430 } | |
3687 | 1431 } |
1380 | 1432 } |
1433 } | |
1434 | |
5269 | 1435 #ifdef HAVE_ZLIB |
1436 #define HAVE_ZLIB_HELP_STRING "" | |
1437 #else /* ! HAVE_ZLIB */ | |
1438 #define HAVE_ZLIB_HELP_STRING "\n\ | |
1439 This option is not available, as this Octave executable was not linked with\n\ | |
1440 the zlib library." | |
1441 #endif /* ! HAVE ZLIB */ | |
1442 | |
4208 | 1443 DEFCMD (save, args, , |
3372 | 1444 "-*- texinfo -*-\n\ |
5665 | 1445 @deffn {Command} save options file @var{v1} @var{v2} @dots{}\n\ |
7635
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1446 @deffnx {Command} save options file -struct @var{STR} @var{f1} @var{f2} @dots{}\n\ |
7247 | 1447 Save the named variables @var{v1}, @var{v2}, @dots{}, in the file\n\ |
7635
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1448 @var{file}. The special filename @samp{-} may be used to write the\n\ |
3372 | 1449 output to your terminal. If no variable names are listed, Octave saves\n\ |
7635
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1450 all the variables in the current scope.\n\ |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1451 If the @code{-struct} modifier is used, fields @var{f1} @var{f2} @dots{}\n\ |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1452 of the scalar structure @var{STR} are saved as if they were variables\n\ |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1453 with corresponding names.\n\ |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1454 Valid options for the @code{save} command are listed in the following table.\n\ |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1455 Options that modify the output format override the format specified by \n\ |
5794 | 1456 @code{default_save_options}.\n\ |
3372 | 1457 \n\ |
5665 | 1458 If save is invoked using the functional form\n\ |
1459 \n\ | |
1460 @example\n\ | |
7247 | 1461 save (\"-option1\", @dots{}, \"file\", \"v1\", @dots{})\n\ |
5665 | 1462 @end example\n\ |
1463 \n\ | |
1464 @noindent\n\ | |
1465 then the @var{options}, @var{file}, and variable name arguments\n\ | |
7247 | 1466 (@var{v1}, @dots{}) must be specified as character strings.\n\ |
5665 | 1467 \n\ |
3372 | 1468 @table @code\n\ |
1469 @item -ascii\n\ | |
5938 | 1470 Save a single matrix in a text file.\n\ |
5197 | 1471 \n\ |
3372 | 1472 @item -binary\n\ |
1473 Save the data in Octave's binary data format.\n\ | |
1474 \n\ | |
1475 @item -float-binary\n\ | |
1476 Save the data in Octave's binary data format but only using single\n\ | |
1477 precision. You should use this format only if you know that all the\n\ | |
1478 values to be saved can be represented in single precision.\n\ | |
1479 \n\ | |
5269 | 1480 @item -V7\n\ |
1481 @itemx -v7\n\ | |
1482 @itemx -7\n\ | |
1483 @itemx -mat7-binary\n\ | |
1484 Save the data in @sc{Matlab}'s v7 binary data format.\n" | |
1485 | |
1486 HAVE_ZLIB_HELP_STRING | |
1487 | |
1488 "\n\ | |
1489 @item -V6\n\ | |
5284 | 1490 @itemx -v6\n\ |
5269 | 1491 @itemx -6\n\ |
1492 @itemx -mat\n\ | |
4884 | 1493 @itemx -mat-binary\n\ |
5269 | 1494 Save the data in @sc{Matlab}'s v6 binary data format.\n\ |
3372 | 1495 \n\ |
5256 | 1496 @item -V4\n\ |
1497 @itemx -v4\n\ | |
1498 @itemx -4\n\ | |
1499 @itemx -mat4-binary\n\ | |
3688 | 1500 Save the data in the binary format written by @sc{Matlab} version 4.\n\ |
1501 \n\ | |
3687 | 1502 @item -hdf5\n\ |
1503 Save the data in HDF5 format.\n\ | |
1504 (HDF5 is a free, portable binary format developed by the National\n\ | |
1505 Center for Supercomputing Applications at the University of Illinois.)\n" | |
1506 | |
1507 HAVE_HDF5_HELP_STRING | |
1508 | |
1509 "\n\ | |
1510 @item -float-hdf5\n\ | |
1511 Save the data in HDF5 format but only using single precision.\n\ | |
1512 You should use this format only if you know that all the\n\ | |
1513 values to be saved can be represented in single precision.\n\ | |
1514 \n\ | |
5269 | 1515 @item -zip\n\ |
1516 @itemx -z\n\ | |
1517 Use the gzip algorithm to compress the file. This works equally on files that\n\ | |
1518 are compressed with gzip outside of octave, and gzip can equally be used to\n\ | |
5380 | 1519 convert the files for backward compatibility.\n" |
5322 | 1520 |
1521 HAVE_ZLIB_HELP_STRING | |
1522 | |
5269 | 1523 "@end table\n\ |
604 | 1524 \n\ |
3372 | 1525 The list of variables to save may include wildcard patterns containing\n\ |
1526 the following special characters:\n\ | |
1527 @table @code\n\ | |
1528 @item ?\n\ | |
1529 Match any single character.\n\ | |
1530 \n\ | |
1531 @item *\n\ | |
1532 Match zero or more characters.\n\ | |
1533 \n\ | |
1534 @item [ @var{list} ]\n\ | |
1535 Match the list of characters specified by @var{list}. If the first\n\ | |
1536 character is @code{!} or @code{^}, match all characters except those\n\ | |
1537 specified by @var{list}. For example, the pattern @samp{[a-zA-Z]} will\n\ | |
1538 match all lower and upper case alphabetic characters. \n\ | |
7718
62279ce5654c
save: fix continuation character in doc string
John W. Eaton <jwe@octave.org>
parents:
7635
diff
changeset
|
1539 \n\ |
7635
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1540 Wildcards may also be used in the field names specifications when using\n\ |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1541 the @code{-struct} modifier (but not in the struct name itself).\n\ |
5197 | 1542 \n\ |
5198 | 1543 @item -text\n\ |
5197 | 1544 Save the data in Octave's text data format.\n\ |
3372 | 1545 @end table\n\ |
1546 \n\ | |
1547 Except when using the @sc{Matlab} binary data file format, saving global\n\ | |
1548 variables also saves the global status of the variable, so that if it is\n\ | |
1549 restored at a later time using @samp{load}, it will be restored as a\n\ | |
1550 global variable.\n\ | |
1551 \n\ | |
1552 The command\n\ | |
1553 \n\ | |
1554 @example\n\ | |
1555 save -binary data a b*\n\ | |
1556 @end example\n\ | |
1557 \n\ | |
1558 @noindent\n\ | |
1559 saves the variable @samp{a} and all variables beginning with @samp{b} to\n\ | |
1560 the file @file{data} in Octave's binary format.\n\ | |
1561 @end deffn") | |
604 | 1562 { |
2086 | 1563 octave_value_list retval; |
604 | 1564 |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1565 int argc = args.length (); |
1755 | 1566 |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1567 string_vector argv = args.make_argv (); |
1755 | 1568 |
1569 if (error_state) | |
1570 return retval; | |
604 | 1571 |
1358 | 1572 // Here is where we would get the default save format if it were |
1573 // stored in a user preference variable. | |
604 | 1574 |
3019 | 1575 bool save_as_floats = false; |
630 | 1576 |
5284 | 1577 load_save_format format = LS_ASCII; |
604 | 1578 |
3185 | 1579 bool append = false; |
1580 | |
5269 | 1581 bool use_zlib = false; |
1582 | |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1583 // get default options |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1584 parse_save_options (Vdefault_save_options, format, append, save_as_floats, |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1585 use_zlib); |
5351 | 1586 |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1587 // override from command line |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1588 argv = parse_save_options (argv, format, append, save_as_floats, |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1589 use_zlib); |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1590 argc = argv.length (); |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1591 int i = 0; |
5197 | 1592 |
5284 | 1593 if (error_state) |
1594 return retval; | |
604 | 1595 |
2057 | 1596 if (i == argc) |
604 | 1597 { |
5823 | 1598 print_usage (); |
604 | 1599 return retval; |
1600 } | |
1601 | |
630 | 1602 if (save_as_floats && format == LS_ASCII) |
1603 { | |
1604 error ("save: cannot specify both -ascii and -float-binary"); | |
1605 return retval; | |
1606 } | |
1607 | |
1755 | 1608 if (argv[i] == "-") |
604 | 1609 { |
1755 | 1610 i++; |
863 | 1611 |
3687 | 1612 #ifdef HAVE_HDF5 |
1613 if (format == LS_HDF5) | |
4687 | 1614 error ("save: cannot write HDF5 format to stdout"); |
3687 | 1615 else |
1616 #endif /* HAVE_HDF5 */ | |
1617 // don't insert any commands here! the brace below must go | |
1618 // with the "else" above! | |
1619 { | |
6759 | 1620 if (append) |
1621 warning ("save: ignoring -append option for output to stdout"); | |
1622 | |
5775 | 1623 // FIXME -- should things intended for the screen end up |
3687 | 1624 // in a octave_value (string)? |
1625 | |
5794 | 1626 save_vars (argv, i, argc, octave_stdout, format, |
3687 | 1627 save_as_floats, true); |
1628 } | |
604 | 1629 } |
1755 | 1630 |
1631 // Guard against things like `save a*', which are probably mistakes... | |
1632 | |
1633 else if (i == argc - 1 && glob_pattern_p (argv[i])) | |
1634 { | |
5823 | 1635 print_usage (); |
604 | 1636 return retval; |
1637 } | |
1638 else | |
1639 { | |
3523 | 1640 std::string fname = file_ops::tilde_expand (argv[i]); |
1755 | 1641 |
1642 i++; | |
604 | 1643 |
6625 | 1644 // Matlab v7 files are always compressed |
1645 if (format == LS_MAT7_BINARY) | |
1646 use_zlib = false; | |
1647 | |
6759 | 1648 std::ios::openmode mode |
1649 = append ? (std::ios::app | std::ios::ate) : std::ios::out; | |
1650 | |
4791 | 1651 if (format == LS_BINARY |
1652 #ifdef HAVE_HDF5 | |
1653 || format == LS_HDF5 | |
1654 #endif | |
1655 || format == LS_MAT_BINARY | |
5269 | 1656 || format == LS_MAT5_BINARY |
1657 || format == LS_MAT7_BINARY) | |
3552 | 1658 mode |= std::ios::binary; |
3538 | 1659 |
3687 | 1660 #ifdef HAVE_HDF5 |
1661 if (format == LS_HDF5) | |
863 | 1662 { |
6760 | 1663 // FIXME. It should be possible to append to HDF5 files. |
6759 | 1664 if (append) |
1665 { | |
1666 error ("save: appending to HDF5 files is not implemented"); | |
1667 return retval; | |
1668 } | |
1669 | |
6760 | 1670 bool write_header_info = ! (append && |
1671 H5Fis_hdf5 (fname.c_str ()) > 0); | |
3687 | 1672 |
6760 | 1673 hdf5_ofstream hdf5_file (fname.c_str (), mode); |
1674 | |
1675 if (hdf5_file.file_id != -1) | |
4687 | 1676 { |
5794 | 1677 save_vars (argv, i, argc, hdf5_file, format, |
6760 | 1678 save_as_floats, write_header_info); |
3687 | 1679 |
4687 | 1680 hdf5_file.close (); |
3687 | 1681 } |
1682 else | |
1683 { | |
5369 | 1684 gripe_file_open ("save", fname); |
3687 | 1685 return retval; |
1686 } | |
863 | 1687 } |
1688 else | |
3687 | 1689 #endif /* HAVE_HDF5 */ |
1690 // don't insert any statements here! The brace below must go | |
1691 // with the "else" above! | |
604 | 1692 { |
5269 | 1693 #ifdef HAVE_ZLIB |
1694 if (use_zlib) | |
3687 | 1695 { |
5269 | 1696 gzofstream file (fname.c_str (), mode); |
1697 | |
1698 if (file) | |
1699 { | |
6760 | 1700 bool write_header_info = ! file.tellp (); |
1701 | |
5794 | 1702 save_vars (argv, i, argc, file, format, |
5269 | 1703 save_as_floats, write_header_info); |
1704 | |
1705 file.close (); | |
1706 } | |
1707 else | |
1708 { | |
5369 | 1709 gripe_file_open ("save", fname); |
5269 | 1710 return retval; |
1711 } | |
3687 | 1712 } |
1713 else | |
5269 | 1714 #endif |
3687 | 1715 { |
5269 | 1716 std::ofstream file (fname.c_str (), mode); |
1717 | |
1718 if (file) | |
1719 { | |
6760 | 1720 bool write_header_info = ! file.tellp (); |
1721 | |
5794 | 1722 save_vars (argv, i, argc, file, format, |
5269 | 1723 save_as_floats, write_header_info); |
1724 | |
1725 file.close (); | |
1726 } | |
1727 else | |
1728 { | |
5369 | 1729 gripe_file_open ("save", fname); |
5269 | 1730 return retval; |
1731 } | |
3687 | 1732 } |
604 | 1733 } |
1734 } | |
1735 | |
1736 return retval; | |
1737 } | |
1738 | |
5794 | 1739 DEFUN (crash_dumps_octave_core, args, nargout, |
1740 "-*- texinfo -*-\n\ | |
1741 @deftypefn {Built-in Function} {@var{val} =} crash_dumps_octave_core ()\n\ | |
1742 @deftypefnx {Built-in Function} {@var{old_val} =} crash_dumps_octave_core (@var{new_val})\n\ | |
1743 Query or set the internal variable that controls whether Octave tries\n\ | |
6653 | 1744 to save all current variables to the file \"octave-core\" if it\n\ |
5794 | 1745 crashes or receives a hangup, terminate or similar signal.\n\ |
1746 @seealso{octave_core_file_limit, octave_core_file_name, octave_core_file_options}\n\ | |
1747 @end deftypefn") | |
2194 | 1748 { |
5794 | 1749 return SET_INTERNAL_VARIABLE (crash_dumps_octave_core); |
2194 | 1750 } |
1751 | |
5794 | 1752 DEFUN (default_save_options, args, nargout, |
1753 "-*- texinfo -*-\n\ | |
1754 @deftypefn {Built-in Function} {@var{val} =} default_save_options ()\n\ | |
1755 @deftypefnx {Built-in Function} {@var{old_val} =} default_save_options (@var{new_val})\n\ | |
1756 Query or set the internal variable that specifies the default options\n\ | |
1757 for the @code{save} command, and defines the default format.\n\ | |
1758 Typical values include @code{\"-ascii\"}, @code{\"-ascii -zip\"}.\n\ | |
1759 The default value is @code{-ascii}.\n\ | |
1760 @seealso{save}\n\ | |
1761 @end deftypefn") | |
4788 | 1762 { |
5794 | 1763 return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (default_save_options); |
4788 | 1764 } |
1765 | |
5794 | 1766 DEFUN (octave_core_file_limit, args, nargout, |
1767 "-*- texinfo -*-\n\ | |
1768 @deftypefn {Built-in Function} {@var{val} =} octave_core_file_limit ()\n\ | |
1769 @deftypefnx {Built-in Function} {@var{old_val} =} octave_core_file_limit (@var{new_val})\n\ | |
1770 Query or set the internal variable that specifies the maximum amount\n\ | |
1771 of memory (in kilobytes) of the top-level workspace that Octave will\n\ | |
1772 attempt to save when writing data to the crash dump file (the name of\n\ | |
1773 the file is specified by @var{octave_core_file_name}). If\n\ | |
7001 | 1774 @var{octave_core_file_options} flags specify a binary format,\n\ |
5794 | 1775 then @var{octave_core_file_limit} will be approximately the maximum\n\ |
1776 size of the file. If a text file format is used, then the file could\n\ | |
1777 be much larger than the limit. The default value is -1 (unlimited)\n\ | |
1778 @seealso{crash_dumps_octave_core, octave_core_file_name, octave_core_file_options}\n\ | |
1779 @end deftypefn") | |
3709 | 1780 { |
5794 | 1781 return SET_INTERNAL_VARIABLE (octave_core_file_limit); |
3709 | 1782 } |
1783 | |
5794 | 1784 DEFUN (octave_core_file_name, args, nargout, |
1785 "-*- texinfo -*-\n\ | |
1786 @deftypefn {Built-in Function} {@var{val} =} octave_core_file_name ()\n\ | |
1787 @deftypefnx {Built-in Function} {@var{old_val} =} octave_core_file_name (@var{new_val})\n\ | |
1788 Query or set the internal variable that specifies the name of the file\n\ | |
1789 used for saving data from the top-level workspace if Octave aborts.\n\ | |
1790 The default value is @code{\"octave-core\"}\n\ | |
1791 @seealso{crash_dumps_octave_core, octave_core_file_name, octave_core_file_options}\n\ | |
1792 @end deftypefn") | |
2194 | 1793 { |
5794 | 1794 return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (octave_core_file_name); |
1795 } | |
4791 | 1796 |
5794 | 1797 DEFUN (octave_core_file_options, args, nargout, |
1798 "-*- texinfo -*-\n\ | |
1799 @deftypefn {Built-in Function} {@var{val} =} octave_core_file_options ()\n\ | |
1800 @deftypefnx {Built-in Function} {@var{old_val} =} octave_core_file_options (@var{new_val})\n\ | |
1801 Query or set the internal variable that specifies the options used for\n\ | |
1802 saving the workspace data if Octave aborts. The value of\n\ | |
1803 @code{octave_core_file_options} should follow the same format as the\n\ | |
1804 options for the @code{save} function. The default value is Octave's binary\n\ | |
5284 | 1805 format.\n\ |
5642 | 1806 @seealso{crash_dumps_octave_core, octave_core_file_name, octave_core_file_limit}\n\ |
5794 | 1807 @end deftypefn") |
1808 { | |
1809 return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (octave_core_file_options); | |
1810 } | |
2194 | 1811 |
5794 | 1812 DEFUN (save_header_format_string, args, nargout, |
1813 "-*- texinfo -*-\n\ | |
1814 @deftypefn {Built-in Function} {@var{val} =} save_header_format_string ()\n\ | |
1815 @deftypefnx {Built-in Function} {@var{old_val} =} save_header_format_string (@var{new_val})\n\ | |
1816 Query or set the internal variable that specifies the format\n\ | |
1817 string used for the comment line written at the beginning of\n\ | |
1818 text-format data files saved by Octave. The format string is\n\ | |
1819 passed to @code{strftime} and should begin with the character\n\ | |
1820 @samp{#} and contain no newline characters. If the value of\n\ | |
1821 @code{save_header_format_string} is the empty string,\n\ | |
3709 | 1822 the header comment is omitted from text-format data files. The\n\ |
1823 default value is\n\ | |
1824 \n\ | |
7031 | 1825 @smallexample\n\ |
4060 | 1826 \"# Created by Octave VERSION, %a %b %d %H:%M:%S %Y %Z <USER@@HOST>\"\n\ |
7031 | 1827 @end smallexample\n\ |
3709 | 1828 @seealso{strftime}\n\ |
5794 | 1829 @end deftypefn") |
1830 { | |
1831 return SET_INTERNAL_VARIABLE (save_header_format_string); | |
2194 | 1832 } |
1833 | |
604 | 1834 /* |
1835 ;;; Local Variables: *** | |
1836 ;;; mode: C++ *** | |
1837 ;;; End: *** | |
1838 */ |