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 */ 174a5c1416dSdrh u32 bFlags, /* SQLITE_PRINTF_* flags */ 1755f968436Sdrh const char *fmt, /* Format string */ 1765f968436Sdrh va_list ap /* arguments */ 177a18c5681Sdrh ){ 178e84a306bSdrh int c; /* Next character in the format string */ 179e84a306bSdrh char *bufpt; /* Pointer to the conversion buffer */ 180e84a306bSdrh int precision; /* Precision of the current field */ 181e84a306bSdrh int length; /* Length of the field */ 182e84a306bSdrh int idx; /* A general purpose loop counter */ 183a18c5681Sdrh int width; /* Width of the current field */ 184e84a306bSdrh etByte flag_leftjustify; /* True if "-" flag is present */ 185e84a306bSdrh etByte flag_plussign; /* True if "+" flag is present */ 186e84a306bSdrh etByte flag_blanksign; /* True if " " flag is present */ 187e84a306bSdrh etByte flag_alternateform; /* True if "#" flag is present */ 188531fe878Sdrh etByte flag_altform2; /* True if "!" flag is present */ 189e84a306bSdrh etByte flag_zeropad; /* True if field width constant starts with zero */ 190e84a306bSdrh etByte flag_long; /* True if "l" flag is present */ 191a34b6764Sdrh etByte flag_longlong; /* True if the "ll" flag is present */ 1923e9aeec0Sdrh etByte done; /* Loop termination flag */ 193ed1fddf4Sdrh etByte xtype = 0; /* Conversion paradigm */ 194a5c1416dSdrh u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */ 195a5c1416dSdrh u8 useIntern; /* Ok to use internal conversions (ex: %T) */ 196ed1fddf4Sdrh char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ 19727436af7Sdrh sqlite_uint64 longvalue; /* Value for integer types */ 198384eef32Sdrh LONGDOUBLE_TYPE realvalue; /* Value for real types */ 1995719628aSdrh const et_info *infop; /* Pointer to the appropriate info structure */ 20059eedf79Sdrh char *zOut; /* Rendering buffer */ 20159eedf79Sdrh int nOut; /* Size of the rendering buffer */ 202af8f513fSdrh char *zExtra = 0; /* Malloced memory used by some conversion */ 203b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 204557cc60fSdrh int exp, e2; /* exponent of real numbers */ 205ed1fddf4Sdrh int nsd; /* Number of significant digits returned */ 206a18c5681Sdrh double rounder; /* Used for rounding floating point values */ 207e84a306bSdrh etByte flag_dp; /* True if decimal point should be shown */ 208e84a306bSdrh etByte flag_rtz; /* True if trailing zeros should be removed */ 209a18c5681Sdrh #endif 210a5c1416dSdrh PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */ 211ed1fddf4Sdrh char buf[etBUFSIZE]; /* Conversion buffer */ 212a18c5681Sdrh 213a18c5681Sdrh bufpt = 0; 214a5c1416dSdrh if( bFlags ){ 215a5c1416dSdrh if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){ 216a5c1416dSdrh pArgList = va_arg(ap, PrintfArguments*); 217a5c1416dSdrh } 218a5c1416dSdrh useIntern = bFlags & SQLITE_PRINTF_INTERNAL; 219a5c1416dSdrh }else{ 220a5c1416dSdrh bArgList = useIntern = 0; 221a5c1416dSdrh } 222a18c5681Sdrh for(; (c=(*fmt))!=0; ++fmt){ 223a18c5681Sdrh if( c!='%' ){ 224a18c5681Sdrh bufpt = (char *)fmt; 225760b1598Sdrh #if HAVE_STRCHRNUL 226760b1598Sdrh fmt = strchrnul(fmt, '%'); 227760b1598Sdrh #else 228760b1598Sdrh do{ fmt++; }while( *fmt && *fmt != '%' ); 229760b1598Sdrh #endif 230a70a073bSdrh sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt)); 231760b1598Sdrh if( *fmt==0 ) break; 232a18c5681Sdrh } 233a18c5681Sdrh if( (c=(*++fmt))==0 ){ 234ade86483Sdrh sqlite3StrAccumAppend(pAccum, "%", 1); 235a18c5681Sdrh break; 236a18c5681Sdrh } 237a18c5681Sdrh /* Find out what flags are present */ 238a18c5681Sdrh flag_leftjustify = flag_plussign = flag_blanksign = 239557cc60fSdrh flag_alternateform = flag_altform2 = flag_zeropad = 0; 2403e9aeec0Sdrh done = 0; 241a18c5681Sdrh do{ 242a18c5681Sdrh switch( c ){ 2433e9aeec0Sdrh case '-': flag_leftjustify = 1; break; 2443e9aeec0Sdrh case '+': flag_plussign = 1; break; 2453e9aeec0Sdrh case ' ': flag_blanksign = 1; break; 2463e9aeec0Sdrh case '#': flag_alternateform = 1; break; 2473e9aeec0Sdrh case '!': flag_altform2 = 1; break; 2483e9aeec0Sdrh case '0': flag_zeropad = 1; break; 2493e9aeec0Sdrh default: done = 1; break; 250a18c5681Sdrh } 2513e9aeec0Sdrh }while( !done && (c=(*++fmt))!=0 ); 252a18c5681Sdrh /* Get the field width */ 253a18c5681Sdrh if( c=='*' ){ 254a5c1416dSdrh if( bArgList ){ 255a5c1416dSdrh width = (int)getIntArg(pArgList); 256a5c1416dSdrh }else{ 257a18c5681Sdrh width = va_arg(ap,int); 258a5c1416dSdrh } 259a18c5681Sdrh if( width<0 ){ 260a18c5681Sdrh flag_leftjustify = 1; 261b6f47debSdrh width = width >= -2147483647 ? -width : 0; 262a18c5681Sdrh } 263a18c5681Sdrh c = *++fmt; 264a18c5681Sdrh }else{ 265b6f47debSdrh unsigned wx = 0; 26617a68934Sdrh while( c>='0' && c<='9' ){ 267b6f47debSdrh wx = wx*10 + c - '0'; 268a18c5681Sdrh c = *++fmt; 269a18c5681Sdrh } 270b6f47debSdrh testcase( wx>0x7fffffff ); 271b6f47debSdrh width = wx & 0x7fffffff; 272a18c5681Sdrh } 273*c386ef4fSdrh assert( width>=0 ); 274*c386ef4fSdrh #ifdef SQLITE_PRINTF_PRECISION_LIMIT 275*c386ef4fSdrh if( width>SQLITE_PRINTF_PRECISION_LIMIT ){ 276*c386ef4fSdrh width = SQLITE_PRINTF_PRECISION_LIMIT; 277*c386ef4fSdrh } 278*c386ef4fSdrh #endif 2798c069147Sdan 280a18c5681Sdrh /* Get the precision */ 281a18c5681Sdrh if( c=='.' ){ 282a18c5681Sdrh c = *++fmt; 283a18c5681Sdrh if( c=='*' ){ 284a5c1416dSdrh if( bArgList ){ 285a5c1416dSdrh precision = (int)getIntArg(pArgList); 286a5c1416dSdrh }else{ 287a18c5681Sdrh precision = va_arg(ap,int); 288a5c1416dSdrh } 289a18c5681Sdrh c = *++fmt; 290b6f47debSdrh if( precision<0 ){ 291b6f47debSdrh precision = precision >= -2147483647 ? -precision : -1; 292b6f47debSdrh } 293a18c5681Sdrh }else{ 294b6f47debSdrh unsigned px = 0; 29517a68934Sdrh while( c>='0' && c<='9' ){ 296b6f47debSdrh px = px*10 + c - '0'; 297a18c5681Sdrh c = *++fmt; 298a18c5681Sdrh } 299b6f47debSdrh testcase( px>0x7fffffff ); 300b6f47debSdrh precision = px & 0x7fffffff; 301a18c5681Sdrh } 302a18c5681Sdrh }else{ 303a18c5681Sdrh precision = -1; 304a18c5681Sdrh } 305*c386ef4fSdrh assert( precision>=(-1) ); 306*c386ef4fSdrh #ifdef SQLITE_PRINTF_PRECISION_LIMIT 307*c386ef4fSdrh if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){ 308*c386ef4fSdrh precision = SQLITE_PRINTF_PRECISION_LIMIT; 309*c386ef4fSdrh } 310*c386ef4fSdrh #endif 311*c386ef4fSdrh 312*c386ef4fSdrh 313a18c5681Sdrh /* Get the conversion type modifier */ 314a18c5681Sdrh if( c=='l' ){ 315a18c5681Sdrh flag_long = 1; 316a18c5681Sdrh c = *++fmt; 317a34b6764Sdrh if( c=='l' ){ 318a34b6764Sdrh flag_longlong = 1; 319a34b6764Sdrh c = *++fmt; 320a18c5681Sdrh }else{ 321a34b6764Sdrh flag_longlong = 0; 322a34b6764Sdrh } 323a34b6764Sdrh }else{ 324a34b6764Sdrh flag_long = flag_longlong = 0; 325a18c5681Sdrh } 326a18c5681Sdrh /* Fetch the info entry for the field */ 327874ba04cSdrh infop = &fmtinfo[0]; 328874ba04cSdrh xtype = etINVALID; 32900e13613Sdanielk1977 for(idx=0; idx<ArraySize(fmtinfo); idx++){ 330a18c5681Sdrh if( c==fmtinfo[idx].fmttype ){ 331a18c5681Sdrh infop = &fmtinfo[idx]; 332a5c1416dSdrh if( useIntern || (infop->flags & FLAG_INTERN)==0 ){ 333e84a306bSdrh xtype = infop->type; 334c4413565Sdrh }else{ 335ade86483Sdrh return; 3365f968436Sdrh } 337a18c5681Sdrh break; 338a18c5681Sdrh } 339a18c5681Sdrh } 34043617e9aSdrh 341a18c5681Sdrh /* 342a18c5681Sdrh ** At this point, variables are initialized as follows: 343a18c5681Sdrh ** 344a18c5681Sdrh ** flag_alternateform TRUE if a '#' is present. 3453e9aeec0Sdrh ** flag_altform2 TRUE if a '!' is present. 346a18c5681Sdrh ** flag_plussign TRUE if a '+' is present. 347a18c5681Sdrh ** flag_leftjustify TRUE if a '-' is present or if the 348a18c5681Sdrh ** field width was negative. 349a18c5681Sdrh ** flag_zeropad TRUE if the width began with 0. 350a18c5681Sdrh ** flag_long TRUE if the letter 'l' (ell) prefixed 351a18c5681Sdrh ** the conversion character. 352a34b6764Sdrh ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed 353a34b6764Sdrh ** the conversion character. 354a18c5681Sdrh ** flag_blanksign TRUE if a ' ' is present. 355a18c5681Sdrh ** width The specified field width. This is 356a18c5681Sdrh ** always non-negative. Zero is the default. 357a18c5681Sdrh ** precision The specified precision. The default 358a18c5681Sdrh ** is -1. 359a18c5681Sdrh ** xtype The class of the conversion. 360a18c5681Sdrh ** infop Pointer to the appropriate info struct. 361a18c5681Sdrh */ 362a18c5681Sdrh switch( xtype ){ 363fe63d1c9Sdrh case etPOINTER: 364fe63d1c9Sdrh flag_longlong = sizeof(char*)==sizeof(i64); 365fe63d1c9Sdrh flag_long = sizeof(char*)==sizeof(long int); 366fe63d1c9Sdrh /* Fall through into the next case */ 3679a99334dSdrh case etORDINAL: 368a18c5681Sdrh case etRADIX: 369e84a306bSdrh if( infop->flags & FLAG_SIGNED ){ 370e9707671Sdrh i64 v; 371a5c1416dSdrh if( bArgList ){ 372a5c1416dSdrh v = getIntArg(pArgList); 373a5c1416dSdrh }else if( flag_longlong ){ 374eeb23a4cSdrh v = va_arg(ap,i64); 375eeb23a4cSdrh }else if( flag_long ){ 376eeb23a4cSdrh v = va_arg(ap,long int); 377eeb23a4cSdrh }else{ 378eeb23a4cSdrh v = va_arg(ap,int); 379eeb23a4cSdrh } 380e9707671Sdrh if( v<0 ){ 381158b9cb9Sdrh if( v==SMALLEST_INT64 ){ 382158b9cb9Sdrh longvalue = ((u64)1)<<63; 383158b9cb9Sdrh }else{ 384158b9cb9Sdrh longvalue = -v; 385158b9cb9Sdrh } 386cfcdaefeSdanielk1977 prefix = '-'; 387cfcdaefeSdanielk1977 }else{ 388e9707671Sdrh longvalue = v; 389e9707671Sdrh if( flag_plussign ) prefix = '+'; 390a18c5681Sdrh else if( flag_blanksign ) prefix = ' '; 391a18c5681Sdrh else prefix = 0; 392cfcdaefeSdanielk1977 } 393e9707671Sdrh }else{ 394a5c1416dSdrh if( bArgList ){ 395a5c1416dSdrh longvalue = (u64)getIntArg(pArgList); 396a5c1416dSdrh }else if( flag_longlong ){ 397eeb23a4cSdrh longvalue = va_arg(ap,u64); 398eeb23a4cSdrh }else if( flag_long ){ 399eeb23a4cSdrh longvalue = va_arg(ap,unsigned long int); 400eeb23a4cSdrh }else{ 401eeb23a4cSdrh longvalue = va_arg(ap,unsigned int); 402eeb23a4cSdrh } 403e9707671Sdrh prefix = 0; 404e9707671Sdrh } 405e9707671Sdrh if( longvalue==0 ) flag_alternateform = 0; 406a18c5681Sdrh if( flag_zeropad && precision<width-(prefix!=0) ){ 407a18c5681Sdrh precision = width-(prefix!=0); 408a18c5681Sdrh } 40959eedf79Sdrh if( precision<etBUFSIZE-10 ){ 41059eedf79Sdrh nOut = etBUFSIZE; 41159eedf79Sdrh zOut = buf; 41259eedf79Sdrh }else{ 41359eedf79Sdrh nOut = precision + 10; 41459eedf79Sdrh zOut = zExtra = sqlite3Malloc( nOut ); 41559eedf79Sdrh if( zOut==0 ){ 416a6353a3fSdrh setStrAccumError(pAccum, STRACCUM_NOMEM); 41759eedf79Sdrh return; 41859eedf79Sdrh } 41959eedf79Sdrh } 42059eedf79Sdrh bufpt = &zOut[nOut-1]; 4219a99334dSdrh if( xtype==etORDINAL ){ 42243f6e064Sdrh static const char zOrd[] = "thstndrd"; 423ea678832Sdrh int x = (int)(longvalue % 10); 42443f6e064Sdrh if( x>=4 || (longvalue/10)%10==1 ){ 42543f6e064Sdrh x = 0; 42643f6e064Sdrh } 42759eedf79Sdrh *(--bufpt) = zOrd[x*2+1]; 42859eedf79Sdrh *(--bufpt) = zOrd[x*2]; 4299a99334dSdrh } 430a18c5681Sdrh { 4310e682099Sdrh const char *cset = &aDigits[infop->charset]; 4320e682099Sdrh u8 base = infop->base; 433a18c5681Sdrh do{ /* Convert to ascii */ 434a18c5681Sdrh *(--bufpt) = cset[longvalue%base]; 435a18c5681Sdrh longvalue = longvalue/base; 436a18c5681Sdrh }while( longvalue>0 ); 437a18c5681Sdrh } 43859eedf79Sdrh length = (int)(&zOut[nOut-1]-bufpt); 439a18c5681Sdrh for(idx=precision-length; idx>0; idx--){ 440a18c5681Sdrh *(--bufpt) = '0'; /* Zero pad */ 441a18c5681Sdrh } 442a18c5681Sdrh if( prefix ) *(--bufpt) = prefix; /* Add sign */ 443a18c5681Sdrh if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ 44476ff3a0eSdrh const char *pre; 44576ff3a0eSdrh char x; 44676ff3a0eSdrh pre = &aPrefix[infop->prefix]; 44776ff3a0eSdrh for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; 448a18c5681Sdrh } 44959eedf79Sdrh length = (int)(&zOut[nOut-1]-bufpt); 450a18c5681Sdrh break; 451a18c5681Sdrh case etFLOAT: 452a18c5681Sdrh case etEXP: 453a18c5681Sdrh case etGENERIC: 454a5c1416dSdrh if( bArgList ){ 455a5c1416dSdrh realvalue = getDoubleArg(pArgList); 456a5c1416dSdrh }else{ 457a18c5681Sdrh realvalue = va_arg(ap,double); 458a5c1416dSdrh } 45969ef7036Sdrh #ifdef SQLITE_OMIT_FLOATING_POINT 46069ef7036Sdrh length = 0; 46169ef7036Sdrh #else 462a18c5681Sdrh if( precision<0 ) precision = 6; /* Set default precision */ 463a18c5681Sdrh if( realvalue<0.0 ){ 464a18c5681Sdrh realvalue = -realvalue; 465a18c5681Sdrh prefix = '-'; 466a18c5681Sdrh }else{ 467a18c5681Sdrh if( flag_plussign ) prefix = '+'; 468a18c5681Sdrh else if( flag_blanksign ) prefix = ' '; 469a18c5681Sdrh else prefix = 0; 470a18c5681Sdrh } 4713e9aeec0Sdrh if( xtype==etGENERIC && precision>0 ) precision--; 472a30d22a7Sdrh testcase( precision>0xfff ); 47374b42275Sdrh for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){} 4743e9aeec0Sdrh if( xtype==etFLOAT ) realvalue += rounder; 475a18c5681Sdrh /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ 476a18c5681Sdrh exp = 0; 477ea678832Sdrh if( sqlite3IsNaN((double)realvalue) ){ 47853c14021Sdrh bufpt = "NaN"; 47953c14021Sdrh length = 3; 48053c14021Sdrh break; 48153c14021Sdrh } 482a18c5681Sdrh if( realvalue>0.0 ){ 48372b3fbc7Sdrh LONGDOUBLE_TYPE scale = 1.0; 4844ef94130Sdrh while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;} 4852a8f6712Sdrh while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; } 48672b3fbc7Sdrh while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; } 48772b3fbc7Sdrh realvalue /= scale; 488af005fbcSdrh while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } 489af005fbcSdrh while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } 490af005fbcSdrh if( exp>350 ){ 4912a8f6712Sdrh bufpt = buf; 4922a8f6712Sdrh buf[0] = prefix; 4932a8f6712Sdrh memcpy(buf+(prefix!=0),"Inf",4); 4942a8f6712Sdrh length = 3+(prefix!=0); 495a18c5681Sdrh break; 496a18c5681Sdrh } 497a18c5681Sdrh } 498a18c5681Sdrh bufpt = buf; 499a18c5681Sdrh /* 500a18c5681Sdrh ** If the field type is etGENERIC, then convert to either etEXP 501a18c5681Sdrh ** or etFLOAT, as appropriate. 502a18c5681Sdrh */ 503a18c5681Sdrh if( xtype!=etFLOAT ){ 504a18c5681Sdrh realvalue += rounder; 505a18c5681Sdrh if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } 506a18c5681Sdrh } 507a18c5681Sdrh if( xtype==etGENERIC ){ 508a18c5681Sdrh flag_rtz = !flag_alternateform; 509a18c5681Sdrh if( exp<-4 || exp>precision ){ 510a18c5681Sdrh xtype = etEXP; 511a18c5681Sdrh }else{ 512a18c5681Sdrh precision = precision - exp; 513a18c5681Sdrh xtype = etFLOAT; 514a18c5681Sdrh } 515a18c5681Sdrh }else{ 51672b3fbc7Sdrh flag_rtz = flag_altform2; 517a18c5681Sdrh } 518557cc60fSdrh if( xtype==etEXP ){ 519557cc60fSdrh e2 = 0; 520557cc60fSdrh }else{ 521557cc60fSdrh e2 = exp; 522557cc60fSdrh } 52374b42275Sdrh if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){ 52474b42275Sdrh bufpt = zExtra 52574b42275Sdrh = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 ); 52659eedf79Sdrh if( bufpt==0 ){ 527a6353a3fSdrh setStrAccumError(pAccum, STRACCUM_NOMEM); 52859eedf79Sdrh return; 52959eedf79Sdrh } 53059eedf79Sdrh } 53159eedf79Sdrh zOut = bufpt; 53272b3fbc7Sdrh nsd = 16 + flag_altform2*10; 533ea678832Sdrh flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; 534557cc60fSdrh /* The sign in front of the number */ 535557cc60fSdrh if( prefix ){ 536557cc60fSdrh *(bufpt++) = prefix; 537557cc60fSdrh } 538557cc60fSdrh /* Digits prior to the decimal point */ 539557cc60fSdrh if( e2<0 ){ 540557cc60fSdrh *(bufpt++) = '0'; 541557cc60fSdrh }else{ 542557cc60fSdrh for(; e2>=0; e2--){ 543557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 544557cc60fSdrh } 545557cc60fSdrh } 546557cc60fSdrh /* The decimal point */ 547557cc60fSdrh if( flag_dp ){ 548557cc60fSdrh *(bufpt++) = '.'; 549557cc60fSdrh } 550557cc60fSdrh /* "0" digits after the decimal point but before the first 551557cc60fSdrh ** significant digit of the number */ 552af005fbcSdrh for(e2++; e2<0; precision--, e2++){ 553af005fbcSdrh assert( precision>0 ); 554a18c5681Sdrh *(bufpt++) = '0'; 555a18c5681Sdrh } 556557cc60fSdrh /* Significant digits after the decimal point */ 557557cc60fSdrh while( (precision--)>0 ){ 558557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 559a18c5681Sdrh } 560557cc60fSdrh /* Remove trailing zeros and the "." if no digits follow the "." */ 561557cc60fSdrh if( flag_rtz && flag_dp ){ 5623e9aeec0Sdrh while( bufpt[-1]=='0' ) *(--bufpt) = 0; 56359eedf79Sdrh assert( bufpt>zOut ); 5643e9aeec0Sdrh if( bufpt[-1]=='.' ){ 565557cc60fSdrh if( flag_altform2 ){ 566557cc60fSdrh *(bufpt++) = '0'; 567557cc60fSdrh }else{ 568557cc60fSdrh *(--bufpt) = 0; 569a18c5681Sdrh } 570557cc60fSdrh } 571557cc60fSdrh } 572557cc60fSdrh /* Add the "eNNN" suffix */ 57359eedf79Sdrh if( xtype==etEXP ){ 57476ff3a0eSdrh *(bufpt++) = aDigits[infop->charset]; 575557cc60fSdrh if( exp<0 ){ 576557cc60fSdrh *(bufpt++) = '-'; exp = -exp; 577557cc60fSdrh }else{ 578557cc60fSdrh *(bufpt++) = '+'; 579557cc60fSdrh } 580a18c5681Sdrh if( exp>=100 ){ 581ea678832Sdrh *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */ 582a18c5681Sdrh exp %= 100; 583a18c5681Sdrh } 584ea678832Sdrh *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */ 585ea678832Sdrh *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */ 586a18c5681Sdrh } 587557cc60fSdrh *bufpt = 0; 588557cc60fSdrh 589a18c5681Sdrh /* The converted number is in buf[] and zero terminated. Output it. 590a18c5681Sdrh ** Note that the number is in the usual order, not reversed as with 591a18c5681Sdrh ** integer conversions. */ 59259eedf79Sdrh length = (int)(bufpt-zOut); 59359eedf79Sdrh bufpt = zOut; 594a18c5681Sdrh 595a18c5681Sdrh /* Special case: Add leading zeros if the flag_zeropad flag is 596a18c5681Sdrh ** set and we are not left justified */ 597a18c5681Sdrh if( flag_zeropad && !flag_leftjustify && length < width){ 598a18c5681Sdrh int i; 599a18c5681Sdrh int nPad = width - length; 600a18c5681Sdrh for(i=width; i>=nPad; i--){ 601a18c5681Sdrh bufpt[i] = bufpt[i-nPad]; 602a18c5681Sdrh } 603a18c5681Sdrh i = prefix!=0; 604a18c5681Sdrh while( nPad-- ) bufpt[i++] = '0'; 605a18c5681Sdrh length = width; 606a18c5681Sdrh } 60769ef7036Sdrh #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */ 608a18c5681Sdrh break; 609a18c5681Sdrh case etSIZE: 610fc6ee9dfSdrh if( !bArgList ){ 611fc6ee9dfSdrh *(va_arg(ap,int*)) = pAccum->nChar; 612fc6ee9dfSdrh } 613a18c5681Sdrh length = width = 0; 614a18c5681Sdrh break; 615a18c5681Sdrh case etPERCENT: 616a18c5681Sdrh buf[0] = '%'; 617a18c5681Sdrh bufpt = buf; 618a18c5681Sdrh length = 1; 619a18c5681Sdrh break; 620a18c5681Sdrh case etCHARX: 621a5c1416dSdrh if( bArgList ){ 622fc6ee9dfSdrh bufpt = getTextArg(pArgList); 623fc6ee9dfSdrh c = bufpt ? bufpt[0] : 0; 624a5c1416dSdrh }else{ 625ea678832Sdrh c = va_arg(ap,int); 626a5c1416dSdrh } 627af8f513fSdrh if( precision>1 ){ 628af8f513fSdrh width -= precision-1; 629af8f513fSdrh if( width>1 && !flag_leftjustify ){ 630af8f513fSdrh sqlite3AppendChar(pAccum, width-1, ' '); 631af8f513fSdrh width = 0; 632a18c5681Sdrh } 633af8f513fSdrh sqlite3AppendChar(pAccum, precision-1, c); 634af8f513fSdrh } 635af8f513fSdrh length = 1; 636af8f513fSdrh buf[0] = c; 637a18c5681Sdrh bufpt = buf; 638a18c5681Sdrh break; 639a18c5681Sdrh case etSTRING: 640d93d8a81Sdrh case etDYNSTRING: 641a5c1416dSdrh if( bArgList ){ 642a5c1416dSdrh bufpt = getTextArg(pArgList); 6432a8f6712Sdrh xtype = etSTRING; 644a5c1416dSdrh }else{ 645cb485882Sdrh bufpt = va_arg(ap,char*); 646a5c1416dSdrh } 647d93d8a81Sdrh if( bufpt==0 ){ 648d93d8a81Sdrh bufpt = ""; 6492a8f6712Sdrh }else if( xtype==etDYNSTRING ){ 650d93d8a81Sdrh zExtra = bufpt; 651d93d8a81Sdrh } 652e509094bSdrh if( precision>=0 ){ 653e509094bSdrh for(length=0; length<precision && bufpt[length]; length++){} 654e509094bSdrh }else{ 655ea678832Sdrh length = sqlite3Strlen30(bufpt); 656e509094bSdrh } 657a18c5681Sdrh break; 6582a8f6712Sdrh case etSQLESCAPE: /* Escape ' characters */ 6592a8f6712Sdrh case etSQLESCAPE2: /* Escape ' and enclose in '...' */ 6602a8f6712Sdrh case etSQLESCAPE3: { /* Escape " characters */ 6618965b50eSdrh int i, j, k, n, isnull; 6624794f735Sdrh int needQuote; 663ea678832Sdrh char ch; 664f3b863edSdanielk1977 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */ 665a5c1416dSdrh char *escarg; 666a5c1416dSdrh 667a5c1416dSdrh if( bArgList ){ 668a5c1416dSdrh escarg = getTextArg(pArgList); 669a5c1416dSdrh }else{ 670a5c1416dSdrh escarg = va_arg(ap,char*); 671a5c1416dSdrh } 672f0113000Sdanielk1977 isnull = escarg==0; 673f0113000Sdanielk1977 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); 6748965b50eSdrh k = precision; 67560d4a304Sdan for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ 676f3b863edSdanielk1977 if( ch==q ) n++; 677a18c5681Sdrh } 6784794f735Sdrh needQuote = !isnull && xtype==etSQLESCAPE2; 6792a8f6712Sdrh n += i + 3; 680a18c5681Sdrh if( n>etBUFSIZE ){ 681e5ae5735Sdrh bufpt = zExtra = sqlite3Malloc( n ); 682d164fd34Sdrh if( bufpt==0 ){ 683a6353a3fSdrh setStrAccumError(pAccum, STRACCUM_NOMEM); 684d164fd34Sdrh return; 685d164fd34Sdrh } 686a18c5681Sdrh }else{ 687a18c5681Sdrh bufpt = buf; 688a18c5681Sdrh } 6890cfcf3fbSchw j = 0; 690f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 6918965b50eSdrh k = i; 6928965b50eSdrh for(i=0; i<k; i++){ 6938965b50eSdrh bufpt[j++] = ch = escarg[i]; 694f3b863edSdanielk1977 if( ch==q ) bufpt[j++] = ch; 695a18c5681Sdrh } 696f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 697a18c5681Sdrh bufpt[j] = 0; 698a18c5681Sdrh length = j; 6998965b50eSdrh /* The precision in %q and %Q means how many input characters to 7008965b50eSdrh ** consume, not the length of the output... 7018965b50eSdrh ** if( precision>=0 && precision<length ) length = precision; */ 702a18c5681Sdrh break; 7035eba8c09Sdrh } 7045f968436Sdrh case etTOKEN: { 7055f968436Sdrh Token *pToken = va_arg(ap, Token*); 706a5c1416dSdrh assert( bArgList==0 ); 707a9ab481fSdrh if( pToken && pToken->n ){ 708ade86483Sdrh sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n); 709ad6d9460Sdrh } 7105f968436Sdrh length = width = 0; 7115f968436Sdrh break; 7125f968436Sdrh } 7135f968436Sdrh case etSRCLIST: { 7145f968436Sdrh SrcList *pSrc = va_arg(ap, SrcList*); 7155f968436Sdrh int k = va_arg(ap, int); 7165f968436Sdrh struct SrcList_item *pItem = &pSrc->a[k]; 717a5c1416dSdrh assert( bArgList==0 ); 7185f968436Sdrh assert( k>=0 && k<pSrc->nSrc ); 71993a960a0Sdrh if( pItem->zDatabase ){ 720a6353a3fSdrh sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase); 721ade86483Sdrh sqlite3StrAccumAppend(pAccum, ".", 1); 7225f968436Sdrh } 723a6353a3fSdrh sqlite3StrAccumAppendAll(pAccum, pItem->zName); 7245f968436Sdrh length = width = 0; 7255f968436Sdrh break; 7265f968436Sdrh } 727874ba04cSdrh default: { 728874ba04cSdrh assert( xtype==etINVALID ); 729874ba04cSdrh return; 730874ba04cSdrh } 731a18c5681Sdrh }/* End switch over the format type */ 732a18c5681Sdrh /* 733a18c5681Sdrh ** The text of the conversion is pointed to by "bufpt" and is 734a18c5681Sdrh ** "length" characters long. The field width is "width". Do 735a18c5681Sdrh ** the output. 736a18c5681Sdrh */ 737a70a073bSdrh width -= length; 738af8f513fSdrh if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); 739ade86483Sdrh sqlite3StrAccumAppend(pAccum, bufpt, length); 740af8f513fSdrh if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); 741a70a073bSdrh 742af8f513fSdrh if( zExtra ){ 74396ceaf86Sdrh sqlite3DbFree(pAccum->db, zExtra); 744af8f513fSdrh zExtra = 0; 745af8f513fSdrh } 746a18c5681Sdrh }/* End for loop over the format string */ 747a18c5681Sdrh } /* End of function */ 748a18c5681Sdrh 749a18c5681Sdrh /* 750a70a073bSdrh ** Enlarge the memory allocation on a StrAccum object so that it is 751a70a073bSdrh ** able to accept at least N more bytes of text. 752a70a073bSdrh ** 753a70a073bSdrh ** Return the number of bytes of text that StrAccum is able to accept 754a70a073bSdrh ** after the attempted enlargement. The value returned might be zero. 755a18c5681Sdrh */ 756a70a073bSdrh static int sqlite3StrAccumEnlarge(StrAccum *p, int N){ 757a6353a3fSdrh char *zNew; 758a30d22a7Sdrh assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */ 759b49bc86aSdrh if( p->accError ){ 760b49bc86aSdrh testcase(p->accError==STRACCUM_TOOBIG); 761b49bc86aSdrh testcase(p->accError==STRACCUM_NOMEM); 762a70a073bSdrh return 0; 763ade86483Sdrh } 764c0490572Sdrh if( p->mxAlloc==0 ){ 765ade86483Sdrh N = p->nAlloc - p->nChar - 1; 766a6353a3fSdrh setStrAccumError(p, STRACCUM_TOOBIG); 767a70a073bSdrh return N; 768a18c5681Sdrh }else{ 769a9ef7097Sdan char *zOld = (p->zText==p->zBase ? 0 : p->zText); 77093a960a0Sdrh i64 szNew = p->nChar; 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 ); 791b07028f7Sdrh if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); 792ade86483Sdrh p->zText = zNew; 7937f5a7ecdSdrh p->nAlloc = sqlite3DbMallocSize(p->db, zNew); 794ade86483Sdrh }else{ 795ade86483Sdrh sqlite3StrAccumReset(p); 796a6353a3fSdrh setStrAccumError(p, STRACCUM_NOMEM); 797a70a073bSdrh return 0; 798a70a073bSdrh } 799a70a073bSdrh } 800a70a073bSdrh return N; 801a70a073bSdrh } 802a70a073bSdrh 803a70a073bSdrh /* 804af8f513fSdrh ** Append N copies of character c to the given string buffer. 805a70a073bSdrh */ 806af8f513fSdrh void sqlite3AppendChar(StrAccum *p, int N, char c){ 807a30d22a7Sdrh testcase( p->nChar + (i64)N > 0x7fffffff ); 808a30d22a7Sdrh if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ 809a30d22a7Sdrh return; 810a30d22a7Sdrh } 811af8f513fSdrh while( (N--)>0 ) p->zText[p->nChar++] = c; 812a70a073bSdrh } 813a70a073bSdrh 814a70a073bSdrh /* 815a70a073bSdrh ** The StrAccum "p" is not large enough to accept N new bytes of z[]. 816a70a073bSdrh ** So enlarge if first, then do the append. 817a70a073bSdrh ** 818a70a073bSdrh ** This is a helper routine to sqlite3StrAccumAppend() that does special-case 819a70a073bSdrh ** work (enlarging the buffer) using tail recursion, so that the 820a70a073bSdrh ** sqlite3StrAccumAppend() routine can use fast calling semantics. 821a70a073bSdrh */ 822172087fbSdrh static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){ 823a70a073bSdrh N = sqlite3StrAccumEnlarge(p, N); 824a70a073bSdrh if( N>0 ){ 825a70a073bSdrh memcpy(&p->zText[p->nChar], z, N); 826a70a073bSdrh p->nChar += N; 827a70a073bSdrh } 828a70a073bSdrh } 829a70a073bSdrh 830a70a073bSdrh /* 831a70a073bSdrh ** Append N bytes of text from z to the StrAccum object. Increase the 832a70a073bSdrh ** size of the memory allocation for StrAccum if necessary. 833a70a073bSdrh */ 834a70a073bSdrh void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){ 8353457338cSdrh assert( z!=0 || N==0 ); 836a70a073bSdrh assert( p->zText!=0 || p->nChar==0 || p->accError ); 837a70a073bSdrh assert( N>=0 ); 838a70a073bSdrh assert( p->accError==0 || p->nAlloc==0 ); 839a70a073bSdrh if( p->nChar+N >= p->nAlloc ){ 840a70a073bSdrh enlargeAndAppend(p,z,N); 841172087fbSdrh }else{ 842b07028f7Sdrh assert( p->zText ); 843ade86483Sdrh p->nChar += N; 844172087fbSdrh memcpy(&p->zText[p->nChar-N], z, N); 845172087fbSdrh } 846483750baSdrh } 847483750baSdrh 848483750baSdrh /* 849a6353a3fSdrh ** Append the complete text of zero-terminated string z[] to the p string. 850a6353a3fSdrh */ 851a6353a3fSdrh void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){ 85253cd9646Smistachkin sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z)); 853a6353a3fSdrh } 854a6353a3fSdrh 855a6353a3fSdrh 856a6353a3fSdrh /* 857ade86483Sdrh ** Finish off a string by making sure it is zero-terminated. 858ade86483Sdrh ** Return a pointer to the resulting string. Return a NULL 859ade86483Sdrh ** pointer if any kind of error was encountered. 8605f968436Sdrh */ 861ade86483Sdrh char *sqlite3StrAccumFinish(StrAccum *p){ 862ade86483Sdrh if( p->zText ){ 863ade86483Sdrh p->zText[p->nChar] = 0; 864c0490572Sdrh if( p->mxAlloc>0 && p->zText==p->zBase ){ 865633e6d57Sdrh p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); 866ade86483Sdrh if( p->zText ){ 867ade86483Sdrh memcpy(p->zText, p->zBase, p->nChar+1); 868ade86483Sdrh }else{ 869a6353a3fSdrh setStrAccumError(p, STRACCUM_NOMEM); 870ade86483Sdrh } 871ade86483Sdrh } 872ade86483Sdrh } 873ade86483Sdrh return p->zText; 874ade86483Sdrh } 875ade86483Sdrh 876ade86483Sdrh /* 877ade86483Sdrh ** Reset an StrAccum string. Reclaim all malloced memory. 878ade86483Sdrh */ 879ade86483Sdrh void sqlite3StrAccumReset(StrAccum *p){ 880ade86483Sdrh if( p->zText!=p->zBase ){ 881633e6d57Sdrh sqlite3DbFree(p->db, p->zText); 882ade86483Sdrh } 883f089aa45Sdrh p->zText = 0; 884ade86483Sdrh } 885ade86483Sdrh 886ade86483Sdrh /* 887c0490572Sdrh ** Initialize a string accumulator. 888c0490572Sdrh ** 889c0490572Sdrh ** p: The accumulator to be initialized. 890c0490572Sdrh ** db: Pointer to a database connection. May be NULL. Lookaside 891c0490572Sdrh ** memory is used if not NULL. db->mallocFailed is set appropriately 892c0490572Sdrh ** when not NULL. 893c0490572Sdrh ** zBase: An initial buffer. May be NULL in which case the initial buffer 894c0490572Sdrh ** is malloced. 895c0490572Sdrh ** n: Size of zBase in bytes. If total space requirements never exceed 896c0490572Sdrh ** n then no memory allocations ever occur. 897c0490572Sdrh ** mx: Maximum number of bytes to accumulate. If mx==0 then no memory 898c0490572Sdrh ** allocations will ever occur. 899ade86483Sdrh */ 900c0490572Sdrh void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){ 901ade86483Sdrh p->zText = p->zBase = zBase; 902c0490572Sdrh p->db = db; 903ade86483Sdrh p->nChar = 0; 904ade86483Sdrh p->nAlloc = n; 905bb4957f8Sdrh p->mxAlloc = mx; 906b49bc86aSdrh p->accError = 0; 9075f968436Sdrh } 9085f968436Sdrh 9095f968436Sdrh /* 9105f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 9115f968436Sdrh ** %-conversion extensions. 9125f968436Sdrh */ 91317435752Sdrh char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ 91417435752Sdrh char *z; 91579158e18Sdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 916ade86483Sdrh StrAccum acc; 917bc6160b0Sdrh assert( db!=0 ); 918c0490572Sdrh sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase), 919bc6160b0Sdrh db->aLimit[SQLITE_LIMIT_LENGTH]); 920a5c1416dSdrh sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap); 921ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 922b49bc86aSdrh if( acc.accError==STRACCUM_NOMEM ){ 92317435752Sdrh db->mallocFailed = 1; 92417435752Sdrh } 92517435752Sdrh return z; 9265f968436Sdrh } 9275f968436Sdrh 9285f968436Sdrh /* 9295f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 9305f968436Sdrh ** %-conversion extensions. 9315f968436Sdrh */ 93217435752Sdrh char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ 9335f968436Sdrh va_list ap; 9345f968436Sdrh char *z; 9355f968436Sdrh va_start(ap, zFormat); 936ade86483Sdrh z = sqlite3VMPrintf(db, zFormat, ap); 9375f968436Sdrh va_end(ap); 9385f968436Sdrh return z; 9395f968436Sdrh } 9405f968436Sdrh 9415f968436Sdrh /* 94228dd479cSdrh ** Print into memory obtained from sqlite3_malloc(). Omit the internal 94328dd479cSdrh ** %-conversion extensions. 94428dd479cSdrh */ 94528dd479cSdrh char *sqlite3_vmprintf(const char *zFormat, va_list ap){ 946ade86483Sdrh char *z; 94728dd479cSdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 948ade86483Sdrh StrAccum acc; 9499ca95730Sdrh 9509ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 9519ca95730Sdrh if( zFormat==0 ){ 9529ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 9539ca95730Sdrh return 0; 9549ca95730Sdrh } 9559ca95730Sdrh #endif 956ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT 957ff1590eeSdrh if( sqlite3_initialize() ) return 0; 958ff1590eeSdrh #endif 959c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); 960f089aa45Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 961ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 962ade86483Sdrh return z; 96328dd479cSdrh } 96428dd479cSdrh 96528dd479cSdrh /* 96628dd479cSdrh ** Print into memory obtained from sqlite3_malloc()(). Omit the internal 96728dd479cSdrh ** %-conversion extensions. 968483750baSdrh */ 9696f8a503dSdanielk1977 char *sqlite3_mprintf(const char *zFormat, ...){ 970a18c5681Sdrh va_list ap; 9715f968436Sdrh char *z; 972ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT 973ff1590eeSdrh if( sqlite3_initialize() ) return 0; 974ff1590eeSdrh #endif 975a18c5681Sdrh va_start(ap, zFormat); 976b3738b6cSdrh z = sqlite3_vmprintf(zFormat, ap); 977a18c5681Sdrh va_end(ap); 9785f968436Sdrh return z; 979a18c5681Sdrh } 980a18c5681Sdrh 981a18c5681Sdrh /* 9826f8a503dSdanielk1977 ** sqlite3_snprintf() works like snprintf() except that it ignores the 98393a5c6bdSdrh ** current locale settings. This is important for SQLite because we 98493a5c6bdSdrh ** are not able to use a "," as the decimal point in place of "." as 98593a5c6bdSdrh ** specified by some locales. 986db26d4c9Sdrh ** 987db26d4c9Sdrh ** Oops: The first two arguments of sqlite3_snprintf() are backwards 988db26d4c9Sdrh ** from the snprintf() standard. Unfortunately, it is too late to change 989db26d4c9Sdrh ** this without breaking compatibility, so we just have to live with the 990db26d4c9Sdrh ** mistake. 991db26d4c9Sdrh ** 992db26d4c9Sdrh ** sqlite3_vsnprintf() is the varargs version. 99393a5c6bdSdrh */ 994db26d4c9Sdrh char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ 995db26d4c9Sdrh StrAccum acc; 996db26d4c9Sdrh if( n<=0 ) return zBuf; 9979ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 9989ca95730Sdrh if( zBuf==0 || zFormat==0 ) { 9999ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 100096c707a3Sdrh if( zBuf ) zBuf[0] = 0; 10019ca95730Sdrh return zBuf; 10029ca95730Sdrh } 10039ca95730Sdrh #endif 1004c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zBuf, n, 0); 1005db26d4c9Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 1006db26d4c9Sdrh return sqlite3StrAccumFinish(&acc); 1007db26d4c9Sdrh } 10086f8a503dSdanielk1977 char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ 10095f968436Sdrh char *z; 101093a5c6bdSdrh va_list ap; 101193a5c6bdSdrh va_start(ap,zFormat); 1012db26d4c9Sdrh z = sqlite3_vsnprintf(n, zBuf, zFormat, ap); 101393a5c6bdSdrh va_end(ap); 10145f968436Sdrh return z; 101593a5c6bdSdrh } 101693a5c6bdSdrh 10173f280701Sdrh /* 10187c0c460fSdrh ** This is the routine that actually formats the sqlite3_log() message. 10197c0c460fSdrh ** We house it in a separate routine from sqlite3_log() to avoid using 10207c0c460fSdrh ** stack space on small-stack systems when logging is disabled. 10217c0c460fSdrh ** 10227c0c460fSdrh ** sqlite3_log() must render into a static buffer. It cannot dynamically 10237c0c460fSdrh ** allocate memory because it might be called while the memory allocator 10247c0c460fSdrh ** mutex is held. 1025a8dbd52aSdrh ** 1026a8dbd52aSdrh ** sqlite3VXPrintf() might ask for *temporary* memory allocations for 1027a8dbd52aSdrh ** certain format characters (%q) or for very large precisions or widths. 1028a8dbd52aSdrh ** Care must be taken that any sqlite3_log() calls that occur while the 1029a8dbd52aSdrh ** memory mutex is held do not use these mechanisms. 10307c0c460fSdrh */ 10317c0c460fSdrh static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){ 10327c0c460fSdrh StrAccum acc; /* String accumulator */ 1033a64fa912Sdrh char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */ 10347c0c460fSdrh 1035c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0); 10367c0c460fSdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 10377c0c460fSdrh sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode, 10387c0c460fSdrh sqlite3StrAccumFinish(&acc)); 10397c0c460fSdrh } 10407c0c460fSdrh 10417c0c460fSdrh /* 10423f280701Sdrh ** Format and write a message to the log if logging is enabled. 10433f280701Sdrh */ 1044a7564663Sdrh void sqlite3_log(int iErrCode, const char *zFormat, ...){ 10453f280701Sdrh va_list ap; /* Vararg list */ 10467c0c460fSdrh if( sqlite3GlobalConfig.xLog ){ 10473f280701Sdrh va_start(ap, zFormat); 10487c0c460fSdrh renderLogMsg(iErrCode, zFormat, ap); 10493f280701Sdrh va_end(ap); 10503f280701Sdrh } 10513f280701Sdrh } 10523f280701Sdrh 105302b0e267Smistachkin #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) 1054e54ca3feSdrh /* 1055e54ca3feSdrh ** A version of printf() that understands %lld. Used for debugging. 1056e54ca3feSdrh ** The printf() built into some versions of windows does not understand %lld 1057e54ca3feSdrh ** and segfaults if you give it a long long int. 1058e54ca3feSdrh */ 1059e54ca3feSdrh void sqlite3DebugPrintf(const char *zFormat, ...){ 1060e54ca3feSdrh va_list ap; 1061ade86483Sdrh StrAccum acc; 1062e54ca3feSdrh char zBuf[500]; 1063c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); 1064e54ca3feSdrh va_start(ap,zFormat); 1065f089aa45Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 1066e54ca3feSdrh va_end(ap); 1067bed8e7e5Sdrh sqlite3StrAccumFinish(&acc); 1068485f0039Sdrh fprintf(stdout,"%s", zBuf); 10692ac3ee97Sdrh fflush(stdout); 1070e54ca3feSdrh } 1071e54ca3feSdrh #endif 1072c7bc4fdeSdrh 10734fa4a54fSdrh 1074c7bc4fdeSdrh /* 1075d37bea5bSdrh ** variable-argument wrapper around sqlite3VXPrintf(). The bFlags argument 1076d37bea5bSdrh ** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats. 1077c7bc4fdeSdrh */ 1078a5c1416dSdrh void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){ 1079c7bc4fdeSdrh va_list ap; 1080c7bc4fdeSdrh va_start(ap,zFormat); 1081a5c1416dSdrh sqlite3VXPrintf(p, bFlags, zFormat, ap); 1082c7bc4fdeSdrh va_end(ap); 1083c7bc4fdeSdrh } 1084