xref: /sqlite-3.40.0/src/printf.c (revision 72b3fbc7)
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;
127*72b3fbc7Sdrh   if( (*cnt)<=0 ) return '0';
128*72b3fbc7Sdrh   (*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 /*
15279158e18Sdrh ** On machines with a small stack size, you can redefine the
153ed1fddf4Sdrh ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
15479158e18Sdrh */
15579158e18Sdrh #ifndef SQLITE_PRINT_BUF_SIZE
15659eedf79Sdrh # define SQLITE_PRINT_BUF_SIZE 70
15750d654daSdrh #endif
15879158e18Sdrh #define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
159a18c5681Sdrh 
160a18c5681Sdrh /*
161ed1fddf4Sdrh ** Render a string given by "fmt" into the StrAccum object.
162a18c5681Sdrh */
163f089aa45Sdrh void sqlite3VXPrintf(
164ade86483Sdrh   StrAccum *pAccum,                  /* Accumulate results here */
1655f968436Sdrh   int useExtended,                   /* Allow extended %-conversions */
1665f968436Sdrh   const char *fmt,                   /* Format string */
1675f968436Sdrh   va_list ap                         /* arguments */
168a18c5681Sdrh ){
169e84a306bSdrh   int c;                     /* Next character in the format string */
170e84a306bSdrh   char *bufpt;               /* Pointer to the conversion buffer */
171e84a306bSdrh   int precision;             /* Precision of the current field */
172e84a306bSdrh   int length;                /* Length of the field */
173e84a306bSdrh   int idx;                   /* A general purpose loop counter */
174a18c5681Sdrh   int width;                 /* Width of the current field */
175e84a306bSdrh   etByte flag_leftjustify;   /* True if "-" flag is present */
176e84a306bSdrh   etByte flag_plussign;      /* True if "+" flag is present */
177e84a306bSdrh   etByte flag_blanksign;     /* True if " " flag is present */
178e84a306bSdrh   etByte flag_alternateform; /* True if "#" flag is present */
179531fe878Sdrh   etByte flag_altform2;      /* True if "!" flag is present */
180e84a306bSdrh   etByte flag_zeropad;       /* True if field width constant starts with zero */
181e84a306bSdrh   etByte flag_long;          /* True if "l" flag is present */
182a34b6764Sdrh   etByte flag_longlong;      /* True if the "ll" flag is present */
1833e9aeec0Sdrh   etByte done;               /* Loop termination flag */
184ed1fddf4Sdrh   etByte xtype = 0;          /* Conversion paradigm */
185ed1fddf4Sdrh   char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
18627436af7Sdrh   sqlite_uint64 longvalue;   /* Value for integer types */
187384eef32Sdrh   LONGDOUBLE_TYPE realvalue; /* Value for real types */
1885719628aSdrh   const et_info *infop;      /* Pointer to the appropriate info structure */
18959eedf79Sdrh   char *zOut;                /* Rendering buffer */
19059eedf79Sdrh   int nOut;                  /* Size of the rendering buffer */
191ed1fddf4Sdrh   char *zExtra;              /* Malloced memory used by some conversion */
192b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT
193557cc60fSdrh   int  exp, e2;              /* exponent of real numbers */
194ed1fddf4Sdrh   int nsd;                   /* Number of significant digits returned */
195a18c5681Sdrh   double rounder;            /* Used for rounding floating point values */
196e84a306bSdrh   etByte flag_dp;            /* True if decimal point should be shown */
197e84a306bSdrh   etByte flag_rtz;           /* True if trailing zeros should be removed */
198a18c5681Sdrh #endif
199ed1fddf4Sdrh   char buf[etBUFSIZE];       /* Conversion buffer */
200a18c5681Sdrh 
201a18c5681Sdrh   bufpt = 0;
202a18c5681Sdrh   for(; (c=(*fmt))!=0; ++fmt){
203a18c5681Sdrh     if( c!='%' ){
204e84a306bSdrh       int amt;
205a18c5681Sdrh       bufpt = (char *)fmt;
206a18c5681Sdrh       amt = 1;
207a18c5681Sdrh       while( (c=(*++fmt))!='%' && c!=0 ) amt++;
208ade86483Sdrh       sqlite3StrAccumAppend(pAccum, bufpt, amt);
209a18c5681Sdrh       if( c==0 ) break;
210a18c5681Sdrh     }
211a18c5681Sdrh     if( (c=(*++fmt))==0 ){
212ade86483Sdrh       sqlite3StrAccumAppend(pAccum, "%", 1);
213a18c5681Sdrh       break;
214a18c5681Sdrh     }
215a18c5681Sdrh     /* Find out what flags are present */
216a18c5681Sdrh     flag_leftjustify = flag_plussign = flag_blanksign =
217557cc60fSdrh      flag_alternateform = flag_altform2 = flag_zeropad = 0;
2183e9aeec0Sdrh     done = 0;
219a18c5681Sdrh     do{
220a18c5681Sdrh       switch( c ){
2213e9aeec0Sdrh         case '-':   flag_leftjustify = 1;     break;
2223e9aeec0Sdrh         case '+':   flag_plussign = 1;        break;
2233e9aeec0Sdrh         case ' ':   flag_blanksign = 1;       break;
2243e9aeec0Sdrh         case '#':   flag_alternateform = 1;   break;
2253e9aeec0Sdrh         case '!':   flag_altform2 = 1;        break;
2263e9aeec0Sdrh         case '0':   flag_zeropad = 1;         break;
2273e9aeec0Sdrh         default:    done = 1;                 break;
228a18c5681Sdrh       }
2293e9aeec0Sdrh     }while( !done && (c=(*++fmt))!=0 );
230a18c5681Sdrh     /* Get the field width */
231a18c5681Sdrh     width = 0;
232a18c5681Sdrh     if( c=='*' ){
233a18c5681Sdrh       width = va_arg(ap,int);
234a18c5681Sdrh       if( width<0 ){
235a18c5681Sdrh         flag_leftjustify = 1;
236a18c5681Sdrh         width = -width;
237a18c5681Sdrh       }
238a18c5681Sdrh       c = *++fmt;
239a18c5681Sdrh     }else{
24017a68934Sdrh       while( c>='0' && c<='9' ){
241a18c5681Sdrh         width = width*10 + c - '0';
242a18c5681Sdrh         c = *++fmt;
243a18c5681Sdrh       }
244a18c5681Sdrh     }
245a18c5681Sdrh     /* Get the precision */
246a18c5681Sdrh     if( c=='.' ){
247a18c5681Sdrh       precision = 0;
248a18c5681Sdrh       c = *++fmt;
249a18c5681Sdrh       if( c=='*' ){
250a18c5681Sdrh         precision = va_arg(ap,int);
251a18c5681Sdrh         if( precision<0 ) precision = -precision;
252a18c5681Sdrh         c = *++fmt;
253a18c5681Sdrh       }else{
25417a68934Sdrh         while( c>='0' && c<='9' ){
255a18c5681Sdrh           precision = precision*10 + c - '0';
256a18c5681Sdrh           c = *++fmt;
257a18c5681Sdrh         }
258a18c5681Sdrh       }
259a18c5681Sdrh     }else{
260a18c5681Sdrh       precision = -1;
261a18c5681Sdrh     }
262a18c5681Sdrh     /* Get the conversion type modifier */
263a18c5681Sdrh     if( c=='l' ){
264a18c5681Sdrh       flag_long = 1;
265a18c5681Sdrh       c = *++fmt;
266a34b6764Sdrh       if( c=='l' ){
267a34b6764Sdrh         flag_longlong = 1;
268a34b6764Sdrh         c = *++fmt;
269a18c5681Sdrh       }else{
270a34b6764Sdrh         flag_longlong = 0;
271a34b6764Sdrh       }
272a34b6764Sdrh     }else{
273a34b6764Sdrh       flag_long = flag_longlong = 0;
274a18c5681Sdrh     }
275a18c5681Sdrh     /* Fetch the info entry for the field */
276874ba04cSdrh     infop = &fmtinfo[0];
277874ba04cSdrh     xtype = etINVALID;
27800e13613Sdanielk1977     for(idx=0; idx<ArraySize(fmtinfo); idx++){
279a18c5681Sdrh       if( c==fmtinfo[idx].fmttype ){
280a18c5681Sdrh         infop = &fmtinfo[idx];
2815f968436Sdrh         if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
282e84a306bSdrh           xtype = infop->type;
283c4413565Sdrh         }else{
284ade86483Sdrh           return;
2855f968436Sdrh         }
286a18c5681Sdrh         break;
287a18c5681Sdrh       }
288a18c5681Sdrh     }
289a18c5681Sdrh     zExtra = 0;
29043617e9aSdrh 
291a18c5681Sdrh     /*
292a18c5681Sdrh     ** At this point, variables are initialized as follows:
293a18c5681Sdrh     **
294a18c5681Sdrh     **   flag_alternateform          TRUE if a '#' is present.
2953e9aeec0Sdrh     **   flag_altform2               TRUE if a '!' is present.
296a18c5681Sdrh     **   flag_plussign               TRUE if a '+' is present.
297a18c5681Sdrh     **   flag_leftjustify            TRUE if a '-' is present or if the
298a18c5681Sdrh     **                               field width was negative.
299a18c5681Sdrh     **   flag_zeropad                TRUE if the width began with 0.
300a18c5681Sdrh     **   flag_long                   TRUE if the letter 'l' (ell) prefixed
301a18c5681Sdrh     **                               the conversion character.
302a34b6764Sdrh     **   flag_longlong               TRUE if the letter 'll' (ell ell) prefixed
303a34b6764Sdrh     **                               the conversion character.
304a18c5681Sdrh     **   flag_blanksign              TRUE if a ' ' is present.
305a18c5681Sdrh     **   width                       The specified field width.  This is
306a18c5681Sdrh     **                               always non-negative.  Zero is the default.
307a18c5681Sdrh     **   precision                   The specified precision.  The default
308a18c5681Sdrh     **                               is -1.
309a18c5681Sdrh     **   xtype                       The class of the conversion.
310a18c5681Sdrh     **   infop                       Pointer to the appropriate info struct.
311a18c5681Sdrh     */
312a18c5681Sdrh     switch( xtype ){
313fe63d1c9Sdrh       case etPOINTER:
314fe63d1c9Sdrh         flag_longlong = sizeof(char*)==sizeof(i64);
315fe63d1c9Sdrh         flag_long = sizeof(char*)==sizeof(long int);
316fe63d1c9Sdrh         /* Fall through into the next case */
3179a99334dSdrh       case etORDINAL:
318a18c5681Sdrh       case etRADIX:
319e84a306bSdrh         if( infop->flags & FLAG_SIGNED ){
320e9707671Sdrh           i64 v;
321eeb23a4cSdrh           if( flag_longlong ){
322eeb23a4cSdrh             v = va_arg(ap,i64);
323eeb23a4cSdrh           }else if( flag_long ){
324eeb23a4cSdrh             v = va_arg(ap,long int);
325eeb23a4cSdrh           }else{
326eeb23a4cSdrh             v = va_arg(ap,int);
327eeb23a4cSdrh           }
328e9707671Sdrh           if( v<0 ){
329158b9cb9Sdrh             if( v==SMALLEST_INT64 ){
330158b9cb9Sdrh               longvalue = ((u64)1)<<63;
331158b9cb9Sdrh             }else{
332158b9cb9Sdrh               longvalue = -v;
333158b9cb9Sdrh             }
334cfcdaefeSdanielk1977             prefix = '-';
335cfcdaefeSdanielk1977           }else{
336e9707671Sdrh             longvalue = v;
337e9707671Sdrh             if( flag_plussign )        prefix = '+';
338a18c5681Sdrh             else if( flag_blanksign )  prefix = ' ';
339a18c5681Sdrh             else                       prefix = 0;
340cfcdaefeSdanielk1977           }
341e9707671Sdrh         }else{
342eeb23a4cSdrh           if( flag_longlong ){
343eeb23a4cSdrh             longvalue = va_arg(ap,u64);
344eeb23a4cSdrh           }else if( flag_long ){
345eeb23a4cSdrh             longvalue = va_arg(ap,unsigned long int);
346eeb23a4cSdrh           }else{
347eeb23a4cSdrh             longvalue = va_arg(ap,unsigned int);
348eeb23a4cSdrh           }
349e9707671Sdrh           prefix = 0;
350e9707671Sdrh         }
351e9707671Sdrh         if( longvalue==0 ) flag_alternateform = 0;
352a18c5681Sdrh         if( flag_zeropad && precision<width-(prefix!=0) ){
353a18c5681Sdrh           precision = width-(prefix!=0);
354a18c5681Sdrh         }
35559eedf79Sdrh         if( precision<etBUFSIZE-10 ){
35659eedf79Sdrh           nOut = etBUFSIZE;
35759eedf79Sdrh           zOut = buf;
35859eedf79Sdrh         }else{
35959eedf79Sdrh           nOut = precision + 10;
36059eedf79Sdrh           zOut = zExtra = sqlite3Malloc( nOut );
36159eedf79Sdrh           if( zOut==0 ){
36259eedf79Sdrh             pAccum->mallocFailed = 1;
36359eedf79Sdrh             return;
36459eedf79Sdrh           }
36559eedf79Sdrh         }
36659eedf79Sdrh         bufpt = &zOut[nOut-1];
3679a99334dSdrh         if( xtype==etORDINAL ){
36843f6e064Sdrh           static const char zOrd[] = "thstndrd";
369ea678832Sdrh           int x = (int)(longvalue % 10);
37043f6e064Sdrh           if( x>=4 || (longvalue/10)%10==1 ){
37143f6e064Sdrh             x = 0;
37243f6e064Sdrh           }
37359eedf79Sdrh           *(--bufpt) = zOrd[x*2+1];
37459eedf79Sdrh           *(--bufpt) = zOrd[x*2];
3759a99334dSdrh         }
376a18c5681Sdrh         {
37776ff3a0eSdrh           register const char *cset;      /* Use registers for speed */
378a18c5681Sdrh           register int base;
37976ff3a0eSdrh           cset = &aDigits[infop->charset];
380a18c5681Sdrh           base = infop->base;
381a18c5681Sdrh           do{                                           /* Convert to ascii */
382a18c5681Sdrh             *(--bufpt) = cset[longvalue%base];
383a18c5681Sdrh             longvalue = longvalue/base;
384a18c5681Sdrh           }while( longvalue>0 );
385a18c5681Sdrh         }
38659eedf79Sdrh         length = (int)(&zOut[nOut-1]-bufpt);
387a18c5681Sdrh         for(idx=precision-length; idx>0; idx--){
388a18c5681Sdrh           *(--bufpt) = '0';                             /* Zero pad */
389a18c5681Sdrh         }
390a18c5681Sdrh         if( prefix ) *(--bufpt) = prefix;               /* Add sign */
391a18c5681Sdrh         if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
39276ff3a0eSdrh           const char *pre;
39376ff3a0eSdrh           char x;
39476ff3a0eSdrh           pre = &aPrefix[infop->prefix];
39576ff3a0eSdrh           for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
396a18c5681Sdrh         }
39759eedf79Sdrh         length = (int)(&zOut[nOut-1]-bufpt);
398a18c5681Sdrh         break;
399a18c5681Sdrh       case etFLOAT:
400a18c5681Sdrh       case etEXP:
401a18c5681Sdrh       case etGENERIC:
402a18c5681Sdrh         realvalue = va_arg(ap,double);
40369ef7036Sdrh #ifdef SQLITE_OMIT_FLOATING_POINT
40469ef7036Sdrh         length = 0;
40569ef7036Sdrh #else
406a18c5681Sdrh         if( precision<0 ) precision = 6;         /* Set default precision */
407a18c5681Sdrh         if( realvalue<0.0 ){
408a18c5681Sdrh           realvalue = -realvalue;
409a18c5681Sdrh           prefix = '-';
410a18c5681Sdrh         }else{
411a18c5681Sdrh           if( flag_plussign )          prefix = '+';
412a18c5681Sdrh           else if( flag_blanksign )    prefix = ' ';
413a18c5681Sdrh           else                         prefix = 0;
414a18c5681Sdrh         }
4153e9aeec0Sdrh         if( xtype==etGENERIC && precision>0 ) precision--;
4165f968436Sdrh #if 0
417a18c5681Sdrh         /* Rounding works like BSD when the constant 0.4999 is used.  Wierd! */
418a18c5681Sdrh         for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
419a18c5681Sdrh #else
420a18c5681Sdrh         /* It makes more sense to use 0.5 */
42174161705Sdrh         for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
422a18c5681Sdrh #endif
4233e9aeec0Sdrh         if( xtype==etFLOAT ) realvalue += rounder;
424a18c5681Sdrh         /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
425a18c5681Sdrh         exp = 0;
426ea678832Sdrh         if( sqlite3IsNaN((double)realvalue) ){
42753c14021Sdrh           bufpt = "NaN";
42853c14021Sdrh           length = 3;
42953c14021Sdrh           break;
43053c14021Sdrh         }
431a18c5681Sdrh         if( realvalue>0.0 ){
432*72b3fbc7Sdrh           LONGDOUBLE_TYPE scale = 1.0;
433*72b3fbc7Sdrh           while( realvalue>=1e64*scale && exp<=350 ){ scale *= 1e64; exp+=64; }
434*72b3fbc7Sdrh           while( realvalue>=1e8*scale && exp<=350 ){ scale *= 1e8; exp+=8; }
435*72b3fbc7Sdrh           while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
436*72b3fbc7Sdrh           realvalue /= scale;
437af005fbcSdrh           while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
438af005fbcSdrh           while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
439af005fbcSdrh           if( exp>350 ){
44053c14021Sdrh             if( prefix=='-' ){
44153c14021Sdrh               bufpt = "-Inf";
44253c14021Sdrh             }else if( prefix=='+' ){
44353c14021Sdrh               bufpt = "+Inf";
44453c14021Sdrh             }else{
44553c14021Sdrh               bufpt = "Inf";
44653c14021Sdrh             }
447ea678832Sdrh             length = sqlite3Strlen30(bufpt);
448a18c5681Sdrh             break;
449a18c5681Sdrh           }
450a18c5681Sdrh         }
451a18c5681Sdrh         bufpt = buf;
452a18c5681Sdrh         /*
453a18c5681Sdrh         ** If the field type is etGENERIC, then convert to either etEXP
454a18c5681Sdrh         ** or etFLOAT, as appropriate.
455a18c5681Sdrh         */
456a18c5681Sdrh         if( xtype!=etFLOAT ){
457a18c5681Sdrh           realvalue += rounder;
458a18c5681Sdrh           if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
459a18c5681Sdrh         }
460a18c5681Sdrh         if( xtype==etGENERIC ){
461a18c5681Sdrh           flag_rtz = !flag_alternateform;
462a18c5681Sdrh           if( exp<-4 || exp>precision ){
463a18c5681Sdrh             xtype = etEXP;
464a18c5681Sdrh           }else{
465a18c5681Sdrh             precision = precision - exp;
466a18c5681Sdrh             xtype = etFLOAT;
467a18c5681Sdrh           }
468a18c5681Sdrh         }else{
469*72b3fbc7Sdrh           flag_rtz = flag_altform2;
470a18c5681Sdrh         }
471557cc60fSdrh         if( xtype==etEXP ){
472557cc60fSdrh           e2 = 0;
473557cc60fSdrh         }else{
474557cc60fSdrh           e2 = exp;
475557cc60fSdrh         }
47659eedf79Sdrh         if( e2+precision+width > etBUFSIZE - 15 ){
47759eedf79Sdrh           bufpt = zExtra = sqlite3Malloc( e2+precision+width+15 );
47859eedf79Sdrh           if( bufpt==0 ){
47959eedf79Sdrh             pAccum->mallocFailed = 1;
48059eedf79Sdrh             return;
48159eedf79Sdrh           }
48259eedf79Sdrh         }
48359eedf79Sdrh         zOut = bufpt;
484*72b3fbc7Sdrh         nsd = 16 + flag_altform2*10;
485ea678832Sdrh         flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
486557cc60fSdrh         /* The sign in front of the number */
487557cc60fSdrh         if( prefix ){
488557cc60fSdrh           *(bufpt++) = prefix;
489557cc60fSdrh         }
490557cc60fSdrh         /* Digits prior to the decimal point */
491557cc60fSdrh         if( e2<0 ){
492557cc60fSdrh           *(bufpt++) = '0';
493557cc60fSdrh         }else{
494557cc60fSdrh           for(; e2>=0; e2--){
495557cc60fSdrh             *(bufpt++) = et_getdigit(&realvalue,&nsd);
496557cc60fSdrh           }
497557cc60fSdrh         }
498557cc60fSdrh         /* The decimal point */
499557cc60fSdrh         if( flag_dp ){
500557cc60fSdrh           *(bufpt++) = '.';
501557cc60fSdrh         }
502557cc60fSdrh         /* "0" digits after the decimal point but before the first
503557cc60fSdrh         ** significant digit of the number */
504af005fbcSdrh         for(e2++; e2<0; precision--, e2++){
505af005fbcSdrh           assert( precision>0 );
506a18c5681Sdrh           *(bufpt++) = '0';
507a18c5681Sdrh         }
508557cc60fSdrh         /* Significant digits after the decimal point */
509557cc60fSdrh         while( (precision--)>0 ){
510557cc60fSdrh           *(bufpt++) = et_getdigit(&realvalue,&nsd);
511a18c5681Sdrh         }
512557cc60fSdrh         /* Remove trailing zeros and the "." if no digits follow the "." */
513557cc60fSdrh         if( flag_rtz && flag_dp ){
5143e9aeec0Sdrh           while( bufpt[-1]=='0' ) *(--bufpt) = 0;
51559eedf79Sdrh           assert( bufpt>zOut );
5163e9aeec0Sdrh           if( bufpt[-1]=='.' ){
517557cc60fSdrh             if( flag_altform2 ){
518557cc60fSdrh               *(bufpt++) = '0';
519557cc60fSdrh             }else{
520557cc60fSdrh               *(--bufpt) = 0;
521a18c5681Sdrh             }
522557cc60fSdrh           }
523557cc60fSdrh         }
524557cc60fSdrh         /* Add the "eNNN" suffix */
52559eedf79Sdrh         if( xtype==etEXP ){
52676ff3a0eSdrh           *(bufpt++) = aDigits[infop->charset];
527557cc60fSdrh           if( exp<0 ){
528557cc60fSdrh             *(bufpt++) = '-'; exp = -exp;
529557cc60fSdrh           }else{
530557cc60fSdrh             *(bufpt++) = '+';
531557cc60fSdrh           }
532a18c5681Sdrh           if( exp>=100 ){
533ea678832Sdrh             *(bufpt++) = (char)((exp/100)+'0');        /* 100's digit */
534a18c5681Sdrh             exp %= 100;
535a18c5681Sdrh           }
536ea678832Sdrh           *(bufpt++) = (char)(exp/10+'0');             /* 10's digit */
537ea678832Sdrh           *(bufpt++) = (char)(exp%10+'0');             /* 1's digit */
538a18c5681Sdrh         }
539557cc60fSdrh         *bufpt = 0;
540557cc60fSdrh 
541a18c5681Sdrh         /* The converted number is in buf[] and zero terminated. Output it.
542a18c5681Sdrh         ** Note that the number is in the usual order, not reversed as with
543a18c5681Sdrh         ** integer conversions. */
54459eedf79Sdrh         length = (int)(bufpt-zOut);
54559eedf79Sdrh         bufpt = zOut;
546a18c5681Sdrh 
547a18c5681Sdrh         /* Special case:  Add leading zeros if the flag_zeropad flag is
548a18c5681Sdrh         ** set and we are not left justified */
549a18c5681Sdrh         if( flag_zeropad && !flag_leftjustify && length < width){
550a18c5681Sdrh           int i;
551a18c5681Sdrh           int nPad = width - length;
552a18c5681Sdrh           for(i=width; i>=nPad; i--){
553a18c5681Sdrh             bufpt[i] = bufpt[i-nPad];
554a18c5681Sdrh           }
555a18c5681Sdrh           i = prefix!=0;
556a18c5681Sdrh           while( nPad-- ) bufpt[i++] = '0';
557a18c5681Sdrh           length = width;
558a18c5681Sdrh         }
55969ef7036Sdrh #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
560a18c5681Sdrh         break;
561a18c5681Sdrh       case etSIZE:
562ade86483Sdrh         *(va_arg(ap,int*)) = pAccum->nChar;
563a18c5681Sdrh         length = width = 0;
564a18c5681Sdrh         break;
565a18c5681Sdrh       case etPERCENT:
566a18c5681Sdrh         buf[0] = '%';
567a18c5681Sdrh         bufpt = buf;
568a18c5681Sdrh         length = 1;
569a18c5681Sdrh         break;
570a18c5681Sdrh       case etCHARX:
571ea678832Sdrh         c = va_arg(ap,int);
572ea678832Sdrh         buf[0] = (char)c;
573a18c5681Sdrh         if( precision>=0 ){
574ea678832Sdrh           for(idx=1; idx<precision; idx++) buf[idx] = (char)c;
575a18c5681Sdrh           length = precision;
576a18c5681Sdrh         }else{
577a18c5681Sdrh           length =1;
578a18c5681Sdrh         }
579a18c5681Sdrh         bufpt = buf;
580a18c5681Sdrh         break;
581a18c5681Sdrh       case etSTRING:
582d93d8a81Sdrh       case etDYNSTRING:
583cb485882Sdrh         bufpt = va_arg(ap,char*);
584d93d8a81Sdrh         if( bufpt==0 ){
585d93d8a81Sdrh           bufpt = "";
586d93d8a81Sdrh         }else if( xtype==etDYNSTRING ){
587d93d8a81Sdrh           zExtra = bufpt;
588d93d8a81Sdrh         }
589e509094bSdrh         if( precision>=0 ){
590e509094bSdrh           for(length=0; length<precision && bufpt[length]; length++){}
591e509094bSdrh         }else{
592ea678832Sdrh           length = sqlite3Strlen30(bufpt);
593e509094bSdrh         }
594a18c5681Sdrh         break;
595a18c5681Sdrh       case etSQLESCAPE:
596f3b863edSdanielk1977       case etSQLESCAPE2:
597f3b863edSdanielk1977       case etSQLESCAPE3: {
5988965b50eSdrh         int i, j, k, n, isnull;
5994794f735Sdrh         int needQuote;
600ea678832Sdrh         char ch;
601f3b863edSdanielk1977         char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
602f0113000Sdanielk1977         char *escarg = va_arg(ap,char*);
603f0113000Sdanielk1977         isnull = escarg==0;
604f0113000Sdanielk1977         if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
6058965b50eSdrh         k = precision;
60660d4a304Sdan         for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
607f3b863edSdanielk1977           if( ch==q )  n++;
608a18c5681Sdrh         }
6094794f735Sdrh         needQuote = !isnull && xtype==etSQLESCAPE2;
6104794f735Sdrh         n += i + 1 + needQuote*2;
611a18c5681Sdrh         if( n>etBUFSIZE ){
612e5ae5735Sdrh           bufpt = zExtra = sqlite3Malloc( n );
613d164fd34Sdrh           if( bufpt==0 ){
614d164fd34Sdrh             pAccum->mallocFailed = 1;
615d164fd34Sdrh             return;
616d164fd34Sdrh           }
617a18c5681Sdrh         }else{
618a18c5681Sdrh           bufpt = buf;
619a18c5681Sdrh         }
6200cfcf3fbSchw         j = 0;
621f3b863edSdanielk1977         if( needQuote ) bufpt[j++] = q;
6228965b50eSdrh         k = i;
6238965b50eSdrh         for(i=0; i<k; i++){
6248965b50eSdrh           bufpt[j++] = ch = escarg[i];
625f3b863edSdanielk1977           if( ch==q ) bufpt[j++] = ch;
626a18c5681Sdrh         }
627f3b863edSdanielk1977         if( needQuote ) bufpt[j++] = q;
628a18c5681Sdrh         bufpt[j] = 0;
629a18c5681Sdrh         length = j;
6308965b50eSdrh         /* The precision in %q and %Q means how many input characters to
6318965b50eSdrh         ** consume, not the length of the output...
6328965b50eSdrh         ** if( precision>=0 && precision<length ) length = precision; */
633a18c5681Sdrh         break;
6345eba8c09Sdrh       }
6355f968436Sdrh       case etTOKEN: {
6365f968436Sdrh         Token *pToken = va_arg(ap, Token*);
637af005fbcSdrh         if( pToken ){
638ade86483Sdrh           sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
639ad6d9460Sdrh         }
6405f968436Sdrh         length = width = 0;
6415f968436Sdrh         break;
6425f968436Sdrh       }
6435f968436Sdrh       case etSRCLIST: {
6445f968436Sdrh         SrcList *pSrc = va_arg(ap, SrcList*);
6455f968436Sdrh         int k = va_arg(ap, int);
6465f968436Sdrh         struct SrcList_item *pItem = &pSrc->a[k];
6475f968436Sdrh         assert( k>=0 && k<pSrc->nSrc );
64893a960a0Sdrh         if( pItem->zDatabase ){
649ade86483Sdrh           sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1);
650ade86483Sdrh           sqlite3StrAccumAppend(pAccum, ".", 1);
6515f968436Sdrh         }
652ade86483Sdrh         sqlite3StrAccumAppend(pAccum, pItem->zName, -1);
6535f968436Sdrh         length = width = 0;
6545f968436Sdrh         break;
6555f968436Sdrh       }
656874ba04cSdrh       default: {
657874ba04cSdrh         assert( xtype==etINVALID );
658874ba04cSdrh         return;
659874ba04cSdrh       }
660a18c5681Sdrh     }/* End switch over the format type */
661a18c5681Sdrh     /*
662a18c5681Sdrh     ** The text of the conversion is pointed to by "bufpt" and is
663a18c5681Sdrh     ** "length" characters long.  The field width is "width".  Do
664a18c5681Sdrh     ** the output.
665a18c5681Sdrh     */
666a18c5681Sdrh     if( !flag_leftjustify ){
667a18c5681Sdrh       register int nspace;
668a18c5681Sdrh       nspace = width-length;
669a18c5681Sdrh       if( nspace>0 ){
6707e02e5e6Sdrh         sqlite3AppendSpace(pAccum, nspace);
671a18c5681Sdrh       }
672a18c5681Sdrh     }
673a18c5681Sdrh     if( length>0 ){
674ade86483Sdrh       sqlite3StrAccumAppend(pAccum, bufpt, length);
675a18c5681Sdrh     }
676a18c5681Sdrh     if( flag_leftjustify ){
677a18c5681Sdrh       register int nspace;
678a18c5681Sdrh       nspace = width-length;
679a18c5681Sdrh       if( nspace>0 ){
6807e02e5e6Sdrh         sqlite3AppendSpace(pAccum, nspace);
681a18c5681Sdrh       }
682a18c5681Sdrh     }
6831e536953Sdanielk1977     sqlite3_free(zExtra);
684a18c5681Sdrh   }/* End for loop over the format string */
685a18c5681Sdrh } /* End of function */
686a18c5681Sdrh 
687a18c5681Sdrh /*
688ade86483Sdrh ** Append N bytes of text from z to the StrAccum object.
689a18c5681Sdrh */
690ade86483Sdrh void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
691bc6160b0Sdrh   assert( z!=0 || N==0 );
692ade86483Sdrh   if( p->tooBig | p->mallocFailed ){
693bc6160b0Sdrh     testcase(p->tooBig);
694bc6160b0Sdrh     testcase(p->mallocFailed);
69517435752Sdrh     return;
696ade86483Sdrh   }
697b07028f7Sdrh   assert( p->zText!=0 || p->nChar==0 );
698ade86483Sdrh   if( N<0 ){
699ea678832Sdrh     N = sqlite3Strlen30(z);
700ade86483Sdrh   }
701bc6160b0Sdrh   if( N==0 || NEVER(z==0) ){
702ade86483Sdrh     return;
703ade86483Sdrh   }
704ade86483Sdrh   if( p->nChar+N >= p->nAlloc ){
705ade86483Sdrh     char *zNew;
706ade86483Sdrh     if( !p->useMalloc ){
707ade86483Sdrh       p->tooBig = 1;
708ade86483Sdrh       N = p->nAlloc - p->nChar - 1;
709ade86483Sdrh       if( N<=0 ){
710ade86483Sdrh         return;
7115f968436Sdrh       }
712a18c5681Sdrh     }else{
713a9ef7097Sdan       char *zOld = (p->zText==p->zBase ? 0 : p->zText);
71493a960a0Sdrh       i64 szNew = p->nChar;
715b1a6c3c1Sdrh       szNew += N + 1;
716b1a6c3c1Sdrh       if( szNew > p->mxAlloc ){
717ade86483Sdrh         sqlite3StrAccumReset(p);
718ade86483Sdrh         p->tooBig = 1;
719eaad32b1Sdrh         return;
720b1a6c3c1Sdrh       }else{
721ea678832Sdrh         p->nAlloc = (int)szNew;
722a18c5681Sdrh       }
723b975598eSdrh       if( p->useMalloc==1 ){
724a9ef7097Sdan         zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
725b975598eSdrh       }else{
726a9ef7097Sdan         zNew = sqlite3_realloc(zOld, p->nAlloc);
727b975598eSdrh       }
72853f733c7Sdrh       if( zNew ){
729b07028f7Sdrh         if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
730ade86483Sdrh         p->zText = zNew;
731ade86483Sdrh       }else{
732ade86483Sdrh         p->mallocFailed = 1;
733ade86483Sdrh         sqlite3StrAccumReset(p);
734ade86483Sdrh         return;
73553f733c7Sdrh       }
7365f968436Sdrh     }
7375f968436Sdrh   }
738b07028f7Sdrh   assert( p->zText );
739ade86483Sdrh   memcpy(&p->zText[p->nChar], z, N);
740ade86483Sdrh   p->nChar += N;
741483750baSdrh }
742483750baSdrh 
743483750baSdrh /*
744ade86483Sdrh ** Finish off a string by making sure it is zero-terminated.
745ade86483Sdrh ** Return a pointer to the resulting string.  Return a NULL
746ade86483Sdrh ** pointer if any kind of error was encountered.
7475f968436Sdrh */
748ade86483Sdrh char *sqlite3StrAccumFinish(StrAccum *p){
749ade86483Sdrh   if( p->zText ){
750ade86483Sdrh     p->zText[p->nChar] = 0;
751ade86483Sdrh     if( p->useMalloc && p->zText==p->zBase ){
752b975598eSdrh       if( p->useMalloc==1 ){
753633e6d57Sdrh         p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
754b975598eSdrh       }else{
755b975598eSdrh         p->zText = sqlite3_malloc(p->nChar+1);
756b975598eSdrh       }
757ade86483Sdrh       if( p->zText ){
758ade86483Sdrh         memcpy(p->zText, p->zBase, p->nChar+1);
759ade86483Sdrh       }else{
760ade86483Sdrh         p->mallocFailed = 1;
761ade86483Sdrh       }
762ade86483Sdrh     }
763ade86483Sdrh   }
764ade86483Sdrh   return p->zText;
765ade86483Sdrh }
766ade86483Sdrh 
767ade86483Sdrh /*
768ade86483Sdrh ** Reset an StrAccum string.  Reclaim all malloced memory.
769ade86483Sdrh */
770ade86483Sdrh void sqlite3StrAccumReset(StrAccum *p){
771ade86483Sdrh   if( p->zText!=p->zBase ){
772b975598eSdrh     if( p->useMalloc==1 ){
773633e6d57Sdrh       sqlite3DbFree(p->db, p->zText);
774b975598eSdrh     }else{
775b975598eSdrh       sqlite3_free(p->zText);
776b975598eSdrh     }
777ade86483Sdrh   }
778f089aa45Sdrh   p->zText = 0;
779ade86483Sdrh }
780ade86483Sdrh 
781ade86483Sdrh /*
782ade86483Sdrh ** Initialize a string accumulator
783ade86483Sdrh */
784f089aa45Sdrh void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
785ade86483Sdrh   p->zText = p->zBase = zBase;
786633e6d57Sdrh   p->db = 0;
787ade86483Sdrh   p->nChar = 0;
788ade86483Sdrh   p->nAlloc = n;
789bb4957f8Sdrh   p->mxAlloc = mx;
790ade86483Sdrh   p->useMalloc = 1;
791ade86483Sdrh   p->tooBig = 0;
792ade86483Sdrh   p->mallocFailed = 0;
7935f968436Sdrh }
7945f968436Sdrh 
7955f968436Sdrh /*
7965f968436Sdrh ** Print into memory obtained from sqliteMalloc().  Use the internal
7975f968436Sdrh ** %-conversion extensions.
7985f968436Sdrh */
79917435752Sdrh char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
80017435752Sdrh   char *z;
80179158e18Sdrh   char zBase[SQLITE_PRINT_BUF_SIZE];
802ade86483Sdrh   StrAccum acc;
803bc6160b0Sdrh   assert( db!=0 );
804bb4957f8Sdrh   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
805bc6160b0Sdrh                       db->aLimit[SQLITE_LIMIT_LENGTH]);
806633e6d57Sdrh   acc.db = db;
807f089aa45Sdrh   sqlite3VXPrintf(&acc, 1, zFormat, ap);
808ade86483Sdrh   z = sqlite3StrAccumFinish(&acc);
809bc6160b0Sdrh   if( acc.mallocFailed ){
81017435752Sdrh     db->mallocFailed = 1;
81117435752Sdrh   }
81217435752Sdrh   return z;
8135f968436Sdrh }
8145f968436Sdrh 
8155f968436Sdrh /*
8165f968436Sdrh ** Print into memory obtained from sqliteMalloc().  Use the internal
8175f968436Sdrh ** %-conversion extensions.
8185f968436Sdrh */
81917435752Sdrh char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
8205f968436Sdrh   va_list ap;
8215f968436Sdrh   char *z;
8225f968436Sdrh   va_start(ap, zFormat);
823ade86483Sdrh   z = sqlite3VMPrintf(db, zFormat, ap);
8245f968436Sdrh   va_end(ap);
8255f968436Sdrh   return z;
8265f968436Sdrh }
8275f968436Sdrh 
8285f968436Sdrh /*
829633e6d57Sdrh ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
830633e6d57Sdrh ** the string and before returnning.  This routine is intended to be used
831633e6d57Sdrh ** to modify an existing string.  For example:
832633e6d57Sdrh **
833633e6d57Sdrh **       x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
834633e6d57Sdrh **
835633e6d57Sdrh */
836633e6d57Sdrh char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
837633e6d57Sdrh   va_list ap;
838633e6d57Sdrh   char *z;
839633e6d57Sdrh   va_start(ap, zFormat);
840633e6d57Sdrh   z = sqlite3VMPrintf(db, zFormat, ap);
841633e6d57Sdrh   va_end(ap);
842633e6d57Sdrh   sqlite3DbFree(db, zStr);
843633e6d57Sdrh   return z;
844633e6d57Sdrh }
845633e6d57Sdrh 
846633e6d57Sdrh /*
84728dd479cSdrh ** Print into memory obtained from sqlite3_malloc().  Omit the internal
84828dd479cSdrh ** %-conversion extensions.
84928dd479cSdrh */
85028dd479cSdrh char *sqlite3_vmprintf(const char *zFormat, va_list ap){
851ade86483Sdrh   char *z;
85228dd479cSdrh   char zBase[SQLITE_PRINT_BUF_SIZE];
853ade86483Sdrh   StrAccum acc;
854ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT
855ff1590eeSdrh   if( sqlite3_initialize() ) return 0;
856ff1590eeSdrh #endif
857bb4957f8Sdrh   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
858b975598eSdrh   acc.useMalloc = 2;
859f089aa45Sdrh   sqlite3VXPrintf(&acc, 0, zFormat, ap);
860ade86483Sdrh   z = sqlite3StrAccumFinish(&acc);
861ade86483Sdrh   return z;
86228dd479cSdrh }
86328dd479cSdrh 
86428dd479cSdrh /*
86528dd479cSdrh ** Print into memory obtained from sqlite3_malloc()().  Omit the internal
86628dd479cSdrh ** %-conversion extensions.
867483750baSdrh */
8686f8a503dSdanielk1977 char *sqlite3_mprintf(const char *zFormat, ...){
869a18c5681Sdrh   va_list ap;
8705f968436Sdrh   char *z;
871ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT
872ff1590eeSdrh   if( sqlite3_initialize() ) return 0;
873ff1590eeSdrh #endif
874a18c5681Sdrh   va_start(ap, zFormat);
875b3738b6cSdrh   z = sqlite3_vmprintf(zFormat, ap);
876a18c5681Sdrh   va_end(ap);
8775f968436Sdrh   return z;
878a18c5681Sdrh }
879a18c5681Sdrh 
880a18c5681Sdrh /*
8816f8a503dSdanielk1977 ** sqlite3_snprintf() works like snprintf() except that it ignores the
88293a5c6bdSdrh ** current locale settings.  This is important for SQLite because we
88393a5c6bdSdrh ** are not able to use a "," as the decimal point in place of "." as
88493a5c6bdSdrh ** specified by some locales.
885db26d4c9Sdrh **
886db26d4c9Sdrh ** Oops:  The first two arguments of sqlite3_snprintf() are backwards
887db26d4c9Sdrh ** from the snprintf() standard.  Unfortunately, it is too late to change
888db26d4c9Sdrh ** this without breaking compatibility, so we just have to live with the
889db26d4c9Sdrh ** mistake.
890db26d4c9Sdrh **
891db26d4c9Sdrh ** sqlite3_vsnprintf() is the varargs version.
89293a5c6bdSdrh */
893db26d4c9Sdrh char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
894db26d4c9Sdrh   StrAccum acc;
895db26d4c9Sdrh   if( n<=0 ) return zBuf;
896db26d4c9Sdrh   sqlite3StrAccumInit(&acc, zBuf, n, 0);
897db26d4c9Sdrh   acc.useMalloc = 0;
898db26d4c9Sdrh   sqlite3VXPrintf(&acc, 0, zFormat, ap);
899db26d4c9Sdrh   return sqlite3StrAccumFinish(&acc);
900db26d4c9Sdrh }
9016f8a503dSdanielk1977 char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
9025f968436Sdrh   char *z;
90393a5c6bdSdrh   va_list ap;
90493a5c6bdSdrh   va_start(ap,zFormat);
905db26d4c9Sdrh   z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
90693a5c6bdSdrh   va_end(ap);
9075f968436Sdrh   return z;
90893a5c6bdSdrh }
90993a5c6bdSdrh 
9103f280701Sdrh /*
9117c0c460fSdrh ** This is the routine that actually formats the sqlite3_log() message.
9127c0c460fSdrh ** We house it in a separate routine from sqlite3_log() to avoid using
9137c0c460fSdrh ** stack space on small-stack systems when logging is disabled.
9147c0c460fSdrh **
9157c0c460fSdrh ** sqlite3_log() must render into a static buffer.  It cannot dynamically
9167c0c460fSdrh ** allocate memory because it might be called while the memory allocator
9177c0c460fSdrh ** mutex is held.
9187c0c460fSdrh */
9197c0c460fSdrh static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
9207c0c460fSdrh   StrAccum acc;                          /* String accumulator */
921a64fa912Sdrh   char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
9227c0c460fSdrh 
9237c0c460fSdrh   sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0);
9247c0c460fSdrh   acc.useMalloc = 0;
9257c0c460fSdrh   sqlite3VXPrintf(&acc, 0, zFormat, ap);
9267c0c460fSdrh   sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
9277c0c460fSdrh                            sqlite3StrAccumFinish(&acc));
9287c0c460fSdrh }
9297c0c460fSdrh 
9307c0c460fSdrh /*
9313f280701Sdrh ** Format and write a message to the log if logging is enabled.
9323f280701Sdrh */
933a7564663Sdrh void sqlite3_log(int iErrCode, const char *zFormat, ...){
9343f280701Sdrh   va_list ap;                             /* Vararg list */
9357c0c460fSdrh   if( sqlite3GlobalConfig.xLog ){
9363f280701Sdrh     va_start(ap, zFormat);
9377c0c460fSdrh     renderLogMsg(iErrCode, zFormat, ap);
9383f280701Sdrh     va_end(ap);
9393f280701Sdrh   }
9403f280701Sdrh }
9413f280701Sdrh 
94285e9e22bSdrh #if defined(SQLITE_DEBUG)
943e54ca3feSdrh /*
944e54ca3feSdrh ** A version of printf() that understands %lld.  Used for debugging.
945e54ca3feSdrh ** The printf() built into some versions of windows does not understand %lld
946e54ca3feSdrh ** and segfaults if you give it a long long int.
947e54ca3feSdrh */
948e54ca3feSdrh void sqlite3DebugPrintf(const char *zFormat, ...){
949e54ca3feSdrh   va_list ap;
950ade86483Sdrh   StrAccum acc;
951e54ca3feSdrh   char zBuf[500];
952bb4957f8Sdrh   sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
953ade86483Sdrh   acc.useMalloc = 0;
954e54ca3feSdrh   va_start(ap,zFormat);
955f089aa45Sdrh   sqlite3VXPrintf(&acc, 0, zFormat, ap);
956e54ca3feSdrh   va_end(ap);
957bed8e7e5Sdrh   sqlite3StrAccumFinish(&acc);
958485f0039Sdrh   fprintf(stdout,"%s", zBuf);
9592ac3ee97Sdrh   fflush(stdout);
960e54ca3feSdrh }
961e54ca3feSdrh #endif
962c7bc4fdeSdrh 
963c7bc4fdeSdrh #ifndef SQLITE_OMIT_TRACE
964c7bc4fdeSdrh /*
965c7bc4fdeSdrh ** variable-argument wrapper around sqlite3VXPrintf().
966c7bc4fdeSdrh */
967c7bc4fdeSdrh void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){
968c7bc4fdeSdrh   va_list ap;
969c7bc4fdeSdrh   va_start(ap,zFormat);
970c7bc4fdeSdrh   sqlite3VXPrintf(p, 1, zFormat, ap);
971c7bc4fdeSdrh   va_end(ap);
972c7bc4fdeSdrh }
973c7bc4fdeSdrh #endif
974