home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / MATH.ZIP / STRTOD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  4.1 KB  |  148 lines

  1. /*------------------------------------------------------------------------
  2.  * filename - strtod.c
  3.  *
  4.  * function(s)
  5.  *        strtod - converts string to a double value
  6.  *       Get    - gets a character from a string
  7.  *      UnGet  - ungets a character from a string
  8.  *-----------------------------------------------------------------------*/
  9.  
  10. /*[]------------------------------------------------------------[]*/
  11. /*|                                                              |*/
  12. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  13. /*|                                                              |*/
  14. /*|                                                              |*/
  15. /*|     Copyright (c) 1987, 1990 by Borland International        |*/
  16. /*|     All Rights Reserved.                                     |*/
  17. /*|                                                              |*/
  18. /*[]------------------------------------------------------------[]*/
  19.  
  20.  
  21. #include <errno.h>
  22. #include <_scanf.h>
  23. #include <stddef.h>
  24.  
  25. #include <_math.h>
  26. #include <stdlib.h>
  27. #include <math.h>
  28. #include <float.h>
  29.  
  30. /*
  31.   Internal RTL function to perform double/float truncations.
  32. */
  33. #define    FLT    0
  34. #define    DBL    1
  35. double near pascal __ldtrunc(int flag, long double x, double xhuge);
  36.  
  37.  
  38. /*---------------------------------------------------------------------*
  39.  
  40. Name        Get - gets a character from a string
  41.  
  42. Usage        int Get (char **strPP)
  43.  
  44. Description    returns the next character in a string
  45.  
  46. Return value    the next character in a string.  if that character is
  47.         the null character, Get returns -1.
  48.  
  49. *---------------------------------------------------------------------*/
  50. static  int  near  Get (char **strPP)
  51. {
  52. register    unsigned    c;
  53.  
  54.     return  ((c = *((*strPP) ++)) == 0) ? -1 : c;
  55. }
  56.  
  57.  
  58. /*---------------------------------------------------------------------*
  59.  
  60. Name        UnGet - ungets a character from a string
  61.  
  62. Usage        void UnGet (char c, char **strPP)
  63.  
  64. Description    decrements a string pointer
  65.  
  66. Return value    Nothing
  67.  
  68. *---------------------------------------------------------------------*/
  69. static  void  near  UnGet (char c, char **strPP)
  70. {
  71.     --(*strPP);         /* ignore c, we don't allow the string to change */
  72. #pragma    warn -par
  73. }
  74. #pragma    warn .par
  75.  
  76.  
  77. /*--------------------------------------------------------------------------*
  78.  
  79. Name        strtod - converts string to a double value
  80.  
  81. Usage        double strtod(const char *strP, char **suffixPP);
  82.  
  83. Prototype in    stdlib.h
  84.  
  85. Description    Convert a string to a  double precision real. The syntax of
  86.         the string must be:
  87.  
  88.         float     ::= [isspace]* [sign] [realnum] [exponent]
  89.  
  90.         isspace  ::= as per <ctype.h>:isspace
  91.  
  92.         realnum  ::= {digit [digit]* ['.' [digit]* ]} |
  93.                  {'.' digit [digit]*}
  94.  
  95.         exponent ::= 'e'|'E' [sign] digit [digit]*
  96.  
  97.         "strP" is a pointer to the ASCII string to be scanned.
  98.  
  99.         "suffixPP" is a pointer to  a string pointer to be updated.
  100.         If it is not NULL then (*suffixPP) will be updated to point
  101.         to the first character following  the section of the string
  102.         which was consumed.
  103.  
  104.         The digits must be decimal.
  105.  
  106. Return value    strtod returns the converted value of the input string.
  107.         If the source string is  not a valid floating point numeral
  108.         then the  result value is   zero and the  next char pointer
  109.         will equal  the starting string  pointer. If the  number is
  110.         too large or too tiny then the result is signed HUGE_VAL or
  111.         zero, and errno is set to ERANGE.
  112.  
  113. *--------------------------------------------------------------------------*/
  114. double  strtod (const char *strP, char **suffixPP)
  115. {
  116.     int     charCt = 0;
  117.     int     status;
  118.     long double  result;
  119.  
  120.     result = _scantod (
  121. #if    I_declared_it_right
  122.             Get,
  123.             UnGet,
  124. #else
  125.             (int near (*) (void *))Get,
  126.             (void near (*) (int ch, void *))UnGet,
  127. #endif
  128.             &strP,
  129.             0x7FFF,
  130.             &charCt,
  131.             &status
  132.             );
  133.  
  134.     if (status <= 0)
  135.         strP -= charCt;
  136.     else if (status == 2)
  137.         errno = ERANGE;
  138.  
  139.     if (suffixPP != NULL)
  140.         *suffixPP = (char *)strP;
  141.  
  142.     /*
  143.       __ldtrunc sets 'errno' to ERANGE if the result
  144.       is to become 0 or HUGE_VAL.
  145.     */
  146.     return __ldtrunc(DBL, result, HUGE_VAL);
  147. }
  148.