52786fcfa2c5f8a0f779318c0e5a2c96124dc275
[reactos.git] / lib / crtdll / wchar / wcstod.c
1 /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
2 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
3 #include <msvcrt/stdlib.h>
4
5
6 double wcstod(const wchar_t* s, wchar_t** sret)
7 {
8   long double r;                /* result */
9   int e;                        /* exponent */
10   long double d;                /* scale */
11   int sign;                     /* +- 1.0 */
12   int esign;
13   int i;
14   int flags=0;
15
16   r = 0.0;
17   sign = 1;
18   e = 0;
19   esign = 1;
20
21   while ((*s == L' ') || (*s == L'\t'))
22     s++;
23
24   if (*s == L'+')
25     s++;
26   else if (*s == L'-')
27   {
28     sign = -1;
29     s++;
30   }
31
32   while ((*s >= L'0') && (*s <= L'9'))
33   {
34     flags |= 1;
35     r *= 10.0;
36     r += *s - L'0';
37     s++;
38   }
39
40   if (*s == L'.')
41   {
42     d = 0.1L;
43     s++;
44     while ((*s >= L'0') && (*s <= L'9'))
45     {
46       flags |= 2;
47       r += d * (*s - L'0');
48       s++;
49       d *= 0.1L;
50     }
51   }
52
53   if (flags == 0)
54   {
55     if (sret)
56       *sret = (wchar_t *)s;
57     return 0;
58   }
59
60   if ((*s == L'e') || (*s == L'E'))
61   {
62     s++;
63     if (*s == L'+')
64       s++;
65     else if (*s == L'-')
66     {
67       s++;
68       esign = -1;
69     }
70     if ((*s < L'0') || (*s > L'9'))
71     {
72       if (sret)
73         *sret = (wchar_t *)s;
74       return r;
75     }
76
77     while ((*s >= L'0') && (*s <= L'9'))
78     {
79       e *= 10;
80       e += *s - L'0';
81       s++;
82     }
83   }
84
85   if (esign < 0)
86     for (i = 1; i <= e; i++)
87       r *= 0.1L;
88   else
89     for (i = 1; i <= e; i++)
90       r *= 10.0;
91
92   if (sret)
93     *sret = (wchar_t *)s;
94   return r * sign;
95 }