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