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