63 #define LONGDOUBLE_TYPE long double 80 #define etSQLESCAPE 11 81 #define etSQLESCAPE2 12 83 #ifdef __UNSUPPORTED__ 88 #define etSQLESCAPE3 16 89 #define etSQLESCAPE4 17 112 #define FLAG_SIGNED 1 113 #define FLAG_INTERN 2 114 #define FLAG_STRING 4 121 static const char aDigits[] =
"0123456789ABCDEF0123456789abcdef";
137 #ifndef SWITCH_OMIT_FLOATING_POINT 139 {
'e', 0, 1,
etEXP, 30, 0},
140 {
'E', 0, 1,
etEXP, 14, 0},
144 {
'n', 0, 0,
etSIZE, 0, 0},
147 #ifdef __UNSUPPORTED__ 148 {
'T', 0, 2, etTOKEN, 0, 0},
149 {
'S', 0, 2, etSRCLIST, 0, 0},
153 #define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0])) 159 #ifndef SWITCH_OMIT_FLOATING_POINT 182 *val = (*val - d) * 10.0;
192 #ifndef SWITCH_PRINT_BUF_SIZE 193 # define SWITCH_PRINT_BUF_SIZE 350 195 #define etBUFSIZE SWITCH_PRINT_BUF_SIZE 224 static int vxprintf(
void (*func) (
void *,
const char *,
int),
241 etByte flag_alternateform;
255 static const char spaces[] =
" ";
256 #define etSPACESIZE (sizeof(spaces)-1) 257 #ifndef SWITCH_OMIT_FLOATING_POINT 269 for (; (c = (*fmt)) != 0; ++
fmt) {
272 bufpt = (
char *) fmt;
274 while ((c = (*++fmt)) !=
'%' && c != 0)
276 (*func) (arg, bufpt, amt);
281 if ((c = (*++fmt)) == 0) {
283 (*func) (arg,
"%", 1);
288 flag_leftjustify = flag_plussign = flag_blanksign = flag_alternateform = flag_altform2 = flag_zeropad = 0;
293 flag_leftjustify = 1;
302 flag_alternateform = 1;
314 }
while (!done && (c = (*++fmt)) != 0);
318 width = va_arg(ap,
int);
320 flag_leftjustify = 1;
325 while (c >=
'0' && c <=
'9') {
326 width = width * 10 + c -
'0';
338 precision = va_arg(ap,
int);
340 precision = -precision;
343 while (c >=
'0' && c <=
'9') {
344 precision = precision * 10 + c -
'0';
362 flag_long = flag_longlong = 0;
366 for (idx = 0; idx <
etNINFO; idx++) {
367 if (c == fmtinfo[idx].
fmttype) {
368 infop = &fmtinfo[idx];
411 flag_longlong =
sizeof(
char *) ==
sizeof(int64_t);
412 flag_long =
sizeof(
char *) ==
sizeof(
long int);
418 v = va_arg(ap, int64_t);
420 v = va_arg(ap,
long int);
430 else if (flag_blanksign)
437 longvalue = va_arg(ap, uint64_t);
439 longvalue = va_arg(ap,
unsigned long int);
441 longvalue = va_arg(ap,
unsigned int);
445 flag_alternateform = 0;
446 if (flag_zeropad && precision < width - (prefix != 0)) {
447 precision = width - (prefix != 0);
451 register const char *cset;
456 *(--bufpt) = cset[longvalue % base];
457 longvalue = longvalue /
base;
458 }
while (longvalue > 0);
460 length = (int)(&buf[
etBUFSIZE - 1] - bufpt);
461 for (idx = precision - length; idx > 0; idx--) {
466 if (flag_alternateform && infop->
prefix) {
470 if (*bufpt != pre[0]) {
471 for (; (x = (*pre)) != 0; pre++)
475 length = (int)(&buf[
etBUFSIZE - 1] - bufpt);
480 realvalue = va_arg(ap,
double);
481 #ifndef SWITCH_OMIT_FLOATING_POINT 486 if (realvalue < 0.0) {
487 realvalue = -realvalue;
492 else if (flag_blanksign)
501 for (idx = precision, rounder = 0.4999; idx > 0; idx--, rounder *= 0.1);
504 for (idx = precision, rounder = 0.5; idx > 0; idx--, rounder *= 0.1) {
508 realvalue += rounder;
511 if (realvalue > 0.0) {
512 while (realvalue >= 1e32 && exp <= 350) {
516 while (realvalue >= 1e8 && exp <= 350) {
520 while (realvalue >= 10.0 && exp <= 350) {
524 while (realvalue < 1e-8 && exp >= -350) {
528 while (realvalue < 1.0 && exp >= -350) {
532 if (exp > 350 || exp < -350) {
543 flag_exp = xtype ==
etEXP;
545 realvalue += rounder;
546 if (realvalue >= 10.0) {
552 flag_rtz = !flag_alternateform;
553 if (exp < -4 || exp > precision) {
556 precision = precision - exp;
562 if (xtype ==
etEXP) {
568 flag_dp = (precision > 0) | flag_alternateform | flag_altform2;
577 for (; e2 >= 0; e2--) {
587 for (e2++; e2 < 0 && precision > 0; precision--, e2++) {
591 while ((precision--) > 0) {
595 if (flag_rtz && flag_dp) {
596 while (bufpt[-1] ==
'0')
599 if (bufpt[-1] ==
'.') {
608 if (flag_exp || (xtype ==
etEXP && exp)) {
617 *(bufpt++) = (
char) (exp / 100) +
'0';
620 *(bufpt++) = (
char) exp / 10 +
'0';
621 *(bufpt++) = exp % 10 +
'0';
628 length = (int)(bufpt - buf);
633 if (flag_zeropad && !flag_leftjustify && length < width) {
635 int nPad = width -
length;
636 for (i = width; i >= nPad; i--) {
637 bufpt[i] = bufpt[i - nPad];
647 *(va_arg(ap,
int *)) = count;
657 c = buf[0] = (char) (xtype ==
etCHARX ? va_arg(ap,
int) : *++
fmt);
658 if (precision >= 0) {
659 for (idx = 1; idx < precision; idx++)
669 bufpt = va_arg(ap,
char *);
675 length = (int)strlen(bufpt);
676 if (precision >= 0 && precision < length)
684 int needQuote, isnull;
685 char *escarg = va_arg(ap,
char *);
686 isnull = escarg == 0;
689 for (i = n = 0; (ch = escarg[i]) != 0; i++) {
690 if (ch ==
'\'' || (xtype ==
etSQLESCAPE3 && ch ==
'\\'))
694 n += i + 1 + needQuote * 2;
696 bufpt = zExtra = malloc(n);
705 for (i = 0; (ch = escarg[i]) != 0; i++) {
706 bufpt[j++] = (char) ch;
708 if (ch ==
'\'' || (xtype ==
etSQLESCAPE3 && ch ==
'\\')) {
709 bufpt[j] = (char) ch;
710 bufpt[j-1] = (char)
'\\';
714 if (ch ==
'\'' || (xtype ==
etSQLESCAPE3 && ch ==
'\\'))
715 bufpt[j++] = (
char) ch;
726 #ifdef __UNSUPPORTED__ 728 Token *pToken = va_arg(ap, Token *);
729 if (pToken && pToken->z) {
730 (*func) (arg, (
char *) pToken->z, pToken->n);
736 SrcList *pSrc = va_arg(ap, SrcList *);
737 int k = va_arg(ap,
int);
738 struct SrcList_item *pItem = &pSrc->a[k];
739 assert(k >= 0 && k < pSrc->nSrc);
740 if (pItem->zDatabase && pItem->zDatabase[0]) {
741 (*func) (arg, pItem->zDatabase, strlen(pItem->zDatabase));
742 (*func) (arg,
".", 1);
744 (*func) (arg, pItem->zName, strlen(pItem->zName));
755 if (!flag_leftjustify) {
765 (*func) (arg, spaces, nspace);
769 (*func) (arg, bufpt,
length);
772 if (flag_leftjustify) {
782 (*func) (arg, spaces, nspace);
789 return errorflag ? -1 :
count;
802 void *(*xRealloc) (
void *, int);
811 static void mout(
void *arg,
const char *zNewText,
int nNewChar)
836 memcpy(&pM->
zText[pM->
nChar], zNewText, nNewChar);
837 pM->
nChar += nNewChar;
882 return realloc(old, size);
902 va_start(ap, zFormat);
919 va_start(ap, zFormat);
char * switch_mprintf(const char *zFormat,...)
#define SWITCH_PRINT_BUF_SIZE
static char * base_vprintf(void *(*xRealloc)(void *, int), int useInternal, char *zInitBuf, int nInitBuf, const char *zFormat, va_list ap)
static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt)
static const et_info fmtinfo[]
static void * printf_realloc(void *old, int size)
static const char aPrefix[]
static const char aDigits[]
switch_byte_t switch_byte_t * buf
if((uint32_t)(unpack->cur - unpack->buf) > unpack->buflen)
void *(* xRealloc)(void *, int)
static void mout(void *arg, const char *zNewText, int nNewChar)
static int vxprintf(void(*func)(void *, const char *, int), void *arg, int useExtended, const char *fmt, va_list ap)
char * switch_snprintfv(char *zBuf, int n, const char *zFormat,...)
char * switch_vmprintf(const char *zFormat, va_list ap)