OSDN Git Service

Adjust repository version following WSL-5.2.2 release.
[mingw/mingw-org-wsl.git] / mingwrt / mingwex / wcstof.c
1 /*  Wide char wrapper for strtof
2  *  Revision history:
3  *  25 Aug 2006 Initial version.
4  *
5  *  Contributor:   Danny Smith <dannysmith@users.sourceforege.net>
6  */
7
8  /* This routine has been placed in the public domain.*/
9
10 #define WIN32_LEAN_AND_MEAN
11 #include <windows.h>
12 #include <locale.h>
13 #include <wchar.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <mbstring.h>
17
18 #include "mb_wc_common.h"
19
20 float wcstof (const wchar_t * __restrict__ wcs, wchar_t ** __restrict__ wcse)
21 {
22   char * cs;
23   char * cse;
24   unsigned int i;
25   float ret;
26   const unsigned int cp = get_codepage ();
27
28   /* Allocate enough room for (possibly) mb chars */
29   cs = (char *) malloc ((wcslen(wcs)+1) * MB_CUR_MAX);
30
31   if (cp == 0) /* C locale */
32     {
33       for (i = 0; (wcs[i] != 0) && wcs[i] <= 255; i++)
34         cs[i] = (char) wcs[i];
35       cs[i]  = '\0';
36     }
37   else
38     {
39       int nbytes = -1;
40       int mb_len = 0;
41       /* loop through till we hit null or invalid character */
42       for (i = 0; (wcs[i] != 0) && (nbytes != 0); i++)
43         {
44           nbytes = WideCharToMultiByte(cp, WC_COMPOSITECHECK | WC_SEPCHARS,
45                                        wcs + i, 1, cs + mb_len, MB_CUR_MAX,
46                                        NULL, NULL);
47           mb_len += nbytes;
48         }
49       cs[mb_len] = '\0';
50     }
51
52   ret =  strtof (cs, &cse);
53
54   if (wcse)
55     {
56       /* Make sure temp mbstring cs has 0 at cse.  */
57       *cse = '\0';
58       i = _mbslen ((unsigned char*) cs); /* Number of chars, not bytes */
59       *wcse = (wchar_t *) wcs + i;
60     }
61   free (cs);
62
63   return ret;
64 }