GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
lo-utils.cc
Go to the documentation of this file.
1 // utils.cc
2 /*
3 
4 Copyright (C) 1996-2013 John W. Eaton
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 3 of the License, or (at your
11 option) any 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, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include <cctype>
29 #include <cstdlib>
30 #include <cstdio>
31 #include <cstring>
32 #include <cfloat>
33 
34 #include <limits>
35 #include <string>
36 
37 #include <sys/types.h>
38 #include <unistd.h>
39 
40 #include "quit.h"
41 
42 #include "lo-error.h"
43 #include "lo-ieee.h"
44 #include "lo-mappers.h"
45 #include "lo-utils.h"
46 
47 bool xis_int_or_inf_or_nan (double x)
48 { return xisnan (x) || D_NINT (x) == x; }
49 
50 bool xis_one_or_zero (double x)
51 { return x == 0 || x == 1; }
52 
53 bool xis_zero (double x)
54 { return x == 0; }
55 
56 bool xtoo_large_for_float (double x)
57 {
58  return (xfinite (x) && fabs (x) > std::numeric_limits<float>::max ());
59 }
60 
62 {
63  return (xtoo_large_for_float (x.real ())
64  || xtoo_large_for_float (x.imag ()));
65 }
66 
68 { return xisnan (x) || D_NINT (x) == x; }
69 
70 bool xis_one_or_zero (float x)
71 { return x == 0 || x == 1; }
72 
73 bool xis_zero (float x)
74 { return x == 0; }
75 
76 // Save a string.
77 
78 char *
79 strsave (const char *s)
80 {
81  if (! s)
82  return 0;
83 
84  int len = strlen (s);
85  char *tmp = new char [len+1];
86  tmp = strcpy (tmp, s);
87  return tmp;
88 }
89 
90 // This function was adapted from xputenv from Karl Berry's kpathsearch
91 // library.
92 
93 // FIXME: make this do the right thing if we don't have a SMART_PUTENV.
94 
95 void
96 octave_putenv (const std::string& name, const std::string& value)
97 {
98  int new_len = name.length () + value.length () + 2;
99 
100  char *new_item = static_cast<char*> (gnulib::malloc (new_len));
101 
102  sprintf (new_item, "%s=%s", name.c_str (), value.c_str ());
103 
104  // As far as I can see there's no way to distinguish between the
105  // various errors; putenv doesn't have errno values.
106 
107  if (gnulib::putenv (new_item) < 0)
108  (*current_liboctave_error_handler) ("putenv (%s) failed", new_item);
109 }
110 
111 std::string
113 {
114  bool eof;
115  return octave_fgets (f, eof);
116 }
117 
118 std::string
119 octave_fgets (FILE *f, bool& eof)
120 {
121  eof = false;
122 
123  std::string retval;
124 
125  int grow_size = 1024;
126  int max_size = grow_size;
127 
128  char *buf = static_cast<char *> (gnulib::malloc (max_size));
129  char *bufptr = buf;
130  int len = 0;
131 
132  do
133  {
134  if (gnulib::fgets (bufptr, grow_size, f))
135  {
136  len = strlen (bufptr);
137 
138  if (len == grow_size - 1)
139  {
140  int tmp = bufptr - buf + grow_size - 1;
141  grow_size *= 2;
142  max_size += grow_size;
143  buf = static_cast<char *> (gnulib::realloc (buf, max_size));
144  bufptr = buf + tmp;
145 
146  if (*(bufptr-1) == '\n')
147  {
148  *bufptr = '\0';
149  retval = buf;
150  }
151  }
152  else if (bufptr[len-1] != '\n')
153  {
154  bufptr[len++] = '\n';
155  bufptr[len] = '\0';
156  retval = buf;
157  }
158  else
159  retval = buf;
160  }
161  else
162  {
163  if (len == 0)
164  {
165  eof = true;
166 
167  free (buf);
168 
169  buf = 0;
170  }
171 
172  break;
173  }
174  }
175  while (retval.empty ());
176 
177  if (buf)
178  free (buf);
179 
180  octave_quit ();
181 
182  return retval;
183 }
184 
185 std::string
187 {
188  bool eof;
189  return octave_fgetl (f, eof);
190 }
191 
192 std::string
193 octave_fgetl (FILE *f, bool& eof)
194 {
195  std::string retval = octave_fgets (f, eof);
196 
197  size_t len = retval.length ();
198 
199  if (retval[len-1] == '\n')
200  retval.resize (len-1);
201 
202  return retval;
203 }
204 
205 // Note that the caller is responsible for repositioning the stream on failure.
206 
207 template <typename T>
208 T
209 read_inf_nan_na (std::istream& is, char c0)
210 {
211  T val = 0.0;
212 
213  switch (c0)
214  {
215  case 'i': case 'I':
216  {
217  char c1 = is.get ();
218  if (c1 == 'n' || c1 == 'N')
219  {
220  char c2 = is.get ();
221  if (c2 == 'f' || c2 == 'F')
222  val = std::numeric_limits<T>::infinity ();
223  else
224  is.setstate (std::ios::failbit);
225  }
226  else
227  is.setstate (std::ios::failbit);
228  }
229  break;
230 
231  case 'n': case 'N':
232  {
233  char c1 = is.get ();
234  if (c1 == 'a' || c1 == 'A')
235  {
236  char c2 = is.get ();
237  if (c2 == 'n' || c2 == 'N')
238  val = std::numeric_limits<T>::quiet_NaN ();
239  else
240  {
241  val = octave_numeric_limits<T>::NA ();
242  is.putback (c2);
243  }
244  }
245  else
246  is.setstate (std::ios::failbit);
247  }
248  break;
249 
250  default:
251  abort ();
252  }
253 
254  return val;
255 }
256 
257 // Read a double value. Discard any sign on NaN and NA.
258 
259 template <typename T>
260 double
261 octave_read_fp_value (std::istream& is)
262 {
263  T val = 0.0;
264 
265  // FIXME: resetting stream position is likely to fail unless we are
266  // reading from a file.
267  std::ios::streampos pos = is.tellg ();
268 
269  char c1 = ' ';
270 
271  while (isspace (c1))
272  c1 = is.get ();
273 
274  bool neg = false;
275 
276  switch (c1)
277  {
278  case '-':
279  neg = true;
280  // fall through...
281 
282  case '+':
283  {
284  char c2 = 0;
285  c2 = is.get ();
286  if (c2 == 'i' || c2 == 'I' || c2 == 'n' || c2 == 'N')
287  val = read_inf_nan_na<T> (is, c2);
288  else
289  {
290  is.putback (c2);
291  is >> val;
292  }
293 
294  if (neg && ! is.fail ())
295  val = -val;
296  }
297  break;
298 
299  case 'i': case 'I':
300  case 'n': case 'N':
301  val = read_inf_nan_na<T> (is, c1);
302  break;
303 
304  default:
305  is.putback (c1);
306  is >> val;
307  break;
308  }
309 
310  std::ios::iostate status = is.rdstate ();
311  if (status & std::ios::failbit)
312  {
313  is.clear ();
314  is.seekg (pos);
315  is.setstate (status);
316  }
317 
318  return val;
319 }
320 
321 template <typename T>
322 std::complex<T>
323 octave_read_cx_fp_value (std::istream& is)
324 {
325  T re = 0.0, im = 0.0;
326 
327  std::complex<T> cx = 0.0;
328 
329  char ch = ' ';
330 
331  while (isspace (ch))
332  ch = is.get ();
333 
334  if (ch == '(')
335  {
336  re = octave_read_value<T> (is);
337  ch = is.get ();
338 
339  if (ch == ',')
340  {
341  im = octave_read_value<T> (is);
342  ch = is.get ();
343 
344  if (ch == ')')
345  cx = std::complex<T> (re, im);
346  else
347  is.setstate (std::ios::failbit);
348  }
349  else if (ch == ')')
350  cx = re;
351  else
352  is.setstate (std::ios::failbit);
353  }
354  else
355  {
356  is.putback (ch);
357  cx = octave_read_value<double> (is);
358  }
359 
360  return cx;
361 }
362 
363 template <> OCTAVE_API double octave_read_value (std::istream& is)
364 {
365  return octave_read_fp_value<double> (is);
366 }
367 
368 template <> OCTAVE_API Complex octave_read_value (std::istream& is)
369 {
370  return octave_read_cx_fp_value<double> (is);
371 }
372 
373 template <> OCTAVE_API float octave_read_value (std::istream& is)
374 {
375  return octave_read_fp_value<float> (is);
376 }
377 
378 template <> OCTAVE_API FloatComplex octave_read_value (std::istream& is)
379 {
380  return octave_read_cx_fp_value<float> (is);
381 }
382 
383 void
384 octave_write_double (std::ostream& os, double d)
385 {
386  if (lo_ieee_is_NA (d))
387  os << "NA";
388  else if (lo_ieee_isnan (d))
389  os << "NaN";
390  else if (lo_ieee_isinf (d))
391  os << (d < 0 ? "-Inf" : "Inf");
392  else
393  os << d;
394 }
395 
396 void
397 octave_write_complex (std::ostream& os, const Complex& c)
398 {
399  os << "(";
400  octave_write_double (os, real (c));
401  os << ",";
402  octave_write_double (os, imag (c));
403  os << ")";
404 }
405 
406 void
407 octave_write_float (std::ostream& os, float d)
408 {
409  if (lo_ieee_is_NA (d))
410  os << "NA";
411  else if (lo_ieee_isnan (d))
412  os << "NaN";
413  else if (lo_ieee_isinf (d))
414  os << (d < 0 ? "-Inf" : "Inf");
415  else
416  os << d;
417 }
418 
419 void
420 octave_write_float_complex (std::ostream& os, const FloatComplex& c)
421 {
422  os << "(";
423  octave_write_float (os, real (c));
424  os << ",";
425  octave_write_float (os, imag (c));
426  os << ")";
427 }