xref: /sqlite-3.40.0/src/printf.c (revision ea678832)
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 **
8*ea678832Sdrh ** $Id: printf.c,v 1.99 2008/12/10 19:26:24 drh Exp $
9822a5162Sdanielk1977 **
10e84a306bSdrh **************************************************************************
11a18c5681Sdrh **
12e78e8284Sdrh ** The following modules is an enhanced replacement for the "printf" subroutines
13e78e8284Sdrh ** found in the standard C library.  The following enhancements are
14a18c5681Sdrh ** supported:
15a18c5681Sdrh **
16a18c5681Sdrh **      +  Additional functions.  The standard set of "printf" functions
17a18c5681Sdrh **         includes printf, fprintf, sprintf, vprintf, vfprintf, and
18a18c5681Sdrh **         vsprintf.  This module adds the following:
19a18c5681Sdrh **
20a18c5681Sdrh **           *  snprintf -- Works like sprintf, but has an extra argument
21a18c5681Sdrh **                          which is the size of the buffer written to.
22a18c5681Sdrh **
23a18c5681Sdrh **           *  mprintf --  Similar to sprintf.  Writes output to memory
24a18c5681Sdrh **                          obtained from malloc.
25a18c5681Sdrh **
26a18c5681Sdrh **           *  xprintf --  Calls a function to dispose of output.
27a18c5681Sdrh **
28a18c5681Sdrh **           *  nprintf --  No output, but returns the number of characters
29a18c5681Sdrh **                          that would have been output by printf.
30a18c5681Sdrh **
31a18c5681Sdrh **           *  A v- version (ex: vsnprintf) of every function is also
32a18c5681Sdrh **              supplied.
33a18c5681Sdrh **
34a18c5681Sdrh **      +  A few extensions to the formatting notation are supported:
35a18c5681Sdrh **
36a18c5681Sdrh **           *  The "=" flag (similar to "-") causes the output to be
37a18c5681Sdrh **              be centered in the appropriately sized field.
38a18c5681Sdrh **
39a18c5681Sdrh **           *  The %b field outputs an integer in binary notation.
40a18c5681Sdrh **
41a18c5681Sdrh **           *  The %c field now accepts a precision.  The character output
42a18c5681Sdrh **              is repeated by the number of times the precision specifies.
43a18c5681Sdrh **
44a18c5681Sdrh **           *  The %' field works like %c, but takes as its character the
45a18c5681Sdrh **              next character of the format string, instead of the next
46a18c5681Sdrh **              argument.  For example,  printf("%.78'-")  prints 78 minus
47a18c5681Sdrh **              signs, the same as  printf("%.78c",'-').
48a18c5681Sdrh **
49a18c5681Sdrh **      +  When compiled using GCC on a SPARC, this version of printf is
50a18c5681Sdrh **         faster than the library printf for SUN OS 4.1.
51a18c5681Sdrh **
52a18c5681Sdrh **      +  All functions are fully reentrant.
53a18c5681Sdrh **
54a18c5681Sdrh */
55a18c5681Sdrh #include "sqliteInt.h"
567c68d60bSdrh 
57a18c5681Sdrh /*
58a18c5681Sdrh ** Conversion types fall into various categories as defined by the
59a18c5681Sdrh ** following enumeration.
60a18c5681Sdrh */
61e84a306bSdrh #define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
62e84a306bSdrh #define etFLOAT       2 /* Floating point.  %f */
63e84a306bSdrh #define etEXP         3 /* Exponentional notation. %e and %E */
64e84a306bSdrh #define etGENERIC     4 /* Floating or exponential, depending on exponent. %g */
65e84a306bSdrh #define etSIZE        5 /* Return number of characters processed so far. %n */
66e84a306bSdrh #define etSTRING      6 /* Strings. %s */
67e84a306bSdrh #define etDYNSTRING   7 /* Dynamically allocated strings. %z */
68e84a306bSdrh #define etPERCENT     8 /* Percent symbol. %% */
69e84a306bSdrh #define etCHARX       9 /* Characters. %c */
70a18c5681Sdrh /* The rest are extensions, not normally found in printf() */
71af005fbcSdrh #define etSQLESCAPE  10 /* Strings with '\'' doubled.  %q */
72af005fbcSdrh #define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
730cfcf3fbSchw                           NULL pointers replaced by SQL NULL.  %Q */
74af005fbcSdrh #define etTOKEN      12 /* a pointer to a Token structure */
75af005fbcSdrh #define etSRCLIST    13 /* a pointer to a SrcList */
76af005fbcSdrh #define etPOINTER    14 /* The %p conversion */
77af005fbcSdrh #define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */
78af005fbcSdrh #define etORDINAL    16 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
79e84a306bSdrh 
80e84a306bSdrh 
81e84a306bSdrh /*
82e84a306bSdrh ** An "etByte" is an 8-bit unsigned value.
83e84a306bSdrh */
84e84a306bSdrh typedef unsigned char etByte;
85a18c5681Sdrh 
86a18c5681Sdrh /*
87a18c5681Sdrh ** Each builtin conversion character (ex: the 'd' in "%d") is described
88a18c5681Sdrh ** by an instance of the following structure
89a18c5681Sdrh */
90a18c5681Sdrh typedef struct et_info {   /* Information about each format field */
91e84a306bSdrh   char fmttype;            /* The format field code letter */
92e84a306bSdrh   etByte base;             /* The base for radix conversion */
93e84a306bSdrh   etByte flags;            /* One or more of FLAG_ constants below */
94e84a306bSdrh   etByte type;             /* Conversion paradigm */
9576ff3a0eSdrh   etByte charset;          /* Offset into aDigits[] of the digits string */
9676ff3a0eSdrh   etByte prefix;           /* Offset into aPrefix[] of the prefix string */
97a18c5681Sdrh } et_info;
98a18c5681Sdrh 
99a18c5681Sdrh /*
100e84a306bSdrh ** Allowed values for et_info.flags
101e84a306bSdrh */
102e84a306bSdrh #define FLAG_SIGNED  1     /* True if the value to convert is signed */
103e84a306bSdrh #define FLAG_INTERN  2     /* True if for internal use only */
1044794f735Sdrh #define FLAG_STRING  4     /* Allow infinity precision */
105e84a306bSdrh 
106e84a306bSdrh 
107e84a306bSdrh /*
108a18c5681Sdrh ** The following table is searched linearly, so it is good to put the
109a18c5681Sdrh ** most frequently used conversion types first.
110a18c5681Sdrh */
11176ff3a0eSdrh static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
11276ff3a0eSdrh static const char aPrefix[] = "-x0\000X0";
1135719628aSdrh static const et_info fmtinfo[] = {
11476ff3a0eSdrh   {  'd', 10, 1, etRADIX,      0,  0 },
1154794f735Sdrh   {  's',  0, 4, etSTRING,     0,  0 },
116557cc60fSdrh   {  'g',  0, 1, etGENERIC,    30, 0 },
117153c62c4Sdrh   {  'z',  0, 4, etDYNSTRING,  0,  0 },
1184794f735Sdrh   {  'q',  0, 4, etSQLESCAPE,  0,  0 },
1194794f735Sdrh   {  'Q',  0, 4, etSQLESCAPE2, 0,  0 },
120f3b863edSdanielk1977   {  'w',  0, 4, etSQLESCAPE3, 0,  0 },
121e84a306bSdrh   {  'c',  0, 0, etCHARX,      0,  0 },
12276ff3a0eSdrh   {  'o',  8, 0, etRADIX,      0,  2 },
12376ff3a0eSdrh   {  'u', 10, 0, etRADIX,      0,  0 },
12476ff3a0eSdrh   {  'x', 16, 0, etRADIX,      16, 1 },
12576ff3a0eSdrh   {  'X', 16, 0, etRADIX,      0,  4 },
126b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT
127e84a306bSdrh   {  'f',  0, 1, etFLOAT,      0,  0 },
12876ff3a0eSdrh   {  'e',  0, 1, etEXP,        30, 0 },
12976ff3a0eSdrh   {  'E',  0, 1, etEXP,        14, 0 },
13076ff3a0eSdrh   {  'G',  0, 1, etGENERIC,    14, 0 },
131b37df7b9Sdrh #endif
13276ff3a0eSdrh   {  'i', 10, 1, etRADIX,      0,  0 },
133e84a306bSdrh   {  'n',  0, 0, etSIZE,       0,  0 },
134e84a306bSdrh   {  '%',  0, 0, etPERCENT,    0,  0 },
13576ff3a0eSdrh   {  'p', 16, 0, etPOINTER,    0,  1 },
1365f968436Sdrh   {  'T',  0, 2, etTOKEN,      0,  0 },
1375f968436Sdrh   {  'S',  0, 2, etSRCLIST,    0,  0 },
1389a99334dSdrh   {  'r', 10, 3, etORDINAL,    0,  0 },
139a18c5681Sdrh };
140a18c5681Sdrh 
141a18c5681Sdrh /*
142b37df7b9Sdrh ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
143a18c5681Sdrh ** conversions will work.
144a18c5681Sdrh */
145b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT
146a18c5681Sdrh /*
147a18c5681Sdrh ** "*val" is a double such that 0.1 <= *val < 10.0
148a18c5681Sdrh ** Return the ascii code for the leading digit of *val, then
149a18c5681Sdrh ** multiply "*val" by 10.0 to renormalize.
150a18c5681Sdrh **
151a18c5681Sdrh ** Example:
152a18c5681Sdrh **     input:     *val = 3.14159
153a18c5681Sdrh **     output:    *val = 1.4159    function return = '3'
154a18c5681Sdrh **
155a18c5681Sdrh ** The counter *cnt is incremented each time.  After counter exceeds
156a18c5681Sdrh ** 16 (the number of significant digits in a 64-bit float) '0' is
157a18c5681Sdrh ** always returned.
158a18c5681Sdrh */
159*ea678832Sdrh static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
160a18c5681Sdrh   int digit;
161384eef32Sdrh   LONGDOUBLE_TYPE d;
162a18c5681Sdrh   if( (*cnt)++ >= 16 ) return '0';
163a18c5681Sdrh   digit = (int)*val;
164a18c5681Sdrh   d = digit;
165a18c5681Sdrh   digit += '0';
166a18c5681Sdrh   *val = (*val - d)*10.0;
167*ea678832Sdrh   return (char)digit;
168a18c5681Sdrh }
169b37df7b9Sdrh #endif /* SQLITE_OMIT_FLOATING_POINT */
170a18c5681Sdrh 
17179158e18Sdrh /*
172ade86483Sdrh ** Append N space characters to the given string buffer.
173ade86483Sdrh */
174ade86483Sdrh static void appendSpace(StrAccum *pAccum, int N){
175ade86483Sdrh   static const char zSpaces[] = "                             ";
17600e13613Sdanielk1977   while( N>=(int)sizeof(zSpaces)-1 ){
177ade86483Sdrh     sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
178ade86483Sdrh     N -= sizeof(zSpaces)-1;
179ade86483Sdrh   }
180ade86483Sdrh   if( N>0 ){
181ade86483Sdrh     sqlite3StrAccumAppend(pAccum, zSpaces, N);
182ade86483Sdrh   }
183ade86483Sdrh }
184ade86483Sdrh 
185ade86483Sdrh /*
18679158e18Sdrh ** On machines with a small stack size, you can redefine the
18779158e18Sdrh ** SQLITE_PRINT_BUF_SIZE to be less than 350.  But beware - for
18879158e18Sdrh ** smaller values some %f conversions may go into an infinite loop.
18979158e18Sdrh */
19079158e18Sdrh #ifndef SQLITE_PRINT_BUF_SIZE
19179158e18Sdrh # define SQLITE_PRINT_BUF_SIZE 350
19279158e18Sdrh #endif
19379158e18Sdrh #define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
194a18c5681Sdrh 
195a18c5681Sdrh /*
196a18c5681Sdrh ** The root program.  All variations call this core.
197a18c5681Sdrh **
198a18c5681Sdrh ** INPUTS:
199a18c5681Sdrh **   func   This is a pointer to a function taking three arguments
200a18c5681Sdrh **            1. A pointer to anything.  Same as the "arg" parameter.
201a18c5681Sdrh **            2. A pointer to the list of characters to be output
202a18c5681Sdrh **               (Note, this list is NOT null terminated.)
203a18c5681Sdrh **            3. An integer number of characters to be output.
204a18c5681Sdrh **               (Note: This number might be zero.)
205a18c5681Sdrh **
206a18c5681Sdrh **   arg    This is the pointer to anything which will be passed as the
207a18c5681Sdrh **          first argument to "func".  Use it for whatever you like.
208a18c5681Sdrh **
209a18c5681Sdrh **   fmt    This is the format string, as in the usual print.
210a18c5681Sdrh **
211a18c5681Sdrh **   ap     This is a pointer to a list of arguments.  Same as in
212a18c5681Sdrh **          vfprint.
213a18c5681Sdrh **
214a18c5681Sdrh ** OUTPUTS:
215a18c5681Sdrh **          The return value is the total number of characters sent to
216a18c5681Sdrh **          the function "func".  Returns -1 on a error.
217a18c5681Sdrh **
218a18c5681Sdrh ** Note that the order in which automatic variables are declared below
219a18c5681Sdrh ** seems to make a big difference in determining how fast this beast
220a18c5681Sdrh ** will run.
221a18c5681Sdrh */
222f089aa45Sdrh void sqlite3VXPrintf(
223ade86483Sdrh   StrAccum *pAccum,                  /* Accumulate results here */
2245f968436Sdrh   int useExtended,                   /* Allow extended %-conversions */
2255f968436Sdrh   const char *fmt,                   /* Format string */
2265f968436Sdrh   va_list ap                         /* arguments */
227a18c5681Sdrh ){
228e84a306bSdrh   int c;                     /* Next character in the format string */
229e84a306bSdrh   char *bufpt;               /* Pointer to the conversion buffer */
230e84a306bSdrh   int precision;             /* Precision of the current field */
231e84a306bSdrh   int length;                /* Length of the field */
232e84a306bSdrh   int idx;                   /* A general purpose loop counter */
233a18c5681Sdrh   int width;                 /* Width of the current field */
234e84a306bSdrh   etByte flag_leftjustify;   /* True if "-" flag is present */
235e84a306bSdrh   etByte flag_plussign;      /* True if "+" flag is present */
236e84a306bSdrh   etByte flag_blanksign;     /* True if " " flag is present */
237e84a306bSdrh   etByte flag_alternateform; /* True if "#" flag is present */
238531fe878Sdrh   etByte flag_altform2;      /* True if "!" flag is present */
239e84a306bSdrh   etByte flag_zeropad;       /* True if field width constant starts with zero */
240e84a306bSdrh   etByte flag_long;          /* True if "l" flag is present */
241a34b6764Sdrh   etByte flag_longlong;      /* True if the "ll" flag is present */
2423e9aeec0Sdrh   etByte done;               /* Loop termination flag */
24327436af7Sdrh   sqlite_uint64 longvalue;   /* Value for integer types */
244384eef32Sdrh   LONGDOUBLE_TYPE realvalue; /* Value for real types */
2455719628aSdrh   const et_info *infop;      /* Pointer to the appropriate info structure */
246a18c5681Sdrh   char buf[etBUFSIZE];       /* Conversion buffer */
247a18c5681Sdrh   char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
248b27b7f5dSdrh   etByte xtype = 0;          /* Conversion paradigm */
249a18c5681Sdrh   char *zExtra;              /* Extra memory used for etTCLESCAPE conversions */
250b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT
251557cc60fSdrh   int  exp, e2;              /* exponent of real numbers */
252a18c5681Sdrh   double rounder;            /* Used for rounding floating point values */
253e84a306bSdrh   etByte flag_dp;            /* True if decimal point should be shown */
254e84a306bSdrh   etByte flag_rtz;           /* True if trailing zeros should be removed */
255e84a306bSdrh   etByte flag_exp;           /* True to force display of the exponent */
256a18c5681Sdrh   int nsd;                   /* Number of significant digits returned */
257a18c5681Sdrh #endif
258a18c5681Sdrh 
259ade86483Sdrh   length = 0;
260a18c5681Sdrh   bufpt = 0;
261a18c5681Sdrh   for(; (c=(*fmt))!=0; ++fmt){
262a18c5681Sdrh     if( c!='%' ){
263e84a306bSdrh       int amt;
264a18c5681Sdrh       bufpt = (char *)fmt;
265a18c5681Sdrh       amt = 1;
266a18c5681Sdrh       while( (c=(*++fmt))!='%' && c!=0 ) amt++;
267ade86483Sdrh       sqlite3StrAccumAppend(pAccum, bufpt, amt);
268a18c5681Sdrh       if( c==0 ) break;
269a18c5681Sdrh     }
270a18c5681Sdrh     if( (c=(*++fmt))==0 ){
271ade86483Sdrh       sqlite3StrAccumAppend(pAccum, "%", 1);
272a18c5681Sdrh       break;
273a18c5681Sdrh     }
274a18c5681Sdrh     /* Find out what flags are present */
275a18c5681Sdrh     flag_leftjustify = flag_plussign = flag_blanksign =
276557cc60fSdrh      flag_alternateform = flag_altform2 = flag_zeropad = 0;
2773e9aeec0Sdrh     done = 0;
278a18c5681Sdrh     do{
279a18c5681Sdrh       switch( c ){
2803e9aeec0Sdrh         case '-':   flag_leftjustify = 1;     break;
2813e9aeec0Sdrh         case '+':   flag_plussign = 1;        break;
2823e9aeec0Sdrh         case ' ':   flag_blanksign = 1;       break;
2833e9aeec0Sdrh         case '#':   flag_alternateform = 1;   break;
2843e9aeec0Sdrh         case '!':   flag_altform2 = 1;        break;
2853e9aeec0Sdrh         case '0':   flag_zeropad = 1;         break;
2863e9aeec0Sdrh         default:    done = 1;                 break;
287a18c5681Sdrh       }
2883e9aeec0Sdrh     }while( !done && (c=(*++fmt))!=0 );
289a18c5681Sdrh     /* Get the field width */
290a18c5681Sdrh     width = 0;
291a18c5681Sdrh     if( c=='*' ){
292a18c5681Sdrh       width = va_arg(ap,int);
293a18c5681Sdrh       if( width<0 ){
294a18c5681Sdrh         flag_leftjustify = 1;
295a18c5681Sdrh         width = -width;
296a18c5681Sdrh       }
297a18c5681Sdrh       c = *++fmt;
298a18c5681Sdrh     }else{
29917a68934Sdrh       while( c>='0' && c<='9' ){
300a18c5681Sdrh         width = width*10 + c - '0';
301a18c5681Sdrh         c = *++fmt;
302a18c5681Sdrh       }
303a18c5681Sdrh     }
304a18c5681Sdrh     if( width > etBUFSIZE-10 ){
305a18c5681Sdrh       width = etBUFSIZE-10;
306a18c5681Sdrh     }
307a18c5681Sdrh     /* Get the precision */
308a18c5681Sdrh     if( c=='.' ){
309a18c5681Sdrh       precision = 0;
310a18c5681Sdrh       c = *++fmt;
311a18c5681Sdrh       if( c=='*' ){
312a18c5681Sdrh         precision = va_arg(ap,int);
313a18c5681Sdrh         if( precision<0 ) precision = -precision;
314a18c5681Sdrh         c = *++fmt;
315a18c5681Sdrh       }else{
31617a68934Sdrh         while( c>='0' && c<='9' ){
317a18c5681Sdrh           precision = precision*10 + c - '0';
318a18c5681Sdrh           c = *++fmt;
319a18c5681Sdrh         }
320a18c5681Sdrh       }
321a18c5681Sdrh     }else{
322a18c5681Sdrh       precision = -1;
323a18c5681Sdrh     }
324a18c5681Sdrh     /* Get the conversion type modifier */
325a18c5681Sdrh     if( c=='l' ){
326a18c5681Sdrh       flag_long = 1;
327a18c5681Sdrh       c = *++fmt;
328a34b6764Sdrh       if( c=='l' ){
329a34b6764Sdrh         flag_longlong = 1;
330a34b6764Sdrh         c = *++fmt;
331a18c5681Sdrh       }else{
332a34b6764Sdrh         flag_longlong = 0;
333a34b6764Sdrh       }
334a34b6764Sdrh     }else{
335a34b6764Sdrh       flag_long = flag_longlong = 0;
336a18c5681Sdrh     }
337a18c5681Sdrh     /* Fetch the info entry for the field */
338a18c5681Sdrh     infop = 0;
33900e13613Sdanielk1977     for(idx=0; idx<ArraySize(fmtinfo); idx++){
340a18c5681Sdrh       if( c==fmtinfo[idx].fmttype ){
341a18c5681Sdrh         infop = &fmtinfo[idx];
3425f968436Sdrh         if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
343e84a306bSdrh           xtype = infop->type;
344c4413565Sdrh         }else{
345ade86483Sdrh           return;
3465f968436Sdrh         }
347a18c5681Sdrh         break;
348a18c5681Sdrh       }
349a18c5681Sdrh     }
350a18c5681Sdrh     zExtra = 0;
35143617e9aSdrh     if( infop==0 ){
352ade86483Sdrh       return;
35343617e9aSdrh     }
35443617e9aSdrh 
355a18c5681Sdrh 
3564794f735Sdrh     /* Limit the precision to prevent overflowing buf[] during conversion */
3574794f735Sdrh     if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
3584794f735Sdrh       precision = etBUFSIZE-40;
3594794f735Sdrh     }
3604794f735Sdrh 
361a18c5681Sdrh     /*
362a18c5681Sdrh     ** At this point, variables are initialized as follows:
363a18c5681Sdrh     **
364a18c5681Sdrh     **   flag_alternateform          TRUE if a '#' is present.
3653e9aeec0Sdrh     **   flag_altform2               TRUE if a '!' is present.
366a18c5681Sdrh     **   flag_plussign               TRUE if a '+' is present.
367a18c5681Sdrh     **   flag_leftjustify            TRUE if a '-' is present or if the
368a18c5681Sdrh     **                               field width was negative.
369a18c5681Sdrh     **   flag_zeropad                TRUE if the width began with 0.
370a18c5681Sdrh     **   flag_long                   TRUE if the letter 'l' (ell) prefixed
371a18c5681Sdrh     **                               the conversion character.
372a34b6764Sdrh     **   flag_longlong               TRUE if the letter 'll' (ell ell) prefixed
373a34b6764Sdrh     **                               the conversion character.
374a18c5681Sdrh     **   flag_blanksign              TRUE if a ' ' is present.
375a18c5681Sdrh     **   width                       The specified field width.  This is
376a18c5681Sdrh     **                               always non-negative.  Zero is the default.
377a18c5681Sdrh     **   precision                   The specified precision.  The default
378a18c5681Sdrh     **                               is -1.
379a18c5681Sdrh     **   xtype                       The class of the conversion.
380a18c5681Sdrh     **   infop                       Pointer to the appropriate info struct.
381a18c5681Sdrh     */
382a18c5681Sdrh     switch( xtype ){
383fe63d1c9Sdrh       case etPOINTER:
384fe63d1c9Sdrh         flag_longlong = sizeof(char*)==sizeof(i64);
385fe63d1c9Sdrh         flag_long = sizeof(char*)==sizeof(long int);
386fe63d1c9Sdrh         /* Fall through into the next case */
3879a99334dSdrh       case etORDINAL:
388a18c5681Sdrh       case etRADIX:
389e84a306bSdrh         if( infop->flags & FLAG_SIGNED ){
390e9707671Sdrh           i64 v;
391e9707671Sdrh           if( flag_longlong )   v = va_arg(ap,i64);
392e9707671Sdrh           else if( flag_long )  v = va_arg(ap,long int);
393e9707671Sdrh           else                  v = va_arg(ap,int);
394e9707671Sdrh           if( v<0 ){
395e9707671Sdrh             longvalue = -v;
396cfcdaefeSdanielk1977             prefix = '-';
397cfcdaefeSdanielk1977           }else{
398e9707671Sdrh             longvalue = v;
399e9707671Sdrh             if( flag_plussign )        prefix = '+';
400a18c5681Sdrh             else if( flag_blanksign )  prefix = ' ';
401a18c5681Sdrh             else                       prefix = 0;
402cfcdaefeSdanielk1977           }
403e9707671Sdrh         }else{
404e9707671Sdrh           if( flag_longlong )   longvalue = va_arg(ap,u64);
405e9707671Sdrh           else if( flag_long )  longvalue = va_arg(ap,unsigned long int);
406e9707671Sdrh           else                  longvalue = va_arg(ap,unsigned int);
407e9707671Sdrh           prefix = 0;
408e9707671Sdrh         }
409e9707671Sdrh         if( longvalue==0 ) flag_alternateform = 0;
410a18c5681Sdrh         if( flag_zeropad && precision<width-(prefix!=0) ){
411a18c5681Sdrh           precision = width-(prefix!=0);
412a18c5681Sdrh         }
4133039c0a8Sdrh         bufpt = &buf[etBUFSIZE-1];
4149a99334dSdrh         if( xtype==etORDINAL ){
41543f6e064Sdrh           static const char zOrd[] = "thstndrd";
416*ea678832Sdrh           int x = (int)(longvalue % 10);
41743f6e064Sdrh           if( x>=4 || (longvalue/10)%10==1 ){
41843f6e064Sdrh             x = 0;
41943f6e064Sdrh           }
42043f6e064Sdrh           buf[etBUFSIZE-3] = zOrd[x*2];
42143f6e064Sdrh           buf[etBUFSIZE-2] = zOrd[x*2+1];
4229a99334dSdrh           bufpt -= 2;
4239a99334dSdrh         }
424a18c5681Sdrh         {
42576ff3a0eSdrh           register const char *cset;      /* Use registers for speed */
426a18c5681Sdrh           register int base;
42776ff3a0eSdrh           cset = &aDigits[infop->charset];
428a18c5681Sdrh           base = infop->base;
429a18c5681Sdrh           do{                                           /* Convert to ascii */
430a18c5681Sdrh             *(--bufpt) = cset[longvalue%base];
431a18c5681Sdrh             longvalue = longvalue/base;
432a18c5681Sdrh           }while( longvalue>0 );
433a18c5681Sdrh         }
434*ea678832Sdrh         length = (int)(&buf[etBUFSIZE-1]-bufpt);
435a18c5681Sdrh         for(idx=precision-length; idx>0; idx--){
436a18c5681Sdrh           *(--bufpt) = '0';                             /* Zero pad */
437a18c5681Sdrh         }
438a18c5681Sdrh         if( prefix ) *(--bufpt) = prefix;               /* Add sign */
439a18c5681Sdrh         if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
44076ff3a0eSdrh           const char *pre;
44176ff3a0eSdrh           char x;
44276ff3a0eSdrh           pre = &aPrefix[infop->prefix];
44376ff3a0eSdrh           for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
444a18c5681Sdrh         }
445*ea678832Sdrh         length = (int)(&buf[etBUFSIZE-1]-bufpt);
446a18c5681Sdrh         break;
447a18c5681Sdrh       case etFLOAT:
448a18c5681Sdrh       case etEXP:
449a18c5681Sdrh       case etGENERIC:
450a18c5681Sdrh         realvalue = va_arg(ap,double);
451b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT
452a18c5681Sdrh         if( precision<0 ) precision = 6;         /* Set default precision */
4535eba8c09Sdrh         if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10;
454a18c5681Sdrh         if( realvalue<0.0 ){
455a18c5681Sdrh           realvalue = -realvalue;
456a18c5681Sdrh           prefix = '-';
457a18c5681Sdrh         }else{
458a18c5681Sdrh           if( flag_plussign )          prefix = '+';
459a18c5681Sdrh           else if( flag_blanksign )    prefix = ' ';
460a18c5681Sdrh           else                         prefix = 0;
461a18c5681Sdrh         }
4623e9aeec0Sdrh         if( xtype==etGENERIC && precision>0 ) precision--;
4635f968436Sdrh #if 0
464a18c5681Sdrh         /* Rounding works like BSD when the constant 0.4999 is used.  Wierd! */
465a18c5681Sdrh         for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
466a18c5681Sdrh #else
467a18c5681Sdrh         /* It makes more sense to use 0.5 */
46874161705Sdrh         for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
469a18c5681Sdrh #endif
4703e9aeec0Sdrh         if( xtype==etFLOAT ) realvalue += rounder;
471a18c5681Sdrh         /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
472a18c5681Sdrh         exp = 0;
473*ea678832Sdrh         if( sqlite3IsNaN((double)realvalue) ){
47453c14021Sdrh           bufpt = "NaN";
47553c14021Sdrh           length = 3;
47653c14021Sdrh           break;
47753c14021Sdrh         }
478a18c5681Sdrh         if( realvalue>0.0 ){
47963782855Sdrh           while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
48063782855Sdrh           while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
48163782855Sdrh           while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
482af005fbcSdrh           while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
483af005fbcSdrh           while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
484af005fbcSdrh           if( exp>350 ){
48553c14021Sdrh             if( prefix=='-' ){
48653c14021Sdrh               bufpt = "-Inf";
48753c14021Sdrh             }else if( prefix=='+' ){
48853c14021Sdrh               bufpt = "+Inf";
48953c14021Sdrh             }else{
49053c14021Sdrh               bufpt = "Inf";
49153c14021Sdrh             }
492*ea678832Sdrh             length = sqlite3Strlen30(bufpt);
493a18c5681Sdrh             break;
494a18c5681Sdrh           }
495a18c5681Sdrh         }
496a18c5681Sdrh         bufpt = buf;
497a18c5681Sdrh         /*
498a18c5681Sdrh         ** If the field type is etGENERIC, then convert to either etEXP
499a18c5681Sdrh         ** or etFLOAT, as appropriate.
500a18c5681Sdrh         */
501a18c5681Sdrh         flag_exp = xtype==etEXP;
502a18c5681Sdrh         if( xtype!=etFLOAT ){
503a18c5681Sdrh           realvalue += rounder;
504a18c5681Sdrh           if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
505a18c5681Sdrh         }
506a18c5681Sdrh         if( xtype==etGENERIC ){
507a18c5681Sdrh           flag_rtz = !flag_alternateform;
508a18c5681Sdrh           if( exp<-4 || exp>precision ){
509a18c5681Sdrh             xtype = etEXP;
510a18c5681Sdrh           }else{
511a18c5681Sdrh             precision = precision - exp;
512a18c5681Sdrh             xtype = etFLOAT;
513a18c5681Sdrh           }
514a18c5681Sdrh         }else{
515a18c5681Sdrh           flag_rtz = 0;
516a18c5681Sdrh         }
517557cc60fSdrh         if( xtype==etEXP ){
518557cc60fSdrh           e2 = 0;
519557cc60fSdrh         }else{
520557cc60fSdrh           e2 = exp;
521557cc60fSdrh         }
522a18c5681Sdrh         nsd = 0;
523*ea678832Sdrh         flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
524557cc60fSdrh         /* The sign in front of the number */
525557cc60fSdrh         if( prefix ){
526557cc60fSdrh           *(bufpt++) = prefix;
527557cc60fSdrh         }
528557cc60fSdrh         /* Digits prior to the decimal point */
529557cc60fSdrh         if( e2<0 ){
530557cc60fSdrh           *(bufpt++) = '0';
531557cc60fSdrh         }else{
532557cc60fSdrh           for(; e2>=0; e2--){
533557cc60fSdrh             *(bufpt++) = et_getdigit(&realvalue,&nsd);
534557cc60fSdrh           }
535557cc60fSdrh         }
536557cc60fSdrh         /* The decimal point */
537557cc60fSdrh         if( flag_dp ){
538557cc60fSdrh           *(bufpt++) = '.';
539557cc60fSdrh         }
540557cc60fSdrh         /* "0" digits after the decimal point but before the first
541557cc60fSdrh         ** significant digit of the number */
542af005fbcSdrh         for(e2++; e2<0; precision--, e2++){
543af005fbcSdrh           assert( precision>0 );
544a18c5681Sdrh           *(bufpt++) = '0';
545a18c5681Sdrh         }
546557cc60fSdrh         /* Significant digits after the decimal point */
547557cc60fSdrh         while( (precision--)>0 ){
548557cc60fSdrh           *(bufpt++) = et_getdigit(&realvalue,&nsd);
549a18c5681Sdrh         }
550557cc60fSdrh         /* Remove trailing zeros and the "." if no digits follow the "." */
551557cc60fSdrh         if( flag_rtz && flag_dp ){
5523e9aeec0Sdrh           while( bufpt[-1]=='0' ) *(--bufpt) = 0;
5533e9aeec0Sdrh           assert( bufpt>buf );
5543e9aeec0Sdrh           if( bufpt[-1]=='.' ){
555557cc60fSdrh             if( flag_altform2 ){
556557cc60fSdrh               *(bufpt++) = '0';
557557cc60fSdrh             }else{
558557cc60fSdrh               *(--bufpt) = 0;
559a18c5681Sdrh             }
560557cc60fSdrh           }
561557cc60fSdrh         }
562557cc60fSdrh         /* Add the "eNNN" suffix */
563af005fbcSdrh         if( flag_exp || xtype==etEXP ){
56476ff3a0eSdrh           *(bufpt++) = aDigits[infop->charset];
565557cc60fSdrh           if( exp<0 ){
566557cc60fSdrh             *(bufpt++) = '-'; exp = -exp;
567557cc60fSdrh           }else{
568557cc60fSdrh             *(bufpt++) = '+';
569557cc60fSdrh           }
570a18c5681Sdrh           if( exp>=100 ){
571*ea678832Sdrh             *(bufpt++) = (char)((exp/100)+'0');        /* 100's digit */
572a18c5681Sdrh             exp %= 100;
573a18c5681Sdrh           }
574*ea678832Sdrh           *(bufpt++) = (char)(exp/10+'0');             /* 10's digit */
575*ea678832Sdrh           *(bufpt++) = (char)(exp%10+'0');             /* 1's digit */
576a18c5681Sdrh         }
577557cc60fSdrh         *bufpt = 0;
578557cc60fSdrh 
579a18c5681Sdrh         /* The converted number is in buf[] and zero terminated. Output it.
580a18c5681Sdrh         ** Note that the number is in the usual order, not reversed as with
581a18c5681Sdrh         ** integer conversions. */
582*ea678832Sdrh         length = (int)(bufpt-buf);
583a18c5681Sdrh         bufpt = buf;
584a18c5681Sdrh 
585a18c5681Sdrh         /* Special case:  Add leading zeros if the flag_zeropad flag is
586a18c5681Sdrh         ** set and we are not left justified */
587a18c5681Sdrh         if( flag_zeropad && !flag_leftjustify && length < width){
588a18c5681Sdrh           int i;
589a18c5681Sdrh           int nPad = width - length;
590a18c5681Sdrh           for(i=width; i>=nPad; i--){
591a18c5681Sdrh             bufpt[i] = bufpt[i-nPad];
592a18c5681Sdrh           }
593a18c5681Sdrh           i = prefix!=0;
594a18c5681Sdrh           while( nPad-- ) bufpt[i++] = '0';
595a18c5681Sdrh           length = width;
596a18c5681Sdrh         }
597a18c5681Sdrh #endif
598a18c5681Sdrh         break;
599a18c5681Sdrh       case etSIZE:
600ade86483Sdrh         *(va_arg(ap,int*)) = pAccum->nChar;
601a18c5681Sdrh         length = width = 0;
602a18c5681Sdrh         break;
603a18c5681Sdrh       case etPERCENT:
604a18c5681Sdrh         buf[0] = '%';
605a18c5681Sdrh         bufpt = buf;
606a18c5681Sdrh         length = 1;
607a18c5681Sdrh         break;
608a18c5681Sdrh       case etCHARX:
609*ea678832Sdrh         c = va_arg(ap,int);
610*ea678832Sdrh         buf[0] = (char)c;
611a18c5681Sdrh         if( precision>=0 ){
612*ea678832Sdrh           for(idx=1; idx<precision; idx++) buf[idx] = (char)c;
613a18c5681Sdrh           length = precision;
614a18c5681Sdrh         }else{
615a18c5681Sdrh           length =1;
616a18c5681Sdrh         }
617a18c5681Sdrh         bufpt = buf;
618a18c5681Sdrh         break;
619a18c5681Sdrh       case etSTRING:
620d93d8a81Sdrh       case etDYNSTRING:
621cb485882Sdrh         bufpt = va_arg(ap,char*);
622d93d8a81Sdrh         if( bufpt==0 ){
623d93d8a81Sdrh           bufpt = "";
624d93d8a81Sdrh         }else if( xtype==etDYNSTRING ){
625d93d8a81Sdrh           zExtra = bufpt;
626d93d8a81Sdrh         }
627e509094bSdrh         if( precision>=0 ){
628e509094bSdrh           for(length=0; length<precision && bufpt[length]; length++){}
629e509094bSdrh         }else{
630*ea678832Sdrh           length = sqlite3Strlen30(bufpt);
631e509094bSdrh         }
632a18c5681Sdrh         break;
633a18c5681Sdrh       case etSQLESCAPE:
634f3b863edSdanielk1977       case etSQLESCAPE2:
635f3b863edSdanielk1977       case etSQLESCAPE3: {
636*ea678832Sdrh         int i, j, n, isnull;
6374794f735Sdrh         int needQuote;
638*ea678832Sdrh         char ch;
639f3b863edSdanielk1977         char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
640f0113000Sdanielk1977         char *escarg = va_arg(ap,char*);
641f0113000Sdanielk1977         isnull = escarg==0;
642f0113000Sdanielk1977         if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
643f0113000Sdanielk1977         for(i=n=0; (ch=escarg[i])!=0; i++){
644f3b863edSdanielk1977           if( ch==q )  n++;
645a18c5681Sdrh         }
6464794f735Sdrh         needQuote = !isnull && xtype==etSQLESCAPE2;
6474794f735Sdrh         n += i + 1 + needQuote*2;
648a18c5681Sdrh         if( n>etBUFSIZE ){
649e5ae5735Sdrh           bufpt = zExtra = sqlite3Malloc( n );
650d164fd34Sdrh           if( bufpt==0 ){
651d164fd34Sdrh             pAccum->mallocFailed = 1;
652d164fd34Sdrh             return;
653d164fd34Sdrh           }
654a18c5681Sdrh         }else{
655a18c5681Sdrh           bufpt = buf;
656a18c5681Sdrh         }
6570cfcf3fbSchw         j = 0;
658f3b863edSdanielk1977         if( needQuote ) bufpt[j++] = q;
659f0113000Sdanielk1977         for(i=0; (ch=escarg[i])!=0; i++){
660f0113000Sdanielk1977           bufpt[j++] = ch;
661f3b863edSdanielk1977           if( ch==q ) bufpt[j++] = ch;
662a18c5681Sdrh         }
663f3b863edSdanielk1977         if( needQuote ) bufpt[j++] = q;
664a18c5681Sdrh         bufpt[j] = 0;
665a18c5681Sdrh         length = j;
66605a82983Sdrh         /* The precision is ignored on %q and %Q */
66705a82983Sdrh         /* if( precision>=0 && precision<length ) length = precision; */
668a18c5681Sdrh         break;
6695eba8c09Sdrh       }
6705f968436Sdrh       case etTOKEN: {
6715f968436Sdrh         Token *pToken = va_arg(ap, Token*);
672af005fbcSdrh         if( pToken ){
673ade86483Sdrh           sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
674ad6d9460Sdrh         }
6755f968436Sdrh         length = width = 0;
6765f968436Sdrh         break;
6775f968436Sdrh       }
6785f968436Sdrh       case etSRCLIST: {
6795f968436Sdrh         SrcList *pSrc = va_arg(ap, SrcList*);
6805f968436Sdrh         int k = va_arg(ap, int);
6815f968436Sdrh         struct SrcList_item *pItem = &pSrc->a[k];
6825f968436Sdrh         assert( k>=0 && k<pSrc->nSrc );
68393a960a0Sdrh         if( pItem->zDatabase ){
684ade86483Sdrh           sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1);
685ade86483Sdrh           sqlite3StrAccumAppend(pAccum, ".", 1);
6865f968436Sdrh         }
687ade86483Sdrh         sqlite3StrAccumAppend(pAccum, pItem->zName, -1);
6885f968436Sdrh         length = width = 0;
6895f968436Sdrh         break;
6905f968436Sdrh       }
691a18c5681Sdrh     }/* End switch over the format type */
692a18c5681Sdrh     /*
693a18c5681Sdrh     ** The text of the conversion is pointed to by "bufpt" and is
694a18c5681Sdrh     ** "length" characters long.  The field width is "width".  Do
695a18c5681Sdrh     ** the output.
696a18c5681Sdrh     */
697a18c5681Sdrh     if( !flag_leftjustify ){
698a18c5681Sdrh       register int nspace;
699a18c5681Sdrh       nspace = width-length;
700a18c5681Sdrh       if( nspace>0 ){
701ade86483Sdrh         appendSpace(pAccum, nspace);
702a18c5681Sdrh       }
703a18c5681Sdrh     }
704a18c5681Sdrh     if( length>0 ){
705ade86483Sdrh       sqlite3StrAccumAppend(pAccum, bufpt, length);
706a18c5681Sdrh     }
707a18c5681Sdrh     if( flag_leftjustify ){
708a18c5681Sdrh       register int nspace;
709a18c5681Sdrh       nspace = width-length;
710a18c5681Sdrh       if( nspace>0 ){
711ade86483Sdrh         appendSpace(pAccum, nspace);
712a18c5681Sdrh       }
713a18c5681Sdrh     }
714a18c5681Sdrh     if( zExtra ){
7151e536953Sdanielk1977       sqlite3_free(zExtra);
716a18c5681Sdrh     }
717a18c5681Sdrh   }/* End for loop over the format string */
718a18c5681Sdrh } /* End of function */
719a18c5681Sdrh 
720a18c5681Sdrh /*
721ade86483Sdrh ** Append N bytes of text from z to the StrAccum object.
722a18c5681Sdrh */
723ade86483Sdrh void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
724ade86483Sdrh   if( p->tooBig | p->mallocFailed ){
72517435752Sdrh     return;
726ade86483Sdrh   }
727ade86483Sdrh   if( N<0 ){
728*ea678832Sdrh     N = sqlite3Strlen30(z);
729ade86483Sdrh   }
73017d46fc7Sdrh   if( N==0 || z==0 ){
731ade86483Sdrh     return;
732ade86483Sdrh   }
733ade86483Sdrh   if( p->nChar+N >= p->nAlloc ){
734ade86483Sdrh     char *zNew;
735ade86483Sdrh     if( !p->useMalloc ){
736ade86483Sdrh       p->tooBig = 1;
737ade86483Sdrh       N = p->nAlloc - p->nChar - 1;
738ade86483Sdrh       if( N<=0 ){
739ade86483Sdrh         return;
7405f968436Sdrh       }
741a18c5681Sdrh     }else{
74293a960a0Sdrh       i64 szNew = p->nChar;
743b1a6c3c1Sdrh       szNew += N + 1;
744b1a6c3c1Sdrh       if( szNew > p->mxAlloc ){
745ade86483Sdrh         sqlite3StrAccumReset(p);
746ade86483Sdrh         p->tooBig = 1;
747eaad32b1Sdrh         return;
748b1a6c3c1Sdrh       }else{
749*ea678832Sdrh         p->nAlloc = (int)szNew;
750a18c5681Sdrh       }
751633e6d57Sdrh       zNew = sqlite3DbMallocRaw(p->db, p->nAlloc );
75253f733c7Sdrh       if( zNew ){
753ade86483Sdrh         memcpy(zNew, p->zText, p->nChar);
754ade86483Sdrh         sqlite3StrAccumReset(p);
755ade86483Sdrh         p->zText = zNew;
756ade86483Sdrh       }else{
757ade86483Sdrh         p->mallocFailed = 1;
758ade86483Sdrh         sqlite3StrAccumReset(p);
759ade86483Sdrh         return;
76053f733c7Sdrh       }
7615f968436Sdrh     }
7625f968436Sdrh   }
763ade86483Sdrh   memcpy(&p->zText[p->nChar], z, N);
764ade86483Sdrh   p->nChar += N;
765483750baSdrh }
766483750baSdrh 
767483750baSdrh /*
768ade86483Sdrh ** Finish off a string by making sure it is zero-terminated.
769ade86483Sdrh ** Return a pointer to the resulting string.  Return a NULL
770ade86483Sdrh ** pointer if any kind of error was encountered.
7715f968436Sdrh */
772ade86483Sdrh char *sqlite3StrAccumFinish(StrAccum *p){
773ade86483Sdrh   if( p->zText ){
774ade86483Sdrh     p->zText[p->nChar] = 0;
775ade86483Sdrh     if( p->useMalloc && p->zText==p->zBase ){
776633e6d57Sdrh       p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
777ade86483Sdrh       if( p->zText ){
778ade86483Sdrh         memcpy(p->zText, p->zBase, p->nChar+1);
779ade86483Sdrh       }else{
780ade86483Sdrh         p->mallocFailed = 1;
781ade86483Sdrh       }
782ade86483Sdrh     }
783ade86483Sdrh   }
784ade86483Sdrh   return p->zText;
785ade86483Sdrh }
786ade86483Sdrh 
787ade86483Sdrh /*
788ade86483Sdrh ** Reset an StrAccum string.  Reclaim all malloced memory.
789ade86483Sdrh */
790ade86483Sdrh void sqlite3StrAccumReset(StrAccum *p){
791ade86483Sdrh   if( p->zText!=p->zBase ){
792633e6d57Sdrh     sqlite3DbFree(p->db, p->zText);
793ade86483Sdrh   }
794f089aa45Sdrh   p->zText = 0;
795ade86483Sdrh }
796ade86483Sdrh 
797ade86483Sdrh /*
798ade86483Sdrh ** Initialize a string accumulator
799ade86483Sdrh */
800f089aa45Sdrh void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
801ade86483Sdrh   p->zText = p->zBase = zBase;
802633e6d57Sdrh   p->db = 0;
803ade86483Sdrh   p->nChar = 0;
804ade86483Sdrh   p->nAlloc = n;
805bb4957f8Sdrh   p->mxAlloc = mx;
806ade86483Sdrh   p->useMalloc = 1;
807ade86483Sdrh   p->tooBig = 0;
808ade86483Sdrh   p->mallocFailed = 0;
8095f968436Sdrh }
8105f968436Sdrh 
8115f968436Sdrh /*
8125f968436Sdrh ** Print into memory obtained from sqliteMalloc().  Use the internal
8135f968436Sdrh ** %-conversion extensions.
8145f968436Sdrh */
81517435752Sdrh char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
81617435752Sdrh   char *z;
81779158e18Sdrh   char zBase[SQLITE_PRINT_BUF_SIZE];
818ade86483Sdrh   StrAccum acc;
819bb4957f8Sdrh   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
820bb4957f8Sdrh                       db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
821633e6d57Sdrh   acc.db = db;
822f089aa45Sdrh   sqlite3VXPrintf(&acc, 1, zFormat, ap);
823ade86483Sdrh   z = sqlite3StrAccumFinish(&acc);
824ade86483Sdrh   if( acc.mallocFailed && db ){
82517435752Sdrh     db->mallocFailed = 1;
82617435752Sdrh   }
82717435752Sdrh   return z;
8285f968436Sdrh }
8295f968436Sdrh 
8305f968436Sdrh /*
8315f968436Sdrh ** Print into memory obtained from sqliteMalloc().  Use the internal
8325f968436Sdrh ** %-conversion extensions.
8335f968436Sdrh */
83417435752Sdrh char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
8355f968436Sdrh   va_list ap;
8365f968436Sdrh   char *z;
8375f968436Sdrh   va_start(ap, zFormat);
838ade86483Sdrh   z = sqlite3VMPrintf(db, zFormat, ap);
8395f968436Sdrh   va_end(ap);
8405f968436Sdrh   return z;
8415f968436Sdrh }
8425f968436Sdrh 
8435f968436Sdrh /*
844633e6d57Sdrh ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
845633e6d57Sdrh ** the string and before returnning.  This routine is intended to be used
846633e6d57Sdrh ** to modify an existing string.  For example:
847633e6d57Sdrh **
848633e6d57Sdrh **       x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
849633e6d57Sdrh **
850633e6d57Sdrh */
851633e6d57Sdrh char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
852633e6d57Sdrh   va_list ap;
853633e6d57Sdrh   char *z;
854633e6d57Sdrh   va_start(ap, zFormat);
855633e6d57Sdrh   z = sqlite3VMPrintf(db, zFormat, ap);
856633e6d57Sdrh   va_end(ap);
857633e6d57Sdrh   sqlite3DbFree(db, zStr);
858633e6d57Sdrh   return z;
859633e6d57Sdrh }
860633e6d57Sdrh 
861633e6d57Sdrh /*
86228dd479cSdrh ** Print into memory obtained from sqlite3_malloc().  Omit the internal
86328dd479cSdrh ** %-conversion extensions.
86428dd479cSdrh */
86528dd479cSdrh char *sqlite3_vmprintf(const char *zFormat, va_list ap){
866ade86483Sdrh   char *z;
86728dd479cSdrh   char zBase[SQLITE_PRINT_BUF_SIZE];
868ade86483Sdrh   StrAccum acc;
869ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT
870ff1590eeSdrh   if( sqlite3_initialize() ) return 0;
871ff1590eeSdrh #endif
872bb4957f8Sdrh   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
873f089aa45Sdrh   sqlite3VXPrintf(&acc, 0, zFormat, ap);
874ade86483Sdrh   z = sqlite3StrAccumFinish(&acc);
875ade86483Sdrh   return z;
87628dd479cSdrh }
87728dd479cSdrh 
87828dd479cSdrh /*
87928dd479cSdrh ** Print into memory obtained from sqlite3_malloc()().  Omit the internal
88028dd479cSdrh ** %-conversion extensions.
881483750baSdrh */
8826f8a503dSdanielk1977 char *sqlite3_mprintf(const char *zFormat, ...){
883a18c5681Sdrh   va_list ap;
8845f968436Sdrh   char *z;
885ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT
886ff1590eeSdrh   if( sqlite3_initialize() ) return 0;
887ff1590eeSdrh #endif
888a18c5681Sdrh   va_start(ap, zFormat);
889b3738b6cSdrh   z = sqlite3_vmprintf(zFormat, ap);
890a18c5681Sdrh   va_end(ap);
8915f968436Sdrh   return z;
892a18c5681Sdrh }
893a18c5681Sdrh 
894a18c5681Sdrh /*
8956f8a503dSdanielk1977 ** sqlite3_snprintf() works like snprintf() except that it ignores the
89693a5c6bdSdrh ** current locale settings.  This is important for SQLite because we
89793a5c6bdSdrh ** are not able to use a "," as the decimal point in place of "." as
89893a5c6bdSdrh ** specified by some locales.
89993a5c6bdSdrh */
9006f8a503dSdanielk1977 char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
9015f968436Sdrh   char *z;
90293a5c6bdSdrh   va_list ap;
903ade86483Sdrh   StrAccum acc;
90493a5c6bdSdrh 
90568853907Sdrh   if( n<=0 ){
90668853907Sdrh     return zBuf;
90768853907Sdrh   }
908bb4957f8Sdrh   sqlite3StrAccumInit(&acc, zBuf, n, 0);
909ade86483Sdrh   acc.useMalloc = 0;
91093a5c6bdSdrh   va_start(ap,zFormat);
911f089aa45Sdrh   sqlite3VXPrintf(&acc, 0, zFormat, ap);
91293a5c6bdSdrh   va_end(ap);
913ade86483Sdrh   z = sqlite3StrAccumFinish(&acc);
9145f968436Sdrh   return z;
91593a5c6bdSdrh }
91693a5c6bdSdrh 
91785e9e22bSdrh #if defined(SQLITE_DEBUG)
918e54ca3feSdrh /*
919e54ca3feSdrh ** A version of printf() that understands %lld.  Used for debugging.
920e54ca3feSdrh ** The printf() built into some versions of windows does not understand %lld
921e54ca3feSdrh ** and segfaults if you give it a long long int.
922e54ca3feSdrh */
923e54ca3feSdrh void sqlite3DebugPrintf(const char *zFormat, ...){
924e54ca3feSdrh   va_list ap;
925ade86483Sdrh   StrAccum acc;
926e54ca3feSdrh   char zBuf[500];
927bb4957f8Sdrh   sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
928ade86483Sdrh   acc.useMalloc = 0;
929e54ca3feSdrh   va_start(ap,zFormat);
930f089aa45Sdrh   sqlite3VXPrintf(&acc, 0, zFormat, ap);
931e54ca3feSdrh   va_end(ap);
932bed8e7e5Sdrh   sqlite3StrAccumFinish(&acc);
933485f0039Sdrh   fprintf(stdout,"%s", zBuf);
9342ac3ee97Sdrh   fflush(stdout);
935e54ca3feSdrh }
936e54ca3feSdrh #endif
937