ReactOS 0.4.16-dev-852-gcfcc8d8
gcvt.cpp
Go to the documentation of this file.
1//
2// gcvt.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// Defines the _gcvt functions, which convert a floating point value to a narrow
7// character string. It attempts to produce 'precision' significant digits in
8// the Fortran F format if possible, otherwise the E format. Trailing zeroes may
9// be suppressed. The _s-suffixed function returns zero on success; an error
10// code on failure. If the buffer is too small, that is an error.
11//
12#include <corecrt_internal.h>
17#include <locale.h>
18#include <stdlib.h>
19
20
21
23 char* const buffer,
24 size_t const buffer_count,
25 double const value,
26 int const precision,
27 __crt_cached_ptd_host& ptd
28 )
29{
34 // Additional validation will be performed in the fp_format functions.
35
36 char const decimal_point = *ptd.get_locale()->locinfo->lconv->decimal_point;
37
38 // We only call __acrt_fltout in order to parse the correct exponent value (strflt.decpt).
39 // Therefore, we don't want to generate any digits, so we pass a buffer size only large
40 // enough to hold the inf, nan, or ind string to prevent failure.
41
42 size_t const restricted_count = 7; // "1#SNAN" + 1 null terminator
43 char result_string[restricted_count];
44
45 _strflt strflt{};
46
48 reinterpret_cast<_CRT_DOUBLE const&>(value),
51 &strflt,
52 result_string,
53 restricted_count);
54
55 int const magnitude = strflt.decpt - 1;
56
57 // Output the result according to the Fortran G format as outlined in the
58 // Fortran language specification.
59 if (magnitude < -1 || magnitude > precision - 1)
60 {
61 // Ew.d where d = precision
62 char scratch_buffer[_CVTBUFSIZE + 1];
64 &value,
65 buffer,
67 scratch_buffer,
68 _countof(scratch_buffer),
69 'e',
70 precision - 1,
73 ptd);
74
75 if (e != 0)
76 {
77 return ptd.get_errno().set(e);
78 }
79 }
80 else
81 {
82 // Fw.d where d = precision-string->decpt
83 char scratch_buffer[_CVTBUFSIZE + 1];
85 &value,
86 buffer,
88 scratch_buffer,
89 _countof(scratch_buffer),
90 'f',
91 precision - strflt.decpt,
94 ptd);
95
96 if (e != 0)
97 {
98 return ptd.get_errno().set(e);
99 }
100 }
101
102 // Remove the trailing zeroes before the exponent; we don't need to check
103 // for buffer_count:
104 char* p = buffer;
105 while (*p && *p != decimal_point)
106 {
107 ++p;
108 }
109
110 if (*p == '\0')
111 {
112 return 0;
113 }
114
115 ++p;
116
117 while (*p && *p != 'e')
118 {
119 ++p;
120 }
121
122 char* stop = p;
123 --p;
124
125 while (*p == '0')
126 {
127 --p;
128 }
129
130 while ((*++p = *stop++) != '\0') { }
131
132 return 0;
133}
134
136 char* const buffer,
137 size_t const buffer_count,
138 double const value,
139 int const precision
140 )
141{
142 __crt_cached_ptd_host ptd;
144}
145
146extern "C" char* __cdecl _gcvt(
147 double const value,
148 int const precision,
149 char* const buffer
150 )
151{
153 if (e != 0)
154 {
155 return nullptr;
156 }
157
158 return buffer;
159}
#define EINVAL
Definition: acclib.h:90
#define ERANGE
Definition: acclib.h:92
#define __cdecl
Definition: accygwin.h:79
__acrt_has_trailing_digits __cdecl __acrt_fltout(_CRT_DOUBLE value, unsigned const precision, __acrt_precision_style const precision_style, STRFLT const flt, char *const result, size_t const result_count)
Definition: cfout.cpp:315
#define _CRT_UNBOUNDED_BUFFER_SIZE
#define _UCRT_VALIDATE_RETURN_ERRCODE(ptd, expr, errorcode)
#define _RESET_STRING(_String, _Size)
#define _CRT_INTERNAL_PRINTF_LEGACY_THREE_DIGIT_EXPONENTS
errno_t __cdecl __acrt_fp_format(double const *const value, char *const result_buffer, size_t const result_buffer_count, char *const scratch_buffer, size_t const scratch_buffer_count, int const format, int const precision, uint64_t const options, __acrt_rounding_mode rounding_mode, __crt_cached_ptd_host &ptd)
Definition: cvt.cpp:786
_In_ size_t const _In_ int _In_ bool const _In_ unsigned const _In_ __acrt_rounding_mode const _Inout_ __crt_cached_ptd_host & ptd
Definition: cvt.cpp:355
errno_t __cdecl _gcvt_s(char *const buffer, size_t const buffer_count, double const value, int const precision)
Definition: gcvt.cpp:135
char *__cdecl _gcvt(double const value, int const precision, char *const buffer)
Definition: gcvt.cpp:146
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)
Definition: gcvt.cpp:22
GLuint buffer
Definition: glext.h:5915
GLfloat GLfloat p
Definition: glext.h:8902
GLenum GLint GLint * precision
Definition: glext.h:7539
#define _CVTBUFSIZE
Definition: stdlib.h:1050
#define e
Definition: ke_i.h:82
#define _countof(array)
Definition: sndvol32.h:70
int errno_t
Definition: corecrt.h:615
Definition: pdh_main.c:96
size_t const buffer_count
Definition: xtoa.cpp:36
#define const
Definition: zconf.h:233