xref: /minix3/lib/libc/locale/_wcstod.h (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /* $NetBSD: _wcstod.h,v 1.4 2013/05/17 12:55:57 joerg Exp $ */
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras  * Copyright (c) 2002 Tim J. Robbins
52fe8fb19SBen Gras  * All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
9*84d9c625SLionel Sambuc  * aINre met:
102fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras  *
162fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
172fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
182fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
192fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
202fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
212fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
222fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
232fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
242fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
252fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
262fe8fb19SBen Gras  * SUCH DAMAGE.
272fe8fb19SBen Gras  *
282fe8fb19SBen Gras  * Original version ID:
292fe8fb19SBen Gras  *   FreeBSD: /repoman/r/ncvs/src/lib/libc/locale/wcstod.c,v 1.4 2004/04/07 09:47:56 tjr Exp
302fe8fb19SBen Gras  *   NetBSD: wcstod.c,v 1.8 2006/04/13 01:25:13 tnozaki Exp
312fe8fb19SBen Gras  */
322fe8fb19SBen Gras 
332fe8fb19SBen Gras /*
342fe8fb19SBen Gras  * function template for wcstof, wcstod, wcstold.
352fe8fb19SBen Gras  *
362fe8fb19SBen Gras  * parameters:
372fe8fb19SBen Gras  *	_FUNCNAME    : function name
382fe8fb19SBen Gras  *	_RETURN_TYPE : return type
392fe8fb19SBen Gras  *	_STRTOD_FUNC : real conversion function
402fe8fb19SBen Gras  */
412fe8fb19SBen Gras #ifndef __WCSTOD_H_
422fe8fb19SBen Gras #define __WCSTOD_H_
432fe8fb19SBen Gras 
44*84d9c625SLionel Sambuc #include <locale.h>
45*84d9c625SLionel Sambuc #include "setlocale_local.h"
46*84d9c625SLionel Sambuc #define INT_NAME_(pre, middle, post) pre ## middle ## post
47*84d9c625SLionel Sambuc #define INT_NAME(pre, middle, post) INT_NAME_(pre, middle, post)
48*84d9c625SLionel Sambuc 
492fe8fb19SBen Gras /*
502fe8fb19SBen Gras  * Convert a string to a double-precision number.
512fe8fb19SBen Gras  *
522fe8fb19SBen Gras  * This is the wide-character counterpart of strto{f,d,ld}(). So that
532fe8fb19SBen Gras  * we do not have to duplicate the code of strto{f,d,ld}() here,
542fe8fb19SBen Gras  * we convert the supplied wide-character string to multibyte and
552fe8fb19SBen Gras  * call strto{f,d,ld}() on the result.
562fe8fb19SBen Gras  * This assumes that the multibyte encoding is compatible with ASCII
572fe8fb19SBen Gras  * for at least the digits, radix character and letters.
582fe8fb19SBen Gras  */
59*84d9c625SLionel Sambuc static _RETURN_TYPE
INT_NAME(_int_,_FUNCNAME,_l)60*84d9c625SLionel Sambuc INT_NAME(_int_, _FUNCNAME, _l)(const wchar_t * __restrict nptr,
61*84d9c625SLionel Sambuc 			   wchar_t ** __restrict endptr, locale_t loc)
622fe8fb19SBen Gras {
632fe8fb19SBen Gras 	const wchar_t *src, *start;
642fe8fb19SBen Gras 	_RETURN_TYPE val;
652fe8fb19SBen Gras 	char *buf, *end;
662fe8fb19SBen Gras 	size_t bufsiz, len;
672fe8fb19SBen Gras 
682fe8fb19SBen Gras 	_DIAGASSERT(nptr != NULL);
692fe8fb19SBen Gras 	/* endptr may be null */
702fe8fb19SBen Gras 
712fe8fb19SBen Gras 	src = nptr;
72*84d9c625SLionel Sambuc 	while (iswspace_l((wint_t)*src, loc) != 0)
732fe8fb19SBen Gras 		++src;
742fe8fb19SBen Gras 	if (*src == L'\0')
752fe8fb19SBen Gras 		goto no_convert;
762fe8fb19SBen Gras 
772fe8fb19SBen Gras 	/*
782fe8fb19SBen Gras 	 * Convert the supplied numeric wide-char. string to multibyte.
792fe8fb19SBen Gras 	 *
802fe8fb19SBen Gras 	 * We could attempt to find the end of the numeric portion of the
812fe8fb19SBen Gras 	 * wide-char. string to avoid converting unneeded characters but
822fe8fb19SBen Gras 	 * choose not to bother; optimising the uncommon case where
832fe8fb19SBen Gras 	 * the input string contains a lot of text after the number
842fe8fb19SBen Gras 	 * duplicates a lot of strto{f,d,ld}()'s functionality and
852fe8fb19SBen Gras 	 * slows down the most common cases.
862fe8fb19SBen Gras 	 */
872fe8fb19SBen Gras 	start = src;
88*84d9c625SLionel Sambuc 	len = wcstombs_l(NULL, src, 0, loc);
892fe8fb19SBen Gras 	if (len == (size_t)-1)
902fe8fb19SBen Gras 		/* errno = EILSEQ */
912fe8fb19SBen Gras 		goto no_convert;
922fe8fb19SBen Gras 
932fe8fb19SBen Gras 	_DIAGASSERT(len > 0);
942fe8fb19SBen Gras 
952fe8fb19SBen Gras 	bufsiz = len;
962fe8fb19SBen Gras 	buf = (void *)malloc(bufsiz + 1);
972fe8fb19SBen Gras 	if (buf == NULL)
982fe8fb19SBen Gras 		/* errno = ENOMEM */
992fe8fb19SBen Gras 		goto no_convert;
1002fe8fb19SBen Gras 
101*84d9c625SLionel Sambuc 	len = wcstombs_l(buf, src, bufsiz + 1, loc);
1022fe8fb19SBen Gras 
1032fe8fb19SBen Gras 	_DIAGASSERT(len == bufsiz);
1042fe8fb19SBen Gras 	_DIAGASSERT(buf[len] == '\0');
1052fe8fb19SBen Gras 
1062fe8fb19SBen Gras 	/* Let strto{f,d,ld}() do most of the work for us. */
107*84d9c625SLionel Sambuc 	val = _STRTOD_FUNC(buf, &end, loc);
1082fe8fb19SBen Gras 	if (buf == end) {
1092fe8fb19SBen Gras 		free(buf);
1102fe8fb19SBen Gras 		goto no_convert;
1112fe8fb19SBen Gras 	}
1122fe8fb19SBen Gras 
1132fe8fb19SBen Gras 	/*
1142fe8fb19SBen Gras 	 * We only know where the number ended in the _multibyte_
1152fe8fb19SBen Gras 	 * representation of the string. If the caller wants to know
1162fe8fb19SBen Gras 	 * where it ended, count multibyte characters to find the
1172fe8fb19SBen Gras 	 * corresponding position in the wide-char string.
1182fe8fb19SBen Gras 	 */
1192fe8fb19SBen Gras 	if (endptr != NULL)
1202fe8fb19SBen Gras 		/* XXX Assume each wide char is one byte. */
1212fe8fb19SBen Gras 		*endptr = __UNCONST(start + (size_t)(end - buf));
1222fe8fb19SBen Gras 
1232fe8fb19SBen Gras 	free(buf);
1242fe8fb19SBen Gras 
1252fe8fb19SBen Gras 	return val;
1262fe8fb19SBen Gras 
1272fe8fb19SBen Gras no_convert:
1282fe8fb19SBen Gras 	if (endptr != NULL)
1292fe8fb19SBen Gras 		*endptr = __UNCONST(nptr);
1302fe8fb19SBen Gras 	return 0;
1312fe8fb19SBen Gras }
132*84d9c625SLionel Sambuc 
133*84d9c625SLionel Sambuc _RETURN_TYPE
134*84d9c625SLionel Sambuc INT_NAME(, _FUNCNAME, )(const wchar_t * __restrict nptr,
135*84d9c625SLionel Sambuc     wchar_t ** __restrict endptr)
136*84d9c625SLionel Sambuc {
137*84d9c625SLionel Sambuc 	return INT_NAME(_int_, _FUNCNAME, _l)(nptr, endptr, _current_locale());
138*84d9c625SLionel Sambuc }
139*84d9c625SLionel Sambuc 
140*84d9c625SLionel Sambuc _RETURN_TYPE
141*84d9c625SLionel Sambuc INT_NAME(, _FUNCNAME, _l)(const wchar_t * __restrict nptr,
142*84d9c625SLionel Sambuc     wchar_t ** __restrict endptr, locale_t loc)
143*84d9c625SLionel Sambuc {
144*84d9c625SLionel Sambuc 	return INT_NAME(_int_, _FUNCNAME, _l)(nptr, endptr, loc);
145*84d9c625SLionel Sambuc }
1462fe8fb19SBen Gras #endif /*__WCSTOD_H_*/
147