:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / lib / msvcrt / stdlib / wcstoul.c
1 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
2 #include <limits.h>
3 #include <msvcrt/ctype.h>
4 #include <msvcrt/errno.h>
5 #include <msvcrt/stdlib.h>
6
7 /*
8  * Convert a unicode string to an unsigned long integer.
9  *
10  * Ignores `locale' stuff.  Assumes that the upper and lower case
11  * alphabets and digits are each contiguous.
12  */
13 unsigned long
14 wcstoul(const wchar_t *nptr, wchar_t **endptr, int base)
15 {
16   const wchar_t *s = nptr;
17   unsigned long acc;
18   int c;
19   unsigned long cutoff;
20   int neg = 0, any, cutlim;
21
22   /*
23    * See strtol for comments as to the logic used.
24    */
25   do {
26     c = *s++;
27   } while (iswspace(c));
28   if (c == L'-')
29   {
30     neg = 1;
31     c = *s++;
32   }
33   else if (c == L'+')
34     c = *s++;
35   if ((base == 0 || base == 16) &&
36       c == L'0' && (*s == L'x' || *s == L'X'))
37   {
38     c = s[1];
39     s += 2;
40     base = 16;
41   }
42   if (base == 0)
43     base = c == L'0' ? 8 : 10;
44   cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
45   cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
46   for (acc = 0, any = 0;; c = *s++)
47   {
48     if (iswdigit(c))
49       c -= L'0';
50     else if (iswalpha(c))
51       c -= iswupper(c) ? L'A' - 10 : L'a' - 10;
52     else
53       break;
54     if (c >= base)
55       break;
56     if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
57       any = -1;
58     else {
59       any = 1;
60       acc *= base;
61       acc += c;
62     }
63   }
64   if (any < 0)
65   {
66     acc = ULONG_MAX;
67     __set_errno(ERANGE);
68   }
69   else if (neg)
70     acc = -acc;
71   if (endptr != 0)
72     *endptr = any ? (wchar_t *)s - 1 : (wchar_t *)nptr;
73   return acc;
74 }
75
76 #if 0
77 unsigned long wcstoul(const wchar_t *cp,wchar_t **endp,int base)
78 {
79         unsigned long result = 0,value;
80
81         if (!base) {
82                 base = 10;
83                 if (*cp == L'0') {
84                         base = 8;
85                         cp++;
86                         if ((*cp == L'x') && iswxdigit(cp[1])) {
87                                 cp++;
88                                 base = 16;
89                         }
90                 }
91         }
92         while (iswxdigit(*cp) && (value = iswdigit(*cp) ? *cp-L'0' : (iswlower(*cp)
93             ? towupper(*cp) : *cp)-L'A'+10) < base) {
94                 result = result*base + value;
95                 cp++;
96         }
97         if (endp)
98                 *endp = (wchar_t *)cp;
99         return result;
100 }
101 #endif