wcstod.c revision 1.3.2.2 1 1.3.2.2 nathanw /* $NetBSD: wcstod.c,v 1.3.2.2 2001/10/08 20:19:57 nathanw Exp $ */
2 1.3.2.2 nathanw
3 1.3.2.2 nathanw /*-
4 1.3.2.2 nathanw * Copyright (c)1999, 2000, 2001 Citrus Project,
5 1.3.2.2 nathanw * All rights reserved.
6 1.3.2.2 nathanw *
7 1.3.2.2 nathanw * Redistribution and use in source and binary forms, with or without
8 1.3.2.2 nathanw * modification, are permitted provided that the following conditions
9 1.3.2.2 nathanw * are met:
10 1.3.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
11 1.3.2.2 nathanw * notice, this list of conditions and the following disclaimer.
12 1.3.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
13 1.3.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
14 1.3.2.2 nathanw * documentation and/or other materials provided with the distribution.
15 1.3.2.2 nathanw *
16 1.3.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.3.2.2 nathanw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.3.2.2 nathanw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.3.2.2 nathanw * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.3.2.2 nathanw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.3.2.2 nathanw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.3.2.2 nathanw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.3.2.2 nathanw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.3.2.2 nathanw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.3.2.2 nathanw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.3.2.2 nathanw * SUCH DAMAGE.
27 1.3.2.2 nathanw *
28 1.3.2.2 nathanw * $Citrus: xpg4dl/FreeBSD/lib/libc/locale/wcstod.c,v 1.2 2001/09/27 16:23:57 yamt Exp $
29 1.3.2.2 nathanw */
30 1.3.2.2 nathanw
31 1.3.2.2 nathanw #include <sys/cdefs.h>
32 1.3.2.2 nathanw #include <assert.h>
33 1.3.2.2 nathanw #include <errno.h>
34 1.3.2.2 nathanw #include <stdlib.h>
35 1.3.2.2 nathanw #include <string.h>
36 1.3.2.2 nathanw #include <wchar.h>
37 1.3.2.2 nathanw #include <wctype.h>
38 1.3.2.2 nathanw
39 1.3.2.2 nathanw /* XXX our lint can't handle wide char constant */
40 1.3.2.2 nathanw #if defined(lint)
41 1.3.2.2 nathanw #define _L(x) ((const wchar_t *)0)
42 1.3.2.2 nathanw #define _LC(x) ((wchar_t)0)
43 1.3.2.2 nathanw #else
44 1.3.2.2 nathanw #define _L(x) __CONCAT(L,x)
45 1.3.2.2 nathanw #define _LC(x) __CONCAT(L,x)
46 1.3.2.2 nathanw #endif
47 1.3.2.2 nathanw
48 1.3.2.2 nathanw double
49 1.3.2.2 nathanw wcstod(const wchar_t *nptr, wchar_t **endptr)
50 1.3.2.2 nathanw {
51 1.3.2.2 nathanw const wchar_t *src;
52 1.3.2.2 nathanw size_t size;
53 1.3.2.2 nathanw const wchar_t *start;
54 1.3.2.2 nathanw
55 1.3.2.2 nathanw _DIAGASSERT(nptr);
56 1.3.2.2 nathanw
57 1.3.2.2 nathanw /*
58 1.3.2.2 nathanw * we do only check length of string
59 1.3.2.2 nathanw * and give it over strtod.
60 1.3.2.2 nathanw */
61 1.3.2.2 nathanw src = nptr;
62 1.3.2.2 nathanw
63 1.3.2.2 nathanw /* skip space first */
64 1.3.2.2 nathanw while (iswspace(*src)) {
65 1.3.2.2 nathanw src++;
66 1.3.2.2 nathanw }
67 1.3.2.2 nathanw
68 1.3.2.2 nathanw /* get length of string */
69 1.3.2.2 nathanw start = src;
70 1.3.2.2 nathanw if (wcschr(_L("+-"), *src))
71 1.3.2.2 nathanw src++;
72 1.3.2.2 nathanw size = wcsspn(src, _L("0123456789"));
73 1.3.2.2 nathanw src += size;
74 1.3.2.2 nathanw if (*src == _LC('.')) {/* XXX use localeconv */
75 1.3.2.2 nathanw src++;
76 1.3.2.2 nathanw size = wcsspn(src, _L("0123456789"));
77 1.3.2.2 nathanw src += size;
78 1.3.2.2 nathanw }
79 1.3.2.2 nathanw if (wcschr(_L("Ee"), *src)) {
80 1.3.2.2 nathanw src++;
81 1.3.2.2 nathanw if (wcschr(_L("+-"), *src))
82 1.3.2.2 nathanw src++;
83 1.3.2.2 nathanw size = wcsspn(src, _L("0123456789"));
84 1.3.2.2 nathanw src += size;
85 1.3.2.2 nathanw }
86 1.3.2.2 nathanw size = src - start;
87 1.3.2.2 nathanw
88 1.3.2.2 nathanw /*
89 1.3.2.2 nathanw * convert to a char-string and pass it to strtod.
90 1.3.2.2 nathanw *
91 1.3.2.2 nathanw * since all chars used to represent a double-constant
92 1.3.2.2 nathanw * are in the portable character set, we can assume
93 1.3.2.2 nathanw * that they are 1-byte chars.
94 1.3.2.2 nathanw */
95 1.3.2.2 nathanw if (size)
96 1.3.2.2 nathanw {
97 1.3.2.2 nathanw mbstate_t st;
98 1.3.2.2 nathanw char *buf;
99 1.3.2.2 nathanw char *end;
100 1.3.2.2 nathanw const wchar_t *s;
101 1.3.2.2 nathanw size_t size_converted;
102 1.3.2.2 nathanw double result;
103 1.3.2.2 nathanw
104 1.3.2.2 nathanw buf = malloc(size + 1);
105 1.3.2.2 nathanw if (!buf) {
106 1.3.2.2 nathanw /* error */
107 1.3.2.2 nathanw errno = ENOMEM; /* XXX */
108 1.3.2.2 nathanw return 0;
109 1.3.2.2 nathanw }
110 1.3.2.2 nathanw
111 1.3.2.2 nathanw s = start;
112 1.3.2.2 nathanw memset(&st, 0, sizeof(st));
113 1.3.2.2 nathanw size_converted = wcsrtombs(buf, &s, size, &st);
114 1.3.2.2 nathanw if (size != size_converted) {
115 1.3.2.2 nathanw /* XXX should not happen */
116 1.3.2.2 nathanw free(buf);
117 1.3.2.2 nathanw errno = EILSEQ;
118 1.3.2.2 nathanw return 0;
119 1.3.2.2 nathanw }
120 1.3.2.2 nathanw
121 1.3.2.2 nathanw buf[size] = 0;
122 1.3.2.2 nathanw result = strtod(buf, &end);
123 1.3.2.2 nathanw
124 1.3.2.2 nathanw free(buf);
125 1.3.2.2 nathanw
126 1.3.2.2 nathanw if (endptr)
127 1.3.2.2 nathanw /* LINTED bad interface */
128 1.3.2.2 nathanw *endptr = (wchar_t*)start + (end - buf);
129 1.3.2.2 nathanw
130 1.3.2.2 nathanw return result;
131 1.3.2.2 nathanw }
132 1.3.2.2 nathanw
133 1.3.2.2 nathanw if (endptr)
134 1.3.2.2 nathanw /* LINTED bad interface */
135 1.3.2.2 nathanw *endptr = (wchar_t*)start;
136 1.3.2.2 nathanw
137 1.3.2.2 nathanw return 0;
138 1.3.2.2 nathanw }
139