Home | History | Annotate | Line # | Download | only in locale
wcstod.c revision 1.6
      1 /* $NetBSD: wcstod.c,v 1.6 2005/11/29 03:11:59 christos 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.6 2005/11/29 03:11:59 christos 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 	}
     65 
     66 	/* get length of string */
     67 	start = src;
     68 	if (wcschr(_L("+-"), *src))
     69 		src++;
     70 	size = wcsspn(src, _L("0123456789"));
     71 	src += size;
     72 	if (*src == _LC('.')) {/* XXX use localeconv */
     73 		src++;
     74 		size = wcsspn(src, _L("0123456789"));
     75 		src += size;
     76 	}
     77 	if (wcschr(_L("Ee"), *src)) {
     78 		src++;
     79 		if (wcschr(_L("+-"), *src))
     80 			src++;
     81 		size = wcsspn(src, _L("0123456789"));
     82 		src += size;
     83 	}
     84 	size = src - start;
     85 
     86 	/*
     87 	 * convert to a char-string and pass it to strtod.
     88 	 *
     89 	 * since all chars used to represent a double-constant
     90 	 * are in the portable character set, we can assume
     91 	 * that they are 1-byte chars.
     92 	 */
     93 	if (size)
     94 	{
     95 		mbstate_t st;
     96 		char *buf;
     97 		char *end;
     98 		const wchar_t *s;
     99 		size_t size_converted;
    100 		double result;
    101 
    102 		buf = malloc(size + 1);
    103 		if (!buf) {
    104 			/* error */
    105 			errno = ENOMEM; /* XXX */
    106 			return 0;
    107 		}
    108 
    109 		s = start;
    110 		memset(&st, 0, sizeof(st));
    111 		size_converted = wcsrtombs(buf, &s, size, &st);
    112 		if (size != size_converted) {
    113 			/* XXX should not happen */
    114 			free(buf);
    115 			errno = EILSEQ;
    116 			return 0;
    117 		}
    118 
    119 		buf[size] = 0;
    120 		result = strtod(buf, &end);
    121 
    122 		free(buf);
    123 
    124 		if (endptr)
    125 			*endptr = __UNCONST(
    126 			    (const wchar_t*)start + (end - buf));
    127 
    128 		return result;
    129 	}
    130 
    131 	if (endptr)
    132 		*endptr = __UNCONST(start);
    133 
    134 	return 0;
    135 }
    136