4634
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
4634
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <cfloat> |
|
29 #include <cstring> |
|
30 #include <cctype> |
|
31 |
|
32 #include <fstream> |
|
33 #include <iomanip> |
|
34 #include <iostream> |
|
35 #include <string> |
4726
|
36 #include <vector> |
4634
|
37 |
|
38 #include "byte-swap.h" |
|
39 #include "data-conv.h" |
|
40 #include "file-ops.h" |
|
41 #include "glob-match.h" |
|
42 #include "lo-mappers.h" |
|
43 #include "mach-info.h" |
|
44 #include "oct-env.h" |
|
45 #include "oct-time.h" |
|
46 #include "quit.h" |
|
47 #include "str-vec.h" |
|
48 |
|
49 #include "Cell.h" |
|
50 #include "defun.h" |
|
51 #include "error.h" |
|
52 #include "gripes.h" |
|
53 #include "load-save.h" |
|
54 #include "oct-obj.h" |
|
55 #include "oct-map.h" |
|
56 #include "ov-cell.h" |
|
57 #include "pager.h" |
|
58 #include "pt-exp.h" |
|
59 #include "symtab.h" |
|
60 #include "sysdep.h" |
|
61 #include "unwind-prot.h" |
|
62 #include "utils.h" |
|
63 #include "variables.h" |
|
64 #include "version.h" |
|
65 #include "dMatrix.h" |
|
66 |
|
67 #include "ls-utils.h" |
|
68 #include "ls-oct-binary.h" |
|
69 |
|
70 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
71 // place it in TC, returning the name of the variable. If the value |
|
72 // is tagged as global in the file, return TRUE in GLOBAL. If SWAP |
|
73 // is TRUE, swap bytes after reading. |
|
74 // |
|
75 // The data is expected to be in the following format: |
|
76 // |
|
77 // Header (one per file): |
|
78 // ===================== |
|
79 // |
|
80 // object type bytes |
|
81 // ------ ---- ----- |
|
82 // magic number string 10 |
|
83 // |
|
84 // float format integer 1 |
|
85 // |
|
86 // |
|
87 // Data (one set for each item): |
|
88 // ============================ |
|
89 // |
|
90 // object type bytes |
|
91 // ------ ---- ----- |
|
92 // name_length integer 4 |
|
93 // |
|
94 // name string name_length |
|
95 // |
|
96 // doc_length integer 4 |
|
97 // |
|
98 // doc string doc_length |
|
99 // |
|
100 // global flag integer 1 |
|
101 // |
4687
|
102 // data type char 1 |
4634
|
103 // |
4687
|
104 // In general "data type" is 255, and in that case the next arguments |
|
105 // in the data set are |
4634
|
106 // |
4687
|
107 // object type bytes |
|
108 // ------ ---- ----- |
|
109 // type_length integer 4 |
4634
|
110 // |
4687
|
111 // type string type_length |
|
112 // |
|
113 // The string "type" is then used with octave_value_typeinfo::lookup_type |
|
114 // to create an octave_value of the correct type. The specific load/save |
|
115 // function is then called. |
4634
|
116 // |
4687
|
117 // For backward compatiablity "data type" can also be a value between 1 |
|
118 // and 7, where this defines a hardcoded octave_value of the type |
4634
|
119 // |
4687
|
120 // data type octave_value |
|
121 // --------- ------------ |
|
122 // 1 scalar |
|
123 // 2 matrix |
|
124 // 3 complex scalar |
|
125 // 4 complex matrix |
|
126 // 5 string (old style storage) |
|
127 // 6 range |
|
128 // 7 string |
4634
|
129 // |
4687
|
130 // Except for "data type" equal 5 that requires special treatment, these |
|
131 // old style "data type" value also cause the specific load/save functions |
|
132 // to be called. FILENAME is used for error messages. |
4634
|
133 |
|
134 std::string |
|
135 read_binary_data (std::istream& is, bool swap, |
|
136 oct_mach_info::float_format fmt, |
|
137 const std::string& filename, bool& global, |
|
138 octave_value& tc, std::string& doc) |
|
139 { |
|
140 std::string retval; |
|
141 |
6240
|
142 unsigned char tmp = 0; |
4634
|
143 |
5828
|
144 int32_t name_len = 0; |
|
145 int32_t doc_len = 0; |
4634
|
146 |
|
147 doc.resize (0); |
|
148 |
|
149 // We expect to fail here, at the beginning of a record, so not |
|
150 // being able to read another name should not result in an error. |
|
151 |
5760
|
152 is.read (reinterpret_cast<char *> (&name_len), 4); |
4634
|
153 if (! is) |
|
154 return retval; |
|
155 if (swap) |
4944
|
156 swap_bytes<4> (&name_len); |
4634
|
157 |
|
158 { |
|
159 OCTAVE_LOCAL_BUFFER (char, name, name_len+1); |
|
160 name[name_len] = '\0'; |
5760
|
161 if (! is.read (reinterpret_cast<char *> (name), name_len)) |
4634
|
162 goto data_read_error; |
|
163 retval = name; |
|
164 } |
|
165 |
5760
|
166 is.read (reinterpret_cast<char *> (&doc_len), 4); |
4634
|
167 if (! is) |
|
168 goto data_read_error; |
|
169 if (swap) |
4944
|
170 swap_bytes<4> (&doc_len); |
4634
|
171 |
|
172 { |
|
173 OCTAVE_LOCAL_BUFFER (char, tdoc, doc_len+1); |
|
174 tdoc[doc_len] = '\0'; |
5760
|
175 if (! is.read (reinterpret_cast<char *> (tdoc), doc_len)) |
4634
|
176 goto data_read_error; |
|
177 doc = tdoc; |
|
178 } |
|
179 |
5760
|
180 if (! is.read (reinterpret_cast<char *> (&tmp), 1)) |
4634
|
181 goto data_read_error; |
|
182 global = tmp ? 1 : 0; |
|
183 |
|
184 tmp = 0; |
5760
|
185 if (! is.read (reinterpret_cast<char *> (&tmp), 1)) |
4634
|
186 goto data_read_error; |
|
187 |
4687
|
188 // All cases except 255 kept for backwards compatibility |
4634
|
189 switch (tmp) |
|
190 { |
|
191 case 1: |
4687
|
192 tc = octave_value_typeinfo::lookup_type ("scalar"); |
4634
|
193 break; |
|
194 |
|
195 case 2: |
4687
|
196 tc = octave_value_typeinfo::lookup_type ("matrix"); |
4634
|
197 break; |
|
198 |
|
199 case 3: |
4687
|
200 tc = octave_value_typeinfo::lookup_type ("complex scalar"); |
4634
|
201 break; |
|
202 |
|
203 case 4: |
4687
|
204 tc = octave_value_typeinfo::lookup_type ("complex matrix"); |
4634
|
205 break; |
|
206 |
|
207 case 5: |
|
208 { |
5775
|
209 // FIXMEX |
4687
|
210 // This is cruft, since its for a save type that is old. Maybe |
|
211 // this is taking backward compatability too far!! |
5828
|
212 int32_t len; |
5760
|
213 if (! is.read (reinterpret_cast<char *> (&len), 4)) |
4634
|
214 goto data_read_error; |
|
215 if (swap) |
4944
|
216 swap_bytes<4> (&len); |
4634
|
217 OCTAVE_LOCAL_BUFFER (char, s, len+1); |
5760
|
218 if (! is.read (reinterpret_cast<char *> (s), len)) |
4634
|
219 goto data_read_error; |
|
220 s[len] = '\0'; |
|
221 tc = s; |
4687
|
222 |
|
223 // Early return, since don't want rest of this function |
|
224 return retval; |
4634
|
225 } |
|
226 break; |
|
227 |
|
228 case 6: |
4687
|
229 tc = octave_value_typeinfo::lookup_type ("range"); |
4634
|
230 break; |
|
231 |
|
232 case 7: |
4687
|
233 tc = octave_value_typeinfo::lookup_type ("string"); |
|
234 break; |
|
235 |
|
236 case 255: |
4634
|
237 { |
4687
|
238 // Read the saved variable type |
5828
|
239 int32_t len; |
5760
|
240 if (! is.read (reinterpret_cast<char *> (&len), 4)) |
4634
|
241 goto data_read_error; |
|
242 if (swap) |
4944
|
243 swap_bytes<4> (&len); |
4687
|
244 OCTAVE_LOCAL_BUFFER (char, s, len+1); |
5760
|
245 if (! is.read (s, len)) |
4687
|
246 goto data_read_error; |
|
247 s[len] = '\0'; |
|
248 std::string typ = s; |
|
249 tc = octave_value_typeinfo::lookup_type (typ); |
4634
|
250 } |
|
251 break; |
|
252 default: |
4687
|
253 goto data_read_error; |
|
254 break; |
|
255 } |
|
256 |
|
257 if (!tc.load_binary (is, swap, fmt)) |
|
258 { |
4634
|
259 data_read_error: |
|
260 error ("load: trouble reading binary file `%s'", filename.c_str ()); |
|
261 } |
|
262 |
|
263 return retval; |
|
264 } |
|
265 |
|
266 // Save the data from TC along with the corresponding NAME, help |
|
267 // string DOC, and global flag MARK_AS_GLOBAL on stream OS in the |
|
268 // binary format described above for read_binary_data. |
|
269 |
|
270 bool |
|
271 save_binary_data (std::ostream& os, const octave_value& tc, |
|
272 const std::string& name, const std::string& doc, |
|
273 bool mark_as_global, bool save_as_floats) |
|
274 { |
5828
|
275 int32_t name_len = name.length (); |
4634
|
276 |
5760
|
277 os.write (reinterpret_cast<char *> (&name_len), 4); |
4634
|
278 os << name; |
|
279 |
5828
|
280 int32_t doc_len = doc.length (); |
4634
|
281 |
5760
|
282 os.write (reinterpret_cast<char *> (&doc_len), 4); |
4634
|
283 os << doc; |
|
284 |
4730
|
285 unsigned char tmp; |
4634
|
286 |
|
287 tmp = mark_as_global; |
5760
|
288 os.write (reinterpret_cast<char *> (&tmp), 1); |
4634
|
289 |
4687
|
290 // 255 flags the new binary format |
|
291 tmp = 255; |
5760
|
292 os.write (reinterpret_cast<char *> (&tmp), 1); |
4634
|
293 |
4687
|
294 // Write the string corresponding to the octave_value type |
|
295 std::string typ = tc.type_name (); |
5828
|
296 int32_t len = typ.length (); |
5760
|
297 os.write (reinterpret_cast<char *> (&len), 4); |
4687
|
298 const char *btmp = typ.data (); |
5760
|
299 os.write (btmp, len); |
4687
|
300 |
|
301 // The octave_value of tc is const. Make a copy... |
|
302 octave_value val = tc; |
|
303 |
|
304 // Call specific save function |
|
305 bool success = val.save_binary (os, save_as_floats); |
|
306 |
|
307 return (os && success); |
4634
|
308 } |
|
309 |
|
310 /* |
|
311 ;;; Local Variables: *** |
|
312 ;;; mode: C++ *** |
|
313 ;;; End: *** |
|
314 */ |
|
315 |