Windows NT 4.0 source code leak
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.3 KiB

4 years ago
  1. /***
  2. *strtod.c - convert string to floating point number
  3. *
  4. * Copyright (c) 1985-1992, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Convert character string to floating point number
  8. *
  9. *Revision History:
  10. * 09-09-83 RKW Module created
  11. * 08-19-85 TDC changed to strtod
  12. * 04-13-87 JCR Added "const" to declaration
  13. * 04-20-87 BCM Added checks for negative overflow and for underflow
  14. * 11-09-87 BCM different interface under ifdef MTHREAD
  15. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  16. * 02-22-88 JCR Added cast to nptr to get rid of cl const warning
  17. * 05-24-88 PHG Merged DLL and normal versions
  18. * 08-24-88 PHG No digits found => *endptr = nptr [ANSI]
  19. * Revised test order so invalid detection works always
  20. * 10-20-88 JCR Changed 'DOUBLE' to 'double' for 386
  21. * 11-20-89 JCR atof() is always _cdecl in 386 (not pascal)
  22. * 03-05-90 GJF Fixed calling type, added #include <cruntime.h>,
  23. * removed #include <register.h>, removed redundant
  24. * prototypes, removed some leftover 16-bit support and
  25. * fixed the copyright. Also, cleaned up the formatting
  26. * a bit.
  27. * 07-23-90 SBM Compiles cleanly with -W3 (added/removed appropriate
  28. * #includes)
  29. * 08-01-90 SBM Renamed <struct.h> to <fltintrn.h>
  30. * 09-27-90 GJF New-style function declarators.
  31. * 10-21-92 GJF Made char-to-int conversion unsigned.
  32. *
  33. *******************************************************************************/
  34. #include <cruntime.h>
  35. #include <stdlib.h>
  36. #include <fltintrn.h>
  37. #include <string.h>
  38. #include <ctype.h>
  39. #include <errno.h>
  40. #include <math.h>
  41. /***
  42. *double strtod(nptr, endptr) - convert string to double
  43. *
  44. *Purpose:
  45. * strtod recognizes an optional string of tabs and spaces,
  46. * then an optional sign, then a string of digits optionally
  47. * containing a decimal point, then an optional e or E followed
  48. * by an optionally signed integer, and converts all this to
  49. * to a floating point number. The first unrecognized
  50. * character ends the string, and is pointed to by endptr.
  51. *
  52. *Entry:
  53. * nptr - pointer to string to convert
  54. *
  55. *Exit:
  56. * returns value of character string
  57. * char **endptr - if not NULL, points to character which stopped
  58. * the scan
  59. *
  60. *Exceptions:
  61. *
  62. *******************************************************************************/
  63. double _CRTAPI1 strtod (
  64. const char *nptr,
  65. REG2 char **endptr
  66. )
  67. {
  68. #ifdef MTHREAD
  69. struct _flt answerstruct;
  70. #endif
  71. FLT answer;
  72. double tmp;
  73. unsigned int flags;
  74. REG1 char *ptr = (char *) nptr;
  75. /* scan past leading space/tab characters */
  76. while ( isspace((int)(unsigned char)*ptr) )
  77. ptr++;
  78. /* let _fltin routine do the rest of the work */
  79. #ifdef MTHREAD
  80. /* ok to take address of stack variable here; fltin2 knows to use ss */
  81. answer = _fltin2( &answerstruct, ptr, strlen(ptr), 0, 0);
  82. #else
  83. answer = _fltin(ptr, strlen(ptr), 0, 0);
  84. #endif
  85. if ( endptr != NULL )
  86. *endptr = (char *) ptr + answer->nbytes;
  87. flags = answer->flags;
  88. if ( flags & (512 | 64)) {
  89. /* no digits found or invalid format:
  90. ANSI says return 0.0, and *endptr = nptr */
  91. tmp = 0.0;
  92. if ( endptr != NULL )
  93. *endptr = (char *) nptr;
  94. }
  95. else if ( flags & (128 | 1) ) {
  96. if ( *ptr == '-' )
  97. tmp = -HUGE_VAL; /* negative overflow */
  98. else
  99. tmp = HUGE_VAL; /* positive overflow */
  100. errno = ERANGE;
  101. }
  102. else if ( flags & 256 ) {
  103. tmp = 0.0; /* underflow */
  104. errno = ERANGE;
  105. }
  106. else
  107. tmp = answer->dval;
  108. return(tmp);
  109. }