GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
load-save.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1994-2026 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#include <unordered_map>
34
35#include "mach-info.h"
36
37#include "ovl.h"
38
40
41class interpreter;
43class symbol_info;
45
47{
48public:
49
50 // FIXME: maybe MAT5 and MAT7 should be options to MAT_BINARY.
51 // Similarly, save_as_floats may be an option for LS_BINARY,
52 // LS_HDF5 etc.
53
65
67 {
68 // MAT_ASCII options (not exclusive)
71 // MAT_BINARY options
74 // zero means no option.
75 NO_OPTION = 0
76 };
77
78 OCTINTERP_API load_save_system (interpreter& interp);
79
80 OCTINTERP_API ~load_save_system ();
81
82 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (load_save_system)
83
84 OCTINTERP_API octave_value
85 crash_dumps_octave_core (const octave_value_list& args, int nargout);
86
88 {
89 return m_crash_dumps_octave_core;
90 }
91
92 bool crash_dumps_octave_core (bool flag)
93 {
94 return set (m_crash_dumps_octave_core, flag);
95 }
96
98 int nargout);
99
101 {
102 return m_octave_core_file_limit;
103 }
104
105 double octave_core_file_limit (double limit)
106 {
107 return set (m_octave_core_file_limit, limit);
108 }
109
110 OCTINTERP_API octave_value
111 octave_core_file_name (const octave_value_list& args, int nargout);
112
113 std::string octave_core_file_name () const
114 {
115 return m_octave_core_file_name;
116 }
117
118 std::string octave_core_file_name (const std::string& file)
119 {
120 return set (m_octave_core_file_name, file);
121 }
122
123 OCTINTERP_API octave_value
124 save_default_options (const octave_value_list& args, int nargout);
125
126 std::string save_default_options () const
127 {
128 return m_save_default_options;
129 }
130
131 std::string save_default_options (const std::string& options)
132 {
133 return set (m_save_default_options, options);
134 }
135
136 OCTINTERP_API octave_value
137 octave_core_file_options (const octave_value_list& args, int nargout);
138
139 std::string octave_core_file_options () const
140 {
141 return m_octave_core_file_options;
142 }
143
144 std::string octave_core_file_options (const std::string& options)
145 {
146 return set (m_octave_core_file_options, options);
147 }
148
149 OCTINTERP_API octave_value
150 save_header_format_string (const octave_value_list& args, int nargout);
151
152 std::string save_header_format_string () const
153 {
154 return m_save_header_format_string;
155 }
156
157 std::string save_header_format_string (const std::string& format)
158 {
159 return set (m_save_header_format_string, format);
160 }
161
162 static OCTINTERP_API load_save_format
163 get_file_format (const std::string& fname, const std::string& orig_fname,
164 bool& use_zlib, bool quiet = false);
165
166 // FIXME: this is probably not the best public interface for
167 // loading and saving variables, but it is what is currently
168 // needed for the Fload and Fsave functions.
169
170 OCTINTERP_API octave_value
171 load_vars (std::istream& stream, const std::string& orig_fname,
172 const load_save_format& fmt, mach_info::float_format flt_fmt,
173 bool list_only, bool swap, bool verbose,
174 const string_vector& argv, int argv_idx, int argc, int nargout);
175
176 static OCTINTERP_API string_vector
178 bool& append, bool& save_as_floats, bool& use_zlib);
179
180 static OCTINTERP_API string_vector
181 parse_save_options (const std::string& arg, load_save_format& fmt,
182 bool& append, bool& save_as_floats, bool& use_zlib);
183
184 OCTINTERP_API void
185 save_vars (const string_vector& argv, int argv_idx, int argc,
186 std::ostream& os, const load_save_format& fmt,
187 bool save_as_floats, bool write_header_info);
188
189 OCTINTERP_API void dump_octave_core ();
190
191 OCTINTERP_API octave_value_list
192 load (const octave_value_list& args = octave_value_list (),
193 int nargout = 0);
194
195 OCTINTERP_API octave_value_list
196 save (const octave_value_list& args = octave_value_list (),
197 int nargout = 0);
198
199 // MCOS object cache management for loading classdef objects
200 OCTINTERP_API void clear_mcos_object_cache ();
201
202 OCTINTERP_API void
203 set_mcos_object_cache_entry (uint32_t object_id, octave_value& obj);
204
205 OCTINTERP_API octave_value& get_mcos_object_cache_entry (uint32_t object_id);
206
207 OCTINTERP_API bool is_mcos_object_cache_entry (uint32_t object_id);
208
209 OCTINTERP_API uint32_t
210 get_mcos_object_cache_id (const void *obj, bool& new_entry);
211
212 OCTINTERP_API void create_subsystem_handler ();
213
214 OCTINTERP_API subsystem_handler * get_subsystem_handler ();
215
216 OCTINTERP_API void clear_subsystem_handler ();
217
218private:
219
220 // Subsystem handler is used to read and write MCOS objects in MAT-files
221 std::unique_ptr<subsystem_handler> m_subsystem_handler;
222
223 interpreter& m_interpreter;
224
225 // Write octave-workspace file if Octave crashes or is killed by a
226 // signal.
227 bool m_crash_dumps_octave_core;
228
229 // The maximum amount of memory (in kilobytes) that we will
230 // attempt to write to the Octave core file.
231 double m_octave_core_file_limit;
232
233 // The name of the Octave core file.
234 std::string m_octave_core_file_name;
235
236 // The default output format. May be one of "binary", "text",
237 // "mat-binary", or "hdf5".
238 std::string m_save_default_options;
239
240 // The output format for Octave core files.
241 std::string m_octave_core_file_options;
242
243 // The format string for the comment line at the top of
244 // text-format save files. Passed to strftime. Should begin with
245 // '#' and contain no newline characters.
246 std::string m_save_header_format_string;
247
248 // Cache for constructed MCOS objects and their object IDs during loading
249 std::unordered_map<uint32_t, octave_value> m_mcos_object_load_cache;
250
251 // Cache for constructed MCOS objects and their object IDs during saving
252 std::unordered_map<const void *, uint32_t> m_mcos_object_save_cache;
253
254 OCTINTERP_API void
255 write_header (std::ostream& os, const load_save_format& fmt);
256
257 OCTINTERP_API std::size_t
258 save_vars (std::ostream& os, const std::string& pattern,
259 const load_save_format& fmt, bool save_as_floats);
260
261 OCTINTERP_API void
262 do_save (std::ostream& os, const octave_value& tc, const std::string& name,
263 const std::string& help, bool global, const load_save_format& fmt,
264 bool save_as_floats);
265
266 OCTINTERP_API void
267 do_save (std::ostream& os, const symbol_info& syminfo,
268 const load_save_format& fmt, bool save_as_floats);
269
270 OCTINTERP_API std::size_t
271 save_fields (std::ostream& os, const octave_scalar_map& m,
272 const std::string& pattern, const load_save_format& fmt,
273 bool save_as_floats);
274
275 OCTINTERP_API void
276 dump_octave_core (std::ostream& os, const char *fname,
277 const load_save_format& fmt, bool save_as_floats);
278
279 OCTINTERP_API void
280 install_loaded_variable (const std::string& name, const octave_value& val,
281 bool global, const std::string& /*doc*/);
282
283 static OCTINTERP_API std::string init_save_header_format ();
284
285 static OCTINTERP_API load_save_format
286 get_file_format (std::istream& file, const std::string& filename);
287
288 template <typename T>
289 T set (T& var, const T& new_val)
290 {
291 T old_val = var;
292 var = new_val;
293 return old_val;
294 }
295};
296
298{
299public:
300
305
307
308 load_save_system::format_type type () const { return m_type; }
309
311 {
312 m_options |= option;
313 }
314
315 int options () const { return m_options; }
316
317private:
318
320 int m_options;
321};
322
323OCTAVE_END_NAMESPACE(octave)
324
325#endif
int options() const
Definition load-save.h:315
void set_option(load_save_system::format_options option)
Definition load-save.h:310
load_save_format(load_save_system::format_type type, load_save_system::format_options options=load_save_system::NO_OPTION)
Definition load-save.h:301
void set_type(load_save_system::format_type type)
Definition load-save.h:306
load_save_system::format_type type() const
Definition load-save.h:308
octave_value_list save(const octave_value_list &args=octave_value_list(), int nargout=0)
std::string save_header_format_string() const
Definition load-save.h:152
subsystem_handler * get_subsystem_handler()
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:675
octave_value & get_mcos_object_cache_entry(uint32_t object_id)
void create_subsystem_handler()
bool crash_dumps_octave_core() const
Definition load-save.h:87
void clear_mcos_object_cache()
void dump_octave_core()
Definition load-save.cc:744
std::string octave_core_file_name(const std::string &file)
Definition load-save.h:118
std::string octave_core_file_name() const
Definition load-save.h:113
bool is_mcos_object_cache_entry(uint32_t object_id)
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:379
std::string octave_core_file_options() const
Definition load-save.h:139
double octave_core_file_limit() const
Definition load-save.h:100
bool crash_dumps_octave_core(bool flag)
Definition load-save.h:92
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)
Definition load-save.cc:325
double octave_core_file_limit(double limit)
Definition load-save.h:105
void clear_subsystem_handler()
std::string octave_core_file_options(const std::string &options)
Definition load-save.h:144
std::string save_default_options() const
Definition load-save.h:126
std::string save_header_format_string(const std::string &format)
Definition load-save.h:157
void set_mcos_object_cache_entry(uint32_t object_id, octave_value &obj)
std::string save_default_options(const std::string &options)
Definition load-save.h:131
uint32_t get_mcos_object_cache_id(const void *obj, bool &new_entry)
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:530
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
std::size_t format(std::ostream &os, const char *fmt,...)
Definition utils.cc:1514