_wcstod.h revision 1.2 1 1.2 wiz /* $NetBSD: _wcstod.h,v 1.2 2010/12/16 17:42:27 wiz Exp $ */
2 1.1 tnozaki
3 1.1 tnozaki /*-
4 1.1 tnozaki * Copyright (c) 2002 Tim J. Robbins
5 1.1 tnozaki * All rights reserved.
6 1.1 tnozaki *
7 1.1 tnozaki * Redistribution and use in source and binary forms, with or without
8 1.1 tnozaki * modification, are permitted provided that the following conditions
9 1.1 tnozaki * are met:
10 1.1 tnozaki * 1. Redistributions of source code must retain the above copyright
11 1.1 tnozaki * notice, this list of conditions and the following disclaimer.
12 1.1 tnozaki * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 tnozaki * notice, this list of conditions and the following disclaimer in the
14 1.1 tnozaki * documentation and/or other materials provided with the distribution.
15 1.1 tnozaki *
16 1.1 tnozaki * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 tnozaki * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 tnozaki * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 tnozaki * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 tnozaki * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 tnozaki * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 tnozaki * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 tnozaki * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 tnozaki * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 tnozaki * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 tnozaki * SUCH DAMAGE.
27 1.1 tnozaki *
28 1.1 tnozaki * Original version ID:
29 1.1 tnozaki * FreeBSD: /repoman/r/ncvs/src/lib/libc/locale/wcstod.c,v 1.4 2004/04/07 09:47:56 tjr Exp
30 1.1 tnozaki * NetBSD: wcstod.c,v 1.8 2006/04/13 01:25:13 tnozaki Exp
31 1.1 tnozaki */
32 1.1 tnozaki
33 1.1 tnozaki /*
34 1.1 tnozaki * function template for wcstof, wcstod, wcstold.
35 1.1 tnozaki *
36 1.1 tnozaki * parameters:
37 1.1 tnozaki * _FUNCNAME : function name
38 1.1 tnozaki * _RETURN_TYPE : return type
39 1.1 tnozaki * _STRTOD_FUNC : real conversion function
40 1.1 tnozaki */
41 1.1 tnozaki #ifndef __WCSTOD_H_
42 1.1 tnozaki #define __WCSTOD_H_
43 1.1 tnozaki
44 1.1 tnozaki /*
45 1.1 tnozaki * Convert a string to a double-precision number.
46 1.1 tnozaki *
47 1.1 tnozaki * This is the wide-character counterpart of strto{f,d,ld}(). So that
48 1.1 tnozaki * we do not have to duplicate the code of strto{f,d,ld}() here,
49 1.2 wiz * we convert the supplied wide-character string to multibyte and
50 1.1 tnozaki * call strto{f,d,ld}() on the result.
51 1.1 tnozaki * This assumes that the multibyte encoding is compatible with ASCII
52 1.1 tnozaki * for at least the digits, radix character and letters.
53 1.1 tnozaki */
54 1.1 tnozaki _RETURN_TYPE
55 1.1 tnozaki _FUNCNAME(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
56 1.1 tnozaki {
57 1.1 tnozaki const wchar_t *src, *start;
58 1.1 tnozaki _RETURN_TYPE val;
59 1.1 tnozaki char *buf, *end;
60 1.1 tnozaki size_t bufsiz, len;
61 1.1 tnozaki
62 1.1 tnozaki _DIAGASSERT(nptr != NULL);
63 1.1 tnozaki /* endptr may be null */
64 1.1 tnozaki
65 1.1 tnozaki src = nptr;
66 1.1 tnozaki while (iswspace((wint_t)*src) != 0)
67 1.1 tnozaki ++src;
68 1.1 tnozaki if (*src == L'\0')
69 1.1 tnozaki goto no_convert;
70 1.1 tnozaki
71 1.1 tnozaki /*
72 1.2 wiz * Convert the supplied numeric wide-char. string to multibyte.
73 1.1 tnozaki *
74 1.1 tnozaki * We could attempt to find the end of the numeric portion of the
75 1.2 wiz * wide-char. string to avoid converting unneeded characters but
76 1.1 tnozaki * choose not to bother; optimising the uncommon case where
77 1.1 tnozaki * the input string contains a lot of text after the number
78 1.1 tnozaki * duplicates a lot of strto{f,d,ld}()'s functionality and
79 1.1 tnozaki * slows down the most common cases.
80 1.1 tnozaki */
81 1.1 tnozaki start = src;
82 1.1 tnozaki len = wcstombs(NULL, src, 0);
83 1.1 tnozaki if (len == (size_t)-1)
84 1.1 tnozaki /* errno = EILSEQ */
85 1.1 tnozaki goto no_convert;
86 1.1 tnozaki
87 1.1 tnozaki _DIAGASSERT(len > 0);
88 1.1 tnozaki
89 1.1 tnozaki bufsiz = len;
90 1.1 tnozaki buf = (void *)malloc(bufsiz + 1);
91 1.1 tnozaki if (buf == NULL)
92 1.1 tnozaki /* errno = ENOMEM */
93 1.1 tnozaki goto no_convert;
94 1.1 tnozaki
95 1.1 tnozaki len = wcstombs(buf, src, bufsiz + 1);
96 1.1 tnozaki
97 1.1 tnozaki _DIAGASSERT(len == bufsiz);
98 1.1 tnozaki _DIAGASSERT(buf[len] == '\0');
99 1.1 tnozaki
100 1.1 tnozaki /* Let strto{f,d,ld}() do most of the work for us. */
101 1.1 tnozaki val = _STRTOD_FUNC(buf, &end);
102 1.1 tnozaki if (buf == end) {
103 1.1 tnozaki free(buf);
104 1.1 tnozaki goto no_convert;
105 1.1 tnozaki }
106 1.1 tnozaki
107 1.1 tnozaki /*
108 1.1 tnozaki * We only know where the number ended in the _multibyte_
109 1.1 tnozaki * representation of the string. If the caller wants to know
110 1.1 tnozaki * where it ended, count multibyte characters to find the
111 1.2 wiz * corresponding position in the wide-char string.
112 1.1 tnozaki */
113 1.1 tnozaki if (endptr != NULL)
114 1.1 tnozaki /* XXX Assume each wide char is one byte. */
115 1.1 tnozaki *endptr = __UNCONST(start + (size_t)(end - buf));
116 1.1 tnozaki
117 1.1 tnozaki free(buf);
118 1.1 tnozaki
119 1.1 tnozaki return val;
120 1.1 tnozaki
121 1.1 tnozaki no_convert:
122 1.1 tnozaki if (endptr != NULL)
123 1.1 tnozaki *endptr = __UNCONST(nptr);
124 1.1 tnozaki return 0;
125 1.1 tnozaki }
126 1.1 tnozaki #endif /*__WCSTOD_H_*/
127