update for HEAD-2003091401
[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  * @implemented
14  */
15 unsigned long
16 wcstoul(const wchar_t *nptr, wchar_t **endptr, int base)
17 {
18   const wchar_t *s = nptr;
19   unsigned long acc;
20   int c;
21   unsigned long cutoff;
22   int neg = 0, any, cutlim;
23
24   /*
25    * See strtol for comments as to the logic used.
26    */
27   do {
28     c = *s++;
29   } while (iswspace(c));
30   if (c == L'-')
31   {
32     neg = 1;
33     c = *s++;
34   }
35   else if (c == L'+')
36     c = *s++;
37   if ((base == 0 || base == 16) &&
38       c == L'0' && (*s == L'x' || *s == L'X'))
39   {
40     c = s[1];
41     s += 2;
42     base = 16;
43   }
44   if (base == 0)
45     base = c == L'0' ? 8 : 10;
46   cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
47   cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
48   for (acc = 0, any = 0;; c = *s++)
49   {
50     if (iswdigit(c))
51       c -= L'0';
52     else if (iswalpha(c))
53       c -= iswupper(c) ? L'A' - 10 : L'a' - 10;
54     else
55       break;
56     if (c >= base)
57       break;
58     if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
59       any = -1;
60     else {
61       any = 1;
62       acc *= base;
63       acc += c;
64     }
65   }
66   if (any < 0)
67   {
68     acc = ULONG_MAX;
69     __set_errno(ERANGE);
70   }
71   else if (neg)
72     acc = -acc;
73   if (endptr != 0)
74     *endptr = any ? (wchar_t *)s - 1 : (wchar_t *)nptr;
75   return acc;
76 }
77
78 #if 0
79 unsigned long wcstoul(const wchar_t *cp,wchar_t **endp,int base)
80 {
81         unsigned long result = 0,value;
82
83         if (!base) {
84                 base = 10;
85                 if (*cp == L'0') {
86                         base = 8;
87                         cp++;
88                         if ((*cp == L'x') && iswxdigit(cp[1])) {
89                                 cp++;
90                                 base = 16;
91                         }
92                 }
93         }
94         while (iswxdigit(*cp) && (value = iswdigit(*cp) ? *cp-L'0' : (iswlower(*cp)
95             ? towupper(*cp) : *cp)-L'A'+10) < base) {
96                 result = result*base + value;
97                 cp++;
98         }
99         if (endp)
100                 *endp = (wchar_t *)cp;
101         return result;
102 }
103 #endif