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