1a18c5681Sdrh /* 2a18c5681Sdrh ** The "printf" code that follows dates from the 1980's. It is in 338b4149cSdrh ** the public domain. 4e84a306bSdrh ** 5e84a306bSdrh ************************************************************************** 6a18c5681Sdrh ** 7ed1fddf4Sdrh ** This file contains code for a set of "printf"-like routines. These 8ed1fddf4Sdrh ** routines format strings much like the printf() from the standard C 9ed1fddf4Sdrh ** library, though the implementation here has enhancements to support 10f5b5f9b9Smistachkin ** SQLite. 11a18c5681Sdrh */ 12a18c5681Sdrh #include "sqliteInt.h" 137c68d60bSdrh 14a18c5681Sdrh /* 15a18c5681Sdrh ** Conversion types fall into various categories as defined by the 16a18c5681Sdrh ** following enumeration. 17a18c5681Sdrh */ 182c338a9dSdrh #define etRADIX 0 /* non-decimal integer types. %x %o */ 19ad5a9d71Sdrh #define etFLOAT 1 /* Floating point. %f */ 20ad5a9d71Sdrh #define etEXP 2 /* Exponentional notation. %e and %E */ 21ad5a9d71Sdrh #define etGENERIC 3 /* Floating or exponential, depending on exponent. %g */ 22ad5a9d71Sdrh #define etSIZE 4 /* Return number of characters processed so far. %n */ 23ad5a9d71Sdrh #define etSTRING 5 /* Strings. %s */ 24ad5a9d71Sdrh #define etDYNSTRING 6 /* Dynamically allocated strings. %z */ 25ad5a9d71Sdrh #define etPERCENT 7 /* Percent symbol. %% */ 26ad5a9d71Sdrh #define etCHARX 8 /* Characters. %c */ 27a18c5681Sdrh /* The rest are extensions, not normally found in printf() */ 28ad5a9d71Sdrh #define etSQLESCAPE 9 /* Strings with '\'' doubled. %q */ 29ad5a9d71Sdrh #define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '', 300cfcf3fbSchw NULL pointers replaced by SQL NULL. %Q */ 31ad5a9d71Sdrh #define etTOKEN 11 /* a pointer to a Token structure */ 32ad5a9d71Sdrh #define etSRCLIST 12 /* a pointer to a SrcList */ 33ad5a9d71Sdrh #define etPOINTER 13 /* The %p conversion */ 34ad5a9d71Sdrh #define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */ 35ad5a9d71Sdrh #define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */ 362c338a9dSdrh #define etDECIMAL 16 /* %d or %u, but not %x, %o */ 37e84a306bSdrh 382c338a9dSdrh #define etINVALID 17 /* Any unrecognized conversion type */ 39874ba04cSdrh 40e84a306bSdrh 41e84a306bSdrh /* 42e84a306bSdrh ** An "etByte" is an 8-bit unsigned value. 43e84a306bSdrh */ 44e84a306bSdrh typedef unsigned char etByte; 45a18c5681Sdrh 46a18c5681Sdrh /* 47a18c5681Sdrh ** Each builtin conversion character (ex: the 'd' in "%d") is described 48a18c5681Sdrh ** by an instance of the following structure 49a18c5681Sdrh */ 50a18c5681Sdrh typedef struct et_info { /* Information about each format field */ 51e84a306bSdrh char fmttype; /* The format field code letter */ 52e84a306bSdrh etByte base; /* The base for radix conversion */ 53e84a306bSdrh etByte flags; /* One or more of FLAG_ constants below */ 54e84a306bSdrh etByte type; /* Conversion paradigm */ 5576ff3a0eSdrh etByte charset; /* Offset into aDigits[] of the digits string */ 5676ff3a0eSdrh etByte prefix; /* Offset into aPrefix[] of the prefix string */ 57a18c5681Sdrh } et_info; 58a18c5681Sdrh 59a18c5681Sdrh /* 60e84a306bSdrh ** Allowed values for et_info.flags 61e84a306bSdrh */ 62e84a306bSdrh #define FLAG_SIGNED 1 /* True if the value to convert is signed */ 632c338a9dSdrh #define FLAG_STRING 4 /* Allow infinite precision */ 64e84a306bSdrh 65e84a306bSdrh 66e84a306bSdrh /* 67a18c5681Sdrh ** The following table is searched linearly, so it is good to put the 68a18c5681Sdrh ** most frequently used conversion types first. 69a18c5681Sdrh */ 7076ff3a0eSdrh static const char aDigits[] = "0123456789ABCDEF0123456789abcdef"; 7176ff3a0eSdrh static const char aPrefix[] = "-x0\000X0"; 725719628aSdrh static const et_info fmtinfo[] = { 732c338a9dSdrh { 'd', 10, 1, etDECIMAL, 0, 0 }, 744794f735Sdrh { 's', 0, 4, etSTRING, 0, 0 }, 75557cc60fSdrh { 'g', 0, 1, etGENERIC, 30, 0 }, 76153c62c4Sdrh { 'z', 0, 4, etDYNSTRING, 0, 0 }, 774794f735Sdrh { 'q', 0, 4, etSQLESCAPE, 0, 0 }, 784794f735Sdrh { 'Q', 0, 4, etSQLESCAPE2, 0, 0 }, 79f3b863edSdanielk1977 { 'w', 0, 4, etSQLESCAPE3, 0, 0 }, 80e84a306bSdrh { 'c', 0, 0, etCHARX, 0, 0 }, 8176ff3a0eSdrh { 'o', 8, 0, etRADIX, 0, 2 }, 822c338a9dSdrh { 'u', 10, 0, etDECIMAL, 0, 0 }, 8376ff3a0eSdrh { 'x', 16, 0, etRADIX, 16, 1 }, 8476ff3a0eSdrh { 'X', 16, 0, etRADIX, 0, 4 }, 85b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 86e84a306bSdrh { 'f', 0, 1, etFLOAT, 0, 0 }, 8776ff3a0eSdrh { 'e', 0, 1, etEXP, 30, 0 }, 8876ff3a0eSdrh { 'E', 0, 1, etEXP, 14, 0 }, 8976ff3a0eSdrh { 'G', 0, 1, etGENERIC, 14, 0 }, 90b37df7b9Sdrh #endif 912c338a9dSdrh { 'i', 10, 1, etDECIMAL, 0, 0 }, 92e84a306bSdrh { 'n', 0, 0, etSIZE, 0, 0 }, 93e84a306bSdrh { '%', 0, 0, etPERCENT, 0, 0 }, 9476ff3a0eSdrh { 'p', 16, 0, etPOINTER, 0, 1 }, 957e3ff5d8Sdrh 968236f688Sdrh /* All the rest are undocumented and are for internal use only */ 978236f688Sdrh { 'T', 0, 0, etTOKEN, 0, 0 }, 988236f688Sdrh { 'S', 0, 0, etSRCLIST, 0, 0 }, 998236f688Sdrh { 'r', 10, 1, etORDINAL, 0, 0 }, 100a18c5681Sdrh }; 101a18c5681Sdrh 102a18c5681Sdrh /* 103b37df7b9Sdrh ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point 104a18c5681Sdrh ** conversions will work. 105a18c5681Sdrh */ 106b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 107a18c5681Sdrh /* 108a18c5681Sdrh ** "*val" is a double such that 0.1 <= *val < 10.0 109a18c5681Sdrh ** Return the ascii code for the leading digit of *val, then 110a18c5681Sdrh ** multiply "*val" by 10.0 to renormalize. 111a18c5681Sdrh ** 112a18c5681Sdrh ** Example: 113a18c5681Sdrh ** input: *val = 3.14159 114a18c5681Sdrh ** output: *val = 1.4159 function return = '3' 115a18c5681Sdrh ** 116a18c5681Sdrh ** The counter *cnt is incremented each time. After counter exceeds 117a18c5681Sdrh ** 16 (the number of significant digits in a 64-bit float) '0' is 118a18c5681Sdrh ** always returned. 119a18c5681Sdrh */ 120ea678832Sdrh static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ 121a18c5681Sdrh int digit; 122384eef32Sdrh LONGDOUBLE_TYPE d; 12372b3fbc7Sdrh if( (*cnt)<=0 ) return '0'; 12472b3fbc7Sdrh (*cnt)--; 125a18c5681Sdrh digit = (int)*val; 126a18c5681Sdrh d = digit; 127a18c5681Sdrh digit += '0'; 128a18c5681Sdrh *val = (*val - d)*10.0; 129ea678832Sdrh return (char)digit; 130a18c5681Sdrh } 131b37df7b9Sdrh #endif /* SQLITE_OMIT_FLOATING_POINT */ 132a18c5681Sdrh 13379158e18Sdrh /* 134a6353a3fSdrh ** Set the StrAccum object to an error mode. 135a6353a3fSdrh */ 136a5c1416dSdrh static void setStrAccumError(StrAccum *p, u8 eError){ 1370cdbe1aeSdrh assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG ); 138a6353a3fSdrh p->accError = eError; 139a6353a3fSdrh p->nAlloc = 0; 140a6353a3fSdrh } 141a6353a3fSdrh 142a6353a3fSdrh /* 143a5c1416dSdrh ** Extra argument values from a PrintfArguments object 144a5c1416dSdrh */ 145a5c1416dSdrh static sqlite3_int64 getIntArg(PrintfArguments *p){ 146a5c1416dSdrh if( p->nArg<=p->nUsed ) return 0; 147a5c1416dSdrh return sqlite3_value_int64(p->apArg[p->nUsed++]); 148a5c1416dSdrh } 149a5c1416dSdrh static double getDoubleArg(PrintfArguments *p){ 150a5c1416dSdrh if( p->nArg<=p->nUsed ) return 0.0; 151a5c1416dSdrh return sqlite3_value_double(p->apArg[p->nUsed++]); 152a5c1416dSdrh } 153a5c1416dSdrh static char *getTextArg(PrintfArguments *p){ 154a5c1416dSdrh if( p->nArg<=p->nUsed ) return 0; 155a5c1416dSdrh return (char*)sqlite3_value_text(p->apArg[p->nUsed++]); 156a5c1416dSdrh } 157a5c1416dSdrh 158a5c1416dSdrh 159a5c1416dSdrh /* 16079158e18Sdrh ** On machines with a small stack size, you can redefine the 161ed1fddf4Sdrh ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired. 16279158e18Sdrh */ 16379158e18Sdrh #ifndef SQLITE_PRINT_BUF_SIZE 16459eedf79Sdrh # define SQLITE_PRINT_BUF_SIZE 70 16550d654daSdrh #endif 16679158e18Sdrh #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ 167a18c5681Sdrh 168a18c5681Sdrh /* 169ed1fddf4Sdrh ** Render a string given by "fmt" into the StrAccum object. 170a18c5681Sdrh */ 1710cdbe1aeSdrh void sqlite3_str_vappendf( 1720cdbe1aeSdrh sqlite3_str *pAccum, /* Accumulate results here */ 1735f968436Sdrh const char *fmt, /* Format string */ 1745f968436Sdrh va_list ap /* arguments */ 175a18c5681Sdrh ){ 176e84a306bSdrh int c; /* Next character in the format string */ 177e84a306bSdrh char *bufpt; /* Pointer to the conversion buffer */ 178e84a306bSdrh int precision; /* Precision of the current field */ 179e84a306bSdrh int length; /* Length of the field */ 180e84a306bSdrh int idx; /* A general purpose loop counter */ 181a18c5681Sdrh int width; /* Width of the current field */ 182e84a306bSdrh etByte flag_leftjustify; /* True if "-" flag is present */ 1832c338a9dSdrh etByte flag_prefix; /* '+' or ' ' or 0 for prefix */ 184e84a306bSdrh etByte flag_alternateform; /* True if "#" flag is present */ 185531fe878Sdrh etByte flag_altform2; /* True if "!" flag is present */ 186e84a306bSdrh etByte flag_zeropad; /* True if field width constant starts with zero */ 1872c338a9dSdrh etByte flag_long; /* 1 for the "l" flag, 2 for "ll", 0 by default */ 1883e9aeec0Sdrh etByte done; /* Loop termination flag */ 1892c338a9dSdrh etByte cThousand; /* Thousands separator for %d and %u */ 190ad5a9d71Sdrh etByte xtype = etINVALID; /* Conversion paradigm */ 191a5c1416dSdrh u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */ 192ed1fddf4Sdrh char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ 19327436af7Sdrh sqlite_uint64 longvalue; /* Value for integer types */ 194384eef32Sdrh LONGDOUBLE_TYPE realvalue; /* Value for real types */ 1955719628aSdrh const et_info *infop; /* Pointer to the appropriate info structure */ 19659eedf79Sdrh char *zOut; /* Rendering buffer */ 19759eedf79Sdrh int nOut; /* Size of the rendering buffer */ 198af8f513fSdrh char *zExtra = 0; /* Malloced memory used by some conversion */ 199b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 200557cc60fSdrh int exp, e2; /* exponent of real numbers */ 201ed1fddf4Sdrh int nsd; /* Number of significant digits returned */ 202a18c5681Sdrh double rounder; /* Used for rounding floating point values */ 203e84a306bSdrh etByte flag_dp; /* True if decimal point should be shown */ 204e84a306bSdrh etByte flag_rtz; /* True if trailing zeros should be removed */ 205a18c5681Sdrh #endif 206a5c1416dSdrh PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */ 207ed1fddf4Sdrh char buf[etBUFSIZE]; /* Conversion buffer */ 208a18c5681Sdrh 209cc398969Sdrh /* pAccum never starts out with an empty buffer that was obtained from 210cc398969Sdrh ** malloc(). This precondition is required by the mprintf("%z...") 211cc398969Sdrh ** optimization. */ 212cc398969Sdrh assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); 213cc398969Sdrh 214a18c5681Sdrh bufpt = 0; 2158236f688Sdrh if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){ 216a5c1416dSdrh pArgList = va_arg(ap, PrintfArguments*); 2178236f688Sdrh bArgList = 1; 218a5c1416dSdrh }else{ 2198236f688Sdrh bArgList = 0; 220a5c1416dSdrh } 221a18c5681Sdrh for(; (c=(*fmt))!=0; ++fmt){ 222a18c5681Sdrh if( c!='%' ){ 223a18c5681Sdrh bufpt = (char *)fmt; 224760b1598Sdrh #if HAVE_STRCHRNUL 225760b1598Sdrh fmt = strchrnul(fmt, '%'); 226760b1598Sdrh #else 227760b1598Sdrh do{ fmt++; }while( *fmt && *fmt != '%' ); 228760b1598Sdrh #endif 2290cdbe1aeSdrh sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt)); 230760b1598Sdrh if( *fmt==0 ) break; 231a18c5681Sdrh } 232a18c5681Sdrh if( (c=(*++fmt))==0 ){ 2330cdbe1aeSdrh sqlite3_str_append(pAccum, "%", 1); 234a18c5681Sdrh break; 235a18c5681Sdrh } 236a18c5681Sdrh /* Find out what flags are present */ 2372c338a9dSdrh flag_leftjustify = flag_prefix = cThousand = 238557cc60fSdrh flag_alternateform = flag_altform2 = flag_zeropad = 0; 2393e9aeec0Sdrh done = 0; 240a18c5681Sdrh do{ 241a18c5681Sdrh switch( c ){ 2423e9aeec0Sdrh case '-': flag_leftjustify = 1; break; 2432c338a9dSdrh case '+': flag_prefix = '+'; break; 2442c338a9dSdrh case ' ': flag_prefix = ' '; break; 2453e9aeec0Sdrh case '#': flag_alternateform = 1; break; 2463e9aeec0Sdrh case '!': flag_altform2 = 1; break; 2473e9aeec0Sdrh case '0': flag_zeropad = 1; break; 2482c338a9dSdrh case ',': cThousand = ','; break; 2493e9aeec0Sdrh default: done = 1; break; 250a18c5681Sdrh } 2513e9aeec0Sdrh }while( !done && (c=(*++fmt))!=0 ); 252a18c5681Sdrh /* Get the field width */ 253a18c5681Sdrh if( c=='*' ){ 254a5c1416dSdrh if( bArgList ){ 255a5c1416dSdrh width = (int)getIntArg(pArgList); 256a5c1416dSdrh }else{ 257a18c5681Sdrh width = va_arg(ap,int); 258a5c1416dSdrh } 259a18c5681Sdrh if( width<0 ){ 260a18c5681Sdrh flag_leftjustify = 1; 261b6f47debSdrh width = width >= -2147483647 ? -width : 0; 262a18c5681Sdrh } 263a18c5681Sdrh c = *++fmt; 264a18c5681Sdrh }else{ 265b6f47debSdrh unsigned wx = 0; 26617a68934Sdrh while( c>='0' && c<='9' ){ 267b6f47debSdrh wx = wx*10 + c - '0'; 268a18c5681Sdrh c = *++fmt; 269a18c5681Sdrh } 270b6f47debSdrh testcase( wx>0x7fffffff ); 271b6f47debSdrh width = wx & 0x7fffffff; 272a18c5681Sdrh } 273c386ef4fSdrh assert( width>=0 ); 274c386ef4fSdrh #ifdef SQLITE_PRINTF_PRECISION_LIMIT 275c386ef4fSdrh if( width>SQLITE_PRINTF_PRECISION_LIMIT ){ 276c386ef4fSdrh width = SQLITE_PRINTF_PRECISION_LIMIT; 277c386ef4fSdrh } 278c386ef4fSdrh #endif 2798c069147Sdan 280a18c5681Sdrh /* Get the precision */ 281a18c5681Sdrh if( c=='.' ){ 282a18c5681Sdrh c = *++fmt; 283a18c5681Sdrh if( c=='*' ){ 284a5c1416dSdrh if( bArgList ){ 285a5c1416dSdrh precision = (int)getIntArg(pArgList); 286a5c1416dSdrh }else{ 287a18c5681Sdrh precision = va_arg(ap,int); 288a5c1416dSdrh } 289a18c5681Sdrh c = *++fmt; 290b6f47debSdrh if( precision<0 ){ 291b6f47debSdrh precision = precision >= -2147483647 ? -precision : -1; 292b6f47debSdrh } 293a18c5681Sdrh }else{ 294b6f47debSdrh unsigned px = 0; 29517a68934Sdrh while( c>='0' && c<='9' ){ 296b6f47debSdrh px = px*10 + c - '0'; 297a18c5681Sdrh c = *++fmt; 298a18c5681Sdrh } 299b6f47debSdrh testcase( px>0x7fffffff ); 300b6f47debSdrh precision = px & 0x7fffffff; 301a18c5681Sdrh } 302a18c5681Sdrh }else{ 303a18c5681Sdrh precision = -1; 304a18c5681Sdrh } 305c386ef4fSdrh assert( precision>=(-1) ); 306c386ef4fSdrh #ifdef SQLITE_PRINTF_PRECISION_LIMIT 307c386ef4fSdrh if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){ 308c386ef4fSdrh precision = SQLITE_PRINTF_PRECISION_LIMIT; 309c386ef4fSdrh } 310c386ef4fSdrh #endif 311c386ef4fSdrh 312c386ef4fSdrh 313a18c5681Sdrh /* Get the conversion type modifier */ 314a18c5681Sdrh if( c=='l' ){ 315a18c5681Sdrh flag_long = 1; 316a18c5681Sdrh c = *++fmt; 317a34b6764Sdrh if( c=='l' ){ 3182c338a9dSdrh flag_long = 2; 319a34b6764Sdrh c = *++fmt; 320a34b6764Sdrh } 321a34b6764Sdrh }else{ 3222c338a9dSdrh flag_long = 0; 323a18c5681Sdrh } 324a18c5681Sdrh /* Fetch the info entry for the field */ 325874ba04cSdrh infop = &fmtinfo[0]; 326874ba04cSdrh xtype = etINVALID; 32700e13613Sdanielk1977 for(idx=0; idx<ArraySize(fmtinfo); idx++){ 328a18c5681Sdrh if( c==fmtinfo[idx].fmttype ){ 329a18c5681Sdrh infop = &fmtinfo[idx]; 330e84a306bSdrh xtype = infop->type; 331a18c5681Sdrh break; 332a18c5681Sdrh } 333a18c5681Sdrh } 33443617e9aSdrh 335a18c5681Sdrh /* 336a18c5681Sdrh ** At this point, variables are initialized as follows: 337a18c5681Sdrh ** 338a18c5681Sdrh ** flag_alternateform TRUE if a '#' is present. 3393e9aeec0Sdrh ** flag_altform2 TRUE if a '!' is present. 3402c338a9dSdrh ** flag_prefix '+' or ' ' or zero 341a18c5681Sdrh ** flag_leftjustify TRUE if a '-' is present or if the 342a18c5681Sdrh ** field width was negative. 343a18c5681Sdrh ** flag_zeropad TRUE if the width began with 0. 3442c338a9dSdrh ** flag_long 1 for "l", 2 for "ll" 345a18c5681Sdrh ** width The specified field width. This is 346a18c5681Sdrh ** always non-negative. Zero is the default. 347a18c5681Sdrh ** precision The specified precision. The default 348a18c5681Sdrh ** is -1. 349a18c5681Sdrh ** xtype The class of the conversion. 350a18c5681Sdrh ** infop Pointer to the appropriate info struct. 351a18c5681Sdrh */ 352a18c5681Sdrh switch( xtype ){ 353fe63d1c9Sdrh case etPOINTER: 3542c338a9dSdrh flag_long = sizeof(char*)==sizeof(i64) ? 2 : 3552c338a9dSdrh sizeof(char*)==sizeof(long int) ? 1 : 0; 356fe63d1c9Sdrh /* Fall through into the next case */ 3579a99334dSdrh case etORDINAL: 358a18c5681Sdrh case etRADIX: 3592c338a9dSdrh cThousand = 0; 3602c338a9dSdrh /* Fall through into the next case */ 3612c338a9dSdrh case etDECIMAL: 362e84a306bSdrh if( infop->flags & FLAG_SIGNED ){ 363e9707671Sdrh i64 v; 364a5c1416dSdrh if( bArgList ){ 365a5c1416dSdrh v = getIntArg(pArgList); 366eeb23a4cSdrh }else if( flag_long ){ 3672c338a9dSdrh if( flag_long==2 ){ 3682c338a9dSdrh v = va_arg(ap,i64) ; 3692c338a9dSdrh }else{ 370eeb23a4cSdrh v = va_arg(ap,long int); 3712c338a9dSdrh } 372eeb23a4cSdrh }else{ 373eeb23a4cSdrh v = va_arg(ap,int); 374eeb23a4cSdrh } 375e9707671Sdrh if( v<0 ){ 376158b9cb9Sdrh if( v==SMALLEST_INT64 ){ 377158b9cb9Sdrh longvalue = ((u64)1)<<63; 378158b9cb9Sdrh }else{ 379158b9cb9Sdrh longvalue = -v; 380158b9cb9Sdrh } 381cfcdaefeSdanielk1977 prefix = '-'; 382cfcdaefeSdanielk1977 }else{ 383e9707671Sdrh longvalue = v; 3842c338a9dSdrh prefix = flag_prefix; 385cfcdaefeSdanielk1977 } 386e9707671Sdrh }else{ 387a5c1416dSdrh if( bArgList ){ 388a5c1416dSdrh longvalue = (u64)getIntArg(pArgList); 389eeb23a4cSdrh }else if( flag_long ){ 3902c338a9dSdrh if( flag_long==2 ){ 3912c338a9dSdrh longvalue = va_arg(ap,u64); 3922c338a9dSdrh }else{ 393eeb23a4cSdrh longvalue = va_arg(ap,unsigned long int); 3942c338a9dSdrh } 395eeb23a4cSdrh }else{ 396eeb23a4cSdrh longvalue = va_arg(ap,unsigned int); 397eeb23a4cSdrh } 398e9707671Sdrh prefix = 0; 399e9707671Sdrh } 400e9707671Sdrh if( longvalue==0 ) flag_alternateform = 0; 401a18c5681Sdrh if( flag_zeropad && precision<width-(prefix!=0) ){ 402a18c5681Sdrh precision = width-(prefix!=0); 403a18c5681Sdrh } 4042c338a9dSdrh if( precision<etBUFSIZE-10-etBUFSIZE/3 ){ 40559eedf79Sdrh nOut = etBUFSIZE; 40659eedf79Sdrh zOut = buf; 40759eedf79Sdrh }else{ 4085f42995aSdrh u64 n = (u64)precision + 10 + precision/3; 4095f42995aSdrh zOut = zExtra = sqlite3Malloc( n ); 41059eedf79Sdrh if( zOut==0 ){ 4110cdbe1aeSdrh setStrAccumError(pAccum, SQLITE_NOMEM); 41259eedf79Sdrh return; 41359eedf79Sdrh } 4145f42995aSdrh nOut = (int)n; 41559eedf79Sdrh } 41659eedf79Sdrh bufpt = &zOut[nOut-1]; 4179a99334dSdrh if( xtype==etORDINAL ){ 41843f6e064Sdrh static const char zOrd[] = "thstndrd"; 419ea678832Sdrh int x = (int)(longvalue % 10); 42043f6e064Sdrh if( x>=4 || (longvalue/10)%10==1 ){ 42143f6e064Sdrh x = 0; 42243f6e064Sdrh } 42359eedf79Sdrh *(--bufpt) = zOrd[x*2+1]; 42459eedf79Sdrh *(--bufpt) = zOrd[x*2]; 4259a99334dSdrh } 426a18c5681Sdrh { 4270e682099Sdrh const char *cset = &aDigits[infop->charset]; 4280e682099Sdrh u8 base = infop->base; 429a18c5681Sdrh do{ /* Convert to ascii */ 430a18c5681Sdrh *(--bufpt) = cset[longvalue%base]; 431a18c5681Sdrh longvalue = longvalue/base; 432a18c5681Sdrh }while( longvalue>0 ); 433a18c5681Sdrh } 43459eedf79Sdrh length = (int)(&zOut[nOut-1]-bufpt); 4352c338a9dSdrh while( precision>length ){ 436a18c5681Sdrh *(--bufpt) = '0'; /* Zero pad */ 4372c338a9dSdrh length++; 4382c338a9dSdrh } 4392c338a9dSdrh if( cThousand ){ 4402c338a9dSdrh int nn = (length - 1)/3; /* Number of "," to insert */ 4412c338a9dSdrh int ix = (length - 1)%3 + 1; 4422c338a9dSdrh bufpt -= nn; 4432c338a9dSdrh for(idx=0; nn>0; idx++){ 4442c338a9dSdrh bufpt[idx] = bufpt[idx+nn]; 4452c338a9dSdrh ix--; 4462c338a9dSdrh if( ix==0 ){ 4472c338a9dSdrh bufpt[++idx] = cThousand; 4482c338a9dSdrh nn--; 4492c338a9dSdrh ix = 3; 4502c338a9dSdrh } 4512c338a9dSdrh } 452a18c5681Sdrh } 453a18c5681Sdrh if( prefix ) *(--bufpt) = prefix; /* Add sign */ 454a18c5681Sdrh if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ 45576ff3a0eSdrh const char *pre; 45676ff3a0eSdrh char x; 45776ff3a0eSdrh pre = &aPrefix[infop->prefix]; 45876ff3a0eSdrh for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; 459a18c5681Sdrh } 46059eedf79Sdrh length = (int)(&zOut[nOut-1]-bufpt); 461a18c5681Sdrh break; 462a18c5681Sdrh case etFLOAT: 463a18c5681Sdrh case etEXP: 464a18c5681Sdrh case etGENERIC: 465a5c1416dSdrh if( bArgList ){ 466a5c1416dSdrh realvalue = getDoubleArg(pArgList); 467a5c1416dSdrh }else{ 468a18c5681Sdrh realvalue = va_arg(ap,double); 469a5c1416dSdrh } 47069ef7036Sdrh #ifdef SQLITE_OMIT_FLOATING_POINT 47169ef7036Sdrh length = 0; 47269ef7036Sdrh #else 473a18c5681Sdrh if( precision<0 ) precision = 6; /* Set default precision */ 474a18c5681Sdrh if( realvalue<0.0 ){ 475a18c5681Sdrh realvalue = -realvalue; 476a18c5681Sdrh prefix = '-'; 477a18c5681Sdrh }else{ 4782c338a9dSdrh prefix = flag_prefix; 479a18c5681Sdrh } 4803e9aeec0Sdrh if( xtype==etGENERIC && precision>0 ) precision--; 481a30d22a7Sdrh testcase( precision>0xfff ); 48274b42275Sdrh for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){} 4833e9aeec0Sdrh if( xtype==etFLOAT ) realvalue += rounder; 484a18c5681Sdrh /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ 485a18c5681Sdrh exp = 0; 486ea678832Sdrh if( sqlite3IsNaN((double)realvalue) ){ 48753c14021Sdrh bufpt = "NaN"; 48853c14021Sdrh length = 3; 48953c14021Sdrh break; 49053c14021Sdrh } 491a18c5681Sdrh if( realvalue>0.0 ){ 49272b3fbc7Sdrh LONGDOUBLE_TYPE scale = 1.0; 4934ef94130Sdrh while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;} 4942a8f6712Sdrh while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; } 49572b3fbc7Sdrh while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; } 49672b3fbc7Sdrh realvalue /= scale; 497af005fbcSdrh while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } 498af005fbcSdrh while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } 499af005fbcSdrh if( exp>350 ){ 5002a8f6712Sdrh bufpt = buf; 5012a8f6712Sdrh buf[0] = prefix; 5022a8f6712Sdrh memcpy(buf+(prefix!=0),"Inf",4); 5032a8f6712Sdrh length = 3+(prefix!=0); 504a18c5681Sdrh break; 505a18c5681Sdrh } 506a18c5681Sdrh } 507a18c5681Sdrh bufpt = buf; 508a18c5681Sdrh /* 509a18c5681Sdrh ** If the field type is etGENERIC, then convert to either etEXP 510a18c5681Sdrh ** or etFLOAT, as appropriate. 511a18c5681Sdrh */ 512a18c5681Sdrh if( xtype!=etFLOAT ){ 513a18c5681Sdrh realvalue += rounder; 514a18c5681Sdrh if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } 515a18c5681Sdrh } 516a18c5681Sdrh if( xtype==etGENERIC ){ 517a18c5681Sdrh flag_rtz = !flag_alternateform; 518a18c5681Sdrh if( exp<-4 || exp>precision ){ 519a18c5681Sdrh xtype = etEXP; 520a18c5681Sdrh }else{ 521a18c5681Sdrh precision = precision - exp; 522a18c5681Sdrh xtype = etFLOAT; 523a18c5681Sdrh } 524a18c5681Sdrh }else{ 52572b3fbc7Sdrh flag_rtz = flag_altform2; 526a18c5681Sdrh } 527557cc60fSdrh if( xtype==etEXP ){ 528557cc60fSdrh e2 = 0; 529557cc60fSdrh }else{ 530557cc60fSdrh e2 = exp; 531557cc60fSdrh } 53274b42275Sdrh if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){ 53374b42275Sdrh bufpt = zExtra 53474b42275Sdrh = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 ); 53559eedf79Sdrh if( bufpt==0 ){ 5360cdbe1aeSdrh setStrAccumError(pAccum, SQLITE_NOMEM); 53759eedf79Sdrh return; 53859eedf79Sdrh } 53959eedf79Sdrh } 54059eedf79Sdrh zOut = bufpt; 54172b3fbc7Sdrh nsd = 16 + flag_altform2*10; 542ea678832Sdrh flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; 543557cc60fSdrh /* The sign in front of the number */ 544557cc60fSdrh if( prefix ){ 545557cc60fSdrh *(bufpt++) = prefix; 546557cc60fSdrh } 547557cc60fSdrh /* Digits prior to the decimal point */ 548557cc60fSdrh if( e2<0 ){ 549557cc60fSdrh *(bufpt++) = '0'; 550557cc60fSdrh }else{ 551557cc60fSdrh for(; e2>=0; e2--){ 552557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 553557cc60fSdrh } 554557cc60fSdrh } 555557cc60fSdrh /* The decimal point */ 556557cc60fSdrh if( flag_dp ){ 557557cc60fSdrh *(bufpt++) = '.'; 558557cc60fSdrh } 559557cc60fSdrh /* "0" digits after the decimal point but before the first 560557cc60fSdrh ** significant digit of the number */ 561af005fbcSdrh for(e2++; e2<0; precision--, e2++){ 562af005fbcSdrh assert( precision>0 ); 563a18c5681Sdrh *(bufpt++) = '0'; 564a18c5681Sdrh } 565557cc60fSdrh /* Significant digits after the decimal point */ 566557cc60fSdrh while( (precision--)>0 ){ 567557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 568a18c5681Sdrh } 569557cc60fSdrh /* Remove trailing zeros and the "." if no digits follow the "." */ 570557cc60fSdrh if( flag_rtz && flag_dp ){ 5713e9aeec0Sdrh while( bufpt[-1]=='0' ) *(--bufpt) = 0; 57259eedf79Sdrh assert( bufpt>zOut ); 5733e9aeec0Sdrh if( bufpt[-1]=='.' ){ 574557cc60fSdrh if( flag_altform2 ){ 575557cc60fSdrh *(bufpt++) = '0'; 576557cc60fSdrh }else{ 577557cc60fSdrh *(--bufpt) = 0; 578a18c5681Sdrh } 579557cc60fSdrh } 580557cc60fSdrh } 581557cc60fSdrh /* Add the "eNNN" suffix */ 58259eedf79Sdrh if( xtype==etEXP ){ 58376ff3a0eSdrh *(bufpt++) = aDigits[infop->charset]; 584557cc60fSdrh if( exp<0 ){ 585557cc60fSdrh *(bufpt++) = '-'; exp = -exp; 586557cc60fSdrh }else{ 587557cc60fSdrh *(bufpt++) = '+'; 588557cc60fSdrh } 589a18c5681Sdrh if( exp>=100 ){ 590ea678832Sdrh *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */ 591a18c5681Sdrh exp %= 100; 592a18c5681Sdrh } 593ea678832Sdrh *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */ 594ea678832Sdrh *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */ 595a18c5681Sdrh } 596557cc60fSdrh *bufpt = 0; 597557cc60fSdrh 598a18c5681Sdrh /* The converted number is in buf[] and zero terminated. Output it. 599a18c5681Sdrh ** Note that the number is in the usual order, not reversed as with 600a18c5681Sdrh ** integer conversions. */ 60159eedf79Sdrh length = (int)(bufpt-zOut); 60259eedf79Sdrh bufpt = zOut; 603a18c5681Sdrh 604a18c5681Sdrh /* Special case: Add leading zeros if the flag_zeropad flag is 605a18c5681Sdrh ** set and we are not left justified */ 606a18c5681Sdrh if( flag_zeropad && !flag_leftjustify && length < width){ 607a18c5681Sdrh int i; 608a18c5681Sdrh int nPad = width - length; 609a18c5681Sdrh for(i=width; i>=nPad; i--){ 610a18c5681Sdrh bufpt[i] = bufpt[i-nPad]; 611a18c5681Sdrh } 612a18c5681Sdrh i = prefix!=0; 613a18c5681Sdrh while( nPad-- ) bufpt[i++] = '0'; 614a18c5681Sdrh length = width; 615a18c5681Sdrh } 61669ef7036Sdrh #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */ 617a18c5681Sdrh break; 618a18c5681Sdrh case etSIZE: 619fc6ee9dfSdrh if( !bArgList ){ 620fc6ee9dfSdrh *(va_arg(ap,int*)) = pAccum->nChar; 621fc6ee9dfSdrh } 622a18c5681Sdrh length = width = 0; 623a18c5681Sdrh break; 624a18c5681Sdrh case etPERCENT: 625a18c5681Sdrh buf[0] = '%'; 626a18c5681Sdrh bufpt = buf; 627a18c5681Sdrh length = 1; 628a18c5681Sdrh break; 629a18c5681Sdrh case etCHARX: 630a5c1416dSdrh if( bArgList ){ 631fc6ee9dfSdrh bufpt = getTextArg(pArgList); 632a15a7c35Sdrh length = 1; 633136102beSdrh if( bufpt ){ 634136102beSdrh buf[0] = c = *(bufpt++); 635136102beSdrh if( (c&0xc0)==0xc0 ){ 636136102beSdrh while( length<4 && (bufpt[0]&0xc0)==0x80 ){ 637136102beSdrh buf[length++] = *(bufpt++); 638136102beSdrh } 639136102beSdrh } 640a15a7c35Sdrh }else{ 641a15a7c35Sdrh buf[0] = 0; 642136102beSdrh } 643a5c1416dSdrh }else{ 644136102beSdrh unsigned int ch = va_arg(ap,unsigned int); 645136102beSdrh if( ch<0x00080 ){ 646136102beSdrh buf[0] = ch & 0xff; 647136102beSdrh length = 1; 648136102beSdrh }else if( ch<0x00800 ){ 649136102beSdrh buf[0] = 0xc0 + (u8)((ch>>6)&0x1f); 650136102beSdrh buf[1] = 0x80 + (u8)(ch & 0x3f); 651136102beSdrh length = 2; 652136102beSdrh }else if( ch<0x10000 ){ 653136102beSdrh buf[0] = 0xe0 + (u8)((ch>>12)&0x0f); 654136102beSdrh buf[1] = 0x80 + (u8)((ch>>6) & 0x3f); 655136102beSdrh buf[2] = 0x80 + (u8)(ch & 0x3f); 656136102beSdrh length = 3; 657136102beSdrh }else{ 658136102beSdrh buf[0] = 0xf0 + (u8)((ch>>18) & 0x07); 659136102beSdrh buf[1] = 0x80 + (u8)((ch>>12) & 0x3f); 660136102beSdrh buf[2] = 0x80 + (u8)((ch>>6) & 0x3f); 661136102beSdrh buf[3] = 0x80 + (u8)(ch & 0x3f); 662136102beSdrh length = 4; 663136102beSdrh } 664a5c1416dSdrh } 665af8f513fSdrh if( precision>1 ){ 666af8f513fSdrh width -= precision-1; 667af8f513fSdrh if( width>1 && !flag_leftjustify ){ 6680cdbe1aeSdrh sqlite3_str_appendchar(pAccum, width-1, ' '); 669af8f513fSdrh width = 0; 670a18c5681Sdrh } 671136102beSdrh while( precision-- > 1 ){ 6720cdbe1aeSdrh sqlite3_str_append(pAccum, buf, length); 673af8f513fSdrh } 674136102beSdrh } 675a18c5681Sdrh bufpt = buf; 676cf7c8370Sdrh flag_altform2 = 1; 677cf7c8370Sdrh goto adjust_width_for_utf8; 678a18c5681Sdrh case etSTRING: 679d93d8a81Sdrh case etDYNSTRING: 680a5c1416dSdrh if( bArgList ){ 681a5c1416dSdrh bufpt = getTextArg(pArgList); 6822a8f6712Sdrh xtype = etSTRING; 683a5c1416dSdrh }else{ 684cb485882Sdrh bufpt = va_arg(ap,char*); 685a5c1416dSdrh } 686d93d8a81Sdrh if( bufpt==0 ){ 687d93d8a81Sdrh bufpt = ""; 6882a8f6712Sdrh }else if( xtype==etDYNSTRING ){ 689cc398969Sdrh if( pAccum->nChar==0 && pAccum->mxAlloc && width==0 && precision<0 ){ 690cc398969Sdrh /* Special optimization for sqlite3_mprintf("%z..."): 691cc398969Sdrh ** Extend an existing memory allocation rather than creating 692cc398969Sdrh ** a new one. */ 693cc398969Sdrh assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); 694cc398969Sdrh pAccum->zText = bufpt; 695cc398969Sdrh pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt); 696cc398969Sdrh pAccum->nChar = 0x7fffffff & (int)strlen(bufpt); 697cc398969Sdrh pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED; 698cc398969Sdrh length = 0; 699cc398969Sdrh break; 700cc398969Sdrh } 701d93d8a81Sdrh zExtra = bufpt; 702d93d8a81Sdrh } 703e509094bSdrh if( precision>=0 ){ 70462856465Sdrh if( flag_altform2 ){ 70562856465Sdrh /* Set length to the number of bytes needed in order to display 70662856465Sdrh ** precision characters */ 70762856465Sdrh unsigned char *z = (unsigned char*)bufpt; 70862856465Sdrh while( precision-- > 0 && z[0] ){ 70962856465Sdrh SQLITE_SKIP_UTF8(z); 71062856465Sdrh } 71162856465Sdrh length = (int)(z - (unsigned char*)bufpt); 71262856465Sdrh }else{ 713e509094bSdrh for(length=0; length<precision && bufpt[length]; length++){} 71462856465Sdrh } 715e509094bSdrh }else{ 716c84ddf14Sdrh length = 0x7fffffff & (int)strlen(bufpt); 717e509094bSdrh } 71857e3ba76Sdrh adjust_width_for_utf8: 71962856465Sdrh if( flag_altform2 && width>0 ){ 72062856465Sdrh /* Adjust width to account for extra bytes in UTF-8 characters */ 72162856465Sdrh int ii = length - 1; 72262856465Sdrh while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++; 72362856465Sdrh } 724a18c5681Sdrh break; 72557e3ba76Sdrh case etSQLESCAPE: /* %q: Escape ' characters */ 72657e3ba76Sdrh case etSQLESCAPE2: /* %Q: Escape ' and enclose in '...' */ 72757e3ba76Sdrh case etSQLESCAPE3: { /* %w: Escape " characters */ 7288965b50eSdrh int i, j, k, n, isnull; 7294794f735Sdrh int needQuote; 730ea678832Sdrh char ch; 731f3b863edSdanielk1977 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */ 732a5c1416dSdrh char *escarg; 733a5c1416dSdrh 734a5c1416dSdrh if( bArgList ){ 735a5c1416dSdrh escarg = getTextArg(pArgList); 736a5c1416dSdrh }else{ 737a5c1416dSdrh escarg = va_arg(ap,char*); 738a5c1416dSdrh } 739f0113000Sdanielk1977 isnull = escarg==0; 740f0113000Sdanielk1977 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); 74157e3ba76Sdrh /* For %q, %Q, and %w, the precision is the number of byte (or 74257e3ba76Sdrh ** characters if the ! flags is present) to use from the input. 74357e3ba76Sdrh ** Because of the extra quoting characters inserted, the number 74457e3ba76Sdrh ** of output characters may be larger than the precision. 74557e3ba76Sdrh */ 7468965b50eSdrh k = precision; 74760d4a304Sdan for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ 748f3b863edSdanielk1977 if( ch==q ) n++; 74957e3ba76Sdrh if( flag_altform2 && (ch&0xc0)==0xc0 ){ 75057e3ba76Sdrh while( (escarg[i+1]&0xc0)==0x80 ){ i++; } 75157e3ba76Sdrh } 752a18c5681Sdrh } 7534794f735Sdrh needQuote = !isnull && xtype==etSQLESCAPE2; 7542a8f6712Sdrh n += i + 3; 755a18c5681Sdrh if( n>etBUFSIZE ){ 756e5ae5735Sdrh bufpt = zExtra = sqlite3Malloc( n ); 757d164fd34Sdrh if( bufpt==0 ){ 7580cdbe1aeSdrh setStrAccumError(pAccum, SQLITE_NOMEM); 759d164fd34Sdrh return; 760d164fd34Sdrh } 761a18c5681Sdrh }else{ 762a18c5681Sdrh bufpt = buf; 763a18c5681Sdrh } 7640cfcf3fbSchw j = 0; 765f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 7668965b50eSdrh k = i; 7678965b50eSdrh for(i=0; i<k; i++){ 7688965b50eSdrh bufpt[j++] = ch = escarg[i]; 769f3b863edSdanielk1977 if( ch==q ) bufpt[j++] = ch; 770a18c5681Sdrh } 771f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 772a18c5681Sdrh bufpt[j] = 0; 773a18c5681Sdrh length = j; 77457e3ba76Sdrh goto adjust_width_for_utf8; 7755eba8c09Sdrh } 7765f968436Sdrh case etTOKEN: { 7778236f688Sdrh Token *pToken; 7788236f688Sdrh if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; 7798236f688Sdrh pToken = va_arg(ap, Token*); 780a5c1416dSdrh assert( bArgList==0 ); 781a9ab481fSdrh if( pToken && pToken->n ){ 7820cdbe1aeSdrh sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n); 783ad6d9460Sdrh } 7845f968436Sdrh length = width = 0; 7855f968436Sdrh break; 7865f968436Sdrh } 7875f968436Sdrh case etSRCLIST: { 7888236f688Sdrh SrcList *pSrc; 7898236f688Sdrh int k; 7908236f688Sdrh struct SrcList_item *pItem; 7918236f688Sdrh if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; 7928236f688Sdrh pSrc = va_arg(ap, SrcList*); 7938236f688Sdrh k = va_arg(ap, int); 7948236f688Sdrh pItem = &pSrc->a[k]; 795a5c1416dSdrh assert( bArgList==0 ); 7965f968436Sdrh assert( k>=0 && k<pSrc->nSrc ); 79793a960a0Sdrh if( pItem->zDatabase ){ 7980cdbe1aeSdrh sqlite3_str_appendall(pAccum, pItem->zDatabase); 7990cdbe1aeSdrh sqlite3_str_append(pAccum, ".", 1); 8005f968436Sdrh } 8010cdbe1aeSdrh sqlite3_str_appendall(pAccum, pItem->zName); 8025f968436Sdrh length = width = 0; 8035f968436Sdrh break; 8045f968436Sdrh } 805874ba04cSdrh default: { 806874ba04cSdrh assert( xtype==etINVALID ); 807874ba04cSdrh return; 808874ba04cSdrh } 809a18c5681Sdrh }/* End switch over the format type */ 810a18c5681Sdrh /* 811a18c5681Sdrh ** The text of the conversion is pointed to by "bufpt" and is 812a18c5681Sdrh ** "length" characters long. The field width is "width". Do 81362856465Sdrh ** the output. Both length and width are in bytes, not characters, 81462856465Sdrh ** at this point. If the "!" flag was present on string conversions 81562856465Sdrh ** indicating that width and precision should be expressed in characters, 81662856465Sdrh ** then the values have been translated prior to reaching this point. 817a18c5681Sdrh */ 818a70a073bSdrh width -= length; 8198236f688Sdrh if( width>0 ){ 8200cdbe1aeSdrh if( !flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' '); 8210cdbe1aeSdrh sqlite3_str_append(pAccum, bufpt, length); 8220cdbe1aeSdrh if( flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' '); 8238236f688Sdrh }else{ 8240cdbe1aeSdrh sqlite3_str_append(pAccum, bufpt, length); 8258236f688Sdrh } 826a70a073bSdrh 827af8f513fSdrh if( zExtra ){ 82896ceaf86Sdrh sqlite3DbFree(pAccum->db, zExtra); 829af8f513fSdrh zExtra = 0; 830af8f513fSdrh } 831a18c5681Sdrh }/* End for loop over the format string */ 832a18c5681Sdrh } /* End of function */ 833a18c5681Sdrh 834a18c5681Sdrh /* 835a70a073bSdrh ** Enlarge the memory allocation on a StrAccum object so that it is 836a70a073bSdrh ** able to accept at least N more bytes of text. 837a70a073bSdrh ** 838a70a073bSdrh ** Return the number of bytes of text that StrAccum is able to accept 839a70a073bSdrh ** after the attempted enlargement. The value returned might be zero. 840a18c5681Sdrh */ 841a70a073bSdrh static int sqlite3StrAccumEnlarge(StrAccum *p, int N){ 842a6353a3fSdrh char *zNew; 843a30d22a7Sdrh assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */ 844b49bc86aSdrh if( p->accError ){ 8450cdbe1aeSdrh testcase(p->accError==SQLITE_TOOBIG); 8460cdbe1aeSdrh testcase(p->accError==SQLITE_NOMEM); 847a70a073bSdrh return 0; 848ade86483Sdrh } 849c0490572Sdrh if( p->mxAlloc==0 ){ 850ade86483Sdrh N = p->nAlloc - p->nChar - 1; 8510cdbe1aeSdrh setStrAccumError(p, SQLITE_TOOBIG); 852a70a073bSdrh return N; 853a18c5681Sdrh }else{ 8545f4a686fSdrh char *zOld = isMalloced(p) ? p->zText : 0; 85593a960a0Sdrh i64 szNew = p->nChar; 856b1a6c3c1Sdrh szNew += N + 1; 8577b4d780bSdrh if( szNew+p->nChar<=p->mxAlloc ){ 8587b4d780bSdrh /* Force exponential buffer size growth as long as it does not overflow, 8597b4d780bSdrh ** to avoid having to call this routine too often */ 8607b4d780bSdrh szNew += p->nChar; 8617b4d780bSdrh } 862b1a6c3c1Sdrh if( szNew > p->mxAlloc ){ 8630cdbe1aeSdrh sqlite3_str_reset(p); 8640cdbe1aeSdrh setStrAccumError(p, SQLITE_TOOBIG); 865a70a073bSdrh return 0; 866b1a6c3c1Sdrh }else{ 867ea678832Sdrh p->nAlloc = (int)szNew; 868a18c5681Sdrh } 869c0490572Sdrh if( p->db ){ 870a9ef7097Sdan zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); 871b975598eSdrh }else{ 872f3cdcdccSdrh zNew = sqlite3_realloc64(zOld, p->nAlloc); 873b975598eSdrh } 87453f733c7Sdrh if( zNew ){ 8757ef4d1c4Sdrh assert( p->zText!=0 || p->nChar==0 ); 8765f4a686fSdrh if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); 877ade86483Sdrh p->zText = zNew; 8787f5a7ecdSdrh p->nAlloc = sqlite3DbMallocSize(p->db, zNew); 8795f4a686fSdrh p->printfFlags |= SQLITE_PRINTF_MALLOCED; 880ade86483Sdrh }else{ 8810cdbe1aeSdrh sqlite3_str_reset(p); 8820cdbe1aeSdrh setStrAccumError(p, SQLITE_NOMEM); 883a70a073bSdrh return 0; 884a70a073bSdrh } 885a70a073bSdrh } 886a70a073bSdrh return N; 887a70a073bSdrh } 888a70a073bSdrh 889a70a073bSdrh /* 890af8f513fSdrh ** Append N copies of character c to the given string buffer. 891a70a073bSdrh */ 8920cdbe1aeSdrh void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){ 893a30d22a7Sdrh testcase( p->nChar + (i64)N > 0x7fffffff ); 894a30d22a7Sdrh if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ 895a30d22a7Sdrh return; 896a30d22a7Sdrh } 897af8f513fSdrh while( (N--)>0 ) p->zText[p->nChar++] = c; 898a70a073bSdrh } 899a70a073bSdrh 900a70a073bSdrh /* 901a70a073bSdrh ** The StrAccum "p" is not large enough to accept N new bytes of z[]. 902a70a073bSdrh ** So enlarge if first, then do the append. 903a70a073bSdrh ** 9040cdbe1aeSdrh ** This is a helper routine to sqlite3_str_append() that does special-case 905a70a073bSdrh ** work (enlarging the buffer) using tail recursion, so that the 9060cdbe1aeSdrh ** sqlite3_str_append() routine can use fast calling semantics. 907a70a073bSdrh */ 908172087fbSdrh static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){ 909a70a073bSdrh N = sqlite3StrAccumEnlarge(p, N); 910a70a073bSdrh if( N>0 ){ 911a70a073bSdrh memcpy(&p->zText[p->nChar], z, N); 912a70a073bSdrh p->nChar += N; 913a70a073bSdrh } 914a70a073bSdrh } 915a70a073bSdrh 916a70a073bSdrh /* 917a70a073bSdrh ** Append N bytes of text from z to the StrAccum object. Increase the 918a70a073bSdrh ** size of the memory allocation for StrAccum if necessary. 919a70a073bSdrh */ 9200cdbe1aeSdrh void sqlite3_str_append(sqlite3_str *p, const char *z, int N){ 9213457338cSdrh assert( z!=0 || N==0 ); 922a70a073bSdrh assert( p->zText!=0 || p->nChar==0 || p->accError ); 923a70a073bSdrh assert( N>=0 ); 924a70a073bSdrh assert( p->accError==0 || p->nAlloc==0 ); 925a70a073bSdrh if( p->nChar+N >= p->nAlloc ){ 926a70a073bSdrh enlargeAndAppend(p,z,N); 927895decf6Sdan }else if( N ){ 928b07028f7Sdrh assert( p->zText ); 929ade86483Sdrh p->nChar += N; 930172087fbSdrh memcpy(&p->zText[p->nChar-N], z, N); 931172087fbSdrh } 932483750baSdrh } 933483750baSdrh 934483750baSdrh /* 935a6353a3fSdrh ** Append the complete text of zero-terminated string z[] to the p string. 936a6353a3fSdrh */ 9370cdbe1aeSdrh void sqlite3_str_appendall(sqlite3_str *p, const char *z){ 9380cdbe1aeSdrh sqlite3_str_append(p, z, sqlite3Strlen30(z)); 939a6353a3fSdrh } 940a6353a3fSdrh 941a6353a3fSdrh 942a6353a3fSdrh /* 943ade86483Sdrh ** Finish off a string by making sure it is zero-terminated. 944ade86483Sdrh ** Return a pointer to the resulting string. Return a NULL 945ade86483Sdrh ** pointer if any kind of error was encountered. 9465f968436Sdrh */ 947043e586eSdrh static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){ 9483f18e6d7Sdrh char *zText; 949043e586eSdrh assert( p->mxAlloc>0 && !isMalloced(p) ); 9503f18e6d7Sdrh zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); 9513f18e6d7Sdrh if( zText ){ 9523f18e6d7Sdrh memcpy(zText, p->zText, p->nChar+1); 9535f4a686fSdrh p->printfFlags |= SQLITE_PRINTF_MALLOCED; 954ade86483Sdrh }else{ 9550cdbe1aeSdrh setStrAccumError(p, SQLITE_NOMEM); 956ade86483Sdrh } 9573f18e6d7Sdrh p->zText = zText; 9583f18e6d7Sdrh return zText; 959043e586eSdrh } 960043e586eSdrh char *sqlite3StrAccumFinish(StrAccum *p){ 961043e586eSdrh if( p->zText ){ 962043e586eSdrh p->zText[p->nChar] = 0; 963043e586eSdrh if( p->mxAlloc>0 && !isMalloced(p) ){ 964043e586eSdrh return strAccumFinishRealloc(p); 965ade86483Sdrh } 966ade86483Sdrh } 967ade86483Sdrh return p->zText; 968ade86483Sdrh } 969ade86483Sdrh 970f80bba9dSdrh /* 971f80bba9dSdrh ** This singleton is an sqlite3_str object that is returned if 972f80bba9dSdrh ** sqlite3_malloc() fails to provide space for a real one. This 973f80bba9dSdrh ** sqlite3_str object accepts no new text and always returns 974f80bba9dSdrh ** an SQLITE_NOMEM error. 975f80bba9dSdrh */ 976f80bba9dSdrh static sqlite3_str sqlite3OomStr = { 977*3e62ddbfSdrh 0, 0, 0, 0, 0, SQLITE_NOMEM, 0 978f80bba9dSdrh }; 979f80bba9dSdrh 9800cdbe1aeSdrh /* Finalize a string created using sqlite3_str_new(). 9810cdbe1aeSdrh */ 9820cdbe1aeSdrh char *sqlite3_str_finish(sqlite3_str *p){ 9830cdbe1aeSdrh char *z; 984f80bba9dSdrh if( p!=0 && p!=&sqlite3OomStr ){ 9850cdbe1aeSdrh z = sqlite3StrAccumFinish(p); 986446135d7Sdrh sqlite3_free(p); 9870cdbe1aeSdrh }else{ 9880cdbe1aeSdrh z = 0; 9890cdbe1aeSdrh } 9900cdbe1aeSdrh return z; 9910cdbe1aeSdrh } 9920cdbe1aeSdrh 9930cdbe1aeSdrh /* Return any error code associated with p */ 9940cdbe1aeSdrh int sqlite3_str_errcode(sqlite3_str *p){ 9950cdbe1aeSdrh return p ? p->accError : SQLITE_NOMEM; 9960cdbe1aeSdrh } 9970cdbe1aeSdrh 9980cdbe1aeSdrh /* Return the current length of p in bytes */ 9990cdbe1aeSdrh int sqlite3_str_length(sqlite3_str *p){ 10000cdbe1aeSdrh return p ? p->nChar : 0; 10010cdbe1aeSdrh } 10020cdbe1aeSdrh 10030cdbe1aeSdrh /* Return the current value for p */ 10040cdbe1aeSdrh char *sqlite3_str_value(sqlite3_str *p){ 1005446135d7Sdrh if( p==0 || p->nChar==0 ) return 0; 1006446135d7Sdrh p->zText[p->nChar] = 0; 1007446135d7Sdrh return p->zText; 10080cdbe1aeSdrh } 10090cdbe1aeSdrh 1010ade86483Sdrh /* 1011ade86483Sdrh ** Reset an StrAccum string. Reclaim all malloced memory. 1012ade86483Sdrh */ 10130cdbe1aeSdrh void sqlite3_str_reset(StrAccum *p){ 10145f4a686fSdrh if( isMalloced(p) ){ 1015633e6d57Sdrh sqlite3DbFree(p->db, p->zText); 10165f4a686fSdrh p->printfFlags &= ~SQLITE_PRINTF_MALLOCED; 1017ade86483Sdrh } 1018446135d7Sdrh p->nAlloc = 0; 1019446135d7Sdrh p->nChar = 0; 1020f089aa45Sdrh p->zText = 0; 1021ade86483Sdrh } 1022ade86483Sdrh 1023ade86483Sdrh /* 1024c0490572Sdrh ** Initialize a string accumulator. 1025c0490572Sdrh ** 1026c0490572Sdrh ** p: The accumulator to be initialized. 1027c0490572Sdrh ** db: Pointer to a database connection. May be NULL. Lookaside 1028c0490572Sdrh ** memory is used if not NULL. db->mallocFailed is set appropriately 1029c0490572Sdrh ** when not NULL. 1030c0490572Sdrh ** zBase: An initial buffer. May be NULL in which case the initial buffer 1031c0490572Sdrh ** is malloced. 1032c0490572Sdrh ** n: Size of zBase in bytes. If total space requirements never exceed 1033c0490572Sdrh ** n then no memory allocations ever occur. 1034c0490572Sdrh ** mx: Maximum number of bytes to accumulate. If mx==0 then no memory 1035c0490572Sdrh ** allocations will ever occur. 1036ade86483Sdrh */ 1037c0490572Sdrh void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){ 10383f18e6d7Sdrh p->zText = zBase; 1039c0490572Sdrh p->db = db; 1040ade86483Sdrh p->nAlloc = n; 1041bb4957f8Sdrh p->mxAlloc = mx; 10423f18e6d7Sdrh p->nChar = 0; 1043b49bc86aSdrh p->accError = 0; 10445f4a686fSdrh p->printfFlags = 0; 10455f968436Sdrh } 10465f968436Sdrh 10470cdbe1aeSdrh /* Allocate and initialize a new dynamic string object */ 10480cdbe1aeSdrh sqlite3_str *sqlite3_str_new(sqlite3 *db){ 1049446135d7Sdrh sqlite3_str *p = sqlite3_malloc64(sizeof(*p)); 10500cdbe1aeSdrh if( p ){ 1051446135d7Sdrh sqlite3StrAccumInit(p, 0, 0, 0, 1052446135d7Sdrh db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH); 1053f80bba9dSdrh }else{ 1054f80bba9dSdrh p = &sqlite3OomStr; 10550cdbe1aeSdrh } 10560cdbe1aeSdrh return p; 10570cdbe1aeSdrh } 10580cdbe1aeSdrh 10595f968436Sdrh /* 10605f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 10615f968436Sdrh ** %-conversion extensions. 10625f968436Sdrh */ 106317435752Sdrh char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ 106417435752Sdrh char *z; 106579158e18Sdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 1066ade86483Sdrh StrAccum acc; 1067bc6160b0Sdrh assert( db!=0 ); 1068c0490572Sdrh sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase), 1069bc6160b0Sdrh db->aLimit[SQLITE_LIMIT_LENGTH]); 10705f4a686fSdrh acc.printfFlags = SQLITE_PRINTF_INTERNAL; 10710cdbe1aeSdrh sqlite3_str_vappendf(&acc, zFormat, ap); 1072ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 10730cdbe1aeSdrh if( acc.accError==SQLITE_NOMEM ){ 10744a642b60Sdrh sqlite3OomFault(db); 107517435752Sdrh } 107617435752Sdrh return z; 10775f968436Sdrh } 10785f968436Sdrh 10795f968436Sdrh /* 10805f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 10815f968436Sdrh ** %-conversion extensions. 10825f968436Sdrh */ 108317435752Sdrh char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ 10845f968436Sdrh va_list ap; 10855f968436Sdrh char *z; 10865f968436Sdrh va_start(ap, zFormat); 1087ade86483Sdrh z = sqlite3VMPrintf(db, zFormat, ap); 10885f968436Sdrh va_end(ap); 10895f968436Sdrh return z; 10905f968436Sdrh } 10915f968436Sdrh 10925f968436Sdrh /* 109328dd479cSdrh ** Print into memory obtained from sqlite3_malloc(). Omit the internal 109428dd479cSdrh ** %-conversion extensions. 109528dd479cSdrh */ 109628dd479cSdrh char *sqlite3_vmprintf(const char *zFormat, va_list ap){ 1097ade86483Sdrh char *z; 109828dd479cSdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 1099ade86483Sdrh StrAccum acc; 11009ca95730Sdrh 11019ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 11029ca95730Sdrh if( zFormat==0 ){ 11039ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 11049ca95730Sdrh return 0; 11059ca95730Sdrh } 11069ca95730Sdrh #endif 1107ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT 1108ff1590eeSdrh if( sqlite3_initialize() ) return 0; 1109ff1590eeSdrh #endif 1110c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); 11110cdbe1aeSdrh sqlite3_str_vappendf(&acc, zFormat, ap); 1112ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 1113ade86483Sdrh return z; 111428dd479cSdrh } 111528dd479cSdrh 111628dd479cSdrh /* 111728dd479cSdrh ** Print into memory obtained from sqlite3_malloc()(). Omit the internal 111828dd479cSdrh ** %-conversion extensions. 1119483750baSdrh */ 11206f8a503dSdanielk1977 char *sqlite3_mprintf(const char *zFormat, ...){ 1121a18c5681Sdrh va_list ap; 11225f968436Sdrh char *z; 1123ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT 1124ff1590eeSdrh if( sqlite3_initialize() ) return 0; 1125ff1590eeSdrh #endif 1126a18c5681Sdrh va_start(ap, zFormat); 1127b3738b6cSdrh z = sqlite3_vmprintf(zFormat, ap); 1128a18c5681Sdrh va_end(ap); 11295f968436Sdrh return z; 1130a18c5681Sdrh } 1131a18c5681Sdrh 1132a18c5681Sdrh /* 11336f8a503dSdanielk1977 ** sqlite3_snprintf() works like snprintf() except that it ignores the 113493a5c6bdSdrh ** current locale settings. This is important for SQLite because we 113593a5c6bdSdrh ** are not able to use a "," as the decimal point in place of "." as 113693a5c6bdSdrh ** specified by some locales. 1137db26d4c9Sdrh ** 1138db26d4c9Sdrh ** Oops: The first two arguments of sqlite3_snprintf() are backwards 1139db26d4c9Sdrh ** from the snprintf() standard. Unfortunately, it is too late to change 1140db26d4c9Sdrh ** this without breaking compatibility, so we just have to live with the 1141db26d4c9Sdrh ** mistake. 1142db26d4c9Sdrh ** 1143db26d4c9Sdrh ** sqlite3_vsnprintf() is the varargs version. 114493a5c6bdSdrh */ 1145db26d4c9Sdrh char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ 1146db26d4c9Sdrh StrAccum acc; 1147db26d4c9Sdrh if( n<=0 ) return zBuf; 11489ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 11499ca95730Sdrh if( zBuf==0 || zFormat==0 ) { 11509ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 115196c707a3Sdrh if( zBuf ) zBuf[0] = 0; 11529ca95730Sdrh return zBuf; 11539ca95730Sdrh } 11549ca95730Sdrh #endif 1155c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zBuf, n, 0); 11560cdbe1aeSdrh sqlite3_str_vappendf(&acc, zFormat, ap); 1157e9bb5660Sdrh zBuf[acc.nChar] = 0; 1158e9bb5660Sdrh return zBuf; 1159db26d4c9Sdrh } 11606f8a503dSdanielk1977 char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ 11615f968436Sdrh char *z; 116293a5c6bdSdrh va_list ap; 116393a5c6bdSdrh va_start(ap,zFormat); 1164db26d4c9Sdrh z = sqlite3_vsnprintf(n, zBuf, zFormat, ap); 116593a5c6bdSdrh va_end(ap); 11665f968436Sdrh return z; 116793a5c6bdSdrh } 116893a5c6bdSdrh 11693f280701Sdrh /* 11707c0c460fSdrh ** This is the routine that actually formats the sqlite3_log() message. 11717c0c460fSdrh ** We house it in a separate routine from sqlite3_log() to avoid using 11727c0c460fSdrh ** stack space on small-stack systems when logging is disabled. 11737c0c460fSdrh ** 11747c0c460fSdrh ** sqlite3_log() must render into a static buffer. It cannot dynamically 11757c0c460fSdrh ** allocate memory because it might be called while the memory allocator 11767c0c460fSdrh ** mutex is held. 1177a8dbd52aSdrh ** 11780cdbe1aeSdrh ** sqlite3_str_vappendf() might ask for *temporary* memory allocations for 1179a8dbd52aSdrh ** certain format characters (%q) or for very large precisions or widths. 1180a8dbd52aSdrh ** Care must be taken that any sqlite3_log() calls that occur while the 1181a8dbd52aSdrh ** memory mutex is held do not use these mechanisms. 11827c0c460fSdrh */ 11837c0c460fSdrh static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){ 11847c0c460fSdrh StrAccum acc; /* String accumulator */ 1185a64fa912Sdrh char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */ 11867c0c460fSdrh 1187c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0); 11880cdbe1aeSdrh sqlite3_str_vappendf(&acc, zFormat, ap); 11897c0c460fSdrh sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode, 11907c0c460fSdrh sqlite3StrAccumFinish(&acc)); 11917c0c460fSdrh } 11927c0c460fSdrh 11937c0c460fSdrh /* 11943f280701Sdrh ** Format and write a message to the log if logging is enabled. 11953f280701Sdrh */ 1196a7564663Sdrh void sqlite3_log(int iErrCode, const char *zFormat, ...){ 11973f280701Sdrh va_list ap; /* Vararg list */ 11987c0c460fSdrh if( sqlite3GlobalConfig.xLog ){ 11993f280701Sdrh va_start(ap, zFormat); 12007c0c460fSdrh renderLogMsg(iErrCode, zFormat, ap); 12013f280701Sdrh va_end(ap); 12023f280701Sdrh } 12033f280701Sdrh } 12043f280701Sdrh 120502b0e267Smistachkin #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) 1206e54ca3feSdrh /* 1207e54ca3feSdrh ** A version of printf() that understands %lld. Used for debugging. 1208e54ca3feSdrh ** The printf() built into some versions of windows does not understand %lld 1209e54ca3feSdrh ** and segfaults if you give it a long long int. 1210e54ca3feSdrh */ 1211e54ca3feSdrh void sqlite3DebugPrintf(const char *zFormat, ...){ 1212e54ca3feSdrh va_list ap; 1213ade86483Sdrh StrAccum acc; 1214e54ca3feSdrh char zBuf[500]; 1215c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); 1216e54ca3feSdrh va_start(ap,zFormat); 12170cdbe1aeSdrh sqlite3_str_vappendf(&acc, zFormat, ap); 1218e54ca3feSdrh va_end(ap); 1219bed8e7e5Sdrh sqlite3StrAccumFinish(&acc); 12204a9ff918Smistachkin #ifdef SQLITE_OS_TRACE_PROC 12214a9ff918Smistachkin { 12224a9ff918Smistachkin extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf); 12234a9ff918Smistachkin SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf)); 12244a9ff918Smistachkin } 12254a9ff918Smistachkin #else 1226485f0039Sdrh fprintf(stdout,"%s", zBuf); 12272ac3ee97Sdrh fflush(stdout); 12284a9ff918Smistachkin #endif 1229e54ca3feSdrh } 1230e54ca3feSdrh #endif 1231c7bc4fdeSdrh 12324fa4a54fSdrh 1233c7bc4fdeSdrh /* 12340cdbe1aeSdrh ** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument 1235d37bea5bSdrh ** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats. 1236c7bc4fdeSdrh */ 12370cdbe1aeSdrh void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){ 1238c7bc4fdeSdrh va_list ap; 1239c7bc4fdeSdrh va_start(ap,zFormat); 12400cdbe1aeSdrh sqlite3_str_vappendf(p, zFormat, ap); 1241c7bc4fdeSdrh va_end(ap); 1242c7bc4fdeSdrh } 1243