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 */ 18e84a306bSdrh #define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */ 19e84a306bSdrh #define etFLOAT 2 /* Floating point. %f */ 20e84a306bSdrh #define etEXP 3 /* Exponentional notation. %e and %E */ 21e84a306bSdrh #define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */ 22e84a306bSdrh #define etSIZE 5 /* Return number of characters processed so far. %n */ 23e84a306bSdrh #define etSTRING 6 /* Strings. %s */ 24e84a306bSdrh #define etDYNSTRING 7 /* Dynamically allocated strings. %z */ 25e84a306bSdrh #define etPERCENT 8 /* Percent symbol. %% */ 26e84a306bSdrh #define etCHARX 9 /* Characters. %c */ 27a18c5681Sdrh /* The rest are extensions, not normally found in printf() */ 28af005fbcSdrh #define etSQLESCAPE 10 /* Strings with '\'' doubled. %q */ 29af005fbcSdrh #define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '', 300cfcf3fbSchw NULL pointers replaced by SQL NULL. %Q */ 31af005fbcSdrh #define etTOKEN 12 /* a pointer to a Token structure */ 32af005fbcSdrh #define etSRCLIST 13 /* a pointer to a SrcList */ 33af005fbcSdrh #define etPOINTER 14 /* The %p conversion */ 34af005fbcSdrh #define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */ 35af005fbcSdrh #define etORDINAL 16 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */ 36e84a306bSdrh 37874ba04cSdrh #define etINVALID 0 /* Any unrecognized conversion type */ 38874ba04cSdrh 39e84a306bSdrh 40e84a306bSdrh /* 41e84a306bSdrh ** An "etByte" is an 8-bit unsigned value. 42e84a306bSdrh */ 43e84a306bSdrh typedef unsigned char etByte; 44a18c5681Sdrh 45a18c5681Sdrh /* 46a18c5681Sdrh ** Each builtin conversion character (ex: the 'd' in "%d") is described 47a18c5681Sdrh ** by an instance of the following structure 48a18c5681Sdrh */ 49a18c5681Sdrh typedef struct et_info { /* Information about each format field */ 50e84a306bSdrh char fmttype; /* The format field code letter */ 51e84a306bSdrh etByte base; /* The base for radix conversion */ 52e84a306bSdrh etByte flags; /* One or more of FLAG_ constants below */ 53e84a306bSdrh etByte type; /* Conversion paradigm */ 5476ff3a0eSdrh etByte charset; /* Offset into aDigits[] of the digits string */ 5576ff3a0eSdrh etByte prefix; /* Offset into aPrefix[] of the prefix string */ 56a18c5681Sdrh } et_info; 57a18c5681Sdrh 58a18c5681Sdrh /* 59e84a306bSdrh ** Allowed values for et_info.flags 60e84a306bSdrh */ 61e84a306bSdrh #define FLAG_SIGNED 1 /* True if the value to convert is signed */ 62e84a306bSdrh #define FLAG_INTERN 2 /* True if for internal use only */ 634794f735Sdrh #define FLAG_STRING 4 /* Allow infinity 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[] = { 7376ff3a0eSdrh { 'd', 10, 1, etRADIX, 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 }, 8276ff3a0eSdrh { 'u', 10, 0, etRADIX, 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 9176ff3a0eSdrh { 'i', 10, 1, etRADIX, 0, 0 }, 92e84a306bSdrh { 'n', 0, 0, etSIZE, 0, 0 }, 93e84a306bSdrh { '%', 0, 0, etPERCENT, 0, 0 }, 9476ff3a0eSdrh { 'p', 16, 0, etPOINTER, 0, 1 }, 957e3ff5d8Sdrh 967e3ff5d8Sdrh /* All the rest have the FLAG_INTERN bit set and are thus for internal 977e3ff5d8Sdrh ** use only */ 985f968436Sdrh { 'T', 0, 2, etTOKEN, 0, 0 }, 995f968436Sdrh { 'S', 0, 2, etSRCLIST, 0, 0 }, 1009a99334dSdrh { 'r', 10, 3, etORDINAL, 0, 0 }, 101a18c5681Sdrh }; 102a18c5681Sdrh 103a18c5681Sdrh /* 104b37df7b9Sdrh ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point 105a18c5681Sdrh ** conversions will work. 106a18c5681Sdrh */ 107b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 108a18c5681Sdrh /* 109a18c5681Sdrh ** "*val" is a double such that 0.1 <= *val < 10.0 110a18c5681Sdrh ** Return the ascii code for the leading digit of *val, then 111a18c5681Sdrh ** multiply "*val" by 10.0 to renormalize. 112a18c5681Sdrh ** 113a18c5681Sdrh ** Example: 114a18c5681Sdrh ** input: *val = 3.14159 115a18c5681Sdrh ** output: *val = 1.4159 function return = '3' 116a18c5681Sdrh ** 117a18c5681Sdrh ** The counter *cnt is incremented each time. After counter exceeds 118a18c5681Sdrh ** 16 (the number of significant digits in a 64-bit float) '0' is 119a18c5681Sdrh ** always returned. 120a18c5681Sdrh */ 121ea678832Sdrh static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ 122a18c5681Sdrh int digit; 123384eef32Sdrh LONGDOUBLE_TYPE d; 12472b3fbc7Sdrh if( (*cnt)<=0 ) return '0'; 12572b3fbc7Sdrh (*cnt)--; 126a18c5681Sdrh digit = (int)*val; 127a18c5681Sdrh d = digit; 128a18c5681Sdrh digit += '0'; 129a18c5681Sdrh *val = (*val - d)*10.0; 130ea678832Sdrh return (char)digit; 131a18c5681Sdrh } 132b37df7b9Sdrh #endif /* SQLITE_OMIT_FLOATING_POINT */ 133a18c5681Sdrh 13479158e18Sdrh /* 135a6353a3fSdrh ** Set the StrAccum object to an error mode. 136a6353a3fSdrh */ 137a5c1416dSdrh static void setStrAccumError(StrAccum *p, u8 eError){ 138c0490572Sdrh assert( eError==STRACCUM_NOMEM || eError==STRACCUM_TOOBIG ); 139a6353a3fSdrh p->accError = eError; 140a6353a3fSdrh p->nAlloc = 0; 141a6353a3fSdrh } 142a6353a3fSdrh 143a6353a3fSdrh /* 144a5c1416dSdrh ** Extra argument values from a PrintfArguments object 145a5c1416dSdrh */ 146a5c1416dSdrh static sqlite3_int64 getIntArg(PrintfArguments *p){ 147a5c1416dSdrh if( p->nArg<=p->nUsed ) return 0; 148a5c1416dSdrh return sqlite3_value_int64(p->apArg[p->nUsed++]); 149a5c1416dSdrh } 150a5c1416dSdrh static double getDoubleArg(PrintfArguments *p){ 151a5c1416dSdrh if( p->nArg<=p->nUsed ) return 0.0; 152a5c1416dSdrh return sqlite3_value_double(p->apArg[p->nUsed++]); 153a5c1416dSdrh } 154a5c1416dSdrh static char *getTextArg(PrintfArguments *p){ 155a5c1416dSdrh if( p->nArg<=p->nUsed ) return 0; 156a5c1416dSdrh return (char*)sqlite3_value_text(p->apArg[p->nUsed++]); 157a5c1416dSdrh } 158a5c1416dSdrh 159a5c1416dSdrh 160a5c1416dSdrh /* 16179158e18Sdrh ** On machines with a small stack size, you can redefine the 162ed1fddf4Sdrh ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired. 16379158e18Sdrh */ 16479158e18Sdrh #ifndef SQLITE_PRINT_BUF_SIZE 16559eedf79Sdrh # define SQLITE_PRINT_BUF_SIZE 70 16650d654daSdrh #endif 16779158e18Sdrh #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ 168a18c5681Sdrh 169a18c5681Sdrh /* 170ed1fddf4Sdrh ** Render a string given by "fmt" into the StrAccum object. 171a18c5681Sdrh */ 172f089aa45Sdrh void sqlite3VXPrintf( 173ade86483Sdrh StrAccum *pAccum, /* Accumulate results here */ 1745f968436Sdrh const char *fmt, /* Format string */ 1755f968436Sdrh va_list ap /* arguments */ 176a18c5681Sdrh ){ 177e84a306bSdrh int c; /* Next character in the format string */ 178e84a306bSdrh char *bufpt; /* Pointer to the conversion buffer */ 179e84a306bSdrh int precision; /* Precision of the current field */ 180e84a306bSdrh int length; /* Length of the field */ 181e84a306bSdrh int idx; /* A general purpose loop counter */ 182a18c5681Sdrh int width; /* Width of the current field */ 183e84a306bSdrh etByte flag_leftjustify; /* True if "-" flag is present */ 184e84a306bSdrh etByte flag_plussign; /* True if "+" flag is present */ 185e84a306bSdrh etByte flag_blanksign; /* True if " " flag is present */ 186e84a306bSdrh etByte flag_alternateform; /* True if "#" flag is present */ 187531fe878Sdrh etByte flag_altform2; /* True if "!" flag is present */ 188e84a306bSdrh etByte flag_zeropad; /* True if field width constant starts with zero */ 189e84a306bSdrh etByte flag_long; /* True if "l" flag is present */ 190a34b6764Sdrh etByte flag_longlong; /* True if the "ll" flag is present */ 1913e9aeec0Sdrh etByte done; /* Loop termination flag */ 192ed1fddf4Sdrh etByte xtype = 0; /* Conversion paradigm */ 193a5c1416dSdrh u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */ 194a5c1416dSdrh u8 useIntern; /* Ok to use internal conversions (ex: %T) */ 195ed1fddf4Sdrh char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ 19627436af7Sdrh sqlite_uint64 longvalue; /* Value for integer types */ 197384eef32Sdrh LONGDOUBLE_TYPE realvalue; /* Value for real types */ 1985719628aSdrh const et_info *infop; /* Pointer to the appropriate info structure */ 19959eedf79Sdrh char *zOut; /* Rendering buffer */ 20059eedf79Sdrh int nOut; /* Size of the rendering buffer */ 201af8f513fSdrh char *zExtra = 0; /* Malloced memory used by some conversion */ 202b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 203557cc60fSdrh int exp, e2; /* exponent of real numbers */ 204ed1fddf4Sdrh int nsd; /* Number of significant digits returned */ 205a18c5681Sdrh double rounder; /* Used for rounding floating point values */ 206e84a306bSdrh etByte flag_dp; /* True if decimal point should be shown */ 207e84a306bSdrh etByte flag_rtz; /* True if trailing zeros should be removed */ 208a18c5681Sdrh #endif 209a5c1416dSdrh PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */ 210ed1fddf4Sdrh char buf[etBUFSIZE]; /* Conversion buffer */ 211a18c5681Sdrh 212a18c5681Sdrh bufpt = 0; 2135f4a686fSdrh if( pAccum->printfFlags ){ 2145f4a686fSdrh if( (bArgList = (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){ 215a5c1416dSdrh pArgList = va_arg(ap, PrintfArguments*); 216a5c1416dSdrh } 2175f4a686fSdrh useIntern = pAccum->printfFlags & SQLITE_PRINTF_INTERNAL; 218a5c1416dSdrh }else{ 219a5c1416dSdrh bArgList = useIntern = 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 229a70a073bSdrh sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt)); 230760b1598Sdrh if( *fmt==0 ) break; 231a18c5681Sdrh } 232a18c5681Sdrh if( (c=(*++fmt))==0 ){ 233ade86483Sdrh sqlite3StrAccumAppend(pAccum, "%", 1); 234a18c5681Sdrh break; 235a18c5681Sdrh } 236a18c5681Sdrh /* Find out what flags are present */ 237a18c5681Sdrh flag_leftjustify = flag_plussign = flag_blanksign = 238557cc60fSdrh flag_alternateform = flag_altform2 = flag_zeropad = 0; 2393e9aeec0Sdrh done = 0; 240a18c5681Sdrh do{ 241a18c5681Sdrh switch( c ){ 2423e9aeec0Sdrh case '-': flag_leftjustify = 1; break; 2433e9aeec0Sdrh case '+': flag_plussign = 1; break; 2443e9aeec0Sdrh case ' ': flag_blanksign = 1; break; 2453e9aeec0Sdrh case '#': flag_alternateform = 1; break; 2463e9aeec0Sdrh case '!': flag_altform2 = 1; break; 2473e9aeec0Sdrh case '0': flag_zeropad = 1; break; 2483e9aeec0Sdrh default: done = 1; break; 249a18c5681Sdrh } 2503e9aeec0Sdrh }while( !done && (c=(*++fmt))!=0 ); 251a18c5681Sdrh /* Get the field width */ 252a18c5681Sdrh if( c=='*' ){ 253a5c1416dSdrh if( bArgList ){ 254a5c1416dSdrh width = (int)getIntArg(pArgList); 255a5c1416dSdrh }else{ 256a18c5681Sdrh width = va_arg(ap,int); 257a5c1416dSdrh } 258a18c5681Sdrh if( width<0 ){ 259a18c5681Sdrh flag_leftjustify = 1; 260b6f47debSdrh width = width >= -2147483647 ? -width : 0; 261a18c5681Sdrh } 262a18c5681Sdrh c = *++fmt; 263a18c5681Sdrh }else{ 264b6f47debSdrh unsigned wx = 0; 26517a68934Sdrh while( c>='0' && c<='9' ){ 266b6f47debSdrh wx = wx*10 + c - '0'; 267a18c5681Sdrh c = *++fmt; 268a18c5681Sdrh } 269b6f47debSdrh testcase( wx>0x7fffffff ); 270b6f47debSdrh width = wx & 0x7fffffff; 271a18c5681Sdrh } 272c386ef4fSdrh assert( width>=0 ); 273c386ef4fSdrh #ifdef SQLITE_PRINTF_PRECISION_LIMIT 274c386ef4fSdrh if( width>SQLITE_PRINTF_PRECISION_LIMIT ){ 275c386ef4fSdrh width = SQLITE_PRINTF_PRECISION_LIMIT; 276c386ef4fSdrh } 277c386ef4fSdrh #endif 2788c069147Sdan 279a18c5681Sdrh /* Get the precision */ 280a18c5681Sdrh if( c=='.' ){ 281a18c5681Sdrh c = *++fmt; 282a18c5681Sdrh if( c=='*' ){ 283a5c1416dSdrh if( bArgList ){ 284a5c1416dSdrh precision = (int)getIntArg(pArgList); 285a5c1416dSdrh }else{ 286a18c5681Sdrh precision = va_arg(ap,int); 287a5c1416dSdrh } 288a18c5681Sdrh c = *++fmt; 289b6f47debSdrh if( precision<0 ){ 290b6f47debSdrh precision = precision >= -2147483647 ? -precision : -1; 291b6f47debSdrh } 292a18c5681Sdrh }else{ 293b6f47debSdrh unsigned px = 0; 29417a68934Sdrh while( c>='0' && c<='9' ){ 295b6f47debSdrh px = px*10 + c - '0'; 296a18c5681Sdrh c = *++fmt; 297a18c5681Sdrh } 298b6f47debSdrh testcase( px>0x7fffffff ); 299b6f47debSdrh precision = px & 0x7fffffff; 300a18c5681Sdrh } 301a18c5681Sdrh }else{ 302a18c5681Sdrh precision = -1; 303a18c5681Sdrh } 304c386ef4fSdrh assert( precision>=(-1) ); 305c386ef4fSdrh #ifdef SQLITE_PRINTF_PRECISION_LIMIT 306c386ef4fSdrh if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){ 307c386ef4fSdrh precision = SQLITE_PRINTF_PRECISION_LIMIT; 308c386ef4fSdrh } 309c386ef4fSdrh #endif 310c386ef4fSdrh 311c386ef4fSdrh 312a18c5681Sdrh /* Get the conversion type modifier */ 313a18c5681Sdrh if( c=='l' ){ 314a18c5681Sdrh flag_long = 1; 315a18c5681Sdrh c = *++fmt; 316a34b6764Sdrh if( c=='l' ){ 317a34b6764Sdrh flag_longlong = 1; 318a34b6764Sdrh c = *++fmt; 319a18c5681Sdrh }else{ 320a34b6764Sdrh flag_longlong = 0; 321a34b6764Sdrh } 322a34b6764Sdrh }else{ 323a34b6764Sdrh flag_long = flag_longlong = 0; 324a18c5681Sdrh } 325a18c5681Sdrh /* Fetch the info entry for the field */ 326874ba04cSdrh infop = &fmtinfo[0]; 327874ba04cSdrh xtype = etINVALID; 32800e13613Sdanielk1977 for(idx=0; idx<ArraySize(fmtinfo); idx++){ 329a18c5681Sdrh if( c==fmtinfo[idx].fmttype ){ 330a18c5681Sdrh infop = &fmtinfo[idx]; 331a5c1416dSdrh if( useIntern || (infop->flags & FLAG_INTERN)==0 ){ 332e84a306bSdrh xtype = infop->type; 333c4413565Sdrh }else{ 334ade86483Sdrh return; 3355f968436Sdrh } 336a18c5681Sdrh break; 337a18c5681Sdrh } 338a18c5681Sdrh } 33943617e9aSdrh 340a18c5681Sdrh /* 341a18c5681Sdrh ** At this point, variables are initialized as follows: 342a18c5681Sdrh ** 343a18c5681Sdrh ** flag_alternateform TRUE if a '#' is present. 3443e9aeec0Sdrh ** flag_altform2 TRUE if a '!' is present. 345a18c5681Sdrh ** flag_plussign TRUE if a '+' is present. 346a18c5681Sdrh ** flag_leftjustify TRUE if a '-' is present or if the 347a18c5681Sdrh ** field width was negative. 348a18c5681Sdrh ** flag_zeropad TRUE if the width began with 0. 349a18c5681Sdrh ** flag_long TRUE if the letter 'l' (ell) prefixed 350a18c5681Sdrh ** the conversion character. 351a34b6764Sdrh ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed 352a34b6764Sdrh ** the conversion character. 353a18c5681Sdrh ** flag_blanksign TRUE if a ' ' is present. 354a18c5681Sdrh ** width The specified field width. This is 355a18c5681Sdrh ** always non-negative. Zero is the default. 356a18c5681Sdrh ** precision The specified precision. The default 357a18c5681Sdrh ** is -1. 358a18c5681Sdrh ** xtype The class of the conversion. 359a18c5681Sdrh ** infop Pointer to the appropriate info struct. 360a18c5681Sdrh */ 361a18c5681Sdrh switch( xtype ){ 362fe63d1c9Sdrh case etPOINTER: 363fe63d1c9Sdrh flag_longlong = sizeof(char*)==sizeof(i64); 364fe63d1c9Sdrh flag_long = sizeof(char*)==sizeof(long int); 365fe63d1c9Sdrh /* Fall through into the next case */ 3669a99334dSdrh case etORDINAL: 367a18c5681Sdrh case etRADIX: 368e84a306bSdrh if( infop->flags & FLAG_SIGNED ){ 369e9707671Sdrh i64 v; 370a5c1416dSdrh if( bArgList ){ 371a5c1416dSdrh v = getIntArg(pArgList); 372a5c1416dSdrh }else if( flag_longlong ){ 373eeb23a4cSdrh v = va_arg(ap,i64); 374eeb23a4cSdrh }else if( flag_long ){ 375eeb23a4cSdrh v = va_arg(ap,long int); 376eeb23a4cSdrh }else{ 377eeb23a4cSdrh v = va_arg(ap,int); 378eeb23a4cSdrh } 379e9707671Sdrh if( v<0 ){ 380158b9cb9Sdrh if( v==SMALLEST_INT64 ){ 381158b9cb9Sdrh longvalue = ((u64)1)<<63; 382158b9cb9Sdrh }else{ 383158b9cb9Sdrh longvalue = -v; 384158b9cb9Sdrh } 385cfcdaefeSdanielk1977 prefix = '-'; 386cfcdaefeSdanielk1977 }else{ 387e9707671Sdrh longvalue = v; 388e9707671Sdrh if( flag_plussign ) prefix = '+'; 389a18c5681Sdrh else if( flag_blanksign ) prefix = ' '; 390a18c5681Sdrh else prefix = 0; 391cfcdaefeSdanielk1977 } 392e9707671Sdrh }else{ 393a5c1416dSdrh if( bArgList ){ 394a5c1416dSdrh longvalue = (u64)getIntArg(pArgList); 395a5c1416dSdrh }else if( flag_longlong ){ 396eeb23a4cSdrh longvalue = va_arg(ap,u64); 397eeb23a4cSdrh }else if( flag_long ){ 398eeb23a4cSdrh longvalue = va_arg(ap,unsigned long int); 399eeb23a4cSdrh }else{ 400eeb23a4cSdrh longvalue = va_arg(ap,unsigned int); 401eeb23a4cSdrh } 402e9707671Sdrh prefix = 0; 403e9707671Sdrh } 404e9707671Sdrh if( longvalue==0 ) flag_alternateform = 0; 405a18c5681Sdrh if( flag_zeropad && precision<width-(prefix!=0) ){ 406a18c5681Sdrh precision = width-(prefix!=0); 407a18c5681Sdrh } 40859eedf79Sdrh if( precision<etBUFSIZE-10 ){ 40959eedf79Sdrh nOut = etBUFSIZE; 41059eedf79Sdrh zOut = buf; 41159eedf79Sdrh }else{ 41259eedf79Sdrh nOut = precision + 10; 41359eedf79Sdrh zOut = zExtra = sqlite3Malloc( nOut ); 41459eedf79Sdrh if( zOut==0 ){ 415a6353a3fSdrh setStrAccumError(pAccum, STRACCUM_NOMEM); 41659eedf79Sdrh return; 41759eedf79Sdrh } 41859eedf79Sdrh } 41959eedf79Sdrh bufpt = &zOut[nOut-1]; 4209a99334dSdrh if( xtype==etORDINAL ){ 42143f6e064Sdrh static const char zOrd[] = "thstndrd"; 422ea678832Sdrh int x = (int)(longvalue % 10); 42343f6e064Sdrh if( x>=4 || (longvalue/10)%10==1 ){ 42443f6e064Sdrh x = 0; 42543f6e064Sdrh } 42659eedf79Sdrh *(--bufpt) = zOrd[x*2+1]; 42759eedf79Sdrh *(--bufpt) = zOrd[x*2]; 4289a99334dSdrh } 429a18c5681Sdrh { 4300e682099Sdrh const char *cset = &aDigits[infop->charset]; 4310e682099Sdrh u8 base = infop->base; 432a18c5681Sdrh do{ /* Convert to ascii */ 433a18c5681Sdrh *(--bufpt) = cset[longvalue%base]; 434a18c5681Sdrh longvalue = longvalue/base; 435a18c5681Sdrh }while( longvalue>0 ); 436a18c5681Sdrh } 43759eedf79Sdrh length = (int)(&zOut[nOut-1]-bufpt); 438a18c5681Sdrh for(idx=precision-length; idx>0; idx--){ 439a18c5681Sdrh *(--bufpt) = '0'; /* Zero pad */ 440a18c5681Sdrh } 441a18c5681Sdrh if( prefix ) *(--bufpt) = prefix; /* Add sign */ 442a18c5681Sdrh if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ 44376ff3a0eSdrh const char *pre; 44476ff3a0eSdrh char x; 44576ff3a0eSdrh pre = &aPrefix[infop->prefix]; 44676ff3a0eSdrh for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; 447a18c5681Sdrh } 44859eedf79Sdrh length = (int)(&zOut[nOut-1]-bufpt); 449a18c5681Sdrh break; 450a18c5681Sdrh case etFLOAT: 451a18c5681Sdrh case etEXP: 452a18c5681Sdrh case etGENERIC: 453a5c1416dSdrh if( bArgList ){ 454a5c1416dSdrh realvalue = getDoubleArg(pArgList); 455a5c1416dSdrh }else{ 456a18c5681Sdrh realvalue = va_arg(ap,double); 457a5c1416dSdrh } 45869ef7036Sdrh #ifdef SQLITE_OMIT_FLOATING_POINT 45969ef7036Sdrh length = 0; 46069ef7036Sdrh #else 461a18c5681Sdrh if( precision<0 ) precision = 6; /* Set default precision */ 462a18c5681Sdrh if( realvalue<0.0 ){ 463a18c5681Sdrh realvalue = -realvalue; 464a18c5681Sdrh prefix = '-'; 465a18c5681Sdrh }else{ 466a18c5681Sdrh if( flag_plussign ) prefix = '+'; 467a18c5681Sdrh else if( flag_blanksign ) prefix = ' '; 468a18c5681Sdrh else prefix = 0; 469a18c5681Sdrh } 4703e9aeec0Sdrh if( xtype==etGENERIC && precision>0 ) precision--; 471a30d22a7Sdrh testcase( precision>0xfff ); 47274b42275Sdrh for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){} 4733e9aeec0Sdrh if( xtype==etFLOAT ) realvalue += rounder; 474a18c5681Sdrh /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ 475a18c5681Sdrh exp = 0; 476ea678832Sdrh if( sqlite3IsNaN((double)realvalue) ){ 47753c14021Sdrh bufpt = "NaN"; 47853c14021Sdrh length = 3; 47953c14021Sdrh break; 48053c14021Sdrh } 481a18c5681Sdrh if( realvalue>0.0 ){ 48272b3fbc7Sdrh LONGDOUBLE_TYPE scale = 1.0; 4834ef94130Sdrh while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;} 4842a8f6712Sdrh while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; } 48572b3fbc7Sdrh while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; } 48672b3fbc7Sdrh realvalue /= scale; 487af005fbcSdrh while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } 488af005fbcSdrh while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } 489af005fbcSdrh if( exp>350 ){ 4902a8f6712Sdrh bufpt = buf; 4912a8f6712Sdrh buf[0] = prefix; 4922a8f6712Sdrh memcpy(buf+(prefix!=0),"Inf",4); 4932a8f6712Sdrh length = 3+(prefix!=0); 494a18c5681Sdrh break; 495a18c5681Sdrh } 496a18c5681Sdrh } 497a18c5681Sdrh bufpt = buf; 498a18c5681Sdrh /* 499a18c5681Sdrh ** If the field type is etGENERIC, then convert to either etEXP 500a18c5681Sdrh ** or etFLOAT, as appropriate. 501a18c5681Sdrh */ 502a18c5681Sdrh if( xtype!=etFLOAT ){ 503a18c5681Sdrh realvalue += rounder; 504a18c5681Sdrh if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } 505a18c5681Sdrh } 506a18c5681Sdrh if( xtype==etGENERIC ){ 507a18c5681Sdrh flag_rtz = !flag_alternateform; 508a18c5681Sdrh if( exp<-4 || exp>precision ){ 509a18c5681Sdrh xtype = etEXP; 510a18c5681Sdrh }else{ 511a18c5681Sdrh precision = precision - exp; 512a18c5681Sdrh xtype = etFLOAT; 513a18c5681Sdrh } 514a18c5681Sdrh }else{ 51572b3fbc7Sdrh flag_rtz = flag_altform2; 516a18c5681Sdrh } 517557cc60fSdrh if( xtype==etEXP ){ 518557cc60fSdrh e2 = 0; 519557cc60fSdrh }else{ 520557cc60fSdrh e2 = exp; 521557cc60fSdrh } 52274b42275Sdrh if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){ 52374b42275Sdrh bufpt = zExtra 52474b42275Sdrh = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 ); 52559eedf79Sdrh if( bufpt==0 ){ 526a6353a3fSdrh setStrAccumError(pAccum, STRACCUM_NOMEM); 52759eedf79Sdrh return; 52859eedf79Sdrh } 52959eedf79Sdrh } 53059eedf79Sdrh zOut = bufpt; 53172b3fbc7Sdrh nsd = 16 + flag_altform2*10; 532ea678832Sdrh flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; 533557cc60fSdrh /* The sign in front of the number */ 534557cc60fSdrh if( prefix ){ 535557cc60fSdrh *(bufpt++) = prefix; 536557cc60fSdrh } 537557cc60fSdrh /* Digits prior to the decimal point */ 538557cc60fSdrh if( e2<0 ){ 539557cc60fSdrh *(bufpt++) = '0'; 540557cc60fSdrh }else{ 541557cc60fSdrh for(; e2>=0; e2--){ 542557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 543557cc60fSdrh } 544557cc60fSdrh } 545557cc60fSdrh /* The decimal point */ 546557cc60fSdrh if( flag_dp ){ 547557cc60fSdrh *(bufpt++) = '.'; 548557cc60fSdrh } 549557cc60fSdrh /* "0" digits after the decimal point but before the first 550557cc60fSdrh ** significant digit of the number */ 551af005fbcSdrh for(e2++; e2<0; precision--, e2++){ 552af005fbcSdrh assert( precision>0 ); 553a18c5681Sdrh *(bufpt++) = '0'; 554a18c5681Sdrh } 555557cc60fSdrh /* Significant digits after the decimal point */ 556557cc60fSdrh while( (precision--)>0 ){ 557557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 558a18c5681Sdrh } 559557cc60fSdrh /* Remove trailing zeros and the "." if no digits follow the "." */ 560557cc60fSdrh if( flag_rtz && flag_dp ){ 5613e9aeec0Sdrh while( bufpt[-1]=='0' ) *(--bufpt) = 0; 56259eedf79Sdrh assert( bufpt>zOut ); 5633e9aeec0Sdrh if( bufpt[-1]=='.' ){ 564557cc60fSdrh if( flag_altform2 ){ 565557cc60fSdrh *(bufpt++) = '0'; 566557cc60fSdrh }else{ 567557cc60fSdrh *(--bufpt) = 0; 568a18c5681Sdrh } 569557cc60fSdrh } 570557cc60fSdrh } 571557cc60fSdrh /* Add the "eNNN" suffix */ 57259eedf79Sdrh if( xtype==etEXP ){ 57376ff3a0eSdrh *(bufpt++) = aDigits[infop->charset]; 574557cc60fSdrh if( exp<0 ){ 575557cc60fSdrh *(bufpt++) = '-'; exp = -exp; 576557cc60fSdrh }else{ 577557cc60fSdrh *(bufpt++) = '+'; 578557cc60fSdrh } 579a18c5681Sdrh if( exp>=100 ){ 580ea678832Sdrh *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */ 581a18c5681Sdrh exp %= 100; 582a18c5681Sdrh } 583ea678832Sdrh *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */ 584ea678832Sdrh *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */ 585a18c5681Sdrh } 586557cc60fSdrh *bufpt = 0; 587557cc60fSdrh 588a18c5681Sdrh /* The converted number is in buf[] and zero terminated. Output it. 589a18c5681Sdrh ** Note that the number is in the usual order, not reversed as with 590a18c5681Sdrh ** integer conversions. */ 59159eedf79Sdrh length = (int)(bufpt-zOut); 59259eedf79Sdrh bufpt = zOut; 593a18c5681Sdrh 594a18c5681Sdrh /* Special case: Add leading zeros if the flag_zeropad flag is 595a18c5681Sdrh ** set and we are not left justified */ 596a18c5681Sdrh if( flag_zeropad && !flag_leftjustify && length < width){ 597a18c5681Sdrh int i; 598a18c5681Sdrh int nPad = width - length; 599a18c5681Sdrh for(i=width; i>=nPad; i--){ 600a18c5681Sdrh bufpt[i] = bufpt[i-nPad]; 601a18c5681Sdrh } 602a18c5681Sdrh i = prefix!=0; 603a18c5681Sdrh while( nPad-- ) bufpt[i++] = '0'; 604a18c5681Sdrh length = width; 605a18c5681Sdrh } 60669ef7036Sdrh #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */ 607a18c5681Sdrh break; 608a18c5681Sdrh case etSIZE: 609fc6ee9dfSdrh if( !bArgList ){ 610fc6ee9dfSdrh *(va_arg(ap,int*)) = pAccum->nChar; 611fc6ee9dfSdrh } 612a18c5681Sdrh length = width = 0; 613a18c5681Sdrh break; 614a18c5681Sdrh case etPERCENT: 615a18c5681Sdrh buf[0] = '%'; 616a18c5681Sdrh bufpt = buf; 617a18c5681Sdrh length = 1; 618a18c5681Sdrh break; 619a18c5681Sdrh case etCHARX: 620a5c1416dSdrh if( bArgList ){ 621fc6ee9dfSdrh bufpt = getTextArg(pArgList); 622fc6ee9dfSdrh c = bufpt ? bufpt[0] : 0; 623a5c1416dSdrh }else{ 624ea678832Sdrh c = va_arg(ap,int); 625a5c1416dSdrh } 626af8f513fSdrh if( precision>1 ){ 627af8f513fSdrh width -= precision-1; 628af8f513fSdrh if( width>1 && !flag_leftjustify ){ 629af8f513fSdrh sqlite3AppendChar(pAccum, width-1, ' '); 630af8f513fSdrh width = 0; 631a18c5681Sdrh } 632af8f513fSdrh sqlite3AppendChar(pAccum, precision-1, c); 633af8f513fSdrh } 634af8f513fSdrh length = 1; 635af8f513fSdrh buf[0] = c; 636a18c5681Sdrh bufpt = buf; 637a18c5681Sdrh break; 638a18c5681Sdrh case etSTRING: 639d93d8a81Sdrh case etDYNSTRING: 640a5c1416dSdrh if( bArgList ){ 641a5c1416dSdrh bufpt = getTextArg(pArgList); 6422a8f6712Sdrh xtype = etSTRING; 643a5c1416dSdrh }else{ 644cb485882Sdrh bufpt = va_arg(ap,char*); 645a5c1416dSdrh } 646d93d8a81Sdrh if( bufpt==0 ){ 647d93d8a81Sdrh bufpt = ""; 6482a8f6712Sdrh }else if( xtype==etDYNSTRING ){ 649d93d8a81Sdrh zExtra = bufpt; 650d93d8a81Sdrh } 651e509094bSdrh if( precision>=0 ){ 652e509094bSdrh for(length=0; length<precision && bufpt[length]; length++){} 653e509094bSdrh }else{ 654ea678832Sdrh length = sqlite3Strlen30(bufpt); 655e509094bSdrh } 656a18c5681Sdrh break; 6572a8f6712Sdrh case etSQLESCAPE: /* Escape ' characters */ 6582a8f6712Sdrh case etSQLESCAPE2: /* Escape ' and enclose in '...' */ 6592a8f6712Sdrh case etSQLESCAPE3: { /* Escape " characters */ 6608965b50eSdrh int i, j, k, n, isnull; 6614794f735Sdrh int needQuote; 662ea678832Sdrh char ch; 663f3b863edSdanielk1977 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */ 664a5c1416dSdrh char *escarg; 665a5c1416dSdrh 666a5c1416dSdrh if( bArgList ){ 667a5c1416dSdrh escarg = getTextArg(pArgList); 668a5c1416dSdrh }else{ 669a5c1416dSdrh escarg = va_arg(ap,char*); 670a5c1416dSdrh } 671f0113000Sdanielk1977 isnull = escarg==0; 672f0113000Sdanielk1977 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); 6738965b50eSdrh k = precision; 67460d4a304Sdan for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ 675f3b863edSdanielk1977 if( ch==q ) n++; 676a18c5681Sdrh } 6774794f735Sdrh needQuote = !isnull && xtype==etSQLESCAPE2; 6782a8f6712Sdrh n += i + 3; 679a18c5681Sdrh if( n>etBUFSIZE ){ 680e5ae5735Sdrh bufpt = zExtra = sqlite3Malloc( n ); 681d164fd34Sdrh if( bufpt==0 ){ 682a6353a3fSdrh setStrAccumError(pAccum, STRACCUM_NOMEM); 683d164fd34Sdrh return; 684d164fd34Sdrh } 685a18c5681Sdrh }else{ 686a18c5681Sdrh bufpt = buf; 687a18c5681Sdrh } 6880cfcf3fbSchw j = 0; 689f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 6908965b50eSdrh k = i; 6918965b50eSdrh for(i=0; i<k; i++){ 6928965b50eSdrh bufpt[j++] = ch = escarg[i]; 693f3b863edSdanielk1977 if( ch==q ) bufpt[j++] = ch; 694a18c5681Sdrh } 695f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 696a18c5681Sdrh bufpt[j] = 0; 697a18c5681Sdrh length = j; 6988965b50eSdrh /* The precision in %q and %Q means how many input characters to 6998965b50eSdrh ** consume, not the length of the output... 7008965b50eSdrh ** if( precision>=0 && precision<length ) length = precision; */ 701a18c5681Sdrh break; 7025eba8c09Sdrh } 7035f968436Sdrh case etTOKEN: { 7045f968436Sdrh Token *pToken = va_arg(ap, Token*); 705a5c1416dSdrh assert( bArgList==0 ); 706a9ab481fSdrh if( pToken && pToken->n ){ 707ade86483Sdrh sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n); 708ad6d9460Sdrh } 7095f968436Sdrh length = width = 0; 7105f968436Sdrh break; 7115f968436Sdrh } 7125f968436Sdrh case etSRCLIST: { 7135f968436Sdrh SrcList *pSrc = va_arg(ap, SrcList*); 7145f968436Sdrh int k = va_arg(ap, int); 7155f968436Sdrh struct SrcList_item *pItem = &pSrc->a[k]; 716a5c1416dSdrh assert( bArgList==0 ); 7175f968436Sdrh assert( k>=0 && k<pSrc->nSrc ); 71893a960a0Sdrh if( pItem->zDatabase ){ 719a6353a3fSdrh sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase); 720ade86483Sdrh sqlite3StrAccumAppend(pAccum, ".", 1); 7215f968436Sdrh } 722a6353a3fSdrh sqlite3StrAccumAppendAll(pAccum, pItem->zName); 7235f968436Sdrh length = width = 0; 7245f968436Sdrh break; 7255f968436Sdrh } 726874ba04cSdrh default: { 727874ba04cSdrh assert( xtype==etINVALID ); 728874ba04cSdrh return; 729874ba04cSdrh } 730a18c5681Sdrh }/* End switch over the format type */ 731a18c5681Sdrh /* 732a18c5681Sdrh ** The text of the conversion is pointed to by "bufpt" and is 733a18c5681Sdrh ** "length" characters long. The field width is "width". Do 734a18c5681Sdrh ** the output. 735a18c5681Sdrh */ 736a70a073bSdrh width -= length; 737af8f513fSdrh if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); 738ade86483Sdrh sqlite3StrAccumAppend(pAccum, bufpt, length); 739af8f513fSdrh if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); 740a70a073bSdrh 741af8f513fSdrh if( zExtra ){ 74296ceaf86Sdrh sqlite3DbFree(pAccum->db, zExtra); 743af8f513fSdrh zExtra = 0; 744af8f513fSdrh } 745a18c5681Sdrh }/* End for loop over the format string */ 746a18c5681Sdrh } /* End of function */ 747a18c5681Sdrh 748a18c5681Sdrh /* 749a70a073bSdrh ** Enlarge the memory allocation on a StrAccum object so that it is 750a70a073bSdrh ** able to accept at least N more bytes of text. 751a70a073bSdrh ** 752a70a073bSdrh ** Return the number of bytes of text that StrAccum is able to accept 753a70a073bSdrh ** after the attempted enlargement. The value returned might be zero. 754a18c5681Sdrh */ 755a70a073bSdrh static int sqlite3StrAccumEnlarge(StrAccum *p, int N){ 756a6353a3fSdrh char *zNew; 757a30d22a7Sdrh assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */ 758b49bc86aSdrh if( p->accError ){ 759b49bc86aSdrh testcase(p->accError==STRACCUM_TOOBIG); 760b49bc86aSdrh testcase(p->accError==STRACCUM_NOMEM); 761a70a073bSdrh return 0; 762ade86483Sdrh } 763c0490572Sdrh if( p->mxAlloc==0 ){ 764ade86483Sdrh N = p->nAlloc - p->nChar - 1; 765a6353a3fSdrh setStrAccumError(p, STRACCUM_TOOBIG); 766a70a073bSdrh return N; 767a18c5681Sdrh }else{ 7685f4a686fSdrh char *zOld = isMalloced(p) ? p->zText : 0; 76993a960a0Sdrh i64 szNew = p->nChar; 7705f4a686fSdrh assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) ); 771b1a6c3c1Sdrh szNew += N + 1; 7727b4d780bSdrh if( szNew+p->nChar<=p->mxAlloc ){ 7737b4d780bSdrh /* Force exponential buffer size growth as long as it does not overflow, 7747b4d780bSdrh ** to avoid having to call this routine too often */ 7757b4d780bSdrh szNew += p->nChar; 7767b4d780bSdrh } 777b1a6c3c1Sdrh if( szNew > p->mxAlloc ){ 778ade86483Sdrh sqlite3StrAccumReset(p); 779a6353a3fSdrh setStrAccumError(p, STRACCUM_TOOBIG); 780a70a073bSdrh return 0; 781b1a6c3c1Sdrh }else{ 782ea678832Sdrh p->nAlloc = (int)szNew; 783a18c5681Sdrh } 784c0490572Sdrh if( p->db ){ 785a9ef7097Sdan zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); 786b975598eSdrh }else{ 787f3cdcdccSdrh zNew = sqlite3_realloc64(zOld, p->nAlloc); 788b975598eSdrh } 78953f733c7Sdrh if( zNew ){ 7907ef4d1c4Sdrh assert( p->zText!=0 || p->nChar==0 ); 7915f4a686fSdrh if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); 792ade86483Sdrh p->zText = zNew; 7937f5a7ecdSdrh p->nAlloc = sqlite3DbMallocSize(p->db, zNew); 7945f4a686fSdrh p->printfFlags |= SQLITE_PRINTF_MALLOCED; 795ade86483Sdrh }else{ 796ade86483Sdrh sqlite3StrAccumReset(p); 797a6353a3fSdrh setStrAccumError(p, STRACCUM_NOMEM); 798a70a073bSdrh return 0; 799a70a073bSdrh } 800a70a073bSdrh } 801a70a073bSdrh return N; 802a70a073bSdrh } 803a70a073bSdrh 804a70a073bSdrh /* 805af8f513fSdrh ** Append N copies of character c to the given string buffer. 806a70a073bSdrh */ 807af8f513fSdrh void sqlite3AppendChar(StrAccum *p, int N, char c){ 808a30d22a7Sdrh testcase( p->nChar + (i64)N > 0x7fffffff ); 809a30d22a7Sdrh if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ 810a30d22a7Sdrh return; 811a30d22a7Sdrh } 8125f4a686fSdrh assert( (p->zText==p->zBase)==!isMalloced(p) ); 813af8f513fSdrh while( (N--)>0 ) p->zText[p->nChar++] = c; 814a70a073bSdrh } 815a70a073bSdrh 816a70a073bSdrh /* 817a70a073bSdrh ** The StrAccum "p" is not large enough to accept N new bytes of z[]. 818a70a073bSdrh ** So enlarge if first, then do the append. 819a70a073bSdrh ** 820a70a073bSdrh ** This is a helper routine to sqlite3StrAccumAppend() that does special-case 821a70a073bSdrh ** work (enlarging the buffer) using tail recursion, so that the 822a70a073bSdrh ** sqlite3StrAccumAppend() routine can use fast calling semantics. 823a70a073bSdrh */ 824172087fbSdrh static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){ 825a70a073bSdrh N = sqlite3StrAccumEnlarge(p, N); 826a70a073bSdrh if( N>0 ){ 827a70a073bSdrh memcpy(&p->zText[p->nChar], z, N); 828a70a073bSdrh p->nChar += N; 829a70a073bSdrh } 8305f4a686fSdrh assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) ); 831a70a073bSdrh } 832a70a073bSdrh 833a70a073bSdrh /* 834a70a073bSdrh ** Append N bytes of text from z to the StrAccum object. Increase the 835a70a073bSdrh ** size of the memory allocation for StrAccum if necessary. 836a70a073bSdrh */ 837a70a073bSdrh void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){ 8383457338cSdrh assert( z!=0 || N==0 ); 839a70a073bSdrh assert( p->zText!=0 || p->nChar==0 || p->accError ); 840a70a073bSdrh assert( N>=0 ); 841a70a073bSdrh assert( p->accError==0 || p->nAlloc==0 ); 842a70a073bSdrh if( p->nChar+N >= p->nAlloc ){ 843a70a073bSdrh enlargeAndAppend(p,z,N); 844172087fbSdrh }else{ 845b07028f7Sdrh assert( p->zText ); 846ade86483Sdrh p->nChar += N; 847172087fbSdrh memcpy(&p->zText[p->nChar-N], z, N); 848172087fbSdrh } 849483750baSdrh } 850483750baSdrh 851483750baSdrh /* 852a6353a3fSdrh ** Append the complete text of zero-terminated string z[] to the p string. 853a6353a3fSdrh */ 854a6353a3fSdrh void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){ 85553cd9646Smistachkin sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z)); 856a6353a3fSdrh } 857a6353a3fSdrh 858a6353a3fSdrh 859a6353a3fSdrh /* 860ade86483Sdrh ** Finish off a string by making sure it is zero-terminated. 861ade86483Sdrh ** Return a pointer to the resulting string. Return a NULL 862ade86483Sdrh ** pointer if any kind of error was encountered. 8635f968436Sdrh */ 864ade86483Sdrh char *sqlite3StrAccumFinish(StrAccum *p){ 865ade86483Sdrh if( p->zText ){ 8665f4a686fSdrh assert( (p->zText==p->zBase)==!isMalloced(p) ); 867ade86483Sdrh p->zText[p->nChar] = 0; 8685f4a686fSdrh if( p->mxAlloc>0 && !isMalloced(p) ){ 869633e6d57Sdrh p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); 870ade86483Sdrh if( p->zText ){ 871ade86483Sdrh memcpy(p->zText, p->zBase, p->nChar+1); 8725f4a686fSdrh p->printfFlags |= SQLITE_PRINTF_MALLOCED; 873ade86483Sdrh }else{ 874a6353a3fSdrh setStrAccumError(p, STRACCUM_NOMEM); 875ade86483Sdrh } 876ade86483Sdrh } 877ade86483Sdrh } 878ade86483Sdrh return p->zText; 879ade86483Sdrh } 880ade86483Sdrh 881ade86483Sdrh /* 882ade86483Sdrh ** Reset an StrAccum string. Reclaim all malloced memory. 883ade86483Sdrh */ 884ade86483Sdrh void sqlite3StrAccumReset(StrAccum *p){ 8855f4a686fSdrh assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) ); 8865f4a686fSdrh if( isMalloced(p) ){ 887633e6d57Sdrh sqlite3DbFree(p->db, p->zText); 8885f4a686fSdrh p->printfFlags &= ~SQLITE_PRINTF_MALLOCED; 889ade86483Sdrh } 890f089aa45Sdrh p->zText = 0; 891ade86483Sdrh } 892ade86483Sdrh 893ade86483Sdrh /* 894c0490572Sdrh ** Initialize a string accumulator. 895c0490572Sdrh ** 896c0490572Sdrh ** p: The accumulator to be initialized. 897c0490572Sdrh ** db: Pointer to a database connection. May be NULL. Lookaside 898c0490572Sdrh ** memory is used if not NULL. db->mallocFailed is set appropriately 899c0490572Sdrh ** when not NULL. 900c0490572Sdrh ** zBase: An initial buffer. May be NULL in which case the initial buffer 901c0490572Sdrh ** is malloced. 902c0490572Sdrh ** n: Size of zBase in bytes. If total space requirements never exceed 903c0490572Sdrh ** n then no memory allocations ever occur. 904c0490572Sdrh ** mx: Maximum number of bytes to accumulate. If mx==0 then no memory 905c0490572Sdrh ** allocations will ever occur. 906ade86483Sdrh */ 907c0490572Sdrh void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){ 908ade86483Sdrh p->zText = p->zBase = zBase; 909c0490572Sdrh p->db = db; 910ade86483Sdrh p->nChar = 0; 911ade86483Sdrh p->nAlloc = n; 912bb4957f8Sdrh p->mxAlloc = mx; 913b49bc86aSdrh p->accError = 0; 9145f4a686fSdrh p->printfFlags = 0; 9155f968436Sdrh } 9165f968436Sdrh 9175f968436Sdrh /* 9185f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 9195f968436Sdrh ** %-conversion extensions. 9205f968436Sdrh */ 92117435752Sdrh char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ 92217435752Sdrh char *z; 92379158e18Sdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 924ade86483Sdrh StrAccum acc; 925bc6160b0Sdrh assert( db!=0 ); 926c0490572Sdrh sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase), 927bc6160b0Sdrh db->aLimit[SQLITE_LIMIT_LENGTH]); 9285f4a686fSdrh acc.printfFlags = SQLITE_PRINTF_INTERNAL; 9295f4a686fSdrh sqlite3VXPrintf(&acc, zFormat, ap); 930ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 931b49bc86aSdrh if( acc.accError==STRACCUM_NOMEM ){ 932*4a642b60Sdrh sqlite3OomFault(db); 93317435752Sdrh } 93417435752Sdrh return z; 9355f968436Sdrh } 9365f968436Sdrh 9375f968436Sdrh /* 9385f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 9395f968436Sdrh ** %-conversion extensions. 9405f968436Sdrh */ 94117435752Sdrh char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ 9425f968436Sdrh va_list ap; 9435f968436Sdrh char *z; 9445f968436Sdrh va_start(ap, zFormat); 945ade86483Sdrh z = sqlite3VMPrintf(db, zFormat, ap); 9465f968436Sdrh va_end(ap); 9475f968436Sdrh return z; 9485f968436Sdrh } 9495f968436Sdrh 9505f968436Sdrh /* 95128dd479cSdrh ** Print into memory obtained from sqlite3_malloc(). Omit the internal 95228dd479cSdrh ** %-conversion extensions. 95328dd479cSdrh */ 95428dd479cSdrh char *sqlite3_vmprintf(const char *zFormat, va_list ap){ 955ade86483Sdrh char *z; 95628dd479cSdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 957ade86483Sdrh StrAccum acc; 9589ca95730Sdrh 9599ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 9609ca95730Sdrh if( zFormat==0 ){ 9619ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 9629ca95730Sdrh return 0; 9639ca95730Sdrh } 9649ca95730Sdrh #endif 965ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT 966ff1590eeSdrh if( sqlite3_initialize() ) return 0; 967ff1590eeSdrh #endif 968c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); 9695f4a686fSdrh sqlite3VXPrintf(&acc, zFormat, ap); 970ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 971ade86483Sdrh return z; 97228dd479cSdrh } 97328dd479cSdrh 97428dd479cSdrh /* 97528dd479cSdrh ** Print into memory obtained from sqlite3_malloc()(). Omit the internal 97628dd479cSdrh ** %-conversion extensions. 977483750baSdrh */ 9786f8a503dSdanielk1977 char *sqlite3_mprintf(const char *zFormat, ...){ 979a18c5681Sdrh va_list ap; 9805f968436Sdrh char *z; 981ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT 982ff1590eeSdrh if( sqlite3_initialize() ) return 0; 983ff1590eeSdrh #endif 984a18c5681Sdrh va_start(ap, zFormat); 985b3738b6cSdrh z = sqlite3_vmprintf(zFormat, ap); 986a18c5681Sdrh va_end(ap); 9875f968436Sdrh return z; 988a18c5681Sdrh } 989a18c5681Sdrh 990a18c5681Sdrh /* 9916f8a503dSdanielk1977 ** sqlite3_snprintf() works like snprintf() except that it ignores the 99293a5c6bdSdrh ** current locale settings. This is important for SQLite because we 99393a5c6bdSdrh ** are not able to use a "," as the decimal point in place of "." as 99493a5c6bdSdrh ** specified by some locales. 995db26d4c9Sdrh ** 996db26d4c9Sdrh ** Oops: The first two arguments of sqlite3_snprintf() are backwards 997db26d4c9Sdrh ** from the snprintf() standard. Unfortunately, it is too late to change 998db26d4c9Sdrh ** this without breaking compatibility, so we just have to live with the 999db26d4c9Sdrh ** mistake. 1000db26d4c9Sdrh ** 1001db26d4c9Sdrh ** sqlite3_vsnprintf() is the varargs version. 100293a5c6bdSdrh */ 1003db26d4c9Sdrh char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ 1004db26d4c9Sdrh StrAccum acc; 1005db26d4c9Sdrh if( n<=0 ) return zBuf; 10069ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 10079ca95730Sdrh if( zBuf==0 || zFormat==0 ) { 10089ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 100996c707a3Sdrh if( zBuf ) zBuf[0] = 0; 10109ca95730Sdrh return zBuf; 10119ca95730Sdrh } 10129ca95730Sdrh #endif 1013c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zBuf, n, 0); 10145f4a686fSdrh sqlite3VXPrintf(&acc, zFormat, ap); 1015db26d4c9Sdrh return sqlite3StrAccumFinish(&acc); 1016db26d4c9Sdrh } 10176f8a503dSdanielk1977 char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ 10185f968436Sdrh char *z; 101993a5c6bdSdrh va_list ap; 102093a5c6bdSdrh va_start(ap,zFormat); 1021db26d4c9Sdrh z = sqlite3_vsnprintf(n, zBuf, zFormat, ap); 102293a5c6bdSdrh va_end(ap); 10235f968436Sdrh return z; 102493a5c6bdSdrh } 102593a5c6bdSdrh 10263f280701Sdrh /* 10277c0c460fSdrh ** This is the routine that actually formats the sqlite3_log() message. 10287c0c460fSdrh ** We house it in a separate routine from sqlite3_log() to avoid using 10297c0c460fSdrh ** stack space on small-stack systems when logging is disabled. 10307c0c460fSdrh ** 10317c0c460fSdrh ** sqlite3_log() must render into a static buffer. It cannot dynamically 10327c0c460fSdrh ** allocate memory because it might be called while the memory allocator 10337c0c460fSdrh ** mutex is held. 1034a8dbd52aSdrh ** 1035a8dbd52aSdrh ** sqlite3VXPrintf() might ask for *temporary* memory allocations for 1036a8dbd52aSdrh ** certain format characters (%q) or for very large precisions or widths. 1037a8dbd52aSdrh ** Care must be taken that any sqlite3_log() calls that occur while the 1038a8dbd52aSdrh ** memory mutex is held do not use these mechanisms. 10397c0c460fSdrh */ 10407c0c460fSdrh static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){ 10417c0c460fSdrh StrAccum acc; /* String accumulator */ 1042a64fa912Sdrh char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */ 10437c0c460fSdrh 1044c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0); 10455f4a686fSdrh sqlite3VXPrintf(&acc, zFormat, ap); 10467c0c460fSdrh sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode, 10477c0c460fSdrh sqlite3StrAccumFinish(&acc)); 10487c0c460fSdrh } 10497c0c460fSdrh 10507c0c460fSdrh /* 10513f280701Sdrh ** Format and write a message to the log if logging is enabled. 10523f280701Sdrh */ 1053a7564663Sdrh void sqlite3_log(int iErrCode, const char *zFormat, ...){ 10543f280701Sdrh va_list ap; /* Vararg list */ 10557c0c460fSdrh if( sqlite3GlobalConfig.xLog ){ 10563f280701Sdrh va_start(ap, zFormat); 10577c0c460fSdrh renderLogMsg(iErrCode, zFormat, ap); 10583f280701Sdrh va_end(ap); 10593f280701Sdrh } 10603f280701Sdrh } 10613f280701Sdrh 106202b0e267Smistachkin #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) 1063e54ca3feSdrh /* 1064e54ca3feSdrh ** A version of printf() that understands %lld. Used for debugging. 1065e54ca3feSdrh ** The printf() built into some versions of windows does not understand %lld 1066e54ca3feSdrh ** and segfaults if you give it a long long int. 1067e54ca3feSdrh */ 1068e54ca3feSdrh void sqlite3DebugPrintf(const char *zFormat, ...){ 1069e54ca3feSdrh va_list ap; 1070ade86483Sdrh StrAccum acc; 1071e54ca3feSdrh char zBuf[500]; 1072c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); 1073e54ca3feSdrh va_start(ap,zFormat); 10745f4a686fSdrh sqlite3VXPrintf(&acc, zFormat, ap); 1075e54ca3feSdrh va_end(ap); 1076bed8e7e5Sdrh sqlite3StrAccumFinish(&acc); 1077485f0039Sdrh fprintf(stdout,"%s", zBuf); 10782ac3ee97Sdrh fflush(stdout); 1079e54ca3feSdrh } 1080e54ca3feSdrh #endif 1081c7bc4fdeSdrh 10824fa4a54fSdrh 1083c7bc4fdeSdrh /* 1084d37bea5bSdrh ** variable-argument wrapper around sqlite3VXPrintf(). The bFlags argument 1085d37bea5bSdrh ** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats. 1086c7bc4fdeSdrh */ 10875f4a686fSdrh void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){ 1088c7bc4fdeSdrh va_list ap; 1089c7bc4fdeSdrh va_start(ap,zFormat); 10905f4a686fSdrh sqlite3VXPrintf(p, zFormat, ap); 1091c7bc4fdeSdrh va_end(ap); 1092c7bc4fdeSdrh } 1093