1 |
207 |
jeremybenn |
/*
|
2 |
|
|
FUNCTION
|
3 |
|
|
<<wcstod>>, <<wcstof>>---wide char string to double or float
|
4 |
|
|
|
5 |
|
|
INDEX
|
6 |
|
|
wcstod
|
7 |
|
|
INDEX
|
8 |
|
|
_wcstod_r
|
9 |
|
|
INDEX
|
10 |
|
|
wcstof
|
11 |
|
|
INDEX
|
12 |
|
|
_wcstof_r
|
13 |
|
|
|
14 |
|
|
ANSI_SYNOPSIS
|
15 |
|
|
#include <stdlib.h>
|
16 |
|
|
double wcstod(const wchar_t *<[str]>, wchar_t **<[tail]>);
|
17 |
|
|
float wcstof(const wchar_t *<[str]>, wchar_t **<[tail]>);
|
18 |
|
|
|
19 |
|
|
double _wcstod_r(void *<[reent]>,
|
20 |
|
|
const wchar_t *<[str]>, wchar_t **<[tail]>);
|
21 |
|
|
float _wcstof_r(void *<[reent]>,
|
22 |
|
|
const wchar_t *<[str]>, wchar_t **<[tail]>);
|
23 |
|
|
|
24 |
|
|
TRAD_SYNOPSIS
|
25 |
|
|
#include <stdlib.h>
|
26 |
|
|
double wcstod(<[str]>,<[tail]>)
|
27 |
|
|
wchar_t *<[str]>;
|
28 |
|
|
wchar_t **<[tail]>;
|
29 |
|
|
|
30 |
|
|
float wcstof(<[str]>,<[tail]>)
|
31 |
|
|
wchar_t *<[str]>;
|
32 |
|
|
wchar_t **<[tail]>;
|
33 |
|
|
|
34 |
|
|
double _wcstod_r(<[reent]>,<[str]>,<[tail]>)
|
35 |
|
|
wchar_t *<[reent]>;
|
36 |
|
|
wchar_t *<[str]>;
|
37 |
|
|
wchar_t **<[tail]>;
|
38 |
|
|
|
39 |
|
|
float _wcstof_r(<[reent]>,<[str]>,<[tail]>)
|
40 |
|
|
wchar_t *<[reent]>;
|
41 |
|
|
wchar_t *<[str]>;
|
42 |
|
|
wchar_t **<[tail]>;
|
43 |
|
|
|
44 |
|
|
DESCRIPTION
|
45 |
|
|
The function <<wcstod>> parses the wide character string <[str]>,
|
46 |
|
|
producing a substring which can be converted to a double
|
47 |
|
|
value. The substring converted is the longest initial
|
48 |
|
|
subsequence of <[str]>, beginning with the first
|
49 |
|
|
non-whitespace character, that has one of these formats:
|
50 |
|
|
.[+|-]<[digits]>[.[<[digits]>]][(e|E)[+|-]<[digits]>]
|
51 |
|
|
.[+|-].<[digits]>[(e|E)[+|-]<[digits]>]
|
52 |
|
|
.[+|-](i|I)(n|N)(f|F)[(i|I)(n|N)(i|I)(t|T)(y|Y)]
|
53 |
|
|
.[+|-](n|N)(a|A)(n|N)[<(>[<[hexdigits]>]<)>]
|
54 |
|
|
.[+|-]0(x|X)<[hexdigits]>[.[<[hexdigits]>]][(p|P)[+|-]<[digits]>]
|
55 |
|
|
.[+|-]0(x|X).<[hexdigits]>[(p|P)[+|-]<[digits]>]
|
56 |
|
|
The substring contains no characters if <[str]> is empty, consists
|
57 |
|
|
entirely of whitespace, or if the first non-whitespace
|
58 |
|
|
character is something other than <<+>>, <<->>, <<.>>, or a
|
59 |
|
|
digit, and cannot be parsed as infinity or NaN. If the platform
|
60 |
|
|
does not support NaN, then NaN is treated as an empty substring.
|
61 |
|
|
If the substring is empty, no conversion is done, and
|
62 |
|
|
the value of <[str]> is stored in <<*<[tail]>>>. Otherwise,
|
63 |
|
|
the substring is converted, and a pointer to the final string
|
64 |
|
|
(which will contain at least the terminating null character of
|
65 |
|
|
<[str]>) is stored in <<*<[tail]>>>. If you want no
|
66 |
|
|
assignment to <<*<[tail]>>>, pass a null pointer as <[tail]>.
|
67 |
|
|
<<wcstof>> is identical to <<wcstod>> except for its return type.
|
68 |
|
|
|
69 |
|
|
This implementation returns the nearest machine number to the
|
70 |
|
|
input decimal string. Ties are broken by using the IEEE
|
71 |
|
|
round-even rule. However, <<wcstof>> is currently subject to
|
72 |
|
|
double rounding errors.
|
73 |
|
|
|
74 |
|
|
The alternate functions <<_wcstod_r>> and <<_wcstof_r>> are
|
75 |
|
|
reentrant versions of <<wcstod>> and <<wcstof>>, respectively.
|
76 |
|
|
The extra argument <[reent]> is a pointer to a reentrancy structure.
|
77 |
|
|
|
78 |
|
|
RETURNS
|
79 |
|
|
Return the converted substring value, if any. If
|
80 |
|
|
no conversion could be performed, 0 is returned. If the
|
81 |
|
|
correct value is out of the range of representable values,
|
82 |
|
|
plus or minus <<HUGE_VAL>> is returned, and <<ERANGE>> is
|
83 |
|
|
stored in errno. If the correct value would cause underflow, 0
|
84 |
|
|
is returned and <<ERANGE>> is stored in errno.
|
85 |
|
|
|
86 |
|
|
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
|
87 |
|
|
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
|
88 |
|
|
*/
|
89 |
|
|
|
90 |
|
|
/*-
|
91 |
|
|
* Copyright (c) 2002 Tim J. Robbins
|
92 |
|
|
* All rights reserved.
|
93 |
|
|
*
|
94 |
|
|
* Redistribution and use in source and binary forms, with or without
|
95 |
|
|
* modification, are permitted provided that the following conditions
|
96 |
|
|
* are met:
|
97 |
|
|
* 1. Redistributions of source code must retain the above copyright
|
98 |
|
|
* notice, this list of conditions and the following disclaimer.
|
99 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
100 |
|
|
* notice, this list of conditions and the following disclaimer in the
|
101 |
|
|
* documentation and/or other materials provided with the distribution.
|
102 |
|
|
*
|
103 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
104 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
105 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
106 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
107 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
108 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
109 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
110 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
111 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
112 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
113 |
|
|
* SUCH DAMAGE.
|
114 |
|
|
*/
|
115 |
|
|
|
116 |
|
|
#include <_ansi.h>
|
117 |
|
|
#include <errno.h>
|
118 |
|
|
#include <stdlib.h>
|
119 |
|
|
#include <string.h>
|
120 |
|
|
#include <wchar.h>
|
121 |
|
|
#include <wctype.h>
|
122 |
|
|
#include <locale.h>
|
123 |
|
|
#include <math.h>
|
124 |
|
|
|
125 |
|
|
double
|
126 |
|
|
_DEFUN (_wcstod_r, (ptr, nptr, endptr),
|
127 |
|
|
struct _reent *ptr _AND
|
128 |
|
|
_CONST wchar_t *nptr _AND
|
129 |
|
|
wchar_t **endptr)
|
130 |
|
|
{
|
131 |
|
|
static const mbstate_t initial;
|
132 |
|
|
mbstate_t mbs;
|
133 |
|
|
double val;
|
134 |
|
|
char *buf, *end;
|
135 |
|
|
const wchar_t *wcp;
|
136 |
|
|
size_t len;
|
137 |
|
|
|
138 |
|
|
while (iswspace(*nptr))
|
139 |
|
|
nptr++;
|
140 |
|
|
|
141 |
|
|
/*
|
142 |
|
|
* Convert the supplied numeric wide char. string to multibyte.
|
143 |
|
|
*
|
144 |
|
|
* We could attempt to find the end of the numeric portion of the
|
145 |
|
|
* wide char. string to avoid converting unneeded characters but
|
146 |
|
|
* choose not to bother; optimising the uncommon case where
|
147 |
|
|
* the input string contains a lot of text after the number
|
148 |
|
|
* duplicates a lot of strtod()'s functionality and slows down the
|
149 |
|
|
* most common cases.
|
150 |
|
|
*/
|
151 |
|
|
wcp = nptr;
|
152 |
|
|
mbs = initial;
|
153 |
|
|
if ((len = _wcsrtombs_r(ptr, NULL, &wcp, 0, &mbs)) == (size_t)-1) {
|
154 |
|
|
if (endptr != NULL)
|
155 |
|
|
*endptr = (wchar_t *)nptr;
|
156 |
|
|
return (0.0);
|
157 |
|
|
}
|
158 |
|
|
if ((buf = _malloc_r(ptr, len + 1)) == NULL)
|
159 |
|
|
return (0.0);
|
160 |
|
|
mbs = initial;
|
161 |
|
|
_wcsrtombs_r(ptr, buf, &wcp, len + 1, &mbs);
|
162 |
|
|
|
163 |
|
|
/* Let strtod() do most of the work for us. */
|
164 |
|
|
val = _strtod_r(ptr, buf, &end);
|
165 |
|
|
|
166 |
|
|
/*
|
167 |
|
|
* We only know where the number ended in the _multibyte_
|
168 |
|
|
* representation of the string. If the caller wants to know
|
169 |
|
|
* where it ended, count multibyte characters to find the
|
170 |
|
|
* corresponding position in the wide char string.
|
171 |
|
|
*/
|
172 |
|
|
if (endptr != NULL) {
|
173 |
|
|
/* The only valid multibyte char in a float converted by
|
174 |
|
|
strtod/wcstod is the radix char. What we do here is,
|
175 |
|
|
figure out if the radix char was in the valid leading
|
176 |
|
|
float sequence in the incoming string. If so, the
|
177 |
|
|
multibyte float string is strlen(radix char) - 1 bytes
|
178 |
|
|
longer than the incoming wide char string has characters.
|
179 |
|
|
To fix endptr, reposition end as if the radix char was
|
180 |
|
|
just one byte long. The resulting difference (end - buf)
|
181 |
|
|
is then equivalent to the number of valid wide characters
|
182 |
|
|
in the input string. */
|
183 |
|
|
len = strlen (_localeconv_r (ptr)->decimal_point);
|
184 |
|
|
if (len > 1) {
|
185 |
|
|
char *d = strstr (buf,
|
186 |
|
|
_localeconv_r (ptr)->decimal_point);
|
187 |
|
|
if (d && d < end)
|
188 |
|
|
end -= len - 1;
|
189 |
|
|
}
|
190 |
|
|
*endptr = (wchar_t *)nptr + (end - buf);
|
191 |
|
|
}
|
192 |
|
|
|
193 |
|
|
_free_r(ptr, buf);
|
194 |
|
|
|
195 |
|
|
return (val);
|
196 |
|
|
}
|
197 |
|
|
|
198 |
|
|
float
|
199 |
|
|
_DEFUN (_wcstof_r, (ptr, nptr, endptr),
|
200 |
|
|
struct _reent *ptr _AND
|
201 |
|
|
_CONST wchar_t *nptr _AND
|
202 |
|
|
wchar_t **endptr)
|
203 |
|
|
{
|
204 |
|
|
double retval = _wcstod_r (ptr, nptr, endptr);
|
205 |
|
|
if (isnan (retval))
|
206 |
|
|
return nanf (NULL);
|
207 |
|
|
return (float)retval;
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
#ifndef _REENT_ONLY
|
211 |
|
|
|
212 |
|
|
double
|
213 |
|
|
_DEFUN (wcstod, (nptr, endptr),
|
214 |
|
|
_CONST wchar_t *nptr _AND wchar_t **endptr)
|
215 |
|
|
{
|
216 |
|
|
return _wcstod_r (_REENT, nptr, endptr);
|
217 |
|
|
}
|
218 |
|
|
|
219 |
|
|
float
|
220 |
|
|
_DEFUN (wcstof, (nptr, endptr),
|
221 |
|
|
_CONST wchar_t *nptr _AND
|
222 |
|
|
wchar_t **endptr)
|
223 |
|
|
{
|
224 |
|
|
double retval = _wcstod_r (_REENT, nptr, endptr);
|
225 |
|
|
if (isnan (retval))
|
226 |
|
|
return nanf (NULL);
|
227 |
|
|
return (float)retval;
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
#endif
|