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