|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <corecrt_internal.h>
|
| #include <corecrt_internal_fltintrn.h>
|
| #include <corecrt_internal_ptd_propagation.h>
|
| #include <corecrt_internal_securecrt.h>
|
| #include <corecrt_stdio_config.h>
|
| #include <locale.h>
|
| #include <stdlib.h>
|
|
|
|
|
|
|
| static errno_t __cdecl _gcvt_s_internal(
|
| char* const buffer,
|
| size_t const buffer_count,
|
| double const value,
|
| int const precision,
|
| __crt_cached_ptd_host& ptd
|
| )
|
| {
|
| _UCRT_VALIDATE_RETURN_ERRCODE(ptd, buffer != nullptr, EINVAL);
|
| _UCRT_VALIDATE_RETURN_ERRCODE(ptd, buffer_count > 0, EINVAL);
|
| _RESET_STRING(buffer, buffer_count);
|
| _UCRT_VALIDATE_RETURN_ERRCODE(ptd, static_cast<size_t>(precision) < buffer_count, ERANGE);
|
|
|
|
|
| char const decimal_point = *ptd.get_locale()->locinfo->lconv->decimal_point;
|
|
|
|
|
|
|
|
|
|
|
| size_t const restricted_count = 7;
|
| char result_string[restricted_count];
|
|
|
| _strflt strflt{};
|
|
|
| __acrt_fltout(
|
| reinterpret_cast<_CRT_DOUBLE const&>(value),
|
| precision,
|
| __acrt_precision_style::fixed,
|
| &strflt,
|
| result_string,
|
| restricted_count);
|
|
|
| int const magnitude = strflt.decpt - 1;
|
|
|
|
|
|
|
| if (magnitude < -1 || magnitude > precision - 1)
|
| {
|
|
|
| char scratch_buffer[_CVTBUFSIZE + 1];
|
| errno_t const e = __acrt_fp_format(
|
| &value,
|
| buffer,
|
| buffer_count,
|
| scratch_buffer,
|
| _countof(scratch_buffer),
|
| 'e',
|
| precision - 1,
|
| _CRT_INTERNAL_PRINTF_LEGACY_THREE_DIGIT_EXPONENTS,
|
| __acrt_rounding_mode::legacy,
|
| ptd);
|
|
|
| if (e != 0)
|
| {
|
| return ptd.get_errno().set(e);
|
| }
|
| }
|
| else
|
| {
|
|
|
| char scratch_buffer[_CVTBUFSIZE + 1];
|
| errno_t const e = __acrt_fp_format(
|
| &value,
|
| buffer,
|
| buffer_count,
|
| scratch_buffer,
|
| _countof(scratch_buffer),
|
| 'f',
|
| precision - strflt.decpt,
|
| _CRT_INTERNAL_PRINTF_LEGACY_THREE_DIGIT_EXPONENTS,
|
| __acrt_rounding_mode::legacy,
|
| ptd);
|
|
|
| if (e != 0)
|
| {
|
| return ptd.get_errno().set(e);
|
| }
|
| }
|
|
|
|
|
|
|
| char* p = buffer;
|
| while (*p && *p != decimal_point)
|
| {
|
| ++p;
|
| }
|
|
|
| if (*p == '\0')
|
| {
|
| return 0;
|
| }
|
|
|
| ++p;
|
|
|
| while (*p && *p != 'e')
|
| {
|
| ++p;
|
| }
|
|
|
| char* stop = p;
|
| --p;
|
|
|
| while (*p == '0')
|
| {
|
| --p;
|
| }
|
|
|
| while ((*++p = *stop++) != '\0') { }
|
|
|
| return 0;
|
| }
|
|
|
| extern "C" errno_t __cdecl _gcvt_s(
|
| char* const buffer,
|
| size_t const buffer_count,
|
| double const value,
|
| int const precision
|
| )
|
| {
|
| __crt_cached_ptd_host ptd;
|
| return _gcvt_s_internal(buffer, buffer_count, value, precision, ptd);
|
| }
|
|
|
| extern "C" char* __cdecl _gcvt(
|
| double const value,
|
| int const precision,
|
| char* const buffer
|
| )
|
| {
|
| errno_t const e = _gcvt_s(buffer, _CRT_UNBOUNDED_BUFFER_SIZE, value, precision);
|
| if (e != 0)
|
| {
|
| return nullptr;
|
| }
|
|
|
| return buffer;
|
| }
|
|
|