Mercurial > octave-nkf
annotate liboctave/lo-utils.cc @ 16088:b29b10fbb744 stable release-3-6-4
Version 3.6.4 released.
* configure.ac (AC_INIT): Version is now 3.6.4.
(OCTAVE_RELEASE_DATE): Now 2013-02-21.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 21 Feb 2013 15:17:54 -0500 |
parents | 980e2d5c83f7 |
children | 52cb71787cd1 |
rev | line source |
---|---|
1993 | 1 // utils.cc |
1967 | 2 /* |
3 | |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
12995
diff
changeset
|
4 Copyright (C) 1996-2012 John W. Eaton |
1967 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
1967 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
1967 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
6907 | 28 #include <cctype> |
10463
bbe99b2a5ba7
undo recent gnulib-related changes
John W. Eaton <jwe@octave.org>
parents:
10447
diff
changeset
|
29 #include <cstdlib> |
2926 | 30 #include <cstdio> |
7048 | 31 #include <cstring> |
11010
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
32 #include <cfloat> |
1967 | 33 |
6490 | 34 #include <limits> |
2926 | 35 #include <string> |
36 | |
37 #include <sys/types.h> | |
38 #include <unistd.h> | |
39 | |
10068
ca93f583573d
handle interrupts octave_fgets
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
40 #include "quit.h" |
ca93f583573d
handle interrupts octave_fgets
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
41 |
2926 | 42 #include "lo-error.h" |
4130 | 43 #include "lo-ieee.h" |
1967 | 44 #include "lo-mappers.h" |
45 #include "lo-utils.h" | |
46 | |
11010
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
47 bool xis_int_or_inf_or_nan (double x) |
11013
63f79f798a14
fix small typos in new tests
Jaroslav Hajek <highegg@gmail.com>
parents:
11010
diff
changeset
|
48 { return xisnan (x) || D_NINT (x) == x; } |
11010
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
49 |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
50 bool xis_one_or_zero (double x) |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
51 { return x == 0 || x == 1; } |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
52 |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
53 bool xis_zero (double x) |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
54 { return x == 0; } |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
55 |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
56 bool xtoo_large_for_float (double x) |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
57 { return (! (xisnan (x) || xisinf (x)) && fabs (x) > FLT_MAX); } |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
58 |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
59 bool xis_int_or_inf_or_nan (float x) |
11013
63f79f798a14
fix small typos in new tests
Jaroslav Hajek <highegg@gmail.com>
parents:
11010
diff
changeset
|
60 { return xisnan (x) || D_NINT (x) == x; } |
11010
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
61 |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
62 bool xis_one_or_zero (float x) |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
63 { return x == 0 || x == 1; } |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
64 |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
65 bool xis_zero (float x) |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
66 { return x == 0; } |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
67 |
2926 | 68 // Save a string. |
69 | |
70 char * | |
71 strsave (const char *s) | |
72 { | |
73 if (! s) | |
74 return 0; | |
75 | |
76 int len = strlen (s); | |
77 char *tmp = new char [len+1]; | |
78 tmp = strcpy (tmp, s); | |
79 return tmp; | |
80 } | |
81 | |
82 // This function was adapted from xputenv from Karl Berry's kpathsearch | |
83 // library. | |
84 | |
5775 | 85 // FIXME -- make this do the right thing if we don't have a |
2926 | 86 // SMART_PUTENV. |
87 | |
88 void | |
3504 | 89 octave_putenv (const std::string& name, const std::string& value) |
2926 | 90 { |
91 int new_len = name.length () + value.length () + 2; | |
92 | |
10411 | 93 char *new_item = static_cast<char*> (gnulib::malloc (new_len)); |
2926 | 94 |
95 sprintf (new_item, "%s=%s", name.c_str (), value.c_str ()); | |
96 | |
97 // As far as I can see there's no way to distinguish between the | |
98 // various errors; putenv doesn't have errno values. | |
99 | |
100 if (putenv (new_item) < 0) | |
101 (*current_liboctave_error_handler) ("putenv (%s) failed", new_item); | |
102 } | |
103 | |
3504 | 104 std::string |
2926 | 105 octave_fgets (FILE *f) |
106 { | |
4527 | 107 bool eof; |
108 return octave_fgets (f, eof); | |
109 } | |
110 | |
111 std::string | |
112 octave_fgets (FILE *f, bool& eof) | |
113 { | |
114 eof = false; | |
115 | |
3504 | 116 std::string retval; |
2926 | 117 |
118 int grow_size = 1024; | |
119 int max_size = grow_size; | |
120 | |
10411 | 121 char *buf = static_cast<char *> (gnulib::malloc (max_size)); |
2926 | 122 char *bufptr = buf; |
123 int len = 0; | |
124 | |
125 do | |
126 { | |
12918
f3a8d1efe2c1
use gnulib:: qualifiers for more stdio functions
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
127 if (gnulib::fgets (bufptr, grow_size, f)) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
128 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
129 len = strlen (bufptr); |
2926 | 130 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
131 if (len == grow_size - 1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
132 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
133 int tmp = bufptr - buf + grow_size - 1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
134 grow_size *= 2; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
135 max_size += grow_size; |
10411 | 136 buf = static_cast<char *> (gnulib::realloc (buf, max_size)); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
137 bufptr = buf + tmp; |
2926 | 138 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
139 if (*(bufptr-1) == '\n') |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
140 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
141 *bufptr = '\0'; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
142 retval = buf; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
143 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
144 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
145 else if (bufptr[len-1] != '\n') |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
146 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
147 bufptr[len++] = '\n'; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
148 bufptr[len] = '\0'; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
149 retval = buf; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
150 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
151 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
152 retval = buf; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
153 } |
2926 | 154 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
155 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
156 if (len == 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
157 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
158 eof = true; |
4527 | 159 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
160 free (buf); |
2926 | 161 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
162 buf = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
163 } |
2926 | 164 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
165 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
166 } |
2926 | 167 } |
168 while (retval.empty ()); | |
169 | |
170 if (buf) | |
171 free (buf); | |
172 | |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
10068
diff
changeset
|
173 octave_quit (); |
10068
ca93f583573d
handle interrupts octave_fgets
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
174 |
2926 | 175 return retval; |
176 } | |
177 | |
3970 | 178 std::string |
179 octave_fgetl (FILE *f) | |
180 { | |
4527 | 181 bool eof; |
182 return octave_fgetl (f, eof); | |
183 } | |
184 | |
185 std::string | |
186 octave_fgetl (FILE *f, bool& eof) | |
187 { | |
188 std::string retval = octave_fgets (f, eof); | |
3970 | 189 |
190 size_t len = retval.length (); | |
191 | |
192 if (retval[len-1] == '\n') | |
193 retval.resize (len-1); | |
194 | |
195 return retval; | |
196 } | |
197 | |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
198 // Note that the caller is responsible for repositioning the stream on |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
199 // failure. |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
200 |
4130 | 201 static inline double |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
202 read_inf_nan_na (std::istream& is, char c0) |
4130 | 203 { |
204 double d = 0.0; | |
205 | |
12995
7872afb42fab
fix scanf problem with reading I (bug #33722)
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
206 switch (c0) |
4130 | 207 { |
208 case 'i': case 'I': | |
209 { | |
12995
7872afb42fab
fix scanf problem with reading I (bug #33722)
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
210 char c1 = is.get (); |
7872afb42fab
fix scanf problem with reading I (bug #33722)
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
211 if (c1 == 'n' || c1 == 'N') |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
212 { |
12995
7872afb42fab
fix scanf problem with reading I (bug #33722)
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
213 char c2 = is.get (); |
7872afb42fab
fix scanf problem with reading I (bug #33722)
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
214 if (c2 == 'f' || c2 == 'F') |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
215 d = octave_Inf; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
216 else |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
217 is.setstate (std::ios::failbit); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
218 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
219 else |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
220 is.setstate (std::ios::failbit); |
4130 | 221 } |
222 break; | |
223 | |
224 case 'n': case 'N': | |
225 { | |
12995
7872afb42fab
fix scanf problem with reading I (bug #33722)
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
226 char c1 = is.get (); |
7872afb42fab
fix scanf problem with reading I (bug #33722)
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
227 if (c1 == 'a' || c1 == 'A') |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
228 { |
12995
7872afb42fab
fix scanf problem with reading I (bug #33722)
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
229 char c2 = is.get (); |
7872afb42fab
fix scanf problem with reading I (bug #33722)
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
230 if (c2 == 'n' || c2 == 'N') |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
231 d = octave_NaN; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
232 else |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
233 d = octave_NA; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
234 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
235 else |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
236 is.setstate (std::ios::failbit); |
4130 | 237 } |
238 break; | |
239 | |
240 default: | |
241 abort (); | |
242 } | |
243 | |
244 return d; | |
245 } | |
246 | |
12936
b74cb659e757
accept but discard sign when reading NA and NaN values
John W. Eaton <jwe@octave.org>
parents:
12918
diff
changeset
|
247 // Read a double value. Discard any sign on NaN and NA. |
b74cb659e757
accept but discard sign when reading NA and NaN values
John W. Eaton <jwe@octave.org>
parents:
12918
diff
changeset
|
248 |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
249 template <> |
4130 | 250 double |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
251 octave_read_value (std::istream& is) |
4130 | 252 { |
253 double d = 0.0; | |
254 | |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
255 // FIXME -- resetting stream position is likely to fail unless we are |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
256 // reading from a file. |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
257 std::ios::streampos pos = is.tellg (); |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
258 |
6907 | 259 char c1 = ' '; |
4130 | 260 |
6907 | 261 while (isspace (c1)) |
262 c1 = is.get (); | |
263 | |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
264 bool neg = false; |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
265 |
6194 | 266 switch (c1) |
4130 | 267 { |
6194 | 268 case '-': |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
269 neg = true; |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
270 // fall through... |
6194 | 271 |
272 case '+': | |
273 { | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
274 char c2 = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
275 c2 = is.get (); |
12936
b74cb659e757
accept but discard sign when reading NA and NaN values
John W. Eaton <jwe@octave.org>
parents:
12918
diff
changeset
|
276 if (c2 == 'i' || c2 == 'I' || c2 == 'n' || c2 == 'N') |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
277 d = read_inf_nan_na (is, c2); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
278 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
279 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
280 is.putback (c2); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
281 is >> d; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
282 } |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
283 |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
284 if (neg && ! is.fail ()) |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
285 d = -d; |
6194 | 286 } |
287 break; | |
288 | |
4130 | 289 case 'i': case 'I': |
290 case 'n': case 'N': | |
6194 | 291 d = read_inf_nan_na (is, c1); |
4130 | 292 break; |
293 | |
294 default: | |
6194 | 295 is.putback (c1); |
4130 | 296 is >> d; |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
297 break; |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
298 } |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
299 |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
300 std::ios::iostate status = is.rdstate (); |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
301 if (status & std::ios::failbit) |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
302 { |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
303 is.clear (); |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
304 is.seekg (pos); |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
305 is.setstate (status); |
4130 | 306 } |
307 | |
308 return d; | |
309 } | |
310 | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
311 template <> |
4130 | 312 Complex |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
313 octave_read_value (std::istream& is) |
4130 | 314 { |
315 double re = 0.0, im = 0.0; | |
316 | |
317 Complex cx = 0.0; | |
318 | |
6907 | 319 char ch = ' '; |
4130 | 320 |
6907 | 321 while (isspace (ch)) |
322 ch = is.get (); | |
4130 | 323 |
324 if (ch == '(') | |
325 { | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
326 re = octave_read_value<double> (is); |
6897 | 327 ch = is.get (); |
4130 | 328 |
329 if (ch == ',') | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
330 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
331 im = octave_read_value<double> (is); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
332 ch = is.get (); |
4130 | 333 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
334 if (ch == ')') |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
335 cx = Complex (re, im); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
336 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
337 is.setstate (std::ios::failbit); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
338 } |
4130 | 339 else if (ch == ')') |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
340 cx = re; |
4130 | 341 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
342 is.setstate (std::ios::failbit); |
4130 | 343 } |
344 else | |
345 { | |
346 is.putback (ch); | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
347 cx = octave_read_value<double> (is); |
4130 | 348 } |
349 | |
350 return cx; | |
351 | |
352 } | |
353 | |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
354 // Note that the caller is responsible for repositioning the stream on |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
355 // failure. |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
356 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
357 static inline float |
12995
7872afb42fab
fix scanf problem with reading I (bug #33722)
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
358 read_float_inf_nan_na (std::istream& is, char c0, char sign = '+') |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
359 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
360 float d = 0.0; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
361 |
12995
7872afb42fab
fix scanf problem with reading I (bug #33722)
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
362 switch (c0) |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
363 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
364 case 'i': case 'I': |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
365 { |
12995
7872afb42fab
fix scanf problem with reading I (bug #33722)
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
366 char c1 = is.get (); |
7872afb42fab
fix scanf problem with reading I (bug #33722)
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
367 if (c1 == 'n' || c1 == 'N') |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
368 { |
12995
7872afb42fab
fix scanf problem with reading I (bug #33722)
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
369 char c2 = is.get (); |
7872afb42fab
fix scanf problem with reading I (bug #33722)
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
370 if (c2 == 'f' || c2 == 'F') |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
371 d = octave_Float_Inf; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
372 else |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
373 is.setstate (std::ios::failbit); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
374 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
375 else |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
376 is.setstate (std::ios::failbit); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
377 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
378 break; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
379 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
380 case 'n': case 'N': |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
381 { |
12995
7872afb42fab
fix scanf problem with reading I (bug #33722)
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
382 char c1 = is.get (); |
7872afb42fab
fix scanf problem with reading I (bug #33722)
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
383 if (c1 == 'a' || c1 == 'A') |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
384 { |
12995
7872afb42fab
fix scanf problem with reading I (bug #33722)
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
385 char c2 = is.get (); |
7872afb42fab
fix scanf problem with reading I (bug #33722)
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
386 if (c2 == 'n' || c2 == 'N') |
7872afb42fab
fix scanf problem with reading I (bug #33722)
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
387 d = octave_Float_NaN; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
388 else |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
389 d = octave_Float_NA; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
390 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
391 else |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
392 is.setstate (std::ios::failbit); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
393 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
394 break; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
395 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
396 default: |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
397 abort (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
398 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
399 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
400 return d; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
401 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
402 |
12936
b74cb659e757
accept but discard sign when reading NA and NaN values
John W. Eaton <jwe@octave.org>
parents:
12918
diff
changeset
|
403 // Read a float value. Discard any sign on NaN and NA. |
b74cb659e757
accept but discard sign when reading NA and NaN values
John W. Eaton <jwe@octave.org>
parents:
12918
diff
changeset
|
404 |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
405 template <> |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
406 float |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
407 octave_read_value (std::istream& is) |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
408 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
409 float d = 0.0; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
410 |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
411 // FIXME -- resetting stream position is likely to fail unless we are |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
412 // reading from a file. |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
413 std::ios::streampos pos = is.tellg (); |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
414 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
415 char c1 = ' '; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
416 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
417 while (isspace (c1)) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
418 c1 = is.get (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
419 |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
420 bool neg = false; |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
421 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
422 switch (c1) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
423 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
424 case '-': |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
425 neg = true; |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
426 // fall through... |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
427 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
428 case '+': |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
429 { |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
430 char c2 = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
431 c2 = is.get (); |
12936
b74cb659e757
accept but discard sign when reading NA and NaN values
John W. Eaton <jwe@octave.org>
parents:
12918
diff
changeset
|
432 if (c2 == 'i' || c2 == 'I' || c2 == 'n' || c2 == 'N') |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
433 d = read_float_inf_nan_na (is, c2); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
434 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
435 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
436 is.putback (c2); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
437 is >> d; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
438 } |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
439 |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
440 if (neg && ! is.fail ()) |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
441 d = -d; |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
442 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
443 break; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
444 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
445 case 'i': case 'I': |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
446 case 'n': case 'N': |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
447 d = read_float_inf_nan_na (is, c1); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
448 break; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
449 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
450 default: |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
451 is.putback (c1); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
452 is >> d; |
14806
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
453 break; |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
454 } |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
455 |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
456 std::ios::iostate status = is.rdstate (); |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
457 if (status & std::ios::failbit) |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
458 { |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
459 is.clear (); |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
460 is.seekg (pos); |
980e2d5c83f7
avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
461 is.setstate (status); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
462 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
463 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
464 return d; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
465 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
466 |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
467 template <> |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
468 FloatComplex |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
469 octave_read_value (std::istream& is) |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
470 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
471 float re = 0.0, im = 0.0; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
472 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
473 FloatComplex cx = 0.0; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
474 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
475 char ch = ' '; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
476 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
477 while (isspace (ch)) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
478 ch = is.get (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
479 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
480 if (ch == '(') |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
481 { |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
482 re = octave_read_value<float> (is); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
483 ch = is.get (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
484 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
485 if (ch == ',') |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
486 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
487 im = octave_read_value<float> (is); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
488 ch = is.get (); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
489 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
490 if (ch == ')') |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
491 cx = FloatComplex (re, im); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
492 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
493 is.setstate (std::ios::failbit); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
494 } |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
495 else if (ch == ')') |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
496 cx = re; |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
497 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
498 is.setstate (std::ios::failbit); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
499 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
500 else |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
501 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
502 is.putback (ch); |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
503 cx = octave_read_value<float> (is); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
504 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
505 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
506 return cx; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
507 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
508 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
509 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
510 void |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
511 octave_write_double (std::ostream& os, double d) |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
512 { |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
513 if (lo_ieee_is_NA (d)) |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
514 os << "NA"; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
515 else if (lo_ieee_isnan (d)) |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
516 os << "NaN"; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
517 else if (lo_ieee_isinf (d)) |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
518 os << (d < 0 ? "-Inf" : "Inf"); |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
519 else |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
520 os << d; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
521 } |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
522 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
523 void |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
524 octave_write_complex (std::ostream& os, const Complex& c) |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
525 { |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
526 os << "("; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
527 octave_write_double (os, real (c)); |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
528 os << ","; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
529 octave_write_double (os, imag (c)); |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
530 os << ")"; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
531 } |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
532 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
533 void |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
534 octave_write_float (std::ostream& os, float d) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
535 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
536 if (lo_ieee_is_NA (d)) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
537 os << "NA"; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
538 else if (lo_ieee_isnan (d)) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
539 os << "NaN"; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
540 else if (lo_ieee_isinf (d)) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
541 os << (d < 0 ? "-Inf" : "Inf"); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
542 else |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
543 os << d; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
544 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
545 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
546 void |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
547 octave_write_float_complex (std::ostream& os, const FloatComplex& c) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
548 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
549 os << "("; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
550 octave_write_float (os, real (c)); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
551 os << ","; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
552 octave_write_float (os, imag (c)); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
553 os << ")"; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
554 } |