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