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 |
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <climits> |
2926
|
29 #include <cstdlib> |
|
30 #include <cstdio> |
1967
|
31 |
2926
|
32 #include <string> |
|
33 |
|
34 #ifdef HAVE_UNISTD_H |
|
35 #ifdef HAVE_SYS_TYPES_H |
|
36 #include <sys/types.h> |
|
37 #endif |
|
38 #include <unistd.h> |
|
39 #endif |
|
40 |
|
41 #include "lo-error.h" |
4130
|
42 #include "lo-ieee.h" |
1967
|
43 #include "lo-mappers.h" |
|
44 #include "lo-utils.h" |
|
45 |
|
46 // Convert X to the nearest integer value. Should not pass NaN to |
|
47 // this function. |
|
48 |
|
49 int |
|
50 NINT (double x) |
|
51 { |
|
52 if (x > INT_MAX) |
|
53 return INT_MAX; |
|
54 else if (x < INT_MIN) |
|
55 return INT_MIN; |
|
56 else |
|
57 return (x > 0) ? ((int) (x + 0.5)) : ((int) (x - 0.5)); |
|
58 } |
|
59 |
|
60 double |
|
61 D_NINT (double x) |
|
62 { |
|
63 if (xisinf (x) || xisnan (x)) |
|
64 return x; |
|
65 else |
|
66 return floor (x + 0.5); |
|
67 } |
|
68 |
2926
|
69 // Save a string. |
|
70 |
|
71 char * |
|
72 strsave (const char *s) |
|
73 { |
|
74 if (! s) |
|
75 return 0; |
|
76 |
|
77 int len = strlen (s); |
|
78 char *tmp = new char [len+1]; |
|
79 tmp = strcpy (tmp, s); |
|
80 return tmp; |
|
81 } |
|
82 |
|
83 // This function was adapted from xputenv from Karl Berry's kpathsearch |
|
84 // library. |
|
85 |
|
86 // XXX FIXME XXX -- make this do the right thing if we don't have a |
|
87 // SMART_PUTENV. |
|
88 |
|
89 void |
3504
|
90 octave_putenv (const std::string& name, const std::string& value) |
2926
|
91 { |
|
92 int new_len = name.length () + value.length () + 2; |
|
93 |
|
94 char *new_item = static_cast<char*> (malloc (new_len)); |
|
95 |
|
96 sprintf (new_item, "%s=%s", name.c_str (), value.c_str ()); |
|
97 |
|
98 // As far as I can see there's no way to distinguish between the |
|
99 // various errors; putenv doesn't have errno values. |
|
100 |
|
101 if (putenv (new_item) < 0) |
|
102 (*current_liboctave_error_handler) ("putenv (%s) failed", new_item); |
|
103 } |
|
104 |
3504
|
105 std::string |
2926
|
106 octave_fgets (FILE *f) |
|
107 { |
3504
|
108 std::string retval; |
2926
|
109 |
|
110 int grow_size = 1024; |
|
111 int max_size = grow_size; |
|
112 |
|
113 char *buf = static_cast<char *> (malloc (max_size)); |
|
114 char *bufptr = buf; |
|
115 int len = 0; |
|
116 |
|
117 do |
|
118 { |
|
119 if (fgets (bufptr, grow_size, f)) |
|
120 { |
|
121 len = strlen (bufptr); |
|
122 |
|
123 if (len == grow_size - 1) |
|
124 { |
|
125 int tmp = bufptr - buf + grow_size - 1; |
|
126 grow_size *= 2; |
|
127 max_size += grow_size; |
|
128 buf = static_cast<char *> (realloc (buf, max_size)); |
|
129 bufptr = buf + tmp; |
|
130 |
|
131 if (*(bufptr-1) == '\n') |
|
132 { |
|
133 *bufptr = '\0'; |
|
134 retval = buf; |
|
135 } |
|
136 } |
|
137 else if (bufptr[len-1] != '\n') |
|
138 { |
|
139 bufptr[len++] = '\n'; |
|
140 bufptr[len] = '\0'; |
|
141 retval = buf; |
|
142 } |
|
143 else |
|
144 retval = buf; |
|
145 } |
|
146 else |
|
147 { |
|
148 if (len == 0) |
|
149 { |
|
150 free (buf); |
|
151 |
|
152 buf = 0; |
|
153 } |
|
154 |
|
155 break; |
|
156 } |
|
157 } |
|
158 while (retval.empty ()); |
|
159 |
|
160 if (buf) |
|
161 free (buf); |
|
162 |
|
163 return retval; |
|
164 } |
|
165 |
3970
|
166 std::string |
|
167 octave_fgetl (FILE *f) |
|
168 { |
|
169 std::string retval = octave_fgets (f); |
|
170 |
|
171 size_t len = retval.length (); |
|
172 |
|
173 if (retval[len-1] == '\n') |
|
174 retval.resize (len-1); |
|
175 |
|
176 return retval; |
|
177 } |
|
178 |
4130
|
179 static inline double |
|
180 read_inf_nan_na (std::istream& is, char c) |
|
181 { |
|
182 double d = 0.0; |
|
183 |
|
184 switch (c) |
|
185 { |
|
186 case 'i': case 'I': |
|
187 { |
|
188 is >> c; |
|
189 if (c == 'n' || c == 'N') |
|
190 { |
|
191 is >> c; |
|
192 if (c == 'f' || c == 'F') |
|
193 d = octave_Inf; |
|
194 else |
|
195 is.putback (c); |
|
196 } |
|
197 else |
|
198 is.putback (c); |
|
199 } |
|
200 break; |
|
201 |
|
202 case 'n': case 'N': |
|
203 { |
|
204 is >> c; |
|
205 if (c == 'a' || c == 'A') |
|
206 { |
|
207 is >> c; |
|
208 if (c == 'n' || c == 'N') |
|
209 d = octave_NaN; |
|
210 else |
|
211 { |
|
212 is.putback (c); |
|
213 d = octave_NA; |
|
214 } |
|
215 } |
|
216 else |
|
217 is.putback (c); |
|
218 } |
|
219 break; |
|
220 |
|
221 default: |
|
222 abort (); |
|
223 } |
|
224 |
|
225 return d; |
|
226 } |
|
227 |
|
228 double |
|
229 octave_read_double (std::istream& is) |
|
230 { |
|
231 double d = 0.0; |
|
232 |
|
233 char c = 0; |
|
234 |
|
235 is >> c; |
|
236 switch (c) |
|
237 { |
|
238 case 'i': case 'I': |
|
239 case 'n': case 'N': |
|
240 d = read_inf_nan_na (is, c); |
|
241 break; |
|
242 |
|
243 default: |
|
244 is.putback (c); |
|
245 is >> d; |
|
246 } |
|
247 |
|
248 return d; |
|
249 } |
|
250 |
|
251 Complex |
|
252 octave_read_complex (std::istream& is) |
|
253 { |
|
254 double re = 0.0, im = 0.0; |
|
255 |
|
256 Complex cx = 0.0; |
|
257 |
|
258 char ch = 0; |
|
259 |
|
260 is >> ch; |
|
261 |
|
262 if (ch == '(') |
|
263 { |
|
264 re = octave_read_double (is); |
|
265 is >> ch; |
|
266 |
|
267 if (ch == ',') |
|
268 { |
|
269 im = octave_read_double (is); |
|
270 is >> ch; |
|
271 |
|
272 if (ch == ')') |
|
273 cx = Complex (re, im); |
|
274 else |
|
275 is.setstate (std::ios::failbit); |
|
276 } |
|
277 else if (ch == ')') |
|
278 cx = re; |
|
279 else |
|
280 is.setstate (std::ios::failbit); |
|
281 } |
|
282 else |
|
283 { |
|
284 is.putback (ch); |
|
285 cx = octave_read_double (is); |
|
286 } |
|
287 |
|
288 return cx; |
|
289 |
|
290 } |
|
291 |
|
292 void |
|
293 octave_write_double (std::ostream& os, double d) |
|
294 { |
|
295 if (lo_ieee_is_NA (d)) |
|
296 os << "NA"; |
|
297 else if (lo_ieee_isnan (d)) |
|
298 os << "NaN"; |
|
299 else if (lo_ieee_isinf (d)) |
|
300 os << (d < 0 ? "-Inf" : "Inf"); |
|
301 else |
|
302 os << d; |
|
303 } |
|
304 |
|
305 void |
|
306 octave_write_complex (std::ostream& os, const Complex& c) |
|
307 { |
|
308 os << "("; |
|
309 octave_write_double (os, real (c)); |
|
310 os << ","; |
|
311 octave_write_double (os, imag (c)); |
|
312 os << ")"; |
|
313 } |
|
314 |
1967
|
315 /* |
|
316 ;;; Local Variables: *** |
|
317 ;;; mode: C++ *** |
|
318 ;;; End: *** |
|
319 */ |