ds@1952: /* ds@1952: ** The "printf" code that follows dates from the 1980's. It is in ds@1952: ** the public domain. The original comments are included here for ds@1952: ** completeness. They are very out-of-date but might be useful as ds@1952: ** an historical reference. Most of the "enhancements" have been backed ds@1952: ** out so that the functionality is now the same as standard printf(). ds@1952: ** ds@1952: ************************************************************************** ds@1952: ** ds@1952: ** The following modules is an enhanced replacement for the "printf" subroutines ds@1952: ** found in the standard C library. The following enhancements are ds@1952: ** supported: ds@1952: ** ds@1952: ** + Additional functions. The standard set of "printf" functions ds@1952: ** includes printf, fprintf, sprintf, vprintf, vfprintf, and ds@1952: ** vsprintf. This module adds the following: ds@1952: ** ds@1952: ** * snprintf -- Works like sprintf, but has an extra argument ds@1952: ** which is the size of the buffer written to. ds@1952: ** ds@1952: ** * mprintf -- Similar to sprintf. Writes output to memory ds@1952: ** obtained from malloc. ds@1952: ** ds@1952: ** * xprintf -- Calls a function to dispose of output. ds@1952: ** ds@1952: ** * nprintf -- No output, but returns the number of characters ds@1952: ** that would have been output by printf. ds@1952: ** ds@1952: ** * A v- version (ex: vsnprintf) of every function is also ds@1952: ** supplied. ds@1952: ** ds@1952: ** + A few extensions to the formatting notation are supported: ds@1952: ** ds@1952: ** * The "=" flag (similar to "-") causes the output to be ds@1952: ** be centered in the appropriately sized field. ds@1952: ** ds@1952: ** * The %b field outputs an integer in binary notation. ds@1952: ** ds@1952: ** * The %c field now accepts a precision. The character output ds@1952: ** is repeated by the number of times the precision specifies. ds@1952: ** ds@1952: ** * The %' field works like %c, but takes as its character the ds@1952: ** next character of the format string, instead of the next ds@1952: ** argument. For example, printf("%.78'-") prints 78 minus ds@1952: ** signs, the same as printf("%.78c",'-'). ds@1952: ** ds@1952: ** + When compiled using GCC on a SPARC, this version of printf is ds@1952: ** faster than the library printf for SUN OS 4.1. ds@1952: ** ds@1952: ** + All functions are fully reentrant. ds@1952: ** ds@1952: */ ds@1952: #include "sqliteInt.h" ds@1952: #include ds@1952: ds@1952: /* ds@1952: ** Conversion types fall into various categories as defined by the ds@1952: ** following enumeration. ds@1952: */ ds@1952: #define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */ ds@1952: #define etFLOAT 2 /* Floating point. %f */ ds@1952: #define etEXP 3 /* Exponentional notation. %e and %E */ ds@1952: #define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */ ds@1952: #define etSIZE 5 /* Return number of characters processed so far. %n */ ds@1952: #define etSTRING 6 /* Strings. %s */ ds@1952: #define etDYNSTRING 7 /* Dynamically allocated strings. %z */ ds@1952: #define etPERCENT 8 /* Percent symbol. %% */ ds@1952: #define etCHARX 9 /* Characters. %c */ ds@1952: /* The rest are extensions, not normally found in printf() */ ds@1952: #define etCHARLIT 10 /* Literal characters. %' */ ds@1952: #define etSQLESCAPE 11 /* Strings with '\'' doubled. %q */ ds@1952: #define etSQLESCAPE2 12 /* Strings with '\'' doubled and enclosed in '', ds@1952: NULL pointers replaced by SQL NULL. %Q */ ds@1952: #define etTOKEN 13 /* a pointer to a Token structure */ ds@1952: #define etSRCLIST 14 /* a pointer to a SrcList */ ds@1952: #define etPOINTER 15 /* The %p conversion */ ds@1952: #define etSQLESCAPE3 16 /* %w -> Strings with '\"' doubled */ ds@1952: ds@1952: ds@1952: /* ds@1952: ** An "etByte" is an 8-bit unsigned value. ds@1952: */ ds@1952: typedef unsigned char etByte; ds@1952: ds@1952: /* ds@1952: ** Each builtin conversion character (ex: the 'd' in "%d") is described ds@1952: ** by an instance of the following structure ds@1952: */ ds@1952: typedef struct et_info { /* Information about each format field */ ds@1952: char fmttype; /* The format field code letter */ ds@1952: etByte base; /* The base for radix conversion */ ds@1952: etByte flags; /* One or more of FLAG_ constants below */ ds@1952: etByte type; /* Conversion paradigm */ ds@1952: etByte charset; /* Offset into aDigits[] of the digits string */ ds@1952: etByte prefix; /* Offset into aPrefix[] of the prefix string */ ds@1952: } et_info; ds@1952: ds@1952: /* ds@1952: ** Allowed values for et_info.flags ds@1952: */ ds@1952: #define FLAG_SIGNED 1 /* True if the value to convert is signed */ ds@1952: #define FLAG_INTERN 2 /* True if for internal use only */ ds@1952: #define FLAG_STRING 4 /* Allow infinity precision */ ds@1952: ds@1952: ds@1952: /* ds@1952: ** The following table is searched linearly, so it is good to put the ds@1952: ** most frequently used conversion types first. ds@1952: */ ds@1952: static const char aDigits[] = "0123456789ABCDEF0123456789abcdef"; ds@1952: static const char aPrefix[] = "-x0\000X0"; ds@1952: static const et_info fmtinfo[] = { ds@1952: { 'd', 10, 1, etRADIX, 0, 0 }, ds@1952: { 's', 0, 4, etSTRING, 0, 0 }, ds@1952: { 'g', 0, 1, etGENERIC, 30, 0 }, ds@1952: { 'z', 0, 4, etDYNSTRING, 0, 0 }, ds@1952: { 'q', 0, 4, etSQLESCAPE, 0, 0 }, ds@1952: { 'Q', 0, 4, etSQLESCAPE2, 0, 0 }, ds@1952: { 'w', 0, 4, etSQLESCAPE3, 0, 0 }, ds@1952: { 'c', 0, 0, etCHARX, 0, 0 }, ds@1952: { 'o', 8, 0, etRADIX, 0, 2 }, ds@1952: { 'u', 10, 0, etRADIX, 0, 0 }, ds@1952: { 'x', 16, 0, etRADIX, 16, 1 }, ds@1952: { 'X', 16, 0, etRADIX, 0, 4 }, ds@1952: #ifndef SQLITE_OMIT_FLOATING_POINT ds@1952: { 'f', 0, 1, etFLOAT, 0, 0 }, ds@1952: { 'e', 0, 1, etEXP, 30, 0 }, ds@1952: { 'E', 0, 1, etEXP, 14, 0 }, ds@1952: { 'G', 0, 1, etGENERIC, 14, 0 }, ds@1952: #endif ds@1952: { 'i', 10, 1, etRADIX, 0, 0 }, ds@1952: { 'n', 0, 0, etSIZE, 0, 0 }, ds@1952: { '%', 0, 0, etPERCENT, 0, 0 }, ds@1952: { 'p', 16, 0, etPOINTER, 0, 1 }, ds@1952: { 'T', 0, 2, etTOKEN, 0, 0 }, ds@1952: { 'S', 0, 2, etSRCLIST, 0, 0 }, ds@1952: }; ds@1952: #define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0])) ds@1952: ds@1952: /* ds@1952: ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point ds@1952: ** conversions will work. ds@1952: */ ds@1952: #ifndef SQLITE_OMIT_FLOATING_POINT ds@1952: /* ds@1952: ** "*val" is a double such that 0.1 <= *val < 10.0 ds@1952: ** Return the ascii code for the leading digit of *val, then ds@1952: ** multiply "*val" by 10.0 to renormalize. ds@1952: ** ds@1952: ** Example: ds@1952: ** input: *val = 3.14159 ds@1952: ** output: *val = 1.4159 function return = '3' ds@1952: ** ds@1952: ** The counter *cnt is incremented each time. After counter exceeds ds@1952: ** 16 (the number of significant digits in a 64-bit float) '0' is ds@1952: ** always returned. ds@1952: */ ds@1952: static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ ds@1952: int digit; ds@1952: LONGDOUBLE_TYPE d; ds@1952: if( (*cnt)++ >= 16 ) return '0'; ds@1952: digit = (int)*val; ds@1952: d = digit; ds@1952: digit += '0'; ds@1952: *val = (*val - d)*10.0; ds@1952: return digit; ds@1952: } ds@1952: #endif /* SQLITE_OMIT_FLOATING_POINT */ ds@1952: ds@1952: /* ds@1952: ** On machines with a small stack size, you can redefine the ds@1952: ** SQLITE_PRINT_BUF_SIZE to be less than 350. But beware - for ds@1952: ** smaller values some %f conversions may go into an infinite loop. ds@1952: */ ds@1952: #ifndef SQLITE_PRINT_BUF_SIZE ds@1952: # define SQLITE_PRINT_BUF_SIZE 350 ds@1952: #endif ds@1952: #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ ds@1952: ds@1952: /* ds@1952: ** The root program. All variations call this core. ds@1952: ** ds@1952: ** INPUTS: ds@1952: ** func This is a pointer to a function taking three arguments ds@1952: ** 1. A pointer to anything. Same as the "arg" parameter. ds@1952: ** 2. A pointer to the list of characters to be output ds@1952: ** (Note, this list is NOT null terminated.) ds@1952: ** 3. An integer number of characters to be output. ds@1952: ** (Note: This number might be zero.) ds@1952: ** ds@1952: ** arg This is the pointer to anything which will be passed as the ds@1952: ** first argument to "func". Use it for whatever you like. ds@1952: ** ds@1952: ** fmt This is the format string, as in the usual print. ds@1952: ** ds@1952: ** ap This is a pointer to a list of arguments. Same as in ds@1952: ** vfprint. ds@1952: ** ds@1952: ** OUTPUTS: ds@1952: ** The return value is the total number of characters sent to ds@1952: ** the function "func". Returns -1 on a error. ds@1952: ** ds@1952: ** Note that the order in which automatic variables are declared below ds@1952: ** seems to make a big difference in determining how fast this beast ds@1952: ** will run. ds@1952: */ ds@1952: static int vxprintf( ds@1952: void (*func)(void*,const char*,int), /* Consumer of text */ ds@1952: void *arg, /* First argument to the consumer */ ds@1952: int useExtended, /* Allow extended %-conversions */ ds@1952: const char *fmt, /* Format string */ ds@1952: va_list ap /* arguments */ ds@1952: ){ ds@1952: int c; /* Next character in the format string */ ds@1952: char *bufpt; /* Pointer to the conversion buffer */ ds@1952: int precision; /* Precision of the current field */ ds@1952: int length; /* Length of the field */ ds@1952: int idx; /* A general purpose loop counter */ ds@1952: int count; /* Total number of characters output */ ds@1952: int width; /* Width of the current field */ ds@1952: etByte flag_leftjustify; /* True if "-" flag is present */ ds@1952: etByte flag_plussign; /* True if "+" flag is present */ ds@1952: etByte flag_blanksign; /* True if " " flag is present */ ds@1952: etByte flag_alternateform; /* True if "#" flag is present */ ds@1952: etByte flag_altform2; /* True if "!" flag is present */ ds@1952: etByte flag_zeropad; /* True if field width constant starts with zero */ ds@1952: etByte flag_long; /* True if "l" flag is present */ ds@1952: etByte flag_longlong; /* True if the "ll" flag is present */ ds@1952: etByte done; /* Loop termination flag */ ds@1952: sqlite_uint64 longvalue; /* Value for integer types */ ds@1952: LONGDOUBLE_TYPE realvalue; /* Value for real types */ ds@1952: const et_info *infop; /* Pointer to the appropriate info structure */ ds@1952: char buf[etBUFSIZE]; /* Conversion buffer */ ds@1952: char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ ds@1952: etByte errorflag = 0; /* True if an error is encountered */ ds@1952: etByte xtype; /* Conversion paradigm */ ds@1952: char *zExtra; /* Extra memory used for etTCLESCAPE conversions */ ds@1952: static const char spaces[] = ds@1952: " "; ds@1952: #define etSPACESIZE (sizeof(spaces)-1) ds@1952: #ifndef SQLITE_OMIT_FLOATING_POINT ds@1952: int exp, e2; /* exponent of real numbers */ ds@1952: double rounder; /* Used for rounding floating point values */ ds@1952: etByte flag_dp; /* True if decimal point should be shown */ ds@1952: etByte flag_rtz; /* True if trailing zeros should be removed */ ds@1952: etByte flag_exp; /* True to force display of the exponent */ ds@1952: int nsd; /* Number of significant digits returned */ ds@1952: #endif ds@1952: ds@1952: func(arg,"",0); ds@1952: count = length = 0; ds@1952: bufpt = 0; ds@1952: for(; (c=(*fmt))!=0; ++fmt){ ds@1952: if( c!='%' ){ ds@1952: int amt; ds@1952: bufpt = (char *)fmt; ds@1952: amt = 1; ds@1952: while( (c=(*++fmt))!='%' && c!=0 ) amt++; ds@1952: (*func)(arg,bufpt,amt); ds@1952: count += amt; ds@1952: if( c==0 ) break; ds@1952: } ds@1952: if( (c=(*++fmt))==0 ){ ds@1952: errorflag = 1; ds@1952: (*func)(arg,"%",1); ds@1952: count++; ds@1952: break; ds@1952: } ds@1952: /* Find out what flags are present */ ds@1952: flag_leftjustify = flag_plussign = flag_blanksign = ds@1952: flag_alternateform = flag_altform2 = flag_zeropad = 0; ds@1952: done = 0; ds@1952: do{ ds@1952: switch( c ){ ds@1952: case '-': flag_leftjustify = 1; break; ds@1952: case '+': flag_plussign = 1; break; ds@1952: case ' ': flag_blanksign = 1; break; ds@1952: case '#': flag_alternateform = 1; break; ds@1952: case '!': flag_altform2 = 1; break; ds@1952: case '0': flag_zeropad = 1; break; ds@1952: default: done = 1; break; ds@1952: } ds@1952: }while( !done && (c=(*++fmt))!=0 ); ds@1952: /* Get the field width */ ds@1952: width = 0; ds@1952: if( c=='*' ){ ds@1952: width = va_arg(ap,int); ds@1952: if( width<0 ){ ds@1952: flag_leftjustify = 1; ds@1952: width = -width; ds@1952: } ds@1952: c = *++fmt; ds@1952: }else{ ds@1952: while( c>='0' && c<='9' ){ ds@1952: width = width*10 + c - '0'; ds@1952: c = *++fmt; ds@1952: } ds@1952: } ds@1952: if( width > etBUFSIZE-10 ){ ds@1952: width = etBUFSIZE-10; ds@1952: } ds@1952: /* Get the precision */ ds@1952: if( c=='.' ){ ds@1952: precision = 0; ds@1952: c = *++fmt; ds@1952: if( c=='*' ){ ds@1952: precision = va_arg(ap,int); ds@1952: if( precision<0 ) precision = -precision; ds@1952: c = *++fmt; ds@1952: }else{ ds@1952: while( c>='0' && c<='9' ){ ds@1952: precision = precision*10 + c - '0'; ds@1952: c = *++fmt; ds@1952: } ds@1952: } ds@1952: }else{ ds@1952: precision = -1; ds@1952: } ds@1952: /* Get the conversion type modifier */ ds@1952: if( c=='l' ){ ds@1952: flag_long = 1; ds@1952: c = *++fmt; ds@1952: if( c=='l' ){ ds@1952: flag_longlong = 1; ds@1952: c = *++fmt; ds@1952: }else{ ds@1952: flag_longlong = 0; ds@1952: } ds@1952: }else{ ds@1952: flag_long = flag_longlong = 0; ds@1952: } ds@1952: /* Fetch the info entry for the field */ ds@1952: infop = 0; ds@1952: for(idx=0; idxflags & FLAG_INTERN)==0 ){ ds@1952: xtype = infop->type; ds@1952: }else{ ds@1952: return -1; ds@1952: } ds@1952: break; ds@1952: } ds@1952: } ds@1952: zExtra = 0; ds@1952: if( infop==0 ){ ds@1952: return -1; ds@1952: } ds@1952: ds@1952: ds@1952: /* Limit the precision to prevent overflowing buf[] during conversion */ ds@1952: if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){ ds@1952: precision = etBUFSIZE-40; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** At this point, variables are initialized as follows: ds@1952: ** ds@1952: ** flag_alternateform TRUE if a '#' is present. ds@1952: ** flag_altform2 TRUE if a '!' is present. ds@1952: ** flag_plussign TRUE if a '+' is present. ds@1952: ** flag_leftjustify TRUE if a '-' is present or if the ds@1952: ** field width was negative. ds@1952: ** flag_zeropad TRUE if the width began with 0. ds@1952: ** flag_long TRUE if the letter 'l' (ell) prefixed ds@1952: ** the conversion character. ds@1952: ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed ds@1952: ** the conversion character. ds@1952: ** flag_blanksign TRUE if a ' ' is present. ds@1952: ** width The specified field width. This is ds@1952: ** always non-negative. Zero is the default. ds@1952: ** precision The specified precision. The default ds@1952: ** is -1. ds@1952: ** xtype The class of the conversion. ds@1952: ** infop Pointer to the appropriate info struct. ds@1952: */ ds@1952: switch( xtype ){ ds@1952: case etPOINTER: ds@1952: flag_longlong = sizeof(char*)==sizeof(i64); ds@1952: flag_long = sizeof(char*)==sizeof(long int); ds@1952: /* Fall through into the next case */ ds@1952: case etRADIX: ds@1952: if( infop->flags & FLAG_SIGNED ){ ds@1952: i64 v; ds@1952: if( flag_longlong ) v = va_arg(ap,i64); ds@1952: else if( flag_long ) v = va_arg(ap,long int); ds@1952: else v = va_arg(ap,int); ds@1952: if( v<0 ){ ds@1952: longvalue = -v; ds@1952: prefix = '-'; ds@1952: }else{ ds@1952: longvalue = v; ds@1952: if( flag_plussign ) prefix = '+'; ds@1952: else if( flag_blanksign ) prefix = ' '; ds@1952: else prefix = 0; ds@1952: } ds@1952: }else{ ds@1952: if( flag_longlong ) longvalue = va_arg(ap,u64); ds@1952: else if( flag_long ) longvalue = va_arg(ap,unsigned long int); ds@1952: else longvalue = va_arg(ap,unsigned int); ds@1952: prefix = 0; ds@1952: } ds@1952: if( longvalue==0 ) flag_alternateform = 0; ds@1952: if( flag_zeropad && precisioncharset]; ds@1952: base = infop->base; ds@1952: do{ /* Convert to ascii */ ds@1952: *(--bufpt) = cset[longvalue%base]; ds@1952: longvalue = longvalue/base; ds@1952: }while( longvalue>0 ); ds@1952: } ds@1952: length = &buf[etBUFSIZE-1]-bufpt; ds@1952: for(idx=precision-length; idx>0; idx--){ ds@1952: *(--bufpt) = '0'; /* Zero pad */ ds@1952: } ds@1952: if( prefix ) *(--bufpt) = prefix; /* Add sign */ ds@1952: if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ ds@1952: const char *pre; ds@1952: char x; ds@1952: pre = &aPrefix[infop->prefix]; ds@1952: if( *bufpt!=pre[0] ){ ds@1952: for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; ds@1952: } ds@1952: } ds@1952: length = &buf[etBUFSIZE-1]-bufpt; ds@1952: break; ds@1952: case etFLOAT: ds@1952: case etEXP: ds@1952: case etGENERIC: ds@1952: realvalue = va_arg(ap,double); ds@1952: #ifndef SQLITE_OMIT_FLOATING_POINT ds@1952: if( precision<0 ) precision = 6; /* Set default precision */ ds@1952: if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10; ds@1952: if( realvalue<0.0 ){ ds@1952: realvalue = -realvalue; ds@1952: prefix = '-'; ds@1952: }else{ ds@1952: if( flag_plussign ) prefix = '+'; ds@1952: else if( flag_blanksign ) prefix = ' '; ds@1952: else prefix = 0; ds@1952: } ds@1952: if( xtype==etGENERIC && precision>0 ) precision--; ds@1952: #if 0 ds@1952: /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */ ds@1952: for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1); ds@1952: #else ds@1952: /* It makes more sense to use 0.5 */ ds@1952: for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){} ds@1952: #endif ds@1952: if( xtype==etFLOAT ) realvalue += rounder; ds@1952: /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ ds@1952: exp = 0; ds@1952: if( sqlite3_isnan(realvalue) ){ ds@1952: bufpt = "NaN"; ds@1952: length = 3; ds@1952: break; ds@1952: } ds@1952: if( realvalue>0.0 ){ ds@1952: while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; } ds@1952: while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; } ds@1952: while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; } ds@1952: while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; } ds@1952: while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; } ds@1952: if( exp>350 || exp<-350 ){ ds@1952: if( prefix=='-' ){ ds@1952: bufpt = "-Inf"; ds@1952: }else if( prefix=='+' ){ ds@1952: bufpt = "+Inf"; ds@1952: }else{ ds@1952: bufpt = "Inf"; ds@1952: } ds@1952: length = strlen(bufpt); ds@1952: break; ds@1952: } ds@1952: } ds@1952: bufpt = buf; ds@1952: /* ds@1952: ** If the field type is etGENERIC, then convert to either etEXP ds@1952: ** or etFLOAT, as appropriate. ds@1952: */ ds@1952: flag_exp = xtype==etEXP; ds@1952: if( xtype!=etFLOAT ){ ds@1952: realvalue += rounder; ds@1952: if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } ds@1952: } ds@1952: if( xtype==etGENERIC ){ ds@1952: flag_rtz = !flag_alternateform; ds@1952: if( exp<-4 || exp>precision ){ ds@1952: xtype = etEXP; ds@1952: }else{ ds@1952: precision = precision - exp; ds@1952: xtype = etFLOAT; ds@1952: } ds@1952: }else{ ds@1952: flag_rtz = 0; ds@1952: } ds@1952: if( xtype==etEXP ){ ds@1952: e2 = 0; ds@1952: }else{ ds@1952: e2 = exp; ds@1952: } ds@1952: nsd = 0; ds@1952: flag_dp = (precision>0) | flag_alternateform | flag_altform2; ds@1952: /* The sign in front of the number */ ds@1952: if( prefix ){ ds@1952: *(bufpt++) = prefix; ds@1952: } ds@1952: /* Digits prior to the decimal point */ ds@1952: if( e2<0 ){ ds@1952: *(bufpt++) = '0'; ds@1952: }else{ ds@1952: for(; e2>=0; e2--){ ds@1952: *(bufpt++) = et_getdigit(&realvalue,&nsd); ds@1952: } ds@1952: } ds@1952: /* The decimal point */ ds@1952: if( flag_dp ){ ds@1952: *(bufpt++) = '.'; ds@1952: } ds@1952: /* "0" digits after the decimal point but before the first ds@1952: ** significant digit of the number */ ds@1952: for(e2++; e2<0 && precision>0; precision--, e2++){ ds@1952: *(bufpt++) = '0'; ds@1952: } ds@1952: /* Significant digits after the decimal point */ ds@1952: while( (precision--)>0 ){ ds@1952: *(bufpt++) = et_getdigit(&realvalue,&nsd); ds@1952: } ds@1952: /* Remove trailing zeros and the "." if no digits follow the "." */ ds@1952: if( flag_rtz && flag_dp ){ ds@1952: while( bufpt[-1]=='0' ) *(--bufpt) = 0; ds@1952: assert( bufpt>buf ); ds@1952: if( bufpt[-1]=='.' ){ ds@1952: if( flag_altform2 ){ ds@1952: *(bufpt++) = '0'; ds@1952: }else{ ds@1952: *(--bufpt) = 0; ds@1952: } ds@1952: } ds@1952: } ds@1952: /* Add the "eNNN" suffix */ ds@1952: if( flag_exp || (xtype==etEXP && exp) ){ ds@1952: *(bufpt++) = aDigits[infop->charset]; ds@1952: if( exp<0 ){ ds@1952: *(bufpt++) = '-'; exp = -exp; ds@1952: }else{ ds@1952: *(bufpt++) = '+'; ds@1952: } ds@1952: if( exp>=100 ){ ds@1952: *(bufpt++) = (exp/100)+'0'; /* 100's digit */ ds@1952: exp %= 100; ds@1952: } ds@1952: *(bufpt++) = exp/10+'0'; /* 10's digit */ ds@1952: *(bufpt++) = exp%10+'0'; /* 1's digit */ ds@1952: } ds@1952: *bufpt = 0; ds@1952: ds@1952: /* The converted number is in buf[] and zero terminated. Output it. ds@1952: ** Note that the number is in the usual order, not reversed as with ds@1952: ** integer conversions. */ ds@1952: length = bufpt-buf; ds@1952: bufpt = buf; ds@1952: ds@1952: /* Special case: Add leading zeros if the flag_zeropad flag is ds@1952: ** set and we are not left justified */ ds@1952: if( flag_zeropad && !flag_leftjustify && length < width){ ds@1952: int i; ds@1952: int nPad = width - length; ds@1952: for(i=width; i>=nPad; i--){ ds@1952: bufpt[i] = bufpt[i-nPad]; ds@1952: } ds@1952: i = prefix!=0; ds@1952: while( nPad-- ) bufpt[i++] = '0'; ds@1952: length = width; ds@1952: } ds@1952: #endif ds@1952: break; ds@1952: case etSIZE: ds@1952: *(va_arg(ap,int*)) = count; ds@1952: length = width = 0; ds@1952: break; ds@1952: case etPERCENT: ds@1952: buf[0] = '%'; ds@1952: bufpt = buf; ds@1952: length = 1; ds@1952: break; ds@1952: case etCHARLIT: ds@1952: case etCHARX: ds@1952: c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt); ds@1952: if( precision>=0 ){ ds@1952: for(idx=1; idx=0 && precisionetBUFSIZE ){ ds@1952: bufpt = zExtra = sqlite3_malloc( n ); ds@1952: if( bufpt==0 ) return -1; ds@1952: }else{ ds@1952: bufpt = buf; ds@1952: } ds@1952: j = 0; ds@1952: if( needQuote ) bufpt[j++] = q; ds@1952: for(i=0; (ch=escarg[i])!=0; i++){ ds@1952: bufpt[j++] = ch; ds@1952: if( ch==q ) bufpt[j++] = ch; ds@1952: } ds@1952: if( needQuote ) bufpt[j++] = q; ds@1952: bufpt[j] = 0; ds@1952: length = j; ds@1952: /* The precision is ignored on %q and %Q */ ds@1952: /* if( precision>=0 && precisionz ){ ds@1952: (*func)(arg, (char*)pToken->z, pToken->n); ds@1952: } ds@1952: length = width = 0; ds@1952: break; ds@1952: } ds@1952: case etSRCLIST: { ds@1952: SrcList *pSrc = va_arg(ap, SrcList*); ds@1952: int k = va_arg(ap, int); ds@1952: struct SrcList_item *pItem = &pSrc->a[k]; ds@1952: assert( k>=0 && knSrc ); ds@1952: if( pItem->zDatabase && pItem->zDatabase[0] ){ ds@1952: (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase)); ds@1952: (*func)(arg, ".", 1); ds@1952: } ds@1952: (*func)(arg, pItem->zName, strlen(pItem->zName)); ds@1952: length = width = 0; ds@1952: break; ds@1952: } ds@1952: }/* End switch over the format type */ ds@1952: /* ds@1952: ** The text of the conversion is pointed to by "bufpt" and is ds@1952: ** "length" characters long. The field width is "width". Do ds@1952: ** the output. ds@1952: */ ds@1952: if( !flag_leftjustify ){ ds@1952: register int nspace; ds@1952: nspace = width-length; ds@1952: if( nspace>0 ){ ds@1952: count += nspace; ds@1952: while( nspace>=etSPACESIZE ){ ds@1952: (*func)(arg,spaces,etSPACESIZE); ds@1952: nspace -= etSPACESIZE; ds@1952: } ds@1952: if( nspace>0 ) (*func)(arg,spaces,nspace); ds@1952: } ds@1952: } ds@1952: if( length>0 ){ ds@1952: (*func)(arg,bufpt,length); ds@1952: count += length; ds@1952: } ds@1952: if( flag_leftjustify ){ ds@1952: register int nspace; ds@1952: nspace = width-length; ds@1952: if( nspace>0 ){ ds@1952: count += nspace; ds@1952: while( nspace>=etSPACESIZE ){ ds@1952: (*func)(arg,spaces,etSPACESIZE); ds@1952: nspace -= etSPACESIZE; ds@1952: } ds@1952: if( nspace>0 ) (*func)(arg,spaces,nspace); ds@1952: } ds@1952: } ds@1952: if( zExtra ){ ds@1952: sqlite3_free(zExtra); ds@1952: } ds@1952: }/* End for loop over the format string */ ds@1952: return errorflag ? -1 : count; ds@1952: } /* End of function */ ds@1952: ds@1952: ds@1952: /* This structure is used to store state information about the ds@1952: ** write to memory that is currently in progress. ds@1952: */ ds@1952: struct sgMprintf { ds@1952: char *zBase; /* A base allocation */ ds@1952: char *zText; /* The string collected so far */ ds@1952: int nChar; /* Length of the string so far */ ds@1952: int nTotal; /* Output size if unconstrained */ ds@1952: int nAlloc; /* Amount of space allocated in zText */ ds@1952: void *(*xRealloc)(void*,int); /* Function used to realloc memory */ ds@1952: int iMallocFailed; /* True if xRealloc() has failed */ ds@1952: }; ds@1952: ds@1952: /* ds@1952: ** This function implements the callback from vxprintf. ds@1952: ** ds@1952: ** This routine add nNewChar characters of text in zNewText to ds@1952: ** the sgMprintf structure pointed to by "arg". ds@1952: */ ds@1952: static void mout(void *arg, const char *zNewText, int nNewChar){ ds@1952: struct sgMprintf *pM = (struct sgMprintf*)arg; ds@1952: if( pM->iMallocFailed ) return; ds@1952: pM->nTotal += nNewChar; ds@1952: if( pM->zText ){ ds@1952: if( pM->nChar + nNewChar + 1 > pM->nAlloc ){ ds@1952: if( pM->xRealloc==0 ){ ds@1952: nNewChar = pM->nAlloc - pM->nChar - 1; ds@1952: }else{ ds@1952: int nAlloc = pM->nChar + nNewChar*2 + 1; ds@1952: if( pM->zText==pM->zBase ){ ds@1952: pM->zText = pM->xRealloc(0, nAlloc); ds@1952: if( pM->zText==0 ){ ds@1952: pM->nAlloc = 0; ds@1952: pM->iMallocFailed = 1; ds@1952: return; ds@1952: }else if( pM->nChar ){ ds@1952: memcpy(pM->zText, pM->zBase, pM->nChar); ds@1952: } ds@1952: }else{ ds@1952: char *zNew; ds@1952: zNew = pM->xRealloc(pM->zText, nAlloc); ds@1952: if( zNew ){ ds@1952: pM->zText = zNew; ds@1952: }else{ ds@1952: pM->iMallocFailed = 1; ds@1952: pM->xRealloc(pM->zText, 0); ds@1952: pM->zText = 0; ds@1952: pM->nAlloc = 0; ds@1952: return; ds@1952: } ds@1952: } ds@1952: pM->nAlloc = nAlloc; ds@1952: } ds@1952: } ds@1952: if( nNewChar>0 ){ ds@1952: memcpy(&pM->zText[pM->nChar], zNewText, nNewChar); ds@1952: pM->nChar += nNewChar; ds@1952: } ds@1952: pM->zText[pM->nChar] = 0; ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** This routine is a wrapper around xprintf() that invokes mout() as ds@1952: ** the consumer. ds@1952: */ ds@1952: static char *base_vprintf( ds@1952: void *(*xRealloc)(void*, int), /* realloc() function. May be NULL */ ds@1952: int useInternal, /* Use internal %-conversions if true */ ds@1952: char *zInitBuf, /* Initially write here, before mallocing */ ds@1952: int nInitBuf, /* Size of zInitBuf[] */ ds@1952: const char *zFormat, /* format string */ ds@1952: va_list ap /* arguments */ ds@1952: ){ ds@1952: struct sgMprintf sM; ds@1952: sM.zBase = sM.zText = zInitBuf; ds@1952: sM.nChar = sM.nTotal = 0; ds@1952: sM.nAlloc = nInitBuf; ds@1952: sM.xRealloc = xRealloc; ds@1952: sM.iMallocFailed = 0; ds@1952: vxprintf(mout, &sM, useInternal, zFormat, ap); ds@1952: assert(sM.iMallocFailed==0 || sM.zText==0); ds@1952: if( xRealloc && !sM.iMallocFailed ){ ds@1952: if( sM.zText==sM.zBase ){ ds@1952: sM.zText = xRealloc(0, sM.nChar+1); ds@1952: if( sM.zText ){ ds@1952: memcpy(sM.zText, sM.zBase, sM.nChar+1); ds@1952: } ds@1952: }else if( sM.nAlloc>sM.nChar+10 ){ ds@1952: char *zNew; ds@1952: sqlite3MallocBenignFailure(1); ds@1952: zNew = xRealloc(sM.zText, sM.nChar+1); ds@1952: if( zNew ){ ds@1952: sM.zText = zNew; ds@1952: } ds@1952: } ds@1952: } ds@1952: return sM.zText; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Realloc that is a real function, not a macro. ds@1952: */ ds@1952: static void *printf_realloc(void *old, int size){ ds@1952: return sqlite3_realloc(old, size); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Print into memory obtained from sqliteMalloc(). Use the internal ds@1952: ** %-conversion extensions. ds@1952: */ ds@1952: char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ ds@1952: char *z; ds@1952: char zBase[SQLITE_PRINT_BUF_SIZE]; ds@1952: z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap); ds@1952: if( z==0 && db!=0 ){ ds@1952: db->mallocFailed = 1; ds@1952: } ds@1952: return z; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Print into memory obtained from sqliteMalloc(). Use the internal ds@1952: ** %-conversion extensions. ds@1952: */ ds@1952: char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ ds@1952: va_list ap; ds@1952: char *z; ds@1952: char zBase[SQLITE_PRINT_BUF_SIZE]; ds@1952: va_start(ap, zFormat); ds@1952: z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap); ds@1952: va_end(ap); ds@1952: if( z==0 && db!=0 ){ ds@1952: db->mallocFailed = 1; ds@1952: } ds@1952: return z; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Print into memory obtained from sqlite3_malloc(). Omit the internal ds@1952: ** %-conversion extensions. ds@1952: */ ds@1952: char *sqlite3_vmprintf(const char *zFormat, va_list ap){ ds@1952: char zBase[SQLITE_PRINT_BUF_SIZE]; ds@1952: return base_vprintf(sqlite3_realloc, 0, zBase, sizeof(zBase), zFormat, ap); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Print into memory obtained from sqlite3_malloc()(). Omit the internal ds@1952: ** %-conversion extensions. ds@1952: */ ds@1952: char *sqlite3_mprintf(const char *zFormat, ...){ ds@1952: va_list ap; ds@1952: char *z; ds@1952: va_start(ap, zFormat); ds@1952: z = sqlite3_vmprintf(zFormat, ap); ds@1952: va_end(ap); ds@1952: return z; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** sqlite3_snprintf() works like snprintf() except that it ignores the ds@1952: ** current locale settings. This is important for SQLite because we ds@1952: ** are not able to use a "," as the decimal point in place of "." as ds@1952: ** specified by some locales. ds@1952: */ ds@1952: char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ ds@1952: char *z; ds@1952: va_list ap; ds@1952: ds@1952: if( n<=0 ){ ds@1952: return zBuf; ds@1952: } ds@1952: zBuf[0] = 0; ds@1952: va_start(ap,zFormat); ds@1952: z = base_vprintf(0, 0, zBuf, n, zFormat, ap); ds@1952: va_end(ap); ds@1952: return z; ds@1952: } ds@1952: ds@1952: #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) || defined(SQLITE_MEMDEBUG) ds@1952: /* ds@1952: ** A version of printf() that understands %lld. Used for debugging. ds@1952: ** The printf() built into some versions of windows does not understand %lld ds@1952: ** and segfaults if you give it a long long int. ds@1952: */ ds@1952: void sqlite3DebugPrintf(const char *zFormat, ...){ ds@1952: extern int getpid(void); ds@1952: va_list ap; ds@1952: char zBuf[500]; ds@1952: va_start(ap, zFormat); ds@1952: base_vprintf(0, 0, zBuf, sizeof(zBuf), zFormat, ap); ds@1952: va_end(ap); ds@1952: fprintf(stdout,"%s", zBuf); ds@1952: fflush(stdout); ds@1952: } ds@1952: #endif