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