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 ** 8*50d654daSdrh ** $Id: printf.c,v 1.104 2009/06/03 01:24:54 drh Exp $ 9822a5162Sdanielk1977 ** 10e84a306bSdrh ************************************************************************** 11a18c5681Sdrh ** 12e78e8284Sdrh ** The following modules is an enhanced replacement for the "printf" subroutines 13e78e8284Sdrh ** found in the standard C library. The following enhancements are 14a18c5681Sdrh ** supported: 15a18c5681Sdrh ** 16a18c5681Sdrh ** + Additional functions. The standard set of "printf" functions 17a18c5681Sdrh ** includes printf, fprintf, sprintf, vprintf, vfprintf, and 18a18c5681Sdrh ** vsprintf. This module adds the following: 19a18c5681Sdrh ** 20a18c5681Sdrh ** * snprintf -- Works like sprintf, but has an extra argument 21a18c5681Sdrh ** which is the size of the buffer written to. 22a18c5681Sdrh ** 23a18c5681Sdrh ** * mprintf -- Similar to sprintf. Writes output to memory 24a18c5681Sdrh ** obtained from malloc. 25a18c5681Sdrh ** 26a18c5681Sdrh ** * xprintf -- Calls a function to dispose of output. 27a18c5681Sdrh ** 28a18c5681Sdrh ** * nprintf -- No output, but returns the number of characters 29a18c5681Sdrh ** that would have been output by printf. 30a18c5681Sdrh ** 31a18c5681Sdrh ** * A v- version (ex: vsnprintf) of every function is also 32a18c5681Sdrh ** supplied. 33a18c5681Sdrh ** 34a18c5681Sdrh ** + A few extensions to the formatting notation are supported: 35a18c5681Sdrh ** 36a18c5681Sdrh ** * The "=" flag (similar to "-") causes the output to be 37a18c5681Sdrh ** be centered in the appropriately sized field. 38a18c5681Sdrh ** 39a18c5681Sdrh ** * The %b field outputs an integer in binary notation. 40a18c5681Sdrh ** 41a18c5681Sdrh ** * The %c field now accepts a precision. The character output 42a18c5681Sdrh ** is repeated by the number of times the precision specifies. 43a18c5681Sdrh ** 44a18c5681Sdrh ** * The %' field works like %c, but takes as its character the 45a18c5681Sdrh ** next character of the format string, instead of the next 46a18c5681Sdrh ** argument. For example, printf("%.78'-") prints 78 minus 47a18c5681Sdrh ** signs, the same as printf("%.78c",'-'). 48a18c5681Sdrh ** 49a18c5681Sdrh ** + When compiled using GCC on a SPARC, this version of printf is 50a18c5681Sdrh ** faster than the library printf for SUN OS 4.1. 51a18c5681Sdrh ** 52a18c5681Sdrh ** + All functions are fully reentrant. 53a18c5681Sdrh ** 54a18c5681Sdrh */ 55a18c5681Sdrh #include "sqliteInt.h" 567c68d60bSdrh 57a18c5681Sdrh /* 58a18c5681Sdrh ** Conversion types fall into various categories as defined by the 59a18c5681Sdrh ** following enumeration. 60a18c5681Sdrh */ 61e84a306bSdrh #define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */ 62e84a306bSdrh #define etFLOAT 2 /* Floating point. %f */ 63e84a306bSdrh #define etEXP 3 /* Exponentional notation. %e and %E */ 64e84a306bSdrh #define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */ 65e84a306bSdrh #define etSIZE 5 /* Return number of characters processed so far. %n */ 66e84a306bSdrh #define etSTRING 6 /* Strings. %s */ 67e84a306bSdrh #define etDYNSTRING 7 /* Dynamically allocated strings. %z */ 68e84a306bSdrh #define etPERCENT 8 /* Percent symbol. %% */ 69e84a306bSdrh #define etCHARX 9 /* Characters. %c */ 70a18c5681Sdrh /* The rest are extensions, not normally found in printf() */ 71af005fbcSdrh #define etSQLESCAPE 10 /* Strings with '\'' doubled. %q */ 72af005fbcSdrh #define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '', 730cfcf3fbSchw NULL pointers replaced by SQL NULL. %Q */ 74af005fbcSdrh #define etTOKEN 12 /* a pointer to a Token structure */ 75af005fbcSdrh #define etSRCLIST 13 /* a pointer to a SrcList */ 76af005fbcSdrh #define etPOINTER 14 /* The %p conversion */ 77af005fbcSdrh #define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */ 78af005fbcSdrh #define etORDINAL 16 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */ 79e84a306bSdrh 80874ba04cSdrh #define etINVALID 0 /* Any unrecognized conversion type */ 81874ba04cSdrh 82e84a306bSdrh 83e84a306bSdrh /* 84e84a306bSdrh ** An "etByte" is an 8-bit unsigned value. 85e84a306bSdrh */ 86e84a306bSdrh typedef unsigned char etByte; 87a18c5681Sdrh 88a18c5681Sdrh /* 89a18c5681Sdrh ** Each builtin conversion character (ex: the 'd' in "%d") is described 90a18c5681Sdrh ** by an instance of the following structure 91a18c5681Sdrh */ 92a18c5681Sdrh typedef struct et_info { /* Information about each format field */ 93e84a306bSdrh char fmttype; /* The format field code letter */ 94e84a306bSdrh etByte base; /* The base for radix conversion */ 95e84a306bSdrh etByte flags; /* One or more of FLAG_ constants below */ 96e84a306bSdrh etByte type; /* Conversion paradigm */ 9776ff3a0eSdrh etByte charset; /* Offset into aDigits[] of the digits string */ 9876ff3a0eSdrh etByte prefix; /* Offset into aPrefix[] of the prefix string */ 99a18c5681Sdrh } et_info; 100a18c5681Sdrh 101a18c5681Sdrh /* 102e84a306bSdrh ** Allowed values for et_info.flags 103e84a306bSdrh */ 104e84a306bSdrh #define FLAG_SIGNED 1 /* True if the value to convert is signed */ 105e84a306bSdrh #define FLAG_INTERN 2 /* True if for internal use only */ 1064794f735Sdrh #define FLAG_STRING 4 /* Allow infinity precision */ 107e84a306bSdrh 108e84a306bSdrh 109e84a306bSdrh /* 110a18c5681Sdrh ** The following table is searched linearly, so it is good to put the 111a18c5681Sdrh ** most frequently used conversion types first. 112a18c5681Sdrh */ 11376ff3a0eSdrh static const char aDigits[] = "0123456789ABCDEF0123456789abcdef"; 11476ff3a0eSdrh static const char aPrefix[] = "-x0\000X0"; 1155719628aSdrh static const et_info fmtinfo[] = { 11676ff3a0eSdrh { 'd', 10, 1, etRADIX, 0, 0 }, 1174794f735Sdrh { 's', 0, 4, etSTRING, 0, 0 }, 118557cc60fSdrh { 'g', 0, 1, etGENERIC, 30, 0 }, 119153c62c4Sdrh { 'z', 0, 4, etDYNSTRING, 0, 0 }, 1204794f735Sdrh { 'q', 0, 4, etSQLESCAPE, 0, 0 }, 1214794f735Sdrh { 'Q', 0, 4, etSQLESCAPE2, 0, 0 }, 122f3b863edSdanielk1977 { 'w', 0, 4, etSQLESCAPE3, 0, 0 }, 123e84a306bSdrh { 'c', 0, 0, etCHARX, 0, 0 }, 12476ff3a0eSdrh { 'o', 8, 0, etRADIX, 0, 2 }, 12576ff3a0eSdrh { 'u', 10, 0, etRADIX, 0, 0 }, 12676ff3a0eSdrh { 'x', 16, 0, etRADIX, 16, 1 }, 12776ff3a0eSdrh { 'X', 16, 0, etRADIX, 0, 4 }, 128b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 129e84a306bSdrh { 'f', 0, 1, etFLOAT, 0, 0 }, 13076ff3a0eSdrh { 'e', 0, 1, etEXP, 30, 0 }, 13176ff3a0eSdrh { 'E', 0, 1, etEXP, 14, 0 }, 13276ff3a0eSdrh { 'G', 0, 1, etGENERIC, 14, 0 }, 133b37df7b9Sdrh #endif 13476ff3a0eSdrh { 'i', 10, 1, etRADIX, 0, 0 }, 135e84a306bSdrh { 'n', 0, 0, etSIZE, 0, 0 }, 136e84a306bSdrh { '%', 0, 0, etPERCENT, 0, 0 }, 13776ff3a0eSdrh { 'p', 16, 0, etPOINTER, 0, 1 }, 1387e3ff5d8Sdrh 1397e3ff5d8Sdrh /* All the rest have the FLAG_INTERN bit set and are thus for internal 1407e3ff5d8Sdrh ** use only */ 1415f968436Sdrh { 'T', 0, 2, etTOKEN, 0, 0 }, 1425f968436Sdrh { 'S', 0, 2, etSRCLIST, 0, 0 }, 1439a99334dSdrh { 'r', 10, 3, etORDINAL, 0, 0 }, 144a18c5681Sdrh }; 145a18c5681Sdrh 146a18c5681Sdrh /* 147b37df7b9Sdrh ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point 148a18c5681Sdrh ** conversions will work. 149a18c5681Sdrh */ 150b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 151a18c5681Sdrh /* 152a18c5681Sdrh ** "*val" is a double such that 0.1 <= *val < 10.0 153a18c5681Sdrh ** Return the ascii code for the leading digit of *val, then 154a18c5681Sdrh ** multiply "*val" by 10.0 to renormalize. 155a18c5681Sdrh ** 156a18c5681Sdrh ** Example: 157a18c5681Sdrh ** input: *val = 3.14159 158a18c5681Sdrh ** output: *val = 1.4159 function return = '3' 159a18c5681Sdrh ** 160a18c5681Sdrh ** The counter *cnt is incremented each time. After counter exceeds 161a18c5681Sdrh ** 16 (the number of significant digits in a 64-bit float) '0' is 162a18c5681Sdrh ** always returned. 163a18c5681Sdrh */ 164ea678832Sdrh static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ 165a18c5681Sdrh int digit; 166384eef32Sdrh LONGDOUBLE_TYPE d; 167a18c5681Sdrh if( (*cnt)++ >= 16 ) return '0'; 168a18c5681Sdrh digit = (int)*val; 169a18c5681Sdrh d = digit; 170a18c5681Sdrh digit += '0'; 171a18c5681Sdrh *val = (*val - d)*10.0; 172ea678832Sdrh return (char)digit; 173a18c5681Sdrh } 174b37df7b9Sdrh #endif /* SQLITE_OMIT_FLOATING_POINT */ 175a18c5681Sdrh 17679158e18Sdrh /* 177ade86483Sdrh ** Append N space characters to the given string buffer. 178ade86483Sdrh */ 179ade86483Sdrh static void appendSpace(StrAccum *pAccum, int N){ 180ade86483Sdrh static const char zSpaces[] = " "; 18100e13613Sdanielk1977 while( N>=(int)sizeof(zSpaces)-1 ){ 182ade86483Sdrh sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1); 183ade86483Sdrh N -= sizeof(zSpaces)-1; 184ade86483Sdrh } 185ade86483Sdrh if( N>0 ){ 186ade86483Sdrh sqlite3StrAccumAppend(pAccum, zSpaces, N); 187ade86483Sdrh } 188ade86483Sdrh } 189ade86483Sdrh 190ade86483Sdrh /* 19179158e18Sdrh ** On machines with a small stack size, you can redefine the 192*50d654daSdrh ** SQLITE_PRINT_BUF_SIZE to be less than 350. 19379158e18Sdrh */ 19479158e18Sdrh #ifndef SQLITE_PRINT_BUF_SIZE 195*50d654daSdrh # if defined(SQLITE_SMALL_STACK) 196*50d654daSdrh # define SQLITE_PRINT_BUF_SIZE 50 197*50d654daSdrh # else 19879158e18Sdrh # define SQLITE_PRINT_BUF_SIZE 350 19979158e18Sdrh # endif 200*50d654daSdrh #endif 20179158e18Sdrh #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ 202a18c5681Sdrh 203a18c5681Sdrh /* 204a18c5681Sdrh ** The root program. All variations call this core. 205a18c5681Sdrh ** 206a18c5681Sdrh ** INPUTS: 207a18c5681Sdrh ** func This is a pointer to a function taking three arguments 208a18c5681Sdrh ** 1. A pointer to anything. Same as the "arg" parameter. 209a18c5681Sdrh ** 2. A pointer to the list of characters to be output 210a18c5681Sdrh ** (Note, this list is NOT null terminated.) 211a18c5681Sdrh ** 3. An integer number of characters to be output. 212a18c5681Sdrh ** (Note: This number might be zero.) 213a18c5681Sdrh ** 214a18c5681Sdrh ** arg This is the pointer to anything which will be passed as the 215a18c5681Sdrh ** first argument to "func". Use it for whatever you like. 216a18c5681Sdrh ** 217a18c5681Sdrh ** fmt This is the format string, as in the usual print. 218a18c5681Sdrh ** 219a18c5681Sdrh ** ap This is a pointer to a list of arguments. Same as in 220a18c5681Sdrh ** vfprint. 221a18c5681Sdrh ** 222a18c5681Sdrh ** OUTPUTS: 223a18c5681Sdrh ** The return value is the total number of characters sent to 224a18c5681Sdrh ** the function "func". Returns -1 on a error. 225a18c5681Sdrh ** 226a18c5681Sdrh ** Note that the order in which automatic variables are declared below 227a18c5681Sdrh ** seems to make a big difference in determining how fast this beast 228a18c5681Sdrh ** will run. 229a18c5681Sdrh */ 230f089aa45Sdrh void sqlite3VXPrintf( 231ade86483Sdrh StrAccum *pAccum, /* Accumulate results here */ 2325f968436Sdrh int useExtended, /* Allow extended %-conversions */ 2335f968436Sdrh const char *fmt, /* Format string */ 2345f968436Sdrh va_list ap /* arguments */ 235a18c5681Sdrh ){ 236e84a306bSdrh int c; /* Next character in the format string */ 237e84a306bSdrh char *bufpt; /* Pointer to the conversion buffer */ 238e84a306bSdrh int precision; /* Precision of the current field */ 239e84a306bSdrh int length; /* Length of the field */ 240e84a306bSdrh int idx; /* A general purpose loop counter */ 241a18c5681Sdrh int width; /* Width of the current field */ 242e84a306bSdrh etByte flag_leftjustify; /* True if "-" flag is present */ 243e84a306bSdrh etByte flag_plussign; /* True if "+" flag is present */ 244e84a306bSdrh etByte flag_blanksign; /* True if " " flag is present */ 245e84a306bSdrh etByte flag_alternateform; /* True if "#" flag is present */ 246531fe878Sdrh etByte flag_altform2; /* True if "!" flag is present */ 247e84a306bSdrh etByte flag_zeropad; /* True if field width constant starts with zero */ 248e84a306bSdrh etByte flag_long; /* True if "l" flag is present */ 249a34b6764Sdrh etByte flag_longlong; /* True if the "ll" flag is present */ 2503e9aeec0Sdrh etByte done; /* Loop termination flag */ 25127436af7Sdrh sqlite_uint64 longvalue; /* Value for integer types */ 252384eef32Sdrh LONGDOUBLE_TYPE realvalue; /* Value for real types */ 2535719628aSdrh const et_info *infop; /* Pointer to the appropriate info structure */ 254a18c5681Sdrh char buf[etBUFSIZE]; /* Conversion buffer */ 255a18c5681Sdrh char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ 256b27b7f5dSdrh etByte xtype = 0; /* Conversion paradigm */ 257a18c5681Sdrh char *zExtra; /* Extra memory used for etTCLESCAPE conversions */ 258b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 259557cc60fSdrh int exp, e2; /* exponent of real numbers */ 260a18c5681Sdrh double rounder; /* Used for rounding floating point values */ 261e84a306bSdrh etByte flag_dp; /* True if decimal point should be shown */ 262e84a306bSdrh etByte flag_rtz; /* True if trailing zeros should be removed */ 263e84a306bSdrh etByte flag_exp; /* True to force display of the exponent */ 264a18c5681Sdrh int nsd; /* Number of significant digits returned */ 265a18c5681Sdrh #endif 266a18c5681Sdrh 267ade86483Sdrh length = 0; 268a18c5681Sdrh bufpt = 0; 269a18c5681Sdrh for(; (c=(*fmt))!=0; ++fmt){ 270a18c5681Sdrh if( c!='%' ){ 271e84a306bSdrh int amt; 272a18c5681Sdrh bufpt = (char *)fmt; 273a18c5681Sdrh amt = 1; 274a18c5681Sdrh while( (c=(*++fmt))!='%' && c!=0 ) amt++; 275ade86483Sdrh sqlite3StrAccumAppend(pAccum, bufpt, amt); 276a18c5681Sdrh if( c==0 ) break; 277a18c5681Sdrh } 278a18c5681Sdrh if( (c=(*++fmt))==0 ){ 279ade86483Sdrh sqlite3StrAccumAppend(pAccum, "%", 1); 280a18c5681Sdrh break; 281a18c5681Sdrh } 282a18c5681Sdrh /* Find out what flags are present */ 283a18c5681Sdrh flag_leftjustify = flag_plussign = flag_blanksign = 284557cc60fSdrh flag_alternateform = flag_altform2 = flag_zeropad = 0; 2853e9aeec0Sdrh done = 0; 286a18c5681Sdrh do{ 287a18c5681Sdrh switch( c ){ 2883e9aeec0Sdrh case '-': flag_leftjustify = 1; break; 2893e9aeec0Sdrh case '+': flag_plussign = 1; break; 2903e9aeec0Sdrh case ' ': flag_blanksign = 1; break; 2913e9aeec0Sdrh case '#': flag_alternateform = 1; break; 2923e9aeec0Sdrh case '!': flag_altform2 = 1; break; 2933e9aeec0Sdrh case '0': flag_zeropad = 1; break; 2943e9aeec0Sdrh default: done = 1; break; 295a18c5681Sdrh } 2963e9aeec0Sdrh }while( !done && (c=(*++fmt))!=0 ); 297a18c5681Sdrh /* Get the field width */ 298a18c5681Sdrh width = 0; 299a18c5681Sdrh if( c=='*' ){ 300a18c5681Sdrh width = va_arg(ap,int); 301a18c5681Sdrh if( width<0 ){ 302a18c5681Sdrh flag_leftjustify = 1; 303a18c5681Sdrh width = -width; 304a18c5681Sdrh } 305a18c5681Sdrh c = *++fmt; 306a18c5681Sdrh }else{ 30717a68934Sdrh while( c>='0' && c<='9' ){ 308a18c5681Sdrh width = width*10 + c - '0'; 309a18c5681Sdrh c = *++fmt; 310a18c5681Sdrh } 311a18c5681Sdrh } 312a18c5681Sdrh if( width > etBUFSIZE-10 ){ 313a18c5681Sdrh width = etBUFSIZE-10; 314a18c5681Sdrh } 315a18c5681Sdrh /* Get the precision */ 316a18c5681Sdrh if( c=='.' ){ 317a18c5681Sdrh precision = 0; 318a18c5681Sdrh c = *++fmt; 319a18c5681Sdrh if( c=='*' ){ 320a18c5681Sdrh precision = va_arg(ap,int); 321a18c5681Sdrh if( precision<0 ) precision = -precision; 322a18c5681Sdrh c = *++fmt; 323a18c5681Sdrh }else{ 32417a68934Sdrh while( c>='0' && c<='9' ){ 325a18c5681Sdrh precision = precision*10 + c - '0'; 326a18c5681Sdrh c = *++fmt; 327a18c5681Sdrh } 328a18c5681Sdrh } 329a18c5681Sdrh }else{ 330a18c5681Sdrh precision = -1; 331a18c5681Sdrh } 332a18c5681Sdrh /* Get the conversion type modifier */ 333a18c5681Sdrh if( c=='l' ){ 334a18c5681Sdrh flag_long = 1; 335a18c5681Sdrh c = *++fmt; 336a34b6764Sdrh if( c=='l' ){ 337a34b6764Sdrh flag_longlong = 1; 338a34b6764Sdrh c = *++fmt; 339a18c5681Sdrh }else{ 340a34b6764Sdrh flag_longlong = 0; 341a34b6764Sdrh } 342a34b6764Sdrh }else{ 343a34b6764Sdrh flag_long = flag_longlong = 0; 344a18c5681Sdrh } 345a18c5681Sdrh /* Fetch the info entry for the field */ 346874ba04cSdrh infop = &fmtinfo[0]; 347874ba04cSdrh xtype = etINVALID; 34800e13613Sdanielk1977 for(idx=0; idx<ArraySize(fmtinfo); idx++){ 349a18c5681Sdrh if( c==fmtinfo[idx].fmttype ){ 350a18c5681Sdrh infop = &fmtinfo[idx]; 3515f968436Sdrh if( useExtended || (infop->flags & FLAG_INTERN)==0 ){ 352e84a306bSdrh xtype = infop->type; 353c4413565Sdrh }else{ 354ade86483Sdrh return; 3555f968436Sdrh } 356a18c5681Sdrh break; 357a18c5681Sdrh } 358a18c5681Sdrh } 359a18c5681Sdrh zExtra = 0; 36043617e9aSdrh 361a18c5681Sdrh 3624794f735Sdrh /* Limit the precision to prevent overflowing buf[] during conversion */ 3634794f735Sdrh if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){ 3644794f735Sdrh precision = etBUFSIZE-40; 3654794f735Sdrh } 3664794f735Sdrh 367a18c5681Sdrh /* 368a18c5681Sdrh ** At this point, variables are initialized as follows: 369a18c5681Sdrh ** 370a18c5681Sdrh ** flag_alternateform TRUE if a '#' is present. 3713e9aeec0Sdrh ** flag_altform2 TRUE if a '!' is present. 372a18c5681Sdrh ** flag_plussign TRUE if a '+' is present. 373a18c5681Sdrh ** flag_leftjustify TRUE if a '-' is present or if the 374a18c5681Sdrh ** field width was negative. 375a18c5681Sdrh ** flag_zeropad TRUE if the width began with 0. 376a18c5681Sdrh ** flag_long TRUE if the letter 'l' (ell) prefixed 377a18c5681Sdrh ** the conversion character. 378a34b6764Sdrh ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed 379a34b6764Sdrh ** the conversion character. 380a18c5681Sdrh ** flag_blanksign TRUE if a ' ' is present. 381a18c5681Sdrh ** width The specified field width. This is 382a18c5681Sdrh ** always non-negative. Zero is the default. 383a18c5681Sdrh ** precision The specified precision. The default 384a18c5681Sdrh ** is -1. 385a18c5681Sdrh ** xtype The class of the conversion. 386a18c5681Sdrh ** infop Pointer to the appropriate info struct. 387a18c5681Sdrh */ 388a18c5681Sdrh switch( xtype ){ 389fe63d1c9Sdrh case etPOINTER: 390fe63d1c9Sdrh flag_longlong = sizeof(char*)==sizeof(i64); 391fe63d1c9Sdrh flag_long = sizeof(char*)==sizeof(long int); 392fe63d1c9Sdrh /* Fall through into the next case */ 3939a99334dSdrh case etORDINAL: 394a18c5681Sdrh case etRADIX: 395e84a306bSdrh if( infop->flags & FLAG_SIGNED ){ 396e9707671Sdrh i64 v; 397eeb23a4cSdrh if( flag_longlong ){ 398eeb23a4cSdrh v = va_arg(ap,i64); 399eeb23a4cSdrh }else if( flag_long ){ 400eeb23a4cSdrh v = va_arg(ap,long int); 401eeb23a4cSdrh }else{ 402eeb23a4cSdrh v = va_arg(ap,int); 403eeb23a4cSdrh } 404e9707671Sdrh if( v<0 ){ 405e9707671Sdrh longvalue = -v; 406cfcdaefeSdanielk1977 prefix = '-'; 407cfcdaefeSdanielk1977 }else{ 408e9707671Sdrh longvalue = v; 409e9707671Sdrh if( flag_plussign ) prefix = '+'; 410a18c5681Sdrh else if( flag_blanksign ) prefix = ' '; 411a18c5681Sdrh else prefix = 0; 412cfcdaefeSdanielk1977 } 413e9707671Sdrh }else{ 414eeb23a4cSdrh if( flag_longlong ){ 415eeb23a4cSdrh longvalue = va_arg(ap,u64); 416eeb23a4cSdrh }else if( flag_long ){ 417eeb23a4cSdrh longvalue = va_arg(ap,unsigned long int); 418eeb23a4cSdrh }else{ 419eeb23a4cSdrh longvalue = va_arg(ap,unsigned int); 420eeb23a4cSdrh } 421e9707671Sdrh prefix = 0; 422e9707671Sdrh } 423e9707671Sdrh if( longvalue==0 ) flag_alternateform = 0; 424a18c5681Sdrh if( flag_zeropad && precision<width-(prefix!=0) ){ 425a18c5681Sdrh precision = width-(prefix!=0); 426a18c5681Sdrh } 4273039c0a8Sdrh bufpt = &buf[etBUFSIZE-1]; 4289a99334dSdrh if( xtype==etORDINAL ){ 42943f6e064Sdrh static const char zOrd[] = "thstndrd"; 430ea678832Sdrh int x = (int)(longvalue % 10); 43143f6e064Sdrh if( x>=4 || (longvalue/10)%10==1 ){ 43243f6e064Sdrh x = 0; 43343f6e064Sdrh } 43443f6e064Sdrh buf[etBUFSIZE-3] = zOrd[x*2]; 43543f6e064Sdrh buf[etBUFSIZE-2] = zOrd[x*2+1]; 4369a99334dSdrh bufpt -= 2; 4379a99334dSdrh } 438a18c5681Sdrh { 43976ff3a0eSdrh register const char *cset; /* Use registers for speed */ 440a18c5681Sdrh register int base; 44176ff3a0eSdrh cset = &aDigits[infop->charset]; 442a18c5681Sdrh base = infop->base; 443a18c5681Sdrh do{ /* Convert to ascii */ 444a18c5681Sdrh *(--bufpt) = cset[longvalue%base]; 445a18c5681Sdrh longvalue = longvalue/base; 446a18c5681Sdrh }while( longvalue>0 ); 447a18c5681Sdrh } 448ea678832Sdrh length = (int)(&buf[etBUFSIZE-1]-bufpt); 449a18c5681Sdrh for(idx=precision-length; idx>0; idx--){ 450a18c5681Sdrh *(--bufpt) = '0'; /* Zero pad */ 451a18c5681Sdrh } 452a18c5681Sdrh if( prefix ) *(--bufpt) = prefix; /* Add sign */ 453a18c5681Sdrh if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ 45476ff3a0eSdrh const char *pre; 45576ff3a0eSdrh char x; 45676ff3a0eSdrh pre = &aPrefix[infop->prefix]; 45776ff3a0eSdrh for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; 458a18c5681Sdrh } 459ea678832Sdrh length = (int)(&buf[etBUFSIZE-1]-bufpt); 460a18c5681Sdrh break; 461a18c5681Sdrh case etFLOAT: 462a18c5681Sdrh case etEXP: 463a18c5681Sdrh case etGENERIC: 464a18c5681Sdrh realvalue = va_arg(ap,double); 465b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 466a18c5681Sdrh if( precision<0 ) precision = 6; /* Set default precision */ 4675eba8c09Sdrh if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10; 468a18c5681Sdrh if( realvalue<0.0 ){ 469a18c5681Sdrh realvalue = -realvalue; 470a18c5681Sdrh prefix = '-'; 471a18c5681Sdrh }else{ 472a18c5681Sdrh if( flag_plussign ) prefix = '+'; 473a18c5681Sdrh else if( flag_blanksign ) prefix = ' '; 474a18c5681Sdrh else prefix = 0; 475a18c5681Sdrh } 4763e9aeec0Sdrh if( xtype==etGENERIC && precision>0 ) precision--; 4775f968436Sdrh #if 0 478a18c5681Sdrh /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */ 479a18c5681Sdrh for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1); 480a18c5681Sdrh #else 481a18c5681Sdrh /* It makes more sense to use 0.5 */ 48274161705Sdrh for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){} 483a18c5681Sdrh #endif 4843e9aeec0Sdrh if( xtype==etFLOAT ) realvalue += rounder; 485a18c5681Sdrh /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ 486a18c5681Sdrh exp = 0; 487ea678832Sdrh if( sqlite3IsNaN((double)realvalue) ){ 48853c14021Sdrh bufpt = "NaN"; 48953c14021Sdrh length = 3; 49053c14021Sdrh break; 49153c14021Sdrh } 492a18c5681Sdrh if( realvalue>0.0 ){ 49363782855Sdrh while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; } 49463782855Sdrh while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; } 49563782855Sdrh while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; } 496af005fbcSdrh while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } 497af005fbcSdrh while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } 498af005fbcSdrh if( exp>350 ){ 49953c14021Sdrh if( prefix=='-' ){ 50053c14021Sdrh bufpt = "-Inf"; 50153c14021Sdrh }else if( prefix=='+' ){ 50253c14021Sdrh bufpt = "+Inf"; 50353c14021Sdrh }else{ 50453c14021Sdrh bufpt = "Inf"; 50553c14021Sdrh } 506ea678832Sdrh length = sqlite3Strlen30(bufpt); 507a18c5681Sdrh break; 508a18c5681Sdrh } 509a18c5681Sdrh } 510a18c5681Sdrh bufpt = buf; 511a18c5681Sdrh /* 512a18c5681Sdrh ** If the field type is etGENERIC, then convert to either etEXP 513a18c5681Sdrh ** or etFLOAT, as appropriate. 514a18c5681Sdrh */ 515a18c5681Sdrh flag_exp = xtype==etEXP; 516a18c5681Sdrh if( xtype!=etFLOAT ){ 517a18c5681Sdrh realvalue += rounder; 518a18c5681Sdrh if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } 519a18c5681Sdrh } 520a18c5681Sdrh if( xtype==etGENERIC ){ 521a18c5681Sdrh flag_rtz = !flag_alternateform; 522a18c5681Sdrh if( exp<-4 || exp>precision ){ 523a18c5681Sdrh xtype = etEXP; 524a18c5681Sdrh }else{ 525a18c5681Sdrh precision = precision - exp; 526a18c5681Sdrh xtype = etFLOAT; 527a18c5681Sdrh } 528a18c5681Sdrh }else{ 529a18c5681Sdrh flag_rtz = 0; 530a18c5681Sdrh } 531557cc60fSdrh if( xtype==etEXP ){ 532557cc60fSdrh e2 = 0; 533557cc60fSdrh }else{ 534557cc60fSdrh e2 = exp; 535557cc60fSdrh } 536a18c5681Sdrh nsd = 0; 537ea678832Sdrh flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; 538557cc60fSdrh /* The sign in front of the number */ 539557cc60fSdrh if( prefix ){ 540557cc60fSdrh *(bufpt++) = prefix; 541557cc60fSdrh } 542557cc60fSdrh /* Digits prior to the decimal point */ 543557cc60fSdrh if( e2<0 ){ 544557cc60fSdrh *(bufpt++) = '0'; 545557cc60fSdrh }else{ 546557cc60fSdrh for(; e2>=0; e2--){ 547557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 548557cc60fSdrh } 549557cc60fSdrh } 550557cc60fSdrh /* The decimal point */ 551557cc60fSdrh if( flag_dp ){ 552557cc60fSdrh *(bufpt++) = '.'; 553557cc60fSdrh } 554557cc60fSdrh /* "0" digits after the decimal point but before the first 555557cc60fSdrh ** significant digit of the number */ 556af005fbcSdrh for(e2++; e2<0; precision--, e2++){ 557af005fbcSdrh assert( precision>0 ); 558a18c5681Sdrh *(bufpt++) = '0'; 559a18c5681Sdrh } 560557cc60fSdrh /* Significant digits after the decimal point */ 561557cc60fSdrh while( (precision--)>0 ){ 562557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 563a18c5681Sdrh } 564557cc60fSdrh /* Remove trailing zeros and the "." if no digits follow the "." */ 565557cc60fSdrh if( flag_rtz && flag_dp ){ 5663e9aeec0Sdrh while( bufpt[-1]=='0' ) *(--bufpt) = 0; 5673e9aeec0Sdrh assert( bufpt>buf ); 5683e9aeec0Sdrh if( bufpt[-1]=='.' ){ 569557cc60fSdrh if( flag_altform2 ){ 570557cc60fSdrh *(bufpt++) = '0'; 571557cc60fSdrh }else{ 572557cc60fSdrh *(--bufpt) = 0; 573a18c5681Sdrh } 574557cc60fSdrh } 575557cc60fSdrh } 576557cc60fSdrh /* Add the "eNNN" suffix */ 577af005fbcSdrh if( flag_exp || xtype==etEXP ){ 57876ff3a0eSdrh *(bufpt++) = aDigits[infop->charset]; 579557cc60fSdrh if( exp<0 ){ 580557cc60fSdrh *(bufpt++) = '-'; exp = -exp; 581557cc60fSdrh }else{ 582557cc60fSdrh *(bufpt++) = '+'; 583557cc60fSdrh } 584a18c5681Sdrh if( exp>=100 ){ 585ea678832Sdrh *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */ 586a18c5681Sdrh exp %= 100; 587a18c5681Sdrh } 588ea678832Sdrh *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */ 589ea678832Sdrh *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */ 590a18c5681Sdrh } 591557cc60fSdrh *bufpt = 0; 592557cc60fSdrh 593a18c5681Sdrh /* The converted number is in buf[] and zero terminated. Output it. 594a18c5681Sdrh ** Note that the number is in the usual order, not reversed as with 595a18c5681Sdrh ** integer conversions. */ 596ea678832Sdrh length = (int)(bufpt-buf); 597a18c5681Sdrh bufpt = buf; 598a18c5681Sdrh 599a18c5681Sdrh /* Special case: Add leading zeros if the flag_zeropad flag is 600a18c5681Sdrh ** set and we are not left justified */ 601a18c5681Sdrh if( flag_zeropad && !flag_leftjustify && length < width){ 602a18c5681Sdrh int i; 603a18c5681Sdrh int nPad = width - length; 604a18c5681Sdrh for(i=width; i>=nPad; i--){ 605a18c5681Sdrh bufpt[i] = bufpt[i-nPad]; 606a18c5681Sdrh } 607a18c5681Sdrh i = prefix!=0; 608a18c5681Sdrh while( nPad-- ) bufpt[i++] = '0'; 609a18c5681Sdrh length = width; 610a18c5681Sdrh } 611a18c5681Sdrh #endif 612a18c5681Sdrh break; 613a18c5681Sdrh case etSIZE: 614ade86483Sdrh *(va_arg(ap,int*)) = pAccum->nChar; 615a18c5681Sdrh length = width = 0; 616a18c5681Sdrh break; 617a18c5681Sdrh case etPERCENT: 618a18c5681Sdrh buf[0] = '%'; 619a18c5681Sdrh bufpt = buf; 620a18c5681Sdrh length = 1; 621a18c5681Sdrh break; 622a18c5681Sdrh case etCHARX: 623ea678832Sdrh c = va_arg(ap,int); 624ea678832Sdrh buf[0] = (char)c; 625a18c5681Sdrh if( precision>=0 ){ 626ea678832Sdrh for(idx=1; idx<precision; idx++) buf[idx] = (char)c; 627a18c5681Sdrh length = precision; 628a18c5681Sdrh }else{ 629a18c5681Sdrh length =1; 630a18c5681Sdrh } 631a18c5681Sdrh bufpt = buf; 632a18c5681Sdrh break; 633a18c5681Sdrh case etSTRING: 634d93d8a81Sdrh case etDYNSTRING: 635cb485882Sdrh bufpt = va_arg(ap,char*); 636d93d8a81Sdrh if( bufpt==0 ){ 637d93d8a81Sdrh bufpt = ""; 638d93d8a81Sdrh }else if( xtype==etDYNSTRING ){ 639d93d8a81Sdrh zExtra = bufpt; 640d93d8a81Sdrh } 641e509094bSdrh if( precision>=0 ){ 642e509094bSdrh for(length=0; length<precision && bufpt[length]; length++){} 643e509094bSdrh }else{ 644ea678832Sdrh length = sqlite3Strlen30(bufpt); 645e509094bSdrh } 646a18c5681Sdrh break; 647a18c5681Sdrh case etSQLESCAPE: 648f3b863edSdanielk1977 case etSQLESCAPE2: 649f3b863edSdanielk1977 case etSQLESCAPE3: { 650ea678832Sdrh int i, j, n, isnull; 6514794f735Sdrh int needQuote; 652ea678832Sdrh char ch; 653f3b863edSdanielk1977 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */ 654f0113000Sdanielk1977 char *escarg = va_arg(ap,char*); 655f0113000Sdanielk1977 isnull = escarg==0; 656f0113000Sdanielk1977 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); 657f0113000Sdanielk1977 for(i=n=0; (ch=escarg[i])!=0; i++){ 658f3b863edSdanielk1977 if( ch==q ) n++; 659a18c5681Sdrh } 6604794f735Sdrh needQuote = !isnull && xtype==etSQLESCAPE2; 6614794f735Sdrh n += i + 1 + needQuote*2; 662a18c5681Sdrh if( n>etBUFSIZE ){ 663e5ae5735Sdrh bufpt = zExtra = sqlite3Malloc( n ); 664d164fd34Sdrh if( bufpt==0 ){ 665d164fd34Sdrh pAccum->mallocFailed = 1; 666d164fd34Sdrh return; 667d164fd34Sdrh } 668a18c5681Sdrh }else{ 669a18c5681Sdrh bufpt = buf; 670a18c5681Sdrh } 6710cfcf3fbSchw j = 0; 672f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 673f0113000Sdanielk1977 for(i=0; (ch=escarg[i])!=0; i++){ 674f0113000Sdanielk1977 bufpt[j++] = ch; 675f3b863edSdanielk1977 if( ch==q ) bufpt[j++] = ch; 676a18c5681Sdrh } 677f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 678a18c5681Sdrh bufpt[j] = 0; 679a18c5681Sdrh length = j; 68005a82983Sdrh /* The precision is ignored on %q and %Q */ 68105a82983Sdrh /* if( precision>=0 && precision<length ) length = precision; */ 682a18c5681Sdrh break; 6835eba8c09Sdrh } 6845f968436Sdrh case etTOKEN: { 6855f968436Sdrh Token *pToken = va_arg(ap, Token*); 686af005fbcSdrh if( pToken ){ 687ade86483Sdrh sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n); 688ad6d9460Sdrh } 6895f968436Sdrh length = width = 0; 6905f968436Sdrh break; 6915f968436Sdrh } 6925f968436Sdrh case etSRCLIST: { 6935f968436Sdrh SrcList *pSrc = va_arg(ap, SrcList*); 6945f968436Sdrh int k = va_arg(ap, int); 6955f968436Sdrh struct SrcList_item *pItem = &pSrc->a[k]; 6965f968436Sdrh assert( k>=0 && k<pSrc->nSrc ); 69793a960a0Sdrh if( pItem->zDatabase ){ 698ade86483Sdrh sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1); 699ade86483Sdrh sqlite3StrAccumAppend(pAccum, ".", 1); 7005f968436Sdrh } 701ade86483Sdrh sqlite3StrAccumAppend(pAccum, pItem->zName, -1); 7025f968436Sdrh length = width = 0; 7035f968436Sdrh break; 7045f968436Sdrh } 705874ba04cSdrh default: { 706874ba04cSdrh assert( xtype==etINVALID ); 707874ba04cSdrh return; 708874ba04cSdrh } 709a18c5681Sdrh }/* End switch over the format type */ 710a18c5681Sdrh /* 711a18c5681Sdrh ** The text of the conversion is pointed to by "bufpt" and is 712a18c5681Sdrh ** "length" characters long. The field width is "width". Do 713a18c5681Sdrh ** the output. 714a18c5681Sdrh */ 715a18c5681Sdrh if( !flag_leftjustify ){ 716a18c5681Sdrh register int nspace; 717a18c5681Sdrh nspace = width-length; 718a18c5681Sdrh if( nspace>0 ){ 719ade86483Sdrh appendSpace(pAccum, nspace); 720a18c5681Sdrh } 721a18c5681Sdrh } 722a18c5681Sdrh if( length>0 ){ 723ade86483Sdrh sqlite3StrAccumAppend(pAccum, bufpt, length); 724a18c5681Sdrh } 725a18c5681Sdrh if( flag_leftjustify ){ 726a18c5681Sdrh register int nspace; 727a18c5681Sdrh nspace = width-length; 728a18c5681Sdrh if( nspace>0 ){ 729ade86483Sdrh appendSpace(pAccum, nspace); 730a18c5681Sdrh } 731a18c5681Sdrh } 732a18c5681Sdrh if( zExtra ){ 7331e536953Sdanielk1977 sqlite3_free(zExtra); 734a18c5681Sdrh } 735a18c5681Sdrh }/* End for loop over the format string */ 736a18c5681Sdrh } /* End of function */ 737a18c5681Sdrh 738a18c5681Sdrh /* 739ade86483Sdrh ** Append N bytes of text from z to the StrAccum object. 740a18c5681Sdrh */ 741ade86483Sdrh void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){ 742bc6160b0Sdrh assert( z!=0 || N==0 ); 743ade86483Sdrh if( p->tooBig | p->mallocFailed ){ 744bc6160b0Sdrh testcase(p->tooBig); 745bc6160b0Sdrh testcase(p->mallocFailed); 74617435752Sdrh return; 747ade86483Sdrh } 748ade86483Sdrh if( N<0 ){ 749ea678832Sdrh N = sqlite3Strlen30(z); 750ade86483Sdrh } 751bc6160b0Sdrh if( N==0 || NEVER(z==0) ){ 752ade86483Sdrh return; 753ade86483Sdrh } 754ade86483Sdrh if( p->nChar+N >= p->nAlloc ){ 755ade86483Sdrh char *zNew; 756ade86483Sdrh if( !p->useMalloc ){ 757ade86483Sdrh p->tooBig = 1; 758ade86483Sdrh N = p->nAlloc - p->nChar - 1; 759ade86483Sdrh if( N<=0 ){ 760ade86483Sdrh return; 7615f968436Sdrh } 762a18c5681Sdrh }else{ 76393a960a0Sdrh i64 szNew = p->nChar; 764b1a6c3c1Sdrh szNew += N + 1; 765b1a6c3c1Sdrh if( szNew > p->mxAlloc ){ 766ade86483Sdrh sqlite3StrAccumReset(p); 767ade86483Sdrh p->tooBig = 1; 768eaad32b1Sdrh return; 769b1a6c3c1Sdrh }else{ 770ea678832Sdrh p->nAlloc = (int)szNew; 771a18c5681Sdrh } 772633e6d57Sdrh zNew = sqlite3DbMallocRaw(p->db, p->nAlloc ); 77353f733c7Sdrh if( zNew ){ 774ade86483Sdrh memcpy(zNew, p->zText, p->nChar); 775ade86483Sdrh sqlite3StrAccumReset(p); 776ade86483Sdrh p->zText = zNew; 777ade86483Sdrh }else{ 778ade86483Sdrh p->mallocFailed = 1; 779ade86483Sdrh sqlite3StrAccumReset(p); 780ade86483Sdrh return; 78153f733c7Sdrh } 7825f968436Sdrh } 7835f968436Sdrh } 784ade86483Sdrh memcpy(&p->zText[p->nChar], z, N); 785ade86483Sdrh p->nChar += N; 786483750baSdrh } 787483750baSdrh 788483750baSdrh /* 789ade86483Sdrh ** Finish off a string by making sure it is zero-terminated. 790ade86483Sdrh ** Return a pointer to the resulting string. Return a NULL 791ade86483Sdrh ** pointer if any kind of error was encountered. 7925f968436Sdrh */ 793ade86483Sdrh char *sqlite3StrAccumFinish(StrAccum *p){ 794ade86483Sdrh if( p->zText ){ 795ade86483Sdrh p->zText[p->nChar] = 0; 796ade86483Sdrh if( p->useMalloc && p->zText==p->zBase ){ 797633e6d57Sdrh p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); 798ade86483Sdrh if( p->zText ){ 799ade86483Sdrh memcpy(p->zText, p->zBase, p->nChar+1); 800ade86483Sdrh }else{ 801ade86483Sdrh p->mallocFailed = 1; 802ade86483Sdrh } 803ade86483Sdrh } 804ade86483Sdrh } 805ade86483Sdrh return p->zText; 806ade86483Sdrh } 807ade86483Sdrh 808ade86483Sdrh /* 809ade86483Sdrh ** Reset an StrAccum string. Reclaim all malloced memory. 810ade86483Sdrh */ 811ade86483Sdrh void sqlite3StrAccumReset(StrAccum *p){ 812ade86483Sdrh if( p->zText!=p->zBase ){ 813633e6d57Sdrh sqlite3DbFree(p->db, p->zText); 814ade86483Sdrh } 815f089aa45Sdrh p->zText = 0; 816ade86483Sdrh } 817ade86483Sdrh 818ade86483Sdrh /* 819ade86483Sdrh ** Initialize a string accumulator 820ade86483Sdrh */ 821f089aa45Sdrh void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){ 822ade86483Sdrh p->zText = p->zBase = zBase; 823633e6d57Sdrh p->db = 0; 824ade86483Sdrh p->nChar = 0; 825ade86483Sdrh p->nAlloc = n; 826bb4957f8Sdrh p->mxAlloc = mx; 827ade86483Sdrh p->useMalloc = 1; 828ade86483Sdrh p->tooBig = 0; 829ade86483Sdrh p->mallocFailed = 0; 8305f968436Sdrh } 8315f968436Sdrh 8325f968436Sdrh /* 8335f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 8345f968436Sdrh ** %-conversion extensions. 8355f968436Sdrh */ 83617435752Sdrh char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ 83717435752Sdrh char *z; 83879158e18Sdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 839ade86483Sdrh StrAccum acc; 840bc6160b0Sdrh assert( db!=0 ); 841bb4957f8Sdrh sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), 842bc6160b0Sdrh db->aLimit[SQLITE_LIMIT_LENGTH]); 843633e6d57Sdrh acc.db = db; 844f089aa45Sdrh sqlite3VXPrintf(&acc, 1, zFormat, ap); 845ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 846bc6160b0Sdrh if( acc.mallocFailed ){ 84717435752Sdrh db->mallocFailed = 1; 84817435752Sdrh } 84917435752Sdrh return z; 8505f968436Sdrh } 8515f968436Sdrh 8525f968436Sdrh /* 8535f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 8545f968436Sdrh ** %-conversion extensions. 8555f968436Sdrh */ 85617435752Sdrh char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ 8575f968436Sdrh va_list ap; 8585f968436Sdrh char *z; 8595f968436Sdrh va_start(ap, zFormat); 860ade86483Sdrh z = sqlite3VMPrintf(db, zFormat, ap); 8615f968436Sdrh va_end(ap); 8625f968436Sdrh return z; 8635f968436Sdrh } 8645f968436Sdrh 8655f968436Sdrh /* 866633e6d57Sdrh ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting 867633e6d57Sdrh ** the string and before returnning. This routine is intended to be used 868633e6d57Sdrh ** to modify an existing string. For example: 869633e6d57Sdrh ** 870633e6d57Sdrh ** x = sqlite3MPrintf(db, x, "prefix %s suffix", x); 871633e6d57Sdrh ** 872633e6d57Sdrh */ 873633e6d57Sdrh char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){ 874633e6d57Sdrh va_list ap; 875633e6d57Sdrh char *z; 876633e6d57Sdrh va_start(ap, zFormat); 877633e6d57Sdrh z = sqlite3VMPrintf(db, zFormat, ap); 878633e6d57Sdrh va_end(ap); 879633e6d57Sdrh sqlite3DbFree(db, zStr); 880633e6d57Sdrh return z; 881633e6d57Sdrh } 882633e6d57Sdrh 883633e6d57Sdrh /* 88428dd479cSdrh ** Print into memory obtained from sqlite3_malloc(). Omit the internal 88528dd479cSdrh ** %-conversion extensions. 88628dd479cSdrh */ 88728dd479cSdrh char *sqlite3_vmprintf(const char *zFormat, va_list ap){ 888ade86483Sdrh char *z; 88928dd479cSdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 890ade86483Sdrh StrAccum acc; 891ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT 892ff1590eeSdrh if( sqlite3_initialize() ) return 0; 893ff1590eeSdrh #endif 894bb4957f8Sdrh sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); 895f089aa45Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 896ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 897ade86483Sdrh return z; 89828dd479cSdrh } 89928dd479cSdrh 90028dd479cSdrh /* 90128dd479cSdrh ** Print into memory obtained from sqlite3_malloc()(). Omit the internal 90228dd479cSdrh ** %-conversion extensions. 903483750baSdrh */ 9046f8a503dSdanielk1977 char *sqlite3_mprintf(const char *zFormat, ...){ 905a18c5681Sdrh va_list ap; 9065f968436Sdrh char *z; 907ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT 908ff1590eeSdrh if( sqlite3_initialize() ) return 0; 909ff1590eeSdrh #endif 910a18c5681Sdrh va_start(ap, zFormat); 911b3738b6cSdrh z = sqlite3_vmprintf(zFormat, ap); 912a18c5681Sdrh va_end(ap); 9135f968436Sdrh return z; 914a18c5681Sdrh } 915a18c5681Sdrh 916a18c5681Sdrh /* 9176f8a503dSdanielk1977 ** sqlite3_snprintf() works like snprintf() except that it ignores the 91893a5c6bdSdrh ** current locale settings. This is important for SQLite because we 91993a5c6bdSdrh ** are not able to use a "," as the decimal point in place of "." as 92093a5c6bdSdrh ** specified by some locales. 92193a5c6bdSdrh */ 9226f8a503dSdanielk1977 char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ 9235f968436Sdrh char *z; 92493a5c6bdSdrh va_list ap; 925ade86483Sdrh StrAccum acc; 92693a5c6bdSdrh 92768853907Sdrh if( n<=0 ){ 92868853907Sdrh return zBuf; 92968853907Sdrh } 930bb4957f8Sdrh sqlite3StrAccumInit(&acc, zBuf, n, 0); 931ade86483Sdrh acc.useMalloc = 0; 93293a5c6bdSdrh va_start(ap,zFormat); 933f089aa45Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 93493a5c6bdSdrh va_end(ap); 935ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 9365f968436Sdrh return z; 93793a5c6bdSdrh } 93893a5c6bdSdrh 93985e9e22bSdrh #if defined(SQLITE_DEBUG) 940e54ca3feSdrh /* 941e54ca3feSdrh ** A version of printf() that understands %lld. Used for debugging. 942e54ca3feSdrh ** The printf() built into some versions of windows does not understand %lld 943e54ca3feSdrh ** and segfaults if you give it a long long int. 944e54ca3feSdrh */ 945e54ca3feSdrh void sqlite3DebugPrintf(const char *zFormat, ...){ 946e54ca3feSdrh va_list ap; 947ade86483Sdrh StrAccum acc; 948e54ca3feSdrh char zBuf[500]; 949bb4957f8Sdrh sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0); 950ade86483Sdrh acc.useMalloc = 0; 951e54ca3feSdrh va_start(ap,zFormat); 952f089aa45Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 953e54ca3feSdrh va_end(ap); 954bed8e7e5Sdrh sqlite3StrAccumFinish(&acc); 955485f0039Sdrh fprintf(stdout,"%s", zBuf); 9562ac3ee97Sdrh fflush(stdout); 957e54ca3feSdrh } 958e54ca3feSdrh #endif 959