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