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