blob: 539455a00a2e5bb3472fc011e85c1b9e56979c57 [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 */
drh4794f732004-11-05 17:17:50 +0000102#define FLAG_STRING 4 /* Allow infinity precision */
drhe84a3062004-02-02 12:29:25 +0000103
104
105/*
drha18c5682000-10-08 22:20:57 +0000106** The following table is searched linearly, so it is good to put the
107** most frequently used conversion types first.
108*/
drh76ff3a02004-09-24 22:32:30 +0000109static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
110static const char aPrefix[] = "-x0\000X0";
drh57196282004-10-06 15:41:16 +0000111static const et_info fmtinfo[] = {
drh76ff3a02004-09-24 22:32:30 +0000112 { 'd', 10, 1, etRADIX, 0, 0 },
drh4794f732004-11-05 17:17:50 +0000113 { 's', 0, 4, etSTRING, 0, 0 },
drh557cc602005-08-13 12:59:14 +0000114 { 'g', 0, 1, etGENERIC, 30, 0 },
drh4794f732004-11-05 17:17:50 +0000115 { 'z', 0, 6, etDYNSTRING, 0, 0 },
116 { 'q', 0, 4, etSQLESCAPE, 0, 0 },
117 { 'Q', 0, 4, etSQLESCAPE2, 0, 0 },
drh76ff3a02004-09-24 22:32:30 +0000118 { 'c', 0, 0, etCHARX, 0, 0 },
119 { 'o', 8, 0, etRADIX, 0, 2 },
120 { 'u', 10, 0, etRADIX, 0, 0 },
121 { 'x', 16, 0, etRADIX, 16, 1 },
122 { 'X', 16, 0, etRADIX, 0, 4 },
drhb37df7b2005-10-13 02:09:49 +0000123#ifndef SQLITE_OMIT_FLOATING_POINT
drh76ff3a02004-09-24 22:32:30 +0000124 { 'f', 0, 1, etFLOAT, 0, 0 },
125 { 'e', 0, 1, etEXP, 30, 0 },
126 { 'E', 0, 1, etEXP, 14, 0 },
drh76ff3a02004-09-24 22:32:30 +0000127 { 'G', 0, 1, etGENERIC, 14, 0 },
drhb37df7b2005-10-13 02:09:49 +0000128#endif
drh76ff3a02004-09-24 22:32:30 +0000129 { 'i', 10, 1, etRADIX, 0, 0 },
130 { 'n', 0, 0, etSIZE, 0, 0 },
131 { '%', 0, 0, etPERCENT, 0, 0 },
132 { 'p', 16, 0, etPOINTER, 0, 1 },
133 { 'T', 0, 2, etTOKEN, 0, 0 },
134 { 'S', 0, 2, etSRCLIST, 0, 0 },
drha18c5682000-10-08 22:20:57 +0000135};
136#define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0]))
137
138/*
drhb37df7b2005-10-13 02:09:49 +0000139** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
drha18c5682000-10-08 22:20:57 +0000140** conversions will work.
141*/
drhb37df7b2005-10-13 02:09:49 +0000142#ifndef SQLITE_OMIT_FLOATING_POINT
drha18c5682000-10-08 22:20:57 +0000143/*
144** "*val" is a double such that 0.1 <= *val < 10.0
145** Return the ascii code for the leading digit of *val, then
146** multiply "*val" by 10.0 to renormalize.
147**
148** Example:
149** input: *val = 3.14159
150** output: *val = 1.4159 function return = '3'
151**
152** The counter *cnt is incremented each time. After counter exceeds
153** 16 (the number of significant digits in a 64-bit float) '0' is
154** always returned.
155*/
drh384eef32004-01-07 03:04:27 +0000156static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
drha18c5682000-10-08 22:20:57 +0000157 int digit;
drh384eef32004-01-07 03:04:27 +0000158 LONGDOUBLE_TYPE d;
drha18c5682000-10-08 22:20:57 +0000159 if( (*cnt)++ >= 16 ) return '0';
160 digit = (int)*val;
161 d = digit;
162 digit += '0';
163 *val = (*val - d)*10.0;
164 return digit;
165}
drhb37df7b2005-10-13 02:09:49 +0000166#endif /* SQLITE_OMIT_FLOATING_POINT */
drha18c5682000-10-08 22:20:57 +0000167
drh79158e12005-09-06 21:40:45 +0000168/*
169** On machines with a small stack size, you can redefine the
170** SQLITE_PRINT_BUF_SIZE to be less than 350. But beware - for
171** smaller values some %f conversions may go into an infinite loop.
172*/
173#ifndef SQLITE_PRINT_BUF_SIZE
174# define SQLITE_PRINT_BUF_SIZE 350
175#endif
176#define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
drha18c5682000-10-08 22:20:57 +0000177
178/*
179** The root program. All variations call this core.
180**
181** INPUTS:
182** func This is a pointer to a function taking three arguments
183** 1. A pointer to anything. Same as the "arg" parameter.
184** 2. A pointer to the list of characters to be output
185** (Note, this list is NOT null terminated.)
186** 3. An integer number of characters to be output.
187** (Note: This number might be zero.)
188**
189** arg This is the pointer to anything which will be passed as the
190** first argument to "func". Use it for whatever you like.
191**
192** fmt This is the format string, as in the usual print.
193**
194** ap This is a pointer to a list of arguments. Same as in
195** vfprint.
196**
197** OUTPUTS:
198** The return value is the total number of characters sent to
199** the function "func". Returns -1 on a error.
200**
201** Note that the order in which automatic variables are declared below
202** seems to make a big difference in determining how fast this beast
203** will run.
204*/
205static int vxprintf(
drh5f968432004-02-21 19:02:30 +0000206 void (*func)(void*,const char*,int), /* Consumer of text */
207 void *arg, /* First argument to the consumer */
208 int useExtended, /* Allow extended %-conversions */
209 const char *fmt, /* Format string */
210 va_list ap /* arguments */
drha18c5682000-10-08 22:20:57 +0000211){
drhe84a3062004-02-02 12:29:25 +0000212 int c; /* Next character in the format string */
213 char *bufpt; /* Pointer to the conversion buffer */
214 int precision; /* Precision of the current field */
215 int length; /* Length of the field */
216 int idx; /* A general purpose loop counter */
217 int count; /* Total number of characters output */
218 int width; /* Width of the current field */
219 etByte flag_leftjustify; /* True if "-" flag is present */
220 etByte flag_plussign; /* True if "+" flag is present */
221 etByte flag_blanksign; /* True if " " flag is present */
222 etByte flag_alternateform; /* True if "#" flag is present */
drh531fe872005-08-13 13:40:42 +0000223 etByte flag_altform2; /* True if "!" flag is present */
drhe84a3062004-02-02 12:29:25 +0000224 etByte flag_zeropad; /* True if field width constant starts with zero */
225 etByte flag_long; /* True if "l" flag is present */
drha34b6762004-05-07 13:30:42 +0000226 etByte flag_longlong; /* True if the "ll" flag is present */
drh3e9aeec2005-08-13 13:39:02 +0000227 etByte done; /* Loop termination flag */
drha34b6762004-05-07 13:30:42 +0000228 UINT64_TYPE longvalue; /* Value for integer types */
drh384eef32004-01-07 03:04:27 +0000229 LONGDOUBLE_TYPE realvalue; /* Value for real types */
drh57196282004-10-06 15:41:16 +0000230 const et_info *infop; /* Pointer to the appropriate info structure */
drhe84a3062004-02-02 12:29:25 +0000231 char buf[etBUFSIZE]; /* Conversion buffer */
232 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
233 etByte errorflag = 0; /* True if an error is encountered */
234 etByte xtype; /* Conversion paradigm */
235 char *zExtra; /* Extra memory used for etTCLESCAPE conversions */
drh57196282004-10-06 15:41:16 +0000236 static const char spaces[] =
237 " ";
drha18c5682000-10-08 22:20:57 +0000238#define etSPACESIZE (sizeof(spaces)-1)
drhb37df7b2005-10-13 02:09:49 +0000239#ifndef SQLITE_OMIT_FLOATING_POINT
drh557cc602005-08-13 12:59:14 +0000240 int exp, e2; /* exponent of real numbers */
drhe84a3062004-02-02 12:29:25 +0000241 double rounder; /* Used for rounding floating point values */
242 etByte flag_dp; /* True if decimal point should be shown */
243 etByte flag_rtz; /* True if trailing zeros should be removed */
244 etByte flag_exp; /* True to force display of the exponent */
245 int nsd; /* Number of significant digits returned */
drha18c5682000-10-08 22:20:57 +0000246#endif
247
drhe29b1a02004-07-17 21:56:09 +0000248 func(arg,"",0);
drha18c5682000-10-08 22:20:57 +0000249 count = length = 0;
250 bufpt = 0;
251 for(; (c=(*fmt))!=0; ++fmt){
252 if( c!='%' ){
drhe84a3062004-02-02 12:29:25 +0000253 int amt;
drha18c5682000-10-08 22:20:57 +0000254 bufpt = (char *)fmt;
255 amt = 1;
256 while( (c=(*++fmt))!='%' && c!=0 ) amt++;
257 (*func)(arg,bufpt,amt);
258 count += amt;
259 if( c==0 ) break;
260 }
261 if( (c=(*++fmt))==0 ){
262 errorflag = 1;
263 (*func)(arg,"%",1);
264 count++;
265 break;
266 }
267 /* Find out what flags are present */
268 flag_leftjustify = flag_plussign = flag_blanksign =
drh557cc602005-08-13 12:59:14 +0000269 flag_alternateform = flag_altform2 = flag_zeropad = 0;
drh3e9aeec2005-08-13 13:39:02 +0000270 done = 0;
drha18c5682000-10-08 22:20:57 +0000271 do{
272 switch( c ){
drh3e9aeec2005-08-13 13:39:02 +0000273 case '-': flag_leftjustify = 1; break;
274 case '+': flag_plussign = 1; break;
275 case ' ': flag_blanksign = 1; break;
276 case '#': flag_alternateform = 1; break;
277 case '!': flag_altform2 = 1; break;
278 case '0': flag_zeropad = 1; break;
279 default: done = 1; break;
drha18c5682000-10-08 22:20:57 +0000280 }
drh3e9aeec2005-08-13 13:39:02 +0000281 }while( !done && (c=(*++fmt))!=0 );
drha18c5682000-10-08 22:20:57 +0000282 /* Get the field width */
283 width = 0;
284 if( c=='*' ){
285 width = va_arg(ap,int);
286 if( width<0 ){
287 flag_leftjustify = 1;
288 width = -width;
289 }
290 c = *++fmt;
291 }else{
drh17a68932001-01-31 13:28:08 +0000292 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000293 width = width*10 + c - '0';
294 c = *++fmt;
295 }
296 }
297 if( width > etBUFSIZE-10 ){
298 width = etBUFSIZE-10;
299 }
300 /* Get the precision */
301 if( c=='.' ){
302 precision = 0;
303 c = *++fmt;
304 if( c=='*' ){
305 precision = va_arg(ap,int);
drha18c5682000-10-08 22:20:57 +0000306 if( precision<0 ) precision = -precision;
drha18c5682000-10-08 22:20:57 +0000307 c = *++fmt;
308 }else{
drh17a68932001-01-31 13:28:08 +0000309 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000310 precision = precision*10 + c - '0';
311 c = *++fmt;
312 }
313 }
drha18c5682000-10-08 22:20:57 +0000314 }else{
315 precision = -1;
316 }
317 /* Get the conversion type modifier */
318 if( c=='l' ){
319 flag_long = 1;
320 c = *++fmt;
drha34b6762004-05-07 13:30:42 +0000321 if( c=='l' ){
322 flag_longlong = 1;
323 c = *++fmt;
324 }else{
325 flag_longlong = 0;
326 }
drha18c5682000-10-08 22:20:57 +0000327 }else{
drha34b6762004-05-07 13:30:42 +0000328 flag_long = flag_longlong = 0;
drha18c5682000-10-08 22:20:57 +0000329 }
330 /* Fetch the info entry for the field */
331 infop = 0;
drhe84a3062004-02-02 12:29:25 +0000332 xtype = etERROR;
drha18c5682000-10-08 22:20:57 +0000333 for(idx=0; idx<etNINFO; idx++){
334 if( c==fmtinfo[idx].fmttype ){
335 infop = &fmtinfo[idx];
drh5f968432004-02-21 19:02:30 +0000336 if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
337 xtype = infop->type;
338 }
drha18c5682000-10-08 22:20:57 +0000339 break;
340 }
341 }
drha18c5682000-10-08 22:20:57 +0000342 zExtra = 0;
343
drh4794f732004-11-05 17:17:50 +0000344 /* Limit the precision to prevent overflowing buf[] during conversion */
345 if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
346 precision = etBUFSIZE-40;
347 }
348
drha18c5682000-10-08 22:20:57 +0000349 /*
350 ** At this point, variables are initialized as follows:
351 **
352 ** flag_alternateform TRUE if a '#' is present.
drh3e9aeec2005-08-13 13:39:02 +0000353 ** flag_altform2 TRUE if a '!' is present.
drha18c5682000-10-08 22:20:57 +0000354 ** flag_plussign TRUE if a '+' is present.
355 ** flag_leftjustify TRUE if a '-' is present or if the
356 ** field width was negative.
357 ** flag_zeropad TRUE if the width began with 0.
358 ** flag_long TRUE if the letter 'l' (ell) prefixed
359 ** the conversion character.
drha34b6762004-05-07 13:30:42 +0000360 ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed
361 ** the conversion character.
drha18c5682000-10-08 22:20:57 +0000362 ** flag_blanksign TRUE if a ' ' is present.
363 ** width The specified field width. This is
364 ** always non-negative. Zero is the default.
365 ** precision The specified precision. The default
366 ** is -1.
367 ** xtype The class of the conversion.
368 ** infop Pointer to the appropriate info struct.
369 */
370 switch( xtype ){
drhfe63d1c2004-09-08 20:13:04 +0000371 case etPOINTER:
372 flag_longlong = sizeof(char*)==sizeof(i64);
373 flag_long = sizeof(char*)==sizeof(long int);
374 /* Fall through into the next case */
drha18c5682000-10-08 22:20:57 +0000375 case etRADIX:
drhe84a3062004-02-02 12:29:25 +0000376 if( infop->flags & FLAG_SIGNED ){
drhe9707672004-06-25 01:10:48 +0000377 i64 v;
378 if( flag_longlong ) v = va_arg(ap,i64);
379 else if( flag_long ) v = va_arg(ap,long int);
380 else v = va_arg(ap,int);
381 if( v<0 ){
382 longvalue = -v;
383 prefix = '-';
danielk1977cfcdaef2004-05-12 07:33:33 +0000384 }else{
drhe9707672004-06-25 01:10:48 +0000385 longvalue = v;
386 if( flag_plussign ) prefix = '+';
danielk1977cfcdaef2004-05-12 07:33:33 +0000387 else if( flag_blanksign ) prefix = ' ';
388 else prefix = 0;
389 }
drhe9707672004-06-25 01:10:48 +0000390 }else{
391 if( flag_longlong ) longvalue = va_arg(ap,u64);
392 else if( flag_long ) longvalue = va_arg(ap,unsigned long int);
393 else longvalue = va_arg(ap,unsigned int);
394 prefix = 0;
395 }
396 if( longvalue==0 ) flag_alternateform = 0;
drha18c5682000-10-08 22:20:57 +0000397 if( flag_zeropad && precision<width-(prefix!=0) ){
398 precision = width-(prefix!=0);
drh9adf9ac2002-05-15 11:44:13 +0000399 }
drh3039c0a2004-02-29 00:11:30 +0000400 bufpt = &buf[etBUFSIZE-1];
drha18c5682000-10-08 22:20:57 +0000401 {
drh76ff3a02004-09-24 22:32:30 +0000402 register const char *cset; /* Use registers for speed */
drha18c5682000-10-08 22:20:57 +0000403 register int base;
drh76ff3a02004-09-24 22:32:30 +0000404 cset = &aDigits[infop->charset];
drha18c5682000-10-08 22:20:57 +0000405 base = infop->base;
406 do{ /* Convert to ascii */
407 *(--bufpt) = cset[longvalue%base];
408 longvalue = longvalue/base;
409 }while( longvalue>0 );
drh9adf9ac2002-05-15 11:44:13 +0000410 }
drh3039c0a2004-02-29 00:11:30 +0000411 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000412 for(idx=precision-length; idx>0; idx--){
413 *(--bufpt) = '0'; /* Zero pad */
drh9adf9ac2002-05-15 11:44:13 +0000414 }
drha18c5682000-10-08 22:20:57 +0000415 if( prefix ) *(--bufpt) = prefix; /* Add sign */
416 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
drh76ff3a02004-09-24 22:32:30 +0000417 const char *pre;
418 char x;
419 pre = &aPrefix[infop->prefix];
drha18c5682000-10-08 22:20:57 +0000420 if( *bufpt!=pre[0] ){
drh76ff3a02004-09-24 22:32:30 +0000421 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
drh9adf9ac2002-05-15 11:44:13 +0000422 }
drha18c5682000-10-08 22:20:57 +0000423 }
drh3039c0a2004-02-29 00:11:30 +0000424 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000425 break;
426 case etFLOAT:
427 case etEXP:
428 case etGENERIC:
429 realvalue = va_arg(ap,double);
drhb37df7b2005-10-13 02:09:49 +0000430#ifndef SQLITE_OMIT_FLOATING_POINT
drha18c5682000-10-08 22:20:57 +0000431 if( precision<0 ) precision = 6; /* Set default precision */
drh5eba8c02005-08-19 02:26:27 +0000432 if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10;
drha18c5682000-10-08 22:20:57 +0000433 if( realvalue<0.0 ){
434 realvalue = -realvalue;
435 prefix = '-';
drh9adf9ac2002-05-15 11:44:13 +0000436 }else{
drha18c5682000-10-08 22:20:57 +0000437 if( flag_plussign ) prefix = '+';
438 else if( flag_blanksign ) prefix = ' ';
439 else prefix = 0;
drh9adf9ac2002-05-15 11:44:13 +0000440 }
drh3e9aeec2005-08-13 13:39:02 +0000441 if( xtype==etGENERIC && precision>0 ) precision--;
drh5f968432004-02-21 19:02:30 +0000442#if 0
drha18c5682000-10-08 22:20:57 +0000443 /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */
444 for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
445#else
446 /* It makes more sense to use 0.5 */
447 for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1);
448#endif
drh3e9aeec2005-08-13 13:39:02 +0000449 if( xtype==etFLOAT ) realvalue += rounder;
drha18c5682000-10-08 22:20:57 +0000450 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
451 exp = 0;
452 if( realvalue>0.0 ){
drh63782852005-08-30 19:30:59 +0000453 while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
454 while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
455 while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
drhb621c232004-02-21 19:41:04 +0000456 while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; }
457 while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; }
458 if( exp>350 || exp<-350 ){
drha18c5682000-10-08 22:20:57 +0000459 bufpt = "NaN";
460 length = 3;
461 break;
462 }
drh9adf9ac2002-05-15 11:44:13 +0000463 }
drha18c5682000-10-08 22:20:57 +0000464 bufpt = buf;
465 /*
466 ** If the field type is etGENERIC, then convert to either etEXP
467 ** or etFLOAT, as appropriate.
468 */
469 flag_exp = xtype==etEXP;
470 if( xtype!=etFLOAT ){
471 realvalue += rounder;
472 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
473 }
474 if( xtype==etGENERIC ){
475 flag_rtz = !flag_alternateform;
476 if( exp<-4 || exp>precision ){
477 xtype = etEXP;
478 }else{
479 precision = precision - exp;
480 xtype = etFLOAT;
481 }
drh9adf9ac2002-05-15 11:44:13 +0000482 }else{
drha18c5682000-10-08 22:20:57 +0000483 flag_rtz = 0;
drh9adf9ac2002-05-15 11:44:13 +0000484 }
drh557cc602005-08-13 12:59:14 +0000485 if( xtype==etEXP ){
486 e2 = 0;
487 }else{
488 e2 = exp;
489 }
drha18c5682000-10-08 22:20:57 +0000490 nsd = 0;
drh557cc602005-08-13 12:59:14 +0000491 flag_dp = (precision>0) | flag_alternateform | flag_altform2;
492 /* The sign in front of the number */
493 if( prefix ){
494 *(bufpt++) = prefix;
495 }
496 /* Digits prior to the decimal point */
497 if( e2<0 ){
498 *(bufpt++) = '0';
499 }else{
500 for(; e2>=0; e2--){
501 *(bufpt++) = et_getdigit(&realvalue,&nsd);
drha18c5682000-10-08 22:20:57 +0000502 }
drh9adf9ac2002-05-15 11:44:13 +0000503 }
drh557cc602005-08-13 12:59:14 +0000504 /* The decimal point */
505 if( flag_dp ){
506 *(bufpt++) = '.';
507 }
508 /* "0" digits after the decimal point but before the first
509 ** significant digit of the number */
510 for(e2++; e2<0 && precision>0; precision--, e2++){
511 *(bufpt++) = '0';
512 }
513 /* Significant digits after the decimal point */
514 while( (precision--)>0 ){
515 *(bufpt++) = et_getdigit(&realvalue,&nsd);
516 }
517 /* Remove trailing zeros and the "." if no digits follow the "." */
518 if( flag_rtz && flag_dp ){
drh3e9aeec2005-08-13 13:39:02 +0000519 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
520 assert( bufpt>buf );
521 if( bufpt[-1]=='.' ){
drh557cc602005-08-13 12:59:14 +0000522 if( flag_altform2 ){
523 *(bufpt++) = '0';
524 }else{
525 *(--bufpt) = 0;
526 }
527 }
528 }
529 /* Add the "eNNN" suffix */
530 if( flag_exp || (xtype==etEXP && exp) ){
531 *(bufpt++) = aDigits[infop->charset];
532 if( exp<0 ){
533 *(bufpt++) = '-'; exp = -exp;
534 }else{
535 *(bufpt++) = '+';
536 }
537 if( exp>=100 ){
538 *(bufpt++) = (exp/100)+'0'; /* 100's digit */
539 exp %= 100;
540 }
541 *(bufpt++) = exp/10+'0'; /* 10's digit */
542 *(bufpt++) = exp%10+'0'; /* 1's digit */
543 }
544 *bufpt = 0;
545
drha18c5682000-10-08 22:20:57 +0000546 /* The converted number is in buf[] and zero terminated. Output it.
547 ** Note that the number is in the usual order, not reversed as with
548 ** integer conversions. */
drh94ce4c12003-06-07 11:29:50 +0000549 length = bufpt-buf;
drha18c5682000-10-08 22:20:57 +0000550 bufpt = buf;
551
552 /* Special case: Add leading zeros if the flag_zeropad flag is
553 ** set and we are not left justified */
554 if( flag_zeropad && !flag_leftjustify && length < width){
555 int i;
556 int nPad = width - length;
557 for(i=width; i>=nPad; i--){
558 bufpt[i] = bufpt[i-nPad];
559 }
560 i = prefix!=0;
561 while( nPad-- ) bufpt[i++] = '0';
562 length = width;
563 }
564#endif
565 break;
566 case etSIZE:
567 *(va_arg(ap,int*)) = count;
568 length = width = 0;
569 break;
570 case etPERCENT:
571 buf[0] = '%';
572 bufpt = buf;
573 length = 1;
574 break;
575 case etCHARLIT:
576 case etCHARX:
577 c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt);
578 if( precision>=0 ){
579 for(idx=1; idx<precision; idx++) buf[idx] = c;
580 length = precision;
drh9adf9ac2002-05-15 11:44:13 +0000581 }else{
drha18c5682000-10-08 22:20:57 +0000582 length =1;
drh9adf9ac2002-05-15 11:44:13 +0000583 }
drha18c5682000-10-08 22:20:57 +0000584 bufpt = buf;
585 break;
586 case etSTRING:
drhd93d8a82003-06-16 03:08:18 +0000587 case etDYNSTRING:
drhcb485882002-08-15 13:50:48 +0000588 bufpt = va_arg(ap,char*);
drhd93d8a82003-06-16 03:08:18 +0000589 if( bufpt==0 ){
590 bufpt = "";
591 }else if( xtype==etDYNSTRING ){
592 zExtra = bufpt;
593 }
drha18c5682000-10-08 22:20:57 +0000594 length = strlen(bufpt);
595 if( precision>=0 && precision<length ) length = precision;
596 break;
597 case etSQLESCAPE:
drh5eba8c02005-08-19 02:26:27 +0000598 case etSQLESCAPE2: {
599 int i, j, n, c, isnull;
600 int needQuote;
601 char *arg = va_arg(ap,char*);
602 isnull = arg==0;
603 if( isnull ) arg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
604 for(i=n=0; (c=arg[i])!=0; i++){
605 if( c=='\'' ) n++;
drha18c5682000-10-08 22:20:57 +0000606 }
drh5eba8c02005-08-19 02:26:27 +0000607 needQuote = !isnull && xtype==etSQLESCAPE2;
608 n += i + 1 + needQuote*2;
609 if( n>etBUFSIZE ){
610 bufpt = zExtra = sqliteMalloc( n );
611 if( bufpt==0 ) return -1;
612 }else{
613 bufpt = buf;
614 }
615 j = 0;
616 if( needQuote ) bufpt[j++] = '\'';
617 for(i=0; (c=arg[i])!=0; i++){
618 bufpt[j++] = c;
619 if( c=='\'' ) bufpt[j++] = c;
620 }
621 if( needQuote ) bufpt[j++] = '\'';
622 bufpt[j] = 0;
623 length = j;
624 if( precision>=0 && precision<length ) length = precision;
drha18c5682000-10-08 22:20:57 +0000625 break;
drh5eba8c02005-08-19 02:26:27 +0000626 }
drh5f968432004-02-21 19:02:30 +0000627 case etTOKEN: {
628 Token *pToken = va_arg(ap, Token*);
drhad6d9462004-09-19 02:15:24 +0000629 if( pToken && pToken->z ){
630 (*func)(arg, pToken->z, pToken->n);
631 }
drh5f968432004-02-21 19:02:30 +0000632 length = width = 0;
633 break;
634 }
635 case etSRCLIST: {
636 SrcList *pSrc = va_arg(ap, SrcList*);
637 int k = va_arg(ap, int);
638 struct SrcList_item *pItem = &pSrc->a[k];
639 assert( k>=0 && k<pSrc->nSrc );
640 if( pItem->zDatabase && pItem->zDatabase[0] ){
641 (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase));
642 (*func)(arg, ".", 1);
643 }
644 (*func)(arg, pItem->zName, strlen(pItem->zName));
645 length = width = 0;
646 break;
647 }
drha18c5682000-10-08 22:20:57 +0000648 case etERROR:
649 buf[0] = '%';
650 buf[1] = c;
651 errorflag = 0;
652 idx = 1+(c!=0);
653 (*func)(arg,"%",idx);
654 count += idx;
655 if( c==0 ) fmt--;
656 break;
657 }/* End switch over the format type */
658 /*
659 ** The text of the conversion is pointed to by "bufpt" and is
660 ** "length" characters long. The field width is "width". Do
661 ** the output.
662 */
663 if( !flag_leftjustify ){
664 register int nspace;
665 nspace = width-length;
666 if( nspace>0 ){
drha18c5682000-10-08 22:20:57 +0000667 count += nspace;
668 while( nspace>=etSPACESIZE ){
669 (*func)(arg,spaces,etSPACESIZE);
670 nspace -= etSPACESIZE;
671 }
672 if( nspace>0 ) (*func)(arg,spaces,nspace);
673 }
674 }
675 if( length>0 ){
676 (*func)(arg,bufpt,length);
677 count += length;
678 }
679 if( flag_leftjustify ){
680 register int nspace;
681 nspace = width-length;
682 if( nspace>0 ){
683 count += nspace;
684 while( nspace>=etSPACESIZE ){
685 (*func)(arg,spaces,etSPACESIZE);
686 nspace -= etSPACESIZE;
687 }
688 if( nspace>0 ) (*func)(arg,spaces,nspace);
689 }
690 }
691 if( zExtra ){
drh5f968432004-02-21 19:02:30 +0000692 sqliteFree(zExtra);
drha18c5682000-10-08 22:20:57 +0000693 }
694 }/* End for loop over the format string */
695 return errorflag ? -1 : count;
696} /* End of function */
697
698
699/* This structure is used to store state information about the
700** write to memory that is currently in progress.
701*/
702struct sgMprintf {
703 char *zBase; /* A base allocation */
704 char *zText; /* The string collected so far */
705 int nChar; /* Length of the string so far */
drh5f968432004-02-21 19:02:30 +0000706 int nTotal; /* Output size if unconstrained */
drha18c5682000-10-08 22:20:57 +0000707 int nAlloc; /* Amount of space allocated in zText */
drh5f968432004-02-21 19:02:30 +0000708 void *(*xRealloc)(void*,int); /* Function used to realloc memory */
drha18c5682000-10-08 22:20:57 +0000709};
710
711/*
712** This function implements the callback from vxprintf.
713**
714** This routine add nNewChar characters of text in zNewText to
715** the sgMprintf structure pointed to by "arg".
716*/
drh5f968432004-02-21 19:02:30 +0000717static void mout(void *arg, const char *zNewText, int nNewChar){
drha18c5682000-10-08 22:20:57 +0000718 struct sgMprintf *pM = (struct sgMprintf*)arg;
drh5f968432004-02-21 19:02:30 +0000719 pM->nTotal += nNewChar;
drha18c5682000-10-08 22:20:57 +0000720 if( pM->nChar + nNewChar + 1 > pM->nAlloc ){
drh5f968432004-02-21 19:02:30 +0000721 if( pM->xRealloc==0 ){
722 nNewChar = pM->nAlloc - pM->nChar - 1;
drha18c5682000-10-08 22:20:57 +0000723 }else{
drh5f968432004-02-21 19:02:30 +0000724 pM->nAlloc = pM->nChar + nNewChar*2 + 1;
725 if( pM->zText==pM->zBase ){
726 pM->zText = pM->xRealloc(0, pM->nAlloc);
727 if( pM->zText && pM->nChar ){
728 memcpy(pM->zText, pM->zBase, pM->nChar);
729 }
730 }else{
drh53f733c2005-09-16 02:38:09 +0000731 char *zNew;
732 zNew = pM->xRealloc(pM->zText, pM->nAlloc);
733 if( zNew ){
734 pM->zText = zNew;
735 }
drh6d4abfb2001-10-22 02:58:08 +0000736 }
drha18c5682000-10-08 22:20:57 +0000737 }
738 }
drhe29b1a02004-07-17 21:56:09 +0000739 if( pM->zText ){
740 if( nNewChar>0 ){
741 memcpy(&pM->zText[pM->nChar], zNewText, nNewChar);
742 pM->nChar += nNewChar;
743 }
drha18c5682000-10-08 22:20:57 +0000744 pM->zText[pM->nChar] = 0;
745 }
746}
747
748/*
drh5f968432004-02-21 19:02:30 +0000749** This routine is a wrapper around xprintf() that invokes mout() as
750** the consumer.
drha18c5682000-10-08 22:20:57 +0000751*/
drh5f968432004-02-21 19:02:30 +0000752static char *base_vprintf(
753 void *(*xRealloc)(void*,int), /* Routine to realloc memory. May be NULL */
754 int useInternal, /* Use internal %-conversions if true */
755 char *zInitBuf, /* Initially write here, before mallocing */
756 int nInitBuf, /* Size of zInitBuf[] */
757 const char *zFormat, /* format string */
758 va_list ap /* arguments */
759){
760 struct sgMprintf sM;
761 sM.zBase = sM.zText = zInitBuf;
762 sM.nChar = sM.nTotal = 0;
763 sM.nAlloc = nInitBuf;
764 sM.xRealloc = xRealloc;
765 vxprintf(mout, &sM, useInternal, zFormat, ap);
766 if( xRealloc ){
767 if( sM.zText==sM.zBase ){
768 sM.zText = xRealloc(0, sM.nChar+1);
danielk19778def5ea2004-06-16 10:39:52 +0000769 if( sM.zText ){
770 memcpy(sM.zText, sM.zBase, sM.nChar+1);
771 }
drh5f968432004-02-21 19:02:30 +0000772 }else if( sM.nAlloc>sM.nChar+10 ){
drh53f733c2005-09-16 02:38:09 +0000773 char *zNew = xRealloc(sM.zText, sM.nChar+1);
774 if( zNew ){
775 sM.zText = zNew;
776 }
drh5f968432004-02-21 19:02:30 +0000777 }
778 }
779 return sM.zText;
drh483750b2003-01-29 18:46:51 +0000780}
781
782/*
drh5f968432004-02-21 19:02:30 +0000783** Realloc that is a real function, not a macro.
784*/
785static void *printf_realloc(void *old, int size){
786 return sqliteRealloc(old,size);
787}
788
789/*
790** Print into memory obtained from sqliteMalloc(). Use the internal
791** %-conversion extensions.
792*/
danielk19774adee202004-05-08 08:23:19 +0000793char *sqlite3VMPrintf(const char *zFormat, va_list ap){
drh79158e12005-09-06 21:40:45 +0000794 char zBase[SQLITE_PRINT_BUF_SIZE];
drh5f968432004-02-21 19:02:30 +0000795 return base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
796}
797
798/*
799** Print into memory obtained from sqliteMalloc(). Use the internal
800** %-conversion extensions.
801*/
danielk19774adee202004-05-08 08:23:19 +0000802char *sqlite3MPrintf(const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000803 va_list ap;
804 char *z;
drh79158e12005-09-06 21:40:45 +0000805 char zBase[SQLITE_PRINT_BUF_SIZE];
drh5f968432004-02-21 19:02:30 +0000806 va_start(ap, zFormat);
807 z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
808 va_end(ap);
809 return z;
810}
811
812/*
813** Print into memory obtained from malloc(). Do not use the internal
814** %-conversion extensions. This routine is for use by external users.
drh483750b2003-01-29 18:46:51 +0000815*/
danielk19776f8a5032004-05-10 10:34:51 +0000816char *sqlite3_mprintf(const char *zFormat, ...){
drha18c5682000-10-08 22:20:57 +0000817 va_list ap;
drh5f968432004-02-21 19:02:30 +0000818 char *z;
drha18c5682000-10-08 22:20:57 +0000819 char zBuf[200];
820
drha18c5682000-10-08 22:20:57 +0000821 va_start(ap,zFormat);
drh5f968432004-02-21 19:02:30 +0000822 z = base_vprintf((void*(*)(void*,int))realloc, 0,
823 zBuf, sizeof(zBuf), zFormat, ap);
drha18c5682000-10-08 22:20:57 +0000824 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000825 return z;
drha18c5682000-10-08 22:20:57 +0000826}
827
danielk19776f8a5032004-05-10 10:34:51 +0000828/* This is the varargs version of sqlite3_mprintf.
drha18c5682000-10-08 22:20:57 +0000829*/
danielk19776f8a5032004-05-10 10:34:51 +0000830char *sqlite3_vmprintf(const char *zFormat, va_list ap){
drha18c5682000-10-08 22:20:57 +0000831 char zBuf[200];
drh5f968432004-02-21 19:02:30 +0000832 return base_vprintf((void*(*)(void*,int))realloc, 0,
833 zBuf, sizeof(zBuf), zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +0000834}
835
836/*
danielk19776f8a5032004-05-10 10:34:51 +0000837** sqlite3_snprintf() works like snprintf() except that it ignores the
drh93a5c6b2003-12-23 02:17:35 +0000838** current locale settings. This is important for SQLite because we
839** are not able to use a "," as the decimal point in place of "." as
840** specified by some locales.
841*/
danielk19776f8a5032004-05-10 10:34:51 +0000842char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000843 char *z;
drh93a5c6b2003-12-23 02:17:35 +0000844 va_list ap;
drh93a5c6b2003-12-23 02:17:35 +0000845
drh93a5c6b2003-12-23 02:17:35 +0000846 va_start(ap,zFormat);
drh5f968432004-02-21 19:02:30 +0000847 z = base_vprintf(0, 0, zBuf, n, zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +0000848 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000849 return z;
drh93a5c6b2003-12-23 02:17:35 +0000850}
851
drh1b743be2004-06-22 22:04:46 +0000852#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
drhe54ca3f2004-06-07 01:52:14 +0000853/*
854** A version of printf() that understands %lld. Used for debugging.
855** The printf() built into some versions of windows does not understand %lld
856** and segfaults if you give it a long long int.
857*/
858void sqlite3DebugPrintf(const char *zFormat, ...){
drh76ff3a02004-09-24 22:32:30 +0000859 extern int getpid(void);
drhe54ca3f2004-06-07 01:52:14 +0000860 va_list ap;
861 char zBuf[500];
862 va_start(ap, zFormat);
863 base_vprintf(0, 0, zBuf, sizeof(zBuf), zFormat, ap);
864 va_end(ap);
drh59c98a62004-09-24 19:39:26 +0000865 fprintf(stdout,"%d: %s", getpid(), zBuf);
drh2ac3ee92004-06-07 16:27:46 +0000866 fflush(stdout);
drhe54ca3f2004-06-07 01:52:14 +0000867}
868#endif