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