extensions/sqlite/sqlite-source/printf.c
changeset 1952 b7aa2b626807
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/sqlite/sqlite-source/printf.c	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,907 @@
     1.4 +/*
     1.5 +** The "printf" code that follows dates from the 1980's.  It is in
     1.6 +** the public domain.  The original comments are included here for
     1.7 +** completeness.  They are very out-of-date but might be useful as
     1.8 +** an historical reference.  Most of the "enhancements" have been backed
     1.9 +** out so that the functionality is now the same as standard printf().
    1.10 +**
    1.11 +**************************************************************************
    1.12 +**
    1.13 +** The following modules is an enhanced replacement for the "printf" subroutines
    1.14 +** found in the standard C library.  The following enhancements are
    1.15 +** supported:
    1.16 +**
    1.17 +**      +  Additional functions.  The standard set of "printf" functions
    1.18 +**         includes printf, fprintf, sprintf, vprintf, vfprintf, and
    1.19 +**         vsprintf.  This module adds the following:
    1.20 +**
    1.21 +**           *  snprintf -- Works like sprintf, but has an extra argument
    1.22 +**                          which is the size of the buffer written to.
    1.23 +**
    1.24 +**           *  mprintf --  Similar to sprintf.  Writes output to memory
    1.25 +**                          obtained from malloc.
    1.26 +**
    1.27 +**           *  xprintf --  Calls a function to dispose of output.
    1.28 +**
    1.29 +**           *  nprintf --  No output, but returns the number of characters
    1.30 +**                          that would have been output by printf.
    1.31 +**
    1.32 +**           *  A v- version (ex: vsnprintf) of every function is also
    1.33 +**              supplied.
    1.34 +**
    1.35 +**      +  A few extensions to the formatting notation are supported:
    1.36 +**
    1.37 +**           *  The "=" flag (similar to "-") causes the output to be
    1.38 +**              be centered in the appropriately sized field.
    1.39 +**
    1.40 +**           *  The %b field outputs an integer in binary notation.
    1.41 +**
    1.42 +**           *  The %c field now accepts a precision.  The character output
    1.43 +**              is repeated by the number of times the precision specifies.
    1.44 +**
    1.45 +**           *  The %' field works like %c, but takes as its character the
    1.46 +**              next character of the format string, instead of the next
    1.47 +**              argument.  For example,  printf("%.78'-")  prints 78 minus
    1.48 +**              signs, the same as  printf("%.78c",'-').
    1.49 +**
    1.50 +**      +  When compiled using GCC on a SPARC, this version of printf is
    1.51 +**         faster than the library printf for SUN OS 4.1.
    1.52 +**
    1.53 +**      +  All functions are fully reentrant.
    1.54 +**
    1.55 +*/
    1.56 +#include "sqliteInt.h"
    1.57 +#include <math.h>
    1.58 +
    1.59 +/*
    1.60 +** Conversion types fall into various categories as defined by the
    1.61 +** following enumeration.
    1.62 +*/
    1.63 +#define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
    1.64 +#define etFLOAT       2 /* Floating point.  %f */
    1.65 +#define etEXP         3 /* Exponentional notation. %e and %E */
    1.66 +#define etGENERIC     4 /* Floating or exponential, depending on exponent. %g */
    1.67 +#define etSIZE        5 /* Return number of characters processed so far. %n */
    1.68 +#define etSTRING      6 /* Strings. %s */
    1.69 +#define etDYNSTRING   7 /* Dynamically allocated strings. %z */
    1.70 +#define etPERCENT     8 /* Percent symbol. %% */
    1.71 +#define etCHARX       9 /* Characters. %c */
    1.72 +/* The rest are extensions, not normally found in printf() */
    1.73 +#define etCHARLIT    10 /* Literal characters.  %' */
    1.74 +#define etSQLESCAPE  11 /* Strings with '\'' doubled.  %q */
    1.75 +#define etSQLESCAPE2 12 /* Strings with '\'' doubled and enclosed in '',
    1.76 +                          NULL pointers replaced by SQL NULL.  %Q */
    1.77 +#define etTOKEN      13 /* a pointer to a Token structure */
    1.78 +#define etSRCLIST    14 /* a pointer to a SrcList */
    1.79 +#define etPOINTER    15 /* The %p conversion */
    1.80 +#define etSQLESCAPE3 16 /* %w -> Strings with '\"' doubled */
    1.81 +
    1.82 +
    1.83 +/*
    1.84 +** An "etByte" is an 8-bit unsigned value.
    1.85 +*/
    1.86 +typedef unsigned char etByte;
    1.87 +
    1.88 +/*
    1.89 +** Each builtin conversion character (ex: the 'd' in "%d") is described
    1.90 +** by an instance of the following structure
    1.91 +*/
    1.92 +typedef struct et_info {   /* Information about each format field */
    1.93 +  char fmttype;            /* The format field code letter */
    1.94 +  etByte base;             /* The base for radix conversion */
    1.95 +  etByte flags;            /* One or more of FLAG_ constants below */
    1.96 +  etByte type;             /* Conversion paradigm */
    1.97 +  etByte charset;          /* Offset into aDigits[] of the digits string */
    1.98 +  etByte prefix;           /* Offset into aPrefix[] of the prefix string */
    1.99 +} et_info;
   1.100 +
   1.101 +/*
   1.102 +** Allowed values for et_info.flags
   1.103 +*/
   1.104 +#define FLAG_SIGNED  1     /* True if the value to convert is signed */
   1.105 +#define FLAG_INTERN  2     /* True if for internal use only */
   1.106 +#define FLAG_STRING  4     /* Allow infinity precision */
   1.107 +
   1.108 +
   1.109 +/*
   1.110 +** The following table is searched linearly, so it is good to put the
   1.111 +** most frequently used conversion types first.
   1.112 +*/
   1.113 +static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
   1.114 +static const char aPrefix[] = "-x0\000X0";
   1.115 +static const et_info fmtinfo[] = {
   1.116 +  {  'd', 10, 1, etRADIX,      0,  0 },
   1.117 +  {  's',  0, 4, etSTRING,     0,  0 },
   1.118 +  {  'g',  0, 1, etGENERIC,    30, 0 },
   1.119 +  {  'z',  0, 4, etDYNSTRING,  0,  0 },
   1.120 +  {  'q',  0, 4, etSQLESCAPE,  0,  0 },
   1.121 +  {  'Q',  0, 4, etSQLESCAPE2, 0,  0 },
   1.122 +  {  'w',  0, 4, etSQLESCAPE3, 0,  0 },
   1.123 +  {  'c',  0, 0, etCHARX,      0,  0 },
   1.124 +  {  'o',  8, 0, etRADIX,      0,  2 },
   1.125 +  {  'u', 10, 0, etRADIX,      0,  0 },
   1.126 +  {  'x', 16, 0, etRADIX,      16, 1 },
   1.127 +  {  'X', 16, 0, etRADIX,      0,  4 },
   1.128 +#ifndef SQLITE_OMIT_FLOATING_POINT
   1.129 +  {  'f',  0, 1, etFLOAT,      0,  0 },
   1.130 +  {  'e',  0, 1, etEXP,        30, 0 },
   1.131 +  {  'E',  0, 1, etEXP,        14, 0 },
   1.132 +  {  'G',  0, 1, etGENERIC,    14, 0 },
   1.133 +#endif
   1.134 +  {  'i', 10, 1, etRADIX,      0,  0 },
   1.135 +  {  'n',  0, 0, etSIZE,       0,  0 },
   1.136 +  {  '%',  0, 0, etPERCENT,    0,  0 },
   1.137 +  {  'p', 16, 0, etPOINTER,    0,  1 },
   1.138 +  {  'T',  0, 2, etTOKEN,      0,  0 },
   1.139 +  {  'S',  0, 2, etSRCLIST,    0,  0 },
   1.140 +};
   1.141 +#define etNINFO  (sizeof(fmtinfo)/sizeof(fmtinfo[0]))
   1.142 +
   1.143 +/*
   1.144 +** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
   1.145 +** conversions will work.
   1.146 +*/
   1.147 +#ifndef SQLITE_OMIT_FLOATING_POINT
   1.148 +/*
   1.149 +** "*val" is a double such that 0.1 <= *val < 10.0
   1.150 +** Return the ascii code for the leading digit of *val, then
   1.151 +** multiply "*val" by 10.0 to renormalize.
   1.152 +**
   1.153 +** Example:
   1.154 +**     input:     *val = 3.14159
   1.155 +**     output:    *val = 1.4159    function return = '3'
   1.156 +**
   1.157 +** The counter *cnt is incremented each time.  After counter exceeds
   1.158 +** 16 (the number of significant digits in a 64-bit float) '0' is
   1.159 +** always returned.
   1.160 +*/
   1.161 +static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
   1.162 +  int digit;
   1.163 +  LONGDOUBLE_TYPE d;
   1.164 +  if( (*cnt)++ >= 16 ) return '0';
   1.165 +  digit = (int)*val;
   1.166 +  d = digit;
   1.167 +  digit += '0';
   1.168 +  *val = (*val - d)*10.0;
   1.169 +  return digit;
   1.170 +}
   1.171 +#endif /* SQLITE_OMIT_FLOATING_POINT */
   1.172 +
   1.173 +/*
   1.174 +** On machines with a small stack size, you can redefine the
   1.175 +** SQLITE_PRINT_BUF_SIZE to be less than 350.  But beware - for
   1.176 +** smaller values some %f conversions may go into an infinite loop.
   1.177 +*/
   1.178 +#ifndef SQLITE_PRINT_BUF_SIZE
   1.179 +# define SQLITE_PRINT_BUF_SIZE 350
   1.180 +#endif
   1.181 +#define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
   1.182 +
   1.183 +/*
   1.184 +** The root program.  All variations call this core.
   1.185 +**
   1.186 +** INPUTS:
   1.187 +**   func   This is a pointer to a function taking three arguments
   1.188 +**            1. A pointer to anything.  Same as the "arg" parameter.
   1.189 +**            2. A pointer to the list of characters to be output
   1.190 +**               (Note, this list is NOT null terminated.)
   1.191 +**            3. An integer number of characters to be output.
   1.192 +**               (Note: This number might be zero.)
   1.193 +**
   1.194 +**   arg    This is the pointer to anything which will be passed as the
   1.195 +**          first argument to "func".  Use it for whatever you like.
   1.196 +**
   1.197 +**   fmt    This is the format string, as in the usual print.
   1.198 +**
   1.199 +**   ap     This is a pointer to a list of arguments.  Same as in
   1.200 +**          vfprint.
   1.201 +**
   1.202 +** OUTPUTS:
   1.203 +**          The return value is the total number of characters sent to
   1.204 +**          the function "func".  Returns -1 on a error.
   1.205 +**
   1.206 +** Note that the order in which automatic variables are declared below
   1.207 +** seems to make a big difference in determining how fast this beast
   1.208 +** will run.
   1.209 +*/
   1.210 +static int vxprintf(
   1.211 +  void (*func)(void*,const char*,int),     /* Consumer of text */
   1.212 +  void *arg,                         /* First argument to the consumer */
   1.213 +  int useExtended,                   /* Allow extended %-conversions */
   1.214 +  const char *fmt,                   /* Format string */
   1.215 +  va_list ap                         /* arguments */
   1.216 +){
   1.217 +  int c;                     /* Next character in the format string */
   1.218 +  char *bufpt;               /* Pointer to the conversion buffer */
   1.219 +  int precision;             /* Precision of the current field */
   1.220 +  int length;                /* Length of the field */
   1.221 +  int idx;                   /* A general purpose loop counter */
   1.222 +  int count;                 /* Total number of characters output */
   1.223 +  int width;                 /* Width of the current field */
   1.224 +  etByte flag_leftjustify;   /* True if "-" flag is present */
   1.225 +  etByte flag_plussign;      /* True if "+" flag is present */
   1.226 +  etByte flag_blanksign;     /* True if " " flag is present */
   1.227 +  etByte flag_alternateform; /* True if "#" flag is present */
   1.228 +  etByte flag_altform2;      /* True if "!" flag is present */
   1.229 +  etByte flag_zeropad;       /* True if field width constant starts with zero */
   1.230 +  etByte flag_long;          /* True if "l" flag is present */
   1.231 +  etByte flag_longlong;      /* True if the "ll" flag is present */
   1.232 +  etByte done;               /* Loop termination flag */
   1.233 +  sqlite_uint64 longvalue;   /* Value for integer types */
   1.234 +  LONGDOUBLE_TYPE realvalue; /* Value for real types */
   1.235 +  const et_info *infop;      /* Pointer to the appropriate info structure */
   1.236 +  char buf[etBUFSIZE];       /* Conversion buffer */
   1.237 +  char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
   1.238 +  etByte errorflag = 0;      /* True if an error is encountered */
   1.239 +  etByte xtype;              /* Conversion paradigm */
   1.240 +  char *zExtra;              /* Extra memory used for etTCLESCAPE conversions */
   1.241 +  static const char spaces[] =
   1.242 +   "                                                                         ";
   1.243 +#define etSPACESIZE (sizeof(spaces)-1)
   1.244 +#ifndef SQLITE_OMIT_FLOATING_POINT
   1.245 +  int  exp, e2;              /* exponent of real numbers */
   1.246 +  double rounder;            /* Used for rounding floating point values */
   1.247 +  etByte flag_dp;            /* True if decimal point should be shown */
   1.248 +  etByte flag_rtz;           /* True if trailing zeros should be removed */
   1.249 +  etByte flag_exp;           /* True to force display of the exponent */
   1.250 +  int nsd;                   /* Number of significant digits returned */
   1.251 +#endif
   1.252 +
   1.253 +  func(arg,"",0);
   1.254 +  count = length = 0;
   1.255 +  bufpt = 0;
   1.256 +  for(; (c=(*fmt))!=0; ++fmt){
   1.257 +    if( c!='%' ){
   1.258 +      int amt;
   1.259 +      bufpt = (char *)fmt;
   1.260 +      amt = 1;
   1.261 +      while( (c=(*++fmt))!='%' && c!=0 ) amt++;
   1.262 +      (*func)(arg,bufpt,amt);
   1.263 +      count += amt;
   1.264 +      if( c==0 ) break;
   1.265 +    }
   1.266 +    if( (c=(*++fmt))==0 ){
   1.267 +      errorflag = 1;
   1.268 +      (*func)(arg,"%",1);
   1.269 +      count++;
   1.270 +      break;
   1.271 +    }
   1.272 +    /* Find out what flags are present */
   1.273 +    flag_leftjustify = flag_plussign = flag_blanksign = 
   1.274 +     flag_alternateform = flag_altform2 = flag_zeropad = 0;
   1.275 +    done = 0;
   1.276 +    do{
   1.277 +      switch( c ){
   1.278 +        case '-':   flag_leftjustify = 1;     break;
   1.279 +        case '+':   flag_plussign = 1;        break;
   1.280 +        case ' ':   flag_blanksign = 1;       break;
   1.281 +        case '#':   flag_alternateform = 1;   break;
   1.282 +        case '!':   flag_altform2 = 1;        break;
   1.283 +        case '0':   flag_zeropad = 1;         break;
   1.284 +        default:    done = 1;                 break;
   1.285 +      }
   1.286 +    }while( !done && (c=(*++fmt))!=0 );
   1.287 +    /* Get the field width */
   1.288 +    width = 0;
   1.289 +    if( c=='*' ){
   1.290 +      width = va_arg(ap,int);
   1.291 +      if( width<0 ){
   1.292 +        flag_leftjustify = 1;
   1.293 +        width = -width;
   1.294 +      }
   1.295 +      c = *++fmt;
   1.296 +    }else{
   1.297 +      while( c>='0' && c<='9' ){
   1.298 +        width = width*10 + c - '0';
   1.299 +        c = *++fmt;
   1.300 +      }
   1.301 +    }
   1.302 +    if( width > etBUFSIZE-10 ){
   1.303 +      width = etBUFSIZE-10;
   1.304 +    }
   1.305 +    /* Get the precision */
   1.306 +    if( c=='.' ){
   1.307 +      precision = 0;
   1.308 +      c = *++fmt;
   1.309 +      if( c=='*' ){
   1.310 +        precision = va_arg(ap,int);
   1.311 +        if( precision<0 ) precision = -precision;
   1.312 +        c = *++fmt;
   1.313 +      }else{
   1.314 +        while( c>='0' && c<='9' ){
   1.315 +          precision = precision*10 + c - '0';
   1.316 +          c = *++fmt;
   1.317 +        }
   1.318 +      }
   1.319 +    }else{
   1.320 +      precision = -1;
   1.321 +    }
   1.322 +    /* Get the conversion type modifier */
   1.323 +    if( c=='l' ){
   1.324 +      flag_long = 1;
   1.325 +      c = *++fmt;
   1.326 +      if( c=='l' ){
   1.327 +        flag_longlong = 1;
   1.328 +        c = *++fmt;
   1.329 +      }else{
   1.330 +        flag_longlong = 0;
   1.331 +      }
   1.332 +    }else{
   1.333 +      flag_long = flag_longlong = 0;
   1.334 +    }
   1.335 +    /* Fetch the info entry for the field */
   1.336 +    infop = 0;
   1.337 +    for(idx=0; idx<etNINFO; idx++){
   1.338 +      if( c==fmtinfo[idx].fmttype ){
   1.339 +        infop = &fmtinfo[idx];
   1.340 +        if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
   1.341 +          xtype = infop->type;
   1.342 +        }else{
   1.343 +          return -1;
   1.344 +        }
   1.345 +        break;
   1.346 +      }
   1.347 +    }
   1.348 +    zExtra = 0;
   1.349 +    if( infop==0 ){
   1.350 +      return -1;
   1.351 +    }
   1.352 +
   1.353 +
   1.354 +    /* Limit the precision to prevent overflowing buf[] during conversion */
   1.355 +    if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
   1.356 +      precision = etBUFSIZE-40;
   1.357 +    }
   1.358 +
   1.359 +    /*
   1.360 +    ** At this point, variables are initialized as follows:
   1.361 +    **
   1.362 +    **   flag_alternateform          TRUE if a '#' is present.
   1.363 +    **   flag_altform2               TRUE if a '!' is present.
   1.364 +    **   flag_plussign               TRUE if a '+' is present.
   1.365 +    **   flag_leftjustify            TRUE if a '-' is present or if the
   1.366 +    **                               field width was negative.
   1.367 +    **   flag_zeropad                TRUE if the width began with 0.
   1.368 +    **   flag_long                   TRUE if the letter 'l' (ell) prefixed
   1.369 +    **                               the conversion character.
   1.370 +    **   flag_longlong               TRUE if the letter 'll' (ell ell) prefixed
   1.371 +    **                               the conversion character.
   1.372 +    **   flag_blanksign              TRUE if a ' ' is present.
   1.373 +    **   width                       The specified field width.  This is
   1.374 +    **                               always non-negative.  Zero is the default.
   1.375 +    **   precision                   The specified precision.  The default
   1.376 +    **                               is -1.
   1.377 +    **   xtype                       The class of the conversion.
   1.378 +    **   infop                       Pointer to the appropriate info struct.
   1.379 +    */
   1.380 +    switch( xtype ){
   1.381 +      case etPOINTER:
   1.382 +        flag_longlong = sizeof(char*)==sizeof(i64);
   1.383 +        flag_long = sizeof(char*)==sizeof(long int);
   1.384 +        /* Fall through into the next case */
   1.385 +      case etRADIX:
   1.386 +        if( infop->flags & FLAG_SIGNED ){
   1.387 +          i64 v;
   1.388 +          if( flag_longlong )   v = va_arg(ap,i64);
   1.389 +          else if( flag_long )  v = va_arg(ap,long int);
   1.390 +          else                  v = va_arg(ap,int);
   1.391 +          if( v<0 ){
   1.392 +            longvalue = -v;
   1.393 +            prefix = '-';
   1.394 +          }else{
   1.395 +            longvalue = v;
   1.396 +            if( flag_plussign )        prefix = '+';
   1.397 +            else if( flag_blanksign )  prefix = ' ';
   1.398 +            else                       prefix = 0;
   1.399 +          }
   1.400 +        }else{
   1.401 +          if( flag_longlong )   longvalue = va_arg(ap,u64);
   1.402 +          else if( flag_long )  longvalue = va_arg(ap,unsigned long int);
   1.403 +          else                  longvalue = va_arg(ap,unsigned int);
   1.404 +          prefix = 0;
   1.405 +        }
   1.406 +        if( longvalue==0 ) flag_alternateform = 0;
   1.407 +        if( flag_zeropad && precision<width-(prefix!=0) ){
   1.408 +          precision = width-(prefix!=0);
   1.409 +        }
   1.410 +        bufpt = &buf[etBUFSIZE-1];
   1.411 +        {
   1.412 +          register const char *cset;      /* Use registers for speed */
   1.413 +          register int base;
   1.414 +          cset = &aDigits[infop->charset];
   1.415 +          base = infop->base;
   1.416 +          do{                                           /* Convert to ascii */
   1.417 +            *(--bufpt) = cset[longvalue%base];
   1.418 +            longvalue = longvalue/base;
   1.419 +          }while( longvalue>0 );
   1.420 +        }
   1.421 +        length = &buf[etBUFSIZE-1]-bufpt;
   1.422 +        for(idx=precision-length; idx>0; idx--){
   1.423 +          *(--bufpt) = '0';                             /* Zero pad */
   1.424 +        }
   1.425 +        if( prefix ) *(--bufpt) = prefix;               /* Add sign */
   1.426 +        if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
   1.427 +          const char *pre;
   1.428 +          char x;
   1.429 +          pre = &aPrefix[infop->prefix];
   1.430 +          if( *bufpt!=pre[0] ){
   1.431 +            for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
   1.432 +          }
   1.433 +        }
   1.434 +        length = &buf[etBUFSIZE-1]-bufpt;
   1.435 +        break;
   1.436 +      case etFLOAT:
   1.437 +      case etEXP:
   1.438 +      case etGENERIC:
   1.439 +        realvalue = va_arg(ap,double);
   1.440 +#ifndef SQLITE_OMIT_FLOATING_POINT
   1.441 +        if( precision<0 ) precision = 6;         /* Set default precision */
   1.442 +        if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10;
   1.443 +        if( realvalue<0.0 ){
   1.444 +          realvalue = -realvalue;
   1.445 +          prefix = '-';
   1.446 +        }else{
   1.447 +          if( flag_plussign )          prefix = '+';
   1.448 +          else if( flag_blanksign )    prefix = ' ';
   1.449 +          else                         prefix = 0;
   1.450 +        }
   1.451 +        if( xtype==etGENERIC && precision>0 ) precision--;
   1.452 +#if 0
   1.453 +        /* Rounding works like BSD when the constant 0.4999 is used.  Wierd! */
   1.454 +        for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
   1.455 +#else
   1.456 +        /* It makes more sense to use 0.5 */
   1.457 +        for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
   1.458 +#endif
   1.459 +        if( xtype==etFLOAT ) realvalue += rounder;
   1.460 +        /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
   1.461 +        exp = 0;
   1.462 +        if( sqlite3_isnan(realvalue) ){
   1.463 +          bufpt = "NaN";
   1.464 +          length = 3;
   1.465 +          break;
   1.466 +        }
   1.467 +        if( realvalue>0.0 ){
   1.468 +          while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
   1.469 +          while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
   1.470 +          while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
   1.471 +          while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; }
   1.472 +          while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; }
   1.473 +          if( exp>350 || exp<-350 ){
   1.474 +            if( prefix=='-' ){
   1.475 +              bufpt = "-Inf";
   1.476 +            }else if( prefix=='+' ){
   1.477 +              bufpt = "+Inf";
   1.478 +            }else{
   1.479 +              bufpt = "Inf";
   1.480 +            }
   1.481 +            length = strlen(bufpt);
   1.482 +            break;
   1.483 +          }
   1.484 +        }
   1.485 +        bufpt = buf;
   1.486 +        /*
   1.487 +        ** If the field type is etGENERIC, then convert to either etEXP
   1.488 +        ** or etFLOAT, as appropriate.
   1.489 +        */
   1.490 +        flag_exp = xtype==etEXP;
   1.491 +        if( xtype!=etFLOAT ){
   1.492 +          realvalue += rounder;
   1.493 +          if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
   1.494 +        }
   1.495 +        if( xtype==etGENERIC ){
   1.496 +          flag_rtz = !flag_alternateform;
   1.497 +          if( exp<-4 || exp>precision ){
   1.498 +            xtype = etEXP;
   1.499 +          }else{
   1.500 +            precision = precision - exp;
   1.501 +            xtype = etFLOAT;
   1.502 +          }
   1.503 +        }else{
   1.504 +          flag_rtz = 0;
   1.505 +        }
   1.506 +        if( xtype==etEXP ){
   1.507 +          e2 = 0;
   1.508 +        }else{
   1.509 +          e2 = exp;
   1.510 +        }
   1.511 +        nsd = 0;
   1.512 +        flag_dp = (precision>0) | flag_alternateform | flag_altform2;
   1.513 +        /* The sign in front of the number */
   1.514 +        if( prefix ){
   1.515 +          *(bufpt++) = prefix;
   1.516 +        }
   1.517 +        /* Digits prior to the decimal point */
   1.518 +        if( e2<0 ){
   1.519 +          *(bufpt++) = '0';
   1.520 +        }else{
   1.521 +          for(; e2>=0; e2--){
   1.522 +            *(bufpt++) = et_getdigit(&realvalue,&nsd);
   1.523 +          }
   1.524 +        }
   1.525 +        /* The decimal point */
   1.526 +        if( flag_dp ){
   1.527 +          *(bufpt++) = '.';
   1.528 +        }
   1.529 +        /* "0" digits after the decimal point but before the first
   1.530 +        ** significant digit of the number */
   1.531 +        for(e2++; e2<0 && precision>0; precision--, e2++){
   1.532 +          *(bufpt++) = '0';
   1.533 +        }
   1.534 +        /* Significant digits after the decimal point */
   1.535 +        while( (precision--)>0 ){
   1.536 +          *(bufpt++) = et_getdigit(&realvalue,&nsd);
   1.537 +        }
   1.538 +        /* Remove trailing zeros and the "." if no digits follow the "." */
   1.539 +        if( flag_rtz && flag_dp ){
   1.540 +          while( bufpt[-1]=='0' ) *(--bufpt) = 0;
   1.541 +          assert( bufpt>buf );
   1.542 +          if( bufpt[-1]=='.' ){
   1.543 +            if( flag_altform2 ){
   1.544 +              *(bufpt++) = '0';
   1.545 +            }else{
   1.546 +              *(--bufpt) = 0;
   1.547 +            }
   1.548 +          }
   1.549 +        }
   1.550 +        /* Add the "eNNN" suffix */
   1.551 +        if( flag_exp || (xtype==etEXP && exp) ){
   1.552 +          *(bufpt++) = aDigits[infop->charset];
   1.553 +          if( exp<0 ){
   1.554 +            *(bufpt++) = '-'; exp = -exp;
   1.555 +          }else{
   1.556 +            *(bufpt++) = '+';
   1.557 +          }
   1.558 +          if( exp>=100 ){
   1.559 +            *(bufpt++) = (exp/100)+'0';                /* 100's digit */
   1.560 +            exp %= 100;
   1.561 +          }
   1.562 +          *(bufpt++) = exp/10+'0';                     /* 10's digit */
   1.563 +          *(bufpt++) = exp%10+'0';                     /* 1's digit */
   1.564 +        }
   1.565 +        *bufpt = 0;
   1.566 +
   1.567 +        /* The converted number is in buf[] and zero terminated. Output it.
   1.568 +        ** Note that the number is in the usual order, not reversed as with
   1.569 +        ** integer conversions. */
   1.570 +        length = bufpt-buf;
   1.571 +        bufpt = buf;
   1.572 +
   1.573 +        /* Special case:  Add leading zeros if the flag_zeropad flag is
   1.574 +        ** set and we are not left justified */
   1.575 +        if( flag_zeropad && !flag_leftjustify && length < width){
   1.576 +          int i;
   1.577 +          int nPad = width - length;
   1.578 +          for(i=width; i>=nPad; i--){
   1.579 +            bufpt[i] = bufpt[i-nPad];
   1.580 +          }
   1.581 +          i = prefix!=0;
   1.582 +          while( nPad-- ) bufpt[i++] = '0';
   1.583 +          length = width;
   1.584 +        }
   1.585 +#endif
   1.586 +        break;
   1.587 +      case etSIZE:
   1.588 +        *(va_arg(ap,int*)) = count;
   1.589 +        length = width = 0;
   1.590 +        break;
   1.591 +      case etPERCENT:
   1.592 +        buf[0] = '%';
   1.593 +        bufpt = buf;
   1.594 +        length = 1;
   1.595 +        break;
   1.596 +      case etCHARLIT:
   1.597 +      case etCHARX:
   1.598 +        c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt);
   1.599 +        if( precision>=0 ){
   1.600 +          for(idx=1; idx<precision; idx++) buf[idx] = c;
   1.601 +          length = precision;
   1.602 +        }else{
   1.603 +          length =1;
   1.604 +        }
   1.605 +        bufpt = buf;
   1.606 +        break;
   1.607 +      case etSTRING:
   1.608 +      case etDYNSTRING:
   1.609 +        bufpt = va_arg(ap,char*);
   1.610 +        if( bufpt==0 ){
   1.611 +          bufpt = "";
   1.612 +        }else if( xtype==etDYNSTRING ){
   1.613 +          zExtra = bufpt;
   1.614 +        }
   1.615 +        length = strlen(bufpt);
   1.616 +        if( precision>=0 && precision<length ) length = precision;
   1.617 +        break;
   1.618 +      case etSQLESCAPE:
   1.619 +      case etSQLESCAPE2:
   1.620 +      case etSQLESCAPE3: {
   1.621 +        int i, j, n, ch, isnull;
   1.622 +        int needQuote;
   1.623 +        char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
   1.624 +        char *escarg = va_arg(ap,char*);
   1.625 +        isnull = escarg==0;
   1.626 +        if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
   1.627 +        for(i=n=0; (ch=escarg[i])!=0; i++){
   1.628 +          if( ch==q )  n++;
   1.629 +        }
   1.630 +        needQuote = !isnull && xtype==etSQLESCAPE2;
   1.631 +        n += i + 1 + needQuote*2;
   1.632 +        if( n>etBUFSIZE ){
   1.633 +          bufpt = zExtra = sqlite3_malloc( n );
   1.634 +          if( bufpt==0 ) return -1;
   1.635 +        }else{
   1.636 +          bufpt = buf;
   1.637 +        }
   1.638 +        j = 0;
   1.639 +        if( needQuote ) bufpt[j++] = q;
   1.640 +        for(i=0; (ch=escarg[i])!=0; i++){
   1.641 +          bufpt[j++] = ch;
   1.642 +          if( ch==q ) bufpt[j++] = ch;
   1.643 +        }
   1.644 +        if( needQuote ) bufpt[j++] = q;
   1.645 +        bufpt[j] = 0;
   1.646 +        length = j;
   1.647 +        /* The precision is ignored on %q and %Q */
   1.648 +        /* if( precision>=0 && precision<length ) length = precision; */
   1.649 +        break;
   1.650 +      }
   1.651 +      case etTOKEN: {
   1.652 +        Token *pToken = va_arg(ap, Token*);
   1.653 +        if( pToken && pToken->z ){
   1.654 +          (*func)(arg, (char*)pToken->z, pToken->n);
   1.655 +        }
   1.656 +        length = width = 0;
   1.657 +        break;
   1.658 +      }
   1.659 +      case etSRCLIST: {
   1.660 +        SrcList *pSrc = va_arg(ap, SrcList*);
   1.661 +        int k = va_arg(ap, int);
   1.662 +        struct SrcList_item *pItem = &pSrc->a[k];
   1.663 +        assert( k>=0 && k<pSrc->nSrc );
   1.664 +        if( pItem->zDatabase && pItem->zDatabase[0] ){
   1.665 +          (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase));
   1.666 +          (*func)(arg, ".", 1);
   1.667 +        }
   1.668 +        (*func)(arg, pItem->zName, strlen(pItem->zName));
   1.669 +        length = width = 0;
   1.670 +        break;
   1.671 +      }
   1.672 +    }/* End switch over the format type */
   1.673 +    /*
   1.674 +    ** The text of the conversion is pointed to by "bufpt" and is
   1.675 +    ** "length" characters long.  The field width is "width".  Do
   1.676 +    ** the output.
   1.677 +    */
   1.678 +    if( !flag_leftjustify ){
   1.679 +      register int nspace;
   1.680 +      nspace = width-length;
   1.681 +      if( nspace>0 ){
   1.682 +        count += nspace;
   1.683 +        while( nspace>=etSPACESIZE ){
   1.684 +          (*func)(arg,spaces,etSPACESIZE);
   1.685 +          nspace -= etSPACESIZE;
   1.686 +        }
   1.687 +        if( nspace>0 ) (*func)(arg,spaces,nspace);
   1.688 +      }
   1.689 +    }
   1.690 +    if( length>0 ){
   1.691 +      (*func)(arg,bufpt,length);
   1.692 +      count += length;
   1.693 +    }
   1.694 +    if( flag_leftjustify ){
   1.695 +      register int nspace;
   1.696 +      nspace = width-length;
   1.697 +      if( nspace>0 ){
   1.698 +        count += nspace;
   1.699 +        while( nspace>=etSPACESIZE ){
   1.700 +          (*func)(arg,spaces,etSPACESIZE);
   1.701 +          nspace -= etSPACESIZE;
   1.702 +        }
   1.703 +        if( nspace>0 ) (*func)(arg,spaces,nspace);
   1.704 +      }
   1.705 +    }
   1.706 +    if( zExtra ){
   1.707 +      sqlite3_free(zExtra);
   1.708 +    }
   1.709 +  }/* End for loop over the format string */
   1.710 +  return errorflag ? -1 : count;
   1.711 +} /* End of function */
   1.712 +
   1.713 +
   1.714 +/* This structure is used to store state information about the
   1.715 +** write to memory that is currently in progress.
   1.716 +*/
   1.717 +struct sgMprintf {
   1.718 +  char *zBase;     /* A base allocation */
   1.719 +  char *zText;     /* The string collected so far */
   1.720 +  int  nChar;      /* Length of the string so far */
   1.721 +  int  nTotal;     /* Output size if unconstrained */
   1.722 +  int  nAlloc;     /* Amount of space allocated in zText */
   1.723 +  void *(*xRealloc)(void*,int);  /* Function used to realloc memory */
   1.724 +  int  iMallocFailed;            /* True if xRealloc() has failed */
   1.725 +};
   1.726 +
   1.727 +/* 
   1.728 +** This function implements the callback from vxprintf. 
   1.729 +**
   1.730 +** This routine add nNewChar characters of text in zNewText to
   1.731 +** the sgMprintf structure pointed to by "arg".
   1.732 +*/
   1.733 +static void mout(void *arg, const char *zNewText, int nNewChar){
   1.734 +  struct sgMprintf *pM = (struct sgMprintf*)arg;
   1.735 +  if( pM->iMallocFailed ) return;
   1.736 +  pM->nTotal += nNewChar;
   1.737 +  if( pM->zText ){
   1.738 +    if( pM->nChar + nNewChar + 1 > pM->nAlloc ){
   1.739 +      if( pM->xRealloc==0 ){
   1.740 +        nNewChar =  pM->nAlloc - pM->nChar - 1;
   1.741 +      }else{
   1.742 +        int nAlloc = pM->nChar + nNewChar*2 + 1;
   1.743 +        if( pM->zText==pM->zBase ){
   1.744 +          pM->zText = pM->xRealloc(0, nAlloc);
   1.745 +          if( pM->zText==0 ){
   1.746 +            pM->nAlloc = 0;
   1.747 +            pM->iMallocFailed = 1;
   1.748 +            return;
   1.749 +          }else if( pM->nChar ){
   1.750 +            memcpy(pM->zText, pM->zBase, pM->nChar);
   1.751 +          }
   1.752 +        }else{
   1.753 +          char *zNew;
   1.754 +          zNew = pM->xRealloc(pM->zText, nAlloc);
   1.755 +          if( zNew ){
   1.756 +            pM->zText = zNew;
   1.757 +          }else{
   1.758 +            pM->iMallocFailed = 1;
   1.759 +            pM->xRealloc(pM->zText, 0);
   1.760 +            pM->zText = 0;
   1.761 +            pM->nAlloc = 0;
   1.762 +            return;
   1.763 +          }
   1.764 +        }
   1.765 +        pM->nAlloc = nAlloc;
   1.766 +      }
   1.767 +    }
   1.768 +    if( nNewChar>0 ){
   1.769 +      memcpy(&pM->zText[pM->nChar], zNewText, nNewChar);
   1.770 +      pM->nChar += nNewChar;
   1.771 +    }
   1.772 +    pM->zText[pM->nChar] = 0;
   1.773 +  }
   1.774 +}
   1.775 +
   1.776 +/*
   1.777 +** This routine is a wrapper around xprintf() that invokes mout() as
   1.778 +** the consumer.  
   1.779 +*/
   1.780 +static char *base_vprintf(
   1.781 +  void *(*xRealloc)(void*, int),  /* realloc() function. May be NULL */
   1.782 +  int useInternal,                /* Use internal %-conversions if true */
   1.783 +  char *zInitBuf,                 /* Initially write here, before mallocing */
   1.784 +  int nInitBuf,                   /* Size of zInitBuf[] */
   1.785 +  const char *zFormat,            /* format string */
   1.786 +  va_list ap                      /* arguments */
   1.787 +){
   1.788 +  struct sgMprintf sM;
   1.789 +  sM.zBase = sM.zText = zInitBuf;
   1.790 +  sM.nChar = sM.nTotal = 0;
   1.791 +  sM.nAlloc = nInitBuf;
   1.792 +  sM.xRealloc = xRealloc;
   1.793 +  sM.iMallocFailed = 0;
   1.794 +  vxprintf(mout, &sM, useInternal, zFormat, ap);
   1.795 +  assert(sM.iMallocFailed==0 || sM.zText==0);
   1.796 +  if( xRealloc && !sM.iMallocFailed ){
   1.797 +    if( sM.zText==sM.zBase ){
   1.798 +      sM.zText = xRealloc(0, sM.nChar+1);
   1.799 +      if( sM.zText ){
   1.800 +        memcpy(sM.zText, sM.zBase, sM.nChar+1);
   1.801 +      }
   1.802 +    }else if( sM.nAlloc>sM.nChar+10 ){
   1.803 +      char *zNew;
   1.804 +      sqlite3MallocBenignFailure(1);
   1.805 +      zNew = xRealloc(sM.zText, sM.nChar+1);
   1.806 +      if( zNew ){
   1.807 +        sM.zText = zNew;
   1.808 +      }
   1.809 +    }
   1.810 +  }
   1.811 +  return sM.zText;
   1.812 +}
   1.813 +
   1.814 +/*
   1.815 +** Realloc that is a real function, not a macro.
   1.816 +*/
   1.817 +static void *printf_realloc(void *old, int size){
   1.818 +  return sqlite3_realloc(old, size);
   1.819 +}
   1.820 +
   1.821 +/*
   1.822 +** Print into memory obtained from sqliteMalloc().  Use the internal
   1.823 +** %-conversion extensions.
   1.824 +*/
   1.825 +char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
   1.826 +  char *z;
   1.827 +  char zBase[SQLITE_PRINT_BUF_SIZE];
   1.828 +  z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
   1.829 +  if( z==0 && db!=0 ){
   1.830 +    db->mallocFailed = 1;
   1.831 +  }
   1.832 +  return z;
   1.833 +}
   1.834 +
   1.835 +/*
   1.836 +** Print into memory obtained from sqliteMalloc().  Use the internal
   1.837 +** %-conversion extensions.
   1.838 +*/
   1.839 +char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
   1.840 +  va_list ap;
   1.841 +  char *z;
   1.842 +  char zBase[SQLITE_PRINT_BUF_SIZE];
   1.843 +  va_start(ap, zFormat);
   1.844 +  z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
   1.845 +  va_end(ap);
   1.846 +  if( z==0 && db!=0 ){
   1.847 +    db->mallocFailed = 1;
   1.848 +  }
   1.849 +  return z;
   1.850 +}
   1.851 +
   1.852 +/*
   1.853 +** Print into memory obtained from sqlite3_malloc().  Omit the internal
   1.854 +** %-conversion extensions.
   1.855 +*/
   1.856 +char *sqlite3_vmprintf(const char *zFormat, va_list ap){
   1.857 +  char zBase[SQLITE_PRINT_BUF_SIZE];
   1.858 +  return base_vprintf(sqlite3_realloc, 0, zBase, sizeof(zBase), zFormat, ap);
   1.859 +}
   1.860 +
   1.861 +/*
   1.862 +** Print into memory obtained from sqlite3_malloc()().  Omit the internal
   1.863 +** %-conversion extensions.
   1.864 +*/
   1.865 +char *sqlite3_mprintf(const char *zFormat, ...){
   1.866 +  va_list ap;
   1.867 +  char *z;
   1.868 +  va_start(ap, zFormat);
   1.869 +  z = sqlite3_vmprintf(zFormat, ap);
   1.870 +  va_end(ap);
   1.871 +  return z;
   1.872 +}
   1.873 +
   1.874 +/*
   1.875 +** sqlite3_snprintf() works like snprintf() except that it ignores the
   1.876 +** current locale settings.  This is important for SQLite because we
   1.877 +** are not able to use a "," as the decimal point in place of "." as
   1.878 +** specified by some locales.
   1.879 +*/
   1.880 +char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
   1.881 +  char *z;
   1.882 +  va_list ap;
   1.883 +
   1.884 +  if( n<=0 ){
   1.885 +    return zBuf;
   1.886 +  }
   1.887 +  zBuf[0] = 0;
   1.888 +  va_start(ap,zFormat);
   1.889 +  z = base_vprintf(0, 0, zBuf, n, zFormat, ap);
   1.890 +  va_end(ap);
   1.891 +  return z;
   1.892 +}
   1.893 +
   1.894 +#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) || defined(SQLITE_MEMDEBUG)
   1.895 +/*
   1.896 +** A version of printf() that understands %lld.  Used for debugging.
   1.897 +** The printf() built into some versions of windows does not understand %lld
   1.898 +** and segfaults if you give it a long long int.
   1.899 +*/
   1.900 +void sqlite3DebugPrintf(const char *zFormat, ...){
   1.901 +  extern int getpid(void);
   1.902 +  va_list ap;
   1.903 +  char zBuf[500];
   1.904 +  va_start(ap, zFormat);
   1.905 +  base_vprintf(0, 0, zBuf, sizeof(zBuf), zFormat, ap);
   1.906 +  va_end(ap);
   1.907 +  fprintf(stdout,"%s", zBuf);
   1.908 +  fflush(stdout);
   1.909 +}
   1.910 +#endif