blob: f4dc4eeae80bd6625653e723b15f1b094adaea10 [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 */
93 char *charset; /* The character set for conversion */
94 char *prefix; /* Prefix on non-zero values in alt format */
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*/
108static et_info fmtinfo[] = {
drhe84a3062004-02-02 12:29:25 +0000109 { 'd', 10, 1, etRADIX, "0123456789", 0 },
110 { 's', 0, 0, etSTRING, 0, 0 },
111 { 'z', 0, 2, etDYNSTRING, 0, 0 },
112 { 'q', 0, 0, etSQLESCAPE, 0, 0 },
113 { 'Q', 0, 0, etSQLESCAPE2, 0, 0 },
114 { 'c', 0, 0, etCHARX, 0, 0 },
115 { 'o', 8, 0, etRADIX, "01234567", "0" },
116 { 'u', 10, 0, etRADIX, "0123456789", 0 },
117 { 'x', 16, 0, etRADIX, "0123456789abcdef", "x0" },
118 { 'X', 16, 0, etRADIX, "0123456789ABCDEF", "X0" },
119 { 'f', 0, 1, etFLOAT, 0, 0 },
120 { 'e', 0, 1, etEXP, "e", 0 },
121 { 'E', 0, 1, etEXP, "E", 0 },
122 { 'g', 0, 1, etGENERIC, "e", 0 },
123 { 'G', 0, 1, etGENERIC, "E", 0 },
124 { 'i', 10, 1, etRADIX, "0123456789", 0 },
125 { 'n', 0, 0, etSIZE, 0, 0 },
126 { '%', 0, 0, etPERCENT, 0, 0 },
drhfe63d1c2004-09-08 20:13:04 +0000127 { 'p', 16, 0, etPOINTER, "0123456789abcdef", "x0" },
drh5f968432004-02-21 19:02:30 +0000128 { 'T', 0, 2, etTOKEN, 0, 0 },
129 { 'S', 0, 2, etSRCLIST, 0, 0 },
drha18c5682000-10-08 22:20:57 +0000130};
131#define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0]))
132
133/*
134** If NOFLOATINGPOINT is defined, then none of the floating point
135** conversions will work.
136*/
137#ifndef etNOFLOATINGPOINT
138/*
139** "*val" is a double such that 0.1 <= *val < 10.0
140** Return the ascii code for the leading digit of *val, then
141** multiply "*val" by 10.0 to renormalize.
142**
143** Example:
144** input: *val = 3.14159
145** output: *val = 1.4159 function return = '3'
146**
147** The counter *cnt is incremented each time. After counter exceeds
148** 16 (the number of significant digits in a 64-bit float) '0' is
149** always returned.
150*/
drh384eef32004-01-07 03:04:27 +0000151static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
drha18c5682000-10-08 22:20:57 +0000152 int digit;
drh384eef32004-01-07 03:04:27 +0000153 LONGDOUBLE_TYPE d;
drha18c5682000-10-08 22:20:57 +0000154 if( (*cnt)++ >= 16 ) return '0';
155 digit = (int)*val;
156 d = digit;
157 digit += '0';
158 *val = (*val - d)*10.0;
159 return digit;
160}
161#endif
162
163#define etBUFSIZE 1000 /* Size of the output buffer */
164
165/*
166** The root program. All variations call this core.
167**
168** INPUTS:
169** func This is a pointer to a function taking three arguments
170** 1. A pointer to anything. Same as the "arg" parameter.
171** 2. A pointer to the list of characters to be output
172** (Note, this list is NOT null terminated.)
173** 3. An integer number of characters to be output.
174** (Note: This number might be zero.)
175**
176** arg This is the pointer to anything which will be passed as the
177** first argument to "func". Use it for whatever you like.
178**
179** fmt This is the format string, as in the usual print.
180**
181** ap This is a pointer to a list of arguments. Same as in
182** vfprint.
183**
184** OUTPUTS:
185** The return value is the total number of characters sent to
186** the function "func". Returns -1 on a error.
187**
188** Note that the order in which automatic variables are declared below
189** seems to make a big difference in determining how fast this beast
190** will run.
191*/
192static int vxprintf(
drh5f968432004-02-21 19:02:30 +0000193 void (*func)(void*,const char*,int), /* Consumer of text */
194 void *arg, /* First argument to the consumer */
195 int useExtended, /* Allow extended %-conversions */
196 const char *fmt, /* Format string */
197 va_list ap /* arguments */
drha18c5682000-10-08 22:20:57 +0000198){
drhe84a3062004-02-02 12:29:25 +0000199 int c; /* Next character in the format string */
200 char *bufpt; /* Pointer to the conversion buffer */
201 int precision; /* Precision of the current field */
202 int length; /* Length of the field */
203 int idx; /* A general purpose loop counter */
204 int count; /* Total number of characters output */
205 int width; /* Width of the current field */
206 etByte flag_leftjustify; /* True if "-" flag is present */
207 etByte flag_plussign; /* True if "+" flag is present */
208 etByte flag_blanksign; /* True if " " flag is present */
209 etByte flag_alternateform; /* True if "#" flag is present */
210 etByte flag_zeropad; /* True if field width constant starts with zero */
211 etByte flag_long; /* True if "l" flag is present */
drha34b6762004-05-07 13:30:42 +0000212 etByte flag_longlong; /* True if the "ll" flag is present */
213 UINT64_TYPE longvalue; /* Value for integer types */
drh384eef32004-01-07 03:04:27 +0000214 LONGDOUBLE_TYPE realvalue; /* Value for real types */
drhe84a3062004-02-02 12:29:25 +0000215 et_info *infop; /* Pointer to the appropriate info structure */
216 char buf[etBUFSIZE]; /* Conversion buffer */
217 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
218 etByte errorflag = 0; /* True if an error is encountered */
219 etByte xtype; /* Conversion paradigm */
220 char *zExtra; /* Extra memory used for etTCLESCAPE conversions */
drh905793e2004-02-21 13:31:09 +0000221 static char spaces[] = " ";
drha18c5682000-10-08 22:20:57 +0000222#define etSPACESIZE (sizeof(spaces)-1)
223#ifndef etNOFLOATINGPOINT
drhe84a3062004-02-02 12:29:25 +0000224 int exp; /* exponent of real numbers */
225 double rounder; /* Used for rounding floating point values */
226 etByte flag_dp; /* True if decimal point should be shown */
227 etByte flag_rtz; /* True if trailing zeros should be removed */
228 etByte flag_exp; /* True to force display of the exponent */
229 int nsd; /* Number of significant digits returned */
drha18c5682000-10-08 22:20:57 +0000230#endif
231
drhe29b1a02004-07-17 21:56:09 +0000232 func(arg,"",0);
drha18c5682000-10-08 22:20:57 +0000233 count = length = 0;
234 bufpt = 0;
235 for(; (c=(*fmt))!=0; ++fmt){
236 if( c!='%' ){
drhe84a3062004-02-02 12:29:25 +0000237 int amt;
drha18c5682000-10-08 22:20:57 +0000238 bufpt = (char *)fmt;
239 amt = 1;
240 while( (c=(*++fmt))!='%' && c!=0 ) amt++;
241 (*func)(arg,bufpt,amt);
242 count += amt;
243 if( c==0 ) break;
244 }
245 if( (c=(*++fmt))==0 ){
246 errorflag = 1;
247 (*func)(arg,"%",1);
248 count++;
249 break;
250 }
251 /* Find out what flags are present */
252 flag_leftjustify = flag_plussign = flag_blanksign =
drhe84a3062004-02-02 12:29:25 +0000253 flag_alternateform = flag_zeropad = 0;
drha18c5682000-10-08 22:20:57 +0000254 do{
255 switch( c ){
256 case '-': flag_leftjustify = 1; c = 0; break;
257 case '+': flag_plussign = 1; c = 0; break;
258 case ' ': flag_blanksign = 1; c = 0; break;
259 case '#': flag_alternateform = 1; c = 0; break;
260 case '0': flag_zeropad = 1; c = 0; break;
drha18c5682000-10-08 22:20:57 +0000261 default: break;
262 }
263 }while( c==0 && (c=(*++fmt))!=0 );
drha18c5682000-10-08 22:20:57 +0000264 /* Get the field width */
265 width = 0;
266 if( c=='*' ){
267 width = va_arg(ap,int);
268 if( width<0 ){
269 flag_leftjustify = 1;
270 width = -width;
271 }
272 c = *++fmt;
273 }else{
drh17a68932001-01-31 13:28:08 +0000274 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000275 width = width*10 + c - '0';
276 c = *++fmt;
277 }
278 }
279 if( width > etBUFSIZE-10 ){
280 width = etBUFSIZE-10;
281 }
282 /* Get the precision */
283 if( c=='.' ){
284 precision = 0;
285 c = *++fmt;
286 if( c=='*' ){
287 precision = va_arg(ap,int);
drha18c5682000-10-08 22:20:57 +0000288 if( precision<0 ) precision = -precision;
drha18c5682000-10-08 22:20:57 +0000289 c = *++fmt;
290 }else{
drh17a68932001-01-31 13:28:08 +0000291 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000292 precision = precision*10 + c - '0';
293 c = *++fmt;
294 }
295 }
296 /* Limit the precision to prevent overflowing buf[] during conversion */
297 if( precision>etBUFSIZE-40 ) precision = etBUFSIZE-40;
298 }else{
299 precision = -1;
300 }
301 /* Get the conversion type modifier */
302 if( c=='l' ){
303 flag_long = 1;
304 c = *++fmt;
drha34b6762004-05-07 13:30:42 +0000305 if( c=='l' ){
306 flag_longlong = 1;
307 c = *++fmt;
308 }else{
309 flag_longlong = 0;
310 }
drha18c5682000-10-08 22:20:57 +0000311 }else{
drha34b6762004-05-07 13:30:42 +0000312 flag_long = flag_longlong = 0;
drha18c5682000-10-08 22:20:57 +0000313 }
314 /* Fetch the info entry for the field */
315 infop = 0;
drhe84a3062004-02-02 12:29:25 +0000316 xtype = etERROR;
drha18c5682000-10-08 22:20:57 +0000317 for(idx=0; idx<etNINFO; idx++){
318 if( c==fmtinfo[idx].fmttype ){
319 infop = &fmtinfo[idx];
drh5f968432004-02-21 19:02:30 +0000320 if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
321 xtype = infop->type;
322 }
drha18c5682000-10-08 22:20:57 +0000323 break;
324 }
325 }
drha18c5682000-10-08 22:20:57 +0000326 zExtra = 0;
327
328 /*
329 ** At this point, variables are initialized as follows:
330 **
331 ** flag_alternateform TRUE if a '#' is present.
332 ** flag_plussign TRUE if a '+' is present.
333 ** flag_leftjustify TRUE if a '-' is present or if the
334 ** field width was negative.
335 ** flag_zeropad TRUE if the width began with 0.
336 ** flag_long TRUE if the letter 'l' (ell) prefixed
337 ** the conversion character.
drha34b6762004-05-07 13:30:42 +0000338 ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed
339 ** the conversion character.
drha18c5682000-10-08 22:20:57 +0000340 ** flag_blanksign TRUE if a ' ' is present.
341 ** width The specified field width. This is
342 ** always non-negative. Zero is the default.
343 ** precision The specified precision. The default
344 ** is -1.
345 ** xtype The class of the conversion.
346 ** infop Pointer to the appropriate info struct.
347 */
348 switch( xtype ){
drhfe63d1c2004-09-08 20:13:04 +0000349 case etPOINTER:
350 flag_longlong = sizeof(char*)==sizeof(i64);
351 flag_long = sizeof(char*)==sizeof(long int);
352 /* Fall through into the next case */
drha18c5682000-10-08 22:20:57 +0000353 case etRADIX:
drhe84a3062004-02-02 12:29:25 +0000354 if( infop->flags & FLAG_SIGNED ){
drhe9707672004-06-25 01:10:48 +0000355 i64 v;
356 if( flag_longlong ) v = va_arg(ap,i64);
357 else if( flag_long ) v = va_arg(ap,long int);
358 else v = va_arg(ap,int);
359 if( v<0 ){
360 longvalue = -v;
361 prefix = '-';
danielk1977cfcdaef2004-05-12 07:33:33 +0000362 }else{
drhe9707672004-06-25 01:10:48 +0000363 longvalue = v;
364 if( flag_plussign ) prefix = '+';
danielk1977cfcdaef2004-05-12 07:33:33 +0000365 else if( flag_blanksign ) prefix = ' ';
366 else prefix = 0;
367 }
drhe9707672004-06-25 01:10:48 +0000368 }else{
369 if( flag_longlong ) longvalue = va_arg(ap,u64);
370 else if( flag_long ) longvalue = va_arg(ap,unsigned long int);
371 else longvalue = va_arg(ap,unsigned int);
372 prefix = 0;
373 }
374 if( longvalue==0 ) flag_alternateform = 0;
drha18c5682000-10-08 22:20:57 +0000375 if( flag_zeropad && precision<width-(prefix!=0) ){
376 precision = width-(prefix!=0);
drh9adf9ac2002-05-15 11:44:13 +0000377 }
drh3039c0a2004-02-29 00:11:30 +0000378 bufpt = &buf[etBUFSIZE-1];
drha18c5682000-10-08 22:20:57 +0000379 {
380 register char *cset; /* Use registers for speed */
381 register int base;
382 cset = infop->charset;
383 base = infop->base;
384 do{ /* Convert to ascii */
385 *(--bufpt) = cset[longvalue%base];
386 longvalue = longvalue/base;
387 }while( longvalue>0 );
drh9adf9ac2002-05-15 11:44:13 +0000388 }
drh3039c0a2004-02-29 00:11:30 +0000389 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000390 for(idx=precision-length; idx>0; idx--){
391 *(--bufpt) = '0'; /* Zero pad */
drh9adf9ac2002-05-15 11:44:13 +0000392 }
drha18c5682000-10-08 22:20:57 +0000393 if( prefix ) *(--bufpt) = prefix; /* Add sign */
394 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
395 char *pre, x;
396 pre = infop->prefix;
397 if( *bufpt!=pre[0] ){
398 for(pre=infop->prefix; (x=(*pre))!=0; pre++) *(--bufpt) = x;
drh9adf9ac2002-05-15 11:44:13 +0000399 }
drha18c5682000-10-08 22:20:57 +0000400 }
drh3039c0a2004-02-29 00:11:30 +0000401 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000402 break;
403 case etFLOAT:
404 case etEXP:
405 case etGENERIC:
406 realvalue = va_arg(ap,double);
407#ifndef etNOFLOATINGPOINT
408 if( precision<0 ) precision = 6; /* Set default precision */
409 if( precision>etBUFSIZE-10 ) precision = etBUFSIZE-10;
410 if( realvalue<0.0 ){
411 realvalue = -realvalue;
412 prefix = '-';
drh9adf9ac2002-05-15 11:44:13 +0000413 }else{
drha18c5682000-10-08 22:20:57 +0000414 if( flag_plussign ) prefix = '+';
415 else if( flag_blanksign ) prefix = ' ';
416 else prefix = 0;
drh9adf9ac2002-05-15 11:44:13 +0000417 }
drha18c5682000-10-08 22:20:57 +0000418 if( infop->type==etGENERIC && precision>0 ) precision--;
419 rounder = 0.0;
drh5f968432004-02-21 19:02:30 +0000420#if 0
drha18c5682000-10-08 22:20:57 +0000421 /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */
422 for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
423#else
424 /* It makes more sense to use 0.5 */
425 for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1);
426#endif
427 if( infop->type==etFLOAT ) realvalue += rounder;
428 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
429 exp = 0;
430 if( realvalue>0.0 ){
drhb621c232004-02-21 19:41:04 +0000431 while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
432 while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
433 while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; }
434 while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; }
435 if( exp>350 || exp<-350 ){
drha18c5682000-10-08 22:20:57 +0000436 bufpt = "NaN";
437 length = 3;
438 break;
439 }
drh9adf9ac2002-05-15 11:44:13 +0000440 }
drha18c5682000-10-08 22:20:57 +0000441 bufpt = buf;
442 /*
443 ** If the field type is etGENERIC, then convert to either etEXP
444 ** or etFLOAT, as appropriate.
445 */
446 flag_exp = xtype==etEXP;
447 if( xtype!=etFLOAT ){
448 realvalue += rounder;
449 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
450 }
451 if( xtype==etGENERIC ){
452 flag_rtz = !flag_alternateform;
453 if( exp<-4 || exp>precision ){
454 xtype = etEXP;
455 }else{
456 precision = precision - exp;
457 xtype = etFLOAT;
458 }
drh9adf9ac2002-05-15 11:44:13 +0000459 }else{
drha18c5682000-10-08 22:20:57 +0000460 flag_rtz = 0;
drh9adf9ac2002-05-15 11:44:13 +0000461 }
drha18c5682000-10-08 22:20:57 +0000462 /*
463 ** The "exp+precision" test causes output to be of type etEXP if
464 ** the precision is too large to fit in buf[].
465 */
466 nsd = 0;
467 if( xtype==etFLOAT && exp+precision<etBUFSIZE-30 ){
468 flag_dp = (precision>0 || flag_alternateform);
469 if( prefix ) *(bufpt++) = prefix; /* Sign */
470 if( exp<0 ) *(bufpt++) = '0'; /* Digits before "." */
471 else for(; exp>=0; exp--) *(bufpt++) = et_getdigit(&realvalue,&nsd);
472 if( flag_dp ) *(bufpt++) = '.'; /* The decimal point */
473 for(exp++; exp<0 && precision>0; precision--, exp++){
474 *(bufpt++) = '0';
475 }
476 while( (precision--)>0 ) *(bufpt++) = et_getdigit(&realvalue,&nsd);
477 *(bufpt--) = 0; /* Null terminate */
478 if( flag_rtz && flag_dp ){ /* Remove trailing zeros and "." */
479 while( bufpt>=buf && *bufpt=='0' ) *(bufpt--) = 0;
480 if( bufpt>=buf && *bufpt=='.' ) *(bufpt--) = 0;
481 }
482 bufpt++; /* point to next free slot */
drh9adf9ac2002-05-15 11:44:13 +0000483 }else{ /* etEXP or etGENERIC */
drha18c5682000-10-08 22:20:57 +0000484 flag_dp = (precision>0 || flag_alternateform);
485 if( prefix ) *(bufpt++) = prefix; /* Sign */
486 *(bufpt++) = et_getdigit(&realvalue,&nsd); /* First digit */
487 if( flag_dp ) *(bufpt++) = '.'; /* Decimal point */
488 while( (precision--)>0 ) *(bufpt++) = et_getdigit(&realvalue,&nsd);
489 bufpt--; /* point to last digit */
490 if( flag_rtz && flag_dp ){ /* Remove tail zeros */
491 while( bufpt>=buf && *bufpt=='0' ) *(bufpt--) = 0;
492 if( bufpt>=buf && *bufpt=='.' ) *(bufpt--) = 0;
493 }
494 bufpt++; /* point to next free slot */
495 if( exp || flag_exp ){
496 *(bufpt++) = infop->charset[0];
497 if( exp<0 ){ *(bufpt++) = '-'; exp = -exp; } /* sign of exp */
498 else { *(bufpt++) = '+'; }
499 if( exp>=100 ){
500 *(bufpt++) = (exp/100)+'0'; /* 100's digit */
501 exp %= 100;
drh9adf9ac2002-05-15 11:44:13 +0000502 }
drha18c5682000-10-08 22:20:57 +0000503 *(bufpt++) = exp/10+'0'; /* 10's digit */
504 *(bufpt++) = exp%10+'0'; /* 1's digit */
505 }
drh9adf9ac2002-05-15 11:44:13 +0000506 }
drha18c5682000-10-08 22:20:57 +0000507 /* The converted number is in buf[] and zero terminated. Output it.
508 ** Note that the number is in the usual order, not reversed as with
509 ** integer conversions. */
drh94ce4c12003-06-07 11:29:50 +0000510 length = bufpt-buf;
drha18c5682000-10-08 22:20:57 +0000511 bufpt = buf;
512
513 /* Special case: Add leading zeros if the flag_zeropad flag is
514 ** set and we are not left justified */
515 if( flag_zeropad && !flag_leftjustify && length < width){
516 int i;
517 int nPad = width - length;
518 for(i=width; i>=nPad; i--){
519 bufpt[i] = bufpt[i-nPad];
520 }
521 i = prefix!=0;
522 while( nPad-- ) bufpt[i++] = '0';
523 length = width;
524 }
525#endif
526 break;
527 case etSIZE:
528 *(va_arg(ap,int*)) = count;
529 length = width = 0;
530 break;
531 case etPERCENT:
532 buf[0] = '%';
533 bufpt = buf;
534 length = 1;
535 break;
536 case etCHARLIT:
537 case etCHARX:
538 c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt);
539 if( precision>=0 ){
540 for(idx=1; idx<precision; idx++) buf[idx] = c;
541 length = precision;
drh9adf9ac2002-05-15 11:44:13 +0000542 }else{
drha18c5682000-10-08 22:20:57 +0000543 length =1;
drh9adf9ac2002-05-15 11:44:13 +0000544 }
drha18c5682000-10-08 22:20:57 +0000545 bufpt = buf;
546 break;
547 case etSTRING:
drhd93d8a82003-06-16 03:08:18 +0000548 case etDYNSTRING:
drhcb485882002-08-15 13:50:48 +0000549 bufpt = va_arg(ap,char*);
drhd93d8a82003-06-16 03:08:18 +0000550 if( bufpt==0 ){
551 bufpt = "";
552 }else if( xtype==etDYNSTRING ){
553 zExtra = bufpt;
554 }
drha18c5682000-10-08 22:20:57 +0000555 length = strlen(bufpt);
556 if( precision>=0 && precision<length ) length = precision;
557 break;
558 case etSQLESCAPE:
chw0cfcf3f2002-06-16 04:55:48 +0000559 case etSQLESCAPE2:
drha18c5682000-10-08 22:20:57 +0000560 {
chw0cfcf3f2002-06-16 04:55:48 +0000561 int i, j, n, c, isnull;
drha18c5682000-10-08 22:20:57 +0000562 char *arg = va_arg(ap,char*);
chw0cfcf3f2002-06-16 04:55:48 +0000563 isnull = arg==0;
564 if( isnull ) arg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
drha18c5682000-10-08 22:20:57 +0000565 for(i=n=0; (c=arg[i])!=0; i++){
566 if( c=='\'' ) n++;
567 }
chw0cfcf3f2002-06-16 04:55:48 +0000568 n += i + 1 + ((!isnull && xtype==etSQLESCAPE2) ? 2 : 0);
drha18c5682000-10-08 22:20:57 +0000569 if( n>etBUFSIZE ){
570 bufpt = zExtra = sqliteMalloc( n );
drhdaffd0e2001-04-11 14:28:42 +0000571 if( bufpt==0 ) return -1;
drha18c5682000-10-08 22:20:57 +0000572 }else{
573 bufpt = buf;
574 }
chw0cfcf3f2002-06-16 04:55:48 +0000575 j = 0;
576 if( !isnull && xtype==etSQLESCAPE2 ) bufpt[j++] = '\'';
577 for(i=0; (c=arg[i])!=0; i++){
drha18c5682000-10-08 22:20:57 +0000578 bufpt[j++] = c;
579 if( c=='\'' ) bufpt[j++] = c;
580 }
chw0cfcf3f2002-06-16 04:55:48 +0000581 if( !isnull && xtype==etSQLESCAPE2 ) bufpt[j++] = '\'';
drha18c5682000-10-08 22:20:57 +0000582 bufpt[j] = 0;
583 length = j;
584 if( precision>=0 && precision<length ) length = precision;
585 }
586 break;
drh5f968432004-02-21 19:02:30 +0000587 case etTOKEN: {
588 Token *pToken = va_arg(ap, Token*);
589 (*func)(arg, pToken->z, pToken->n);
590 length = width = 0;
591 break;
592 }
593 case etSRCLIST: {
594 SrcList *pSrc = va_arg(ap, SrcList*);
595 int k = va_arg(ap, int);
596 struct SrcList_item *pItem = &pSrc->a[k];
597 assert( k>=0 && k<pSrc->nSrc );
598 if( pItem->zDatabase && pItem->zDatabase[0] ){
599 (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase));
600 (*func)(arg, ".", 1);
601 }
602 (*func)(arg, pItem->zName, strlen(pItem->zName));
603 length = width = 0;
604 break;
605 }
drha18c5682000-10-08 22:20:57 +0000606 case etERROR:
607 buf[0] = '%';
608 buf[1] = c;
609 errorflag = 0;
610 idx = 1+(c!=0);
611 (*func)(arg,"%",idx);
612 count += idx;
613 if( c==0 ) fmt--;
614 break;
615 }/* End switch over the format type */
616 /*
617 ** The text of the conversion is pointed to by "bufpt" and is
618 ** "length" characters long. The field width is "width". Do
619 ** the output.
620 */
621 if( !flag_leftjustify ){
622 register int nspace;
623 nspace = width-length;
624 if( nspace>0 ){
drha18c5682000-10-08 22:20:57 +0000625 count += nspace;
626 while( nspace>=etSPACESIZE ){
627 (*func)(arg,spaces,etSPACESIZE);
628 nspace -= etSPACESIZE;
629 }
630 if( nspace>0 ) (*func)(arg,spaces,nspace);
631 }
632 }
633 if( length>0 ){
634 (*func)(arg,bufpt,length);
635 count += length;
636 }
637 if( flag_leftjustify ){
638 register int nspace;
639 nspace = width-length;
640 if( nspace>0 ){
641 count += nspace;
642 while( nspace>=etSPACESIZE ){
643 (*func)(arg,spaces,etSPACESIZE);
644 nspace -= etSPACESIZE;
645 }
646 if( nspace>0 ) (*func)(arg,spaces,nspace);
647 }
648 }
649 if( zExtra ){
drh5f968432004-02-21 19:02:30 +0000650 sqliteFree(zExtra);
drha18c5682000-10-08 22:20:57 +0000651 }
652 }/* End for loop over the format string */
653 return errorflag ? -1 : count;
654} /* End of function */
655
656
657/* This structure is used to store state information about the
658** write to memory that is currently in progress.
659*/
660struct sgMprintf {
661 char *zBase; /* A base allocation */
662 char *zText; /* The string collected so far */
663 int nChar; /* Length of the string so far */
drh5f968432004-02-21 19:02:30 +0000664 int nTotal; /* Output size if unconstrained */
drha18c5682000-10-08 22:20:57 +0000665 int nAlloc; /* Amount of space allocated in zText */
drh5f968432004-02-21 19:02:30 +0000666 void *(*xRealloc)(void*,int); /* Function used to realloc memory */
drha18c5682000-10-08 22:20:57 +0000667};
668
669/*
670** This function implements the callback from vxprintf.
671**
672** This routine add nNewChar characters of text in zNewText to
673** the sgMprintf structure pointed to by "arg".
674*/
drh5f968432004-02-21 19:02:30 +0000675static void mout(void *arg, const char *zNewText, int nNewChar){
drha18c5682000-10-08 22:20:57 +0000676 struct sgMprintf *pM = (struct sgMprintf*)arg;
drh5f968432004-02-21 19:02:30 +0000677 pM->nTotal += nNewChar;
drha18c5682000-10-08 22:20:57 +0000678 if( pM->nChar + nNewChar + 1 > pM->nAlloc ){
drh5f968432004-02-21 19:02:30 +0000679 if( pM->xRealloc==0 ){
680 nNewChar = pM->nAlloc - pM->nChar - 1;
drha18c5682000-10-08 22:20:57 +0000681 }else{
drh5f968432004-02-21 19:02:30 +0000682 pM->nAlloc = pM->nChar + nNewChar*2 + 1;
683 if( pM->zText==pM->zBase ){
684 pM->zText = pM->xRealloc(0, pM->nAlloc);
685 if( pM->zText && pM->nChar ){
686 memcpy(pM->zText, pM->zBase, pM->nChar);
687 }
688 }else{
689 pM->zText = pM->xRealloc(pM->zText, pM->nAlloc);
drh6d4abfb2001-10-22 02:58:08 +0000690 }
drha18c5682000-10-08 22:20:57 +0000691 }
692 }
drhe29b1a02004-07-17 21:56:09 +0000693 if( pM->zText ){
694 if( nNewChar>0 ){
695 memcpy(&pM->zText[pM->nChar], zNewText, nNewChar);
696 pM->nChar += nNewChar;
697 }
drha18c5682000-10-08 22:20:57 +0000698 pM->zText[pM->nChar] = 0;
699 }
700}
701
702/*
drh5f968432004-02-21 19:02:30 +0000703** This routine is a wrapper around xprintf() that invokes mout() as
704** the consumer.
drha18c5682000-10-08 22:20:57 +0000705*/
drh5f968432004-02-21 19:02:30 +0000706static char *base_vprintf(
707 void *(*xRealloc)(void*,int), /* Routine to realloc memory. May be NULL */
708 int useInternal, /* Use internal %-conversions if true */
709 char *zInitBuf, /* Initially write here, before mallocing */
710 int nInitBuf, /* Size of zInitBuf[] */
711 const char *zFormat, /* format string */
712 va_list ap /* arguments */
713){
714 struct sgMprintf sM;
715 sM.zBase = sM.zText = zInitBuf;
716 sM.nChar = sM.nTotal = 0;
717 sM.nAlloc = nInitBuf;
718 sM.xRealloc = xRealloc;
719 vxprintf(mout, &sM, useInternal, zFormat, ap);
720 if( xRealloc ){
721 if( sM.zText==sM.zBase ){
722 sM.zText = xRealloc(0, sM.nChar+1);
danielk19778def5ea2004-06-16 10:39:52 +0000723 if( sM.zText ){
724 memcpy(sM.zText, sM.zBase, sM.nChar+1);
725 }
drh5f968432004-02-21 19:02:30 +0000726 }else if( sM.nAlloc>sM.nChar+10 ){
727 sM.zText = xRealloc(sM.zText, sM.nChar+1);
728 }
729 }
730 return sM.zText;
drh483750b2003-01-29 18:46:51 +0000731}
732
733/*
drh5f968432004-02-21 19:02:30 +0000734** Realloc that is a real function, not a macro.
735*/
736static void *printf_realloc(void *old, int size){
737 return sqliteRealloc(old,size);
738}
739
740/*
741** Print into memory obtained from sqliteMalloc(). Use the internal
742** %-conversion extensions.
743*/
danielk19774adee202004-05-08 08:23:19 +0000744char *sqlite3VMPrintf(const char *zFormat, va_list ap){
drh5f968432004-02-21 19:02:30 +0000745 char zBase[1000];
746 return base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
747}
748
749/*
750** Print into memory obtained from sqliteMalloc(). Use the internal
751** %-conversion extensions.
752*/
danielk19774adee202004-05-08 08:23:19 +0000753char *sqlite3MPrintf(const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000754 va_list ap;
755 char *z;
756 char zBase[1000];
757 va_start(ap, zFormat);
758 z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
759 va_end(ap);
760 return z;
761}
762
763/*
764** Print into memory obtained from malloc(). Do not use the internal
765** %-conversion extensions. This routine is for use by external users.
drh483750b2003-01-29 18:46:51 +0000766*/
danielk19776f8a5032004-05-10 10:34:51 +0000767char *sqlite3_mprintf(const char *zFormat, ...){
drha18c5682000-10-08 22:20:57 +0000768 va_list ap;
drh5f968432004-02-21 19:02:30 +0000769 char *z;
drha18c5682000-10-08 22:20:57 +0000770 char zBuf[200];
771
drha18c5682000-10-08 22:20:57 +0000772 va_start(ap,zFormat);
drh5f968432004-02-21 19:02:30 +0000773 z = base_vprintf((void*(*)(void*,int))realloc, 0,
774 zBuf, sizeof(zBuf), zFormat, ap);
drha18c5682000-10-08 22:20:57 +0000775 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000776 return z;
drha18c5682000-10-08 22:20:57 +0000777}
778
danielk19776f8a5032004-05-10 10:34:51 +0000779/* This is the varargs version of sqlite3_mprintf.
drha18c5682000-10-08 22:20:57 +0000780*/
danielk19776f8a5032004-05-10 10:34:51 +0000781char *sqlite3_vmprintf(const char *zFormat, va_list ap){
drha18c5682000-10-08 22:20:57 +0000782 char zBuf[200];
drh5f968432004-02-21 19:02:30 +0000783 return base_vprintf((void*(*)(void*,int))realloc, 0,
784 zBuf, sizeof(zBuf), zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +0000785}
786
787/*
danielk19776f8a5032004-05-10 10:34:51 +0000788** sqlite3_snprintf() works like snprintf() except that it ignores the
drh93a5c6b2003-12-23 02:17:35 +0000789** current locale settings. This is important for SQLite because we
790** are not able to use a "," as the decimal point in place of "." as
791** specified by some locales.
792*/
danielk19776f8a5032004-05-10 10:34:51 +0000793char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000794 char *z;
drh93a5c6b2003-12-23 02:17:35 +0000795 va_list ap;
drh93a5c6b2003-12-23 02:17:35 +0000796
drh93a5c6b2003-12-23 02:17:35 +0000797 va_start(ap,zFormat);
drh5f968432004-02-21 19:02:30 +0000798 z = base_vprintf(0, 0, zBuf, n, zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +0000799 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000800 return z;
drh93a5c6b2003-12-23 02:17:35 +0000801}
802
drh1b743be2004-06-22 22:04:46 +0000803#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
drhe54ca3f2004-06-07 01:52:14 +0000804/*
805** A version of printf() that understands %lld. Used for debugging.
806** The printf() built into some versions of windows does not understand %lld
807** and segfaults if you give it a long long int.
808*/
809void sqlite3DebugPrintf(const char *zFormat, ...){
810 va_list ap;
811 char zBuf[500];
812 va_start(ap, zFormat);
813 base_vprintf(0, 0, zBuf, sizeof(zBuf), zFormat, ap);
814 va_end(ap);
drh2ac3ee92004-06-07 16:27:46 +0000815 fprintf(stdout,"%s", zBuf);
816 fflush(stdout);
drhe54ca3f2004-06-07 01:52:14 +0000817}
818#endif