1 /* 2 ** The "printf" code that follows dates from the 1980's. It is in 3 ** the public domain. 4 ** 5 ************************************************************************** 6 ** 7 ** This file contains code for a set of "printf"-like routines. These 8 ** routines format strings much like the printf() from the standard C 9 ** library, though the implementation here has enhancements to support 10 ** SQLite. 11 */ 12 #include "sqliteInt.h" 13 14 /* 15 ** Conversion types fall into various categories as defined by the 16 ** following enumeration. 17 */ 18 #define etRADIX 0 /* non-decimal integer types. %x %o */ 19 #define etFLOAT 1 /* Floating point. %f */ 20 #define etEXP 2 /* Exponentional notation. %e and %E */ 21 #define etGENERIC 3 /* Floating or exponential, depending on exponent. %g */ 22 #define etSIZE 4 /* Return number of characters processed so far. %n */ 23 #define etSTRING 5 /* Strings. %s */ 24 #define etDYNSTRING 6 /* Dynamically allocated strings. %z */ 25 #define etPERCENT 7 /* Percent symbol. %% */ 26 #define etCHARX 8 /* Characters. %c */ 27 /* The rest are extensions, not normally found in printf() */ 28 #define etSQLESCAPE 9 /* Strings with '\'' doubled. %q */ 29 #define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '', 30 NULL pointers replaced by SQL NULL. %Q */ 31 #define etTOKEN 11 /* a pointer to a Token structure */ 32 #define etSRCLIST 12 /* a pointer to a SrcList */ 33 #define etPOINTER 13 /* The %p conversion */ 34 #define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */ 35 #define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */ 36 #define etDECIMAL 16 /* %d or %u, but not %x, %o */ 37 38 #define etINVALID 17 /* Any unrecognized conversion type */ 39 40 41 /* 42 ** An "etByte" is an 8-bit unsigned value. 43 */ 44 typedef unsigned char etByte; 45 46 /* 47 ** Each builtin conversion character (ex: the 'd' in "%d") is described 48 ** by an instance of the following structure 49 */ 50 typedef struct et_info { /* Information about each format field */ 51 char fmttype; /* The format field code letter */ 52 etByte base; /* The base for radix conversion */ 53 etByte flags; /* One or more of FLAG_ constants below */ 54 etByte type; /* Conversion paradigm */ 55 etByte charset; /* Offset into aDigits[] of the digits string */ 56 etByte prefix; /* Offset into aPrefix[] of the prefix string */ 57 } et_info; 58 59 /* 60 ** Allowed values for et_info.flags 61 */ 62 #define FLAG_SIGNED 1 /* True if the value to convert is signed */ 63 #define FLAG_STRING 4 /* Allow infinite precision */ 64 65 66 /* 67 ** The following table is searched linearly, so it is good to put the 68 ** most frequently used conversion types first. 69 */ 70 static const char aDigits[] = "0123456789ABCDEF0123456789abcdef"; 71 static const char aPrefix[] = "-x0\000X0"; 72 static const et_info fmtinfo[] = { 73 { 'd', 10, 1, etDECIMAL, 0, 0 }, 74 { 's', 0, 4, etSTRING, 0, 0 }, 75 { 'g', 0, 1, etGENERIC, 30, 0 }, 76 { 'z', 0, 4, etDYNSTRING, 0, 0 }, 77 { 'q', 0, 4, etSQLESCAPE, 0, 0 }, 78 { 'Q', 0, 4, etSQLESCAPE2, 0, 0 }, 79 { 'w', 0, 4, etSQLESCAPE3, 0, 0 }, 80 { 'c', 0, 0, etCHARX, 0, 0 }, 81 { 'o', 8, 0, etRADIX, 0, 2 }, 82 { 'u', 10, 0, etDECIMAL, 0, 0 }, 83 { 'x', 16, 0, etRADIX, 16, 1 }, 84 { 'X', 16, 0, etRADIX, 0, 4 }, 85 #ifndef SQLITE_OMIT_FLOATING_POINT 86 { 'f', 0, 1, etFLOAT, 0, 0 }, 87 { 'e', 0, 1, etEXP, 30, 0 }, 88 { 'E', 0, 1, etEXP, 14, 0 }, 89 { 'G', 0, 1, etGENERIC, 14, 0 }, 90 #endif 91 { 'i', 10, 1, etDECIMAL, 0, 0 }, 92 { 'n', 0, 0, etSIZE, 0, 0 }, 93 { '%', 0, 0, etPERCENT, 0, 0 }, 94 { 'p', 16, 0, etPOINTER, 0, 1 }, 95 96 /* All the rest are undocumented and are for internal use only */ 97 { 'T', 0, 0, etTOKEN, 0, 0 }, 98 { 'S', 0, 0, etSRCLIST, 0, 0 }, 99 { 'r', 10, 1, etORDINAL, 0, 0 }, 100 }; 101 102 /* 103 ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point 104 ** conversions will work. 105 */ 106 #ifndef SQLITE_OMIT_FLOATING_POINT 107 /* 108 ** "*val" is a double such that 0.1 <= *val < 10.0 109 ** Return the ascii code for the leading digit of *val, then 110 ** multiply "*val" by 10.0 to renormalize. 111 ** 112 ** Example: 113 ** input: *val = 3.14159 114 ** output: *val = 1.4159 function return = '3' 115 ** 116 ** The counter *cnt is incremented each time. After counter exceeds 117 ** 16 (the number of significant digits in a 64-bit float) '0' is 118 ** always returned. 119 */ 120 static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ 121 int digit; 122 LONGDOUBLE_TYPE d; 123 if( (*cnt)<=0 ) return '0'; 124 (*cnt)--; 125 digit = (int)*val; 126 d = digit; 127 digit += '0'; 128 *val = (*val - d)*10.0; 129 return (char)digit; 130 } 131 #endif /* SQLITE_OMIT_FLOATING_POINT */ 132 133 /* 134 ** Set the StrAccum object to an error mode. 135 */ 136 static void setStrAccumError(StrAccum *p, u8 eError){ 137 assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG ); 138 p->accError = eError; 139 if( p->mxAlloc ) sqlite3_str_reset(p); 140 } 141 142 /* 143 ** Extra argument values from a PrintfArguments object 144 */ 145 static sqlite3_int64 getIntArg(PrintfArguments *p){ 146 if( p->nArg<=p->nUsed ) return 0; 147 return sqlite3_value_int64(p->apArg[p->nUsed++]); 148 } 149 static double getDoubleArg(PrintfArguments *p){ 150 if( p->nArg<=p->nUsed ) return 0.0; 151 return sqlite3_value_double(p->apArg[p->nUsed++]); 152 } 153 static char *getTextArg(PrintfArguments *p){ 154 if( p->nArg<=p->nUsed ) return 0; 155 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]); 156 } 157 158 /* 159 ** Allocate memory for a temporary buffer needed for printf rendering. 160 ** 161 ** If the requested size of the temp buffer is larger than the size 162 ** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error. 163 ** Do the size check before the memory allocation to prevent rogue 164 ** SQL from requesting large allocations using the precision or width 165 ** field of the printf() function. 166 */ 167 static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){ 168 char *z; 169 if( pAccum->accError ) return 0; 170 if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){ 171 setStrAccumError(pAccum, SQLITE_TOOBIG); 172 return 0; 173 } 174 z = sqlite3DbMallocRaw(pAccum->db, n); 175 if( z==0 ){ 176 setStrAccumError(pAccum, SQLITE_NOMEM); 177 } 178 return z; 179 } 180 181 /* 182 ** On machines with a small stack size, you can redefine the 183 ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired. 184 */ 185 #ifndef SQLITE_PRINT_BUF_SIZE 186 # define SQLITE_PRINT_BUF_SIZE 70 187 #endif 188 #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ 189 190 /* 191 ** Render a string given by "fmt" into the StrAccum object. 192 */ 193 void sqlite3_str_vappendf( 194 sqlite3_str *pAccum, /* Accumulate results here */ 195 const char *fmt, /* Format string */ 196 va_list ap /* arguments */ 197 ){ 198 int c; /* Next character in the format string */ 199 char *bufpt; /* Pointer to the conversion buffer */ 200 int precision; /* Precision of the current field */ 201 int length; /* Length of the field */ 202 int idx; /* A general purpose loop counter */ 203 int width; /* Width of the current field */ 204 etByte flag_leftjustify; /* True if "-" flag is present */ 205 etByte flag_prefix; /* '+' or ' ' or 0 for prefix */ 206 etByte flag_alternateform; /* True if "#" flag is present */ 207 etByte flag_altform2; /* True if "!" flag is present */ 208 etByte flag_zeropad; /* True if field width constant starts with zero */ 209 etByte flag_long; /* 1 for the "l" flag, 2 for "ll", 0 by default */ 210 etByte done; /* Loop termination flag */ 211 etByte cThousand; /* Thousands separator for %d and %u */ 212 etByte xtype = etINVALID; /* Conversion paradigm */ 213 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */ 214 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ 215 sqlite_uint64 longvalue; /* Value for integer types */ 216 LONGDOUBLE_TYPE realvalue; /* Value for real types */ 217 const et_info *infop; /* Pointer to the appropriate info structure */ 218 char *zOut; /* Rendering buffer */ 219 int nOut; /* Size of the rendering buffer */ 220 char *zExtra = 0; /* Malloced memory used by some conversion */ 221 #ifndef SQLITE_OMIT_FLOATING_POINT 222 int exp, e2; /* exponent of real numbers */ 223 int nsd; /* Number of significant digits returned */ 224 double rounder; /* Used for rounding floating point values */ 225 etByte flag_dp; /* True if decimal point should be shown */ 226 etByte flag_rtz; /* True if trailing zeros should be removed */ 227 #endif 228 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */ 229 char buf[etBUFSIZE]; /* Conversion buffer */ 230 231 /* pAccum never starts out with an empty buffer that was obtained from 232 ** malloc(). This precondition is required by the mprintf("%z...") 233 ** optimization. */ 234 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); 235 236 bufpt = 0; 237 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){ 238 pArgList = va_arg(ap, PrintfArguments*); 239 bArgList = 1; 240 }else{ 241 bArgList = 0; 242 } 243 for(; (c=(*fmt))!=0; ++fmt){ 244 if( c!='%' ){ 245 bufpt = (char *)fmt; 246 #if HAVE_STRCHRNUL 247 fmt = strchrnul(fmt, '%'); 248 #else 249 do{ fmt++; }while( *fmt && *fmt != '%' ); 250 #endif 251 sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt)); 252 if( *fmt==0 ) break; 253 } 254 if( (c=(*++fmt))==0 ){ 255 sqlite3_str_append(pAccum, "%", 1); 256 break; 257 } 258 /* Find out what flags are present */ 259 flag_leftjustify = flag_prefix = cThousand = 260 flag_alternateform = flag_altform2 = flag_zeropad = 0; 261 done = 0; 262 width = 0; 263 flag_long = 0; 264 precision = -1; 265 do{ 266 switch( c ){ 267 case '-': flag_leftjustify = 1; break; 268 case '+': flag_prefix = '+'; break; 269 case ' ': flag_prefix = ' '; break; 270 case '#': flag_alternateform = 1; break; 271 case '!': flag_altform2 = 1; break; 272 case '0': flag_zeropad = 1; break; 273 case ',': cThousand = ','; break; 274 default: done = 1; break; 275 case 'l': { 276 flag_long = 1; 277 c = *++fmt; 278 if( c=='l' ){ 279 c = *++fmt; 280 flag_long = 2; 281 } 282 done = 1; 283 break; 284 } 285 case '1': case '2': case '3': case '4': case '5': 286 case '6': case '7': case '8': case '9': { 287 unsigned wx = c - '0'; 288 while( (c = *++fmt)>='0' && c<='9' ){ 289 wx = wx*10 + c - '0'; 290 } 291 testcase( wx>0x7fffffff ); 292 width = wx & 0x7fffffff; 293 #ifdef SQLITE_PRINTF_PRECISION_LIMIT 294 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){ 295 width = SQLITE_PRINTF_PRECISION_LIMIT; 296 } 297 #endif 298 if( c!='.' && c!='l' ){ 299 done = 1; 300 }else{ 301 fmt--; 302 } 303 break; 304 } 305 case '*': { 306 if( bArgList ){ 307 width = (int)getIntArg(pArgList); 308 }else{ 309 width = va_arg(ap,int); 310 } 311 if( width<0 ){ 312 flag_leftjustify = 1; 313 width = width >= -2147483647 ? -width : 0; 314 } 315 #ifdef SQLITE_PRINTF_PRECISION_LIMIT 316 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){ 317 width = SQLITE_PRINTF_PRECISION_LIMIT; 318 } 319 #endif 320 if( (c = fmt[1])!='.' && c!='l' ){ 321 c = *++fmt; 322 done = 1; 323 } 324 break; 325 } 326 case '.': { 327 c = *++fmt; 328 if( c=='*' ){ 329 if( bArgList ){ 330 precision = (int)getIntArg(pArgList); 331 }else{ 332 precision = va_arg(ap,int); 333 } 334 if( precision<0 ){ 335 precision = precision >= -2147483647 ? -precision : -1; 336 } 337 c = *++fmt; 338 }else{ 339 unsigned px = 0; 340 while( c>='0' && c<='9' ){ 341 px = px*10 + c - '0'; 342 c = *++fmt; 343 } 344 testcase( px>0x7fffffff ); 345 precision = px & 0x7fffffff; 346 } 347 #ifdef SQLITE_PRINTF_PRECISION_LIMIT 348 if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){ 349 precision = SQLITE_PRINTF_PRECISION_LIMIT; 350 } 351 #endif 352 if( c=='l' ){ 353 --fmt; 354 }else{ 355 done = 1; 356 } 357 break; 358 } 359 } 360 }while( !done && (c=(*++fmt))!=0 ); 361 362 /* Fetch the info entry for the field */ 363 infop = &fmtinfo[0]; 364 xtype = etINVALID; 365 for(idx=0; idx<ArraySize(fmtinfo); idx++){ 366 if( c==fmtinfo[idx].fmttype ){ 367 infop = &fmtinfo[idx]; 368 xtype = infop->type; 369 break; 370 } 371 } 372 373 /* 374 ** At this point, variables are initialized as follows: 375 ** 376 ** flag_alternateform TRUE if a '#' is present. 377 ** flag_altform2 TRUE if a '!' is present. 378 ** flag_prefix '+' or ' ' or zero 379 ** flag_leftjustify TRUE if a '-' is present or if the 380 ** field width was negative. 381 ** flag_zeropad TRUE if the width began with 0. 382 ** flag_long 1 for "l", 2 for "ll" 383 ** width The specified field width. This is 384 ** always non-negative. Zero is the default. 385 ** precision The specified precision. The default 386 ** is -1. 387 ** xtype The class of the conversion. 388 ** infop Pointer to the appropriate info struct. 389 */ 390 switch( xtype ){ 391 case etPOINTER: 392 flag_long = sizeof(char*)==sizeof(i64) ? 2 : 393 sizeof(char*)==sizeof(long int) ? 1 : 0; 394 /* Fall through into the next case */ 395 case etORDINAL: 396 case etRADIX: 397 cThousand = 0; 398 /* Fall through into the next case */ 399 case etDECIMAL: 400 if( infop->flags & FLAG_SIGNED ){ 401 i64 v; 402 if( bArgList ){ 403 v = getIntArg(pArgList); 404 }else if( flag_long ){ 405 if( flag_long==2 ){ 406 v = va_arg(ap,i64) ; 407 }else{ 408 v = va_arg(ap,long int); 409 } 410 }else{ 411 v = va_arg(ap,int); 412 } 413 if( v<0 ){ 414 if( v==SMALLEST_INT64 ){ 415 longvalue = ((u64)1)<<63; 416 }else{ 417 longvalue = -v; 418 } 419 prefix = '-'; 420 }else{ 421 longvalue = v; 422 prefix = flag_prefix; 423 } 424 }else{ 425 if( bArgList ){ 426 longvalue = (u64)getIntArg(pArgList); 427 }else if( flag_long ){ 428 if( flag_long==2 ){ 429 longvalue = va_arg(ap,u64); 430 }else{ 431 longvalue = va_arg(ap,unsigned long int); 432 } 433 }else{ 434 longvalue = va_arg(ap,unsigned int); 435 } 436 prefix = 0; 437 } 438 if( longvalue==0 ) flag_alternateform = 0; 439 if( flag_zeropad && precision<width-(prefix!=0) ){ 440 precision = width-(prefix!=0); 441 } 442 if( precision<etBUFSIZE-10-etBUFSIZE/3 ){ 443 nOut = etBUFSIZE; 444 zOut = buf; 445 }else{ 446 u64 n; 447 n = (u64)precision + 10; 448 if( cThousand ) n += precision/3; 449 zOut = zExtra = printfTempBuf(pAccum, n); 450 if( zOut==0 ) return; 451 nOut = (int)n; 452 } 453 bufpt = &zOut[nOut-1]; 454 if( xtype==etORDINAL ){ 455 static const char zOrd[] = "thstndrd"; 456 int x = (int)(longvalue % 10); 457 if( x>=4 || (longvalue/10)%10==1 ){ 458 x = 0; 459 } 460 *(--bufpt) = zOrd[x*2+1]; 461 *(--bufpt) = zOrd[x*2]; 462 } 463 { 464 const char *cset = &aDigits[infop->charset]; 465 u8 base = infop->base; 466 do{ /* Convert to ascii */ 467 *(--bufpt) = cset[longvalue%base]; 468 longvalue = longvalue/base; 469 }while( longvalue>0 ); 470 } 471 length = (int)(&zOut[nOut-1]-bufpt); 472 while( precision>length ){ 473 *(--bufpt) = '0'; /* Zero pad */ 474 length++; 475 } 476 if( cThousand ){ 477 int nn = (length - 1)/3; /* Number of "," to insert */ 478 int ix = (length - 1)%3 + 1; 479 bufpt -= nn; 480 for(idx=0; nn>0; idx++){ 481 bufpt[idx] = bufpt[idx+nn]; 482 ix--; 483 if( ix==0 ){ 484 bufpt[++idx] = cThousand; 485 nn--; 486 ix = 3; 487 } 488 } 489 } 490 if( prefix ) *(--bufpt) = prefix; /* Add sign */ 491 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ 492 const char *pre; 493 char x; 494 pre = &aPrefix[infop->prefix]; 495 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; 496 } 497 length = (int)(&zOut[nOut-1]-bufpt); 498 break; 499 case etFLOAT: 500 case etEXP: 501 case etGENERIC: 502 if( bArgList ){ 503 realvalue = getDoubleArg(pArgList); 504 }else{ 505 realvalue = va_arg(ap,double); 506 } 507 #ifdef SQLITE_OMIT_FLOATING_POINT 508 length = 0; 509 #else 510 if( precision<0 ) precision = 6; /* Set default precision */ 511 if( realvalue<0.0 ){ 512 realvalue = -realvalue; 513 prefix = '-'; 514 }else{ 515 prefix = flag_prefix; 516 } 517 if( xtype==etGENERIC && precision>0 ) precision--; 518 testcase( precision>0xfff ); 519 for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){} 520 if( xtype==etFLOAT ) realvalue += rounder; 521 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ 522 exp = 0; 523 if( sqlite3IsNaN((double)realvalue) ){ 524 bufpt = "NaN"; 525 length = 3; 526 break; 527 } 528 if( realvalue>0.0 ){ 529 LONGDOUBLE_TYPE scale = 1.0; 530 while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;} 531 while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; } 532 while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; } 533 realvalue /= scale; 534 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } 535 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } 536 if( exp>350 ){ 537 bufpt = buf; 538 buf[0] = prefix; 539 memcpy(buf+(prefix!=0),"Inf",4); 540 length = 3+(prefix!=0); 541 break; 542 } 543 } 544 bufpt = buf; 545 /* 546 ** If the field type is etGENERIC, then convert to either etEXP 547 ** or etFLOAT, as appropriate. 548 */ 549 if( xtype!=etFLOAT ){ 550 realvalue += rounder; 551 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } 552 } 553 if( xtype==etGENERIC ){ 554 flag_rtz = !flag_alternateform; 555 if( exp<-4 || exp>precision ){ 556 xtype = etEXP; 557 }else{ 558 precision = precision - exp; 559 xtype = etFLOAT; 560 } 561 }else{ 562 flag_rtz = flag_altform2; 563 } 564 if( xtype==etEXP ){ 565 e2 = 0; 566 }else{ 567 e2 = exp; 568 } 569 { 570 i64 szBufNeeded; /* Size of a temporary buffer needed */ 571 szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+15; 572 if( szBufNeeded > etBUFSIZE ){ 573 bufpt = zExtra = printfTempBuf(pAccum, szBufNeeded); 574 if( bufpt==0 ) return; 575 } 576 } 577 zOut = bufpt; 578 nsd = 16 + flag_altform2*10; 579 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; 580 /* The sign in front of the number */ 581 if( prefix ){ 582 *(bufpt++) = prefix; 583 } 584 /* Digits prior to the decimal point */ 585 if( e2<0 ){ 586 *(bufpt++) = '0'; 587 }else{ 588 for(; e2>=0; e2--){ 589 *(bufpt++) = et_getdigit(&realvalue,&nsd); 590 } 591 } 592 /* The decimal point */ 593 if( flag_dp ){ 594 *(bufpt++) = '.'; 595 } 596 /* "0" digits after the decimal point but before the first 597 ** significant digit of the number */ 598 for(e2++; e2<0; precision--, e2++){ 599 assert( precision>0 ); 600 *(bufpt++) = '0'; 601 } 602 /* Significant digits after the decimal point */ 603 while( (precision--)>0 ){ 604 *(bufpt++) = et_getdigit(&realvalue,&nsd); 605 } 606 /* Remove trailing zeros and the "." if no digits follow the "." */ 607 if( flag_rtz && flag_dp ){ 608 while( bufpt[-1]=='0' ) *(--bufpt) = 0; 609 assert( bufpt>zOut ); 610 if( bufpt[-1]=='.' ){ 611 if( flag_altform2 ){ 612 *(bufpt++) = '0'; 613 }else{ 614 *(--bufpt) = 0; 615 } 616 } 617 } 618 /* Add the "eNNN" suffix */ 619 if( xtype==etEXP ){ 620 *(bufpt++) = aDigits[infop->charset]; 621 if( exp<0 ){ 622 *(bufpt++) = '-'; exp = -exp; 623 }else{ 624 *(bufpt++) = '+'; 625 } 626 if( exp>=100 ){ 627 *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */ 628 exp %= 100; 629 } 630 *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */ 631 *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */ 632 } 633 *bufpt = 0; 634 635 /* The converted number is in buf[] and zero terminated. Output it. 636 ** Note that the number is in the usual order, not reversed as with 637 ** integer conversions. */ 638 length = (int)(bufpt-zOut); 639 bufpt = zOut; 640 641 /* Special case: Add leading zeros if the flag_zeropad flag is 642 ** set and we are not left justified */ 643 if( flag_zeropad && !flag_leftjustify && length < width){ 644 int i; 645 int nPad = width - length; 646 for(i=width; i>=nPad; i--){ 647 bufpt[i] = bufpt[i-nPad]; 648 } 649 i = prefix!=0; 650 while( nPad-- ) bufpt[i++] = '0'; 651 length = width; 652 } 653 #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */ 654 break; 655 case etSIZE: 656 if( !bArgList ){ 657 *(va_arg(ap,int*)) = pAccum->nChar; 658 } 659 length = width = 0; 660 break; 661 case etPERCENT: 662 buf[0] = '%'; 663 bufpt = buf; 664 length = 1; 665 break; 666 case etCHARX: 667 if( bArgList ){ 668 bufpt = getTextArg(pArgList); 669 length = 1; 670 if( bufpt ){ 671 buf[0] = c = *(bufpt++); 672 if( (c&0xc0)==0xc0 ){ 673 while( length<4 && (bufpt[0]&0xc0)==0x80 ){ 674 buf[length++] = *(bufpt++); 675 } 676 } 677 }else{ 678 buf[0] = 0; 679 } 680 }else{ 681 unsigned int ch = va_arg(ap,unsigned int); 682 if( ch<0x00080 ){ 683 buf[0] = ch & 0xff; 684 length = 1; 685 }else if( ch<0x00800 ){ 686 buf[0] = 0xc0 + (u8)((ch>>6)&0x1f); 687 buf[1] = 0x80 + (u8)(ch & 0x3f); 688 length = 2; 689 }else if( ch<0x10000 ){ 690 buf[0] = 0xe0 + (u8)((ch>>12)&0x0f); 691 buf[1] = 0x80 + (u8)((ch>>6) & 0x3f); 692 buf[2] = 0x80 + (u8)(ch & 0x3f); 693 length = 3; 694 }else{ 695 buf[0] = 0xf0 + (u8)((ch>>18) & 0x07); 696 buf[1] = 0x80 + (u8)((ch>>12) & 0x3f); 697 buf[2] = 0x80 + (u8)((ch>>6) & 0x3f); 698 buf[3] = 0x80 + (u8)(ch & 0x3f); 699 length = 4; 700 } 701 } 702 if( precision>1 ){ 703 width -= precision-1; 704 if( width>1 && !flag_leftjustify ){ 705 sqlite3_str_appendchar(pAccum, width-1, ' '); 706 width = 0; 707 } 708 while( precision-- > 1 ){ 709 sqlite3_str_append(pAccum, buf, length); 710 } 711 } 712 bufpt = buf; 713 flag_altform2 = 1; 714 goto adjust_width_for_utf8; 715 case etSTRING: 716 case etDYNSTRING: 717 if( bArgList ){ 718 bufpt = getTextArg(pArgList); 719 xtype = etSTRING; 720 }else{ 721 bufpt = va_arg(ap,char*); 722 } 723 if( bufpt==0 ){ 724 bufpt = ""; 725 }else if( xtype==etDYNSTRING ){ 726 if( pAccum->nChar==0 727 && pAccum->mxAlloc 728 && width==0 729 && precision<0 730 && pAccum->accError==0 731 ){ 732 /* Special optimization for sqlite3_mprintf("%z..."): 733 ** Extend an existing memory allocation rather than creating 734 ** a new one. */ 735 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); 736 pAccum->zText = bufpt; 737 pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt); 738 pAccum->nChar = 0x7fffffff & (int)strlen(bufpt); 739 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED; 740 length = 0; 741 break; 742 } 743 zExtra = bufpt; 744 } 745 if( precision>=0 ){ 746 if( flag_altform2 ){ 747 /* Set length to the number of bytes needed in order to display 748 ** precision characters */ 749 unsigned char *z = (unsigned char*)bufpt; 750 while( precision-- > 0 && z[0] ){ 751 SQLITE_SKIP_UTF8(z); 752 } 753 length = (int)(z - (unsigned char*)bufpt); 754 }else{ 755 for(length=0; length<precision && bufpt[length]; length++){} 756 } 757 }else{ 758 length = 0x7fffffff & (int)strlen(bufpt); 759 } 760 adjust_width_for_utf8: 761 if( flag_altform2 && width>0 ){ 762 /* Adjust width to account for extra bytes in UTF-8 characters */ 763 int ii = length - 1; 764 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++; 765 } 766 break; 767 case etSQLESCAPE: /* %q: Escape ' characters */ 768 case etSQLESCAPE2: /* %Q: Escape ' and enclose in '...' */ 769 case etSQLESCAPE3: { /* %w: Escape " characters */ 770 int i, j, k, n, isnull; 771 int needQuote; 772 char ch; 773 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */ 774 char *escarg; 775 776 if( bArgList ){ 777 escarg = getTextArg(pArgList); 778 }else{ 779 escarg = va_arg(ap,char*); 780 } 781 isnull = escarg==0; 782 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); 783 /* For %q, %Q, and %w, the precision is the number of byte (or 784 ** characters if the ! flags is present) to use from the input. 785 ** Because of the extra quoting characters inserted, the number 786 ** of output characters may be larger than the precision. 787 */ 788 k = precision; 789 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ 790 if( ch==q ) n++; 791 if( flag_altform2 && (ch&0xc0)==0xc0 ){ 792 while( (escarg[i+1]&0xc0)==0x80 ){ i++; } 793 } 794 } 795 needQuote = !isnull && xtype==etSQLESCAPE2; 796 n += i + 3; 797 if( n>etBUFSIZE ){ 798 bufpt = zExtra = printfTempBuf(pAccum, n); 799 if( bufpt==0 ) return; 800 }else{ 801 bufpt = buf; 802 } 803 j = 0; 804 if( needQuote ) bufpt[j++] = q; 805 k = i; 806 for(i=0; i<k; i++){ 807 bufpt[j++] = ch = escarg[i]; 808 if( ch==q ) bufpt[j++] = ch; 809 } 810 if( needQuote ) bufpt[j++] = q; 811 bufpt[j] = 0; 812 length = j; 813 goto adjust_width_for_utf8; 814 } 815 case etTOKEN: { 816 Token *pToken; 817 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; 818 pToken = va_arg(ap, Token*); 819 assert( bArgList==0 ); 820 if( pToken && pToken->n ){ 821 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n); 822 } 823 length = width = 0; 824 break; 825 } 826 case etSRCLIST: { 827 SrcList *pSrc; 828 int k; 829 struct SrcList_item *pItem; 830 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; 831 pSrc = va_arg(ap, SrcList*); 832 k = va_arg(ap, int); 833 pItem = &pSrc->a[k]; 834 assert( bArgList==0 ); 835 assert( k>=0 && k<pSrc->nSrc ); 836 if( pItem->zDatabase ){ 837 sqlite3_str_appendall(pAccum, pItem->zDatabase); 838 sqlite3_str_append(pAccum, ".", 1); 839 } 840 sqlite3_str_appendall(pAccum, pItem->zName); 841 length = width = 0; 842 break; 843 } 844 default: { 845 assert( xtype==etINVALID ); 846 return; 847 } 848 }/* End switch over the format type */ 849 /* 850 ** The text of the conversion is pointed to by "bufpt" and is 851 ** "length" characters long. The field width is "width". Do 852 ** the output. Both length and width are in bytes, not characters, 853 ** at this point. If the "!" flag was present on string conversions 854 ** indicating that width and precision should be expressed in characters, 855 ** then the values have been translated prior to reaching this point. 856 */ 857 width -= length; 858 if( width>0 ){ 859 if( !flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' '); 860 sqlite3_str_append(pAccum, bufpt, length); 861 if( flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' '); 862 }else{ 863 sqlite3_str_append(pAccum, bufpt, length); 864 } 865 866 if( zExtra ){ 867 sqlite3DbFree(pAccum->db, zExtra); 868 zExtra = 0; 869 } 870 }/* End for loop over the format string */ 871 } /* End of function */ 872 873 /* 874 ** Enlarge the memory allocation on a StrAccum object so that it is 875 ** able to accept at least N more bytes of text. 876 ** 877 ** Return the number of bytes of text that StrAccum is able to accept 878 ** after the attempted enlargement. The value returned might be zero. 879 */ 880 static int sqlite3StrAccumEnlarge(StrAccum *p, int N){ 881 char *zNew; 882 assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */ 883 if( p->accError ){ 884 testcase(p->accError==SQLITE_TOOBIG); 885 testcase(p->accError==SQLITE_NOMEM); 886 return 0; 887 } 888 if( p->mxAlloc==0 ){ 889 setStrAccumError(p, SQLITE_TOOBIG); 890 return p->nAlloc - p->nChar - 1; 891 }else{ 892 char *zOld = isMalloced(p) ? p->zText : 0; 893 i64 szNew = p->nChar; 894 szNew += N + 1; 895 if( szNew+p->nChar<=p->mxAlloc ){ 896 /* Force exponential buffer size growth as long as it does not overflow, 897 ** to avoid having to call this routine too often */ 898 szNew += p->nChar; 899 } 900 if( szNew > p->mxAlloc ){ 901 sqlite3_str_reset(p); 902 setStrAccumError(p, SQLITE_TOOBIG); 903 return 0; 904 }else{ 905 p->nAlloc = (int)szNew; 906 } 907 if( p->db ){ 908 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); 909 }else{ 910 zNew = sqlite3_realloc64(zOld, p->nAlloc); 911 } 912 if( zNew ){ 913 assert( p->zText!=0 || p->nChar==0 ); 914 if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); 915 p->zText = zNew; 916 p->nAlloc = sqlite3DbMallocSize(p->db, zNew); 917 p->printfFlags |= SQLITE_PRINTF_MALLOCED; 918 }else{ 919 sqlite3_str_reset(p); 920 setStrAccumError(p, SQLITE_NOMEM); 921 return 0; 922 } 923 } 924 return N; 925 } 926 927 /* 928 ** Append N copies of character c to the given string buffer. 929 */ 930 void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){ 931 testcase( p->nChar + (i64)N > 0x7fffffff ); 932 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ 933 return; 934 } 935 while( (N--)>0 ) p->zText[p->nChar++] = c; 936 } 937 938 /* 939 ** The StrAccum "p" is not large enough to accept N new bytes of z[]. 940 ** So enlarge if first, then do the append. 941 ** 942 ** This is a helper routine to sqlite3_str_append() that does special-case 943 ** work (enlarging the buffer) using tail recursion, so that the 944 ** sqlite3_str_append() routine can use fast calling semantics. 945 */ 946 static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){ 947 N = sqlite3StrAccumEnlarge(p, N); 948 if( N>0 ){ 949 memcpy(&p->zText[p->nChar], z, N); 950 p->nChar += N; 951 } 952 } 953 954 /* 955 ** Append N bytes of text from z to the StrAccum object. Increase the 956 ** size of the memory allocation for StrAccum if necessary. 957 */ 958 void sqlite3_str_append(sqlite3_str *p, const char *z, int N){ 959 assert( z!=0 || N==0 ); 960 assert( p->zText!=0 || p->nChar==0 || p->accError ); 961 assert( N>=0 ); 962 assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 ); 963 if( p->nChar+N >= p->nAlloc ){ 964 enlargeAndAppend(p,z,N); 965 }else if( N ){ 966 assert( p->zText ); 967 p->nChar += N; 968 memcpy(&p->zText[p->nChar-N], z, N); 969 } 970 } 971 972 /* 973 ** Append the complete text of zero-terminated string z[] to the p string. 974 */ 975 void sqlite3_str_appendall(sqlite3_str *p, const char *z){ 976 sqlite3_str_append(p, z, sqlite3Strlen30(z)); 977 } 978 979 980 /* 981 ** Finish off a string by making sure it is zero-terminated. 982 ** Return a pointer to the resulting string. Return a NULL 983 ** pointer if any kind of error was encountered. 984 */ 985 static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){ 986 char *zText; 987 assert( p->mxAlloc>0 && !isMalloced(p) ); 988 zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); 989 if( zText ){ 990 memcpy(zText, p->zText, p->nChar+1); 991 p->printfFlags |= SQLITE_PRINTF_MALLOCED; 992 }else{ 993 setStrAccumError(p, SQLITE_NOMEM); 994 } 995 p->zText = zText; 996 return zText; 997 } 998 char *sqlite3StrAccumFinish(StrAccum *p){ 999 if( p->zText ){ 1000 p->zText[p->nChar] = 0; 1001 if( p->mxAlloc>0 && !isMalloced(p) ){ 1002 return strAccumFinishRealloc(p); 1003 } 1004 } 1005 return p->zText; 1006 } 1007 1008 /* 1009 ** This singleton is an sqlite3_str object that is returned if 1010 ** sqlite3_malloc() fails to provide space for a real one. This 1011 ** sqlite3_str object accepts no new text and always returns 1012 ** an SQLITE_NOMEM error. 1013 */ 1014 static sqlite3_str sqlite3OomStr = { 1015 0, 0, 0, 0, 0, SQLITE_NOMEM, 0 1016 }; 1017 1018 /* Finalize a string created using sqlite3_str_new(). 1019 */ 1020 char *sqlite3_str_finish(sqlite3_str *p){ 1021 char *z; 1022 if( p!=0 && p!=&sqlite3OomStr ){ 1023 z = sqlite3StrAccumFinish(p); 1024 sqlite3_free(p); 1025 }else{ 1026 z = 0; 1027 } 1028 return z; 1029 } 1030 1031 /* Return any error code associated with p */ 1032 int sqlite3_str_errcode(sqlite3_str *p){ 1033 return p ? p->accError : SQLITE_NOMEM; 1034 } 1035 1036 /* Return the current length of p in bytes */ 1037 int sqlite3_str_length(sqlite3_str *p){ 1038 return p ? p->nChar : 0; 1039 } 1040 1041 /* Return the current value for p */ 1042 char *sqlite3_str_value(sqlite3_str *p){ 1043 if( p==0 || p->nChar==0 ) return 0; 1044 p->zText[p->nChar] = 0; 1045 return p->zText; 1046 } 1047 1048 /* 1049 ** Reset an StrAccum string. Reclaim all malloced memory. 1050 */ 1051 void sqlite3_str_reset(StrAccum *p){ 1052 if( isMalloced(p) ){ 1053 sqlite3DbFree(p->db, p->zText); 1054 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED; 1055 } 1056 p->nAlloc = 0; 1057 p->nChar = 0; 1058 p->zText = 0; 1059 } 1060 1061 /* 1062 ** Initialize a string accumulator. 1063 ** 1064 ** p: The accumulator to be initialized. 1065 ** db: Pointer to a database connection. May be NULL. Lookaside 1066 ** memory is used if not NULL. db->mallocFailed is set appropriately 1067 ** when not NULL. 1068 ** zBase: An initial buffer. May be NULL in which case the initial buffer 1069 ** is malloced. 1070 ** n: Size of zBase in bytes. If total space requirements never exceed 1071 ** n then no memory allocations ever occur. 1072 ** mx: Maximum number of bytes to accumulate. If mx==0 then no memory 1073 ** allocations will ever occur. 1074 */ 1075 void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){ 1076 p->zText = zBase; 1077 p->db = db; 1078 p->nAlloc = n; 1079 p->mxAlloc = mx; 1080 p->nChar = 0; 1081 p->accError = 0; 1082 p->printfFlags = 0; 1083 } 1084 1085 /* Allocate and initialize a new dynamic string object */ 1086 sqlite3_str *sqlite3_str_new(sqlite3 *db){ 1087 sqlite3_str *p = sqlite3_malloc64(sizeof(*p)); 1088 if( p ){ 1089 sqlite3StrAccumInit(p, 0, 0, 0, 1090 db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH); 1091 }else{ 1092 p = &sqlite3OomStr; 1093 } 1094 return p; 1095 } 1096 1097 /* 1098 ** Print into memory obtained from sqliteMalloc(). Use the internal 1099 ** %-conversion extensions. 1100 */ 1101 char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ 1102 char *z; 1103 char zBase[SQLITE_PRINT_BUF_SIZE]; 1104 StrAccum acc; 1105 assert( db!=0 ); 1106 sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase), 1107 db->aLimit[SQLITE_LIMIT_LENGTH]); 1108 acc.printfFlags = SQLITE_PRINTF_INTERNAL; 1109 sqlite3_str_vappendf(&acc, zFormat, ap); 1110 z = sqlite3StrAccumFinish(&acc); 1111 if( acc.accError==SQLITE_NOMEM ){ 1112 sqlite3OomFault(db); 1113 } 1114 return z; 1115 } 1116 1117 /* 1118 ** Print into memory obtained from sqliteMalloc(). Use the internal 1119 ** %-conversion extensions. 1120 */ 1121 char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ 1122 va_list ap; 1123 char *z; 1124 va_start(ap, zFormat); 1125 z = sqlite3VMPrintf(db, zFormat, ap); 1126 va_end(ap); 1127 return z; 1128 } 1129 1130 /* 1131 ** Print into memory obtained from sqlite3_malloc(). Omit the internal 1132 ** %-conversion extensions. 1133 */ 1134 char *sqlite3_vmprintf(const char *zFormat, va_list ap){ 1135 char *z; 1136 char zBase[SQLITE_PRINT_BUF_SIZE]; 1137 StrAccum acc; 1138 1139 #ifdef SQLITE_ENABLE_API_ARMOR 1140 if( zFormat==0 ){ 1141 (void)SQLITE_MISUSE_BKPT; 1142 return 0; 1143 } 1144 #endif 1145 #ifndef SQLITE_OMIT_AUTOINIT 1146 if( sqlite3_initialize() ) return 0; 1147 #endif 1148 sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); 1149 sqlite3_str_vappendf(&acc, zFormat, ap); 1150 z = sqlite3StrAccumFinish(&acc); 1151 return z; 1152 } 1153 1154 /* 1155 ** Print into memory obtained from sqlite3_malloc()(). Omit the internal 1156 ** %-conversion extensions. 1157 */ 1158 char *sqlite3_mprintf(const char *zFormat, ...){ 1159 va_list ap; 1160 char *z; 1161 #ifndef SQLITE_OMIT_AUTOINIT 1162 if( sqlite3_initialize() ) return 0; 1163 #endif 1164 va_start(ap, zFormat); 1165 z = sqlite3_vmprintf(zFormat, ap); 1166 va_end(ap); 1167 return z; 1168 } 1169 1170 /* 1171 ** sqlite3_snprintf() works like snprintf() except that it ignores the 1172 ** current locale settings. This is important for SQLite because we 1173 ** are not able to use a "," as the decimal point in place of "." as 1174 ** specified by some locales. 1175 ** 1176 ** Oops: The first two arguments of sqlite3_snprintf() are backwards 1177 ** from the snprintf() standard. Unfortunately, it is too late to change 1178 ** this without breaking compatibility, so we just have to live with the 1179 ** mistake. 1180 ** 1181 ** sqlite3_vsnprintf() is the varargs version. 1182 */ 1183 char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ 1184 StrAccum acc; 1185 if( n<=0 ) return zBuf; 1186 #ifdef SQLITE_ENABLE_API_ARMOR 1187 if( zBuf==0 || zFormat==0 ) { 1188 (void)SQLITE_MISUSE_BKPT; 1189 if( zBuf ) zBuf[0] = 0; 1190 return zBuf; 1191 } 1192 #endif 1193 sqlite3StrAccumInit(&acc, 0, zBuf, n, 0); 1194 sqlite3_str_vappendf(&acc, zFormat, ap); 1195 zBuf[acc.nChar] = 0; 1196 return zBuf; 1197 } 1198 char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ 1199 char *z; 1200 va_list ap; 1201 va_start(ap,zFormat); 1202 z = sqlite3_vsnprintf(n, zBuf, zFormat, ap); 1203 va_end(ap); 1204 return z; 1205 } 1206 1207 /* 1208 ** This is the routine that actually formats the sqlite3_log() message. 1209 ** We house it in a separate routine from sqlite3_log() to avoid using 1210 ** stack space on small-stack systems when logging is disabled. 1211 ** 1212 ** sqlite3_log() must render into a static buffer. It cannot dynamically 1213 ** allocate memory because it might be called while the memory allocator 1214 ** mutex is held. 1215 ** 1216 ** sqlite3_str_vappendf() might ask for *temporary* memory allocations for 1217 ** certain format characters (%q) or for very large precisions or widths. 1218 ** Care must be taken that any sqlite3_log() calls that occur while the 1219 ** memory mutex is held do not use these mechanisms. 1220 */ 1221 static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){ 1222 StrAccum acc; /* String accumulator */ 1223 char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */ 1224 1225 sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0); 1226 sqlite3_str_vappendf(&acc, zFormat, ap); 1227 sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode, 1228 sqlite3StrAccumFinish(&acc)); 1229 } 1230 1231 /* 1232 ** Format and write a message to the log if logging is enabled. 1233 */ 1234 void sqlite3_log(int iErrCode, const char *zFormat, ...){ 1235 va_list ap; /* Vararg list */ 1236 if( sqlite3GlobalConfig.xLog ){ 1237 va_start(ap, zFormat); 1238 renderLogMsg(iErrCode, zFormat, ap); 1239 va_end(ap); 1240 } 1241 } 1242 1243 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) 1244 /* 1245 ** A version of printf() that understands %lld. Used for debugging. 1246 ** The printf() built into some versions of windows does not understand %lld 1247 ** and segfaults if you give it a long long int. 1248 */ 1249 void sqlite3DebugPrintf(const char *zFormat, ...){ 1250 va_list ap; 1251 StrAccum acc; 1252 char zBuf[500]; 1253 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); 1254 va_start(ap,zFormat); 1255 sqlite3_str_vappendf(&acc, zFormat, ap); 1256 va_end(ap); 1257 sqlite3StrAccumFinish(&acc); 1258 #ifdef SQLITE_OS_TRACE_PROC 1259 { 1260 extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf); 1261 SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf)); 1262 } 1263 #else 1264 fprintf(stdout,"%s", zBuf); 1265 fflush(stdout); 1266 #endif 1267 } 1268 #endif 1269 1270 1271 /* 1272 ** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument 1273 ** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats. 1274 */ 1275 void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){ 1276 va_list ap; 1277 va_start(ap,zFormat); 1278 sqlite3_str_vappendf(p, zFormat, ap); 1279 va_end(ap); 1280 } 1281