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 
wcstof(const wchar_t * __restrict__ wcs,wchar_t ** __restrict__ wcse)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 = ___lc_codepage_func();
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 = MultiByteToWideChar (cp, MB_ERR_INVALID_CHARS, cs, -1, NULL, 0);
59       if (i > 0)
60         i -= 1; /* Remove zero terminator from length.  */
61       *wcse = (wchar_t *) wcs + i;
62     }
63   free (cs);
64 
65   return ret;
66 }
67