xref: /sqlite-3.40.0/src/printf.c (revision 40f22bed)
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;
12772b3fbc7Sdrh   if( (*cnt)<=0 ) return '0';
12872b3fbc7Sdrh   (*cnt)--;
129a18c5681Sdrh   digit = (int)*val;
130a18c5681Sdrh   d = digit;
131a18c5681Sdrh   digit += '0';
132a18c5681Sdrh   *val = (*val - d)*10.0;
133ea678832Sdrh   return (char)digit;
134a18c5681Sdrh }
135b37df7b9Sdrh #endif /* SQLITE_OMIT_FLOATING_POINT */
136a18c5681Sdrh 
13779158e18Sdrh /*
138ade86483Sdrh ** Append N space characters to the given string buffer.
139ade86483Sdrh */
1407e02e5e6Sdrh void sqlite3AppendSpace(StrAccum *pAccum, int N){
141ade86483Sdrh   static const char zSpaces[] = "                             ";
14200e13613Sdanielk1977   while( N>=(int)sizeof(zSpaces)-1 ){
143ade86483Sdrh     sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
144ade86483Sdrh     N -= sizeof(zSpaces)-1;
145ade86483Sdrh   }
146ade86483Sdrh   if( N>0 ){
147ade86483Sdrh     sqlite3StrAccumAppend(pAccum, zSpaces, N);
148ade86483Sdrh   }
149ade86483Sdrh }
150ade86483Sdrh 
151ade86483Sdrh /*
152a6353a3fSdrh ** Set the StrAccum object to an error mode.
153a6353a3fSdrh */
154a6353a3fSdrh void setStrAccumError(StrAccum *p, u8 eError){
155a6353a3fSdrh   p->accError = eError;
156a6353a3fSdrh   p->nAlloc = 0;
157a6353a3fSdrh }
158a6353a3fSdrh 
159a6353a3fSdrh /*
16079158e18Sdrh ** On machines with a small stack size, you can redefine the
161ed1fddf4Sdrh ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
16279158e18Sdrh */
16379158e18Sdrh #ifndef SQLITE_PRINT_BUF_SIZE
16459eedf79Sdrh # define SQLITE_PRINT_BUF_SIZE 70
16550d654daSdrh #endif
16679158e18Sdrh #define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
167a18c5681Sdrh 
168a18c5681Sdrh /*
169ed1fddf4Sdrh ** Render a string given by "fmt" into the StrAccum object.
170a18c5681Sdrh */
171f089aa45Sdrh void sqlite3VXPrintf(
172ade86483Sdrh   StrAccum *pAccum,                  /* Accumulate results here */
1735f968436Sdrh   int useExtended,                   /* Allow extended %-conversions */
1745f968436Sdrh   const char *fmt,                   /* Format string */
1755f968436Sdrh   va_list ap                         /* arguments */
176a18c5681Sdrh ){
177e84a306bSdrh   int c;                     /* Next character in the format string */
178e84a306bSdrh   char *bufpt;               /* Pointer to the conversion buffer */
179e84a306bSdrh   int precision;             /* Precision of the current field */
180e84a306bSdrh   int length;                /* Length of the field */
181e84a306bSdrh   int idx;                   /* A general purpose loop counter */
182a18c5681Sdrh   int width;                 /* Width of the current field */
183e84a306bSdrh   etByte flag_leftjustify;   /* True if "-" flag is present */
184e84a306bSdrh   etByte flag_plussign;      /* True if "+" flag is present */
185e84a306bSdrh   etByte flag_blanksign;     /* True if " " flag is present */
186e84a306bSdrh   etByte flag_alternateform; /* True if "#" flag is present */
187531fe878Sdrh   etByte flag_altform2;      /* True if "!" flag is present */
188e84a306bSdrh   etByte flag_zeropad;       /* True if field width constant starts with zero */
189e84a306bSdrh   etByte flag_long;          /* True if "l" flag is present */
190a34b6764Sdrh   etByte flag_longlong;      /* True if the "ll" flag is present */
1913e9aeec0Sdrh   etByte done;               /* Loop termination flag */
192ed1fddf4Sdrh   etByte xtype = 0;          /* Conversion paradigm */
193ed1fddf4Sdrh   char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
19427436af7Sdrh   sqlite_uint64 longvalue;   /* Value for integer types */
195384eef32Sdrh   LONGDOUBLE_TYPE realvalue; /* Value for real types */
1965719628aSdrh   const et_info *infop;      /* Pointer to the appropriate info structure */
19759eedf79Sdrh   char *zOut;                /* Rendering buffer */
19859eedf79Sdrh   int nOut;                  /* Size of the rendering buffer */
199ed1fddf4Sdrh   char *zExtra;              /* Malloced memory used by some conversion */
200b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT
201557cc60fSdrh   int  exp, e2;              /* exponent of real numbers */
202ed1fddf4Sdrh   int nsd;                   /* Number of significant digits returned */
203a18c5681Sdrh   double rounder;            /* Used for rounding floating point values */
204e84a306bSdrh   etByte flag_dp;            /* True if decimal point should be shown */
205e84a306bSdrh   etByte flag_rtz;           /* True if trailing zeros should be removed */
206a18c5681Sdrh #endif
207ed1fddf4Sdrh   char buf[etBUFSIZE];       /* Conversion buffer */
208a18c5681Sdrh 
209a18c5681Sdrh   bufpt = 0;
210a18c5681Sdrh   for(; (c=(*fmt))!=0; ++fmt){
211a18c5681Sdrh     if( c!='%' ){
212e84a306bSdrh       int amt;
213a18c5681Sdrh       bufpt = (char *)fmt;
214a18c5681Sdrh       amt = 1;
215a18c5681Sdrh       while( (c=(*++fmt))!='%' && c!=0 ) amt++;
216ade86483Sdrh       sqlite3StrAccumAppend(pAccum, bufpt, amt);
217a18c5681Sdrh       if( c==0 ) break;
218a18c5681Sdrh     }
219a18c5681Sdrh     if( (c=(*++fmt))==0 ){
220ade86483Sdrh       sqlite3StrAccumAppend(pAccum, "%", 1);
221a18c5681Sdrh       break;
222a18c5681Sdrh     }
223a18c5681Sdrh     /* Find out what flags are present */
224a18c5681Sdrh     flag_leftjustify = flag_plussign = flag_blanksign =
225557cc60fSdrh      flag_alternateform = flag_altform2 = flag_zeropad = 0;
2263e9aeec0Sdrh     done = 0;
227a18c5681Sdrh     do{
228a18c5681Sdrh       switch( c ){
2293e9aeec0Sdrh         case '-':   flag_leftjustify = 1;     break;
2303e9aeec0Sdrh         case '+':   flag_plussign = 1;        break;
2313e9aeec0Sdrh         case ' ':   flag_blanksign = 1;       break;
2323e9aeec0Sdrh         case '#':   flag_alternateform = 1;   break;
2333e9aeec0Sdrh         case '!':   flag_altform2 = 1;        break;
2343e9aeec0Sdrh         case '0':   flag_zeropad = 1;         break;
2353e9aeec0Sdrh         default:    done = 1;                 break;
236a18c5681Sdrh       }
2373e9aeec0Sdrh     }while( !done && (c=(*++fmt))!=0 );
238a18c5681Sdrh     /* Get the field width */
239a18c5681Sdrh     width = 0;
240a18c5681Sdrh     if( c=='*' ){
241a18c5681Sdrh       width = va_arg(ap,int);
242a18c5681Sdrh       if( width<0 ){
243a18c5681Sdrh         flag_leftjustify = 1;
244a18c5681Sdrh         width = -width;
245a18c5681Sdrh       }
246a18c5681Sdrh       c = *++fmt;
247a18c5681Sdrh     }else{
24817a68934Sdrh       while( c>='0' && c<='9' ){
249a18c5681Sdrh         width = width*10 + c - '0';
250a18c5681Sdrh         c = *++fmt;
251a18c5681Sdrh       }
252a18c5681Sdrh     }
253a18c5681Sdrh     /* Get the precision */
254a18c5681Sdrh     if( c=='.' ){
255a18c5681Sdrh       precision = 0;
256a18c5681Sdrh       c = *++fmt;
257a18c5681Sdrh       if( c=='*' ){
258a18c5681Sdrh         precision = va_arg(ap,int);
259a18c5681Sdrh         if( precision<0 ) precision = -precision;
260a18c5681Sdrh         c = *++fmt;
261a18c5681Sdrh       }else{
26217a68934Sdrh         while( c>='0' && c<='9' ){
263a18c5681Sdrh           precision = precision*10 + c - '0';
264a18c5681Sdrh           c = *++fmt;
265a18c5681Sdrh         }
266a18c5681Sdrh       }
267a18c5681Sdrh     }else{
268a18c5681Sdrh       precision = -1;
269a18c5681Sdrh     }
270a18c5681Sdrh     /* Get the conversion type modifier */
271a18c5681Sdrh     if( c=='l' ){
272a18c5681Sdrh       flag_long = 1;
273a18c5681Sdrh       c = *++fmt;
274a34b6764Sdrh       if( c=='l' ){
275a34b6764Sdrh         flag_longlong = 1;
276a34b6764Sdrh         c = *++fmt;
277a18c5681Sdrh       }else{
278a34b6764Sdrh         flag_longlong = 0;
279a34b6764Sdrh       }
280a34b6764Sdrh     }else{
281a34b6764Sdrh       flag_long = flag_longlong = 0;
282a18c5681Sdrh     }
283a18c5681Sdrh     /* Fetch the info entry for the field */
284874ba04cSdrh     infop = &fmtinfo[0];
285874ba04cSdrh     xtype = etINVALID;
28600e13613Sdanielk1977     for(idx=0; idx<ArraySize(fmtinfo); idx++){
287a18c5681Sdrh       if( c==fmtinfo[idx].fmttype ){
288a18c5681Sdrh         infop = &fmtinfo[idx];
2895f968436Sdrh         if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
290e84a306bSdrh           xtype = infop->type;
291c4413565Sdrh         }else{
292ade86483Sdrh           return;
2935f968436Sdrh         }
294a18c5681Sdrh         break;
295a18c5681Sdrh       }
296a18c5681Sdrh     }
297a18c5681Sdrh     zExtra = 0;
29843617e9aSdrh 
299a18c5681Sdrh     /*
300a18c5681Sdrh     ** At this point, variables are initialized as follows:
301a18c5681Sdrh     **
302a18c5681Sdrh     **   flag_alternateform          TRUE if a '#' is present.
3033e9aeec0Sdrh     **   flag_altform2               TRUE if a '!' is present.
304a18c5681Sdrh     **   flag_plussign               TRUE if a '+' is present.
305a18c5681Sdrh     **   flag_leftjustify            TRUE if a '-' is present or if the
306a18c5681Sdrh     **                               field width was negative.
307a18c5681Sdrh     **   flag_zeropad                TRUE if the width began with 0.
308a18c5681Sdrh     **   flag_long                   TRUE if the letter 'l' (ell) prefixed
309a18c5681Sdrh     **                               the conversion character.
310a34b6764Sdrh     **   flag_longlong               TRUE if the letter 'll' (ell ell) prefixed
311a34b6764Sdrh     **                               the conversion character.
312a18c5681Sdrh     **   flag_blanksign              TRUE if a ' ' is present.
313a18c5681Sdrh     **   width                       The specified field width.  This is
314a18c5681Sdrh     **                               always non-negative.  Zero is the default.
315a18c5681Sdrh     **   precision                   The specified precision.  The default
316a18c5681Sdrh     **                               is -1.
317a18c5681Sdrh     **   xtype                       The class of the conversion.
318a18c5681Sdrh     **   infop                       Pointer to the appropriate info struct.
319a18c5681Sdrh     */
320a18c5681Sdrh     switch( xtype ){
321fe63d1c9Sdrh       case etPOINTER:
322fe63d1c9Sdrh         flag_longlong = sizeof(char*)==sizeof(i64);
323fe63d1c9Sdrh         flag_long = sizeof(char*)==sizeof(long int);
324fe63d1c9Sdrh         /* Fall through into the next case */
3259a99334dSdrh       case etORDINAL:
326a18c5681Sdrh       case etRADIX:
327e84a306bSdrh         if( infop->flags & FLAG_SIGNED ){
328e9707671Sdrh           i64 v;
329eeb23a4cSdrh           if( flag_longlong ){
330eeb23a4cSdrh             v = va_arg(ap,i64);
331eeb23a4cSdrh           }else if( flag_long ){
332eeb23a4cSdrh             v = va_arg(ap,long int);
333eeb23a4cSdrh           }else{
334eeb23a4cSdrh             v = va_arg(ap,int);
335eeb23a4cSdrh           }
336e9707671Sdrh           if( v<0 ){
337158b9cb9Sdrh             if( v==SMALLEST_INT64 ){
338158b9cb9Sdrh               longvalue = ((u64)1)<<63;
339158b9cb9Sdrh             }else{
340158b9cb9Sdrh               longvalue = -v;
341158b9cb9Sdrh             }
342cfcdaefeSdanielk1977             prefix = '-';
343cfcdaefeSdanielk1977           }else{
344e9707671Sdrh             longvalue = v;
345e9707671Sdrh             if( flag_plussign )        prefix = '+';
346a18c5681Sdrh             else if( flag_blanksign )  prefix = ' ';
347a18c5681Sdrh             else                       prefix = 0;
348cfcdaefeSdanielk1977           }
349e9707671Sdrh         }else{
350eeb23a4cSdrh           if( flag_longlong ){
351eeb23a4cSdrh             longvalue = va_arg(ap,u64);
352eeb23a4cSdrh           }else if( flag_long ){
353eeb23a4cSdrh             longvalue = va_arg(ap,unsigned long int);
354eeb23a4cSdrh           }else{
355eeb23a4cSdrh             longvalue = va_arg(ap,unsigned int);
356eeb23a4cSdrh           }
357e9707671Sdrh           prefix = 0;
358e9707671Sdrh         }
359e9707671Sdrh         if( longvalue==0 ) flag_alternateform = 0;
360a18c5681Sdrh         if( flag_zeropad && precision<width-(prefix!=0) ){
361a18c5681Sdrh           precision = width-(prefix!=0);
362a18c5681Sdrh         }
36359eedf79Sdrh         if( precision<etBUFSIZE-10 ){
36459eedf79Sdrh           nOut = etBUFSIZE;
36559eedf79Sdrh           zOut = buf;
36659eedf79Sdrh         }else{
36759eedf79Sdrh           nOut = precision + 10;
36859eedf79Sdrh           zOut = zExtra = sqlite3Malloc( nOut );
36959eedf79Sdrh           if( zOut==0 ){
370a6353a3fSdrh             setStrAccumError(pAccum, STRACCUM_NOMEM);
37159eedf79Sdrh             return;
37259eedf79Sdrh           }
37359eedf79Sdrh         }
37459eedf79Sdrh         bufpt = &zOut[nOut-1];
3759a99334dSdrh         if( xtype==etORDINAL ){
37643f6e064Sdrh           static const char zOrd[] = "thstndrd";
377ea678832Sdrh           int x = (int)(longvalue % 10);
37843f6e064Sdrh           if( x>=4 || (longvalue/10)%10==1 ){
37943f6e064Sdrh             x = 0;
38043f6e064Sdrh           }
38159eedf79Sdrh           *(--bufpt) = zOrd[x*2+1];
38259eedf79Sdrh           *(--bufpt) = zOrd[x*2];
3839a99334dSdrh         }
384a18c5681Sdrh         {
38576ff3a0eSdrh           register const char *cset;      /* Use registers for speed */
386a18c5681Sdrh           register int base;
38776ff3a0eSdrh           cset = &aDigits[infop->charset];
388a18c5681Sdrh           base = infop->base;
389a18c5681Sdrh           do{                                           /* Convert to ascii */
390a18c5681Sdrh             *(--bufpt) = cset[longvalue%base];
391a18c5681Sdrh             longvalue = longvalue/base;
392a18c5681Sdrh           }while( longvalue>0 );
393a18c5681Sdrh         }
39459eedf79Sdrh         length = (int)(&zOut[nOut-1]-bufpt);
395a18c5681Sdrh         for(idx=precision-length; idx>0; idx--){
396a18c5681Sdrh           *(--bufpt) = '0';                             /* Zero pad */
397a18c5681Sdrh         }
398a18c5681Sdrh         if( prefix ) *(--bufpt) = prefix;               /* Add sign */
399a18c5681Sdrh         if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
40076ff3a0eSdrh           const char *pre;
40176ff3a0eSdrh           char x;
40276ff3a0eSdrh           pre = &aPrefix[infop->prefix];
40376ff3a0eSdrh           for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
404a18c5681Sdrh         }
40559eedf79Sdrh         length = (int)(&zOut[nOut-1]-bufpt);
406a18c5681Sdrh         break;
407a18c5681Sdrh       case etFLOAT:
408a18c5681Sdrh       case etEXP:
409a18c5681Sdrh       case etGENERIC:
410a18c5681Sdrh         realvalue = va_arg(ap,double);
41169ef7036Sdrh #ifdef SQLITE_OMIT_FLOATING_POINT
41269ef7036Sdrh         length = 0;
41369ef7036Sdrh #else
414a18c5681Sdrh         if( precision<0 ) precision = 6;         /* Set default precision */
415a18c5681Sdrh         if( realvalue<0.0 ){
416a18c5681Sdrh           realvalue = -realvalue;
417a18c5681Sdrh           prefix = '-';
418a18c5681Sdrh         }else{
419a18c5681Sdrh           if( flag_plussign )          prefix = '+';
420a18c5681Sdrh           else if( flag_blanksign )    prefix = ' ';
421a18c5681Sdrh           else                         prefix = 0;
422a18c5681Sdrh         }
4233e9aeec0Sdrh         if( xtype==etGENERIC && precision>0 ) precision--;
42474161705Sdrh         for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
4253e9aeec0Sdrh         if( xtype==etFLOAT ) realvalue += rounder;
426a18c5681Sdrh         /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
427a18c5681Sdrh         exp = 0;
428ea678832Sdrh         if( sqlite3IsNaN((double)realvalue) ){
42953c14021Sdrh           bufpt = "NaN";
43053c14021Sdrh           length = 3;
43153c14021Sdrh           break;
43253c14021Sdrh         }
433a18c5681Sdrh         if( realvalue>0.0 ){
43472b3fbc7Sdrh           LONGDOUBLE_TYPE scale = 1.0;
4354ef94130Sdrh           while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
43672b3fbc7Sdrh           while( realvalue>=1e64*scale && exp<=350 ){ scale *= 1e64; exp+=64; }
43772b3fbc7Sdrh           while( realvalue>=1e8*scale && exp<=350 ){ scale *= 1e8; exp+=8; }
43872b3fbc7Sdrh           while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
43972b3fbc7Sdrh           realvalue /= scale;
440af005fbcSdrh           while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
441af005fbcSdrh           while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
442af005fbcSdrh           if( exp>350 ){
44353c14021Sdrh             if( prefix=='-' ){
44453c14021Sdrh               bufpt = "-Inf";
44553c14021Sdrh             }else if( prefix=='+' ){
44653c14021Sdrh               bufpt = "+Inf";
44753c14021Sdrh             }else{
44853c14021Sdrh               bufpt = "Inf";
44953c14021Sdrh             }
450ea678832Sdrh             length = sqlite3Strlen30(bufpt);
451a18c5681Sdrh             break;
452a18c5681Sdrh           }
453a18c5681Sdrh         }
454a18c5681Sdrh         bufpt = buf;
455a18c5681Sdrh         /*
456a18c5681Sdrh         ** If the field type is etGENERIC, then convert to either etEXP
457a18c5681Sdrh         ** or etFLOAT, as appropriate.
458a18c5681Sdrh         */
459a18c5681Sdrh         if( xtype!=etFLOAT ){
460a18c5681Sdrh           realvalue += rounder;
461a18c5681Sdrh           if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
462a18c5681Sdrh         }
463a18c5681Sdrh         if( xtype==etGENERIC ){
464a18c5681Sdrh           flag_rtz = !flag_alternateform;
465a18c5681Sdrh           if( exp<-4 || exp>precision ){
466a18c5681Sdrh             xtype = etEXP;
467a18c5681Sdrh           }else{
468a18c5681Sdrh             precision = precision - exp;
469a18c5681Sdrh             xtype = etFLOAT;
470a18c5681Sdrh           }
471a18c5681Sdrh         }else{
47272b3fbc7Sdrh           flag_rtz = flag_altform2;
473a18c5681Sdrh         }
474557cc60fSdrh         if( xtype==etEXP ){
475557cc60fSdrh           e2 = 0;
476557cc60fSdrh         }else{
477557cc60fSdrh           e2 = exp;
478557cc60fSdrh         }
479e8c13bf2Sdrh         if( MAX(e2,0)+precision+width > etBUFSIZE - 15 ){
480e8c13bf2Sdrh           bufpt = zExtra = sqlite3Malloc( MAX(e2,0)+precision+width+15 );
48159eedf79Sdrh           if( bufpt==0 ){
482a6353a3fSdrh             setStrAccumError(pAccum, STRACCUM_NOMEM);
48359eedf79Sdrh             return;
48459eedf79Sdrh           }
48559eedf79Sdrh         }
48659eedf79Sdrh         zOut = bufpt;
48772b3fbc7Sdrh         nsd = 16 + flag_altform2*10;
488ea678832Sdrh         flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
489557cc60fSdrh         /* The sign in front of the number */
490557cc60fSdrh         if( prefix ){
491557cc60fSdrh           *(bufpt++) = prefix;
492557cc60fSdrh         }
493557cc60fSdrh         /* Digits prior to the decimal point */
494557cc60fSdrh         if( e2<0 ){
495557cc60fSdrh           *(bufpt++) = '0';
496557cc60fSdrh         }else{
497557cc60fSdrh           for(; e2>=0; e2--){
498557cc60fSdrh             *(bufpt++) = et_getdigit(&realvalue,&nsd);
499557cc60fSdrh           }
500557cc60fSdrh         }
501557cc60fSdrh         /* The decimal point */
502557cc60fSdrh         if( flag_dp ){
503557cc60fSdrh           *(bufpt++) = '.';
504557cc60fSdrh         }
505557cc60fSdrh         /* "0" digits after the decimal point but before the first
506557cc60fSdrh         ** significant digit of the number */
507af005fbcSdrh         for(e2++; e2<0; precision--, e2++){
508af005fbcSdrh           assert( precision>0 );
509a18c5681Sdrh           *(bufpt++) = '0';
510a18c5681Sdrh         }
511557cc60fSdrh         /* Significant digits after the decimal point */
512557cc60fSdrh         while( (precision--)>0 ){
513557cc60fSdrh           *(bufpt++) = et_getdigit(&realvalue,&nsd);
514a18c5681Sdrh         }
515557cc60fSdrh         /* Remove trailing zeros and the "." if no digits follow the "." */
516557cc60fSdrh         if( flag_rtz && flag_dp ){
5173e9aeec0Sdrh           while( bufpt[-1]=='0' ) *(--bufpt) = 0;
51859eedf79Sdrh           assert( bufpt>zOut );
5193e9aeec0Sdrh           if( bufpt[-1]=='.' ){
520557cc60fSdrh             if( flag_altform2 ){
521557cc60fSdrh               *(bufpt++) = '0';
522557cc60fSdrh             }else{
523557cc60fSdrh               *(--bufpt) = 0;
524a18c5681Sdrh             }
525557cc60fSdrh           }
526557cc60fSdrh         }
527557cc60fSdrh         /* Add the "eNNN" suffix */
52859eedf79Sdrh         if( xtype==etEXP ){
52976ff3a0eSdrh           *(bufpt++) = aDigits[infop->charset];
530557cc60fSdrh           if( exp<0 ){
531557cc60fSdrh             *(bufpt++) = '-'; exp = -exp;
532557cc60fSdrh           }else{
533557cc60fSdrh             *(bufpt++) = '+';
534557cc60fSdrh           }
535a18c5681Sdrh           if( exp>=100 ){
536ea678832Sdrh             *(bufpt++) = (char)((exp/100)+'0');        /* 100's digit */
537a18c5681Sdrh             exp %= 100;
538a18c5681Sdrh           }
539ea678832Sdrh           *(bufpt++) = (char)(exp/10+'0');             /* 10's digit */
540ea678832Sdrh           *(bufpt++) = (char)(exp%10+'0');             /* 1's digit */
541a18c5681Sdrh         }
542557cc60fSdrh         *bufpt = 0;
543557cc60fSdrh 
544a18c5681Sdrh         /* The converted number is in buf[] and zero terminated. Output it.
545a18c5681Sdrh         ** Note that the number is in the usual order, not reversed as with
546a18c5681Sdrh         ** integer conversions. */
54759eedf79Sdrh         length = (int)(bufpt-zOut);
54859eedf79Sdrh         bufpt = zOut;
549a18c5681Sdrh 
550a18c5681Sdrh         /* Special case:  Add leading zeros if the flag_zeropad flag is
551a18c5681Sdrh         ** set and we are not left justified */
552a18c5681Sdrh         if( flag_zeropad && !flag_leftjustify && length < width){
553a18c5681Sdrh           int i;
554a18c5681Sdrh           int nPad = width - length;
555a18c5681Sdrh           for(i=width; i>=nPad; i--){
556a18c5681Sdrh             bufpt[i] = bufpt[i-nPad];
557a18c5681Sdrh           }
558a18c5681Sdrh           i = prefix!=0;
559a18c5681Sdrh           while( nPad-- ) bufpt[i++] = '0';
560a18c5681Sdrh           length = width;
561a18c5681Sdrh         }
56269ef7036Sdrh #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
563a18c5681Sdrh         break;
564a18c5681Sdrh       case etSIZE:
565ade86483Sdrh         *(va_arg(ap,int*)) = pAccum->nChar;
566a18c5681Sdrh         length = width = 0;
567a18c5681Sdrh         break;
568a18c5681Sdrh       case etPERCENT:
569a18c5681Sdrh         buf[0] = '%';
570a18c5681Sdrh         bufpt = buf;
571a18c5681Sdrh         length = 1;
572a18c5681Sdrh         break;
573a18c5681Sdrh       case etCHARX:
574ea678832Sdrh         c = va_arg(ap,int);
575ea678832Sdrh         buf[0] = (char)c;
576a18c5681Sdrh         if( precision>=0 ){
577ea678832Sdrh           for(idx=1; idx<precision; idx++) buf[idx] = (char)c;
578a18c5681Sdrh           length = precision;
579a18c5681Sdrh         }else{
580a18c5681Sdrh           length =1;
581a18c5681Sdrh         }
582a18c5681Sdrh         bufpt = buf;
583a18c5681Sdrh         break;
584a18c5681Sdrh       case etSTRING:
585d93d8a81Sdrh       case etDYNSTRING:
586cb485882Sdrh         bufpt = va_arg(ap,char*);
587d93d8a81Sdrh         if( bufpt==0 ){
588d93d8a81Sdrh           bufpt = "";
589d93d8a81Sdrh         }else if( xtype==etDYNSTRING ){
590d93d8a81Sdrh           zExtra = bufpt;
591d93d8a81Sdrh         }
592e509094bSdrh         if( precision>=0 ){
593e509094bSdrh           for(length=0; length<precision && bufpt[length]; length++){}
594e509094bSdrh         }else{
595ea678832Sdrh           length = sqlite3Strlen30(bufpt);
596e509094bSdrh         }
597a18c5681Sdrh         break;
598a18c5681Sdrh       case etSQLESCAPE:
599f3b863edSdanielk1977       case etSQLESCAPE2:
600f3b863edSdanielk1977       case etSQLESCAPE3: {
6018965b50eSdrh         int i, j, k, n, isnull;
6024794f735Sdrh         int needQuote;
603ea678832Sdrh         char ch;
604f3b863edSdanielk1977         char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
605f0113000Sdanielk1977         char *escarg = va_arg(ap,char*);
606f0113000Sdanielk1977         isnull = escarg==0;
607f0113000Sdanielk1977         if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
6088965b50eSdrh         k = precision;
60960d4a304Sdan         for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
610f3b863edSdanielk1977           if( ch==q )  n++;
611a18c5681Sdrh         }
6124794f735Sdrh         needQuote = !isnull && xtype==etSQLESCAPE2;
6134794f735Sdrh         n += i + 1 + needQuote*2;
614a18c5681Sdrh         if( n>etBUFSIZE ){
615e5ae5735Sdrh           bufpt = zExtra = sqlite3Malloc( n );
616d164fd34Sdrh           if( bufpt==0 ){
617a6353a3fSdrh             setStrAccumError(pAccum, STRACCUM_NOMEM);
618d164fd34Sdrh             return;
619d164fd34Sdrh           }
620a18c5681Sdrh         }else{
621a18c5681Sdrh           bufpt = buf;
622a18c5681Sdrh         }
6230cfcf3fbSchw         j = 0;
624f3b863edSdanielk1977         if( needQuote ) bufpt[j++] = q;
6258965b50eSdrh         k = i;
6268965b50eSdrh         for(i=0; i<k; i++){
6278965b50eSdrh           bufpt[j++] = ch = escarg[i];
628f3b863edSdanielk1977           if( ch==q ) bufpt[j++] = ch;
629a18c5681Sdrh         }
630f3b863edSdanielk1977         if( needQuote ) bufpt[j++] = q;
631a18c5681Sdrh         bufpt[j] = 0;
632a18c5681Sdrh         length = j;
6338965b50eSdrh         /* The precision in %q and %Q means how many input characters to
6348965b50eSdrh         ** consume, not the length of the output...
6358965b50eSdrh         ** if( precision>=0 && precision<length ) length = precision; */
636a18c5681Sdrh         break;
6375eba8c09Sdrh       }
6385f968436Sdrh       case etTOKEN: {
6395f968436Sdrh         Token *pToken = va_arg(ap, Token*);
640a9ab481fSdrh         if( pToken && pToken->n ){
641ade86483Sdrh           sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
642ad6d9460Sdrh         }
6435f968436Sdrh         length = width = 0;
6445f968436Sdrh         break;
6455f968436Sdrh       }
6465f968436Sdrh       case etSRCLIST: {
6475f968436Sdrh         SrcList *pSrc = va_arg(ap, SrcList*);
6485f968436Sdrh         int k = va_arg(ap, int);
6495f968436Sdrh         struct SrcList_item *pItem = &pSrc->a[k];
6505f968436Sdrh         assert( k>=0 && k<pSrc->nSrc );
65193a960a0Sdrh         if( pItem->zDatabase ){
652a6353a3fSdrh           sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
653ade86483Sdrh           sqlite3StrAccumAppend(pAccum, ".", 1);
6545f968436Sdrh         }
655a6353a3fSdrh         sqlite3StrAccumAppendAll(pAccum, pItem->zName);
6565f968436Sdrh         length = width = 0;
6575f968436Sdrh         break;
6585f968436Sdrh       }
659874ba04cSdrh       default: {
660874ba04cSdrh         assert( xtype==etINVALID );
661874ba04cSdrh         return;
662874ba04cSdrh       }
663a18c5681Sdrh     }/* End switch over the format type */
664a18c5681Sdrh     /*
665a18c5681Sdrh     ** The text of the conversion is pointed to by "bufpt" and is
666a18c5681Sdrh     ** "length" characters long.  The field width is "width".  Do
667a18c5681Sdrh     ** the output.
668a18c5681Sdrh     */
669a18c5681Sdrh     if( !flag_leftjustify ){
670a18c5681Sdrh       register int nspace;
671a18c5681Sdrh       nspace = width-length;
672a18c5681Sdrh       if( nspace>0 ){
6737e02e5e6Sdrh         sqlite3AppendSpace(pAccum, nspace);
674a18c5681Sdrh       }
675a18c5681Sdrh     }
676a18c5681Sdrh     if( length>0 ){
677ade86483Sdrh       sqlite3StrAccumAppend(pAccum, bufpt, length);
678a18c5681Sdrh     }
679a18c5681Sdrh     if( flag_leftjustify ){
680a18c5681Sdrh       register int nspace;
681a18c5681Sdrh       nspace = width-length;
682a18c5681Sdrh       if( nspace>0 ){
6837e02e5e6Sdrh         sqlite3AppendSpace(pAccum, nspace);
684a18c5681Sdrh       }
685a18c5681Sdrh     }
686*40f22bedSdrh     if( zExtra ) sqlite3_free(zExtra);
687a18c5681Sdrh   }/* End for loop over the format string */
688a18c5681Sdrh } /* End of function */
689a18c5681Sdrh 
690a18c5681Sdrh /*
691ade86483Sdrh ** Append N bytes of text from z to the StrAccum object.
692a18c5681Sdrh */
693ade86483Sdrh void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
694a9ab481fSdrh   assert( z!=0 );
695a6353a3fSdrh   assert( p->zText!=0 || p->nChar==0 || p->accError );
696a6353a3fSdrh   assert( N>=0 );
697a6353a3fSdrh   assert( p->accError==0 || p->nAlloc==0 );
698a6353a3fSdrh   if( p->nChar+N >= p->nAlloc ){
699a6353a3fSdrh     char *zNew;
700b49bc86aSdrh     if( p->accError ){
701b49bc86aSdrh       testcase(p->accError==STRACCUM_TOOBIG);
702b49bc86aSdrh       testcase(p->accError==STRACCUM_NOMEM);
70317435752Sdrh       return;
704ade86483Sdrh     }
705ade86483Sdrh     if( !p->useMalloc ){
706ade86483Sdrh       N = p->nAlloc - p->nChar - 1;
707a6353a3fSdrh       setStrAccumError(p, STRACCUM_TOOBIG);
708ade86483Sdrh       if( N<=0 ){
709ade86483Sdrh         return;
7105f968436Sdrh       }
711a18c5681Sdrh     }else{
712a9ef7097Sdan       char *zOld = (p->zText==p->zBase ? 0 : p->zText);
71393a960a0Sdrh       i64 szNew = p->nChar;
714b1a6c3c1Sdrh       szNew += N + 1;
715b1a6c3c1Sdrh       if( szNew > p->mxAlloc ){
716ade86483Sdrh         sqlite3StrAccumReset(p);
717a6353a3fSdrh         setStrAccumError(p, STRACCUM_TOOBIG);
718eaad32b1Sdrh         return;
719b1a6c3c1Sdrh       }else{
720ea678832Sdrh         p->nAlloc = (int)szNew;
721a18c5681Sdrh       }
722b975598eSdrh       if( p->useMalloc==1 ){
723a9ef7097Sdan         zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
724b975598eSdrh       }else{
725a9ef7097Sdan         zNew = sqlite3_realloc(zOld, p->nAlloc);
726b975598eSdrh       }
72753f733c7Sdrh       if( zNew ){
728b07028f7Sdrh         if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
729ade86483Sdrh         p->zText = zNew;
730ade86483Sdrh       }else{
731ade86483Sdrh         sqlite3StrAccumReset(p);
732a6353a3fSdrh         setStrAccumError(p, STRACCUM_NOMEM);
733ade86483Sdrh         return;
73453f733c7Sdrh       }
7355f968436Sdrh     }
7365f968436Sdrh   }
737b07028f7Sdrh   assert( p->zText );
738ade86483Sdrh   memcpy(&p->zText[p->nChar], z, N);
739ade86483Sdrh   p->nChar += N;
740483750baSdrh }
741483750baSdrh 
742483750baSdrh /*
743a6353a3fSdrh ** Append the complete text of zero-terminated string z[] to the p string.
744a6353a3fSdrh */
745a6353a3fSdrh void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){
74653cd9646Smistachkin   sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z));
747a6353a3fSdrh }
748a6353a3fSdrh 
749a6353a3fSdrh 
750a6353a3fSdrh /*
751ade86483Sdrh ** Finish off a string by making sure it is zero-terminated.
752ade86483Sdrh ** Return a pointer to the resulting string.  Return a NULL
753ade86483Sdrh ** pointer if any kind of error was encountered.
7545f968436Sdrh */
755ade86483Sdrh char *sqlite3StrAccumFinish(StrAccum *p){
756ade86483Sdrh   if( p->zText ){
757ade86483Sdrh     p->zText[p->nChar] = 0;
758ade86483Sdrh     if( p->useMalloc && p->zText==p->zBase ){
759b975598eSdrh       if( p->useMalloc==1 ){
760633e6d57Sdrh         p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
761b975598eSdrh       }else{
762b975598eSdrh         p->zText = sqlite3_malloc(p->nChar+1);
763b975598eSdrh       }
764ade86483Sdrh       if( p->zText ){
765ade86483Sdrh         memcpy(p->zText, p->zBase, p->nChar+1);
766ade86483Sdrh       }else{
767a6353a3fSdrh         setStrAccumError(p, STRACCUM_NOMEM);
768ade86483Sdrh       }
769ade86483Sdrh     }
770ade86483Sdrh   }
771ade86483Sdrh   return p->zText;
772ade86483Sdrh }
773ade86483Sdrh 
774ade86483Sdrh /*
775ade86483Sdrh ** Reset an StrAccum string.  Reclaim all malloced memory.
776ade86483Sdrh */
777ade86483Sdrh void sqlite3StrAccumReset(StrAccum *p){
778ade86483Sdrh   if( p->zText!=p->zBase ){
779b975598eSdrh     if( p->useMalloc==1 ){
780633e6d57Sdrh       sqlite3DbFree(p->db, p->zText);
781b975598eSdrh     }else{
782b975598eSdrh       sqlite3_free(p->zText);
783b975598eSdrh     }
784ade86483Sdrh   }
785f089aa45Sdrh   p->zText = 0;
786ade86483Sdrh }
787ade86483Sdrh 
788ade86483Sdrh /*
789ade86483Sdrh ** Initialize a string accumulator
790ade86483Sdrh */
791f089aa45Sdrh void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
792ade86483Sdrh   p->zText = p->zBase = zBase;
793633e6d57Sdrh   p->db = 0;
794ade86483Sdrh   p->nChar = 0;
795ade86483Sdrh   p->nAlloc = n;
796bb4957f8Sdrh   p->mxAlloc = mx;
797ade86483Sdrh   p->useMalloc = 1;
798b49bc86aSdrh   p->accError = 0;
7995f968436Sdrh }
8005f968436Sdrh 
8015f968436Sdrh /*
8025f968436Sdrh ** Print into memory obtained from sqliteMalloc().  Use the internal
8035f968436Sdrh ** %-conversion extensions.
8045f968436Sdrh */
80517435752Sdrh char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
80617435752Sdrh   char *z;
80779158e18Sdrh   char zBase[SQLITE_PRINT_BUF_SIZE];
808ade86483Sdrh   StrAccum acc;
809bc6160b0Sdrh   assert( db!=0 );
810bb4957f8Sdrh   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
811bc6160b0Sdrh                       db->aLimit[SQLITE_LIMIT_LENGTH]);
812633e6d57Sdrh   acc.db = db;
813f089aa45Sdrh   sqlite3VXPrintf(&acc, 1, zFormat, ap);
814ade86483Sdrh   z = sqlite3StrAccumFinish(&acc);
815b49bc86aSdrh   if( acc.accError==STRACCUM_NOMEM ){
81617435752Sdrh     db->mallocFailed = 1;
81717435752Sdrh   }
81817435752Sdrh   return z;
8195f968436Sdrh }
8205f968436Sdrh 
8215f968436Sdrh /*
8225f968436Sdrh ** Print into memory obtained from sqliteMalloc().  Use the internal
8235f968436Sdrh ** %-conversion extensions.
8245f968436Sdrh */
82517435752Sdrh char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
8265f968436Sdrh   va_list ap;
8275f968436Sdrh   char *z;
8285f968436Sdrh   va_start(ap, zFormat);
829ade86483Sdrh   z = sqlite3VMPrintf(db, zFormat, ap);
8305f968436Sdrh   va_end(ap);
8315f968436Sdrh   return z;
8325f968436Sdrh }
8335f968436Sdrh 
8345f968436Sdrh /*
835633e6d57Sdrh ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
836633e6d57Sdrh ** the string and before returnning.  This routine is intended to be used
837633e6d57Sdrh ** to modify an existing string.  For example:
838633e6d57Sdrh **
839633e6d57Sdrh **       x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
840633e6d57Sdrh **
841633e6d57Sdrh */
842633e6d57Sdrh char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
843633e6d57Sdrh   va_list ap;
844633e6d57Sdrh   char *z;
845633e6d57Sdrh   va_start(ap, zFormat);
846633e6d57Sdrh   z = sqlite3VMPrintf(db, zFormat, ap);
847633e6d57Sdrh   va_end(ap);
848633e6d57Sdrh   sqlite3DbFree(db, zStr);
849633e6d57Sdrh   return z;
850633e6d57Sdrh }
851633e6d57Sdrh 
852633e6d57Sdrh /*
85328dd479cSdrh ** Print into memory obtained from sqlite3_malloc().  Omit the internal
85428dd479cSdrh ** %-conversion extensions.
85528dd479cSdrh */
85628dd479cSdrh char *sqlite3_vmprintf(const char *zFormat, va_list ap){
857ade86483Sdrh   char *z;
85828dd479cSdrh   char zBase[SQLITE_PRINT_BUF_SIZE];
859ade86483Sdrh   StrAccum acc;
860ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT
861ff1590eeSdrh   if( sqlite3_initialize() ) return 0;
862ff1590eeSdrh #endif
863bb4957f8Sdrh   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
864b975598eSdrh   acc.useMalloc = 2;
865f089aa45Sdrh   sqlite3VXPrintf(&acc, 0, zFormat, ap);
866ade86483Sdrh   z = sqlite3StrAccumFinish(&acc);
867ade86483Sdrh   return z;
86828dd479cSdrh }
86928dd479cSdrh 
87028dd479cSdrh /*
87128dd479cSdrh ** Print into memory obtained from sqlite3_malloc()().  Omit the internal
87228dd479cSdrh ** %-conversion extensions.
873483750baSdrh */
8746f8a503dSdanielk1977 char *sqlite3_mprintf(const char *zFormat, ...){
875a18c5681Sdrh   va_list ap;
8765f968436Sdrh   char *z;
877ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT
878ff1590eeSdrh   if( sqlite3_initialize() ) return 0;
879ff1590eeSdrh #endif
880a18c5681Sdrh   va_start(ap, zFormat);
881b3738b6cSdrh   z = sqlite3_vmprintf(zFormat, ap);
882a18c5681Sdrh   va_end(ap);
8835f968436Sdrh   return z;
884a18c5681Sdrh }
885a18c5681Sdrh 
886a18c5681Sdrh /*
8876f8a503dSdanielk1977 ** sqlite3_snprintf() works like snprintf() except that it ignores the
88893a5c6bdSdrh ** current locale settings.  This is important for SQLite because we
88993a5c6bdSdrh ** are not able to use a "," as the decimal point in place of "." as
89093a5c6bdSdrh ** specified by some locales.
891db26d4c9Sdrh **
892db26d4c9Sdrh ** Oops:  The first two arguments of sqlite3_snprintf() are backwards
893db26d4c9Sdrh ** from the snprintf() standard.  Unfortunately, it is too late to change
894db26d4c9Sdrh ** this without breaking compatibility, so we just have to live with the
895db26d4c9Sdrh ** mistake.
896db26d4c9Sdrh **
897db26d4c9Sdrh ** sqlite3_vsnprintf() is the varargs version.
89893a5c6bdSdrh */
899db26d4c9Sdrh char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
900db26d4c9Sdrh   StrAccum acc;
901db26d4c9Sdrh   if( n<=0 ) return zBuf;
902db26d4c9Sdrh   sqlite3StrAccumInit(&acc, zBuf, n, 0);
903db26d4c9Sdrh   acc.useMalloc = 0;
904db26d4c9Sdrh   sqlite3VXPrintf(&acc, 0, zFormat, ap);
905db26d4c9Sdrh   return sqlite3StrAccumFinish(&acc);
906db26d4c9Sdrh }
9076f8a503dSdanielk1977 char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
9085f968436Sdrh   char *z;
90993a5c6bdSdrh   va_list ap;
91093a5c6bdSdrh   va_start(ap,zFormat);
911db26d4c9Sdrh   z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
91293a5c6bdSdrh   va_end(ap);
9135f968436Sdrh   return z;
91493a5c6bdSdrh }
91593a5c6bdSdrh 
9163f280701Sdrh /*
9177c0c460fSdrh ** This is the routine that actually formats the sqlite3_log() message.
9187c0c460fSdrh ** We house it in a separate routine from sqlite3_log() to avoid using
9197c0c460fSdrh ** stack space on small-stack systems when logging is disabled.
9207c0c460fSdrh **
9217c0c460fSdrh ** sqlite3_log() must render into a static buffer.  It cannot dynamically
9227c0c460fSdrh ** allocate memory because it might be called while the memory allocator
9237c0c460fSdrh ** mutex is held.
9247c0c460fSdrh */
9257c0c460fSdrh static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
9267c0c460fSdrh   StrAccum acc;                          /* String accumulator */
927a64fa912Sdrh   char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
9287c0c460fSdrh 
9297c0c460fSdrh   sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0);
9307c0c460fSdrh   acc.useMalloc = 0;
9317c0c460fSdrh   sqlite3VXPrintf(&acc, 0, zFormat, ap);
9327c0c460fSdrh   sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
9337c0c460fSdrh                            sqlite3StrAccumFinish(&acc));
9347c0c460fSdrh }
9357c0c460fSdrh 
9367c0c460fSdrh /*
9373f280701Sdrh ** Format and write a message to the log if logging is enabled.
9383f280701Sdrh */
939a7564663Sdrh void sqlite3_log(int iErrCode, const char *zFormat, ...){
9403f280701Sdrh   va_list ap;                             /* Vararg list */
9417c0c460fSdrh   if( sqlite3GlobalConfig.xLog ){
9423f280701Sdrh     va_start(ap, zFormat);
9437c0c460fSdrh     renderLogMsg(iErrCode, zFormat, ap);
9443f280701Sdrh     va_end(ap);
9453f280701Sdrh   }
9463f280701Sdrh }
9473f280701Sdrh 
94885e9e22bSdrh #if defined(SQLITE_DEBUG)
949e54ca3feSdrh /*
950e54ca3feSdrh ** A version of printf() that understands %lld.  Used for debugging.
951e54ca3feSdrh ** The printf() built into some versions of windows does not understand %lld
952e54ca3feSdrh ** and segfaults if you give it a long long int.
953e54ca3feSdrh */
954e54ca3feSdrh void sqlite3DebugPrintf(const char *zFormat, ...){
955e54ca3feSdrh   va_list ap;
956ade86483Sdrh   StrAccum acc;
957e54ca3feSdrh   char zBuf[500];
958bb4957f8Sdrh   sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
959ade86483Sdrh   acc.useMalloc = 0;
960e54ca3feSdrh   va_start(ap,zFormat);
961f089aa45Sdrh   sqlite3VXPrintf(&acc, 0, zFormat, ap);
962e54ca3feSdrh   va_end(ap);
963bed8e7e5Sdrh   sqlite3StrAccumFinish(&acc);
964485f0039Sdrh   fprintf(stdout,"%s", zBuf);
9652ac3ee97Sdrh   fflush(stdout);
966e54ca3feSdrh }
967e54ca3feSdrh #endif
968c7bc4fdeSdrh 
969c7bc4fdeSdrh #ifndef SQLITE_OMIT_TRACE
970c7bc4fdeSdrh /*
971c7bc4fdeSdrh ** variable-argument wrapper around sqlite3VXPrintf().
972c7bc4fdeSdrh */
973c7bc4fdeSdrh void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){
974c7bc4fdeSdrh   va_list ap;
975c7bc4fdeSdrh   va_start(ap,zFormat);
976c7bc4fdeSdrh   sqlite3VXPrintf(p, 1, zFormat, ap);
977c7bc4fdeSdrh   va_end(ap);
978c7bc4fdeSdrh }
979c7bc4fdeSdrh #endif
980