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