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