xref: /sqlite-3.40.0/src/printf.c (revision 7e02e5e6)
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 **
10ed1fddf4Sdrh ** This file contains code for a set of "printf"-like routines.  These
11ed1fddf4Sdrh ** routines format strings much like the printf() from the standard C
12ed1fddf4Sdrh ** library, though the implementation here has enhancements to support
13ed1fddf4Sdrh ** SQLlite.
14a18c5681Sdrh */
15a18c5681Sdrh #include "sqliteInt.h"
167c68d60bSdrh 
17a18c5681Sdrh /*
18a18c5681Sdrh ** Conversion types fall into various categories as defined by the
19a18c5681Sdrh ** following enumeration.
20a18c5681Sdrh */
21e84a306bSdrh #define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
22e84a306bSdrh #define etFLOAT       2 /* Floating point.  %f */
23e84a306bSdrh #define etEXP         3 /* Exponentional notation. %e and %E */
24e84a306bSdrh #define etGENERIC     4 /* Floating or exponential, depending on exponent. %g */
25e84a306bSdrh #define etSIZE        5 /* Return number of characters processed so far. %n */
26e84a306bSdrh #define etSTRING      6 /* Strings. %s */
27e84a306bSdrh #define etDYNSTRING   7 /* Dynamically allocated strings. %z */
28e84a306bSdrh #define etPERCENT     8 /* Percent symbol. %% */
29e84a306bSdrh #define etCHARX       9 /* Characters. %c */
30a18c5681Sdrh /* The rest are extensions, not normally found in printf() */
31af005fbcSdrh #define etSQLESCAPE  10 /* Strings with '\'' doubled.  %q */
32af005fbcSdrh #define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
330cfcf3fbSchw                           NULL pointers replaced by SQL NULL.  %Q */
34af005fbcSdrh #define etTOKEN      12 /* a pointer to a Token structure */
35af005fbcSdrh #define etSRCLIST    13 /* a pointer to a SrcList */
36af005fbcSdrh #define etPOINTER    14 /* The %p conversion */
37af005fbcSdrh #define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */
38af005fbcSdrh #define etORDINAL    16 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
39e84a306bSdrh 
40874ba04cSdrh #define etINVALID     0 /* Any unrecognized conversion type */
41874ba04cSdrh 
42e84a306bSdrh 
43e84a306bSdrh /*
44e84a306bSdrh ** An "etByte" is an 8-bit unsigned value.
45e84a306bSdrh */
46e84a306bSdrh typedef unsigned char etByte;
47a18c5681Sdrh 
48a18c5681Sdrh /*
49a18c5681Sdrh ** Each builtin conversion character (ex: the 'd' in "%d") is described
50a18c5681Sdrh ** by an instance of the following structure
51a18c5681Sdrh */
52a18c5681Sdrh typedef struct et_info {   /* Information about each format field */
53e84a306bSdrh   char fmttype;            /* The format field code letter */
54e84a306bSdrh   etByte base;             /* The base for radix conversion */
55e84a306bSdrh   etByte flags;            /* One or more of FLAG_ constants below */
56e84a306bSdrh   etByte type;             /* Conversion paradigm */
5776ff3a0eSdrh   etByte charset;          /* Offset into aDigits[] of the digits string */
5876ff3a0eSdrh   etByte prefix;           /* Offset into aPrefix[] of the prefix string */
59a18c5681Sdrh } et_info;
60a18c5681Sdrh 
61a18c5681Sdrh /*
62e84a306bSdrh ** Allowed values for et_info.flags
63e84a306bSdrh */
64e84a306bSdrh #define FLAG_SIGNED  1     /* True if the value to convert is signed */
65e84a306bSdrh #define FLAG_INTERN  2     /* True if for internal use only */
664794f735Sdrh #define FLAG_STRING  4     /* Allow infinity precision */
67e84a306bSdrh 
68e84a306bSdrh 
69e84a306bSdrh /*
70a18c5681Sdrh ** The following table is searched linearly, so it is good to put the
71a18c5681Sdrh ** most frequently used conversion types first.
72a18c5681Sdrh */
7376ff3a0eSdrh static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
7476ff3a0eSdrh static const char aPrefix[] = "-x0\000X0";
755719628aSdrh static const et_info fmtinfo[] = {
7676ff3a0eSdrh   {  'd', 10, 1, etRADIX,      0,  0 },
774794f735Sdrh   {  's',  0, 4, etSTRING,     0,  0 },
78557cc60fSdrh   {  'g',  0, 1, etGENERIC,    30, 0 },
79153c62c4Sdrh   {  'z',  0, 4, etDYNSTRING,  0,  0 },
804794f735Sdrh   {  'q',  0, 4, etSQLESCAPE,  0,  0 },
814794f735Sdrh   {  'Q',  0, 4, etSQLESCAPE2, 0,  0 },
82f3b863edSdanielk1977   {  'w',  0, 4, etSQLESCAPE3, 0,  0 },
83e84a306bSdrh   {  'c',  0, 0, etCHARX,      0,  0 },
8476ff3a0eSdrh   {  'o',  8, 0, etRADIX,      0,  2 },
8576ff3a0eSdrh   {  'u', 10, 0, etRADIX,      0,  0 },
8676ff3a0eSdrh   {  'x', 16, 0, etRADIX,      16, 1 },
8776ff3a0eSdrh   {  'X', 16, 0, etRADIX,      0,  4 },
88b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT
89e84a306bSdrh   {  'f',  0, 1, etFLOAT,      0,  0 },
9076ff3a0eSdrh   {  'e',  0, 1, etEXP,        30, 0 },
9176ff3a0eSdrh   {  'E',  0, 1, etEXP,        14, 0 },
9276ff3a0eSdrh   {  'G',  0, 1, etGENERIC,    14, 0 },
93b37df7b9Sdrh #endif
9476ff3a0eSdrh   {  'i', 10, 1, etRADIX,      0,  0 },
95e84a306bSdrh   {  'n',  0, 0, etSIZE,       0,  0 },
96e84a306bSdrh   {  '%',  0, 0, etPERCENT,    0,  0 },
9776ff3a0eSdrh   {  'p', 16, 0, etPOINTER,    0,  1 },
987e3ff5d8Sdrh 
997e3ff5d8Sdrh /* All the rest have the FLAG_INTERN bit set and are thus for internal
1007e3ff5d8Sdrh ** use only */
1015f968436Sdrh   {  'T',  0, 2, etTOKEN,      0,  0 },
1025f968436Sdrh   {  'S',  0, 2, etSRCLIST,    0,  0 },
1039a99334dSdrh   {  'r', 10, 3, etORDINAL,    0,  0 },
104a18c5681Sdrh };
105a18c5681Sdrh 
106a18c5681Sdrh /*
107b37df7b9Sdrh ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
108a18c5681Sdrh ** conversions will work.
109a18c5681Sdrh */
110b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT
111a18c5681Sdrh /*
112a18c5681Sdrh ** "*val" is a double such that 0.1 <= *val < 10.0
113a18c5681Sdrh ** Return the ascii code for the leading digit of *val, then
114a18c5681Sdrh ** multiply "*val" by 10.0 to renormalize.
115a18c5681Sdrh **
116a18c5681Sdrh ** Example:
117a18c5681Sdrh **     input:     *val = 3.14159
118a18c5681Sdrh **     output:    *val = 1.4159    function return = '3'
119a18c5681Sdrh **
120a18c5681Sdrh ** The counter *cnt is incremented each time.  After counter exceeds
121a18c5681Sdrh ** 16 (the number of significant digits in a 64-bit float) '0' is
122a18c5681Sdrh ** always returned.
123a18c5681Sdrh */
124ea678832Sdrh static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
125a18c5681Sdrh   int digit;
126384eef32Sdrh   LONGDOUBLE_TYPE d;
127a18c5681Sdrh   if( (*cnt)++ >= 16 ) return '0';
128a18c5681Sdrh   digit = (int)*val;
129a18c5681Sdrh   d = digit;
130a18c5681Sdrh   digit += '0';
131a18c5681Sdrh   *val = (*val - d)*10.0;
132ea678832Sdrh   return (char)digit;
133a18c5681Sdrh }
134b37df7b9Sdrh #endif /* SQLITE_OMIT_FLOATING_POINT */
135a18c5681Sdrh 
13679158e18Sdrh /*
137ade86483Sdrh ** Append N space characters to the given string buffer.
138ade86483Sdrh */
139*7e02e5e6Sdrh void sqlite3AppendSpace(StrAccum *pAccum, int N){
140ade86483Sdrh   static const char zSpaces[] = "                             ";
14100e13613Sdanielk1977   while( N>=(int)sizeof(zSpaces)-1 ){
142ade86483Sdrh     sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
143ade86483Sdrh     N -= sizeof(zSpaces)-1;
144ade86483Sdrh   }
145ade86483Sdrh   if( N>0 ){
146ade86483Sdrh     sqlite3StrAccumAppend(pAccum, zSpaces, N);
147ade86483Sdrh   }
148ade86483Sdrh }
149ade86483Sdrh 
150ade86483Sdrh /*
15179158e18Sdrh ** On machines with a small stack size, you can redefine the
152ed1fddf4Sdrh ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
15379158e18Sdrh */
15479158e18Sdrh #ifndef SQLITE_PRINT_BUF_SIZE
15559eedf79Sdrh # define SQLITE_PRINT_BUF_SIZE 70
15650d654daSdrh #endif
15779158e18Sdrh #define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
158a18c5681Sdrh 
159a18c5681Sdrh /*
160ed1fddf4Sdrh ** Render a string given by "fmt" into the StrAccum object.
161a18c5681Sdrh */
162f089aa45Sdrh void sqlite3VXPrintf(
163ade86483Sdrh   StrAccum *pAccum,                  /* Accumulate results here */
1645f968436Sdrh   int useExtended,                   /* Allow extended %-conversions */
1655f968436Sdrh   const char *fmt,                   /* Format string */
1665f968436Sdrh   va_list ap                         /* arguments */
167a18c5681Sdrh ){
168e84a306bSdrh   int c;                     /* Next character in the format string */
169e84a306bSdrh   char *bufpt;               /* Pointer to the conversion buffer */
170e84a306bSdrh   int precision;             /* Precision of the current field */
171e84a306bSdrh   int length;                /* Length of the field */
172e84a306bSdrh   int idx;                   /* A general purpose loop counter */
173a18c5681Sdrh   int width;                 /* Width of the current field */
174e84a306bSdrh   etByte flag_leftjustify;   /* True if "-" flag is present */
175e84a306bSdrh   etByte flag_plussign;      /* True if "+" flag is present */
176e84a306bSdrh   etByte flag_blanksign;     /* True if " " flag is present */
177e84a306bSdrh   etByte flag_alternateform; /* True if "#" flag is present */
178531fe878Sdrh   etByte flag_altform2;      /* True if "!" flag is present */
179e84a306bSdrh   etByte flag_zeropad;       /* True if field width constant starts with zero */
180e84a306bSdrh   etByte flag_long;          /* True if "l" flag is present */
181a34b6764Sdrh   etByte flag_longlong;      /* True if the "ll" flag is present */
1823e9aeec0Sdrh   etByte done;               /* Loop termination flag */
183ed1fddf4Sdrh   etByte xtype = 0;          /* Conversion paradigm */
184ed1fddf4Sdrh   char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
18527436af7Sdrh   sqlite_uint64 longvalue;   /* Value for integer types */
186384eef32Sdrh   LONGDOUBLE_TYPE realvalue; /* Value for real types */
1875719628aSdrh   const et_info *infop;      /* Pointer to the appropriate info structure */
18859eedf79Sdrh   char *zOut;                /* Rendering buffer */
18959eedf79Sdrh   int nOut;                  /* Size of the rendering buffer */
190ed1fddf4Sdrh   char *zExtra;              /* Malloced memory used by some conversion */
191b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT
192557cc60fSdrh   int  exp, e2;              /* exponent of real numbers */
193ed1fddf4Sdrh   int nsd;                   /* Number of significant digits returned */
194a18c5681Sdrh   double rounder;            /* Used for rounding floating point values */
195e84a306bSdrh   etByte flag_dp;            /* True if decimal point should be shown */
196e84a306bSdrh   etByte flag_rtz;           /* True if trailing zeros should be removed */
197a18c5681Sdrh #endif
198ed1fddf4Sdrh   char buf[etBUFSIZE];       /* Conversion buffer */
199a18c5681Sdrh 
200a18c5681Sdrh   bufpt = 0;
201a18c5681Sdrh   for(; (c=(*fmt))!=0; ++fmt){
202a18c5681Sdrh     if( c!='%' ){
203e84a306bSdrh       int amt;
204a18c5681Sdrh       bufpt = (char *)fmt;
205a18c5681Sdrh       amt = 1;
206a18c5681Sdrh       while( (c=(*++fmt))!='%' && c!=0 ) amt++;
207ade86483Sdrh       sqlite3StrAccumAppend(pAccum, bufpt, amt);
208a18c5681Sdrh       if( c==0 ) break;
209a18c5681Sdrh     }
210a18c5681Sdrh     if( (c=(*++fmt))==0 ){
211ade86483Sdrh       sqlite3StrAccumAppend(pAccum, "%", 1);
212a18c5681Sdrh       break;
213a18c5681Sdrh     }
214a18c5681Sdrh     /* Find out what flags are present */
215a18c5681Sdrh     flag_leftjustify = flag_plussign = flag_blanksign =
216557cc60fSdrh      flag_alternateform = flag_altform2 = flag_zeropad = 0;
2173e9aeec0Sdrh     done = 0;
218a18c5681Sdrh     do{
219a18c5681Sdrh       switch( c ){
2203e9aeec0Sdrh         case '-':   flag_leftjustify = 1;     break;
2213e9aeec0Sdrh         case '+':   flag_plussign = 1;        break;
2223e9aeec0Sdrh         case ' ':   flag_blanksign = 1;       break;
2233e9aeec0Sdrh         case '#':   flag_alternateform = 1;   break;
2243e9aeec0Sdrh         case '!':   flag_altform2 = 1;        break;
2253e9aeec0Sdrh         case '0':   flag_zeropad = 1;         break;
2263e9aeec0Sdrh         default:    done = 1;                 break;
227a18c5681Sdrh       }
2283e9aeec0Sdrh     }while( !done && (c=(*++fmt))!=0 );
229a18c5681Sdrh     /* Get the field width */
230a18c5681Sdrh     width = 0;
231a18c5681Sdrh     if( c=='*' ){
232a18c5681Sdrh       width = va_arg(ap,int);
233a18c5681Sdrh       if( width<0 ){
234a18c5681Sdrh         flag_leftjustify = 1;
235a18c5681Sdrh         width = -width;
236a18c5681Sdrh       }
237a18c5681Sdrh       c = *++fmt;
238a18c5681Sdrh     }else{
23917a68934Sdrh       while( c>='0' && c<='9' ){
240a18c5681Sdrh         width = width*10 + c - '0';
241a18c5681Sdrh         c = *++fmt;
242a18c5681Sdrh       }
243a18c5681Sdrh     }
244a18c5681Sdrh     /* Get the precision */
245a18c5681Sdrh     if( c=='.' ){
246a18c5681Sdrh       precision = 0;
247a18c5681Sdrh       c = *++fmt;
248a18c5681Sdrh       if( c=='*' ){
249a18c5681Sdrh         precision = va_arg(ap,int);
250a18c5681Sdrh         if( precision<0 ) precision = -precision;
251a18c5681Sdrh         c = *++fmt;
252a18c5681Sdrh       }else{
25317a68934Sdrh         while( c>='0' && c<='9' ){
254a18c5681Sdrh           precision = precision*10 + c - '0';
255a18c5681Sdrh           c = *++fmt;
256a18c5681Sdrh         }
257a18c5681Sdrh       }
258a18c5681Sdrh     }else{
259a18c5681Sdrh       precision = -1;
260a18c5681Sdrh     }
261a18c5681Sdrh     /* Get the conversion type modifier */
262a18c5681Sdrh     if( c=='l' ){
263a18c5681Sdrh       flag_long = 1;
264a18c5681Sdrh       c = *++fmt;
265a34b6764Sdrh       if( c=='l' ){
266a34b6764Sdrh         flag_longlong = 1;
267a34b6764Sdrh         c = *++fmt;
268a18c5681Sdrh       }else{
269a34b6764Sdrh         flag_longlong = 0;
270a34b6764Sdrh       }
271a34b6764Sdrh     }else{
272a34b6764Sdrh       flag_long = flag_longlong = 0;
273a18c5681Sdrh     }
274a18c5681Sdrh     /* Fetch the info entry for the field */
275874ba04cSdrh     infop = &fmtinfo[0];
276874ba04cSdrh     xtype = etINVALID;
27700e13613Sdanielk1977     for(idx=0; idx<ArraySize(fmtinfo); idx++){
278a18c5681Sdrh       if( c==fmtinfo[idx].fmttype ){
279a18c5681Sdrh         infop = &fmtinfo[idx];
2805f968436Sdrh         if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
281e84a306bSdrh           xtype = infop->type;
282c4413565Sdrh         }else{
283ade86483Sdrh           return;
2845f968436Sdrh         }
285a18c5681Sdrh         break;
286a18c5681Sdrh       }
287a18c5681Sdrh     }
288a18c5681Sdrh     zExtra = 0;
28943617e9aSdrh 
290a18c5681Sdrh     /*
291a18c5681Sdrh     ** At this point, variables are initialized as follows:
292a18c5681Sdrh     **
293a18c5681Sdrh     **   flag_alternateform          TRUE if a '#' is present.
2943e9aeec0Sdrh     **   flag_altform2               TRUE if a '!' is present.
295a18c5681Sdrh     **   flag_plussign               TRUE if a '+' is present.
296a18c5681Sdrh     **   flag_leftjustify            TRUE if a '-' is present or if the
297a18c5681Sdrh     **                               field width was negative.
298a18c5681Sdrh     **   flag_zeropad                TRUE if the width began with 0.
299a18c5681Sdrh     **   flag_long                   TRUE if the letter 'l' (ell) prefixed
300a18c5681Sdrh     **                               the conversion character.
301a34b6764Sdrh     **   flag_longlong               TRUE if the letter 'll' (ell ell) prefixed
302a34b6764Sdrh     **                               the conversion character.
303a18c5681Sdrh     **   flag_blanksign              TRUE if a ' ' is present.
304a18c5681Sdrh     **   width                       The specified field width.  This is
305a18c5681Sdrh     **                               always non-negative.  Zero is the default.
306a18c5681Sdrh     **   precision                   The specified precision.  The default
307a18c5681Sdrh     **                               is -1.
308a18c5681Sdrh     **   xtype                       The class of the conversion.
309a18c5681Sdrh     **   infop                       Pointer to the appropriate info struct.
310a18c5681Sdrh     */
311a18c5681Sdrh     switch( xtype ){
312fe63d1c9Sdrh       case etPOINTER:
313fe63d1c9Sdrh         flag_longlong = sizeof(char*)==sizeof(i64);
314fe63d1c9Sdrh         flag_long = sizeof(char*)==sizeof(long int);
315fe63d1c9Sdrh         /* Fall through into the next case */
3169a99334dSdrh       case etORDINAL:
317a18c5681Sdrh       case etRADIX:
318e84a306bSdrh         if( infop->flags & FLAG_SIGNED ){
319e9707671Sdrh           i64 v;
320eeb23a4cSdrh           if( flag_longlong ){
321eeb23a4cSdrh             v = va_arg(ap,i64);
322eeb23a4cSdrh           }else if( flag_long ){
323eeb23a4cSdrh             v = va_arg(ap,long int);
324eeb23a4cSdrh           }else{
325eeb23a4cSdrh             v = va_arg(ap,int);
326eeb23a4cSdrh           }
327e9707671Sdrh           if( v<0 ){
328158b9cb9Sdrh             if( v==SMALLEST_INT64 ){
329158b9cb9Sdrh               longvalue = ((u64)1)<<63;
330158b9cb9Sdrh             }else{
331158b9cb9Sdrh               longvalue = -v;
332158b9cb9Sdrh             }
333cfcdaefeSdanielk1977             prefix = '-';
334cfcdaefeSdanielk1977           }else{
335e9707671Sdrh             longvalue = v;
336e9707671Sdrh             if( flag_plussign )        prefix = '+';
337a18c5681Sdrh             else if( flag_blanksign )  prefix = ' ';
338a18c5681Sdrh             else                       prefix = 0;
339cfcdaefeSdanielk1977           }
340e9707671Sdrh         }else{
341eeb23a4cSdrh           if( flag_longlong ){
342eeb23a4cSdrh             longvalue = va_arg(ap,u64);
343eeb23a4cSdrh           }else if( flag_long ){
344eeb23a4cSdrh             longvalue = va_arg(ap,unsigned long int);
345eeb23a4cSdrh           }else{
346eeb23a4cSdrh             longvalue = va_arg(ap,unsigned int);
347eeb23a4cSdrh           }
348e9707671Sdrh           prefix = 0;
349e9707671Sdrh         }
350e9707671Sdrh         if( longvalue==0 ) flag_alternateform = 0;
351a18c5681Sdrh         if( flag_zeropad && precision<width-(prefix!=0) ){
352a18c5681Sdrh           precision = width-(prefix!=0);
353a18c5681Sdrh         }
35459eedf79Sdrh         if( precision<etBUFSIZE-10 ){
35559eedf79Sdrh           nOut = etBUFSIZE;
35659eedf79Sdrh           zOut = buf;
35759eedf79Sdrh         }else{
35859eedf79Sdrh           nOut = precision + 10;
35959eedf79Sdrh           zOut = zExtra = sqlite3Malloc( nOut );
36059eedf79Sdrh           if( zOut==0 ){
36159eedf79Sdrh             pAccum->mallocFailed = 1;
36259eedf79Sdrh             return;
36359eedf79Sdrh           }
36459eedf79Sdrh         }
36559eedf79Sdrh         bufpt = &zOut[nOut-1];
3669a99334dSdrh         if( xtype==etORDINAL ){
36743f6e064Sdrh           static const char zOrd[] = "thstndrd";
368ea678832Sdrh           int x = (int)(longvalue % 10);
36943f6e064Sdrh           if( x>=4 || (longvalue/10)%10==1 ){
37043f6e064Sdrh             x = 0;
37143f6e064Sdrh           }
37259eedf79Sdrh           *(--bufpt) = zOrd[x*2+1];
37359eedf79Sdrh           *(--bufpt) = zOrd[x*2];
3749a99334dSdrh         }
375a18c5681Sdrh         {
37676ff3a0eSdrh           register const char *cset;      /* Use registers for speed */
377a18c5681Sdrh           register int base;
37876ff3a0eSdrh           cset = &aDigits[infop->charset];
379a18c5681Sdrh           base = infop->base;
380a18c5681Sdrh           do{                                           /* Convert to ascii */
381a18c5681Sdrh             *(--bufpt) = cset[longvalue%base];
382a18c5681Sdrh             longvalue = longvalue/base;
383a18c5681Sdrh           }while( longvalue>0 );
384a18c5681Sdrh         }
38559eedf79Sdrh         length = (int)(&zOut[nOut-1]-bufpt);
386a18c5681Sdrh         for(idx=precision-length; idx>0; idx--){
387a18c5681Sdrh           *(--bufpt) = '0';                             /* Zero pad */
388a18c5681Sdrh         }
389a18c5681Sdrh         if( prefix ) *(--bufpt) = prefix;               /* Add sign */
390a18c5681Sdrh         if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
39176ff3a0eSdrh           const char *pre;
39276ff3a0eSdrh           char x;
39376ff3a0eSdrh           pre = &aPrefix[infop->prefix];
39476ff3a0eSdrh           for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
395a18c5681Sdrh         }
39659eedf79Sdrh         length = (int)(&zOut[nOut-1]-bufpt);
397a18c5681Sdrh         break;
398a18c5681Sdrh       case etFLOAT:
399a18c5681Sdrh       case etEXP:
400a18c5681Sdrh       case etGENERIC:
401a18c5681Sdrh         realvalue = va_arg(ap,double);
40269ef7036Sdrh #ifdef SQLITE_OMIT_FLOATING_POINT
40369ef7036Sdrh         length = 0;
40469ef7036Sdrh #else
405a18c5681Sdrh         if( precision<0 ) precision = 6;         /* Set default precision */
406a18c5681Sdrh         if( realvalue<0.0 ){
407a18c5681Sdrh           realvalue = -realvalue;
408a18c5681Sdrh           prefix = '-';
409a18c5681Sdrh         }else{
410a18c5681Sdrh           if( flag_plussign )          prefix = '+';
411a18c5681Sdrh           else if( flag_blanksign )    prefix = ' ';
412a18c5681Sdrh           else                         prefix = 0;
413a18c5681Sdrh         }
4143e9aeec0Sdrh         if( xtype==etGENERIC && precision>0 ) precision--;
4155f968436Sdrh #if 0
416a18c5681Sdrh         /* Rounding works like BSD when the constant 0.4999 is used.  Wierd! */
417a18c5681Sdrh         for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
418a18c5681Sdrh #else
419a18c5681Sdrh         /* It makes more sense to use 0.5 */
42074161705Sdrh         for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
421a18c5681Sdrh #endif
4223e9aeec0Sdrh         if( xtype==etFLOAT ) realvalue += rounder;
423a18c5681Sdrh         /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
424a18c5681Sdrh         exp = 0;
425ea678832Sdrh         if( sqlite3IsNaN((double)realvalue) ){
42653c14021Sdrh           bufpt = "NaN";
42753c14021Sdrh           length = 3;
42853c14021Sdrh           break;
42953c14021Sdrh         }
430a18c5681Sdrh         if( realvalue>0.0 ){
43163782855Sdrh           while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
43263782855Sdrh           while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
43363782855Sdrh           while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
434af005fbcSdrh           while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
435af005fbcSdrh           while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
436af005fbcSdrh           if( exp>350 ){
43753c14021Sdrh             if( prefix=='-' ){
43853c14021Sdrh               bufpt = "-Inf";
43953c14021Sdrh             }else if( prefix=='+' ){
44053c14021Sdrh               bufpt = "+Inf";
44153c14021Sdrh             }else{
44253c14021Sdrh               bufpt = "Inf";
44353c14021Sdrh             }
444ea678832Sdrh             length = sqlite3Strlen30(bufpt);
445a18c5681Sdrh             break;
446a18c5681Sdrh           }
447a18c5681Sdrh         }
448a18c5681Sdrh         bufpt = buf;
449a18c5681Sdrh         /*
450a18c5681Sdrh         ** If the field type is etGENERIC, then convert to either etEXP
451a18c5681Sdrh         ** or etFLOAT, as appropriate.
452a18c5681Sdrh         */
453a18c5681Sdrh         if( xtype!=etFLOAT ){
454a18c5681Sdrh           realvalue += rounder;
455a18c5681Sdrh           if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
456a18c5681Sdrh         }
457a18c5681Sdrh         if( xtype==etGENERIC ){
458a18c5681Sdrh           flag_rtz = !flag_alternateform;
459a18c5681Sdrh           if( exp<-4 || exp>precision ){
460a18c5681Sdrh             xtype = etEXP;
461a18c5681Sdrh           }else{
462a18c5681Sdrh             precision = precision - exp;
463a18c5681Sdrh             xtype = etFLOAT;
464a18c5681Sdrh           }
465a18c5681Sdrh         }else{
466a18c5681Sdrh           flag_rtz = 0;
467a18c5681Sdrh         }
468557cc60fSdrh         if( xtype==etEXP ){
469557cc60fSdrh           e2 = 0;
470557cc60fSdrh         }else{
471557cc60fSdrh           e2 = exp;
472557cc60fSdrh         }
47359eedf79Sdrh         if( e2+precision+width > etBUFSIZE - 15 ){
47459eedf79Sdrh           bufpt = zExtra = sqlite3Malloc( e2+precision+width+15 );
47559eedf79Sdrh           if( bufpt==0 ){
47659eedf79Sdrh             pAccum->mallocFailed = 1;
47759eedf79Sdrh             return;
47859eedf79Sdrh           }
47959eedf79Sdrh         }
48059eedf79Sdrh         zOut = bufpt;
481a18c5681Sdrh         nsd = 0;
482ea678832Sdrh         flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
483557cc60fSdrh         /* The sign in front of the number */
484557cc60fSdrh         if( prefix ){
485557cc60fSdrh           *(bufpt++) = prefix;
486557cc60fSdrh         }
487557cc60fSdrh         /* Digits prior to the decimal point */
488557cc60fSdrh         if( e2<0 ){
489557cc60fSdrh           *(bufpt++) = '0';
490557cc60fSdrh         }else{
491557cc60fSdrh           for(; e2>=0; e2--){
492557cc60fSdrh             *(bufpt++) = et_getdigit(&realvalue,&nsd);
493557cc60fSdrh           }
494557cc60fSdrh         }
495557cc60fSdrh         /* The decimal point */
496557cc60fSdrh         if( flag_dp ){
497557cc60fSdrh           *(bufpt++) = '.';
498557cc60fSdrh         }
499557cc60fSdrh         /* "0" digits after the decimal point but before the first
500557cc60fSdrh         ** significant digit of the number */
501af005fbcSdrh         for(e2++; e2<0; precision--, e2++){
502af005fbcSdrh           assert( precision>0 );
503a18c5681Sdrh           *(bufpt++) = '0';
504a18c5681Sdrh         }
505557cc60fSdrh         /* Significant digits after the decimal point */
506557cc60fSdrh         while( (precision--)>0 ){
507557cc60fSdrh           *(bufpt++) = et_getdigit(&realvalue,&nsd);
508a18c5681Sdrh         }
509557cc60fSdrh         /* Remove trailing zeros and the "." if no digits follow the "." */
510557cc60fSdrh         if( flag_rtz && flag_dp ){
5113e9aeec0Sdrh           while( bufpt[-1]=='0' ) *(--bufpt) = 0;
51259eedf79Sdrh           assert( bufpt>zOut );
5133e9aeec0Sdrh           if( bufpt[-1]=='.' ){
514557cc60fSdrh             if( flag_altform2 ){
515557cc60fSdrh               *(bufpt++) = '0';
516557cc60fSdrh             }else{
517557cc60fSdrh               *(--bufpt) = 0;
518a18c5681Sdrh             }
519557cc60fSdrh           }
520557cc60fSdrh         }
521557cc60fSdrh         /* Add the "eNNN" suffix */
52259eedf79Sdrh         if( xtype==etEXP ){
52376ff3a0eSdrh           *(bufpt++) = aDigits[infop->charset];
524557cc60fSdrh           if( exp<0 ){
525557cc60fSdrh             *(bufpt++) = '-'; exp = -exp;
526557cc60fSdrh           }else{
527557cc60fSdrh             *(bufpt++) = '+';
528557cc60fSdrh           }
529a18c5681Sdrh           if( exp>=100 ){
530ea678832Sdrh             *(bufpt++) = (char)((exp/100)+'0');        /* 100's digit */
531a18c5681Sdrh             exp %= 100;
532a18c5681Sdrh           }
533ea678832Sdrh           *(bufpt++) = (char)(exp/10+'0');             /* 10's digit */
534ea678832Sdrh           *(bufpt++) = (char)(exp%10+'0');             /* 1's digit */
535a18c5681Sdrh         }
536557cc60fSdrh         *bufpt = 0;
537557cc60fSdrh 
538a18c5681Sdrh         /* The converted number is in buf[] and zero terminated. Output it.
539a18c5681Sdrh         ** Note that the number is in the usual order, not reversed as with
540a18c5681Sdrh         ** integer conversions. */
54159eedf79Sdrh         length = (int)(bufpt-zOut);
54259eedf79Sdrh         bufpt = zOut;
543a18c5681Sdrh 
544a18c5681Sdrh         /* Special case:  Add leading zeros if the flag_zeropad flag is
545a18c5681Sdrh         ** set and we are not left justified */
546a18c5681Sdrh         if( flag_zeropad && !flag_leftjustify && length < width){
547a18c5681Sdrh           int i;
548a18c5681Sdrh           int nPad = width - length;
549a18c5681Sdrh           for(i=width; i>=nPad; i--){
550a18c5681Sdrh             bufpt[i] = bufpt[i-nPad];
551a18c5681Sdrh           }
552a18c5681Sdrh           i = prefix!=0;
553a18c5681Sdrh           while( nPad-- ) bufpt[i++] = '0';
554a18c5681Sdrh           length = width;
555a18c5681Sdrh         }
55669ef7036Sdrh #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
557a18c5681Sdrh         break;
558a18c5681Sdrh       case etSIZE:
559ade86483Sdrh         *(va_arg(ap,int*)) = pAccum->nChar;
560a18c5681Sdrh         length = width = 0;
561a18c5681Sdrh         break;
562a18c5681Sdrh       case etPERCENT:
563a18c5681Sdrh         buf[0] = '%';
564a18c5681Sdrh         bufpt = buf;
565a18c5681Sdrh         length = 1;
566a18c5681Sdrh         break;
567a18c5681Sdrh       case etCHARX:
568ea678832Sdrh         c = va_arg(ap,int);
569ea678832Sdrh         buf[0] = (char)c;
570a18c5681Sdrh         if( precision>=0 ){
571ea678832Sdrh           for(idx=1; idx<precision; idx++) buf[idx] = (char)c;
572a18c5681Sdrh           length = precision;
573a18c5681Sdrh         }else{
574a18c5681Sdrh           length =1;
575a18c5681Sdrh         }
576a18c5681Sdrh         bufpt = buf;
577a18c5681Sdrh         break;
578a18c5681Sdrh       case etSTRING:
579d93d8a81Sdrh       case etDYNSTRING:
580cb485882Sdrh         bufpt = va_arg(ap,char*);
581d93d8a81Sdrh         if( bufpt==0 ){
582d93d8a81Sdrh           bufpt = "";
583d93d8a81Sdrh         }else if( xtype==etDYNSTRING ){
584d93d8a81Sdrh           zExtra = bufpt;
585d93d8a81Sdrh         }
586e509094bSdrh         if( precision>=0 ){
587e509094bSdrh           for(length=0; length<precision && bufpt[length]; length++){}
588e509094bSdrh         }else{
589ea678832Sdrh           length = sqlite3Strlen30(bufpt);
590e509094bSdrh         }
591a18c5681Sdrh         break;
592a18c5681Sdrh       case etSQLESCAPE:
593f3b863edSdanielk1977       case etSQLESCAPE2:
594f3b863edSdanielk1977       case etSQLESCAPE3: {
5958965b50eSdrh         int i, j, k, n, isnull;
5964794f735Sdrh         int needQuote;
597ea678832Sdrh         char ch;
598f3b863edSdanielk1977         char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
599f0113000Sdanielk1977         char *escarg = va_arg(ap,char*);
600f0113000Sdanielk1977         isnull = escarg==0;
601f0113000Sdanielk1977         if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
6028965b50eSdrh         k = precision;
60360d4a304Sdan         for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
604f3b863edSdanielk1977           if( ch==q )  n++;
605a18c5681Sdrh         }
6064794f735Sdrh         needQuote = !isnull && xtype==etSQLESCAPE2;
6074794f735Sdrh         n += i + 1 + needQuote*2;
608a18c5681Sdrh         if( n>etBUFSIZE ){
609e5ae5735Sdrh           bufpt = zExtra = sqlite3Malloc( n );
610d164fd34Sdrh           if( bufpt==0 ){
611d164fd34Sdrh             pAccum->mallocFailed = 1;
612d164fd34Sdrh             return;
613d164fd34Sdrh           }
614a18c5681Sdrh         }else{
615a18c5681Sdrh           bufpt = buf;
616a18c5681Sdrh         }
6170cfcf3fbSchw         j = 0;
618f3b863edSdanielk1977         if( needQuote ) bufpt[j++] = q;
6198965b50eSdrh         k = i;
6208965b50eSdrh         for(i=0; i<k; i++){
6218965b50eSdrh           bufpt[j++] = ch = escarg[i];
622f3b863edSdanielk1977           if( ch==q ) bufpt[j++] = ch;
623a18c5681Sdrh         }
624f3b863edSdanielk1977         if( needQuote ) bufpt[j++] = q;
625a18c5681Sdrh         bufpt[j] = 0;
626a18c5681Sdrh         length = j;
6278965b50eSdrh         /* The precision in %q and %Q means how many input characters to
6288965b50eSdrh         ** consume, not the length of the output...
6298965b50eSdrh         ** if( precision>=0 && precision<length ) length = precision; */
630a18c5681Sdrh         break;
6315eba8c09Sdrh       }
6325f968436Sdrh       case etTOKEN: {
6335f968436Sdrh         Token *pToken = va_arg(ap, Token*);
634af005fbcSdrh         if( pToken ){
635ade86483Sdrh           sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
636ad6d9460Sdrh         }
6375f968436Sdrh         length = width = 0;
6385f968436Sdrh         break;
6395f968436Sdrh       }
6405f968436Sdrh       case etSRCLIST: {
6415f968436Sdrh         SrcList *pSrc = va_arg(ap, SrcList*);
6425f968436Sdrh         int k = va_arg(ap, int);
6435f968436Sdrh         struct SrcList_item *pItem = &pSrc->a[k];
6445f968436Sdrh         assert( k>=0 && k<pSrc->nSrc );
64593a960a0Sdrh         if( pItem->zDatabase ){
646ade86483Sdrh           sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1);
647ade86483Sdrh           sqlite3StrAccumAppend(pAccum, ".", 1);
6485f968436Sdrh         }
649ade86483Sdrh         sqlite3StrAccumAppend(pAccum, pItem->zName, -1);
6505f968436Sdrh         length = width = 0;
6515f968436Sdrh         break;
6525f968436Sdrh       }
653874ba04cSdrh       default: {
654874ba04cSdrh         assert( xtype==etINVALID );
655874ba04cSdrh         return;
656874ba04cSdrh       }
657a18c5681Sdrh     }/* End switch over the format type */
658a18c5681Sdrh     /*
659a18c5681Sdrh     ** The text of the conversion is pointed to by "bufpt" and is
660a18c5681Sdrh     ** "length" characters long.  The field width is "width".  Do
661a18c5681Sdrh     ** the output.
662a18c5681Sdrh     */
663a18c5681Sdrh     if( !flag_leftjustify ){
664a18c5681Sdrh       register int nspace;
665a18c5681Sdrh       nspace = width-length;
666a18c5681Sdrh       if( nspace>0 ){
667*7e02e5e6Sdrh         sqlite3AppendSpace(pAccum, nspace);
668a18c5681Sdrh       }
669a18c5681Sdrh     }
670a18c5681Sdrh     if( length>0 ){
671ade86483Sdrh       sqlite3StrAccumAppend(pAccum, bufpt, length);
672a18c5681Sdrh     }
673a18c5681Sdrh     if( flag_leftjustify ){
674a18c5681Sdrh       register int nspace;
675a18c5681Sdrh       nspace = width-length;
676a18c5681Sdrh       if( nspace>0 ){
677*7e02e5e6Sdrh         sqlite3AppendSpace(pAccum, nspace);
678a18c5681Sdrh       }
679a18c5681Sdrh     }
6801e536953Sdanielk1977     sqlite3_free(zExtra);
681a18c5681Sdrh   }/* End for loop over the format string */
682a18c5681Sdrh } /* End of function */
683a18c5681Sdrh 
684a18c5681Sdrh /*
685ade86483Sdrh ** Append N bytes of text from z to the StrAccum object.
686a18c5681Sdrh */
687ade86483Sdrh void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
688bc6160b0Sdrh   assert( z!=0 || N==0 );
689ade86483Sdrh   if( p->tooBig | p->mallocFailed ){
690bc6160b0Sdrh     testcase(p->tooBig);
691bc6160b0Sdrh     testcase(p->mallocFailed);
69217435752Sdrh     return;
693ade86483Sdrh   }
694b07028f7Sdrh   assert( p->zText!=0 || p->nChar==0 );
695ade86483Sdrh   if( N<0 ){
696ea678832Sdrh     N = sqlite3Strlen30(z);
697ade86483Sdrh   }
698bc6160b0Sdrh   if( N==0 || NEVER(z==0) ){
699ade86483Sdrh     return;
700ade86483Sdrh   }
701ade86483Sdrh   if( p->nChar+N >= p->nAlloc ){
702ade86483Sdrh     char *zNew;
703ade86483Sdrh     if( !p->useMalloc ){
704ade86483Sdrh       p->tooBig = 1;
705ade86483Sdrh       N = p->nAlloc - p->nChar - 1;
706ade86483Sdrh       if( N<=0 ){
707ade86483Sdrh         return;
7085f968436Sdrh       }
709a18c5681Sdrh     }else{
710a9ef7097Sdan       char *zOld = (p->zText==p->zBase ? 0 : p->zText);
71193a960a0Sdrh       i64 szNew = p->nChar;
712b1a6c3c1Sdrh       szNew += N + 1;
713b1a6c3c1Sdrh       if( szNew > p->mxAlloc ){
714ade86483Sdrh         sqlite3StrAccumReset(p);
715ade86483Sdrh         p->tooBig = 1;
716eaad32b1Sdrh         return;
717b1a6c3c1Sdrh       }else{
718ea678832Sdrh         p->nAlloc = (int)szNew;
719a18c5681Sdrh       }
720b975598eSdrh       if( p->useMalloc==1 ){
721a9ef7097Sdan         zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
722b975598eSdrh       }else{
723a9ef7097Sdan         zNew = sqlite3_realloc(zOld, p->nAlloc);
724b975598eSdrh       }
72553f733c7Sdrh       if( zNew ){
726b07028f7Sdrh         if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
727ade86483Sdrh         p->zText = zNew;
728ade86483Sdrh       }else{
729ade86483Sdrh         p->mallocFailed = 1;
730ade86483Sdrh         sqlite3StrAccumReset(p);
731ade86483Sdrh         return;
73253f733c7Sdrh       }
7335f968436Sdrh     }
7345f968436Sdrh   }
735b07028f7Sdrh   assert( p->zText );
736ade86483Sdrh   memcpy(&p->zText[p->nChar], z, N);
737ade86483Sdrh   p->nChar += N;
738483750baSdrh }
739483750baSdrh 
740483750baSdrh /*
741ade86483Sdrh ** Finish off a string by making sure it is zero-terminated.
742ade86483Sdrh ** Return a pointer to the resulting string.  Return a NULL
743ade86483Sdrh ** pointer if any kind of error was encountered.
7445f968436Sdrh */
745ade86483Sdrh char *sqlite3StrAccumFinish(StrAccum *p){
746ade86483Sdrh   if( p->zText ){
747ade86483Sdrh     p->zText[p->nChar] = 0;
748ade86483Sdrh     if( p->useMalloc && p->zText==p->zBase ){
749b975598eSdrh       if( p->useMalloc==1 ){
750633e6d57Sdrh         p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
751b975598eSdrh       }else{
752b975598eSdrh         p->zText = sqlite3_malloc(p->nChar+1);
753b975598eSdrh       }
754ade86483Sdrh       if( p->zText ){
755ade86483Sdrh         memcpy(p->zText, p->zBase, p->nChar+1);
756ade86483Sdrh       }else{
757ade86483Sdrh         p->mallocFailed = 1;
758ade86483Sdrh       }
759ade86483Sdrh     }
760ade86483Sdrh   }
761ade86483Sdrh   return p->zText;
762ade86483Sdrh }
763ade86483Sdrh 
764ade86483Sdrh /*
765ade86483Sdrh ** Reset an StrAccum string.  Reclaim all malloced memory.
766ade86483Sdrh */
767ade86483Sdrh void sqlite3StrAccumReset(StrAccum *p){
768ade86483Sdrh   if( p->zText!=p->zBase ){
769b975598eSdrh     if( p->useMalloc==1 ){
770633e6d57Sdrh       sqlite3DbFree(p->db, p->zText);
771b975598eSdrh     }else{
772b975598eSdrh       sqlite3_free(p->zText);
773b975598eSdrh     }
774ade86483Sdrh   }
775f089aa45Sdrh   p->zText = 0;
776ade86483Sdrh }
777ade86483Sdrh 
778ade86483Sdrh /*
779ade86483Sdrh ** Initialize a string accumulator
780ade86483Sdrh */
781f089aa45Sdrh void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
782ade86483Sdrh   p->zText = p->zBase = zBase;
783633e6d57Sdrh   p->db = 0;
784ade86483Sdrh   p->nChar = 0;
785ade86483Sdrh   p->nAlloc = n;
786bb4957f8Sdrh   p->mxAlloc = mx;
787ade86483Sdrh   p->useMalloc = 1;
788ade86483Sdrh   p->tooBig = 0;
789ade86483Sdrh   p->mallocFailed = 0;
7905f968436Sdrh }
7915f968436Sdrh 
7925f968436Sdrh /*
7935f968436Sdrh ** Print into memory obtained from sqliteMalloc().  Use the internal
7945f968436Sdrh ** %-conversion extensions.
7955f968436Sdrh */
79617435752Sdrh char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
79717435752Sdrh   char *z;
79879158e18Sdrh   char zBase[SQLITE_PRINT_BUF_SIZE];
799ade86483Sdrh   StrAccum acc;
800bc6160b0Sdrh   assert( db!=0 );
801bb4957f8Sdrh   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
802bc6160b0Sdrh                       db->aLimit[SQLITE_LIMIT_LENGTH]);
803633e6d57Sdrh   acc.db = db;
804f089aa45Sdrh   sqlite3VXPrintf(&acc, 1, zFormat, ap);
805ade86483Sdrh   z = sqlite3StrAccumFinish(&acc);
806bc6160b0Sdrh   if( acc.mallocFailed ){
80717435752Sdrh     db->mallocFailed = 1;
80817435752Sdrh   }
80917435752Sdrh   return z;
8105f968436Sdrh }
8115f968436Sdrh 
8125f968436Sdrh /*
8135f968436Sdrh ** Print into memory obtained from sqliteMalloc().  Use the internal
8145f968436Sdrh ** %-conversion extensions.
8155f968436Sdrh */
81617435752Sdrh char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
8175f968436Sdrh   va_list ap;
8185f968436Sdrh   char *z;
8195f968436Sdrh   va_start(ap, zFormat);
820ade86483Sdrh   z = sqlite3VMPrintf(db, zFormat, ap);
8215f968436Sdrh   va_end(ap);
8225f968436Sdrh   return z;
8235f968436Sdrh }
8245f968436Sdrh 
8255f968436Sdrh /*
826633e6d57Sdrh ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
827633e6d57Sdrh ** the string and before returnning.  This routine is intended to be used
828633e6d57Sdrh ** to modify an existing string.  For example:
829633e6d57Sdrh **
830633e6d57Sdrh **       x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
831633e6d57Sdrh **
832633e6d57Sdrh */
833633e6d57Sdrh char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
834633e6d57Sdrh   va_list ap;
835633e6d57Sdrh   char *z;
836633e6d57Sdrh   va_start(ap, zFormat);
837633e6d57Sdrh   z = sqlite3VMPrintf(db, zFormat, ap);
838633e6d57Sdrh   va_end(ap);
839633e6d57Sdrh   sqlite3DbFree(db, zStr);
840633e6d57Sdrh   return z;
841633e6d57Sdrh }
842633e6d57Sdrh 
843633e6d57Sdrh /*
84428dd479cSdrh ** Print into memory obtained from sqlite3_malloc().  Omit the internal
84528dd479cSdrh ** %-conversion extensions.
84628dd479cSdrh */
84728dd479cSdrh char *sqlite3_vmprintf(const char *zFormat, va_list ap){
848ade86483Sdrh   char *z;
84928dd479cSdrh   char zBase[SQLITE_PRINT_BUF_SIZE];
850ade86483Sdrh   StrAccum acc;
851ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT
852ff1590eeSdrh   if( sqlite3_initialize() ) return 0;
853ff1590eeSdrh #endif
854bb4957f8Sdrh   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
855b975598eSdrh   acc.useMalloc = 2;
856f089aa45Sdrh   sqlite3VXPrintf(&acc, 0, zFormat, ap);
857ade86483Sdrh   z = sqlite3StrAccumFinish(&acc);
858ade86483Sdrh   return z;
85928dd479cSdrh }
86028dd479cSdrh 
86128dd479cSdrh /*
86228dd479cSdrh ** Print into memory obtained from sqlite3_malloc()().  Omit the internal
86328dd479cSdrh ** %-conversion extensions.
864483750baSdrh */
8656f8a503dSdanielk1977 char *sqlite3_mprintf(const char *zFormat, ...){
866a18c5681Sdrh   va_list ap;
8675f968436Sdrh   char *z;
868ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT
869ff1590eeSdrh   if( sqlite3_initialize() ) return 0;
870ff1590eeSdrh #endif
871a18c5681Sdrh   va_start(ap, zFormat);
872b3738b6cSdrh   z = sqlite3_vmprintf(zFormat, ap);
873a18c5681Sdrh   va_end(ap);
8745f968436Sdrh   return z;
875a18c5681Sdrh }
876a18c5681Sdrh 
877a18c5681Sdrh /*
8786f8a503dSdanielk1977 ** sqlite3_snprintf() works like snprintf() except that it ignores the
87993a5c6bdSdrh ** current locale settings.  This is important for SQLite because we
88093a5c6bdSdrh ** are not able to use a "," as the decimal point in place of "." as
88193a5c6bdSdrh ** specified by some locales.
882db26d4c9Sdrh **
883db26d4c9Sdrh ** Oops:  The first two arguments of sqlite3_snprintf() are backwards
884db26d4c9Sdrh ** from the snprintf() standard.  Unfortunately, it is too late to change
885db26d4c9Sdrh ** this without breaking compatibility, so we just have to live with the
886db26d4c9Sdrh ** mistake.
887db26d4c9Sdrh **
888db26d4c9Sdrh ** sqlite3_vsnprintf() is the varargs version.
88993a5c6bdSdrh */
890db26d4c9Sdrh char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
891db26d4c9Sdrh   StrAccum acc;
892db26d4c9Sdrh   if( n<=0 ) return zBuf;
893db26d4c9Sdrh   sqlite3StrAccumInit(&acc, zBuf, n, 0);
894db26d4c9Sdrh   acc.useMalloc = 0;
895db26d4c9Sdrh   sqlite3VXPrintf(&acc, 0, zFormat, ap);
896db26d4c9Sdrh   return sqlite3StrAccumFinish(&acc);
897db26d4c9Sdrh }
8986f8a503dSdanielk1977 char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
8995f968436Sdrh   char *z;
90093a5c6bdSdrh   va_list ap;
90193a5c6bdSdrh   va_start(ap,zFormat);
902db26d4c9Sdrh   z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
90393a5c6bdSdrh   va_end(ap);
9045f968436Sdrh   return z;
90593a5c6bdSdrh }
90693a5c6bdSdrh 
9073f280701Sdrh /*
9087c0c460fSdrh ** This is the routine that actually formats the sqlite3_log() message.
9097c0c460fSdrh ** We house it in a separate routine from sqlite3_log() to avoid using
9107c0c460fSdrh ** stack space on small-stack systems when logging is disabled.
9117c0c460fSdrh **
9127c0c460fSdrh ** sqlite3_log() must render into a static buffer.  It cannot dynamically
9137c0c460fSdrh ** allocate memory because it might be called while the memory allocator
9147c0c460fSdrh ** mutex is held.
9157c0c460fSdrh */
9167c0c460fSdrh static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
9177c0c460fSdrh   StrAccum acc;                          /* String accumulator */
918a64fa912Sdrh   char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
9197c0c460fSdrh 
9207c0c460fSdrh   sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0);
9217c0c460fSdrh   acc.useMalloc = 0;
9227c0c460fSdrh   sqlite3VXPrintf(&acc, 0, zFormat, ap);
9237c0c460fSdrh   sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
9247c0c460fSdrh                            sqlite3StrAccumFinish(&acc));
9257c0c460fSdrh }
9267c0c460fSdrh 
9277c0c460fSdrh /*
9283f280701Sdrh ** Format and write a message to the log if logging is enabled.
9293f280701Sdrh */
930a7564663Sdrh void sqlite3_log(int iErrCode, const char *zFormat, ...){
9313f280701Sdrh   va_list ap;                             /* Vararg list */
9327c0c460fSdrh   if( sqlite3GlobalConfig.xLog ){
9333f280701Sdrh     va_start(ap, zFormat);
9347c0c460fSdrh     renderLogMsg(iErrCode, zFormat, ap);
9353f280701Sdrh     va_end(ap);
9363f280701Sdrh   }
9373f280701Sdrh }
9383f280701Sdrh 
93985e9e22bSdrh #if defined(SQLITE_DEBUG)
940e54ca3feSdrh /*
941e54ca3feSdrh ** A version of printf() that understands %lld.  Used for debugging.
942e54ca3feSdrh ** The printf() built into some versions of windows does not understand %lld
943e54ca3feSdrh ** and segfaults if you give it a long long int.
944e54ca3feSdrh */
945e54ca3feSdrh void sqlite3DebugPrintf(const char *zFormat, ...){
946e54ca3feSdrh   va_list ap;
947ade86483Sdrh   StrAccum acc;
948e54ca3feSdrh   char zBuf[500];
949bb4957f8Sdrh   sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
950ade86483Sdrh   acc.useMalloc = 0;
951e54ca3feSdrh   va_start(ap,zFormat);
952f089aa45Sdrh   sqlite3VXPrintf(&acc, 0, zFormat, ap);
953e54ca3feSdrh   va_end(ap);
954bed8e7e5Sdrh   sqlite3StrAccumFinish(&acc);
955485f0039Sdrh   fprintf(stdout,"%s", zBuf);
9562ac3ee97Sdrh   fflush(stdout);
957e54ca3feSdrh }
958e54ca3feSdrh #endif
959c7bc4fdeSdrh 
960c7bc4fdeSdrh #ifndef SQLITE_OMIT_TRACE
961c7bc4fdeSdrh /*
962c7bc4fdeSdrh ** variable-argument wrapper around sqlite3VXPrintf().
963c7bc4fdeSdrh */
964c7bc4fdeSdrh void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){
965c7bc4fdeSdrh   va_list ap;
966c7bc4fdeSdrh   va_start(ap,zFormat);
967c7bc4fdeSdrh   sqlite3VXPrintf(p, 1, zFormat, ap);
968c7bc4fdeSdrh   va_end(ap);
969c7bc4fdeSdrh }
970c7bc4fdeSdrh #endif
971