Mercurial > hg > octave-nkf
comparison src/load-save.cc @ 667:b19a14bbd862
[project @ 1994-09-07 04:09:03 by jwe]
author | jwe |
---|---|
date | Wed, 07 Sep 1994 04:09:03 +0000 |
parents | 1787dc40c811 |
children | e9adf666b647 |
comparison
equal
deleted
inserted
replaced
666:fb4f6556b443 | 667:b19a14bbd862 |
---|---|
35 | 35 |
36 #include "tree-base.h" | 36 #include "tree-base.h" |
37 #include "tree-expr.h" | 37 #include "tree-expr.h" |
38 #include "tree-const.h" | 38 #include "tree-const.h" |
39 #include "user-prefs.h" | 39 #include "user-prefs.h" |
40 #include "unwind-prot.h" | |
40 #include "load-save.h" | 41 #include "load-save.h" |
41 #include "symtab.h" | 42 #include "symtab.h" |
42 #include "pager.h" | 43 #include "pager.h" |
43 #include "error.h" | 44 #include "error.h" |
44 #include "defun.h" | 45 #include "defun.h" |
2369 } | 2370 } |
2370 | 2371 |
2371 return (os && ! fail); | 2372 return (os && ! fail); |
2372 } | 2373 } |
2373 | 2374 |
2375 // Save the data from TC along with the corresponding NAME on stream OS | |
2376 // in the MatLab binary format. | |
2377 | |
2378 static int | |
2379 save_mat_binary_data (ostream& os, const tree_constant& tc, char *name) | |
2380 { | |
2381 int fail = 0; | |
2382 | |
2383 FOUR_BYTE_INT mopt = 0; | |
2384 | |
2385 mopt += tc.is_string () ? 1 : 0; | |
2386 mopt += 1000 * get_floating_point_format (NATIVE_FLOAT_FORMAT); | |
2387 | |
2388 os.write (&mopt, 4); | |
2389 | |
2390 FOUR_BYTE_INT nr = tc.rows (); | |
2391 os.write (&nr, 4); | |
2392 | |
2393 FOUR_BYTE_INT nc = tc.columns (); | |
2394 os.write (&nc, 4); | |
2395 | |
2396 int len = nr * nc; | |
2397 | |
2398 FOUR_BYTE_INT imag = tc.is_complex_type () ? 1 : 0; | |
2399 os.write (&imag, 4); | |
2400 | |
2401 FOUR_BYTE_INT name_len = name ? strlen (name) + 1 : 0; | |
2402 | |
2403 os.write (&name_len, 4); | |
2404 os.write (name, name_len); | |
2405 | |
2406 if (tc.is_real_scalar ()) | |
2407 { | |
2408 double tmp = tc.double_value (); | |
2409 os.write (&tmp, 8); | |
2410 } | |
2411 else if (tc.is_real_matrix ()) | |
2412 { | |
2413 Matrix m = tc.matrix_value (); | |
2414 os.write (m.data (), 8 * len); | |
2415 } | |
2416 else if (tc.is_complex_scalar ()) | |
2417 { | |
2418 Complex tmp = tc.complex_value (); | |
2419 os.write (&tmp, 16); | |
2420 } | |
2421 else if (tc.is_complex_matrix ()) | |
2422 { | |
2423 ComplexMatrix m_cmplx = tc.complex_matrix_value (); | |
2424 Matrix m = ::real(m_cmplx); | |
2425 os.write (m.data (), 8 * len); | |
2426 m = ::imag(m_cmplx); | |
2427 os.write (m.data (), 8 * len); | |
2428 } | |
2429 else if (tc.is_string ()) | |
2430 { | |
2431 begin_unwind_frame ("save_mat_binary_data"); | |
2432 unwind_protect_int (user_pref.implicit_str_to_num_ok); | |
2433 user_pref.implicit_str_to_num_ok = 1; | |
2434 Matrix m = tc.matrix_value (); | |
2435 os.write (m.data (), 8 * len); | |
2436 run_unwind_frame ("save_mat_binary_data"); | |
2437 } | |
2438 else | |
2439 { | |
2440 gripe_wrong_type_arg ("save", tc); | |
2441 fail = 1; | |
2442 } | |
2443 | |
2444 return (os && ! fail); | |
2445 } | |
2446 | |
2374 static void | 2447 static void |
2375 ascii_save_type (ostream& os, char *type, int mark_as_global) | 2448 ascii_save_type (ostream& os, char *type, int mark_as_global) |
2376 { | 2449 { |
2377 if (mark_as_global) | 2450 if (mark_as_global) |
2378 os << "# type: global "; | 2451 os << "# type: global "; |
2483 | 2556 |
2484 case LS_BINARY: | 2557 case LS_BINARY: |
2485 save_binary_data (os, tc, name, help, global, save_as_floats); | 2558 save_binary_data (os, tc, name, help, global, save_as_floats); |
2486 break; | 2559 break; |
2487 | 2560 |
2561 case LS_MAT_BINARY: | |
2562 save_mat_binary_data (os, tc, name); | |
2563 break; | |
2564 | |
2488 default: | 2565 default: |
2489 panic_impossible (); | 2566 panic_impossible (); |
2490 break; | 2567 break; |
2491 } | 2568 } |
2492 } | 2569 } |
2551 | 2628 |
2552 return retval; | 2629 return retval; |
2553 } | 2630 } |
2554 | 2631 |
2555 DEFUN_TEXT ("save", Fsave, Ssave, -1, 1, | 2632 DEFUN_TEXT ("save", Fsave, Ssave, -1, 1, |
2556 "save [-ascii] [-binary] [-float-binary] [-save-builtins] file [pattern ...]\n\ | 2633 "save [-ascii] [-binary] [-float-binary] [-mat-binary] \ |
2634 [-save-builtins] file [pattern ...]\n\ | |
2557 \n\ | 2635 \n\ |
2558 save variables in a file") | 2636 save variables in a file") |
2559 { | 2637 { |
2560 Octave_object retval; | 2638 Octave_object retval; |
2561 | 2639 |
2582 argv++; | 2660 argv++; |
2583 } | 2661 } |
2584 else if (strcmp (*argv, "-binary") == 0 || strcmp (*argv, "-b") == 0) | 2662 else if (strcmp (*argv, "-binary") == 0 || strcmp (*argv, "-b") == 0) |
2585 { | 2663 { |
2586 format = LS_BINARY; | 2664 format = LS_BINARY; |
2665 argc--; | |
2666 argv++; | |
2667 } | |
2668 else if (strcmp (*argv, "-mat-binary") == 0 || strcmp (*argv, "-m") == 0) | |
2669 { | |
2670 format = LS_MAT_BINARY; | |
2587 argc--; | 2671 argc--; |
2588 argv++; | 2672 argv++; |
2589 } | 2673 } |
2590 else if (strcmp (*argv, "-float-binary") == 0 | 2674 else if (strcmp (*argv, "-float-binary") == 0 |
2591 || strcmp (*argv, "-f") == 0) | 2675 || strcmp (*argv, "-f") == 0) |