Mercurial > hg > octave-lyh
annotate src/load-save.cc @ 10010:c5e9931c7ba7
Correctly produce postcript output for geometryimages when QHULL library is not present
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Sun, 20 Dec 2009 17:02:57 -0800 |
parents | 31436dcf7d0f |
children | cd96d29c5efa |
rev | line source |
---|---|
6763 | 1 /* |
604 | 2 |
7017 | 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, |
8920 | 4 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 | |
9946 | 42 #include "strftime.h" |
43 | |
1961 | 44 #include "byte-swap.h" |
45 #include "data-conv.h" | |
2926 | 46 #include "file-ops.h" |
6159 | 47 #include "file-stat.h" |
2926 | 48 #include "glob-match.h" |
2890 | 49 #include "lo-mappers.h" |
2318 | 50 #include "mach-info.h" |
3185 | 51 #include "oct-env.h" |
3258 | 52 #include "oct-time.h" |
4171 | 53 #include "quit.h" |
1755 | 54 #include "str-vec.h" |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8110
diff
changeset
|
55 #include "oct-locbuf.h" |
1755 | 56 |
4332 | 57 #include "Cell.h" |
1352 | 58 #include "defun.h" |
604 | 59 #include "error.h" |
777 | 60 #include "gripes.h" |
6159 | 61 #include "load-path.h" |
1352 | 62 #include "load-save.h" |
1750 | 63 #include "oct-obj.h" |
3687 | 64 #include "oct-map.h" |
4332 | 65 #include "ov-cell.h" |
1352 | 66 #include "pager.h" |
1750 | 67 #include "pt-exp.h" |
1352 | 68 #include "symtab.h" |
69 #include "sysdep.h" | |
70 #include "unwind-prot.h" | |
604 | 71 #include "utils.h" |
2371 | 72 #include "variables.h" |
3185 | 73 #include "version.h" |
3688 | 74 #include "dMatrix.h" |
75 | |
4633 | 76 #include "ls-hdf5.h" |
77 #include "ls-mat-ascii.h" | |
78 #include "ls-mat4.h" | |
79 #include "ls-mat5.h" | |
80 #include "ls-oct-ascii.h" | |
81 #include "ls-oct-binary.h" | |
3688 | 82 |
5269 | 83 #ifdef HAVE_ZLIB |
84 #include "zfstream.h" | |
85 #endif | |
86 | |
3598 | 87 // Write octave-core file if Octave crashes or is killed by a signal. |
5794 | 88 static bool Vcrash_dumps_octave_core = true; |
3189 | 89 |
4791 | 90 // The maximum amount of memory (in kilobytes) that we will attempt to |
91 // write to the Octave core file. | |
5794 | 92 static double Voctave_core_file_limit = -1.0; |
4791 | 93 |
94 // The name of the Octave core file. | |
5794 | 95 static std::string Voctave_core_file_name = "octave-core"; |
4791 | 96 |
3687 | 97 // The default output format. May be one of "binary", "text", |
98 // "mat-binary", or "hdf5". | |
5794 | 99 static std::string Vdefault_save_options = "-text"; |
2194 | 100 |
5284 | 101 // The output format for Octave core files. |
5794 | 102 static std::string Voctave_core_file_options = "-binary"; |
103 | |
104 static std::string | |
105 default_save_header_format (void) | |
106 { | |
107 return | |
108 std::string ("# Created by Octave " OCTAVE_VERSION | |
109 ", %a %b %d %H:%M:%S %Y %Z <") | |
110 + octave_env::get_user_name () | |
111 + std::string ("@") | |
112 + octave_env::get_host_name () | |
113 + std::string (">"); | |
114 } | |
4788 | 115 |
3709 | 116 // The format string for the comment line at the top of text-format |
117 // save files. Passed to strftime. Should begin with `#' and contain | |
118 // no newline characters. | |
5794 | 119 static std::string Vsave_header_format_string = default_save_header_format (); |
3709 | 120 |
5369 | 121 static void |
122 gripe_file_open (const std::string& fcn, const std::string& file) | |
123 { | |
124 if (fcn == "load") | |
125 error ("%s: unable to open input file `%s'", fcn.c_str (), file.c_str ()); | |
126 else if (fcn == "save") | |
127 error ("%s: unable to open output file `%s'", fcn.c_str (), file.c_str ()); | |
128 else | |
129 error ("%s: unable to open file `%s'", fcn.c_str (), file.c_str ()); | |
130 } | |
131 | |
7336 | 132 // Install a variable with name NAME and the value VAL in the |
133 // symbol table. If GLOBAL is TRUE, make the variable global. | |
604 | 134 |
135 static void | |
7336 | 136 install_loaded_variable (const std::string& name, |
4171 | 137 const octave_value& val, |
7336 | 138 bool global, const std::string& /*doc*/) |
604 | 139 { |
140 if (global) | |
141 { | |
7336 | 142 symbol_table::clear (name); |
143 symbol_table::mark_global (name); | |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7718
diff
changeset
|
144 symbol_table::global_varref (name) = val; |
604 | 145 } |
146 else | |
7336 | 147 symbol_table::varref (name) = val; |
604 | 148 } |
149 | |
3019 | 150 // Return TRUE if NAME matches one of the given globbing PATTERNS. |
604 | 151 |
3013 | 152 static bool |
3769 | 153 matches_patterns (const string_vector& patterns, int pat_idx, |
3523 | 154 int num_pat, const std::string& name) |
604 | 155 { |
1755 | 156 for (int i = pat_idx; i < num_pat; i++) |
604 | 157 { |
1792 | 158 glob_match pattern (patterns[i]); |
3013 | 159 |
1792 | 160 if (pattern.match (name)) |
3013 | 161 return true; |
604 | 162 } |
3688 | 163 |
3013 | 164 return false; |
604 | 165 } |
166 | |
4329 | 167 int |
3523 | 168 read_binary_file_header (std::istream& is, bool& swap, |
4329 | 169 oct_mach_info::float_format& flt_fmt, bool quiet) |
604 | 170 { |
3552 | 171 const int magic_len = 10; |
172 char magic[magic_len+1]; | |
5760 | 173 is.read (magic, magic_len); |
604 | 174 magic[magic_len] = '\0'; |
3688 | 175 |
604 | 176 if (strncmp (magic, "Octave-1-L", magic_len) == 0) |
2318 | 177 swap = oct_mach_info::words_big_endian (); |
604 | 178 else if (strncmp (magic, "Octave-1-B", magic_len) == 0) |
2318 | 179 swap = ! oct_mach_info::words_big_endian (); |
604 | 180 else |
181 { | |
182 if (! quiet) | |
5369 | 183 error ("load: unable to read read binary file"); |
604 | 184 return -1; |
185 } | |
186 | |
187 char tmp = 0; | |
5760 | 188 is.read (&tmp, 1); |
604 | 189 |
2318 | 190 flt_fmt = mopt_digit_to_float_format (tmp); |
191 | |
4574 | 192 if (flt_fmt == oct_mach_info::flt_fmt_unknown) |
604 | 193 { |
194 if (! quiet) | |
195 error ("load: unrecognized binary format!"); | |
3688 | 196 |
604 | 197 return -1; |
198 } | |
199 | |
200 return 0; | |
201 } | |
202 | |
5269 | 203 #ifdef HAVE_ZLIB |
204 static bool | |
205 check_gzip_magic (const std::string& fname) | |
206 { | |
207 bool retval = false; | |
208 std::ifstream file (fname.c_str ()); | |
209 OCTAVE_LOCAL_BUFFER (unsigned char, magic, 2); | |
210 | |
5760 | 211 if (file.read (reinterpret_cast<char *> (magic), 2) && magic[0] == 0x1f && |
5269 | 212 magic[1] == 0x8b) |
213 retval = true; | |
214 | |
215 file.close (); | |
216 return retval; | |
217 } | |
218 #endif | |
219 | |
604 | 220 static load_save_format |
6625 | 221 get_file_format (std::istream& file, const std::string& filename) |
604 | 222 { |
223 load_save_format retval = LS_UNKNOWN; | |
224 | |
4574 | 225 oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown; |
604 | 226 |
3019 | 227 bool swap = false; |
228 | |
229 if (read_binary_file_header (file, swap, flt_fmt, true) == 0) | |
604 | 230 retval = LS_BINARY; |
231 else | |
232 { | |
6202 | 233 file.clear (); |
3538 | 234 file.seekg (0, std::ios::beg); |
604 | 235 |
5828 | 236 int32_t mopt, nr, nc, imag, len; |
1180 | 237 |
238 int err = read_mat_file_header (file, swap, mopt, nr, nc, imag, len, 1); | |
239 | |
240 if (! err) | |
604 | 241 retval = LS_MAT_BINARY; |
242 else | |
243 { | |
2511 | 244 file.clear (); |
3538 | 245 file.seekg (0, std::ios::beg); |
604 | 246 |
6625 | 247 err = read_mat5_binary_file_header (file, swap, true, filename); |
3688 | 248 |
249 if (! err) | |
250 { | |
251 file.clear (); | |
252 file.seekg (0, std::ios::beg); | |
253 retval = LS_MAT5_BINARY; | |
254 } | |
255 else | |
256 { | |
257 file.clear (); | |
258 file.seekg (0, std::ios::beg); | |
259 | |
4171 | 260 std::string tmp = extract_keyword (file, "name"); |
261 | |
262 if (! tmp.empty ()) | |
263 retval = LS_ASCII; | |
2511 | 264 } |
604 | 265 } |
266 } | |
267 | |
5269 | 268 return retval; |
269 } | |
5977 | 270 |
5269 | 271 static load_save_format |
272 get_file_format (const std::string& fname, const std::string& orig_fname, | |
273 bool &use_zlib) | |
274 { | |
275 load_save_format retval = LS_UNKNOWN; | |
276 | |
277 #ifdef HAVE_HDF5 | |
278 // check this before we open the file | |
279 if (H5Fis_hdf5 (fname.c_str ()) > 0) | |
280 return LS_HDF5; | |
281 #endif /* HAVE_HDF5 */ | |
604 | 282 |
5269 | 283 std::ifstream file (fname.c_str ()); |
284 use_zlib = false; | |
285 | |
286 if (file) | |
287 { | |
6625 | 288 retval = get_file_format (file, orig_fname); |
5269 | 289 file.close (); |
5977 | 290 |
5383 | 291 #ifdef HAVE_ZLIB |
5269 | 292 if (retval == LS_UNKNOWN && check_gzip_magic (fname)) |
293 { | |
294 gzifstream gzfile (fname.c_str ()); | |
295 use_zlib = true; | |
296 | |
297 if (gzfile) | |
298 { | |
6625 | 299 retval = get_file_format (gzfile, orig_fname); |
5269 | 300 gzfile.close (); |
301 } | |
302 } | |
5977 | 303 #endif |
5269 | 304 |
305 if (retval == LS_UNKNOWN) | |
306 { | |
307 // Try reading the file as numbers only, determining the | |
308 // number of rows and columns from the data. We don't | |
309 // even bother to check to see if the first item in the | |
310 // file is a number, so that get_complete_line() can | |
311 // skip any comments that might appear at the top of the | |
312 // file. | |
313 | |
314 retval = LS_MAT_ASCII; | |
315 } | |
316 } | |
317 else | |
5369 | 318 gripe_file_open ("load", orig_fname); |
604 | 319 |
320 return retval; | |
321 } | |
322 | |
4329 | 323 octave_value |
7336 | 324 do_load (std::istream& stream, const std::string& orig_fname, |
2318 | 325 load_save_format format, oct_mach_info::float_format flt_fmt, |
4687 | 326 bool list_only, bool swap, bool verbose, |
3687 | 327 const string_vector& argv, int argv_idx, int argc, int nargout) |
604 | 328 { |
3727 | 329 octave_value retval; |
330 | |
331 Octave_map retstruct; | |
604 | 332 |
5765 | 333 std::ostringstream output_buf; |
7779
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
334 std::list<std::string> symbol_names; |
4051 | 335 |
5754 | 336 octave_idx_type count = 0; |
4051 | 337 |
604 | 338 for (;;) |
339 { | |
3019 | 340 bool global = false; |
2086 | 341 octave_value tc; |
604 | 342 |
4171 | 343 std::string name; |
344 std::string doc; | |
604 | 345 |
8425
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8418
diff
changeset
|
346 switch (format.type) |
604 | 347 { |
348 case LS_ASCII: | |
3136 | 349 name = read_ascii_data (stream, orig_fname, global, tc, count); |
604 | 350 break; |
351 | |
352 case LS_BINARY: | |
353 name = read_binary_data (stream, swap, flt_fmt, orig_fname, | |
354 global, tc, doc); | |
355 break; | |
356 | |
2511 | 357 case LS_MAT_ASCII: |
358 name = read_mat_ascii_data (stream, orig_fname, tc); | |
359 break; | |
360 | |
604 | 361 case LS_MAT_BINARY: |
362 name = read_mat_binary_data (stream, orig_fname, tc); | |
363 break; | |
364 | |
3687 | 365 #ifdef HAVE_HDF5 |
366 case LS_HDF5: | |
4687 | 367 name = read_hdf5_data (stream, orig_fname, global, tc, doc); |
3687 | 368 break; |
369 #endif /* HAVE_HDF5 */ | |
370 | |
3688 | 371 case LS_MAT5_BINARY: |
5269 | 372 case LS_MAT7_BINARY: |
3688 | 373 name = read_mat5_binary_element (stream, orig_fname, swap, |
374 global, tc); | |
375 break; | |
376 | |
604 | 377 default: |
775 | 378 gripe_unrecognized_data_fmt ("load"); |
604 | 379 break; |
380 } | |
381 | |
4171 | 382 if (error_state || stream.eof () || name.empty ()) |
383 break; | |
384 else if (! error_state && ! name.empty ()) | |
604 | 385 { |
386 if (tc.is_defined ()) | |
387 { | |
3136 | 388 if (format == LS_MAT_ASCII && argv_idx < argc) |
389 warning ("load: loaded ASCII file `%s' -- ignoring extra args", | |
3687 | 390 orig_fname.c_str ()); |
3136 | 391 |
392 if (format == LS_MAT_ASCII | |
393 || argv_idx == argc | |
1755 | 394 || matches_patterns (argv, argv_idx, argc, name)) |
604 | 395 { |
396 count++; | |
621 | 397 if (list_only) |
398 { | |
399 if (verbose) | |
400 { | |
401 if (count == 1) | |
402 output_buf | |
403 << "type rows cols name\n" | |
404 << "==== ==== ==== ====\n"; | |
405 | |
3013 | 406 output_buf |
3548 | 407 << std::setiosflags (std::ios::left) |
408 << std::setw (16) << tc.type_name () . c_str () | |
409 << std::setiosflags (std::ios::right) | |
410 << std::setw (7) << tc.rows () | |
411 << std::setw (7) << tc.columns () | |
7779
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
412 << " " << name << "\n"; |
621 | 413 } |
7779
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
414 else |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
415 symbol_names.push_back (name); |
621 | 416 } |
417 else | |
418 { | |
3727 | 419 if (nargout == 1) |
420 { | |
421 if (format == LS_MAT_ASCII) | |
422 retval = tc; | |
423 else | |
4675 | 424 retstruct.assign (name, tc); |
3727 | 425 } |
426 else | |
7336 | 427 install_loaded_variable (name, tc, global, doc); |
621 | 428 } |
604 | 429 } |
2511 | 430 |
431 // Only attempt to read one item from a headless text file. | |
432 | |
433 if (format == LS_MAT_ASCII) | |
434 break; | |
604 | 435 } |
436 else | |
4171 | 437 error ("load: unable to load variable `%s'", name.c_str ()); |
604 | 438 } |
439 else | |
440 { | |
441 if (count == 0) | |
442 error ("load: are you sure `%s' is an Octave data file?", | |
1755 | 443 orig_fname.c_str ()); |
604 | 444 |
445 break; | |
446 } | |
447 } | |
448 | |
621 | 449 if (list_only && count) |
450 { | |
7779
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
451 if (verbose) |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
452 { |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
453 std::string msg = output_buf.str (); |
2095 | 454 |
7779
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
455 if (nargout > 0) |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
456 retval = msg; |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
457 else |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
458 octave_stdout << msg; |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
459 } |
621 | 460 else |
7779
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
461 { |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
462 if (nargout > 0) |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
463 retval = Cell (string_vector (symbol_names)); |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
464 else |
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 string_vector names (symbol_names); |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
467 |
791231dac333
Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
7767
diff
changeset
|
468 names.list_in_columns (octave_stdout); |
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 octave_stdout << "\n"; |
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 } |
621 | 473 } |
6639 | 474 else if (retstruct.nfields () != 0) |
3727 | 475 retval = retstruct; |
621 | 476 |
863 | 477 return retval; |
478 } | |
479 | |
6159 | 480 std::string |
481 find_file_to_load (const std::string& name, const std::string& orig_name) | |
482 { | |
483 std::string fname = name; | |
484 | |
6838 | 485 if (! (octave_env::absolute_pathname (fname) |
486 || octave_env::rooted_relative_pathname (fname))) | |
6159 | 487 { |
488 file_stat fs (fname); | |
489 | |
6584 | 490 if (! (fs.exists () && fs.is_reg ())) |
6159 | 491 { |
492 std::string tmp = octave_env::make_absolute | |
493 (load_path::find_file (fname), octave_env::getcwd ()); | |
494 | |
495 if (! tmp.empty ()) | |
496 { | |
497 warning_with_id ("Octave:load-file-in-path", | |
498 "load: file found in load path"); | |
499 fname = tmp; | |
500 } | |
501 } | |
502 } | |
503 | |
6838 | 504 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
|
505 size_t sep_pos = fname.find_last_of (file_ops::dir_sep_chars ()); |
6838 | 506 |
8021 | 507 if (dot_pos == std::string::npos |
508 || (sep_pos != std::string::npos && dot_pos < sep_pos)) | |
6159 | 509 { |
6838 | 510 // Either no '.' in name or no '.' appears after last directory |
511 // separator. | |
512 | |
6159 | 513 file_stat fs (fname); |
514 | |
6584 | 515 if (! (fs.exists () && fs.is_reg ())) |
6159 | 516 fname = find_file_to_load (fname + ".mat", orig_name); |
517 } | |
518 else | |
519 { | |
520 file_stat fs (fname); | |
521 | |
6584 | 522 if (! (fs.exists () && fs.is_reg ())) |
6159 | 523 { |
524 fname = ""; | |
525 | |
526 error ("load: unable to find file %s", orig_name.c_str ()); | |
527 } | |
528 } | |
529 | |
530 return fname; | |
531 } | |
532 | |
533 | |
3687 | 534 // HDF5 load/save documentation is included in the Octave manual |
535 // regardless, but if HDF5 is not linked in we also include a | |
536 // sentence noting this, so the user understands that the features | |
537 // aren't available. Define a macro for this sentence: | |
538 | |
539 #ifdef HAVE_HDF5 | |
540 #define HAVE_HDF5_HELP_STRING "" | |
541 #else /* ! HAVE_HDF5 */ | |
542 #define HAVE_HDF5_HELP_STRING "\n\ | |
543 HDF5 load and save are not available, as this Octave executable was\n\ | |
544 not linked with the HDF5 library." | |
545 #endif /* ! HAVE HDF5 */ | |
546 | |
8746
5dd06f19e9be
handle commands in the lexer
John W. Eaton <jwe@octave.org>
parents:
8676
diff
changeset
|
547 DEFUN (load, args, nargout, |
3372 | 548 "-*- texinfo -*-\n\ |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
549 @deffn {Command} load file\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
550 @deffnx {Command} load options file\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
551 @deffnx {Command} load options file v1 v2 @dots{}\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
552 @deffnx {Command} S = load(\"options\", \"file\", \"v1\", \"v2\", @dots{})\n\ |
7247 | 553 Load the named variables @var{v1}, @var{v2}, @dots{}, from the file\n\ |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
554 @var{file}. If no variables are specified then all variables found in the\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
555 file will be loaded. As with @code{save}, the list of variables to extract\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
556 can be full names or use a pattern syntax. The format of the file is\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
557 automatically detected but may be overridden by supplying the appropriate\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
558 option.\n\ |
863 | 559 \n\ |
5665 | 560 If load is invoked using the functional form\n\ |
561 \n\ | |
562 @example\n\ | |
7247 | 563 load (\"-option1\", @dots{}, \"file\", \"v1\", @dots{})\n\ |
5665 | 564 @end example\n\ |
565 \n\ | |
566 @noindent\n\ | |
567 then the @var{options}, @var{file}, and variable name arguments\n\ | |
568 (@var{v1}, @dots{}) must be specified as character strings.\n\ | |
569 \n\ | |
3372 | 570 If a variable that is not marked as global is loaded from a file when a\n\ |
571 global symbol with the same name already exists, it is loaded in the\n\ | |
572 global symbol table. Also, if a variable is marked as global in a file\n\ | |
573 and a local symbol exists, the local symbol is moved to the global\n\ | |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
574 symbol table and given the value from the file.\n\ |
863 | 575 \n\ |
3727 | 576 If invoked with a single output argument, Octave returns data instead\n\ |
577 of inserting variables in the symbol table. If the data file contains\n\ | |
578 only numbers (TAB- or space-delimited columns), a matrix of values is\n\ | |
579 returned. Otherwise, @code{load} returns a structure with members\n\ | |
580 corresponding to the names of the variables in the file.\n\ | |
581 \n\ | |
3372 | 582 The @code{load} command can read data stored in Octave's text and\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
583 binary formats, and @sc{matlab}'s binary format. If compiled with zlib\n\ |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
584 support, it can also load gzip-compressed files. It will automatically\n\ |
3372 | 585 detect the type of file and do conversion from different floating point\n\ |
586 formats (currently only IEEE big and little endian, though other formats\n\ | |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
587 may be added in the future).\n\ |
3372 | 588 \n\ |
589 Valid options for @code{load} are listed in the following table.\n\ | |
863 | 590 \n\ |
3372 | 591 @table @code\n\ |
592 @item -force\n\ | |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
593 This option is accepted for backward compatibility but is ignored.\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
594 Octave now overwrites variables currently in memory with\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
595 those of the same name found in the file.\n\ |
3372 | 596 \n\ |
597 @item -ascii\n\ | |
5938 | 598 Force Octave to assume the file contains columns of numbers in text format\n\ |
599 without any header or other information. Data in the file will be loaded\n\ | |
600 as a single numeric matrix with the name of the variable derived from the\n\ | |
601 name of the file.\n\ | |
5197 | 602 \n\ |
3372 | 603 @item -binary\n\ |
604 Force Octave to assume the file is in Octave's binary format.\n\ | |
605 \n\ | |
3687 | 606 @item -hdf5\n\ |
607 Force Octave to assume the file is in HDF5 format.\n\ | |
608 (HDF5 is a free, portable binary format developed by the National\n\ | |
609 Center for Supercomputing Applications at the University of Illinois.)\n\ | |
610 Note that Octave can read HDF5 files not created by itself, but may\n\ | |
4687 | 611 skip some datasets in formats that it cannot support.\n" |
3687 | 612 |
613 HAVE_HDF5_HELP_STRING | |
614 | |
615 "\n\ | |
616 @item -import\n\ | |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
617 This option is accepted for backward compatibility but is ignored.\n\ |
4884 | 618 Octave can now support multi-dimensional HDF data and automatically\n\ |
619 modifies variable names if they are invalid Octave identifiers.\n\ | |
3687 | 620 \n\ |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
621 @item -mat\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
622 @itemx -mat-binary\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
623 @itemx -6\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
624 @itemx -v6\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
625 @itemx -7\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
626 @itemx -v7\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
627 Force Octave to assume the file is in @sc{matlab}'s version 6 or 7 binary\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
628 format.\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
629 \n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
630 @item -mat4-binary\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
631 @itemx -4\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
632 @itemx -v4\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
633 @itemx -V4\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
634 Force Octave to assume the file is in the binary format written by\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
635 @sc{matlab} version 4.\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
636 \n\ |
5198 | 637 @item -text\n\ |
5197 | 638 Force Octave to assume the file is in Octave's text format.\n\ |
3372 | 639 @end table\n\ |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
640 @seealso{save, dlmwrite, csvwrite, fwrite}\n\ |
3372 | 641 @end deffn") |
863 | 642 { |
2086 | 643 octave_value_list retval; |
863 | 644 |
1755 | 645 int argc = args.length () + 1; |
646 | |
1968 | 647 string_vector argv = args.make_argv ("load"); |
1755 | 648 |
649 if (error_state) | |
650 return retval; | |
863 | 651 |
1358 | 652 // It isn't necessary to have the default load format stored in a |
653 // user preference variable since we can determine the type of file | |
654 // as we are reading. | |
863 | 655 |
656 load_save_format format = LS_UNKNOWN; | |
657 | |
3019 | 658 bool list_only = false; |
659 bool verbose = false; | |
863 | 660 |
1755 | 661 int i; |
662 for (i = 1; i < argc; i++) | |
863 | 663 { |
1755 | 664 if (argv[i] == "-force" || argv[i] == "-f") |
863 | 665 { |
4884 | 666 // Silently ignore this |
667 // warning ("load: -force ignored"); | |
863 | 668 } |
1755 | 669 else if (argv[i] == "-list" || argv[i] == "-l") |
863 | 670 { |
3019 | 671 list_only = true; |
863 | 672 } |
1755 | 673 else if (argv[i] == "-verbose" || argv[i] == "-v") |
863 | 674 { |
3019 | 675 verbose = true; |
863 | 676 } |
1755 | 677 else if (argv[i] == "-ascii" || argv[i] == "-a") |
863 | 678 { |
5938 | 679 format = LS_MAT_ASCII; |
863 | 680 } |
1755 | 681 else if (argv[i] == "-binary" || argv[i] == "-b") |
863 | 682 { |
683 format = LS_BINARY; | |
684 } | |
5269 | 685 else if (argv[i] == "-mat-binary" || argv[i] == "-mat" || argv[i] == "-m" |
686 || argv[i] == "-6" || argv[i] == "-v6") | |
863 | 687 { |
3688 | 688 format = LS_MAT5_BINARY; |
689 } | |
7819
b7e8ea6a5143
trivial fix option in Fload
Jaroslav Hajek <highegg@gmail.com>
parents:
7779
diff
changeset
|
690 else if (argv[i] == "-7" || argv[i] == "-v7") |
5269 | 691 { |
692 format = LS_MAT7_BINARY; | |
693 } | |
5256 | 694 else if (argv[i] == "-mat4-binary" || argv[i] == "-V4" |
695 || argv[i] == "-v4" || argv[i] == "-4") | |
3688 | 696 { |
863 | 697 format = LS_MAT_BINARY; |
698 } | |
3687 | 699 else if (argv[i] == "-hdf5" || argv[i] == "-h") |
700 { | |
701 #ifdef HAVE_HDF5 | |
702 format = LS_HDF5; | |
703 #else /* ! HAVE_HDF5 */ | |
704 error ("load: octave executable was not linked with HDF5 library"); | |
705 return retval; | |
706 #endif /* ! HAVE_HDF5 */ | |
707 } | |
708 else if (argv[i] == "-import" || argv[i] == "-i") | |
709 { | |
4687 | 710 warning ("load: -import ignored"); |
3687 | 711 } |
5197 | 712 else if (argv[i] == "-text" || argv[i] == "-t") |
713 { | |
714 format = LS_ASCII; | |
715 } | |
863 | 716 else |
717 break; | |
718 } | |
719 | |
1755 | 720 if (i == argc) |
863 | 721 { |
5823 | 722 print_usage (); |
863 | 723 return retval; |
724 } | |
725 | |
3523 | 726 std::string orig_fname = argv[i]; |
863 | 727 |
4574 | 728 oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown; |
863 | 729 |
3019 | 730 bool swap = false; |
863 | 731 |
1755 | 732 if (argv[i] == "-") |
863 | 733 { |
1755 | 734 i++; |
863 | 735 |
3687 | 736 #ifdef HAVE_HDF5 |
737 if (format == LS_HDF5) | |
738 error ("load: cannot read HDF5 format from stdin"); | |
739 else | |
740 #endif /* HAVE_HDF5 */ | |
863 | 741 if (format != LS_UNKNOWN) |
742 { | |
5775 | 743 // FIXME -- if we have already seen EOF on a |
3531 | 744 // previous call, how do we fix up the state of std::cin so |
745 // that we can get additional input? I'm afraid that we | |
746 // can't fix this using std::cin only. | |
747 | |
7336 | 748 retval = do_load (std::cin, orig_fname, format, flt_fmt, |
4687 | 749 list_only, swap, verbose, argv, i, argc, |
863 | 750 nargout); |
751 } | |
752 else | |
753 error ("load: must specify file format if reading from stdin"); | |
754 } | |
755 else | |
756 { | |
3523 | 757 std::string fname = file_ops::tilde_expand (argv[i]); |
6159 | 758 |
759 fname = find_file_to_load (fname, orig_fname); | |
863 | 760 |
6159 | 761 if (error_state) |
762 return retval; | |
763 | |
764 bool use_zlib = false; | |
5089 | 765 |
863 | 766 if (format == LS_UNKNOWN) |
5269 | 767 format = get_file_format (fname, orig_fname, use_zlib); |
863 | 768 |
3687 | 769 #ifdef HAVE_HDF5 |
770 if (format == LS_HDF5) | |
771 { | |
772 i++; | |
773 | |
5089 | 774 hdf5_ifstream hdf5_file (fname.c_str ()); |
3687 | 775 |
5089 | 776 if (hdf5_file.file_id >= 0) |
777 { | |
7336 | 778 retval = do_load (hdf5_file, orig_fname, format, |
5089 | 779 flt_fmt, list_only, swap, verbose, |
780 argv, i, argc, nargout); | |
4844 | 781 |
5089 | 782 hdf5_file.close (); |
3687 | 783 } |
4845 | 784 else |
5369 | 785 gripe_file_open ("load", orig_fname); |
3687 | 786 } |
787 else | |
788 #endif /* HAVE_HDF5 */ | |
789 // don't insert any statements here; the "else" above has to | |
790 // go with the "if" below!!!!! | |
863 | 791 if (format != LS_UNKNOWN) |
792 { | |
1755 | 793 i++; |
863 | 794 |
8946
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
795 // Always open in binary mode and handle various |
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
796 // line-endings explicitly. |
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
797 std::ios::openmode mode = std::ios::in | std::ios::binary; |
863 | 798 |
5269 | 799 #ifdef HAVE_ZLIB |
800 if (use_zlib) | |
801 { | |
802 gzifstream file (fname.c_str (), mode); | |
863 | 803 |
5269 | 804 if (file) |
863 | 805 { |
5269 | 806 if (format == LS_BINARY) |
807 { | |
808 if (read_binary_file_header (file, swap, flt_fmt) < 0) | |
809 { | |
810 if (file) file.close (); | |
811 return retval; | |
812 } | |
813 } | |
814 else if (format == LS_MAT5_BINARY | |
815 || format == LS_MAT7_BINARY) | |
863 | 816 { |
6625 | 817 if (read_mat5_binary_file_header (file, swap, false, orig_fname) < 0) |
5269 | 818 { |
819 if (file) file.close (); | |
820 return retval; | |
821 } | |
863 | 822 } |
5269 | 823 |
7336 | 824 retval = do_load (file, orig_fname, format, |
5269 | 825 flt_fmt, list_only, swap, verbose, |
826 argv, i, argc, nargout); | |
827 | |
828 file.close (); | |
863 | 829 } |
5269 | 830 else |
5369 | 831 gripe_file_open ("load", orig_fname); |
863 | 832 } |
833 else | |
5269 | 834 #endif |
835 { | |
836 std::ifstream file (fname.c_str (), mode); | |
837 | |
838 if (file) | |
839 { | |
840 if (format == LS_BINARY) | |
841 { | |
842 if (read_binary_file_header (file, swap, flt_fmt) < 0) | |
843 { | |
844 if (file) file.close (); | |
845 return retval; | |
846 } | |
847 } | |
848 else if (format == LS_MAT5_BINARY | |
849 || format == LS_MAT7_BINARY) | |
850 { | |
6625 | 851 if (read_mat5_binary_file_header (file, swap, false, orig_fname) < 0) |
5269 | 852 { |
853 if (file) file.close (); | |
854 return retval; | |
855 } | |
856 } | |
857 | |
7336 | 858 retval = do_load (file, orig_fname, format, |
5269 | 859 flt_fmt, list_only, swap, verbose, |
860 argv, i, argc, nargout); | |
861 | |
862 file.close (); | |
863 } | |
864 else | |
5369 | 865 error ("load: unable open input file `%s'", |
5269 | 866 orig_fname.c_str ()); |
867 } | |
863 | 868 } |
869 } | |
5269 | 870 |
604 | 871 return retval; |
872 } | |
873 | |
3019 | 874 // Return TRUE if PATTERN has any special globbing chars in it. |
875 | |
876 static bool | |
3523 | 877 glob_pattern_p (const std::string& pattern) |
604 | 878 { |
879 int open = 0; | |
880 | |
1755 | 881 int len = pattern.length (); |
882 | |
883 for (int i = 0; i < len; i++) | |
604 | 884 { |
1755 | 885 char c = pattern[i]; |
886 | |
604 | 887 switch (c) |
888 { | |
889 case '?': | |
890 case '*': | |
3019 | 891 return true; |
604 | 892 |
893 case '[': // Only accept an open brace if there is a close | |
894 open++; // brace to match it. Bracket expressions must be | |
895 continue; // complete, according to Posix.2 | |
896 | |
897 case ']': | |
898 if (open) | |
3019 | 899 return true; |
604 | 900 continue; |
4402 | 901 |
604 | 902 case '\\': |
1755 | 903 if (i == len - 1) |
3019 | 904 return false; |
604 | 905 |
906 default: | |
907 continue; | |
908 } | |
909 } | |
910 | |
3019 | 911 return false; |
604 | 912 } |
913 | |
4791 | 914 static void |
915 do_save (std::ostream& os, const octave_value& tc, | |
916 const std::string& name, const std::string& help, | |
7336 | 917 bool global, load_save_format fmt, bool save_as_floats) |
604 | 918 { |
8425
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8418
diff
changeset
|
919 switch (fmt.type) |
604 | 920 { |
921 case LS_ASCII: | |
6974 | 922 save_ascii_data (os, tc, name, global, 0); |
604 | 923 break; |
924 | |
925 case LS_BINARY: | |
630 | 926 save_binary_data (os, tc, name, help, global, save_as_floats); |
604 | 927 break; |
928 | |
5938 | 929 case LS_MAT_ASCII: |
8425
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8418
diff
changeset
|
930 if (! save_mat_ascii_data (os, tc, fmt.opts & LS_MAT_ASCII_LONG ? 16 : 8, |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8418
diff
changeset
|
931 fmt.opts & LS_MAT_ASCII_TABS)) |
5938 | 932 warning ("save: unable to save %s in ASCII format", name.c_str ()); |
933 break; | |
934 | |
667 | 935 case LS_MAT_BINARY: |
936 save_mat_binary_data (os, tc, name); | |
937 break; | |
938 | |
3687 | 939 #ifdef HAVE_HDF5 |
940 case LS_HDF5: | |
941 save_hdf5_data (os, tc, name, help, global, save_as_floats); | |
942 break; | |
943 #endif /* HAVE_HDF5 */ | |
944 | |
3688 | 945 case LS_MAT5_BINARY: |
5269 | 946 save_mat5_binary_element (os, tc, name, global, false, save_as_floats); |
947 break; | |
948 | |
949 case LS_MAT7_BINARY: | |
950 save_mat5_binary_element (os, tc, name, global, true, save_as_floats); | |
3688 | 951 break; |
952 | |
604 | 953 default: |
775 | 954 gripe_unrecognized_data_fmt ("save"); |
604 | 955 break; |
956 } | |
957 } | |
958 | |
4791 | 959 // Save the info from SR on stream OS in the format specified by FMT. |
960 | |
961 void | |
7336 | 962 do_save (std::ostream& os, const symbol_table::symbol_record& sr, |
963 load_save_format fmt, bool save_as_floats) | |
4791 | 964 { |
7336 | 965 octave_value val = sr.varval (); |
4791 | 966 |
7336 | 967 if (val.is_defined ()) |
4791 | 968 { |
7336 | 969 std::string name = sr.name (); |
970 std::string help; | |
971 bool global = sr.is_global (); | |
4791 | 972 |
7336 | 973 do_save (os, val, name, help, global, fmt, save_as_floats); |
4791 | 974 } |
975 } | |
976 | |
7635
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
977 // 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
|
978 // in the format specified by FMT. |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
979 |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
980 static size_t |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
981 save_fields (std::ostream& os, const Octave_map& m, |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
982 const std::string& pattern, |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
983 load_save_format fmt, bool save_as_floats) |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
984 { |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
985 glob_match pat (pattern); |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
986 |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
987 size_t saved = 0; |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
988 |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
989 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
|
990 { |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
991 std::string empty_str; |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
992 |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
993 if (pat.match(p->first)) |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
994 { |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
995 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
|
996 0, fmt, save_as_floats); |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
997 |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
998 saved++; |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
999 } |
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 |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1002 return saved; |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1003 } |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1004 |
604 | 1005 // Save variables with names matching PATTERN on stream OS in the |
5794 | 1006 // format specified by FMT. |
604 | 1007 |
7336 | 1008 static size_t |
5794 | 1009 save_vars (std::ostream& os, const std::string& pattern, |
4791 | 1010 load_save_format fmt, bool save_as_floats) |
604 | 1011 { |
7336 | 1012 std::list<symbol_table::symbol_record> vars = symbol_table::glob (pattern); |
1013 | |
1014 size_t saved = 0; | |
3355 | 1015 |
7336 | 1016 typedef std::list<symbol_table::symbol_record>::const_iterator const_vars_iterator; |
3355 | 1017 |
7336 | 1018 for (const_vars_iterator p = vars.begin (); p != vars.end (); p++) |
620 | 1019 { |
7336 | 1020 do_save (os, *p, fmt, save_as_floats); |
620 | 1021 |
1022 if (error_state) | |
1023 break; | |
7336 | 1024 |
1025 saved++; | |
620 | 1026 } |
604 | 1027 |
1028 return saved; | |
1029 } | |
1030 | |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1031 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
|
1032 parse_save_options (const string_vector &argv, |
5284 | 1033 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
|
1034 bool &save_as_floats, bool &use_zlib) |
604 | 1035 { |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1036 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
|
1037 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
|
1038 |
8425
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8418
diff
changeset
|
1039 bool do_double = false, do_tabs = false; |
8418
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1040 |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1041 for (int i = 0; i < argc; i++) |
5284 | 1042 { |
1043 if (argv[i] == "-append") | |
1044 { | |
1045 append = true; | |
1046 } | |
1047 else if (argv[i] == "-ascii" || argv[i] == "-a") | |
1048 { | |
5938 | 1049 format = LS_MAT_ASCII; |
5284 | 1050 } |
8418
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1051 else if (argv[i] == "-double") |
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1052 { |
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1053 do_double = true; |
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1054 } |
8425
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8418
diff
changeset
|
1055 else if (argv[i] == "-tabs") |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8418
diff
changeset
|
1056 { |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8418
diff
changeset
|
1057 do_tabs = true; |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8418
diff
changeset
|
1058 } |
5284 | 1059 else if (argv[i] == "-text" || argv[i] == "-t") |
1060 { | |
1061 format = LS_ASCII; | |
1062 } | |
1063 else if (argv[i] == "-binary" || argv[i] == "-b") | |
1064 { | |
1065 format = LS_BINARY; | |
1066 } | |
1067 else if (argv[i] == "-hdf5" || argv[i] == "-h") | |
1068 { | |
3687 | 1069 #ifdef HAVE_HDF5 |
5284 | 1070 format = LS_HDF5; |
1071 #else /* ! HAVE_HDF5 */ | |
1072 error ("save: octave executable was not linked with HDF5 library"); | |
1073 #endif /* ! HAVE_HDF5 */ | |
1074 } | |
1075 else if (argv[i] == "-mat-binary" || argv[i] == "-mat" | |
1076 || argv[i] == "-m" || argv[i] == "-6" || argv[i] == "-v6" | |
1077 || argv[i] == "-V6") | |
1078 { | |
1079 format = LS_MAT5_BINARY; | |
1080 } | |
1081 #ifdef HAVE_ZLIB | |
1082 else if (argv[i] == "-mat7-binary" || argv[i] == "-7" | |
1083 || argv[i] == "-v7" || argv[i] == "-V7") | |
1084 { | |
1085 format = LS_MAT7_BINARY; | |
1086 } | |
1087 #endif | |
1088 else if (argv[i] == "-mat4-binary" || argv[i] == "-V4" | |
1089 || argv[i] == "-v4" || argv[i] == "-4") | |
1090 { | |
1091 format = LS_MAT_BINARY; | |
1092 } | |
1093 else if (argv[i] == "-float-binary" || argv[i] == "-f") | |
1094 { | |
1095 format = LS_BINARY; | |
1096 save_as_floats = true; | |
1097 } | |
1098 else if (argv[i] == "-float-hdf5") | |
1099 { | |
1100 #ifdef HAVE_HDF5 | |
1101 format = LS_HDF5; | |
1102 save_as_floats = true; | |
1103 #else /* ! HAVE_HDF5 */ | |
1104 error ("save: octave executable was not linked with HDF5 library"); | |
1105 #endif /* ! HAVE_HDF5 */ | |
1106 } | |
1107 #ifdef HAVE_ZLIB | |
1108 else if (argv[i] == "-zip" || argv[i] == "-z") | |
1109 { | |
1110 use_zlib = true; | |
1111 } | |
1112 #endif | |
1113 else | |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1114 retval.append (argv[i]); |
5284 | 1115 } |
1116 | |
8418
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1117 if (do_double) |
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1118 { |
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1119 if (format == LS_MAT_ASCII) |
8425
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8418
diff
changeset
|
1120 format.opts |= LS_MAT_ASCII_LONG; |
8418
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1121 else |
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1122 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
|
1123 } |
679c22082ac7
handle -double option for save
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
1124 |
8425
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8418
diff
changeset
|
1125 if (do_tabs) |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8418
diff
changeset
|
1126 { |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8418
diff
changeset
|
1127 if (format == LS_MAT_ASCII) |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8418
diff
changeset
|
1128 format.opts |= LS_MAT_ASCII_TABS; |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8418
diff
changeset
|
1129 else |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8418
diff
changeset
|
1130 warning ("save: \"-tabs\" option only has an effect with \"-ascii\""); |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8418
diff
changeset
|
1131 } |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8418
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 { |
8425
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8418
diff
changeset
|
1158 switch (format.type) |
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 | |
9947
31436dcf7d0f
call nstrftime, not my_strftime
John W. Eaton <jwe@octave.org>
parents:
9946
diff
changeset
|
1187 nstrftime (headertext, 124, "MATLAB 5.0 MAT-file, written by Octave " |
31436dcf7d0f
call nstrftime, not my_strftime
John W. Eaton <jwe@octave.org>
parents:
9946
diff
changeset
|
1188 OCTAVE_VERSION ", %Y-%m-%d %T UTC", &bdt, 1, 0); |
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 | |
8746
5dd06f19e9be
handle commands in the lexer
John W. Eaton <jwe@octave.org>
parents:
8676
diff
changeset
|
1443 DEFUN (save, args, , |
3372 | 1444 "-*- texinfo -*-\n\ |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1445 @deffn {Command} save file\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1446 @deffnx {Command} save options file\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1447 @deffnx {Command} save options file @var{v1} @var{v2} @dots{}\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1448 @deffnx {Command} save options file -struct @var{STRUCT} @var{f1} @var{f2} @dots{}\n\ |
7247 | 1449 Save the named variables @var{v1}, @var{v2}, @dots{}, in the file\n\ |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1450 @var{file}. The special filename @samp{-} may be used to write\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1451 output to the terminal. If no variable names are listed, Octave saves\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1452 all the variables in the current scope. Otherwise, full variable names or\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1453 pattern syntax can be used to specify the variables to save.\n\ |
7635
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1454 If the @code{-struct} modifier is used, fields @var{f1} @var{f2} @dots{}\n\ |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1455 of the scalar structure @var{STRUCT} are saved as if they were variables\n\ |
7635
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1456 with corresponding names.\n\ |
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1457 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
|
1458 Options that modify the output format override the format specified by \n\ |
5794 | 1459 @code{default_save_options}.\n\ |
3372 | 1460 \n\ |
5665 | 1461 If save is invoked using the functional form\n\ |
1462 \n\ | |
1463 @example\n\ | |
7247 | 1464 save (\"-option1\", @dots{}, \"file\", \"v1\", @dots{})\n\ |
5665 | 1465 @end example\n\ |
1466 \n\ | |
1467 @noindent\n\ | |
1468 then the @var{options}, @var{file}, and variable name arguments\n\ | |
7247 | 1469 (@var{v1}, @dots{}) must be specified as character strings.\n\ |
5665 | 1470 \n\ |
3372 | 1471 @table @code\n\ |
1472 @item -ascii\n\ | |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1473 Save a single matrix in a text file without header or any other information.\n\ |
5197 | 1474 \n\ |
3372 | 1475 @item -binary\n\ |
1476 Save the data in Octave's binary data format.\n\ | |
1477 \n\ | |
1478 @item -float-binary\n\ | |
1479 Save the data in Octave's binary data format but only using single\n\ | |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1480 precision. Only use this format if you know that all the\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1481 values to be saved can be represented in single precision.\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1482 \n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1483 @item -hdf5\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1484 Save the data in HDF5 format.\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1485 (HDF5 is a free, portable binary format developed by the National\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1486 Center for Supercomputing Applications at the University of Illinois.)\n" |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1487 |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1488 HAVE_HDF5_HELP_STRING |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1489 |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1490 "\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1491 @item -float-hdf5\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1492 Save the data in HDF5 format but only using single precision.\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1493 Only use this format if you know that all the\n\ |
3372 | 1494 values to be saved can be represented in single precision.\n\ |
1495 \n\ | |
5269 | 1496 @item -V7\n\ |
1497 @itemx -v7\n\ | |
1498 @itemx -7\n\ | |
1499 @itemx -mat7-binary\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
1500 Save the data in @sc{matlab}'s v7 binary data format.\n" |
5269 | 1501 |
1502 HAVE_ZLIB_HELP_STRING | |
1503 | |
1504 "\n\ | |
1505 @item -V6\n\ | |
5284 | 1506 @itemx -v6\n\ |
5269 | 1507 @itemx -6\n\ |
1508 @itemx -mat\n\ | |
4884 | 1509 @itemx -mat-binary\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
1510 Save the data in @sc{matlab}'s v6 binary data format.\n\ |
3372 | 1511 \n\ |
5256 | 1512 @item -V4\n\ |
1513 @itemx -v4\n\ | |
1514 @itemx -4\n\ | |
1515 @itemx -mat4-binary\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
1516 Save the data in the binary format written by @sc{matlab} version 4.\n\ |
3688 | 1517 \n\ |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1518 @item -text\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1519 Save the data in Octave's text data format. (default).\n\ |
3687 | 1520 \n\ |
5269 | 1521 @item -zip\n\ |
1522 @itemx -z\n\ | |
9039
51dc9691f23f
Cleanup documentation files errors.texi, debug.texi, io.texi
Rik <rdrider0-list@yahoo.com>
parents:
8946
diff
changeset
|
1523 Use the gzip algorithm to compress the file. This works equally on files that\n\ |
5269 | 1524 are compressed with gzip outside of octave, and gzip can equally be used to\n\ |
5380 | 1525 convert the files for backward compatibility.\n" |
5322 | 1526 |
1527 HAVE_ZLIB_HELP_STRING | |
1528 | |
5269 | 1529 "@end table\n\ |
604 | 1530 \n\ |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1531 The list of variables to save may use wildcard patterns containing\n\ |
3372 | 1532 the following special characters:\n\ |
1533 @table @code\n\ | |
1534 @item ?\n\ | |
1535 Match any single character.\n\ | |
1536 \n\ | |
1537 @item *\n\ | |
1538 Match zero or more characters.\n\ | |
1539 \n\ | |
1540 @item [ @var{list} ]\n\ | |
1541 Match the list of characters specified by @var{list}. If the first\n\ | |
1542 character is @code{!} or @code{^}, match all characters except those\n\ | |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1543 specified by @var{list}. For example, the pattern @code{[a-zA-Z]} will\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
1544 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
|
1545 \n\ |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1546 Wildcards may also be used in the field name specifications when using\n\ |
7635
ba7a3e20ee3d
add -struct modifier to save
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
1547 the @code{-struct} modifier (but not in the struct name itself).\n\ |
5197 | 1548 \n\ |
3372 | 1549 @end table\n\ |
1550 \n\ | |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1551 Except when using the @sc{matlab} binary data file format or the\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1552 @samp{-ascii} format, saving global\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1553 variables also saves the global status of the variable. If the variable\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1554 is restored at a later time using @samp{load}, it will be restored as a\n\ |
3372 | 1555 global variable.\n\ |
1556 \n\ | |
1557 The command\n\ | |
1558 \n\ | |
1559 @example\n\ | |
1560 save -binary data a b*\n\ | |
1561 @end example\n\ | |
1562 \n\ | |
1563 @noindent\n\ | |
1564 saves the variable @samp{a} and all variables beginning with @samp{b} to\n\ | |
1565 the file @file{data} in Octave's binary format.\n\ | |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1566 @seealso{load, default_save_options, dlmread, csvread, fread}\n\ |
3372 | 1567 @end deffn") |
604 | 1568 { |
2086 | 1569 octave_value_list retval; |
604 | 1570 |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1571 int argc = args.length (); |
1755 | 1572 |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1573 string_vector argv = args.make_argv (); |
1755 | 1574 |
1575 if (error_state) | |
1576 return retval; | |
604 | 1577 |
1358 | 1578 // Here is where we would get the default save format if it were |
1579 // stored in a user preference variable. | |
604 | 1580 |
3019 | 1581 bool save_as_floats = false; |
630 | 1582 |
5284 | 1583 load_save_format format = LS_ASCII; |
604 | 1584 |
3185 | 1585 bool append = false; |
1586 | |
5269 | 1587 bool use_zlib = false; |
1588 | |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1589 // 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
|
1590 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
|
1591 use_zlib); |
5351 | 1592 |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1593 // 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
|
1594 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
|
1595 use_zlib); |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
1596 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
|
1597 int i = 0; |
5197 | 1598 |
5284 | 1599 if (error_state) |
1600 return retval; | |
604 | 1601 |
2057 | 1602 if (i == argc) |
604 | 1603 { |
5823 | 1604 print_usage (); |
604 | 1605 return retval; |
1606 } | |
1607 | |
630 | 1608 if (save_as_floats && format == LS_ASCII) |
1609 { | |
1610 error ("save: cannot specify both -ascii and -float-binary"); | |
1611 return retval; | |
1612 } | |
1613 | |
1755 | 1614 if (argv[i] == "-") |
604 | 1615 { |
1755 | 1616 i++; |
863 | 1617 |
3687 | 1618 #ifdef HAVE_HDF5 |
1619 if (format == LS_HDF5) | |
4687 | 1620 error ("save: cannot write HDF5 format to stdout"); |
3687 | 1621 else |
1622 #endif /* HAVE_HDF5 */ | |
1623 // don't insert any commands here! the brace below must go | |
1624 // with the "else" above! | |
1625 { | |
6759 | 1626 if (append) |
1627 warning ("save: ignoring -append option for output to stdout"); | |
1628 | |
5775 | 1629 // FIXME -- should things intended for the screen end up |
3687 | 1630 // in a octave_value (string)? |
1631 | |
5794 | 1632 save_vars (argv, i, argc, octave_stdout, format, |
3687 | 1633 save_as_floats, true); |
1634 } | |
604 | 1635 } |
1755 | 1636 |
1637 // Guard against things like `save a*', which are probably mistakes... | |
1638 | |
1639 else if (i == argc - 1 && glob_pattern_p (argv[i])) | |
1640 { | |
5823 | 1641 print_usage (); |
604 | 1642 return retval; |
1643 } | |
1644 else | |
1645 { | |
3523 | 1646 std::string fname = file_ops::tilde_expand (argv[i]); |
1755 | 1647 |
1648 i++; | |
604 | 1649 |
6625 | 1650 // Matlab v7 files are always compressed |
1651 if (format == LS_MAT7_BINARY) | |
1652 use_zlib = false; | |
1653 | |
6759 | 1654 std::ios::openmode mode |
1655 = append ? (std::ios::app | std::ios::ate) : std::ios::out; | |
1656 | |
4791 | 1657 if (format == LS_BINARY |
1658 #ifdef HAVE_HDF5 | |
1659 || format == LS_HDF5 | |
1660 #endif | |
1661 || format == LS_MAT_BINARY | |
5269 | 1662 || format == LS_MAT5_BINARY |
1663 || format == LS_MAT7_BINARY) | |
3552 | 1664 mode |= std::ios::binary; |
3538 | 1665 |
3687 | 1666 #ifdef HAVE_HDF5 |
1667 if (format == LS_HDF5) | |
863 | 1668 { |
6760 | 1669 // FIXME. It should be possible to append to HDF5 files. |
6759 | 1670 if (append) |
1671 { | |
1672 error ("save: appending to HDF5 files is not implemented"); | |
1673 return retval; | |
1674 } | |
1675 | |
6760 | 1676 bool write_header_info = ! (append && |
1677 H5Fis_hdf5 (fname.c_str ()) > 0); | |
3687 | 1678 |
6760 | 1679 hdf5_ofstream hdf5_file (fname.c_str (), mode); |
1680 | |
1681 if (hdf5_file.file_id != -1) | |
4687 | 1682 { |
5794 | 1683 save_vars (argv, i, argc, hdf5_file, format, |
6760 | 1684 save_as_floats, write_header_info); |
3687 | 1685 |
4687 | 1686 hdf5_file.close (); |
3687 | 1687 } |
1688 else | |
1689 { | |
5369 | 1690 gripe_file_open ("save", fname); |
3687 | 1691 return retval; |
1692 } | |
863 | 1693 } |
1694 else | |
3687 | 1695 #endif /* HAVE_HDF5 */ |
1696 // don't insert any statements here! The brace below must go | |
1697 // with the "else" above! | |
604 | 1698 { |
5269 | 1699 #ifdef HAVE_ZLIB |
1700 if (use_zlib) | |
3687 | 1701 { |
5269 | 1702 gzofstream file (fname.c_str (), mode); |
1703 | |
1704 if (file) | |
1705 { | |
6760 | 1706 bool write_header_info = ! file.tellp (); |
1707 | |
5794 | 1708 save_vars (argv, i, argc, file, format, |
5269 | 1709 save_as_floats, write_header_info); |
1710 | |
1711 file.close (); | |
1712 } | |
1713 else | |
1714 { | |
5369 | 1715 gripe_file_open ("save", fname); |
5269 | 1716 return retval; |
1717 } | |
3687 | 1718 } |
1719 else | |
5269 | 1720 #endif |
3687 | 1721 { |
5269 | 1722 std::ofstream file (fname.c_str (), mode); |
1723 | |
1724 if (file) | |
1725 { | |
6760 | 1726 bool write_header_info = ! file.tellp (); |
1727 | |
5794 | 1728 save_vars (argv, i, argc, file, format, |
5269 | 1729 save_as_floats, write_header_info); |
1730 | |
1731 file.close (); | |
1732 } | |
1733 else | |
1734 { | |
5369 | 1735 gripe_file_open ("save", fname); |
5269 | 1736 return retval; |
1737 } | |
3687 | 1738 } |
604 | 1739 } |
1740 } | |
1741 | |
1742 return retval; | |
1743 } | |
1744 | |
5794 | 1745 DEFUN (crash_dumps_octave_core, args, nargout, |
1746 "-*- texinfo -*-\n\ | |
1747 @deftypefn {Built-in Function} {@var{val} =} crash_dumps_octave_core ()\n\ | |
1748 @deftypefnx {Built-in Function} {@var{old_val} =} crash_dumps_octave_core (@var{new_val})\n\ | |
1749 Query or set the internal variable that controls whether Octave tries\n\ | |
6653 | 1750 to save all current variables to the file \"octave-core\" if it\n\ |
5794 | 1751 crashes or receives a hangup, terminate or similar signal.\n\ |
1752 @seealso{octave_core_file_limit, octave_core_file_name, octave_core_file_options}\n\ | |
1753 @end deftypefn") | |
2194 | 1754 { |
5794 | 1755 return SET_INTERNAL_VARIABLE (crash_dumps_octave_core); |
2194 | 1756 } |
1757 | |
5794 | 1758 DEFUN (default_save_options, args, nargout, |
1759 "-*- texinfo -*-\n\ | |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1760 @deftypefn {Built-in Function} {@var{val} =} default_save_options ()\n\ |
5794 | 1761 @deftypefnx {Built-in Function} {@var{old_val} =} default_save_options (@var{new_val})\n\ |
1762 Query or set the internal variable that specifies the default options\n\ | |
1763 for the @code{save} command, and defines the default format.\n\ | |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1764 Typical values include @code{\"-ascii\"}, @code{\"-text -zip\"}.\n\ |
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1765 The default value is @code{-text}.\n\ |
5794 | 1766 @seealso{save}\n\ |
1767 @end deftypefn") | |
4788 | 1768 { |
5794 | 1769 return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (default_save_options); |
4788 | 1770 } |
1771 | |
5794 | 1772 DEFUN (octave_core_file_limit, args, nargout, |
1773 "-*- texinfo -*-\n\ | |
1774 @deftypefn {Built-in Function} {@var{val} =} octave_core_file_limit ()\n\ | |
1775 @deftypefnx {Built-in Function} {@var{old_val} =} octave_core_file_limit (@var{new_val})\n\ | |
1776 Query or set the internal variable that specifies the maximum amount\n\ | |
1777 of memory (in kilobytes) of the top-level workspace that Octave will\n\ | |
1778 attempt to save when writing data to the crash dump file (the name of\n\ | |
1779 the file is specified by @var{octave_core_file_name}). If\n\ | |
7001 | 1780 @var{octave_core_file_options} flags specify a binary format,\n\ |
5794 | 1781 then @var{octave_core_file_limit} will be approximately the maximum\n\ |
1782 size of the file. If a text file format is used, then the file could\n\ | |
1783 be much larger than the limit. The default value is -1 (unlimited)\n\ | |
1784 @seealso{crash_dumps_octave_core, octave_core_file_name, octave_core_file_options}\n\ | |
1785 @end deftypefn") | |
3709 | 1786 { |
5794 | 1787 return SET_INTERNAL_VARIABLE (octave_core_file_limit); |
3709 | 1788 } |
1789 | |
5794 | 1790 DEFUN (octave_core_file_name, args, nargout, |
1791 "-*- texinfo -*-\n\ | |
1792 @deftypefn {Built-in Function} {@var{val} =} octave_core_file_name ()\n\ | |
1793 @deftypefnx {Built-in Function} {@var{old_val} =} octave_core_file_name (@var{new_val})\n\ | |
1794 Query or set the internal variable that specifies the name of the file\n\ | |
1795 used for saving data from the top-level workspace if Octave aborts.\n\ | |
1796 The default value is @code{\"octave-core\"}\n\ | |
1797 @seealso{crash_dumps_octave_core, octave_core_file_name, octave_core_file_options}\n\ | |
1798 @end deftypefn") | |
2194 | 1799 { |
5794 | 1800 return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (octave_core_file_name); |
1801 } | |
4791 | 1802 |
5794 | 1803 DEFUN (octave_core_file_options, args, nargout, |
1804 "-*- texinfo -*-\n\ | |
1805 @deftypefn {Built-in Function} {@var{val} =} octave_core_file_options ()\n\ | |
1806 @deftypefnx {Built-in Function} {@var{old_val} =} octave_core_file_options (@var{new_val})\n\ | |
1807 Query or set the internal variable that specifies the options used for\n\ | |
1808 saving the workspace data if Octave aborts. The value of\n\ | |
1809 @code{octave_core_file_options} should follow the same format as the\n\ | |
9039
51dc9691f23f
Cleanup documentation files errors.texi, debug.texi, io.texi
Rik <rdrider0-list@yahoo.com>
parents:
8946
diff
changeset
|
1810 options for the @code{save} function. The default value is Octave's binary\n\ |
5284 | 1811 format.\n\ |
5642 | 1812 @seealso{crash_dumps_octave_core, octave_core_file_name, octave_core_file_limit}\n\ |
5794 | 1813 @end deftypefn") |
1814 { | |
1815 return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (octave_core_file_options); | |
1816 } | |
2194 | 1817 |
5794 | 1818 DEFUN (save_header_format_string, args, nargout, |
1819 "-*- texinfo -*-\n\ | |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1820 @deftypefn {Built-in Function} {@var{val} =} save_header_format_string ()\n\ |
5794 | 1821 @deftypefnx {Built-in Function} {@var{old_val} =} save_header_format_string (@var{new_val})\n\ |
1822 Query or set the internal variable that specifies the format\n\ | |
1823 string used for the comment line written at the beginning of\n\ | |
1824 text-format data files saved by Octave. The format string is\n\ | |
1825 passed to @code{strftime} and should begin with the character\n\ | |
1826 @samp{#} and contain no newline characters. If the value of\n\ | |
1827 @code{save_header_format_string} is the empty string,\n\ | |
3709 | 1828 the header comment is omitted from text-format data files. The\n\ |
1829 default value is\n\ | |
1830 \n\ | |
9153
5247e89688e1
Eliminate most overfull errors when running texi2pdf for generating pdf documentation
Rik <rdrider0-list@yahoo.com>
parents:
9064
diff
changeset
|
1831 @c Set example in small font to prevent overfull line\n\ |
7031 | 1832 @smallexample\n\ |
4060 | 1833 \"# Created by Octave VERSION, %a %b %d %H:%M:%S %Y %Z <USER@@HOST>\"\n\ |
7031 | 1834 @end smallexample\n\ |
9306
de124edcebf6
Update documentation for load and save
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
1835 @seealso{strftime, save}\n\ |
5794 | 1836 @end deftypefn") |
1837 { | |
1838 return SET_INTERNAL_VARIABLE (save_header_format_string); | |
2194 | 1839 } |
1840 | |
604 | 1841 /* |
1842 ;;; Local Variables: *** | |
1843 ;;; mode: C++ *** | |
1844 ;;; End: *** | |
1845 */ |