4903
|
1 /* |
|
2 |
|
3 Copyright (C) 2004 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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include <climits> |
|
32 |
|
33 #include <iostream> |
|
34 #include <vector> |
|
35 |
|
36 #include "lo-ieee.h" |
|
37 #include "lo-utils.h" |
|
38 #include "mx-base.h" |
|
39 #include "quit.h" |
|
40 |
|
41 #include "defun.h" |
|
42 #include "gripes.h" |
|
43 #include "oct-obj.h" |
|
44 #include "oct-lvalue.h" |
|
45 #include "ops.h" |
|
46 #include "ov-base.h" |
|
47 #include "ov-base-mat.h" |
|
48 #include "ov-base-mat.cc" |
|
49 #include "ov-base-scalar.h" |
|
50 #include "ov-base-scalar.cc" |
|
51 #include "ov-base-int.h" |
|
52 #include "ov-int-traits.h" |
|
53 #include "pr-output.h" |
|
54 #include "variables.h" |
|
55 |
|
56 #include "byte-swap.h" |
|
57 #include "ls-oct-ascii.h" |
|
58 #include "ls-utils.h" |
|
59 #include "ls-hdf5.h" |
|
60 |
|
61 template <class T> |
|
62 octave_value * |
|
63 octave_base_int_matrix<T>::try_narrowing_conversion (void) |
|
64 { |
|
65 octave_value *retval = 0; |
|
66 |
4932
|
67 if (this->matrix.nelem () == 1) |
|
68 retval = new typename octave_value_int_traits<T>::scalar_type (this->matrix (0)); |
4903
|
69 |
|
70 return retval; |
|
71 } |
|
72 |
|
73 template <class T> |
|
74 bool |
4917
|
75 octave_base_int_matrix<T>::save_ascii (std::ostream& os, bool&, bool) |
4903
|
76 { |
4932
|
77 dim_vector d = this->dims (); |
4903
|
78 |
|
79 os << "# ndims: " << d.length () << "\n"; |
|
80 |
|
81 for (int i = 0; i < d.length (); i++) |
|
82 os << " " << d (i); |
|
83 |
4932
|
84 os << "\n" << this->matrix; |
4903
|
85 |
|
86 return true; |
|
87 } |
|
88 |
|
89 template <class T> |
|
90 bool |
|
91 octave_base_int_matrix<T>::load_ascii (std::istream& is) |
|
92 { |
|
93 int mdims = 0; |
|
94 bool success = true; |
|
95 |
|
96 if (extract_keyword (is, "ndims", mdims, true)) |
|
97 { |
|
98 if (mdims >= 0) |
|
99 { |
|
100 dim_vector dv; |
|
101 dv.resize (mdims); |
|
102 |
|
103 for (int i = 0; i < mdims; i++) |
|
104 is >> dv(i); |
|
105 |
|
106 T tmp(dv); |
|
107 |
|
108 is >> tmp; |
|
109 |
|
110 if (!is) |
|
111 { |
|
112 error ("load: failed to load matrix constant"); |
|
113 success = false; |
|
114 } |
|
115 |
4932
|
116 this->matrix = tmp; |
4903
|
117 } |
|
118 else |
|
119 { |
|
120 error ("load: failed to extract number of rows and columns"); |
|
121 success = false; |
|
122 } |
|
123 } |
|
124 else |
|
125 error ("load: failed to extract number of dimensions"); |
|
126 |
|
127 return success; |
|
128 } |
|
129 |
|
130 template <class T> |
|
131 bool |
4917
|
132 octave_base_int_matrix<T>::save_binary (std::ostream& os, bool&) |
4903
|
133 { |
4932
|
134 dim_vector d = this->dims (); |
4903
|
135 if (d.length() < 1) |
|
136 return false; |
|
137 |
|
138 // Use negative value for ndims to differentiate with old format!! |
|
139 FOUR_BYTE_INT tmp = - d.length(); |
|
140 os.write (X_CAST (char *, &tmp), 4); |
|
141 for (int i=0; i < d.length (); i++) |
|
142 { |
|
143 tmp = d(i); |
|
144 os.write (X_CAST (char *, &tmp), 4); |
|
145 } |
|
146 |
4932
|
147 os.write (X_CAST(char *, this->matrix.data()), this->byte_size()); |
4903
|
148 |
|
149 return true; |
|
150 } |
|
151 |
|
152 template <class T> |
|
153 bool |
|
154 octave_base_int_matrix<T>::load_binary (std::istream& is, bool swap, |
4917
|
155 oct_mach_info::float_format ) |
4903
|
156 { |
|
157 FOUR_BYTE_INT mdims; |
|
158 if (! is.read (X_CAST (char *, &mdims), 4)) |
|
159 return false; |
|
160 if (swap) |
|
161 swap_4_bytes (X_CAST (char *, &mdims)); |
4917
|
162 if (mdims >= 0) |
|
163 return false; |
4903
|
164 |
4917
|
165 mdims = - mdims; |
|
166 FOUR_BYTE_INT di; |
|
167 dim_vector dv; |
|
168 dv.resize (mdims); |
4903
|
169 |
4917
|
170 for (int i = 0; i < mdims; i++) |
4903
|
171 { |
4917
|
172 if (! is.read (X_CAST (char *, &di), 4)) |
4903
|
173 return false; |
|
174 if (swap) |
4917
|
175 swap_4_bytes (X_CAST (char *, &di)); |
|
176 dv(i) = di; |
4903
|
177 } |
|
178 |
4917
|
179 T m (dv); |
|
180 |
|
181 if (! is.read (X_CAST (char *, m.data ()), m.byte_size ())) |
|
182 return false; |
4903
|
183 |
4917
|
184 if (swap) |
|
185 { |
|
186 int nel = dv.numel (); |
|
187 int bytes = nel / m.byte_size(); |
|
188 for (int i = 0; i < nel; i++) |
|
189 switch (bytes) |
|
190 { |
|
191 case 8: |
|
192 swap_8_bytes (X_CAST (char *, &m(i))); |
|
193 break; |
|
194 case 4: |
|
195 swap_4_bytes (X_CAST (char *, &m(i))); |
|
196 break; |
|
197 case 2: |
|
198 swap_2_bytes (X_CAST (char *, &m(i))); |
|
199 break; |
|
200 case 1: |
|
201 default: |
|
202 break; |
|
203 } |
|
204 } |
|
205 |
4932
|
206 this->matrix = m; |
4903
|
207 return true; |
|
208 } |
|
209 |
|
210 #if defined (HAVE_HDF5) |
|
211 |
|
212 template <class T> |
|
213 bool |
4917
|
214 octave_base_int_matrix<T>::save_hdf5 (hid_t loc_id, const char *name, bool) |
4903
|
215 { |
4917
|
216 hid_t save_type_hid = HDF5_SAVE_TYPE; |
4903
|
217 bool retval = true; |
4932
|
218 dim_vector dv = this->dims (); |
4903
|
219 int empty = save_hdf5_empty (loc_id, name, dv); |
|
220 if (empty) |
|
221 return (empty > 0); |
|
222 |
|
223 int rank = dv.length (); |
|
224 hid_t space_hid = -1, data_hid = -1; |
|
225 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
|
226 |
|
227 // Octave uses column-major, while HDF5 uses row-major ordering |
|
228 for (int i = 0; i < rank; i++) |
|
229 hdims[i] = dv (rank-i-1); |
|
230 |
|
231 space_hid = H5Screate_simple (rank, hdims, 0); |
|
232 |
|
233 if (space_hid < 0) return false; |
|
234 |
|
235 data_hid = H5Dcreate (loc_id, name, save_type_hid, space_hid, |
|
236 H5P_DEFAULT); |
|
237 if (data_hid < 0) |
|
238 { |
|
239 H5Sclose (space_hid); |
|
240 return false; |
|
241 } |
|
242 |
4917
|
243 retval = H5Dwrite (data_hid, save_type_hid, H5S_ALL, H5S_ALL, |
4932
|
244 H5P_DEFAULT, this->matrix.data()) >= 0; |
4903
|
245 |
|
246 H5Dclose (data_hid); |
|
247 H5Sclose (space_hid); |
|
248 |
|
249 return retval; |
|
250 } |
|
251 |
|
252 template <class T> |
|
253 bool |
|
254 octave_base_int_matrix<T>::load_hdf5 (hid_t loc_id, const char *name, |
|
255 bool /* have_h5giterate_bug */) |
|
256 { |
4917
|
257 hid_t save_type_hid = HDF5_SAVE_TYPE; |
4903
|
258 bool retval = false; |
|
259 dim_vector dv; |
|
260 int empty = load_hdf5_empty (loc_id, name, dv); |
|
261 if (empty > 0) |
4932
|
262 this->matrix.resize(dv); |
4903
|
263 if (empty) |
|
264 return (empty > 0); |
|
265 |
|
266 hid_t data_hid = H5Dopen (loc_id, name); |
|
267 hid_t space_id = H5Dget_space (data_hid); |
|
268 |
|
269 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
270 |
|
271 if (rank < 1) |
|
272 { |
|
273 H5Sclose (space_id); |
|
274 H5Dclose (data_hid); |
|
275 return false; |
|
276 } |
|
277 |
|
278 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
|
279 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); |
|
280 |
|
281 H5Sget_simple_extent_dims (space_id, hdims, maxdims); |
|
282 |
|
283 // Octave uses column-major, while HDF5 uses row-major ordering |
|
284 if (rank == 1) |
|
285 { |
|
286 dv.resize (2); |
|
287 dv(0) = 1; |
|
288 dv(1) = hdims[0]; |
|
289 } |
|
290 else |
|
291 { |
|
292 dv.resize (rank); |
|
293 for (hsize_t i = 0, j = rank - 1; i < rank; i++, j--) |
|
294 dv(j) = hdims[i]; |
|
295 } |
|
296 |
4917
|
297 T m (dv); |
|
298 if (H5Dread (data_hid, save_type_hid, H5S_ALL, H5S_ALL, |
|
299 H5P_DEFAULT, m.fortran_vec()) >= 0) |
4903
|
300 { |
|
301 retval = true; |
4932
|
302 this->matrix = m; |
4903
|
303 } |
|
304 |
|
305 H5Sclose (space_id); |
|
306 H5Dclose (data_hid); |
|
307 |
|
308 return retval; |
|
309 } |
|
310 |
|
311 #endif |
|
312 |
|
313 template <class T> |
|
314 void |
|
315 octave_base_int_matrix<T>::print_raw (std::ostream& os, |
|
316 bool pr_as_read_syntax) const |
|
317 { |
4932
|
318 octave_print_internal (os, this->matrix, pr_as_read_syntax, |
|
319 this->current_print_indent_level ()); |
4903
|
320 } |
|
321 |
|
322 template <class T> |
|
323 bool |
4917
|
324 octave_base_int_scalar<T>::save_ascii (std::ostream& os, bool& , bool) |
4903
|
325 { |
4932
|
326 os << this->scalar << "\n"; |
4903
|
327 return true; |
|
328 } |
|
329 |
|
330 template <class T> |
|
331 bool |
|
332 octave_base_int_scalar<T>::load_ascii (std::istream& is) |
|
333 { |
4932
|
334 is >> this->scalar; |
4903
|
335 if (!is) |
|
336 { |
|
337 error ("load: failed to load scalar constant"); |
|
338 return false; |
|
339 } |
|
340 return true; |
|
341 } |
|
342 |
|
343 template <class T> |
|
344 bool |
4917
|
345 octave_base_int_scalar<T>::save_binary (std::ostream& os, bool&) |
4903
|
346 { |
4932
|
347 os.write (X_CAST(char *, &(this->scalar)), this->byte_size()); |
4903
|
348 return true; |
|
349 } |
|
350 |
|
351 template <class T> |
|
352 bool |
|
353 octave_base_int_scalar<T>::load_binary (std::istream& is, bool swap, |
4917
|
354 oct_mach_info::float_format) |
4903
|
355 { |
4917
|
356 T tmp; |
4932
|
357 if (! is.read (X_CAST (char *, &tmp), this->byte_size())) |
4903
|
358 return false; |
|
359 |
4917
|
360 if (swap) |
4932
|
361 switch (this->byte_size()) |
4917
|
362 { |
|
363 case 8: |
|
364 swap_8_bytes (X_CAST (char *, &tmp)); |
|
365 break; |
|
366 case 4: |
|
367 swap_4_bytes (X_CAST (char *, &tmp)); |
|
368 break; |
|
369 case 2: |
|
370 swap_2_bytes (X_CAST (char *, &tmp)); |
|
371 break; |
|
372 case 1: |
|
373 default: |
|
374 break; |
|
375 } |
4932
|
376 this->scalar = tmp; |
4903
|
377 return true; |
|
378 } |
|
379 |
|
380 #if defined (HAVE_HDF5) |
|
381 template <class T> |
|
382 bool |
4917
|
383 octave_base_int_scalar<T>::save_hdf5 (hid_t loc_id, const char *name, bool) |
4903
|
384 { |
4917
|
385 hid_t save_type_hid = HDF5_SAVE_TYPE; |
4903
|
386 bool retval = true; |
|
387 hsize_t dimens[3]; |
|
388 hid_t space_hid = -1, data_hid = -1; |
|
389 |
|
390 space_hid = H5Screate_simple (0, dimens, 0); |
|
391 if (space_hid < 0) return false; |
|
392 |
4917
|
393 data_hid = H5Dcreate (loc_id, name, save_type_hid, space_hid, |
4903
|
394 H5P_DEFAULT); |
|
395 if (data_hid < 0) |
|
396 { |
|
397 H5Sclose (space_hid); |
|
398 return false; |
|
399 } |
|
400 |
4917
|
401 retval = H5Dwrite (data_hid, save_type_hid, H5S_ALL, H5S_ALL, |
4932
|
402 H5P_DEFAULT, &(this->scalar)) >= 0; |
4903
|
403 |
|
404 H5Dclose (data_hid); |
|
405 H5Sclose (space_hid); |
|
406 |
|
407 return retval; |
|
408 } |
|
409 |
|
410 template <class T> |
|
411 bool |
|
412 octave_base_int_scalar<T>::load_hdf5 (hid_t loc_id, const char *name, |
|
413 bool /* have_h5giterate_bug */) |
|
414 { |
4917
|
415 hid_t save_type_hid = HDF5_SAVE_TYPE; |
4903
|
416 hid_t data_hid = H5Dopen (loc_id, name); |
|
417 hid_t space_id = H5Dget_space (data_hid); |
|
418 |
|
419 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
420 |
|
421 if (rank != 0) |
|
422 { |
|
423 H5Dclose (data_hid); |
|
424 return false; |
|
425 } |
|
426 |
4917
|
427 T tmp; |
|
428 if (H5Dread (data_hid, save_type_hid, H5S_ALL, H5S_ALL, |
|
429 H5P_DEFAULT, &tmp) < 0) |
4903
|
430 { |
|
431 H5Dclose (data_hid); |
|
432 return false; |
|
433 } |
|
434 |
4932
|
435 this->scalar = tmp; |
4903
|
436 |
|
437 H5Dclose (data_hid); |
|
438 |
|
439 return true; |
|
440 } |
|
441 #endif |
|
442 |
|
443 /* |
|
444 ;;; Local Variables: *** |
|
445 ;;; mode: C++ *** |
|
446 ;;; End: *** |
|
447 */ |