wcstod.c revision 1.3.2.3 1 1.3.2.3 nathanw /* $NetBSD: wcstod.c,v 1.3.2.3 2001/11/14 19:31:58 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 #define _L(x) __CONCAT(L,x)
40 1.3.2.2 nathanw #define _LC(x) __CONCAT(L,x)
41 1.3.2.2 nathanw
42 1.3.2.2 nathanw double
43 1.3.2.2 nathanw wcstod(const wchar_t *nptr, wchar_t **endptr)
44 1.3.2.2 nathanw {
45 1.3.2.2 nathanw const wchar_t *src;
46 1.3.2.2 nathanw size_t size;
47 1.3.2.2 nathanw const wchar_t *start;
48 1.3.2.2 nathanw
49 1.3.2.2 nathanw _DIAGASSERT(nptr);
50 1.3.2.2 nathanw
51 1.3.2.2 nathanw /*
52 1.3.2.2 nathanw * we do only check length of string
53 1.3.2.2 nathanw * and give it over strtod.
54 1.3.2.2 nathanw */
55 1.3.2.2 nathanw src = nptr;
56 1.3.2.2 nathanw
57 1.3.2.2 nathanw /* skip space first */
58 1.3.2.2 nathanw while (iswspace(*src)) {
59 1.3.2.2 nathanw src++;
60 1.3.2.2 nathanw }
61 1.3.2.2 nathanw
62 1.3.2.2 nathanw /* get length of string */
63 1.3.2.2 nathanw start = src;
64 1.3.2.2 nathanw if (wcschr(_L("+-"), *src))
65 1.3.2.2 nathanw src++;
66 1.3.2.2 nathanw size = wcsspn(src, _L("0123456789"));
67 1.3.2.2 nathanw src += size;
68 1.3.2.2 nathanw if (*src == _LC('.')) {/* XXX use localeconv */
69 1.3.2.2 nathanw src++;
70 1.3.2.2 nathanw size = wcsspn(src, _L("0123456789"));
71 1.3.2.2 nathanw src += size;
72 1.3.2.2 nathanw }
73 1.3.2.2 nathanw if (wcschr(_L("Ee"), *src)) {
74 1.3.2.2 nathanw src++;
75 1.3.2.2 nathanw if (wcschr(_L("+-"), *src))
76 1.3.2.2 nathanw src++;
77 1.3.2.2 nathanw size = wcsspn(src, _L("0123456789"));
78 1.3.2.2 nathanw src += size;
79 1.3.2.2 nathanw }
80 1.3.2.2 nathanw size = src - start;
81 1.3.2.2 nathanw
82 1.3.2.2 nathanw /*
83 1.3.2.2 nathanw * convert to a char-string and pass it to strtod.
84 1.3.2.2 nathanw *
85 1.3.2.2 nathanw * since all chars used to represent a double-constant
86 1.3.2.2 nathanw * are in the portable character set, we can assume
87 1.3.2.2 nathanw * that they are 1-byte chars.
88 1.3.2.2 nathanw */
89 1.3.2.2 nathanw if (size)
90 1.3.2.2 nathanw {
91 1.3.2.2 nathanw mbstate_t st;
92 1.3.2.2 nathanw char *buf;
93 1.3.2.2 nathanw char *end;
94 1.3.2.2 nathanw const wchar_t *s;
95 1.3.2.2 nathanw size_t size_converted;
96 1.3.2.2 nathanw double result;
97 1.3.2.2 nathanw
98 1.3.2.2 nathanw buf = malloc(size + 1);
99 1.3.2.2 nathanw if (!buf) {
100 1.3.2.2 nathanw /* error */
101 1.3.2.2 nathanw errno = ENOMEM; /* XXX */
102 1.3.2.2 nathanw return 0;
103 1.3.2.2 nathanw }
104 1.3.2.2 nathanw
105 1.3.2.2 nathanw s = start;
106 1.3.2.2 nathanw memset(&st, 0, sizeof(st));
107 1.3.2.2 nathanw size_converted = wcsrtombs(buf, &s, size, &st);
108 1.3.2.2 nathanw if (size != size_converted) {
109 1.3.2.2 nathanw /* XXX should not happen */
110 1.3.2.2 nathanw free(buf);
111 1.3.2.2 nathanw errno = EILSEQ;
112 1.3.2.2 nathanw return 0;
113 1.3.2.2 nathanw }
114 1.3.2.2 nathanw
115 1.3.2.2 nathanw buf[size] = 0;
116 1.3.2.2 nathanw result = strtod(buf, &end);
117 1.3.2.2 nathanw
118 1.3.2.2 nathanw free(buf);
119 1.3.2.2 nathanw
120 1.3.2.2 nathanw if (endptr)
121 1.3.2.2 nathanw /* LINTED bad interface */
122 1.3.2.2 nathanw *endptr = (wchar_t*)start + (end - buf);
123 1.3.2.2 nathanw
124 1.3.2.2 nathanw return result;
125 1.3.2.2 nathanw }
126 1.3.2.2 nathanw
127 1.3.2.2 nathanw if (endptr)
128 1.3.2.2 nathanw /* LINTED bad interface */
129 1.3.2.2 nathanw *endptr = (wchar_t*)start;
130 1.3.2.2 nathanw
131 1.3.2.2 nathanw return 0;
132 1.3.2.2 nathanw }
133