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;} 4712a8f6712Sdrh while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; } 47272b3fbc7Sdrh while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; } 47372b3fbc7Sdrh realvalue /= scale; 474af005fbcSdrh while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } 475af005fbcSdrh while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } 476af005fbcSdrh if( exp>350 ){ 4772a8f6712Sdrh bufpt = buf; 4782a8f6712Sdrh buf[0] = prefix; 4792a8f6712Sdrh memcpy(buf+(prefix!=0),"Inf",4); 4802a8f6712Sdrh length = 3+(prefix!=0); 481a18c5681Sdrh break; 482a18c5681Sdrh } 483a18c5681Sdrh } 484a18c5681Sdrh bufpt = buf; 485a18c5681Sdrh /* 486a18c5681Sdrh ** If the field type is etGENERIC, then convert to either etEXP 487a18c5681Sdrh ** or etFLOAT, as appropriate. 488a18c5681Sdrh */ 489a18c5681Sdrh if( xtype!=etFLOAT ){ 490a18c5681Sdrh realvalue += rounder; 491a18c5681Sdrh if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } 492a18c5681Sdrh } 493a18c5681Sdrh if( xtype==etGENERIC ){ 494a18c5681Sdrh flag_rtz = !flag_alternateform; 495a18c5681Sdrh if( exp<-4 || exp>precision ){ 496a18c5681Sdrh xtype = etEXP; 497a18c5681Sdrh }else{ 498a18c5681Sdrh precision = precision - exp; 499a18c5681Sdrh xtype = etFLOAT; 500a18c5681Sdrh } 501a18c5681Sdrh }else{ 50272b3fbc7Sdrh flag_rtz = flag_altform2; 503a18c5681Sdrh } 504557cc60fSdrh if( xtype==etEXP ){ 505557cc60fSdrh e2 = 0; 506557cc60fSdrh }else{ 507557cc60fSdrh e2 = exp; 508557cc60fSdrh } 50974b42275Sdrh if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){ 51074b42275Sdrh bufpt = zExtra 51174b42275Sdrh = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 ); 51259eedf79Sdrh if( bufpt==0 ){ 513a6353a3fSdrh setStrAccumError(pAccum, STRACCUM_NOMEM); 51459eedf79Sdrh return; 51559eedf79Sdrh } 51659eedf79Sdrh } 51759eedf79Sdrh zOut = bufpt; 51872b3fbc7Sdrh nsd = 16 + flag_altform2*10; 519ea678832Sdrh flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; 520557cc60fSdrh /* The sign in front of the number */ 521557cc60fSdrh if( prefix ){ 522557cc60fSdrh *(bufpt++) = prefix; 523557cc60fSdrh } 524557cc60fSdrh /* Digits prior to the decimal point */ 525557cc60fSdrh if( e2<0 ){ 526557cc60fSdrh *(bufpt++) = '0'; 527557cc60fSdrh }else{ 528557cc60fSdrh for(; e2>=0; e2--){ 529557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 530557cc60fSdrh } 531557cc60fSdrh } 532557cc60fSdrh /* The decimal point */ 533557cc60fSdrh if( flag_dp ){ 534557cc60fSdrh *(bufpt++) = '.'; 535557cc60fSdrh } 536557cc60fSdrh /* "0" digits after the decimal point but before the first 537557cc60fSdrh ** significant digit of the number */ 538af005fbcSdrh for(e2++; e2<0; precision--, e2++){ 539af005fbcSdrh assert( precision>0 ); 540a18c5681Sdrh *(bufpt++) = '0'; 541a18c5681Sdrh } 542557cc60fSdrh /* Significant digits after the decimal point */ 543557cc60fSdrh while( (precision--)>0 ){ 544557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 545a18c5681Sdrh } 546557cc60fSdrh /* Remove trailing zeros and the "." if no digits follow the "." */ 547557cc60fSdrh if( flag_rtz && flag_dp ){ 5483e9aeec0Sdrh while( bufpt[-1]=='0' ) *(--bufpt) = 0; 54959eedf79Sdrh assert( bufpt>zOut ); 5503e9aeec0Sdrh if( bufpt[-1]=='.' ){ 551557cc60fSdrh if( flag_altform2 ){ 552557cc60fSdrh *(bufpt++) = '0'; 553557cc60fSdrh }else{ 554557cc60fSdrh *(--bufpt) = 0; 555a18c5681Sdrh } 556557cc60fSdrh } 557557cc60fSdrh } 558557cc60fSdrh /* Add the "eNNN" suffix */ 55959eedf79Sdrh if( xtype==etEXP ){ 56076ff3a0eSdrh *(bufpt++) = aDigits[infop->charset]; 561557cc60fSdrh if( exp<0 ){ 562557cc60fSdrh *(bufpt++) = '-'; exp = -exp; 563557cc60fSdrh }else{ 564557cc60fSdrh *(bufpt++) = '+'; 565557cc60fSdrh } 566a18c5681Sdrh if( exp>=100 ){ 567ea678832Sdrh *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */ 568a18c5681Sdrh exp %= 100; 569a18c5681Sdrh } 570ea678832Sdrh *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */ 571ea678832Sdrh *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */ 572a18c5681Sdrh } 573557cc60fSdrh *bufpt = 0; 574557cc60fSdrh 575a18c5681Sdrh /* The converted number is in buf[] and zero terminated. Output it. 576a18c5681Sdrh ** Note that the number is in the usual order, not reversed as with 577a18c5681Sdrh ** integer conversions. */ 57859eedf79Sdrh length = (int)(bufpt-zOut); 57959eedf79Sdrh bufpt = zOut; 580a18c5681Sdrh 581a18c5681Sdrh /* Special case: Add leading zeros if the flag_zeropad flag is 582a18c5681Sdrh ** set and we are not left justified */ 583a18c5681Sdrh if( flag_zeropad && !flag_leftjustify && length < width){ 584a18c5681Sdrh int i; 585a18c5681Sdrh int nPad = width - length; 586a18c5681Sdrh for(i=width; i>=nPad; i--){ 587a18c5681Sdrh bufpt[i] = bufpt[i-nPad]; 588a18c5681Sdrh } 589a18c5681Sdrh i = prefix!=0; 590a18c5681Sdrh while( nPad-- ) bufpt[i++] = '0'; 591a18c5681Sdrh length = width; 592a18c5681Sdrh } 59369ef7036Sdrh #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */ 594a18c5681Sdrh break; 595a18c5681Sdrh case etSIZE: 596fc6ee9dfSdrh if( !bArgList ){ 597fc6ee9dfSdrh *(va_arg(ap,int*)) = pAccum->nChar; 598fc6ee9dfSdrh } 599a18c5681Sdrh length = width = 0; 600a18c5681Sdrh break; 601a18c5681Sdrh case etPERCENT: 602a18c5681Sdrh buf[0] = '%'; 603a18c5681Sdrh bufpt = buf; 604a18c5681Sdrh length = 1; 605a18c5681Sdrh break; 606a18c5681Sdrh case etCHARX: 607a5c1416dSdrh if( bArgList ){ 608fc6ee9dfSdrh bufpt = getTextArg(pArgList); 609fc6ee9dfSdrh c = bufpt ? bufpt[0] : 0; 610a5c1416dSdrh }else{ 611ea678832Sdrh c = va_arg(ap,int); 612a5c1416dSdrh } 613af8f513fSdrh if( precision>1 ){ 614af8f513fSdrh width -= precision-1; 615af8f513fSdrh if( width>1 && !flag_leftjustify ){ 616af8f513fSdrh sqlite3AppendChar(pAccum, width-1, ' '); 617af8f513fSdrh width = 0; 618a18c5681Sdrh } 619af8f513fSdrh sqlite3AppendChar(pAccum, precision-1, c); 620af8f513fSdrh } 621af8f513fSdrh length = 1; 622af8f513fSdrh buf[0] = c; 623a18c5681Sdrh bufpt = buf; 624a18c5681Sdrh break; 625a18c5681Sdrh case etSTRING: 626d93d8a81Sdrh case etDYNSTRING: 627a5c1416dSdrh if( bArgList ){ 628a5c1416dSdrh bufpt = getTextArg(pArgList); 6292a8f6712Sdrh xtype = etSTRING; 630a5c1416dSdrh }else{ 631cb485882Sdrh bufpt = va_arg(ap,char*); 632a5c1416dSdrh } 633d93d8a81Sdrh if( bufpt==0 ){ 634d93d8a81Sdrh bufpt = ""; 6352a8f6712Sdrh }else if( xtype==etDYNSTRING ){ 636d93d8a81Sdrh zExtra = bufpt; 637d93d8a81Sdrh } 638e509094bSdrh if( precision>=0 ){ 639e509094bSdrh for(length=0; length<precision && bufpt[length]; length++){} 640e509094bSdrh }else{ 641ea678832Sdrh length = sqlite3Strlen30(bufpt); 642e509094bSdrh } 643a18c5681Sdrh break; 6442a8f6712Sdrh case etSQLESCAPE: /* Escape ' characters */ 6452a8f6712Sdrh case etSQLESCAPE2: /* Escape ' and enclose in '...' */ 6462a8f6712Sdrh case etSQLESCAPE3: { /* Escape " characters */ 6478965b50eSdrh int i, j, k, n, isnull; 6484794f735Sdrh int needQuote; 649ea678832Sdrh char ch; 650f3b863edSdanielk1977 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */ 651a5c1416dSdrh char *escarg; 652a5c1416dSdrh 653a5c1416dSdrh if( bArgList ){ 654a5c1416dSdrh escarg = getTextArg(pArgList); 655a5c1416dSdrh }else{ 656a5c1416dSdrh escarg = va_arg(ap,char*); 657a5c1416dSdrh } 658f0113000Sdanielk1977 isnull = escarg==0; 659f0113000Sdanielk1977 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); 6608965b50eSdrh k = precision; 66160d4a304Sdan for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ 662f3b863edSdanielk1977 if( ch==q ) n++; 663a18c5681Sdrh } 6644794f735Sdrh needQuote = !isnull && xtype==etSQLESCAPE2; 6652a8f6712Sdrh n += i + 3; 666a18c5681Sdrh if( n>etBUFSIZE ){ 667e5ae5735Sdrh bufpt = zExtra = sqlite3Malloc( n ); 668d164fd34Sdrh if( bufpt==0 ){ 669a6353a3fSdrh setStrAccumError(pAccum, STRACCUM_NOMEM); 670d164fd34Sdrh return; 671d164fd34Sdrh } 672a18c5681Sdrh }else{ 673a18c5681Sdrh bufpt = buf; 674a18c5681Sdrh } 6750cfcf3fbSchw j = 0; 676f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 6778965b50eSdrh k = i; 6788965b50eSdrh for(i=0; i<k; i++){ 6798965b50eSdrh bufpt[j++] = ch = escarg[i]; 680f3b863edSdanielk1977 if( ch==q ) bufpt[j++] = ch; 681a18c5681Sdrh } 682f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 683a18c5681Sdrh bufpt[j] = 0; 684a18c5681Sdrh length = j; 6858965b50eSdrh /* The precision in %q and %Q means how many input characters to 6868965b50eSdrh ** consume, not the length of the output... 6878965b50eSdrh ** if( precision>=0 && precision<length ) length = precision; */ 688a18c5681Sdrh break; 6895eba8c09Sdrh } 6905f968436Sdrh case etTOKEN: { 6915f968436Sdrh Token *pToken = va_arg(ap, Token*); 692a5c1416dSdrh assert( bArgList==0 ); 693a9ab481fSdrh if( pToken && pToken->n ){ 694ade86483Sdrh sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n); 695ad6d9460Sdrh } 6965f968436Sdrh length = width = 0; 6975f968436Sdrh break; 6985f968436Sdrh } 6995f968436Sdrh case etSRCLIST: { 7005f968436Sdrh SrcList *pSrc = va_arg(ap, SrcList*); 7015f968436Sdrh int k = va_arg(ap, int); 7025f968436Sdrh struct SrcList_item *pItem = &pSrc->a[k]; 703a5c1416dSdrh assert( bArgList==0 ); 7045f968436Sdrh assert( k>=0 && k<pSrc->nSrc ); 70593a960a0Sdrh if( pItem->zDatabase ){ 706a6353a3fSdrh sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase); 707ade86483Sdrh sqlite3StrAccumAppend(pAccum, ".", 1); 7085f968436Sdrh } 709a6353a3fSdrh sqlite3StrAccumAppendAll(pAccum, pItem->zName); 7105f968436Sdrh length = width = 0; 7115f968436Sdrh break; 7125f968436Sdrh } 713874ba04cSdrh default: { 714874ba04cSdrh assert( xtype==etINVALID ); 715874ba04cSdrh return; 716874ba04cSdrh } 717a18c5681Sdrh }/* End switch over the format type */ 718a18c5681Sdrh /* 719a18c5681Sdrh ** The text of the conversion is pointed to by "bufpt" and is 720a18c5681Sdrh ** "length" characters long. The field width is "width". Do 721a18c5681Sdrh ** the output. 722a18c5681Sdrh */ 723a70a073bSdrh width -= length; 724af8f513fSdrh if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); 725ade86483Sdrh sqlite3StrAccumAppend(pAccum, bufpt, length); 726af8f513fSdrh if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); 727a70a073bSdrh 728af8f513fSdrh if( zExtra ){ 729*96ceaf86Sdrh sqlite3DbFree(pAccum->db, zExtra); 730af8f513fSdrh zExtra = 0; 731af8f513fSdrh } 732a18c5681Sdrh }/* End for loop over the format string */ 733a18c5681Sdrh } /* End of function */ 734a18c5681Sdrh 735a18c5681Sdrh /* 736a70a073bSdrh ** Enlarge the memory allocation on a StrAccum object so that it is 737a70a073bSdrh ** able to accept at least N more bytes of text. 738a70a073bSdrh ** 739a70a073bSdrh ** Return the number of bytes of text that StrAccum is able to accept 740a70a073bSdrh ** after the attempted enlargement. The value returned might be zero. 741a18c5681Sdrh */ 742a70a073bSdrh static int sqlite3StrAccumEnlarge(StrAccum *p, int N){ 743a6353a3fSdrh char *zNew; 744a30d22a7Sdrh assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */ 745b49bc86aSdrh if( p->accError ){ 746b49bc86aSdrh testcase(p->accError==STRACCUM_TOOBIG); 747b49bc86aSdrh testcase(p->accError==STRACCUM_NOMEM); 748a70a073bSdrh return 0; 749ade86483Sdrh } 750c0490572Sdrh if( p->mxAlloc==0 ){ 751ade86483Sdrh N = p->nAlloc - p->nChar - 1; 752a6353a3fSdrh setStrAccumError(p, STRACCUM_TOOBIG); 753a70a073bSdrh return N; 754a18c5681Sdrh }else{ 755a9ef7097Sdan char *zOld = (p->zText==p->zBase ? 0 : p->zText); 75693a960a0Sdrh i64 szNew = p->nChar; 757b1a6c3c1Sdrh szNew += N + 1; 7587b4d780bSdrh if( szNew+p->nChar<=p->mxAlloc ){ 7597b4d780bSdrh /* Force exponential buffer size growth as long as it does not overflow, 7607b4d780bSdrh ** to avoid having to call this routine too often */ 7617b4d780bSdrh szNew += p->nChar; 7627b4d780bSdrh } 763b1a6c3c1Sdrh if( szNew > p->mxAlloc ){ 764ade86483Sdrh sqlite3StrAccumReset(p); 765a6353a3fSdrh setStrAccumError(p, STRACCUM_TOOBIG); 766a70a073bSdrh return 0; 767b1a6c3c1Sdrh }else{ 768ea678832Sdrh p->nAlloc = (int)szNew; 769a18c5681Sdrh } 770c0490572Sdrh if( p->db ){ 771a9ef7097Sdan zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); 772b975598eSdrh }else{ 773f3cdcdccSdrh zNew = sqlite3_realloc64(zOld, p->nAlloc); 774b975598eSdrh } 77553f733c7Sdrh if( zNew ){ 7767ef4d1c4Sdrh assert( p->zText!=0 || p->nChar==0 ); 777b07028f7Sdrh if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); 778ade86483Sdrh p->zText = zNew; 7797f5a7ecdSdrh p->nAlloc = sqlite3DbMallocSize(p->db, zNew); 780ade86483Sdrh }else{ 781ade86483Sdrh sqlite3StrAccumReset(p); 782a6353a3fSdrh setStrAccumError(p, STRACCUM_NOMEM); 783a70a073bSdrh return 0; 784a70a073bSdrh } 785a70a073bSdrh } 786a70a073bSdrh return N; 787a70a073bSdrh } 788a70a073bSdrh 789a70a073bSdrh /* 790af8f513fSdrh ** Append N copies of character c to the given string buffer. 791a70a073bSdrh */ 792af8f513fSdrh void sqlite3AppendChar(StrAccum *p, int N, char c){ 793a30d22a7Sdrh testcase( p->nChar + (i64)N > 0x7fffffff ); 794a30d22a7Sdrh if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ 795a30d22a7Sdrh return; 796a30d22a7Sdrh } 797af8f513fSdrh while( (N--)>0 ) p->zText[p->nChar++] = c; 798a70a073bSdrh } 799a70a073bSdrh 800a70a073bSdrh /* 801a70a073bSdrh ** The StrAccum "p" is not large enough to accept N new bytes of z[]. 802a70a073bSdrh ** So enlarge if first, then do the append. 803a70a073bSdrh ** 804a70a073bSdrh ** This is a helper routine to sqlite3StrAccumAppend() that does special-case 805a70a073bSdrh ** work (enlarging the buffer) using tail recursion, so that the 806a70a073bSdrh ** sqlite3StrAccumAppend() routine can use fast calling semantics. 807a70a073bSdrh */ 808172087fbSdrh static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){ 809a70a073bSdrh N = sqlite3StrAccumEnlarge(p, N); 810a70a073bSdrh if( N>0 ){ 811a70a073bSdrh memcpy(&p->zText[p->nChar], z, N); 812a70a073bSdrh p->nChar += N; 813a70a073bSdrh } 814a70a073bSdrh } 815a70a073bSdrh 816a70a073bSdrh /* 817a70a073bSdrh ** Append N bytes of text from z to the StrAccum object. Increase the 818a70a073bSdrh ** size of the memory allocation for StrAccum if necessary. 819a70a073bSdrh */ 820a70a073bSdrh void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){ 8213457338cSdrh assert( z!=0 || N==0 ); 822a70a073bSdrh assert( p->zText!=0 || p->nChar==0 || p->accError ); 823a70a073bSdrh assert( N>=0 ); 824a70a073bSdrh assert( p->accError==0 || p->nAlloc==0 ); 825a70a073bSdrh if( p->nChar+N >= p->nAlloc ){ 826a70a073bSdrh enlargeAndAppend(p,z,N); 827172087fbSdrh }else{ 828b07028f7Sdrh assert( p->zText ); 829ade86483Sdrh p->nChar += N; 830172087fbSdrh memcpy(&p->zText[p->nChar-N], z, N); 831172087fbSdrh } 832483750baSdrh } 833483750baSdrh 834483750baSdrh /* 835a6353a3fSdrh ** Append the complete text of zero-terminated string z[] to the p string. 836a6353a3fSdrh */ 837a6353a3fSdrh void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){ 83853cd9646Smistachkin sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z)); 839a6353a3fSdrh } 840a6353a3fSdrh 841a6353a3fSdrh 842a6353a3fSdrh /* 843ade86483Sdrh ** Finish off a string by making sure it is zero-terminated. 844ade86483Sdrh ** Return a pointer to the resulting string. Return a NULL 845ade86483Sdrh ** pointer if any kind of error was encountered. 8465f968436Sdrh */ 847ade86483Sdrh char *sqlite3StrAccumFinish(StrAccum *p){ 848ade86483Sdrh if( p->zText ){ 849ade86483Sdrh p->zText[p->nChar] = 0; 850c0490572Sdrh if( p->mxAlloc>0 && p->zText==p->zBase ){ 851633e6d57Sdrh p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); 852ade86483Sdrh if( p->zText ){ 853ade86483Sdrh memcpy(p->zText, p->zBase, p->nChar+1); 854ade86483Sdrh }else{ 855a6353a3fSdrh setStrAccumError(p, STRACCUM_NOMEM); 856ade86483Sdrh } 857ade86483Sdrh } 858ade86483Sdrh } 859ade86483Sdrh return p->zText; 860ade86483Sdrh } 861ade86483Sdrh 862ade86483Sdrh /* 863ade86483Sdrh ** Reset an StrAccum string. Reclaim all malloced memory. 864ade86483Sdrh */ 865ade86483Sdrh void sqlite3StrAccumReset(StrAccum *p){ 866ade86483Sdrh if( p->zText!=p->zBase ){ 867633e6d57Sdrh sqlite3DbFree(p->db, p->zText); 868ade86483Sdrh } 869f089aa45Sdrh p->zText = 0; 870ade86483Sdrh } 871ade86483Sdrh 872ade86483Sdrh /* 873c0490572Sdrh ** Initialize a string accumulator. 874c0490572Sdrh ** 875c0490572Sdrh ** p: The accumulator to be initialized. 876c0490572Sdrh ** db: Pointer to a database connection. May be NULL. Lookaside 877c0490572Sdrh ** memory is used if not NULL. db->mallocFailed is set appropriately 878c0490572Sdrh ** when not NULL. 879c0490572Sdrh ** zBase: An initial buffer. May be NULL in which case the initial buffer 880c0490572Sdrh ** is malloced. 881c0490572Sdrh ** n: Size of zBase in bytes. If total space requirements never exceed 882c0490572Sdrh ** n then no memory allocations ever occur. 883c0490572Sdrh ** mx: Maximum number of bytes to accumulate. If mx==0 then no memory 884c0490572Sdrh ** allocations will ever occur. 885ade86483Sdrh */ 886c0490572Sdrh void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){ 887ade86483Sdrh p->zText = p->zBase = zBase; 888c0490572Sdrh p->db = db; 889ade86483Sdrh p->nChar = 0; 890ade86483Sdrh p->nAlloc = n; 891bb4957f8Sdrh p->mxAlloc = mx; 892b49bc86aSdrh p->accError = 0; 8935f968436Sdrh } 8945f968436Sdrh 8955f968436Sdrh /* 8965f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 8975f968436Sdrh ** %-conversion extensions. 8985f968436Sdrh */ 89917435752Sdrh char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ 90017435752Sdrh char *z; 90179158e18Sdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 902ade86483Sdrh StrAccum acc; 903bc6160b0Sdrh assert( db!=0 ); 904c0490572Sdrh sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase), 905bc6160b0Sdrh db->aLimit[SQLITE_LIMIT_LENGTH]); 906a5c1416dSdrh sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap); 907ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 908b49bc86aSdrh if( acc.accError==STRACCUM_NOMEM ){ 90917435752Sdrh db->mallocFailed = 1; 91017435752Sdrh } 91117435752Sdrh return z; 9125f968436Sdrh } 9135f968436Sdrh 9145f968436Sdrh /* 9155f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 9165f968436Sdrh ** %-conversion extensions. 9175f968436Sdrh */ 91817435752Sdrh char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ 9195f968436Sdrh va_list ap; 9205f968436Sdrh char *z; 9215f968436Sdrh va_start(ap, zFormat); 922ade86483Sdrh z = sqlite3VMPrintf(db, zFormat, ap); 9235f968436Sdrh va_end(ap); 9245f968436Sdrh return z; 9255f968436Sdrh } 9265f968436Sdrh 9275f968436Sdrh /* 92828dd479cSdrh ** Print into memory obtained from sqlite3_malloc(). Omit the internal 92928dd479cSdrh ** %-conversion extensions. 93028dd479cSdrh */ 93128dd479cSdrh char *sqlite3_vmprintf(const char *zFormat, va_list ap){ 932ade86483Sdrh char *z; 93328dd479cSdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 934ade86483Sdrh StrAccum acc; 9359ca95730Sdrh 9369ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 9379ca95730Sdrh if( zFormat==0 ){ 9389ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 9399ca95730Sdrh return 0; 9409ca95730Sdrh } 9419ca95730Sdrh #endif 942ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT 943ff1590eeSdrh if( sqlite3_initialize() ) return 0; 944ff1590eeSdrh #endif 945c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); 946f089aa45Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 947ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 948ade86483Sdrh return z; 94928dd479cSdrh } 95028dd479cSdrh 95128dd479cSdrh /* 95228dd479cSdrh ** Print into memory obtained from sqlite3_malloc()(). Omit the internal 95328dd479cSdrh ** %-conversion extensions. 954483750baSdrh */ 9556f8a503dSdanielk1977 char *sqlite3_mprintf(const char *zFormat, ...){ 956a18c5681Sdrh va_list ap; 9575f968436Sdrh char *z; 958ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT 959ff1590eeSdrh if( sqlite3_initialize() ) return 0; 960ff1590eeSdrh #endif 961a18c5681Sdrh va_start(ap, zFormat); 962b3738b6cSdrh z = sqlite3_vmprintf(zFormat, ap); 963a18c5681Sdrh va_end(ap); 9645f968436Sdrh return z; 965a18c5681Sdrh } 966a18c5681Sdrh 967a18c5681Sdrh /* 9686f8a503dSdanielk1977 ** sqlite3_snprintf() works like snprintf() except that it ignores the 96993a5c6bdSdrh ** current locale settings. This is important for SQLite because we 97093a5c6bdSdrh ** are not able to use a "," as the decimal point in place of "." as 97193a5c6bdSdrh ** specified by some locales. 972db26d4c9Sdrh ** 973db26d4c9Sdrh ** Oops: The first two arguments of sqlite3_snprintf() are backwards 974db26d4c9Sdrh ** from the snprintf() standard. Unfortunately, it is too late to change 975db26d4c9Sdrh ** this without breaking compatibility, so we just have to live with the 976db26d4c9Sdrh ** mistake. 977db26d4c9Sdrh ** 978db26d4c9Sdrh ** sqlite3_vsnprintf() is the varargs version. 97993a5c6bdSdrh */ 980db26d4c9Sdrh char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ 981db26d4c9Sdrh StrAccum acc; 982db26d4c9Sdrh if( n<=0 ) return zBuf; 9839ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 9849ca95730Sdrh if( zBuf==0 || zFormat==0 ) { 9859ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 98696c707a3Sdrh if( zBuf ) zBuf[0] = 0; 9879ca95730Sdrh return zBuf; 9889ca95730Sdrh } 9899ca95730Sdrh #endif 990c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zBuf, n, 0); 991db26d4c9Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 992db26d4c9Sdrh return sqlite3StrAccumFinish(&acc); 993db26d4c9Sdrh } 9946f8a503dSdanielk1977 char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ 9955f968436Sdrh char *z; 99693a5c6bdSdrh va_list ap; 99793a5c6bdSdrh va_start(ap,zFormat); 998db26d4c9Sdrh z = sqlite3_vsnprintf(n, zBuf, zFormat, ap); 99993a5c6bdSdrh va_end(ap); 10005f968436Sdrh return z; 100193a5c6bdSdrh } 100293a5c6bdSdrh 10033f280701Sdrh /* 10047c0c460fSdrh ** This is the routine that actually formats the sqlite3_log() message. 10057c0c460fSdrh ** We house it in a separate routine from sqlite3_log() to avoid using 10067c0c460fSdrh ** stack space on small-stack systems when logging is disabled. 10077c0c460fSdrh ** 10087c0c460fSdrh ** sqlite3_log() must render into a static buffer. It cannot dynamically 10097c0c460fSdrh ** allocate memory because it might be called while the memory allocator 10107c0c460fSdrh ** mutex is held. 1011a8dbd52aSdrh ** 1012a8dbd52aSdrh ** sqlite3VXPrintf() might ask for *temporary* memory allocations for 1013a8dbd52aSdrh ** certain format characters (%q) or for very large precisions or widths. 1014a8dbd52aSdrh ** Care must be taken that any sqlite3_log() calls that occur while the 1015a8dbd52aSdrh ** memory mutex is held do not use these mechanisms. 10167c0c460fSdrh */ 10177c0c460fSdrh static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){ 10187c0c460fSdrh StrAccum acc; /* String accumulator */ 1019a64fa912Sdrh char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */ 10207c0c460fSdrh 1021c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0); 10227c0c460fSdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 10237c0c460fSdrh sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode, 10247c0c460fSdrh sqlite3StrAccumFinish(&acc)); 10257c0c460fSdrh } 10267c0c460fSdrh 10277c0c460fSdrh /* 10283f280701Sdrh ** Format and write a message to the log if logging is enabled. 10293f280701Sdrh */ 1030a7564663Sdrh void sqlite3_log(int iErrCode, const char *zFormat, ...){ 10313f280701Sdrh va_list ap; /* Vararg list */ 10327c0c460fSdrh if( sqlite3GlobalConfig.xLog ){ 10333f280701Sdrh va_start(ap, zFormat); 10347c0c460fSdrh renderLogMsg(iErrCode, zFormat, ap); 10353f280701Sdrh va_end(ap); 10363f280701Sdrh } 10373f280701Sdrh } 10383f280701Sdrh 103902b0e267Smistachkin #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) 1040e54ca3feSdrh /* 1041e54ca3feSdrh ** A version of printf() that understands %lld. Used for debugging. 1042e54ca3feSdrh ** The printf() built into some versions of windows does not understand %lld 1043e54ca3feSdrh ** and segfaults if you give it a long long int. 1044e54ca3feSdrh */ 1045e54ca3feSdrh void sqlite3DebugPrintf(const char *zFormat, ...){ 1046e54ca3feSdrh va_list ap; 1047ade86483Sdrh StrAccum acc; 1048e54ca3feSdrh char zBuf[500]; 1049c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); 1050e54ca3feSdrh va_start(ap,zFormat); 1051f089aa45Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 1052e54ca3feSdrh va_end(ap); 1053bed8e7e5Sdrh sqlite3StrAccumFinish(&acc); 1054485f0039Sdrh fprintf(stdout,"%s", zBuf); 10552ac3ee97Sdrh fflush(stdout); 1056e54ca3feSdrh } 1057e54ca3feSdrh #endif 1058c7bc4fdeSdrh 10594fa4a54fSdrh 1060c7bc4fdeSdrh /* 1061d37bea5bSdrh ** variable-argument wrapper around sqlite3VXPrintf(). The bFlags argument 1062d37bea5bSdrh ** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats. 1063c7bc4fdeSdrh */ 1064a5c1416dSdrh void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){ 1065c7bc4fdeSdrh va_list ap; 1066c7bc4fdeSdrh va_start(ap,zFormat); 1067a5c1416dSdrh sqlite3VXPrintf(p, bFlags, zFormat, ap); 1068c7bc4fdeSdrh va_end(ap); 1069c7bc4fdeSdrh } 1070