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