blob: 716e59d556b2efba2669c20409fd8bbadb65df74 [file] [log] [blame]
drha18c5682000-10-08 22:20:57 +00001/*
2** The "printf" code that follows dates from the 1980's. It is in
3** the public domain. The original comments are included here for
drhe84a3062004-02-02 12:29:25 +00004** completeness. They are very out-of-date but might be useful as
5** an historical reference. Most of the "enhancements" have been backed
6** out so that the functionality is now the same as standard printf().
7**
8**************************************************************************
drha18c5682000-10-08 22:20:57 +00009**
drhe78e8282003-01-19 03:59:45 +000010** The following modules is an enhanced replacement for the "printf" subroutines
11** found in the standard C library. The following enhancements are
drha18c5682000-10-08 22:20:57 +000012** supported:
13**
14** + Additional functions. The standard set of "printf" functions
15** includes printf, fprintf, sprintf, vprintf, vfprintf, and
16** vsprintf. This module adds the following:
17**
18** * snprintf -- Works like sprintf, but has an extra argument
19** which is the size of the buffer written to.
20**
21** * mprintf -- Similar to sprintf. Writes output to memory
22** obtained from malloc.
23**
24** * xprintf -- Calls a function to dispose of output.
25**
26** * nprintf -- No output, but returns the number of characters
27** that would have been output by printf.
28**
29** * A v- version (ex: vsnprintf) of every function is also
30** supplied.
31**
32** + A few extensions to the formatting notation are supported:
33**
34** * The "=" flag (similar to "-") causes the output to be
35** be centered in the appropriately sized field.
36**
37** * The %b field outputs an integer in binary notation.
38**
39** * The %c field now accepts a precision. The character output
40** is repeated by the number of times the precision specifies.
41**
42** * The %' field works like %c, but takes as its character the
43** next character of the format string, instead of the next
44** argument. For example, printf("%.78'-") prints 78 minus
45** signs, the same as printf("%.78c",'-').
46**
47** + When compiled using GCC on a SPARC, this version of printf is
48** faster than the library printf for SUN OS 4.1.
49**
50** + All functions are fully reentrant.
51**
52*/
53#include "sqliteInt.h"
drh7c68d602000-10-11 19:28:51 +000054
drha18c5682000-10-08 22:20:57 +000055/*
drha18c5682000-10-08 22:20:57 +000056** Conversion types fall into various categories as defined by the
57** following enumeration.
58*/
drhe84a3062004-02-02 12:29:25 +000059#define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */
60#define etFLOAT 2 /* Floating point. %f */
61#define etEXP 3 /* Exponentional notation. %e and %E */
62#define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */
63#define etSIZE 5 /* Return number of characters processed so far. %n */
64#define etSTRING 6 /* Strings. %s */
65#define etDYNSTRING 7 /* Dynamically allocated strings. %z */
66#define etPERCENT 8 /* Percent symbol. %% */
67#define etCHARX 9 /* Characters. %c */
68#define etERROR 10 /* Used to indicate no such conversion type */
drha18c5682000-10-08 22:20:57 +000069/* The rest are extensions, not normally found in printf() */
drhe84a3062004-02-02 12:29:25 +000070#define etCHARLIT 11 /* Literal characters. %' */
71#define etSQLESCAPE 12 /* Strings with '\'' doubled. %q */
72#define etSQLESCAPE2 13 /* Strings with '\'' doubled and enclosed in '',
chw0cfcf3f2002-06-16 04:55:48 +000073 NULL pointers replaced by SQL NULL. %Q */
drh5f968432004-02-21 19:02:30 +000074#define etTOKEN 14 /* a pointer to a Token structure */
75#define etSRCLIST 15 /* a pointer to a SrcList */
drhfe63d1c2004-09-08 20:13:04 +000076#define etPOINTER 16 /* The %p conversion */
drhe84a3062004-02-02 12:29:25 +000077
78
79/*
80** An "etByte" is an 8-bit unsigned value.
81*/
82typedef unsigned char etByte;
drha18c5682000-10-08 22:20:57 +000083
84/*
85** Each builtin conversion character (ex: the 'd' in "%d") is described
86** by an instance of the following structure
87*/
88typedef struct et_info { /* Information about each format field */
drhe84a3062004-02-02 12:29:25 +000089 char fmttype; /* The format field code letter */
90 etByte base; /* The base for radix conversion */
91 etByte flags; /* One or more of FLAG_ constants below */
92 etByte type; /* Conversion paradigm */
drh76ff3a02004-09-24 22:32:30 +000093 etByte charset; /* Offset into aDigits[] of the digits string */
94 etByte prefix; /* Offset into aPrefix[] of the prefix string */
drha18c5682000-10-08 22:20:57 +000095} et_info;
96
97/*
drhe84a3062004-02-02 12:29:25 +000098** Allowed values for et_info.flags
99*/
100#define FLAG_SIGNED 1 /* True if the value to convert is signed */
101#define FLAG_INTERN 2 /* True if for internal use only */
102
103
104/*
drha18c5682000-10-08 22:20:57 +0000105** The following table is searched linearly, so it is good to put the
106** most frequently used conversion types first.
107*/
drh76ff3a02004-09-24 22:32:30 +0000108static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
109static const char aPrefix[] = "-x0\000X0";
drha18c5682000-10-08 22:20:57 +0000110static et_info fmtinfo[] = {
drh76ff3a02004-09-24 22:32:30 +0000111 { 'd', 10, 1, etRADIX, 0, 0 },
112 { 's', 0, 0, etSTRING, 0, 0 },
113 { 'z', 0, 2, etDYNSTRING, 0, 0 },
114 { 'q', 0, 0, etSQLESCAPE, 0, 0 },
115 { 'Q', 0, 0, etSQLESCAPE2, 0, 0 },
116 { 'c', 0, 0, etCHARX, 0, 0 },
117 { 'o', 8, 0, etRADIX, 0, 2 },
118 { 'u', 10, 0, etRADIX, 0, 0 },
119 { 'x', 16, 0, etRADIX, 16, 1 },
120 { 'X', 16, 0, etRADIX, 0, 4 },
121 { 'f', 0, 1, etFLOAT, 0, 0 },
122 { 'e', 0, 1, etEXP, 30, 0 },
123 { 'E', 0, 1, etEXP, 14, 0 },
124 { 'g', 0, 1, etGENERIC, 30, 0 },
125 { 'G', 0, 1, etGENERIC, 14, 0 },
126 { 'i', 10, 1, etRADIX, 0, 0 },
127 { 'n', 0, 0, etSIZE, 0, 0 },
128 { '%', 0, 0, etPERCENT, 0, 0 },
129 { 'p', 16, 0, etPOINTER, 0, 1 },
130 { 'T', 0, 2, etTOKEN, 0, 0 },
131 { 'S', 0, 2, etSRCLIST, 0, 0 },
drha18c5682000-10-08 22:20:57 +0000132};
133#define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0]))
134
135/*
136** If NOFLOATINGPOINT is defined, then none of the floating point
137** conversions will work.
138*/
139#ifndef etNOFLOATINGPOINT
140/*
141** "*val" is a double such that 0.1 <= *val < 10.0
142** Return the ascii code for the leading digit of *val, then
143** multiply "*val" by 10.0 to renormalize.
144**
145** Example:
146** input: *val = 3.14159
147** output: *val = 1.4159 function return = '3'
148**
149** The counter *cnt is incremented each time. After counter exceeds
150** 16 (the number of significant digits in a 64-bit float) '0' is
151** always returned.
152*/
drh384eef32004-01-07 03:04:27 +0000153static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
drha18c5682000-10-08 22:20:57 +0000154 int digit;
drh384eef32004-01-07 03:04:27 +0000155 LONGDOUBLE_TYPE d;
drha18c5682000-10-08 22:20:57 +0000156 if( (*cnt)++ >= 16 ) return '0';
157 digit = (int)*val;
158 d = digit;
159 digit += '0';
160 *val = (*val - d)*10.0;
161 return digit;
162}
163#endif
164
165#define etBUFSIZE 1000 /* Size of the output buffer */
166
167/*
168** The root program. All variations call this core.
169**
170** INPUTS:
171** func This is a pointer to a function taking three arguments
172** 1. A pointer to anything. Same as the "arg" parameter.
173** 2. A pointer to the list of characters to be output
174** (Note, this list is NOT null terminated.)
175** 3. An integer number of characters to be output.
176** (Note: This number might be zero.)
177**
178** arg This is the pointer to anything which will be passed as the
179** first argument to "func". Use it for whatever you like.
180**
181** fmt This is the format string, as in the usual print.
182**
183** ap This is a pointer to a list of arguments. Same as in
184** vfprint.
185**
186** OUTPUTS:
187** The return value is the total number of characters sent to
188** the function "func". Returns -1 on a error.
189**
190** Note that the order in which automatic variables are declared below
191** seems to make a big difference in determining how fast this beast
192** will run.
193*/
194static int vxprintf(
drh5f968432004-02-21 19:02:30 +0000195 void (*func)(void*,const char*,int), /* Consumer of text */
196 void *arg, /* First argument to the consumer */
197 int useExtended, /* Allow extended %-conversions */
198 const char *fmt, /* Format string */
199 va_list ap /* arguments */
drha18c5682000-10-08 22:20:57 +0000200){
drhe84a3062004-02-02 12:29:25 +0000201 int c; /* Next character in the format string */
202 char *bufpt; /* Pointer to the conversion buffer */
203 int precision; /* Precision of the current field */
204 int length; /* Length of the field */
205 int idx; /* A general purpose loop counter */
206 int count; /* Total number of characters output */
207 int width; /* Width of the current field */
208 etByte flag_leftjustify; /* True if "-" flag is present */
209 etByte flag_plussign; /* True if "+" flag is present */
210 etByte flag_blanksign; /* True if " " flag is present */
211 etByte flag_alternateform; /* True if "#" flag is present */
212 etByte flag_zeropad; /* True if field width constant starts with zero */
213 etByte flag_long; /* True if "l" flag is present */
drha34b6762004-05-07 13:30:42 +0000214 etByte flag_longlong; /* True if the "ll" flag is present */
215 UINT64_TYPE longvalue; /* Value for integer types */
drh384eef32004-01-07 03:04:27 +0000216 LONGDOUBLE_TYPE realvalue; /* Value for real types */
drhe84a3062004-02-02 12:29:25 +0000217 et_info *infop; /* Pointer to the appropriate info structure */
218 char buf[etBUFSIZE]; /* Conversion buffer */
219 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
220 etByte errorflag = 0; /* True if an error is encountered */
221 etByte xtype; /* Conversion paradigm */
222 char *zExtra; /* Extra memory used for etTCLESCAPE conversions */
drh905793e2004-02-21 13:31:09 +0000223 static char spaces[] = " ";
drha18c5682000-10-08 22:20:57 +0000224#define etSPACESIZE (sizeof(spaces)-1)
225#ifndef etNOFLOATINGPOINT
drhe84a3062004-02-02 12:29:25 +0000226 int exp; /* exponent of real numbers */
227 double rounder; /* Used for rounding floating point values */
228 etByte flag_dp; /* True if decimal point should be shown */
229 etByte flag_rtz; /* True if trailing zeros should be removed */
230 etByte flag_exp; /* True to force display of the exponent */
231 int nsd; /* Number of significant digits returned */
drha18c5682000-10-08 22:20:57 +0000232#endif
233
drhe29b1a02004-07-17 21:56:09 +0000234 func(arg,"",0);
drha18c5682000-10-08 22:20:57 +0000235 count = length = 0;
236 bufpt = 0;
237 for(; (c=(*fmt))!=0; ++fmt){
238 if( c!='%' ){
drhe84a3062004-02-02 12:29:25 +0000239 int amt;
drha18c5682000-10-08 22:20:57 +0000240 bufpt = (char *)fmt;
241 amt = 1;
242 while( (c=(*++fmt))!='%' && c!=0 ) amt++;
243 (*func)(arg,bufpt,amt);
244 count += amt;
245 if( c==0 ) break;
246 }
247 if( (c=(*++fmt))==0 ){
248 errorflag = 1;
249 (*func)(arg,"%",1);
250 count++;
251 break;
252 }
253 /* Find out what flags are present */
254 flag_leftjustify = flag_plussign = flag_blanksign =
drhe84a3062004-02-02 12:29:25 +0000255 flag_alternateform = flag_zeropad = 0;
drha18c5682000-10-08 22:20:57 +0000256 do{
257 switch( c ){
258 case '-': flag_leftjustify = 1; c = 0; break;
259 case '+': flag_plussign = 1; c = 0; break;
260 case ' ': flag_blanksign = 1; c = 0; break;
261 case '#': flag_alternateform = 1; c = 0; break;
262 case '0': flag_zeropad = 1; c = 0; break;
drha18c5682000-10-08 22:20:57 +0000263 default: break;
264 }
265 }while( c==0 && (c=(*++fmt))!=0 );
drha18c5682000-10-08 22:20:57 +0000266 /* Get the field width */
267 width = 0;
268 if( c=='*' ){
269 width = va_arg(ap,int);
270 if( width<0 ){
271 flag_leftjustify = 1;
272 width = -width;
273 }
274 c = *++fmt;
275 }else{
drh17a68932001-01-31 13:28:08 +0000276 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000277 width = width*10 + c - '0';
278 c = *++fmt;
279 }
280 }
281 if( width > etBUFSIZE-10 ){
282 width = etBUFSIZE-10;
283 }
284 /* Get the precision */
285 if( c=='.' ){
286 precision = 0;
287 c = *++fmt;
288 if( c=='*' ){
289 precision = va_arg(ap,int);
drha18c5682000-10-08 22:20:57 +0000290 if( precision<0 ) precision = -precision;
drha18c5682000-10-08 22:20:57 +0000291 c = *++fmt;
292 }else{
drh17a68932001-01-31 13:28:08 +0000293 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000294 precision = precision*10 + c - '0';
295 c = *++fmt;
296 }
297 }
298 /* Limit the precision to prevent overflowing buf[] during conversion */
299 if( precision>etBUFSIZE-40 ) precision = etBUFSIZE-40;
300 }else{
301 precision = -1;
302 }
303 /* Get the conversion type modifier */
304 if( c=='l' ){
305 flag_long = 1;
306 c = *++fmt;
drha34b6762004-05-07 13:30:42 +0000307 if( c=='l' ){
308 flag_longlong = 1;
309 c = *++fmt;
310 }else{
311 flag_longlong = 0;
312 }
drha18c5682000-10-08 22:20:57 +0000313 }else{
drha34b6762004-05-07 13:30:42 +0000314 flag_long = flag_longlong = 0;
drha18c5682000-10-08 22:20:57 +0000315 }
316 /* Fetch the info entry for the field */
317 infop = 0;
drhe84a3062004-02-02 12:29:25 +0000318 xtype = etERROR;
drha18c5682000-10-08 22:20:57 +0000319 for(idx=0; idx<etNINFO; idx++){
320 if( c==fmtinfo[idx].fmttype ){
321 infop = &fmtinfo[idx];
drh5f968432004-02-21 19:02:30 +0000322 if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
323 xtype = infop->type;
324 }
drha18c5682000-10-08 22:20:57 +0000325 break;
326 }
327 }
drha18c5682000-10-08 22:20:57 +0000328 zExtra = 0;
329
330 /*
331 ** At this point, variables are initialized as follows:
332 **
333 ** flag_alternateform TRUE if a '#' is present.
334 ** flag_plussign TRUE if a '+' is present.
335 ** flag_leftjustify TRUE if a '-' is present or if the
336 ** field width was negative.
337 ** flag_zeropad TRUE if the width began with 0.
338 ** flag_long TRUE if the letter 'l' (ell) prefixed
339 ** the conversion character.
drha34b6762004-05-07 13:30:42 +0000340 ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed
341 ** the conversion character.
drha18c5682000-10-08 22:20:57 +0000342 ** flag_blanksign TRUE if a ' ' is present.
343 ** width The specified field width. This is
344 ** always non-negative. Zero is the default.
345 ** precision The specified precision. The default
346 ** is -1.
347 ** xtype The class of the conversion.
348 ** infop Pointer to the appropriate info struct.
349 */
350 switch( xtype ){
drhfe63d1c2004-09-08 20:13:04 +0000351 case etPOINTER:
352 flag_longlong = sizeof(char*)==sizeof(i64);
353 flag_long = sizeof(char*)==sizeof(long int);
354 /* Fall through into the next case */
drha18c5682000-10-08 22:20:57 +0000355 case etRADIX:
drhe84a3062004-02-02 12:29:25 +0000356 if( infop->flags & FLAG_SIGNED ){
drhe9707672004-06-25 01:10:48 +0000357 i64 v;
358 if( flag_longlong ) v = va_arg(ap,i64);
359 else if( flag_long ) v = va_arg(ap,long int);
360 else v = va_arg(ap,int);
361 if( v<0 ){
362 longvalue = -v;
363 prefix = '-';
danielk1977cfcdaef2004-05-12 07:33:33 +0000364 }else{
drhe9707672004-06-25 01:10:48 +0000365 longvalue = v;
366 if( flag_plussign ) prefix = '+';
danielk1977cfcdaef2004-05-12 07:33:33 +0000367 else if( flag_blanksign ) prefix = ' ';
368 else prefix = 0;
369 }
drhe9707672004-06-25 01:10:48 +0000370 }else{
371 if( flag_longlong ) longvalue = va_arg(ap,u64);
372 else if( flag_long ) longvalue = va_arg(ap,unsigned long int);
373 else longvalue = va_arg(ap,unsigned int);
374 prefix = 0;
375 }
376 if( longvalue==0 ) flag_alternateform = 0;
drha18c5682000-10-08 22:20:57 +0000377 if( flag_zeropad && precision<width-(prefix!=0) ){
378 precision = width-(prefix!=0);
drh9adf9ac2002-05-15 11:44:13 +0000379 }
drh3039c0a2004-02-29 00:11:30 +0000380 bufpt = &buf[etBUFSIZE-1];
drha18c5682000-10-08 22:20:57 +0000381 {
drh76ff3a02004-09-24 22:32:30 +0000382 register const char *cset; /* Use registers for speed */
drha18c5682000-10-08 22:20:57 +0000383 register int base;
drh76ff3a02004-09-24 22:32:30 +0000384 cset = &aDigits[infop->charset];
drha18c5682000-10-08 22:20:57 +0000385 base = infop->base;
386 do{ /* Convert to ascii */
387 *(--bufpt) = cset[longvalue%base];
388 longvalue = longvalue/base;
389 }while( longvalue>0 );
drh9adf9ac2002-05-15 11:44:13 +0000390 }
drh3039c0a2004-02-29 00:11:30 +0000391 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000392 for(idx=precision-length; idx>0; idx--){
393 *(--bufpt) = '0'; /* Zero pad */
drh9adf9ac2002-05-15 11:44:13 +0000394 }
drha18c5682000-10-08 22:20:57 +0000395 if( prefix ) *(--bufpt) = prefix; /* Add sign */
396 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
drh76ff3a02004-09-24 22:32:30 +0000397 const char *pre;
398 char x;
399 pre = &aPrefix[infop->prefix];
drha18c5682000-10-08 22:20:57 +0000400 if( *bufpt!=pre[0] ){
drh76ff3a02004-09-24 22:32:30 +0000401 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
drh9adf9ac2002-05-15 11:44:13 +0000402 }
drha18c5682000-10-08 22:20:57 +0000403 }
drh3039c0a2004-02-29 00:11:30 +0000404 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000405 break;
406 case etFLOAT:
407 case etEXP:
408 case etGENERIC:
409 realvalue = va_arg(ap,double);
410#ifndef etNOFLOATINGPOINT
411 if( precision<0 ) precision = 6; /* Set default precision */
412 if( precision>etBUFSIZE-10 ) precision = etBUFSIZE-10;
413 if( realvalue<0.0 ){
414 realvalue = -realvalue;
415 prefix = '-';
drh9adf9ac2002-05-15 11:44:13 +0000416 }else{
drha18c5682000-10-08 22:20:57 +0000417 if( flag_plussign ) prefix = '+';
418 else if( flag_blanksign ) prefix = ' ';
419 else prefix = 0;
drh9adf9ac2002-05-15 11:44:13 +0000420 }
drha18c5682000-10-08 22:20:57 +0000421 if( infop->type==etGENERIC && precision>0 ) precision--;
422 rounder = 0.0;
drh5f968432004-02-21 19:02:30 +0000423#if 0
drha18c5682000-10-08 22:20:57 +0000424 /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */
425 for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
426#else
427 /* It makes more sense to use 0.5 */
428 for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1);
429#endif
430 if( infop->type==etFLOAT ) realvalue += rounder;
431 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
432 exp = 0;
433 if( realvalue>0.0 ){
drhb621c232004-02-21 19:41:04 +0000434 while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
435 while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
436 while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; }
437 while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; }
438 if( exp>350 || exp<-350 ){
drha18c5682000-10-08 22:20:57 +0000439 bufpt = "NaN";
440 length = 3;
441 break;
442 }
drh9adf9ac2002-05-15 11:44:13 +0000443 }
drha18c5682000-10-08 22:20:57 +0000444 bufpt = buf;
445 /*
446 ** If the field type is etGENERIC, then convert to either etEXP
447 ** or etFLOAT, as appropriate.
448 */
449 flag_exp = xtype==etEXP;
450 if( xtype!=etFLOAT ){
451 realvalue += rounder;
452 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
453 }
454 if( xtype==etGENERIC ){
455 flag_rtz = !flag_alternateform;
456 if( exp<-4 || exp>precision ){
457 xtype = etEXP;
458 }else{
459 precision = precision - exp;
460 xtype = etFLOAT;
461 }
drh9adf9ac2002-05-15 11:44:13 +0000462 }else{
drha18c5682000-10-08 22:20:57 +0000463 flag_rtz = 0;
drh9adf9ac2002-05-15 11:44:13 +0000464 }
drha18c5682000-10-08 22:20:57 +0000465 /*
466 ** The "exp+precision" test causes output to be of type etEXP if
467 ** the precision is too large to fit in buf[].
468 */
469 nsd = 0;
470 if( xtype==etFLOAT && exp+precision<etBUFSIZE-30 ){
471 flag_dp = (precision>0 || flag_alternateform);
472 if( prefix ) *(bufpt++) = prefix; /* Sign */
473 if( exp<0 ) *(bufpt++) = '0'; /* Digits before "." */
474 else for(; exp>=0; exp--) *(bufpt++) = et_getdigit(&realvalue,&nsd);
475 if( flag_dp ) *(bufpt++) = '.'; /* The decimal point */
476 for(exp++; exp<0 && precision>0; precision--, exp++){
477 *(bufpt++) = '0';
478 }
479 while( (precision--)>0 ) *(bufpt++) = et_getdigit(&realvalue,&nsd);
480 *(bufpt--) = 0; /* Null terminate */
481 if( flag_rtz && flag_dp ){ /* Remove trailing zeros and "." */
482 while( bufpt>=buf && *bufpt=='0' ) *(bufpt--) = 0;
483 if( bufpt>=buf && *bufpt=='.' ) *(bufpt--) = 0;
484 }
485 bufpt++; /* point to next free slot */
drh9adf9ac2002-05-15 11:44:13 +0000486 }else{ /* etEXP or etGENERIC */
drha18c5682000-10-08 22:20:57 +0000487 flag_dp = (precision>0 || flag_alternateform);
488 if( prefix ) *(bufpt++) = prefix; /* Sign */
489 *(bufpt++) = et_getdigit(&realvalue,&nsd); /* First digit */
490 if( flag_dp ) *(bufpt++) = '.'; /* Decimal point */
491 while( (precision--)>0 ) *(bufpt++) = et_getdigit(&realvalue,&nsd);
492 bufpt--; /* point to last digit */
493 if( flag_rtz && flag_dp ){ /* Remove tail zeros */
494 while( bufpt>=buf && *bufpt=='0' ) *(bufpt--) = 0;
495 if( bufpt>=buf && *bufpt=='.' ) *(bufpt--) = 0;
496 }
497 bufpt++; /* point to next free slot */
498 if( exp || flag_exp ){
drh76ff3a02004-09-24 22:32:30 +0000499 *(bufpt++) = aDigits[infop->charset];
drha18c5682000-10-08 22:20:57 +0000500 if( exp<0 ){ *(bufpt++) = '-'; exp = -exp; } /* sign of exp */
501 else { *(bufpt++) = '+'; }
502 if( exp>=100 ){
503 *(bufpt++) = (exp/100)+'0'; /* 100's digit */
504 exp %= 100;
drh9adf9ac2002-05-15 11:44:13 +0000505 }
drha18c5682000-10-08 22:20:57 +0000506 *(bufpt++) = exp/10+'0'; /* 10's digit */
507 *(bufpt++) = exp%10+'0'; /* 1's digit */
508 }
drh9adf9ac2002-05-15 11:44:13 +0000509 }
drha18c5682000-10-08 22:20:57 +0000510 /* The converted number is in buf[] and zero terminated. Output it.
511 ** Note that the number is in the usual order, not reversed as with
512 ** integer conversions. */
drh94ce4c12003-06-07 11:29:50 +0000513 length = bufpt-buf;
drha18c5682000-10-08 22:20:57 +0000514 bufpt = buf;
515
516 /* Special case: Add leading zeros if the flag_zeropad flag is
517 ** set and we are not left justified */
518 if( flag_zeropad && !flag_leftjustify && length < width){
519 int i;
520 int nPad = width - length;
521 for(i=width; i>=nPad; i--){
522 bufpt[i] = bufpt[i-nPad];
523 }
524 i = prefix!=0;
525 while( nPad-- ) bufpt[i++] = '0';
526 length = width;
527 }
528#endif
529 break;
530 case etSIZE:
531 *(va_arg(ap,int*)) = count;
532 length = width = 0;
533 break;
534 case etPERCENT:
535 buf[0] = '%';
536 bufpt = buf;
537 length = 1;
538 break;
539 case etCHARLIT:
540 case etCHARX:
541 c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt);
542 if( precision>=0 ){
543 for(idx=1; idx<precision; idx++) buf[idx] = c;
544 length = precision;
drh9adf9ac2002-05-15 11:44:13 +0000545 }else{
drha18c5682000-10-08 22:20:57 +0000546 length =1;
drh9adf9ac2002-05-15 11:44:13 +0000547 }
drha18c5682000-10-08 22:20:57 +0000548 bufpt = buf;
549 break;
550 case etSTRING:
drhd93d8a82003-06-16 03:08:18 +0000551 case etDYNSTRING:
drhcb485882002-08-15 13:50:48 +0000552 bufpt = va_arg(ap,char*);
drhd93d8a82003-06-16 03:08:18 +0000553 if( bufpt==0 ){
554 bufpt = "";
555 }else if( xtype==etDYNSTRING ){
556 zExtra = bufpt;
557 }
drha18c5682000-10-08 22:20:57 +0000558 length = strlen(bufpt);
559 if( precision>=0 && precision<length ) length = precision;
560 break;
561 case etSQLESCAPE:
chw0cfcf3f2002-06-16 04:55:48 +0000562 case etSQLESCAPE2:
drha18c5682000-10-08 22:20:57 +0000563 {
chw0cfcf3f2002-06-16 04:55:48 +0000564 int i, j, n, c, isnull;
drha18c5682000-10-08 22:20:57 +0000565 char *arg = va_arg(ap,char*);
chw0cfcf3f2002-06-16 04:55:48 +0000566 isnull = arg==0;
567 if( isnull ) arg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
drha18c5682000-10-08 22:20:57 +0000568 for(i=n=0; (c=arg[i])!=0; i++){
569 if( c=='\'' ) n++;
570 }
chw0cfcf3f2002-06-16 04:55:48 +0000571 n += i + 1 + ((!isnull && xtype==etSQLESCAPE2) ? 2 : 0);
drha18c5682000-10-08 22:20:57 +0000572 if( n>etBUFSIZE ){
573 bufpt = zExtra = sqliteMalloc( n );
drhdaffd0e2001-04-11 14:28:42 +0000574 if( bufpt==0 ) return -1;
drha18c5682000-10-08 22:20:57 +0000575 }else{
576 bufpt = buf;
577 }
chw0cfcf3f2002-06-16 04:55:48 +0000578 j = 0;
579 if( !isnull && xtype==etSQLESCAPE2 ) bufpt[j++] = '\'';
580 for(i=0; (c=arg[i])!=0; i++){
drha18c5682000-10-08 22:20:57 +0000581 bufpt[j++] = c;
582 if( c=='\'' ) bufpt[j++] = c;
583 }
chw0cfcf3f2002-06-16 04:55:48 +0000584 if( !isnull && xtype==etSQLESCAPE2 ) bufpt[j++] = '\'';
drha18c5682000-10-08 22:20:57 +0000585 bufpt[j] = 0;
586 length = j;
587 if( precision>=0 && precision<length ) length = precision;
588 }
589 break;
drh5f968432004-02-21 19:02:30 +0000590 case etTOKEN: {
591 Token *pToken = va_arg(ap, Token*);
drhad6d9462004-09-19 02:15:24 +0000592 if( pToken && pToken->z ){
593 (*func)(arg, pToken->z, pToken->n);
594 }
drh5f968432004-02-21 19:02:30 +0000595 length = width = 0;
596 break;
597 }
598 case etSRCLIST: {
599 SrcList *pSrc = va_arg(ap, SrcList*);
600 int k = va_arg(ap, int);
601 struct SrcList_item *pItem = &pSrc->a[k];
602 assert( k>=0 && k<pSrc->nSrc );
603 if( pItem->zDatabase && pItem->zDatabase[0] ){
604 (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase));
605 (*func)(arg, ".", 1);
606 }
607 (*func)(arg, pItem->zName, strlen(pItem->zName));
608 length = width = 0;
609 break;
610 }
drha18c5682000-10-08 22:20:57 +0000611 case etERROR:
612 buf[0] = '%';
613 buf[1] = c;
614 errorflag = 0;
615 idx = 1+(c!=0);
616 (*func)(arg,"%",idx);
617 count += idx;
618 if( c==0 ) fmt--;
619 break;
620 }/* End switch over the format type */
621 /*
622 ** The text of the conversion is pointed to by "bufpt" and is
623 ** "length" characters long. The field width is "width". Do
624 ** the output.
625 */
626 if( !flag_leftjustify ){
627 register int nspace;
628 nspace = width-length;
629 if( nspace>0 ){
drha18c5682000-10-08 22:20:57 +0000630 count += nspace;
631 while( nspace>=etSPACESIZE ){
632 (*func)(arg,spaces,etSPACESIZE);
633 nspace -= etSPACESIZE;
634 }
635 if( nspace>0 ) (*func)(arg,spaces,nspace);
636 }
637 }
638 if( length>0 ){
639 (*func)(arg,bufpt,length);
640 count += length;
641 }
642 if( flag_leftjustify ){
643 register int nspace;
644 nspace = width-length;
645 if( nspace>0 ){
646 count += nspace;
647 while( nspace>=etSPACESIZE ){
648 (*func)(arg,spaces,etSPACESIZE);
649 nspace -= etSPACESIZE;
650 }
651 if( nspace>0 ) (*func)(arg,spaces,nspace);
652 }
653 }
654 if( zExtra ){
drh5f968432004-02-21 19:02:30 +0000655 sqliteFree(zExtra);
drha18c5682000-10-08 22:20:57 +0000656 }
657 }/* End for loop over the format string */
658 return errorflag ? -1 : count;
659} /* End of function */
660
661
662/* This structure is used to store state information about the
663** write to memory that is currently in progress.
664*/
665struct sgMprintf {
666 char *zBase; /* A base allocation */
667 char *zText; /* The string collected so far */
668 int nChar; /* Length of the string so far */
drh5f968432004-02-21 19:02:30 +0000669 int nTotal; /* Output size if unconstrained */
drha18c5682000-10-08 22:20:57 +0000670 int nAlloc; /* Amount of space allocated in zText */
drh5f968432004-02-21 19:02:30 +0000671 void *(*xRealloc)(void*,int); /* Function used to realloc memory */
drha18c5682000-10-08 22:20:57 +0000672};
673
674/*
675** This function implements the callback from vxprintf.
676**
677** This routine add nNewChar characters of text in zNewText to
678** the sgMprintf structure pointed to by "arg".
679*/
drh5f968432004-02-21 19:02:30 +0000680static void mout(void *arg, const char *zNewText, int nNewChar){
drha18c5682000-10-08 22:20:57 +0000681 struct sgMprintf *pM = (struct sgMprintf*)arg;
drh5f968432004-02-21 19:02:30 +0000682 pM->nTotal += nNewChar;
drha18c5682000-10-08 22:20:57 +0000683 if( pM->nChar + nNewChar + 1 > pM->nAlloc ){
drh5f968432004-02-21 19:02:30 +0000684 if( pM->xRealloc==0 ){
685 nNewChar = pM->nAlloc - pM->nChar - 1;
drha18c5682000-10-08 22:20:57 +0000686 }else{
drh5f968432004-02-21 19:02:30 +0000687 pM->nAlloc = pM->nChar + nNewChar*2 + 1;
688 if( pM->zText==pM->zBase ){
689 pM->zText = pM->xRealloc(0, pM->nAlloc);
690 if( pM->zText && pM->nChar ){
691 memcpy(pM->zText, pM->zBase, pM->nChar);
692 }
693 }else{
694 pM->zText = pM->xRealloc(pM->zText, pM->nAlloc);
drh6d4abfb2001-10-22 02:58:08 +0000695 }
drha18c5682000-10-08 22:20:57 +0000696 }
697 }
drhe29b1a02004-07-17 21:56:09 +0000698 if( pM->zText ){
699 if( nNewChar>0 ){
700 memcpy(&pM->zText[pM->nChar], zNewText, nNewChar);
701 pM->nChar += nNewChar;
702 }
drha18c5682000-10-08 22:20:57 +0000703 pM->zText[pM->nChar] = 0;
704 }
705}
706
707/*
drh5f968432004-02-21 19:02:30 +0000708** This routine is a wrapper around xprintf() that invokes mout() as
709** the consumer.
drha18c5682000-10-08 22:20:57 +0000710*/
drh5f968432004-02-21 19:02:30 +0000711static char *base_vprintf(
712 void *(*xRealloc)(void*,int), /* Routine to realloc memory. May be NULL */
713 int useInternal, /* Use internal %-conversions if true */
714 char *zInitBuf, /* Initially write here, before mallocing */
715 int nInitBuf, /* Size of zInitBuf[] */
716 const char *zFormat, /* format string */
717 va_list ap /* arguments */
718){
719 struct sgMprintf sM;
720 sM.zBase = sM.zText = zInitBuf;
721 sM.nChar = sM.nTotal = 0;
722 sM.nAlloc = nInitBuf;
723 sM.xRealloc = xRealloc;
724 vxprintf(mout, &sM, useInternal, zFormat, ap);
725 if( xRealloc ){
726 if( sM.zText==sM.zBase ){
727 sM.zText = xRealloc(0, sM.nChar+1);
danielk19778def5ea2004-06-16 10:39:52 +0000728 if( sM.zText ){
729 memcpy(sM.zText, sM.zBase, sM.nChar+1);
730 }
drh5f968432004-02-21 19:02:30 +0000731 }else if( sM.nAlloc>sM.nChar+10 ){
732 sM.zText = xRealloc(sM.zText, sM.nChar+1);
733 }
734 }
735 return sM.zText;
drh483750b2003-01-29 18:46:51 +0000736}
737
738/*
drh5f968432004-02-21 19:02:30 +0000739** Realloc that is a real function, not a macro.
740*/
741static void *printf_realloc(void *old, int size){
742 return sqliteRealloc(old,size);
743}
744
745/*
746** Print into memory obtained from sqliteMalloc(). Use the internal
747** %-conversion extensions.
748*/
danielk19774adee202004-05-08 08:23:19 +0000749char *sqlite3VMPrintf(const char *zFormat, va_list ap){
drh5f968432004-02-21 19:02:30 +0000750 char zBase[1000];
751 return base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
752}
753
754/*
755** Print into memory obtained from sqliteMalloc(). Use the internal
756** %-conversion extensions.
757*/
danielk19774adee202004-05-08 08:23:19 +0000758char *sqlite3MPrintf(const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000759 va_list ap;
760 char *z;
761 char zBase[1000];
762 va_start(ap, zFormat);
763 z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
764 va_end(ap);
765 return z;
766}
767
768/*
769** Print into memory obtained from malloc(). Do not use the internal
770** %-conversion extensions. This routine is for use by external users.
drh483750b2003-01-29 18:46:51 +0000771*/
danielk19776f8a5032004-05-10 10:34:51 +0000772char *sqlite3_mprintf(const char *zFormat, ...){
drha18c5682000-10-08 22:20:57 +0000773 va_list ap;
drh5f968432004-02-21 19:02:30 +0000774 char *z;
drha18c5682000-10-08 22:20:57 +0000775 char zBuf[200];
776
drha18c5682000-10-08 22:20:57 +0000777 va_start(ap,zFormat);
drh5f968432004-02-21 19:02:30 +0000778 z = base_vprintf((void*(*)(void*,int))realloc, 0,
779 zBuf, sizeof(zBuf), zFormat, ap);
drha18c5682000-10-08 22:20:57 +0000780 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000781 return z;
drha18c5682000-10-08 22:20:57 +0000782}
783
danielk19776f8a5032004-05-10 10:34:51 +0000784/* This is the varargs version of sqlite3_mprintf.
drha18c5682000-10-08 22:20:57 +0000785*/
danielk19776f8a5032004-05-10 10:34:51 +0000786char *sqlite3_vmprintf(const char *zFormat, va_list ap){
drha18c5682000-10-08 22:20:57 +0000787 char zBuf[200];
drh5f968432004-02-21 19:02:30 +0000788 return base_vprintf((void*(*)(void*,int))realloc, 0,
789 zBuf, sizeof(zBuf), zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +0000790}
791
792/*
danielk19776f8a5032004-05-10 10:34:51 +0000793** sqlite3_snprintf() works like snprintf() except that it ignores the
drh93a5c6b2003-12-23 02:17:35 +0000794** current locale settings. This is important for SQLite because we
795** are not able to use a "," as the decimal point in place of "." as
796** specified by some locales.
797*/
danielk19776f8a5032004-05-10 10:34:51 +0000798char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000799 char *z;
drh93a5c6b2003-12-23 02:17:35 +0000800 va_list ap;
drh93a5c6b2003-12-23 02:17:35 +0000801
drh93a5c6b2003-12-23 02:17:35 +0000802 va_start(ap,zFormat);
drh5f968432004-02-21 19:02:30 +0000803 z = base_vprintf(0, 0, zBuf, n, zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +0000804 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000805 return z;
drh93a5c6b2003-12-23 02:17:35 +0000806}
807
drh1b743be2004-06-22 22:04:46 +0000808#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
drhe54ca3f2004-06-07 01:52:14 +0000809/*
810** A version of printf() that understands %lld. Used for debugging.
811** The printf() built into some versions of windows does not understand %lld
812** and segfaults if you give it a long long int.
813*/
814void sqlite3DebugPrintf(const char *zFormat, ...){
drh76ff3a02004-09-24 22:32:30 +0000815 extern int getpid(void);
drhe54ca3f2004-06-07 01:52:14 +0000816 va_list ap;
817 char zBuf[500];
818 va_start(ap, zFormat);
819 base_vprintf(0, 0, zBuf, sizeof(zBuf), zFormat, ap);
820 va_end(ap);
drh59c98a62004-09-24 19:39:26 +0000821 fprintf(stdout,"%d: %s", getpid(), zBuf);
drh2ac3ee92004-06-07 16:27:46 +0000822 fflush(stdout);
drhe54ca3f2004-06-07 01:52:14 +0000823}
824#endif