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() */ 69af005fbcSdrh #define etSQLESCAPE 10 /* Strings with '\'' doubled. %q */ 70af005fbcSdrh #define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '', 710cfcf3fbSchw NULL pointers replaced by SQL NULL. %Q */ 72af005fbcSdrh #define etTOKEN 12 /* a pointer to a Token structure */ 73af005fbcSdrh #define etSRCLIST 13 /* a pointer to a SrcList */ 74af005fbcSdrh #define etPOINTER 14 /* The %p conversion */ 75af005fbcSdrh #define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */ 76af005fbcSdrh #define etORDINAL 16 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */ 77e84a306bSdrh 78874ba04cSdrh #define etINVALID 0 /* Any unrecognized conversion type */ 79874ba04cSdrh 80e84a306bSdrh 81e84a306bSdrh /* 82e84a306bSdrh ** An "etByte" is an 8-bit unsigned value. 83e84a306bSdrh */ 84e84a306bSdrh typedef unsigned char etByte; 85a18c5681Sdrh 86a18c5681Sdrh /* 87a18c5681Sdrh ** Each builtin conversion character (ex: the 'd' in "%d") is described 88a18c5681Sdrh ** by an instance of the following structure 89a18c5681Sdrh */ 90a18c5681Sdrh typedef struct et_info { /* Information about each format field */ 91e84a306bSdrh char fmttype; /* The format field code letter */ 92e84a306bSdrh etByte base; /* The base for radix conversion */ 93e84a306bSdrh etByte flags; /* One or more of FLAG_ constants below */ 94e84a306bSdrh etByte type; /* Conversion paradigm */ 9576ff3a0eSdrh etByte charset; /* Offset into aDigits[] of the digits string */ 9676ff3a0eSdrh etByte prefix; /* Offset into aPrefix[] of the prefix string */ 97a18c5681Sdrh } et_info; 98a18c5681Sdrh 99a18c5681Sdrh /* 100e84a306bSdrh ** Allowed values for et_info.flags 101e84a306bSdrh */ 102e84a306bSdrh #define FLAG_SIGNED 1 /* True if the value to convert is signed */ 103e84a306bSdrh #define FLAG_INTERN 2 /* True if for internal use only */ 1044794f735Sdrh #define FLAG_STRING 4 /* Allow infinity precision */ 105e84a306bSdrh 106e84a306bSdrh 107e84a306bSdrh /* 108a18c5681Sdrh ** The following table is searched linearly, so it is good to put the 109a18c5681Sdrh ** most frequently used conversion types first. 110a18c5681Sdrh */ 11176ff3a0eSdrh static const char aDigits[] = "0123456789ABCDEF0123456789abcdef"; 11276ff3a0eSdrh static const char aPrefix[] = "-x0\000X0"; 1135719628aSdrh static const et_info fmtinfo[] = { 11476ff3a0eSdrh { 'd', 10, 1, etRADIX, 0, 0 }, 1154794f735Sdrh { 's', 0, 4, etSTRING, 0, 0 }, 116557cc60fSdrh { 'g', 0, 1, etGENERIC, 30, 0 }, 117153c62c4Sdrh { 'z', 0, 4, etDYNSTRING, 0, 0 }, 1184794f735Sdrh { 'q', 0, 4, etSQLESCAPE, 0, 0 }, 1194794f735Sdrh { 'Q', 0, 4, etSQLESCAPE2, 0, 0 }, 120f3b863edSdanielk1977 { 'w', 0, 4, etSQLESCAPE3, 0, 0 }, 121e84a306bSdrh { 'c', 0, 0, etCHARX, 0, 0 }, 12276ff3a0eSdrh { 'o', 8, 0, etRADIX, 0, 2 }, 12376ff3a0eSdrh { 'u', 10, 0, etRADIX, 0, 0 }, 12476ff3a0eSdrh { 'x', 16, 0, etRADIX, 16, 1 }, 12576ff3a0eSdrh { 'X', 16, 0, etRADIX, 0, 4 }, 126b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 127e84a306bSdrh { 'f', 0, 1, etFLOAT, 0, 0 }, 12876ff3a0eSdrh { 'e', 0, 1, etEXP, 30, 0 }, 12976ff3a0eSdrh { 'E', 0, 1, etEXP, 14, 0 }, 13076ff3a0eSdrh { 'G', 0, 1, etGENERIC, 14, 0 }, 131b37df7b9Sdrh #endif 13276ff3a0eSdrh { 'i', 10, 1, etRADIX, 0, 0 }, 133e84a306bSdrh { 'n', 0, 0, etSIZE, 0, 0 }, 134e84a306bSdrh { '%', 0, 0, etPERCENT, 0, 0 }, 13576ff3a0eSdrh { 'p', 16, 0, etPOINTER, 0, 1 }, 1367e3ff5d8Sdrh 1377e3ff5d8Sdrh /* All the rest have the FLAG_INTERN bit set and are thus for internal 1387e3ff5d8Sdrh ** use only */ 1395f968436Sdrh { 'T', 0, 2, etTOKEN, 0, 0 }, 1405f968436Sdrh { 'S', 0, 2, etSRCLIST, 0, 0 }, 1419a99334dSdrh { 'r', 10, 3, etORDINAL, 0, 0 }, 142a18c5681Sdrh }; 143a18c5681Sdrh 144a18c5681Sdrh /* 145b37df7b9Sdrh ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point 146a18c5681Sdrh ** conversions will work. 147a18c5681Sdrh */ 148b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 149a18c5681Sdrh /* 150a18c5681Sdrh ** "*val" is a double such that 0.1 <= *val < 10.0 151a18c5681Sdrh ** Return the ascii code for the leading digit of *val, then 152a18c5681Sdrh ** multiply "*val" by 10.0 to renormalize. 153a18c5681Sdrh ** 154a18c5681Sdrh ** Example: 155a18c5681Sdrh ** input: *val = 3.14159 156a18c5681Sdrh ** output: *val = 1.4159 function return = '3' 157a18c5681Sdrh ** 158a18c5681Sdrh ** The counter *cnt is incremented each time. After counter exceeds 159a18c5681Sdrh ** 16 (the number of significant digits in a 64-bit float) '0' is 160a18c5681Sdrh ** always returned. 161a18c5681Sdrh */ 162ea678832Sdrh static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ 163a18c5681Sdrh int digit; 164384eef32Sdrh LONGDOUBLE_TYPE d; 165a18c5681Sdrh if( (*cnt)++ >= 16 ) return '0'; 166a18c5681Sdrh digit = (int)*val; 167a18c5681Sdrh d = digit; 168a18c5681Sdrh digit += '0'; 169a18c5681Sdrh *val = (*val - d)*10.0; 170ea678832Sdrh return (char)digit; 171a18c5681Sdrh } 172b37df7b9Sdrh #endif /* SQLITE_OMIT_FLOATING_POINT */ 173a18c5681Sdrh 17479158e18Sdrh /* 175ade86483Sdrh ** Append N space characters to the given string buffer. 176ade86483Sdrh */ 177ade86483Sdrh static void appendSpace(StrAccum *pAccum, int N){ 178ade86483Sdrh static const char zSpaces[] = " "; 17900e13613Sdanielk1977 while( N>=(int)sizeof(zSpaces)-1 ){ 180ade86483Sdrh sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1); 181ade86483Sdrh N -= sizeof(zSpaces)-1; 182ade86483Sdrh } 183ade86483Sdrh if( N>0 ){ 184ade86483Sdrh sqlite3StrAccumAppend(pAccum, zSpaces, N); 185ade86483Sdrh } 186ade86483Sdrh } 187ade86483Sdrh 188ade86483Sdrh /* 18979158e18Sdrh ** On machines with a small stack size, you can redefine the 19050d654daSdrh ** SQLITE_PRINT_BUF_SIZE to be less than 350. 19179158e18Sdrh */ 19279158e18Sdrh #ifndef SQLITE_PRINT_BUF_SIZE 193*59eedf79Sdrh # define SQLITE_PRINT_BUF_SIZE 70 19450d654daSdrh #endif 19579158e18Sdrh #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ 196a18c5681Sdrh 197a18c5681Sdrh /* 198a18c5681Sdrh ** The root program. All variations call this core. 199a18c5681Sdrh ** 200a18c5681Sdrh ** INPUTS: 201a18c5681Sdrh ** func This is a pointer to a function taking three arguments 202a18c5681Sdrh ** 1. A pointer to anything. Same as the "arg" parameter. 203a18c5681Sdrh ** 2. A pointer to the list of characters to be output 204a18c5681Sdrh ** (Note, this list is NOT null terminated.) 205a18c5681Sdrh ** 3. An integer number of characters to be output. 206a18c5681Sdrh ** (Note: This number might be zero.) 207a18c5681Sdrh ** 208a18c5681Sdrh ** arg This is the pointer to anything which will be passed as the 209a18c5681Sdrh ** first argument to "func". Use it for whatever you like. 210a18c5681Sdrh ** 211a18c5681Sdrh ** fmt This is the format string, as in the usual print. 212a18c5681Sdrh ** 213a18c5681Sdrh ** ap This is a pointer to a list of arguments. Same as in 214a18c5681Sdrh ** vfprint. 215a18c5681Sdrh ** 216a18c5681Sdrh ** OUTPUTS: 217a18c5681Sdrh ** The return value is the total number of characters sent to 218a18c5681Sdrh ** the function "func". Returns -1 on a error. 219a18c5681Sdrh ** 220a18c5681Sdrh ** Note that the order in which automatic variables are declared below 221a18c5681Sdrh ** seems to make a big difference in determining how fast this beast 222a18c5681Sdrh ** will run. 223a18c5681Sdrh */ 224f089aa45Sdrh void sqlite3VXPrintf( 225ade86483Sdrh StrAccum *pAccum, /* Accumulate results here */ 2265f968436Sdrh int useExtended, /* Allow extended %-conversions */ 2275f968436Sdrh const char *fmt, /* Format string */ 2285f968436Sdrh va_list ap /* arguments */ 229a18c5681Sdrh ){ 230e84a306bSdrh int c; /* Next character in the format string */ 231e84a306bSdrh char *bufpt; /* Pointer to the conversion buffer */ 232e84a306bSdrh int precision; /* Precision of the current field */ 233e84a306bSdrh int length; /* Length of the field */ 234e84a306bSdrh int idx; /* A general purpose loop counter */ 235a18c5681Sdrh int width; /* Width of the current field */ 236e84a306bSdrh etByte flag_leftjustify; /* True if "-" flag is present */ 237e84a306bSdrh etByte flag_plussign; /* True if "+" flag is present */ 238e84a306bSdrh etByte flag_blanksign; /* True if " " flag is present */ 239e84a306bSdrh etByte flag_alternateform; /* True if "#" flag is present */ 240531fe878Sdrh etByte flag_altform2; /* True if "!" flag is present */ 241e84a306bSdrh etByte flag_zeropad; /* True if field width constant starts with zero */ 242e84a306bSdrh etByte flag_long; /* True if "l" flag is present */ 243a34b6764Sdrh etByte flag_longlong; /* True if the "ll" flag is present */ 2443e9aeec0Sdrh etByte done; /* Loop termination flag */ 24527436af7Sdrh sqlite_uint64 longvalue; /* Value for integer types */ 246384eef32Sdrh LONGDOUBLE_TYPE realvalue; /* Value for real types */ 2475719628aSdrh const et_info *infop; /* Pointer to the appropriate info structure */ 248a18c5681Sdrh char buf[etBUFSIZE]; /* Conversion buffer */ 249*59eedf79Sdrh char *zOut; /* Rendering buffer */ 250*59eedf79Sdrh int nOut; /* Size of the rendering buffer */ 251a18c5681Sdrh char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ 252b27b7f5dSdrh etByte xtype = 0; /* Conversion paradigm */ 253a18c5681Sdrh char *zExtra; /* Extra memory used for etTCLESCAPE conversions */ 254b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 255557cc60fSdrh int exp, e2; /* exponent of real numbers */ 256a18c5681Sdrh double rounder; /* Used for rounding floating point values */ 257e84a306bSdrh etByte flag_dp; /* True if decimal point should be shown */ 258e84a306bSdrh etByte flag_rtz; /* True if trailing zeros should be removed */ 259a18c5681Sdrh int nsd; /* Number of significant digits returned */ 260a18c5681Sdrh #endif 261a18c5681Sdrh 262ade86483Sdrh length = 0; 263a18c5681Sdrh bufpt = 0; 264a18c5681Sdrh for(; (c=(*fmt))!=0; ++fmt){ 265a18c5681Sdrh if( c!='%' ){ 266e84a306bSdrh int amt; 267a18c5681Sdrh bufpt = (char *)fmt; 268a18c5681Sdrh amt = 1; 269a18c5681Sdrh while( (c=(*++fmt))!='%' && c!=0 ) amt++; 270ade86483Sdrh sqlite3StrAccumAppend(pAccum, bufpt, amt); 271a18c5681Sdrh if( c==0 ) break; 272a18c5681Sdrh } 273a18c5681Sdrh if( (c=(*++fmt))==0 ){ 274ade86483Sdrh sqlite3StrAccumAppend(pAccum, "%", 1); 275a18c5681Sdrh break; 276a18c5681Sdrh } 277a18c5681Sdrh /* Find out what flags are present */ 278a18c5681Sdrh flag_leftjustify = flag_plussign = flag_blanksign = 279557cc60fSdrh flag_alternateform = flag_altform2 = flag_zeropad = 0; 2803e9aeec0Sdrh done = 0; 281a18c5681Sdrh do{ 282a18c5681Sdrh switch( c ){ 2833e9aeec0Sdrh case '-': flag_leftjustify = 1; break; 2843e9aeec0Sdrh case '+': flag_plussign = 1; break; 2853e9aeec0Sdrh case ' ': flag_blanksign = 1; break; 2863e9aeec0Sdrh case '#': flag_alternateform = 1; break; 2873e9aeec0Sdrh case '!': flag_altform2 = 1; break; 2883e9aeec0Sdrh case '0': flag_zeropad = 1; break; 2893e9aeec0Sdrh default: done = 1; break; 290a18c5681Sdrh } 2913e9aeec0Sdrh }while( !done && (c=(*++fmt))!=0 ); 292a18c5681Sdrh /* Get the field width */ 293a18c5681Sdrh width = 0; 294a18c5681Sdrh if( c=='*' ){ 295a18c5681Sdrh width = va_arg(ap,int); 296a18c5681Sdrh if( width<0 ){ 297a18c5681Sdrh flag_leftjustify = 1; 298a18c5681Sdrh width = -width; 299a18c5681Sdrh } 300a18c5681Sdrh c = *++fmt; 301a18c5681Sdrh }else{ 30217a68934Sdrh while( c>='0' && c<='9' ){ 303a18c5681Sdrh width = width*10 + c - '0'; 304a18c5681Sdrh c = *++fmt; 305a18c5681Sdrh } 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 */ 338874ba04cSdrh infop = &fmtinfo[0]; 339874ba04cSdrh xtype = etINVALID; 34000e13613Sdanielk1977 for(idx=0; idx<ArraySize(fmtinfo); idx++){ 341a18c5681Sdrh if( c==fmtinfo[idx].fmttype ){ 342a18c5681Sdrh infop = &fmtinfo[idx]; 3435f968436Sdrh if( useExtended || (infop->flags & FLAG_INTERN)==0 ){ 344e84a306bSdrh xtype = infop->type; 345c4413565Sdrh }else{ 346ade86483Sdrh return; 3475f968436Sdrh } 348a18c5681Sdrh break; 349a18c5681Sdrh } 350a18c5681Sdrh } 351a18c5681Sdrh zExtra = 0; 35243617e9aSdrh 353a18c5681Sdrh /* 354a18c5681Sdrh ** At this point, variables are initialized as follows: 355a18c5681Sdrh ** 356a18c5681Sdrh ** flag_alternateform TRUE if a '#' is present. 3573e9aeec0Sdrh ** flag_altform2 TRUE if a '!' is present. 358a18c5681Sdrh ** flag_plussign TRUE if a '+' is present. 359a18c5681Sdrh ** flag_leftjustify TRUE if a '-' is present or if the 360a18c5681Sdrh ** field width was negative. 361a18c5681Sdrh ** flag_zeropad TRUE if the width began with 0. 362a18c5681Sdrh ** flag_long TRUE if the letter 'l' (ell) prefixed 363a18c5681Sdrh ** the conversion character. 364a34b6764Sdrh ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed 365a34b6764Sdrh ** the conversion character. 366a18c5681Sdrh ** flag_blanksign TRUE if a ' ' is present. 367a18c5681Sdrh ** width The specified field width. This is 368a18c5681Sdrh ** always non-negative. Zero is the default. 369a18c5681Sdrh ** precision The specified precision. The default 370a18c5681Sdrh ** is -1. 371a18c5681Sdrh ** xtype The class of the conversion. 372a18c5681Sdrh ** infop Pointer to the appropriate info struct. 373a18c5681Sdrh */ 374a18c5681Sdrh switch( xtype ){ 375fe63d1c9Sdrh case etPOINTER: 376fe63d1c9Sdrh flag_longlong = sizeof(char*)==sizeof(i64); 377fe63d1c9Sdrh flag_long = sizeof(char*)==sizeof(long int); 378fe63d1c9Sdrh /* Fall through into the next case */ 3799a99334dSdrh case etORDINAL: 380a18c5681Sdrh case etRADIX: 381e84a306bSdrh if( infop->flags & FLAG_SIGNED ){ 382e9707671Sdrh i64 v; 383eeb23a4cSdrh if( flag_longlong ){ 384eeb23a4cSdrh v = va_arg(ap,i64); 385eeb23a4cSdrh }else if( flag_long ){ 386eeb23a4cSdrh v = va_arg(ap,long int); 387eeb23a4cSdrh }else{ 388eeb23a4cSdrh v = va_arg(ap,int); 389eeb23a4cSdrh } 390e9707671Sdrh if( v<0 ){ 391158b9cb9Sdrh if( v==SMALLEST_INT64 ){ 392158b9cb9Sdrh longvalue = ((u64)1)<<63; 393158b9cb9Sdrh }else{ 394158b9cb9Sdrh longvalue = -v; 395158b9cb9Sdrh } 396cfcdaefeSdanielk1977 prefix = '-'; 397cfcdaefeSdanielk1977 }else{ 398e9707671Sdrh longvalue = v; 399e9707671Sdrh if( flag_plussign ) prefix = '+'; 400a18c5681Sdrh else if( flag_blanksign ) prefix = ' '; 401a18c5681Sdrh else prefix = 0; 402cfcdaefeSdanielk1977 } 403e9707671Sdrh }else{ 404eeb23a4cSdrh if( flag_longlong ){ 405eeb23a4cSdrh longvalue = va_arg(ap,u64); 406eeb23a4cSdrh }else if( flag_long ){ 407eeb23a4cSdrh longvalue = va_arg(ap,unsigned long int); 408eeb23a4cSdrh }else{ 409eeb23a4cSdrh longvalue = va_arg(ap,unsigned int); 410eeb23a4cSdrh } 411e9707671Sdrh prefix = 0; 412e9707671Sdrh } 413e9707671Sdrh if( longvalue==0 ) flag_alternateform = 0; 414a18c5681Sdrh if( flag_zeropad && precision<width-(prefix!=0) ){ 415a18c5681Sdrh precision = width-(prefix!=0); 416a18c5681Sdrh } 417*59eedf79Sdrh if( precision<etBUFSIZE-10 ){ 418*59eedf79Sdrh nOut = etBUFSIZE; 419*59eedf79Sdrh zOut = buf; 420*59eedf79Sdrh }else{ 421*59eedf79Sdrh nOut = precision + 10; 422*59eedf79Sdrh zOut = zExtra = sqlite3Malloc( nOut ); 423*59eedf79Sdrh if( zOut==0 ){ 424*59eedf79Sdrh pAccum->mallocFailed = 1; 425*59eedf79Sdrh return; 426*59eedf79Sdrh } 427*59eedf79Sdrh } 428*59eedf79Sdrh bufpt = &zOut[nOut-1]; 4299a99334dSdrh if( xtype==etORDINAL ){ 43043f6e064Sdrh static const char zOrd[] = "thstndrd"; 431ea678832Sdrh int x = (int)(longvalue % 10); 43243f6e064Sdrh if( x>=4 || (longvalue/10)%10==1 ){ 43343f6e064Sdrh x = 0; 43443f6e064Sdrh } 435*59eedf79Sdrh *(--bufpt) = zOrd[x*2+1]; 436*59eedf79Sdrh *(--bufpt) = zOrd[x*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 } 448*59eedf79Sdrh length = (int)(&zOut[nOut-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 } 459*59eedf79Sdrh length = (int)(&zOut[nOut-1]-bufpt); 460a18c5681Sdrh break; 461a18c5681Sdrh case etFLOAT: 462a18c5681Sdrh case etEXP: 463a18c5681Sdrh case etGENERIC: 464a18c5681Sdrh realvalue = va_arg(ap,double); 46569ef7036Sdrh #ifdef SQLITE_OMIT_FLOATING_POINT 46669ef7036Sdrh length = 0; 46769ef7036Sdrh #else 468a18c5681Sdrh if( precision<0 ) precision = 6; /* Set default precision */ 469a18c5681Sdrh if( realvalue<0.0 ){ 470a18c5681Sdrh realvalue = -realvalue; 471a18c5681Sdrh prefix = '-'; 472a18c5681Sdrh }else{ 473a18c5681Sdrh if( flag_plussign ) prefix = '+'; 474a18c5681Sdrh else if( flag_blanksign ) prefix = ' '; 475a18c5681Sdrh else prefix = 0; 476a18c5681Sdrh } 4773e9aeec0Sdrh if( xtype==etGENERIC && precision>0 ) precision--; 4785f968436Sdrh #if 0 479a18c5681Sdrh /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */ 480a18c5681Sdrh for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1); 481a18c5681Sdrh #else 482a18c5681Sdrh /* It makes more sense to use 0.5 */ 48374161705Sdrh for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){} 484a18c5681Sdrh #endif 4853e9aeec0Sdrh if( xtype==etFLOAT ) realvalue += rounder; 486a18c5681Sdrh /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ 487a18c5681Sdrh exp = 0; 488ea678832Sdrh if( sqlite3IsNaN((double)realvalue) ){ 48953c14021Sdrh bufpt = "NaN"; 49053c14021Sdrh length = 3; 49153c14021Sdrh break; 49253c14021Sdrh } 493a18c5681Sdrh if( realvalue>0.0 ){ 49463782855Sdrh while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; } 49563782855Sdrh while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; } 49663782855Sdrh while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; } 497af005fbcSdrh while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } 498af005fbcSdrh while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } 499af005fbcSdrh if( exp>350 ){ 50053c14021Sdrh if( prefix=='-' ){ 50153c14021Sdrh bufpt = "-Inf"; 50253c14021Sdrh }else if( prefix=='+' ){ 50353c14021Sdrh bufpt = "+Inf"; 50453c14021Sdrh }else{ 50553c14021Sdrh bufpt = "Inf"; 50653c14021Sdrh } 507ea678832Sdrh length = sqlite3Strlen30(bufpt); 508a18c5681Sdrh break; 509a18c5681Sdrh } 510a18c5681Sdrh } 511a18c5681Sdrh bufpt = buf; 512a18c5681Sdrh /* 513a18c5681Sdrh ** If the field type is etGENERIC, then convert to either etEXP 514a18c5681Sdrh ** or etFLOAT, as appropriate. 515a18c5681Sdrh */ 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 } 536*59eedf79Sdrh if( e2+precision+width > etBUFSIZE - 15 ){ 537*59eedf79Sdrh bufpt = zExtra = sqlite3Malloc( e2+precision+width+15 ); 538*59eedf79Sdrh if( bufpt==0 ){ 539*59eedf79Sdrh pAccum->mallocFailed = 1; 540*59eedf79Sdrh return; 541*59eedf79Sdrh } 542*59eedf79Sdrh } 543*59eedf79Sdrh zOut = bufpt; 544a18c5681Sdrh nsd = 0; 545ea678832Sdrh flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; 546557cc60fSdrh /* The sign in front of the number */ 547557cc60fSdrh if( prefix ){ 548557cc60fSdrh *(bufpt++) = prefix; 549557cc60fSdrh } 550557cc60fSdrh /* Digits prior to the decimal point */ 551557cc60fSdrh if( e2<0 ){ 552557cc60fSdrh *(bufpt++) = '0'; 553557cc60fSdrh }else{ 554557cc60fSdrh for(; e2>=0; e2--){ 555557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 556557cc60fSdrh } 557557cc60fSdrh } 558557cc60fSdrh /* The decimal point */ 559557cc60fSdrh if( flag_dp ){ 560557cc60fSdrh *(bufpt++) = '.'; 561557cc60fSdrh } 562557cc60fSdrh /* "0" digits after the decimal point but before the first 563557cc60fSdrh ** significant digit of the number */ 564af005fbcSdrh for(e2++; e2<0; precision--, e2++){ 565af005fbcSdrh assert( precision>0 ); 566a18c5681Sdrh *(bufpt++) = '0'; 567a18c5681Sdrh } 568557cc60fSdrh /* Significant digits after the decimal point */ 569557cc60fSdrh while( (precision--)>0 ){ 570557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 571a18c5681Sdrh } 572557cc60fSdrh /* Remove trailing zeros and the "." if no digits follow the "." */ 573557cc60fSdrh if( flag_rtz && flag_dp ){ 5743e9aeec0Sdrh while( bufpt[-1]=='0' ) *(--bufpt) = 0; 575*59eedf79Sdrh assert( bufpt>zOut ); 5763e9aeec0Sdrh if( bufpt[-1]=='.' ){ 577557cc60fSdrh if( flag_altform2 ){ 578557cc60fSdrh *(bufpt++) = '0'; 579557cc60fSdrh }else{ 580557cc60fSdrh *(--bufpt) = 0; 581a18c5681Sdrh } 582557cc60fSdrh } 583557cc60fSdrh } 584557cc60fSdrh /* Add the "eNNN" suffix */ 585*59eedf79Sdrh if( xtype==etEXP ){ 58676ff3a0eSdrh *(bufpt++) = aDigits[infop->charset]; 587557cc60fSdrh if( exp<0 ){ 588557cc60fSdrh *(bufpt++) = '-'; exp = -exp; 589557cc60fSdrh }else{ 590557cc60fSdrh *(bufpt++) = '+'; 591557cc60fSdrh } 592a18c5681Sdrh if( exp>=100 ){ 593ea678832Sdrh *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */ 594a18c5681Sdrh exp %= 100; 595a18c5681Sdrh } 596ea678832Sdrh *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */ 597ea678832Sdrh *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */ 598a18c5681Sdrh } 599557cc60fSdrh *bufpt = 0; 600557cc60fSdrh 601a18c5681Sdrh /* The converted number is in buf[] and zero terminated. Output it. 602a18c5681Sdrh ** Note that the number is in the usual order, not reversed as with 603a18c5681Sdrh ** integer conversions. */ 604*59eedf79Sdrh length = (int)(bufpt-zOut); 605*59eedf79Sdrh bufpt = zOut; 606a18c5681Sdrh 607a18c5681Sdrh /* Special case: Add leading zeros if the flag_zeropad flag is 608a18c5681Sdrh ** set and we are not left justified */ 609a18c5681Sdrh if( flag_zeropad && !flag_leftjustify && length < width){ 610a18c5681Sdrh int i; 611a18c5681Sdrh int nPad = width - length; 612a18c5681Sdrh for(i=width; i>=nPad; i--){ 613a18c5681Sdrh bufpt[i] = bufpt[i-nPad]; 614a18c5681Sdrh } 615a18c5681Sdrh i = prefix!=0; 616a18c5681Sdrh while( nPad-- ) bufpt[i++] = '0'; 617a18c5681Sdrh length = width; 618a18c5681Sdrh } 61969ef7036Sdrh #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */ 620a18c5681Sdrh break; 621a18c5681Sdrh case etSIZE: 622ade86483Sdrh *(va_arg(ap,int*)) = pAccum->nChar; 623a18c5681Sdrh length = width = 0; 624a18c5681Sdrh break; 625a18c5681Sdrh case etPERCENT: 626a18c5681Sdrh buf[0] = '%'; 627a18c5681Sdrh bufpt = buf; 628a18c5681Sdrh length = 1; 629a18c5681Sdrh break; 630a18c5681Sdrh case etCHARX: 631ea678832Sdrh c = va_arg(ap,int); 632ea678832Sdrh buf[0] = (char)c; 633a18c5681Sdrh if( precision>=0 ){ 634ea678832Sdrh for(idx=1; idx<precision; idx++) buf[idx] = (char)c; 635a18c5681Sdrh length = precision; 636a18c5681Sdrh }else{ 637a18c5681Sdrh length =1; 638a18c5681Sdrh } 639a18c5681Sdrh bufpt = buf; 640a18c5681Sdrh break; 641a18c5681Sdrh case etSTRING: 642d93d8a81Sdrh case etDYNSTRING: 643cb485882Sdrh bufpt = va_arg(ap,char*); 644d93d8a81Sdrh if( bufpt==0 ){ 645d93d8a81Sdrh bufpt = ""; 646d93d8a81Sdrh }else if( xtype==etDYNSTRING ){ 647d93d8a81Sdrh zExtra = bufpt; 648d93d8a81Sdrh } 649e509094bSdrh if( precision>=0 ){ 650e509094bSdrh for(length=0; length<precision && bufpt[length]; length++){} 651e509094bSdrh }else{ 652ea678832Sdrh length = sqlite3Strlen30(bufpt); 653e509094bSdrh } 654a18c5681Sdrh break; 655a18c5681Sdrh case etSQLESCAPE: 656f3b863edSdanielk1977 case etSQLESCAPE2: 657f3b863edSdanielk1977 case etSQLESCAPE3: { 6588965b50eSdrh int i, j, k, n, isnull; 6594794f735Sdrh int needQuote; 660ea678832Sdrh char ch; 661f3b863edSdanielk1977 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */ 662f0113000Sdanielk1977 char *escarg = va_arg(ap,char*); 663f0113000Sdanielk1977 isnull = escarg==0; 664f0113000Sdanielk1977 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); 6658965b50eSdrh k = precision; 66660d4a304Sdan for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ 667f3b863edSdanielk1977 if( ch==q ) n++; 668a18c5681Sdrh } 6694794f735Sdrh needQuote = !isnull && xtype==etSQLESCAPE2; 6704794f735Sdrh n += i + 1 + needQuote*2; 671a18c5681Sdrh if( n>etBUFSIZE ){ 672e5ae5735Sdrh bufpt = zExtra = sqlite3Malloc( n ); 673d164fd34Sdrh if( bufpt==0 ){ 674d164fd34Sdrh pAccum->mallocFailed = 1; 675d164fd34Sdrh return; 676d164fd34Sdrh } 677a18c5681Sdrh }else{ 678a18c5681Sdrh bufpt = buf; 679a18c5681Sdrh } 6800cfcf3fbSchw j = 0; 681f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 6828965b50eSdrh k = i; 6838965b50eSdrh for(i=0; i<k; i++){ 6848965b50eSdrh bufpt[j++] = ch = escarg[i]; 685f3b863edSdanielk1977 if( ch==q ) bufpt[j++] = ch; 686a18c5681Sdrh } 687f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 688a18c5681Sdrh bufpt[j] = 0; 689a18c5681Sdrh length = j; 6908965b50eSdrh /* The precision in %q and %Q means how many input characters to 6918965b50eSdrh ** consume, not the length of the output... 6928965b50eSdrh ** if( precision>=0 && precision<length ) length = precision; */ 693a18c5681Sdrh break; 6945eba8c09Sdrh } 6955f968436Sdrh case etTOKEN: { 6965f968436Sdrh Token *pToken = va_arg(ap, Token*); 697af005fbcSdrh if( pToken ){ 698ade86483Sdrh sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n); 699ad6d9460Sdrh } 7005f968436Sdrh length = width = 0; 7015f968436Sdrh break; 7025f968436Sdrh } 7035f968436Sdrh case etSRCLIST: { 7045f968436Sdrh SrcList *pSrc = va_arg(ap, SrcList*); 7055f968436Sdrh int k = va_arg(ap, int); 7065f968436Sdrh struct SrcList_item *pItem = &pSrc->a[k]; 7075f968436Sdrh assert( k>=0 && k<pSrc->nSrc ); 70893a960a0Sdrh if( pItem->zDatabase ){ 709ade86483Sdrh sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1); 710ade86483Sdrh sqlite3StrAccumAppend(pAccum, ".", 1); 7115f968436Sdrh } 712ade86483Sdrh sqlite3StrAccumAppend(pAccum, pItem->zName, -1); 7135f968436Sdrh length = width = 0; 7145f968436Sdrh break; 7155f968436Sdrh } 716874ba04cSdrh default: { 717874ba04cSdrh assert( xtype==etINVALID ); 718874ba04cSdrh return; 719874ba04cSdrh } 720a18c5681Sdrh }/* End switch over the format type */ 721a18c5681Sdrh /* 722a18c5681Sdrh ** The text of the conversion is pointed to by "bufpt" and is 723a18c5681Sdrh ** "length" characters long. The field width is "width". Do 724a18c5681Sdrh ** the output. 725a18c5681Sdrh */ 726a18c5681Sdrh if( !flag_leftjustify ){ 727a18c5681Sdrh register int nspace; 728a18c5681Sdrh nspace = width-length; 729a18c5681Sdrh if( nspace>0 ){ 730ade86483Sdrh appendSpace(pAccum, nspace); 731a18c5681Sdrh } 732a18c5681Sdrh } 733a18c5681Sdrh if( length>0 ){ 734ade86483Sdrh sqlite3StrAccumAppend(pAccum, bufpt, length); 735a18c5681Sdrh } 736a18c5681Sdrh if( flag_leftjustify ){ 737a18c5681Sdrh register int nspace; 738a18c5681Sdrh nspace = width-length; 739a18c5681Sdrh if( nspace>0 ){ 740ade86483Sdrh appendSpace(pAccum, nspace); 741a18c5681Sdrh } 742a18c5681Sdrh } 7431e536953Sdanielk1977 sqlite3_free(zExtra); 744a18c5681Sdrh }/* End for loop over the format string */ 745a18c5681Sdrh } /* End of function */ 746a18c5681Sdrh 747a18c5681Sdrh /* 748ade86483Sdrh ** Append N bytes of text from z to the StrAccum object. 749a18c5681Sdrh */ 750ade86483Sdrh void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){ 751bc6160b0Sdrh assert( z!=0 || N==0 ); 752ade86483Sdrh if( p->tooBig | p->mallocFailed ){ 753bc6160b0Sdrh testcase(p->tooBig); 754bc6160b0Sdrh testcase(p->mallocFailed); 75517435752Sdrh return; 756ade86483Sdrh } 757ade86483Sdrh if( N<0 ){ 758ea678832Sdrh N = sqlite3Strlen30(z); 759ade86483Sdrh } 760bc6160b0Sdrh if( N==0 || NEVER(z==0) ){ 761ade86483Sdrh return; 762ade86483Sdrh } 763ade86483Sdrh if( p->nChar+N >= p->nAlloc ){ 764ade86483Sdrh char *zNew; 765ade86483Sdrh if( !p->useMalloc ){ 766ade86483Sdrh p->tooBig = 1; 767ade86483Sdrh N = p->nAlloc - p->nChar - 1; 768ade86483Sdrh if( N<=0 ){ 769ade86483Sdrh return; 7705f968436Sdrh } 771a18c5681Sdrh }else{ 772a9ef7097Sdan char *zOld = (p->zText==p->zBase ? 0 : p->zText); 77393a960a0Sdrh i64 szNew = p->nChar; 774b1a6c3c1Sdrh szNew += N + 1; 775b1a6c3c1Sdrh if( szNew > p->mxAlloc ){ 776ade86483Sdrh sqlite3StrAccumReset(p); 777ade86483Sdrh p->tooBig = 1; 778eaad32b1Sdrh return; 779b1a6c3c1Sdrh }else{ 780ea678832Sdrh p->nAlloc = (int)szNew; 781a18c5681Sdrh } 782b975598eSdrh if( p->useMalloc==1 ){ 783a9ef7097Sdan zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); 784b975598eSdrh }else{ 785a9ef7097Sdan zNew = sqlite3_realloc(zOld, p->nAlloc); 786b975598eSdrh } 78753f733c7Sdrh if( zNew ){ 788a9ef7097Sdan if( zOld==0 ) memcpy(zNew, p->zText, p->nChar); 789ade86483Sdrh p->zText = zNew; 790ade86483Sdrh }else{ 791ade86483Sdrh p->mallocFailed = 1; 792ade86483Sdrh sqlite3StrAccumReset(p); 793ade86483Sdrh return; 79453f733c7Sdrh } 7955f968436Sdrh } 7965f968436Sdrh } 797ade86483Sdrh memcpy(&p->zText[p->nChar], z, N); 798ade86483Sdrh p->nChar += N; 799483750baSdrh } 800483750baSdrh 801483750baSdrh /* 802ade86483Sdrh ** Finish off a string by making sure it is zero-terminated. 803ade86483Sdrh ** Return a pointer to the resulting string. Return a NULL 804ade86483Sdrh ** pointer if any kind of error was encountered. 8055f968436Sdrh */ 806ade86483Sdrh char *sqlite3StrAccumFinish(StrAccum *p){ 807ade86483Sdrh if( p->zText ){ 808ade86483Sdrh p->zText[p->nChar] = 0; 809ade86483Sdrh if( p->useMalloc && p->zText==p->zBase ){ 810b975598eSdrh if( p->useMalloc==1 ){ 811633e6d57Sdrh p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); 812b975598eSdrh }else{ 813b975598eSdrh p->zText = sqlite3_malloc(p->nChar+1); 814b975598eSdrh } 815ade86483Sdrh if( p->zText ){ 816ade86483Sdrh memcpy(p->zText, p->zBase, p->nChar+1); 817ade86483Sdrh }else{ 818ade86483Sdrh p->mallocFailed = 1; 819ade86483Sdrh } 820ade86483Sdrh } 821ade86483Sdrh } 822ade86483Sdrh return p->zText; 823ade86483Sdrh } 824ade86483Sdrh 825ade86483Sdrh /* 826ade86483Sdrh ** Reset an StrAccum string. Reclaim all malloced memory. 827ade86483Sdrh */ 828ade86483Sdrh void sqlite3StrAccumReset(StrAccum *p){ 829ade86483Sdrh if( p->zText!=p->zBase ){ 830b975598eSdrh if( p->useMalloc==1 ){ 831633e6d57Sdrh sqlite3DbFree(p->db, p->zText); 832b975598eSdrh }else{ 833b975598eSdrh sqlite3_free(p->zText); 834b975598eSdrh } 835ade86483Sdrh } 836f089aa45Sdrh p->zText = 0; 837ade86483Sdrh } 838ade86483Sdrh 839ade86483Sdrh /* 840ade86483Sdrh ** Initialize a string accumulator 841ade86483Sdrh */ 842f089aa45Sdrh void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){ 843ade86483Sdrh p->zText = p->zBase = zBase; 844633e6d57Sdrh p->db = 0; 845ade86483Sdrh p->nChar = 0; 846ade86483Sdrh p->nAlloc = n; 847bb4957f8Sdrh p->mxAlloc = mx; 848ade86483Sdrh p->useMalloc = 1; 849ade86483Sdrh p->tooBig = 0; 850ade86483Sdrh p->mallocFailed = 0; 8515f968436Sdrh } 8525f968436Sdrh 8535f968436Sdrh /* 8545f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 8555f968436Sdrh ** %-conversion extensions. 8565f968436Sdrh */ 85717435752Sdrh char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ 85817435752Sdrh char *z; 85979158e18Sdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 860ade86483Sdrh StrAccum acc; 861bc6160b0Sdrh assert( db!=0 ); 862bb4957f8Sdrh sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), 863bc6160b0Sdrh db->aLimit[SQLITE_LIMIT_LENGTH]); 864633e6d57Sdrh acc.db = db; 865f089aa45Sdrh sqlite3VXPrintf(&acc, 1, zFormat, ap); 866ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 867bc6160b0Sdrh if( acc.mallocFailed ){ 86817435752Sdrh db->mallocFailed = 1; 86917435752Sdrh } 87017435752Sdrh return z; 8715f968436Sdrh } 8725f968436Sdrh 8735f968436Sdrh /* 8745f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 8755f968436Sdrh ** %-conversion extensions. 8765f968436Sdrh */ 87717435752Sdrh char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ 8785f968436Sdrh va_list ap; 8795f968436Sdrh char *z; 8805f968436Sdrh va_start(ap, zFormat); 881ade86483Sdrh z = sqlite3VMPrintf(db, zFormat, ap); 8825f968436Sdrh va_end(ap); 8835f968436Sdrh return z; 8845f968436Sdrh } 8855f968436Sdrh 8865f968436Sdrh /* 887633e6d57Sdrh ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting 888633e6d57Sdrh ** the string and before returnning. This routine is intended to be used 889633e6d57Sdrh ** to modify an existing string. For example: 890633e6d57Sdrh ** 891633e6d57Sdrh ** x = sqlite3MPrintf(db, x, "prefix %s suffix", x); 892633e6d57Sdrh ** 893633e6d57Sdrh */ 894633e6d57Sdrh char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){ 895633e6d57Sdrh va_list ap; 896633e6d57Sdrh char *z; 897633e6d57Sdrh va_start(ap, zFormat); 898633e6d57Sdrh z = sqlite3VMPrintf(db, zFormat, ap); 899633e6d57Sdrh va_end(ap); 900633e6d57Sdrh sqlite3DbFree(db, zStr); 901633e6d57Sdrh return z; 902633e6d57Sdrh } 903633e6d57Sdrh 904633e6d57Sdrh /* 90528dd479cSdrh ** Print into memory obtained from sqlite3_malloc(). Omit the internal 90628dd479cSdrh ** %-conversion extensions. 90728dd479cSdrh */ 90828dd479cSdrh char *sqlite3_vmprintf(const char *zFormat, va_list ap){ 909ade86483Sdrh char *z; 91028dd479cSdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 911ade86483Sdrh StrAccum acc; 912ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT 913ff1590eeSdrh if( sqlite3_initialize() ) return 0; 914ff1590eeSdrh #endif 915bb4957f8Sdrh sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); 916b975598eSdrh acc.useMalloc = 2; 917f089aa45Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 918ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 919ade86483Sdrh return z; 92028dd479cSdrh } 92128dd479cSdrh 92228dd479cSdrh /* 92328dd479cSdrh ** Print into memory obtained from sqlite3_malloc()(). Omit the internal 92428dd479cSdrh ** %-conversion extensions. 925483750baSdrh */ 9266f8a503dSdanielk1977 char *sqlite3_mprintf(const char *zFormat, ...){ 927a18c5681Sdrh va_list ap; 9285f968436Sdrh char *z; 929ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT 930ff1590eeSdrh if( sqlite3_initialize() ) return 0; 931ff1590eeSdrh #endif 932a18c5681Sdrh va_start(ap, zFormat); 933b3738b6cSdrh z = sqlite3_vmprintf(zFormat, ap); 934a18c5681Sdrh va_end(ap); 9355f968436Sdrh return z; 936a18c5681Sdrh } 937a18c5681Sdrh 938a18c5681Sdrh /* 9396f8a503dSdanielk1977 ** sqlite3_snprintf() works like snprintf() except that it ignores the 94093a5c6bdSdrh ** current locale settings. This is important for SQLite because we 94193a5c6bdSdrh ** are not able to use a "," as the decimal point in place of "." as 94293a5c6bdSdrh ** specified by some locales. 943db26d4c9Sdrh ** 944db26d4c9Sdrh ** Oops: The first two arguments of sqlite3_snprintf() are backwards 945db26d4c9Sdrh ** from the snprintf() standard. Unfortunately, it is too late to change 946db26d4c9Sdrh ** this without breaking compatibility, so we just have to live with the 947db26d4c9Sdrh ** mistake. 948db26d4c9Sdrh ** 949db26d4c9Sdrh ** sqlite3_vsnprintf() is the varargs version. 95093a5c6bdSdrh */ 951db26d4c9Sdrh char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ 952db26d4c9Sdrh StrAccum acc; 953db26d4c9Sdrh if( n<=0 ) return zBuf; 954db26d4c9Sdrh sqlite3StrAccumInit(&acc, zBuf, n, 0); 955db26d4c9Sdrh acc.useMalloc = 0; 956db26d4c9Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 957db26d4c9Sdrh return sqlite3StrAccumFinish(&acc); 958db26d4c9Sdrh } 9596f8a503dSdanielk1977 char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ 9605f968436Sdrh char *z; 96193a5c6bdSdrh va_list ap; 96293a5c6bdSdrh va_start(ap,zFormat); 963db26d4c9Sdrh z = sqlite3_vsnprintf(n, zBuf, zFormat, ap); 96493a5c6bdSdrh va_end(ap); 9655f968436Sdrh return z; 96693a5c6bdSdrh } 96793a5c6bdSdrh 9683f280701Sdrh /* 9697c0c460fSdrh ** This is the routine that actually formats the sqlite3_log() message. 9707c0c460fSdrh ** We house it in a separate routine from sqlite3_log() to avoid using 9717c0c460fSdrh ** stack space on small-stack systems when logging is disabled. 9727c0c460fSdrh ** 9737c0c460fSdrh ** sqlite3_log() must render into a static buffer. It cannot dynamically 9747c0c460fSdrh ** allocate memory because it might be called while the memory allocator 9757c0c460fSdrh ** mutex is held. 9767c0c460fSdrh */ 9777c0c460fSdrh static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){ 9787c0c460fSdrh StrAccum acc; /* String accumulator */ 979a64fa912Sdrh char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */ 9807c0c460fSdrh 9817c0c460fSdrh sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0); 9827c0c460fSdrh acc.useMalloc = 0; 9837c0c460fSdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 9847c0c460fSdrh sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode, 9857c0c460fSdrh sqlite3StrAccumFinish(&acc)); 9867c0c460fSdrh } 9877c0c460fSdrh 9887c0c460fSdrh /* 9893f280701Sdrh ** Format and write a message to the log if logging is enabled. 9903f280701Sdrh */ 991a7564663Sdrh void sqlite3_log(int iErrCode, const char *zFormat, ...){ 9923f280701Sdrh va_list ap; /* Vararg list */ 9937c0c460fSdrh if( sqlite3GlobalConfig.xLog ){ 9943f280701Sdrh va_start(ap, zFormat); 9957c0c460fSdrh renderLogMsg(iErrCode, zFormat, ap); 9963f280701Sdrh va_end(ap); 9973f280701Sdrh } 9983f280701Sdrh } 9993f280701Sdrh 100085e9e22bSdrh #if defined(SQLITE_DEBUG) 1001e54ca3feSdrh /* 1002e54ca3feSdrh ** A version of printf() that understands %lld. Used for debugging. 1003e54ca3feSdrh ** The printf() built into some versions of windows does not understand %lld 1004e54ca3feSdrh ** and segfaults if you give it a long long int. 1005e54ca3feSdrh */ 1006e54ca3feSdrh void sqlite3DebugPrintf(const char *zFormat, ...){ 1007e54ca3feSdrh va_list ap; 1008ade86483Sdrh StrAccum acc; 1009e54ca3feSdrh char zBuf[500]; 1010bb4957f8Sdrh sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0); 1011ade86483Sdrh acc.useMalloc = 0; 1012e54ca3feSdrh va_start(ap,zFormat); 1013f089aa45Sdrh sqlite3VXPrintf(&acc, 0, zFormat, ap); 1014e54ca3feSdrh va_end(ap); 1015bed8e7e5Sdrh sqlite3StrAccumFinish(&acc); 1016485f0039Sdrh fprintf(stdout,"%s", zBuf); 10172ac3ee97Sdrh fflush(stdout); 1018e54ca3feSdrh } 1019e54ca3feSdrh #endif 1020c7bc4fdeSdrh 1021c7bc4fdeSdrh #ifndef SQLITE_OMIT_TRACE 1022c7bc4fdeSdrh /* 1023c7bc4fdeSdrh ** variable-argument wrapper around sqlite3VXPrintf(). 1024c7bc4fdeSdrh */ 1025c7bc4fdeSdrh void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){ 1026c7bc4fdeSdrh va_list ap; 1027c7bc4fdeSdrh va_start(ap,zFormat); 1028c7bc4fdeSdrh sqlite3VXPrintf(p, 1, zFormat, ap); 1029c7bc4fdeSdrh va_end(ap); 1030c7bc4fdeSdrh } 1031c7bc4fdeSdrh #endif 1032