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