26 #if defined (HAVE_CONFIG_H)
89 #if defined (HAVE_ZLIB)
95 OCTAVE_NORETURN
static
100 error (
"%s: unable to open input file '%s'", fcn.c_str (), file.c_str ());
101 else if (fcn ==
"save")
102 error (
"%s: unable to open output file '%s'", fcn.c_str (), file.c_str ());
104 error (
"%s: unable to open file '%s'", fcn.c_str (), file.c_str ());
111 int num_pat,
const std::string& name)
113 for (
int i = pat_idx; i < num_pat; i++)
117 if (pattern.
match (name))
129 const int magic_len = 10;
130 char magic[magic_len+1];
131 is.read (magic, magic_len);
132 magic[magic_len] =
'\0';
134 if (
strncmp (magic,
"Octave-1-L", magic_len) == 0)
136 else if (
strncmp (magic,
"Octave-1-B", magic_len) == 0)
141 error (
"load: unable to read binary file");
154 error (
"load: unrecognized binary format!");
162 #if defined (HAVE_ZLIB)
169 std::ios::in | std::ios::binary);
171 unsigned char magic[2];
172 if (file.read (
reinterpret_cast<char *
> (&magic[0]), 2)
173 && magic[0] == 0x1f && magic[1] == 0x8b)
187 std::size_t dot_pos = fname.rfind (
'.');
190 if (dot_pos == std::string::npos
191 || (sep_pos != std::string::npos && dot_pos < sep_pos))
196 sys::file_stat fs (fname);
198 if (! (fs.exists () && fs.is_reg ()))
203 sys::file_stat fs (fname);
205 if (! (fs.exists () && fs.is_reg ()))
209 error (
"load: unable to find file %s", orig_name.c_str ());
223 int len = pattern.length ();
225 for (
int i = 0; i <
len; i++)
258 : m_interpreter (interp),
259 m_crash_dumps_octave_core (true),
260 m_octave_core_file_limit (-1.0),
261 m_octave_core_file_name (
"octave-workspace"),
262 m_save_default_options (
"-text"),
263 m_octave_core_file_options (
"-binary"),
264 m_save_header_format_string (init_save_header_format ())
266 #if defined (HAVE_HDF5)
273 #if defined (HAVE_HDF5)
283 "crash_dumps_octave_core");
291 "octave_core_file_limit");
299 "octave_core_file_name",
false);
307 "save_default_options",
false);
315 "octave_core_file_options",
false);
323 "save_header_format_string");
328 const std::string& orig_fname,
329 bool& use_zlib,
bool quiet)
333 #if defined (HAVE_HDF5_UTF8)
334 std::string ascii_fname = fname;
339 #if defined (HAVE_HDF5)
341 if (H5Fis_hdf5 (ascii_fname.c_str ()) > 0)
345 #if defined (HAVE_ZLIB)
354 std::ios::in | std::ios::binary);
363 #if defined (HAVE_ZLIB)
366 gzifstream gzfile (fname.c_str (), std::ios::in | std::ios::binary);
382 const std::string& orig_fname,
385 bool list_only,
bool swap,
bool verbose,
387 int argc,
int nargout)
393 std::ostringstream output_buf;
394 std::list<std::string> symbol_names;
425 #if defined (HAVE_HDF5)
428 argv, argv_idx, argc);
448 error (
"load: unable to load variable '%s'", name.c_str ());
451 warning (
"load: loaded ASCII file '%s' -- ignoring extra args",
452 orig_fname.c_str ());
465 <<
"type rows cols name\n"
466 <<
"==== ==== ==== ====\n";
470 << std::setw (16) << tc.
type_name ().c_str ()
471 << std::setiosflags (std::ios::right)
472 << std::setw (7) << tc.
rows ()
473 << std::setw (7) << tc.
columns ()
474 <<
" " << name <<
"\n";
477 symbol_names.push_back (name);
486 retstruct.
assign (name, tc);
500 if (list_only && count)
504 std::string msg = output_buf.str ();
525 else if (retstruct.
nfields () != 0)
534 bool& save_as_floats,
bool& use_zlib)
536 #if ! defined (HAVE_ZLIB)
537 octave_unused_parameter (use_zlib);
541 int argc = argv.
numel ();
543 bool do_double =
false;
544 bool do_tabs =
false;
546 for (
int i = 0; i < argc; i++)
548 if (argv[i] ==
"-append")
552 else if (argv[i] ==
"-ascii" || argv[i] ==
"-a")
556 else if (argv[i] ==
"-double")
560 else if (argv[i] ==
"-tabs")
564 else if (argv[i] ==
"-text" || argv[i] ==
"-t")
568 else if (argv[i] ==
"-binary" || argv[i] ==
"-b")
572 else if (argv[i] ==
"-hdf5" || argv[i] ==
"-h")
574 #if defined (HAVE_HDF5)
580 else if (argv[i] ==
"-v7.3" || argv[i] ==
"-V7.3" || argv[i] ==
"-7.3")
582 error (
"save: Matlab file format -v7.3 is not yet implemented");
584 #if defined (HAVE_ZLIB)
585 else if (argv[i] ==
"-v7" || argv[i] ==
"-V7" || argv[i] ==
"-7"
586 || argv[i] ==
"-mat7-binary")
591 else if (argv[i] ==
"-mat" || argv[i] ==
"-m"
592 || argv[i] ==
"-v6" || argv[i] ==
"-V6" || argv[i] ==
"-6"
593 || argv[i] ==
"-mat-binary")
597 else if (argv[i] ==
"-v4" || argv[i] ==
"-V4" || argv[i] ==
"-4"
598 || argv[i] ==
"-mat4-binary")
602 else if (argv[i] ==
"-float-binary" || argv[i] ==
"-f")
605 save_as_floats =
true;
607 else if (argv[i] ==
"-float-hdf5")
609 #if defined (HAVE_HDF5)
611 save_as_floats =
true;
616 #if defined (HAVE_ZLIB)
617 else if (argv[i] ==
"-zip" || argv[i] ==
"-z")
622 else if (argv[i] ==
"-struct")
626 else if (argv[i][0] ==
'-' && argv[i] !=
"-")
628 error (
"save: Unrecognized option '%s'", argv[i].c_str ());
639 warning (R
"(save: "-double" option only has an effect with "-ascii")");
647 warning (R
"(save: "-tabs" option only has an effect with "-ascii")");
650 if (append && use_zlib
652 error (
"save: -append and -zip options can only be used together with a text format (-text or -ascii)");
660 bool& append,
bool& save_as_floats,
663 std::istringstream is (arg);
677 int argc, std::ostream& os,
680 bool write_header_info)
682 if (write_header_info)
685 if (argv_idx == argc)
687 save_vars (os,
"*", fmt, save_as_floats);
689 else if (argv[argv_idx] ==
"-struct")
691 if (++argv_idx >= argc)
692 error (
"save: missing struct name");
694 std::string struct_name = argv[argv_idx];
697 error (
"save: no such variable: '%s'", struct_name.c_str ());
702 error (
"save: '%s' is not a scalar structure", struct_name.c_str ());
710 for (
int i = argv_idx; i < argc; i++)
712 if (!
save_fields (os, struct_var_map, argv[i], fmt,
715 warning (
"save: no such field '%s.%s'",
716 struct_name.c_str (), argv[i].c_str ());
721 save_fields (os, struct_var_map,
"*", fmt, save_as_floats);
725 for (
int i = argv_idx; i < argc; i++)
729 if (!
save_vars (os, argv[i], fmt, save_as_floats))
730 warning (
"save: no such variable '%s'", argv[i].c_str ());
743 message (
nullptr,
"attempting to save variables to '%s'...", fname);
747 bool save_as_floats =
false;
751 bool use_zlib =
false;
754 fmt, append, save_as_floats,
757 std::ios::openmode mode = std::ios::out;
764 #
if defined (HAVE_HDF5)
770 mode |= std::ios::binary;
774 #if defined (HAVE_HDF5)
786 warning (
"dump_octave_core: unable to open '%s' for writing...",
794 #if defined (HAVE_ZLIB)
806 warning (
"dump_octave_core: unable to open '%s' for writing...",
821 warning (
"dump_octave_core: unable to open '%s' for writing...",
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)
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";
914 const std::string& pattern,
922 std::size_t saved = 0;
924 for (
const auto& syminfo : syminfo_list)
926 do_save (os, syminfo, fmt, save_as_floats);
935 const std::string& name,
936 const std::string& help,
954 warning (
"save: unable to save %s in ASCII format", name.c_str ());
961 #if defined (HAVE_HDF5)
992 std::string name = syminfo.
name ();
996 do_save (os, val, name, help, global, fmt, save_as_floats);
1005 const std::string& pattern,
1007 bool save_as_floats)
1011 std::size_t saved = 0;
1013 for (
auto it =
m.begin (); it !=
m.end (); it++)
1015 std::string empty_str;
1017 if (pat.
match (
m.key (it)))
1019 do_save (os,
m.contents (it),
m.key (it), empty_str,
1020 0, fmt, save_as_floats);
1032 bool save_as_floats)
1040 double save_mem_size = 0;
1042 for (
const auto& syminfo : syminfo_list)
1046 std::string name = syminfo.name ();
1048 bool global = syminfo.is_global ();
1050 double val_size = val.
byte_size () / 1024;
1057 save_mem_size += val_size;
1059 do_save (os, val, name, help, global, fmt, save_as_floats);
1063 message (
nullptr,
"save to '%s' complete", fname);
1072 const std::string& )
1081 ", %a %b %d %H:%M:%S %Y %Z <")
1082 + sys::env::get_user_name ()
1084 + sys::env::get_host_name ()
1090 const std::string& filename)
1104 file.seekg (0, std::ios::beg);
1106 int32_t mopt, nr, nc,
imag,
len;
1116 file.seekg (0, std::ios::beg);
1123 file.seekg (0, std::ios::beg);
1129 file.seekg (0, std::ios::beg);
1134 if (name_val.empty () !=
true && type_val.empty () !=
true)
1139 file.seekg (0, std::ios::beg);
1164 int argc = args.
length () + 1;
1169 std::string orig_fname =
"";
1172 if (argc > 1 && ! argv[1].empty () && argv[1].at (0) !=
'-')
1174 orig_fname = argv[1];
1184 bool list_only =
false;
1185 bool verbose =
false;
1187 for (; i < argc; i++)
1189 if (argv[i] ==
"-text" || argv[i] ==
"-t")
1193 else if (argv[i] ==
"-binary" || argv[i] ==
"-b")
1197 else if (argv[i] ==
"-hdf5" || argv[i] ==
"-h")
1199 #if defined (HAVE_HDF5)
1205 else if (argv[i] ==
"-ascii" || argv[i] ==
"-a")
1209 else if (argv[i] ==
"-v7.3" || argv[i] ==
"-V7.3" || argv[i] ==
"-7.3")
1211 error (
"load: Matlab file format -v7.3 is not yet implemented");
1213 else if (argv[i] ==
"-v7" || argv[i] ==
"-V7" || argv[i] ==
"-7"
1214 || argv[i] ==
"-mat7-binary")
1218 else if (argv[i] ==
"-mat" || argv[i] ==
"-m"
1219 || argv[i] ==
"-v6" || argv[i] ==
"-V6" || argv[i] ==
"-6"
1220 || argv[i] ==
"-mat-binary")
1224 else if (argv[i] ==
"-v4" || argv[i] ==
"-V4" || argv[i] ==
"-4"
1225 || argv[i] ==
"-mat4-binary")
1229 else if (argv[i] ==
"-force" || argv[i] ==
"-f")
1234 else if (argv[i] ==
"-import" || argv[i] ==
"-i")
1236 warning (
"load: -import ignored");
1238 else if (argv[i] ==
"-list" || argv[i] ==
"-l")
1242 else if (argv[i] ==
"-verbose" || argv[i] ==
"-v")
1250 if (orig_fname ==
"")
1255 orig_fname = argv[i];
1264 if (orig_fname ==
"-")
1268 #if defined (HAVE_HDF5)
1270 error (
"load: cannot read HDF5 format from stdin");
1281 list_only, swap, verbose, argv, i,
1285 error (
"load: must specify file format if reading from stdin");
1293 bool use_zlib =
false;
1298 #if defined (HAVE_HDF5)
1309 list_only, swap, verbose, argv, i,
1324 std::ios::openmode mode = std::ios::in | std::ios::binary;
1326 #if defined (HAVE_ZLIB)
1338 if (file) file.
close ();
1348 if (file) file.
close ();
1354 list_only, swap, verbose, argv, i,
1365 error (
"load: unable to open input file '%s'",
1366 orig_fname.c_str ());
1372 if (file) file.close ();
1382 if (file) file.close ();
1388 list_only, swap, verbose, argv, i,
1395 error (
"load: unable to determine file format of '%s'",
1396 orig_fname.c_str ());
1409 bool save_as_floats =
false;
1410 bool append =
false;
1411 bool use_zlib =
false;
1416 save_as_floats, use_zlib);
1423 int argc = argv.
numel ();
1429 if (save_as_floats &&
format.type () ==
TEXT)
1430 error (
"save: cannot specify both -text and -float-binary");
1438 #if defined (HAVE_HDF5)
1440 error (
"save: cannot write HDF5 format to stdout");
1447 warning (
"save: ignoring -append option for output to stdout");
1451 save_as_floats,
true);
1454 std::ostringstream output_buf;
1456 save_as_floats,
true);
1476 std::ios::openmode mode
1477 = (append ? (std::ios::app | std::ios::ate) : std::ios::out);
1480 mode |= std::ios::binary;
1482 #if defined (HAVE_HDF5)
1487 error (
"save: appending to HDF5 files is not implemented");
1489 # if defined (HAVE_HDF5_UTF8)
1490 bool write_header_info
1491 = ! (append && H5Fis_hdf5 (fname.c_str ()) > 0);
1495 bool write_header_info
1496 = ! (append && H5Fis_hdf5 (ascii_fname.c_str ()) > 0);
1514 #if defined (HAVE_ZLIB)
1522 bool write_header_info = ! file.tellp ();
1537 bool write_header_info = ! file.tellp ();
1660 return load_save_sys.
load (args, nargout);
1815 return load_save_sys.
save (args, nargout);
1915 DEFMETHOD (crash_dumps_octave_core, interp, args, nargout,
1936 DEFMETHOD (save_default_options, interp, args, nargout,
1958 DEFMETHOD (octave_core_file_limit, interp, args, nargout,
1987 DEFMETHOD (octave_core_file_name, interp, args, nargout,
2009 DEFMETHOD (octave_core_file_options, interp, args, nargout,
2032 DEFMETHOD (save_header_format_string, interp, args, nargout,
2071 load_save_sys.dump_octave_core ();
charNDArray min(char d, const charNDArray &m)
bool match(const std::string &str) const
Gzipped file input stream class.
void close()
Close gzipped file.
Gzipped file output stream class.
void close()
Close gzipped file.
OCTINTERP_API void close(void)
octave_value varval(const std::string &name) const
void install_variable(const std::string &name, const octave_value &value, bool global)
tree_evaluator & get_evaluator(void)
bool is_variable(const std::string &name) const
std::string m_save_header_format_string
OCTINTERP_API octave_value save_header_format_string(const octave_value_list &args, int nargout)
double m_octave_core_file_limit
std::string save_header_format_string(void) const
OCTINTERP_API octave_value_list save(const octave_value_list &args=octave_value_list(), int nargout=0)
std::string m_octave_core_file_name
OCTINTERP_API octave_value octave_core_file_name(const octave_value_list &args, int nargout)
OCTINTERP_API 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)
OCTINTERP_API ~load_save_system(void)
interpreter & m_interpreter
OCTINTERP_API octave_value octave_core_file_options(const octave_value_list &args, int nargout)
OCTINTERP_API std::size_t save_fields(std::ostream &os, const octave_scalar_map &m, const std::string &pattern, const load_save_format &fmt, bool save_as_floats)
OCTINTERP_API octave_value save_default_options(const octave_value_list &args, int nargout)
OCTINTERP_API void write_header(std::ostream &os, const load_save_format &fmt)
OCTINTERP_API octave_value crash_dumps_octave_core(const octave_value_list &args, int nargout)
std::string octave_core_file_options(void) const
std::string octave_core_file_name(void) const
OCTINTERP_API 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 m_save_default_options
bool crash_dumps_octave_core(void) const
OCTINTERP_API void install_loaded_variable(const std::string &name, const octave_value &val, bool global, const std::string &)
OCTINTERP_API octave_value_list load(const octave_value_list &args=octave_value_list(), int nargout=0)
static OCTINTERP_API load_save_format get_file_format(const std::string &fname, const std::string &orig_fname, bool &use_zlib, bool quiet=false)
static OCTINTERP_API std::string init_save_header_format(void)
OCTINTERP_API load_save_system(interpreter &interp)
double octave_core_file_limit(void) const
OCTINTERP_API void do_save(std::ostream &os, const octave_value &tc, const std::string &name, const std::string &help, bool global, const load_save_format &fmt, bool save_as_floats)
bool m_crash_dumps_octave_core
std::string m_octave_core_file_options
octave_value octave_core_file_limit(const octave_value_list &args, int nargout)
OCTINTERP_API void dump_octave_core(void)
std::string save_default_options(void) const
static OCTINTERP_API string_vector parse_save_options(const string_vector &argv, load_save_format &fmt, bool &append, bool &save_as_floats, bool &use_zlib)
octave_idx_type nfields(void) const
void assign(const std::string &k, const octave_value &val)
octave_idx_type length(void) const
string_vector make_argv(const std::string &="") const
OCTINTERP_API octave_scalar_map scalar_map_value(void) const
octave_idx_type rows(void) const
octave_idx_type numel(void) const
bool is_defined(void) const
octave_idx_type columns(void) const
bool isstruct(void) const
std::size_t byte_size(void) const
std::string type_name(void) const
OCTINTERP_API bool eof(void) 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(void) const
octave_value value(void) const
bool is_global(void) const
std::string name(void) const
symbol_info_list glob_symbol_info(const std::string &pattern) const
symbol_info_list top_scope_symbol_info(void) const
ColumnVector imag(const ComplexColumnVector &a)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
OCTINTERP_API void print_usage(void)
#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 tilde_expand(const std::string &name)
load_save_system & __get_load_save_system__(void)
std::complex< T > trunc(const std::complex< T > &x)
std::string get_ASCII_filename(const std::string &orig_file_name, const bool allow_locale)
std::ofstream ofstream(const std::string &filename, const std::ios::openmode mode)
std::ifstream ifstream(const std::string &filename, const std::ios::openmode mode)
static bool matches_patterns(const string_vector &patterns, int pat_idx, int num_pat, const std::string &name)
static int read_binary_file_header(std::istream &is, bool &swap, mach_info::float_format &flt_fmt, bool quiet=false)
static OCTAVE_NORETURN void err_file_open(const std::string &fcn, const std::string &file)
static std::string find_file_to_load(const std::string &name, const std::string &orig_name)
static bool glob_pattern_p(const std::string &pattern)
static bool check_gzip_magic(const std::string &fname)
void dump_octave_core(void)
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)
static void read_mat_binary_data(std::istream &is, double *data, int precision, int len, bool swap, octave::mach_info::float_format flt_fmt)
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)
bool words_big_endian(void)
float_format native_float_format(void)
OCTAVE_API 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()) ? '\'' :'"'))
static std::string dir_sep_chars
std::string find_data_file_in_load_path(const std::string &fcn, const std::string &file, bool require_regular_file)
std::size_t format(std::ostream &os, const char *fmt,...)
octave_value set_internal_variable(bool &var, const octave_value_list &args, int nargout, const char *nm)