2011-04-20 14:42:16 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Piotr Caban for CodeWeavers
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2020-07-11 18:44:11 +02:00
|
|
|
#include "bnum.h"
|
|
|
|
|
2011-04-20 14:42:16 +02:00
|
|
|
#ifdef PRINTF_WIDE
|
2020-11-27 14:16:49 +01:00
|
|
|
#define APICHAR wchar_t
|
2011-04-20 14:42:16 +02:00
|
|
|
#define CONVCHAR char
|
|
|
|
#define FUNC_NAME(func) func ## _w
|
|
|
|
#else
|
|
|
|
#define APICHAR char
|
2020-11-27 14:16:49 +01:00
|
|
|
#define CONVCHAR wchar_t
|
2011-04-20 14:42:16 +02:00
|
|
|
#define FUNC_NAME(func) func ## _a
|
|
|
|
#endif
|
|
|
|
|
2011-04-20 14:42:48 +02:00
|
|
|
struct FUNC_NAME(_str_ctx) {
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t len;
|
2011-04-20 14:42:48 +02:00
|
|
|
APICHAR *buf;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int FUNC_NAME(puts_clbk_str)(void *ctx, int len, const APICHAR *str)
|
|
|
|
{
|
|
|
|
struct FUNC_NAME(_str_ctx) *out = ctx;
|
|
|
|
|
|
|
|
if(!out->buf)
|
|
|
|
return len;
|
|
|
|
|
|
|
|
if(out->len < len) {
|
2019-10-10 23:39:35 +02:00
|
|
|
memmove(out->buf, str, out->len*sizeof(APICHAR));
|
2011-04-20 14:42:48 +02:00
|
|
|
out->buf += out->len;
|
|
|
|
out->len = 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-10-10 23:39:35 +02:00
|
|
|
memmove(out->buf, str, len*sizeof(APICHAR));
|
2011-04-20 14:42:48 +02:00
|
|
|
out->buf += len;
|
2011-06-06 14:02:30 +02:00
|
|
|
out->len -= len;
|
2011-04-20 14:42:48 +02:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2011-04-20 14:42:16 +02:00
|
|
|
static inline const APICHAR* FUNC_NAME(pf_parse_int)(const APICHAR *fmt, int *val)
|
|
|
|
{
|
|
|
|
*val = 0;
|
|
|
|
|
2020-11-18 16:03:27 +01:00
|
|
|
while (*fmt >= '0' && *fmt <= '9') {
|
2011-04-20 14:42:16 +02:00
|
|
|
*val *= 10;
|
|
|
|
*val += *fmt++ - '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pf_fill: takes care of signs, alignment, zero and field padding */
|
|
|
|
static inline int FUNC_NAME(pf_fill)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
|
2020-07-14 15:33:33 +02:00
|
|
|
int len, pf_flags *flags, BOOL left)
|
2011-04-20 14:42:16 +02:00
|
|
|
{
|
|
|
|
int i, r = 0, written;
|
|
|
|
|
2020-06-08 17:20:46 +02:00
|
|
|
if(flags->Sign && !strchr("diaAeEfFgG", flags->Format))
|
2011-04-20 14:42:16 +02:00
|
|
|
flags->Sign = 0;
|
|
|
|
|
|
|
|
if(left && flags->Sign) {
|
2020-07-14 15:33:33 +02:00
|
|
|
APICHAR ch = flags->Sign;
|
2011-04-20 14:42:16 +02:00
|
|
|
flags->FieldLength--;
|
|
|
|
if(flags->PadZero)
|
2020-07-14 15:33:33 +02:00
|
|
|
r = pf_puts(puts_ctx, 1, &ch);
|
2011-04-20 14:42:16 +02:00
|
|
|
}
|
|
|
|
written = r;
|
|
|
|
|
|
|
|
if((!left && flags->LeftAlign) || (left && !flags->LeftAlign)) {
|
|
|
|
APICHAR ch;
|
|
|
|
|
|
|
|
if(left && flags->PadZero)
|
|
|
|
ch = '0';
|
|
|
|
else
|
|
|
|
ch = ' ';
|
|
|
|
|
|
|
|
for(i=0; i<flags->FieldLength-len && r>=0; i++) {
|
|
|
|
r = pf_puts(puts_ctx, 1, &ch);
|
|
|
|
written += r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(r>=0 && left && flags->Sign && !flags->PadZero) {
|
2020-07-14 15:33:33 +02:00
|
|
|
APICHAR ch = flags->Sign;
|
|
|
|
r = pf_puts(puts_ctx, 1, &ch);
|
2011-04-20 14:42:16 +02:00
|
|
|
written += r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r>=0 ? written : r;
|
|
|
|
}
|
|
|
|
|
2019-02-14 18:27:59 +01:00
|
|
|
#ifndef PRINTF_HELPERS
|
|
|
|
#define PRINTF_HELPERS
|
2020-11-27 14:16:49 +01:00
|
|
|
static inline int wcstombs_len(char *mbstr, const wchar_t *wcstr,
|
2020-11-26 19:19:18 +01:00
|
|
|
int len, _locale_t locale)
|
2019-02-14 18:27:59 +01:00
|
|
|
{
|
2020-11-27 14:17:01 +01:00
|
|
|
char buf[MB_LEN_MAX];
|
2019-02-14 18:27:59 +01:00
|
|
|
int i, r, mblen = 0;
|
|
|
|
|
|
|
|
for(i=0; i<len; i++) {
|
2020-12-03 19:10:54 +01:00
|
|
|
r = _wctomb_l(mbstr ? mbstr+mblen : buf, wcstr[i], locale);
|
2019-02-14 18:27:59 +01:00
|
|
|
if(r < 0) return r;
|
|
|
|
mblen += r;
|
|
|
|
}
|
|
|
|
return mblen;
|
|
|
|
}
|
|
|
|
|
2020-11-27 14:16:49 +01:00
|
|
|
static inline int mbstowcs_len(wchar_t *wcstr, const char *mbstr,
|
2020-11-26 19:19:18 +01:00
|
|
|
int len, _locale_t locale)
|
2019-02-14 18:27:59 +01:00
|
|
|
{
|
|
|
|
int i, r, wlen = 0;
|
|
|
|
WCHAR buf;
|
|
|
|
|
|
|
|
for(i=0; i<len; wlen++) {
|
2020-12-02 00:53:51 +01:00
|
|
|
r = _mbtowc_l(wcstr ? wcstr+wlen : &buf, mbstr+i, len-i, locale);
|
2019-02-14 18:27:59 +01:00
|
|
|
if(r < 0) return r;
|
|
|
|
i += r ? r : 1;
|
|
|
|
}
|
|
|
|
return wlen;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-04-20 14:42:16 +02:00
|
|
|
static inline int FUNC_NAME(pf_output_wstr)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
|
2020-11-27 14:16:49 +01:00
|
|
|
const wchar_t *str, int len, _locale_t locale)
|
2011-04-20 14:42:16 +02:00
|
|
|
{
|
|
|
|
#ifdef PRINTF_WIDE
|
|
|
|
return pf_puts(puts_ctx, len, str);
|
|
|
|
#else
|
|
|
|
LPSTR out;
|
2019-02-14 18:27:59 +01:00
|
|
|
int len_a = wcstombs_len(NULL, str, len, locale);
|
|
|
|
if(len_a < 0)
|
|
|
|
return -1;
|
2011-04-20 14:42:16 +02:00
|
|
|
|
|
|
|
out = HeapAlloc(GetProcessHeap(), 0, len_a);
|
|
|
|
if(!out)
|
|
|
|
return -1;
|
|
|
|
|
2019-02-14 18:27:59 +01:00
|
|
|
wcstombs_len(out, str, len, locale);
|
2011-04-20 14:42:16 +02:00
|
|
|
len = pf_puts(puts_ctx, len_a, out);
|
|
|
|
HeapFree(GetProcessHeap(), 0, out);
|
|
|
|
return len;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int FUNC_NAME(pf_output_str)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
|
2020-11-26 19:19:18 +01:00
|
|
|
const char *str, int len, _locale_t locale)
|
2011-04-20 14:42:16 +02:00
|
|
|
{
|
|
|
|
#ifdef PRINTF_WIDE
|
|
|
|
LPWSTR out;
|
2019-02-14 18:27:59 +01:00
|
|
|
int len_w = mbstowcs_len(NULL, str, len, locale);
|
|
|
|
if(len_w < 0)
|
|
|
|
return -1;
|
2011-04-20 14:42:16 +02:00
|
|
|
|
|
|
|
out = HeapAlloc(GetProcessHeap(), 0, len_w*sizeof(WCHAR));
|
|
|
|
if(!out)
|
|
|
|
return -1;
|
|
|
|
|
2019-02-14 18:27:59 +01:00
|
|
|
mbstowcs_len(out, str, len, locale);
|
2011-04-20 14:42:16 +02:00
|
|
|
len = pf_puts(puts_ctx, len_w, out);
|
|
|
|
HeapFree(GetProcessHeap(), 0, out);
|
|
|
|
return len;
|
|
|
|
#else
|
|
|
|
return pf_puts(puts_ctx, len, str);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int FUNC_NAME(pf_output_format_wstr)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
|
2020-11-27 14:16:49 +01:00
|
|
|
const wchar_t *str, int len, pf_flags *flags, _locale_t locale)
|
2011-04-20 14:42:16 +02:00
|
|
|
{
|
|
|
|
int r, ret;
|
|
|
|
|
2017-08-31 19:22:55 +02:00
|
|
|
if(len < 0) {
|
|
|
|
/* Do not search past the length specified by the precision. */
|
|
|
|
if(flags->Precision>=0)
|
2020-12-03 19:10:54 +01:00
|
|
|
len = wcsnlen(str, flags->Precision);
|
2017-08-31 19:22:55 +02:00
|
|
|
else
|
2020-12-03 19:10:54 +01:00
|
|
|
len = wcslen(str);
|
2017-08-31 19:22:55 +02:00
|
|
|
}
|
2011-04-20 14:42:16 +02:00
|
|
|
|
|
|
|
if(flags->Precision>=0 && flags->Precision<len)
|
|
|
|
len = flags->Precision;
|
|
|
|
|
|
|
|
r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, TRUE);
|
|
|
|
ret = r;
|
|
|
|
if(r >= 0) {
|
2019-02-14 18:27:59 +01:00
|
|
|
r = FUNC_NAME(pf_output_wstr)(pf_puts, puts_ctx, str, len, locale);
|
2011-04-20 14:42:16 +02:00
|
|
|
ret += r;
|
|
|
|
}
|
|
|
|
if(r >= 0) {
|
|
|
|
r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, FALSE);
|
|
|
|
ret += r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r>=0 ? ret : r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int FUNC_NAME(pf_output_format_str)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
|
2020-11-26 19:19:18 +01:00
|
|
|
const char *str, int len, pf_flags *flags, _locale_t locale)
|
2011-04-20 14:42:16 +02:00
|
|
|
{
|
|
|
|
int r, ret;
|
|
|
|
|
2017-08-31 19:22:55 +02:00
|
|
|
if(len < 0) {
|
|
|
|
/* Do not search past the length specified by the precision. */
|
|
|
|
if(flags->Precision>=0)
|
2020-12-02 00:54:09 +01:00
|
|
|
len = strnlen(str, flags->Precision);
|
2017-08-31 19:22:55 +02:00
|
|
|
else
|
|
|
|
len = strlen(str);
|
|
|
|
}
|
2011-04-20 14:42:16 +02:00
|
|
|
|
|
|
|
if(flags->Precision>=0 && flags->Precision<len)
|
|
|
|
len = flags->Precision;
|
|
|
|
|
|
|
|
r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, TRUE);
|
|
|
|
ret = r;
|
|
|
|
if(r >= 0) {
|
2019-02-14 18:27:59 +01:00
|
|
|
r = FUNC_NAME(pf_output_str)(pf_puts, puts_ctx, str, len, locale);
|
2011-04-20 14:42:16 +02:00
|
|
|
ret += r;
|
|
|
|
}
|
|
|
|
if(r >= 0) {
|
|
|
|
r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, FALSE);
|
|
|
|
ret += r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r>=0 ? ret : r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int FUNC_NAME(pf_handle_string)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
|
2020-11-26 19:19:18 +01:00
|
|
|
const void *str, int len, pf_flags *flags, _locale_t locale, BOOL legacy_wide)
|
2011-04-20 14:42:16 +02:00
|
|
|
{
|
2020-11-27 14:16:49 +01:00
|
|
|
BOOL api_is_wide = sizeof(APICHAR) == sizeof(wchar_t);
|
2016-08-08 14:43:04 +02:00
|
|
|
BOOL complement_is_narrow = legacy_wide ? api_is_wide : FALSE;
|
2011-04-20 14:42:16 +02:00
|
|
|
#ifdef PRINTF_WIDE
|
|
|
|
|
|
|
|
if(!str)
|
2020-11-27 14:16:53 +01:00
|
|
|
return FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, L"(null)", 6, flags, locale);
|
2011-04-20 14:42:16 +02:00
|
|
|
#else
|
|
|
|
if(!str)
|
2019-02-14 18:27:59 +01:00
|
|
|
return FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, "(null)", 6, flags, locale);
|
2011-04-20 14:42:16 +02:00
|
|
|
#endif
|
|
|
|
|
2020-07-14 15:33:33 +02:00
|
|
|
if((flags->NaturalString && api_is_wide) || flags->WideString || flags->IntegerLength == LEN_LONG)
|
2019-02-14 18:27:59 +01:00
|
|
|
return FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, str, len, flags, locale);
|
2020-07-14 15:33:33 +02:00
|
|
|
if((flags->NaturalString && !api_is_wide) || flags->IntegerLength == LEN_SHORT)
|
2019-02-14 18:27:59 +01:00
|
|
|
return FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, str, len, flags, locale);
|
2011-04-20 14:42:16 +02:00
|
|
|
|
2015-11-03 19:40:38 +01:00
|
|
|
if((flags->Format=='S' || flags->Format=='C') == complement_is_narrow)
|
2019-02-14 18:27:59 +01:00
|
|
|
return FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, str, len, flags, locale);
|
2011-04-20 14:42:16 +02:00
|
|
|
else
|
2019-02-14 18:27:59 +01:00
|
|
|
return FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, str, len, flags, locale);
|
2011-04-20 14:42:16 +02:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:20:46 +02:00
|
|
|
static inline int FUNC_NAME(pf_output_special_fp)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
|
2020-11-26 19:19:18 +01:00
|
|
|
double v, pf_flags *flags, _locale_t locale,
|
2020-06-08 17:20:46 +02:00
|
|
|
BOOL legacy_msvcrt_compat, BOOL three_digit_exp)
|
|
|
|
{
|
|
|
|
APICHAR pfx[16], sfx[8], *p;
|
|
|
|
int len = 0, r, frac_len, pfx_len, sfx_len;
|
|
|
|
|
|
|
|
if(!legacy_msvcrt_compat) {
|
|
|
|
const char *str;
|
|
|
|
|
|
|
|
if(isinf(v)) {
|
|
|
|
if(strchr("AEFG", flags->Format)) str = "INF";
|
|
|
|
else str = "inf";
|
|
|
|
}else {
|
|
|
|
if(strchr("AEFG", flags->Format)) str = (flags->Sign == '-' ? "NAN(IND)" : "NAN");
|
|
|
|
else str = (flags->Sign == '-' ? "nan(ind)" : "nan");
|
|
|
|
}
|
|
|
|
|
|
|
|
flags->Precision = -1;
|
2020-07-14 15:33:33 +02:00
|
|
|
flags->PadZero = FALSE;
|
2020-06-08 17:20:46 +02:00
|
|
|
return FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, str, -1, flags, locale);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* workaround a bug in native implementation */
|
|
|
|
if(flags->Format=='g' || flags->Format=='G')
|
|
|
|
flags->Precision--;
|
|
|
|
|
|
|
|
p = pfx;
|
|
|
|
if(flags->PadZero && (flags->Format=='a' || flags->Format=='A')) {
|
|
|
|
if (flags->Sign) *p++ = flags->Sign;
|
|
|
|
*p++ = '0';
|
|
|
|
*p++ = (flags->Format=='a' ? 'x' : 'X');
|
|
|
|
r = pf_puts(puts_ctx, p-pfx, pfx);
|
|
|
|
if(r < 0) return r;
|
|
|
|
len += r;
|
|
|
|
|
|
|
|
flags->FieldLength -= p-pfx;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = pfx;
|
|
|
|
if(!flags->PadZero && (flags->Format=='a' || flags->Format=='A')) {
|
|
|
|
*p++ = '0';
|
|
|
|
*p++ = (flags->Format=='a' ? 'x' : 'X');
|
|
|
|
}
|
|
|
|
|
|
|
|
*p++ = '1';
|
|
|
|
*p++ = *(locale ? locale->locinfo : get_locinfo())->lconv->decimal_point;
|
|
|
|
*p++ = '#';
|
|
|
|
frac_len = 1;
|
|
|
|
|
|
|
|
if(isinf(v)) {
|
|
|
|
*p++ = 'I';
|
|
|
|
*p++ = 'N';
|
|
|
|
*p++ = 'F';
|
|
|
|
frac_len += 3;
|
|
|
|
}else if(flags->Sign == '-') {
|
|
|
|
*p++ = 'I';
|
|
|
|
*p++ = 'N';
|
|
|
|
*p++ = 'D';
|
|
|
|
frac_len += 3;
|
|
|
|
}else {
|
|
|
|
*p++ = 'Q';
|
|
|
|
*p++ = 'N';
|
|
|
|
*p++ = 'A';
|
|
|
|
*p++ = 'N';
|
|
|
|
frac_len += 4;
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
pfx_len = p - pfx;
|
|
|
|
|
|
|
|
if(len) flags->Sign = 0;
|
|
|
|
|
|
|
|
if(flags->Precision>=0 && flags->Precision<frac_len)
|
|
|
|
p[flags->Precision - frac_len - 1]++;
|
|
|
|
|
|
|
|
p = sfx;
|
|
|
|
if(strchr("aAeE", flags->Format)) {
|
|
|
|
if(flags->Format == 'a') *p++ = 'p';
|
|
|
|
else if(flags->Format == 'A') *p++ = 'P';
|
|
|
|
else if(flags->Format == 'e') *p++ = 'e';
|
|
|
|
else *p++ = 'E';
|
|
|
|
|
|
|
|
*p++ = '+';
|
|
|
|
*p++ = '0';
|
|
|
|
|
|
|
|
if(flags->Format == 'e' || flags->Format == 'E') {
|
|
|
|
*p++ = '0';
|
|
|
|
if(three_digit_exp) *p++ = '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
|
|
|
|
if(!flags->Alternate && (flags->Format == 'g' || flags->Format == 'G')) sfx_len = frac_len;
|
|
|
|
else sfx_len = flags->Precision;
|
|
|
|
|
|
|
|
if(sfx_len == -1) {
|
|
|
|
if(strchr("fFeE", flags->Format)) sfx_len = 6;
|
|
|
|
else if(flags->Format == 'a' || flags->Format == 'A') sfx_len = 13;
|
|
|
|
}
|
|
|
|
sfx_len += p - sfx - frac_len;
|
|
|
|
|
|
|
|
if(sfx_len > 0) flags->FieldLength -= sfx_len;
|
|
|
|
if(flags->Precision >= 0) {
|
|
|
|
if(!flags->Precision) flags->Precision--;
|
|
|
|
flags->Precision += pfx_len - frac_len;
|
|
|
|
}
|
|
|
|
#ifdef PRINTF_WIDE
|
|
|
|
r = FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, pfx, -1, flags, locale);
|
|
|
|
#else
|
|
|
|
r = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, pfx, -1, flags, locale);
|
|
|
|
#endif
|
|
|
|
if(r < 0) return r;
|
|
|
|
len += r;
|
|
|
|
|
|
|
|
flags->FieldLength = sfx_len;
|
2020-07-14 15:33:33 +02:00
|
|
|
flags->PadZero = TRUE;
|
2020-06-08 17:20:46 +02:00
|
|
|
flags->Precision = -1;
|
|
|
|
flags->Sign = 0;
|
|
|
|
#ifdef PRINTF_WIDE
|
|
|
|
r = FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, sfx, -1, flags, locale);
|
|
|
|
#else
|
|
|
|
r = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, sfx, -1, flags, locale);
|
|
|
|
#endif
|
|
|
|
if(r < 0) return r;
|
|
|
|
len += r;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:20:51 +02:00
|
|
|
static inline int FUNC_NAME(pf_output_hex_fp)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
|
2020-11-26 19:19:18 +01:00
|
|
|
double v, pf_flags *flags, _locale_t locale, BOOL standard_rounding)
|
2020-06-08 17:20:51 +02:00
|
|
|
{
|
|
|
|
const APICHAR digits[2][16] = {
|
|
|
|
{ '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' },
|
|
|
|
{ '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' }
|
|
|
|
};
|
|
|
|
|
2020-07-08 19:47:40 +02:00
|
|
|
APICHAR pfx[4+MANT_BITS/4+1], sfx[8], *p;
|
2020-06-08 17:20:51 +02:00
|
|
|
ULONGLONG mant;
|
|
|
|
int len = 0, sfx_len = 0, r, exp;
|
|
|
|
|
|
|
|
mant = (*(ULONGLONG*)&v) << 1;
|
|
|
|
exp = (mant >> MANT_BITS);
|
|
|
|
exp -= (1 << (EXP_BITS - 1)) - 1;
|
|
|
|
mant = (mant << EXP_BITS) >> (EXP_BITS+1);
|
|
|
|
|
|
|
|
p = pfx;
|
|
|
|
if(flags->PadZero) {
|
|
|
|
if(flags->Sign) *p++ = flags->Sign;
|
|
|
|
*p++ = '0';
|
|
|
|
*p++ = (flags->Format=='a' ? 'x' : 'X');
|
|
|
|
r = pf_puts(puts_ctx, p-pfx, pfx);
|
|
|
|
if(r < 0) return r;
|
|
|
|
len += r;
|
|
|
|
|
|
|
|
flags->FieldLength -= p-pfx;
|
|
|
|
flags->Sign = 0;
|
|
|
|
p = pfx;
|
|
|
|
}else {
|
|
|
|
*p++ = '0';
|
|
|
|
*p++ = (flags->Format=='a' ? 'x' : 'X');
|
|
|
|
}
|
|
|
|
if(exp == -(1 << (EXP_BITS-1))+1) {
|
|
|
|
if(!mant) exp = 0;
|
|
|
|
else exp++;
|
|
|
|
*p++ = '0';
|
|
|
|
}else {
|
|
|
|
*p++ = '1';
|
|
|
|
}
|
|
|
|
*p++ = *(locale ? locale->locinfo : get_locinfo())->lconv->decimal_point;
|
|
|
|
for(r=MANT_BITS/4-1; r>=0; r--) {
|
|
|
|
p[r] = digits[flags->Format == 'A'][mant & 15];
|
|
|
|
mant >>= 4;
|
|
|
|
}
|
|
|
|
if(!flags->Precision) {
|
2020-08-06 13:18:07 +02:00
|
|
|
if(p[0] >= '8') p[-2]++;
|
2020-06-08 17:20:51 +02:00
|
|
|
if(!flags->Alternate) p--;
|
|
|
|
}else if(flags->Precision>0 && flags->Precision<MANT_BITS/4) {
|
2020-08-06 13:18:14 +02:00
|
|
|
BOOL round_up = FALSE;
|
|
|
|
|
|
|
|
if(!standard_rounding) round_up = (p[flags->Precision] >= '8');
|
|
|
|
else if(p[flags->Precision] > '8') round_up = TRUE;
|
|
|
|
else if(p[flags->Precision] == '8') {
|
|
|
|
for(r = flags->Precision+1; r<MANT_BITS/4; r++) {
|
|
|
|
if(p[r] != '0') {
|
|
|
|
round_up = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!round_up) {
|
|
|
|
if(p[flags->Precision-1] <= '9') round_up = (p[flags->Precision-1] - '0') & 1;
|
|
|
|
else if(p[flags->Precision-1] <= 'F') round_up = (p[flags->Precision-1] - 'A') & 1;
|
|
|
|
else round_up = (p[flags->Precision-1] - 'a') & 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:20:51 +02:00
|
|
|
for(r=flags->Precision-1; r>=0 && round_up; r--) {
|
|
|
|
round_up = FALSE;
|
|
|
|
if(p[r]=='f' || p[r]=='F') {
|
|
|
|
p[r] = '0';
|
|
|
|
round_up = TRUE;
|
|
|
|
}else if(p[r] == '9') {
|
|
|
|
p[r] = (flags->Format == 'a' ? 'a' : 'A');
|
|
|
|
}else {
|
|
|
|
p[r]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(round_up) p[-2]++;
|
|
|
|
p += flags->Precision;
|
|
|
|
}else {
|
|
|
|
p += MANT_BITS/4;
|
|
|
|
if(flags->Precision > MANT_BITS/4) sfx_len += flags->Precision - MANT_BITS/4;
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
|
|
|
|
p = sfx;
|
|
|
|
*p++ = (flags->Format == 'a' ? 'p' : 'P');
|
|
|
|
if(exp < 0) {
|
|
|
|
*p++ = '-';
|
|
|
|
exp = -exp;
|
|
|
|
}else {
|
|
|
|
*p++ = '+';
|
|
|
|
}
|
|
|
|
for(r=3; r>=0; r--) {
|
|
|
|
p[r] = exp%10 + '0';
|
|
|
|
exp /= 10;
|
|
|
|
if(!exp) break;
|
|
|
|
}
|
|
|
|
for(exp=0; exp<4-r; exp++)
|
|
|
|
p[exp] = p[exp+r];
|
|
|
|
p += exp;
|
|
|
|
*p = 0;
|
|
|
|
sfx_len += p - sfx;
|
|
|
|
|
|
|
|
flags->FieldLength -= sfx_len;
|
|
|
|
flags->Precision = -1;
|
|
|
|
#ifdef PRINTF_WIDE
|
|
|
|
r = FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, pfx, -1, flags, locale);
|
|
|
|
#else
|
|
|
|
r = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, pfx, -1, flags, locale);
|
|
|
|
#endif
|
|
|
|
if(r < 0) return r;
|
|
|
|
len += r;
|
|
|
|
|
|
|
|
flags->FieldLength = sfx_len;
|
2020-07-14 15:33:33 +02:00
|
|
|
flags->PadZero = TRUE;
|
2020-06-08 17:20:51 +02:00
|
|
|
flags->Sign = 0;
|
|
|
|
#ifdef PRINTF_WIDE
|
|
|
|
r = FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, sfx, -1, flags, locale);
|
|
|
|
#else
|
|
|
|
r = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, sfx, -1, flags, locale);
|
|
|
|
#endif
|
|
|
|
if(r < 0) return r;
|
|
|
|
len += r;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2011-04-20 14:42:16 +02:00
|
|
|
/* pf_integer_conv: prints x to buf, including alternate formats and
|
|
|
|
additional precision digits, but not field characters or the sign */
|
2020-07-15 14:05:32 +02:00
|
|
|
static inline void FUNC_NAME(pf_integer_conv)(APICHAR *buf, pf_flags *flags, LONGLONG x)
|
2011-04-20 14:42:16 +02:00
|
|
|
{
|
|
|
|
unsigned int base;
|
|
|
|
const char *digits;
|
|
|
|
int i, j, k;
|
|
|
|
|
|
|
|
if(flags->Format == 'o')
|
|
|
|
base = 8;
|
|
|
|
else if(flags->Format=='x' || flags->Format=='X')
|
|
|
|
base = 16;
|
|
|
|
else
|
|
|
|
base = 10;
|
|
|
|
|
|
|
|
if(flags->Format == 'X')
|
|
|
|
digits = "0123456789ABCDEFX";
|
|
|
|
else
|
|
|
|
digits = "0123456789abcdefx";
|
|
|
|
|
|
|
|
if(x<0 && (flags->Format=='d' || flags->Format=='i')) {
|
|
|
|
x = -x;
|
|
|
|
flags->Sign = '-';
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
2014-03-20 16:20:26 +01:00
|
|
|
if(x == 0) {
|
2020-07-14 15:33:33 +02:00
|
|
|
flags->Alternate = FALSE;
|
2014-03-20 16:20:26 +01:00
|
|
|
if(flags->Precision)
|
|
|
|
buf[i++] = '0';
|
|
|
|
} else {
|
2011-04-20 14:42:16 +02:00
|
|
|
while(x != 0) {
|
|
|
|
j = (ULONGLONG)x%base;
|
|
|
|
x = (ULONGLONG)x/base;
|
2011-04-27 12:09:27 +02:00
|
|
|
buf[i++] = digits[j];
|
2011-04-20 14:42:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
k = flags->Precision-i;
|
|
|
|
while(k-- > 0)
|
2011-04-27 12:09:27 +02:00
|
|
|
buf[i++] = '0';
|
2011-04-20 14:42:16 +02:00
|
|
|
if(flags->Alternate) {
|
|
|
|
if(base == 16) {
|
2011-04-27 12:09:27 +02:00
|
|
|
buf[i++] = digits[16];
|
|
|
|
buf[i++] = '0';
|
|
|
|
} else if(base==8 && buf[i-1]!='0')
|
|
|
|
buf[i++] = '0';
|
2011-04-20 14:42:16 +02:00
|
|
|
}
|
|
|
|
|
2011-04-27 12:09:51 +02:00
|
|
|
/* Adjust precision so pf_fill won't truncate the number later */
|
|
|
|
flags->Precision = i;
|
|
|
|
|
2011-04-27 12:09:27 +02:00
|
|
|
buf[i] = '\0';
|
2011-04-20 14:42:16 +02:00
|
|
|
j = 0;
|
2011-04-27 12:09:51 +02:00
|
|
|
while(--i > j) {
|
|
|
|
APICHAR tmp = buf[j];
|
2011-04-27 12:09:27 +02:00
|
|
|
buf[j] = buf[i];
|
|
|
|
buf[i] = tmp;
|
|
|
|
j++;
|
|
|
|
}
|
2011-04-20 14:42:16 +02:00
|
|
|
}
|
|
|
|
|
2020-07-11 18:44:11 +02:00
|
|
|
static inline int FUNC_NAME(pf_output_fp)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
|
2020-11-26 19:19:18 +01:00
|
|
|
double v, pf_flags *flags, _locale_t locale, BOOL three_digit_exp,
|
2020-08-06 13:18:14 +02:00
|
|
|
BOOL standard_rounding)
|
2011-04-20 14:42:16 +02:00
|
|
|
{
|
2020-07-11 18:44:11 +02:00
|
|
|
int e2, e10 = 0, round_pos, round_limb, radix_pos, first_limb_len, i, len, r, ret;
|
2020-07-23 15:38:25 +02:00
|
|
|
BYTE bnum_data[FIELD_OFFSET(struct bnum, data[BNUM_PREC64])];
|
|
|
|
struct bnum *b = (struct bnum*)bnum_data;
|
2020-07-11 18:44:11 +02:00
|
|
|
APICHAR buf[LIMB_DIGITS + 1];
|
2020-08-06 13:18:14 +02:00
|
|
|
BOOL trim_tail = FALSE, round_up = FALSE;
|
2020-07-14 15:33:33 +02:00
|
|
|
pf_flags f;
|
2020-07-11 18:44:11 +02:00
|
|
|
int limb_len, prec;
|
|
|
|
ULONGLONG m;
|
|
|
|
DWORD l;
|
|
|
|
|
|
|
|
if(flags->Precision == -1)
|
|
|
|
flags->Precision = 6;
|
|
|
|
|
2020-12-03 19:10:57 +01:00
|
|
|
v = frexp(v, &e2);
|
2020-07-11 18:44:11 +02:00
|
|
|
if(v) {
|
|
|
|
m = (ULONGLONG)1 << (MANT_BITS - 1);
|
|
|
|
m |= (*(ULONGLONG*)&v & (((ULONGLONG)1 << (MANT_BITS - 1)) - 1));
|
2020-07-23 15:38:25 +02:00
|
|
|
b->b = 0;
|
|
|
|
b->e = 2;
|
|
|
|
b->size = BNUM_PREC64;
|
|
|
|
b->data[0] = m % LIMB_MAX;
|
|
|
|
b->data[1] = m / LIMB_MAX;
|
2020-07-11 18:44:11 +02:00
|
|
|
e2 -= MANT_BITS;
|
|
|
|
|
|
|
|
while(e2 > 0) {
|
|
|
|
int shift = e2 > 29 ? 29 : e2;
|
2020-07-23 15:38:25 +02:00
|
|
|
if(bnum_lshift(b, shift)) e10 += LIMB_DIGITS;
|
2020-07-11 18:44:11 +02:00
|
|
|
e2 -= shift;
|
|
|
|
}
|
|
|
|
while(e2 < 0) {
|
|
|
|
int shift = -e2 > 9 ? 9 : -e2;
|
2020-07-23 15:38:25 +02:00
|
|
|
if(bnum_rshift(b, shift)) e10 -= LIMB_DIGITS;
|
2020-07-11 18:44:11 +02:00
|
|
|
e2 += shift;
|
|
|
|
}
|
|
|
|
} else {
|
2020-07-23 15:38:25 +02:00
|
|
|
b->b = 0;
|
|
|
|
b->e = 1;
|
|
|
|
b->size = BNUM_PREC64;
|
|
|
|
b->data[0] = 0;
|
2020-07-11 18:44:11 +02:00
|
|
|
e10 = -LIMB_DIGITS;
|
|
|
|
}
|
2011-04-20 14:42:16 +02:00
|
|
|
|
2020-07-23 15:38:25 +02:00
|
|
|
if(!b->data[bnum_idx(b, b->e-1)])
|
2020-07-11 18:44:11 +02:00
|
|
|
first_limb_len = 1;
|
|
|
|
else
|
2020-12-03 19:10:57 +01:00
|
|
|
first_limb_len = floor(log10(b->data[bnum_idx(b, b->e - 1)])) + 1;
|
2020-07-11 18:44:11 +02:00
|
|
|
radix_pos = first_limb_len + LIMB_DIGITS + e10;
|
|
|
|
|
|
|
|
round_pos = flags->Precision;
|
|
|
|
if(flags->Format=='f' || flags->Format=='F')
|
|
|
|
round_pos += radix_pos;
|
|
|
|
else if(!flags->Precision || flags->Format=='e' || flags->Format=='E')
|
|
|
|
round_pos++;
|
|
|
|
if (round_pos <= first_limb_len)
|
2020-07-23 15:38:25 +02:00
|
|
|
round_limb = b->e + (first_limb_len - round_pos) / LIMB_DIGITS - 1;
|
2020-07-11 18:44:11 +02:00
|
|
|
else
|
2020-07-23 15:38:25 +02:00
|
|
|
round_limb = b->e - (round_pos - first_limb_len - 1) / LIMB_DIGITS - 2;
|
2011-04-20 14:42:16 +02:00
|
|
|
|
2020-07-23 15:38:25 +02:00
|
|
|
if (b->b<=round_limb && round_limb<b->e) {
|
2020-07-11 18:44:11 +02:00
|
|
|
if (round_pos <= first_limb_len) {
|
|
|
|
round_pos = first_limb_len - round_pos;
|
|
|
|
} else {
|
|
|
|
round_pos = LIMB_DIGITS - (round_pos - first_limb_len) % LIMB_DIGITS;
|
|
|
|
if (round_pos == LIMB_DIGITS) round_pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (round_pos) {
|
2020-07-23 15:38:25 +02:00
|
|
|
l = b->data[bnum_idx(b, round_limb)] % p10s[round_pos];
|
|
|
|
b->data[bnum_idx(b, round_limb)] -= l;
|
2020-08-06 13:18:14 +02:00
|
|
|
if(!standard_rounding) round_up = (2*l >= p10s[round_pos]);
|
|
|
|
else if(2*l > p10s[round_pos]) round_up = TRUE;
|
|
|
|
else if(2*l == p10s[round_pos]) {
|
|
|
|
for(r = round_limb-1; r >= b->b; r--) {
|
|
|
|
if(b->data[bnum_idx(b, r)]) {
|
|
|
|
round_up = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!round_up) round_up = b->data[bnum_idx(b, round_limb)] / p10s[round_pos] & 1;
|
|
|
|
}
|
2020-07-23 15:38:25 +02:00
|
|
|
} else if(round_limb - 1 >= b->b) {
|
2020-08-06 13:18:14 +02:00
|
|
|
if(!standard_rounding) round_up = (2*b->data[bnum_idx(b, round_limb-1)] >= LIMB_MAX);
|
|
|
|
else if(2*b->data[bnum_idx(b, round_limb-1)] > LIMB_MAX) round_up = TRUE;
|
|
|
|
else if(2*b->data[bnum_idx(b, round_limb-1)] == LIMB_MAX) {
|
|
|
|
for(r = round_limb-2; r >= b->b; r--) {
|
|
|
|
if(b->data[bnum_idx(b, r)]) {
|
|
|
|
round_up = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!round_up) round_up = b->data[bnum_idx(b, round_limb)] & 1;
|
|
|
|
}
|
2020-07-11 18:44:11 +02:00
|
|
|
}
|
2020-07-23 15:38:25 +02:00
|
|
|
b->b = round_limb;
|
2020-07-11 18:44:11 +02:00
|
|
|
|
|
|
|
if(round_up) {
|
2020-07-23 15:38:25 +02:00
|
|
|
b->data[bnum_idx(b, b->b)] += p10s[round_pos];
|
|
|
|
for(i = b->b; i < b->e; i++) {
|
|
|
|
if(b->data[bnum_idx(b, i)] < LIMB_MAX) break;
|
2020-07-11 18:44:11 +02:00
|
|
|
|
2020-07-23 15:38:25 +02:00
|
|
|
b->data[bnum_idx(b, i)] -= LIMB_MAX;
|
|
|
|
if(i+1 < b->e) b->data[bnum_idx(b, i+1)]++;
|
|
|
|
else b->data[bnum_idx(b, i+1)] = 1;
|
2020-07-11 18:44:11 +02:00
|
|
|
}
|
2020-07-23 15:38:25 +02:00
|
|
|
if(i == b->e-1) {
|
|
|
|
if(!b->data[bnum_idx(b, b->e-1)])
|
2020-07-11 18:44:11 +02:00
|
|
|
i = 1;
|
|
|
|
else
|
2020-12-03 19:10:57 +01:00
|
|
|
i = floor(log10(b->data[bnum_idx(b, b->e-1)])) + 1;
|
2020-07-11 18:44:11 +02:00
|
|
|
if(i != first_limb_len) {
|
|
|
|
first_limb_len = i;
|
|
|
|
radix_pos++;
|
2021-07-10 20:38:16 +02:00
|
|
|
|
|
|
|
round_pos++;
|
|
|
|
if (round_pos == LIMB_DIGITS)
|
|
|
|
{
|
|
|
|
round_pos = 0;
|
|
|
|
round_limb++;
|
|
|
|
}
|
2020-07-11 18:44:11 +02:00
|
|
|
}
|
2020-07-23 15:38:25 +02:00
|
|
|
} else if(i == b->e) {
|
2020-07-11 18:44:11 +02:00
|
|
|
first_limb_len = 1;
|
|
|
|
radix_pos++;
|
2020-07-23 15:38:25 +02:00
|
|
|
b->e++;
|
2021-07-10 20:38:16 +02:00
|
|
|
|
|
|
|
round_pos++;
|
|
|
|
if (round_pos == LIMB_DIGITS)
|
|
|
|
{
|
|
|
|
round_pos = 0;
|
|
|
|
round_limb++;
|
|
|
|
}
|
2020-07-11 18:44:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-23 15:38:25 +02:00
|
|
|
else if(b->e <= round_limb) { /* got 0 or 1 after rounding */
|
2020-08-06 13:18:14 +02:00
|
|
|
if(b->e == round_limb) {
|
|
|
|
if(!standard_rounding) round_up = b->data[bnum_idx(b, b->e-1)] >= LIMB_MAX/2;
|
|
|
|
else if(b->data[bnum_idx(b, b->e-1)] > LIMB_MAX/2) round_up = TRUE;
|
|
|
|
else if(b->data[bnum_idx(b, b->e-1)] == LIMB_MAX/2) {
|
|
|
|
for(r = b->e-2; r >= b->b; r--) {
|
|
|
|
if(b->data[bnum_idx(b, r)]) {
|
|
|
|
round_up = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b->data[bnum_idx(b, round_limb)] = round_up;
|
2020-07-23 15:38:25 +02:00
|
|
|
b->b = round_limb;
|
|
|
|
b->e = b->b + 1;
|
2020-07-11 18:44:11 +02:00
|
|
|
first_limb_len = 1;
|
|
|
|
radix_pos++;
|
|
|
|
}
|
2013-03-27 10:39:30 +01:00
|
|
|
|
2020-07-11 18:44:11 +02:00
|
|
|
if(flags->Format=='g' || flags->Format=='G') {
|
|
|
|
trim_tail = TRUE;
|
|
|
|
|
2020-08-06 13:17:25 +02:00
|
|
|
if(radix_pos>=-3 && radix_pos<=flags->Precision) {
|
2020-07-11 18:44:11 +02:00
|
|
|
flags->Format -= 1;
|
|
|
|
if(!flags->Precision) flags->Precision++;
|
|
|
|
flags->Precision -= radix_pos;
|
|
|
|
} else {
|
|
|
|
flags->Format -= 2;
|
|
|
|
if(flags->Precision > 0) flags->Precision--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(trim_tail && !flags->Alternate) {
|
2020-07-23 15:38:25 +02:00
|
|
|
for(i=round_limb; flags->Precision>0 && i<b->e; i++) {
|
|
|
|
if(i>=b->b)
|
|
|
|
l = b->data[bnum_idx(b, i)];
|
2020-07-11 18:44:11 +02:00
|
|
|
else
|
|
|
|
l = 0;
|
|
|
|
|
|
|
|
if(i == round_limb) {
|
|
|
|
if(flags->Format=='f' || flags->Format=='F')
|
|
|
|
r = radix_pos + flags->Precision;
|
|
|
|
else
|
|
|
|
r = flags->Precision + 1;
|
2020-07-23 15:38:25 +02:00
|
|
|
r = first_limb_len + LIMB_DIGITS * (b->e-1 - b->b) - r;
|
2020-07-11 18:44:11 +02:00
|
|
|
r %= LIMB_DIGITS;
|
|
|
|
if(r < 0) r += LIMB_DIGITS;
|
|
|
|
l /= p10s[r];
|
|
|
|
limb_len = LIMB_DIGITS - r;
|
|
|
|
} else {
|
|
|
|
limb_len = LIMB_DIGITS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!l) {
|
|
|
|
flags->Precision -= limb_len;
|
|
|
|
} else {
|
|
|
|
while(l % 10 == 0) {
|
|
|
|
flags->Precision--;
|
|
|
|
l /= 10;
|
|
|
|
}
|
2013-03-27 10:39:30 +01:00
|
|
|
}
|
2011-04-20 14:42:16 +02:00
|
|
|
|
2020-07-11 18:44:11 +02:00
|
|
|
if(flags->Precision <= 0) {
|
|
|
|
flags->Precision = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(l)
|
|
|
|
break;
|
2013-03-27 10:39:30 +01:00
|
|
|
}
|
2020-07-11 18:44:11 +02:00
|
|
|
}
|
2011-04-20 14:42:16 +02:00
|
|
|
|
2020-07-11 18:44:11 +02:00
|
|
|
len = flags->Precision;
|
|
|
|
if(flags->Precision || flags->Alternate) len++;
|
|
|
|
if(flags->Format=='f' || flags->Format=='F') {
|
|
|
|
len += (radix_pos > 0 ? radix_pos : 1);
|
|
|
|
} else if(flags->Format=='e' || flags->Format=='E') {
|
|
|
|
radix_pos--;
|
|
|
|
if(!trim_tail || radix_pos) {
|
|
|
|
len += 3; /* strlen("1e+") */
|
|
|
|
if(three_digit_exp || radix_pos<-99 || radix_pos>99) len += 3;
|
|
|
|
else len += 2;
|
|
|
|
} else {
|
|
|
|
len++;
|
|
|
|
}
|
2011-04-20 14:42:16 +02:00
|
|
|
}
|
2020-07-11 18:44:11 +02:00
|
|
|
|
|
|
|
r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, TRUE);
|
|
|
|
if(r < 0) return r;
|
|
|
|
ret = r;
|
|
|
|
|
|
|
|
f.Format = 'd';
|
2020-07-14 15:33:33 +02:00
|
|
|
f.PadZero = TRUE;
|
2020-07-11 18:44:11 +02:00
|
|
|
if(flags->Format=='f' || flags->Format=='F') {
|
|
|
|
if(radix_pos <= 0) {
|
|
|
|
buf[0] = '0';
|
|
|
|
r = pf_puts(puts_ctx, 1, buf);
|
|
|
|
if(r < 0) return r;
|
|
|
|
ret += r;
|
|
|
|
}
|
|
|
|
|
|
|
|
limb_len = LIMB_DIGITS;
|
2020-07-23 15:38:25 +02:00
|
|
|
for(i=b->e-1; radix_pos>0 && i>=b->b; i--) {
|
|
|
|
limb_len = (i == b->e-1 ? first_limb_len : LIMB_DIGITS);
|
|
|
|
l = b->data[bnum_idx(b, i)];
|
2020-07-11 18:44:11 +02:00
|
|
|
if(limb_len > radix_pos) {
|
|
|
|
f.Precision = radix_pos;
|
|
|
|
l /= p10s[limb_len - radix_pos];
|
|
|
|
limb_len = limb_len - radix_pos;
|
|
|
|
} else {
|
|
|
|
f.Precision = limb_len;
|
|
|
|
limb_len = LIMB_DIGITS;
|
|
|
|
}
|
|
|
|
radix_pos -= f.Precision;
|
2020-07-15 14:05:32 +02:00
|
|
|
FUNC_NAME(pf_integer_conv)(buf, &f, l);
|
2020-07-11 18:44:11 +02:00
|
|
|
|
|
|
|
r = pf_puts(puts_ctx, f.Precision, buf);
|
|
|
|
if(r < 0) return r;
|
|
|
|
ret += r;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[0] = '0';
|
|
|
|
for(; radix_pos>0; radix_pos--) {
|
|
|
|
r = pf_puts(puts_ctx, 1, buf);
|
|
|
|
if(r < 0) return r;
|
|
|
|
ret += r;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(flags->Precision || flags->Alternate) {
|
|
|
|
buf[0] = *(locale ? locale->locinfo : get_locinfo())->lconv->decimal_point;
|
|
|
|
r = pf_puts(puts_ctx, 1, buf);
|
|
|
|
if(r < 0) return r;
|
|
|
|
ret += r;
|
|
|
|
}
|
|
|
|
|
|
|
|
prec = flags->Precision;
|
|
|
|
buf[0] = '0';
|
|
|
|
for(; prec>0 && radix_pos+LIMB_DIGITS-first_limb_len<0; radix_pos++, prec--) {
|
|
|
|
r = pf_puts(puts_ctx, 1, buf);
|
|
|
|
if(r < 0) return r;
|
|
|
|
ret += r;
|
|
|
|
}
|
|
|
|
|
2020-07-23 15:38:25 +02:00
|
|
|
for(; prec>0 && i>=b->b; i--) {
|
|
|
|
l = b->data[bnum_idx(b, i)];
|
2020-07-11 18:44:11 +02:00
|
|
|
if(limb_len != LIMB_DIGITS)
|
|
|
|
l %= p10s[limb_len];
|
|
|
|
if(limb_len > prec) {
|
|
|
|
f.Precision = prec;
|
|
|
|
l /= p10s[limb_len - prec];
|
|
|
|
} else {
|
|
|
|
f.Precision = limb_len;
|
|
|
|
limb_len = LIMB_DIGITS;
|
|
|
|
}
|
|
|
|
prec -= f.Precision;
|
2020-07-15 14:05:32 +02:00
|
|
|
FUNC_NAME(pf_integer_conv)(buf, &f, l);
|
2020-07-11 18:44:11 +02:00
|
|
|
|
|
|
|
r = pf_puts(puts_ctx, f.Precision, buf);
|
|
|
|
if(r < 0) return r;
|
|
|
|
ret += r;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[0] = '0';
|
|
|
|
for(; prec>0; prec--) {
|
|
|
|
r = pf_puts(puts_ctx, 1, buf);
|
|
|
|
if(r < 0) return r;
|
|
|
|
ret += r;
|
|
|
|
}
|
|
|
|
} else {
|
2020-07-23 15:38:25 +02:00
|
|
|
l = b->data[bnum_idx(b, b->e - 1)];
|
2020-07-11 18:44:11 +02:00
|
|
|
l /= p10s[first_limb_len - 1];
|
|
|
|
|
|
|
|
buf[0] = '0' + l;
|
|
|
|
r = pf_puts(puts_ctx, 1, buf);
|
|
|
|
if(r < 0) return r;
|
|
|
|
ret += r;
|
|
|
|
|
|
|
|
if(flags->Precision || flags->Alternate) {
|
|
|
|
buf[0] = *(locale ? locale->locinfo : get_locinfo())->lconv->decimal_point;
|
|
|
|
r = pf_puts(puts_ctx, 1, buf);
|
|
|
|
if(r < 0) return r;
|
|
|
|
ret += r;
|
|
|
|
}
|
|
|
|
|
|
|
|
prec = flags->Precision;
|
|
|
|
limb_len = LIMB_DIGITS;
|
2020-07-23 15:38:25 +02:00
|
|
|
for(i=b->e-1; prec>0 && i>=b->b; i--) {
|
|
|
|
l = b->data[bnum_idx(b, i)];
|
|
|
|
if(i == b->e-1) {
|
2020-07-11 18:44:11 +02:00
|
|
|
limb_len = first_limb_len - 1;
|
|
|
|
l %= p10s[limb_len];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(limb_len > prec) {
|
|
|
|
f.Precision = prec;
|
|
|
|
l /= p10s[limb_len - prec];
|
|
|
|
} else {
|
|
|
|
f.Precision = limb_len;
|
|
|
|
limb_len = LIMB_DIGITS;
|
|
|
|
}
|
|
|
|
prec -= f.Precision;
|
2020-07-15 14:05:32 +02:00
|
|
|
FUNC_NAME(pf_integer_conv)(buf, &f, l);
|
2020-07-11 18:44:11 +02:00
|
|
|
|
|
|
|
r = pf_puts(puts_ctx, f.Precision, buf);
|
|
|
|
if(r < 0) return r;
|
|
|
|
ret += r;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[0] = '0';
|
|
|
|
for(; prec>0; prec--) {
|
|
|
|
r = pf_puts(puts_ctx, 1, buf);
|
|
|
|
if(r < 0) return r;
|
|
|
|
ret += r;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!trim_tail || radix_pos) {
|
|
|
|
buf[0] = flags->Format;
|
|
|
|
buf[1] = radix_pos < 0 ? '-' : '+';
|
|
|
|
r = pf_puts(puts_ctx, 2, buf);
|
|
|
|
if(r < 0) return r;
|
|
|
|
ret += r;
|
|
|
|
|
|
|
|
f.Precision = three_digit_exp ? 3 : 2;
|
2020-07-15 14:05:32 +02:00
|
|
|
FUNC_NAME(pf_integer_conv)(buf, &f, radix_pos);
|
2020-07-11 18:44:11 +02:00
|
|
|
r = pf_puts(puts_ctx, f.Precision, buf);
|
|
|
|
if(r < 0) return r;
|
|
|
|
ret += r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, FALSE);
|
|
|
|
if(r < 0) return r;
|
|
|
|
ret += r;
|
|
|
|
return ret;
|
2011-04-20 14:42:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int FUNC_NAME(pf_printf)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx, const APICHAR *fmt,
|
2020-11-26 19:19:18 +01:00
|
|
|
_locale_t locale, DWORD options,
|
2011-05-11 15:39:07 +02:00
|
|
|
args_clbk pf_args, void *args_ctx, __ms_va_list *valist)
|
2011-04-20 14:42:16 +02:00
|
|
|
{
|
|
|
|
const APICHAR *q, *p = fmt;
|
2011-04-27 12:09:51 +02:00
|
|
|
APICHAR buf[32];
|
2011-04-20 14:42:16 +02:00
|
|
|
int written = 0, pos, i;
|
2020-07-14 15:33:33 +02:00
|
|
|
pf_flags flags;
|
2015-11-03 19:40:36 +01:00
|
|
|
BOOL positional_params = options & MSVCRT_PRINTF_POSITIONAL_PARAMS;
|
|
|
|
BOOL invoke_invalid_param_handler = options & MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER;
|
2015-11-03 19:40:38 +01:00
|
|
|
#if _MSVCR_VER >= 140
|
2020-11-30 14:37:33 +01:00
|
|
|
BOOL legacy_wide = options & _CRT_INTERNAL_PRINTF_LEGACY_WIDE_SPECIFIERS;
|
|
|
|
BOOL legacy_msvcrt_compat = options & _CRT_INTERNAL_PRINTF_LEGACY_MSVCRT_COMPATIBILITY;
|
|
|
|
BOOL three_digit_exp = options & _CRT_INTERNAL_PRINTF_LEGACY_THREE_DIGIT_EXPONENTS;
|
|
|
|
BOOL standard_rounding = options & _CRT_INTERNAL_PRINTF_STANDARD_ROUNDING;
|
2015-11-03 19:40:38 +01:00
|
|
|
#else
|
2020-07-11 18:44:11 +02:00
|
|
|
BOOL legacy_wide = TRUE, legacy_msvcrt_compat = TRUE;
|
2020-12-04 15:50:17 +01:00
|
|
|
BOOL three_digit_exp = _get_output_format() != _TWO_DIGIT_EXPONENT;
|
2020-08-06 13:18:14 +02:00
|
|
|
BOOL standard_rounding = FALSE;
|
2015-11-03 19:40:38 +01:00
|
|
|
#endif
|
2011-04-20 14:42:16 +02:00
|
|
|
|
2017-12-01 18:19:31 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(fmt != NULL))
|
|
|
|
return -1;
|
|
|
|
|
2011-04-20 14:42:16 +02:00
|
|
|
while(*p) {
|
|
|
|
/* output characters before '%' */
|
|
|
|
for(q=p; *q && *q!='%'; q++);
|
|
|
|
if(p != q) {
|
|
|
|
i = pf_puts(puts_ctx, q-p, p);
|
|
|
|
if(i < 0)
|
|
|
|
return i;
|
|
|
|
|
|
|
|
written += i;
|
|
|
|
p = q;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* *p == '%' here */
|
|
|
|
p++;
|
|
|
|
|
|
|
|
/* output a single '%' character */
|
|
|
|
if(*p == '%') {
|
|
|
|
i = pf_puts(puts_ctx, 1, p++);
|
|
|
|
if(i < 0)
|
|
|
|
return i;
|
|
|
|
|
|
|
|
written += i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check parameter position */
|
|
|
|
if(positional_params && (q = FUNC_NAME(pf_parse_int)(p, &pos)) && *q=='$')
|
|
|
|
p = q+1;
|
|
|
|
else
|
|
|
|
pos = -1;
|
|
|
|
|
|
|
|
/* parse the flags */
|
|
|
|
memset(&flags, 0, sizeof(flags));
|
|
|
|
while(*p) {
|
|
|
|
if(*p=='+' || *p==' ') {
|
|
|
|
if(flags.Sign != '+')
|
|
|
|
flags.Sign = *p;
|
|
|
|
} else if(*p == '-')
|
2020-07-14 15:33:33 +02:00
|
|
|
flags.LeftAlign = TRUE;
|
2011-04-20 14:42:16 +02:00
|
|
|
else if(*p == '0')
|
2020-07-14 15:33:33 +02:00
|
|
|
flags.PadZero = TRUE;
|
2011-04-20 14:42:16 +02:00
|
|
|
else if(*p == '#')
|
2020-07-14 15:33:33 +02:00
|
|
|
flags.Alternate = TRUE;
|
2011-04-20 14:42:16 +02:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
2014-12-22 02:16:59 +01:00
|
|
|
/* parse the width */
|
2011-04-20 14:42:16 +02:00
|
|
|
if(*p == '*') {
|
|
|
|
p++;
|
|
|
|
if(positional_params && (q = FUNC_NAME(pf_parse_int)(p, &i)) && *q=='$')
|
|
|
|
p = q+1;
|
|
|
|
else
|
|
|
|
i = -1;
|
|
|
|
|
2011-05-11 15:39:07 +02:00
|
|
|
flags.FieldLength = pf_args(args_ctx, i, VT_INT, valist).get_int;
|
2011-04-20 14:42:16 +02:00
|
|
|
if(flags.FieldLength < 0) {
|
2020-07-14 15:33:33 +02:00
|
|
|
flags.LeftAlign = TRUE;
|
2011-04-20 14:42:16 +02:00
|
|
|
flags.FieldLength = -flags.FieldLength;
|
|
|
|
}
|
2020-11-18 16:03:27 +01:00
|
|
|
} else while (*p >= '0' && *p <= '9') {
|
2011-04-20 14:42:16 +02:00
|
|
|
flags.FieldLength *= 10;
|
|
|
|
flags.FieldLength += *p++ - '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse the precision */
|
|
|
|
flags.Precision = -1;
|
|
|
|
if(*p == '.') {
|
|
|
|
flags.Precision = 0;
|
|
|
|
p++;
|
|
|
|
if(*p == '*') {
|
|
|
|
p++;
|
|
|
|
if(positional_params && (q = FUNC_NAME(pf_parse_int)(p, &i)) && *q=='$')
|
|
|
|
p = q+1;
|
|
|
|
else
|
|
|
|
i = -1;
|
|
|
|
|
2011-05-11 15:39:07 +02:00
|
|
|
flags.Precision = pf_args(args_ctx, i, VT_INT, valist).get_int;
|
2020-11-18 16:03:27 +01:00
|
|
|
} else while (*p >= '0' && *p <= '9') {
|
2011-04-20 14:42:16 +02:00
|
|
|
flags.Precision *= 10;
|
|
|
|
flags.Precision += *p++ - '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse argument size modifier */
|
|
|
|
while(*p) {
|
|
|
|
if(*p=='l' && *(p+1)=='l') {
|
2020-07-14 15:33:33 +02:00
|
|
|
flags.IntegerDouble = TRUE;
|
2011-04-20 14:42:16 +02:00
|
|
|
p++;
|
2020-07-14 15:33:33 +02:00
|
|
|
} else if(*p=='l') {
|
|
|
|
flags.IntegerLength = LEN_LONG;
|
|
|
|
} else if(*p == 'h') {
|
|
|
|
flags.IntegerLength = LEN_SHORT;
|
2011-04-20 14:42:16 +02:00
|
|
|
} else if(*p == 'I') {
|
|
|
|
if(*(p+1)=='6' && *(p+2)=='4') {
|
2020-07-14 15:33:33 +02:00
|
|
|
flags.IntegerDouble = TRUE;
|
|
|
|
p += 2;
|
2011-04-20 14:42:16 +02:00
|
|
|
} else if(*(p+1)=='3' && *(p+2)=='2')
|
2020-07-14 15:33:33 +02:00
|
|
|
p += 2;
|
2019-11-02 14:39:23 +01:00
|
|
|
else if(p[1] && strchr("diouxX", p[1]))
|
2020-07-14 15:33:33 +02:00
|
|
|
flags.IntegerNative = TRUE;
|
2019-11-02 14:39:23 +01:00
|
|
|
else
|
|
|
|
break;
|
2011-04-20 14:42:16 +02:00
|
|
|
} else if(*p == 'w')
|
2020-07-14 15:33:33 +02:00
|
|
|
flags.WideString = TRUE;
|
2019-11-02 14:39:28 +01:00
|
|
|
#if _MSVCR_VER == 0 || _MSVCR_VER >= 140
|
|
|
|
else if((*p == 'z' || *p == 't') && p[1] && strchr("diouxX", p[1]))
|
2020-07-14 15:33:33 +02:00
|
|
|
flags.IntegerNative = TRUE;
|
|
|
|
else if(*p == 'j')
|
|
|
|
flags.IntegerDouble = TRUE;
|
2019-11-02 14:39:28 +01:00
|
|
|
#endif
|
|
|
|
#if _MSVCR_VER >= 140
|
|
|
|
else if(*p == 'T')
|
2020-07-14 15:33:33 +02:00
|
|
|
flags.NaturalString = TRUE;
|
2015-11-03 19:40:40 +01:00
|
|
|
#endif
|
2020-07-14 15:33:33 +02:00
|
|
|
else if(*p != 'L' && ((*p != 'F' && *p != 'N') || !legacy_msvcrt_compat))
|
2011-04-20 14:42:16 +02:00
|
|
|
break;
|
2020-07-14 15:33:33 +02:00
|
|
|
p++;
|
2011-04-20 14:42:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
flags.Format = *p;
|
|
|
|
|
|
|
|
if(flags.Format == 's' || flags.Format == 'S') {
|
|
|
|
i = FUNC_NAME(pf_handle_string)(pf_puts, puts_ctx,
|
2011-05-11 15:39:07 +02:00
|
|
|
pf_args(args_ctx, pos, VT_PTR, valist).get_ptr,
|
2019-02-14 18:27:59 +01:00
|
|
|
-1, &flags, locale, legacy_wide);
|
2011-04-20 14:42:16 +02:00
|
|
|
} else if(flags.Format == 'c' || flags.Format == 'C') {
|
2011-05-11 15:39:07 +02:00
|
|
|
int ch = pf_args(args_ctx, pos, VT_INT, valist).get_int;
|
2011-04-20 14:42:16 +02:00
|
|
|
|
2019-02-14 18:27:59 +01:00
|
|
|
i = FUNC_NAME(pf_handle_string)(pf_puts, puts_ctx, &ch, 1, &flags, locale, legacy_wide);
|
|
|
|
if(i < 0) i = 0; /* ignore conversion error */
|
2011-04-20 14:42:16 +02:00
|
|
|
} else if(flags.Format == 'p') {
|
2011-04-27 12:09:37 +02:00
|
|
|
flags.Format = 'X';
|
2020-07-14 15:33:33 +02:00
|
|
|
flags.PadZero = TRUE;
|
2011-04-27 12:09:37 +02:00
|
|
|
i = flags.Precision;
|
|
|
|
flags.Precision = 2*sizeof(void*);
|
2020-07-15 14:05:32 +02:00
|
|
|
FUNC_NAME(pf_integer_conv)(buf, &flags,
|
|
|
|
(ULONG_PTR)pf_args(args_ctx, pos, VT_PTR, valist).get_ptr);
|
2020-07-14 15:33:33 +02:00
|
|
|
flags.PadZero = FALSE;
|
2011-04-27 12:09:37 +02:00
|
|
|
flags.Precision = i;
|
2011-04-20 14:42:16 +02:00
|
|
|
|
2011-04-27 12:09:51 +02:00
|
|
|
#ifdef PRINTF_WIDE
|
2019-02-14 18:27:59 +01:00
|
|
|
i = FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, buf, -1, &flags, locale);
|
2011-04-27 12:09:51 +02:00
|
|
|
#else
|
2019-02-14 18:27:59 +01:00
|
|
|
i = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, buf, -1, &flags, locale);
|
2011-04-27 12:09:51 +02:00
|
|
|
#endif
|
2011-04-20 14:42:16 +02:00
|
|
|
} else if(flags.Format == 'n') {
|
2011-04-27 20:09:00 +02:00
|
|
|
int *used;
|
|
|
|
|
|
|
|
if(!n_format_enabled) {
|
2020-11-30 14:37:27 +01:00
|
|
|
MSVCRT_INVALID_PMT("\'n\' format specifier disabled", EINVAL);
|
2011-04-27 20:09:00 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-05-11 15:39:07 +02:00
|
|
|
used = pf_args(args_ctx, pos, VT_PTR, valist).get_ptr;
|
2011-04-20 14:42:16 +02:00
|
|
|
*used = written;
|
|
|
|
i = 0;
|
2011-04-27 12:09:17 +02:00
|
|
|
} else if(flags.Format && strchr("diouxX", flags.Format)) {
|
2011-04-27 12:09:51 +02:00
|
|
|
APICHAR *tmp = buf;
|
2012-03-12 19:49:11 +01:00
|
|
|
int max_len;
|
2011-04-20 14:42:16 +02:00
|
|
|
|
2012-03-18 19:19:01 +01:00
|
|
|
/* 0 padding is added after '0x' if Alternate flag is in use */
|
2012-03-12 19:49:11 +01:00
|
|
|
if((flags.Format=='x' || flags.Format=='X') && flags.PadZero && flags.Alternate
|
|
|
|
&& !flags.LeftAlign && flags.Precision<flags.FieldLength-2)
|
|
|
|
flags.Precision = flags.FieldLength - 2;
|
|
|
|
|
|
|
|
max_len = (flags.FieldLength>flags.Precision ? flags.FieldLength : flags.Precision) + 10;
|
2018-07-26 00:05:52 +02:00
|
|
|
if(max_len > ARRAY_SIZE(buf))
|
2011-04-20 14:42:16 +02:00
|
|
|
tmp = HeapAlloc(GetProcessHeap(), 0, max_len);
|
|
|
|
if(!tmp)
|
|
|
|
return -1;
|
|
|
|
|
2015-11-03 19:40:39 +01:00
|
|
|
if(flags.IntegerDouble || (flags.IntegerNative && sizeof(void*) == 8))
|
2020-07-15 14:05:32 +02:00
|
|
|
FUNC_NAME(pf_integer_conv)(tmp, &flags, pf_args(args_ctx, pos,
|
2011-05-11 15:39:07 +02:00
|
|
|
VT_I8, valist).get_longlong);
|
2011-04-27 12:09:17 +02:00
|
|
|
else if(flags.Format=='d' || flags.Format=='i')
|
2020-07-15 14:05:32 +02:00
|
|
|
FUNC_NAME(pf_integer_conv)(tmp, &flags,
|
2020-07-14 15:33:33 +02:00
|
|
|
flags.IntegerLength != LEN_SHORT ?
|
2012-04-16 15:21:47 +02:00
|
|
|
pf_args(args_ctx, pos, VT_INT, valist).get_int :
|
|
|
|
(short)pf_args(args_ctx, pos, VT_INT, valist).get_int);
|
2011-04-27 12:09:17 +02:00
|
|
|
else
|
2020-07-15 14:05:32 +02:00
|
|
|
FUNC_NAME(pf_integer_conv)(tmp, &flags,
|
2020-07-14 15:33:33 +02:00
|
|
|
flags.IntegerLength != LEN_SHORT ?
|
2012-04-16 15:21:47 +02:00
|
|
|
(unsigned)pf_args(args_ctx, pos, VT_INT, valist).get_int :
|
|
|
|
(unsigned short)pf_args(args_ctx, pos, VT_INT, valist).get_int);
|
2011-04-20 14:42:16 +02:00
|
|
|
|
2011-04-27 12:09:51 +02:00
|
|
|
#ifdef PRINTF_WIDE
|
2019-02-14 18:27:59 +01:00
|
|
|
i = FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, tmp, -1, &flags, locale);
|
2011-04-27 12:09:51 +02:00
|
|
|
#else
|
2019-02-14 18:27:59 +01:00
|
|
|
i = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, tmp, -1, &flags, locale);
|
2011-04-27 12:09:51 +02:00
|
|
|
#endif
|
2011-04-20 14:42:16 +02:00
|
|
|
if(tmp != buf)
|
|
|
|
HeapFree(GetProcessHeap(), 0, tmp);
|
2020-06-08 17:20:46 +02:00
|
|
|
} else if(flags.Format && strchr("aAeEfFgG", flags.Format)) {
|
2011-07-26 12:41:46 +02:00
|
|
|
double val = pf_args(args_ctx, pos, VT_R8, valist).get_double;
|
2013-09-16 15:05:16 +02:00
|
|
|
|
2020-06-08 17:20:46 +02:00
|
|
|
if(signbit(val)) {
|
|
|
|
flags.Sign = '-';
|
|
|
|
val = -val;
|
2015-03-25 14:22:02 +01:00
|
|
|
}
|
2013-09-16 15:05:16 +02:00
|
|
|
|
2020-06-08 17:20:46 +02:00
|
|
|
if(isinf(val) || isnan(val))
|
|
|
|
i = FUNC_NAME(pf_output_special_fp)(pf_puts, puts_ctx, val, &flags,
|
|
|
|
locale, legacy_msvcrt_compat, three_digit_exp);
|
2020-06-08 17:20:51 +02:00
|
|
|
else if(flags.Format=='a' || flags.Format=='A')
|
2020-08-06 13:18:14 +02:00
|
|
|
i = FUNC_NAME(pf_output_hex_fp)(pf_puts, puts_ctx, val, &flags,
|
|
|
|
locale, standard_rounding);
|
2020-07-11 18:44:11 +02:00
|
|
|
else
|
2020-08-06 13:18:14 +02:00
|
|
|
i = FUNC_NAME(pf_output_fp)(pf_puts, puts_ctx, val, &flags,
|
|
|
|
locale, three_digit_exp, standard_rounding);
|
2011-04-20 14:42:16 +02:00
|
|
|
} else {
|
|
|
|
if(invoke_invalid_param_handler) {
|
2020-12-01 13:51:23 +01:00
|
|
|
_invalid_parameter(NULL, NULL, NULL, 0, 0);
|
|
|
|
*_errno() = EINVAL;
|
2011-04-20 14:42:16 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(i < 0)
|
|
|
|
return i;
|
|
|
|
written += i;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
2011-04-27 12:09:01 +02:00
|
|
|
#ifndef PRINTF_WIDE
|
|
|
|
enum types_clbk_flags {
|
|
|
|
TYPE_CLBK_VA_LIST = 1,
|
|
|
|
TYPE_CLBK_POSITIONAL = 2,
|
|
|
|
TYPE_CLBK_ERROR_POS = 4,
|
|
|
|
TYPE_CLBK_ERROR_TYPE = 8
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This functions stores types of arguments. It uses args[0] internally */
|
|
|
|
static printf_arg arg_clbk_type(void *ctx, int pos, int type, __ms_va_list *valist)
|
|
|
|
{
|
|
|
|
static const printf_arg ret;
|
|
|
|
printf_arg *args = ctx;
|
|
|
|
|
|
|
|
if(pos == -1) {
|
|
|
|
args[0].get_int |= TYPE_CLBK_VA_LIST;
|
|
|
|
return ret;
|
|
|
|
} else
|
|
|
|
args[0].get_int |= TYPE_CLBK_POSITIONAL;
|
|
|
|
|
2020-12-01 13:52:05 +01:00
|
|
|
if(pos<1 || pos>_ARGMAX)
|
2011-04-27 12:09:01 +02:00
|
|
|
args[0].get_int |= TYPE_CLBK_ERROR_POS;
|
|
|
|
else if(args[pos].get_int && args[pos].get_int!=type)
|
|
|
|
args[0].get_int |= TYPE_CLBK_ERROR_TYPE;
|
|
|
|
else
|
|
|
|
args[pos].get_int = type;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-10-13 20:23:29 +02:00
|
|
|
int FUNC_NAME(create_positional_ctx)(void *args_ctx, const APICHAR *format, __ms_va_list valist)
|
2011-04-27 12:09:01 +02:00
|
|
|
{
|
|
|
|
struct FUNC_NAME(_str_ctx) puts_ctx = {INT_MAX, NULL};
|
|
|
|
printf_arg *args = args_ctx;
|
|
|
|
int i, j;
|
|
|
|
|
2015-11-03 19:40:36 +01:00
|
|
|
i = FUNC_NAME(pf_printf)(FUNC_NAME(puts_clbk_str), &puts_ctx, format, NULL,
|
|
|
|
MSVCRT_PRINTF_POSITIONAL_PARAMS, arg_clbk_type, args_ctx, NULL);
|
2011-04-27 12:09:01 +02:00
|
|
|
if(i < 0)
|
|
|
|
return i;
|
|
|
|
|
|
|
|
if(args[0].get_int==0 || args[0].get_int==TYPE_CLBK_VA_LIST)
|
|
|
|
return 0;
|
|
|
|
if(args[0].get_int != TYPE_CLBK_POSITIONAL)
|
|
|
|
return -1;
|
|
|
|
|
2020-12-01 13:52:05 +01:00
|
|
|
for(i=_ARGMAX; i>0; i--)
|
2011-04-27 12:09:01 +02:00
|
|
|
if(args[i].get_int)
|
|
|
|
break;
|
|
|
|
|
|
|
|
for(j=1; j<=i; j++) {
|
|
|
|
switch(args[j].get_int) {
|
|
|
|
case VT_I8:
|
|
|
|
args[j].get_longlong = va_arg(valist, LONGLONG);
|
|
|
|
break;
|
|
|
|
case VT_INT:
|
|
|
|
args[j].get_int = va_arg(valist, int);
|
|
|
|
break;
|
|
|
|
case VT_R8:
|
|
|
|
args[j].get_double = va_arg(valist, double);
|
|
|
|
break;
|
|
|
|
case VT_PTR:
|
|
|
|
args[j].get_ptr = va_arg(valist, void*);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return j;
|
|
|
|
}
|
|
|
|
|
2011-04-20 14:42:16 +02:00
|
|
|
#undef APICHAR
|
|
|
|
#undef CONVCHAR
|
|
|
|
#undef FUNC_NAME
|