home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / pdc / libsrc / stringlib / strtod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-07  |  2.2 KB  |  85 lines

  1. /*
  2.  * Libraries and headers for PDC release 3.3 (C) 1989 Lionel Hummel.
  3.  * PDC Software Distribution (C) 1989 Lionel Hummel and Paul Petersen.
  4.  *
  5.  * This code is freely redistributable upon the conditions that this 
  6.  * notice remains intact and that modified versions of this file not
  7.  * be included as part of the PDC Software Distribution without the
  8.  * express consent of the copyright holders.  No warrantee of any
  9.  * kind is provided with this code.  For further information, contact:
  10.  *
  11.  *  PDC Software Distribution    Internet:                     BIX:
  12.  *  P.O. Box 4006             or hummel@cs.uiuc.edu            lhummel
  13.  *  Urbana, IL  61801-8801       petersen@uicsrd.csrd.uiuc.edu
  14.  */
  15.  
  16. /* strtod.c - converts ascii to a double */
  17.  
  18. #include <stddef.h>
  19. #include <ctype.h>
  20.  
  21. extern double strtosd(char *, char **, double);
  22. extern double strtosud(char *, char **, double);
  23.  
  24. double strtod(string, ptr)
  25. char  *string;
  26. char **ptr;
  27. {
  28.     double retval = 0.0;
  29.     double sign = 1.0;
  30.     char *strptr = string;
  31.     char *scan_index = string;
  32.     int  exp = 0;
  33.     int  valid = 0;              /* Advances when a value is established */
  34.  
  35.     retval = strtosd(strptr, &scan_index, 10.0);
  36.     if (strptr != scan_index) {
  37.         valid++;
  38.         strptr = scan_index;
  39.         if (retval < 0.0) {
  40.             sign = -1.0;
  41.             retval *= -1.0;
  42.             }
  43.         }
  44.     else {
  45.         while (isspace(*strptr))
  46.             strptr++;
  47.         if (*strptr == '-') {
  48.             sign = -1.0;
  49.             strptr++;
  50.             }
  51.         }
  52.  
  53.     if (*strptr == '.')  {
  54.         strptr++;
  55.         retval += strtosud(strptr, &scan_index, .1);
  56.         if (strptr != scan_index) {
  57.             valid++;
  58.             strptr = scan_index;
  59.             }
  60.         }
  61.  
  62.     if ((valid > 0) && (tolower(*strptr) == 'e'))  {
  63.         strptr++;
  64.         exp = strtol(strptr, &scan_index, 10);
  65.         strptr = scan_index;
  66.         if (exp > 0)  {
  67.             while (exp-- > 0)
  68.                 retval *= 10.0;
  69.             }
  70.         else if (exp < 0)  {
  71.             while (exp++ < 0)
  72.                 retval /= 10.0;
  73.             }
  74.         }
  75.  
  76.     if (ptr != NULL) {
  77.         if (valid > 0)
  78.             *ptr = strptr;
  79.         else
  80.             *ptr = string;
  81.         }
  82.  
  83.     return(retval * sign);
  84. }
  85.