1a18c5681Sdrh /* 2a18c5681Sdrh ** The "printf" code that follows dates from the 1980's. It is in 3a18c5681Sdrh ** the public domain. The original comments are included here for 4e84a306bSdrh ** completeness. They are very out-of-date but might be useful as 5e84a306bSdrh ** an historical reference. Most of the "enhancements" have been backed 6e84a306bSdrh ** out so that the functionality is now the same as standard printf(). 7e84a306bSdrh ** 8e84a306bSdrh ************************************************************************** 9a18c5681Sdrh ** 10ed1fddf4Sdrh ** This file contains code for a set of "printf"-like routines. These 11ed1fddf4Sdrh ** routines format strings much like the printf() from the standard C 12ed1fddf4Sdrh ** library, though the implementation here has enhancements to support 13ed1fddf4Sdrh ** SQLlite. 14a18c5681Sdrh */ 15a18c5681Sdrh #include "sqliteInt.h" 167c68d60bSdrh 17a18c5681Sdrh /* 18a18c5681Sdrh ** Conversion types fall into various categories as defined by the 19a18c5681Sdrh ** following enumeration. 20a18c5681Sdrh */ 21e84a306bSdrh #define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */ 22e84a306bSdrh #define etFLOAT 2 /* Floating point. %f */ 23e84a306bSdrh #define etEXP 3 /* Exponentional notation. %e and %E */ 24e84a306bSdrh #define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */ 25e84a306bSdrh #define etSIZE 5 /* Return number of characters processed so far. %n */ 26e84a306bSdrh #define etSTRING 6 /* Strings. %s */ 27e84a306bSdrh #define etDYNSTRING 7 /* Dynamically allocated strings. %z */ 28e84a306bSdrh #define etPERCENT 8 /* Percent symbol. %% */ 29e84a306bSdrh #define etCHARX 9 /* Characters. %c */ 30a18c5681Sdrh /* The rest are extensions, not normally found in printf() */ 31af005fbcSdrh #define etSQLESCAPE 10 /* Strings with '\'' doubled. %q */ 32af005fbcSdrh #define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '', 330cfcf3fbSchw NULL pointers replaced by SQL NULL. %Q */ 34af005fbcSdrh #define etTOKEN 12 /* a pointer to a Token structure */ 35af005fbcSdrh #define etSRCLIST 13 /* a pointer to a SrcList */ 36af005fbcSdrh #define etPOINTER 14 /* The %p conversion */ 37af005fbcSdrh #define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */ 38af005fbcSdrh #define etORDINAL 16 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */ 39e84a306bSdrh 40874ba04cSdrh #define etINVALID 0 /* Any unrecognized conversion type */ 41874ba04cSdrh 42e84a306bSdrh 43e84a306bSdrh /* 44e84a306bSdrh ** An "etByte" is an 8-bit unsigned value. 45e84a306bSdrh */ 46e84a306bSdrh typedef unsigned char etByte; 47a18c5681Sdrh 48a18c5681Sdrh /* 49a18c5681Sdrh ** Each builtin conversion character (ex: the 'd' in "%d") is described 50a18c5681Sdrh ** by an instance of the following structure 51a18c5681Sdrh */ 52a18c5681Sdrh typedef struct et_info { /* Information about each format field */ 53e84a306bSdrh char fmttype; /* The format field code letter */ 54e84a306bSdrh etByte base; /* The base for radix conversion */ 55e84a306bSdrh etByte flags; /* One or more of FLAG_ constants below */ 56e84a306bSdrh etByte type; /* Conversion paradigm */ 5776ff3a0eSdrh etByte charset; /* Offset into aDigits[] of the digits string */ 5876ff3a0eSdrh etByte prefix; /* Offset into aPrefix[] of the prefix string */ 59a18c5681Sdrh } et_info; 60a18c5681Sdrh 61a18c5681Sdrh /* 62e84a306bSdrh ** Allowed values for et_info.flags 63e84a306bSdrh */ 64e84a306bSdrh #define FLAG_SIGNED 1 /* True if the value to convert is signed */ 65e84a306bSdrh #define FLAG_INTERN 2 /* True if for internal use only */ 664794f735Sdrh #define FLAG_STRING 4 /* Allow infinity precision */ 67e84a306bSdrh 68e84a306bSdrh 69e84a306bSdrh /* 70a18c5681Sdrh ** The following table is searched linearly, so it is good to put the 71a18c5681Sdrh ** most frequently used conversion types first. 72a18c5681Sdrh */ 7376ff3a0eSdrh static const char aDigits[] = "0123456789ABCDEF0123456789abcdef"; 7476ff3a0eSdrh static const char aPrefix[] = "-x0\000X0"; 755719628aSdrh static const et_info fmtinfo[] = { 7676ff3a0eSdrh { 'd', 10, 1, etRADIX, 0, 0 }, 774794f735Sdrh { 's', 0, 4, etSTRING, 0, 0 }, 78557cc60fSdrh { 'g', 0, 1, etGENERIC, 30, 0 }, 79153c62c4Sdrh { 'z', 0, 4, etDYNSTRING, 0, 0 }, 804794f735Sdrh { 'q', 0, 4, etSQLESCAPE, 0, 0 }, 814794f735Sdrh { 'Q', 0, 4, etSQLESCAPE2, 0, 0 }, 82f3b863edSdanielk1977 { 'w', 0, 4, etSQLESCAPE3, 0, 0 }, 83e84a306bSdrh { 'c', 0, 0, etCHARX, 0, 0 }, 8476ff3a0eSdrh { 'o', 8, 0, etRADIX, 0, 2 }, 8576ff3a0eSdrh { 'u', 10, 0, etRADIX, 0, 0 }, 8676ff3a0eSdrh { 'x', 16, 0, etRADIX, 16, 1 }, 8776ff3a0eSdrh { 'X', 16, 0, etRADIX, 0, 4 }, 88b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 89e84a306bSdrh { 'f', 0, 1, etFLOAT, 0, 0 }, 9076ff3a0eSdrh { 'e', 0, 1, etEXP, 30, 0 }, 9176ff3a0eSdrh { 'E', 0, 1, etEXP, 14, 0 }, 9276ff3a0eSdrh { 'G', 0, 1, etGENERIC, 14, 0 }, 93b37df7b9Sdrh #endif 9476ff3a0eSdrh { 'i', 10, 1, etRADIX, 0, 0 }, 95e84a306bSdrh { 'n', 0, 0, etSIZE, 0, 0 }, 96e84a306bSdrh { '%', 0, 0, etPERCENT, 0, 0 }, 9776ff3a0eSdrh { 'p', 16, 0, etPOINTER, 0, 1 }, 987e3ff5d8Sdrh 997e3ff5d8Sdrh /* All the rest have the FLAG_INTERN bit set and are thus for internal 1007e3ff5d8Sdrh ** use only */ 1015f968436Sdrh { 'T', 0, 2, etTOKEN, 0, 0 }, 1025f968436Sdrh { 'S', 0, 2, etSRCLIST, 0, 0 }, 1039a99334dSdrh { 'r', 10, 3, etORDINAL, 0, 0 }, 104a18c5681Sdrh }; 105a18c5681Sdrh 106a18c5681Sdrh /* 107b37df7b9Sdrh ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point 108a18c5681Sdrh ** conversions will work. 109a18c5681Sdrh */ 110b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 111a18c5681Sdrh /* 112a18c5681Sdrh ** "*val" is a double such that 0.1 <= *val < 10.0 113a18c5681Sdrh ** Return the ascii code for the leading digit of *val, then 114a18c5681Sdrh ** multiply "*val" by 10.0 to renormalize. 115a18c5681Sdrh ** 116a18c5681Sdrh ** Example: 117a18c5681Sdrh ** input: *val = 3.14159 118a18c5681Sdrh ** output: *val = 1.4159 function return = '3' 119a18c5681Sdrh ** 120a18c5681Sdrh ** The counter *cnt is incremented each time. After counter exceeds 121a18c5681Sdrh ** 16 (the number of significant digits in a 64-bit float) '0' is 122a18c5681Sdrh ** always returned. 123a18c5681Sdrh */ 124ea678832Sdrh static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ 125a18c5681Sdrh int digit; 126384eef32Sdrh LONGDOUBLE_TYPE d; 12772b3fbc7Sdrh if( (*cnt)<=0 ) return '0'; 12872b3fbc7Sdrh (*cnt)--; 129a18c5681Sdrh digit = (int)*val; 130a18c5681Sdrh d = digit; 131a18c5681Sdrh digit += '0'; 132a18c5681Sdrh *val = (*val - d)*10.0; 133ea678832Sdrh return (char)digit; 134a18c5681Sdrh } 135b37df7b9Sdrh #endif /* SQLITE_OMIT_FLOATING_POINT */ 136a18c5681Sdrh 13779158e18Sdrh /* 138ade86483Sdrh ** Append N space characters to the given string buffer. 139ade86483Sdrh */ 1407e02e5e6Sdrh void sqlite3AppendSpace(StrAccum *pAccum, int N){ 141ade86483Sdrh static const char zSpaces[] = " "; 14200e13613Sdanielk1977 while( N>=(int)sizeof(zSpaces)-1 ){ 143ade86483Sdrh sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1); 144ade86483Sdrh N -= sizeof(zSpaces)-1; 145ade86483Sdrh } 146ade86483Sdrh if( N>0 ){ 147ade86483Sdrh sqlite3StrAccumAppend(pAccum, zSpaces, N); 148ade86483Sdrh } 149ade86483Sdrh } 150ade86483Sdrh 151ade86483Sdrh /* 15279158e18Sdrh ** On machines with a small stack size, you can redefine the 153ed1fddf4Sdrh ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired. 15479158e18Sdrh */ 15579158e18Sdrh #ifndef SQLITE_PRINT_BUF_SIZE 15659eedf79Sdrh # define SQLITE_PRINT_BUF_SIZE 70 15750d654daSdrh #endif 15879158e18Sdrh #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ 159a18c5681Sdrh 160a18c5681Sdrh /* 161ed1fddf4Sdrh ** Render a string given by "fmt" into the StrAccum object. 162a18c5681Sdrh */ 163f089aa45Sdrh void sqlite3VXPrintf( 164ade86483Sdrh StrAccum *pAccum, /* Accumulate results here */ 1655f968436Sdrh int useExtended, /* Allow extended %-conversions */ 1665f968436Sdrh const char *fmt, /* Format string */ 1675f968436Sdrh va_list ap /* arguments */ 168a18c5681Sdrh ){ 169e84a306bSdrh int c; /* Next character in the format string */ 170e84a306bSdrh char *bufpt; /* Pointer to the conversion buffer */ 171e84a306bSdrh int precision; /* Precision of the current field */ 172e84a306bSdrh int length; /* Length of the field */ 173e84a306bSdrh int idx; /* A general purpose loop counter */ 174a18c5681Sdrh int width; /* Width of the current field */ 175e84a306bSdrh etByte flag_leftjustify; /* True if "-" flag is present */ 176e84a306bSdrh etByte flag_plussign; /* True if "+" flag is present */ 177e84a306bSdrh etByte flag_blanksign; /* True if " " flag is present */ 178e84a306bSdrh etByte flag_alternateform; /* True if "#" flag is present */ 179531fe878Sdrh etByte flag_altform2; /* True if "!" flag is present */ 180e84a306bSdrh etByte flag_zeropad; /* True if field width constant starts with zero */ 181e84a306bSdrh etByte flag_long; /* True if "l" flag is present */ 182a34b6764Sdrh etByte flag_longlong; /* True if the "ll" flag is present */ 1833e9aeec0Sdrh etByte done; /* Loop termination flag */ 184ed1fddf4Sdrh etByte xtype = 0; /* Conversion paradigm */ 185ed1fddf4Sdrh char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ 18627436af7Sdrh sqlite_uint64 longvalue; /* Value for integer types */ 187384eef32Sdrh LONGDOUBLE_TYPE realvalue; /* Value for real types */ 1885719628aSdrh const et_info *infop; /* Pointer to the appropriate info structure */ 18959eedf79Sdrh char *zOut; /* Rendering buffer */ 19059eedf79Sdrh int nOut; /* Size of the rendering buffer */ 191ed1fddf4Sdrh char *zExtra; /* Malloced memory used by some conversion */ 192b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 193557cc60fSdrh int exp, e2; /* exponent of real numbers */ 194ed1fddf4Sdrh int nsd; /* Number of significant digits returned */ 195a18c5681Sdrh double rounder; /* Used for rounding floating point values */ 196e84a306bSdrh etByte flag_dp; /* True if decimal point should be shown */ 197e84a306bSdrh etByte flag_rtz; /* True if trailing zeros should be removed */ 198a18c5681Sdrh #endif 199ed1fddf4Sdrh char buf[etBUFSIZE]; /* Conversion buffer */ 200a18c5681Sdrh 201a18c5681Sdrh bufpt = 0; 202a18c5681Sdrh for(; (c=(*fmt))!=0; ++fmt){ 203a18c5681Sdrh if( c!='%' ){ 204e84a306bSdrh int amt; 205a18c5681Sdrh bufpt = (char *)fmt; 206a18c5681Sdrh amt = 1; 207a18c5681Sdrh while( (c=(*++fmt))!='%' && c!=0 ) amt++; 208ade86483Sdrh sqlite3StrAccumAppend(pAccum, bufpt, amt); 209a18c5681Sdrh if( c==0 ) break; 210a18c5681Sdrh } 211a18c5681Sdrh if( (c=(*++fmt))==0 ){ 212ade86483Sdrh sqlite3StrAccumAppend(pAccum, "%", 1); 213a18c5681Sdrh break; 214a18c5681Sdrh } 215a18c5681Sdrh /* Find out what flags are present */ 216a18c5681Sdrh flag_leftjustify = flag_plussign = flag_blanksign = 217557cc60fSdrh flag_alternateform = flag_altform2 = flag_zeropad = 0; 2183e9aeec0Sdrh done = 0; 219a18c5681Sdrh do{ 220a18c5681Sdrh switch( c ){ 2213e9aeec0Sdrh case '-': flag_leftjustify = 1; break; 2223e9aeec0Sdrh case '+': flag_plussign = 1; break; 2233e9aeec0Sdrh case ' ': flag_blanksign = 1; break; 2243e9aeec0Sdrh case '#': flag_alternateform = 1; break; 2253e9aeec0Sdrh case '!': flag_altform2 = 1; break; 2263e9aeec0Sdrh case '0': flag_zeropad = 1; break; 2273e9aeec0Sdrh default: done = 1; break; 228a18c5681Sdrh } 2293e9aeec0Sdrh }while( !done && (c=(*++fmt))!=0 ); 230a18c5681Sdrh /* Get the field width */ 231a18c5681Sdrh width = 0; 232a18c5681Sdrh if( c=='*' ){ 233a18c5681Sdrh width = va_arg(ap,int); 234a18c5681Sdrh if( width<0 ){ 235a18c5681Sdrh flag_leftjustify = 1; 236a18c5681Sdrh width = -width; 237a18c5681Sdrh } 238a18c5681Sdrh c = *++fmt; 239a18c5681Sdrh }else{ 24017a68934Sdrh while( c>='0' && c<='9' ){ 241a18c5681Sdrh width = width*10 + c - '0'; 242a18c5681Sdrh c = *++fmt; 243a18c5681Sdrh } 244a18c5681Sdrh } 245a18c5681Sdrh /* Get the precision */ 246a18c5681Sdrh if( c=='.' ){ 247a18c5681Sdrh precision = 0; 248a18c5681Sdrh c = *++fmt; 249a18c5681Sdrh if( c=='*' ){ 250a18c5681Sdrh precision = va_arg(ap,int); 251a18c5681Sdrh if( precision<0 ) precision = -precision; 252a18c5681Sdrh c = *++fmt; 253a18c5681Sdrh }else{ 25417a68934Sdrh while( c>='0' && c<='9' ){ 255a18c5681Sdrh precision = precision*10 + c - '0'; 256a18c5681Sdrh c = *++fmt; 257a18c5681Sdrh } 258a18c5681Sdrh } 259a18c5681Sdrh }else{ 260a18c5681Sdrh precision = -1; 261a18c5681Sdrh } 262a18c5681Sdrh /* Get the conversion type modifier */ 263a18c5681Sdrh if( c=='l' ){ 264a18c5681Sdrh flag_long = 1; 265a18c5681Sdrh c = *++fmt; 266a34b6764Sdrh if( c=='l' ){ 267a34b6764Sdrh flag_longlong = 1; 268a34b6764Sdrh c = *++fmt; 269a18c5681Sdrh }else{ 270a34b6764Sdrh flag_longlong = 0; 271a34b6764Sdrh } 272a34b6764Sdrh }else{ 273a34b6764Sdrh flag_long = flag_longlong = 0; 274a18c5681Sdrh } 275a18c5681Sdrh /* Fetch the info entry for the field */ 276874ba04cSdrh infop = &fmtinfo[0]; 277874ba04cSdrh xtype = etINVALID; 27800e13613Sdanielk1977 for(idx=0; idx<ArraySize(fmtinfo); idx++){ 279a18c5681Sdrh if( c==fmtinfo[idx].fmttype ){ 280a18c5681Sdrh infop = &fmtinfo[idx]; 2815f968436Sdrh if( useExtended || (infop->flags & FLAG_INTERN)==0 ){ 282e84a306bSdrh xtype = infop->type; 283c4413565Sdrh }else{ 284ade86483Sdrh return; 2855f968436Sdrh } 286a18c5681Sdrh break; 287a18c5681Sdrh } 288a18c5681Sdrh } 289a18c5681Sdrh zExtra = 0; 29043617e9aSdrh 291a18c5681Sdrh /* 292a18c5681Sdrh ** At this point, variables are initialized as follows: 293a18c5681Sdrh ** 294a18c5681Sdrh ** flag_alternateform TRUE if a '#' is present. 2953e9aeec0Sdrh ** flag_altform2 TRUE if a '!' is present. 296a18c5681Sdrh ** flag_plussign TRUE if a '+' is present. 297a18c5681Sdrh ** flag_leftjustify TRUE if a '-' is present or if the 298a18c5681Sdrh ** field width was negative. 299a18c5681Sdrh ** flag_zeropad TRUE if the width began with 0. 300a18c5681Sdrh ** flag_long TRUE if the letter 'l' (ell) prefixed 301a18c5681Sdrh ** the conversion character. 302a34b6764Sdrh ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed 303a34b6764Sdrh ** the conversion character. 304a18c5681Sdrh ** flag_blanksign TRUE if a ' ' is present. 305a18c5681Sdrh ** width The specified field width. This is 306a18c5681Sdrh ** always non-negative. Zero is the default. 307a18c5681Sdrh ** precision The specified precision. The default 308a18c5681Sdrh ** is -1. 309a18c5681Sdrh ** xtype The class of the conversion. 310a18c5681Sdrh ** infop Pointer to the appropriate info struct. 311a18c5681Sdrh */ 312a18c5681Sdrh switch( xtype ){ 313fe63d1c9Sdrh case etPOINTER: 314fe63d1c9Sdrh flag_longlong = sizeof(char*)==sizeof(i64); 315fe63d1c9Sdrh flag_long = sizeof(char*)==sizeof(long int); 316fe63d1c9Sdrh /* Fall through into the next case */ 3179a99334dSdrh case etORDINAL: 318a18c5681Sdrh case etRADIX: 319e84a306bSdrh if( infop->flags & FLAG_SIGNED ){ 320e9707671Sdrh i64 v; 321eeb23a4cSdrh if( flag_longlong ){ 322eeb23a4cSdrh v = va_arg(ap,i64); 323eeb23a4cSdrh }else if( flag_long ){ 324eeb23a4cSdrh v = va_arg(ap,long int); 325eeb23a4cSdrh }else{ 326eeb23a4cSdrh v = va_arg(ap,int); 327eeb23a4cSdrh } 328e9707671Sdrh if( v<0 ){ 329158b9cb9Sdrh if( v==SMALLEST_INT64 ){ 330158b9cb9Sdrh longvalue = ((u64)1)<<63; 331158b9cb9Sdrh }else{ 332158b9cb9Sdrh longvalue = -v; 333158b9cb9Sdrh } 334cfcdaefeSdanielk1977 prefix = '-'; 335cfcdaefeSdanielk1977 }else{ 336e9707671Sdrh longvalue = v; 337e9707671Sdrh if( flag_plussign ) prefix = '+'; 338a18c5681Sdrh else if( flag_blanksign ) prefix = ' '; 339a18c5681Sdrh else prefix = 0; 340cfcdaefeSdanielk1977 } 341e9707671Sdrh }else{ 342eeb23a4cSdrh if( flag_longlong ){ 343eeb23a4cSdrh longvalue = va_arg(ap,u64); 344eeb23a4cSdrh }else if( flag_long ){ 345eeb23a4cSdrh longvalue = va_arg(ap,unsigned long int); 346eeb23a4cSdrh }else{ 347eeb23a4cSdrh longvalue = va_arg(ap,unsigned int); 348eeb23a4cSdrh } 349e9707671Sdrh prefix = 0; 350e9707671Sdrh } 351e9707671Sdrh if( longvalue==0 ) flag_alternateform = 0; 352a18c5681Sdrh if( flag_zeropad && precision<width-(prefix!=0) ){ 353a18c5681Sdrh precision = width-(prefix!=0); 354a18c5681Sdrh } 35559eedf79Sdrh if( precision<etBUFSIZE-10 ){ 35659eedf79Sdrh nOut = etBUFSIZE; 35759eedf79Sdrh zOut = buf; 35859eedf79Sdrh }else{ 35959eedf79Sdrh nOut = precision + 10; 36059eedf79Sdrh zOut = zExtra = sqlite3Malloc( nOut ); 36159eedf79Sdrh if( zOut==0 ){ 36259eedf79Sdrh pAccum->mallocFailed = 1; 36359eedf79Sdrh return; 36459eedf79Sdrh } 36559eedf79Sdrh } 36659eedf79Sdrh bufpt = &zOut[nOut-1]; 3679a99334dSdrh if( xtype==etORDINAL ){ 36843f6e064Sdrh static const char zOrd[] = "thstndrd"; 369ea678832Sdrh int x = (int)(longvalue % 10); 37043f6e064Sdrh if( x>=4 || (longvalue/10)%10==1 ){ 37143f6e064Sdrh x = 0; 37243f6e064Sdrh } 37359eedf79Sdrh *(--bufpt) = zOrd[x*2+1]; 37459eedf79Sdrh *(--bufpt) = zOrd[x*2]; 3759a99334dSdrh } 376a18c5681Sdrh { 37776ff3a0eSdrh register const char *cset; /* Use registers for speed */ 378a18c5681Sdrh register int base; 37976ff3a0eSdrh cset = &aDigits[infop->charset]; 380a18c5681Sdrh base = infop->base; 381a18c5681Sdrh do{ /* Convert to ascii */ 382a18c5681Sdrh *(--bufpt) = cset[longvalue%base]; 383a18c5681Sdrh longvalue = longvalue/base; 384a18c5681Sdrh }while( longvalue>0 ); 385a18c5681Sdrh } 38659eedf79Sdrh length = (int)(&zOut[nOut-1]-bufpt); 387a18c5681Sdrh for(idx=precision-length; idx>0; idx--){ 388a18c5681Sdrh *(--bufpt) = '0'; /* Zero pad */ 389a18c5681Sdrh } 390a18c5681Sdrh if( prefix ) *(--bufpt) = prefix; /* Add sign */ 391a18c5681Sdrh if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ 39276ff3a0eSdrh const char *pre; 39376ff3a0eSdrh char x; 39476ff3a0eSdrh pre = &aPrefix[infop->prefix]; 39576ff3a0eSdrh for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; 396a18c5681Sdrh } 39759eedf79Sdrh length = (int)(&zOut[nOut-1]-bufpt); 398a18c5681Sdrh break; 399a18c5681Sdrh case etFLOAT: 400a18c5681Sdrh case etEXP: 401a18c5681Sdrh case etGENERIC: 402a18c5681Sdrh realvalue = va_arg(ap,double); 40369ef7036Sdrh #ifdef SQLITE_OMIT_FLOATING_POINT 40469ef7036Sdrh length = 0; 40569ef7036Sdrh #else 406a18c5681Sdrh if( precision<0 ) precision = 6; /* Set default precision */ 407a18c5681Sdrh if( realvalue<0.0 ){ 408a18c5681Sdrh realvalue = -realvalue; 409a18c5681Sdrh prefix = '-'; 410a18c5681Sdrh }else{ 411a18c5681Sdrh if( flag_plussign ) prefix = '+'; 412a18c5681Sdrh else if( flag_blanksign ) prefix = ' '; 413a18c5681Sdrh else prefix = 0; 414a18c5681Sdrh } 4153e9aeec0Sdrh if( xtype==etGENERIC && precision>0 ) precision--; 41674161705Sdrh for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){} 4173e9aeec0Sdrh if( xtype==etFLOAT ) realvalue += rounder; 418a18c5681Sdrh /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ 419a18c5681Sdrh exp = 0; 420ea678832Sdrh if( sqlite3IsNaN((double)realvalue) ){ 42153c14021Sdrh bufpt = "NaN"; 42253c14021Sdrh length = 3; 42353c14021Sdrh break; 42453c14021Sdrh } 425a18c5681Sdrh if( realvalue>0.0 ){ 42672b3fbc7Sdrh LONGDOUBLE_TYPE scale = 1.0; 4274ef94130Sdrh while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;} 42872b3fbc7Sdrh while( realvalue>=1e64*scale && exp<=350 ){ scale *= 1e64; exp+=64; } 42972b3fbc7Sdrh while( realvalue>=1e8*scale && exp<=350 ){ scale *= 1e8; exp+=8; } 43072b3fbc7Sdrh while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; } 43172b3fbc7Sdrh realvalue /= scale; 432af005fbcSdrh while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } 433af005fbcSdrh while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } 434af005fbcSdrh if( exp>350 ){ 43553c14021Sdrh if( prefix=='-' ){ 43653c14021Sdrh bufpt = "-Inf"; 43753c14021Sdrh }else if( prefix=='+' ){ 43853c14021Sdrh bufpt = "+Inf"; 43953c14021Sdrh }else{ 44053c14021Sdrh bufpt = "Inf"; 44153c14021Sdrh } 442ea678832Sdrh length = sqlite3Strlen30(bufpt); 443a18c5681Sdrh break; 444a18c5681Sdrh } 445a18c5681Sdrh } 446a18c5681Sdrh bufpt = buf; 447a18c5681Sdrh /* 448a18c5681Sdrh ** If the field type is etGENERIC, then convert to either etEXP 449a18c5681Sdrh ** or etFLOAT, as appropriate. 450a18c5681Sdrh */ 451a18c5681Sdrh if( xtype!=etFLOAT ){ 452a18c5681Sdrh realvalue += rounder; 453a18c5681Sdrh if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } 454a18c5681Sdrh } 455a18c5681Sdrh if( xtype==etGENERIC ){ 456a18c5681Sdrh flag_rtz = !flag_alternateform; 457a18c5681Sdrh if( exp<-4 || exp>precision ){ 458a18c5681Sdrh xtype = etEXP; 459a18c5681Sdrh }else{ 460a18c5681Sdrh precision = precision - exp; 461a18c5681Sdrh xtype = etFLOAT; 462a18c5681Sdrh } 463a18c5681Sdrh }else{ 46472b3fbc7Sdrh flag_rtz = flag_altform2; 465a18c5681Sdrh } 466557cc60fSdrh if( xtype==etEXP ){ 467557cc60fSdrh e2 = 0; 468557cc60fSdrh }else{ 469557cc60fSdrh e2 = exp; 470557cc60fSdrh } 471*e8c13bf2Sdrh if( MAX(e2,0)+precision+width > etBUFSIZE - 15 ){ 472*e8c13bf2Sdrh bufpt = zExtra = sqlite3Malloc( MAX(e2,0)+precision+width+15 ); 47359eedf79Sdrh if( bufpt==0 ){ 47459eedf79Sdrh pAccum->mallocFailed = 1; 47559eedf79Sdrh return; 47659eedf79Sdrh } 47759eedf79Sdrh } 47859eedf79Sdrh zOut = bufpt; 47972b3fbc7Sdrh nsd = 16 + flag_altform2*10; 480ea678832Sdrh flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; 481557cc60fSdrh /* The sign in front of the number */ 482557cc60fSdrh if( prefix ){ 483557cc60fSdrh *(bufpt++) = prefix; 484557cc60fSdrh } 485557cc60fSdrh /* Digits prior to the decimal point */ 486557cc60fSdrh if( e2<0 ){ 487557cc60fSdrh *(bufpt++) = '0'; 488557cc60fSdrh }else{ 489557cc60fSdrh for(; e2>=0; e2--){ 490557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 491557cc60fSdrh } 492557cc60fSdrh } 493557cc60fSdrh /* The decimal point */ 494557cc60fSdrh if( flag_dp ){ 495557cc60fSdrh *(bufpt++) = '.'; 496557cc60fSdrh } 497557cc60fSdrh /* "0" digits after the decimal point but before the first 498557cc60fSdrh ** significant digit of the number */ 499af005fbcSdrh for(e2++; e2<0; precision--, e2++){ 500af005fbcSdrh assert( precision>0 ); 501a18c5681Sdrh *(bufpt++) = '0'; 502a18c5681Sdrh } 503557cc60fSdrh /* Significant digits after the decimal point */ 504557cc60fSdrh while( (precision--)>0 ){ 505557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 506a18c5681Sdrh } 507557cc60fSdrh /* Remove trailing zeros and the "." if no digits follow the "." */ 508557cc60fSdrh if( flag_rtz && flag_dp ){ 5093e9aeec0Sdrh while( bufpt[-1]=='0' ) *(--bufpt) = 0; 51059eedf79Sdrh assert( bufpt>zOut ); 5113e9aeec0Sdrh if( bufpt[-1]=='.' ){ 512557cc60fSdrh if( flag_altform2 ){ 513557cc60fSdrh *(bufpt++) = '0'; 514557cc60fSdrh }else{ 515557cc60fSdrh *(--bufpt) = 0; 516a18c5681Sdrh } 517557cc60fSdrh } 518557cc60fSdrh } 519557cc60fSdrh /* Add the "eNNN" suffix */ 52059eedf79Sdrh if( xtype==etEXP ){ 52176ff3a0eSdrh *(bufpt++) = aDigits[infop->charset]; 522557cc60fSdrh if( exp<0 ){ 523557cc60fSdrh *(bufpt++) = '-'; exp = -exp; 524557cc60fSdrh }else{ 525557cc60fSdrh *(bufpt++) = '+'; 526557cc60fSdrh } 527a18c5681Sdrh if( exp>=100 ){ 528ea678832Sdrh *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */ 529a18c5681Sdrh exp %= 100; 530a18c5681Sdrh } 531ea678832Sdrh *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */ 532ea678832Sdrh *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */ 533a18c5681Sdrh } 534557cc60fSdrh *bufpt = 0; 535557cc60fSdrh 536a18c5681Sdrh /* The converted number is in buf[] and zero terminated. Output it. 537a18c5681Sdrh ** Note that the number is in the usual order, not reversed as with 538a18c5681Sdrh ** integer conversions. */ 53959eedf79Sdrh length = (int)(bufpt-zOut); 54059eedf79Sdrh bufpt = zOut; 541a18c5681Sdrh 542a18c5681Sdrh /* Special case: Add leading zeros if the flag_zeropad flag is 543a18c5681Sdrh ** set and we are not left justified */ 544a18c5681Sdrh if( flag_zeropad && !flag_leftjustify && length < width){ 545a18c5681Sdrh int i; 546a18c5681Sdrh int nPad = width - length; 547a18c5681Sdrh for(i=width; i>=nPad; i--){ 548a18c5681Sdrh bufpt[i] = bufpt[i-nPad]; 549a18c5681Sdrh } 550a18c5681Sdrh i = prefix!=0; 551a18c5681Sdrh while( nPad-- ) bufpt[i++] = '0'; 552a18c5681Sdrh length = width; 553a18c5681Sdrh } 55469ef7036Sdrh #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */ 555a18c5681Sdrh break; 556a18c5681Sdrh case etSIZE: 557ade86483Sdrh *(va_arg(ap,int*)) = pAccum->nChar; 558a18c5681Sdrh length = width = 0; 559a18c5681Sdrh break; 560a18c5681Sdrh case etPERCENT: 561a18c5681Sdrh buf[0] = '%'; 562a18c5681Sdrh bufpt = buf; 563a18c5681Sdrh length = 1; 564a18c5681Sdrh break; 565a18c5681Sdrh case etCHARX: 566ea678832Sdrh c = va_arg(ap,int); 567ea678832Sdrh buf[0] = (char)c; 568a18c5681Sdrh if( precision>=0 ){ 569ea678832Sdrh for(idx=1; idx<precision; idx++) buf[idx] = (char)c; 570a18c5681Sdrh length = precision; 571a18c5681Sdrh }else{ 572a18c5681Sdrh length =1; 573a18c5681Sdrh } 574a18c5681Sdrh bufpt = buf; 575a18c5681Sdrh break; 576a18c5681Sdrh case etSTRING: 577d93d8a81Sdrh case etDYNSTRING: 578cb485882Sdrh bufpt = va_arg(ap,char*); 579d93d8a81Sdrh if( bufpt==0 ){ 580d93d8a81Sdrh bufpt = ""; 581d93d8a81Sdrh }else if( xtype==etDYNSTRING ){ 582d93d8a81Sdrh zExtra = bufpt; 583d93d8a81Sdrh } 584e509094bSdrh if( precision>=0 ){ 585e509094bSdrh for(length=0; length<precision && bufpt[length]; length++){} 586e509094bSdrh }else{ 587ea678832Sdrh length = sqlite3Strlen30(bufpt); 588e509094bSdrh } 589a18c5681Sdrh break; 590a18c5681Sdrh case etSQLESCAPE: 591f3b863edSdanielk1977 case etSQLESCAPE2: 592f3b863edSdanielk1977 case etSQLESCAPE3: { 5938965b50eSdrh int i, j, k, n, isnull; 5944794f735Sdrh int needQuote; 595ea678832Sdrh char ch; 596f3b863edSdanielk1977 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */ 597f0113000Sdanielk1977 char *escarg = va_arg(ap,char*); 598f0113000Sdanielk1977 isnull = escarg==0; 599f0113000Sdanielk1977 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); 6008965b50eSdrh k = precision; 60160d4a304Sdan for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ 602f3b863edSdanielk1977 if( ch==q ) n++; 603a18c5681Sdrh } 6044794f735Sdrh needQuote = !isnull && xtype==etSQLESCAPE2; 6054794f735Sdrh n += i + 1 + needQuote*2; 606a18c5681Sdrh if( n>etBUFSIZE ){ 607e5ae5735Sdrh bufpt = zExtra = sqlite3Malloc( n ); 608d164fd34Sdrh if( bufpt==0 ){ 609d164fd34Sdrh pAccum->mallocFailed = 1; 610d164fd34Sdrh return; 611d164fd34Sdrh } 612a18c5681Sdrh }else{ 613a18c5681Sdrh bufpt = buf; 614a18c5681Sdrh } 6150cfcf3fbSchw j = 0; 616f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 6178965b50eSdrh k = i; 6188965b50eSdrh for(i=0; i<k; i++){ 6198965b50eSdrh bufpt[j++] = ch = escarg[i]; 620f3b863edSdanielk1977 if( ch==q ) bufpt[j++] = ch; 621a18c5681Sdrh } 622f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 623a18c5681Sdrh bufpt[j] = 0; 624a18c5681Sdrh length = j; 6258965b50eSdrh /* The precision in %q and %Q means how many input characters to 6268965b50eSdrh ** consume, not the length of the output... 6278965b50eSdrh ** if( precision>=0 && precision<length ) length = precision; */ 628a18c5681Sdrh break; 6295eba8c09Sdrh } 6305f968436Sdrh case etTOKEN: { 6315f968436Sdrh Token *pToken = va_arg(ap, Token*); 632af005fbcSdrh if( pToken ){ 633ade86483Sdrh sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n); 634ad6d9460Sdrh } 6355f968436Sdrh length = width = 0; 6365f968436Sdrh break; 6375f968436Sdrh } 6385f968436Sdrh case etSRCLIST: { 6395f968436Sdrh SrcList *pSrc = va_arg(ap, SrcList*); 6405f968436Sdrh int k = va_arg(ap, int); 6415f968436Sdrh struct SrcList_item *pItem = &pSrc->a[k]; 6425f968436Sdrh assert( k>=0 && k<pSrc->nSrc ); 64393a960a0Sdrh if( pItem->zDatabase ){ 644ade86483Sdrh sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1); 645ade86483Sdrh sqlite3StrAccumAppend(pAccum, ".", 1); 6465f968436Sdrh } 647ade86483Sdrh sqlite3StrAccumAppend(pAccum, pItem->zName, -1); 6485f968436Sdrh length = width = 0; 6495f968436Sdrh break; 6505f968436Sdrh } 651874ba04cSdrh default: { 652874ba04cSdrh assert( xtype==etINVALID ); 653874ba04cSdrh return; 654874ba04cSdrh } 655a18c5681Sdrh }/* End switch over the format type */ 656a18c5681Sdrh /* 657a18c5681Sdrh ** The text of the conversion is pointed to by "bufpt" and is 658a18c5681Sdrh ** "length" characters long. The field width is "width". Do 659a18c5681Sdrh ** the output. 660a18c5681Sdrh */ 661a18c5681Sdrh if( !flag_leftjustify ){ 662a18c5681Sdrh register int nspace; 663a18c5681Sdrh nspace = width-length; 664a18c5681Sdrh if( nspace>0 ){ 6657e02e5e6Sdrh sqlite3AppendSpace(pAccum, nspace); 666a18c5681Sdrh } 667a18c5681Sdrh } 668a18c5681Sdrh if( length>0 ){ 669ade86483Sdrh sqlite3StrAccumAppend(pAccum, bufpt, length); 670a18c5681Sdrh } 671a18c5681Sdrh if( flag_leftjustify ){ 672a18c5681Sdrh register int nspace; 673a18c5681Sdrh nspace = width-length; 674a18c5681Sdrh if( nspace>0 ){ 6757e02e5e6Sdrh sqlite3AppendSpace(pAccum, nspace); 676a18c5681Sdrh } 677a18c5681Sdrh } 6781e536953Sdanielk1977 sqlite3_free(zExtra); 679a18c5681Sdrh }/* End for loop over the format string */ 680a18c5681Sdrh } /* End of function */ 681a18c5681Sdrh 682a18c5681Sdrh /* 683ade86483Sdrh ** Append N bytes of text from z to the StrAccum object. 684a18c5681Sdrh */ 685ade86483Sdrh void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){ 686bc6160b0Sdrh assert( z!=0 || N==0 ); 687ade86483Sdrh if( p->tooBig | p->mallocFailed ){ 688bc6160b0Sdrh testcase(p->tooBig); 689bc6160b0Sdrh testcase(p->mallocFailed); 69017435752Sdrh return; 691ade86483Sdrh } 692b07028f7Sdrh assert( p->zText!=0 || p->nChar==0 ); 693ade86483Sdrh if( N<0 ){ 694ea678832Sdrh N = sqlite3Strlen30(z); 695ade86483Sdrh } 696bc6160b0Sdrh if( N==0 || NEVER(z==0) ){ 697ade86483Sdrh return; 698ade86483Sdrh } 699ade86483Sdrh if( p->nChar+N >= p->nAlloc ){ 700ade86483Sdrh char *zNew; 701ade86483Sdrh if( !p->useMalloc ){ 702ade86483Sdrh p->tooBig = 1; 703ade86483Sdrh N = p->nAlloc - p->nChar - 1; 704ade86483Sdrh if( N<=0 ){ 705ade86483Sdrh return; 7065f968436Sdrh } 707a18c5681Sdrh }else{ 708a9ef7097Sdan char *zOld = (p->zText==p->zBase ? 0 : p->zText); 70993a960a0Sdrh i64 szNew = p->nChar; 710b1a6c3c1Sdrh szNew += N + 1; 711b1a6c3c1Sdrh if( szNew > p->mxAlloc ){ 712ade86483Sdrh sqlite3StrAccumReset(p); 713ade86483Sdrh p->tooBig = 1; 714eaad32b1Sdrh return; 715b1a6c3c1Sdrh }else{ 716ea678832Sdrh p->nAlloc = (int)szNew; 717a18c5681Sdrh } 718b975598eSdrh if( p->useMalloc==1 ){ 719a9ef7097Sdan zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); 720b975598eSdrh }else{ 721a9ef7097Sdan zNew = sqlite3_realloc(zOld, p->nAlloc); 722b975598eSdrh } 72353f733c7Sdrh if( zNew ){ 724b07028f7Sdrh if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); 725ade86483Sdrh p->zText = zNew; 726ade86483Sdrh }else{ 727ade86483Sdrh p->mallocFailed = 1; 728ade86483Sdrh sqlite3StrAccumReset(p); 729ade86483Sdrh return; 73053f733c7Sdrh } 7315f968436Sdrh } 7325f968436Sdrh } 733b07028f7Sdrh assert( p->zText ); 734ade86483Sdrh memcpy(&p->zText[p->nChar], z, N); 735ade86483Sdrh p->nChar += N; 736483750baSdrh } 737483750baSdrh 738483750baSdrh /* 739ade86483Sdrh ** Finish off a string by making sure it is zero-terminated. 740ade86483Sdrh ** Return a pointer to the resulting string. Return a NULL 741ade86483Sdrh ** pointer if any kind of error was encountered. 7425f968436Sdrh */ 743ade86483Sdrh char *sqlite3StrAccumFinish(StrAccum *p){ 744ade86483Sdrh if( p->zText ){ 745ade86483Sdrh p->zText[p->nChar] = 0; 746ade86483Sdrh if( p->useMalloc && p->zText==p->zBase ){ 747b975598eSdrh if( p->useMalloc==1 ){ 748633e6d57Sdrh p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); 749b975598eSdrh }else{ 750b975598eSdrh p->zText = sqlite3_malloc(p->nChar+1); 751b975598eSdrh } 752ade86483Sdrh if( p->zText ){ 753ade86483Sdrh memcpy(p->zText, p->zBase, p->nChar+1); 754ade86483Sdrh }else{ 755ade86483Sdrh p->mallocFailed = 1; 756ade86483Sdrh } 757ade86483Sdrh } 758ade86483Sdrh } 759ade86483Sdrh return p->zText; 760ade86483Sdrh } 761ade86483Sdrh 762ade86483Sdrh /* 763ade86483Sdrh ** Reset an StrAccum string. Reclaim all malloced memory. 764ade86483Sdrh */ 765ade86483Sdrh void sqlite3StrAccumReset(StrAccum *p){ 766ade86483Sdrh if( p->zText!=p->zBase ){ 767b975598eSdrh if( p->useMalloc==1 ){ 768633e6d57Sdrh sqlite3DbFree(p->db, p->zText); 769b975598eSdrh }else{ 770b975598eSdrh sqlite3_free(p->zText); 771b975598eSdrh } 772ade86483Sdrh } 773f089aa45Sdrh p->zText = 0; 774ade86483Sdrh } 775ade86483Sdrh 776ade86483Sdrh /* 777ade86483Sdrh ** Initialize a string accumulator 778ade86483Sdrh */ 779f089aa45Sdrh void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){ 780ade86483Sdrh p->zText = p->zBase = zBase; 781633e6d57Sdrh p->db = 0; 782ade86483Sdrh p->nChar = 0; 783ade86483Sdrh p->nAlloc = n; 784bb4957f8Sdrh p->mxAlloc = mx; 785ade86483Sdrh p->useMalloc = 1; 786ade86483Sdrh p->tooBig = 0; 787ade86483Sdrh p->mallocFailed = 0; 7885f968436Sdrh } 7895f968436Sdrh 7905f968436Sdrh /* 7915f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 7925f968436Sdrh ** %-conversion extensions. 7935f968436Sdrh */ 79417435752Sdrh char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ 79517435752Sdrh char *z; 79679158e18Sdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 797ade86483Sdrh StrAccum acc; 798bc6160b0Sdrh assert( db!=0 ); 799bb4957f8Sdrh sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), 800bc6160b0Sdrh db->aLimit[SQLITE_LIMIT_LENGTH]); 801633e6d57Sdrh acc.db = db; 802f089aa45Sdrh sqlite3VXPrintf(&acc, 1, zFormat, ap); 803ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 804bc6160b0Sdrh if( acc.mallocFailed ){ 80517435752Sdrh db->mallocFailed = 1; 80617435752Sdrh } 80717435752Sdrh return z; 8085f968436Sdrh } 8095f968436Sdrh 8105f968436Sdrh /* 8115f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 8125f968436Sdrh ** %-conversion extensions. 8135f968436Sdrh */ 81417435752Sdrh char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ 8155f968436Sdrh va_list ap; 8165f968436Sdrh char *z; 8175f968436Sdrh va_start(ap, zFormat); 818ade86483Sdrh z = sqlite3VMPrintf(db, zFormat, ap); 8195f968436Sdrh va_end(ap); 8205f968436Sdrh return z; 8215f968436Sdrh } 8225f968436Sdrh 8235f968436Sdrh /* 824633e6d57Sdrh ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting 825633e6d57Sdrh ** the string and before returnning. This routine is intended to be used 826633e6d57Sdrh ** to modify an existing string. For example: 827633e6d57Sdrh ** 828633e6d57Sdrh ** x = sqlite3MPrintf(db, x, "prefix %s suffix", x); 829633e6d57Sdrh ** 830633e6d57Sdrh */ 831633e6d57Sdrh char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){ 832633e6d57Sdrh va_list ap; 833633e6d57Sdrh char *z; 834633e6d57Sdrh va_start(ap, zFormat); 835633e6d57Sdrh z = sqlite3VMPrintf(db, zFormat, ap); 836633e6d57Sdrh va_end(ap); 837633e6d57Sdrh sqlite3DbFree(db, zStr); 838633e6d57Sdrh return z; 839633e6d57Sdrh } 840633e6d57Sdrh 841633e6d57Sdrh /* 84228dd479cSdrh ** Print into memory obtained from sqlite3_malloc(). Omit the internal 84328dd479cSdrh ** %-conversion extensions. 84428dd479cSdrh */ 84528dd479cSdrh char *sqlite3_vmprintf(const char *zFormat, va_list ap){ 846ade86483Sdrh char *z; 84728dd479cSdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 848ade86483Sdrh StrAccum acc; 849ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT 850ff1590eeSdrh if( sqlite3_initialize() ) return 0; 851ff1590eeSdrh #endif 852bb4957f8Sdrh sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); 853b975598eSdrh acc.useMalloc = 2; 854f089aa45Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 855ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 856ade86483Sdrh return z; 85728dd479cSdrh } 85828dd479cSdrh 85928dd479cSdrh /* 86028dd479cSdrh ** Print into memory obtained from sqlite3_malloc()(). Omit the internal 86128dd479cSdrh ** %-conversion extensions. 862483750baSdrh */ 8636f8a503dSdanielk1977 char *sqlite3_mprintf(const char *zFormat, ...){ 864a18c5681Sdrh va_list ap; 8655f968436Sdrh char *z; 866ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT 867ff1590eeSdrh if( sqlite3_initialize() ) return 0; 868ff1590eeSdrh #endif 869a18c5681Sdrh va_start(ap, zFormat); 870b3738b6cSdrh z = sqlite3_vmprintf(zFormat, ap); 871a18c5681Sdrh va_end(ap); 8725f968436Sdrh return z; 873a18c5681Sdrh } 874a18c5681Sdrh 875a18c5681Sdrh /* 8766f8a503dSdanielk1977 ** sqlite3_snprintf() works like snprintf() except that it ignores the 87793a5c6bdSdrh ** current locale settings. This is important for SQLite because we 87893a5c6bdSdrh ** are not able to use a "," as the decimal point in place of "." as 87993a5c6bdSdrh ** specified by some locales. 880db26d4c9Sdrh ** 881db26d4c9Sdrh ** Oops: The first two arguments of sqlite3_snprintf() are backwards 882db26d4c9Sdrh ** from the snprintf() standard. Unfortunately, it is too late to change 883db26d4c9Sdrh ** this without breaking compatibility, so we just have to live with the 884db26d4c9Sdrh ** mistake. 885db26d4c9Sdrh ** 886db26d4c9Sdrh ** sqlite3_vsnprintf() is the varargs version. 88793a5c6bdSdrh */ 888db26d4c9Sdrh char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ 889db26d4c9Sdrh StrAccum acc; 890db26d4c9Sdrh if( n<=0 ) return zBuf; 891db26d4c9Sdrh sqlite3StrAccumInit(&acc, zBuf, n, 0); 892db26d4c9Sdrh acc.useMalloc = 0; 893db26d4c9Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 894db26d4c9Sdrh return sqlite3StrAccumFinish(&acc); 895db26d4c9Sdrh } 8966f8a503dSdanielk1977 char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ 8975f968436Sdrh char *z; 89893a5c6bdSdrh va_list ap; 89993a5c6bdSdrh va_start(ap,zFormat); 900db26d4c9Sdrh z = sqlite3_vsnprintf(n, zBuf, zFormat, ap); 90193a5c6bdSdrh va_end(ap); 9025f968436Sdrh return z; 90393a5c6bdSdrh } 90493a5c6bdSdrh 9053f280701Sdrh /* 9067c0c460fSdrh ** This is the routine that actually formats the sqlite3_log() message. 9077c0c460fSdrh ** We house it in a separate routine from sqlite3_log() to avoid using 9087c0c460fSdrh ** stack space on small-stack systems when logging is disabled. 9097c0c460fSdrh ** 9107c0c460fSdrh ** sqlite3_log() must render into a static buffer. It cannot dynamically 9117c0c460fSdrh ** allocate memory because it might be called while the memory allocator 9127c0c460fSdrh ** mutex is held. 9137c0c460fSdrh */ 9147c0c460fSdrh static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){ 9157c0c460fSdrh StrAccum acc; /* String accumulator */ 916a64fa912Sdrh char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */ 9177c0c460fSdrh 9187c0c460fSdrh sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0); 9197c0c460fSdrh acc.useMalloc = 0; 9207c0c460fSdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 9217c0c460fSdrh sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode, 9227c0c460fSdrh sqlite3StrAccumFinish(&acc)); 9237c0c460fSdrh } 9247c0c460fSdrh 9257c0c460fSdrh /* 9263f280701Sdrh ** Format and write a message to the log if logging is enabled. 9273f280701Sdrh */ 928a7564663Sdrh void sqlite3_log(int iErrCode, const char *zFormat, ...){ 9293f280701Sdrh va_list ap; /* Vararg list */ 9307c0c460fSdrh if( sqlite3GlobalConfig.xLog ){ 9313f280701Sdrh va_start(ap, zFormat); 9327c0c460fSdrh renderLogMsg(iErrCode, zFormat, ap); 9333f280701Sdrh va_end(ap); 9343f280701Sdrh } 9353f280701Sdrh } 9363f280701Sdrh 93785e9e22bSdrh #if defined(SQLITE_DEBUG) 938e54ca3feSdrh /* 939e54ca3feSdrh ** A version of printf() that understands %lld. Used for debugging. 940e54ca3feSdrh ** The printf() built into some versions of windows does not understand %lld 941e54ca3feSdrh ** and segfaults if you give it a long long int. 942e54ca3feSdrh */ 943e54ca3feSdrh void sqlite3DebugPrintf(const char *zFormat, ...){ 944e54ca3feSdrh va_list ap; 945ade86483Sdrh StrAccum acc; 946e54ca3feSdrh char zBuf[500]; 947bb4957f8Sdrh sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0); 948ade86483Sdrh acc.useMalloc = 0; 949e54ca3feSdrh va_start(ap,zFormat); 950f089aa45Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 951e54ca3feSdrh va_end(ap); 952bed8e7e5Sdrh sqlite3StrAccumFinish(&acc); 953485f0039Sdrh fprintf(stdout,"%s", zBuf); 9542ac3ee97Sdrh fflush(stdout); 955e54ca3feSdrh } 956e54ca3feSdrh #endif 957c7bc4fdeSdrh 958c7bc4fdeSdrh #ifndef SQLITE_OMIT_TRACE 959c7bc4fdeSdrh /* 960c7bc4fdeSdrh ** variable-argument wrapper around sqlite3VXPrintf(). 961c7bc4fdeSdrh */ 962c7bc4fdeSdrh void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){ 963c7bc4fdeSdrh va_list ap; 964c7bc4fdeSdrh va_start(ap,zFormat); 965c7bc4fdeSdrh sqlite3VXPrintf(p, 1, zFormat, ap); 966c7bc4fdeSdrh va_end(ap); 967c7bc4fdeSdrh } 968c7bc4fdeSdrh #endif 969