26 #if defined (HAVE_CONFIG_H)
90 #if defined (HAVE_ZLIB)
96 OCTAVE_NORETURN
static
98 err_file_open (
const std::string& fcn,
const std::string& file)
101 error (
"%s: unable to open input file '%s'", fcn.c_str (), file.c_str ());
102 else if (fcn ==
"save")
103 error (
"%s: unable to open output file '%s'", fcn.c_str (), file.c_str ());
105 error (
"%s: unable to open file '%s'", fcn.c_str (), file.c_str ());
111 matches_patterns (
const string_vector& patterns,
int pat_idx,
112 int num_pat,
const std::string& name)
114 for (
int i = pat_idx; i < num_pat; i++)
118 if (pattern.match (name))
126 read_binary_file_header (std::istream& is,
bool& swap,
130 const int magic_len = 10;
131 char magic[magic_len+1];
132 is.read (magic, magic_len);
133 magic[magic_len] =
'\0';
135 if (
strncmp (magic,
"Octave-1-L", magic_len) == 0)
137 else if (
strncmp (magic,
"Octave-1-B", magic_len) == 0)
142 error (
"load: unable to read binary file");
155 error (
"load: unrecognized binary format!");
163 #if defined (HAVE_ZLIB)
165 check_gzip_magic (
const std::string& fname)
170 std::ios::in | std::ios::binary);
172 unsigned char magic[2];
173 if (file.read (
reinterpret_cast<char *
> (&magic[0]), 2)
174 && magic[0] == 0x1f && magic[1] == 0x8b)
184 find_file_to_load (
const std::string& name,
const std::string& orig_name)
188 std::size_t dot_pos = fname.rfind (
'.');
191 if (dot_pos == std::string::npos
192 || (sep_pos != std::string::npos && dot_pos < sep_pos))
198 fname = find_file_to_load (fname +
".mat", orig_name);
206 error (
"load: unable to find file %s", orig_name.c_str ());
216 glob_pattern_p (
const std::string& pattern)
220 int len = pattern.length ();
222 for (
int i = 0; i <
len; i++)
255 : m_interpreter (interp),
256 m_crash_dumps_octave_core (true),
257 m_octave_core_file_limit (-1.0),
258 m_octave_core_file_name (
"octave-workspace"),
259 m_save_default_options (
"-text"),
260 m_octave_core_file_options (
"-binary"),
261 m_save_header_format_string (init_save_header_format ())
263 #if defined (HAVE_HDF5)
270 #if defined (HAVE_HDF5)
280 "crash_dumps_octave_core");
288 "octave_core_file_limit");
296 "octave_core_file_name",
false);
304 "save_default_options",
false);
312 "octave_core_file_options",
false);
320 "save_header_format_string");
325 const std::string& orig_fname,
326 bool& use_zlib,
bool quiet)
330 #if defined (HAVE_HDF5_UTF8)
331 std::string ascii_fname = fname;
336 #if defined (HAVE_HDF5)
338 if (H5Fis_hdf5 (ascii_fname.c_str ()) > 0)
342 #if defined (HAVE_ZLIB)
343 use_zlib = check_gzip_magic (fname);
351 std::ios::in | std::ios::binary);
358 err_file_open (
"load", orig_fname);
360 #if defined (HAVE_ZLIB)
363 gzifstream gzfile (fname.c_str (), std::ios::in | std::ios::binary);
370 err_file_open (
"load", orig_fname);
379 const std::string& orig_fname,
382 bool list_only,
bool swap,
bool verbose,
384 int argc,
int nargout)
390 std::ostringstream output_buf;
391 std::list<std::string> symbol_names;
422 #if defined (HAVE_HDF5)
425 argv, argv_idx, argc);
445 error (
"load: unable to load variable '%s'", name.c_str ());
448 warning (
"load: loaded ASCII file '%s' -- ignoring extra args",
449 orig_fname.c_str ());
453 || matches_patterns (argv, argv_idx, argc, name))
462 <<
"type rows cols name\n"
463 <<
"==== ==== ==== ====\n";
466 << std::setiosflags (std::ios::left)
467 << std::setw (16) << tc.
type_name ().c_str ()
468 << std::setiosflags (std::ios::right)
469 << std::setw (7) << tc.
rows ()
470 << std::setw (7) << tc.
columns ()
471 <<
" " << name <<
"\n";
474 symbol_names.push_back (name);
483 retstruct.
assign (name, tc);
486 install_loaded_variable (name, tc, global, doc);
497 if (list_only && count)
501 std::string msg = output_buf.str ();
522 else if (retstruct.
nfields () != 0)
531 bool& save_as_floats,
bool& use_zlib)
533 #if ! defined (HAVE_ZLIB)
534 octave_unused_parameter (use_zlib);
538 int argc = argv.
numel ();
540 bool do_double =
false;
541 bool do_tabs =
false;
543 for (
int i = 0; i < argc; i++)
545 if (argv[i] ==
"-append")
549 else if (argv[i] ==
"-ascii" || argv[i] ==
"-a")
553 else if (argv[i] ==
"-double")
557 else if (argv[i] ==
"-tabs")
561 else if (argv[i] ==
"-text" || argv[i] ==
"-t")
565 else if (argv[i] ==
"-binary" || argv[i] ==
"-b")
569 else if (argv[i] ==
"-hdf5" || argv[i] ==
"-h")
571 #if defined (HAVE_HDF5)
577 else if (argv[i] ==
"-v7.3" || argv[i] ==
"-V7.3" || argv[i] ==
"-7.3")
579 error (
"save: Matlab file format -v7.3 is not yet implemented");
581 #if defined (HAVE_ZLIB)
582 else if (argv[i] ==
"-v7" || argv[i] ==
"-V7" || argv[i] ==
"-7"
583 || argv[i] ==
"-mat7-binary")
588 else if (argv[i] ==
"-mat" || argv[i] ==
"-m"
589 || argv[i] ==
"-v6" || argv[i] ==
"-V6" || argv[i] ==
"-6"
590 || argv[i] ==
"-mat-binary")
594 else if (argv[i] ==
"-v4" || argv[i] ==
"-V4" || argv[i] ==
"-4"
595 || argv[i] ==
"-mat4-binary")
599 else if (argv[i] ==
"-float-binary" || argv[i] ==
"-f")
602 save_as_floats =
true;
604 else if (argv[i] ==
"-float-hdf5")
606 #if defined (HAVE_HDF5)
608 save_as_floats =
true;
613 #if defined (HAVE_ZLIB)
614 else if (argv[i] ==
"-zip" || argv[i] ==
"-z")
619 else if (argv[i] ==
"-struct")
623 else if (argv[i][0] ==
'-' && argv[i] !=
"-")
625 error (
"save: Unrecognized option '%s'", argv[i].c_str ());
636 warning (R
"(save: "-double" option only has an effect with "-ascii")");
644 warning (R
"(save: "-tabs" option only has an effect with "-ascii")");
647 if (append && use_zlib
649 error (
"save: -append and -zip options can only be used together with a text format (-text or -ascii)");
657 bool& append,
bool& save_as_floats,
660 std::istringstream is (arg);
675 int argc, std::ostream& os,
678 bool write_header_info)
680 if (write_header_info)
681 write_header (os, fmt);
683 if (argv_idx == argc)
685 save_vars (os,
"*", fmt, save_as_floats);
687 else if (argv[argv_idx] ==
"-struct")
689 if (++argv_idx >= argc)
690 error (
"save: missing struct name");
692 std::string struct_name = argv[argv_idx];
695 error (
"save: no such variable: '%s'", struct_name.c_str ());
700 error (
"save: '%s' is not a scalar structure", struct_name.c_str ());
708 for (
int i = argv_idx; i < argc; i++)
710 if (! save_fields (os, struct_var_map, argv[i], fmt,
713 warning (
"save: no such field '%s.%s'",
714 struct_name.c_str (), argv[i].c_str ());
719 save_fields (os, struct_var_map,
"*", fmt, save_as_floats);
723 for (
int i = argv_idx; i < argc; i++)
727 if (!
save_vars (os, argv[i], fmt, save_as_floats))
728 warning (
"save: no such variable '%s'", argv[i].c_str ());
736 if (m_crash_dumps_octave_core)
740 const char *fname = m_octave_core_file_name.c_str ();
742 message (
nullptr,
"attempting to save variables to '%s'...", fname);
746 bool save_as_floats =
false;
750 bool use_zlib =
false;
753 fmt, append, save_as_floats,
756 std::ios::openmode mode = std::ios::out;
763 #
if defined (HAVE_HDF5)
769 mode |= std::ios::binary;
773 #if defined (HAVE_HDF5)
785 warning (
"dump_octave_core: unable to open '%s' for writing...",
793 #if defined (HAVE_ZLIB)
805 warning (
"dump_octave_core: unable to open '%s' for writing...",
820 warning (
"dump_octave_core: unable to open '%s' for writing...",
828 load_save_system::write_header (std::ostream& os,
836 ?
"Octave-1-B" :
"Octave-1-L");
849 char const *versionmagic;
850 char headertext[128];
854 const char *matlab_format =
"MATLAB 5.0 MAT-file, written by Octave "
856 std::string comment_string = now.strftime (matlab_format);
858 std::size_t
len =
std::min (comment_string.length (),
859 static_cast<std::size_t
> (124));
860 memset (headertext,
' ', 124);
861 memcpy (headertext, comment_string.data (),
len);
871 versionmagic =
"\x01\x00\x4d\x49";
873 versionmagic =
"\x00\x01\x49\x4d";
875 memcpy (headertext+124, versionmagic, 4);
876 os.write (headertext, 128);
881 #if defined (HAVE_HDF5)
888 std::string comment_string = now.strftime (m_save_header_format_string);
890 if (! comment_string.empty ())
892 #if defined (HAVE_HDF5)
896 H5Gset_comment (hs.
file_id,
"/", comment_string.c_str ());
900 os << comment_string <<
"\n";
915 const std::string& pattern,
923 std::size_t saved = 0;
925 for (
const auto& syminfo : syminfo_list)
927 do_save (os, syminfo, fmt, save_as_floats);
936 load_save_system::do_save (std::ostream& os,
const octave_value& tc,
937 const std::string& name,
938 const std::string& help,
956 warning (
"save: unable to save %s in ASCII format", name.c_str ());
963 #if defined (HAVE_HDF5)
986 load_save_system::do_save (std::ostream& os,
995 std::string name = syminfo.
name ();
999 do_save (os, val, name, help, global, fmt, save_as_floats);
1007 load_save_system::save_fields (std::ostream& os,
1009 const std::string& pattern,
1011 bool save_as_floats)
1015 std::size_t saved = 0;
1017 for (
auto it =
m.begin (); it !=
m.end (); it++)
1019 std::string empty_str;
1021 if (pat.match (
m.key (it)))
1023 do_save (os,
m.contents (it),
m.key (it), empty_str,
1024 0, fmt, save_as_floats);
1037 bool save_as_floats)
1039 write_header (os, fmt);
1045 double save_mem_size = 0;
1047 for (
const auto& syminfo : syminfo_list)
1051 std::string name = syminfo.
name ();
1055 double val_size = val.
byte_size () / 1024;
1059 if (m_octave_core_file_limit < 0
1060 || save_mem_size + val_size < m_octave_core_file_limit)
1062 save_mem_size += val_size;
1064 do_save (os, val, name, help, global, fmt, save_as_floats);
1068 message (
nullptr,
"save to '%s' complete", fname);
1075 load_save_system::install_loaded_variable (
const std::string& name,
1078 const std::string& )
1084 load_save_system::init_save_header_format ()
1088 ", %a %b %d %H:%M:%S %Y %Z <")
1089 + sys::env::get_user_name ()
1091 + sys::env::get_host_name ()
1097 const std::string& filename)
1106 if (read_binary_file_header (file, swap, flt_fmt,
true) == 0)
1111 file.seekg (0, std::ios::beg);
1113 int32_t mopt, nr, nc,
imag,
len;
1123 file.seekg (0, std::ios::beg);
1130 file.seekg (0, std::ios::beg);
1136 file.seekg (0, std::ios::beg);
1141 if (name_val.empty () !=
true && type_val.empty () !=
true)
1146 file.seekg (0, std::ios::beg);
1171 int argc = args.
length () + 1;
1176 std::string orig_fname =
"";
1179 if (argc > 1 && ! argv[1].empty () && argv[1].at (0) !=
'-')
1181 orig_fname = argv[1];
1191 bool list_only =
false;
1192 bool verbose =
false;
1194 for (; i < argc; i++)
1196 if (argv[i] ==
"-text" || argv[i] ==
"-t")
1200 else if (argv[i] ==
"-binary" || argv[i] ==
"-b")
1204 else if (argv[i] ==
"-hdf5" || argv[i] ==
"-h")
1206 #if defined (HAVE_HDF5)
1212 else if (argv[i] ==
"-ascii" || argv[i] ==
"-a")
1216 else if (argv[i] ==
"-v7.3" || argv[i] ==
"-V7.3" || argv[i] ==
"-7.3")
1218 error (
"load: Matlab file format -v7.3 is not yet implemented");
1220 else if (argv[i] ==
"-v7" || argv[i] ==
"-V7" || argv[i] ==
"-7"
1221 || argv[i] ==
"-mat7-binary")
1225 else if (argv[i] ==
"-mat" || argv[i] ==
"-m"
1226 || argv[i] ==
"-v6" || argv[i] ==
"-V6" || argv[i] ==
"-6"
1227 || argv[i] ==
"-mat-binary")
1231 else if (argv[i] ==
"-v4" || argv[i] ==
"-V4" || argv[i] ==
"-4"
1232 || argv[i] ==
"-mat4-binary")
1236 else if (argv[i] ==
"-force" || argv[i] ==
"-f")
1241 else if (argv[i] ==
"-import" || argv[i] ==
"-i")
1243 warning (
"load: -import ignored");
1245 else if (argv[i] ==
"-list" || argv[i] ==
"-l")
1249 else if (argv[i] ==
"-verbose" || argv[i] ==
"-v")
1257 if (orig_fname ==
"")
1262 orig_fname = argv[i];
1271 if (orig_fname ==
"-")
1275 #if defined (HAVE_HDF5)
1277 error (
"load: cannot read HDF5 format from stdin");
1288 list_only, swap, verbose, argv, i,
1292 error (
"load: must specify file format if reading from stdin");
1298 fname = find_file_to_load (fname, orig_fname);
1300 bool use_zlib =
false;
1305 #if defined (HAVE_HDF5)
1313 err_file_open (
"load", orig_fname);
1316 list_only, swap, verbose, argv, i,
1331 std::ios::openmode mode = std::ios::in | std::ios::binary;
1333 #if defined (HAVE_ZLIB)
1339 err_file_open (
"load", orig_fname);
1343 if (read_binary_file_header (file, swap, flt_fmt) < 0)
1345 if (file) file.
close ();
1355 if (file) file.
close ();
1361 list_only, swap, verbose, argv, i,
1372 error (
"load: unable to open input file '%s'",
1373 orig_fname.c_str ());
1377 if (read_binary_file_header (file, swap, flt_fmt) < 0)
1379 if (file) file.close ();
1389 if (file) file.close ();
1395 list_only, swap, verbose, argv, i,
1402 error (
"load: unable to determine file format of '%s'",
1403 orig_fname.c_str ());
1416 bool save_as_floats =
false;
1417 bool append =
false;
1418 bool use_zlib =
false;
1423 save_as_floats, use_zlib);
1430 int argc = argv.
numel ();
1436 if (save_as_floats &&
format.type () ==
TEXT)
1437 error (
"save: cannot specify both -text and -float-binary");
1445 #if defined (HAVE_HDF5)
1447 error (
"save: cannot write HDF5 format to stdout");
1454 warning (
"save: ignoring -append option for output to stdout");
1458 save_as_floats,
true);
1461 std::ostringstream output_buf;
1463 save_as_floats,
true);
1471 else if (i == argc - 1 && glob_pattern_p (argv[i]))
1484 std::string fname = desiredname + (append ?
"" :
".saving_in_progress");
1492 std::ios::openmode mode
1493 = (append ? (std::ios::app | std::ios::ate) : std::ios::out);
1496 mode |= std::ios::binary;
1498 #if defined (HAVE_HDF5)
1503 error (
"save: appending to HDF5 files is not implemented");
1505 # if defined (HAVE_HDF5_UTF8)
1506 bool write_header_info
1507 = ! (append && H5Fis_hdf5 (fname.c_str ()) > 0);
1511 bool write_header_info
1512 = ! (append && H5Fis_hdf5 (ascii_fname.c_str ()) > 0);
1518 err_file_open (
"save", fname);
1530 #if defined (HAVE_ZLIB)
1536 err_file_open (
"save", fname);
1538 bool write_header_info = ! file.tellp ();
1551 err_file_open (
"save", fname);
1553 bool write_header_info = ! file.tellp ();
1572 error (
"save: unable to save to %s %s",
1573 desiredname.c_str (), msg.c_str ());
1694 return load_save_sys.
load (args, nargout);
1850 return load_save_sys.
save (args, nargout);
1950 DEFMETHOD (crash_dumps_octave_core, interp, args, nargout,
1971 DEFMETHOD (save_default_options, interp, args, nargout,
1993 DEFMETHOD (octave_core_file_limit, interp, args, nargout,
2022 DEFMETHOD (octave_core_file_name, interp, args, nargout,
2044 DEFMETHOD (octave_core_file_options, interp, args, nargout,
2067 DEFMETHOD (save_header_format_string, interp, args, nargout,
2097 OCTAVE_END_NAMESPACE(
octave)
charNDArray min(char d, const charNDArray &m)
Gzipped file input stream class.
void close()
Close gzipped file.
Gzipped file output stream class.
void close()
Close gzipped file.
octave_value varval(const std::string &name) const
void install_variable(const std::string &name, const octave_value &value, bool global)
bool is_variable(const std::string &name) const
tree_evaluator & get_evaluator()
octave_value save_header_format_string(const octave_value_list &args, int nargout)
octave_value_list save(const octave_value_list &args=octave_value_list(), int nargout=0)
std::string save_header_format_string() const
octave_value octave_core_file_name(const octave_value_list &args, int nargout)
void save_vars(const string_vector &argv, int argv_idx, int argc, std::ostream &os, const load_save_format &fmt, bool save_as_floats, bool write_header_info)
octave_value octave_core_file_options(const octave_value_list &args, int nargout)
bool crash_dumps_octave_core() const
octave_value save_default_options(const octave_value_list &args, int nargout)
octave_value crash_dumps_octave_core(const octave_value_list &args, int nargout)
std::string octave_core_file_name() const
octave_value load_vars(std::istream &stream, const std::string &orig_fname, const load_save_format &fmt, mach_info::float_format flt_fmt, bool list_only, bool swap, bool verbose, const string_vector &argv, int argv_idx, int argc, int nargout)
std::string octave_core_file_options() const
double octave_core_file_limit() const
octave_value_list load(const octave_value_list &args=octave_value_list(), int nargout=0)
static load_save_format get_file_format(const std::string &fname, const std::string &orig_fname, bool &use_zlib, bool quiet=false)
load_save_system(interpreter &interp)
std::string save_default_options() const
octave_value octave_core_file_limit(const octave_value_list &args, int nargout)
static string_vector parse_save_options(const string_vector &argv, load_save_format &fmt, bool &append, bool &save_as_floats, bool &use_zlib)
void assign(const std::string &k, const octave_value &val)
octave_idx_type nfields() const
string_vector make_argv(const std::string &="") const
octave_idx_type length() const
octave_idx_type rows() const
octave_scalar_map scalar_map_value() const
octave_idx_type numel() const
std::string type_name() const
std::size_t byte_size() const
octave_idx_type columns() const
string_vector & append(const std::string &s)
std::ostream & list_in_columns(std::ostream &, int width=0, const std::string &prefix="") const
octave_idx_type numel() const
octave_value value() const
symbol_info_list top_scope_symbol_info() const
symbol_info_list glob_symbol_info(const std::string &pattern) const
ColumnVector imag(const ComplexColumnVector &a)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define DEFMETHOD(name, interp_name, args_name, nargout_name, doc)
Macro to define a builtin method.
void warning(const char *fmt,...)
void() error(const char *fmt,...)
void message(const char *name, const char *fmt,...)
void err_unrecognized_data_fmt(const char *name)
void err_disabled_feature(const std::string &fcn, const std::string &feature, const std::string &pkg)
std::string dir_sep_chars()
std::complex< T > trunc(const std::complex< T > &x)
std::string get_ASCII_filename(const std::string &orig_file_name, const bool allow_locale)
bool file_exists(const std::string &filename, bool is_dir)
std::ofstream ofstream(const std::string &filename, const std::ios::openmode mode)
std::ifstream ifstream(const std::string &filename, const std::ios::openmode mode)
bool save_hdf5_data(std::ostream &os, const octave_value &tc, const std::string &name, const std::string &doc, bool mark_global, bool save_as_floats)
std::string read_hdf5_data(std::istream &is, const std::string &, bool &global, octave_value &tc, std::string &doc, const string_vector &argv, int argv_idx, int argc)
bool save_mat_ascii_data(std::ostream &os, const octave_value &val, int precision, bool tabs)
std::string read_mat_ascii_data(std::istream &is, const std::string &filename, octave_value &tc)
bool looks_like_mat_ascii_file(std::istream &is, const std::string &filename)
octave::mach_info::float_format mopt_digit_to_float_format(int mach)
bool save_mat_binary_data(std::ostream &os, const octave_value &tc, const std::string &name)
int read_mat_file_header(std::istream &is, bool &swap, int32_t &mopt, int32_t &nr, int32_t &nc, int32_t &imag, int32_t &len, int quiet)
std::string read_mat_binary_data(std::istream &is, const std::string &filename, octave_value &tc)
int float_format_to_mopt_digit(octave::mach_info::float_format flt_fmt)
int read_mat5_binary_file_header(std::istream &is, bool &swap, bool quiet, const std::string &filename)
bool save_mat5_binary_element(std::ostream &os, const octave_value &tc, const std::string &name, bool mark_global, bool mat7_format, bool save_as_floats, bool compressing)
std::string read_mat5_binary_element(std::istream &is, const std::string &filename, bool swap, bool &global, octave_value &tc)
std::string read_binary_data(std::istream &is, bool swap, octave::mach_info::float_format fmt, const std::string &filename, bool &global, octave_value &tc, std::string &doc)
bool save_binary_data(std::ostream &os, const octave_value &tc, const std::string &name, const std::string &doc, bool mark_global, bool save_as_floats)
std::string read_text_data(std::istream &is, const std::string &filename, bool &global, octave_value &tc, octave_idx_type count, const bool do_name_validation)
std::string extract_keyword(std::istream &is, const char *keyword, const bool next_only)
bool save_text_data(std::ostream &os, const octave_value &val_arg, const std::string &name, bool mark_global, int precision)
float_format native_float_format()
std::string tilde_expand(const std::string &name)
int rename(const std::string &from, const std::string &to)
octave_value set_internal_variable(bool &var, const octave_value_list &args, int nargout, const char *nm)
bool strncmp(const T &str_a, const T &str_b, const typename T::size_type n)
True if the first N characters are the same.
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
std::string find_data_file_in_load_path(const std::string &fcn, const std::string &file, bool require_regular_file=false)
std::size_t format(std::ostream &os, const char *fmt,...)