home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / strtod.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  3KB  |  98 lines

  1. /***
  2. *strtod.c - convert string to floating point number
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Convert character string to floating point number
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <stdlib.h>
  13. #include <fltintrn.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include <errno.h>
  17. #include <math.h>
  18.  
  19. /***
  20. *double strtod(nptr, endptr) - convert string to double
  21. *
  22. *Purpose:
  23. *       strtod recognizes an optional string of tabs and spaces,
  24. *       then an optional sign, then a string of digits optionally
  25. *       containing a decimal point, then an optional e or E followed
  26. *       by an optionally signed integer, and converts all this to
  27. *       to a floating point number.  The first unrecognized
  28. *       character ends the string, and is pointed to by endptr.
  29. *
  30. *Entry:
  31. *       nptr - pointer to string to convert
  32. *
  33. *Exit:
  34. *       returns value of character string
  35. *       char **endptr - if not NULL, points to character which stopped
  36. *                       the scan
  37. *
  38. *Exceptions:
  39. *
  40. *******************************************************************************/
  41.  
  42. double __cdecl strtod (
  43.         const char *nptr,
  44.         REG2 char **endptr
  45.         )
  46. {
  47.  
  48. #ifdef _MT
  49.         struct _flt answerstruct;
  50. #endif  /* _MT */
  51.  
  52.         FLT      answer;
  53.         double       tmp;
  54.         unsigned int flags;
  55.         REG1 char *ptr = (char *) nptr;
  56.  
  57.         /* scan past leading space/tab characters */
  58.  
  59.         while ( isspace((int)(unsigned char)*ptr) )
  60.                 ptr++;
  61.  
  62.         /* let _fltin routine do the rest of the work */
  63.  
  64. #ifdef _MT
  65.         /* ok to take address of stack variable here; fltin2 knows to use ss */
  66.         answer = _fltin2( &answerstruct, ptr, strlen(ptr), 0, 0);
  67. #else  /* _MT */
  68.         answer = _fltin(ptr, strlen(ptr), 0, 0);
  69. #endif  /* _MT */
  70.  
  71.         if ( endptr != NULL )
  72.                 *endptr = (char *) ptr + answer->nbytes;
  73.  
  74.         flags = answer->flags;
  75.         if ( flags & (512 | 64)) {
  76.                 /* no digits found or invalid format:
  77.                    ANSI says return 0.0, and *endptr = nptr */
  78.                 tmp = 0.0;
  79.                 if ( endptr != NULL )
  80.                         *endptr = (char *) nptr;
  81.         }
  82.         else if ( flags & (128 | 1) ) {
  83.                 if ( *ptr == '-' )
  84.                         tmp = -HUGE_VAL;        /* negative overflow */
  85.                 else
  86.                         tmp = HUGE_VAL;         /* positive overflow */
  87.                 errno = ERANGE;
  88.         }
  89.         else if ( flags & 256 ) {
  90.                 tmp = 0.0;                      /* underflow */
  91.                 errno = ERANGE;
  92.         }
  93.         else
  94.                 tmp = answer->dval;
  95.  
  96.         return(tmp);
  97. }
  98.