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