2376
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2376
|
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 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
2376
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
3503
|
31 #include <iostream> |
2901
|
32 |
4944
|
33 #include "data-conv.h" |
|
34 #include "mach-info.h" |
|
35 |
2376
|
36 #include "defun.h" |
|
37 #include "gripes.h" |
|
38 #include "oct-obj.h" |
4944
|
39 #include "oct-stream.h" |
2376
|
40 #include "ov-scalar.h" |
3223
|
41 #include "ov-base.h" |
|
42 #include "ov-base-scalar.h" |
|
43 #include "ov-base-scalar.cc" |
2423
|
44 #include "ov-re-mat.h" |
2376
|
45 #include "ov-typeinfo.h" |
|
46 #include "pr-output.h" |
|
47 #include "xdiv.h" |
|
48 #include "xpow.h" |
|
49 |
4687
|
50 #include "ls-oct-ascii.h" |
|
51 #include "ls-hdf5.h" |
|
52 |
3223
|
53 template class octave_base_scalar<double>; |
|
54 |
3219
|
55 DEFINE_OCTAVE_ALLOCATOR (octave_scalar); |
2376
|
56 |
4612
|
57 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_scalar, "scalar", "double"); |
2376
|
58 |
|
59 octave_value |
3933
|
60 octave_scalar::do_index_op (const octave_value_list& idx, int resize_ok) |
2376
|
61 { |
|
62 octave_value retval; |
|
63 |
3933
|
64 if (idx.valid_scalar_indices ()) |
2376
|
65 retval = scalar; |
|
66 else |
|
67 { |
|
68 // XXX FIXME XXX -- this doesn't solve the problem of |
|
69 // |
|
70 // a = 1; a([1,1], [1,1], [1,1]) |
|
71 // |
|
72 // and similar constructions. Hmm... |
|
73 |
2423
|
74 // XXX FIXME XXX -- using this constructor avoids narrowing the |
|
75 // 1x1 matrix back to a scalar value. Need a better solution |
|
76 // to this problem. |
|
77 |
|
78 octave_value tmp (new octave_matrix (matrix_value ())); |
2376
|
79 |
3933
|
80 retval = tmp.do_index_op (idx, resize_ok); |
2376
|
81 } |
|
82 |
|
83 return retval; |
|
84 } |
|
85 |
4645
|
86 std::streamoff |
|
87 octave_scalar::streamoff_value (void) const |
|
88 { |
|
89 std::streamoff retval (-1); |
|
90 |
|
91 if (D_NINT (scalar) == scalar) |
|
92 retval = std::streamoff (static_cast<long> (scalar)); |
|
93 else |
|
94 error ("conversion to streamoff value failed"); |
|
95 |
|
96 return retval; |
|
97 } |
|
98 |
4701
|
99 streamoff_array |
|
100 octave_scalar::streamoff_array_value (void) const |
|
101 { |
|
102 streamoff_array retval; |
|
103 |
|
104 std::streamoff soff = streamoff_value (); |
|
105 |
|
106 if (! error_state) |
|
107 retval = streamoff_array (dim_vector (1, 1), soff); |
|
108 |
|
109 return retval; |
|
110 } |
|
111 |
2376
|
112 octave_value |
4457
|
113 octave_scalar::convert_to_str_internal (bool, bool) const |
2376
|
114 { |
|
115 octave_value retval; |
|
116 |
|
117 if (xisnan (scalar)) |
|
118 ::error ("invalid conversion from NaN to character"); |
|
119 else |
|
120 { |
4100
|
121 int ival = NINT (scalar); |
|
122 |
|
123 if (ival < 0 || ival > UCHAR_MAX) |
|
124 { |
|
125 // XXX FIXME XXX -- is there something better we could do? |
|
126 |
|
127 ival = 0; |
|
128 |
|
129 ::warning ("range error for conversion to character value"); |
|
130 } |
|
131 |
|
132 retval = octave_value (std::string (1, static_cast<char> (ival))); |
2376
|
133 } |
|
134 |
|
135 return retval; |
|
136 } |
|
137 |
4687
|
138 bool |
|
139 octave_scalar::save_ascii (std::ostream& os, bool& infnan_warned, |
|
140 bool strip_nan_and_inf) |
|
141 { |
|
142 double d = double_value (); |
|
143 |
|
144 if (strip_nan_and_inf) |
|
145 { |
|
146 if (xisnan (d)) |
|
147 { |
|
148 error ("only value to plot is NaN"); |
|
149 return false; |
|
150 } |
|
151 else |
|
152 { |
|
153 d = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
|
154 octave_write_double (os, d); |
|
155 os << "\n"; |
|
156 } |
|
157 } |
|
158 else |
|
159 { |
|
160 if (! infnan_warned && (xisnan (d) || xisinf (d))) |
|
161 { |
|
162 warning ("save: Inf or NaN values may not be reloadable"); |
|
163 infnan_warned = true; |
|
164 } |
|
165 |
|
166 octave_write_double (os, d); |
|
167 os << "\n"; |
|
168 } |
|
169 |
|
170 return true; |
|
171 } |
|
172 |
|
173 bool |
|
174 octave_scalar::load_ascii (std::istream& is) |
|
175 { |
|
176 scalar = octave_read_double (is); |
|
177 if (!is) |
|
178 { |
|
179 error ("load: failed to load scalar constant"); |
|
180 return false; |
|
181 } |
|
182 |
|
183 return true; |
|
184 } |
|
185 |
|
186 bool |
|
187 octave_scalar::save_binary (std::ostream& os, bool& /* save_as_floats */) |
|
188 { |
|
189 char tmp = (char) LS_DOUBLE; |
|
190 os.write (X_CAST (char *, &tmp), 1); |
|
191 double dtmp = double_value (); |
|
192 os.write (X_CAST (char *, &dtmp), 8); |
|
193 |
|
194 return true; |
|
195 } |
|
196 |
|
197 bool |
|
198 octave_scalar::load_binary (std::istream& is, bool swap, |
4815
|
199 oct_mach_info::float_format fmt) |
4687
|
200 { |
|
201 char tmp; |
|
202 if (! is.read (X_CAST (char *, &tmp), 1)) |
|
203 return false; |
|
204 |
|
205 double dtmp; |
|
206 read_doubles (is, &dtmp, X_CAST (save_type, tmp), 1, swap, fmt); |
|
207 if (error_state || ! is) |
|
208 return false; |
|
209 |
|
210 scalar = dtmp; |
|
211 return true; |
|
212 } |
|
213 |
|
214 #if defined (HAVE_HDF5) |
4944
|
215 |
4687
|
216 bool |
|
217 octave_scalar::save_hdf5 (hid_t loc_id, const char *name, |
|
218 bool /* save_as_floats */) |
|
219 { |
|
220 hsize_t dimens[3]; |
|
221 hid_t space_hid = -1, data_hid = -1; |
|
222 bool retval = true; |
|
223 |
4815
|
224 space_hid = H5Screate_simple (0, dimens, 0); |
4687
|
225 if (space_hid < 0) return false; |
|
226 |
|
227 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid, |
|
228 H5P_DEFAULT); |
|
229 if (data_hid < 0) |
|
230 { |
|
231 H5Sclose (space_hid); |
|
232 return false; |
|
233 } |
|
234 |
|
235 double tmp = double_value (); |
|
236 retval = H5Dwrite (data_hid, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, |
4815
|
237 H5P_DEFAULT, &tmp) >= 0; |
4687
|
238 |
|
239 H5Dclose (data_hid); |
|
240 H5Sclose (space_hid); |
4837
|
241 |
4687
|
242 return retval; |
|
243 } |
|
244 |
|
245 bool |
|
246 octave_scalar::load_hdf5 (hid_t loc_id, const char *name, |
|
247 bool /* have_h5giterate_bug */) |
|
248 { |
|
249 hid_t data_hid = H5Dopen (loc_id, name); |
|
250 hid_t space_id = H5Dget_space (data_hid); |
|
251 |
|
252 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
253 |
|
254 if (rank != 0) |
|
255 { |
|
256 H5Dclose (data_hid); |
|
257 return false; |
|
258 } |
|
259 |
|
260 double dtmp; |
|
261 if (H5Dread (data_hid, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, |
4815
|
262 H5P_DEFAULT, &dtmp) < 0) |
4687
|
263 { |
|
264 H5Dclose (data_hid); |
|
265 return false; |
|
266 } |
|
267 |
|
268 scalar = dtmp; |
4837
|
269 |
4687
|
270 H5Dclose (data_hid); |
4837
|
271 |
4687
|
272 return true; |
|
273 } |
4944
|
274 |
4687
|
275 #endif |
|
276 |
2376
|
277 /* |
|
278 ;;; Local Variables: *** |
|
279 ;;; mode: C++ *** |
|
280 ;;; End: *** |
|
281 */ |