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