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 } 2738c069147Sdan 274a18c5681Sdrh /* Get the precision */ 275a18c5681Sdrh if( c=='.' ){ 276a18c5681Sdrh c = *++fmt; 277a18c5681Sdrh if( c=='*' ){ 278a5c1416dSdrh if( bArgList ){ 279a5c1416dSdrh precision = (int)getIntArg(pArgList); 280a5c1416dSdrh }else{ 281a18c5681Sdrh precision = va_arg(ap,int); 282a5c1416dSdrh } 283a18c5681Sdrh c = *++fmt; 284b6f47debSdrh if( precision<0 ){ 285b6f47debSdrh precision = precision >= -2147483647 ? -precision : -1; 286b6f47debSdrh } 287a18c5681Sdrh }else{ 288b6f47debSdrh unsigned px = 0; 28917a68934Sdrh while( c>='0' && c<='9' ){ 290b6f47debSdrh px = px*10 + c - '0'; 291a18c5681Sdrh c = *++fmt; 292a18c5681Sdrh } 293b6f47debSdrh testcase( px>0x7fffffff ); 294b6f47debSdrh precision = px & 0x7fffffff; 295a18c5681Sdrh } 296a18c5681Sdrh }else{ 297a18c5681Sdrh precision = -1; 298a18c5681Sdrh } 299a18c5681Sdrh /* Get the conversion type modifier */ 300a18c5681Sdrh if( c=='l' ){ 301a18c5681Sdrh flag_long = 1; 302a18c5681Sdrh c = *++fmt; 303a34b6764Sdrh if( c=='l' ){ 304a34b6764Sdrh flag_longlong = 1; 305a34b6764Sdrh c = *++fmt; 306a18c5681Sdrh }else{ 307a34b6764Sdrh flag_longlong = 0; 308a34b6764Sdrh } 309a34b6764Sdrh }else{ 310a34b6764Sdrh flag_long = flag_longlong = 0; 311a18c5681Sdrh } 312a18c5681Sdrh /* Fetch the info entry for the field */ 313874ba04cSdrh infop = &fmtinfo[0]; 314874ba04cSdrh xtype = etINVALID; 31500e13613Sdanielk1977 for(idx=0; idx<ArraySize(fmtinfo); idx++){ 316a18c5681Sdrh if( c==fmtinfo[idx].fmttype ){ 317a18c5681Sdrh infop = &fmtinfo[idx]; 318a5c1416dSdrh if( useIntern || (infop->flags & FLAG_INTERN)==0 ){ 319e84a306bSdrh xtype = infop->type; 320c4413565Sdrh }else{ 321ade86483Sdrh return; 3225f968436Sdrh } 323a18c5681Sdrh break; 324a18c5681Sdrh } 325a18c5681Sdrh } 32643617e9aSdrh 327a18c5681Sdrh /* 328a18c5681Sdrh ** At this point, variables are initialized as follows: 329a18c5681Sdrh ** 330a18c5681Sdrh ** flag_alternateform TRUE if a '#' is present. 3313e9aeec0Sdrh ** flag_altform2 TRUE if a '!' is present. 332a18c5681Sdrh ** flag_plussign TRUE if a '+' is present. 333a18c5681Sdrh ** flag_leftjustify TRUE if a '-' is present or if the 334a18c5681Sdrh ** field width was negative. 335a18c5681Sdrh ** flag_zeropad TRUE if the width began with 0. 336a18c5681Sdrh ** flag_long TRUE if the letter 'l' (ell) prefixed 337a18c5681Sdrh ** the conversion character. 338a34b6764Sdrh ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed 339a34b6764Sdrh ** the conversion character. 340a18c5681Sdrh ** flag_blanksign TRUE if a ' ' is present. 341a18c5681Sdrh ** width The specified field width. This is 342a18c5681Sdrh ** always non-negative. Zero is the default. 343a18c5681Sdrh ** precision The specified precision. The default 344a18c5681Sdrh ** is -1. 345a18c5681Sdrh ** xtype The class of the conversion. 346a18c5681Sdrh ** infop Pointer to the appropriate info struct. 347a18c5681Sdrh */ 348a18c5681Sdrh switch( xtype ){ 349fe63d1c9Sdrh case etPOINTER: 350fe63d1c9Sdrh flag_longlong = sizeof(char*)==sizeof(i64); 351fe63d1c9Sdrh flag_long = sizeof(char*)==sizeof(long int); 352fe63d1c9Sdrh /* Fall through into the next case */ 3539a99334dSdrh case etORDINAL: 354a18c5681Sdrh case etRADIX: 355e84a306bSdrh if( infop->flags & FLAG_SIGNED ){ 356e9707671Sdrh i64 v; 357a5c1416dSdrh if( bArgList ){ 358a5c1416dSdrh v = getIntArg(pArgList); 359a5c1416dSdrh }else if( flag_longlong ){ 360eeb23a4cSdrh v = va_arg(ap,i64); 361eeb23a4cSdrh }else if( flag_long ){ 362eeb23a4cSdrh v = va_arg(ap,long int); 363eeb23a4cSdrh }else{ 364eeb23a4cSdrh v = va_arg(ap,int); 365eeb23a4cSdrh } 366e9707671Sdrh if( v<0 ){ 367158b9cb9Sdrh if( v==SMALLEST_INT64 ){ 368158b9cb9Sdrh longvalue = ((u64)1)<<63; 369158b9cb9Sdrh }else{ 370158b9cb9Sdrh longvalue = -v; 371158b9cb9Sdrh } 372cfcdaefeSdanielk1977 prefix = '-'; 373cfcdaefeSdanielk1977 }else{ 374e9707671Sdrh longvalue = v; 375e9707671Sdrh if( flag_plussign ) prefix = '+'; 376a18c5681Sdrh else if( flag_blanksign ) prefix = ' '; 377a18c5681Sdrh else prefix = 0; 378cfcdaefeSdanielk1977 } 379e9707671Sdrh }else{ 380a5c1416dSdrh if( bArgList ){ 381a5c1416dSdrh longvalue = (u64)getIntArg(pArgList); 382a5c1416dSdrh }else if( flag_longlong ){ 383eeb23a4cSdrh longvalue = va_arg(ap,u64); 384eeb23a4cSdrh }else if( flag_long ){ 385eeb23a4cSdrh longvalue = va_arg(ap,unsigned long int); 386eeb23a4cSdrh }else{ 387eeb23a4cSdrh longvalue = va_arg(ap,unsigned int); 388eeb23a4cSdrh } 389e9707671Sdrh prefix = 0; 390e9707671Sdrh } 391e9707671Sdrh if( longvalue==0 ) flag_alternateform = 0; 392a18c5681Sdrh if( flag_zeropad && precision<width-(prefix!=0) ){ 393a18c5681Sdrh precision = width-(prefix!=0); 394a18c5681Sdrh } 39559eedf79Sdrh if( precision<etBUFSIZE-10 ){ 39659eedf79Sdrh nOut = etBUFSIZE; 39759eedf79Sdrh zOut = buf; 39859eedf79Sdrh }else{ 39959eedf79Sdrh nOut = precision + 10; 40059eedf79Sdrh zOut = zExtra = sqlite3Malloc( nOut ); 40159eedf79Sdrh if( zOut==0 ){ 402a6353a3fSdrh setStrAccumError(pAccum, STRACCUM_NOMEM); 40359eedf79Sdrh return; 40459eedf79Sdrh } 40559eedf79Sdrh } 40659eedf79Sdrh bufpt = &zOut[nOut-1]; 4079a99334dSdrh if( xtype==etORDINAL ){ 40843f6e064Sdrh static const char zOrd[] = "thstndrd"; 409ea678832Sdrh int x = (int)(longvalue % 10); 41043f6e064Sdrh if( x>=4 || (longvalue/10)%10==1 ){ 41143f6e064Sdrh x = 0; 41243f6e064Sdrh } 41359eedf79Sdrh *(--bufpt) = zOrd[x*2+1]; 41459eedf79Sdrh *(--bufpt) = zOrd[x*2]; 4159a99334dSdrh } 416a18c5681Sdrh { 4170e682099Sdrh const char *cset = &aDigits[infop->charset]; 4180e682099Sdrh u8 base = infop->base; 419a18c5681Sdrh do{ /* Convert to ascii */ 420a18c5681Sdrh *(--bufpt) = cset[longvalue%base]; 421a18c5681Sdrh longvalue = longvalue/base; 422a18c5681Sdrh }while( longvalue>0 ); 423a18c5681Sdrh } 42459eedf79Sdrh length = (int)(&zOut[nOut-1]-bufpt); 425a18c5681Sdrh for(idx=precision-length; idx>0; idx--){ 426a18c5681Sdrh *(--bufpt) = '0'; /* Zero pad */ 427a18c5681Sdrh } 428a18c5681Sdrh if( prefix ) *(--bufpt) = prefix; /* Add sign */ 429a18c5681Sdrh if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ 43076ff3a0eSdrh const char *pre; 43176ff3a0eSdrh char x; 43276ff3a0eSdrh pre = &aPrefix[infop->prefix]; 43376ff3a0eSdrh for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; 434a18c5681Sdrh } 43559eedf79Sdrh length = (int)(&zOut[nOut-1]-bufpt); 436a18c5681Sdrh break; 437a18c5681Sdrh case etFLOAT: 438a18c5681Sdrh case etEXP: 439a18c5681Sdrh case etGENERIC: 440a5c1416dSdrh if( bArgList ){ 441a5c1416dSdrh realvalue = getDoubleArg(pArgList); 442a5c1416dSdrh }else{ 443a18c5681Sdrh realvalue = va_arg(ap,double); 444a5c1416dSdrh } 44569ef7036Sdrh #ifdef SQLITE_OMIT_FLOATING_POINT 44669ef7036Sdrh length = 0; 44769ef7036Sdrh #else 448a18c5681Sdrh if( precision<0 ) precision = 6; /* Set default precision */ 449a18c5681Sdrh if( realvalue<0.0 ){ 450a18c5681Sdrh realvalue = -realvalue; 451a18c5681Sdrh prefix = '-'; 452a18c5681Sdrh }else{ 453a18c5681Sdrh if( flag_plussign ) prefix = '+'; 454a18c5681Sdrh else if( flag_blanksign ) prefix = ' '; 455a18c5681Sdrh else prefix = 0; 456a18c5681Sdrh } 4573e9aeec0Sdrh if( xtype==etGENERIC && precision>0 ) precision--; 458a30d22a7Sdrh testcase( precision>0xfff ); 45974b42275Sdrh for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){} 4603e9aeec0Sdrh if( xtype==etFLOAT ) realvalue += rounder; 461a18c5681Sdrh /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ 462a18c5681Sdrh exp = 0; 463ea678832Sdrh if( sqlite3IsNaN((double)realvalue) ){ 46453c14021Sdrh bufpt = "NaN"; 46553c14021Sdrh length = 3; 46653c14021Sdrh break; 46753c14021Sdrh } 468a18c5681Sdrh if( realvalue>0.0 ){ 46972b3fbc7Sdrh LONGDOUBLE_TYPE scale = 1.0; 4704ef94130Sdrh while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;} 47172b3fbc7Sdrh while( realvalue>=1e64*scale && exp<=350 ){ scale *= 1e64; exp+=64; } 47272b3fbc7Sdrh while( realvalue>=1e8*scale && exp<=350 ){ scale *= 1e8; exp+=8; } 47372b3fbc7Sdrh while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; } 47472b3fbc7Sdrh realvalue /= scale; 475af005fbcSdrh while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } 476af005fbcSdrh while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } 477af005fbcSdrh if( exp>350 ){ 47853c14021Sdrh if( prefix=='-' ){ 47953c14021Sdrh bufpt = "-Inf"; 48053c14021Sdrh }else if( prefix=='+' ){ 48153c14021Sdrh bufpt = "+Inf"; 48253c14021Sdrh }else{ 48353c14021Sdrh bufpt = "Inf"; 48453c14021Sdrh } 485ea678832Sdrh length = sqlite3Strlen30(bufpt); 486a18c5681Sdrh break; 487a18c5681Sdrh } 488a18c5681Sdrh } 489a18c5681Sdrh bufpt = buf; 490a18c5681Sdrh /* 491a18c5681Sdrh ** If the field type is etGENERIC, then convert to either etEXP 492a18c5681Sdrh ** or etFLOAT, as appropriate. 493a18c5681Sdrh */ 494a18c5681Sdrh if( xtype!=etFLOAT ){ 495a18c5681Sdrh realvalue += rounder; 496a18c5681Sdrh if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } 497a18c5681Sdrh } 498a18c5681Sdrh if( xtype==etGENERIC ){ 499a18c5681Sdrh flag_rtz = !flag_alternateform; 500a18c5681Sdrh if( exp<-4 || exp>precision ){ 501a18c5681Sdrh xtype = etEXP; 502a18c5681Sdrh }else{ 503a18c5681Sdrh precision = precision - exp; 504a18c5681Sdrh xtype = etFLOAT; 505a18c5681Sdrh } 506a18c5681Sdrh }else{ 50772b3fbc7Sdrh flag_rtz = flag_altform2; 508a18c5681Sdrh } 509557cc60fSdrh if( xtype==etEXP ){ 510557cc60fSdrh e2 = 0; 511557cc60fSdrh }else{ 512557cc60fSdrh e2 = exp; 513557cc60fSdrh } 51474b42275Sdrh if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){ 51574b42275Sdrh bufpt = zExtra 51674b42275Sdrh = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 ); 51759eedf79Sdrh if( bufpt==0 ){ 518a6353a3fSdrh setStrAccumError(pAccum, STRACCUM_NOMEM); 51959eedf79Sdrh return; 52059eedf79Sdrh } 52159eedf79Sdrh } 52259eedf79Sdrh zOut = bufpt; 52372b3fbc7Sdrh nsd = 16 + flag_altform2*10; 524ea678832Sdrh flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; 525557cc60fSdrh /* The sign in front of the number */ 526557cc60fSdrh if( prefix ){ 527557cc60fSdrh *(bufpt++) = prefix; 528557cc60fSdrh } 529557cc60fSdrh /* Digits prior to the decimal point */ 530557cc60fSdrh if( e2<0 ){ 531557cc60fSdrh *(bufpt++) = '0'; 532557cc60fSdrh }else{ 533557cc60fSdrh for(; e2>=0; e2--){ 534557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 535557cc60fSdrh } 536557cc60fSdrh } 537557cc60fSdrh /* The decimal point */ 538557cc60fSdrh if( flag_dp ){ 539557cc60fSdrh *(bufpt++) = '.'; 540557cc60fSdrh } 541557cc60fSdrh /* "0" digits after the decimal point but before the first 542557cc60fSdrh ** significant digit of the number */ 543af005fbcSdrh for(e2++; e2<0; precision--, e2++){ 544af005fbcSdrh assert( precision>0 ); 545a18c5681Sdrh *(bufpt++) = '0'; 546a18c5681Sdrh } 547557cc60fSdrh /* Significant digits after the decimal point */ 548557cc60fSdrh while( (precision--)>0 ){ 549557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 550a18c5681Sdrh } 551557cc60fSdrh /* Remove trailing zeros and the "." if no digits follow the "." */ 552557cc60fSdrh if( flag_rtz && flag_dp ){ 5533e9aeec0Sdrh while( bufpt[-1]=='0' ) *(--bufpt) = 0; 55459eedf79Sdrh assert( bufpt>zOut ); 5553e9aeec0Sdrh if( bufpt[-1]=='.' ){ 556557cc60fSdrh if( flag_altform2 ){ 557557cc60fSdrh *(bufpt++) = '0'; 558557cc60fSdrh }else{ 559557cc60fSdrh *(--bufpt) = 0; 560a18c5681Sdrh } 561557cc60fSdrh } 562557cc60fSdrh } 563557cc60fSdrh /* Add the "eNNN" suffix */ 56459eedf79Sdrh if( xtype==etEXP ){ 56576ff3a0eSdrh *(bufpt++) = aDigits[infop->charset]; 566557cc60fSdrh if( exp<0 ){ 567557cc60fSdrh *(bufpt++) = '-'; exp = -exp; 568557cc60fSdrh }else{ 569557cc60fSdrh *(bufpt++) = '+'; 570557cc60fSdrh } 571a18c5681Sdrh if( exp>=100 ){ 572ea678832Sdrh *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */ 573a18c5681Sdrh exp %= 100; 574a18c5681Sdrh } 575ea678832Sdrh *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */ 576ea678832Sdrh *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */ 577a18c5681Sdrh } 578557cc60fSdrh *bufpt = 0; 579557cc60fSdrh 580a18c5681Sdrh /* The converted number is in buf[] and zero terminated. Output it. 581a18c5681Sdrh ** Note that the number is in the usual order, not reversed as with 582a18c5681Sdrh ** integer conversions. */ 58359eedf79Sdrh length = (int)(bufpt-zOut); 58459eedf79Sdrh bufpt = zOut; 585a18c5681Sdrh 586a18c5681Sdrh /* Special case: Add leading zeros if the flag_zeropad flag is 587a18c5681Sdrh ** set and we are not left justified */ 588a18c5681Sdrh if( flag_zeropad && !flag_leftjustify && length < width){ 589a18c5681Sdrh int i; 590a18c5681Sdrh int nPad = width - length; 591a18c5681Sdrh for(i=width; i>=nPad; i--){ 592a18c5681Sdrh bufpt[i] = bufpt[i-nPad]; 593a18c5681Sdrh } 594a18c5681Sdrh i = prefix!=0; 595a18c5681Sdrh while( nPad-- ) bufpt[i++] = '0'; 596a18c5681Sdrh length = width; 597a18c5681Sdrh } 59869ef7036Sdrh #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */ 599a18c5681Sdrh break; 600a18c5681Sdrh case etSIZE: 601fc6ee9dfSdrh if( !bArgList ){ 602fc6ee9dfSdrh *(va_arg(ap,int*)) = pAccum->nChar; 603fc6ee9dfSdrh } 604a18c5681Sdrh length = width = 0; 605a18c5681Sdrh break; 606a18c5681Sdrh case etPERCENT: 607a18c5681Sdrh buf[0] = '%'; 608a18c5681Sdrh bufpt = buf; 609a18c5681Sdrh length = 1; 610a18c5681Sdrh break; 611a18c5681Sdrh case etCHARX: 612a5c1416dSdrh if( bArgList ){ 613fc6ee9dfSdrh bufpt = getTextArg(pArgList); 614fc6ee9dfSdrh c = bufpt ? bufpt[0] : 0; 615a5c1416dSdrh }else{ 616ea678832Sdrh c = va_arg(ap,int); 617a5c1416dSdrh } 618af8f513fSdrh if( precision>1 ){ 619af8f513fSdrh width -= precision-1; 620af8f513fSdrh if( width>1 && !flag_leftjustify ){ 621af8f513fSdrh sqlite3AppendChar(pAccum, width-1, ' '); 622af8f513fSdrh width = 0; 623a18c5681Sdrh } 624af8f513fSdrh sqlite3AppendChar(pAccum, precision-1, c); 625af8f513fSdrh } 626af8f513fSdrh length = 1; 627af8f513fSdrh buf[0] = c; 628a18c5681Sdrh bufpt = buf; 629a18c5681Sdrh break; 630a18c5681Sdrh case etSTRING: 631d93d8a81Sdrh case etDYNSTRING: 632a5c1416dSdrh if( bArgList ){ 633a5c1416dSdrh bufpt = getTextArg(pArgList); 634a5c1416dSdrh }else{ 635cb485882Sdrh bufpt = va_arg(ap,char*); 636a5c1416dSdrh } 637d93d8a81Sdrh if( bufpt==0 ){ 638d93d8a81Sdrh bufpt = ""; 639a5c1416dSdrh }else if( xtype==etDYNSTRING && !bArgList ){ 640d93d8a81Sdrh zExtra = bufpt; 641d93d8a81Sdrh } 642e509094bSdrh if( precision>=0 ){ 643e509094bSdrh for(length=0; length<precision && bufpt[length]; length++){} 644e509094bSdrh }else{ 645ea678832Sdrh length = sqlite3Strlen30(bufpt); 646e509094bSdrh } 647a18c5681Sdrh break; 648a18c5681Sdrh case etSQLESCAPE: 649f3b863edSdanielk1977 case etSQLESCAPE2: 650f3b863edSdanielk1977 case etSQLESCAPE3: { 6518965b50eSdrh int i, j, k, n, isnull; 6524794f735Sdrh int needQuote; 653ea678832Sdrh char ch; 654f3b863edSdanielk1977 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */ 655a5c1416dSdrh char *escarg; 656a5c1416dSdrh 657a5c1416dSdrh if( bArgList ){ 658a5c1416dSdrh escarg = getTextArg(pArgList); 659a5c1416dSdrh }else{ 660a5c1416dSdrh escarg = va_arg(ap,char*); 661a5c1416dSdrh } 662f0113000Sdanielk1977 isnull = escarg==0; 663f0113000Sdanielk1977 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); 6648965b50eSdrh k = precision; 66560d4a304Sdan for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ 666f3b863edSdanielk1977 if( ch==q ) n++; 667a18c5681Sdrh } 6684794f735Sdrh needQuote = !isnull && xtype==etSQLESCAPE2; 6694794f735Sdrh n += i + 1 + needQuote*2; 670a18c5681Sdrh if( n>etBUFSIZE ){ 671e5ae5735Sdrh bufpt = zExtra = sqlite3Malloc( n ); 672d164fd34Sdrh if( bufpt==0 ){ 673a6353a3fSdrh setStrAccumError(pAccum, STRACCUM_NOMEM); 674d164fd34Sdrh return; 675d164fd34Sdrh } 676a18c5681Sdrh }else{ 677a18c5681Sdrh bufpt = buf; 678a18c5681Sdrh } 6790cfcf3fbSchw j = 0; 680f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 6818965b50eSdrh k = i; 6828965b50eSdrh for(i=0; i<k; i++){ 6838965b50eSdrh bufpt[j++] = ch = escarg[i]; 684f3b863edSdanielk1977 if( ch==q ) bufpt[j++] = ch; 685a18c5681Sdrh } 686f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 687a18c5681Sdrh bufpt[j] = 0; 688a18c5681Sdrh length = j; 6898965b50eSdrh /* The precision in %q and %Q means how many input characters to 6908965b50eSdrh ** consume, not the length of the output... 6918965b50eSdrh ** if( precision>=0 && precision<length ) length = precision; */ 692a18c5681Sdrh break; 6935eba8c09Sdrh } 6945f968436Sdrh case etTOKEN: { 6955f968436Sdrh Token *pToken = va_arg(ap, Token*); 696a5c1416dSdrh assert( bArgList==0 ); 697a9ab481fSdrh if( pToken && pToken->n ){ 698ade86483Sdrh sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n); 699ad6d9460Sdrh } 7005f968436Sdrh length = width = 0; 7015f968436Sdrh break; 7025f968436Sdrh } 7035f968436Sdrh case etSRCLIST: { 7045f968436Sdrh SrcList *pSrc = va_arg(ap, SrcList*); 7055f968436Sdrh int k = va_arg(ap, int); 7065f968436Sdrh struct SrcList_item *pItem = &pSrc->a[k]; 707a5c1416dSdrh assert( bArgList==0 ); 7085f968436Sdrh assert( k>=0 && k<pSrc->nSrc ); 70993a960a0Sdrh if( pItem->zDatabase ){ 710a6353a3fSdrh sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase); 711ade86483Sdrh sqlite3StrAccumAppend(pAccum, ".", 1); 7125f968436Sdrh } 713a6353a3fSdrh sqlite3StrAccumAppendAll(pAccum, pItem->zName); 7145f968436Sdrh length = width = 0; 7155f968436Sdrh break; 7165f968436Sdrh } 717874ba04cSdrh default: { 718874ba04cSdrh assert( xtype==etINVALID ); 719874ba04cSdrh return; 720874ba04cSdrh } 721a18c5681Sdrh }/* End switch over the format type */ 722a18c5681Sdrh /* 723a18c5681Sdrh ** The text of the conversion is pointed to by "bufpt" and is 724a18c5681Sdrh ** "length" characters long. The field width is "width". Do 725a18c5681Sdrh ** the output. 726a18c5681Sdrh */ 727a70a073bSdrh width -= length; 728af8f513fSdrh if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); 729ade86483Sdrh sqlite3StrAccumAppend(pAccum, bufpt, length); 730af8f513fSdrh if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); 731a70a073bSdrh 732af8f513fSdrh if( zExtra ){ 733af8f513fSdrh sqlite3_free(zExtra); 734af8f513fSdrh zExtra = 0; 735af8f513fSdrh } 736a18c5681Sdrh }/* End for loop over the format string */ 737a18c5681Sdrh } /* End of function */ 738a18c5681Sdrh 739a18c5681Sdrh /* 740a70a073bSdrh ** Enlarge the memory allocation on a StrAccum object so that it is 741a70a073bSdrh ** able to accept at least N more bytes of text. 742a70a073bSdrh ** 743a70a073bSdrh ** Return the number of bytes of text that StrAccum is able to accept 744a70a073bSdrh ** after the attempted enlargement. The value returned might be zero. 745a18c5681Sdrh */ 746a70a073bSdrh static int sqlite3StrAccumEnlarge(StrAccum *p, int N){ 747a6353a3fSdrh char *zNew; 748a30d22a7Sdrh assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */ 749b49bc86aSdrh if( p->accError ){ 750b49bc86aSdrh testcase(p->accError==STRACCUM_TOOBIG); 751b49bc86aSdrh testcase(p->accError==STRACCUM_NOMEM); 752a70a073bSdrh return 0; 753ade86483Sdrh } 754c0490572Sdrh if( p->mxAlloc==0 ){ 755ade86483Sdrh N = p->nAlloc - p->nChar - 1; 756a6353a3fSdrh setStrAccumError(p, STRACCUM_TOOBIG); 757a70a073bSdrh return N; 758a18c5681Sdrh }else{ 759a9ef7097Sdan char *zOld = (p->zText==p->zBase ? 0 : p->zText); 76093a960a0Sdrh i64 szNew = p->nChar; 761b1a6c3c1Sdrh szNew += N + 1; 7627b4d780bSdrh if( szNew+p->nChar<=p->mxAlloc ){ 7637b4d780bSdrh /* Force exponential buffer size growth as long as it does not overflow, 7647b4d780bSdrh ** to avoid having to call this routine too often */ 7657b4d780bSdrh szNew += p->nChar; 7667b4d780bSdrh } 767b1a6c3c1Sdrh if( szNew > p->mxAlloc ){ 768ade86483Sdrh sqlite3StrAccumReset(p); 769a6353a3fSdrh setStrAccumError(p, STRACCUM_TOOBIG); 770a70a073bSdrh return 0; 771b1a6c3c1Sdrh }else{ 772ea678832Sdrh p->nAlloc = (int)szNew; 773a18c5681Sdrh } 774c0490572Sdrh if( p->db ){ 775a9ef7097Sdan zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); 776b975598eSdrh }else{ 777f3cdcdccSdrh zNew = sqlite3_realloc64(zOld, p->nAlloc); 778b975598eSdrh } 77953f733c7Sdrh if( zNew ){ 7807ef4d1c4Sdrh assert( p->zText!=0 || p->nChar==0 ); 781b07028f7Sdrh if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); 782ade86483Sdrh p->zText = zNew; 7837f5a7ecdSdrh p->nAlloc = sqlite3DbMallocSize(p->db, zNew); 784ade86483Sdrh }else{ 785ade86483Sdrh sqlite3StrAccumReset(p); 786a6353a3fSdrh setStrAccumError(p, STRACCUM_NOMEM); 787a70a073bSdrh return 0; 788a70a073bSdrh } 789a70a073bSdrh } 790a70a073bSdrh return N; 791a70a073bSdrh } 792a70a073bSdrh 793a70a073bSdrh /* 794af8f513fSdrh ** Append N copies of character c to the given string buffer. 795a70a073bSdrh */ 796af8f513fSdrh void sqlite3AppendChar(StrAccum *p, int N, char c){ 797a30d22a7Sdrh testcase( p->nChar + (i64)N > 0x7fffffff ); 798a30d22a7Sdrh if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ 799a30d22a7Sdrh return; 800a30d22a7Sdrh } 801af8f513fSdrh while( (N--)>0 ) p->zText[p->nChar++] = c; 802a70a073bSdrh } 803a70a073bSdrh 804a70a073bSdrh /* 805a70a073bSdrh ** The StrAccum "p" is not large enough to accept N new bytes of z[]. 806a70a073bSdrh ** So enlarge if first, then do the append. 807a70a073bSdrh ** 808a70a073bSdrh ** This is a helper routine to sqlite3StrAccumAppend() that does special-case 809a70a073bSdrh ** work (enlarging the buffer) using tail recursion, so that the 810a70a073bSdrh ** sqlite3StrAccumAppend() routine can use fast calling semantics. 811a70a073bSdrh */ 812172087fbSdrh static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){ 813a70a073bSdrh N = sqlite3StrAccumEnlarge(p, N); 814a70a073bSdrh if( N>0 ){ 815a70a073bSdrh memcpy(&p->zText[p->nChar], z, N); 816a70a073bSdrh p->nChar += N; 817a70a073bSdrh } 818a70a073bSdrh } 819a70a073bSdrh 820a70a073bSdrh /* 821a70a073bSdrh ** Append N bytes of text from z to the StrAccum object. Increase the 822a70a073bSdrh ** size of the memory allocation for StrAccum if necessary. 823a70a073bSdrh */ 824a70a073bSdrh void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){ 8253457338cSdrh assert( z!=0 || N==0 ); 826a70a073bSdrh assert( p->zText!=0 || p->nChar==0 || p->accError ); 827a70a073bSdrh assert( N>=0 ); 828a70a073bSdrh assert( p->accError==0 || p->nAlloc==0 ); 829a70a073bSdrh if( p->nChar+N >= p->nAlloc ){ 830a70a073bSdrh enlargeAndAppend(p,z,N); 831172087fbSdrh }else{ 832b07028f7Sdrh assert( p->zText ); 833ade86483Sdrh p->nChar += N; 834172087fbSdrh memcpy(&p->zText[p->nChar-N], z, N); 835172087fbSdrh } 836483750baSdrh } 837483750baSdrh 838483750baSdrh /* 839a6353a3fSdrh ** Append the complete text of zero-terminated string z[] to the p string. 840a6353a3fSdrh */ 841a6353a3fSdrh void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){ 84253cd9646Smistachkin sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z)); 843a6353a3fSdrh } 844a6353a3fSdrh 845a6353a3fSdrh 846a6353a3fSdrh /* 847ade86483Sdrh ** Finish off a string by making sure it is zero-terminated. 848ade86483Sdrh ** Return a pointer to the resulting string. Return a NULL 849ade86483Sdrh ** pointer if any kind of error was encountered. 8505f968436Sdrh */ 851ade86483Sdrh char *sqlite3StrAccumFinish(StrAccum *p){ 852ade86483Sdrh if( p->zText ){ 853ade86483Sdrh p->zText[p->nChar] = 0; 854c0490572Sdrh if( p->mxAlloc>0 && p->zText==p->zBase ){ 855633e6d57Sdrh p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); 856ade86483Sdrh if( p->zText ){ 857ade86483Sdrh memcpy(p->zText, p->zBase, p->nChar+1); 858ade86483Sdrh }else{ 859a6353a3fSdrh setStrAccumError(p, STRACCUM_NOMEM); 860ade86483Sdrh } 861ade86483Sdrh } 862ade86483Sdrh } 863ade86483Sdrh return p->zText; 864ade86483Sdrh } 865ade86483Sdrh 866ade86483Sdrh /* 867ade86483Sdrh ** Reset an StrAccum string. Reclaim all malloced memory. 868ade86483Sdrh */ 869ade86483Sdrh void sqlite3StrAccumReset(StrAccum *p){ 870ade86483Sdrh if( p->zText!=p->zBase ){ 871633e6d57Sdrh sqlite3DbFree(p->db, p->zText); 872ade86483Sdrh } 873f089aa45Sdrh p->zText = 0; 874ade86483Sdrh } 875ade86483Sdrh 876ade86483Sdrh /* 877c0490572Sdrh ** Initialize a string accumulator. 878c0490572Sdrh ** 879c0490572Sdrh ** p: The accumulator to be initialized. 880c0490572Sdrh ** db: Pointer to a database connection. May be NULL. Lookaside 881c0490572Sdrh ** memory is used if not NULL. db->mallocFailed is set appropriately 882c0490572Sdrh ** when not NULL. 883c0490572Sdrh ** zBase: An initial buffer. May be NULL in which case the initial buffer 884c0490572Sdrh ** is malloced. 885c0490572Sdrh ** n: Size of zBase in bytes. If total space requirements never exceed 886c0490572Sdrh ** n then no memory allocations ever occur. 887c0490572Sdrh ** mx: Maximum number of bytes to accumulate. If mx==0 then no memory 888c0490572Sdrh ** allocations will ever occur. 889ade86483Sdrh */ 890c0490572Sdrh void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){ 891ade86483Sdrh p->zText = p->zBase = zBase; 892c0490572Sdrh p->db = db; 893ade86483Sdrh p->nChar = 0; 894ade86483Sdrh p->nAlloc = n; 895bb4957f8Sdrh p->mxAlloc = mx; 896b49bc86aSdrh p->accError = 0; 8975f968436Sdrh } 8985f968436Sdrh 8995f968436Sdrh /* 9005f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 9015f968436Sdrh ** %-conversion extensions. 9025f968436Sdrh */ 90317435752Sdrh char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ 90417435752Sdrh char *z; 90579158e18Sdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 906ade86483Sdrh StrAccum acc; 907bc6160b0Sdrh assert( db!=0 ); 908c0490572Sdrh sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase), 909bc6160b0Sdrh db->aLimit[SQLITE_LIMIT_LENGTH]); 910a5c1416dSdrh sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap); 911ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 912b49bc86aSdrh if( acc.accError==STRACCUM_NOMEM ){ 91317435752Sdrh db->mallocFailed = 1; 91417435752Sdrh } 91517435752Sdrh return z; 9165f968436Sdrh } 9175f968436Sdrh 9185f968436Sdrh /* 9195f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 9205f968436Sdrh ** %-conversion extensions. 9215f968436Sdrh */ 92217435752Sdrh char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ 9235f968436Sdrh va_list ap; 9245f968436Sdrh char *z; 9255f968436Sdrh va_start(ap, zFormat); 926ade86483Sdrh z = sqlite3VMPrintf(db, zFormat, ap); 9275f968436Sdrh va_end(ap); 9285f968436Sdrh return z; 9295f968436Sdrh } 9305f968436Sdrh 9315f968436Sdrh /* 93228dd479cSdrh ** Print into memory obtained from sqlite3_malloc(). Omit the internal 93328dd479cSdrh ** %-conversion extensions. 93428dd479cSdrh */ 93528dd479cSdrh char *sqlite3_vmprintf(const char *zFormat, va_list ap){ 936ade86483Sdrh char *z; 93728dd479cSdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 938ade86483Sdrh StrAccum acc; 9399ca95730Sdrh 9409ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 9419ca95730Sdrh if( zFormat==0 ){ 9429ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 9439ca95730Sdrh return 0; 9449ca95730Sdrh } 9459ca95730Sdrh #endif 946ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT 947ff1590eeSdrh if( sqlite3_initialize() ) return 0; 948ff1590eeSdrh #endif 949c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); 950f089aa45Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 951ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 952ade86483Sdrh return z; 95328dd479cSdrh } 95428dd479cSdrh 95528dd479cSdrh /* 95628dd479cSdrh ** Print into memory obtained from sqlite3_malloc()(). Omit the internal 95728dd479cSdrh ** %-conversion extensions. 958483750baSdrh */ 9596f8a503dSdanielk1977 char *sqlite3_mprintf(const char *zFormat, ...){ 960a18c5681Sdrh va_list ap; 9615f968436Sdrh char *z; 962ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT 963ff1590eeSdrh if( sqlite3_initialize() ) return 0; 964ff1590eeSdrh #endif 965a18c5681Sdrh va_start(ap, zFormat); 966b3738b6cSdrh z = sqlite3_vmprintf(zFormat, ap); 967a18c5681Sdrh va_end(ap); 9685f968436Sdrh return z; 969a18c5681Sdrh } 970a18c5681Sdrh 971a18c5681Sdrh /* 9726f8a503dSdanielk1977 ** sqlite3_snprintf() works like snprintf() except that it ignores the 97393a5c6bdSdrh ** current locale settings. This is important for SQLite because we 97493a5c6bdSdrh ** are not able to use a "," as the decimal point in place of "." as 97593a5c6bdSdrh ** specified by some locales. 976db26d4c9Sdrh ** 977db26d4c9Sdrh ** Oops: The first two arguments of sqlite3_snprintf() are backwards 978db26d4c9Sdrh ** from the snprintf() standard. Unfortunately, it is too late to change 979db26d4c9Sdrh ** this without breaking compatibility, so we just have to live with the 980db26d4c9Sdrh ** mistake. 981db26d4c9Sdrh ** 982db26d4c9Sdrh ** sqlite3_vsnprintf() is the varargs version. 98393a5c6bdSdrh */ 984db26d4c9Sdrh char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ 985db26d4c9Sdrh StrAccum acc; 986db26d4c9Sdrh if( n<=0 ) return zBuf; 9879ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 9889ca95730Sdrh if( zBuf==0 || zFormat==0 ) { 9899ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 99096c707a3Sdrh if( zBuf ) zBuf[0] = 0; 9919ca95730Sdrh return zBuf; 9929ca95730Sdrh } 9939ca95730Sdrh #endif 994c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zBuf, n, 0); 995db26d4c9Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 996db26d4c9Sdrh return sqlite3StrAccumFinish(&acc); 997db26d4c9Sdrh } 9986f8a503dSdanielk1977 char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ 9995f968436Sdrh char *z; 100093a5c6bdSdrh va_list ap; 100193a5c6bdSdrh va_start(ap,zFormat); 1002db26d4c9Sdrh z = sqlite3_vsnprintf(n, zBuf, zFormat, ap); 100393a5c6bdSdrh va_end(ap); 10045f968436Sdrh return z; 100593a5c6bdSdrh } 100693a5c6bdSdrh 10073f280701Sdrh /* 10087c0c460fSdrh ** This is the routine that actually formats the sqlite3_log() message. 10097c0c460fSdrh ** We house it in a separate routine from sqlite3_log() to avoid using 10107c0c460fSdrh ** stack space on small-stack systems when logging is disabled. 10117c0c460fSdrh ** 10127c0c460fSdrh ** sqlite3_log() must render into a static buffer. It cannot dynamically 10137c0c460fSdrh ** allocate memory because it might be called while the memory allocator 10147c0c460fSdrh ** mutex is held. 1015*a8dbd52aSdrh ** 1016*a8dbd52aSdrh ** sqlite3VXPrintf() might ask for *temporary* memory allocations for 1017*a8dbd52aSdrh ** certain format characters (%q) or for very large precisions or widths. 1018*a8dbd52aSdrh ** Care must be taken that any sqlite3_log() calls that occur while the 1019*a8dbd52aSdrh ** memory mutex is held do not use these mechanisms. 10207c0c460fSdrh */ 10217c0c460fSdrh static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){ 10227c0c460fSdrh StrAccum acc; /* String accumulator */ 1023a64fa912Sdrh char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */ 10247c0c460fSdrh 1025c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0); 10267c0c460fSdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 10277c0c460fSdrh sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode, 10287c0c460fSdrh sqlite3StrAccumFinish(&acc)); 10297c0c460fSdrh } 10307c0c460fSdrh 10317c0c460fSdrh /* 10323f280701Sdrh ** Format and write a message to the log if logging is enabled. 10333f280701Sdrh */ 1034a7564663Sdrh void sqlite3_log(int iErrCode, const char *zFormat, ...){ 10353f280701Sdrh va_list ap; /* Vararg list */ 10367c0c460fSdrh if( sqlite3GlobalConfig.xLog ){ 10373f280701Sdrh va_start(ap, zFormat); 10387c0c460fSdrh renderLogMsg(iErrCode, zFormat, ap); 10393f280701Sdrh va_end(ap); 10403f280701Sdrh } 10413f280701Sdrh } 10423f280701Sdrh 104302b0e267Smistachkin #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) 1044e54ca3feSdrh /* 1045e54ca3feSdrh ** A version of printf() that understands %lld. Used for debugging. 1046e54ca3feSdrh ** The printf() built into some versions of windows does not understand %lld 1047e54ca3feSdrh ** and segfaults if you give it a long long int. 1048e54ca3feSdrh */ 1049e54ca3feSdrh void sqlite3DebugPrintf(const char *zFormat, ...){ 1050e54ca3feSdrh va_list ap; 1051ade86483Sdrh StrAccum acc; 1052e54ca3feSdrh char zBuf[500]; 1053c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); 1054e54ca3feSdrh va_start(ap,zFormat); 1055f089aa45Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 1056e54ca3feSdrh va_end(ap); 1057bed8e7e5Sdrh sqlite3StrAccumFinish(&acc); 1058485f0039Sdrh fprintf(stdout,"%s", zBuf); 10592ac3ee97Sdrh fflush(stdout); 1060e54ca3feSdrh } 1061e54ca3feSdrh #endif 1062c7bc4fdeSdrh 10634fa4a54fSdrh 1064c7bc4fdeSdrh /* 1065c7bc4fdeSdrh ** variable-argument wrapper around sqlite3VXPrintf(). 1066c7bc4fdeSdrh */ 1067a5c1416dSdrh void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){ 1068c7bc4fdeSdrh va_list ap; 1069c7bc4fdeSdrh va_start(ap,zFormat); 1070a5c1416dSdrh sqlite3VXPrintf(p, bFlags, zFormat, ap); 1071c7bc4fdeSdrh va_end(ap); 1072c7bc4fdeSdrh } 1073