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