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

  1. /***
  2. *wcstod.c - convert wide char 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. #include <dbgint.h>
  19.  
  20. /***
  21. *double wcstod(nptr, endptr) - convert wide string to double
  22. *
  23. *Purpose:
  24. *       wcstod recognizes an optional string of tabs and spaces,
  25. *       then an optional sign, then a string of digits optionally
  26. *       containing a decimal point, then an optional e or E followed
  27. *       by an optionally signed integer, and converts all this to
  28. *       to a floating point number.  The first unrecognized
  29. *       character ends the string, and is pointed to by endptr.
  30. *
  31. *Entry:
  32. *       nptr - pointer to wide string to convert
  33. *
  34. *Exit:
  35. *       returns value of wide character string
  36. *       wchar_t **endptr - if not NULL, points to character which stopped
  37. *               the scan
  38. *
  39. *Exceptions:
  40. *
  41. *******************************************************************************/
  42.  
  43. double __cdecl wcstod (
  44.         const wchar_t *nptr,
  45.         REG2 wchar_t **endptr
  46.         )
  47. {
  48.  
  49. #ifdef _MT
  50.         struct _flt answerstruct;
  51. #endif  /* _MT */
  52.  
  53.         FLT  answer;
  54.         double       tmp;
  55.         unsigned int flags;
  56.         REG1 wchar_t *ptr = (wchar_t *) nptr;
  57.         char * cptr;
  58.         int retval, len;
  59.         int clen = 0;
  60.  
  61.         /* scan past leading space/tab characters */
  62.  
  63.         while (iswspace(*ptr))
  64.             ptr++;
  65.  
  66.         cptr = (char *)_malloc_crt((wcslen(ptr)+1) * sizeof(wchar_t));
  67.         // UNDONE: check for errors
  68.         for (len = 0; ptr[len]; len++)
  69.             {
  70.             if ((retval = wctomb(cptr+len,ptr[len]))<=0)
  71.             break;
  72.             clen += retval;
  73.             }
  74.         cptr[clen++] = '\0';
  75.  
  76.         /* let _fltin routine do the rest of the work */
  77.  
  78. #ifdef _MT
  79.         /* ok to take address of stack variable here; fltin2 knows to use ss */
  80.         answer = _fltin2( &answerstruct, cptr, clen, 0, 0);
  81. #else  /* _MT */
  82.         answer = _fltin(cptr, clen, 0, 0);
  83. #endif  /* _MT */
  84.  
  85.         _free_crt(cptr);
  86.  
  87.         if ( endptr != NULL )
  88.             *endptr = (wchar_t *) ptr + answer->nbytes;
  89.             /* UNDONE: assumes no multi-byte chars in string */
  90.  
  91.         flags = answer->flags;
  92.         if ( flags & (512 | 64)) {
  93.             /* no digits found or invalid format:
  94.                ANSI says return 0.0, and *endptr = nptr */
  95.             tmp = 0.0;
  96.             if ( endptr != NULL )
  97.                 *endptr = (wchar_t *) nptr;
  98.         }
  99.         else if ( flags & (128 | 1) ) {
  100.             if ( *ptr == '-' )
  101.                 tmp = -HUGE_VAL;    /* negative overflow */
  102.             else
  103.                 tmp = HUGE_VAL;     /* positive overflow */
  104.             errno = ERANGE;
  105.         }
  106.         else if ( flags & 256 ) {
  107.             tmp = 0.0;          /* underflow */
  108.             errno = ERANGE;
  109.         }
  110.         else
  111.             tmp = answer->dval;
  112.  
  113.         return(tmp);
  114. }
  115.