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++)
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)
171 std::ios::in | std::ios::binary);
173 unsigned char magic[2];
174 if (file.read (
reinterpret_cast<char *
> (&magic[0]), 2)
175 && magic[0] == 0x1f && magic[1] == 0x8b)
189 size_t dot_pos = fname.rfind (
'.');
192 if (dot_pos == std::string::npos
193 || (sep_pos != std::string::npos && dot_pos < sep_pos))
211 error (
"load: unable to find file %s", orig_name.c_str ());
225 int len = pattern.length ();
227 for (
int i = 0; i <
len; i++)
260 : m_interpreter (interp),
261 m_crash_dumps_octave_core (true),
262 m_octave_core_file_limit (-1.0),
263 m_octave_core_file_name (
"octave-workspace"),
264 m_save_default_options (
"-text"),
265 m_octave_core_file_options (
"-binary"),
266 m_save_header_format_string (init_save_header_format ())
268 #if defined (HAVE_HDF5)
275 #if defined (HAVE_HDF5)
285 "crash_dumps_octave_core");
293 "octave_core_file_limit");
301 "octave_core_file_name",
false);
309 "save_default_options",
false);
317 "octave_core_file_options",
false);
325 "save_header_format_string");
330 const std::string& orig_fname,
331 bool& use_zlib,
bool quiet)
337 #if defined (HAVE_HDF5)
339 if (H5Fis_hdf5 (ascii_fname.c_str ()) > 0)
343 #if defined (HAVE_ZLIB)
352 std::ios::in | std::ios::binary);
361 #if defined (HAVE_ZLIB)
364 gzifstream gzfile (fname.c_str (), std::ios::in | std::ios::binary);
380 const std::string& orig_fname,
383 bool list_only,
bool swap,
bool verbose,
385 int argc,
int nargout)
391 std::ostringstream output_buf;
392 std::list<std::string> symbol_names;
423 #if defined (HAVE_HDF5)
426 argv, argv_idx, argc);
446 error (
"load: unable to load variable '%s'",
name.c_str ());
449 warning (
"load: loaded ASCII file '%s' -- ignoring extra args",
450 orig_fname.c_str ());
463 <<
"type rows cols name\n"
464 <<
"==== ==== ==== ====\n";
468 << std::setw (16) << tc.
type_name ().c_str ()
469 << std::setiosflags (std::ios::right)
470 << std::setw (7) << tc.
rows ()
471 << std::setw (7) << tc.
columns ()
472 <<
" " <<
name <<
"\n";
475 symbol_names.push_back (
name);
498 if (list_only && count)
502 std::string msg = output_buf.str ();
523 else if (retstruct.
nfields () != 0)
532 bool& save_as_floats,
bool& use_zlib)
534 #if ! defined (HAVE_ZLIB)
535 octave_unused_parameter (use_zlib);
539 int argc = argv.
numel ();
541 bool do_double =
false;
542 bool do_tabs =
false;
544 for (
int i = 0; i < argc; i++)
546 if (argv[i] ==
"-append")
550 else if (argv[i] ==
"-ascii" || argv[i] ==
"-a")
554 else if (argv[i] ==
"-double")
558 else if (argv[i] ==
"-tabs")
562 else if (argv[i] ==
"-text" || argv[i] ==
"-t")
566 else if (argv[i] ==
"-binary" || argv[i] ==
"-b")
570 else if (argv[i] ==
"-hdf5" || argv[i] ==
"-h")
572 #if defined (HAVE_HDF5)
578 else if (argv[i] ==
"-v7.3" || argv[i] ==
"-V7.3" || argv[i] ==
"-7.3")
580 error (
"save: Matlab file format -v7.3 is not yet implemented");
582 #if defined (HAVE_ZLIB)
583 else if (argv[i] ==
"-v7" || argv[i] ==
"-V7" || argv[i] ==
"-7"
584 || argv[i] ==
"-mat7-binary")
589 else if (argv[i] ==
"-mat" || argv[i] ==
"-m"
590 || argv[i] ==
"-v6" || argv[i] ==
"-V6" || argv[i] ==
"-6"
591 || argv[i] ==
"-mat-binary")
595 else if (argv[i] ==
"-v4" || argv[i] ==
"-V4" || argv[i] ==
"-4"
596 || argv[i] ==
"-mat4-binary")
600 else if (argv[i] ==
"-float-binary" || argv[i] ==
"-f")
603 save_as_floats =
true;
605 else if (argv[i] ==
"-float-hdf5")
607 #if defined (HAVE_HDF5)
609 save_as_floats =
true;
614 #if defined (HAVE_ZLIB)
615 else if (argv[i] ==
"-zip" || argv[i] ==
"-z")
620 else if (argv[i] ==
"-struct")
624 else if (argv[i][0] ==
'-' && argv[i] !=
"-")
626 error (
"save: Unrecognized option '%s'", argv[i].c_str ());
637 warning (R
"(save: "-double" option only has an effect with "-ascii")");
645 warning (R
"(save: "-tabs" option only has an effect with "-ascii")");
654 bool& append,
bool& save_as_floats,
657 std::istringstream is (arg);
671 int argc, std::ostream& os,
674 bool write_header_info)
676 if (write_header_info)
679 if (argv_idx == argc)
681 save_vars (os,
"*", fmt, save_as_floats);
683 else if (argv[argv_idx] ==
"-struct")
685 if (++argv_idx >= argc)
686 error (
"save: missing struct name");
688 std::string struct_name = argv[argv_idx];
691 error (
"save: no such variable: '%s'", struct_name.c_str ());
696 error (
"save: '%s' is not a scalar structure", struct_name.c_str ());
704 for (
int i = argv_idx; i < argc; i++)
706 if (!
save_fields (os, struct_var_map, argv[i], fmt,
709 warning (
"save: no such field '%s.%s'",
710 struct_name.c_str (), argv[i].c_str ());
715 save_fields (os, struct_var_map,
"*", fmt, save_as_floats);
719 for (
int i = argv_idx; i < argc; i++)
723 if (!
save_vars (os, argv[i], fmt, save_as_floats))
724 warning (
"save: no such variable '%s'", argv[i].c_str ());
737 message (
nullptr,
"attempting to save variables to '%s'...", fname);
741 bool save_as_floats =
false;
745 bool use_zlib =
false;
748 fmt, append, save_as_floats,
751 std::ios::openmode mode = std::ios::out;
758 #
if defined (HAVE_HDF5)
764 mode |= std::ios::binary;
768 #if defined (HAVE_HDF5)
780 warning (
"dump_octave_core: unable to open '%s' for writing...",
788 #if defined (HAVE_ZLIB)
800 warning (
"dump_octave_core: unable to open '%s' for writing...",
815 warning (
"dump_octave_core: unable to open '%s' for writing...",
830 ?
"Octave-1-B" :
"Octave-1-L");
843 char const *versionmagic;
844 char headertext[128];
848 const char *matlab_format =
"MATLAB 5.0 MAT-file, written by Octave "
850 std::string comment_string = now.
strftime (matlab_format);
852 size_t len =
std::min (comment_string.length (),
static_cast<size_t> (124));
853 memset (headertext,
' ', 124);
854 memcpy (headertext, comment_string.data (),
len);
864 versionmagic =
"\x01\x00\x4d\x49";
866 versionmagic =
"\x00\x01\x49\x4d";
868 memcpy (headertext+124, versionmagic, 4);
869 os.write (headertext, 128);
874 #if defined (HAVE_HDF5)
883 if (! comment_string.empty ())
885 #if defined (HAVE_HDF5)
889 H5Gset_comment (hs.
file_id,
"/", comment_string.c_str ());
893 os << comment_string <<
"\n";
907 const std::string& pattern,
917 for (
const auto& syminfo : syminfo_list)
919 do_save (os, syminfo, fmt, save_as_floats);
928 const std::string&
name,
929 const std::string& help,
947 warning (
"save: unable to save %s in ASCII format",
name.c_str ());
954 #if defined (HAVE_HDF5)
989 do_save (os, val,
name, help, global, fmt, save_as_floats);
998 const std::string& pattern,
1000 bool save_as_floats)
1006 for (
auto it =
m.begin (); it !=
m.end (); it++)
1008 std::string empty_str;
1010 if (pat.
match (
m.key (it)))
1012 do_save (os,
m.contents (it),
m.key (it), empty_str,
1013 0, fmt, save_as_floats);
1025 bool save_as_floats)
1033 double save_mem_size = 0;
1035 for (
const auto& syminfo : syminfo_list)
1039 std::string
name = syminfo.name ();
1041 bool global = syminfo.is_global ();
1043 double val_size = val.
byte_size () / 1024;
1050 save_mem_size += val_size;
1052 do_save (os, val,
name, help, global, fmt, save_as_floats);
1056 message (
nullptr,
"save to '%s' complete", fname);
1065 const std::string& )
1074 ", %a %b %d %H:%M:%S %Y %Z <")
1083 const std::string& filename)
1097 file.seekg (0, std::ios::beg);
1099 int32_t mopt, nr, nc,
imag,
len;
1109 file.seekg (0, std::ios::beg);
1116 file.seekg (0, std::ios::beg);
1122 file.seekg (0, std::ios::beg);
1127 if (name_val.empty () !=
true && type_val.empty () !=
true)
1132 file.seekg (0, std::ios::beg);
1157 int argc = args.
length () + 1;
1162 std::string orig_fname =
"";
1165 if (argc > 1 && ! argv[1].empty () && argv[1].at (0) !=
'-')
1167 orig_fname = argv[1];
1177 bool list_only =
false;
1178 bool verbose =
false;
1180 for (; i < argc; i++)
1182 if (argv[i] ==
"-text" || argv[i] ==
"-t")
1186 else if (argv[i] ==
"-binary" || argv[i] ==
"-b")
1190 else if (argv[i] ==
"-hdf5" || argv[i] ==
"-h")
1192 #if defined (HAVE_HDF5)
1198 else if (argv[i] ==
"-ascii" || argv[i] ==
"-a")
1202 else if (argv[i] ==
"-v7.3" || argv[i] ==
"-V7.3" || argv[i] ==
"-7.3")
1204 error (
"load: Matlab file format -v7.3 is not yet implemented");
1206 else if (argv[i] ==
"-v7" || argv[i] ==
"-V7" || argv[i] ==
"-7"
1207 || argv[i] ==
"-mat7-binary")
1211 else if (argv[i] ==
"-mat" || argv[i] ==
"-m"
1212 || argv[i] ==
"-v6" || argv[i] ==
"-V6" || argv[i] ==
"-6"
1213 || argv[i] ==
"-mat-binary")
1217 else if (argv[i] ==
"-v4" || argv[i] ==
"-V4" || argv[i] ==
"-4"
1218 || argv[i] ==
"-mat4-binary")
1222 else if (argv[i] ==
"-force" || argv[i] ==
"-f")
1227 else if (argv[i] ==
"-import" || argv[i] ==
"-i")
1229 warning (
"load: -import ignored");
1231 else if (argv[i] ==
"-list" || argv[i] ==
"-l")
1235 else if (argv[i] ==
"-verbose" || argv[i] ==
"-v")
1243 if (orig_fname ==
"")
1248 orig_fname = argv[i];
1257 if (orig_fname ==
"-")
1261 #if defined (HAVE_HDF5)
1263 error (
"load: cannot read HDF5 format from stdin");
1274 list_only, swap, verbose, argv, i,
1278 error (
"load: must specify file format if reading from stdin");
1286 bool use_zlib =
false;
1291 #if defined (HAVE_HDF5)
1302 list_only, swap, verbose, argv, i,
1317 std::ios::openmode mode = std::ios::in | std::ios::binary;
1319 #if defined (HAVE_ZLIB)
1331 if (file) file.
close ();
1341 if (file) file.
close ();
1347 list_only, swap, verbose, argv, i,
1358 error (
"load: unable to open input file '%s'",
1359 orig_fname.c_str ());
1365 if (file) file.close ();
1375 if (file) file.close ();
1381 list_only, swap, verbose, argv, i,
1388 error (
"load: unable to determine file format of '%s'",
1389 orig_fname.c_str ());
1402 bool save_as_floats =
false;
1403 bool append =
false;
1404 bool use_zlib =
false;
1409 save_as_floats, use_zlib);
1416 int argc = argv.
numel ();
1422 if (save_as_floats &&
format.type () ==
TEXT)
1423 error (
"save: cannot specify both -text and -float-binary");
1431 #if defined (HAVE_HDF5)
1433 error (
"save: cannot write HDF5 format to stdout");
1440 warning (
"save: ignoring -append option for output to stdout");
1444 save_as_floats,
true);
1447 std::ostringstream output_buf;
1449 save_as_floats,
true);
1469 std::ios::openmode mode
1470 = (append ? (std::ios::app | std::ios::ate) : std::ios::out);
1473 mode |= std::ios::binary;
1475 #if defined (HAVE_HDF5)
1480 error (
"save: appending to HDF5 files is not implemented");
1484 bool write_header_info
1485 = ! (append && H5Fis_hdf5 (ascii_fname.c_str ()) > 0);
1502 #if defined (HAVE_ZLIB)
1510 bool write_header_info = ! file.tellp ();
1525 bool write_header_info = ! file.tellp ();
1658 return load_save_sys.
load (args, nargout);
1812 return load_save_sys.
save (args, nargout);
1815 DEFMETHOD (crash_dumps_octave_core, interp, args, nargout,
1835 DEFMETHOD (save_default_options, interp, args, nargout,
1857 DEFMETHOD (octave_core_file_limit, interp, args, nargout,
1885 DEFMETHOD (octave_core_file_name, interp, args, nargout,
1906 DEFMETHOD (octave_core_file_options, interp, args, nargout,
1929 DEFMETHOD (save_header_format_string, interp, args, nargout,
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.
bool is_variable(const std::string &name) const
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)
octave_value crash_dumps_octave_core(const octave_value_list &args, int nargout)
void write_header(std::ostream &os, const load_save_format &fmt)
bool m_crash_dumps_octave_core
std::string m_octave_core_file_name
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)
octave_value octave_core_file_limit(const octave_value_list &args, int nargout)
octave_value octave_core_file_name(const octave_value_list &args, int nargout)
octave_value save_default_options(const octave_value_list &args, int nargout)
std::string save_default_options(void) const
std::string octave_core_file_options(void) const
std::string save_header_format_string(void) const
void dump_octave_core(void)
std::string octave_core_file_name(void) const
octave_value octave_core_file_options(const octave_value_list &args, int nargout)
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)
void install_loaded_variable(const std::string &name, const octave_value &val, bool global, const std::string &)
static string_vector parse_save_options(const string_vector &argv, load_save_format &fmt, bool &append, bool &save_as_floats, bool &use_zlib)
std::string m_save_header_format_string
octave_value save_header_format_string(const octave_value_list &args, int nargout)
double octave_core_file_limit(void) 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)
static std::string init_save_header_format(void)
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)
interpreter & m_interpreter
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)
bool crash_dumps_octave_core(void) const
double m_octave_core_file_limit
std::string m_save_default_options
octave_value_list save(const octave_value_list &args=octave_value_list(), int nargout=0)
load_save_system(interpreter &interp)
std::string m_octave_core_file_options
octave_value value(void) const
bool is_global(void) const
std::string name(void) const
std::string strftime(const std::string &fmt) const
static std::string get_host_name(void)
static std::string get_user_name(void)
symbol_info_list glob_symbol_info(const std::string &pattern) const
symbol_info_list top_scope_symbol_info(void) const
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
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
octave_scalar_map scalar_map_value(void) const
size_t byte_size(void) const
std::string type_name(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
ColumnVector imag(const ComplexColumnVector &a)
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)
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)
float_format native_float_format(void)
bool words_big_endian(void)
std::complex< T > trunc(const std::complex< T > &x)
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.
std::string tilde_expand(const std::string &name)
std::string dir_sep_chars(void)
std::string get_ASCII_filename(const std::string &orig_file_name)
std::ifstream ifstream(const std::string &filename, const std::ios::openmode mode)
std::ofstream ofstream(const std::string &filename, const std::ios::openmode mode)
static OCTAVE_NORETURN void err_file_open(const std::string &fcn, const std::string &file)
static bool check_gzip_magic(const std::string &fname)
static bool glob_pattern_p(const std::string &pattern)
load_save_system & __get_load_save_system__(const std::string &who)
static int read_binary_file_header(std::istream &is, bool &swap, mach_info::float_format &flt_fmt, bool quiet=false)
static bool matches_patterns(const string_vector &patterns, int pat_idx, int num_pat, const std::string &name)
std::string find_data_file_in_load_path(const std::string &fcn, const std::string &file, bool require_regular_file)
size_t format(std::ostream &os, const char *fmt,...)
static std::string find_file_to_load(const std::string &name, const std::string &orig_name)
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
octave_value::octave_value(const Array< char > &chm, char type) return retval
octave_value set_internal_variable(bool &var, const octave_value_list &args, int nargout, const char *nm)