1993
|
1 // utils.cc |
1967
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 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 |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
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 |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
20 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
21 02110-1301, USA. |
1967
|
22 |
|
23 */ |
|
24 |
|
25 #ifdef HAVE_CONFIG_H |
|
26 #include <config.h> |
|
27 #endif |
|
28 |
2926
|
29 #include <cstdlib> |
|
30 #include <cstdio> |
1967
|
31 |
6490
|
32 #include <limits> |
2926
|
33 #include <string> |
|
34 |
|
35 #ifdef HAVE_UNISTD_H |
|
36 #ifdef HAVE_SYS_TYPES_H |
|
37 #include <sys/types.h> |
|
38 #endif |
|
39 #include <unistd.h> |
|
40 #endif |
|
41 |
|
42 #include "lo-error.h" |
4130
|
43 #include "lo-ieee.h" |
1967
|
44 #include "lo-mappers.h" |
|
45 #include "lo-utils.h" |
|
46 |
|
47 // Convert X to the nearest integer value. Should not pass NaN to |
|
48 // this function. |
|
49 |
5275
|
50 // Sometimes you need a large integer, but not always. |
|
51 |
|
52 octave_idx_type |
|
53 NINTbig (double x) |
|
54 { |
6490
|
55 if (x > std::numeric_limits<octave_idx_type>::max ()) |
|
56 return std::numeric_limits<octave_idx_type>::max (); |
|
57 else if (x < std::numeric_limits<octave_idx_type>::min ()) |
|
58 return std::numeric_limits<octave_idx_type>::min (); |
5275
|
59 else |
|
60 return static_cast<octave_idx_type> ((x > 0) ? (x + 0.5) : (x - 0.5)); |
|
61 } |
|
62 |
1967
|
63 int |
|
64 NINT (double x) |
|
65 { |
6490
|
66 if (x > std::numeric_limits<int>::max ()) |
|
67 return std::numeric_limits<int>::max (); |
|
68 else if (x < std::numeric_limits<int>::min ()) |
|
69 return std::numeric_limits<int>::min (); |
1967
|
70 else |
4784
|
71 return static_cast<int> ((x > 0) ? (x + 0.5) : (x - 0.5)); |
1967
|
72 } |
|
73 |
|
74 double |
|
75 D_NINT (double x) |
|
76 { |
|
77 if (xisinf (x) || xisnan (x)) |
|
78 return x; |
|
79 else |
|
80 return floor (x + 0.5); |
|
81 } |
|
82 |
2926
|
83 // Save a string. |
|
84 |
|
85 char * |
|
86 strsave (const char *s) |
|
87 { |
|
88 if (! s) |
|
89 return 0; |
|
90 |
|
91 int len = strlen (s); |
|
92 char *tmp = new char [len+1]; |
|
93 tmp = strcpy (tmp, s); |
|
94 return tmp; |
|
95 } |
|
96 |
|
97 // This function was adapted from xputenv from Karl Berry's kpathsearch |
|
98 // library. |
|
99 |
5775
|
100 // FIXME -- make this do the right thing if we don't have a |
2926
|
101 // SMART_PUTENV. |
|
102 |
|
103 void |
3504
|
104 octave_putenv (const std::string& name, const std::string& value) |
2926
|
105 { |
|
106 int new_len = name.length () + value.length () + 2; |
|
107 |
|
108 char *new_item = static_cast<char*> (malloc (new_len)); |
|
109 |
|
110 sprintf (new_item, "%s=%s", name.c_str (), value.c_str ()); |
|
111 |
|
112 // As far as I can see there's no way to distinguish between the |
|
113 // various errors; putenv doesn't have errno values. |
|
114 |
|
115 if (putenv (new_item) < 0) |
|
116 (*current_liboctave_error_handler) ("putenv (%s) failed", new_item); |
|
117 } |
|
118 |
3504
|
119 std::string |
2926
|
120 octave_fgets (FILE *f) |
|
121 { |
4527
|
122 bool eof; |
|
123 return octave_fgets (f, eof); |
|
124 } |
|
125 |
|
126 std::string |
|
127 octave_fgets (FILE *f, bool& eof) |
|
128 { |
|
129 eof = false; |
|
130 |
3504
|
131 std::string retval; |
2926
|
132 |
|
133 int grow_size = 1024; |
|
134 int max_size = grow_size; |
|
135 |
|
136 char *buf = static_cast<char *> (malloc (max_size)); |
|
137 char *bufptr = buf; |
|
138 int len = 0; |
|
139 |
|
140 do |
|
141 { |
|
142 if (fgets (bufptr, grow_size, f)) |
|
143 { |
|
144 len = strlen (bufptr); |
|
145 |
|
146 if (len == grow_size - 1) |
|
147 { |
|
148 int tmp = bufptr - buf + grow_size - 1; |
|
149 grow_size *= 2; |
|
150 max_size += grow_size; |
|
151 buf = static_cast<char *> (realloc (buf, max_size)); |
|
152 bufptr = buf + tmp; |
|
153 |
|
154 if (*(bufptr-1) == '\n') |
|
155 { |
|
156 *bufptr = '\0'; |
|
157 retval = buf; |
|
158 } |
|
159 } |
|
160 else if (bufptr[len-1] != '\n') |
|
161 { |
|
162 bufptr[len++] = '\n'; |
|
163 bufptr[len] = '\0'; |
|
164 retval = buf; |
|
165 } |
|
166 else |
|
167 retval = buf; |
|
168 } |
|
169 else |
|
170 { |
|
171 if (len == 0) |
|
172 { |
4527
|
173 eof = true; |
|
174 |
2926
|
175 free (buf); |
|
176 |
|
177 buf = 0; |
|
178 } |
|
179 |
|
180 break; |
|
181 } |
|
182 } |
|
183 while (retval.empty ()); |
|
184 |
|
185 if (buf) |
|
186 free (buf); |
|
187 |
|
188 return retval; |
|
189 } |
|
190 |
3970
|
191 std::string |
|
192 octave_fgetl (FILE *f) |
|
193 { |
4527
|
194 bool eof; |
|
195 return octave_fgetl (f, eof); |
|
196 } |
|
197 |
|
198 std::string |
|
199 octave_fgetl (FILE *f, bool& eof) |
|
200 { |
|
201 std::string retval = octave_fgets (f, eof); |
3970
|
202 |
|
203 size_t len = retval.length (); |
|
204 |
|
205 if (retval[len-1] == '\n') |
|
206 retval.resize (len-1); |
|
207 |
|
208 return retval; |
|
209 } |
|
210 |
4130
|
211 static inline double |
6194
|
212 read_inf_nan_na (std::istream& is, char c, char sign = '+') |
4130
|
213 { |
|
214 double d = 0.0; |
|
215 |
|
216 switch (c) |
|
217 { |
|
218 case 'i': case 'I': |
|
219 { |
6897
|
220 c = is.get (); |
4130
|
221 if (c == 'n' || c == 'N') |
|
222 { |
6897
|
223 c = is.get (); |
4130
|
224 if (c == 'f' || c == 'F') |
6194
|
225 d = sign == '-' ? -octave_Inf : octave_Inf; |
4130
|
226 else |
|
227 is.putback (c); |
|
228 } |
|
229 else |
|
230 is.putback (c); |
|
231 } |
|
232 break; |
|
233 |
|
234 case 'n': case 'N': |
|
235 { |
6897
|
236 c = is.get (); |
4130
|
237 if (c == 'a' || c == 'A') |
|
238 { |
6897
|
239 c = is.get (); |
4130
|
240 if (c == 'n' || c == 'N') |
|
241 d = octave_NaN; |
|
242 else |
|
243 { |
|
244 is.putback (c); |
|
245 d = octave_NA; |
|
246 } |
|
247 } |
|
248 else |
|
249 is.putback (c); |
|
250 } |
|
251 break; |
|
252 |
|
253 default: |
|
254 abort (); |
|
255 } |
|
256 |
|
257 return d; |
|
258 } |
|
259 |
|
260 double |
|
261 octave_read_double (std::istream& is) |
|
262 { |
|
263 double d = 0.0; |
|
264 |
6194
|
265 char c1 = 0; |
4130
|
266 |
6897
|
267 c1 = is.get (); |
6194
|
268 switch (c1) |
4130
|
269 { |
6194
|
270 case '-': |
|
271 { |
|
272 char c2 = 0; |
6897
|
273 c2 = is.get (); |
6194
|
274 if (c2 == 'i' || c2 == 'I') |
|
275 d = read_inf_nan_na (is, c2, c1); |
|
276 else |
|
277 { |
|
278 is.putback (c2); |
|
279 is.putback (c1); |
6202
|
280 is >> d; |
6194
|
281 } |
|
282 } |
|
283 break; |
|
284 |
|
285 case '+': |
|
286 { |
|
287 char c2 = 0; |
6897
|
288 c2 = is.get (); |
6194
|
289 if (c2 == 'i' || c2 == 'I') |
|
290 d = read_inf_nan_na (is, c2, c1); |
|
291 else |
|
292 { |
|
293 is.putback (c2); |
|
294 is.putback (c1); |
6202
|
295 is >> d; |
6194
|
296 } |
|
297 } |
|
298 break; |
|
299 |
4130
|
300 case 'i': case 'I': |
|
301 case 'n': case 'N': |
6194
|
302 d = read_inf_nan_na (is, c1); |
4130
|
303 break; |
|
304 |
|
305 default: |
6194
|
306 is.putback (c1); |
4130
|
307 is >> d; |
|
308 } |
|
309 |
|
310 return d; |
|
311 } |
|
312 |
|
313 Complex |
|
314 octave_read_complex (std::istream& is) |
|
315 { |
|
316 double re = 0.0, im = 0.0; |
|
317 |
|
318 Complex cx = 0.0; |
|
319 |
|
320 char ch = 0; |
|
321 |
6897
|
322 ch = is.get (); |
4130
|
323 |
|
324 if (ch == '(') |
|
325 { |
|
326 re = octave_read_double (is); |
6897
|
327 ch = is.get (); |
4130
|
328 |
|
329 if (ch == ',') |
|
330 { |
|
331 im = octave_read_double (is); |
6897
|
332 ch = is.get (); |
4130
|
333 |
|
334 if (ch == ')') |
|
335 cx = Complex (re, im); |
|
336 else |
|
337 is.setstate (std::ios::failbit); |
|
338 } |
|
339 else if (ch == ')') |
|
340 cx = re; |
|
341 else |
|
342 is.setstate (std::ios::failbit); |
|
343 } |
|
344 else |
|
345 { |
|
346 is.putback (ch); |
|
347 cx = octave_read_double (is); |
|
348 } |
|
349 |
|
350 return cx; |
|
351 |
|
352 } |
|
353 |
|
354 void |
|
355 octave_write_double (std::ostream& os, double d) |
|
356 { |
|
357 if (lo_ieee_is_NA (d)) |
|
358 os << "NA"; |
|
359 else if (lo_ieee_isnan (d)) |
|
360 os << "NaN"; |
|
361 else if (lo_ieee_isinf (d)) |
|
362 os << (d < 0 ? "-Inf" : "Inf"); |
|
363 else |
|
364 os << d; |
|
365 } |
|
366 |
|
367 void |
|
368 octave_write_complex (std::ostream& os, const Complex& c) |
|
369 { |
|
370 os << "("; |
|
371 octave_write_double (os, real (c)); |
|
372 os << ","; |
|
373 octave_write_double (os, imag (c)); |
|
374 os << ")"; |
|
375 } |
|
376 |
1967
|
377 /* |
|
378 ;;; Local Variables: *** |
|
379 ;;; mode: C++ *** |
|
380 ;;; End: *** |
|
381 */ |