xref: /reactos/sdk/lib/ucrt/convert/atof.cpp (revision 04e0dc4a)
1 //
2 // atof.cpp
3 //
4 //      Copyright (c) Microsoft Corporation. All rights reserved.
5 //
6 // Definitions of the functions that convert strings to floating point numbers.
7 // Note that the str- and wcs-prefixed functions are defined elsewhere.  The
8 // functions defined here are provided for legacy support.
9 //
10 // The _atoxxx family of functions convert a string into a floating point
11 // number and return either zero (success), or _UNDERFLOW or _OVERFLOW.
12 //
13 #define _ALLOW_OLD_VALIDATE_MACROS
14 #include <corecrt_internal_strtox.h>
15 
16 
17 
18 template <typename FloatingType, typename Character>
common_atodbl_l(FloatingType * const result,Character const * const string,_locale_t const locale)19 static int __cdecl common_atodbl_l(
20     FloatingType*    const result,
21     Character const* const string,
22     _locale_t        const locale
23     ) throw()
24 {
25     _VALIDATE_RETURN(result != nullptr, EINVAL, _DOMAIN);
26 
27     _LocaleUpdate locale_update(locale);
28     SLD_STATUS const status = __crt_strtox::parse_floating_point(
29         locale_update.GetLocaleT(),
30         __crt_strtox::make_c_string_character_source(string, nullptr),
31         result);
32 
33     switch (status)
34     {
35     case SLD_OVERFLOW:  return _OVERFLOW;
36     case SLD_UNDERFLOW: return _UNDERFLOW;
37     default:            return 0;
38     }
39 }
40 
_atoflt_l(_CRT_FLOAT * const result,char const * const string,_locale_t const locale)41 extern "C" int __cdecl _atoflt_l(_CRT_FLOAT* const result, char const* const string, _locale_t const locale)
42 {
43     return common_atodbl_l(&result->f, string, locale);
44 }
45 
_atoflt(_CRT_FLOAT * const result,char const * const string)46 extern "C" int __cdecl _atoflt(_CRT_FLOAT* const result, char const* const string)
47 {
48     return common_atodbl_l(&result->f, string, nullptr);
49 }
50 
_atodbl_l(_CRT_DOUBLE * const result,char * const string,_locale_t const locale)51 extern "C" int __cdecl _atodbl_l(_CRT_DOUBLE* const result, char* const string, _locale_t const locale)
52 {
53     return common_atodbl_l(&result->x, string, locale);
54 }
55 
_atodbl(_CRT_DOUBLE * const result,char * const string)56 extern "C" int __cdecl _atodbl(_CRT_DOUBLE* const result, char* const string)
57 {
58     return common_atodbl_l(&result->x, string, nullptr);
59 }
60 
61 
62 
63 template <typename Character>
common_atof_l(Character const * const string,_locale_t const locale)64 static double __cdecl common_atof_l(Character const* const string, _locale_t const locale) throw()
65 {
66     _VALIDATE_RETURN(string != nullptr, EINVAL, 0.0);
67 
68     _LocaleUpdate locale_update(locale);
69 
70     double result{};
71     __crt_strtox::parse_floating_point(
72         locale_update.GetLocaleT(),
73         __crt_strtox::make_c_string_character_source(string, nullptr),
74         &result);
75     return result;
76 }
77 
_atof_l(char const * const string,_locale_t const locale)78 extern "C" double __cdecl _atof_l(char const* const string, _locale_t const locale)
79 {
80     return common_atof_l(string, locale);
81 }
82 
atof(char const * const string)83 extern "C" double __cdecl atof(char const* const string)
84 {
85     return common_atof_l(string, nullptr);
86 }
87 
_wtof_l(wchar_t const * const string,_locale_t const locale)88 extern "C" double __cdecl _wtof_l(wchar_t const* const string, _locale_t const locale)
89 {
90     return common_atof_l(string, locale);
91 }
92 
_wtof(wchar_t const * const string)93 extern "C" double __cdecl _wtof(wchar_t const* const string)
94 {
95     return common_atof_l(string, nullptr);
96 }
97