Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #include <stdlib.h>
  2. #include <ctype.h>
  3.  
  4. double strtod (const char* str, char** endptr)
  5. {
  6.         double  res = 0.0;
  7.         int             pwr = 0, pwr1, esign = 1, sign = 1;
  8.  
  9.         while (isspace(*str)) str++;
  10.  
  11.         if (*str == '-') { sign = -1; str++; }
  12.                 else
  13.         if (*str == '+') str++;
  14.  
  15.  
  16.         while (isdigit(*str))
  17.         {
  18.                 res = 10 * res + (*str - '0');
  19.                 str++;
  20.         }
  21.  
  22.         if (*str =='.')
  23.         {
  24.                 str++;
  25.                 double div = 10.0;
  26.                 while (isdigit(*str))
  27.                 {
  28.                         res += (*str - '0') / div;
  29.                         str++;
  30.                         div *= 10;
  31.                 }
  32.         }
  33.  
  34.         if (*str =='e' || *str =='E')
  35.         {
  36.                 str++;
  37.                 if (*str == '-') { esign = -1; str++; }
  38.                         else
  39.                 if (*str == '+') str++;
  40.  
  41.                 while (isdigit(*str))
  42.                 {
  43.                         pwr = 10.0 * pwr + (*str - '0');
  44.                         str++;
  45.                 }
  46.  
  47.                 // fck, i've no pow() yet
  48.                 //  res = res * pow(10, pwr);
  49.                 for (pwr1 = pwr; pwr1 !=0; pwr1--)
  50.                         if (esign == 1)
  51.                                 res *= 10;
  52.                         else
  53.                             res /= 10;
  54.  
  55.         }
  56.         if (endptr)
  57.                 *endptr = (char*)str;
  58.  
  59.         return res * sign;
  60. }
  61.  
  62. long double strtold (const char* str, char** endptr)
  63. {
  64.     return (long double)strtod(str, endptr);
  65. }
  66.  
  67. float strtof (const char* str, char** endptr)
  68. {
  69.     return (float)strtod(str, endptr);
  70. }
  71.