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 ** 10*ed1fddf4Sdrh ** This file contains code for a set of "printf"-like routines. These 11*ed1fddf4Sdrh ** routines format strings much like the printf() from the standard C 12*ed1fddf4Sdrh ** library, though the implementation here has enhancements to support 13*ed1fddf4Sdrh ** 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; 127a18c5681Sdrh if( (*cnt)++ >= 16 ) return '0'; 128a18c5681Sdrh digit = (int)*val; 129a18c5681Sdrh d = digit; 130a18c5681Sdrh digit += '0'; 131a18c5681Sdrh *val = (*val - d)*10.0; 132ea678832Sdrh return (char)digit; 133a18c5681Sdrh } 134b37df7b9Sdrh #endif /* SQLITE_OMIT_FLOATING_POINT */ 135a18c5681Sdrh 13679158e18Sdrh /* 137ade86483Sdrh ** Append N space characters to the given string buffer. 138ade86483Sdrh */ 139ade86483Sdrh static void appendSpace(StrAccum *pAccum, int N){ 140ade86483Sdrh static const char zSpaces[] = " "; 14100e13613Sdanielk1977 while( N>=(int)sizeof(zSpaces)-1 ){ 142ade86483Sdrh sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1); 143ade86483Sdrh N -= sizeof(zSpaces)-1; 144ade86483Sdrh } 145ade86483Sdrh if( N>0 ){ 146ade86483Sdrh sqlite3StrAccumAppend(pAccum, zSpaces, N); 147ade86483Sdrh } 148ade86483Sdrh } 149ade86483Sdrh 150ade86483Sdrh /* 15179158e18Sdrh ** On machines with a small stack size, you can redefine the 152*ed1fddf4Sdrh ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired. 15379158e18Sdrh */ 15479158e18Sdrh #ifndef SQLITE_PRINT_BUF_SIZE 15559eedf79Sdrh # define SQLITE_PRINT_BUF_SIZE 70 15650d654daSdrh #endif 15779158e18Sdrh #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ 158a18c5681Sdrh 159a18c5681Sdrh /* 160*ed1fddf4Sdrh ** Render a string given by "fmt" into the StrAccum object. 161a18c5681Sdrh */ 162f089aa45Sdrh void sqlite3VXPrintf( 163ade86483Sdrh StrAccum *pAccum, /* Accumulate results here */ 1645f968436Sdrh int useExtended, /* Allow extended %-conversions */ 1655f968436Sdrh const char *fmt, /* Format string */ 1665f968436Sdrh va_list ap /* arguments */ 167a18c5681Sdrh ){ 168e84a306bSdrh int c; /* Next character in the format string */ 169e84a306bSdrh char *bufpt; /* Pointer to the conversion buffer */ 170e84a306bSdrh int precision; /* Precision of the current field */ 171e84a306bSdrh int length; /* Length of the field */ 172e84a306bSdrh int idx; /* A general purpose loop counter */ 173a18c5681Sdrh int width; /* Width of the current field */ 174e84a306bSdrh etByte flag_leftjustify; /* True if "-" flag is present */ 175e84a306bSdrh etByte flag_plussign; /* True if "+" flag is present */ 176e84a306bSdrh etByte flag_blanksign; /* True if " " flag is present */ 177e84a306bSdrh etByte flag_alternateform; /* True if "#" flag is present */ 178531fe878Sdrh etByte flag_altform2; /* True if "!" flag is present */ 179e84a306bSdrh etByte flag_zeropad; /* True if field width constant starts with zero */ 180e84a306bSdrh etByte flag_long; /* True if "l" flag is present */ 181a34b6764Sdrh etByte flag_longlong; /* True if the "ll" flag is present */ 1823e9aeec0Sdrh etByte done; /* Loop termination flag */ 183*ed1fddf4Sdrh etByte xtype = 0; /* Conversion paradigm */ 184*ed1fddf4Sdrh char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ 18527436af7Sdrh sqlite_uint64 longvalue; /* Value for integer types */ 186384eef32Sdrh LONGDOUBLE_TYPE realvalue; /* Value for real types */ 1875719628aSdrh const et_info *infop; /* Pointer to the appropriate info structure */ 18859eedf79Sdrh char *zOut; /* Rendering buffer */ 18959eedf79Sdrh int nOut; /* Size of the rendering buffer */ 190*ed1fddf4Sdrh char *zExtra; /* Malloced memory used by some conversion */ 191b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 192557cc60fSdrh int exp, e2; /* exponent of real numbers */ 193*ed1fddf4Sdrh int nsd; /* Number of significant digits returned */ 194a18c5681Sdrh double rounder; /* Used for rounding floating point values */ 195e84a306bSdrh etByte flag_dp; /* True if decimal point should be shown */ 196e84a306bSdrh etByte flag_rtz; /* True if trailing zeros should be removed */ 197a18c5681Sdrh #endif 198*ed1fddf4Sdrh char buf[etBUFSIZE]; /* Conversion buffer */ 199a18c5681Sdrh 200ade86483Sdrh length = 0; 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--; 4165f968436Sdrh #if 0 417a18c5681Sdrh /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */ 418a18c5681Sdrh for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1); 419a18c5681Sdrh #else 420a18c5681Sdrh /* It makes more sense to use 0.5 */ 42174161705Sdrh for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){} 422a18c5681Sdrh #endif 4233e9aeec0Sdrh if( xtype==etFLOAT ) realvalue += rounder; 424a18c5681Sdrh /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ 425a18c5681Sdrh exp = 0; 426ea678832Sdrh if( sqlite3IsNaN((double)realvalue) ){ 42753c14021Sdrh bufpt = "NaN"; 42853c14021Sdrh length = 3; 42953c14021Sdrh break; 43053c14021Sdrh } 431a18c5681Sdrh if( realvalue>0.0 ){ 43263782855Sdrh while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; } 43363782855Sdrh while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; } 43463782855Sdrh while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; } 435af005fbcSdrh while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } 436af005fbcSdrh while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } 437af005fbcSdrh if( exp>350 ){ 43853c14021Sdrh if( prefix=='-' ){ 43953c14021Sdrh bufpt = "-Inf"; 44053c14021Sdrh }else if( prefix=='+' ){ 44153c14021Sdrh bufpt = "+Inf"; 44253c14021Sdrh }else{ 44353c14021Sdrh bufpt = "Inf"; 44453c14021Sdrh } 445ea678832Sdrh length = sqlite3Strlen30(bufpt); 446a18c5681Sdrh break; 447a18c5681Sdrh } 448a18c5681Sdrh } 449a18c5681Sdrh bufpt = buf; 450a18c5681Sdrh /* 451a18c5681Sdrh ** If the field type is etGENERIC, then convert to either etEXP 452a18c5681Sdrh ** or etFLOAT, as appropriate. 453a18c5681Sdrh */ 454a18c5681Sdrh if( xtype!=etFLOAT ){ 455a18c5681Sdrh realvalue += rounder; 456a18c5681Sdrh if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } 457a18c5681Sdrh } 458a18c5681Sdrh if( xtype==etGENERIC ){ 459a18c5681Sdrh flag_rtz = !flag_alternateform; 460a18c5681Sdrh if( exp<-4 || exp>precision ){ 461a18c5681Sdrh xtype = etEXP; 462a18c5681Sdrh }else{ 463a18c5681Sdrh precision = precision - exp; 464a18c5681Sdrh xtype = etFLOAT; 465a18c5681Sdrh } 466a18c5681Sdrh }else{ 467a18c5681Sdrh flag_rtz = 0; 468a18c5681Sdrh } 469557cc60fSdrh if( xtype==etEXP ){ 470557cc60fSdrh e2 = 0; 471557cc60fSdrh }else{ 472557cc60fSdrh e2 = exp; 473557cc60fSdrh } 47459eedf79Sdrh if( e2+precision+width > etBUFSIZE - 15 ){ 47559eedf79Sdrh bufpt = zExtra = sqlite3Malloc( e2+precision+width+15 ); 47659eedf79Sdrh if( bufpt==0 ){ 47759eedf79Sdrh pAccum->mallocFailed = 1; 47859eedf79Sdrh return; 47959eedf79Sdrh } 48059eedf79Sdrh } 48159eedf79Sdrh zOut = bufpt; 482a18c5681Sdrh nsd = 0; 483ea678832Sdrh flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; 484557cc60fSdrh /* The sign in front of the number */ 485557cc60fSdrh if( prefix ){ 486557cc60fSdrh *(bufpt++) = prefix; 487557cc60fSdrh } 488557cc60fSdrh /* Digits prior to the decimal point */ 489557cc60fSdrh if( e2<0 ){ 490557cc60fSdrh *(bufpt++) = '0'; 491557cc60fSdrh }else{ 492557cc60fSdrh for(; e2>=0; e2--){ 493557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 494557cc60fSdrh } 495557cc60fSdrh } 496557cc60fSdrh /* The decimal point */ 497557cc60fSdrh if( flag_dp ){ 498557cc60fSdrh *(bufpt++) = '.'; 499557cc60fSdrh } 500557cc60fSdrh /* "0" digits after the decimal point but before the first 501557cc60fSdrh ** significant digit of the number */ 502af005fbcSdrh for(e2++; e2<0; precision--, e2++){ 503af005fbcSdrh assert( precision>0 ); 504a18c5681Sdrh *(bufpt++) = '0'; 505a18c5681Sdrh } 506557cc60fSdrh /* Significant digits after the decimal point */ 507557cc60fSdrh while( (precision--)>0 ){ 508557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 509a18c5681Sdrh } 510557cc60fSdrh /* Remove trailing zeros and the "." if no digits follow the "." */ 511557cc60fSdrh if( flag_rtz && flag_dp ){ 5123e9aeec0Sdrh while( bufpt[-1]=='0' ) *(--bufpt) = 0; 51359eedf79Sdrh assert( bufpt>zOut ); 5143e9aeec0Sdrh if( bufpt[-1]=='.' ){ 515557cc60fSdrh if( flag_altform2 ){ 516557cc60fSdrh *(bufpt++) = '0'; 517557cc60fSdrh }else{ 518557cc60fSdrh *(--bufpt) = 0; 519a18c5681Sdrh } 520557cc60fSdrh } 521557cc60fSdrh } 522557cc60fSdrh /* Add the "eNNN" suffix */ 52359eedf79Sdrh if( xtype==etEXP ){ 52476ff3a0eSdrh *(bufpt++) = aDigits[infop->charset]; 525557cc60fSdrh if( exp<0 ){ 526557cc60fSdrh *(bufpt++) = '-'; exp = -exp; 527557cc60fSdrh }else{ 528557cc60fSdrh *(bufpt++) = '+'; 529557cc60fSdrh } 530a18c5681Sdrh if( exp>=100 ){ 531ea678832Sdrh *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */ 532a18c5681Sdrh exp %= 100; 533a18c5681Sdrh } 534ea678832Sdrh *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */ 535ea678832Sdrh *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */ 536a18c5681Sdrh } 537557cc60fSdrh *bufpt = 0; 538557cc60fSdrh 539a18c5681Sdrh /* The converted number is in buf[] and zero terminated. Output it. 540a18c5681Sdrh ** Note that the number is in the usual order, not reversed as with 541a18c5681Sdrh ** integer conversions. */ 54259eedf79Sdrh length = (int)(bufpt-zOut); 54359eedf79Sdrh bufpt = zOut; 544a18c5681Sdrh 545a18c5681Sdrh /* Special case: Add leading zeros if the flag_zeropad flag is 546a18c5681Sdrh ** set and we are not left justified */ 547a18c5681Sdrh if( flag_zeropad && !flag_leftjustify && length < width){ 548a18c5681Sdrh int i; 549a18c5681Sdrh int nPad = width - length; 550a18c5681Sdrh for(i=width; i>=nPad; i--){ 551a18c5681Sdrh bufpt[i] = bufpt[i-nPad]; 552a18c5681Sdrh } 553a18c5681Sdrh i = prefix!=0; 554a18c5681Sdrh while( nPad-- ) bufpt[i++] = '0'; 555a18c5681Sdrh length = width; 556a18c5681Sdrh } 55769ef7036Sdrh #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */ 558a18c5681Sdrh break; 559a18c5681Sdrh case etSIZE: 560ade86483Sdrh *(va_arg(ap,int*)) = pAccum->nChar; 561a18c5681Sdrh length = width = 0; 562a18c5681Sdrh break; 563a18c5681Sdrh case etPERCENT: 564a18c5681Sdrh buf[0] = '%'; 565a18c5681Sdrh bufpt = buf; 566a18c5681Sdrh length = 1; 567a18c5681Sdrh break; 568a18c5681Sdrh case etCHARX: 569ea678832Sdrh c = va_arg(ap,int); 570ea678832Sdrh buf[0] = (char)c; 571a18c5681Sdrh if( precision>=0 ){ 572ea678832Sdrh for(idx=1; idx<precision; idx++) buf[idx] = (char)c; 573a18c5681Sdrh length = precision; 574a18c5681Sdrh }else{ 575a18c5681Sdrh length =1; 576a18c5681Sdrh } 577a18c5681Sdrh bufpt = buf; 578a18c5681Sdrh break; 579a18c5681Sdrh case etSTRING: 580d93d8a81Sdrh case etDYNSTRING: 581cb485882Sdrh bufpt = va_arg(ap,char*); 582d93d8a81Sdrh if( bufpt==0 ){ 583d93d8a81Sdrh bufpt = ""; 584d93d8a81Sdrh }else if( xtype==etDYNSTRING ){ 585d93d8a81Sdrh zExtra = bufpt; 586d93d8a81Sdrh } 587e509094bSdrh if( precision>=0 ){ 588e509094bSdrh for(length=0; length<precision && bufpt[length]; length++){} 589e509094bSdrh }else{ 590ea678832Sdrh length = sqlite3Strlen30(bufpt); 591e509094bSdrh } 592a18c5681Sdrh break; 593a18c5681Sdrh case etSQLESCAPE: 594f3b863edSdanielk1977 case etSQLESCAPE2: 595f3b863edSdanielk1977 case etSQLESCAPE3: { 5968965b50eSdrh int i, j, k, n, isnull; 5974794f735Sdrh int needQuote; 598ea678832Sdrh char ch; 599f3b863edSdanielk1977 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */ 600f0113000Sdanielk1977 char *escarg = va_arg(ap,char*); 601f0113000Sdanielk1977 isnull = escarg==0; 602f0113000Sdanielk1977 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); 6038965b50eSdrh k = precision; 60460d4a304Sdan for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ 605f3b863edSdanielk1977 if( ch==q ) n++; 606a18c5681Sdrh } 6074794f735Sdrh needQuote = !isnull && xtype==etSQLESCAPE2; 6084794f735Sdrh n += i + 1 + needQuote*2; 609a18c5681Sdrh if( n>etBUFSIZE ){ 610e5ae5735Sdrh bufpt = zExtra = sqlite3Malloc( n ); 611d164fd34Sdrh if( bufpt==0 ){ 612d164fd34Sdrh pAccum->mallocFailed = 1; 613d164fd34Sdrh return; 614d164fd34Sdrh } 615a18c5681Sdrh }else{ 616a18c5681Sdrh bufpt = buf; 617a18c5681Sdrh } 6180cfcf3fbSchw j = 0; 619f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 6208965b50eSdrh k = i; 6218965b50eSdrh for(i=0; i<k; i++){ 6228965b50eSdrh bufpt[j++] = ch = escarg[i]; 623f3b863edSdanielk1977 if( ch==q ) bufpt[j++] = ch; 624a18c5681Sdrh } 625f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 626a18c5681Sdrh bufpt[j] = 0; 627a18c5681Sdrh length = j; 6288965b50eSdrh /* The precision in %q and %Q means how many input characters to 6298965b50eSdrh ** consume, not the length of the output... 6308965b50eSdrh ** if( precision>=0 && precision<length ) length = precision; */ 631a18c5681Sdrh break; 6325eba8c09Sdrh } 6335f968436Sdrh case etTOKEN: { 6345f968436Sdrh Token *pToken = va_arg(ap, Token*); 635af005fbcSdrh if( pToken ){ 636ade86483Sdrh sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n); 637ad6d9460Sdrh } 6385f968436Sdrh length = width = 0; 6395f968436Sdrh break; 6405f968436Sdrh } 6415f968436Sdrh case etSRCLIST: { 6425f968436Sdrh SrcList *pSrc = va_arg(ap, SrcList*); 6435f968436Sdrh int k = va_arg(ap, int); 6445f968436Sdrh struct SrcList_item *pItem = &pSrc->a[k]; 6455f968436Sdrh assert( k>=0 && k<pSrc->nSrc ); 64693a960a0Sdrh if( pItem->zDatabase ){ 647ade86483Sdrh sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1); 648ade86483Sdrh sqlite3StrAccumAppend(pAccum, ".", 1); 6495f968436Sdrh } 650ade86483Sdrh sqlite3StrAccumAppend(pAccum, pItem->zName, -1); 6515f968436Sdrh length = width = 0; 6525f968436Sdrh break; 6535f968436Sdrh } 654874ba04cSdrh default: { 655874ba04cSdrh assert( xtype==etINVALID ); 656874ba04cSdrh return; 657874ba04cSdrh } 658a18c5681Sdrh }/* End switch over the format type */ 659a18c5681Sdrh /* 660a18c5681Sdrh ** The text of the conversion is pointed to by "bufpt" and is 661a18c5681Sdrh ** "length" characters long. The field width is "width". Do 662a18c5681Sdrh ** the output. 663a18c5681Sdrh */ 664a18c5681Sdrh if( !flag_leftjustify ){ 665a18c5681Sdrh register int nspace; 666a18c5681Sdrh nspace = width-length; 667a18c5681Sdrh if( nspace>0 ){ 668ade86483Sdrh appendSpace(pAccum, nspace); 669a18c5681Sdrh } 670a18c5681Sdrh } 671a18c5681Sdrh if( length>0 ){ 672ade86483Sdrh sqlite3StrAccumAppend(pAccum, bufpt, length); 673a18c5681Sdrh } 674a18c5681Sdrh if( flag_leftjustify ){ 675a18c5681Sdrh register int nspace; 676a18c5681Sdrh nspace = width-length; 677a18c5681Sdrh if( nspace>0 ){ 678ade86483Sdrh appendSpace(pAccum, nspace); 679a18c5681Sdrh } 680a18c5681Sdrh } 6811e536953Sdanielk1977 sqlite3_free(zExtra); 682a18c5681Sdrh }/* End for loop over the format string */ 683a18c5681Sdrh } /* End of function */ 684a18c5681Sdrh 685a18c5681Sdrh /* 686ade86483Sdrh ** Append N bytes of text from z to the StrAccum object. 687a18c5681Sdrh */ 688ade86483Sdrh void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){ 689bc6160b0Sdrh assert( z!=0 || N==0 ); 690ade86483Sdrh if( p->tooBig | p->mallocFailed ){ 691bc6160b0Sdrh testcase(p->tooBig); 692bc6160b0Sdrh testcase(p->mallocFailed); 69317435752Sdrh return; 694ade86483Sdrh } 695ade86483Sdrh if( N<0 ){ 696ea678832Sdrh N = sqlite3Strlen30(z); 697ade86483Sdrh } 698bc6160b0Sdrh if( N==0 || NEVER(z==0) ){ 699ade86483Sdrh return; 700ade86483Sdrh } 701ade86483Sdrh if( p->nChar+N >= p->nAlloc ){ 702ade86483Sdrh char *zNew; 703ade86483Sdrh if( !p->useMalloc ){ 704ade86483Sdrh p->tooBig = 1; 705ade86483Sdrh N = p->nAlloc - p->nChar - 1; 706ade86483Sdrh if( N<=0 ){ 707ade86483Sdrh return; 7085f968436Sdrh } 709a18c5681Sdrh }else{ 710a9ef7097Sdan char *zOld = (p->zText==p->zBase ? 0 : p->zText); 71193a960a0Sdrh i64 szNew = p->nChar; 712b1a6c3c1Sdrh szNew += N + 1; 713b1a6c3c1Sdrh if( szNew > p->mxAlloc ){ 714ade86483Sdrh sqlite3StrAccumReset(p); 715ade86483Sdrh p->tooBig = 1; 716eaad32b1Sdrh return; 717b1a6c3c1Sdrh }else{ 718ea678832Sdrh p->nAlloc = (int)szNew; 719a18c5681Sdrh } 720b975598eSdrh if( p->useMalloc==1 ){ 721a9ef7097Sdan zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); 722b975598eSdrh }else{ 723a9ef7097Sdan zNew = sqlite3_realloc(zOld, p->nAlloc); 724b975598eSdrh } 72553f733c7Sdrh if( zNew ){ 726a9ef7097Sdan if( zOld==0 ) memcpy(zNew, p->zText, p->nChar); 727ade86483Sdrh p->zText = zNew; 728ade86483Sdrh }else{ 729ade86483Sdrh p->mallocFailed = 1; 730ade86483Sdrh sqlite3StrAccumReset(p); 731ade86483Sdrh return; 73253f733c7Sdrh } 7335f968436Sdrh } 7345f968436Sdrh } 735ade86483Sdrh memcpy(&p->zText[p->nChar], z, N); 736ade86483Sdrh p->nChar += N; 737483750baSdrh } 738483750baSdrh 739483750baSdrh /* 740ade86483Sdrh ** Finish off a string by making sure it is zero-terminated. 741ade86483Sdrh ** Return a pointer to the resulting string. Return a NULL 742ade86483Sdrh ** pointer if any kind of error was encountered. 7435f968436Sdrh */ 744ade86483Sdrh char *sqlite3StrAccumFinish(StrAccum *p){ 745ade86483Sdrh if( p->zText ){ 746ade86483Sdrh p->zText[p->nChar] = 0; 747ade86483Sdrh if( p->useMalloc && p->zText==p->zBase ){ 748b975598eSdrh if( p->useMalloc==1 ){ 749633e6d57Sdrh p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); 750b975598eSdrh }else{ 751b975598eSdrh p->zText = sqlite3_malloc(p->nChar+1); 752b975598eSdrh } 753ade86483Sdrh if( p->zText ){ 754ade86483Sdrh memcpy(p->zText, p->zBase, p->nChar+1); 755ade86483Sdrh }else{ 756ade86483Sdrh p->mallocFailed = 1; 757ade86483Sdrh } 758ade86483Sdrh } 759ade86483Sdrh } 760ade86483Sdrh return p->zText; 761ade86483Sdrh } 762ade86483Sdrh 763ade86483Sdrh /* 764ade86483Sdrh ** Reset an StrAccum string. Reclaim all malloced memory. 765ade86483Sdrh */ 766ade86483Sdrh void sqlite3StrAccumReset(StrAccum *p){ 767ade86483Sdrh if( p->zText!=p->zBase ){ 768b975598eSdrh if( p->useMalloc==1 ){ 769633e6d57Sdrh sqlite3DbFree(p->db, p->zText); 770b975598eSdrh }else{ 771b975598eSdrh sqlite3_free(p->zText); 772b975598eSdrh } 773ade86483Sdrh } 774f089aa45Sdrh p->zText = 0; 775ade86483Sdrh } 776ade86483Sdrh 777ade86483Sdrh /* 778ade86483Sdrh ** Initialize a string accumulator 779ade86483Sdrh */ 780f089aa45Sdrh void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){ 781ade86483Sdrh p->zText = p->zBase = zBase; 782633e6d57Sdrh p->db = 0; 783ade86483Sdrh p->nChar = 0; 784ade86483Sdrh p->nAlloc = n; 785bb4957f8Sdrh p->mxAlloc = mx; 786ade86483Sdrh p->useMalloc = 1; 787ade86483Sdrh p->tooBig = 0; 788ade86483Sdrh p->mallocFailed = 0; 7895f968436Sdrh } 7905f968436Sdrh 7915f968436Sdrh /* 7925f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 7935f968436Sdrh ** %-conversion extensions. 7945f968436Sdrh */ 79517435752Sdrh char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ 79617435752Sdrh char *z; 79779158e18Sdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 798ade86483Sdrh StrAccum acc; 799bc6160b0Sdrh assert( db!=0 ); 800bb4957f8Sdrh sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), 801bc6160b0Sdrh db->aLimit[SQLITE_LIMIT_LENGTH]); 802633e6d57Sdrh acc.db = db; 803f089aa45Sdrh sqlite3VXPrintf(&acc, 1, zFormat, ap); 804ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 805bc6160b0Sdrh if( acc.mallocFailed ){ 80617435752Sdrh db->mallocFailed = 1; 80717435752Sdrh } 80817435752Sdrh return z; 8095f968436Sdrh } 8105f968436Sdrh 8115f968436Sdrh /* 8125f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 8135f968436Sdrh ** %-conversion extensions. 8145f968436Sdrh */ 81517435752Sdrh char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ 8165f968436Sdrh va_list ap; 8175f968436Sdrh char *z; 8185f968436Sdrh va_start(ap, zFormat); 819ade86483Sdrh z = sqlite3VMPrintf(db, zFormat, ap); 8205f968436Sdrh va_end(ap); 8215f968436Sdrh return z; 8225f968436Sdrh } 8235f968436Sdrh 8245f968436Sdrh /* 825633e6d57Sdrh ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting 826633e6d57Sdrh ** the string and before returnning. This routine is intended to be used 827633e6d57Sdrh ** to modify an existing string. For example: 828633e6d57Sdrh ** 829633e6d57Sdrh ** x = sqlite3MPrintf(db, x, "prefix %s suffix", x); 830633e6d57Sdrh ** 831633e6d57Sdrh */ 832633e6d57Sdrh char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){ 833633e6d57Sdrh va_list ap; 834633e6d57Sdrh char *z; 835633e6d57Sdrh va_start(ap, zFormat); 836633e6d57Sdrh z = sqlite3VMPrintf(db, zFormat, ap); 837633e6d57Sdrh va_end(ap); 838633e6d57Sdrh sqlite3DbFree(db, zStr); 839633e6d57Sdrh return z; 840633e6d57Sdrh } 841633e6d57Sdrh 842633e6d57Sdrh /* 84328dd479cSdrh ** Print into memory obtained from sqlite3_malloc(). Omit the internal 84428dd479cSdrh ** %-conversion extensions. 84528dd479cSdrh */ 84628dd479cSdrh char *sqlite3_vmprintf(const char *zFormat, va_list ap){ 847ade86483Sdrh char *z; 84828dd479cSdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 849ade86483Sdrh StrAccum acc; 850ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT 851ff1590eeSdrh if( sqlite3_initialize() ) return 0; 852ff1590eeSdrh #endif 853bb4957f8Sdrh sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); 854b975598eSdrh acc.useMalloc = 2; 855f089aa45Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 856ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 857ade86483Sdrh return z; 85828dd479cSdrh } 85928dd479cSdrh 86028dd479cSdrh /* 86128dd479cSdrh ** Print into memory obtained from sqlite3_malloc()(). Omit the internal 86228dd479cSdrh ** %-conversion extensions. 863483750baSdrh */ 8646f8a503dSdanielk1977 char *sqlite3_mprintf(const char *zFormat, ...){ 865a18c5681Sdrh va_list ap; 8665f968436Sdrh char *z; 867ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT 868ff1590eeSdrh if( sqlite3_initialize() ) return 0; 869ff1590eeSdrh #endif 870a18c5681Sdrh va_start(ap, zFormat); 871b3738b6cSdrh z = sqlite3_vmprintf(zFormat, ap); 872a18c5681Sdrh va_end(ap); 8735f968436Sdrh return z; 874a18c5681Sdrh } 875a18c5681Sdrh 876a18c5681Sdrh /* 8776f8a503dSdanielk1977 ** sqlite3_snprintf() works like snprintf() except that it ignores the 87893a5c6bdSdrh ** current locale settings. This is important for SQLite because we 87993a5c6bdSdrh ** are not able to use a "," as the decimal point in place of "." as 88093a5c6bdSdrh ** specified by some locales. 881db26d4c9Sdrh ** 882db26d4c9Sdrh ** Oops: The first two arguments of sqlite3_snprintf() are backwards 883db26d4c9Sdrh ** from the snprintf() standard. Unfortunately, it is too late to change 884db26d4c9Sdrh ** this without breaking compatibility, so we just have to live with the 885db26d4c9Sdrh ** mistake. 886db26d4c9Sdrh ** 887db26d4c9Sdrh ** sqlite3_vsnprintf() is the varargs version. 88893a5c6bdSdrh */ 889db26d4c9Sdrh char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ 890db26d4c9Sdrh StrAccum acc; 891db26d4c9Sdrh if( n<=0 ) return zBuf; 892db26d4c9Sdrh sqlite3StrAccumInit(&acc, zBuf, n, 0); 893db26d4c9Sdrh acc.useMalloc = 0; 894db26d4c9Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 895db26d4c9Sdrh return sqlite3StrAccumFinish(&acc); 896db26d4c9Sdrh } 8976f8a503dSdanielk1977 char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ 8985f968436Sdrh char *z; 89993a5c6bdSdrh va_list ap; 90093a5c6bdSdrh va_start(ap,zFormat); 901db26d4c9Sdrh z = sqlite3_vsnprintf(n, zBuf, zFormat, ap); 90293a5c6bdSdrh va_end(ap); 9035f968436Sdrh return z; 90493a5c6bdSdrh } 90593a5c6bdSdrh 9063f280701Sdrh /* 9077c0c460fSdrh ** This is the routine that actually formats the sqlite3_log() message. 9087c0c460fSdrh ** We house it in a separate routine from sqlite3_log() to avoid using 9097c0c460fSdrh ** stack space on small-stack systems when logging is disabled. 9107c0c460fSdrh ** 9117c0c460fSdrh ** sqlite3_log() must render into a static buffer. It cannot dynamically 9127c0c460fSdrh ** allocate memory because it might be called while the memory allocator 9137c0c460fSdrh ** mutex is held. 9147c0c460fSdrh */ 9157c0c460fSdrh static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){ 9167c0c460fSdrh StrAccum acc; /* String accumulator */ 917a64fa912Sdrh char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */ 9187c0c460fSdrh 9197c0c460fSdrh sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0); 9207c0c460fSdrh acc.useMalloc = 0; 9217c0c460fSdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 9227c0c460fSdrh sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode, 9237c0c460fSdrh sqlite3StrAccumFinish(&acc)); 9247c0c460fSdrh } 9257c0c460fSdrh 9267c0c460fSdrh /* 9273f280701Sdrh ** Format and write a message to the log if logging is enabled. 9283f280701Sdrh */ 929a7564663Sdrh void sqlite3_log(int iErrCode, const char *zFormat, ...){ 9303f280701Sdrh va_list ap; /* Vararg list */ 9317c0c460fSdrh if( sqlite3GlobalConfig.xLog ){ 9323f280701Sdrh va_start(ap, zFormat); 9337c0c460fSdrh renderLogMsg(iErrCode, zFormat, ap); 9343f280701Sdrh va_end(ap); 9353f280701Sdrh } 9363f280701Sdrh } 9373f280701Sdrh 93885e9e22bSdrh #if defined(SQLITE_DEBUG) 939e54ca3feSdrh /* 940e54ca3feSdrh ** A version of printf() that understands %lld. Used for debugging. 941e54ca3feSdrh ** The printf() built into some versions of windows does not understand %lld 942e54ca3feSdrh ** and segfaults if you give it a long long int. 943e54ca3feSdrh */ 944e54ca3feSdrh void sqlite3DebugPrintf(const char *zFormat, ...){ 945e54ca3feSdrh va_list ap; 946ade86483Sdrh StrAccum acc; 947e54ca3feSdrh char zBuf[500]; 948bb4957f8Sdrh sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0); 949ade86483Sdrh acc.useMalloc = 0; 950e54ca3feSdrh va_start(ap,zFormat); 951f089aa45Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 952e54ca3feSdrh va_end(ap); 953bed8e7e5Sdrh sqlite3StrAccumFinish(&acc); 954485f0039Sdrh fprintf(stdout,"%s", zBuf); 9552ac3ee97Sdrh fflush(stdout); 956e54ca3feSdrh } 957e54ca3feSdrh #endif 958c7bc4fdeSdrh 959c7bc4fdeSdrh #ifndef SQLITE_OMIT_TRACE 960c7bc4fdeSdrh /* 961c7bc4fdeSdrh ** variable-argument wrapper around sqlite3VXPrintf(). 962c7bc4fdeSdrh */ 963c7bc4fdeSdrh void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){ 964c7bc4fdeSdrh va_list ap; 965c7bc4fdeSdrh va_start(ap,zFormat); 966c7bc4fdeSdrh sqlite3VXPrintf(p, 1, zFormat, ap); 967c7bc4fdeSdrh va_end(ap); 968c7bc4fdeSdrh } 969c7bc4fdeSdrh #endif 970