Mercurial > octave
annotate libinterp/corefcn/ls-oct-binary.cc @ 27919:1891570abac8
update Octave Project Developers copyright for the new year
In files that have the "Octave Project Developers" copyright notice,
update for 2020.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 06 Jan 2020 22:29:51 -0500 |
parents | b442ec6dda5c |
children | bd51beb6205e |
rev | line source |
---|---|
4634 | 1 /* |
2 | |
27919
1891570abac8
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27918
diff
changeset
|
3 Copyright (C) 1996-2020 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
4 |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
5 See the file COPYRIGHT.md in the top-level directory of this distribution |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
6 or <https://octave.org/COPYRIGHT.html/>. |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
7 |
4634 | 8 |
9 This file is part of Octave. | |
10 | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23812
diff
changeset
|
11 Octave is free software: you can redistribute it and/or modify it |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
12 under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23812
diff
changeset
|
13 the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
14 (at your option) any later version. |
4634 | 15 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
16 Octave is distributed in the hope that it will be useful, but |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
17 WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
19 GNU General Public License for more details. |
4634 | 20 |
21 You should have received a copy of the GNU General Public License | |
7016 | 22 along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23812
diff
changeset
|
23 <https://www.gnu.org/licenses/>. |
4634 | 24 |
25 */ | |
26 | |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
27 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21200
diff
changeset
|
28 # include "config.h" |
4634 | 29 #endif |
30 | |
25438
cb1606f78f6b
prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible
John W. Eaton <jwe@octave.org>
parents:
25392
diff
changeset
|
31 #include <istream> |
cb1606f78f6b
prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible
John W. Eaton <jwe@octave.org>
parents:
25392
diff
changeset
|
32 #include <ostream> |
4634 | 33 #include <string> |
34 | |
35 #include "byte-swap.h" | |
36 #include "data-conv.h" | |
37 #include "file-ops.h" | |
38 #include "glob-match.h" | |
39 #include "lo-mappers.h" | |
40 #include "mach-info.h" | |
41 #include "oct-env.h" | |
23455
73ff72d3d603
maint: Eliminate <cfloat.h> header from libinterp files
Rik <rik@octave.org>
parents:
23450
diff
changeset
|
42 #include "oct-locbuf.h" |
4634 | 43 #include "oct-time.h" |
44 | |
45 #include "defun.h" | |
46 #include "error.h" | |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20955
diff
changeset
|
47 #include "errwarn.h" |
24540
46440078d73b
don't use singleton for octave_value_typeinfo
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
48 #include "interpreter-private.h" |
4634 | 49 #include "load-save.h" |
23455
73ff72d3d603
maint: Eliminate <cfloat.h> header from libinterp files
Rik <rik@octave.org>
parents:
23450
diff
changeset
|
50 #include "ls-oct-binary.h" |
73ff72d3d603
maint: Eliminate <cfloat.h> header from libinterp files
Rik <rik@octave.org>
parents:
23450
diff
changeset
|
51 #include "ls-utils.h" |
4634 | 52 #include "ov-cell.h" |
23455
73ff72d3d603
maint: Eliminate <cfloat.h> header from libinterp files
Rik <rik@octave.org>
parents:
23450
diff
changeset
|
53 #include "ov.h" |
4634 | 54 #include "pager.h" |
55 #include "sysdep.h" | |
56 #include "utils.h" | |
57 #include "variables.h" | |
58 #include "version.h" | |
59 | |
60 // Extract one value (scalar, matrix, string, etc.) from stream IS and | |
61 // place it in TC, returning the name of the variable. If the value | |
62 // is tagged as global in the file, return TRUE in GLOBAL. If SWAP | |
63 // is TRUE, swap bytes after reading. | |
64 // | |
65 // The data is expected to be in the following format: | |
66 // | |
67 // Header (one per file): | |
68 // ===================== | |
69 // | |
70 // object type bytes | |
71 // ------ ---- ----- | |
72 // magic number string 10 | |
73 // | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
74 // float format integer 1 |
4634 | 75 // |
76 // | |
77 // Data (one set for each item): | |
78 // ============================ | |
79 // | |
80 // object type bytes | |
81 // ------ ---- ----- | |
82 // name_length integer 4 | |
83 // | |
84 // name string name_length | |
85 // | |
86 // doc_length integer 4 | |
87 // | |
88 // doc string doc_length | |
89 // | |
90 // global flag integer 1 | |
91 // | |
4687 | 92 // data type char 1 |
4634 | 93 // |
4687 | 94 // In general "data type" is 255, and in that case the next arguments |
95 // in the data set are | |
4634 | 96 // |
4687 | 97 // object type bytes |
98 // ------ ---- ----- | |
99 // type_length integer 4 | |
4634 | 100 // |
4687 | 101 // type string type_length |
102 // | |
24540
46440078d73b
don't use singleton for octave_value_typeinfo
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
103 // The string "type" is then used with octave::type_info::lookup_type |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21739
diff
changeset
|
104 // to create an octave_value of the correct type. The specific load/save |
4687 | 105 // function is then called. |
4634 | 106 // |
20837 | 107 // For backward compatibility "data type" can also be a value between 1 |
4687 | 108 // and 7, where this defines a hardcoded octave_value of the type |
4634 | 109 // |
4687 | 110 // data type octave_value |
111 // --------- ------------ | |
112 // 1 scalar | |
113 // 2 matrix | |
114 // 3 complex scalar | |
115 // 4 complex matrix | |
116 // 5 string (old style storage) | |
117 // 6 range | |
118 // 7 string | |
4634 | 119 // |
4687 | 120 // Except for "data type" equal 5 that requires special treatment, these |
121 // old style "data type" value also cause the specific load/save functions | |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21739
diff
changeset
|
122 // to be called. FILENAME is used for error messages. |
4634 | 123 |
124 std::string | |
125 read_binary_data (std::istream& is, bool swap, | |
21739
c4ab2e54f100
use namespace for oct_mach_info class
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
126 octave::mach_info::float_format fmt, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
127 const std::string& filename, bool& global, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
128 octave_value& tc, std::string& doc) |
4634 | 129 { |
130 std::string retval; | |
131 | |
6240 | 132 unsigned char tmp = 0; |
4634 | 133 |
5828 | 134 int32_t name_len = 0; |
135 int32_t doc_len = 0; | |
4634 | 136 |
23812
057a894914df
Use C++11 string fcns back() and pop_back() to simplify code.
Rik <rik@octave.org>
parents:
23470
diff
changeset
|
137 doc.clear (); |
4634 | 138 |
139 // We expect to fail here, at the beginning of a record, so not | |
140 // being able to read another name should not result in an error. | |
141 | |
5760 | 142 is.read (reinterpret_cast<char *> (&name_len), 4); |
4634 | 143 if (! is) |
144 return retval; | |
145 if (swap) | |
4944 | 146 swap_bytes<4> (&name_len); |
4634 | 147 |
148 { | |
149 OCTAVE_LOCAL_BUFFER (char, name, name_len+1); | |
150 name[name_len] = '\0'; | |
5760 | 151 if (! is.read (reinterpret_cast<char *> (name), name_len)) |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
152 error ("load: trouble reading binary file '%s'", filename.c_str ()); |
4634 | 153 retval = name; |
154 } | |
155 | |
5760 | 156 is.read (reinterpret_cast<char *> (&doc_len), 4); |
4634 | 157 if (! is) |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
158 error ("load: trouble reading binary file '%s'", filename.c_str ()); |
4634 | 159 if (swap) |
4944 | 160 swap_bytes<4> (&doc_len); |
4634 | 161 |
162 { | |
163 OCTAVE_LOCAL_BUFFER (char, tdoc, doc_len+1); | |
164 tdoc[doc_len] = '\0'; | |
5760 | 165 if (! is.read (reinterpret_cast<char *> (tdoc), doc_len)) |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
166 error ("load: trouble reading binary file '%s'", filename.c_str ()); |
4634 | 167 doc = tdoc; |
168 } | |
169 | |
5760 | 170 if (! is.read (reinterpret_cast<char *> (&tmp), 1)) |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
171 error ("load: trouble reading binary file '%s'", filename.c_str ()); |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
172 global = (tmp ? 1 : 0); |
4634 | 173 |
174 tmp = 0; | |
5760 | 175 if (! is.read (reinterpret_cast<char *> (&tmp), 1)) |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
176 error ("load: trouble reading binary file '%s'", filename.c_str ()); |
4634 | 177 |
24540
46440078d73b
don't use singleton for octave_value_typeinfo
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
178 octave::type_info& type_info |
46440078d73b
don't use singleton for octave_value_typeinfo
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
179 = octave::__get_type_info__ ("read_binary_data"); |
46440078d73b
don't use singleton for octave_value_typeinfo
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
180 |
4687 | 181 // All cases except 255 kept for backwards compatibility |
4634 | 182 switch (tmp) |
183 { | |
184 case 1: | |
24540
46440078d73b
don't use singleton for octave_value_typeinfo
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
185 tc = type_info.lookup_type ("scalar"); |
4634 | 186 break; |
187 | |
188 case 2: | |
24540
46440078d73b
don't use singleton for octave_value_typeinfo
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
189 tc = type_info.lookup_type ("matrix"); |
4634 | 190 break; |
191 | |
192 case 3: | |
24540
46440078d73b
don't use singleton for octave_value_typeinfo
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
193 tc = type_info.lookup_type ("complex scalar"); |
4634 | 194 break; |
195 | |
196 case 4: | |
24540
46440078d73b
don't use singleton for octave_value_typeinfo
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
197 tc = type_info.lookup_type ("complex matrix"); |
4634 | 198 break; |
199 | |
200 case 5: | |
201 { | |
17861
870f3e12e163
maint: Use phrase "FIXME:" for problem areas in code.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
202 // FIXME: |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21739
diff
changeset
|
203 // This is cruft, since its for a save type that is old. |
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21739
diff
changeset
|
204 // Maybe this is taking backward compatibility too far! |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
205 int32_t len; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
206 if (! is.read (reinterpret_cast<char *> (&len), 4)) |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
207 error ("load: trouble reading binary file '%s'", filename.c_str ()); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
208 if (swap) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
209 swap_bytes<4> (&len); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
210 OCTAVE_LOCAL_BUFFER (char, s, len+1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
211 if (! is.read (reinterpret_cast<char *> (s), len)) |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
212 error ("load: trouble reading binary file '%s'", filename.c_str ()); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
213 s[len] = '\0'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
214 tc = s; |
4687 | 215 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
216 // Early return, since don't want rest of this function |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
217 return retval; |
4634 | 218 } |
219 break; | |
220 | |
221 case 6: | |
24540
46440078d73b
don't use singleton for octave_value_typeinfo
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
222 tc = type_info.lookup_type ("range"); |
4634 | 223 break; |
224 | |
225 case 7: | |
24540
46440078d73b
don't use singleton for octave_value_typeinfo
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
226 tc = type_info.lookup_type ("string"); |
4687 | 227 break; |
228 | |
229 case 255: | |
4634 | 230 { |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
231 // Read the saved variable type |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
232 int32_t len; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
233 if (! is.read (reinterpret_cast<char *> (&len), 4)) |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
234 error ("load: trouble reading binary file '%s'", filename.c_str ()); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
235 if (swap) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
236 swap_bytes<4> (&len); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
237 OCTAVE_LOCAL_BUFFER (char, s, len+1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
238 if (! is.read (s, len)) |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
239 error ("load: trouble reading binary file '%s'", filename.c_str ()); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
240 s[len] = '\0'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
241 std::string typ = s; |
24540
46440078d73b
don't use singleton for octave_value_typeinfo
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
242 tc = type_info.lookup_type (typ); |
4634 | 243 } |
244 break; | |
245 default: | |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
246 error ("load: trouble reading binary file '%s'", filename.c_str ()); |
4687 | 247 break; |
248 } | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
249 |
20955
77f5591878bf
maint: Use '! expr' rather than '!expr' to conform to coding guidelines.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
250 if (! tc.load_binary (is, swap, fmt)) |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
251 error ("load: trouble reading binary file '%s'", filename.c_str ()); |
4634 | 252 |
253 return retval; | |
254 } | |
255 | |
256 // Save the data from TC along with the corresponding NAME, help | |
257 // string DOC, and global flag MARK_AS_GLOBAL on stream OS in the | |
258 // binary format described above for read_binary_data. | |
259 | |
260 bool | |
261 save_binary_data (std::ostream& os, const octave_value& tc, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
262 const std::string& name, const std::string& doc, |
23470
a41fdb801db6
use mark_global and mark_persistent consistently
John W. Eaton <jwe@octave.org>
parents:
23455
diff
changeset
|
263 bool mark_global, bool save_as_floats) |
4634 | 264 { |
5828 | 265 int32_t name_len = name.length (); |
4634 | 266 |
5760 | 267 os.write (reinterpret_cast<char *> (&name_len), 4); |
4634 | 268 os << name; |
269 | |
5828 | 270 int32_t doc_len = doc.length (); |
4634 | 271 |
5760 | 272 os.write (reinterpret_cast<char *> (&doc_len), 4); |
4634 | 273 os << doc; |
274 | |
4730 | 275 unsigned char tmp; |
4634 | 276 |
23470
a41fdb801db6
use mark_global and mark_persistent consistently
John W. Eaton <jwe@octave.org>
parents:
23455
diff
changeset
|
277 tmp = mark_global; |
5760 | 278 os.write (reinterpret_cast<char *> (&tmp), 1); |
4634 | 279 |
4687 | 280 // 255 flags the new binary format |
281 tmp = 255; | |
5760 | 282 os.write (reinterpret_cast<char *> (&tmp), 1); |
4634 | 283 |
4687 | 284 // Write the string corresponding to the octave_value type |
285 std::string typ = tc.type_name (); | |
5828 | 286 int32_t len = typ.length (); |
5760 | 287 os.write (reinterpret_cast<char *> (&len), 4); |
4687 | 288 const char *btmp = typ.data (); |
5760 | 289 os.write (btmp, len); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
290 |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21739
diff
changeset
|
291 // The octave_value of tc is const. Make a copy... |
4687 | 292 octave_value val = tc; |
293 | |
294 // Call specific save function | |
295 bool success = val.save_binary (os, save_as_floats); | |
296 | |
297 return (os && success); | |
4634 | 298 } |