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 |
4784
|
57 return static_cast<int> ((x > 0) ? (x + 0.5) : (x - 0.5)); |
1967
|
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 { |
4527
|
108 bool eof; |
|
109 return octave_fgets (f, eof); |
|
110 } |
|
111 |
|
112 std::string |
|
113 octave_fgets (FILE *f, bool& eof) |
|
114 { |
|
115 eof = false; |
|
116 |
3504
|
117 std::string retval; |
2926
|
118 |
|
119 int grow_size = 1024; |
|
120 int max_size = grow_size; |
|
121 |
|
122 char *buf = static_cast<char *> (malloc (max_size)); |
|
123 char *bufptr = buf; |
|
124 int len = 0; |
|
125 |
|
126 do |
|
127 { |
|
128 if (fgets (bufptr, grow_size, f)) |
|
129 { |
|
130 len = strlen (bufptr); |
|
131 |
|
132 if (len == grow_size - 1) |
|
133 { |
|
134 int tmp = bufptr - buf + grow_size - 1; |
|
135 grow_size *= 2; |
|
136 max_size += grow_size; |
|
137 buf = static_cast<char *> (realloc (buf, max_size)); |
|
138 bufptr = buf + tmp; |
|
139 |
|
140 if (*(bufptr-1) == '\n') |
|
141 { |
|
142 *bufptr = '\0'; |
|
143 retval = buf; |
|
144 } |
|
145 } |
|
146 else if (bufptr[len-1] != '\n') |
|
147 { |
|
148 bufptr[len++] = '\n'; |
|
149 bufptr[len] = '\0'; |
|
150 retval = buf; |
|
151 } |
|
152 else |
|
153 retval = buf; |
|
154 } |
|
155 else |
|
156 { |
|
157 if (len == 0) |
|
158 { |
4527
|
159 eof = true; |
|
160 |
2926
|
161 free (buf); |
|
162 |
|
163 buf = 0; |
|
164 } |
|
165 |
|
166 break; |
|
167 } |
|
168 } |
|
169 while (retval.empty ()); |
|
170 |
|
171 if (buf) |
|
172 free (buf); |
|
173 |
|
174 return retval; |
|
175 } |
|
176 |
3970
|
177 std::string |
|
178 octave_fgetl (FILE *f) |
|
179 { |
4527
|
180 bool eof; |
|
181 return octave_fgetl (f, eof); |
|
182 } |
|
183 |
|
184 std::string |
|
185 octave_fgetl (FILE *f, bool& eof) |
|
186 { |
|
187 std::string retval = octave_fgets (f, eof); |
3970
|
188 |
|
189 size_t len = retval.length (); |
|
190 |
|
191 if (retval[len-1] == '\n') |
|
192 retval.resize (len-1); |
|
193 |
|
194 return retval; |
|
195 } |
|
196 |
4130
|
197 static inline double |
|
198 read_inf_nan_na (std::istream& is, char c) |
|
199 { |
|
200 double d = 0.0; |
|
201 |
|
202 switch (c) |
|
203 { |
|
204 case 'i': case 'I': |
|
205 { |
|
206 is >> c; |
|
207 if (c == 'n' || c == 'N') |
|
208 { |
|
209 is >> c; |
|
210 if (c == 'f' || c == 'F') |
|
211 d = octave_Inf; |
|
212 else |
|
213 is.putback (c); |
|
214 } |
|
215 else |
|
216 is.putback (c); |
|
217 } |
|
218 break; |
|
219 |
|
220 case 'n': case 'N': |
|
221 { |
|
222 is >> c; |
|
223 if (c == 'a' || c == 'A') |
|
224 { |
|
225 is >> c; |
|
226 if (c == 'n' || c == 'N') |
|
227 d = octave_NaN; |
|
228 else |
|
229 { |
|
230 is.putback (c); |
|
231 d = octave_NA; |
|
232 } |
|
233 } |
|
234 else |
|
235 is.putback (c); |
|
236 } |
|
237 break; |
|
238 |
|
239 default: |
|
240 abort (); |
|
241 } |
|
242 |
|
243 return d; |
|
244 } |
|
245 |
|
246 double |
|
247 octave_read_double (std::istream& is) |
|
248 { |
|
249 double d = 0.0; |
|
250 |
|
251 char c = 0; |
|
252 |
|
253 is >> c; |
|
254 switch (c) |
|
255 { |
|
256 case 'i': case 'I': |
|
257 case 'n': case 'N': |
|
258 d = read_inf_nan_na (is, c); |
|
259 break; |
|
260 |
|
261 default: |
|
262 is.putback (c); |
|
263 is >> d; |
|
264 } |
|
265 |
|
266 return d; |
|
267 } |
|
268 |
|
269 Complex |
|
270 octave_read_complex (std::istream& is) |
|
271 { |
|
272 double re = 0.0, im = 0.0; |
|
273 |
|
274 Complex cx = 0.0; |
|
275 |
|
276 char ch = 0; |
|
277 |
|
278 is >> ch; |
|
279 |
|
280 if (ch == '(') |
|
281 { |
|
282 re = octave_read_double (is); |
|
283 is >> ch; |
|
284 |
|
285 if (ch == ',') |
|
286 { |
|
287 im = octave_read_double (is); |
|
288 is >> ch; |
|
289 |
|
290 if (ch == ')') |
|
291 cx = Complex (re, im); |
|
292 else |
|
293 is.setstate (std::ios::failbit); |
|
294 } |
|
295 else if (ch == ')') |
|
296 cx = re; |
|
297 else |
|
298 is.setstate (std::ios::failbit); |
|
299 } |
|
300 else |
|
301 { |
|
302 is.putback (ch); |
|
303 cx = octave_read_double (is); |
|
304 } |
|
305 |
|
306 return cx; |
|
307 |
|
308 } |
|
309 |
|
310 void |
|
311 octave_write_double (std::ostream& os, double d) |
|
312 { |
|
313 if (lo_ieee_is_NA (d)) |
|
314 os << "NA"; |
|
315 else if (lo_ieee_isnan (d)) |
|
316 os << "NaN"; |
|
317 else if (lo_ieee_isinf (d)) |
|
318 os << (d < 0 ? "-Inf" : "Inf"); |
|
319 else |
|
320 os << d; |
|
321 } |
|
322 |
|
323 void |
|
324 octave_write_complex (std::ostream& os, const Complex& c) |
|
325 { |
|
326 os << "("; |
|
327 octave_write_double (os, real (c)); |
|
328 os << ","; |
|
329 octave_write_double (os, imag (c)); |
|
330 os << ")"; |
|
331 } |
|
332 |
1967
|
333 /* |
|
334 ;;; Local Variables: *** |
|
335 ;;; mode: C++ *** |
|
336 ;;; End: *** |
|
337 */ |