1a18c5681Sdrh /* 2a18c5681Sdrh ** The "printf" code that follows dates from the 1980's. It is in 3a18c5681Sdrh ** the public domain. The original comments are included here for 4e84a306bSdrh ** completeness. They are very out-of-date but might be useful as 5e84a306bSdrh ** an historical reference. Most of the "enhancements" have been backed 6e84a306bSdrh ** out so that the functionality is now the same as standard printf(). 7e84a306bSdrh ** 8e84a306bSdrh ************************************************************************** 9a18c5681Sdrh ** 10e78e8284Sdrh ** The following modules is an enhanced replacement for the "printf" subroutines 11e78e8284Sdrh ** found in the standard C library. The following enhancements are 12a18c5681Sdrh ** supported: 13a18c5681Sdrh ** 14a18c5681Sdrh ** + Additional functions. The standard set of "printf" functions 15a18c5681Sdrh ** includes printf, fprintf, sprintf, vprintf, vfprintf, and 16a18c5681Sdrh ** vsprintf. This module adds the following: 17a18c5681Sdrh ** 18a18c5681Sdrh ** * snprintf -- Works like sprintf, but has an extra argument 19a18c5681Sdrh ** which is the size of the buffer written to. 20a18c5681Sdrh ** 21a18c5681Sdrh ** * mprintf -- Similar to sprintf. Writes output to memory 22a18c5681Sdrh ** obtained from malloc. 23a18c5681Sdrh ** 24a18c5681Sdrh ** * xprintf -- Calls a function to dispose of output. 25a18c5681Sdrh ** 26a18c5681Sdrh ** * nprintf -- No output, but returns the number of characters 27a18c5681Sdrh ** that would have been output by printf. 28a18c5681Sdrh ** 29a18c5681Sdrh ** * A v- version (ex: vsnprintf) of every function is also 30a18c5681Sdrh ** supplied. 31a18c5681Sdrh ** 32a18c5681Sdrh ** + A few extensions to the formatting notation are supported: 33a18c5681Sdrh ** 34a18c5681Sdrh ** * The "=" flag (similar to "-") causes the output to be 35a18c5681Sdrh ** be centered in the appropriately sized field. 36a18c5681Sdrh ** 37a18c5681Sdrh ** * The %b field outputs an integer in binary notation. 38a18c5681Sdrh ** 39a18c5681Sdrh ** * The %c field now accepts a precision. The character output 40a18c5681Sdrh ** is repeated by the number of times the precision specifies. 41a18c5681Sdrh ** 42a18c5681Sdrh ** * The %' field works like %c, but takes as its character the 43a18c5681Sdrh ** next character of the format string, instead of the next 44a18c5681Sdrh ** argument. For example, printf("%.78'-") prints 78 minus 45a18c5681Sdrh ** signs, the same as printf("%.78c",'-'). 46a18c5681Sdrh ** 47a18c5681Sdrh ** + When compiled using GCC on a SPARC, this version of printf is 48a18c5681Sdrh ** faster than the library printf for SUN OS 4.1. 49a18c5681Sdrh ** 50a18c5681Sdrh ** + All functions are fully reentrant. 51a18c5681Sdrh ** 52a18c5681Sdrh */ 53a18c5681Sdrh #include "sqliteInt.h" 5453c14021Sdrh #include <math.h> 557c68d60bSdrh 56a18c5681Sdrh /* 57a18c5681Sdrh ** Conversion types fall into various categories as defined by the 58a18c5681Sdrh ** following enumeration. 59a18c5681Sdrh */ 60e84a306bSdrh #define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */ 61e84a306bSdrh #define etFLOAT 2 /* Floating point. %f */ 62e84a306bSdrh #define etEXP 3 /* Exponentional notation. %e and %E */ 63e84a306bSdrh #define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */ 64e84a306bSdrh #define etSIZE 5 /* Return number of characters processed so far. %n */ 65e84a306bSdrh #define etSTRING 6 /* Strings. %s */ 66e84a306bSdrh #define etDYNSTRING 7 /* Dynamically allocated strings. %z */ 67e84a306bSdrh #define etPERCENT 8 /* Percent symbol. %% */ 68e84a306bSdrh #define etCHARX 9 /* Characters. %c */ 69a18c5681Sdrh /* The rest are extensions, not normally found in printf() */ 7005a82983Sdrh #define etCHARLIT 10 /* Literal characters. %' */ 7105a82983Sdrh #define etSQLESCAPE 11 /* Strings with '\'' doubled. %q */ 7205a82983Sdrh #define etSQLESCAPE2 12 /* Strings with '\'' doubled and enclosed in '', 730cfcf3fbSchw NULL pointers replaced by SQL NULL. %Q */ 7405a82983Sdrh #define etTOKEN 13 /* a pointer to a Token structure */ 7505a82983Sdrh #define etSRCLIST 14 /* a pointer to a SrcList */ 7605a82983Sdrh #define etPOINTER 15 /* The %p conversion */ 77e84a306bSdrh 78e84a306bSdrh 79e84a306bSdrh /* 80e84a306bSdrh ** An "etByte" is an 8-bit unsigned value. 81e84a306bSdrh */ 82e84a306bSdrh typedef unsigned char etByte; 83a18c5681Sdrh 84a18c5681Sdrh /* 85a18c5681Sdrh ** Each builtin conversion character (ex: the 'd' in "%d") is described 86a18c5681Sdrh ** by an instance of the following structure 87a18c5681Sdrh */ 88a18c5681Sdrh typedef struct et_info { /* Information about each format field */ 89e84a306bSdrh char fmttype; /* The format field code letter */ 90e84a306bSdrh etByte base; /* The base for radix conversion */ 91e84a306bSdrh etByte flags; /* One or more of FLAG_ constants below */ 92e84a306bSdrh etByte type; /* Conversion paradigm */ 9376ff3a0eSdrh etByte charset; /* Offset into aDigits[] of the digits string */ 9476ff3a0eSdrh etByte prefix; /* Offset into aPrefix[] of the prefix string */ 95a18c5681Sdrh } et_info; 96a18c5681Sdrh 97a18c5681Sdrh /* 98e84a306bSdrh ** Allowed values for et_info.flags 99e84a306bSdrh */ 100e84a306bSdrh #define FLAG_SIGNED 1 /* True if the value to convert is signed */ 101e84a306bSdrh #define FLAG_INTERN 2 /* True if for internal use only */ 1024794f735Sdrh #define FLAG_STRING 4 /* Allow infinity precision */ 103e84a306bSdrh 104e84a306bSdrh 105e84a306bSdrh /* 106a18c5681Sdrh ** The following table is searched linearly, so it is good to put the 107a18c5681Sdrh ** most frequently used conversion types first. 108a18c5681Sdrh */ 10976ff3a0eSdrh static const char aDigits[] = "0123456789ABCDEF0123456789abcdef"; 11076ff3a0eSdrh static const char aPrefix[] = "-x0\000X0"; 1115719628aSdrh static const et_info fmtinfo[] = { 11276ff3a0eSdrh { 'd', 10, 1, etRADIX, 0, 0 }, 1134794f735Sdrh { 's', 0, 4, etSTRING, 0, 0 }, 114557cc60fSdrh { 'g', 0, 1, etGENERIC, 30, 0 }, 1154794f735Sdrh { 'z', 0, 6, etDYNSTRING, 0, 0 }, 1164794f735Sdrh { 'q', 0, 4, etSQLESCAPE, 0, 0 }, 1174794f735Sdrh { 'Q', 0, 4, etSQLESCAPE2, 0, 0 }, 118e84a306bSdrh { 'c', 0, 0, etCHARX, 0, 0 }, 11976ff3a0eSdrh { 'o', 8, 0, etRADIX, 0, 2 }, 12076ff3a0eSdrh { 'u', 10, 0, etRADIX, 0, 0 }, 12176ff3a0eSdrh { 'x', 16, 0, etRADIX, 16, 1 }, 12276ff3a0eSdrh { 'X', 16, 0, etRADIX, 0, 4 }, 123b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 124e84a306bSdrh { 'f', 0, 1, etFLOAT, 0, 0 }, 12576ff3a0eSdrh { 'e', 0, 1, etEXP, 30, 0 }, 12676ff3a0eSdrh { 'E', 0, 1, etEXP, 14, 0 }, 12776ff3a0eSdrh { 'G', 0, 1, etGENERIC, 14, 0 }, 128b37df7b9Sdrh #endif 12976ff3a0eSdrh { 'i', 10, 1, etRADIX, 0, 0 }, 130e84a306bSdrh { 'n', 0, 0, etSIZE, 0, 0 }, 131e84a306bSdrh { '%', 0, 0, etPERCENT, 0, 0 }, 13276ff3a0eSdrh { 'p', 16, 0, etPOINTER, 0, 1 }, 1335f968436Sdrh { 'T', 0, 2, etTOKEN, 0, 0 }, 1345f968436Sdrh { 'S', 0, 2, etSRCLIST, 0, 0 }, 135a18c5681Sdrh }; 136a18c5681Sdrh #define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0])) 137a18c5681Sdrh 138a18c5681Sdrh /* 139b37df7b9Sdrh ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point 140a18c5681Sdrh ** conversions will work. 141a18c5681Sdrh */ 142b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 143a18c5681Sdrh /* 144a18c5681Sdrh ** "*val" is a double such that 0.1 <= *val < 10.0 145a18c5681Sdrh ** Return the ascii code for the leading digit of *val, then 146a18c5681Sdrh ** multiply "*val" by 10.0 to renormalize. 147a18c5681Sdrh ** 148a18c5681Sdrh ** Example: 149a18c5681Sdrh ** input: *val = 3.14159 150a18c5681Sdrh ** output: *val = 1.4159 function return = '3' 151a18c5681Sdrh ** 152a18c5681Sdrh ** The counter *cnt is incremented each time. After counter exceeds 153a18c5681Sdrh ** 16 (the number of significant digits in a 64-bit float) '0' is 154a18c5681Sdrh ** always returned. 155a18c5681Sdrh */ 156384eef32Sdrh static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ 157a18c5681Sdrh int digit; 158384eef32Sdrh LONGDOUBLE_TYPE d; 159a18c5681Sdrh if( (*cnt)++ >= 16 ) return '0'; 160a18c5681Sdrh digit = (int)*val; 161a18c5681Sdrh d = digit; 162a18c5681Sdrh digit += '0'; 163a18c5681Sdrh *val = (*val - d)*10.0; 164a18c5681Sdrh return digit; 165a18c5681Sdrh } 166b37df7b9Sdrh #endif /* SQLITE_OMIT_FLOATING_POINT */ 167a18c5681Sdrh 16879158e18Sdrh /* 16979158e18Sdrh ** On machines with a small stack size, you can redefine the 17079158e18Sdrh ** SQLITE_PRINT_BUF_SIZE to be less than 350. But beware - for 17179158e18Sdrh ** smaller values some %f conversions may go into an infinite loop. 17279158e18Sdrh */ 17379158e18Sdrh #ifndef SQLITE_PRINT_BUF_SIZE 17479158e18Sdrh # define SQLITE_PRINT_BUF_SIZE 350 17579158e18Sdrh #endif 17679158e18Sdrh #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ 177a18c5681Sdrh 178a18c5681Sdrh /* 179a18c5681Sdrh ** The root program. All variations call this core. 180a18c5681Sdrh ** 181a18c5681Sdrh ** INPUTS: 182a18c5681Sdrh ** func This is a pointer to a function taking three arguments 183a18c5681Sdrh ** 1. A pointer to anything. Same as the "arg" parameter. 184a18c5681Sdrh ** 2. A pointer to the list of characters to be output 185a18c5681Sdrh ** (Note, this list is NOT null terminated.) 186a18c5681Sdrh ** 3. An integer number of characters to be output. 187a18c5681Sdrh ** (Note: This number might be zero.) 188a18c5681Sdrh ** 189a18c5681Sdrh ** arg This is the pointer to anything which will be passed as the 190a18c5681Sdrh ** first argument to "func". Use it for whatever you like. 191a18c5681Sdrh ** 192a18c5681Sdrh ** fmt This is the format string, as in the usual print. 193a18c5681Sdrh ** 194a18c5681Sdrh ** ap This is a pointer to a list of arguments. Same as in 195a18c5681Sdrh ** vfprint. 196a18c5681Sdrh ** 197a18c5681Sdrh ** OUTPUTS: 198a18c5681Sdrh ** The return value is the total number of characters sent to 199a18c5681Sdrh ** the function "func". Returns -1 on a error. 200a18c5681Sdrh ** 201a18c5681Sdrh ** Note that the order in which automatic variables are declared below 202a18c5681Sdrh ** seems to make a big difference in determining how fast this beast 203a18c5681Sdrh ** will run. 204a18c5681Sdrh */ 205a18c5681Sdrh static int vxprintf( 2065f968436Sdrh void (*func)(void*,const char*,int), /* Consumer of text */ 2075f968436Sdrh void *arg, /* First argument to the consumer */ 2085f968436Sdrh int useExtended, /* Allow extended %-conversions */ 2095f968436Sdrh const char *fmt, /* Format string */ 2105f968436Sdrh va_list ap /* arguments */ 211a18c5681Sdrh ){ 212e84a306bSdrh int c; /* Next character in the format string */ 213e84a306bSdrh char *bufpt; /* Pointer to the conversion buffer */ 214e84a306bSdrh int precision; /* Precision of the current field */ 215e84a306bSdrh int length; /* Length of the field */ 216e84a306bSdrh int idx; /* A general purpose loop counter */ 217a18c5681Sdrh int count; /* Total number of characters output */ 218a18c5681Sdrh int width; /* Width of the current field */ 219e84a306bSdrh etByte flag_leftjustify; /* True if "-" flag is present */ 220e84a306bSdrh etByte flag_plussign; /* True if "+" flag is present */ 221e84a306bSdrh etByte flag_blanksign; /* True if " " flag is present */ 222e84a306bSdrh etByte flag_alternateform; /* True if "#" flag is present */ 223531fe878Sdrh etByte flag_altform2; /* True if "!" flag is present */ 224e84a306bSdrh etByte flag_zeropad; /* True if field width constant starts with zero */ 225e84a306bSdrh etByte flag_long; /* True if "l" flag is present */ 226a34b6764Sdrh etByte flag_longlong; /* True if the "ll" flag is present */ 2273e9aeec0Sdrh etByte done; /* Loop termination flag */ 22827436af7Sdrh sqlite_uint64 longvalue; /* Value for integer types */ 229384eef32Sdrh LONGDOUBLE_TYPE realvalue; /* Value for real types */ 2305719628aSdrh const et_info *infop; /* Pointer to the appropriate info structure */ 231a18c5681Sdrh char buf[etBUFSIZE]; /* Conversion buffer */ 232a18c5681Sdrh char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ 233e84a306bSdrh etByte errorflag = 0; /* True if an error is encountered */ 234e84a306bSdrh etByte xtype; /* Conversion paradigm */ 235a18c5681Sdrh char *zExtra; /* Extra memory used for etTCLESCAPE conversions */ 2365719628aSdrh static const char spaces[] = 2375719628aSdrh " "; 238a18c5681Sdrh #define etSPACESIZE (sizeof(spaces)-1) 239b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 240557cc60fSdrh int exp, e2; /* exponent of real numbers */ 241a18c5681Sdrh double rounder; /* Used for rounding floating point values */ 242e84a306bSdrh etByte flag_dp; /* True if decimal point should be shown */ 243e84a306bSdrh etByte flag_rtz; /* True if trailing zeros should be removed */ 244e84a306bSdrh etByte flag_exp; /* True to force display of the exponent */ 245a18c5681Sdrh int nsd; /* Number of significant digits returned */ 246a18c5681Sdrh #endif 247a18c5681Sdrh 248e29b1a05Sdrh func(arg,"",0); 249a18c5681Sdrh count = length = 0; 250a18c5681Sdrh bufpt = 0; 251a18c5681Sdrh for(; (c=(*fmt))!=0; ++fmt){ 252a18c5681Sdrh if( c!='%' ){ 253e84a306bSdrh int amt; 254a18c5681Sdrh bufpt = (char *)fmt; 255a18c5681Sdrh amt = 1; 256a18c5681Sdrh while( (c=(*++fmt))!='%' && c!=0 ) amt++; 257a18c5681Sdrh (*func)(arg,bufpt,amt); 258a18c5681Sdrh count += amt; 259a18c5681Sdrh if( c==0 ) break; 260a18c5681Sdrh } 261a18c5681Sdrh if( (c=(*++fmt))==0 ){ 262a18c5681Sdrh errorflag = 1; 263a18c5681Sdrh (*func)(arg,"%",1); 264a18c5681Sdrh count++; 265a18c5681Sdrh break; 266a18c5681Sdrh } 267a18c5681Sdrh /* Find out what flags are present */ 268a18c5681Sdrh flag_leftjustify = flag_plussign = flag_blanksign = 269557cc60fSdrh flag_alternateform = flag_altform2 = flag_zeropad = 0; 2703e9aeec0Sdrh done = 0; 271a18c5681Sdrh do{ 272a18c5681Sdrh switch( c ){ 2733e9aeec0Sdrh case '-': flag_leftjustify = 1; break; 2743e9aeec0Sdrh case '+': flag_plussign = 1; break; 2753e9aeec0Sdrh case ' ': flag_blanksign = 1; break; 2763e9aeec0Sdrh case '#': flag_alternateform = 1; break; 2773e9aeec0Sdrh case '!': flag_altform2 = 1; break; 2783e9aeec0Sdrh case '0': flag_zeropad = 1; break; 2793e9aeec0Sdrh default: done = 1; break; 280a18c5681Sdrh } 2813e9aeec0Sdrh }while( !done && (c=(*++fmt))!=0 ); 282a18c5681Sdrh /* Get the field width */ 283a18c5681Sdrh width = 0; 284a18c5681Sdrh if( c=='*' ){ 285a18c5681Sdrh width = va_arg(ap,int); 286a18c5681Sdrh if( width<0 ){ 287a18c5681Sdrh flag_leftjustify = 1; 288a18c5681Sdrh width = -width; 289a18c5681Sdrh } 290a18c5681Sdrh c = *++fmt; 291a18c5681Sdrh }else{ 29217a68934Sdrh while( c>='0' && c<='9' ){ 293a18c5681Sdrh width = width*10 + c - '0'; 294a18c5681Sdrh c = *++fmt; 295a18c5681Sdrh } 296a18c5681Sdrh } 297a18c5681Sdrh if( width > etBUFSIZE-10 ){ 298a18c5681Sdrh width = etBUFSIZE-10; 299a18c5681Sdrh } 300a18c5681Sdrh /* Get the precision */ 301a18c5681Sdrh if( c=='.' ){ 302a18c5681Sdrh precision = 0; 303a18c5681Sdrh c = *++fmt; 304a18c5681Sdrh if( c=='*' ){ 305a18c5681Sdrh precision = va_arg(ap,int); 306a18c5681Sdrh if( precision<0 ) precision = -precision; 307a18c5681Sdrh c = *++fmt; 308a18c5681Sdrh }else{ 30917a68934Sdrh while( c>='0' && c<='9' ){ 310a18c5681Sdrh precision = precision*10 + c - '0'; 311a18c5681Sdrh c = *++fmt; 312a18c5681Sdrh } 313a18c5681Sdrh } 314a18c5681Sdrh }else{ 315a18c5681Sdrh precision = -1; 316a18c5681Sdrh } 317a18c5681Sdrh /* Get the conversion type modifier */ 318a18c5681Sdrh if( c=='l' ){ 319a18c5681Sdrh flag_long = 1; 320a18c5681Sdrh c = *++fmt; 321a34b6764Sdrh if( c=='l' ){ 322a34b6764Sdrh flag_longlong = 1; 323a34b6764Sdrh c = *++fmt; 324a18c5681Sdrh }else{ 325a34b6764Sdrh flag_longlong = 0; 326a34b6764Sdrh } 327a34b6764Sdrh }else{ 328a34b6764Sdrh flag_long = flag_longlong = 0; 329a18c5681Sdrh } 330a18c5681Sdrh /* Fetch the info entry for the field */ 331a18c5681Sdrh infop = 0; 332a18c5681Sdrh for(idx=0; idx<etNINFO; idx++){ 333a18c5681Sdrh if( c==fmtinfo[idx].fmttype ){ 334a18c5681Sdrh infop = &fmtinfo[idx]; 3355f968436Sdrh if( useExtended || (infop->flags & FLAG_INTERN)==0 ){ 336e84a306bSdrh xtype = infop->type; 337c4413565Sdrh }else{ 338c4413565Sdrh return -1; 3395f968436Sdrh } 340a18c5681Sdrh break; 341a18c5681Sdrh } 342a18c5681Sdrh } 343a18c5681Sdrh zExtra = 0; 34443617e9aSdrh if( infop==0 ){ 34543617e9aSdrh return -1; 34643617e9aSdrh } 34743617e9aSdrh 348a18c5681Sdrh 3494794f735Sdrh /* Limit the precision to prevent overflowing buf[] during conversion */ 3504794f735Sdrh if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){ 3514794f735Sdrh precision = etBUFSIZE-40; 3524794f735Sdrh } 3534794f735Sdrh 354a18c5681Sdrh /* 355a18c5681Sdrh ** At this point, variables are initialized as follows: 356a18c5681Sdrh ** 357a18c5681Sdrh ** flag_alternateform TRUE if a '#' is present. 3583e9aeec0Sdrh ** flag_altform2 TRUE if a '!' is present. 359a18c5681Sdrh ** flag_plussign TRUE if a '+' is present. 360a18c5681Sdrh ** flag_leftjustify TRUE if a '-' is present or if the 361a18c5681Sdrh ** field width was negative. 362a18c5681Sdrh ** flag_zeropad TRUE if the width began with 0. 363a18c5681Sdrh ** flag_long TRUE if the letter 'l' (ell) prefixed 364a18c5681Sdrh ** the conversion character. 365a34b6764Sdrh ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed 366a34b6764Sdrh ** the conversion character. 367a18c5681Sdrh ** flag_blanksign TRUE if a ' ' is present. 368a18c5681Sdrh ** width The specified field width. This is 369a18c5681Sdrh ** always non-negative. Zero is the default. 370a18c5681Sdrh ** precision The specified precision. The default 371a18c5681Sdrh ** is -1. 372a18c5681Sdrh ** xtype The class of the conversion. 373a18c5681Sdrh ** infop Pointer to the appropriate info struct. 374a18c5681Sdrh */ 375a18c5681Sdrh switch( xtype ){ 376fe63d1c9Sdrh case etPOINTER: 377fe63d1c9Sdrh flag_longlong = sizeof(char*)==sizeof(i64); 378fe63d1c9Sdrh flag_long = sizeof(char*)==sizeof(long int); 379fe63d1c9Sdrh /* Fall through into the next case */ 380a18c5681Sdrh case etRADIX: 381e84a306bSdrh if( infop->flags & FLAG_SIGNED ){ 382e9707671Sdrh i64 v; 383e9707671Sdrh if( flag_longlong ) v = va_arg(ap,i64); 384e9707671Sdrh else if( flag_long ) v = va_arg(ap,long int); 385e9707671Sdrh else v = va_arg(ap,int); 386e9707671Sdrh if( v<0 ){ 387e9707671Sdrh longvalue = -v; 388cfcdaefeSdanielk1977 prefix = '-'; 389cfcdaefeSdanielk1977 }else{ 390e9707671Sdrh longvalue = v; 391e9707671Sdrh if( flag_plussign ) prefix = '+'; 392a18c5681Sdrh else if( flag_blanksign ) prefix = ' '; 393a18c5681Sdrh else prefix = 0; 394cfcdaefeSdanielk1977 } 395e9707671Sdrh }else{ 396e9707671Sdrh if( flag_longlong ) longvalue = va_arg(ap,u64); 397e9707671Sdrh else if( flag_long ) longvalue = va_arg(ap,unsigned long int); 398e9707671Sdrh else longvalue = va_arg(ap,unsigned int); 399e9707671Sdrh prefix = 0; 400e9707671Sdrh } 401e9707671Sdrh if( longvalue==0 ) flag_alternateform = 0; 402a18c5681Sdrh if( flag_zeropad && precision<width-(prefix!=0) ){ 403a18c5681Sdrh precision = width-(prefix!=0); 404a18c5681Sdrh } 4053039c0a8Sdrh bufpt = &buf[etBUFSIZE-1]; 406a18c5681Sdrh { 40776ff3a0eSdrh register const char *cset; /* Use registers for speed */ 408a18c5681Sdrh register int base; 40976ff3a0eSdrh cset = &aDigits[infop->charset]; 410a18c5681Sdrh base = infop->base; 411a18c5681Sdrh do{ /* Convert to ascii */ 412a18c5681Sdrh *(--bufpt) = cset[longvalue%base]; 413a18c5681Sdrh longvalue = longvalue/base; 414a18c5681Sdrh }while( longvalue>0 ); 415a18c5681Sdrh } 4163039c0a8Sdrh length = &buf[etBUFSIZE-1]-bufpt; 417a18c5681Sdrh for(idx=precision-length; idx>0; idx--){ 418a18c5681Sdrh *(--bufpt) = '0'; /* Zero pad */ 419a18c5681Sdrh } 420a18c5681Sdrh if( prefix ) *(--bufpt) = prefix; /* Add sign */ 421a18c5681Sdrh if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ 42276ff3a0eSdrh const char *pre; 42376ff3a0eSdrh char x; 42476ff3a0eSdrh pre = &aPrefix[infop->prefix]; 425a18c5681Sdrh if( *bufpt!=pre[0] ){ 42676ff3a0eSdrh for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; 427a18c5681Sdrh } 428a18c5681Sdrh } 4293039c0a8Sdrh length = &buf[etBUFSIZE-1]-bufpt; 430a18c5681Sdrh break; 431a18c5681Sdrh case etFLOAT: 432a18c5681Sdrh case etEXP: 433a18c5681Sdrh case etGENERIC: 434a18c5681Sdrh realvalue = va_arg(ap,double); 435b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 436a18c5681Sdrh if( precision<0 ) precision = 6; /* Set default precision */ 4375eba8c09Sdrh if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10; 438a18c5681Sdrh if( realvalue<0.0 ){ 439a18c5681Sdrh realvalue = -realvalue; 440a18c5681Sdrh prefix = '-'; 441a18c5681Sdrh }else{ 442a18c5681Sdrh if( flag_plussign ) prefix = '+'; 443a18c5681Sdrh else if( flag_blanksign ) prefix = ' '; 444a18c5681Sdrh else prefix = 0; 445a18c5681Sdrh } 4463e9aeec0Sdrh if( xtype==etGENERIC && precision>0 ) precision--; 4475f968436Sdrh #if 0 448a18c5681Sdrh /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */ 449a18c5681Sdrh for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1); 450a18c5681Sdrh #else 451a18c5681Sdrh /* It makes more sense to use 0.5 */ 45274161705Sdrh for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){} 453a18c5681Sdrh #endif 4543e9aeec0Sdrh if( xtype==etFLOAT ) realvalue += rounder; 455a18c5681Sdrh /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ 456a18c5681Sdrh exp = 0; 457*7cd6927eSdrh if( sqlite3_isnan(realvalue) ){ 45853c14021Sdrh bufpt = "NaN"; 45953c14021Sdrh length = 3; 46053c14021Sdrh break; 46153c14021Sdrh } 462a18c5681Sdrh if( realvalue>0.0 ){ 46363782855Sdrh while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; } 46463782855Sdrh while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; } 46563782855Sdrh while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; } 466b621c237Sdrh while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; } 467b621c237Sdrh while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; } 468b621c237Sdrh if( exp>350 || exp<-350 ){ 46953c14021Sdrh if( prefix=='-' ){ 47053c14021Sdrh bufpt = "-Inf"; 47153c14021Sdrh }else if( prefix=='+' ){ 47253c14021Sdrh bufpt = "+Inf"; 47353c14021Sdrh }else{ 47453c14021Sdrh bufpt = "Inf"; 47553c14021Sdrh } 47653c14021Sdrh length = strlen(bufpt); 477a18c5681Sdrh break; 478a18c5681Sdrh } 479a18c5681Sdrh } 480a18c5681Sdrh bufpt = buf; 481a18c5681Sdrh /* 482a18c5681Sdrh ** If the field type is etGENERIC, then convert to either etEXP 483a18c5681Sdrh ** or etFLOAT, as appropriate. 484a18c5681Sdrh */ 485a18c5681Sdrh flag_exp = xtype==etEXP; 486a18c5681Sdrh if( xtype!=etFLOAT ){ 487a18c5681Sdrh realvalue += rounder; 488a18c5681Sdrh if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } 489a18c5681Sdrh } 490a18c5681Sdrh if( xtype==etGENERIC ){ 491a18c5681Sdrh flag_rtz = !flag_alternateform; 492a18c5681Sdrh if( exp<-4 || exp>precision ){ 493a18c5681Sdrh xtype = etEXP; 494a18c5681Sdrh }else{ 495a18c5681Sdrh precision = precision - exp; 496a18c5681Sdrh xtype = etFLOAT; 497a18c5681Sdrh } 498a18c5681Sdrh }else{ 499a18c5681Sdrh flag_rtz = 0; 500a18c5681Sdrh } 501557cc60fSdrh if( xtype==etEXP ){ 502557cc60fSdrh e2 = 0; 503557cc60fSdrh }else{ 504557cc60fSdrh e2 = exp; 505557cc60fSdrh } 506a18c5681Sdrh nsd = 0; 507557cc60fSdrh flag_dp = (precision>0) | flag_alternateform | flag_altform2; 508557cc60fSdrh /* The sign in front of the number */ 509557cc60fSdrh if( prefix ){ 510557cc60fSdrh *(bufpt++) = prefix; 511557cc60fSdrh } 512557cc60fSdrh /* Digits prior to the decimal point */ 513557cc60fSdrh if( e2<0 ){ 514557cc60fSdrh *(bufpt++) = '0'; 515557cc60fSdrh }else{ 516557cc60fSdrh for(; e2>=0; e2--){ 517557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 518557cc60fSdrh } 519557cc60fSdrh } 520557cc60fSdrh /* The decimal point */ 521557cc60fSdrh if( flag_dp ){ 522557cc60fSdrh *(bufpt++) = '.'; 523557cc60fSdrh } 524557cc60fSdrh /* "0" digits after the decimal point but before the first 525557cc60fSdrh ** significant digit of the number */ 526557cc60fSdrh for(e2++; e2<0 && precision>0; precision--, e2++){ 527a18c5681Sdrh *(bufpt++) = '0'; 528a18c5681Sdrh } 529557cc60fSdrh /* Significant digits after the decimal point */ 530557cc60fSdrh while( (precision--)>0 ){ 531557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 532a18c5681Sdrh } 533557cc60fSdrh /* Remove trailing zeros and the "." if no digits follow the "." */ 534557cc60fSdrh if( flag_rtz && flag_dp ){ 5353e9aeec0Sdrh while( bufpt[-1]=='0' ) *(--bufpt) = 0; 5363e9aeec0Sdrh assert( bufpt>buf ); 5373e9aeec0Sdrh if( bufpt[-1]=='.' ){ 538557cc60fSdrh if( flag_altform2 ){ 539557cc60fSdrh *(bufpt++) = '0'; 540557cc60fSdrh }else{ 541557cc60fSdrh *(--bufpt) = 0; 542a18c5681Sdrh } 543557cc60fSdrh } 544557cc60fSdrh } 545557cc60fSdrh /* Add the "eNNN" suffix */ 546557cc60fSdrh if( flag_exp || (xtype==etEXP && exp) ){ 54776ff3a0eSdrh *(bufpt++) = aDigits[infop->charset]; 548557cc60fSdrh if( exp<0 ){ 549557cc60fSdrh *(bufpt++) = '-'; exp = -exp; 550557cc60fSdrh }else{ 551557cc60fSdrh *(bufpt++) = '+'; 552557cc60fSdrh } 553a18c5681Sdrh if( exp>=100 ){ 554a18c5681Sdrh *(bufpt++) = (exp/100)+'0'; /* 100's digit */ 555a18c5681Sdrh exp %= 100; 556a18c5681Sdrh } 557a18c5681Sdrh *(bufpt++) = exp/10+'0'; /* 10's digit */ 558a18c5681Sdrh *(bufpt++) = exp%10+'0'; /* 1's digit */ 559a18c5681Sdrh } 560557cc60fSdrh *bufpt = 0; 561557cc60fSdrh 562a18c5681Sdrh /* The converted number is in buf[] and zero terminated. Output it. 563a18c5681Sdrh ** Note that the number is in the usual order, not reversed as with 564a18c5681Sdrh ** integer conversions. */ 56594ce4c1eSdrh length = bufpt-buf; 566a18c5681Sdrh bufpt = buf; 567a18c5681Sdrh 568a18c5681Sdrh /* Special case: Add leading zeros if the flag_zeropad flag is 569a18c5681Sdrh ** set and we are not left justified */ 570a18c5681Sdrh if( flag_zeropad && !flag_leftjustify && length < width){ 571a18c5681Sdrh int i; 572a18c5681Sdrh int nPad = width - length; 573a18c5681Sdrh for(i=width; i>=nPad; i--){ 574a18c5681Sdrh bufpt[i] = bufpt[i-nPad]; 575a18c5681Sdrh } 576a18c5681Sdrh i = prefix!=0; 577a18c5681Sdrh while( nPad-- ) bufpt[i++] = '0'; 578a18c5681Sdrh length = width; 579a18c5681Sdrh } 580a18c5681Sdrh #endif 581a18c5681Sdrh break; 582a18c5681Sdrh case etSIZE: 583a18c5681Sdrh *(va_arg(ap,int*)) = count; 584a18c5681Sdrh length = width = 0; 585a18c5681Sdrh break; 586a18c5681Sdrh case etPERCENT: 587a18c5681Sdrh buf[0] = '%'; 588a18c5681Sdrh bufpt = buf; 589a18c5681Sdrh length = 1; 590a18c5681Sdrh break; 591a18c5681Sdrh case etCHARLIT: 592a18c5681Sdrh case etCHARX: 593a18c5681Sdrh c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt); 594a18c5681Sdrh if( precision>=0 ){ 595a18c5681Sdrh for(idx=1; idx<precision; idx++) buf[idx] = c; 596a18c5681Sdrh length = precision; 597a18c5681Sdrh }else{ 598a18c5681Sdrh length =1; 599a18c5681Sdrh } 600a18c5681Sdrh bufpt = buf; 601a18c5681Sdrh break; 602a18c5681Sdrh case etSTRING: 603d93d8a81Sdrh case etDYNSTRING: 604cb485882Sdrh bufpt = va_arg(ap,char*); 605d93d8a81Sdrh if( bufpt==0 ){ 606d93d8a81Sdrh bufpt = ""; 607d93d8a81Sdrh }else if( xtype==etDYNSTRING ){ 608d93d8a81Sdrh zExtra = bufpt; 609d93d8a81Sdrh } 610a18c5681Sdrh length = strlen(bufpt); 611a18c5681Sdrh if( precision>=0 && precision<length ) length = precision; 612a18c5681Sdrh break; 613a18c5681Sdrh case etSQLESCAPE: 6145eba8c09Sdrh case etSQLESCAPE2: { 615f0113000Sdanielk1977 int i, j, n, ch, isnull; 6164794f735Sdrh int needQuote; 617f0113000Sdanielk1977 char *escarg = va_arg(ap,char*); 618f0113000Sdanielk1977 isnull = escarg==0; 619f0113000Sdanielk1977 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); 620f0113000Sdanielk1977 for(i=n=0; (ch=escarg[i])!=0; i++){ 621f0113000Sdanielk1977 if( ch=='\'' ) n++; 622a18c5681Sdrh } 6234794f735Sdrh needQuote = !isnull && xtype==etSQLESCAPE2; 6244794f735Sdrh n += i + 1 + needQuote*2; 625a18c5681Sdrh if( n>etBUFSIZE ){ 626a18c5681Sdrh bufpt = zExtra = sqliteMalloc( n ); 627daffd0e5Sdrh if( bufpt==0 ) return -1; 628a18c5681Sdrh }else{ 629a18c5681Sdrh bufpt = buf; 630a18c5681Sdrh } 6310cfcf3fbSchw j = 0; 6324794f735Sdrh if( needQuote ) bufpt[j++] = '\''; 633f0113000Sdanielk1977 for(i=0; (ch=escarg[i])!=0; i++){ 634f0113000Sdanielk1977 bufpt[j++] = ch; 635f0113000Sdanielk1977 if( ch=='\'' ) bufpt[j++] = ch; 636a18c5681Sdrh } 6374794f735Sdrh if( needQuote ) bufpt[j++] = '\''; 638a18c5681Sdrh bufpt[j] = 0; 639a18c5681Sdrh length = j; 64005a82983Sdrh /* The precision is ignored on %q and %Q */ 64105a82983Sdrh /* if( precision>=0 && precision<length ) length = precision; */ 642a18c5681Sdrh break; 6435eba8c09Sdrh } 6445f968436Sdrh case etTOKEN: { 6455f968436Sdrh Token *pToken = va_arg(ap, Token*); 646ad6d9460Sdrh if( pToken && pToken->z ){ 6472646da7eSdrh (*func)(arg, (char*)pToken->z, pToken->n); 648ad6d9460Sdrh } 6495f968436Sdrh length = width = 0; 6505f968436Sdrh break; 6515f968436Sdrh } 6525f968436Sdrh case etSRCLIST: { 6535f968436Sdrh SrcList *pSrc = va_arg(ap, SrcList*); 6545f968436Sdrh int k = va_arg(ap, int); 6555f968436Sdrh struct SrcList_item *pItem = &pSrc->a[k]; 6565f968436Sdrh assert( k>=0 && k<pSrc->nSrc ); 6575f968436Sdrh if( pItem->zDatabase && pItem->zDatabase[0] ){ 6585f968436Sdrh (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase)); 6595f968436Sdrh (*func)(arg, ".", 1); 6605f968436Sdrh } 6615f968436Sdrh (*func)(arg, pItem->zName, strlen(pItem->zName)); 6625f968436Sdrh length = width = 0; 6635f968436Sdrh break; 6645f968436Sdrh } 665a18c5681Sdrh }/* End switch over the format type */ 666a18c5681Sdrh /* 667a18c5681Sdrh ** The text of the conversion is pointed to by "bufpt" and is 668a18c5681Sdrh ** "length" characters long. The field width is "width". Do 669a18c5681Sdrh ** the output. 670a18c5681Sdrh */ 671a18c5681Sdrh if( !flag_leftjustify ){ 672a18c5681Sdrh register int nspace; 673a18c5681Sdrh nspace = width-length; 674a18c5681Sdrh if( nspace>0 ){ 675a18c5681Sdrh count += nspace; 676a18c5681Sdrh while( nspace>=etSPACESIZE ){ 677a18c5681Sdrh (*func)(arg,spaces,etSPACESIZE); 678a18c5681Sdrh nspace -= etSPACESIZE; 679a18c5681Sdrh } 680a18c5681Sdrh if( nspace>0 ) (*func)(arg,spaces,nspace); 681a18c5681Sdrh } 682a18c5681Sdrh } 683a18c5681Sdrh if( length>0 ){ 684a18c5681Sdrh (*func)(arg,bufpt,length); 685a18c5681Sdrh count += length; 686a18c5681Sdrh } 687a18c5681Sdrh if( flag_leftjustify ){ 688a18c5681Sdrh register int nspace; 689a18c5681Sdrh nspace = width-length; 690a18c5681Sdrh if( nspace>0 ){ 691a18c5681Sdrh count += nspace; 692a18c5681Sdrh while( nspace>=etSPACESIZE ){ 693a18c5681Sdrh (*func)(arg,spaces,etSPACESIZE); 694a18c5681Sdrh nspace -= etSPACESIZE; 695a18c5681Sdrh } 696a18c5681Sdrh if( nspace>0 ) (*func)(arg,spaces,nspace); 697a18c5681Sdrh } 698a18c5681Sdrh } 699a18c5681Sdrh if( zExtra ){ 700a18c5681Sdrh sqliteFree(zExtra); 701a18c5681Sdrh } 702a18c5681Sdrh }/* End for loop over the format string */ 703a18c5681Sdrh return errorflag ? -1 : count; 704a18c5681Sdrh } /* End of function */ 705a18c5681Sdrh 706a18c5681Sdrh 707a18c5681Sdrh /* This structure is used to store state information about the 708a18c5681Sdrh ** write to memory that is currently in progress. 709a18c5681Sdrh */ 710a18c5681Sdrh struct sgMprintf { 711a18c5681Sdrh char *zBase; /* A base allocation */ 712a18c5681Sdrh char *zText; /* The string collected so far */ 713a18c5681Sdrh int nChar; /* Length of the string so far */ 7145f968436Sdrh int nTotal; /* Output size if unconstrained */ 715a18c5681Sdrh int nAlloc; /* Amount of space allocated in zText */ 7165f968436Sdrh void *(*xRealloc)(void*,int); /* Function used to realloc memory */ 717a18c5681Sdrh }; 718a18c5681Sdrh 719a18c5681Sdrh /* 720a18c5681Sdrh ** This function implements the callback from vxprintf. 721a18c5681Sdrh ** 722a18c5681Sdrh ** This routine add nNewChar characters of text in zNewText to 723a18c5681Sdrh ** the sgMprintf structure pointed to by "arg". 724a18c5681Sdrh */ 7255f968436Sdrh static void mout(void *arg, const char *zNewText, int nNewChar){ 726a18c5681Sdrh struct sgMprintf *pM = (struct sgMprintf*)arg; 7275f968436Sdrh pM->nTotal += nNewChar; 728a18c5681Sdrh if( pM->nChar + nNewChar + 1 > pM->nAlloc ){ 7295f968436Sdrh if( pM->xRealloc==0 ){ 7305f968436Sdrh nNewChar = pM->nAlloc - pM->nChar - 1; 7315f968436Sdrh }else{ 732eaad32b1Sdrh int nAlloc = pM->nChar + nNewChar*2 + 1; 733a18c5681Sdrh if( pM->zText==pM->zBase ){ 734eaad32b1Sdrh pM->zText = pM->xRealloc(0, nAlloc); 7355f968436Sdrh if( pM->zText && pM->nChar ){ 7365f968436Sdrh memcpy(pM->zText, pM->zBase, pM->nChar); 7375f968436Sdrh } 738a18c5681Sdrh }else{ 73953f733c7Sdrh char *zNew; 740eaad32b1Sdrh zNew = pM->xRealloc(pM->zText, nAlloc); 74153f733c7Sdrh if( zNew ){ 74253f733c7Sdrh pM->zText = zNew; 743eaad32b1Sdrh }else{ 744eaad32b1Sdrh return; 74553f733c7Sdrh } 746a18c5681Sdrh } 747eaad32b1Sdrh pM->nAlloc = nAlloc; 748a18c5681Sdrh } 7495f968436Sdrh } 750e29b1a05Sdrh if( pM->zText ){ 751e29b1a05Sdrh if( nNewChar>0 ){ 752a18c5681Sdrh memcpy(&pM->zText[pM->nChar], zNewText, nNewChar); 753a18c5681Sdrh pM->nChar += nNewChar; 754e29b1a05Sdrh } 755a18c5681Sdrh pM->zText[pM->nChar] = 0; 756a18c5681Sdrh } 757a18c5681Sdrh } 758a18c5681Sdrh 759a18c5681Sdrh /* 7605f968436Sdrh ** This routine is a wrapper around xprintf() that invokes mout() as 7615f968436Sdrh ** the consumer. 762a18c5681Sdrh */ 7635f968436Sdrh static char *base_vprintf( 7645f968436Sdrh void *(*xRealloc)(void*,int), /* Routine to realloc memory. May be NULL */ 7655f968436Sdrh int useInternal, /* Use internal %-conversions if true */ 7665f968436Sdrh char *zInitBuf, /* Initially write here, before mallocing */ 7675f968436Sdrh int nInitBuf, /* Size of zInitBuf[] */ 7685f968436Sdrh const char *zFormat, /* format string */ 7695f968436Sdrh va_list ap /* arguments */ 7705f968436Sdrh ){ 7715f968436Sdrh struct sgMprintf sM; 7725f968436Sdrh sM.zBase = sM.zText = zInitBuf; 7735f968436Sdrh sM.nChar = sM.nTotal = 0; 7745f968436Sdrh sM.nAlloc = nInitBuf; 7755f968436Sdrh sM.xRealloc = xRealloc; 7765f968436Sdrh vxprintf(mout, &sM, useInternal, zFormat, ap); 7775f968436Sdrh if( xRealloc ){ 7785f968436Sdrh if( sM.zText==sM.zBase ){ 7795f968436Sdrh sM.zText = xRealloc(0, sM.nChar+1); 7808def5ea2Sdanielk1977 if( sM.zText ){ 7815f968436Sdrh memcpy(sM.zText, sM.zBase, sM.nChar+1); 7828def5ea2Sdanielk1977 } 7835f968436Sdrh }else if( sM.nAlloc>sM.nChar+10 ){ 78453f733c7Sdrh char *zNew = xRealloc(sM.zText, sM.nChar+1); 78553f733c7Sdrh if( zNew ){ 78653f733c7Sdrh sM.zText = zNew; 78753f733c7Sdrh } 7885f968436Sdrh } 7895f968436Sdrh } 7905f968436Sdrh return sM.zText; 791483750baSdrh } 792483750baSdrh 793483750baSdrh /* 7945f968436Sdrh ** Realloc that is a real function, not a macro. 7955f968436Sdrh */ 7965f968436Sdrh static void *printf_realloc(void *old, int size){ 7975f968436Sdrh return sqliteRealloc(old,size); 7985f968436Sdrh } 7995f968436Sdrh 8005f968436Sdrh /* 8015f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 8025f968436Sdrh ** %-conversion extensions. 8035f968436Sdrh */ 8044adee20fSdanielk1977 char *sqlite3VMPrintf(const char *zFormat, va_list ap){ 80579158e18Sdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 8065f968436Sdrh return base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap); 8075f968436Sdrh } 8085f968436Sdrh 8095f968436Sdrh /* 8105f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 8115f968436Sdrh ** %-conversion extensions. 8125f968436Sdrh */ 8134adee20fSdanielk1977 char *sqlite3MPrintf(const char *zFormat, ...){ 8145f968436Sdrh va_list ap; 8155f968436Sdrh char *z; 81679158e18Sdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 8175f968436Sdrh va_start(ap, zFormat); 8185f968436Sdrh z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap); 8195f968436Sdrh va_end(ap); 8205f968436Sdrh return z; 8215f968436Sdrh } 8225f968436Sdrh 8235f968436Sdrh /* 82428dd479cSdrh ** Print into memory obtained from sqlite3_malloc(). Omit the internal 82528dd479cSdrh ** %-conversion extensions. 82628dd479cSdrh */ 82728dd479cSdrh char *sqlite3_vmprintf(const char *zFormat, va_list ap){ 82828dd479cSdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 82928dd479cSdrh return base_vprintf(sqlite3_realloc, 0, zBase, sizeof(zBase), zFormat, ap); 83028dd479cSdrh } 83128dd479cSdrh 83228dd479cSdrh /* 83328dd479cSdrh ** Print into memory obtained from sqlite3_malloc()(). Omit the internal 83428dd479cSdrh ** %-conversion extensions. 835483750baSdrh */ 8366f8a503dSdanielk1977 char *sqlite3_mprintf(const char *zFormat, ...){ 837a18c5681Sdrh va_list ap; 8385f968436Sdrh char *z; 839a18c5681Sdrh va_start(ap, zFormat); 840b3738b6cSdrh z = sqlite3_vmprintf(zFormat, ap); 841a18c5681Sdrh va_end(ap); 8425f968436Sdrh return z; 843a18c5681Sdrh } 844a18c5681Sdrh 845a18c5681Sdrh /* 8466f8a503dSdanielk1977 ** sqlite3_snprintf() works like snprintf() except that it ignores the 84793a5c6bdSdrh ** current locale settings. This is important for SQLite because we 84893a5c6bdSdrh ** are not able to use a "," as the decimal point in place of "." as 84993a5c6bdSdrh ** specified by some locales. 85093a5c6bdSdrh */ 8516f8a503dSdanielk1977 char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ 8525f968436Sdrh char *z; 85393a5c6bdSdrh va_list ap; 85493a5c6bdSdrh 85568853907Sdrh if( n<=0 ){ 85668853907Sdrh return zBuf; 85768853907Sdrh } 85868853907Sdrh zBuf[0] = 0; 85993a5c6bdSdrh va_start(ap,zFormat); 8605f968436Sdrh z = base_vprintf(0, 0, zBuf, n, zFormat, ap); 86193a5c6bdSdrh va_end(ap); 8625f968436Sdrh return z; 86393a5c6bdSdrh } 86493a5c6bdSdrh 8657d7f17b6Sdrh #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) || defined(SQLITE_MEMDEBUG) 866e54ca3feSdrh /* 867e54ca3feSdrh ** A version of printf() that understands %lld. Used for debugging. 868e54ca3feSdrh ** The printf() built into some versions of windows does not understand %lld 869e54ca3feSdrh ** and segfaults if you give it a long long int. 870e54ca3feSdrh */ 871e54ca3feSdrh void sqlite3DebugPrintf(const char *zFormat, ...){ 87276ff3a0eSdrh extern int getpid(void); 873e54ca3feSdrh va_list ap; 874e54ca3feSdrh char zBuf[500]; 875e54ca3feSdrh va_start(ap, zFormat); 876e54ca3feSdrh base_vprintf(0, 0, zBuf, sizeof(zBuf), zFormat, ap); 877e54ca3feSdrh va_end(ap); 878485f0039Sdrh fprintf(stdout,"%s", zBuf); 8792ac3ee97Sdrh fflush(stdout); 880e54ca3feSdrh } 881e54ca3feSdrh #endif 882