1a18c5681Sdrh /* 2a18c5681Sdrh ** The "printf" code that follows dates from the 1980's. It is in 3a18c5681Sdrh ** the public domain. The original comments are included here for 4e84a306bSdrh ** completeness. They are very out-of-date but might be useful as 5e84a306bSdrh ** an historical reference. Most of the "enhancements" have been backed 6e84a306bSdrh ** out so that the functionality is now the same as standard printf(). 7e84a306bSdrh ** 8e84a306bSdrh ************************************************************************** 9a18c5681Sdrh ** 10ed1fddf4Sdrh ** This file contains code for a set of "printf"-like routines. These 11ed1fddf4Sdrh ** routines format strings much like the printf() from the standard C 12ed1fddf4Sdrh ** library, though the implementation here has enhancements to support 13ed1fddf4Sdrh ** SQLlite. 14a18c5681Sdrh */ 15a18c5681Sdrh #include "sqliteInt.h" 167c68d60bSdrh 17a18c5681Sdrh /* 18a18c5681Sdrh ** Conversion types fall into various categories as defined by the 19a18c5681Sdrh ** following enumeration. 20a18c5681Sdrh */ 21e84a306bSdrh #define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */ 22e84a306bSdrh #define etFLOAT 2 /* Floating point. %f */ 23e84a306bSdrh #define etEXP 3 /* Exponentional notation. %e and %E */ 24e84a306bSdrh #define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */ 25e84a306bSdrh #define etSIZE 5 /* Return number of characters processed so far. %n */ 26e84a306bSdrh #define etSTRING 6 /* Strings. %s */ 27e84a306bSdrh #define etDYNSTRING 7 /* Dynamically allocated strings. %z */ 28e84a306bSdrh #define etPERCENT 8 /* Percent symbol. %% */ 29e84a306bSdrh #define etCHARX 9 /* Characters. %c */ 30a18c5681Sdrh /* The rest are extensions, not normally found in printf() */ 31af005fbcSdrh #define etSQLESCAPE 10 /* Strings with '\'' doubled. %q */ 32af005fbcSdrh #define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '', 330cfcf3fbSchw NULL pointers replaced by SQL NULL. %Q */ 34af005fbcSdrh #define etTOKEN 12 /* a pointer to a Token structure */ 35af005fbcSdrh #define etSRCLIST 13 /* a pointer to a SrcList */ 36af005fbcSdrh #define etPOINTER 14 /* The %p conversion */ 37af005fbcSdrh #define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */ 38af005fbcSdrh #define etORDINAL 16 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */ 39e84a306bSdrh 40874ba04cSdrh #define etINVALID 0 /* Any unrecognized conversion type */ 41874ba04cSdrh 42e84a306bSdrh 43e84a306bSdrh /* 44e84a306bSdrh ** An "etByte" is an 8-bit unsigned value. 45e84a306bSdrh */ 46e84a306bSdrh typedef unsigned char etByte; 47a18c5681Sdrh 48a18c5681Sdrh /* 49a18c5681Sdrh ** Each builtin conversion character (ex: the 'd' in "%d") is described 50a18c5681Sdrh ** by an instance of the following structure 51a18c5681Sdrh */ 52a18c5681Sdrh typedef struct et_info { /* Information about each format field */ 53e84a306bSdrh char fmttype; /* The format field code letter */ 54e84a306bSdrh etByte base; /* The base for radix conversion */ 55e84a306bSdrh etByte flags; /* One or more of FLAG_ constants below */ 56e84a306bSdrh etByte type; /* Conversion paradigm */ 5776ff3a0eSdrh etByte charset; /* Offset into aDigits[] of the digits string */ 5876ff3a0eSdrh etByte prefix; /* Offset into aPrefix[] of the prefix string */ 59a18c5681Sdrh } et_info; 60a18c5681Sdrh 61a18c5681Sdrh /* 62e84a306bSdrh ** Allowed values for et_info.flags 63e84a306bSdrh */ 64e84a306bSdrh #define FLAG_SIGNED 1 /* True if the value to convert is signed */ 65e84a306bSdrh #define FLAG_INTERN 2 /* True if for internal use only */ 664794f735Sdrh #define FLAG_STRING 4 /* Allow infinity precision */ 67e84a306bSdrh 68e84a306bSdrh 69e84a306bSdrh /* 70a18c5681Sdrh ** The following table is searched linearly, so it is good to put the 71a18c5681Sdrh ** most frequently used conversion types first. 72a18c5681Sdrh */ 7376ff3a0eSdrh static const char aDigits[] = "0123456789ABCDEF0123456789abcdef"; 7476ff3a0eSdrh static const char aPrefix[] = "-x0\000X0"; 755719628aSdrh static const et_info fmtinfo[] = { 7676ff3a0eSdrh { 'd', 10, 1, etRADIX, 0, 0 }, 774794f735Sdrh { 's', 0, 4, etSTRING, 0, 0 }, 78557cc60fSdrh { 'g', 0, 1, etGENERIC, 30, 0 }, 79153c62c4Sdrh { 'z', 0, 4, etDYNSTRING, 0, 0 }, 804794f735Sdrh { 'q', 0, 4, etSQLESCAPE, 0, 0 }, 814794f735Sdrh { 'Q', 0, 4, etSQLESCAPE2, 0, 0 }, 82f3b863edSdanielk1977 { 'w', 0, 4, etSQLESCAPE3, 0, 0 }, 83e84a306bSdrh { 'c', 0, 0, etCHARX, 0, 0 }, 8476ff3a0eSdrh { 'o', 8, 0, etRADIX, 0, 2 }, 8576ff3a0eSdrh { 'u', 10, 0, etRADIX, 0, 0 }, 8676ff3a0eSdrh { 'x', 16, 0, etRADIX, 16, 1 }, 8776ff3a0eSdrh { 'X', 16, 0, etRADIX, 0, 4 }, 88b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 89e84a306bSdrh { 'f', 0, 1, etFLOAT, 0, 0 }, 9076ff3a0eSdrh { 'e', 0, 1, etEXP, 30, 0 }, 9176ff3a0eSdrh { 'E', 0, 1, etEXP, 14, 0 }, 9276ff3a0eSdrh { 'G', 0, 1, etGENERIC, 14, 0 }, 93b37df7b9Sdrh #endif 9476ff3a0eSdrh { 'i', 10, 1, etRADIX, 0, 0 }, 95e84a306bSdrh { 'n', 0, 0, etSIZE, 0, 0 }, 96e84a306bSdrh { '%', 0, 0, etPERCENT, 0, 0 }, 9776ff3a0eSdrh { 'p', 16, 0, etPOINTER, 0, 1 }, 987e3ff5d8Sdrh 997e3ff5d8Sdrh /* All the rest have the FLAG_INTERN bit set and are thus for internal 1007e3ff5d8Sdrh ** use only */ 1015f968436Sdrh { 'T', 0, 2, etTOKEN, 0, 0 }, 1025f968436Sdrh { 'S', 0, 2, etSRCLIST, 0, 0 }, 1039a99334dSdrh { 'r', 10, 3, etORDINAL, 0, 0 }, 104a18c5681Sdrh }; 105a18c5681Sdrh 106a18c5681Sdrh /* 107b37df7b9Sdrh ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point 108a18c5681Sdrh ** conversions will work. 109a18c5681Sdrh */ 110b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 111a18c5681Sdrh /* 112a18c5681Sdrh ** "*val" is a double such that 0.1 <= *val < 10.0 113a18c5681Sdrh ** Return the ascii code for the leading digit of *val, then 114a18c5681Sdrh ** multiply "*val" by 10.0 to renormalize. 115a18c5681Sdrh ** 116a18c5681Sdrh ** Example: 117a18c5681Sdrh ** input: *val = 3.14159 118a18c5681Sdrh ** output: *val = 1.4159 function return = '3' 119a18c5681Sdrh ** 120a18c5681Sdrh ** The counter *cnt is incremented each time. After counter exceeds 121a18c5681Sdrh ** 16 (the number of significant digits in a 64-bit float) '0' is 122a18c5681Sdrh ** always returned. 123a18c5681Sdrh */ 124ea678832Sdrh static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ 125a18c5681Sdrh int digit; 126384eef32Sdrh LONGDOUBLE_TYPE d; 12772b3fbc7Sdrh if( (*cnt)<=0 ) return '0'; 12872b3fbc7Sdrh (*cnt)--; 129a18c5681Sdrh digit = (int)*val; 130a18c5681Sdrh d = digit; 131a18c5681Sdrh digit += '0'; 132a18c5681Sdrh *val = (*val - d)*10.0; 133ea678832Sdrh return (char)digit; 134a18c5681Sdrh } 135b37df7b9Sdrh #endif /* SQLITE_OMIT_FLOATING_POINT */ 136a18c5681Sdrh 13779158e18Sdrh /* 138a6353a3fSdrh ** Set the StrAccum object to an error mode. 139a6353a3fSdrh */ 140a5c1416dSdrh static void setStrAccumError(StrAccum *p, u8 eError){ 141a6353a3fSdrh p->accError = eError; 142a6353a3fSdrh p->nAlloc = 0; 143a6353a3fSdrh } 144a6353a3fSdrh 145a6353a3fSdrh /* 146a5c1416dSdrh ** Extra argument values from a PrintfArguments object 147a5c1416dSdrh */ 148a5c1416dSdrh static sqlite3_int64 getIntArg(PrintfArguments *p){ 149a5c1416dSdrh if( p->nArg<=p->nUsed ) return 0; 150a5c1416dSdrh return sqlite3_value_int64(p->apArg[p->nUsed++]); 151a5c1416dSdrh } 152a5c1416dSdrh static double getDoubleArg(PrintfArguments *p){ 153a5c1416dSdrh if( p->nArg<=p->nUsed ) return 0.0; 154a5c1416dSdrh return sqlite3_value_double(p->apArg[p->nUsed++]); 155a5c1416dSdrh } 156a5c1416dSdrh static char *getTextArg(PrintfArguments *p){ 157a5c1416dSdrh if( p->nArg<=p->nUsed ) return 0; 158a5c1416dSdrh return (char*)sqlite3_value_text(p->apArg[p->nUsed++]); 159a5c1416dSdrh } 160a5c1416dSdrh 161a5c1416dSdrh 162a5c1416dSdrh /* 16379158e18Sdrh ** On machines with a small stack size, you can redefine the 164ed1fddf4Sdrh ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired. 16579158e18Sdrh */ 16679158e18Sdrh #ifndef SQLITE_PRINT_BUF_SIZE 16759eedf79Sdrh # define SQLITE_PRINT_BUF_SIZE 70 16850d654daSdrh #endif 16979158e18Sdrh #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ 170a18c5681Sdrh 171a18c5681Sdrh /* 172ed1fddf4Sdrh ** Render a string given by "fmt" into the StrAccum object. 173a18c5681Sdrh */ 174f089aa45Sdrh void sqlite3VXPrintf( 175ade86483Sdrh StrAccum *pAccum, /* Accumulate results here */ 176a5c1416dSdrh u32 bFlags, /* SQLITE_PRINTF_* flags */ 1775f968436Sdrh const char *fmt, /* Format string */ 1785f968436Sdrh va_list ap /* arguments */ 179a18c5681Sdrh ){ 180e84a306bSdrh int c; /* Next character in the format string */ 181e84a306bSdrh char *bufpt; /* Pointer to the conversion buffer */ 182e84a306bSdrh int precision; /* Precision of the current field */ 183e84a306bSdrh int length; /* Length of the field */ 184e84a306bSdrh int idx; /* A general purpose loop counter */ 185a18c5681Sdrh int width; /* Width of the current field */ 186e84a306bSdrh etByte flag_leftjustify; /* True if "-" flag is present */ 187e84a306bSdrh etByte flag_plussign; /* True if "+" flag is present */ 188e84a306bSdrh etByte flag_blanksign; /* True if " " flag is present */ 189e84a306bSdrh etByte flag_alternateform; /* True if "#" flag is present */ 190531fe878Sdrh etByte flag_altform2; /* True if "!" flag is present */ 191e84a306bSdrh etByte flag_zeropad; /* True if field width constant starts with zero */ 192e84a306bSdrh etByte flag_long; /* True if "l" flag is present */ 193a34b6764Sdrh etByte flag_longlong; /* True if the "ll" flag is present */ 1943e9aeec0Sdrh etByte done; /* Loop termination flag */ 195ed1fddf4Sdrh etByte xtype = 0; /* Conversion paradigm */ 196a5c1416dSdrh u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */ 197a5c1416dSdrh u8 useIntern; /* Ok to use internal conversions (ex: %T) */ 198ed1fddf4Sdrh char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ 19927436af7Sdrh sqlite_uint64 longvalue; /* Value for integer types */ 200384eef32Sdrh LONGDOUBLE_TYPE realvalue; /* Value for real types */ 2015719628aSdrh const et_info *infop; /* Pointer to the appropriate info structure */ 20259eedf79Sdrh char *zOut; /* Rendering buffer */ 20359eedf79Sdrh int nOut; /* Size of the rendering buffer */ 204af8f513fSdrh char *zExtra = 0; /* Malloced memory used by some conversion */ 205b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 206557cc60fSdrh int exp, e2; /* exponent of real numbers */ 207ed1fddf4Sdrh int nsd; /* Number of significant digits returned */ 208a18c5681Sdrh double rounder; /* Used for rounding floating point values */ 209e84a306bSdrh etByte flag_dp; /* True if decimal point should be shown */ 210e84a306bSdrh etByte flag_rtz; /* True if trailing zeros should be removed */ 211a18c5681Sdrh #endif 212a5c1416dSdrh PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */ 213ed1fddf4Sdrh char buf[etBUFSIZE]; /* Conversion buffer */ 214a18c5681Sdrh 215a18c5681Sdrh bufpt = 0; 216a5c1416dSdrh if( bFlags ){ 217a5c1416dSdrh if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){ 218a5c1416dSdrh pArgList = va_arg(ap, PrintfArguments*); 219a5c1416dSdrh } 220a5c1416dSdrh useIntern = bFlags & SQLITE_PRINTF_INTERNAL; 221a5c1416dSdrh }else{ 222a5c1416dSdrh bArgList = useIntern = 0; 223a5c1416dSdrh } 224a18c5681Sdrh for(; (c=(*fmt))!=0; ++fmt){ 225a18c5681Sdrh if( c!='%' ){ 226a18c5681Sdrh bufpt = (char *)fmt; 227760b1598Sdrh #if HAVE_STRCHRNUL 228760b1598Sdrh fmt = strchrnul(fmt, '%'); 229760b1598Sdrh #else 230760b1598Sdrh do{ fmt++; }while( *fmt && *fmt != '%' ); 231760b1598Sdrh #endif 232a70a073bSdrh sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt)); 233760b1598Sdrh if( *fmt==0 ) break; 234a18c5681Sdrh } 235a18c5681Sdrh if( (c=(*++fmt))==0 ){ 236ade86483Sdrh sqlite3StrAccumAppend(pAccum, "%", 1); 237a18c5681Sdrh break; 238a18c5681Sdrh } 239a18c5681Sdrh /* Find out what flags are present */ 240a18c5681Sdrh flag_leftjustify = flag_plussign = flag_blanksign = 241557cc60fSdrh flag_alternateform = flag_altform2 = flag_zeropad = 0; 2423e9aeec0Sdrh done = 0; 243a18c5681Sdrh do{ 244a18c5681Sdrh switch( c ){ 2453e9aeec0Sdrh case '-': flag_leftjustify = 1; break; 2463e9aeec0Sdrh case '+': flag_plussign = 1; break; 2473e9aeec0Sdrh case ' ': flag_blanksign = 1; break; 2483e9aeec0Sdrh case '#': flag_alternateform = 1; break; 2493e9aeec0Sdrh case '!': flag_altform2 = 1; break; 2503e9aeec0Sdrh case '0': flag_zeropad = 1; break; 2513e9aeec0Sdrh default: done = 1; break; 252a18c5681Sdrh } 2533e9aeec0Sdrh }while( !done && (c=(*++fmt))!=0 ); 254a18c5681Sdrh /* Get the field width */ 255a18c5681Sdrh width = 0; 256a18c5681Sdrh if( c=='*' ){ 257a5c1416dSdrh if( bArgList ){ 258a5c1416dSdrh width = (int)getIntArg(pArgList); 259a5c1416dSdrh }else{ 260a18c5681Sdrh width = va_arg(ap,int); 261a5c1416dSdrh } 262a18c5681Sdrh if( width<0 ){ 263a18c5681Sdrh flag_leftjustify = 1; 264b6f47debSdrh width = width >= -2147483647 ? -width : 0; 265a18c5681Sdrh } 266a18c5681Sdrh c = *++fmt; 267a18c5681Sdrh }else{ 268b6f47debSdrh unsigned wx = 0; 26917a68934Sdrh while( c>='0' && c<='9' ){ 270b6f47debSdrh wx = wx*10 + c - '0'; 271a18c5681Sdrh c = *++fmt; 272a18c5681Sdrh } 273b6f47debSdrh testcase( wx>0x7fffffff ); 274b6f47debSdrh width = wx & 0x7fffffff; 275a18c5681Sdrh } 2768c069147Sdan 277a18c5681Sdrh /* Get the precision */ 278a18c5681Sdrh if( c=='.' ){ 279a18c5681Sdrh precision = 0; 280a18c5681Sdrh c = *++fmt; 281a18c5681Sdrh if( c=='*' ){ 282a5c1416dSdrh if( bArgList ){ 283a5c1416dSdrh precision = (int)getIntArg(pArgList); 284a5c1416dSdrh }else{ 285a18c5681Sdrh precision = va_arg(ap,int); 286a5c1416dSdrh } 287a18c5681Sdrh c = *++fmt; 288b6f47debSdrh if( precision<0 ){ 289b6f47debSdrh precision = precision >= -2147483647 ? -precision : -1; 290b6f47debSdrh } 291a18c5681Sdrh }else{ 292b6f47debSdrh unsigned px = 0; 29317a68934Sdrh while( c>='0' && c<='9' ){ 294b6f47debSdrh px = px*10 + c - '0'; 295a18c5681Sdrh c = *++fmt; 296a18c5681Sdrh } 297b6f47debSdrh testcase( px>0x7fffffff ); 298b6f47debSdrh precision = px & 0x7fffffff; 299a18c5681Sdrh } 300a18c5681Sdrh }else{ 301a18c5681Sdrh precision = -1; 302a18c5681Sdrh } 303a18c5681Sdrh /* Get the conversion type modifier */ 304a18c5681Sdrh if( c=='l' ){ 305a18c5681Sdrh flag_long = 1; 306a18c5681Sdrh c = *++fmt; 307a34b6764Sdrh if( c=='l' ){ 308a34b6764Sdrh flag_longlong = 1; 309a34b6764Sdrh c = *++fmt; 310a18c5681Sdrh }else{ 311a34b6764Sdrh flag_longlong = 0; 312a34b6764Sdrh } 313a34b6764Sdrh }else{ 314a34b6764Sdrh flag_long = flag_longlong = 0; 315a18c5681Sdrh } 316a18c5681Sdrh /* Fetch the info entry for the field */ 317874ba04cSdrh infop = &fmtinfo[0]; 318874ba04cSdrh xtype = etINVALID; 31900e13613Sdanielk1977 for(idx=0; idx<ArraySize(fmtinfo); idx++){ 320a18c5681Sdrh if( c==fmtinfo[idx].fmttype ){ 321a18c5681Sdrh infop = &fmtinfo[idx]; 322a5c1416dSdrh if( useIntern || (infop->flags & FLAG_INTERN)==0 ){ 323e84a306bSdrh xtype = infop->type; 324c4413565Sdrh }else{ 325ade86483Sdrh return; 3265f968436Sdrh } 327a18c5681Sdrh break; 328a18c5681Sdrh } 329a18c5681Sdrh } 33043617e9aSdrh 331a18c5681Sdrh /* 332a18c5681Sdrh ** At this point, variables are initialized as follows: 333a18c5681Sdrh ** 334a18c5681Sdrh ** flag_alternateform TRUE if a '#' is present. 3353e9aeec0Sdrh ** flag_altform2 TRUE if a '!' is present. 336a18c5681Sdrh ** flag_plussign TRUE if a '+' is present. 337a18c5681Sdrh ** flag_leftjustify TRUE if a '-' is present or if the 338a18c5681Sdrh ** field width was negative. 339a18c5681Sdrh ** flag_zeropad TRUE if the width began with 0. 340a18c5681Sdrh ** flag_long TRUE if the letter 'l' (ell) prefixed 341a18c5681Sdrh ** the conversion character. 342a34b6764Sdrh ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed 343a34b6764Sdrh ** the conversion character. 344a18c5681Sdrh ** flag_blanksign TRUE if a ' ' is present. 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: 354fe63d1c9Sdrh flag_longlong = sizeof(char*)==sizeof(i64); 355fe63d1c9Sdrh flag_long = sizeof(char*)==sizeof(long int); 356fe63d1c9Sdrh /* Fall through into the next case */ 3579a99334dSdrh case etORDINAL: 358a18c5681Sdrh case etRADIX: 359e84a306bSdrh if( infop->flags & FLAG_SIGNED ){ 360e9707671Sdrh i64 v; 361a5c1416dSdrh if( bArgList ){ 362a5c1416dSdrh v = getIntArg(pArgList); 363a5c1416dSdrh }else if( flag_longlong ){ 364eeb23a4cSdrh v = va_arg(ap,i64); 365eeb23a4cSdrh }else if( flag_long ){ 366eeb23a4cSdrh v = va_arg(ap,long int); 367eeb23a4cSdrh }else{ 368eeb23a4cSdrh v = va_arg(ap,int); 369eeb23a4cSdrh } 370e9707671Sdrh if( v<0 ){ 371158b9cb9Sdrh if( v==SMALLEST_INT64 ){ 372158b9cb9Sdrh longvalue = ((u64)1)<<63; 373158b9cb9Sdrh }else{ 374158b9cb9Sdrh longvalue = -v; 375158b9cb9Sdrh } 376cfcdaefeSdanielk1977 prefix = '-'; 377cfcdaefeSdanielk1977 }else{ 378e9707671Sdrh longvalue = v; 379e9707671Sdrh if( flag_plussign ) prefix = '+'; 380a18c5681Sdrh else if( flag_blanksign ) prefix = ' '; 381a18c5681Sdrh else prefix = 0; 382cfcdaefeSdanielk1977 } 383e9707671Sdrh }else{ 384a5c1416dSdrh if( bArgList ){ 385a5c1416dSdrh longvalue = (u64)getIntArg(pArgList); 386a5c1416dSdrh }else if( flag_longlong ){ 387eeb23a4cSdrh longvalue = va_arg(ap,u64); 388eeb23a4cSdrh }else if( flag_long ){ 389eeb23a4cSdrh longvalue = va_arg(ap,unsigned long int); 390eeb23a4cSdrh }else{ 391eeb23a4cSdrh longvalue = va_arg(ap,unsigned int); 392eeb23a4cSdrh } 393e9707671Sdrh prefix = 0; 394e9707671Sdrh } 395e9707671Sdrh if( longvalue==0 ) flag_alternateform = 0; 396a18c5681Sdrh if( flag_zeropad && precision<width-(prefix!=0) ){ 397a18c5681Sdrh precision = width-(prefix!=0); 398a18c5681Sdrh } 39959eedf79Sdrh if( precision<etBUFSIZE-10 ){ 40059eedf79Sdrh nOut = etBUFSIZE; 40159eedf79Sdrh zOut = buf; 40259eedf79Sdrh }else{ 40359eedf79Sdrh nOut = precision + 10; 40459eedf79Sdrh zOut = zExtra = sqlite3Malloc( nOut ); 40559eedf79Sdrh if( zOut==0 ){ 406a6353a3fSdrh setStrAccumError(pAccum, STRACCUM_NOMEM); 40759eedf79Sdrh return; 40859eedf79Sdrh } 40959eedf79Sdrh } 41059eedf79Sdrh bufpt = &zOut[nOut-1]; 4119a99334dSdrh if( xtype==etORDINAL ){ 41243f6e064Sdrh static const char zOrd[] = "thstndrd"; 413ea678832Sdrh int x = (int)(longvalue % 10); 41443f6e064Sdrh if( x>=4 || (longvalue/10)%10==1 ){ 41543f6e064Sdrh x = 0; 41643f6e064Sdrh } 41759eedf79Sdrh *(--bufpt) = zOrd[x*2+1]; 41859eedf79Sdrh *(--bufpt) = zOrd[x*2]; 4199a99334dSdrh } 420a18c5681Sdrh { 4210e682099Sdrh const char *cset = &aDigits[infop->charset]; 4220e682099Sdrh u8 base = infop->base; 423a18c5681Sdrh do{ /* Convert to ascii */ 424a18c5681Sdrh *(--bufpt) = cset[longvalue%base]; 425a18c5681Sdrh longvalue = longvalue/base; 426a18c5681Sdrh }while( longvalue>0 ); 427a18c5681Sdrh } 42859eedf79Sdrh length = (int)(&zOut[nOut-1]-bufpt); 429a18c5681Sdrh for(idx=precision-length; idx>0; idx--){ 430a18c5681Sdrh *(--bufpt) = '0'; /* Zero pad */ 431a18c5681Sdrh } 432a18c5681Sdrh if( prefix ) *(--bufpt) = prefix; /* Add sign */ 433a18c5681Sdrh if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ 43476ff3a0eSdrh const char *pre; 43576ff3a0eSdrh char x; 43676ff3a0eSdrh pre = &aPrefix[infop->prefix]; 43776ff3a0eSdrh for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; 438a18c5681Sdrh } 43959eedf79Sdrh length = (int)(&zOut[nOut-1]-bufpt); 440a18c5681Sdrh break; 441a18c5681Sdrh case etFLOAT: 442a18c5681Sdrh case etEXP: 443a18c5681Sdrh case etGENERIC: 444a5c1416dSdrh if( bArgList ){ 445a5c1416dSdrh realvalue = getDoubleArg(pArgList); 446a5c1416dSdrh }else{ 447a18c5681Sdrh realvalue = va_arg(ap,double); 448a5c1416dSdrh } 44969ef7036Sdrh #ifdef SQLITE_OMIT_FLOATING_POINT 45069ef7036Sdrh length = 0; 45169ef7036Sdrh #else 452a18c5681Sdrh if( precision<0 ) precision = 6; /* Set default precision */ 453a18c5681Sdrh if( realvalue<0.0 ){ 454a18c5681Sdrh realvalue = -realvalue; 455a18c5681Sdrh prefix = '-'; 456a18c5681Sdrh }else{ 457a18c5681Sdrh if( flag_plussign ) prefix = '+'; 458a18c5681Sdrh else if( flag_blanksign ) prefix = ' '; 459a18c5681Sdrh else prefix = 0; 460a18c5681Sdrh } 4613e9aeec0Sdrh if( xtype==etGENERIC && precision>0 ) precision--; 462a30d22a7Sdrh testcase( precision>0xfff ); 46374b42275Sdrh for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){} 4643e9aeec0Sdrh if( xtype==etFLOAT ) realvalue += rounder; 465a18c5681Sdrh /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ 466a18c5681Sdrh exp = 0; 467ea678832Sdrh if( sqlite3IsNaN((double)realvalue) ){ 46853c14021Sdrh bufpt = "NaN"; 46953c14021Sdrh length = 3; 47053c14021Sdrh break; 47153c14021Sdrh } 472a18c5681Sdrh if( realvalue>0.0 ){ 47372b3fbc7Sdrh LONGDOUBLE_TYPE scale = 1.0; 4744ef94130Sdrh while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;} 47572b3fbc7Sdrh while( realvalue>=1e64*scale && exp<=350 ){ scale *= 1e64; exp+=64; } 47672b3fbc7Sdrh while( realvalue>=1e8*scale && exp<=350 ){ scale *= 1e8; exp+=8; } 47772b3fbc7Sdrh while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; } 47872b3fbc7Sdrh realvalue /= scale; 479af005fbcSdrh while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } 480af005fbcSdrh while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } 481af005fbcSdrh if( exp>350 ){ 48253c14021Sdrh if( prefix=='-' ){ 48353c14021Sdrh bufpt = "-Inf"; 48453c14021Sdrh }else if( prefix=='+' ){ 48553c14021Sdrh bufpt = "+Inf"; 48653c14021Sdrh }else{ 48753c14021Sdrh bufpt = "Inf"; 48853c14021Sdrh } 489ea678832Sdrh length = sqlite3Strlen30(bufpt); 490a18c5681Sdrh break; 491a18c5681Sdrh } 492a18c5681Sdrh } 493a18c5681Sdrh bufpt = buf; 494a18c5681Sdrh /* 495a18c5681Sdrh ** If the field type is etGENERIC, then convert to either etEXP 496a18c5681Sdrh ** or etFLOAT, as appropriate. 497a18c5681Sdrh */ 498a18c5681Sdrh if( xtype!=etFLOAT ){ 499a18c5681Sdrh realvalue += rounder; 500a18c5681Sdrh if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } 501a18c5681Sdrh } 502a18c5681Sdrh if( xtype==etGENERIC ){ 503a18c5681Sdrh flag_rtz = !flag_alternateform; 504a18c5681Sdrh if( exp<-4 || exp>precision ){ 505a18c5681Sdrh xtype = etEXP; 506a18c5681Sdrh }else{ 507a18c5681Sdrh precision = precision - exp; 508a18c5681Sdrh xtype = etFLOAT; 509a18c5681Sdrh } 510a18c5681Sdrh }else{ 51172b3fbc7Sdrh flag_rtz = flag_altform2; 512a18c5681Sdrh } 513557cc60fSdrh if( xtype==etEXP ){ 514557cc60fSdrh e2 = 0; 515557cc60fSdrh }else{ 516557cc60fSdrh e2 = exp; 517557cc60fSdrh } 51874b42275Sdrh if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){ 51974b42275Sdrh bufpt = zExtra 52074b42275Sdrh = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 ); 52159eedf79Sdrh if( bufpt==0 ){ 522a6353a3fSdrh setStrAccumError(pAccum, STRACCUM_NOMEM); 52359eedf79Sdrh return; 52459eedf79Sdrh } 52559eedf79Sdrh } 52659eedf79Sdrh zOut = bufpt; 52772b3fbc7Sdrh nsd = 16 + flag_altform2*10; 528ea678832Sdrh flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; 529557cc60fSdrh /* The sign in front of the number */ 530557cc60fSdrh if( prefix ){ 531557cc60fSdrh *(bufpt++) = prefix; 532557cc60fSdrh } 533557cc60fSdrh /* Digits prior to the decimal point */ 534557cc60fSdrh if( e2<0 ){ 535557cc60fSdrh *(bufpt++) = '0'; 536557cc60fSdrh }else{ 537557cc60fSdrh for(; e2>=0; e2--){ 538557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 539557cc60fSdrh } 540557cc60fSdrh } 541557cc60fSdrh /* The decimal point */ 542557cc60fSdrh if( flag_dp ){ 543557cc60fSdrh *(bufpt++) = '.'; 544557cc60fSdrh } 545557cc60fSdrh /* "0" digits after the decimal point but before the first 546557cc60fSdrh ** significant digit of the number */ 547af005fbcSdrh for(e2++; e2<0; precision--, e2++){ 548af005fbcSdrh assert( precision>0 ); 549a18c5681Sdrh *(bufpt++) = '0'; 550a18c5681Sdrh } 551557cc60fSdrh /* Significant digits after the decimal point */ 552557cc60fSdrh while( (precision--)>0 ){ 553557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 554a18c5681Sdrh } 555557cc60fSdrh /* Remove trailing zeros and the "." if no digits follow the "." */ 556557cc60fSdrh if( flag_rtz && flag_dp ){ 5573e9aeec0Sdrh while( bufpt[-1]=='0' ) *(--bufpt) = 0; 55859eedf79Sdrh assert( bufpt>zOut ); 5593e9aeec0Sdrh if( bufpt[-1]=='.' ){ 560557cc60fSdrh if( flag_altform2 ){ 561557cc60fSdrh *(bufpt++) = '0'; 562557cc60fSdrh }else{ 563557cc60fSdrh *(--bufpt) = 0; 564a18c5681Sdrh } 565557cc60fSdrh } 566557cc60fSdrh } 567557cc60fSdrh /* Add the "eNNN" suffix */ 56859eedf79Sdrh if( xtype==etEXP ){ 56976ff3a0eSdrh *(bufpt++) = aDigits[infop->charset]; 570557cc60fSdrh if( exp<0 ){ 571557cc60fSdrh *(bufpt++) = '-'; exp = -exp; 572557cc60fSdrh }else{ 573557cc60fSdrh *(bufpt++) = '+'; 574557cc60fSdrh } 575a18c5681Sdrh if( exp>=100 ){ 576ea678832Sdrh *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */ 577a18c5681Sdrh exp %= 100; 578a18c5681Sdrh } 579ea678832Sdrh *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */ 580ea678832Sdrh *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */ 581a18c5681Sdrh } 582557cc60fSdrh *bufpt = 0; 583557cc60fSdrh 584a18c5681Sdrh /* The converted number is in buf[] and zero terminated. Output it. 585a18c5681Sdrh ** Note that the number is in the usual order, not reversed as with 586a18c5681Sdrh ** integer conversions. */ 58759eedf79Sdrh length = (int)(bufpt-zOut); 58859eedf79Sdrh bufpt = zOut; 589a18c5681Sdrh 590a18c5681Sdrh /* Special case: Add leading zeros if the flag_zeropad flag is 591a18c5681Sdrh ** set and we are not left justified */ 592a18c5681Sdrh if( flag_zeropad && !flag_leftjustify && length < width){ 593a18c5681Sdrh int i; 594a18c5681Sdrh int nPad = width - length; 595a18c5681Sdrh for(i=width; i>=nPad; i--){ 596a18c5681Sdrh bufpt[i] = bufpt[i-nPad]; 597a18c5681Sdrh } 598a18c5681Sdrh i = prefix!=0; 599a18c5681Sdrh while( nPad-- ) bufpt[i++] = '0'; 600a18c5681Sdrh length = width; 601a18c5681Sdrh } 60269ef7036Sdrh #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */ 603a18c5681Sdrh break; 604a18c5681Sdrh case etSIZE: 605fc6ee9dfSdrh if( !bArgList ){ 606fc6ee9dfSdrh *(va_arg(ap,int*)) = pAccum->nChar; 607fc6ee9dfSdrh } 608a18c5681Sdrh length = width = 0; 609a18c5681Sdrh break; 610a18c5681Sdrh case etPERCENT: 611a18c5681Sdrh buf[0] = '%'; 612a18c5681Sdrh bufpt = buf; 613a18c5681Sdrh length = 1; 614a18c5681Sdrh break; 615a18c5681Sdrh case etCHARX: 616a5c1416dSdrh if( bArgList ){ 617fc6ee9dfSdrh bufpt = getTextArg(pArgList); 618fc6ee9dfSdrh c = bufpt ? bufpt[0] : 0; 619a5c1416dSdrh }else{ 620ea678832Sdrh c = va_arg(ap,int); 621a5c1416dSdrh } 622af8f513fSdrh if( precision>1 ){ 623af8f513fSdrh width -= precision-1; 624af8f513fSdrh if( width>1 && !flag_leftjustify ){ 625af8f513fSdrh sqlite3AppendChar(pAccum, width-1, ' '); 626af8f513fSdrh width = 0; 627a18c5681Sdrh } 628af8f513fSdrh sqlite3AppendChar(pAccum, precision-1, c); 629af8f513fSdrh } 630af8f513fSdrh length = 1; 631af8f513fSdrh buf[0] = c; 632a18c5681Sdrh bufpt = buf; 633a18c5681Sdrh break; 634a18c5681Sdrh case etSTRING: 635d93d8a81Sdrh case etDYNSTRING: 636a5c1416dSdrh if( bArgList ){ 637a5c1416dSdrh bufpt = getTextArg(pArgList); 638a5c1416dSdrh }else{ 639cb485882Sdrh bufpt = va_arg(ap,char*); 640a5c1416dSdrh } 641d93d8a81Sdrh if( bufpt==0 ){ 642d93d8a81Sdrh bufpt = ""; 643a5c1416dSdrh }else if( xtype==etDYNSTRING && !bArgList ){ 644d93d8a81Sdrh zExtra = bufpt; 645d93d8a81Sdrh } 646e509094bSdrh if( precision>=0 ){ 647e509094bSdrh for(length=0; length<precision && bufpt[length]; length++){} 648e509094bSdrh }else{ 649ea678832Sdrh length = sqlite3Strlen30(bufpt); 650e509094bSdrh } 651a18c5681Sdrh break; 652a18c5681Sdrh case etSQLESCAPE: 653f3b863edSdanielk1977 case etSQLESCAPE2: 654f3b863edSdanielk1977 case etSQLESCAPE3: { 6558965b50eSdrh int i, j, k, n, isnull; 6564794f735Sdrh int needQuote; 657ea678832Sdrh char ch; 658f3b863edSdanielk1977 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */ 659a5c1416dSdrh char *escarg; 660a5c1416dSdrh 661a5c1416dSdrh if( bArgList ){ 662a5c1416dSdrh escarg = getTextArg(pArgList); 663a5c1416dSdrh }else{ 664a5c1416dSdrh escarg = va_arg(ap,char*); 665a5c1416dSdrh } 666f0113000Sdanielk1977 isnull = escarg==0; 667f0113000Sdanielk1977 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); 6688965b50eSdrh k = precision; 66960d4a304Sdan for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ 670f3b863edSdanielk1977 if( ch==q ) n++; 671a18c5681Sdrh } 6724794f735Sdrh needQuote = !isnull && xtype==etSQLESCAPE2; 6734794f735Sdrh n += i + 1 + needQuote*2; 674a18c5681Sdrh if( n>etBUFSIZE ){ 675e5ae5735Sdrh bufpt = zExtra = sqlite3Malloc( n ); 676d164fd34Sdrh if( bufpt==0 ){ 677a6353a3fSdrh setStrAccumError(pAccum, STRACCUM_NOMEM); 678d164fd34Sdrh return; 679d164fd34Sdrh } 680a18c5681Sdrh }else{ 681a18c5681Sdrh bufpt = buf; 682a18c5681Sdrh } 6830cfcf3fbSchw j = 0; 684f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 6858965b50eSdrh k = i; 6868965b50eSdrh for(i=0; i<k; i++){ 6878965b50eSdrh bufpt[j++] = ch = escarg[i]; 688f3b863edSdanielk1977 if( ch==q ) bufpt[j++] = ch; 689a18c5681Sdrh } 690f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 691a18c5681Sdrh bufpt[j] = 0; 692a18c5681Sdrh length = j; 6938965b50eSdrh /* The precision in %q and %Q means how many input characters to 6948965b50eSdrh ** consume, not the length of the output... 6958965b50eSdrh ** if( precision>=0 && precision<length ) length = precision; */ 696a18c5681Sdrh break; 6975eba8c09Sdrh } 6985f968436Sdrh case etTOKEN: { 6995f968436Sdrh Token *pToken = va_arg(ap, Token*); 700a5c1416dSdrh assert( bArgList==0 ); 701a9ab481fSdrh if( pToken && pToken->n ){ 702ade86483Sdrh sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n); 703ad6d9460Sdrh } 7045f968436Sdrh length = width = 0; 7055f968436Sdrh break; 7065f968436Sdrh } 7075f968436Sdrh case etSRCLIST: { 7085f968436Sdrh SrcList *pSrc = va_arg(ap, SrcList*); 7095f968436Sdrh int k = va_arg(ap, int); 7105f968436Sdrh struct SrcList_item *pItem = &pSrc->a[k]; 711a5c1416dSdrh assert( bArgList==0 ); 7125f968436Sdrh assert( k>=0 && k<pSrc->nSrc ); 71393a960a0Sdrh if( pItem->zDatabase ){ 714a6353a3fSdrh sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase); 715ade86483Sdrh sqlite3StrAccumAppend(pAccum, ".", 1); 7165f968436Sdrh } 717a6353a3fSdrh sqlite3StrAccumAppendAll(pAccum, pItem->zName); 7185f968436Sdrh length = width = 0; 7195f968436Sdrh break; 7205f968436Sdrh } 721874ba04cSdrh default: { 722874ba04cSdrh assert( xtype==etINVALID ); 723874ba04cSdrh return; 724874ba04cSdrh } 725a18c5681Sdrh }/* End switch over the format type */ 726a18c5681Sdrh /* 727a18c5681Sdrh ** The text of the conversion is pointed to by "bufpt" and is 728a18c5681Sdrh ** "length" characters long. The field width is "width". Do 729a18c5681Sdrh ** the output. 730a18c5681Sdrh */ 731a70a073bSdrh width -= length; 732af8f513fSdrh if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); 733ade86483Sdrh sqlite3StrAccumAppend(pAccum, bufpt, length); 734af8f513fSdrh if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); 735a70a073bSdrh 736af8f513fSdrh if( zExtra ){ 737af8f513fSdrh sqlite3_free(zExtra); 738af8f513fSdrh zExtra = 0; 739af8f513fSdrh } 740a18c5681Sdrh }/* End for loop over the format string */ 741a18c5681Sdrh } /* End of function */ 742a18c5681Sdrh 743a18c5681Sdrh /* 744a70a073bSdrh ** Enlarge the memory allocation on a StrAccum object so that it is 745a70a073bSdrh ** able to accept at least N more bytes of text. 746a70a073bSdrh ** 747a70a073bSdrh ** Return the number of bytes of text that StrAccum is able to accept 748a70a073bSdrh ** after the attempted enlargement. The value returned might be zero. 749a18c5681Sdrh */ 750a70a073bSdrh static int sqlite3StrAccumEnlarge(StrAccum *p, int N){ 751a6353a3fSdrh char *zNew; 752a30d22a7Sdrh assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */ 753b49bc86aSdrh if( p->accError ){ 754b49bc86aSdrh testcase(p->accError==STRACCUM_TOOBIG); 755b49bc86aSdrh testcase(p->accError==STRACCUM_NOMEM); 756a70a073bSdrh return 0; 757ade86483Sdrh } 758ade86483Sdrh if( !p->useMalloc ){ 759ade86483Sdrh N = p->nAlloc - p->nChar - 1; 760a6353a3fSdrh setStrAccumError(p, STRACCUM_TOOBIG); 761a70a073bSdrh return N; 762a18c5681Sdrh }else{ 763a9ef7097Sdan char *zOld = (p->zText==p->zBase ? 0 : p->zText); 76493a960a0Sdrh i64 szNew = p->nChar; 765b1a6c3c1Sdrh szNew += N + 1; 7667b4d780bSdrh if( szNew+p->nChar<=p->mxAlloc ){ 7677b4d780bSdrh /* Force exponential buffer size growth as long as it does not overflow, 7687b4d780bSdrh ** to avoid having to call this routine too often */ 7697b4d780bSdrh szNew += p->nChar; 7707b4d780bSdrh } 771b1a6c3c1Sdrh if( szNew > p->mxAlloc ){ 772ade86483Sdrh sqlite3StrAccumReset(p); 773a6353a3fSdrh setStrAccumError(p, STRACCUM_TOOBIG); 774a70a073bSdrh return 0; 775b1a6c3c1Sdrh }else{ 776ea678832Sdrh p->nAlloc = (int)szNew; 777a18c5681Sdrh } 778b975598eSdrh if( p->useMalloc==1 ){ 779a9ef7097Sdan zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); 780b975598eSdrh }else{ 781*f3cdcdccSdrh zNew = sqlite3_realloc64(zOld, p->nAlloc); 782b975598eSdrh } 78353f733c7Sdrh if( zNew ){ 7847ef4d1c4Sdrh assert( p->zText!=0 || p->nChar==0 ); 785b07028f7Sdrh if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); 786ade86483Sdrh p->zText = zNew; 7877f5a7ecdSdrh p->nAlloc = sqlite3DbMallocSize(p->db, zNew); 788ade86483Sdrh }else{ 789ade86483Sdrh sqlite3StrAccumReset(p); 790a6353a3fSdrh setStrAccumError(p, STRACCUM_NOMEM); 791a70a073bSdrh return 0; 792a70a073bSdrh } 793a70a073bSdrh } 794a70a073bSdrh return N; 795a70a073bSdrh } 796a70a073bSdrh 797a70a073bSdrh /* 798af8f513fSdrh ** Append N copies of character c to the given string buffer. 799a70a073bSdrh */ 800af8f513fSdrh void sqlite3AppendChar(StrAccum *p, int N, char c){ 801a30d22a7Sdrh testcase( p->nChar + (i64)N > 0x7fffffff ); 802a30d22a7Sdrh if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ 803a30d22a7Sdrh return; 804a30d22a7Sdrh } 805af8f513fSdrh while( (N--)>0 ) p->zText[p->nChar++] = c; 806a70a073bSdrh } 807a70a073bSdrh 808a70a073bSdrh /* 809a70a073bSdrh ** The StrAccum "p" is not large enough to accept N new bytes of z[]. 810a70a073bSdrh ** So enlarge if first, then do the append. 811a70a073bSdrh ** 812a70a073bSdrh ** This is a helper routine to sqlite3StrAccumAppend() that does special-case 813a70a073bSdrh ** work (enlarging the buffer) using tail recursion, so that the 814a70a073bSdrh ** sqlite3StrAccumAppend() routine can use fast calling semantics. 815a70a073bSdrh */ 816172087fbSdrh static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){ 817a70a073bSdrh N = sqlite3StrAccumEnlarge(p, N); 818a70a073bSdrh if( N>0 ){ 819a70a073bSdrh memcpy(&p->zText[p->nChar], z, N); 820a70a073bSdrh p->nChar += N; 821a70a073bSdrh } 822a70a073bSdrh } 823a70a073bSdrh 824a70a073bSdrh /* 825a70a073bSdrh ** Append N bytes of text from z to the StrAccum object. Increase the 826a70a073bSdrh ** size of the memory allocation for StrAccum if necessary. 827a70a073bSdrh */ 828a70a073bSdrh void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){ 8293457338cSdrh assert( z!=0 || N==0 ); 830a70a073bSdrh assert( p->zText!=0 || p->nChar==0 || p->accError ); 831a70a073bSdrh assert( N>=0 ); 832a70a073bSdrh assert( p->accError==0 || p->nAlloc==0 ); 833a70a073bSdrh if( p->nChar+N >= p->nAlloc ){ 834a70a073bSdrh enlargeAndAppend(p,z,N); 835172087fbSdrh }else{ 836b07028f7Sdrh assert( p->zText ); 837ade86483Sdrh p->nChar += N; 838172087fbSdrh memcpy(&p->zText[p->nChar-N], z, N); 839172087fbSdrh } 840483750baSdrh } 841483750baSdrh 842483750baSdrh /* 843a6353a3fSdrh ** Append the complete text of zero-terminated string z[] to the p string. 844a6353a3fSdrh */ 845a6353a3fSdrh void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){ 84653cd9646Smistachkin sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z)); 847a6353a3fSdrh } 848a6353a3fSdrh 849a6353a3fSdrh 850a6353a3fSdrh /* 851ade86483Sdrh ** Finish off a string by making sure it is zero-terminated. 852ade86483Sdrh ** Return a pointer to the resulting string. Return a NULL 853ade86483Sdrh ** pointer if any kind of error was encountered. 8545f968436Sdrh */ 855ade86483Sdrh char *sqlite3StrAccumFinish(StrAccum *p){ 856ade86483Sdrh if( p->zText ){ 857ade86483Sdrh p->zText[p->nChar] = 0; 858ade86483Sdrh if( p->useMalloc && p->zText==p->zBase ){ 859b975598eSdrh if( p->useMalloc==1 ){ 860633e6d57Sdrh p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); 861b975598eSdrh }else{ 862*f3cdcdccSdrh p->zText = sqlite3_malloc64(p->nChar+1); 863b975598eSdrh } 864ade86483Sdrh if( p->zText ){ 865ade86483Sdrh memcpy(p->zText, p->zBase, p->nChar+1); 866ade86483Sdrh }else{ 867a6353a3fSdrh setStrAccumError(p, STRACCUM_NOMEM); 868ade86483Sdrh } 869ade86483Sdrh } 870ade86483Sdrh } 871ade86483Sdrh return p->zText; 872ade86483Sdrh } 873ade86483Sdrh 874ade86483Sdrh /* 875ade86483Sdrh ** Reset an StrAccum string. Reclaim all malloced memory. 876ade86483Sdrh */ 877ade86483Sdrh void sqlite3StrAccumReset(StrAccum *p){ 878ade86483Sdrh if( p->zText!=p->zBase ){ 879b975598eSdrh if( p->useMalloc==1 ){ 880633e6d57Sdrh sqlite3DbFree(p->db, p->zText); 881b975598eSdrh }else{ 882b975598eSdrh sqlite3_free(p->zText); 883b975598eSdrh } 884ade86483Sdrh } 885f089aa45Sdrh p->zText = 0; 886ade86483Sdrh } 887ade86483Sdrh 888ade86483Sdrh /* 889ade86483Sdrh ** Initialize a string accumulator 890ade86483Sdrh */ 891f089aa45Sdrh void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){ 892ade86483Sdrh p->zText = p->zBase = zBase; 893633e6d57Sdrh p->db = 0; 894ade86483Sdrh p->nChar = 0; 895ade86483Sdrh p->nAlloc = n; 896bb4957f8Sdrh p->mxAlloc = mx; 897ade86483Sdrh p->useMalloc = 1; 898b49bc86aSdrh p->accError = 0; 8995f968436Sdrh } 9005f968436Sdrh 9015f968436Sdrh /* 9025f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 9035f968436Sdrh ** %-conversion extensions. 9045f968436Sdrh */ 90517435752Sdrh char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ 90617435752Sdrh char *z; 90779158e18Sdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 908ade86483Sdrh StrAccum acc; 909bc6160b0Sdrh assert( db!=0 ); 910bb4957f8Sdrh sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), 911bc6160b0Sdrh db->aLimit[SQLITE_LIMIT_LENGTH]); 912633e6d57Sdrh acc.db = db; 913a5c1416dSdrh sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap); 914ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 915b49bc86aSdrh if( acc.accError==STRACCUM_NOMEM ){ 91617435752Sdrh db->mallocFailed = 1; 91717435752Sdrh } 91817435752Sdrh return z; 9195f968436Sdrh } 9205f968436Sdrh 9215f968436Sdrh /* 9225f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 9235f968436Sdrh ** %-conversion extensions. 9245f968436Sdrh */ 92517435752Sdrh char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ 9265f968436Sdrh va_list ap; 9275f968436Sdrh char *z; 9285f968436Sdrh va_start(ap, zFormat); 929ade86483Sdrh z = sqlite3VMPrintf(db, zFormat, ap); 9305f968436Sdrh va_end(ap); 9315f968436Sdrh return z; 9325f968436Sdrh } 9335f968436Sdrh 9345f968436Sdrh /* 935633e6d57Sdrh ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting 93660ec914cSpeter.d.reid ** the string and before returning. This routine is intended to be used 937633e6d57Sdrh ** to modify an existing string. For example: 938633e6d57Sdrh ** 939633e6d57Sdrh ** x = sqlite3MPrintf(db, x, "prefix %s suffix", x); 940633e6d57Sdrh ** 941633e6d57Sdrh */ 942633e6d57Sdrh char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){ 943633e6d57Sdrh va_list ap; 944633e6d57Sdrh char *z; 945633e6d57Sdrh va_start(ap, zFormat); 946633e6d57Sdrh z = sqlite3VMPrintf(db, zFormat, ap); 947633e6d57Sdrh va_end(ap); 948633e6d57Sdrh sqlite3DbFree(db, zStr); 949633e6d57Sdrh return z; 950633e6d57Sdrh } 951633e6d57Sdrh 952633e6d57Sdrh /* 95328dd479cSdrh ** Print into memory obtained from sqlite3_malloc(). Omit the internal 95428dd479cSdrh ** %-conversion extensions. 95528dd479cSdrh */ 95628dd479cSdrh char *sqlite3_vmprintf(const char *zFormat, va_list ap){ 957ade86483Sdrh char *z; 95828dd479cSdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 959ade86483Sdrh StrAccum acc; 9609ca95730Sdrh 9619ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 9629ca95730Sdrh if( zFormat==0 ){ 9639ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 9649ca95730Sdrh return 0; 9659ca95730Sdrh } 9669ca95730Sdrh #endif 967ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT 968ff1590eeSdrh if( sqlite3_initialize() ) return 0; 969ff1590eeSdrh #endif 970bb4957f8Sdrh sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); 971b975598eSdrh acc.useMalloc = 2; 972f089aa45Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 973ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 974ade86483Sdrh return z; 97528dd479cSdrh } 97628dd479cSdrh 97728dd479cSdrh /* 97828dd479cSdrh ** Print into memory obtained from sqlite3_malloc()(). Omit the internal 97928dd479cSdrh ** %-conversion extensions. 980483750baSdrh */ 9816f8a503dSdanielk1977 char *sqlite3_mprintf(const char *zFormat, ...){ 982a18c5681Sdrh va_list ap; 9835f968436Sdrh char *z; 984ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT 985ff1590eeSdrh if( sqlite3_initialize() ) return 0; 986ff1590eeSdrh #endif 987a18c5681Sdrh va_start(ap, zFormat); 988b3738b6cSdrh z = sqlite3_vmprintf(zFormat, ap); 989a18c5681Sdrh va_end(ap); 9905f968436Sdrh return z; 991a18c5681Sdrh } 992a18c5681Sdrh 993a18c5681Sdrh /* 9946f8a503dSdanielk1977 ** sqlite3_snprintf() works like snprintf() except that it ignores the 99593a5c6bdSdrh ** current locale settings. This is important for SQLite because we 99693a5c6bdSdrh ** are not able to use a "," as the decimal point in place of "." as 99793a5c6bdSdrh ** specified by some locales. 998db26d4c9Sdrh ** 999db26d4c9Sdrh ** Oops: The first two arguments of sqlite3_snprintf() are backwards 1000db26d4c9Sdrh ** from the snprintf() standard. Unfortunately, it is too late to change 1001db26d4c9Sdrh ** this without breaking compatibility, so we just have to live with the 1002db26d4c9Sdrh ** mistake. 1003db26d4c9Sdrh ** 1004db26d4c9Sdrh ** sqlite3_vsnprintf() is the varargs version. 100593a5c6bdSdrh */ 1006db26d4c9Sdrh char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ 1007db26d4c9Sdrh StrAccum acc; 1008db26d4c9Sdrh if( n<=0 ) return zBuf; 10099ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 10109ca95730Sdrh if( zBuf==0 || zFormat==0 ) { 10119ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 101296c707a3Sdrh if( zBuf ) zBuf[0] = 0; 10139ca95730Sdrh return zBuf; 10149ca95730Sdrh } 10159ca95730Sdrh #endif 1016db26d4c9Sdrh sqlite3StrAccumInit(&acc, zBuf, n, 0); 1017db26d4c9Sdrh acc.useMalloc = 0; 1018db26d4c9Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 1019db26d4c9Sdrh return sqlite3StrAccumFinish(&acc); 1020db26d4c9Sdrh } 10216f8a503dSdanielk1977 char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ 10225f968436Sdrh char *z; 102393a5c6bdSdrh va_list ap; 102493a5c6bdSdrh va_start(ap,zFormat); 1025db26d4c9Sdrh z = sqlite3_vsnprintf(n, zBuf, zFormat, ap); 102693a5c6bdSdrh va_end(ap); 10275f968436Sdrh return z; 102893a5c6bdSdrh } 102993a5c6bdSdrh 10303f280701Sdrh /* 10317c0c460fSdrh ** This is the routine that actually formats the sqlite3_log() message. 10327c0c460fSdrh ** We house it in a separate routine from sqlite3_log() to avoid using 10337c0c460fSdrh ** stack space on small-stack systems when logging is disabled. 10347c0c460fSdrh ** 10357c0c460fSdrh ** sqlite3_log() must render into a static buffer. It cannot dynamically 10367c0c460fSdrh ** allocate memory because it might be called while the memory allocator 10377c0c460fSdrh ** mutex is held. 10387c0c460fSdrh */ 10397c0c460fSdrh static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){ 10407c0c460fSdrh StrAccum acc; /* String accumulator */ 1041a64fa912Sdrh char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */ 10427c0c460fSdrh 10437c0c460fSdrh sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0); 10447c0c460fSdrh acc.useMalloc = 0; 10457c0c460fSdrh sqlite3VXPrintf(&acc, 0, 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]; 1072bb4957f8Sdrh sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0); 1073ade86483Sdrh acc.useMalloc = 0; 1074e54ca3feSdrh va_start(ap,zFormat); 1075f089aa45Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 1076e54ca3feSdrh va_end(ap); 1077bed8e7e5Sdrh sqlite3StrAccumFinish(&acc); 1078485f0039Sdrh fprintf(stdout,"%s", zBuf); 10792ac3ee97Sdrh fflush(stdout); 1080e54ca3feSdrh } 1081e54ca3feSdrh #endif 1082c7bc4fdeSdrh 10834fa4a54fSdrh #ifdef SQLITE_DEBUG 10844fa4a54fSdrh /************************************************************************* 10854fa4a54fSdrh ** Routines for implementing the "TreeView" display of hierarchical 10864fa4a54fSdrh ** data structures for debugging. 10874fa4a54fSdrh ** 10884fa4a54fSdrh ** The main entry points (coded elsewhere) are: 10894fa4a54fSdrh ** sqlite3TreeViewExpr(0, pExpr, 0); 10904fa4a54fSdrh ** sqlite3TreeViewExprList(0, pList, 0, 0); 10914fa4a54fSdrh ** sqlite3TreeViewSelect(0, pSelect, 0); 10924fa4a54fSdrh ** Insert calls to those routines while debugging in order to display 10934fa4a54fSdrh ** a diagram of Expr, ExprList, and Select objects. 10944fa4a54fSdrh ** 10954fa4a54fSdrh */ 10964fa4a54fSdrh /* Add a new subitem to the tree. The moreToFollow flag indicates that this 10974fa4a54fSdrh ** is not the last item in the tree. */ 10984fa4a54fSdrh TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){ 10994fa4a54fSdrh if( p==0 ){ 1100*f3cdcdccSdrh p = sqlite3_malloc64( sizeof(*p) ); 11014fa4a54fSdrh if( p==0 ) return 0; 11024fa4a54fSdrh memset(p, 0, sizeof(*p)); 11034fa4a54fSdrh }else{ 11044fa4a54fSdrh p->iLevel++; 11054fa4a54fSdrh } 11064fa4a54fSdrh assert( moreToFollow==0 || moreToFollow==1 ); 1107b08cd3f3Sdrh if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow; 11084fa4a54fSdrh return p; 11094fa4a54fSdrh } 11104fa4a54fSdrh /* Finished with one layer of the tree */ 11114fa4a54fSdrh void sqlite3TreeViewPop(TreeView *p){ 11124fa4a54fSdrh if( p==0 ) return; 11134fa4a54fSdrh p->iLevel--; 11144fa4a54fSdrh if( p->iLevel<0 ) sqlite3_free(p); 11154fa4a54fSdrh } 11164fa4a54fSdrh /* Generate a single line of output for the tree, with a prefix that contains 11174fa4a54fSdrh ** all the appropriate tree lines */ 11184fa4a54fSdrh void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){ 11194fa4a54fSdrh va_list ap; 11204fa4a54fSdrh int i; 11214fa4a54fSdrh StrAccum acc; 11224fa4a54fSdrh char zBuf[500]; 11234fa4a54fSdrh sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0); 11244fa4a54fSdrh acc.useMalloc = 0; 11254fa4a54fSdrh if( p ){ 1126b08cd3f3Sdrh for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){ 1127b08cd3f3Sdrh sqlite3StrAccumAppend(&acc, p->bLine[i] ? "| " : " ", 4); 11284fa4a54fSdrh } 1129b08cd3f3Sdrh sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4); 11304fa4a54fSdrh } 11314fa4a54fSdrh va_start(ap, zFormat); 11324fa4a54fSdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 11334fa4a54fSdrh va_end(ap); 11344fa4a54fSdrh if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1); 11354fa4a54fSdrh sqlite3StrAccumFinish(&acc); 11364fa4a54fSdrh fprintf(stdout,"%s", zBuf); 11374fa4a54fSdrh fflush(stdout); 11384fa4a54fSdrh } 11394fa4a54fSdrh /* Shorthand for starting a new tree item that consists of a single label */ 11404fa4a54fSdrh void sqlite3TreeViewItem(TreeView *p, const char *zLabel, u8 moreToFollow){ 11414fa4a54fSdrh p = sqlite3TreeViewPush(p, moreToFollow); 11424fa4a54fSdrh sqlite3TreeViewLine(p, "%s", zLabel); 11434fa4a54fSdrh } 11444fa4a54fSdrh #endif /* SQLITE_DEBUG */ 11454fa4a54fSdrh 1146c7bc4fdeSdrh /* 1147c7bc4fdeSdrh ** variable-argument wrapper around sqlite3VXPrintf(). 1148c7bc4fdeSdrh */ 1149a5c1416dSdrh void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){ 1150c7bc4fdeSdrh va_list ap; 1151c7bc4fdeSdrh va_start(ap,zFormat); 1152a5c1416dSdrh sqlite3VXPrintf(p, bFlags, zFormat, ap); 1153c7bc4fdeSdrh va_end(ap); 1154c7bc4fdeSdrh } 1155