44 #define PG_NL_ARGMAX 31
253 target.
bufend = buffer +
sizeof(buffer);
306 if (!target->
failed && nc > 0)
311 target->
nchars += written;
321 static void fmtstr(
const char *
value,
int leftjust,
int minlen,
int maxwidth,
325 int leftjust,
int minlen,
int zpad,
int precision,
int pointflag,
329 int leftjust,
int minlen,
int zpad,
int precision,
int pointflag,
334 static int adjust_sign(
int is_negative,
int forcesign,
int *signvalue);
336 static void leading_pad(
int zpad,
int signvalue,
int *padlen,
347 #ifndef HAVE_STRCHRNUL
349 static inline const char *
352 while (*s !=
'\0' && *s !=
c)
378 int save_errno = errno;
379 const char *first_pct = NULL;
397 const char *strvalue;
421 if (*next_pct ==
'\0')
431 if (first_pct == NULL)
441 strvalue = va_arg(
args,
char *);
442 if (strvalue == NULL)
444 dostr(strvalue, strlen(strvalue), target);
450 fieldwidth = precision = zpad = leftjust = forcesign = 0;
451 longflag = longlongflag = pointflag = 0;
453 have_star = afterstar =
false;
466 if (accum == 0 && !pointflag)
478 accum = accum * 10 + (ch -
'0');
501 int starval = va_arg(
args,
int);
514 fieldwidth = starval;
518 fieldwidth = -fieldwidth;
537 int starval = argvalues[accum].
i;
550 fieldwidth = starval;
554 fieldwidth = -fieldwidth;
575 #if SIZEOF_VOID_P == SIZEOF_LONG
577 #elif SIZEOF_VOID_P == SIZEOF_LONG_LONG
580 #error "cannot find integer type of the same size as intptr_t"
592 #if SIZEOF_SIZE_T == SIZEOF_LONG
594 #elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
597 #error "cannot find integer type of the same size as size_t"
616 numvalue = argvalues[fmtpos].
ll;
618 numvalue = argvalues[fmtpos].
l;
620 numvalue = argvalues[fmtpos].
i;
625 numvalue = va_arg(
args,
long long);
627 numvalue = va_arg(
args,
long);
629 numvalue = va_arg(
args,
int);
631 fmtint(numvalue, ch, forcesign, leftjust, fieldwidth, zpad,
632 precision, pointflag, target);
648 numvalue = (
unsigned long long) argvalues[fmtpos].ll;
650 numvalue = (
unsigned long) argvalues[fmtpos].l;
652 numvalue = (
unsigned int) argvalues[fmtpos].
i;
657 numvalue = (
unsigned long long) va_arg(
args,
long long);
659 numvalue = (
unsigned long) va_arg(
args,
long);
661 numvalue = (
unsigned int) va_arg(
args,
int);
663 fmtint(numvalue, ch, forcesign, leftjust, fieldwidth, zpad,
664 precision, pointflag, target);
675 cvalue = (
unsigned char) argvalues[fmtpos].
i;
677 cvalue = (
unsigned char) va_arg(
args,
int);
678 fmtchar(cvalue, leftjust, fieldwidth, target);
689 strvalue = argvalues[fmtpos].
cptr;
691 strvalue = va_arg(
args,
char *);
693 if (strvalue == NULL)
695 fmtstr(strvalue, leftjust, fieldwidth, precision, pointflag,
701 strvalue = argvalues[fmtpos].
cptr;
703 strvalue = va_arg(
args,
char *);
704 fmtptr((
const void *) strvalue, target);
719 fvalue = argvalues[fmtpos].
d;
721 fvalue = va_arg(
args,
double);
722 fmtfloat(fvalue, ch, forcesign, leftjust,
724 precision, pointflag,
731 errbuf,
sizeof(errbuf));
733 dostr(errm, strlen(errm), target);
801 longflag = longlongflag = 0;
821 accum = accum * 10 + (ch -
'0');
837 if (argtypes[accum] &&
841 last_dollar =
Max(last_dollar, accum);
860 #if SIZEOF_VOID_P == SIZEOF_LONG
862 #elif SIZEOF_VOID_P == SIZEOF_LONG_LONG
865 #error "cannot find integer type of the same size as intptr_t"
877 #if SIZEOF_SIZE_T == SIZEOF_LONG
879 #elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
882 #error "cannot find integer type of the same size as size_t"
905 if (argtypes[fmtpos] &&
906 argtypes[fmtpos] != atype)
908 argtypes[fmtpos] = atype;
909 last_dollar =
Max(last_dollar, fmtpos);
917 if (argtypes[fmtpos] &&
921 last_dollar =
Max(last_dollar, fmtpos);
930 if (argtypes[fmtpos] &&
934 last_dollar =
Max(last_dollar, fmtpos);
946 if (argtypes[fmtpos] &&
950 last_dollar =
Max(last_dollar, fmtpos);
975 for (
i = 1;
i <= last_dollar;
i++)
982 argvalues[
i].
i = va_arg(
args,
int);
985 argvalues[
i].
l = va_arg(
args,
long);
988 argvalues[
i].
ll = va_arg(
args,
long long);
991 argvalues[
i].
d = va_arg(
args,
double);
994 argvalues[
i].
cptr = va_arg(
args,
char *);
1016 vallen = strlen(
value);
1047 int minlen,
int zpad,
int precision,
int pointflag,
1050 unsigned long long uvalue;
1053 const char *cvt =
"0123456789abcdef";
1080 cvt =
"0123456789ABCDEF";
1090 #pragma warning(push)
1091 #pragma warning(disable: 4146)
1095 uvalue = -(
unsigned long long)
value;
1097 uvalue = (
unsigned long long)
value;
1099 #pragma warning(pop)
1106 if (
value == 0 && pointflag && precision == 0)
1121 uvalue = uvalue / 10;
1124 else if (base == 16)
1129 uvalue = uvalue / 16;
1137 uvalue = uvalue / 8;
1142 zeropad =
Max(0, precision - vallen);
1176 int minlen,
int zpad,
int precision,
int pointflag,
1206 prec =
Min(precision, 350);
1222 static const double dzero = 0.0;
1226 memcmp(&
value, &dzero,
sizeof(
double)) != 0)),
1227 forcesign, &signvalue))
1238 zeropadlen = precision - prec;
1280 char *epos = strrchr(
convert,
'e');
1344 else if (precision > 32)
1358 static const double dzero = 0.0;
1362 memcmp(&
value, &dzero,
sizeof(
double)) != 0))
1426 if (target->
bufend != NULL)
1433 if (target->
stream == NULL)
1441 avail =
Min(avail, slen);
1455 if (target->
stream == NULL)
1479 if (target->
bufend != NULL)
1486 if (target->
stream == NULL)
1494 avail =
Min(avail, slen);
1495 memset(target->
bufptr,
c, avail);
1521 padlen = minlen - vallen;
1535 if (*padlen > 0 && zpad)
1549 maxpad = (signvalue != 0);
1550 if (*padlen > maxpad)
1560 else if (*padlen < 0)
#define Assert(condition)
static void const char * fmt
#define PG_STRERROR_R_BUFLEN
size_t strnlen(const char *str, size_t maxlen)
static void dostr(const char *str, int slen, PrintfTarget *target)
static void dopr_outch(int c, PrintfTarget *target)
static void fmtint(long long value, char type, int forcesign, int leftjust, int minlen, int zpad, int precision, int pointflag, PrintfTarget *target)
static void leading_pad(int zpad, int signvalue, int *padlen, PrintfTarget *target)
int pg_strfromd(char *str, size_t count, int precision, double value)
static void fmtptr(const void *value, PrintfTarget *target)
static void flushbuffer(PrintfTarget *target)
int pg_printf(const char *fmt,...)
static void fmtchar(int value, int leftjust, int minlen, PrintfTarget *target)
static void fmtstr(const char *value, int leftjust, int minlen, int maxwidth, int pointflag, PrintfTarget *target)
int pg_vprintf(const char *fmt, va_list args)
int pg_snprintf(char *str, size_t count, const char *fmt,...)
static int compute_padlen(int minlen, int vallen, int leftjust)
int pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
static void trailing_pad(int padlen, PrintfTarget *target)
static void fmtfloat(double value, char type, int forcesign, int leftjust, int minlen, int zpad, int precision, int pointflag, PrintfTarget *target)
int pg_sprintf(char *str, const char *fmt,...)
static bool find_arguments(const char *format, va_list args, PrintfArgValue *argvalues)
int pg_vsprintf(char *str, const char *fmt, va_list args)
static void dopr(PrintfTarget *target, const char *format, va_list args)
static const char * strchrnul(const char *s, int c)
static void dopr_outchmulti(int c, int slen, PrintfTarget *target)
int pg_fprintf(FILE *stream, const char *fmt,...)
int pg_vfprintf(FILE *stream, const char *fmt, va_list args)
static int adjust_sign(int is_negative, int forcesign, int *signvalue)
static void convert(const int32 val, char *const buf)