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