1 /* 2 ** The "printf" code that follows dates from the 1980's. It is in 3 ** the public domain. The original comments are included here for 4 ** completeness. They are very out-of-date but might be useful as 5 ** an historical reference. Most of the "enhancements" have been backed 6 ** out so that the functionality is now the same as standard printf(). 7 ** 8 ************************************************************************** 9 ** 10 ** The following modules is an enhanced replacement for the "printf" subroutines 11 ** found in the standard C library. The following enhancements are 12 ** supported: 13 ** 14 ** + Additional functions. The standard set of "printf" functions 15 ** includes printf, fprintf, sprintf, vprintf, vfprintf, and 16 ** vsprintf. This module adds the following: 17 ** 18 ** * snprintf -- Works like sprintf, but has an extra argument 19 ** which is the size of the buffer written to. 20 ** 21 ** * mprintf -- Similar to sprintf. Writes output to memory 22 ** obtained from malloc. 23 ** 24 ** * xprintf -- Calls a function to dispose of output. 25 ** 26 ** * nprintf -- No output, but returns the number of characters 27 ** that would have been output by printf. 28 ** 29 ** * A v- version (ex: vsnprintf) of every function is also 30 ** supplied. 31 ** 32 ** + A few extensions to the formatting notation are supported: 33 ** 34 ** * The "=" flag (similar to "-") causes the output to be 35 ** be centered in the appropriately sized field. 36 ** 37 ** * The %b field outputs an integer in binary notation. 38 ** 39 ** * The %c field now accepts a precision. The character output 40 ** is repeated by the number of times the precision specifies. 41 ** 42 ** * The %' field works like %c, but takes as its character the 43 ** next character of the format string, instead of the next 44 ** argument. For example, printf("%.78'-") prints 78 minus 45 ** signs, the same as printf("%.78c",'-'). 46 ** 47 ** + When compiled using GCC on a SPARC, this version of printf is 48 ** faster than the library printf for SUN OS 4.1. 49 ** 50 ** + All functions are fully reentrant. 51 ** 52 */ 53 #include "sqliteInt.h" 54 55 /* 56 ** Conversion types fall into various categories as defined by the 57 ** following enumeration. 58 */ 59 #define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */ 60 #define etFLOAT 2 /* Floating point. %f */ 61 #define etEXP 3 /* Exponentional notation. %e and %E */ 62 #define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */ 63 #define etSIZE 5 /* Return number of characters processed so far. %n */ 64 #define etSTRING 6 /* Strings. %s */ 65 #define etDYNSTRING 7 /* Dynamically allocated strings. %z */ 66 #define etPERCENT 8 /* Percent symbol. %% */ 67 #define etCHARX 9 /* Characters. %c */ 68 /* The rest are extensions, not normally found in printf() */ 69 #define etCHARLIT 10 /* Literal characters. %' */ 70 #define etSQLESCAPE 11 /* Strings with '\'' doubled. %q */ 71 #define etSQLESCAPE2 12 /* Strings with '\'' doubled and enclosed in '', 72 NULL pointers replaced by SQL NULL. %Q */ 73 #define etTOKEN 13 /* a pointer to a Token structure */ 74 #define etSRCLIST 14 /* a pointer to a SrcList */ 75 #define etPOINTER 15 /* The %p conversion */ 76 #define etSQLESCAPE3 16 /* %w -> Strings with '\"' doubled */ 77 #define etORDINAL 17 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */ 78 79 80 /* 81 ** An "etByte" is an 8-bit unsigned value. 82 */ 83 typedef unsigned char etByte; 84 85 /* 86 ** Each builtin conversion character (ex: the 'd' in "%d") is described 87 ** by an instance of the following structure 88 */ 89 typedef struct et_info { /* Information about each format field */ 90 char fmttype; /* The format field code letter */ 91 etByte base; /* The base for radix conversion */ 92 etByte flags; /* One or more of FLAG_ constants below */ 93 etByte type; /* Conversion paradigm */ 94 etByte charset; /* Offset into aDigits[] of the digits string */ 95 etByte prefix; /* Offset into aPrefix[] of the prefix string */ 96 } et_info; 97 98 /* 99 ** Allowed values for et_info.flags 100 */ 101 #define FLAG_SIGNED 1 /* True if the value to convert is signed */ 102 #define FLAG_INTERN 2 /* True if for internal use only */ 103 #define FLAG_STRING 4 /* Allow infinity precision */ 104 105 106 /* 107 ** The following table is searched linearly, so it is good to put the 108 ** most frequently used conversion types first. 109 */ 110 static const char aDigits[] = "0123456789ABCDEF0123456789abcdef"; 111 static const char aPrefix[] = "-x0\000X0"; 112 static const et_info fmtinfo[] = { 113 { 'd', 10, 1, etRADIX, 0, 0 }, 114 { 's', 0, 4, etSTRING, 0, 0 }, 115 { 'g', 0, 1, etGENERIC, 30, 0 }, 116 { 'z', 0, 4, etDYNSTRING, 0, 0 }, 117 { 'q', 0, 4, etSQLESCAPE, 0, 0 }, 118 { 'Q', 0, 4, etSQLESCAPE2, 0, 0 }, 119 { 'w', 0, 4, etSQLESCAPE3, 0, 0 }, 120 { 'c', 0, 0, etCHARX, 0, 0 }, 121 { 'o', 8, 0, etRADIX, 0, 2 }, 122 { 'u', 10, 0, etRADIX, 0, 0 }, 123 { 'x', 16, 0, etRADIX, 16, 1 }, 124 { 'X', 16, 0, etRADIX, 0, 4 }, 125 #ifndef SQLITE_OMIT_FLOATING_POINT 126 { 'f', 0, 1, etFLOAT, 0, 0 }, 127 { 'e', 0, 1, etEXP, 30, 0 }, 128 { 'E', 0, 1, etEXP, 14, 0 }, 129 { 'G', 0, 1, etGENERIC, 14, 0 }, 130 #endif 131 { 'i', 10, 1, etRADIX, 0, 0 }, 132 { 'n', 0, 0, etSIZE, 0, 0 }, 133 { '%', 0, 0, etPERCENT, 0, 0 }, 134 { 'p', 16, 0, etPOINTER, 0, 1 }, 135 { 'T', 0, 2, etTOKEN, 0, 0 }, 136 { 'S', 0, 2, etSRCLIST, 0, 0 }, 137 { 'r', 10, 3, etORDINAL, 0, 0 }, 138 }; 139 #define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0])) 140 141 /* 142 ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point 143 ** conversions will work. 144 */ 145 #ifndef SQLITE_OMIT_FLOATING_POINT 146 /* 147 ** "*val" is a double such that 0.1 <= *val < 10.0 148 ** Return the ascii code for the leading digit of *val, then 149 ** multiply "*val" by 10.0 to renormalize. 150 ** 151 ** Example: 152 ** input: *val = 3.14159 153 ** output: *val = 1.4159 function return = '3' 154 ** 155 ** The counter *cnt is incremented each time. After counter exceeds 156 ** 16 (the number of significant digits in a 64-bit float) '0' is 157 ** always returned. 158 */ 159 static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ 160 int digit; 161 LONGDOUBLE_TYPE d; 162 if( (*cnt)++ >= 16 ) return '0'; 163 digit = (int)*val; 164 d = digit; 165 digit += '0'; 166 *val = (*val - d)*10.0; 167 return digit; 168 } 169 #endif /* SQLITE_OMIT_FLOATING_POINT */ 170 171 /* 172 ** Append N space characters to the given string buffer. 173 */ 174 static void appendSpace(StrAccum *pAccum, int N){ 175 static const char zSpaces[] = " "; 176 while( N>=sizeof(zSpaces)-1 ){ 177 sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1); 178 N -= sizeof(zSpaces)-1; 179 } 180 if( N>0 ){ 181 sqlite3StrAccumAppend(pAccum, zSpaces, N); 182 } 183 } 184 185 /* 186 ** On machines with a small stack size, you can redefine the 187 ** SQLITE_PRINT_BUF_SIZE to be less than 350. But beware - for 188 ** smaller values some %f conversions may go into an infinite loop. 189 */ 190 #ifndef SQLITE_PRINT_BUF_SIZE 191 # define SQLITE_PRINT_BUF_SIZE 350 192 #endif 193 #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ 194 195 /* 196 ** The root program. All variations call this core. 197 ** 198 ** INPUTS: 199 ** func This is a pointer to a function taking three arguments 200 ** 1. A pointer to anything. Same as the "arg" parameter. 201 ** 2. A pointer to the list of characters to be output 202 ** (Note, this list is NOT null terminated.) 203 ** 3. An integer number of characters to be output. 204 ** (Note: This number might be zero.) 205 ** 206 ** arg This is the pointer to anything which will be passed as the 207 ** first argument to "func". Use it for whatever you like. 208 ** 209 ** fmt This is the format string, as in the usual print. 210 ** 211 ** ap This is a pointer to a list of arguments. Same as in 212 ** vfprint. 213 ** 214 ** OUTPUTS: 215 ** The return value is the total number of characters sent to 216 ** the function "func". Returns -1 on a error. 217 ** 218 ** Note that the order in which automatic variables are declared below 219 ** seems to make a big difference in determining how fast this beast 220 ** will run. 221 */ 222 static void vxprintf( 223 StrAccum *pAccum, /* Accumulate results here */ 224 int useExtended, /* Allow extended %-conversions */ 225 const char *fmt, /* Format string */ 226 va_list ap /* arguments */ 227 ){ 228 int c; /* Next character in the format string */ 229 char *bufpt; /* Pointer to the conversion buffer */ 230 int precision; /* Precision of the current field */ 231 int length; /* Length of the field */ 232 int idx; /* A general purpose loop counter */ 233 int width; /* Width of the current field */ 234 etByte flag_leftjustify; /* True if "-" flag is present */ 235 etByte flag_plussign; /* True if "+" flag is present */ 236 etByte flag_blanksign; /* True if " " flag is present */ 237 etByte flag_alternateform; /* True if "#" flag is present */ 238 etByte flag_altform2; /* True if "!" flag is present */ 239 etByte flag_zeropad; /* True if field width constant starts with zero */ 240 etByte flag_long; /* True if "l" flag is present */ 241 etByte flag_longlong; /* True if the "ll" flag is present */ 242 etByte done; /* Loop termination flag */ 243 sqlite_uint64 longvalue; /* Value for integer types */ 244 LONGDOUBLE_TYPE realvalue; /* Value for real types */ 245 const et_info *infop; /* Pointer to the appropriate info structure */ 246 char buf[etBUFSIZE]; /* Conversion buffer */ 247 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ 248 etByte errorflag = 0; /* True if an error is encountered */ 249 etByte xtype; /* Conversion paradigm */ 250 char *zExtra; /* Extra memory used for etTCLESCAPE conversions */ 251 #ifndef SQLITE_OMIT_FLOATING_POINT 252 int exp, e2; /* exponent of real numbers */ 253 double rounder; /* Used for rounding floating point values */ 254 etByte flag_dp; /* True if decimal point should be shown */ 255 etByte flag_rtz; /* True if trailing zeros should be removed */ 256 etByte flag_exp; /* True to force display of the exponent */ 257 int nsd; /* Number of significant digits returned */ 258 #endif 259 260 length = 0; 261 bufpt = 0; 262 for(; (c=(*fmt))!=0; ++fmt){ 263 if( c!='%' ){ 264 int amt; 265 bufpt = (char *)fmt; 266 amt = 1; 267 while( (c=(*++fmt))!='%' && c!=0 ) amt++; 268 sqlite3StrAccumAppend(pAccum, bufpt, amt); 269 if( c==0 ) break; 270 } 271 if( (c=(*++fmt))==0 ){ 272 errorflag = 1; 273 sqlite3StrAccumAppend(pAccum, "%", 1); 274 break; 275 } 276 /* Find out what flags are present */ 277 flag_leftjustify = flag_plussign = flag_blanksign = 278 flag_alternateform = flag_altform2 = flag_zeropad = 0; 279 done = 0; 280 do{ 281 switch( c ){ 282 case '-': flag_leftjustify = 1; break; 283 case '+': flag_plussign = 1; break; 284 case ' ': flag_blanksign = 1; break; 285 case '#': flag_alternateform = 1; break; 286 case '!': flag_altform2 = 1; break; 287 case '0': flag_zeropad = 1; break; 288 default: done = 1; break; 289 } 290 }while( !done && (c=(*++fmt))!=0 ); 291 /* Get the field width */ 292 width = 0; 293 if( c=='*' ){ 294 width = va_arg(ap,int); 295 if( width<0 ){ 296 flag_leftjustify = 1; 297 width = -width; 298 } 299 c = *++fmt; 300 }else{ 301 while( c>='0' && c<='9' ){ 302 width = width*10 + c - '0'; 303 c = *++fmt; 304 } 305 } 306 if( width > etBUFSIZE-10 ){ 307 width = etBUFSIZE-10; 308 } 309 /* Get the precision */ 310 if( c=='.' ){ 311 precision = 0; 312 c = *++fmt; 313 if( c=='*' ){ 314 precision = va_arg(ap,int); 315 if( precision<0 ) precision = -precision; 316 c = *++fmt; 317 }else{ 318 while( c>='0' && c<='9' ){ 319 precision = precision*10 + c - '0'; 320 c = *++fmt; 321 } 322 } 323 }else{ 324 precision = -1; 325 } 326 /* Get the conversion type modifier */ 327 if( c=='l' ){ 328 flag_long = 1; 329 c = *++fmt; 330 if( c=='l' ){ 331 flag_longlong = 1; 332 c = *++fmt; 333 }else{ 334 flag_longlong = 0; 335 } 336 }else{ 337 flag_long = flag_longlong = 0; 338 } 339 /* Fetch the info entry for the field */ 340 infop = 0; 341 for(idx=0; idx<etNINFO; idx++){ 342 if( c==fmtinfo[idx].fmttype ){ 343 infop = &fmtinfo[idx]; 344 if( useExtended || (infop->flags & FLAG_INTERN)==0 ){ 345 xtype = infop->type; 346 }else{ 347 return; 348 } 349 break; 350 } 351 } 352 zExtra = 0; 353 if( infop==0 ){ 354 return; 355 } 356 357 358 /* Limit the precision to prevent overflowing buf[] during conversion */ 359 if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){ 360 precision = etBUFSIZE-40; 361 } 362 363 /* 364 ** At this point, variables are initialized as follows: 365 ** 366 ** flag_alternateform TRUE if a '#' is present. 367 ** flag_altform2 TRUE if a '!' is present. 368 ** flag_plussign TRUE if a '+' is present. 369 ** flag_leftjustify TRUE if a '-' is present or if the 370 ** field width was negative. 371 ** flag_zeropad TRUE if the width began with 0. 372 ** flag_long TRUE if the letter 'l' (ell) prefixed 373 ** the conversion character. 374 ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed 375 ** the conversion character. 376 ** flag_blanksign TRUE if a ' ' is present. 377 ** width The specified field width. This is 378 ** always non-negative. Zero is the default. 379 ** precision The specified precision. The default 380 ** is -1. 381 ** xtype The class of the conversion. 382 ** infop Pointer to the appropriate info struct. 383 */ 384 switch( xtype ){ 385 case etPOINTER: 386 flag_longlong = sizeof(char*)==sizeof(i64); 387 flag_long = sizeof(char*)==sizeof(long int); 388 /* Fall through into the next case */ 389 case etORDINAL: 390 case etRADIX: 391 if( infop->flags & FLAG_SIGNED ){ 392 i64 v; 393 if( flag_longlong ) v = va_arg(ap,i64); 394 else if( flag_long ) v = va_arg(ap,long int); 395 else v = va_arg(ap,int); 396 if( v<0 ){ 397 longvalue = -v; 398 prefix = '-'; 399 }else{ 400 longvalue = v; 401 if( flag_plussign ) prefix = '+'; 402 else if( flag_blanksign ) prefix = ' '; 403 else prefix = 0; 404 } 405 }else{ 406 if( flag_longlong ) longvalue = va_arg(ap,u64); 407 else if( flag_long ) longvalue = va_arg(ap,unsigned long int); 408 else longvalue = va_arg(ap,unsigned int); 409 prefix = 0; 410 } 411 if( longvalue==0 ) flag_alternateform = 0; 412 if( flag_zeropad && precision<width-(prefix!=0) ){ 413 precision = width-(prefix!=0); 414 } 415 bufpt = &buf[etBUFSIZE-1]; 416 if( xtype==etORDINAL ){ 417 static const char zOrd[] = "thstndrd"; 418 int x = longvalue % 10; 419 if( x>=4 || (longvalue/10)%10==1 ){ 420 x = 0; 421 } 422 buf[etBUFSIZE-3] = zOrd[x*2]; 423 buf[etBUFSIZE-2] = zOrd[x*2+1]; 424 bufpt -= 2; 425 } 426 { 427 register const char *cset; /* Use registers for speed */ 428 register int base; 429 cset = &aDigits[infop->charset]; 430 base = infop->base; 431 do{ /* Convert to ascii */ 432 *(--bufpt) = cset[longvalue%base]; 433 longvalue = longvalue/base; 434 }while( longvalue>0 ); 435 } 436 length = &buf[etBUFSIZE-1]-bufpt; 437 for(idx=precision-length; idx>0; idx--){ 438 *(--bufpt) = '0'; /* Zero pad */ 439 } 440 if( prefix ) *(--bufpt) = prefix; /* Add sign */ 441 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ 442 const char *pre; 443 char x; 444 pre = &aPrefix[infop->prefix]; 445 if( *bufpt!=pre[0] ){ 446 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; 447 } 448 } 449 length = &buf[etBUFSIZE-1]-bufpt; 450 break; 451 case etFLOAT: 452 case etEXP: 453 case etGENERIC: 454 realvalue = va_arg(ap,double); 455 #ifndef SQLITE_OMIT_FLOATING_POINT 456 if( precision<0 ) precision = 6; /* Set default precision */ 457 if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10; 458 if( realvalue<0.0 ){ 459 realvalue = -realvalue; 460 prefix = '-'; 461 }else{ 462 if( flag_plussign ) prefix = '+'; 463 else if( flag_blanksign ) prefix = ' '; 464 else prefix = 0; 465 } 466 if( xtype==etGENERIC && precision>0 ) precision--; 467 #if 0 468 /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */ 469 for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1); 470 #else 471 /* It makes more sense to use 0.5 */ 472 for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){} 473 #endif 474 if( xtype==etFLOAT ) realvalue += rounder; 475 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ 476 exp = 0; 477 if( sqlite3_isnan(realvalue) ){ 478 bufpt = "NaN"; 479 length = 3; 480 break; 481 } 482 if( realvalue>0.0 ){ 483 while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; } 484 while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; } 485 while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; } 486 while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; } 487 while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; } 488 if( exp>350 || exp<-350 ){ 489 if( prefix=='-' ){ 490 bufpt = "-Inf"; 491 }else if( prefix=='+' ){ 492 bufpt = "+Inf"; 493 }else{ 494 bufpt = "Inf"; 495 } 496 length = strlen(bufpt); 497 break; 498 } 499 } 500 bufpt = buf; 501 /* 502 ** If the field type is etGENERIC, then convert to either etEXP 503 ** or etFLOAT, as appropriate. 504 */ 505 flag_exp = xtype==etEXP; 506 if( xtype!=etFLOAT ){ 507 realvalue += rounder; 508 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } 509 } 510 if( xtype==etGENERIC ){ 511 flag_rtz = !flag_alternateform; 512 if( exp<-4 || exp>precision ){ 513 xtype = etEXP; 514 }else{ 515 precision = precision - exp; 516 xtype = etFLOAT; 517 } 518 }else{ 519 flag_rtz = 0; 520 } 521 if( xtype==etEXP ){ 522 e2 = 0; 523 }else{ 524 e2 = exp; 525 } 526 nsd = 0; 527 flag_dp = (precision>0) | flag_alternateform | flag_altform2; 528 /* The sign in front of the number */ 529 if( prefix ){ 530 *(bufpt++) = prefix; 531 } 532 /* Digits prior to the decimal point */ 533 if( e2<0 ){ 534 *(bufpt++) = '0'; 535 }else{ 536 for(; e2>=0; e2--){ 537 *(bufpt++) = et_getdigit(&realvalue,&nsd); 538 } 539 } 540 /* The decimal point */ 541 if( flag_dp ){ 542 *(bufpt++) = '.'; 543 } 544 /* "0" digits after the decimal point but before the first 545 ** significant digit of the number */ 546 for(e2++; e2<0 && precision>0; precision--, e2++){ 547 *(bufpt++) = '0'; 548 } 549 /* Significant digits after the decimal point */ 550 while( (precision--)>0 ){ 551 *(bufpt++) = et_getdigit(&realvalue,&nsd); 552 } 553 /* Remove trailing zeros and the "." if no digits follow the "." */ 554 if( flag_rtz && flag_dp ){ 555 while( bufpt[-1]=='0' ) *(--bufpt) = 0; 556 assert( bufpt>buf ); 557 if( bufpt[-1]=='.' ){ 558 if( flag_altform2 ){ 559 *(bufpt++) = '0'; 560 }else{ 561 *(--bufpt) = 0; 562 } 563 } 564 } 565 /* Add the "eNNN" suffix */ 566 if( flag_exp || (xtype==etEXP && exp) ){ 567 *(bufpt++) = aDigits[infop->charset]; 568 if( exp<0 ){ 569 *(bufpt++) = '-'; exp = -exp; 570 }else{ 571 *(bufpt++) = '+'; 572 } 573 if( exp>=100 ){ 574 *(bufpt++) = (exp/100)+'0'; /* 100's digit */ 575 exp %= 100; 576 } 577 *(bufpt++) = exp/10+'0'; /* 10's digit */ 578 *(bufpt++) = exp%10+'0'; /* 1's digit */ 579 } 580 *bufpt = 0; 581 582 /* The converted number is in buf[] and zero terminated. Output it. 583 ** Note that the number is in the usual order, not reversed as with 584 ** integer conversions. */ 585 length = bufpt-buf; 586 bufpt = buf; 587 588 /* Special case: Add leading zeros if the flag_zeropad flag is 589 ** set and we are not left justified */ 590 if( flag_zeropad && !flag_leftjustify && length < width){ 591 int i; 592 int nPad = width - length; 593 for(i=width; i>=nPad; i--){ 594 bufpt[i] = bufpt[i-nPad]; 595 } 596 i = prefix!=0; 597 while( nPad-- ) bufpt[i++] = '0'; 598 length = width; 599 } 600 #endif 601 break; 602 case etSIZE: 603 *(va_arg(ap,int*)) = pAccum->nChar; 604 length = width = 0; 605 break; 606 case etPERCENT: 607 buf[0] = '%'; 608 bufpt = buf; 609 length = 1; 610 break; 611 case etCHARLIT: 612 case etCHARX: 613 c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt); 614 if( precision>=0 ){ 615 for(idx=1; idx<precision; idx++) buf[idx] = c; 616 length = precision; 617 }else{ 618 length =1; 619 } 620 bufpt = buf; 621 break; 622 case etSTRING: 623 case etDYNSTRING: 624 bufpt = va_arg(ap,char*); 625 if( bufpt==0 ){ 626 bufpt = ""; 627 }else if( xtype==etDYNSTRING ){ 628 zExtra = bufpt; 629 } 630 length = strlen(bufpt); 631 if( precision>=0 && precision<length ) length = precision; 632 break; 633 case etSQLESCAPE: 634 case etSQLESCAPE2: 635 case etSQLESCAPE3: { 636 int i, j, n, ch, isnull; 637 int needQuote; 638 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */ 639 char *escarg = va_arg(ap,char*); 640 isnull = escarg==0; 641 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); 642 for(i=n=0; (ch=escarg[i])!=0; i++){ 643 if( ch==q ) n++; 644 } 645 needQuote = !isnull && xtype==etSQLESCAPE2; 646 n += i + 1 + needQuote*2; 647 if( n>etBUFSIZE ){ 648 bufpt = zExtra = sqlite3_malloc( n ); 649 if( bufpt==0 ) return; 650 }else{ 651 bufpt = buf; 652 } 653 j = 0; 654 if( needQuote ) bufpt[j++] = q; 655 for(i=0; (ch=escarg[i])!=0; i++){ 656 bufpt[j++] = ch; 657 if( ch==q ) bufpt[j++] = ch; 658 } 659 if( needQuote ) bufpt[j++] = q; 660 bufpt[j] = 0; 661 length = j; 662 /* The precision is ignored on %q and %Q */ 663 /* if( precision>=0 && precision<length ) length = precision; */ 664 break; 665 } 666 case etTOKEN: { 667 Token *pToken = va_arg(ap, Token*); 668 if( pToken && pToken->z ){ 669 sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n); 670 } 671 length = width = 0; 672 break; 673 } 674 case etSRCLIST: { 675 SrcList *pSrc = va_arg(ap, SrcList*); 676 int k = va_arg(ap, int); 677 struct SrcList_item *pItem = &pSrc->a[k]; 678 assert( k>=0 && k<pSrc->nSrc ); 679 if( pItem->zDatabase && pItem->zDatabase[0] ){ 680 sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1); 681 sqlite3StrAccumAppend(pAccum, ".", 1); 682 } 683 sqlite3StrAccumAppend(pAccum, pItem->zName, -1); 684 length = width = 0; 685 break; 686 } 687 }/* End switch over the format type */ 688 /* 689 ** The text of the conversion is pointed to by "bufpt" and is 690 ** "length" characters long. The field width is "width". Do 691 ** the output. 692 */ 693 if( !flag_leftjustify ){ 694 register int nspace; 695 nspace = width-length; 696 if( nspace>0 ){ 697 appendSpace(pAccum, nspace); 698 } 699 } 700 if( length>0 ){ 701 sqlite3StrAccumAppend(pAccum, bufpt, length); 702 } 703 if( flag_leftjustify ){ 704 register int nspace; 705 nspace = width-length; 706 if( nspace>0 ){ 707 appendSpace(pAccum, nspace); 708 } 709 } 710 if( zExtra ){ 711 sqlite3_free(zExtra); 712 } 713 }/* End for loop over the format string */ 714 } /* End of function */ 715 716 /* 717 ** Append N bytes of text from z to the StrAccum object. 718 */ 719 void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){ 720 if( p->tooBig | p->mallocFailed ){ 721 return; 722 } 723 if( N<0 ){ 724 N = strlen(z); 725 } 726 if( N==0 ){ 727 return; 728 } 729 if( p->nChar+N >= p->nAlloc ){ 730 char *zNew; 731 if( !p->useMalloc ){ 732 p->tooBig = 1; 733 N = p->nAlloc - p->nChar - 1; 734 if( N<=0 ){ 735 return; 736 } 737 }else{ 738 i64 szNew = p->nAlloc; 739 szNew += N + 1; 740 if( szNew > p->mxAlloc ){ 741 p->nAlloc = p->mxAlloc; 742 if( ((i64)p->nChar)+((i64)N) >= p->nAlloc ){ 743 sqlite3StrAccumReset(p); 744 p->tooBig = 1; 745 return; 746 } 747 }else{ 748 p->nAlloc = szNew; 749 } 750 zNew = sqlite3_malloc( p->nAlloc ); 751 if( zNew ){ 752 memcpy(zNew, p->zText, p->nChar); 753 sqlite3StrAccumReset(p); 754 p->zText = zNew; 755 }else{ 756 p->mallocFailed = 1; 757 sqlite3StrAccumReset(p); 758 return; 759 } 760 } 761 } 762 memcpy(&p->zText[p->nChar], z, N); 763 p->nChar += N; 764 } 765 766 /* 767 ** Finish off a string by making sure it is zero-terminated. 768 ** Return a pointer to the resulting string. Return a NULL 769 ** pointer if any kind of error was encountered. 770 */ 771 char *sqlite3StrAccumFinish(StrAccum *p){ 772 if( p->zText ){ 773 p->zText[p->nChar] = 0; 774 if( p->useMalloc && p->zText==p->zBase ){ 775 p->zText = sqlite3_malloc( p->nChar+1 ); 776 if( p->zText ){ 777 memcpy(p->zText, p->zBase, p->nChar+1); 778 }else{ 779 p->mallocFailed = 1; 780 } 781 } 782 } 783 return p->zText; 784 } 785 786 /* 787 ** Reset an StrAccum string. Reclaim all malloced memory. 788 */ 789 void sqlite3StrAccumReset(StrAccum *p){ 790 if( p->zText!=p->zBase ){ 791 sqlite3_free(p->zText); 792 p->zText = 0; 793 } 794 } 795 796 /* 797 ** Initialize a string accumulator 798 */ 799 static void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){ 800 p->zText = p->zBase = zBase; 801 p->nChar = 0; 802 p->nAlloc = n; 803 p->mxAlloc = mx; 804 p->useMalloc = 1; 805 p->tooBig = 0; 806 p->mallocFailed = 0; 807 } 808 809 /* 810 ** Print into memory obtained from sqliteMalloc(). Use the internal 811 ** %-conversion extensions. 812 */ 813 char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ 814 char *z; 815 char zBase[SQLITE_PRINT_BUF_SIZE]; 816 StrAccum acc; 817 sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), 818 db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH); 819 vxprintf(&acc, 1, zFormat, ap); 820 z = sqlite3StrAccumFinish(&acc); 821 if( acc.mallocFailed && db ){ 822 db->mallocFailed = 1; 823 } 824 return z; 825 } 826 827 /* 828 ** Print into memory obtained from sqliteMalloc(). Use the internal 829 ** %-conversion extensions. 830 */ 831 char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ 832 va_list ap; 833 char *z; 834 va_start(ap, zFormat); 835 z = sqlite3VMPrintf(db, zFormat, ap); 836 va_end(ap); 837 return z; 838 } 839 840 /* 841 ** Print into memory obtained from sqlite3_malloc(). Omit the internal 842 ** %-conversion extensions. 843 */ 844 char *sqlite3_vmprintf(const char *zFormat, va_list ap){ 845 char *z; 846 char zBase[SQLITE_PRINT_BUF_SIZE]; 847 StrAccum acc; 848 sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); 849 vxprintf(&acc, 0, zFormat, ap); 850 z = sqlite3StrAccumFinish(&acc); 851 return z; 852 } 853 854 /* 855 ** Print into memory obtained from sqlite3_malloc()(). Omit the internal 856 ** %-conversion extensions. 857 */ 858 char *sqlite3_mprintf(const char *zFormat, ...){ 859 va_list ap; 860 char *z; 861 va_start(ap, zFormat); 862 z = sqlite3_vmprintf(zFormat, ap); 863 va_end(ap); 864 return z; 865 } 866 867 /* 868 ** sqlite3_snprintf() works like snprintf() except that it ignores the 869 ** current locale settings. This is important for SQLite because we 870 ** are not able to use a "," as the decimal point in place of "." as 871 ** specified by some locales. 872 */ 873 char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ 874 char *z; 875 va_list ap; 876 StrAccum acc; 877 878 if( n<=0 ){ 879 return zBuf; 880 } 881 sqlite3StrAccumInit(&acc, zBuf, n, 0); 882 acc.useMalloc = 0; 883 va_start(ap,zFormat); 884 vxprintf(&acc, 0, zFormat, ap); 885 va_end(ap); 886 z = sqlite3StrAccumFinish(&acc); 887 return z; 888 } 889 890 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) || defined(SQLITE_MEMDEBUG) 891 /* 892 ** A version of printf() that understands %lld. Used for debugging. 893 ** The printf() built into some versions of windows does not understand %lld 894 ** and segfaults if you give it a long long int. 895 */ 896 void sqlite3DebugPrintf(const char *zFormat, ...){ 897 va_list ap; 898 StrAccum acc; 899 char zBuf[500]; 900 sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0); 901 acc.useMalloc = 0; 902 va_start(ap,zFormat); 903 vxprintf(&acc, 0, zFormat, ap); 904 va_end(ap); 905 sqlite3StrAccumFinish(&acc); 906 fprintf(stdout,"%s", zBuf); 907 fflush(stdout); 908 } 909 #endif 910