Mercurial > hg > octave-nkf
comparison src/load-save.cc @ 3185:9580887dd160
[project @ 1998-09-26 02:45:55 by jwe]
author | jwe |
---|---|
date | Sat, 26 Sep 1998 02:45:59 +0000 |
parents | 0d640dc625c7 |
children | bef7b73c0724 |
comparison
equal
deleted
inserted
replaced
3184:3988763ec9d3 | 3185:9580887dd160 |
---|---|
25 #endif | 25 #endif |
26 | 26 |
27 #include <cfloat> | 27 #include <cfloat> |
28 #include <cstring> | 28 #include <cstring> |
29 #include <cctype> | 29 #include <cctype> |
30 #include <ctime> | |
30 | 31 |
31 #include <string> | 32 #include <string> |
32 | 33 |
33 #include <iomanip.h> | 34 #include <iomanip.h> |
34 #include <iostream.h> | 35 #include <iostream.h> |
39 #include "data-conv.h" | 40 #include "data-conv.h" |
40 #include "file-ops.h" | 41 #include "file-ops.h" |
41 #include "glob-match.h" | 42 #include "glob-match.h" |
42 #include "lo-mappers.h" | 43 #include "lo-mappers.h" |
43 #include "mach-info.h" | 44 #include "mach-info.h" |
45 #include "oct-env.h" | |
44 #include "str-vec.h" | 46 #include "str-vec.h" |
45 | 47 |
46 #include "defun.h" | 48 #include "defun.h" |
47 #include "error.h" | 49 #include "error.h" |
48 #include "gripes.h" | 50 #include "gripes.h" |
53 #include "symtab.h" | 55 #include "symtab.h" |
54 #include "sysdep.h" | 56 #include "sysdep.h" |
55 #include "unwind-prot.h" | 57 #include "unwind-prot.h" |
56 #include "utils.h" | 58 #include "utils.h" |
57 #include "variables.h" | 59 #include "variables.h" |
60 #include "version.h" | |
58 | 61 |
59 // The default output format. May be one of "binary", "text", or | 62 // The default output format. May be one of "binary", "text", or |
60 // "mat-binary". | 63 // "mat-binary". |
61 static string Vdefault_save_format; | 64 static string Vdefault_save_format; |
62 | 65 |
2364 | 2367 |
2365 return retval; | 2368 return retval; |
2366 } | 2369 } |
2367 | 2370 |
2368 static void | 2371 static void |
2369 write_binary_header (ostream& os, load_save_format format) | 2372 write_header (ostream& os, load_save_format format) |
2370 { | 2373 { |
2371 if (format == LS_BINARY) | 2374 switch (format) |
2372 { | 2375 { |
2373 os << (oct_mach_info::words_big_endian () | 2376 case LS_BINARY: |
2374 ? "Octave-1-B" : "Octave-1-L"); | 2377 { |
2375 | 2378 os << (oct_mach_info::words_big_endian () |
2376 oct_mach_info::float_format flt_fmt = | 2379 ? "Octave-1-B" : "Octave-1-L"); |
2377 oct_mach_info::native_float_format (); | 2380 |
2378 | 2381 oct_mach_info::float_format flt_fmt = |
2379 char tmp = (char) float_format_to_mopt_digit (flt_fmt); | 2382 oct_mach_info::native_float_format (); |
2380 | 2383 |
2381 os.write (&tmp, 1); | 2384 char tmp = (char) float_format_to_mopt_digit (flt_fmt); |
2385 | |
2386 os.write (&tmp, 1); | |
2387 } | |
2388 break; | |
2389 | |
2390 case LS_ASCII: | |
2391 { | |
2392 time_t now = time (0); | |
2393 | |
2394 string time_string = asctime (gmtime (&now)); | |
2395 time_string = time_string.substr (0, time_string.length () - 1); | |
2396 | |
2397 os << "# Created by Octave " OCTAVE_VERSION ", " | |
2398 << time_string | |
2399 << " <" | |
2400 << octave_env::get_user_name () | |
2401 << "@" | |
2402 << octave_env::get_host_name () | |
2403 << ">" << "\n"; | |
2404 } | |
2405 break; | |
2406 | |
2407 default: | |
2408 break; | |
2382 } | 2409 } |
2383 } | 2410 } |
2384 | 2411 |
2385 static void | 2412 static void |
2386 save_vars (const string_vector& argv, int argv_idx, int argc, | 2413 save_vars (const string_vector& argv, int argv_idx, int argc, |
2387 ostream& os, bool save_builtins, load_save_format fmt, | 2414 ostream& os, bool save_builtins, load_save_format fmt, |
2388 bool save_as_floats) | 2415 bool save_as_floats, bool write_header_info) |
2389 { | 2416 { |
2390 write_binary_header (os, fmt); | 2417 if (write_header_info) |
2418 write_header (os, fmt); | |
2391 | 2419 |
2392 if (argv_idx == argc) | 2420 if (argv_idx == argc) |
2393 { | 2421 { |
2394 save_vars (os, "*", save_builtins, fmt, save_as_floats); | 2422 save_vars (os, "*", save_builtins, fmt, save_as_floats); |
2395 } | 2423 } |
2422 | 2450 |
2423 ofstream file (fname, mode); | 2451 ofstream file (fname, mode); |
2424 | 2452 |
2425 if (file) | 2453 if (file) |
2426 { | 2454 { |
2427 save_vars (string_vector (), 0, 0, file, false, format, false); | 2455 save_vars (string_vector (), 0, 0, file, false, format, false, true); |
2428 message (0, "save to `%s' complete", fname); | 2456 message (0, "save to `%s' complete", fname); |
2429 } | 2457 } |
2430 else | 2458 else |
2431 warning ("unable to open `%s' for writing...", fname); | 2459 warning ("unable to open `%s' for writing...", fname); |
2432 } | 2460 } |
2433 | 2461 |
2434 DEFUN_TEXT (save, args, , | 2462 DEFUN_TEXT (save, args, , |
2435 "save [-ascii] [-binary] [-float-binary] [-mat-binary] \n\ | 2463 "save [-append] [-ascii] [-binary] [-float-binary] [-mat-binary] \n\ |
2436 [-save-builtins] file [pattern ...]\n\ | 2464 [-save-builtins] file [pattern ...]\n\ |
2437 \n\ | 2465 \n\ |
2438 save variables in a file") | 2466 save variables in a file") |
2439 { | 2467 { |
2440 octave_value_list retval; | 2468 octave_value_list retval; |
2453 | 2481 |
2454 bool save_as_floats = false; | 2482 bool save_as_floats = false; |
2455 | 2483 |
2456 load_save_format format = get_default_save_format (); | 2484 load_save_format format = get_default_save_format (); |
2457 | 2485 |
2486 bool append = false; | |
2487 | |
2458 int i; | 2488 int i; |
2459 for (i = 1; i < argc; i++) | 2489 for (i = 1; i < argc; i++) |
2460 { | 2490 { |
2491 if (argv[i] == "-append") | |
2492 { | |
2493 append = true; | |
2494 } | |
2461 if (argv[i] == "-ascii" || argv[i] == "-a") | 2495 if (argv[i] == "-ascii" || argv[i] == "-a") |
2462 { | 2496 { |
2463 format = LS_ASCII; | 2497 format = LS_ASCII; |
2464 } | 2498 } |
2465 else if (argv[i] == "-binary" || argv[i] == "-b") | 2499 else if (argv[i] == "-binary" || argv[i] == "-b") |
2501 | 2535 |
2502 // XXX FIXME XXX -- should things intended for the screen end up | 2536 // XXX FIXME XXX -- should things intended for the screen end up |
2503 // in a octave_value (string)? | 2537 // in a octave_value (string)? |
2504 | 2538 |
2505 save_vars (argv, i, argc, octave_stdout, save_builtins, format, | 2539 save_vars (argv, i, argc, octave_stdout, save_builtins, format, |
2506 save_as_floats); | 2540 save_as_floats, true); |
2507 } | 2541 } |
2508 | 2542 |
2509 // Guard against things like `save a*', which are probably mistakes... | 2543 // Guard against things like `save a*', which are probably mistakes... |
2510 | 2544 |
2511 else if (i == argc - 1 && glob_pattern_p (argv[i])) | 2545 else if (i == argc - 1 && glob_pattern_p (argv[i])) |
2517 { | 2551 { |
2518 string fname = file_ops::tilde_expand (argv[i]); | 2552 string fname = file_ops::tilde_expand (argv[i]); |
2519 | 2553 |
2520 i++; | 2554 i++; |
2521 | 2555 |
2522 unsigned mode = ios::out|ios::trunc; | 2556 unsigned mode = ios::out; |
2523 if (format == LS_BINARY || format == LS_MAT_BINARY) | 2557 if (format == LS_BINARY || format == LS_MAT_BINARY) |
2524 mode |= ios::bin; | 2558 mode |= ios::bin; |
2525 | 2559 |
2560 mode |= append ? ios::ate : ios::trunc; | |
2561 | |
2526 ofstream file (fname.c_str (), mode); | 2562 ofstream file (fname.c_str (), mode); |
2527 | 2563 |
2528 if (file) | 2564 if (file) |
2529 { | 2565 { |
2566 bool write_header_info | |
2567 = ((file.rdbuf ())->seekoff (0, ios::cur) == 0); | |
2568 | |
2530 save_vars (argv, i, argc, file, save_builtins, format, | 2569 save_vars (argv, i, argc, file, save_builtins, format, |
2531 save_as_floats); | 2570 save_as_floats, write_header_info); |
2532 } | 2571 } |
2533 else | 2572 else |
2534 { | 2573 { |
2535 error ("save: couldn't open output file `%s'", fname.c_str ()); | 2574 error ("save: couldn't open output file `%s'", fname.c_str ()); |
2536 return retval; | 2575 return retval; |