]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/LibC/Locale/_wcstol.h
Standard Libraries for EDK II.
[mirror_edk2.git] / StdLib / LibC / Locale / _wcstol.h
CommitLineData
2aa62f2b 1/** @file\r
2 Copyright (c) 2010 - 2011, 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.\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 @(#)strtol.c 8.1 (Berkeley) 6/4/93\r
40 NetBSD: wcstol.c,v 1.1 2001/09/27 16:30:36 yamt Exp\r
41 Citrus: xpg4dl/FreeBSD/lib/libc/locale/wcstol.c,v 1.2 2001/09/21 16:11:41 yamt Exp\r
42 NetBSD: _wcstol.h,v 1.3 2005/11/29 03:11:59 christos Exp\r
43 */\r
44\r
45/*\r
46 * function template for wcstol, wcstoll and wcstoimax.\r
47 *\r
48 * parameters:\r
49 * _FUNCNAME : function name\r
50 * __wINT : return type\r
51 * __wINT_MIN : lower limit of the return type\r
52 * __wINT_MAX : upper limit of the return type\r
53 */\r
54\r
55__wINT\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 __wINT 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#ifdef __GNUC__\r
72 (void)&acc; (void)&cutoff;\r
73#endif\r
74\r
75 /* check base value */\r
76 if (base && (base < 2 || base > 36)) {\r
77 errno = EINVAL;\r
78 return 0;\r
79 }\r
80\r
81 /*\r
82 * Skip white space and pick up leading +/- sign if any.\r
83 * If base is 0, allow 0x for hex and 0 for octal, else\r
84 * assume decimal; if base is already 16, allow 0x.\r
85 */\r
86 s = nptr;\r
87 do {\r
88 wc = (wchar_t) *s++;\r
89 } while (iswspace(wc));\r
90 if (wc == L'-') {\r
91 neg = 1;\r
92 wc = *s++;\r
93 } else {\r
94 neg = 0;\r
95 if (wc == L'+')\r
96 wc = *s++;\r
97 }\r
98 if ((base == 0 || base == 16) &&\r
99 wc == L'0' && (*s == L'x' || *s == L'X')) {\r
100 wc = s[1];\r
101 s += 2;\r
102 base = 16;\r
103 }\r
104 if (base == 0)\r
105 base = ((wc == L'0') ? 8 : 10);\r
106\r
107 /*\r
108 * See strtol for comments as to the logic used.\r
109 */\r
110 cutoff = neg ? __wINT_MIN : __wINT_MAX;\r
111 cutlim = (int)(cutoff % base);\r
112 cutoff /= base;\r
113 if (neg) {\r
114 if (cutlim > 0) {\r
115 cutlim -= base;\r
116 cutoff += 1;\r
117 }\r
118 cutlim = -cutlim;\r
119 }\r
120 for (acc = 0, any = 0;; wc = (wchar_t) *s++) {\r
121 i = __wctoint((wchar_t)wc);\r
122 if (i == -1)\r
123 break;\r
124 if (i >= base)\r
125 break;\r
126 if (any < 0)\r
127 continue;\r
128 if (neg) {\r
129 if (acc < cutoff || (acc == cutoff && i > cutlim)) {\r
130 any = -1;\r
131 acc = __wINT_MIN;\r
132 errno = ERANGE;\r
133 } else {\r
134 any = 1;\r
135 acc *= base;\r
136 acc -= i;\r
137 }\r
138 } else {\r
139 if (acc > cutoff || (acc == cutoff && i > cutlim)) {\r
140 any = -1;\r
141 acc = __wINT_MAX;\r
142 errno = ERANGE;\r
143 } else {\r
144 any = 1;\r
145 acc *= base;\r
146 acc += i;\r
147 }\r
148 }\r
149 }\r
150 if (endptr != 0)\r
151 *endptr = __UNCONST(any ? s - 1 : nptr);\r
152 return (acc);\r
153}\r