Mercurial > hg > octave-nkf
annotate src/ov-base-int.cc @ 9585:06b8b51dca48
also handle user-defined graphics properties in new property name validation scheme
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 28 Aug 2009 18:37:31 -0400 |
parents | 7120fbbecf97 |
children | 34d6f005db4b |
rev | line source |
---|---|
4903 | 1 /* |
2 | |
8920 | 3 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 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" | |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
7534
diff
changeset
|
36 #include "oct-locbuf.h" |
4903 | 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 | |
7534
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
59 // We have all the machinery below (octave_base_int_helper and |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
60 // octave_base_int_helper_traits) to avoid a few warnings from GCC |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
61 // about comparisons always false due to limited range of data types. |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
62 // Ugh. The cure may be worse than the disease. |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
63 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
64 template <class T, bool is_signed = true, bool can_be_too_big = true> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
65 struct octave_base_int_helper |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
66 { |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
67 static bool |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
68 char_value_out_of_range (T val) { return val < 0 || val > UCHAR_MAX; } |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
69 }; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
70 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
71 template <class T> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
72 struct octave_base_int_helper<T, false, false> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
73 { |
9149
7120fbbecf97
ov-base-int.cc: correct result for template specialization
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
74 static bool char_value_out_of_range (T) { return false; } |
7534
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
75 }; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
76 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
77 template <class T> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
78 struct octave_base_int_helper<T, false, true> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
79 { |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
80 static bool char_value_out_of_range (T val) { return val > UCHAR_MAX; } |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
81 }; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
82 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
83 template <class T> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
84 struct octave_base_int_helper<T, true, false> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
85 { |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
86 static bool char_value_out_of_range (T val) { return val < 0; } |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
87 }; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
88 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
89 // For all types other than char, signed char, and unsigned char, we |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
90 // assume that the upper limit for the range of allowable values is |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
91 // larger than the range for unsigned char. If that's not true, we |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
92 // are still OK, but will see the warnings again for any other types |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
93 // that do not meet this assumption. |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
94 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
95 template <class T> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
96 struct octave_base_int_helper_traits |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
97 { |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
98 static const bool can_be_larger_than_uchar_max = true; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
99 }; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
100 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
101 template <> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
102 struct octave_base_int_helper_traits<char> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
103 { |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
104 static const bool can_be_larger_than_uchar_max = false; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
105 }; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
106 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
107 template <> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
108 struct octave_base_int_helper_traits<signed char> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
109 { |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
110 static const bool can_be_larger_than_uchar_max = false; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
111 }; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
112 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
113 template <> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
114 struct octave_base_int_helper_traits<unsigned char> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
115 { |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
116 static const bool can_be_larger_than_uchar_max = false; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
117 }; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
118 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
119 |
4903 | 120 template <class T> |
5759 | 121 octave_base_value * |
4903 | 122 octave_base_int_matrix<T>::try_narrowing_conversion (void) |
123 { | |
5759 | 124 octave_base_value *retval = 0; |
4903 | 125 |
4932 | 126 if (this->matrix.nelem () == 1) |
127 retval = new typename octave_value_int_traits<T>::scalar_type (this->matrix (0)); | |
4903 | 128 |
129 return retval; | |
130 } | |
131 | |
132 template <class T> | |
5992 | 133 octave_value |
134 octave_base_int_matrix<T>::convert_to_str_internal (bool, bool, char type) const | |
135 { | |
136 octave_value retval; | |
137 dim_vector dv = this->dims (); | |
138 octave_idx_type nel = dv.numel (); | |
139 | |
140 charNDArray chm (dv); | |
141 | |
142 bool warned = false; | |
143 | |
144 for (octave_idx_type i = 0; i < nel; i++) | |
145 { | |
146 OCTAVE_QUIT; | |
147 | |
8918
f5408862892f
Consistently use element_type in the array classes.
Jason Riedy <jason@acm.org>
parents:
8377
diff
changeset
|
148 typename T::element_type tmp = this->matrix(i); |
5992 | 149 |
8918
f5408862892f
Consistently use element_type in the array classes.
Jason Riedy <jason@acm.org>
parents:
8377
diff
changeset
|
150 typedef typename T::element_type::val_type val_type; |
7534
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
151 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
152 val_type ival = tmp.value (); |
5992 | 153 |
7534
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
154 static const bool is_signed = std::numeric_limits<val_type>::is_signed; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
155 static const bool can_be_larger_than_uchar_max |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
156 = octave_base_int_helper_traits<val_type>::can_be_larger_than_uchar_max; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
157 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
158 if (octave_base_int_helper<val_type, is_signed, |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
159 can_be_larger_than_uchar_max>::char_value_out_of_range (ival)) |
5992 | 160 { |
161 // FIXME -- is there something better we could do? | |
162 | |
163 ival = 0; | |
164 | |
165 if (! warned) | |
166 { | |
167 ::warning ("range error for conversion to character value"); | |
168 warned = true; | |
169 } | |
170 } | |
171 else | |
172 chm (i) = static_cast<char> (ival); | |
173 } | |
174 | |
175 retval = octave_value (chm, true, type); | |
176 | |
177 return retval; | |
178 } | |
179 | |
180 template <class T> | |
4903 | 181 bool |
6974 | 182 octave_base_int_matrix<T>::save_ascii (std::ostream& os) |
4903 | 183 { |
4932 | 184 dim_vector d = this->dims (); |
4903 | 185 |
186 os << "# ndims: " << d.length () << "\n"; | |
187 | |
188 for (int i = 0; i < d.length (); i++) | |
189 os << " " << d (i); | |
190 | |
4932 | 191 os << "\n" << this->matrix; |
4903 | 192 |
193 return true; | |
194 } | |
195 | |
196 template <class T> | |
197 bool | |
198 octave_base_int_matrix<T>::load_ascii (std::istream& is) | |
199 { | |
200 int mdims = 0; | |
201 bool success = true; | |
202 | |
203 if (extract_keyword (is, "ndims", mdims, true)) | |
204 { | |
205 if (mdims >= 0) | |
206 { | |
207 dim_vector dv; | |
208 dv.resize (mdims); | |
209 | |
210 for (int i = 0; i < mdims; i++) | |
211 is >> dv(i); | |
212 | |
213 T tmp(dv); | |
214 | |
215 is >> tmp; | |
216 | |
217 if (!is) | |
218 { | |
219 error ("load: failed to load matrix constant"); | |
220 success = false; | |
221 } | |
222 | |
4932 | 223 this->matrix = tmp; |
4903 | 224 } |
225 else | |
226 { | |
227 error ("load: failed to extract number of rows and columns"); | |
228 success = false; | |
229 } | |
230 } | |
231 else | |
232 error ("load: failed to extract number of dimensions"); | |
233 | |
234 return success; | |
235 } | |
236 | |
237 template <class T> | |
238 bool | |
4917 | 239 octave_base_int_matrix<T>::save_binary (std::ostream& os, bool&) |
4903 | 240 { |
4932 | 241 dim_vector d = this->dims (); |
4903 | 242 if (d.length() < 1) |
243 return false; | |
244 | |
245 // Use negative value for ndims to differentiate with old format!! | |
5828 | 246 int32_t tmp = - d.length(); |
5760 | 247 os.write (reinterpret_cast<char *> (&tmp), 4); |
4903 | 248 for (int i=0; i < d.length (); i++) |
249 { | |
250 tmp = d(i); | |
5760 | 251 os.write (reinterpret_cast<char *> (&tmp), 4); |
4903 | 252 } |
253 | |
5760 | 254 os.write (reinterpret_cast<const char *> (this->matrix.data()), this->byte_size()); |
4903 | 255 |
256 return true; | |
257 } | |
258 | |
259 template <class T> | |
260 bool | |
261 octave_base_int_matrix<T>::load_binary (std::istream& is, bool swap, | |
4917 | 262 oct_mach_info::float_format ) |
4903 | 263 { |
5828 | 264 int32_t mdims; |
5760 | 265 if (! is.read (reinterpret_cast<char *> (&mdims), 4)) |
4903 | 266 return false; |
267 if (swap) | |
4944 | 268 swap_bytes<4> (&mdims); |
4917 | 269 if (mdims >= 0) |
270 return false; | |
4903 | 271 |
4917 | 272 mdims = - mdims; |
5828 | 273 int32_t di; |
4917 | 274 dim_vector dv; |
275 dv.resize (mdims); | |
4903 | 276 |
4917 | 277 for (int i = 0; i < mdims; i++) |
4903 | 278 { |
5760 | 279 if (! is.read (reinterpret_cast<char *> (&di), 4)) |
4903 | 280 return false; |
281 if (swap) | |
4944 | 282 swap_bytes<4> (&di); |
4917 | 283 dv(i) = di; |
4903 | 284 } |
285 | |
5157 | 286 // Convert an array with a single dimension to be a row vector. |
287 // Octave should never write files like this, other software | |
288 // might. | |
289 | |
290 if (mdims == 1) | |
291 { | |
292 mdims = 2; | |
293 dv.resize (mdims); | |
294 dv(1) = dv(0); | |
295 dv(0) = 1; | |
296 } | |
297 | |
4917 | 298 T m (dv); |
299 | |
5760 | 300 if (! is.read (reinterpret_cast<char *> (m.fortran_vec ()), m.byte_size ())) |
4917 | 301 return false; |
4903 | 302 |
4917 | 303 if (swap) |
304 { | |
305 int nel = dv.numel (); | |
306 int bytes = nel / m.byte_size(); | |
307 for (int i = 0; i < nel; i++) | |
308 switch (bytes) | |
309 { | |
310 case 8: | |
4944 | 311 swap_bytes<8> (&m(i)); |
4917 | 312 break; |
313 case 4: | |
4944 | 314 swap_bytes<4> (&m(i)); |
4917 | 315 break; |
316 case 2: | |
4944 | 317 swap_bytes<2> (&m(i)); |
4917 | 318 break; |
319 case 1: | |
320 default: | |
321 break; | |
322 } | |
323 } | |
324 | |
4932 | 325 this->matrix = m; |
4903 | 326 return true; |
327 } | |
328 | |
329 #if defined (HAVE_HDF5) | |
330 | |
331 template <class T> | |
332 bool | |
4917 | 333 octave_base_int_matrix<T>::save_hdf5 (hid_t loc_id, const char *name, bool) |
4903 | 334 { |
4917 | 335 hid_t save_type_hid = HDF5_SAVE_TYPE; |
4903 | 336 bool retval = true; |
4932 | 337 dim_vector dv = this->dims (); |
4903 | 338 int empty = save_hdf5_empty (loc_id, name, dv); |
339 if (empty) | |
340 return (empty > 0); | |
341 | |
342 int rank = dv.length (); | |
343 hid_t space_hid = -1, data_hid = -1; | |
344 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); | |
345 | |
346 // Octave uses column-major, while HDF5 uses row-major ordering | |
347 for (int i = 0; i < rank; i++) | |
348 hdims[i] = dv (rank-i-1); | |
349 | |
350 space_hid = H5Screate_simple (rank, hdims, 0); | |
351 | |
352 if (space_hid < 0) return false; | |
353 | |
354 data_hid = H5Dcreate (loc_id, name, save_type_hid, space_hid, | |
355 H5P_DEFAULT); | |
356 if (data_hid < 0) | |
357 { | |
358 H5Sclose (space_hid); | |
359 return false; | |
360 } | |
361 | |
4917 | 362 retval = H5Dwrite (data_hid, save_type_hid, H5S_ALL, H5S_ALL, |
4932 | 363 H5P_DEFAULT, this->matrix.data()) >= 0; |
4903 | 364 |
365 H5Dclose (data_hid); | |
366 H5Sclose (space_hid); | |
367 | |
368 return retval; | |
369 } | |
370 | |
371 template <class T> | |
372 bool | |
373 octave_base_int_matrix<T>::load_hdf5 (hid_t loc_id, const char *name, | |
374 bool /* have_h5giterate_bug */) | |
375 { | |
4917 | 376 hid_t save_type_hid = HDF5_SAVE_TYPE; |
4903 | 377 bool retval = false; |
378 dim_vector dv; | |
379 int empty = load_hdf5_empty (loc_id, name, dv); | |
380 if (empty > 0) | |
4932 | 381 this->matrix.resize(dv); |
4903 | 382 if (empty) |
383 return (empty > 0); | |
384 | |
385 hid_t data_hid = H5Dopen (loc_id, name); | |
386 hid_t space_id = H5Dget_space (data_hid); | |
387 | |
388 hsize_t rank = H5Sget_simple_extent_ndims (space_id); | |
389 | |
390 if (rank < 1) | |
391 { | |
392 H5Sclose (space_id); | |
393 H5Dclose (data_hid); | |
394 return false; | |
395 } | |
396 | |
397 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); | |
398 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); | |
399 | |
400 H5Sget_simple_extent_dims (space_id, hdims, maxdims); | |
401 | |
402 // Octave uses column-major, while HDF5 uses row-major ordering | |
403 if (rank == 1) | |
404 { | |
405 dv.resize (2); | |
406 dv(0) = 1; | |
407 dv(1) = hdims[0]; | |
408 } | |
409 else | |
410 { | |
411 dv.resize (rank); | |
412 for (hsize_t i = 0, j = rank - 1; i < rank; i++, j--) | |
413 dv(j) = hdims[i]; | |
414 } | |
415 | |
4917 | 416 T m (dv); |
417 if (H5Dread (data_hid, save_type_hid, H5S_ALL, H5S_ALL, | |
418 H5P_DEFAULT, m.fortran_vec()) >= 0) | |
4903 | 419 { |
420 retval = true; | |
4932 | 421 this->matrix = m; |
4903 | 422 } |
423 | |
424 H5Sclose (space_id); | |
425 H5Dclose (data_hid); | |
426 | |
427 return retval; | |
428 } | |
429 | |
430 #endif | |
431 | |
432 template <class T> | |
433 void | |
434 octave_base_int_matrix<T>::print_raw (std::ostream& os, | |
435 bool pr_as_read_syntax) const | |
436 { | |
4932 | 437 octave_print_internal (os, this->matrix, pr_as_read_syntax, |
438 this->current_print_indent_level ()); | |
4903 | 439 } |
440 | |
441 template <class T> | |
5992 | 442 octave_value |
443 octave_base_int_scalar<T>::convert_to_str_internal (bool, bool, char type) const | |
444 { | |
445 octave_value retval; | |
446 | |
447 T tmp = this->scalar; | |
448 | |
7534
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
449 typedef typename T::val_type val_type; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
450 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
451 val_type ival = tmp.value (); |
5992 | 452 |
7534
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
453 static const bool is_signed = std::numeric_limits<val_type>::is_signed; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
454 static const bool can_be_larger_than_uchar_max |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
455 = octave_base_int_helper_traits<val_type>::can_be_larger_than_uchar_max; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
456 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
457 if (octave_base_int_helper<val_type, is_signed, |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
458 can_be_larger_than_uchar_max>::char_value_out_of_range (ival)) |
5992 | 459 { |
460 // FIXME -- is there something better we could do? | |
461 | |
462 ival = 0; | |
463 | |
464 ::warning ("range error for conversion to character value"); | |
465 } | |
466 else | |
467 retval = octave_value (std::string (1, static_cast<char> (ival)), type); | |
468 | |
469 return retval; | |
470 } | |
471 | |
472 template <class T> | |
4903 | 473 bool |
6974 | 474 octave_base_int_scalar<T>::save_ascii (std::ostream& os) |
4903 | 475 { |
4932 | 476 os << this->scalar << "\n"; |
4903 | 477 return true; |
478 } | |
479 | |
480 template <class T> | |
481 bool | |
482 octave_base_int_scalar<T>::load_ascii (std::istream& is) | |
483 { | |
4932 | 484 is >> this->scalar; |
4903 | 485 if (!is) |
486 { | |
487 error ("load: failed to load scalar constant"); | |
488 return false; | |
489 } | |
490 return true; | |
491 } | |
492 | |
493 template <class T> | |
494 bool | |
4917 | 495 octave_base_int_scalar<T>::save_binary (std::ostream& os, bool&) |
4903 | 496 { |
5760 | 497 os.write (reinterpret_cast<char *> (&(this->scalar)), this->byte_size()); |
4903 | 498 return true; |
499 } | |
500 | |
501 template <class T> | |
502 bool | |
503 octave_base_int_scalar<T>::load_binary (std::istream& is, bool swap, | |
4917 | 504 oct_mach_info::float_format) |
4903 | 505 { |
4917 | 506 T tmp; |
5760 | 507 if (! is.read (reinterpret_cast<char *> (&tmp), this->byte_size())) |
4903 | 508 return false; |
509 | |
4917 | 510 if (swap) |
4932 | 511 switch (this->byte_size()) |
4917 | 512 { |
513 case 8: | |
4944 | 514 swap_bytes<8> (&tmp); |
4917 | 515 break; |
516 case 4: | |
4944 | 517 swap_bytes<4> (&tmp); |
4917 | 518 break; |
519 case 2: | |
4944 | 520 swap_bytes<2> (&tmp); |
4917 | 521 break; |
522 case 1: | |
523 default: | |
524 break; | |
525 } | |
4932 | 526 this->scalar = tmp; |
4903 | 527 return true; |
528 } | |
529 | |
530 #if defined (HAVE_HDF5) | |
4944 | 531 |
4903 | 532 template <class T> |
533 bool | |
4917 | 534 octave_base_int_scalar<T>::save_hdf5 (hid_t loc_id, const char *name, bool) |
4903 | 535 { |
4917 | 536 hid_t save_type_hid = HDF5_SAVE_TYPE; |
4903 | 537 bool retval = true; |
538 hsize_t dimens[3]; | |
539 hid_t space_hid = -1, data_hid = -1; | |
540 | |
541 space_hid = H5Screate_simple (0, dimens, 0); | |
542 if (space_hid < 0) return false; | |
543 | |
4917 | 544 data_hid = H5Dcreate (loc_id, name, save_type_hid, space_hid, |
4903 | 545 H5P_DEFAULT); |
546 if (data_hid < 0) | |
547 { | |
548 H5Sclose (space_hid); | |
549 return false; | |
550 } | |
551 | |
4917 | 552 retval = H5Dwrite (data_hid, save_type_hid, H5S_ALL, H5S_ALL, |
4932 | 553 H5P_DEFAULT, &(this->scalar)) >= 0; |
4903 | 554 |
555 H5Dclose (data_hid); | |
556 H5Sclose (space_hid); | |
557 | |
558 return retval; | |
559 } | |
560 | |
561 template <class T> | |
562 bool | |
563 octave_base_int_scalar<T>::load_hdf5 (hid_t loc_id, const char *name, | |
564 bool /* have_h5giterate_bug */) | |
565 { | |
4917 | 566 hid_t save_type_hid = HDF5_SAVE_TYPE; |
4903 | 567 hid_t data_hid = H5Dopen (loc_id, name); |
568 hid_t space_id = H5Dget_space (data_hid); | |
569 | |
570 hsize_t rank = H5Sget_simple_extent_ndims (space_id); | |
571 | |
572 if (rank != 0) | |
573 { | |
574 H5Dclose (data_hid); | |
575 return false; | |
576 } | |
577 | |
4917 | 578 T tmp; |
579 if (H5Dread (data_hid, save_type_hid, H5S_ALL, H5S_ALL, | |
580 H5P_DEFAULT, &tmp) < 0) | |
4903 | 581 { |
582 H5Dclose (data_hid); | |
583 return false; | |
584 } | |
585 | |
4932 | 586 this->scalar = tmp; |
4903 | 587 |
588 H5Dclose (data_hid); | |
589 | |
590 return true; | |
591 } | |
4944 | 592 |
4903 | 593 #endif |
594 | |
595 /* | |
596 ;;; Local Variables: *** | |
597 ;;; mode: C++ *** | |
598 ;;; End: *** | |
599 */ |