home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / libg++-2.6-fsf.lha / libg++-2.6 / libiberty / strtod.c < prev    next >
C/C++ Source or Header  |  1994-01-11  |  3KB  |  118 lines

  1. /* Implementation of strtod for systems with atof.
  2.    Copyright (C) 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of the libiberty library.
  5. Libiberty is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9.  
  10. Libiberty is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with libiberty; see the file COPYING.LIB.  If
  17. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  18. Cambridge, MA 02139, USA.  */
  19.  
  20. #include <ctype.h>
  21.  
  22. extern double atof ();
  23.  
  24. /* Disclaimer: this is currently just used by CHILL in GDB and therefore
  25.    has not been tested well.  It may have been tested for nothing except
  26.    that it compiles.  */
  27.  
  28. double
  29. strtod (str, ptr)
  30.      char *str;
  31.      char **ptr;
  32. {
  33.   char *p;
  34.  
  35.   if (ptr == (char **)0)
  36.     return atof (str);
  37.   
  38.   p = str;
  39.   
  40.   while (isspace (*p))
  41.     ++p;
  42.   
  43.   if (*p == '+' || *p == '-')
  44.     ++p;
  45.  
  46.   /* INF or INFINITY.  */
  47.   if ((p[0] == 'i' || p[0] == 'I')
  48.       && (p[1] == 'n' || p[1] == 'N')
  49.       && (p[2] == 'f' || p[2] == 'F'))
  50.     {
  51.       if ((p[3] == 'i' || p[3] == 'I')
  52.       && (p[4] == 'n' || p[4] == 'N')
  53.       && (p[5] == 'i' || p[5] == 'I')
  54.       && (p[6] == 't' || p[6] == 'T')
  55.       && (p[7] == 'y' || p[7] == 'Y'))
  56.     {
  57.       *ptr = p + 7;
  58.       return atof (str);
  59.     }
  60.       else
  61.     {
  62.       *ptr = p + 3;
  63.       return atof (str);
  64.     }
  65.     }
  66.  
  67.   /* NAN or NAN(foo).  */
  68.   if ((p[0] == 'n' || p[0] == 'N')
  69.       && (p[1] == 'a' || p[1] == 'A')
  70.       && (p[2] == 'n' || p[2] == 'N'))
  71.     {
  72.       p += 3;
  73.       if (*p == '(')
  74.     {
  75.       ++p;
  76.       while (*p != '\0' && *p != ')')
  77.         ++p;
  78.       if (*p == ')')
  79.         ++p;
  80.     }
  81.       *ptr = p;
  82.       return atof (str);
  83.     }
  84.  
  85.   /* digits, with 0 or 1 periods in it.  */
  86.   if (isdigit (*p) || *p == '.')
  87.     {
  88.       int got_dot = 0;
  89.       while (isdigit (*p) || (!got_dot && *p == '.'))
  90.     {
  91.       if (*p == '.')
  92.         got_dot = 1;
  93.       ++p;
  94.     }
  95.  
  96.       /* Exponent.  */
  97.       if (*p == 'e' || *p == 'E')
  98.     {
  99.       int i;
  100.       i = 1;
  101.       if (p[i] == '+' || p[i] == '-')
  102.         ++i;
  103.       if (isdigit (p[i]))
  104.         {
  105.           while (isdigit (p[i]))
  106.         ++i;
  107.           *ptr = p + i;
  108.           return atof (str);
  109.         }
  110.     }
  111.       *ptr = p;
  112.       return atof (str);
  113.     }
  114.   /* Didn't find any digits.  Doesn't look like a number.  */
  115.   *ptr = str;
  116.   return 0.0;
  117. }
  118.