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