]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/LibC/Locale/_wcstoul.h
Standard Libraries for EDK II.
[mirror_edk2.git] / StdLib / LibC / Locale / _wcstoul.h
CommitLineData
2aa62f2b 1/** @file\r
2 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
3 This program and the accompanying materials\r
4 are licensed and made available under the terms and conditions of the BSD License\r
5 which accompanies this distribution. The full text of the license may be found at\r
6 http://opensource.org/licenses/bsd-license.php\r
7\r
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
10\r
11 Copyright (c) 1990, 1993\r
12 The Regents of the University of California. All rights reserved.\r
13\r
14 Redistribution and use in source and binary forms, with or without\r
15 modification, are permitted provided that the following conditions\r
16 are met:\r
17 1. Redistributions of source code must retain the above copyright\r
18 notice, this list of conditions and the following disclaimer.\r
19 2. Redistributions in binary form must reproduce the above copyright\r
20 notice, this list of conditions and the following disclaimer in the\r
21 documentation and/or other materials provided with the distribution.\r
22 3. Neither the name of the University nor the names of its contributors\r
23 may be used to endorse or promote products derived from this software\r
24 without specific prior written permission.\r
25\r
26 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\r
27 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
28 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
29 ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r
30 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
31 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
32 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
33 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
34 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
35 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
36 SUCH DAMAGE.\r
37\r
38 Original version ID:\r
39 @(#)strtoul.c 8.1 (Berkeley) 6/4/93\r
40 Citrus: xpg4dl/FreeBSD/lib/libc/locale/wcstoul.c,v 1.2 2001/09/21 16:11:41 yamt Exp\r
41 NetBSD: wcstoul.c,v 1.1 2001/09/27 16:30:37 yamt Exp\r
42 NetBSD: _wcstoul.h,v 1.3 2005/11/29 03:11:59 christos Exp\r
43 */\r
44\r
45/*\r
46 * function template for wcstoul, wcstoull and wcstoumax.\r
47 *\r
48 * parameters:\r
49 * _FUNCNAME : function name\r
50 * __wUINT : return type\r
51 * __wINT : signed version of __wUINT\r
52 * __wUINT_MAX : upper limit of the return type\r
53 */\r
54\r
55__wUINT\r
56_FUNCNAME(\r
57 const wchar_t *nptr,\r
58 wchar_t **endptr,\r
59 int base\r
60 )\r
61{\r
62 const wchar_t *s;\r
63 __wUINT acc, cutoff;\r
64 wint_t wc;\r
65 int i;\r
66 int neg, any, cutlim;\r
67\r
68 _DIAGASSERT(nptr != NULL);\r
69 /* endptr may be NULL */\r
70\r
71 if (base && (base < 2 || base > 36)) {\r
72 errno = EINVAL;\r
73 return 0;\r
74 }\r
75\r
76 /*\r
77 * Skip white space and pick up leading +/- sign if any.\r
78 * If base is 0, allow 0x for hex and 0 for octal, else\r
79 * assume decimal; if base is already 16, allow 0x.\r
80 */\r
81 s = nptr;\r
82 do {\r
83 wc = (wchar_t) *s++;\r
84 } while (iswspace(wc));\r
85 if (wc == L'-') {\r
86 neg = 1;\r
87 wc = *s++;\r
88 } else {\r
89 neg = 0;\r
90 if (wc == L'+')\r
91 wc = *s++;\r
92 }\r
93 if ((base == 0 || base == 16) &&\r
94 wc == L'0' && (*s == L'x' || *s == L'X')) {\r
95 wc = s[1];\r
96 s += 2;\r
97 base = 16;\r
98 }\r
99 if (base == 0)\r
100 base = wc == L'0' ? 8 : 10;\r
101\r
102 /*\r
103 * See strtoul for comments as to the logic used.\r
104 */\r
105 cutoff = __wUINT_MAX / (__wUINT)base;\r
106 cutlim = (int)(__wUINT_MAX % (__wUINT)base);\r
107 for (acc = 0, any = 0;; wc = (wint_t) *s++) {\r
108 i = __wctoint((wchar_t)wc);\r
109 if (i == -1) {\r
110 break;\r
111 }\r
112 if (i >= base)\r
113 break;\r
114 if (any < 0)\r
115 continue;\r
116 if (acc > cutoff || (acc == cutoff && i > cutlim)) {\r
117 any = -1;\r
118 acc = __wUINT_MAX;\r
119 errno = ERANGE;\r
120 } else {\r
121 any = 1;\r
122 acc *= (__wUINT)base;\r
123 acc += i;\r
124 }\r
125 }\r
126 if (neg && any > 0)\r
127 acc = (__wUINT)(-((__wINT)acc));\r
128 if (endptr != 0)\r
129 *endptr = __UNCONST(any ? s - 1 : nptr);\r
130 return (acc);\r
131}\r