GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
load-save.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1994-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_load_save_h)
27 #define octave_load_save_h 1
28 
29 #include "octave-config.h"
30 
31 #include <iosfwd>
32 #include <string>
33 
34 #include "mach-info.h"
35 
36 #include "ovl.h"
37 
39 
40 class interpreter;
41 class load_save_format;
42 class symbol_info;
43 
45 {
46 public:
47 
48  // FIXME: maybe MAT5 and MAT7 should be options to MAT_BINARY.
49  // Similarly, save_as_floats may be an option for LS_BINARY,
50  // LS_HDF5 etc.
51 
53  {
61  UNKNOWN
62  };
63 
65  {
66  // MAT_ASCII options (not exclusive)
69  // MAT_BINARY options
72  // zero means no option.
73  NO_OPTION = 0
74  };
75 
76  OCTINTERP_API load_save_system (interpreter& interp);
77 
78  OCTINTERP_API ~load_save_system ();
79 
80  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (load_save_system)
81 
82  OCTINTERP_API octave_value
83  crash_dumps_octave_core (const octave_value_list& args, int nargout);
84 
86  {
87  return m_crash_dumps_octave_core;
88  }
89 
90  bool crash_dumps_octave_core (bool flag)
91  {
92  return set (m_crash_dumps_octave_core, flag);
93  }
94 
96  int nargout);
97 
98  double octave_core_file_limit () const
99  {
100  return m_octave_core_file_limit;
101  }
102 
103  double octave_core_file_limit (double limit)
104  {
105  return set (m_octave_core_file_limit, limit);
106  }
107 
108  OCTINTERP_API octave_value
109  octave_core_file_name (const octave_value_list& args, int nargout);
110 
111  std::string octave_core_file_name () const
112  {
113  return m_octave_core_file_name;
114  }
115 
116  std::string octave_core_file_name (const std::string& file)
117  {
118  return set (m_octave_core_file_name, file);
119  }
120 
121  OCTINTERP_API octave_value
122  save_default_options (const octave_value_list& args, int nargout);
123 
124  std::string save_default_options () const
125  {
126  return m_save_default_options;
127  }
128 
129  std::string save_default_options (const std::string& options)
130  {
131  return set (m_save_default_options, options);
132  }
133 
134  OCTINTERP_API octave_value
135  octave_core_file_options (const octave_value_list& args, int nargout);
136 
137  std::string octave_core_file_options () const
138  {
139  return m_octave_core_file_options;
140  }
141 
142  std::string octave_core_file_options (const std::string& options)
143  {
144  return set (m_octave_core_file_options, options);
145  }
146 
147  OCTINTERP_API octave_value
148  save_header_format_string (const octave_value_list& args, int nargout);
149 
150  std::string save_header_format_string () const
151  {
152  return m_save_header_format_string;
153  }
154 
155  std::string save_header_format_string (const std::string& format)
156  {
157  return set (m_save_header_format_string, format);
158  }
159 
160  static OCTINTERP_API load_save_format
161  get_file_format (const std::string& fname, const std::string& orig_fname,
162  bool& use_zlib, bool quiet = false);
163 
164  // FIXME: this is probably not the best public interface for
165  // loading and saving variables, but it is what is currently
166  // needed for the Fload and Fsave functions.
167 
168  OCTINTERP_API octave_value
169  load_vars (std::istream& stream, const std::string& orig_fname,
170  const load_save_format& fmt, mach_info::float_format flt_fmt,
171  bool list_only, bool swap, bool verbose,
172  const string_vector& argv, int argv_idx, int argc, int nargout);
173 
174  static OCTINTERP_API string_vector
176  bool& append, bool& save_as_floats, bool& use_zlib);
177 
178  static OCTINTERP_API string_vector
179  parse_save_options (const std::string& arg, load_save_format& fmt,
180  bool& append, bool& save_as_floats, bool& use_zlib);
181 
182  OCTINTERP_API void
183  save_vars (const string_vector& argv, int argv_idx, int argc,
184  std::ostream& os, const load_save_format& fmt,
185  bool save_as_floats, bool write_header_info);
186 
187  OCTINTERP_API void dump_octave_core ();
188 
189  OCTINTERP_API octave_value_list
190  load (const octave_value_list& args = octave_value_list (),
191  int nargout = 0);
192 
193  OCTINTERP_API octave_value_list
194  save (const octave_value_list& args = octave_value_list (),
195  int nargout = 0);
196 
197 private:
198 
199  interpreter& m_interpreter;
200 
201  // Write octave-workspace file if Octave crashes or is killed by a
202  // signal.
203  bool m_crash_dumps_octave_core;
204 
205  // The maximum amount of memory (in kilobytes) that we will
206  // attempt to write to the Octave core file.
207  double m_octave_core_file_limit;
208 
209  // The name of the Octave core file.
210  std::string m_octave_core_file_name;
211 
212  // The default output format. May be one of "binary", "text",
213  // "mat-binary", or "hdf5".
214  std::string m_save_default_options;
215 
216  // The output format for Octave core files.
217  std::string m_octave_core_file_options;
218 
219  // The format string for the comment line at the top of
220  // text-format save files. Passed to strftime. Should begin with
221  // '#' and contain no newline characters.
222  std::string m_save_header_format_string;
223 
224  OCTINTERP_API void
225  write_header (std::ostream& os, const load_save_format& fmt);
226 
227  OCTINTERP_API std::size_t
228  save_vars (std::ostream& os, const std::string& pattern,
229  const load_save_format& fmt, bool save_as_floats);
230 
231  OCTINTERP_API void
232  do_save (std::ostream& os, const octave_value& tc, const std::string& name,
233  const std::string& help, bool global, const load_save_format& fmt,
234  bool save_as_floats);
235 
236  OCTINTERP_API void
237  do_save (std::ostream& os, const symbol_info& syminfo,
238  const load_save_format& fmt, bool save_as_floats);
239 
240  OCTINTERP_API std::size_t
241  save_fields (std::ostream& os, const octave_scalar_map& m,
242  const std::string& pattern, const load_save_format& fmt,
243  bool save_as_floats);
244 
245  OCTINTERP_API void
246  dump_octave_core (std::ostream& os, const char *fname,
247  const load_save_format& fmt, bool save_as_floats);
248 
249  OCTINTERP_API void
250  install_loaded_variable (const std::string& name, const octave_value& val,
251  bool global, const std::string& /*doc*/);
252 
253  static OCTINTERP_API std::string init_save_header_format ();
254 
255  static OCTINTERP_API load_save_format
256  get_file_format (std::istream& file, const std::string& filename);
257 
258  template <typename T>
259  T set (T& var, const T& new_val)
260  {
261  T old_val = var;
262  var = new_val;
263  return old_val;
264  }
265 };
266 
268 {
269 public:
270 
273  : m_type (type), m_options (options)
274  { }
275 
277 
278  load_save_system::format_type type () const { return m_type; }
279 
281  {
282  m_options |= option;
283  }
284 
285  int options () const { return m_options; }
286 
287 private:
288 
290  int m_options;
291 };
292 
293 OCTAVE_END_NAMESPACE(octave)
294 
295 #endif
int options() const
Definition: load-save.h:285
void set_option(load_save_system::format_options option)
Definition: load-save.h:280
load_save_format(load_save_system::format_type type, load_save_system::format_options options=load_save_system::NO_OPTION)
Definition: load-save.h:271
void set_type(load_save_system::format_type type)
Definition: load-save.h:276
load_save_system::format_type type() const
Definition: load-save.h:278
octave_value_list save(const octave_value_list &args=octave_value_list(), int nargout=0)
Definition: load-save.cc:1411
std::string save_header_format_string() const
Definition: load-save.h:150
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)
Definition: load-save.cc:674
bool crash_dumps_octave_core() const
Definition: load-save.h:85
void dump_octave_core()
Definition: load-save.cc:734
std::string octave_core_file_name(const std::string &file)
Definition: load-save.h:116
std::string octave_core_file_name() const
Definition: load-save.h:111
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)
Definition: load-save.cc:378
std::string octave_core_file_options() const
Definition: load-save.h:137
double octave_core_file_limit() const
Definition: load-save.h:98
bool crash_dumps_octave_core(bool flag)
Definition: load-save.h:90
octave_value_list load(const octave_value_list &args=octave_value_list(), int nargout=0)
Definition: load-save.cc:1167
static load_save_format get_file_format(const std::string &fname, const std::string &orig_fname, bool &use_zlib, bool quiet=false)
Definition: load-save.cc:324
double octave_core_file_limit(double limit)
Definition: load-save.h:103
std::string octave_core_file_options(const std::string &options)
Definition: load-save.h:142
load_save_system(interpreter &interp)
Definition: load-save.cc:254
std::string save_default_options() const
Definition: load-save.h:124
std::string save_header_format_string(const std::string &format)
Definition: load-save.h:155
std::string save_default_options(const std::string &options)
Definition: load-save.h:129
static string_vector parse_save_options(const string_vector &argv, load_save_format &fmt, bool &append, bool &save_as_floats, bool &use_zlib)
Definition: load-save.cc:529
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
float_format
Definition: mach-info.h:38
T octave_idx_type m
Definition: mx-inlines.cc:781
std::size_t format(std::ostream &os, const char *fmt,...)