blob: 7c4b6b007124e585b01c458b8bd690ef4c6f280a [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;
drh43617e92006-03-06 20:55:46 +0000343 if( infop==0 ){
344 return -1;
345 }
346
drha18c5682000-10-08 22:20:57 +0000347
drh4794f732004-11-05 17:17:50 +0000348 /* Limit the precision to prevent overflowing buf[] during conversion */
349 if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
350 precision = etBUFSIZE-40;
351 }
352
drha18c5682000-10-08 22:20:57 +0000353 /*
354 ** At this point, variables are initialized as follows:
355 **
356 ** flag_alternateform TRUE if a '#' is present.
drh3e9aeec2005-08-13 13:39:02 +0000357 ** flag_altform2 TRUE if a '!' is present.
drha18c5682000-10-08 22:20:57 +0000358 ** flag_plussign TRUE if a '+' is present.
359 ** flag_leftjustify TRUE if a '-' is present or if the
360 ** field width was negative.
361 ** flag_zeropad TRUE if the width began with 0.
362 ** flag_long TRUE if the letter 'l' (ell) prefixed
363 ** the conversion character.
drha34b6762004-05-07 13:30:42 +0000364 ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed
365 ** the conversion character.
drha18c5682000-10-08 22:20:57 +0000366 ** flag_blanksign TRUE if a ' ' is present.
367 ** width The specified field width. This is
368 ** always non-negative. Zero is the default.
369 ** precision The specified precision. The default
370 ** is -1.
371 ** xtype The class of the conversion.
372 ** infop Pointer to the appropriate info struct.
373 */
374 switch( xtype ){
drhfe63d1c2004-09-08 20:13:04 +0000375 case etPOINTER:
376 flag_longlong = sizeof(char*)==sizeof(i64);
377 flag_long = sizeof(char*)==sizeof(long int);
378 /* Fall through into the next case */
drha18c5682000-10-08 22:20:57 +0000379 case etRADIX:
drhe84a3062004-02-02 12:29:25 +0000380 if( infop->flags & FLAG_SIGNED ){
drhe9707672004-06-25 01:10:48 +0000381 i64 v;
382 if( flag_longlong ) v = va_arg(ap,i64);
383 else if( flag_long ) v = va_arg(ap,long int);
384 else v = va_arg(ap,int);
385 if( v<0 ){
386 longvalue = -v;
387 prefix = '-';
danielk1977cfcdaef2004-05-12 07:33:33 +0000388 }else{
drhe9707672004-06-25 01:10:48 +0000389 longvalue = v;
390 if( flag_plussign ) prefix = '+';
danielk1977cfcdaef2004-05-12 07:33:33 +0000391 else if( flag_blanksign ) prefix = ' ';
392 else prefix = 0;
393 }
drhe9707672004-06-25 01:10:48 +0000394 }else{
395 if( flag_longlong ) longvalue = va_arg(ap,u64);
396 else if( flag_long ) longvalue = va_arg(ap,unsigned long int);
397 else longvalue = va_arg(ap,unsigned int);
398 prefix = 0;
399 }
400 if( longvalue==0 ) flag_alternateform = 0;
drha18c5682000-10-08 22:20:57 +0000401 if( flag_zeropad && precision<width-(prefix!=0) ){
402 precision = width-(prefix!=0);
drh9adf9ac2002-05-15 11:44:13 +0000403 }
drh3039c0a2004-02-29 00:11:30 +0000404 bufpt = &buf[etBUFSIZE-1];
drha18c5682000-10-08 22:20:57 +0000405 {
drh76ff3a02004-09-24 22:32:30 +0000406 register const char *cset; /* Use registers for speed */
drha18c5682000-10-08 22:20:57 +0000407 register int base;
drh76ff3a02004-09-24 22:32:30 +0000408 cset = &aDigits[infop->charset];
drha18c5682000-10-08 22:20:57 +0000409 base = infop->base;
410 do{ /* Convert to ascii */
411 *(--bufpt) = cset[longvalue%base];
412 longvalue = longvalue/base;
413 }while( longvalue>0 );
drh9adf9ac2002-05-15 11:44:13 +0000414 }
drh3039c0a2004-02-29 00:11:30 +0000415 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000416 for(idx=precision-length; idx>0; idx--){
417 *(--bufpt) = '0'; /* Zero pad */
drh9adf9ac2002-05-15 11:44:13 +0000418 }
drha18c5682000-10-08 22:20:57 +0000419 if( prefix ) *(--bufpt) = prefix; /* Add sign */
420 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
drh76ff3a02004-09-24 22:32:30 +0000421 const char *pre;
422 char x;
423 pre = &aPrefix[infop->prefix];
drha18c5682000-10-08 22:20:57 +0000424 if( *bufpt!=pre[0] ){
drh76ff3a02004-09-24 22:32:30 +0000425 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
drh9adf9ac2002-05-15 11:44:13 +0000426 }
drha18c5682000-10-08 22:20:57 +0000427 }
drh3039c0a2004-02-29 00:11:30 +0000428 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000429 break;
430 case etFLOAT:
431 case etEXP:
432 case etGENERIC:
433 realvalue = va_arg(ap,double);
drhb37df7b2005-10-13 02:09:49 +0000434#ifndef SQLITE_OMIT_FLOATING_POINT
drha18c5682000-10-08 22:20:57 +0000435 if( precision<0 ) precision = 6; /* Set default precision */
drh5eba8c02005-08-19 02:26:27 +0000436 if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10;
drha18c5682000-10-08 22:20:57 +0000437 if( realvalue<0.0 ){
438 realvalue = -realvalue;
439 prefix = '-';
drh9adf9ac2002-05-15 11:44:13 +0000440 }else{
drha18c5682000-10-08 22:20:57 +0000441 if( flag_plussign ) prefix = '+';
442 else if( flag_blanksign ) prefix = ' ';
443 else prefix = 0;
drh9adf9ac2002-05-15 11:44:13 +0000444 }
drh3e9aeec2005-08-13 13:39:02 +0000445 if( xtype==etGENERIC && precision>0 ) precision--;
drh5f968432004-02-21 19:02:30 +0000446#if 0
drha18c5682000-10-08 22:20:57 +0000447 /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */
448 for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
449#else
450 /* It makes more sense to use 0.5 */
drh74161702006-02-24 02:53:49 +0000451 for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
drha18c5682000-10-08 22:20:57 +0000452#endif
drh3e9aeec2005-08-13 13:39:02 +0000453 if( xtype==etFLOAT ) realvalue += rounder;
drha18c5682000-10-08 22:20:57 +0000454 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
455 exp = 0;
456 if( realvalue>0.0 ){
drh63782852005-08-30 19:30:59 +0000457 while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
458 while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
459 while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
drhb621c232004-02-21 19:41:04 +0000460 while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; }
461 while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; }
462 if( exp>350 || exp<-350 ){
drha18c5682000-10-08 22:20:57 +0000463 bufpt = "NaN";
464 length = 3;
465 break;
466 }
drh9adf9ac2002-05-15 11:44:13 +0000467 }
drha18c5682000-10-08 22:20:57 +0000468 bufpt = buf;
469 /*
470 ** If the field type is etGENERIC, then convert to either etEXP
471 ** or etFLOAT, as appropriate.
472 */
473 flag_exp = xtype==etEXP;
474 if( xtype!=etFLOAT ){
475 realvalue += rounder;
476 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
477 }
478 if( xtype==etGENERIC ){
479 flag_rtz = !flag_alternateform;
480 if( exp<-4 || exp>precision ){
481 xtype = etEXP;
482 }else{
483 precision = precision - exp;
484 xtype = etFLOAT;
485 }
drh9adf9ac2002-05-15 11:44:13 +0000486 }else{
drha18c5682000-10-08 22:20:57 +0000487 flag_rtz = 0;
drh9adf9ac2002-05-15 11:44:13 +0000488 }
drh557cc602005-08-13 12:59:14 +0000489 if( xtype==etEXP ){
490 e2 = 0;
491 }else{
492 e2 = exp;
493 }
drha18c5682000-10-08 22:20:57 +0000494 nsd = 0;
drh557cc602005-08-13 12:59:14 +0000495 flag_dp = (precision>0) | flag_alternateform | flag_altform2;
496 /* The sign in front of the number */
497 if( prefix ){
498 *(bufpt++) = prefix;
499 }
500 /* Digits prior to the decimal point */
501 if( e2<0 ){
502 *(bufpt++) = '0';
503 }else{
504 for(; e2>=0; e2--){
505 *(bufpt++) = et_getdigit(&realvalue,&nsd);
drha18c5682000-10-08 22:20:57 +0000506 }
drh9adf9ac2002-05-15 11:44:13 +0000507 }
drh557cc602005-08-13 12:59:14 +0000508 /* The decimal point */
509 if( flag_dp ){
510 *(bufpt++) = '.';
511 }
512 /* "0" digits after the decimal point but before the first
513 ** significant digit of the number */
514 for(e2++; e2<0 && precision>0; precision--, e2++){
515 *(bufpt++) = '0';
516 }
517 /* Significant digits after the decimal point */
518 while( (precision--)>0 ){
519 *(bufpt++) = et_getdigit(&realvalue,&nsd);
520 }
521 /* Remove trailing zeros and the "." if no digits follow the "." */
522 if( flag_rtz && flag_dp ){
drh3e9aeec2005-08-13 13:39:02 +0000523 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
524 assert( bufpt>buf );
525 if( bufpt[-1]=='.' ){
drh557cc602005-08-13 12:59:14 +0000526 if( flag_altform2 ){
527 *(bufpt++) = '0';
528 }else{
529 *(--bufpt) = 0;
530 }
531 }
532 }
533 /* Add the "eNNN" suffix */
534 if( flag_exp || (xtype==etEXP && exp) ){
535 *(bufpt++) = aDigits[infop->charset];
536 if( exp<0 ){
537 *(bufpt++) = '-'; exp = -exp;
538 }else{
539 *(bufpt++) = '+';
540 }
541 if( exp>=100 ){
542 *(bufpt++) = (exp/100)+'0'; /* 100's digit */
543 exp %= 100;
544 }
545 *(bufpt++) = exp/10+'0'; /* 10's digit */
546 *(bufpt++) = exp%10+'0'; /* 1's digit */
547 }
548 *bufpt = 0;
549
drha18c5682000-10-08 22:20:57 +0000550 /* The converted number is in buf[] and zero terminated. Output it.
551 ** Note that the number is in the usual order, not reversed as with
552 ** integer conversions. */
drh94ce4c12003-06-07 11:29:50 +0000553 length = bufpt-buf;
drha18c5682000-10-08 22:20:57 +0000554 bufpt = buf;
555
556 /* Special case: Add leading zeros if the flag_zeropad flag is
557 ** set and we are not left justified */
558 if( flag_zeropad && !flag_leftjustify && length < width){
559 int i;
560 int nPad = width - length;
561 for(i=width; i>=nPad; i--){
562 bufpt[i] = bufpt[i-nPad];
563 }
564 i = prefix!=0;
565 while( nPad-- ) bufpt[i++] = '0';
566 length = width;
567 }
568#endif
569 break;
570 case etSIZE:
571 *(va_arg(ap,int*)) = count;
572 length = width = 0;
573 break;
574 case etPERCENT:
575 buf[0] = '%';
576 bufpt = buf;
577 length = 1;
578 break;
579 case etCHARLIT:
580 case etCHARX:
581 c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt);
582 if( precision>=0 ){
583 for(idx=1; idx<precision; idx++) buf[idx] = c;
584 length = precision;
drh9adf9ac2002-05-15 11:44:13 +0000585 }else{
drha18c5682000-10-08 22:20:57 +0000586 length =1;
drh9adf9ac2002-05-15 11:44:13 +0000587 }
drha18c5682000-10-08 22:20:57 +0000588 bufpt = buf;
589 break;
590 case etSTRING:
drhd93d8a82003-06-16 03:08:18 +0000591 case etDYNSTRING:
drhcb485882002-08-15 13:50:48 +0000592 bufpt = va_arg(ap,char*);
drhd93d8a82003-06-16 03:08:18 +0000593 if( bufpt==0 ){
594 bufpt = "";
595 }else if( xtype==etDYNSTRING ){
596 zExtra = bufpt;
597 }
drha18c5682000-10-08 22:20:57 +0000598 length = strlen(bufpt);
599 if( precision>=0 && precision<length ) length = precision;
600 break;
601 case etSQLESCAPE:
drh5eba8c02005-08-19 02:26:27 +0000602 case etSQLESCAPE2: {
danielk1977f0113002006-01-24 12:09:17 +0000603 int i, j, n, ch, isnull;
drh5eba8c02005-08-19 02:26:27 +0000604 int needQuote;
danielk1977f0113002006-01-24 12:09:17 +0000605 char *escarg = va_arg(ap,char*);
606 isnull = escarg==0;
607 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
608 for(i=n=0; (ch=escarg[i])!=0; i++){
609 if( ch=='\'' ) n++;
drha18c5682000-10-08 22:20:57 +0000610 }
drh5eba8c02005-08-19 02:26:27 +0000611 needQuote = !isnull && xtype==etSQLESCAPE2;
612 n += i + 1 + needQuote*2;
613 if( n>etBUFSIZE ){
614 bufpt = zExtra = sqliteMalloc( n );
615 if( bufpt==0 ) return -1;
616 }else{
617 bufpt = buf;
618 }
619 j = 0;
620 if( needQuote ) bufpt[j++] = '\'';
danielk1977f0113002006-01-24 12:09:17 +0000621 for(i=0; (ch=escarg[i])!=0; i++){
622 bufpt[j++] = ch;
623 if( ch=='\'' ) bufpt[j++] = ch;
drh5eba8c02005-08-19 02:26:27 +0000624 }
625 if( needQuote ) bufpt[j++] = '\'';
626 bufpt[j] = 0;
627 length = j;
628 if( precision>=0 && precision<length ) length = precision;
drha18c5682000-10-08 22:20:57 +0000629 break;
drh5eba8c02005-08-19 02:26:27 +0000630 }
drh5f968432004-02-21 19:02:30 +0000631 case etTOKEN: {
632 Token *pToken = va_arg(ap, Token*);
drhad6d9462004-09-19 02:15:24 +0000633 if( pToken && pToken->z ){
drh2646da72005-12-09 20:02:05 +0000634 (*func)(arg, (char*)pToken->z, pToken->n);
drhad6d9462004-09-19 02:15:24 +0000635 }
drh5f968432004-02-21 19:02:30 +0000636 length = width = 0;
637 break;
638 }
639 case etSRCLIST: {
640 SrcList *pSrc = va_arg(ap, SrcList*);
641 int k = va_arg(ap, int);
642 struct SrcList_item *pItem = &pSrc->a[k];
643 assert( k>=0 && k<pSrc->nSrc );
644 if( pItem->zDatabase && pItem->zDatabase[0] ){
645 (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase));
646 (*func)(arg, ".", 1);
647 }
648 (*func)(arg, pItem->zName, strlen(pItem->zName));
649 length = width = 0;
650 break;
651 }
drha18c5682000-10-08 22:20:57 +0000652 case etERROR:
653 buf[0] = '%';
654 buf[1] = c;
655 errorflag = 0;
656 idx = 1+(c!=0);
657 (*func)(arg,"%",idx);
658 count += idx;
659 if( c==0 ) fmt--;
660 break;
661 }/* End switch over the format type */
662 /*
663 ** The text of the conversion is pointed to by "bufpt" and is
664 ** "length" characters long. The field width is "width". Do
665 ** the output.
666 */
667 if( !flag_leftjustify ){
668 register int nspace;
669 nspace = width-length;
670 if( nspace>0 ){
drha18c5682000-10-08 22:20:57 +0000671 count += nspace;
672 while( nspace>=etSPACESIZE ){
673 (*func)(arg,spaces,etSPACESIZE);
674 nspace -= etSPACESIZE;
675 }
676 if( nspace>0 ) (*func)(arg,spaces,nspace);
677 }
678 }
679 if( length>0 ){
680 (*func)(arg,bufpt,length);
681 count += length;
682 }
683 if( flag_leftjustify ){
684 register int nspace;
685 nspace = width-length;
686 if( nspace>0 ){
687 count += nspace;
688 while( nspace>=etSPACESIZE ){
689 (*func)(arg,spaces,etSPACESIZE);
690 nspace -= etSPACESIZE;
691 }
692 if( nspace>0 ) (*func)(arg,spaces,nspace);
693 }
694 }
695 if( zExtra ){
drh5f968432004-02-21 19:02:30 +0000696 sqliteFree(zExtra);
drha18c5682000-10-08 22:20:57 +0000697 }
698 }/* End for loop over the format string */
699 return errorflag ? -1 : count;
700} /* End of function */
701
702
703/* This structure is used to store state information about the
704** write to memory that is currently in progress.
705*/
706struct sgMprintf {
707 char *zBase; /* A base allocation */
708 char *zText; /* The string collected so far */
709 int nChar; /* Length of the string so far */
drh5f968432004-02-21 19:02:30 +0000710 int nTotal; /* Output size if unconstrained */
drha18c5682000-10-08 22:20:57 +0000711 int nAlloc; /* Amount of space allocated in zText */
drh5f968432004-02-21 19:02:30 +0000712 void *(*xRealloc)(void*,int); /* Function used to realloc memory */
drha18c5682000-10-08 22:20:57 +0000713};
714
715/*
716** This function implements the callback from vxprintf.
717**
718** This routine add nNewChar characters of text in zNewText to
719** the sgMprintf structure pointed to by "arg".
720*/
drh5f968432004-02-21 19:02:30 +0000721static void mout(void *arg, const char *zNewText, int nNewChar){
drha18c5682000-10-08 22:20:57 +0000722 struct sgMprintf *pM = (struct sgMprintf*)arg;
drh5f968432004-02-21 19:02:30 +0000723 pM->nTotal += nNewChar;
drha18c5682000-10-08 22:20:57 +0000724 if( pM->nChar + nNewChar + 1 > pM->nAlloc ){
drh5f968432004-02-21 19:02:30 +0000725 if( pM->xRealloc==0 ){
726 nNewChar = pM->nAlloc - pM->nChar - 1;
drha18c5682000-10-08 22:20:57 +0000727 }else{
drh5f968432004-02-21 19:02:30 +0000728 pM->nAlloc = pM->nChar + nNewChar*2 + 1;
729 if( pM->zText==pM->zBase ){
730 pM->zText = pM->xRealloc(0, pM->nAlloc);
731 if( pM->zText && pM->nChar ){
732 memcpy(pM->zText, pM->zBase, pM->nChar);
733 }
734 }else{
drh53f733c2005-09-16 02:38:09 +0000735 char *zNew;
736 zNew = pM->xRealloc(pM->zText, pM->nAlloc);
737 if( zNew ){
738 pM->zText = zNew;
739 }
drh6d4abfb2001-10-22 02:58:08 +0000740 }
drha18c5682000-10-08 22:20:57 +0000741 }
742 }
drhe29b1a02004-07-17 21:56:09 +0000743 if( pM->zText ){
744 if( nNewChar>0 ){
745 memcpy(&pM->zText[pM->nChar], zNewText, nNewChar);
746 pM->nChar += nNewChar;
747 }
drha18c5682000-10-08 22:20:57 +0000748 pM->zText[pM->nChar] = 0;
749 }
750}
751
752/*
drh5f968432004-02-21 19:02:30 +0000753** This routine is a wrapper around xprintf() that invokes mout() as
754** the consumer.
drha18c5682000-10-08 22:20:57 +0000755*/
drh5f968432004-02-21 19:02:30 +0000756static char *base_vprintf(
757 void *(*xRealloc)(void*,int), /* Routine to realloc memory. May be NULL */
758 int useInternal, /* Use internal %-conversions if true */
759 char *zInitBuf, /* Initially write here, before mallocing */
760 int nInitBuf, /* Size of zInitBuf[] */
761 const char *zFormat, /* format string */
762 va_list ap /* arguments */
763){
764 struct sgMprintf sM;
765 sM.zBase = sM.zText = zInitBuf;
766 sM.nChar = sM.nTotal = 0;
767 sM.nAlloc = nInitBuf;
768 sM.xRealloc = xRealloc;
769 vxprintf(mout, &sM, useInternal, zFormat, ap);
770 if( xRealloc ){
771 if( sM.zText==sM.zBase ){
772 sM.zText = xRealloc(0, sM.nChar+1);
danielk19778def5ea2004-06-16 10:39:52 +0000773 if( sM.zText ){
774 memcpy(sM.zText, sM.zBase, sM.nChar+1);
775 }
drh5f968432004-02-21 19:02:30 +0000776 }else if( sM.nAlloc>sM.nChar+10 ){
drh53f733c2005-09-16 02:38:09 +0000777 char *zNew = xRealloc(sM.zText, sM.nChar+1);
778 if( zNew ){
779 sM.zText = zNew;
780 }
drh5f968432004-02-21 19:02:30 +0000781 }
782 }
783 return sM.zText;
drh483750b2003-01-29 18:46:51 +0000784}
785
786/*
drh5f968432004-02-21 19:02:30 +0000787** Realloc that is a real function, not a macro.
788*/
789static void *printf_realloc(void *old, int size){
790 return sqliteRealloc(old,size);
791}
792
793/*
794** Print into memory obtained from sqliteMalloc(). Use the internal
795** %-conversion extensions.
796*/
danielk19774adee202004-05-08 08:23:19 +0000797char *sqlite3VMPrintf(const char *zFormat, va_list ap){
drh79158e12005-09-06 21:40:45 +0000798 char zBase[SQLITE_PRINT_BUF_SIZE];
drh5f968432004-02-21 19:02:30 +0000799 return base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
800}
801
802/*
803** Print into memory obtained from sqliteMalloc(). Use the internal
804** %-conversion extensions.
805*/
danielk19774adee202004-05-08 08:23:19 +0000806char *sqlite3MPrintf(const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000807 va_list ap;
808 char *z;
drh79158e12005-09-06 21:40:45 +0000809 char zBase[SQLITE_PRINT_BUF_SIZE];
drh5f968432004-02-21 19:02:30 +0000810 va_start(ap, zFormat);
811 z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
812 va_end(ap);
813 return z;
814}
815
816/*
817** Print into memory obtained from malloc(). Do not use the internal
818** %-conversion extensions. This routine is for use by external users.
drh483750b2003-01-29 18:46:51 +0000819*/
danielk19776f8a5032004-05-10 10:34:51 +0000820char *sqlite3_mprintf(const char *zFormat, ...){
drha18c5682000-10-08 22:20:57 +0000821 va_list ap;
drh5f968432004-02-21 19:02:30 +0000822 char *z;
drha18c5682000-10-08 22:20:57 +0000823 char zBuf[200];
824
drha18c5682000-10-08 22:20:57 +0000825 va_start(ap,zFormat);
drh5f968432004-02-21 19:02:30 +0000826 z = base_vprintf((void*(*)(void*,int))realloc, 0,
827 zBuf, sizeof(zBuf), zFormat, ap);
drha18c5682000-10-08 22:20:57 +0000828 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000829 return z;
drha18c5682000-10-08 22:20:57 +0000830}
831
danielk19776f8a5032004-05-10 10:34:51 +0000832/* This is the varargs version of sqlite3_mprintf.
drha18c5682000-10-08 22:20:57 +0000833*/
danielk19776f8a5032004-05-10 10:34:51 +0000834char *sqlite3_vmprintf(const char *zFormat, va_list ap){
drha18c5682000-10-08 22:20:57 +0000835 char zBuf[200];
drh5f968432004-02-21 19:02:30 +0000836 return base_vprintf((void*(*)(void*,int))realloc, 0,
837 zBuf, sizeof(zBuf), zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +0000838}
839
840/*
danielk19776f8a5032004-05-10 10:34:51 +0000841** sqlite3_snprintf() works like snprintf() except that it ignores the
drh93a5c6b2003-12-23 02:17:35 +0000842** current locale settings. This is important for SQLite because we
843** are not able to use a "," as the decimal point in place of "." as
844** specified by some locales.
845*/
danielk19776f8a5032004-05-10 10:34:51 +0000846char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000847 char *z;
drh93a5c6b2003-12-23 02:17:35 +0000848 va_list ap;
drh93a5c6b2003-12-23 02:17:35 +0000849
drh93a5c6b2003-12-23 02:17:35 +0000850 va_start(ap,zFormat);
drh5f968432004-02-21 19:02:30 +0000851 z = base_vprintf(0, 0, zBuf, n, zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +0000852 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000853 return z;
drh93a5c6b2003-12-23 02:17:35 +0000854}
855
drh1b743be2004-06-22 22:04:46 +0000856#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
drhe54ca3f2004-06-07 01:52:14 +0000857/*
858** A version of printf() that understands %lld. Used for debugging.
859** The printf() built into some versions of windows does not understand %lld
860** and segfaults if you give it a long long int.
861*/
862void sqlite3DebugPrintf(const char *zFormat, ...){
drh76ff3a02004-09-24 22:32:30 +0000863 extern int getpid(void);
drhe54ca3f2004-06-07 01:52:14 +0000864 va_list ap;
865 char zBuf[500];
866 va_start(ap, zFormat);
867 base_vprintf(0, 0, zBuf, sizeof(zBuf), zFormat, ap);
868 va_end(ap);
drh59c98a62004-09-24 19:39:26 +0000869 fprintf(stdout,"%d: %s", getpid(), zBuf);
drh2ac3ee92004-06-07 16:27:46 +0000870 fflush(stdout);
drhe54ca3f2004-06-07 01:52:14 +0000871}
872#endif