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" 547c68d60bSdrh 55a18c5681Sdrh /* 56a18c5681Sdrh ** Conversion types fall into various categories as defined by the 57a18c5681Sdrh ** following enumeration. 58a18c5681Sdrh */ 59e84a306bSdrh #define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */ 60e84a306bSdrh #define etFLOAT 2 /* Floating point. %f */ 61e84a306bSdrh #define etEXP 3 /* Exponentional notation. %e and %E */ 62e84a306bSdrh #define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */ 63e84a306bSdrh #define etSIZE 5 /* Return number of characters processed so far. %n */ 64e84a306bSdrh #define etSTRING 6 /* Strings. %s */ 65e84a306bSdrh #define etDYNSTRING 7 /* Dynamically allocated strings. %z */ 66e84a306bSdrh #define etPERCENT 8 /* Percent symbol. %% */ 67e84a306bSdrh #define etCHARX 9 /* Characters. %c */ 68a18c5681Sdrh /* The rest are extensions, not normally found in printf() */ 6905a82983Sdrh #define etCHARLIT 10 /* Literal characters. %' */ 7005a82983Sdrh #define etSQLESCAPE 11 /* Strings with '\'' doubled. %q */ 7105a82983Sdrh #define etSQLESCAPE2 12 /* Strings with '\'' doubled and enclosed in '', 720cfcf3fbSchw NULL pointers replaced by SQL NULL. %Q */ 7305a82983Sdrh #define etTOKEN 13 /* a pointer to a Token structure */ 7405a82983Sdrh #define etSRCLIST 14 /* a pointer to a SrcList */ 7505a82983Sdrh #define etPOINTER 15 /* The %p conversion */ 76f3b863edSdanielk1977 #define etSQLESCAPE3 16 /* %w -> Strings with '\"' doubled */ 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 }, 115153c62c4Sdrh { 'z', 0, 4, etDYNSTRING, 0, 0 }, 1164794f735Sdrh { 'q', 0, 4, etSQLESCAPE, 0, 0 }, 1174794f735Sdrh { 'Q', 0, 4, etSQLESCAPE2, 0, 0 }, 118f3b863edSdanielk1977 { 'w', 0, 4, etSQLESCAPE3, 0, 0 }, 119e84a306bSdrh { 'c', 0, 0, etCHARX, 0, 0 }, 12076ff3a0eSdrh { 'o', 8, 0, etRADIX, 0, 2 }, 12176ff3a0eSdrh { 'u', 10, 0, etRADIX, 0, 0 }, 12276ff3a0eSdrh { 'x', 16, 0, etRADIX, 16, 1 }, 12376ff3a0eSdrh { 'X', 16, 0, etRADIX, 0, 4 }, 124b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 125e84a306bSdrh { 'f', 0, 1, etFLOAT, 0, 0 }, 12676ff3a0eSdrh { 'e', 0, 1, etEXP, 30, 0 }, 12776ff3a0eSdrh { 'E', 0, 1, etEXP, 14, 0 }, 12876ff3a0eSdrh { 'G', 0, 1, etGENERIC, 14, 0 }, 129b37df7b9Sdrh #endif 13076ff3a0eSdrh { 'i', 10, 1, etRADIX, 0, 0 }, 131e84a306bSdrh { 'n', 0, 0, etSIZE, 0, 0 }, 132e84a306bSdrh { '%', 0, 0, etPERCENT, 0, 0 }, 13376ff3a0eSdrh { 'p', 16, 0, etPOINTER, 0, 1 }, 1345f968436Sdrh { 'T', 0, 2, etTOKEN, 0, 0 }, 1355f968436Sdrh { 'S', 0, 2, etSRCLIST, 0, 0 }, 136a18c5681Sdrh }; 137a18c5681Sdrh #define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0])) 138a18c5681Sdrh 139a18c5681Sdrh /* 140b37df7b9Sdrh ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point 141a18c5681Sdrh ** conversions will work. 142a18c5681Sdrh */ 143b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 144a18c5681Sdrh /* 145a18c5681Sdrh ** "*val" is a double such that 0.1 <= *val < 10.0 146a18c5681Sdrh ** Return the ascii code for the leading digit of *val, then 147a18c5681Sdrh ** multiply "*val" by 10.0 to renormalize. 148a18c5681Sdrh ** 149a18c5681Sdrh ** Example: 150a18c5681Sdrh ** input: *val = 3.14159 151a18c5681Sdrh ** output: *val = 1.4159 function return = '3' 152a18c5681Sdrh ** 153a18c5681Sdrh ** The counter *cnt is incremented each time. After counter exceeds 154a18c5681Sdrh ** 16 (the number of significant digits in a 64-bit float) '0' is 155a18c5681Sdrh ** always returned. 156a18c5681Sdrh */ 157384eef32Sdrh static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ 158a18c5681Sdrh int digit; 159384eef32Sdrh LONGDOUBLE_TYPE d; 160a18c5681Sdrh if( (*cnt)++ >= 16 ) return '0'; 161a18c5681Sdrh digit = (int)*val; 162a18c5681Sdrh d = digit; 163a18c5681Sdrh digit += '0'; 164a18c5681Sdrh *val = (*val - d)*10.0; 165a18c5681Sdrh return digit; 166a18c5681Sdrh } 167b37df7b9Sdrh #endif /* SQLITE_OMIT_FLOATING_POINT */ 168a18c5681Sdrh 16979158e18Sdrh /* 170ade86483Sdrh ** Append N space characters to the given string buffer. 171ade86483Sdrh */ 172ade86483Sdrh static void appendSpace(StrAccum *pAccum, int N){ 173ade86483Sdrh static const char zSpaces[] = " "; 174ade86483Sdrh while( N>=sizeof(zSpaces)-1 ){ 175ade86483Sdrh sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1); 176ade86483Sdrh N -= sizeof(zSpaces)-1; 177ade86483Sdrh } 178ade86483Sdrh if( N>0 ){ 179ade86483Sdrh sqlite3StrAccumAppend(pAccum, zSpaces, N); 180ade86483Sdrh } 181ade86483Sdrh } 182ade86483Sdrh 183ade86483Sdrh /* 18479158e18Sdrh ** On machines with a small stack size, you can redefine the 18579158e18Sdrh ** SQLITE_PRINT_BUF_SIZE to be less than 350. But beware - for 18679158e18Sdrh ** smaller values some %f conversions may go into an infinite loop. 18779158e18Sdrh */ 18879158e18Sdrh #ifndef SQLITE_PRINT_BUF_SIZE 18979158e18Sdrh # define SQLITE_PRINT_BUF_SIZE 350 19079158e18Sdrh #endif 19179158e18Sdrh #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ 192a18c5681Sdrh 193a18c5681Sdrh /* 194a18c5681Sdrh ** The root program. All variations call this core. 195a18c5681Sdrh ** 196a18c5681Sdrh ** INPUTS: 197a18c5681Sdrh ** func This is a pointer to a function taking three arguments 198a18c5681Sdrh ** 1. A pointer to anything. Same as the "arg" parameter. 199a18c5681Sdrh ** 2. A pointer to the list of characters to be output 200a18c5681Sdrh ** (Note, this list is NOT null terminated.) 201a18c5681Sdrh ** 3. An integer number of characters to be output. 202a18c5681Sdrh ** (Note: This number might be zero.) 203a18c5681Sdrh ** 204a18c5681Sdrh ** arg This is the pointer to anything which will be passed as the 205a18c5681Sdrh ** first argument to "func". Use it for whatever you like. 206a18c5681Sdrh ** 207a18c5681Sdrh ** fmt This is the format string, as in the usual print. 208a18c5681Sdrh ** 209a18c5681Sdrh ** ap This is a pointer to a list of arguments. Same as in 210a18c5681Sdrh ** vfprint. 211a18c5681Sdrh ** 212a18c5681Sdrh ** OUTPUTS: 213a18c5681Sdrh ** The return value is the total number of characters sent to 214a18c5681Sdrh ** the function "func". Returns -1 on a error. 215a18c5681Sdrh ** 216a18c5681Sdrh ** Note that the order in which automatic variables are declared below 217a18c5681Sdrh ** seems to make a big difference in determining how fast this beast 218a18c5681Sdrh ** will run. 219a18c5681Sdrh */ 220ade86483Sdrh static void vxprintf( 221ade86483Sdrh StrAccum *pAccum, /* Accumulate results here */ 2225f968436Sdrh int useExtended, /* Allow extended %-conversions */ 2235f968436Sdrh const char *fmt, /* Format string */ 2245f968436Sdrh va_list ap /* arguments */ 225a18c5681Sdrh ){ 226e84a306bSdrh int c; /* Next character in the format string */ 227e84a306bSdrh char *bufpt; /* Pointer to the conversion buffer */ 228e84a306bSdrh int precision; /* Precision of the current field */ 229e84a306bSdrh int length; /* Length of the field */ 230e84a306bSdrh int idx; /* A general purpose loop counter */ 231a18c5681Sdrh int width; /* Width of the current field */ 232e84a306bSdrh etByte flag_leftjustify; /* True if "-" flag is present */ 233e84a306bSdrh etByte flag_plussign; /* True if "+" flag is present */ 234e84a306bSdrh etByte flag_blanksign; /* True if " " flag is present */ 235e84a306bSdrh etByte flag_alternateform; /* True if "#" flag is present */ 236531fe878Sdrh etByte flag_altform2; /* True if "!" flag is present */ 237e84a306bSdrh etByte flag_zeropad; /* True if field width constant starts with zero */ 238e84a306bSdrh etByte flag_long; /* True if "l" flag is present */ 239a34b6764Sdrh etByte flag_longlong; /* True if the "ll" flag is present */ 2403e9aeec0Sdrh etByte done; /* Loop termination flag */ 24127436af7Sdrh sqlite_uint64 longvalue; /* Value for integer types */ 242384eef32Sdrh LONGDOUBLE_TYPE realvalue; /* Value for real types */ 2435719628aSdrh const et_info *infop; /* Pointer to the appropriate info structure */ 244a18c5681Sdrh char buf[etBUFSIZE]; /* Conversion buffer */ 245a18c5681Sdrh char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ 246e84a306bSdrh etByte errorflag = 0; /* True if an error is encountered */ 247e84a306bSdrh etByte xtype; /* Conversion paradigm */ 248a18c5681Sdrh char *zExtra; /* Extra memory used for etTCLESCAPE conversions */ 249b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 250557cc60fSdrh int exp, e2; /* exponent of real numbers */ 251a18c5681Sdrh double rounder; /* Used for rounding floating point values */ 252e84a306bSdrh etByte flag_dp; /* True if decimal point should be shown */ 253e84a306bSdrh etByte flag_rtz; /* True if trailing zeros should be removed */ 254e84a306bSdrh etByte flag_exp; /* True to force display of the exponent */ 255a18c5681Sdrh int nsd; /* Number of significant digits returned */ 256a18c5681Sdrh #endif 257a18c5681Sdrh 258ade86483Sdrh length = 0; 259a18c5681Sdrh bufpt = 0; 260a18c5681Sdrh for(; (c=(*fmt))!=0; ++fmt){ 261a18c5681Sdrh if( c!='%' ){ 262e84a306bSdrh int amt; 263a18c5681Sdrh bufpt = (char *)fmt; 264a18c5681Sdrh amt = 1; 265a18c5681Sdrh while( (c=(*++fmt))!='%' && c!=0 ) amt++; 266ade86483Sdrh sqlite3StrAccumAppend(pAccum, bufpt, amt); 267a18c5681Sdrh if( c==0 ) break; 268a18c5681Sdrh } 269a18c5681Sdrh if( (c=(*++fmt))==0 ){ 270a18c5681Sdrh errorflag = 1; 271ade86483Sdrh sqlite3StrAccumAppend(pAccum, "%", 1); 272a18c5681Sdrh break; 273a18c5681Sdrh } 274a18c5681Sdrh /* Find out what flags are present */ 275a18c5681Sdrh flag_leftjustify = flag_plussign = flag_blanksign = 276557cc60fSdrh flag_alternateform = flag_altform2 = flag_zeropad = 0; 2773e9aeec0Sdrh done = 0; 278a18c5681Sdrh do{ 279a18c5681Sdrh switch( c ){ 2803e9aeec0Sdrh case '-': flag_leftjustify = 1; break; 2813e9aeec0Sdrh case '+': flag_plussign = 1; break; 2823e9aeec0Sdrh case ' ': flag_blanksign = 1; break; 2833e9aeec0Sdrh case '#': flag_alternateform = 1; break; 2843e9aeec0Sdrh case '!': flag_altform2 = 1; break; 2853e9aeec0Sdrh case '0': flag_zeropad = 1; break; 2863e9aeec0Sdrh default: done = 1; break; 287a18c5681Sdrh } 2883e9aeec0Sdrh }while( !done && (c=(*++fmt))!=0 ); 289a18c5681Sdrh /* Get the field width */ 290a18c5681Sdrh width = 0; 291a18c5681Sdrh if( c=='*' ){ 292a18c5681Sdrh width = va_arg(ap,int); 293a18c5681Sdrh if( width<0 ){ 294a18c5681Sdrh flag_leftjustify = 1; 295a18c5681Sdrh width = -width; 296a18c5681Sdrh } 297a18c5681Sdrh c = *++fmt; 298a18c5681Sdrh }else{ 29917a68934Sdrh while( c>='0' && c<='9' ){ 300a18c5681Sdrh width = width*10 + c - '0'; 301a18c5681Sdrh c = *++fmt; 302a18c5681Sdrh } 303a18c5681Sdrh } 304a18c5681Sdrh if( width > etBUFSIZE-10 ){ 305a18c5681Sdrh width = etBUFSIZE-10; 306a18c5681Sdrh } 307a18c5681Sdrh /* Get the precision */ 308a18c5681Sdrh if( c=='.' ){ 309a18c5681Sdrh precision = 0; 310a18c5681Sdrh c = *++fmt; 311a18c5681Sdrh if( c=='*' ){ 312a18c5681Sdrh precision = va_arg(ap,int); 313a18c5681Sdrh if( precision<0 ) precision = -precision; 314a18c5681Sdrh c = *++fmt; 315a18c5681Sdrh }else{ 31617a68934Sdrh while( c>='0' && c<='9' ){ 317a18c5681Sdrh precision = precision*10 + c - '0'; 318a18c5681Sdrh c = *++fmt; 319a18c5681Sdrh } 320a18c5681Sdrh } 321a18c5681Sdrh }else{ 322a18c5681Sdrh precision = -1; 323a18c5681Sdrh } 324a18c5681Sdrh /* Get the conversion type modifier */ 325a18c5681Sdrh if( c=='l' ){ 326a18c5681Sdrh flag_long = 1; 327a18c5681Sdrh c = *++fmt; 328a34b6764Sdrh if( c=='l' ){ 329a34b6764Sdrh flag_longlong = 1; 330a34b6764Sdrh c = *++fmt; 331a18c5681Sdrh }else{ 332a34b6764Sdrh flag_longlong = 0; 333a34b6764Sdrh } 334a34b6764Sdrh }else{ 335a34b6764Sdrh flag_long = flag_longlong = 0; 336a18c5681Sdrh } 337a18c5681Sdrh /* Fetch the info entry for the field */ 338a18c5681Sdrh infop = 0; 339a18c5681Sdrh for(idx=0; idx<etNINFO; idx++){ 340a18c5681Sdrh if( c==fmtinfo[idx].fmttype ){ 341a18c5681Sdrh infop = &fmtinfo[idx]; 3425f968436Sdrh if( useExtended || (infop->flags & FLAG_INTERN)==0 ){ 343e84a306bSdrh xtype = infop->type; 344c4413565Sdrh }else{ 345ade86483Sdrh return; 3465f968436Sdrh } 347a18c5681Sdrh break; 348a18c5681Sdrh } 349a18c5681Sdrh } 350a18c5681Sdrh zExtra = 0; 35143617e9aSdrh if( infop==0 ){ 352ade86483Sdrh return; 35343617e9aSdrh } 35443617e9aSdrh 355a18c5681Sdrh 3564794f735Sdrh /* Limit the precision to prevent overflowing buf[] during conversion */ 3574794f735Sdrh if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){ 3584794f735Sdrh precision = etBUFSIZE-40; 3594794f735Sdrh } 3604794f735Sdrh 361a18c5681Sdrh /* 362a18c5681Sdrh ** At this point, variables are initialized as follows: 363a18c5681Sdrh ** 364a18c5681Sdrh ** flag_alternateform TRUE if a '#' is present. 3653e9aeec0Sdrh ** flag_altform2 TRUE if a '!' is present. 366a18c5681Sdrh ** flag_plussign TRUE if a '+' is present. 367a18c5681Sdrh ** flag_leftjustify TRUE if a '-' is present or if the 368a18c5681Sdrh ** field width was negative. 369a18c5681Sdrh ** flag_zeropad TRUE if the width began with 0. 370a18c5681Sdrh ** flag_long TRUE if the letter 'l' (ell) prefixed 371a18c5681Sdrh ** the conversion character. 372a34b6764Sdrh ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed 373a34b6764Sdrh ** the conversion character. 374a18c5681Sdrh ** flag_blanksign TRUE if a ' ' is present. 375a18c5681Sdrh ** width The specified field width. This is 376a18c5681Sdrh ** always non-negative. Zero is the default. 377a18c5681Sdrh ** precision The specified precision. The default 378a18c5681Sdrh ** is -1. 379a18c5681Sdrh ** xtype The class of the conversion. 380a18c5681Sdrh ** infop Pointer to the appropriate info struct. 381a18c5681Sdrh */ 382a18c5681Sdrh switch( xtype ){ 383fe63d1c9Sdrh case etPOINTER: 384fe63d1c9Sdrh flag_longlong = sizeof(char*)==sizeof(i64); 385fe63d1c9Sdrh flag_long = sizeof(char*)==sizeof(long int); 386fe63d1c9Sdrh /* Fall through into the next case */ 387a18c5681Sdrh case etRADIX: 388e84a306bSdrh if( infop->flags & FLAG_SIGNED ){ 389e9707671Sdrh i64 v; 390e9707671Sdrh if( flag_longlong ) v = va_arg(ap,i64); 391e9707671Sdrh else if( flag_long ) v = va_arg(ap,long int); 392e9707671Sdrh else v = va_arg(ap,int); 393e9707671Sdrh if( v<0 ){ 394e9707671Sdrh longvalue = -v; 395cfcdaefeSdanielk1977 prefix = '-'; 396cfcdaefeSdanielk1977 }else{ 397e9707671Sdrh longvalue = v; 398e9707671Sdrh if( flag_plussign ) prefix = '+'; 399a18c5681Sdrh else if( flag_blanksign ) prefix = ' '; 400a18c5681Sdrh else prefix = 0; 401cfcdaefeSdanielk1977 } 402e9707671Sdrh }else{ 403e9707671Sdrh if( flag_longlong ) longvalue = va_arg(ap,u64); 404e9707671Sdrh else if( flag_long ) longvalue = va_arg(ap,unsigned long int); 405e9707671Sdrh else longvalue = va_arg(ap,unsigned int); 406e9707671Sdrh prefix = 0; 407e9707671Sdrh } 408e9707671Sdrh if( longvalue==0 ) flag_alternateform = 0; 409a18c5681Sdrh if( flag_zeropad && precision<width-(prefix!=0) ){ 410a18c5681Sdrh precision = width-(prefix!=0); 411a18c5681Sdrh } 4123039c0a8Sdrh bufpt = &buf[etBUFSIZE-1]; 413a18c5681Sdrh { 41476ff3a0eSdrh register const char *cset; /* Use registers for speed */ 415a18c5681Sdrh register int base; 41676ff3a0eSdrh cset = &aDigits[infop->charset]; 417a18c5681Sdrh base = infop->base; 418a18c5681Sdrh do{ /* Convert to ascii */ 419a18c5681Sdrh *(--bufpt) = cset[longvalue%base]; 420a18c5681Sdrh longvalue = longvalue/base; 421a18c5681Sdrh }while( longvalue>0 ); 422a18c5681Sdrh } 4233039c0a8Sdrh length = &buf[etBUFSIZE-1]-bufpt; 424a18c5681Sdrh for(idx=precision-length; idx>0; idx--){ 425a18c5681Sdrh *(--bufpt) = '0'; /* Zero pad */ 426a18c5681Sdrh } 427a18c5681Sdrh if( prefix ) *(--bufpt) = prefix; /* Add sign */ 428a18c5681Sdrh if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ 42976ff3a0eSdrh const char *pre; 43076ff3a0eSdrh char x; 43176ff3a0eSdrh pre = &aPrefix[infop->prefix]; 432a18c5681Sdrh if( *bufpt!=pre[0] ){ 43376ff3a0eSdrh for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; 434a18c5681Sdrh } 435a18c5681Sdrh } 4363039c0a8Sdrh length = &buf[etBUFSIZE-1]-bufpt; 437a18c5681Sdrh break; 438a18c5681Sdrh case etFLOAT: 439a18c5681Sdrh case etEXP: 440a18c5681Sdrh case etGENERIC: 441a18c5681Sdrh realvalue = va_arg(ap,double); 442b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 443a18c5681Sdrh if( precision<0 ) precision = 6; /* Set default precision */ 4445eba8c09Sdrh if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10; 445a18c5681Sdrh if( realvalue<0.0 ){ 446a18c5681Sdrh realvalue = -realvalue; 447a18c5681Sdrh prefix = '-'; 448a18c5681Sdrh }else{ 449a18c5681Sdrh if( flag_plussign ) prefix = '+'; 450a18c5681Sdrh else if( flag_blanksign ) prefix = ' '; 451a18c5681Sdrh else prefix = 0; 452a18c5681Sdrh } 4533e9aeec0Sdrh if( xtype==etGENERIC && precision>0 ) precision--; 4545f968436Sdrh #if 0 455a18c5681Sdrh /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */ 456a18c5681Sdrh for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1); 457a18c5681Sdrh #else 458a18c5681Sdrh /* It makes more sense to use 0.5 */ 45974161705Sdrh for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){} 460a18c5681Sdrh #endif 4613e9aeec0Sdrh if( xtype==etFLOAT ) realvalue += rounder; 462a18c5681Sdrh /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ 463a18c5681Sdrh exp = 0; 4647cd6927eSdrh if( sqlite3_isnan(realvalue) ){ 46553c14021Sdrh bufpt = "NaN"; 46653c14021Sdrh length = 3; 46753c14021Sdrh break; 46853c14021Sdrh } 469a18c5681Sdrh if( realvalue>0.0 ){ 47063782855Sdrh while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; } 47163782855Sdrh while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; } 47263782855Sdrh while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; } 473b621c237Sdrh while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; } 474b621c237Sdrh while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; } 475b621c237Sdrh if( exp>350 || exp<-350 ){ 47653c14021Sdrh if( prefix=='-' ){ 47753c14021Sdrh bufpt = "-Inf"; 47853c14021Sdrh }else if( prefix=='+' ){ 47953c14021Sdrh bufpt = "+Inf"; 48053c14021Sdrh }else{ 48153c14021Sdrh bufpt = "Inf"; 48253c14021Sdrh } 48353c14021Sdrh length = strlen(bufpt); 484a18c5681Sdrh break; 485a18c5681Sdrh } 486a18c5681Sdrh } 487a18c5681Sdrh bufpt = buf; 488a18c5681Sdrh /* 489a18c5681Sdrh ** If the field type is etGENERIC, then convert to either etEXP 490a18c5681Sdrh ** or etFLOAT, as appropriate. 491a18c5681Sdrh */ 492a18c5681Sdrh flag_exp = xtype==etEXP; 493a18c5681Sdrh if( xtype!=etFLOAT ){ 494a18c5681Sdrh realvalue += rounder; 495a18c5681Sdrh if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } 496a18c5681Sdrh } 497a18c5681Sdrh if( xtype==etGENERIC ){ 498a18c5681Sdrh flag_rtz = !flag_alternateform; 499a18c5681Sdrh if( exp<-4 || exp>precision ){ 500a18c5681Sdrh xtype = etEXP; 501a18c5681Sdrh }else{ 502a18c5681Sdrh precision = precision - exp; 503a18c5681Sdrh xtype = etFLOAT; 504a18c5681Sdrh } 505a18c5681Sdrh }else{ 506a18c5681Sdrh flag_rtz = 0; 507a18c5681Sdrh } 508557cc60fSdrh if( xtype==etEXP ){ 509557cc60fSdrh e2 = 0; 510557cc60fSdrh }else{ 511557cc60fSdrh e2 = exp; 512557cc60fSdrh } 513a18c5681Sdrh nsd = 0; 514557cc60fSdrh flag_dp = (precision>0) | flag_alternateform | flag_altform2; 515557cc60fSdrh /* The sign in front of the number */ 516557cc60fSdrh if( prefix ){ 517557cc60fSdrh *(bufpt++) = prefix; 518557cc60fSdrh } 519557cc60fSdrh /* Digits prior to the decimal point */ 520557cc60fSdrh if( e2<0 ){ 521557cc60fSdrh *(bufpt++) = '0'; 522557cc60fSdrh }else{ 523557cc60fSdrh for(; e2>=0; e2--){ 524557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 525557cc60fSdrh } 526557cc60fSdrh } 527557cc60fSdrh /* The decimal point */ 528557cc60fSdrh if( flag_dp ){ 529557cc60fSdrh *(bufpt++) = '.'; 530557cc60fSdrh } 531557cc60fSdrh /* "0" digits after the decimal point but before the first 532557cc60fSdrh ** significant digit of the number */ 533557cc60fSdrh for(e2++; e2<0 && precision>0; precision--, e2++){ 534a18c5681Sdrh *(bufpt++) = '0'; 535a18c5681Sdrh } 536557cc60fSdrh /* Significant digits after the decimal point */ 537557cc60fSdrh while( (precision--)>0 ){ 538557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 539a18c5681Sdrh } 540557cc60fSdrh /* Remove trailing zeros and the "." if no digits follow the "." */ 541557cc60fSdrh if( flag_rtz && flag_dp ){ 5423e9aeec0Sdrh while( bufpt[-1]=='0' ) *(--bufpt) = 0; 5433e9aeec0Sdrh assert( bufpt>buf ); 5443e9aeec0Sdrh if( bufpt[-1]=='.' ){ 545557cc60fSdrh if( flag_altform2 ){ 546557cc60fSdrh *(bufpt++) = '0'; 547557cc60fSdrh }else{ 548557cc60fSdrh *(--bufpt) = 0; 549a18c5681Sdrh } 550557cc60fSdrh } 551557cc60fSdrh } 552557cc60fSdrh /* Add the "eNNN" suffix */ 553557cc60fSdrh if( flag_exp || (xtype==etEXP && exp) ){ 55476ff3a0eSdrh *(bufpt++) = aDigits[infop->charset]; 555557cc60fSdrh if( exp<0 ){ 556557cc60fSdrh *(bufpt++) = '-'; exp = -exp; 557557cc60fSdrh }else{ 558557cc60fSdrh *(bufpt++) = '+'; 559557cc60fSdrh } 560a18c5681Sdrh if( exp>=100 ){ 561a18c5681Sdrh *(bufpt++) = (exp/100)+'0'; /* 100's digit */ 562a18c5681Sdrh exp %= 100; 563a18c5681Sdrh } 564a18c5681Sdrh *(bufpt++) = exp/10+'0'; /* 10's digit */ 565a18c5681Sdrh *(bufpt++) = exp%10+'0'; /* 1's digit */ 566a18c5681Sdrh } 567557cc60fSdrh *bufpt = 0; 568557cc60fSdrh 569a18c5681Sdrh /* The converted number is in buf[] and zero terminated. Output it. 570a18c5681Sdrh ** Note that the number is in the usual order, not reversed as with 571a18c5681Sdrh ** integer conversions. */ 57294ce4c1eSdrh length = bufpt-buf; 573a18c5681Sdrh bufpt = buf; 574a18c5681Sdrh 575a18c5681Sdrh /* Special case: Add leading zeros if the flag_zeropad flag is 576a18c5681Sdrh ** set and we are not left justified */ 577a18c5681Sdrh if( flag_zeropad && !flag_leftjustify && length < width){ 578a18c5681Sdrh int i; 579a18c5681Sdrh int nPad = width - length; 580a18c5681Sdrh for(i=width; i>=nPad; i--){ 581a18c5681Sdrh bufpt[i] = bufpt[i-nPad]; 582a18c5681Sdrh } 583a18c5681Sdrh i = prefix!=0; 584a18c5681Sdrh while( nPad-- ) bufpt[i++] = '0'; 585a18c5681Sdrh length = width; 586a18c5681Sdrh } 587a18c5681Sdrh #endif 588a18c5681Sdrh break; 589a18c5681Sdrh case etSIZE: 590ade86483Sdrh *(va_arg(ap,int*)) = pAccum->nChar; 591a18c5681Sdrh length = width = 0; 592a18c5681Sdrh break; 593a18c5681Sdrh case etPERCENT: 594a18c5681Sdrh buf[0] = '%'; 595a18c5681Sdrh bufpt = buf; 596a18c5681Sdrh length = 1; 597a18c5681Sdrh break; 598a18c5681Sdrh case etCHARLIT: 599a18c5681Sdrh case etCHARX: 600a18c5681Sdrh c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt); 601a18c5681Sdrh if( precision>=0 ){ 602a18c5681Sdrh for(idx=1; idx<precision; idx++) buf[idx] = c; 603a18c5681Sdrh length = precision; 604a18c5681Sdrh }else{ 605a18c5681Sdrh length =1; 606a18c5681Sdrh } 607a18c5681Sdrh bufpt = buf; 608a18c5681Sdrh break; 609a18c5681Sdrh case etSTRING: 610d93d8a81Sdrh case etDYNSTRING: 611cb485882Sdrh bufpt = va_arg(ap,char*); 612d93d8a81Sdrh if( bufpt==0 ){ 613d93d8a81Sdrh bufpt = ""; 614d93d8a81Sdrh }else if( xtype==etDYNSTRING ){ 615d93d8a81Sdrh zExtra = bufpt; 616d93d8a81Sdrh } 617a18c5681Sdrh length = strlen(bufpt); 618a18c5681Sdrh if( precision>=0 && precision<length ) length = precision; 619a18c5681Sdrh break; 620a18c5681Sdrh case etSQLESCAPE: 621f3b863edSdanielk1977 case etSQLESCAPE2: 622f3b863edSdanielk1977 case etSQLESCAPE3: { 623f0113000Sdanielk1977 int i, j, n, ch, isnull; 6244794f735Sdrh int needQuote; 625f3b863edSdanielk1977 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */ 626f0113000Sdanielk1977 char *escarg = va_arg(ap,char*); 627f0113000Sdanielk1977 isnull = escarg==0; 628f0113000Sdanielk1977 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); 629f0113000Sdanielk1977 for(i=n=0; (ch=escarg[i])!=0; i++){ 630f3b863edSdanielk1977 if( ch==q ) n++; 631a18c5681Sdrh } 6324794f735Sdrh needQuote = !isnull && xtype==etSQLESCAPE2; 6334794f735Sdrh n += i + 1 + needQuote*2; 634a18c5681Sdrh if( n>etBUFSIZE ){ 63517435752Sdrh bufpt = zExtra = sqlite3_malloc( n ); 636ade86483Sdrh if( bufpt==0 ) return; 637a18c5681Sdrh }else{ 638a18c5681Sdrh bufpt = buf; 639a18c5681Sdrh } 6400cfcf3fbSchw j = 0; 641f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 642f0113000Sdanielk1977 for(i=0; (ch=escarg[i])!=0; i++){ 643f0113000Sdanielk1977 bufpt[j++] = ch; 644f3b863edSdanielk1977 if( ch==q ) bufpt[j++] = ch; 645a18c5681Sdrh } 646f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 647a18c5681Sdrh bufpt[j] = 0; 648a18c5681Sdrh length = j; 64905a82983Sdrh /* The precision is ignored on %q and %Q */ 65005a82983Sdrh /* if( precision>=0 && precision<length ) length = precision; */ 651a18c5681Sdrh break; 6525eba8c09Sdrh } 6535f968436Sdrh case etTOKEN: { 6545f968436Sdrh Token *pToken = va_arg(ap, Token*); 655ad6d9460Sdrh if( pToken && pToken->z ){ 656ade86483Sdrh sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n); 657ad6d9460Sdrh } 6585f968436Sdrh length = width = 0; 6595f968436Sdrh break; 6605f968436Sdrh } 6615f968436Sdrh case etSRCLIST: { 6625f968436Sdrh SrcList *pSrc = va_arg(ap, SrcList*); 6635f968436Sdrh int k = va_arg(ap, int); 6645f968436Sdrh struct SrcList_item *pItem = &pSrc->a[k]; 6655f968436Sdrh assert( k>=0 && k<pSrc->nSrc ); 6665f968436Sdrh if( pItem->zDatabase && pItem->zDatabase[0] ){ 667ade86483Sdrh sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1); 668ade86483Sdrh sqlite3StrAccumAppend(pAccum, ".", 1); 6695f968436Sdrh } 670ade86483Sdrh sqlite3StrAccumAppend(pAccum, pItem->zName, -1); 6715f968436Sdrh length = width = 0; 6725f968436Sdrh break; 6735f968436Sdrh } 674a18c5681Sdrh }/* End switch over the format type */ 675a18c5681Sdrh /* 676a18c5681Sdrh ** The text of the conversion is pointed to by "bufpt" and is 677a18c5681Sdrh ** "length" characters long. The field width is "width". Do 678a18c5681Sdrh ** the output. 679a18c5681Sdrh */ 680a18c5681Sdrh if( !flag_leftjustify ){ 681a18c5681Sdrh register int nspace; 682a18c5681Sdrh nspace = width-length; 683a18c5681Sdrh if( nspace>0 ){ 684ade86483Sdrh appendSpace(pAccum, nspace); 685a18c5681Sdrh } 686a18c5681Sdrh } 687a18c5681Sdrh if( length>0 ){ 688ade86483Sdrh sqlite3StrAccumAppend(pAccum, bufpt, length); 689a18c5681Sdrh } 690a18c5681Sdrh if( flag_leftjustify ){ 691a18c5681Sdrh register int nspace; 692a18c5681Sdrh nspace = width-length; 693a18c5681Sdrh if( nspace>0 ){ 694ade86483Sdrh appendSpace(pAccum, nspace); 695a18c5681Sdrh } 696a18c5681Sdrh } 697a18c5681Sdrh if( zExtra ){ 6981e536953Sdanielk1977 sqlite3_free(zExtra); 699a18c5681Sdrh } 700a18c5681Sdrh }/* End for loop over the format string */ 701a18c5681Sdrh } /* End of function */ 702a18c5681Sdrh 703a18c5681Sdrh /* 704ade86483Sdrh ** Append N bytes of text from z to the StrAccum object. 705a18c5681Sdrh */ 706ade86483Sdrh void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){ 707ade86483Sdrh if( p->tooBig | p->mallocFailed ){ 70817435752Sdrh return; 709ade86483Sdrh } 710ade86483Sdrh if( N<0 ){ 711ade86483Sdrh N = strlen(z); 712ade86483Sdrh } 713ade86483Sdrh if( N==0 ){ 714ade86483Sdrh return; 715ade86483Sdrh } 716ade86483Sdrh if( p->nChar+N >= p->nAlloc ){ 717ade86483Sdrh char *zNew; 718ade86483Sdrh if( !p->useMalloc ){ 719ade86483Sdrh p->tooBig = 1; 720ade86483Sdrh N = p->nAlloc - p->nChar - 1; 721ade86483Sdrh if( N<=0 ){ 722ade86483Sdrh return; 7235f968436Sdrh } 724a18c5681Sdrh }else{ 725ade86483Sdrh p->nAlloc += p->nAlloc + N + 1; 726ade86483Sdrh if( p->nAlloc > SQLITE_MAX_LENGTH ){ 727ade86483Sdrh p->nAlloc = SQLITE_MAX_LENGTH; 728ade86483Sdrh if( p->nChar+N >= p->nAlloc ){ 729ade86483Sdrh sqlite3StrAccumReset(p); 730ade86483Sdrh p->tooBig = 1; 731eaad32b1Sdrh return; 73253f733c7Sdrh } 733a18c5681Sdrh } 734ade86483Sdrh zNew = sqlite3_malloc( p->nAlloc ); 73553f733c7Sdrh if( zNew ){ 736ade86483Sdrh memcpy(zNew, p->zText, p->nChar); 737ade86483Sdrh sqlite3StrAccumReset(p); 738ade86483Sdrh p->zText = zNew; 739ade86483Sdrh }else{ 740ade86483Sdrh p->mallocFailed = 1; 741ade86483Sdrh sqlite3StrAccumReset(p); 742ade86483Sdrh return; 74353f733c7Sdrh } 7445f968436Sdrh } 7455f968436Sdrh } 746ade86483Sdrh memcpy(&p->zText[p->nChar], z, N); 747ade86483Sdrh p->nChar += N; 748483750baSdrh } 749483750baSdrh 750483750baSdrh /* 751ade86483Sdrh ** Finish off a string by making sure it is zero-terminated. 752ade86483Sdrh ** Return a pointer to the resulting string. Return a NULL 753ade86483Sdrh ** pointer if any kind of error was encountered. 7545f968436Sdrh */ 755ade86483Sdrh char *sqlite3StrAccumFinish(StrAccum *p){ 756ade86483Sdrh if( p->zText ){ 757ade86483Sdrh p->zText[p->nChar] = 0; 758ade86483Sdrh if( p->useMalloc && p->zText==p->zBase ){ 759ade86483Sdrh p->zText = sqlite3_malloc( p->nChar+1 ); 760ade86483Sdrh if( p->zText ){ 761ade86483Sdrh memcpy(p->zText, p->zBase, p->nChar+1); 762ade86483Sdrh }else{ 763ade86483Sdrh p->mallocFailed = 1; 764ade86483Sdrh } 765ade86483Sdrh } 766ade86483Sdrh } 767ade86483Sdrh return p->zText; 768ade86483Sdrh } 769ade86483Sdrh 770ade86483Sdrh /* 771ade86483Sdrh ** Reset an StrAccum string. Reclaim all malloced memory. 772ade86483Sdrh */ 773ade86483Sdrh void sqlite3StrAccumReset(StrAccum *p){ 774ade86483Sdrh if( p->zText!=p->zBase ){ 775ade86483Sdrh sqlite3_free(p->zText); 776ade86483Sdrh p->zText = 0; 777ade86483Sdrh } 778ade86483Sdrh } 779ade86483Sdrh 780ade86483Sdrh /* 781ade86483Sdrh ** Initialize a string accumulator 782ade86483Sdrh */ 783ade86483Sdrh static void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n){ 784ade86483Sdrh p->zText = p->zBase = zBase; 785ade86483Sdrh p->nChar = 0; 786ade86483Sdrh p->nAlloc = n; 787ade86483Sdrh p->useMalloc = 1; 788ade86483Sdrh p->tooBig = 0; 789ade86483Sdrh p->mallocFailed = 0; 7905f968436Sdrh } 7915f968436Sdrh 7925f968436Sdrh /* 7935f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 7945f968436Sdrh ** %-conversion extensions. 7955f968436Sdrh */ 79617435752Sdrh char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ 79717435752Sdrh char *z; 79879158e18Sdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 799ade86483Sdrh StrAccum acc; 800ade86483Sdrh sqlite3StrAccumInit(&acc, zBase, sizeof(zBase)); 801ade86483Sdrh vxprintf(&acc, 1, zFormat, ap); 802ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 803ade86483Sdrh if( acc.mallocFailed && db ){ 80417435752Sdrh db->mallocFailed = 1; 80517435752Sdrh } 80617435752Sdrh return z; 8075f968436Sdrh } 8085f968436Sdrh 8095f968436Sdrh /* 8105f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 8115f968436Sdrh ** %-conversion extensions. 8125f968436Sdrh */ 81317435752Sdrh char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ 8145f968436Sdrh va_list ap; 8155f968436Sdrh char *z; 8165f968436Sdrh va_start(ap, zFormat); 817ade86483Sdrh z = sqlite3VMPrintf(db, zFormat, ap); 8185f968436Sdrh va_end(ap); 8195f968436Sdrh return z; 8205f968436Sdrh } 8215f968436Sdrh 8225f968436Sdrh /* 82328dd479cSdrh ** Print into memory obtained from sqlite3_malloc(). Omit the internal 82428dd479cSdrh ** %-conversion extensions. 82528dd479cSdrh */ 82628dd479cSdrh char *sqlite3_vmprintf(const char *zFormat, va_list ap){ 827ade86483Sdrh char *z; 82828dd479cSdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 829ade86483Sdrh StrAccum acc; 830ade86483Sdrh sqlite3StrAccumInit(&acc, zBase, sizeof(zBase)); 831ade86483Sdrh vxprintf(&acc, 0, zFormat, ap); 832ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 833ade86483Sdrh return z; 83428dd479cSdrh } 83528dd479cSdrh 83628dd479cSdrh /* 83728dd479cSdrh ** Print into memory obtained from sqlite3_malloc()(). Omit the internal 83828dd479cSdrh ** %-conversion extensions. 839483750baSdrh */ 8406f8a503dSdanielk1977 char *sqlite3_mprintf(const char *zFormat, ...){ 841a18c5681Sdrh va_list ap; 8425f968436Sdrh char *z; 843a18c5681Sdrh va_start(ap, zFormat); 844b3738b6cSdrh z = sqlite3_vmprintf(zFormat, ap); 845a18c5681Sdrh va_end(ap); 8465f968436Sdrh return z; 847a18c5681Sdrh } 848a18c5681Sdrh 849a18c5681Sdrh /* 8506f8a503dSdanielk1977 ** sqlite3_snprintf() works like snprintf() except that it ignores the 85193a5c6bdSdrh ** current locale settings. This is important for SQLite because we 85293a5c6bdSdrh ** are not able to use a "," as the decimal point in place of "." as 85393a5c6bdSdrh ** specified by some locales. 85493a5c6bdSdrh */ 8556f8a503dSdanielk1977 char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ 8565f968436Sdrh char *z; 85793a5c6bdSdrh va_list ap; 858ade86483Sdrh StrAccum acc; 85993a5c6bdSdrh 86068853907Sdrh if( n<=0 ){ 86168853907Sdrh return zBuf; 86268853907Sdrh } 863ade86483Sdrh sqlite3StrAccumInit(&acc, zBuf, n); 864ade86483Sdrh acc.useMalloc = 0; 86593a5c6bdSdrh va_start(ap,zFormat); 866ade86483Sdrh vxprintf(&acc, 0, zFormat, ap); 86793a5c6bdSdrh va_end(ap); 868ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 8695f968436Sdrh return z; 87093a5c6bdSdrh } 87193a5c6bdSdrh 8727d7f17b6Sdrh #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) || defined(SQLITE_MEMDEBUG) 873e54ca3feSdrh /* 874e54ca3feSdrh ** A version of printf() that understands %lld. Used for debugging. 875e54ca3feSdrh ** The printf() built into some versions of windows does not understand %lld 876e54ca3feSdrh ** and segfaults if you give it a long long int. 877e54ca3feSdrh */ 878e54ca3feSdrh void sqlite3DebugPrintf(const char *zFormat, ...){ 879e54ca3feSdrh va_list ap; 880ade86483Sdrh StrAccum acc; 881e54ca3feSdrh char zBuf[500]; 882ade86483Sdrh sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf)); 883ade86483Sdrh acc.useMalloc = 0; 884e54ca3feSdrh va_start(ap,zFormat); 885ade86483Sdrh vxprintf(&acc, 0, zFormat, ap); 886e54ca3feSdrh va_end(ap); 887*bed8e7e5Sdrh sqlite3StrAccumFinish(&acc); 888485f0039Sdrh fprintf(stdout,"%s", zBuf); 8892ac3ee97Sdrh fflush(stdout); 890e54ca3feSdrh } 891e54ca3feSdrh #endif 892