xref: /sqlite-3.40.0/src/printf.c (revision 384eef32)
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
4a18c5681Sdrh ** completeness.  They are slightly out-of-date.
5a18c5681Sdrh **
6e78e8284Sdrh ** The following modules is an enhanced replacement for the "printf" subroutines
7e78e8284Sdrh ** found in the standard C library.  The following enhancements are
8a18c5681Sdrh ** supported:
9a18c5681Sdrh **
10a18c5681Sdrh **      +  Additional functions.  The standard set of "printf" functions
11a18c5681Sdrh **         includes printf, fprintf, sprintf, vprintf, vfprintf, and
12a18c5681Sdrh **         vsprintf.  This module adds the following:
13a18c5681Sdrh **
14a18c5681Sdrh **           *  snprintf -- Works like sprintf, but has an extra argument
15a18c5681Sdrh **                          which is the size of the buffer written to.
16a18c5681Sdrh **
17a18c5681Sdrh **           *  mprintf --  Similar to sprintf.  Writes output to memory
18a18c5681Sdrh **                          obtained from malloc.
19a18c5681Sdrh **
20a18c5681Sdrh **           *  xprintf --  Calls a function to dispose of output.
21a18c5681Sdrh **
22a18c5681Sdrh **           *  nprintf --  No output, but returns the number of characters
23a18c5681Sdrh **                          that would have been output by printf.
24a18c5681Sdrh **
25a18c5681Sdrh **           *  A v- version (ex: vsnprintf) of every function is also
26a18c5681Sdrh **              supplied.
27a18c5681Sdrh **
28a18c5681Sdrh **      +  A few extensions to the formatting notation are supported:
29a18c5681Sdrh **
30a18c5681Sdrh **           *  The "=" flag (similar to "-") causes the output to be
31a18c5681Sdrh **              be centered in the appropriately sized field.
32a18c5681Sdrh **
33a18c5681Sdrh **           *  The %b field outputs an integer in binary notation.
34a18c5681Sdrh **
35a18c5681Sdrh **           *  The %c field now accepts a precision.  The character output
36a18c5681Sdrh **              is repeated by the number of times the precision specifies.
37a18c5681Sdrh **
38a18c5681Sdrh **           *  The %' field works like %c, but takes as its character the
39a18c5681Sdrh **              next character of the format string, instead of the next
40a18c5681Sdrh **              argument.  For example,  printf("%.78'-")  prints 78 minus
41a18c5681Sdrh **              signs, the same as  printf("%.78c",'-').
42a18c5681Sdrh **
43a18c5681Sdrh **      +  When compiled using GCC on a SPARC, this version of printf is
44a18c5681Sdrh **         faster than the library printf for SUN OS 4.1.
45a18c5681Sdrh **
46a18c5681Sdrh **      +  All functions are fully reentrant.
47a18c5681Sdrh **
48a18c5681Sdrh */
49a18c5681Sdrh #include "sqliteInt.h"
507c68d60bSdrh 
51a18c5681Sdrh /*
52a18c5681Sdrh ** Undefine COMPATIBILITY to make some slight changes in the way things
53a18c5681Sdrh ** work.  I think the changes are an improvement, but they are not
54a18c5681Sdrh ** backwards compatible.
55a18c5681Sdrh */
56a18c5681Sdrh /* #define COMPATIBILITY       / * Compatible with SUN OS 4.1 */
57a18c5681Sdrh 
58a18c5681Sdrh /*
59a18c5681Sdrh ** Conversion types fall into various categories as defined by the
60a18c5681Sdrh ** following enumeration.
61a18c5681Sdrh */
62a18c5681Sdrh enum et_type {    /* The type of the format field */
63a18c5681Sdrh    etRADIX,            /* Integer types.  %d, %x, %o, and so forth */
64a18c5681Sdrh    etFLOAT,            /* Floating point.  %f */
65a18c5681Sdrh    etEXP,              /* Exponentional notation. %e and %E */
66a18c5681Sdrh    etGENERIC,          /* Floating or exponential, depending on exponent. %g */
67a18c5681Sdrh    etSIZE,             /* Return number of characters processed so far. %n */
68a18c5681Sdrh    etSTRING,           /* Strings. %s */
69d93d8a81Sdrh    etDYNSTRING,        /* Dynamically allocated strings. %z */
70a18c5681Sdrh    etPERCENT,          /* Percent symbol. %% */
71a18c5681Sdrh    etCHARX,            /* Characters. %c */
72a18c5681Sdrh    etERROR,            /* Used to indicate no such conversion type */
73a18c5681Sdrh /* The rest are extensions, not normally found in printf() */
74a18c5681Sdrh    etCHARLIT,          /* Literal characters.  %' */
75a18c5681Sdrh    etSQLESCAPE,        /* Strings with '\'' doubled.  %q */
760cfcf3fbSchw    etSQLESCAPE2,       /* Strings with '\'' doubled and enclosed in '',
770cfcf3fbSchw                           NULL pointers replaced by SQL NULL.  %Q */
78a18c5681Sdrh    etORDINAL           /* 1st, 2nd, 3rd and so forth */
79a18c5681Sdrh };
80a18c5681Sdrh 
81a18c5681Sdrh /*
82a18c5681Sdrh ** Each builtin conversion character (ex: the 'd' in "%d") is described
83a18c5681Sdrh ** by an instance of the following structure
84a18c5681Sdrh */
85a18c5681Sdrh typedef struct et_info {   /* Information about each format field */
86a18c5681Sdrh   int  fmttype;              /* The format field code letter */
87a18c5681Sdrh   int  base;                 /* The base for radix conversion */
88a18c5681Sdrh   char *charset;             /* The character set for conversion */
89a18c5681Sdrh   int  flag_signed;          /* Is the quantity signed? */
90a18c5681Sdrh   char *prefix;              /* Prefix on non-zero values in alt format */
91a18c5681Sdrh   enum et_type type;          /* Conversion paradigm */
92a18c5681Sdrh } et_info;
93a18c5681Sdrh 
94a18c5681Sdrh /*
95a18c5681Sdrh ** The following table is searched linearly, so it is good to put the
96a18c5681Sdrh ** most frequently used conversion types first.
97a18c5681Sdrh */
98a18c5681Sdrh static et_info fmtinfo[] = {
99a18c5681Sdrh   { 'd',  10,  "0123456789",       1,    0, etRADIX,      },
100a18c5681Sdrh   { 's',   0,  0,                  0,    0, etSTRING,     },
101d93d8a81Sdrh   { 'z',   0,  0,                  0,    0, etDYNSTRING,  },
102a18c5681Sdrh   { 'q',   0,  0,                  0,    0, etSQLESCAPE,  },
1030cfcf3fbSchw   { 'Q',   0,  0,                  0,    0, etSQLESCAPE2, },
104a18c5681Sdrh   { 'c',   0,  0,                  0,    0, etCHARX,      },
105a18c5681Sdrh   { 'o',   8,  "01234567",         0,  "0", etRADIX,      },
106a18c5681Sdrh   { 'u',  10,  "0123456789",       0,    0, etRADIX,      },
107a18c5681Sdrh   { 'x',  16,  "0123456789abcdef", 0, "x0", etRADIX,      },
108a18c5681Sdrh   { 'X',  16,  "0123456789ABCDEF", 0, "X0", etRADIX,      },
109a18c5681Sdrh   { 'r',  10,  "0123456789",       0,    0, etORDINAL,    },
110a18c5681Sdrh   { 'f',   0,  0,                  1,    0, etFLOAT,      },
111a18c5681Sdrh   { 'e',   0,  "e",                1,    0, etEXP,        },
112a18c5681Sdrh   { 'E',   0,  "E",                1,    0, etEXP,        },
113a18c5681Sdrh   { 'g',   0,  "e",                1,    0, etGENERIC,    },
114a18c5681Sdrh   { 'G',   0,  "E",                1,    0, etGENERIC,    },
115a18c5681Sdrh   { 'i',  10,  "0123456789",       1,    0, etRADIX,      },
116a18c5681Sdrh   { 'n',   0,  0,                  0,    0, etSIZE,       },
117a18c5681Sdrh   { '%',   0,  0,                  0,    0, etPERCENT,    },
118a18c5681Sdrh   { 'b',   2,  "01",               0, "b0", etRADIX,      }, /* Binary */
119a18c5681Sdrh   { 'p',  10,  "0123456789",       0,    0, etRADIX,      }, /* Pointers */
120a18c5681Sdrh   { '\'',  0,  0,                  0,    0, etCHARLIT,    }, /* Literal char */
121a18c5681Sdrh };
122a18c5681Sdrh #define etNINFO  (sizeof(fmtinfo)/sizeof(fmtinfo[0]))
123a18c5681Sdrh 
124a18c5681Sdrh /*
125a18c5681Sdrh ** If NOFLOATINGPOINT is defined, then none of the floating point
126a18c5681Sdrh ** conversions will work.
127a18c5681Sdrh */
128a18c5681Sdrh #ifndef etNOFLOATINGPOINT
129a18c5681Sdrh /*
130a18c5681Sdrh ** "*val" is a double such that 0.1 <= *val < 10.0
131a18c5681Sdrh ** Return the ascii code for the leading digit of *val, then
132a18c5681Sdrh ** multiply "*val" by 10.0 to renormalize.
133a18c5681Sdrh **
134a18c5681Sdrh ** Example:
135a18c5681Sdrh **     input:     *val = 3.14159
136a18c5681Sdrh **     output:    *val = 1.4159    function return = '3'
137a18c5681Sdrh **
138a18c5681Sdrh ** The counter *cnt is incremented each time.  After counter exceeds
139a18c5681Sdrh ** 16 (the number of significant digits in a 64-bit float) '0' is
140a18c5681Sdrh ** always returned.
141a18c5681Sdrh */
142*384eef32Sdrh static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
143a18c5681Sdrh   int digit;
144*384eef32Sdrh   LONGDOUBLE_TYPE d;
145a18c5681Sdrh   if( (*cnt)++ >= 16 ) return '0';
146a18c5681Sdrh   digit = (int)*val;
147a18c5681Sdrh   d = digit;
148a18c5681Sdrh   digit += '0';
149a18c5681Sdrh   *val = (*val - d)*10.0;
150a18c5681Sdrh   return digit;
151a18c5681Sdrh }
152a18c5681Sdrh #endif
153a18c5681Sdrh 
154a18c5681Sdrh #define etBUFSIZE 1000  /* Size of the output buffer */
155a18c5681Sdrh 
156a18c5681Sdrh /*
157a18c5681Sdrh ** The root program.  All variations call this core.
158a18c5681Sdrh **
159a18c5681Sdrh ** INPUTS:
160a18c5681Sdrh **   func   This is a pointer to a function taking three arguments
161a18c5681Sdrh **            1. A pointer to anything.  Same as the "arg" parameter.
162a18c5681Sdrh **            2. A pointer to the list of characters to be output
163a18c5681Sdrh **               (Note, this list is NOT null terminated.)
164a18c5681Sdrh **            3. An integer number of characters to be output.
165a18c5681Sdrh **               (Note: This number might be zero.)
166a18c5681Sdrh **
167a18c5681Sdrh **   arg    This is the pointer to anything which will be passed as the
168a18c5681Sdrh **          first argument to "func".  Use it for whatever you like.
169a18c5681Sdrh **
170a18c5681Sdrh **   fmt    This is the format string, as in the usual print.
171a18c5681Sdrh **
172a18c5681Sdrh **   ap     This is a pointer to a list of arguments.  Same as in
173a18c5681Sdrh **          vfprint.
174a18c5681Sdrh **
175a18c5681Sdrh ** OUTPUTS:
176a18c5681Sdrh **          The return value is the total number of characters sent to
177a18c5681Sdrh **          the function "func".  Returns -1 on a error.
178a18c5681Sdrh **
179a18c5681Sdrh ** Note that the order in which automatic variables are declared below
180a18c5681Sdrh ** seems to make a big difference in determining how fast this beast
181a18c5681Sdrh ** will run.
182a18c5681Sdrh */
183a18c5681Sdrh static int vxprintf(
184a18c5681Sdrh   void (*func)(void*,char*,int),
185a18c5681Sdrh   void *arg,
186a18c5681Sdrh   const char *format,
187a18c5681Sdrh   va_list ap
188a18c5681Sdrh ){
189a18c5681Sdrh   register const char *fmt; /* The format string. */
190a18c5681Sdrh   register int c;           /* Next character in the format string */
191a18c5681Sdrh   register char *bufpt;     /* Pointer to the conversion buffer */
192a18c5681Sdrh   register int  precision;  /* Precision of the current field */
193a18c5681Sdrh   register int  length;     /* Length of the field */
194a18c5681Sdrh   register int  idx;        /* A general purpose loop counter */
195a18c5681Sdrh   int count;                /* Total number of characters output */
196a18c5681Sdrh   int width;                /* Width of the current field */
197a18c5681Sdrh   int flag_leftjustify;     /* True if "-" flag is present */
198a18c5681Sdrh   int flag_plussign;        /* True if "+" flag is present */
199a18c5681Sdrh   int flag_blanksign;       /* True if " " flag is present */
200a18c5681Sdrh   int flag_alternateform;   /* True if "#" flag is present */
201a18c5681Sdrh   int flag_zeropad;         /* True if field width constant starts with zero */
202a18c5681Sdrh   int flag_long;            /* True if "l" flag is present */
203a18c5681Sdrh   int flag_center;          /* True if "=" flag is present */
204a18c5681Sdrh   unsigned long longvalue;  /* Value for integer types */
205*384eef32Sdrh   LONGDOUBLE_TYPE realvalue; /* Value for real types */
206a18c5681Sdrh   et_info *infop;           /* Pointer to the appropriate info structure */
207a18c5681Sdrh   char buf[etBUFSIZE];      /* Conversion buffer */
208a18c5681Sdrh   char prefix;              /* Prefix character.  "+" or "-" or " " or '\0'. */
209a18c5681Sdrh   int  errorflag = 0;       /* True if an error is encountered */
210a18c5681Sdrh   enum et_type xtype;       /* Conversion paradigm */
211a18c5681Sdrh   char *zExtra;             /* Extra memory used for etTCLESCAPE conversions */
212a18c5681Sdrh   static char spaces[] = "                                                  "
213a18c5681Sdrh      "                                                                      ";
214a18c5681Sdrh #define etSPACESIZE (sizeof(spaces)-1)
215a18c5681Sdrh #ifndef etNOFLOATINGPOINT
216a18c5681Sdrh   int  exp;                 /* exponent of real numbers */
217a18c5681Sdrh   double rounder;           /* Used for rounding floating point values */
218a18c5681Sdrh   int flag_dp;              /* True if decimal point should be shown */
219a18c5681Sdrh   int flag_rtz;             /* True if trailing zeros should be removed */
220a18c5681Sdrh   int flag_exp;             /* True to force display of the exponent */
221a18c5681Sdrh   int nsd;                  /* Number of significant digits returned */
222a18c5681Sdrh #endif
223a18c5681Sdrh 
224a18c5681Sdrh   fmt = format;                     /* Put in a register for speed */
225a18c5681Sdrh   count = length = 0;
226a18c5681Sdrh   bufpt = 0;
227a18c5681Sdrh   for(; (c=(*fmt))!=0; ++fmt){
228a18c5681Sdrh     if( c!='%' ){
229a18c5681Sdrh       register int amt;
230a18c5681Sdrh       bufpt = (char *)fmt;
231a18c5681Sdrh       amt = 1;
232a18c5681Sdrh       while( (c=(*++fmt))!='%' && c!=0 ) amt++;
233a18c5681Sdrh       (*func)(arg,bufpt,amt);
234a18c5681Sdrh       count += amt;
235a18c5681Sdrh       if( c==0 ) break;
236a18c5681Sdrh     }
237a18c5681Sdrh     if( (c=(*++fmt))==0 ){
238a18c5681Sdrh       errorflag = 1;
239a18c5681Sdrh       (*func)(arg,"%",1);
240a18c5681Sdrh       count++;
241a18c5681Sdrh       break;
242a18c5681Sdrh     }
243a18c5681Sdrh     /* Find out what flags are present */
244a18c5681Sdrh     flag_leftjustify = flag_plussign = flag_blanksign =
245a18c5681Sdrh      flag_alternateform = flag_zeropad = flag_center = 0;
246a18c5681Sdrh     do{
247a18c5681Sdrh       switch( c ){
248a18c5681Sdrh         case '-':   flag_leftjustify = 1;     c = 0;   break;
249a18c5681Sdrh         case '+':   flag_plussign = 1;        c = 0;   break;
250a18c5681Sdrh         case ' ':   flag_blanksign = 1;       c = 0;   break;
251a18c5681Sdrh         case '#':   flag_alternateform = 1;   c = 0;   break;
252a18c5681Sdrh         case '0':   flag_zeropad = 1;         c = 0;   break;
253a18c5681Sdrh         case '=':   flag_center = 1;          c = 0;   break;
254a18c5681Sdrh         default:                                       break;
255a18c5681Sdrh       }
256a18c5681Sdrh     }while( c==0 && (c=(*++fmt))!=0 );
257a18c5681Sdrh     if( flag_center ) flag_leftjustify = 0;
258a18c5681Sdrh     /* Get the field width */
259a18c5681Sdrh     width = 0;
260a18c5681Sdrh     if( c=='*' ){
261a18c5681Sdrh       width = va_arg(ap,int);
262a18c5681Sdrh       if( width<0 ){
263a18c5681Sdrh         flag_leftjustify = 1;
264a18c5681Sdrh         width = -width;
265a18c5681Sdrh       }
266a18c5681Sdrh       c = *++fmt;
267a18c5681Sdrh     }else{
26817a68934Sdrh       while( c>='0' && c<='9' ){
269a18c5681Sdrh         width = width*10 + c - '0';
270a18c5681Sdrh         c = *++fmt;
271a18c5681Sdrh       }
272a18c5681Sdrh     }
273a18c5681Sdrh     if( width > etBUFSIZE-10 ){
274a18c5681Sdrh       width = etBUFSIZE-10;
275a18c5681Sdrh     }
276a18c5681Sdrh     /* Get the precision */
277a18c5681Sdrh     if( c=='.' ){
278a18c5681Sdrh       precision = 0;
279a18c5681Sdrh       c = *++fmt;
280a18c5681Sdrh       if( c=='*' ){
281a18c5681Sdrh         precision = va_arg(ap,int);
282a18c5681Sdrh #ifndef etCOMPATIBILITY
283a18c5681Sdrh         /* This is sensible, but SUN OS 4.1 doesn't do it. */
284a18c5681Sdrh         if( precision<0 ) precision = -precision;
285a18c5681Sdrh #endif
286a18c5681Sdrh         c = *++fmt;
287a18c5681Sdrh       }else{
28817a68934Sdrh         while( c>='0' && c<='9' ){
289a18c5681Sdrh           precision = precision*10 + c - '0';
290a18c5681Sdrh           c = *++fmt;
291a18c5681Sdrh         }
292a18c5681Sdrh       }
293a18c5681Sdrh       /* Limit the precision to prevent overflowing buf[] during conversion */
294a18c5681Sdrh       if( precision>etBUFSIZE-40 ) precision = etBUFSIZE-40;
295a18c5681Sdrh     }else{
296a18c5681Sdrh       precision = -1;
297a18c5681Sdrh     }
298a18c5681Sdrh     /* Get the conversion type modifier */
299a18c5681Sdrh     if( c=='l' ){
300a18c5681Sdrh       flag_long = 1;
301a18c5681Sdrh       c = *++fmt;
302a18c5681Sdrh     }else{
303a18c5681Sdrh       flag_long = 0;
304a18c5681Sdrh     }
305a18c5681Sdrh     /* Fetch the info entry for the field */
306a18c5681Sdrh     infop = 0;
307a18c5681Sdrh     for(idx=0; idx<etNINFO; idx++){
308a18c5681Sdrh       if( c==fmtinfo[idx].fmttype ){
309a18c5681Sdrh         infop = &fmtinfo[idx];
310a18c5681Sdrh         break;
311a18c5681Sdrh       }
312a18c5681Sdrh     }
313a18c5681Sdrh     /* No info entry found.  It must be an error. */
314a18c5681Sdrh     if( infop==0 ){
315a18c5681Sdrh       xtype = etERROR;
316a18c5681Sdrh     }else{
317a18c5681Sdrh       xtype = infop->type;
318a18c5681Sdrh     }
319a18c5681Sdrh     zExtra = 0;
320a18c5681Sdrh 
321a18c5681Sdrh     /*
322a18c5681Sdrh     ** At this point, variables are initialized as follows:
323a18c5681Sdrh     **
324a18c5681Sdrh     **   flag_alternateform          TRUE if a '#' is present.
325a18c5681Sdrh     **   flag_plussign               TRUE if a '+' is present.
326a18c5681Sdrh     **   flag_leftjustify            TRUE if a '-' is present or if the
327a18c5681Sdrh     **                               field width was negative.
328a18c5681Sdrh     **   flag_zeropad                TRUE if the width began with 0.
329a18c5681Sdrh     **   flag_long                   TRUE if the letter 'l' (ell) prefixed
330a18c5681Sdrh     **                               the conversion character.
331a18c5681Sdrh     **   flag_blanksign              TRUE if a ' ' is present.
332a18c5681Sdrh     **   width                       The specified field width.  This is
333a18c5681Sdrh     **                               always non-negative.  Zero is the default.
334a18c5681Sdrh     **   precision                   The specified precision.  The default
335a18c5681Sdrh     **                               is -1.
336a18c5681Sdrh     **   xtype                       The class of the conversion.
337a18c5681Sdrh     **   infop                       Pointer to the appropriate info struct.
338a18c5681Sdrh     */
339a18c5681Sdrh     switch( xtype ){
340a18c5681Sdrh       case etORDINAL:
341a18c5681Sdrh       case etRADIX:
342a18c5681Sdrh         if( flag_long )  longvalue = va_arg(ap,long);
343a18c5681Sdrh         else             longvalue = va_arg(ap,int);
344a18c5681Sdrh #ifdef etCOMPATIBILITY
345a18c5681Sdrh         /* For the format %#x, the value zero is printed "0" not "0x0".
346a18c5681Sdrh         ** I think this is stupid. */
347a18c5681Sdrh         if( longvalue==0 ) flag_alternateform = 0;
348a18c5681Sdrh #else
349a18c5681Sdrh         /* More sensible: turn off the prefix for octal (to prevent "00"),
350a18c5681Sdrh         ** but leave the prefix for hex. */
351a18c5681Sdrh         if( longvalue==0 && infop->base==8 ) flag_alternateform = 0;
352a18c5681Sdrh #endif
353a18c5681Sdrh         if( infop->flag_signed ){
354a18c5681Sdrh           if( *(long*)&longvalue<0 ){
355a18c5681Sdrh             longvalue = -*(long*)&longvalue;
356a18c5681Sdrh             prefix = '-';
357a18c5681Sdrh           }else if( flag_plussign )  prefix = '+';
358a18c5681Sdrh           else if( flag_blanksign )  prefix = ' ';
359a18c5681Sdrh           else                       prefix = 0;
360a18c5681Sdrh         }else                        prefix = 0;
361a18c5681Sdrh         if( flag_zeropad && precision<width-(prefix!=0) ){
362a18c5681Sdrh           precision = width-(prefix!=0);
363a18c5681Sdrh         }
364a18c5681Sdrh         bufpt = &buf[etBUFSIZE];
365a18c5681Sdrh         if( xtype==etORDINAL ){
366a18c5681Sdrh           long a,b;
367a18c5681Sdrh           a = longvalue%10;
368a18c5681Sdrh           b = longvalue%100;
369a18c5681Sdrh           bufpt -= 2;
370a18c5681Sdrh           if( a==0 || a>3 || (b>10 && b<14) ){
371a18c5681Sdrh             bufpt[0] = 't';
372a18c5681Sdrh             bufpt[1] = 'h';
373a18c5681Sdrh           }else if( a==1 ){
374a18c5681Sdrh             bufpt[0] = 's';
375a18c5681Sdrh             bufpt[1] = 't';
376a18c5681Sdrh           }else if( a==2 ){
377a18c5681Sdrh             bufpt[0] = 'n';
378a18c5681Sdrh             bufpt[1] = 'd';
379a18c5681Sdrh           }else if( a==3 ){
380a18c5681Sdrh             bufpt[0] = 'r';
381a18c5681Sdrh             bufpt[1] = 'd';
382a18c5681Sdrh           }
383a18c5681Sdrh         }
384a18c5681Sdrh         {
385a18c5681Sdrh           register char *cset;      /* Use registers for speed */
386a18c5681Sdrh           register int base;
387a18c5681Sdrh           cset = infop->charset;
388a18c5681Sdrh           base = infop->base;
389a18c5681Sdrh           do{                                           /* Convert to ascii */
390a18c5681Sdrh             *(--bufpt) = cset[longvalue%base];
391a18c5681Sdrh             longvalue = longvalue/base;
392a18c5681Sdrh           }while( longvalue>0 );
393a18c5681Sdrh         }
39494ce4c1eSdrh         length = &buf[etBUFSIZE]-bufpt;
395a18c5681Sdrh         for(idx=precision-length; idx>0; idx--){
396a18c5681Sdrh           *(--bufpt) = '0';                             /* Zero pad */
397a18c5681Sdrh         }
398a18c5681Sdrh         if( prefix ) *(--bufpt) = prefix;               /* Add sign */
399a18c5681Sdrh         if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
400a18c5681Sdrh           char *pre, x;
401a18c5681Sdrh           pre = infop->prefix;
402a18c5681Sdrh           if( *bufpt!=pre[0] ){
403a18c5681Sdrh             for(pre=infop->prefix; (x=(*pre))!=0; pre++) *(--bufpt) = x;
404a18c5681Sdrh           }
405a18c5681Sdrh         }
40694ce4c1eSdrh         length = &buf[etBUFSIZE]-bufpt;
407a18c5681Sdrh         break;
408a18c5681Sdrh       case etFLOAT:
409a18c5681Sdrh       case etEXP:
410a18c5681Sdrh       case etGENERIC:
411a18c5681Sdrh         realvalue = va_arg(ap,double);
412a18c5681Sdrh #ifndef etNOFLOATINGPOINT
413a18c5681Sdrh         if( precision<0 ) precision = 6;         /* Set default precision */
414a18c5681Sdrh         if( precision>etBUFSIZE-10 ) precision = etBUFSIZE-10;
415a18c5681Sdrh         if( realvalue<0.0 ){
416a18c5681Sdrh           realvalue = -realvalue;
417a18c5681Sdrh           prefix = '-';
418a18c5681Sdrh         }else{
419a18c5681Sdrh           if( flag_plussign )          prefix = '+';
420a18c5681Sdrh           else if( flag_blanksign )    prefix = ' ';
421a18c5681Sdrh           else                         prefix = 0;
422a18c5681Sdrh         }
423a18c5681Sdrh         if( infop->type==etGENERIC && precision>0 ) precision--;
424a18c5681Sdrh         rounder = 0.0;
425a18c5681Sdrh #ifdef COMPATIBILITY
426a18c5681Sdrh         /* Rounding works like BSD when the constant 0.4999 is used.  Wierd! */
427a18c5681Sdrh         for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
428a18c5681Sdrh #else
429a18c5681Sdrh         /* It makes more sense to use 0.5 */
430a18c5681Sdrh         for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1);
431a18c5681Sdrh #endif
432a18c5681Sdrh         if( infop->type==etFLOAT ) realvalue += rounder;
433a18c5681Sdrh         /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
434a18c5681Sdrh         exp = 0;
435a18c5681Sdrh         if( realvalue>0.0 ){
436a18c5681Sdrh           int k = 0;
437a18c5681Sdrh           while( realvalue>=1e8 && k++<100 ){ realvalue *= 1e-8; exp+=8; }
438a18c5681Sdrh           while( realvalue>=10.0 && k++<100 ){ realvalue *= 0.1; exp++; }
439a18c5681Sdrh           while( realvalue<1e-8 && k++<100 ){ realvalue *= 1e8; exp-=8; }
440a18c5681Sdrh           while( realvalue<1.0 && k++<100 ){ realvalue *= 10.0; exp--; }
441a18c5681Sdrh           if( k>=100 ){
442a18c5681Sdrh             bufpt = "NaN";
443a18c5681Sdrh             length = 3;
444a18c5681Sdrh             break;
445a18c5681Sdrh           }
446a18c5681Sdrh         }
447a18c5681Sdrh         bufpt = buf;
448a18c5681Sdrh         /*
449a18c5681Sdrh         ** If the field type is etGENERIC, then convert to either etEXP
450a18c5681Sdrh         ** or etFLOAT, as appropriate.
451a18c5681Sdrh         */
452a18c5681Sdrh         flag_exp = xtype==etEXP;
453a18c5681Sdrh         if( xtype!=etFLOAT ){
454a18c5681Sdrh           realvalue += rounder;
455a18c5681Sdrh           if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
456a18c5681Sdrh         }
457a18c5681Sdrh         if( xtype==etGENERIC ){
458a18c5681Sdrh           flag_rtz = !flag_alternateform;
459a18c5681Sdrh           if( exp<-4 || exp>precision ){
460a18c5681Sdrh             xtype = etEXP;
461a18c5681Sdrh           }else{
462a18c5681Sdrh             precision = precision - exp;
463a18c5681Sdrh             xtype = etFLOAT;
464a18c5681Sdrh           }
465a18c5681Sdrh         }else{
466a18c5681Sdrh           flag_rtz = 0;
467a18c5681Sdrh         }
468a18c5681Sdrh         /*
469a18c5681Sdrh         ** The "exp+precision" test causes output to be of type etEXP if
470a18c5681Sdrh         ** the precision is too large to fit in buf[].
471a18c5681Sdrh         */
472a18c5681Sdrh         nsd = 0;
473a18c5681Sdrh         if( xtype==etFLOAT && exp+precision<etBUFSIZE-30 ){
474a18c5681Sdrh           flag_dp = (precision>0 || flag_alternateform);
475a18c5681Sdrh           if( prefix ) *(bufpt++) = prefix;         /* Sign */
476a18c5681Sdrh           if( exp<0 )  *(bufpt++) = '0';            /* Digits before "." */
477a18c5681Sdrh           else for(; exp>=0; exp--) *(bufpt++) = et_getdigit(&realvalue,&nsd);
478a18c5681Sdrh           if( flag_dp ) *(bufpt++) = '.';           /* The decimal point */
479a18c5681Sdrh           for(exp++; exp<0 && precision>0; precision--, exp++){
480a18c5681Sdrh             *(bufpt++) = '0';
481a18c5681Sdrh           }
482a18c5681Sdrh           while( (precision--)>0 ) *(bufpt++) = et_getdigit(&realvalue,&nsd);
483a18c5681Sdrh           *(bufpt--) = 0;                           /* Null terminate */
484a18c5681Sdrh           if( flag_rtz && flag_dp ){     /* Remove trailing zeros and "." */
485a18c5681Sdrh             while( bufpt>=buf && *bufpt=='0' ) *(bufpt--) = 0;
486a18c5681Sdrh             if( bufpt>=buf && *bufpt=='.' ) *(bufpt--) = 0;
487a18c5681Sdrh           }
488a18c5681Sdrh           bufpt++;                            /* point to next free slot */
489a18c5681Sdrh         }else{    /* etEXP or etGENERIC */
490a18c5681Sdrh           flag_dp = (precision>0 || flag_alternateform);
491a18c5681Sdrh           if( prefix ) *(bufpt++) = prefix;   /* Sign */
492a18c5681Sdrh           *(bufpt++) = et_getdigit(&realvalue,&nsd);  /* First digit */
493a18c5681Sdrh           if( flag_dp ) *(bufpt++) = '.';     /* Decimal point */
494a18c5681Sdrh           while( (precision--)>0 ) *(bufpt++) = et_getdigit(&realvalue,&nsd);
495a18c5681Sdrh           bufpt--;                            /* point to last digit */
496a18c5681Sdrh           if( flag_rtz && flag_dp ){          /* Remove tail zeros */
497a18c5681Sdrh             while( bufpt>=buf && *bufpt=='0' ) *(bufpt--) = 0;
498a18c5681Sdrh             if( bufpt>=buf && *bufpt=='.' ) *(bufpt--) = 0;
499a18c5681Sdrh           }
500a18c5681Sdrh           bufpt++;                            /* point to next free slot */
501a18c5681Sdrh           if( exp || flag_exp ){
502a18c5681Sdrh             *(bufpt++) = infop->charset[0];
503a18c5681Sdrh             if( exp<0 ){ *(bufpt++) = '-'; exp = -exp; } /* sign of exp */
504a18c5681Sdrh             else       { *(bufpt++) = '+'; }
505a18c5681Sdrh             if( exp>=100 ){
506a18c5681Sdrh               *(bufpt++) = (exp/100)+'0';                /* 100's digit */
507a18c5681Sdrh               exp %= 100;
508a18c5681Sdrh             }
509a18c5681Sdrh             *(bufpt++) = exp/10+'0';                     /* 10's digit */
510a18c5681Sdrh             *(bufpt++) = exp%10+'0';                     /* 1's digit */
511a18c5681Sdrh           }
512a18c5681Sdrh         }
513a18c5681Sdrh         /* The converted number is in buf[] and zero terminated. Output it.
514a18c5681Sdrh         ** Note that the number is in the usual order, not reversed as with
515a18c5681Sdrh         ** integer conversions. */
51694ce4c1eSdrh         length = bufpt-buf;
517a18c5681Sdrh         bufpt = buf;
518a18c5681Sdrh 
519a18c5681Sdrh         /* Special case:  Add leading zeros if the flag_zeropad flag is
520a18c5681Sdrh         ** set and we are not left justified */
521a18c5681Sdrh         if( flag_zeropad && !flag_leftjustify && length < width){
522a18c5681Sdrh           int i;
523a18c5681Sdrh           int nPad = width - length;
524a18c5681Sdrh           for(i=width; i>=nPad; i--){
525a18c5681Sdrh             bufpt[i] = bufpt[i-nPad];
526a18c5681Sdrh           }
527a18c5681Sdrh           i = prefix!=0;
528a18c5681Sdrh           while( nPad-- ) bufpt[i++] = '0';
529a18c5681Sdrh           length = width;
530a18c5681Sdrh         }
531a18c5681Sdrh #endif
532a18c5681Sdrh         break;
533a18c5681Sdrh       case etSIZE:
534a18c5681Sdrh         *(va_arg(ap,int*)) = count;
535a18c5681Sdrh         length = width = 0;
536a18c5681Sdrh         break;
537a18c5681Sdrh       case etPERCENT:
538a18c5681Sdrh         buf[0] = '%';
539a18c5681Sdrh         bufpt = buf;
540a18c5681Sdrh         length = 1;
541a18c5681Sdrh         break;
542a18c5681Sdrh       case etCHARLIT:
543a18c5681Sdrh       case etCHARX:
544a18c5681Sdrh         c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt);
545a18c5681Sdrh         if( precision>=0 ){
546a18c5681Sdrh           for(idx=1; idx<precision; idx++) buf[idx] = c;
547a18c5681Sdrh           length = precision;
548a18c5681Sdrh         }else{
549a18c5681Sdrh           length =1;
550a18c5681Sdrh         }
551a18c5681Sdrh         bufpt = buf;
552a18c5681Sdrh         break;
553a18c5681Sdrh       case etSTRING:
554d93d8a81Sdrh       case etDYNSTRING:
555cb485882Sdrh         bufpt = va_arg(ap,char*);
556d93d8a81Sdrh         if( bufpt==0 ){
557d93d8a81Sdrh           bufpt = "";
558d93d8a81Sdrh         }else if( xtype==etDYNSTRING ){
559d93d8a81Sdrh           zExtra = bufpt;
560d93d8a81Sdrh         }
561a18c5681Sdrh         length = strlen(bufpt);
562a18c5681Sdrh         if( precision>=0 && precision<length ) length = precision;
563a18c5681Sdrh         break;
564a18c5681Sdrh       case etSQLESCAPE:
5650cfcf3fbSchw       case etSQLESCAPE2:
566a18c5681Sdrh         {
5670cfcf3fbSchw           int i, j, n, c, isnull;
568a18c5681Sdrh           char *arg = va_arg(ap,char*);
5690cfcf3fbSchw           isnull = arg==0;
5700cfcf3fbSchw           if( isnull ) arg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
571a18c5681Sdrh           for(i=n=0; (c=arg[i])!=0; i++){
572a18c5681Sdrh             if( c=='\'' )  n++;
573a18c5681Sdrh           }
5740cfcf3fbSchw           n += i + 1 + ((!isnull && xtype==etSQLESCAPE2) ? 2 : 0);
575a18c5681Sdrh           if( n>etBUFSIZE ){
576a18c5681Sdrh             bufpt = zExtra = sqliteMalloc( n );
577daffd0e5Sdrh             if( bufpt==0 ) return -1;
578a18c5681Sdrh           }else{
579a18c5681Sdrh             bufpt = buf;
580a18c5681Sdrh           }
5810cfcf3fbSchw           j = 0;
5820cfcf3fbSchw           if( !isnull && xtype==etSQLESCAPE2 ) bufpt[j++] = '\'';
5830cfcf3fbSchw           for(i=0; (c=arg[i])!=0; i++){
584a18c5681Sdrh             bufpt[j++] = c;
585a18c5681Sdrh             if( c=='\'' ) bufpt[j++] = c;
586a18c5681Sdrh           }
5870cfcf3fbSchw           if( !isnull && xtype==etSQLESCAPE2 ) bufpt[j++] = '\'';
588a18c5681Sdrh           bufpt[j] = 0;
589a18c5681Sdrh           length = j;
590a18c5681Sdrh           if( precision>=0 && precision<length ) length = precision;
591a18c5681Sdrh         }
592a18c5681Sdrh         break;
593a18c5681Sdrh       case etERROR:
594a18c5681Sdrh         buf[0] = '%';
595a18c5681Sdrh         buf[1] = c;
596a18c5681Sdrh         errorflag = 0;
597a18c5681Sdrh         idx = 1+(c!=0);
598a18c5681Sdrh         (*func)(arg,"%",idx);
599a18c5681Sdrh         count += idx;
600a18c5681Sdrh         if( c==0 ) fmt--;
601a18c5681Sdrh         break;
602a18c5681Sdrh     }/* End switch over the format type */
603a18c5681Sdrh     /*
604a18c5681Sdrh     ** The text of the conversion is pointed to by "bufpt" and is
605a18c5681Sdrh     ** "length" characters long.  The field width is "width".  Do
606a18c5681Sdrh     ** the output.
607a18c5681Sdrh     */
608a18c5681Sdrh     if( !flag_leftjustify ){
609a18c5681Sdrh       register int nspace;
610a18c5681Sdrh       nspace = width-length;
611a18c5681Sdrh       if( nspace>0 ){
612a18c5681Sdrh         if( flag_center ){
613a18c5681Sdrh           nspace = nspace/2;
614a18c5681Sdrh           width -= nspace;
615a18c5681Sdrh           flag_leftjustify = 1;
616a18c5681Sdrh         }
617a18c5681Sdrh         count += nspace;
618a18c5681Sdrh         while( nspace>=etSPACESIZE ){
619a18c5681Sdrh           (*func)(arg,spaces,etSPACESIZE);
620a18c5681Sdrh           nspace -= etSPACESIZE;
621a18c5681Sdrh         }
622a18c5681Sdrh         if( nspace>0 ) (*func)(arg,spaces,nspace);
623a18c5681Sdrh       }
624a18c5681Sdrh     }
625a18c5681Sdrh     if( length>0 ){
626a18c5681Sdrh       (*func)(arg,bufpt,length);
627a18c5681Sdrh       count += length;
628a18c5681Sdrh     }
629a18c5681Sdrh     if( flag_leftjustify ){
630a18c5681Sdrh       register int nspace;
631a18c5681Sdrh       nspace = width-length;
632a18c5681Sdrh       if( nspace>0 ){
633a18c5681Sdrh         count += nspace;
634a18c5681Sdrh         while( nspace>=etSPACESIZE ){
635a18c5681Sdrh           (*func)(arg,spaces,etSPACESIZE);
636a18c5681Sdrh           nspace -= etSPACESIZE;
637a18c5681Sdrh         }
638a18c5681Sdrh         if( nspace>0 ) (*func)(arg,spaces,nspace);
639a18c5681Sdrh       }
640a18c5681Sdrh     }
641a18c5681Sdrh     if( zExtra ){
642d93d8a81Sdrh       if( xtype==etDYNSTRING ){
643d93d8a81Sdrh         free(zExtra);
644d93d8a81Sdrh       }else{
645a18c5681Sdrh         sqliteFree(zExtra);
646a18c5681Sdrh       }
647d93d8a81Sdrh     }
648a18c5681Sdrh   }/* End for loop over the format string */
649a18c5681Sdrh   return errorflag ? -1 : count;
650a18c5681Sdrh } /* End of function */
651a18c5681Sdrh 
652a18c5681Sdrh 
653a18c5681Sdrh /* This structure is used to store state information about the
654a18c5681Sdrh ** write to memory that is currently in progress.
655a18c5681Sdrh */
656a18c5681Sdrh struct sgMprintf {
657a18c5681Sdrh   char *zBase;     /* A base allocation */
658a18c5681Sdrh   char *zText;     /* The string collected so far */
659a18c5681Sdrh   int  nChar;      /* Length of the string so far */
660a18c5681Sdrh   int  nAlloc;     /* Amount of space allocated in zText */
661a18c5681Sdrh };
662a18c5681Sdrh 
663a18c5681Sdrh /*
664a18c5681Sdrh ** This function implements the callback from vxprintf.
665a18c5681Sdrh **
666a18c5681Sdrh ** This routine add nNewChar characters of text in zNewText to
667a18c5681Sdrh ** the sgMprintf structure pointed to by "arg".
668a18c5681Sdrh */
669a18c5681Sdrh static void mout(void *arg, char *zNewText, int nNewChar){
670a18c5681Sdrh   struct sgMprintf *pM = (struct sgMprintf*)arg;
671a18c5681Sdrh   if( pM->nChar + nNewChar + 1 > pM->nAlloc ){
672a18c5681Sdrh     pM->nAlloc = pM->nChar + nNewChar*2 + 1;
673a18c5681Sdrh     if( pM->zText==pM->zBase ){
674a18c5681Sdrh       pM->zText = sqliteMalloc(pM->nAlloc);
675a18c5681Sdrh       if( pM->zText && pM->nChar ) memcpy(pM->zText,pM->zBase,pM->nChar);
676a18c5681Sdrh     }else{
6776d4abfbeSdrh       char *z = sqliteRealloc(pM->zText, pM->nAlloc);
6786d4abfbeSdrh       if( z==0 ){
6796d4abfbeSdrh         sqliteFree(pM->zText);
6806d4abfbeSdrh         pM->nChar = 0;
6816d4abfbeSdrh         pM->nAlloc = 0;
6826d4abfbeSdrh       }
6836d4abfbeSdrh       pM->zText = z;
684a18c5681Sdrh     }
685a18c5681Sdrh   }
686a18c5681Sdrh   if( pM->zText ){
687a18c5681Sdrh     memcpy(&pM->zText[pM->nChar], zNewText, nNewChar);
688a18c5681Sdrh     pM->nChar += nNewChar;
689a18c5681Sdrh     pM->zText[pM->nChar] = 0;
690a18c5681Sdrh   }
691a18c5681Sdrh }
692a18c5681Sdrh 
693a18c5681Sdrh /*
694a18c5681Sdrh ** sqlite_mprintf() works like printf(), but allocations memory to hold the
695a18c5681Sdrh ** resulting string and returns a pointer to the allocated memory.  Use
696a18c5681Sdrh ** sqliteFree() to release the memory allocated.
697a18c5681Sdrh */
698483750baSdrh char *sqliteMPrintf(const char *zFormat, ...){
699483750baSdrh   va_list ap;
700483750baSdrh   struct sgMprintf sMprintf;
701483750baSdrh   char zBuf[200];
702483750baSdrh 
703483750baSdrh   sMprintf.nChar = 0;
704483750baSdrh   sMprintf.nAlloc = sizeof(zBuf);
705483750baSdrh   sMprintf.zText = zBuf;
706483750baSdrh   sMprintf.zBase = zBuf;
707483750baSdrh   va_start(ap,zFormat);
708483750baSdrh   vxprintf(mout,&sMprintf,zFormat,ap);
709483750baSdrh   va_end(ap);
710483750baSdrh   sMprintf.zText[sMprintf.nChar] = 0;
711483750baSdrh   return sqliteRealloc(sMprintf.zText, sMprintf.nChar+1);
712483750baSdrh }
713483750baSdrh 
714483750baSdrh /*
715483750baSdrh ** sqlite_mprintf() works like printf(), but allocations memory to hold the
716483750baSdrh ** resulting string and returns a pointer to the allocated memory.  Use
717483750baSdrh ** sqliteFree() to release the memory allocated.
718483750baSdrh */
719a18c5681Sdrh char *sqlite_mprintf(const char *zFormat, ...){
720a18c5681Sdrh   va_list ap;
721a18c5681Sdrh   struct sgMprintf sMprintf;
722a18c5681Sdrh   char *zNew;
723a18c5681Sdrh   char zBuf[200];
724a18c5681Sdrh 
725a18c5681Sdrh   sMprintf.nChar = 0;
726a18c5681Sdrh   sMprintf.nAlloc = sizeof(zBuf);
727a18c5681Sdrh   sMprintf.zText = zBuf;
728a18c5681Sdrh   sMprintf.zBase = zBuf;
729a18c5681Sdrh   va_start(ap,zFormat);
730a18c5681Sdrh   vxprintf(mout,&sMprintf,zFormat,ap);
731a18c5681Sdrh   va_end(ap);
732a18c5681Sdrh   sMprintf.zText[sMprintf.nChar] = 0;
733fa173a76Sdrh   zNew = malloc( sMprintf.nChar+1 );
734fa173a76Sdrh   if( zNew ) strcpy(zNew,sMprintf.zText);
735fa173a76Sdrh   if( sMprintf.zText!=sMprintf.zBase ){
7366d4abfbeSdrh     sqliteFree(sMprintf.zText);
7376d4abfbeSdrh   }
738a18c5681Sdrh   return zNew;
739a18c5681Sdrh }
740a18c5681Sdrh 
741a18c5681Sdrh /* This is the varargs version of sqlite_mprintf.
742a18c5681Sdrh */
743a18c5681Sdrh char *sqlite_vmprintf(const char *zFormat, va_list ap){
744a18c5681Sdrh   struct sgMprintf sMprintf;
745fa173a76Sdrh   char *zNew;
746a18c5681Sdrh   char zBuf[200];
747a18c5681Sdrh   sMprintf.nChar = 0;
748a18c5681Sdrh   sMprintf.zText = zBuf;
749a18c5681Sdrh   sMprintf.nAlloc = sizeof(zBuf);
750a18c5681Sdrh   sMprintf.zBase = zBuf;
751a18c5681Sdrh   vxprintf(mout,&sMprintf,zFormat,ap);
752a18c5681Sdrh   sMprintf.zText[sMprintf.nChar] = 0;
753fa173a76Sdrh   zNew = malloc( sMprintf.nChar+1 );
754fa173a76Sdrh   if( zNew ) strcpy(zNew,sMprintf.zText);
755fa173a76Sdrh   if( sMprintf.zText!=sMprintf.zBase ){
7566d4abfbeSdrh     sqliteFree(sMprintf.zText);
7576d4abfbeSdrh   }
758fa173a76Sdrh   return zNew;
759a18c5681Sdrh }
760a18c5681Sdrh 
761a18c5681Sdrh /*
76293a5c6bdSdrh ** This function implements the callback from vxprintf.
76393a5c6bdSdrh **
76493a5c6bdSdrh ** This routine add nNewChar characters of text in zNewText to
76593a5c6bdSdrh ** the sgMprintf structure pointed to by "arg".  Unlike mout() above,
76693a5c6bdSdrh ** this routine does not allocate new space when the buffer fills.
76793a5c6bdSdrh ** It just truncates.
76893a5c6bdSdrh */
76993a5c6bdSdrh static void sout(void *arg, char *zNewText, int nNewChar){
77093a5c6bdSdrh   struct sgMprintf *pM = (struct sgMprintf*)arg;
77193a5c6bdSdrh   if( pM->nChar + nNewChar + 1 > pM->nAlloc ){
77293a5c6bdSdrh     nNewChar = pM->nAlloc - pM->nChar - 1;
77393a5c6bdSdrh     if( nNewChar<=0 ) return;
77493a5c6bdSdrh   }
77593a5c6bdSdrh   memcpy(&pM->zText[pM->nChar], zNewText, nNewChar);
77693a5c6bdSdrh   pM->nChar += nNewChar;
77793a5c6bdSdrh   pM->zText[pM->nChar] = 0;
77893a5c6bdSdrh }
77993a5c6bdSdrh 
78093a5c6bdSdrh /*
78193a5c6bdSdrh ** sqlite_sprintf() works like sprintf() except that it ignores the
78293a5c6bdSdrh ** current locale settings.  This is important for SQLite because we
78393a5c6bdSdrh ** are not able to use a "," as the decimal point in place of "." as
78493a5c6bdSdrh ** specified by some locales.
78593a5c6bdSdrh */
78693a5c6bdSdrh int sqlite_snprintf(int n, char *zBuf, const char *zFormat, ...){
78793a5c6bdSdrh   va_list ap;
78893a5c6bdSdrh   struct sgMprintf sMprintf;
78993a5c6bdSdrh 
79093a5c6bdSdrh   sMprintf.nChar = 0;
79193a5c6bdSdrh   sMprintf.nAlloc = n;
79293a5c6bdSdrh   sMprintf.zText = zBuf;
79393a5c6bdSdrh   sMprintf.zBase = zBuf;
79493a5c6bdSdrh   va_start(ap,zFormat);
79593a5c6bdSdrh   vxprintf(sout,&sMprintf,zFormat,ap);
79693a5c6bdSdrh   va_end(ap);
79793a5c6bdSdrh   return sMprintf.nChar;
79893a5c6bdSdrh }
79993a5c6bdSdrh 
80093a5c6bdSdrh 
80193a5c6bdSdrh 
80293a5c6bdSdrh /*
803a18c5681Sdrh ** The following four routines implement the varargs versions of the
804a18c5681Sdrh ** sqlite_exec() and sqlite_get_table() interfaces.  See the sqlite.h
805a18c5681Sdrh ** header files for a more detailed description of how these interfaces
806a18c5681Sdrh ** work.
807a18c5681Sdrh **
808a18c5681Sdrh ** These routines are all just simple wrappers.
809a18c5681Sdrh */
810a18c5681Sdrh int sqlite_exec_printf(
811a18c5681Sdrh   sqlite *db,                   /* An open database */
8129f71c2e0Sdrh   const char *sqlFormat,        /* printf-style format string for the SQL */
813a18c5681Sdrh   sqlite_callback xCallback,    /* Callback function */
814a18c5681Sdrh   void *pArg,                   /* 1st argument to callback function */
815a18c5681Sdrh   char **errmsg,                /* Error msg written here */
816a18c5681Sdrh   ...                           /* Arguments to the format string. */
817a18c5681Sdrh ){
818a18c5681Sdrh   va_list ap;
819a18c5681Sdrh   int rc;
820a18c5681Sdrh 
821a18c5681Sdrh   va_start(ap, errmsg);
822a18c5681Sdrh   rc = sqlite_exec_vprintf(db, sqlFormat, xCallback, pArg, errmsg, ap);
823a18c5681Sdrh   va_end(ap);
824a18c5681Sdrh   return rc;
825a18c5681Sdrh }
826a18c5681Sdrh int sqlite_exec_vprintf(
827a18c5681Sdrh   sqlite *db,                   /* An open database */
8289f71c2e0Sdrh   const char *sqlFormat,        /* printf-style format string for the SQL */
829a18c5681Sdrh   sqlite_callback xCallback,    /* Callback function */
830a18c5681Sdrh   void *pArg,                   /* 1st argument to callback function */
831a18c5681Sdrh   char **errmsg,                /* Error msg written here */
832a18c5681Sdrh   va_list ap                    /* Arguments to the format string. */
833a18c5681Sdrh ){
834a18c5681Sdrh   char *zSql;
835a18c5681Sdrh   int rc;
836a18c5681Sdrh 
837a18c5681Sdrh   zSql = sqlite_vmprintf(sqlFormat, ap);
838a18c5681Sdrh   rc = sqlite_exec(db, zSql, xCallback, pArg, errmsg);
839fa173a76Sdrh   free(zSql);
840a18c5681Sdrh   return rc;
841a18c5681Sdrh }
842a18c5681Sdrh int sqlite_get_table_printf(
843a18c5681Sdrh   sqlite *db,            /* An open database */
8449f71c2e0Sdrh   const char *sqlFormat, /* printf-style format string for the SQL */
845a18c5681Sdrh   char ***resultp,       /* Result written to a char *[]  that this points to */
846a18c5681Sdrh   int *nrow,             /* Number of result rows written here */
847a18c5681Sdrh   int *ncol,             /* Number of result columns written here */
848a18c5681Sdrh   char **errmsg,         /* Error msg written here */
849a18c5681Sdrh   ...                    /* Arguments to the format string */
850a18c5681Sdrh ){
851a18c5681Sdrh   va_list ap;
852a18c5681Sdrh   int rc;
853a18c5681Sdrh 
854a18c5681Sdrh   va_start(ap, errmsg);
855a18c5681Sdrh   rc = sqlite_get_table_vprintf(db, sqlFormat, resultp, nrow, ncol, errmsg, ap);
856a18c5681Sdrh   va_end(ap);
857a18c5681Sdrh   return rc;
858a18c5681Sdrh }
859a18c5681Sdrh int sqlite_get_table_vprintf(
860a18c5681Sdrh   sqlite *db,            /* An open database */
8619f71c2e0Sdrh   const char *sqlFormat, /* printf-style format string for the SQL */
862a18c5681Sdrh   char ***resultp,       /* Result written to a char *[]  that this points to */
863a18c5681Sdrh   int *nrow,             /* Number of result rows written here */
864a18c5681Sdrh   int *ncolumn,          /* Number of result columns written here */
865a18c5681Sdrh   char **errmsg,         /* Error msg written here */
866a18c5681Sdrh   va_list ap             /* Arguments to the format string */
867a18c5681Sdrh ){
868a18c5681Sdrh   char *zSql;
869a18c5681Sdrh   int rc;
870a18c5681Sdrh 
871a18c5681Sdrh   zSql = sqlite_vmprintf(sqlFormat, ap);
872a18c5681Sdrh   rc = sqlite_get_table(db, zSql, resultp, nrow, ncolumn, errmsg);
873fa173a76Sdrh   free(zSql);
874a18c5681Sdrh   return rc;
875a18c5681Sdrh }
876