xref: /sqlite-3.40.0/src/printf.c (revision 557cc60f)
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 */
68e84a306bSdrh #define etERROR      10 /* Used to indicate no such conversion type */
69a18c5681Sdrh /* The rest are extensions, not normally found in printf() */
70e84a306bSdrh #define etCHARLIT    11 /* Literal characters.  %' */
71e84a306bSdrh #define etSQLESCAPE  12 /* Strings with '\'' doubled.  %q */
72e84a306bSdrh #define etSQLESCAPE2 13 /* Strings with '\'' doubled and enclosed in '',
730cfcf3fbSchw                           NULL pointers replaced by SQL NULL.  %Q */
745f968436Sdrh #define etTOKEN      14 /* a pointer to a Token structure */
755f968436Sdrh #define etSRCLIST    15 /* a pointer to a SrcList */
76fe63d1c9Sdrh #define etPOINTER    16 /* The %p conversion */
77e84a306bSdrh 
78e84a306bSdrh 
79e84a306bSdrh /*
80e84a306bSdrh ** An "etByte" is an 8-bit unsigned value.
81e84a306bSdrh */
82e84a306bSdrh typedef unsigned char etByte;
83a18c5681Sdrh 
84a18c5681Sdrh /*
85a18c5681Sdrh ** Each builtin conversion character (ex: the 'd' in "%d") is described
86a18c5681Sdrh ** by an instance of the following structure
87a18c5681Sdrh */
88a18c5681Sdrh typedef struct et_info {   /* Information about each format field */
89e84a306bSdrh   char fmttype;            /* The format field code letter */
90e84a306bSdrh   etByte base;             /* The base for radix conversion */
91e84a306bSdrh   etByte flags;            /* One or more of FLAG_ constants below */
92e84a306bSdrh   etByte type;             /* Conversion paradigm */
9376ff3a0eSdrh   etByte charset;          /* Offset into aDigits[] of the digits string */
9476ff3a0eSdrh   etByte prefix;           /* Offset into aPrefix[] of the prefix string */
95a18c5681Sdrh } et_info;
96a18c5681Sdrh 
97a18c5681Sdrh /*
98e84a306bSdrh ** Allowed values for et_info.flags
99e84a306bSdrh */
100e84a306bSdrh #define FLAG_SIGNED  1     /* True if the value to convert is signed */
101e84a306bSdrh #define FLAG_INTERN  2     /* True if for internal use only */
1024794f735Sdrh #define FLAG_STRING  4     /* Allow infinity precision */
103e84a306bSdrh 
104e84a306bSdrh 
105e84a306bSdrh /*
106a18c5681Sdrh ** The following table is searched linearly, so it is good to put the
107a18c5681Sdrh ** most frequently used conversion types first.
108a18c5681Sdrh */
10976ff3a0eSdrh static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
11076ff3a0eSdrh static const char aPrefix[] = "-x0\000X0";
1115719628aSdrh static const et_info fmtinfo[] = {
11276ff3a0eSdrh   {  'd', 10, 1, etRADIX,      0,  0 },
1134794f735Sdrh   {  's',  0, 4, etSTRING,     0,  0 },
114*557cc60fSdrh   {  'g',  0, 1, etGENERIC,    30, 0 },
1154794f735Sdrh   {  'z',  0, 6, etDYNSTRING,  0,  0 },
1164794f735Sdrh   {  'q',  0, 4, etSQLESCAPE,  0,  0 },
1174794f735Sdrh   {  'Q',  0, 4, etSQLESCAPE2, 0,  0 },
118e84a306bSdrh   {  'c',  0, 0, etCHARX,      0,  0 },
11976ff3a0eSdrh   {  'o',  8, 0, etRADIX,      0,  2 },
12076ff3a0eSdrh   {  'u', 10, 0, etRADIX,      0,  0 },
12176ff3a0eSdrh   {  'x', 16, 0, etRADIX,      16, 1 },
12276ff3a0eSdrh   {  'X', 16, 0, etRADIX,      0,  4 },
123e84a306bSdrh   {  'f',  0, 1, etFLOAT,      0,  0 },
12476ff3a0eSdrh   {  'e',  0, 1, etEXP,        30, 0 },
12576ff3a0eSdrh   {  'E',  0, 1, etEXP,        14, 0 },
12676ff3a0eSdrh   {  'G',  0, 1, etGENERIC,    14, 0 },
12776ff3a0eSdrh   {  'i', 10, 1, etRADIX,      0,  0 },
128e84a306bSdrh   {  'n',  0, 0, etSIZE,       0,  0 },
129e84a306bSdrh   {  '%',  0, 0, etPERCENT,    0,  0 },
13076ff3a0eSdrh   {  'p', 16, 0, etPOINTER,    0,  1 },
1315f968436Sdrh   {  'T',  0, 2, etTOKEN,      0,  0 },
1325f968436Sdrh   {  'S',  0, 2, etSRCLIST,    0,  0 },
133a18c5681Sdrh };
134a18c5681Sdrh #define etNINFO  (sizeof(fmtinfo)/sizeof(fmtinfo[0]))
135a18c5681Sdrh 
136a18c5681Sdrh /*
137a18c5681Sdrh ** If NOFLOATINGPOINT is defined, then none of the floating point
138a18c5681Sdrh ** conversions will work.
139a18c5681Sdrh */
140a18c5681Sdrh #ifndef etNOFLOATINGPOINT
141a18c5681Sdrh /*
142a18c5681Sdrh ** "*val" is a double such that 0.1 <= *val < 10.0
143a18c5681Sdrh ** Return the ascii code for the leading digit of *val, then
144a18c5681Sdrh ** multiply "*val" by 10.0 to renormalize.
145a18c5681Sdrh **
146a18c5681Sdrh ** Example:
147a18c5681Sdrh **     input:     *val = 3.14159
148a18c5681Sdrh **     output:    *val = 1.4159    function return = '3'
149a18c5681Sdrh **
150a18c5681Sdrh ** The counter *cnt is incremented each time.  After counter exceeds
151a18c5681Sdrh ** 16 (the number of significant digits in a 64-bit float) '0' is
152a18c5681Sdrh ** always returned.
153a18c5681Sdrh */
154384eef32Sdrh static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
155a18c5681Sdrh   int digit;
156384eef32Sdrh   LONGDOUBLE_TYPE d;
157a18c5681Sdrh   if( (*cnt)++ >= 16 ) return '0';
158a18c5681Sdrh   digit = (int)*val;
159a18c5681Sdrh   d = digit;
160a18c5681Sdrh   digit += '0';
161a18c5681Sdrh   *val = (*val - d)*10.0;
162a18c5681Sdrh   return digit;
163a18c5681Sdrh }
164a18c5681Sdrh #endif
165a18c5681Sdrh 
166a18c5681Sdrh #define etBUFSIZE 1000  /* Size of the output buffer */
167a18c5681Sdrh 
168a18c5681Sdrh /*
169a18c5681Sdrh ** The root program.  All variations call this core.
170a18c5681Sdrh **
171a18c5681Sdrh ** INPUTS:
172a18c5681Sdrh **   func   This is a pointer to a function taking three arguments
173a18c5681Sdrh **            1. A pointer to anything.  Same as the "arg" parameter.
174a18c5681Sdrh **            2. A pointer to the list of characters to be output
175a18c5681Sdrh **               (Note, this list is NOT null terminated.)
176a18c5681Sdrh **            3. An integer number of characters to be output.
177a18c5681Sdrh **               (Note: This number might be zero.)
178a18c5681Sdrh **
179a18c5681Sdrh **   arg    This is the pointer to anything which will be passed as the
180a18c5681Sdrh **          first argument to "func".  Use it for whatever you like.
181a18c5681Sdrh **
182a18c5681Sdrh **   fmt    This is the format string, as in the usual print.
183a18c5681Sdrh **
184a18c5681Sdrh **   ap     This is a pointer to a list of arguments.  Same as in
185a18c5681Sdrh **          vfprint.
186a18c5681Sdrh **
187a18c5681Sdrh ** OUTPUTS:
188a18c5681Sdrh **          The return value is the total number of characters sent to
189a18c5681Sdrh **          the function "func".  Returns -1 on a error.
190a18c5681Sdrh **
191a18c5681Sdrh ** Note that the order in which automatic variables are declared below
192a18c5681Sdrh ** seems to make a big difference in determining how fast this beast
193a18c5681Sdrh ** will run.
194a18c5681Sdrh */
195a18c5681Sdrh static int vxprintf(
1965f968436Sdrh   void (*func)(void*,const char*,int),     /* Consumer of text */
1975f968436Sdrh   void *arg,                         /* First argument to the consumer */
1985f968436Sdrh   int useExtended,                   /* Allow extended %-conversions */
1995f968436Sdrh   const char *fmt,                   /* Format string */
2005f968436Sdrh   va_list ap                         /* arguments */
201a18c5681Sdrh ){
202e84a306bSdrh   int c;                     /* Next character in the format string */
203e84a306bSdrh   char *bufpt;               /* Pointer to the conversion buffer */
204e84a306bSdrh   int precision;             /* Precision of the current field */
205e84a306bSdrh   int length;                /* Length of the field */
206e84a306bSdrh   int idx;                   /* A general purpose loop counter */
207a18c5681Sdrh   int count;                 /* Total number of characters output */
208a18c5681Sdrh   int width;                 /* Width of the current field */
209e84a306bSdrh   etByte flag_leftjustify;   /* True if "-" flag is present */
210e84a306bSdrh   etByte flag_plussign;      /* True if "+" flag is present */
211e84a306bSdrh   etByte flag_blanksign;     /* True if " " flag is present */
212e84a306bSdrh   etByte flag_alternateform; /* True if "#" flag is present */
213*557cc60fSdrh   etByte flag_altform2;      /* True if "$" flag is present */
214e84a306bSdrh   etByte flag_zeropad;       /* True if field width constant starts with zero */
215e84a306bSdrh   etByte flag_long;          /* True if "l" flag is present */
216a34b6764Sdrh   etByte flag_longlong;      /* True if the "ll" flag is present */
217a34b6764Sdrh   UINT64_TYPE longvalue;     /* Value for integer types */
218384eef32Sdrh   LONGDOUBLE_TYPE realvalue; /* Value for real types */
2195719628aSdrh   const et_info *infop;      /* Pointer to the appropriate info structure */
220a18c5681Sdrh   char buf[etBUFSIZE];       /* Conversion buffer */
221a18c5681Sdrh   char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
222e84a306bSdrh   etByte errorflag = 0;      /* True if an error is encountered */
223e84a306bSdrh   etByte xtype;              /* Conversion paradigm */
224a18c5681Sdrh   char *zExtra;              /* Extra memory used for etTCLESCAPE conversions */
2255719628aSdrh   static const char spaces[] =
2265719628aSdrh    "                                                                         ";
227a18c5681Sdrh #define etSPACESIZE (sizeof(spaces)-1)
228a18c5681Sdrh #ifndef etNOFLOATINGPOINT
229*557cc60fSdrh   int  exp, e2;              /* exponent of real numbers */
230a18c5681Sdrh   double rounder;            /* Used for rounding floating point values */
231e84a306bSdrh   etByte flag_dp;            /* True if decimal point should be shown */
232e84a306bSdrh   etByte flag_rtz;           /* True if trailing zeros should be removed */
233e84a306bSdrh   etByte flag_exp;           /* True to force display of the exponent */
234a18c5681Sdrh   int nsd;                   /* Number of significant digits returned */
235a18c5681Sdrh #endif
236a18c5681Sdrh 
237e29b1a05Sdrh   func(arg,"",0);
238a18c5681Sdrh   count = length = 0;
239a18c5681Sdrh   bufpt = 0;
240a18c5681Sdrh   for(; (c=(*fmt))!=0; ++fmt){
241a18c5681Sdrh     if( c!='%' ){
242e84a306bSdrh       int amt;
243a18c5681Sdrh       bufpt = (char *)fmt;
244a18c5681Sdrh       amt = 1;
245a18c5681Sdrh       while( (c=(*++fmt))!='%' && c!=0 ) amt++;
246a18c5681Sdrh       (*func)(arg,bufpt,amt);
247a18c5681Sdrh       count += amt;
248a18c5681Sdrh       if( c==0 ) break;
249a18c5681Sdrh     }
250a18c5681Sdrh     if( (c=(*++fmt))==0 ){
251a18c5681Sdrh       errorflag = 1;
252a18c5681Sdrh       (*func)(arg,"%",1);
253a18c5681Sdrh       count++;
254a18c5681Sdrh       break;
255a18c5681Sdrh     }
256a18c5681Sdrh     /* Find out what flags are present */
257a18c5681Sdrh     flag_leftjustify = flag_plussign = flag_blanksign =
258*557cc60fSdrh      flag_alternateform = flag_altform2 = flag_zeropad = 0;
259a18c5681Sdrh     do{
260a18c5681Sdrh       switch( c ){
261a18c5681Sdrh         case '-':   flag_leftjustify = 1;     c = 0;   break;
262a18c5681Sdrh         case '+':   flag_plussign = 1;        c = 0;   break;
263a18c5681Sdrh         case ' ':   flag_blanksign = 1;       c = 0;   break;
264a18c5681Sdrh         case '#':   flag_alternateform = 1;   c = 0;   break;
265*557cc60fSdrh         case '!':   flag_altform2 = 1;        c = 0;   break;
266a18c5681Sdrh         case '0':   flag_zeropad = 1;         c = 0;   break;
267a18c5681Sdrh         default:                                       break;
268a18c5681Sdrh       }
269a18c5681Sdrh     }while( c==0 && (c=(*++fmt))!=0 );
270a18c5681Sdrh     /* Get the field width */
271a18c5681Sdrh     width = 0;
272a18c5681Sdrh     if( c=='*' ){
273a18c5681Sdrh       width = va_arg(ap,int);
274a18c5681Sdrh       if( width<0 ){
275a18c5681Sdrh         flag_leftjustify = 1;
276a18c5681Sdrh         width = -width;
277a18c5681Sdrh       }
278a18c5681Sdrh       c = *++fmt;
279a18c5681Sdrh     }else{
28017a68934Sdrh       while( c>='0' && c<='9' ){
281a18c5681Sdrh         width = width*10 + c - '0';
282a18c5681Sdrh         c = *++fmt;
283a18c5681Sdrh       }
284a18c5681Sdrh     }
285a18c5681Sdrh     if( width > etBUFSIZE-10 ){
286a18c5681Sdrh       width = etBUFSIZE-10;
287a18c5681Sdrh     }
288a18c5681Sdrh     /* Get the precision */
289a18c5681Sdrh     if( c=='.' ){
290a18c5681Sdrh       precision = 0;
291a18c5681Sdrh       c = *++fmt;
292a18c5681Sdrh       if( c=='*' ){
293a18c5681Sdrh         precision = va_arg(ap,int);
294a18c5681Sdrh         if( precision<0 ) precision = -precision;
295a18c5681Sdrh         c = *++fmt;
296a18c5681Sdrh       }else{
29717a68934Sdrh         while( c>='0' && c<='9' ){
298a18c5681Sdrh           precision = precision*10 + c - '0';
299a18c5681Sdrh           c = *++fmt;
300a18c5681Sdrh         }
301a18c5681Sdrh       }
302a18c5681Sdrh     }else{
303a18c5681Sdrh       precision = -1;
304a18c5681Sdrh     }
305a18c5681Sdrh     /* Get the conversion type modifier */
306a18c5681Sdrh     if( c=='l' ){
307a18c5681Sdrh       flag_long = 1;
308a18c5681Sdrh       c = *++fmt;
309a34b6764Sdrh       if( c=='l' ){
310a34b6764Sdrh         flag_longlong = 1;
311a34b6764Sdrh         c = *++fmt;
312a18c5681Sdrh       }else{
313a34b6764Sdrh         flag_longlong = 0;
314a34b6764Sdrh       }
315a34b6764Sdrh     }else{
316a34b6764Sdrh       flag_long = flag_longlong = 0;
317a18c5681Sdrh     }
318a18c5681Sdrh     /* Fetch the info entry for the field */
319a18c5681Sdrh     infop = 0;
320e84a306bSdrh     xtype = etERROR;
321a18c5681Sdrh     for(idx=0; idx<etNINFO; idx++){
322a18c5681Sdrh       if( c==fmtinfo[idx].fmttype ){
323a18c5681Sdrh         infop = &fmtinfo[idx];
3245f968436Sdrh         if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
325e84a306bSdrh           xtype = infop->type;
3265f968436Sdrh         }
327a18c5681Sdrh         break;
328a18c5681Sdrh       }
329a18c5681Sdrh     }
330a18c5681Sdrh     zExtra = 0;
331a18c5681Sdrh 
3324794f735Sdrh     /* Limit the precision to prevent overflowing buf[] during conversion */
3334794f735Sdrh     if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
3344794f735Sdrh       precision = etBUFSIZE-40;
3354794f735Sdrh     }
3364794f735Sdrh 
337a18c5681Sdrh     /*
338a18c5681Sdrh     ** At this point, variables are initialized as follows:
339a18c5681Sdrh     **
340a18c5681Sdrh     **   flag_alternateform          TRUE if a '#' is present.
341a18c5681Sdrh     **   flag_plussign               TRUE if a '+' is present.
342a18c5681Sdrh     **   flag_leftjustify            TRUE if a '-' is present or if the
343a18c5681Sdrh     **                               field width was negative.
344a18c5681Sdrh     **   flag_zeropad                TRUE if the width began with 0.
345a18c5681Sdrh     **   flag_long                   TRUE if the letter 'l' (ell) prefixed
346a18c5681Sdrh     **                               the conversion character.
347a34b6764Sdrh     **   flag_longlong               TRUE if the letter 'll' (ell ell) prefixed
348a34b6764Sdrh     **                               the conversion character.
349a18c5681Sdrh     **   flag_blanksign              TRUE if a ' ' is present.
350a18c5681Sdrh     **   width                       The specified field width.  This is
351a18c5681Sdrh     **                               always non-negative.  Zero is the default.
352a18c5681Sdrh     **   precision                   The specified precision.  The default
353a18c5681Sdrh     **                               is -1.
354a18c5681Sdrh     **   xtype                       The class of the conversion.
355a18c5681Sdrh     **   infop                       Pointer to the appropriate info struct.
356a18c5681Sdrh     */
357a18c5681Sdrh     switch( xtype ){
358fe63d1c9Sdrh       case etPOINTER:
359fe63d1c9Sdrh         flag_longlong = sizeof(char*)==sizeof(i64);
360fe63d1c9Sdrh         flag_long = sizeof(char*)==sizeof(long int);
361fe63d1c9Sdrh         /* Fall through into the next case */
362a18c5681Sdrh       case etRADIX:
363e84a306bSdrh         if( infop->flags & FLAG_SIGNED ){
364e9707671Sdrh           i64 v;
365e9707671Sdrh           if( flag_longlong )   v = va_arg(ap,i64);
366e9707671Sdrh           else if( flag_long )  v = va_arg(ap,long int);
367e9707671Sdrh           else                  v = va_arg(ap,int);
368e9707671Sdrh           if( v<0 ){
369e9707671Sdrh             longvalue = -v;
370cfcdaefeSdanielk1977             prefix = '-';
371cfcdaefeSdanielk1977           }else{
372e9707671Sdrh             longvalue = v;
373e9707671Sdrh             if( flag_plussign )        prefix = '+';
374a18c5681Sdrh             else if( flag_blanksign )  prefix = ' ';
375a18c5681Sdrh             else                       prefix = 0;
376cfcdaefeSdanielk1977           }
377e9707671Sdrh         }else{
378e9707671Sdrh           if( flag_longlong )   longvalue = va_arg(ap,u64);
379e9707671Sdrh           else if( flag_long )  longvalue = va_arg(ap,unsigned long int);
380e9707671Sdrh           else                  longvalue = va_arg(ap,unsigned int);
381e9707671Sdrh           prefix = 0;
382e9707671Sdrh         }
383e9707671Sdrh         if( longvalue==0 ) flag_alternateform = 0;
384a18c5681Sdrh         if( flag_zeropad && precision<width-(prefix!=0) ){
385a18c5681Sdrh           precision = width-(prefix!=0);
386a18c5681Sdrh         }
3873039c0a8Sdrh         bufpt = &buf[etBUFSIZE-1];
388a18c5681Sdrh         {
38976ff3a0eSdrh           register const char *cset;      /* Use registers for speed */
390a18c5681Sdrh           register int base;
39176ff3a0eSdrh           cset = &aDigits[infop->charset];
392a18c5681Sdrh           base = infop->base;
393a18c5681Sdrh           do{                                           /* Convert to ascii */
394a18c5681Sdrh             *(--bufpt) = cset[longvalue%base];
395a18c5681Sdrh             longvalue = longvalue/base;
396a18c5681Sdrh           }while( longvalue>0 );
397a18c5681Sdrh         }
3983039c0a8Sdrh         length = &buf[etBUFSIZE-1]-bufpt;
399a18c5681Sdrh         for(idx=precision-length; idx>0; idx--){
400a18c5681Sdrh           *(--bufpt) = '0';                             /* Zero pad */
401a18c5681Sdrh         }
402a18c5681Sdrh         if( prefix ) *(--bufpt) = prefix;               /* Add sign */
403a18c5681Sdrh         if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
40476ff3a0eSdrh           const char *pre;
40576ff3a0eSdrh           char x;
40676ff3a0eSdrh           pre = &aPrefix[infop->prefix];
407a18c5681Sdrh           if( *bufpt!=pre[0] ){
40876ff3a0eSdrh             for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
409a18c5681Sdrh           }
410a18c5681Sdrh         }
4113039c0a8Sdrh         length = &buf[etBUFSIZE-1]-bufpt;
412a18c5681Sdrh         break;
413a18c5681Sdrh       case etFLOAT:
414a18c5681Sdrh       case etEXP:
415a18c5681Sdrh       case etGENERIC:
416a18c5681Sdrh         realvalue = va_arg(ap,double);
417a18c5681Sdrh #ifndef etNOFLOATINGPOINT
418a18c5681Sdrh         if( precision<0 ) precision = 6;         /* Set default precision */
419a18c5681Sdrh         if( precision>etBUFSIZE-10 ) precision = etBUFSIZE-10;
420a18c5681Sdrh         if( realvalue<0.0 ){
421a18c5681Sdrh           realvalue = -realvalue;
422a18c5681Sdrh           prefix = '-';
423a18c5681Sdrh         }else{
424a18c5681Sdrh           if( flag_plussign )          prefix = '+';
425a18c5681Sdrh           else if( flag_blanksign )    prefix = ' ';
426a18c5681Sdrh           else                         prefix = 0;
427a18c5681Sdrh         }
428a18c5681Sdrh         if( infop->type==etGENERIC && precision>0 ) precision--;
429a18c5681Sdrh         rounder = 0.0;
4305f968436Sdrh #if 0
431a18c5681Sdrh         /* Rounding works like BSD when the constant 0.4999 is used.  Wierd! */
432a18c5681Sdrh         for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
433a18c5681Sdrh #else
434a18c5681Sdrh         /* It makes more sense to use 0.5 */
435a18c5681Sdrh         for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1);
436a18c5681Sdrh #endif
437a18c5681Sdrh         if( infop->type==etFLOAT ) realvalue += rounder;
438a18c5681Sdrh         /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
439a18c5681Sdrh         exp = 0;
440a18c5681Sdrh         if( realvalue>0.0 ){
441*557cc60fSdrh           while( realvalue>1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
442*557cc60fSdrh           while( realvalue>1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
443*557cc60fSdrh           while( realvalue>10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
444b621c237Sdrh           while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; }
445b621c237Sdrh           while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; }
446b621c237Sdrh           if( exp>350 || exp<-350 ){
447a18c5681Sdrh             bufpt = "NaN";
448a18c5681Sdrh             length = 3;
449a18c5681Sdrh             break;
450a18c5681Sdrh           }
451a18c5681Sdrh         }
452a18c5681Sdrh         bufpt = buf;
453a18c5681Sdrh         /*
454a18c5681Sdrh         ** If the field type is etGENERIC, then convert to either etEXP
455a18c5681Sdrh         ** or etFLOAT, as appropriate.
456a18c5681Sdrh         */
457a18c5681Sdrh         flag_exp = xtype==etEXP;
458a18c5681Sdrh         if( xtype!=etFLOAT ){
459a18c5681Sdrh           realvalue += rounder;
460a18c5681Sdrh           if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
461a18c5681Sdrh         }
462a18c5681Sdrh         if( xtype==etGENERIC ){
463a18c5681Sdrh           flag_rtz = !flag_alternateform;
464a18c5681Sdrh           if( exp<-4 || exp>precision ){
465a18c5681Sdrh             xtype = etEXP;
466a18c5681Sdrh           }else{
467a18c5681Sdrh             precision = precision - exp;
468a18c5681Sdrh             xtype = etFLOAT;
469a18c5681Sdrh           }
470a18c5681Sdrh         }else{
471a18c5681Sdrh           flag_rtz = 0;
472a18c5681Sdrh         }
473*557cc60fSdrh         /* If exp+precision causes the output to be too big for etFLOAT, then
474*557cc60fSdrh         ** do etEXP instead
475a18c5681Sdrh         */
476*557cc60fSdrh         if( xtype==etFLOAT && exp+precision>=etBUFSIZE-30 ){
477*557cc60fSdrh           xtype = etEXP;
478*557cc60fSdrh         }
479*557cc60fSdrh         if( xtype==etEXP ){
480*557cc60fSdrh           e2 = 0;
481*557cc60fSdrh         }else{
482*557cc60fSdrh           e2 = exp;
483*557cc60fSdrh         }
484a18c5681Sdrh         nsd = 0;
485*557cc60fSdrh         flag_dp = (precision>0) | flag_alternateform | flag_altform2;
486*557cc60fSdrh         /* The sign in front of the number */
487*557cc60fSdrh         if( prefix ){
488*557cc60fSdrh           *(bufpt++) = prefix;
489*557cc60fSdrh         }
490*557cc60fSdrh         /* Digits prior to the decimal point */
491*557cc60fSdrh         if( e2<0 ){
492*557cc60fSdrh           *(bufpt++) = '0';
493*557cc60fSdrh         }else{
494*557cc60fSdrh           for(; e2>=0; e2--){
495*557cc60fSdrh             *(bufpt++) = et_getdigit(&realvalue,&nsd);
496*557cc60fSdrh           }
497*557cc60fSdrh         }
498*557cc60fSdrh         /* The decimal point */
499*557cc60fSdrh         if( flag_dp ){
500*557cc60fSdrh           *(bufpt++) = '.';
501*557cc60fSdrh         }
502*557cc60fSdrh         /* "0" digits after the decimal point but before the first
503*557cc60fSdrh         ** significant digit of the number */
504*557cc60fSdrh         for(e2++; e2<0 && precision>0; precision--, e2++){
505a18c5681Sdrh           *(bufpt++) = '0';
506a18c5681Sdrh         }
507*557cc60fSdrh         /* Significant digits after the decimal point */
508*557cc60fSdrh         while( (precision--)>0 ){
509*557cc60fSdrh           *(bufpt++) = et_getdigit(&realvalue,&nsd);
510a18c5681Sdrh         }
511*557cc60fSdrh         /* Remove trailing zeros and the "." if no digits follow the "." */
512*557cc60fSdrh         if( flag_rtz && flag_dp ){
513*557cc60fSdrh           while( bufpt>buf && bufpt[-1]=='0' ) *(--bufpt) = 0;
514*557cc60fSdrh           if( bufpt>buf && bufpt[-1]=='.' ){
515*557cc60fSdrh             if( flag_altform2 ){
516*557cc60fSdrh               *(bufpt++) = '0';
517*557cc60fSdrh             }else{
518*557cc60fSdrh               *(--bufpt) = 0;
519a18c5681Sdrh             }
520*557cc60fSdrh           }
521*557cc60fSdrh         }
522*557cc60fSdrh         /* Add the "eNNN" suffix */
523*557cc60fSdrh         if( flag_exp || (xtype==etEXP && exp) ){
52476ff3a0eSdrh           *(bufpt++) = aDigits[infop->charset];
525*557cc60fSdrh           if( exp<0 ){
526*557cc60fSdrh             *(bufpt++) = '-'; exp = -exp;
527*557cc60fSdrh           }else{
528*557cc60fSdrh             *(bufpt++) = '+';
529*557cc60fSdrh           }
530a18c5681Sdrh           if( exp>=100 ){
531a18c5681Sdrh             *(bufpt++) = (exp/100)+'0';                /* 100's digit */
532a18c5681Sdrh             exp %= 100;
533a18c5681Sdrh           }
534a18c5681Sdrh           *(bufpt++) = exp/10+'0';                     /* 10's digit */
535a18c5681Sdrh           *(bufpt++) = exp%10+'0';                     /* 1's digit */
536a18c5681Sdrh         }
537*557cc60fSdrh         *bufpt = 0;
538*557cc60fSdrh 
539a18c5681Sdrh         /* The converted number is in buf[] and zero terminated. Output it.
540a18c5681Sdrh         ** Note that the number is in the usual order, not reversed as with
541a18c5681Sdrh         ** integer conversions. */
54294ce4c1eSdrh         length = bufpt-buf;
543a18c5681Sdrh         bufpt = buf;
544a18c5681Sdrh 
545a18c5681Sdrh         /* Special case:  Add leading zeros if the flag_zeropad flag is
546a18c5681Sdrh         ** set and we are not left justified */
547a18c5681Sdrh         if( flag_zeropad && !flag_leftjustify && length < width){
548a18c5681Sdrh           int i;
549a18c5681Sdrh           int nPad = width - length;
550a18c5681Sdrh           for(i=width; i>=nPad; i--){
551a18c5681Sdrh             bufpt[i] = bufpt[i-nPad];
552a18c5681Sdrh           }
553a18c5681Sdrh           i = prefix!=0;
554a18c5681Sdrh           while( nPad-- ) bufpt[i++] = '0';
555a18c5681Sdrh           length = width;
556a18c5681Sdrh         }
557a18c5681Sdrh #endif
558a18c5681Sdrh         break;
559a18c5681Sdrh       case etSIZE:
560a18c5681Sdrh         *(va_arg(ap,int*)) = count;
561a18c5681Sdrh         length = width = 0;
562a18c5681Sdrh         break;
563a18c5681Sdrh       case etPERCENT:
564a18c5681Sdrh         buf[0] = '%';
565a18c5681Sdrh         bufpt = buf;
566a18c5681Sdrh         length = 1;
567a18c5681Sdrh         break;
568a18c5681Sdrh       case etCHARLIT:
569a18c5681Sdrh       case etCHARX:
570a18c5681Sdrh         c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt);
571a18c5681Sdrh         if( precision>=0 ){
572a18c5681Sdrh           for(idx=1; idx<precision; idx++) buf[idx] = c;
573a18c5681Sdrh           length = precision;
574a18c5681Sdrh         }else{
575a18c5681Sdrh           length =1;
576a18c5681Sdrh         }
577a18c5681Sdrh         bufpt = buf;
578a18c5681Sdrh         break;
579a18c5681Sdrh       case etSTRING:
580d93d8a81Sdrh       case etDYNSTRING:
581cb485882Sdrh         bufpt = va_arg(ap,char*);
582d93d8a81Sdrh         if( bufpt==0 ){
583d93d8a81Sdrh           bufpt = "";
584d93d8a81Sdrh         }else if( xtype==etDYNSTRING ){
585d93d8a81Sdrh           zExtra = bufpt;
586d93d8a81Sdrh         }
587a18c5681Sdrh         length = strlen(bufpt);
588a18c5681Sdrh         if( precision>=0 && precision<length ) length = precision;
589a18c5681Sdrh         break;
590a18c5681Sdrh       case etSQLESCAPE:
5910cfcf3fbSchw       case etSQLESCAPE2:
592a18c5681Sdrh         {
5930cfcf3fbSchw           int i, j, n, c, isnull;
5944794f735Sdrh           int needQuote;
595a18c5681Sdrh           char *arg = va_arg(ap,char*);
5960cfcf3fbSchw           isnull = arg==0;
5970cfcf3fbSchw           if( isnull ) arg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
598a18c5681Sdrh           for(i=n=0; (c=arg[i])!=0; i++){
599a18c5681Sdrh             if( c=='\'' )  n++;
600a18c5681Sdrh           }
6014794f735Sdrh           needQuote = !isnull && xtype==etSQLESCAPE2;
6024794f735Sdrh           n += i + 1 + needQuote*2;
603a18c5681Sdrh           if( n>etBUFSIZE ){
604a18c5681Sdrh             bufpt = zExtra = sqliteMalloc( n );
605daffd0e5Sdrh             if( bufpt==0 ) return -1;
606a18c5681Sdrh           }else{
607a18c5681Sdrh             bufpt = buf;
608a18c5681Sdrh           }
6090cfcf3fbSchw           j = 0;
6104794f735Sdrh           if( needQuote ) bufpt[j++] = '\'';
6110cfcf3fbSchw           for(i=0; (c=arg[i])!=0; i++){
612a18c5681Sdrh             bufpt[j++] = c;
613a18c5681Sdrh             if( c=='\'' ) bufpt[j++] = c;
614a18c5681Sdrh           }
6154794f735Sdrh           if( needQuote ) bufpt[j++] = '\'';
616a18c5681Sdrh           bufpt[j] = 0;
617a18c5681Sdrh           length = j;
618a18c5681Sdrh           if( precision>=0 && precision<length ) length = precision;
619a18c5681Sdrh         }
620a18c5681Sdrh         break;
6215f968436Sdrh       case etTOKEN: {
6225f968436Sdrh         Token *pToken = va_arg(ap, Token*);
623ad6d9460Sdrh         if( pToken && pToken->z ){
6245f968436Sdrh           (*func)(arg, pToken->z, pToken->n);
625ad6d9460Sdrh         }
6265f968436Sdrh         length = width = 0;
6275f968436Sdrh         break;
6285f968436Sdrh       }
6295f968436Sdrh       case etSRCLIST: {
6305f968436Sdrh         SrcList *pSrc = va_arg(ap, SrcList*);
6315f968436Sdrh         int k = va_arg(ap, int);
6325f968436Sdrh         struct SrcList_item *pItem = &pSrc->a[k];
6335f968436Sdrh         assert( k>=0 && k<pSrc->nSrc );
6345f968436Sdrh         if( pItem->zDatabase && pItem->zDatabase[0] ){
6355f968436Sdrh           (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase));
6365f968436Sdrh           (*func)(arg, ".", 1);
6375f968436Sdrh         }
6385f968436Sdrh         (*func)(arg, pItem->zName, strlen(pItem->zName));
6395f968436Sdrh         length = width = 0;
6405f968436Sdrh         break;
6415f968436Sdrh       }
642a18c5681Sdrh       case etERROR:
643a18c5681Sdrh         buf[0] = '%';
644a18c5681Sdrh         buf[1] = c;
645a18c5681Sdrh         errorflag = 0;
646a18c5681Sdrh         idx = 1+(c!=0);
647a18c5681Sdrh         (*func)(arg,"%",idx);
648a18c5681Sdrh         count += idx;
649a18c5681Sdrh         if( c==0 ) fmt--;
650a18c5681Sdrh         break;
651a18c5681Sdrh     }/* End switch over the format type */
652a18c5681Sdrh     /*
653a18c5681Sdrh     ** The text of the conversion is pointed to by "bufpt" and is
654a18c5681Sdrh     ** "length" characters long.  The field width is "width".  Do
655a18c5681Sdrh     ** the output.
656a18c5681Sdrh     */
657a18c5681Sdrh     if( !flag_leftjustify ){
658a18c5681Sdrh       register int nspace;
659a18c5681Sdrh       nspace = width-length;
660a18c5681Sdrh       if( nspace>0 ){
661a18c5681Sdrh         count += nspace;
662a18c5681Sdrh         while( nspace>=etSPACESIZE ){
663a18c5681Sdrh           (*func)(arg,spaces,etSPACESIZE);
664a18c5681Sdrh           nspace -= etSPACESIZE;
665a18c5681Sdrh         }
666a18c5681Sdrh         if( nspace>0 ) (*func)(arg,spaces,nspace);
667a18c5681Sdrh       }
668a18c5681Sdrh     }
669a18c5681Sdrh     if( length>0 ){
670a18c5681Sdrh       (*func)(arg,bufpt,length);
671a18c5681Sdrh       count += length;
672a18c5681Sdrh     }
673a18c5681Sdrh     if( flag_leftjustify ){
674a18c5681Sdrh       register int nspace;
675a18c5681Sdrh       nspace = width-length;
676a18c5681Sdrh       if( nspace>0 ){
677a18c5681Sdrh         count += nspace;
678a18c5681Sdrh         while( nspace>=etSPACESIZE ){
679a18c5681Sdrh           (*func)(arg,spaces,etSPACESIZE);
680a18c5681Sdrh           nspace -= etSPACESIZE;
681a18c5681Sdrh         }
682a18c5681Sdrh         if( nspace>0 ) (*func)(arg,spaces,nspace);
683a18c5681Sdrh       }
684a18c5681Sdrh     }
685a18c5681Sdrh     if( zExtra ){
686a18c5681Sdrh       sqliteFree(zExtra);
687a18c5681Sdrh     }
688a18c5681Sdrh   }/* End for loop over the format string */
689a18c5681Sdrh   return errorflag ? -1 : count;
690a18c5681Sdrh } /* End of function */
691a18c5681Sdrh 
692a18c5681Sdrh 
693a18c5681Sdrh /* This structure is used to store state information about the
694a18c5681Sdrh ** write to memory that is currently in progress.
695a18c5681Sdrh */
696a18c5681Sdrh struct sgMprintf {
697a18c5681Sdrh   char *zBase;     /* A base allocation */
698a18c5681Sdrh   char *zText;     /* The string collected so far */
699a18c5681Sdrh   int  nChar;      /* Length of the string so far */
7005f968436Sdrh   int  nTotal;     /* Output size if unconstrained */
701a18c5681Sdrh   int  nAlloc;     /* Amount of space allocated in zText */
7025f968436Sdrh   void *(*xRealloc)(void*,int);  /* Function used to realloc memory */
703a18c5681Sdrh };
704a18c5681Sdrh 
705a18c5681Sdrh /*
706a18c5681Sdrh ** This function implements the callback from vxprintf.
707a18c5681Sdrh **
708a18c5681Sdrh ** This routine add nNewChar characters of text in zNewText to
709a18c5681Sdrh ** the sgMprintf structure pointed to by "arg".
710a18c5681Sdrh */
7115f968436Sdrh static void mout(void *arg, const char *zNewText, int nNewChar){
712a18c5681Sdrh   struct sgMprintf *pM = (struct sgMprintf*)arg;
7135f968436Sdrh   pM->nTotal += nNewChar;
714a18c5681Sdrh   if( pM->nChar + nNewChar + 1 > pM->nAlloc ){
7155f968436Sdrh     if( pM->xRealloc==0 ){
7165f968436Sdrh       nNewChar =  pM->nAlloc - pM->nChar - 1;
7175f968436Sdrh     }else{
718a18c5681Sdrh       pM->nAlloc = pM->nChar + nNewChar*2 + 1;
719a18c5681Sdrh       if( pM->zText==pM->zBase ){
7205f968436Sdrh         pM->zText = pM->xRealloc(0, pM->nAlloc);
7215f968436Sdrh         if( pM->zText && pM->nChar ){
7225f968436Sdrh           memcpy(pM->zText, pM->zBase, pM->nChar);
7235f968436Sdrh         }
724a18c5681Sdrh       }else{
7255f968436Sdrh         pM->zText = pM->xRealloc(pM->zText, pM->nAlloc);
726a18c5681Sdrh       }
727a18c5681Sdrh     }
7285f968436Sdrh   }
729e29b1a05Sdrh   if( pM->zText ){
730e29b1a05Sdrh     if( nNewChar>0 ){
731a18c5681Sdrh       memcpy(&pM->zText[pM->nChar], zNewText, nNewChar);
732a18c5681Sdrh       pM->nChar += nNewChar;
733e29b1a05Sdrh     }
734a18c5681Sdrh     pM->zText[pM->nChar] = 0;
735a18c5681Sdrh   }
736a18c5681Sdrh }
737a18c5681Sdrh 
738a18c5681Sdrh /*
7395f968436Sdrh ** This routine is a wrapper around xprintf() that invokes mout() as
7405f968436Sdrh ** the consumer.
741a18c5681Sdrh */
7425f968436Sdrh static char *base_vprintf(
7435f968436Sdrh   void *(*xRealloc)(void*,int),   /* Routine to realloc memory. May be NULL */
7445f968436Sdrh   int useInternal,                /* Use internal %-conversions if true */
7455f968436Sdrh   char *zInitBuf,                 /* Initially write here, before mallocing */
7465f968436Sdrh   int nInitBuf,                   /* Size of zInitBuf[] */
7475f968436Sdrh   const char *zFormat,            /* format string */
7485f968436Sdrh   va_list ap                      /* arguments */
7495f968436Sdrh ){
7505f968436Sdrh   struct sgMprintf sM;
7515f968436Sdrh   sM.zBase = sM.zText = zInitBuf;
7525f968436Sdrh   sM.nChar = sM.nTotal = 0;
7535f968436Sdrh   sM.nAlloc = nInitBuf;
7545f968436Sdrh   sM.xRealloc = xRealloc;
7555f968436Sdrh   vxprintf(mout, &sM, useInternal, zFormat, ap);
7565f968436Sdrh   if( xRealloc ){
7575f968436Sdrh     if( sM.zText==sM.zBase ){
7585f968436Sdrh       sM.zText = xRealloc(0, sM.nChar+1);
7598def5ea2Sdanielk1977       if( sM.zText ){
7605f968436Sdrh         memcpy(sM.zText, sM.zBase, sM.nChar+1);
7618def5ea2Sdanielk1977       }
7625f968436Sdrh     }else if( sM.nAlloc>sM.nChar+10 ){
7635f968436Sdrh       sM.zText = xRealloc(sM.zText, sM.nChar+1);
7645f968436Sdrh     }
7655f968436Sdrh   }
7665f968436Sdrh   return sM.zText;
767483750baSdrh }
768483750baSdrh 
769483750baSdrh /*
7705f968436Sdrh ** Realloc that is a real function, not a macro.
7715f968436Sdrh */
7725f968436Sdrh static void *printf_realloc(void *old, int size){
7735f968436Sdrh   return sqliteRealloc(old,size);
7745f968436Sdrh }
7755f968436Sdrh 
7765f968436Sdrh /*
7775f968436Sdrh ** Print into memory obtained from sqliteMalloc().  Use the internal
7785f968436Sdrh ** %-conversion extensions.
7795f968436Sdrh */
7804adee20fSdanielk1977 char *sqlite3VMPrintf(const char *zFormat, va_list ap){
7815f968436Sdrh   char zBase[1000];
7825f968436Sdrh   return base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
7835f968436Sdrh }
7845f968436Sdrh 
7855f968436Sdrh /*
7865f968436Sdrh ** Print into memory obtained from sqliteMalloc().  Use the internal
7875f968436Sdrh ** %-conversion extensions.
7885f968436Sdrh */
7894adee20fSdanielk1977 char *sqlite3MPrintf(const char *zFormat, ...){
7905f968436Sdrh   va_list ap;
7915f968436Sdrh   char *z;
7925f968436Sdrh   char zBase[1000];
7935f968436Sdrh   va_start(ap, zFormat);
7945f968436Sdrh   z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
7955f968436Sdrh   va_end(ap);
7965f968436Sdrh   return z;
7975f968436Sdrh }
7985f968436Sdrh 
7995f968436Sdrh /*
8005f968436Sdrh ** Print into memory obtained from malloc().  Do not use the internal
8015f968436Sdrh ** %-conversion extensions.  This routine is for use by external users.
802483750baSdrh */
8036f8a503dSdanielk1977 char *sqlite3_mprintf(const char *zFormat, ...){
804a18c5681Sdrh   va_list ap;
8055f968436Sdrh   char *z;
806a18c5681Sdrh   char zBuf[200];
807a18c5681Sdrh 
808a18c5681Sdrh   va_start(ap,zFormat);
8095f968436Sdrh   z = base_vprintf((void*(*)(void*,int))realloc, 0,
8105f968436Sdrh                    zBuf, sizeof(zBuf), zFormat, ap);
811a18c5681Sdrh   va_end(ap);
8125f968436Sdrh   return z;
813a18c5681Sdrh }
814a18c5681Sdrh 
8156f8a503dSdanielk1977 /* This is the varargs version of sqlite3_mprintf.
816a18c5681Sdrh */
8176f8a503dSdanielk1977 char *sqlite3_vmprintf(const char *zFormat, va_list ap){
818a18c5681Sdrh   char zBuf[200];
8195f968436Sdrh   return base_vprintf((void*(*)(void*,int))realloc, 0,
8205f968436Sdrh                       zBuf, sizeof(zBuf), zFormat, ap);
821a18c5681Sdrh }
822a18c5681Sdrh 
823a18c5681Sdrh /*
8246f8a503dSdanielk1977 ** sqlite3_snprintf() works like snprintf() except that it ignores the
82593a5c6bdSdrh ** current locale settings.  This is important for SQLite because we
82693a5c6bdSdrh ** are not able to use a "," as the decimal point in place of "." as
82793a5c6bdSdrh ** specified by some locales.
82893a5c6bdSdrh */
8296f8a503dSdanielk1977 char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
8305f968436Sdrh   char *z;
83193a5c6bdSdrh   va_list ap;
83293a5c6bdSdrh 
83393a5c6bdSdrh   va_start(ap,zFormat);
8345f968436Sdrh   z = base_vprintf(0, 0, zBuf, n, zFormat, ap);
83593a5c6bdSdrh   va_end(ap);
8365f968436Sdrh   return z;
83793a5c6bdSdrh }
83893a5c6bdSdrh 
8391b743be8Sdrh #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
840e54ca3feSdrh /*
841e54ca3feSdrh ** A version of printf() that understands %lld.  Used for debugging.
842e54ca3feSdrh ** The printf() built into some versions of windows does not understand %lld
843e54ca3feSdrh ** and segfaults if you give it a long long int.
844e54ca3feSdrh */
845e54ca3feSdrh void sqlite3DebugPrintf(const char *zFormat, ...){
84676ff3a0eSdrh   extern int getpid(void);
847e54ca3feSdrh   va_list ap;
848e54ca3feSdrh   char zBuf[500];
849e54ca3feSdrh   va_start(ap, zFormat);
850e54ca3feSdrh   base_vprintf(0, 0, zBuf, sizeof(zBuf), zFormat, ap);
851e54ca3feSdrh   va_end(ap);
85259c98a6fSdrh   fprintf(stdout,"%d: %s", getpid(), zBuf);
8532ac3ee97Sdrh   fflush(stdout);
854e54ca3feSdrh }
855e54ca3feSdrh #endif
856