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