blob: 37390a5f00cf3f6d86b8cdee4f619bc5688bc1f3 [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"
drh53c14022007-05-10 17:23:11 +000054#include <math.h>
drh7c68d602000-10-11 19:28:51 +000055
drha18c5682000-10-08 22:20:57 +000056/*
drha18c5682000-10-08 22:20:57 +000057** Conversion types fall into various categories as defined by the
58** following enumeration.
59*/
drhe84a3062004-02-02 12:29:25 +000060#define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */
61#define etFLOAT 2 /* Floating point. %f */
62#define etEXP 3 /* Exponentional notation. %e and %E */
63#define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */
64#define etSIZE 5 /* Return number of characters processed so far. %n */
65#define etSTRING 6 /* Strings. %s */
66#define etDYNSTRING 7 /* Dynamically allocated strings. %z */
67#define etPERCENT 8 /* Percent symbol. %% */
68#define etCHARX 9 /* Characters. %c */
drha18c5682000-10-08 22:20:57 +000069/* The rest are extensions, not normally found in printf() */
drh05a82982006-03-19 13:00:25 +000070#define etCHARLIT 10 /* Literal characters. %' */
71#define etSQLESCAPE 11 /* Strings with '\'' doubled. %q */
72#define etSQLESCAPE2 12 /* Strings with '\'' doubled and enclosed in '',
chw0cfcf3f2002-06-16 04:55:48 +000073 NULL pointers replaced by SQL NULL. %Q */
drh05a82982006-03-19 13:00:25 +000074#define etTOKEN 13 /* a pointer to a Token structure */
75#define etSRCLIST 14 /* a pointer to a SrcList */
76#define etPOINTER 15 /* The %p conversion */
danielk1977f3b863e2007-06-24 06:32:17 +000077#define etSQLESCAPE3 16 /* %w -> Strings with '\"' doubled */
drhe84a3062004-02-02 12:29:25 +000078
79
80/*
81** An "etByte" is an 8-bit unsigned value.
82*/
83typedef unsigned char etByte;
drha18c5682000-10-08 22:20:57 +000084
85/*
86** Each builtin conversion character (ex: the 'd' in "%d") is described
87** by an instance of the following structure
88*/
89typedef struct et_info { /* Information about each format field */
drhe84a3062004-02-02 12:29:25 +000090 char fmttype; /* The format field code letter */
91 etByte base; /* The base for radix conversion */
92 etByte flags; /* One or more of FLAG_ constants below */
93 etByte type; /* Conversion paradigm */
drh76ff3a02004-09-24 22:32:30 +000094 etByte charset; /* Offset into aDigits[] of the digits string */
95 etByte prefix; /* Offset into aPrefix[] of the prefix string */
drha18c5682000-10-08 22:20:57 +000096} et_info;
97
98/*
drhe84a3062004-02-02 12:29:25 +000099** Allowed values for et_info.flags
100*/
101#define FLAG_SIGNED 1 /* True if the value to convert is signed */
102#define FLAG_INTERN 2 /* True if for internal use only */
drh4794f732004-11-05 17:17:50 +0000103#define FLAG_STRING 4 /* Allow infinity precision */
drhe84a3062004-02-02 12:29:25 +0000104
105
106/*
drha18c5682000-10-08 22:20:57 +0000107** The following table is searched linearly, so it is good to put the
108** most frequently used conversion types first.
109*/
drh76ff3a02004-09-24 22:32:30 +0000110static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
111static const char aPrefix[] = "-x0\000X0";
drh57196282004-10-06 15:41:16 +0000112static const et_info fmtinfo[] = {
drh76ff3a02004-09-24 22:32:30 +0000113 { 'd', 10, 1, etRADIX, 0, 0 },
drh4794f732004-11-05 17:17:50 +0000114 { 's', 0, 4, etSTRING, 0, 0 },
drh557cc602005-08-13 12:59:14 +0000115 { 'g', 0, 1, etGENERIC, 30, 0 },
drh153c62c2007-08-24 03:51:33 +0000116 { 'z', 0, 4, etDYNSTRING, 0, 0 },
drh4794f732004-11-05 17:17:50 +0000117 { 'q', 0, 4, etSQLESCAPE, 0, 0 },
118 { 'Q', 0, 4, etSQLESCAPE2, 0, 0 },
danielk1977f3b863e2007-06-24 06:32:17 +0000119 { 'w', 0, 4, etSQLESCAPE3, 0, 0 },
drh76ff3a02004-09-24 22:32:30 +0000120 { 'c', 0, 0, etCHARX, 0, 0 },
121 { 'o', 8, 0, etRADIX, 0, 2 },
122 { 'u', 10, 0, etRADIX, 0, 0 },
123 { 'x', 16, 0, etRADIX, 16, 1 },
124 { 'X', 16, 0, etRADIX, 0, 4 },
drhb37df7b2005-10-13 02:09:49 +0000125#ifndef SQLITE_OMIT_FLOATING_POINT
drh76ff3a02004-09-24 22:32:30 +0000126 { 'f', 0, 1, etFLOAT, 0, 0 },
127 { 'e', 0, 1, etEXP, 30, 0 },
128 { 'E', 0, 1, etEXP, 14, 0 },
drh76ff3a02004-09-24 22:32:30 +0000129 { 'G', 0, 1, etGENERIC, 14, 0 },
drhb37df7b2005-10-13 02:09:49 +0000130#endif
drh76ff3a02004-09-24 22:32:30 +0000131 { 'i', 10, 1, etRADIX, 0, 0 },
132 { 'n', 0, 0, etSIZE, 0, 0 },
133 { '%', 0, 0, etPERCENT, 0, 0 },
134 { 'p', 16, 0, etPOINTER, 0, 1 },
135 { 'T', 0, 2, etTOKEN, 0, 0 },
136 { 'S', 0, 2, etSRCLIST, 0, 0 },
drha18c5682000-10-08 22:20:57 +0000137};
138#define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0]))
139
140/*
drhb37df7b2005-10-13 02:09:49 +0000141** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
drha18c5682000-10-08 22:20:57 +0000142** conversions will work.
143*/
drhb37df7b2005-10-13 02:09:49 +0000144#ifndef SQLITE_OMIT_FLOATING_POINT
drha18c5682000-10-08 22:20:57 +0000145/*
146** "*val" is a double such that 0.1 <= *val < 10.0
147** Return the ascii code for the leading digit of *val, then
148** multiply "*val" by 10.0 to renormalize.
149**
150** Example:
151** input: *val = 3.14159
152** output: *val = 1.4159 function return = '3'
153**
154** The counter *cnt is incremented each time. After counter exceeds
155** 16 (the number of significant digits in a 64-bit float) '0' is
156** always returned.
157*/
drh384eef32004-01-07 03:04:27 +0000158static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
drha18c5682000-10-08 22:20:57 +0000159 int digit;
drh384eef32004-01-07 03:04:27 +0000160 LONGDOUBLE_TYPE d;
drha18c5682000-10-08 22:20:57 +0000161 if( (*cnt)++ >= 16 ) return '0';
162 digit = (int)*val;
163 d = digit;
164 digit += '0';
165 *val = (*val - d)*10.0;
166 return digit;
167}
drhb37df7b2005-10-13 02:09:49 +0000168#endif /* SQLITE_OMIT_FLOATING_POINT */
drha18c5682000-10-08 22:20:57 +0000169
drh79158e12005-09-06 21:40:45 +0000170/*
171** On machines with a small stack size, you can redefine the
172** SQLITE_PRINT_BUF_SIZE to be less than 350. But beware - for
173** smaller values some %f conversions may go into an infinite loop.
174*/
175#ifndef SQLITE_PRINT_BUF_SIZE
176# define SQLITE_PRINT_BUF_SIZE 350
177#endif
178#define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
drha18c5682000-10-08 22:20:57 +0000179
180/*
181** The root program. All variations call this core.
182**
183** INPUTS:
184** func This is a pointer to a function taking three arguments
185** 1. A pointer to anything. Same as the "arg" parameter.
186** 2. A pointer to the list of characters to be output
187** (Note, this list is NOT null terminated.)
188** 3. An integer number of characters to be output.
189** (Note: This number might be zero.)
190**
191** arg This is the pointer to anything which will be passed as the
192** first argument to "func". Use it for whatever you like.
193**
194** fmt This is the format string, as in the usual print.
195**
196** ap This is a pointer to a list of arguments. Same as in
197** vfprint.
198**
199** OUTPUTS:
200** The return value is the total number of characters sent to
201** the function "func". Returns -1 on a error.
202**
203** Note that the order in which automatic variables are declared below
204** seems to make a big difference in determining how fast this beast
205** will run.
206*/
207static int vxprintf(
drh5f968432004-02-21 19:02:30 +0000208 void (*func)(void*,const char*,int), /* Consumer of text */
209 void *arg, /* First argument to the consumer */
210 int useExtended, /* Allow extended %-conversions */
211 const char *fmt, /* Format string */
212 va_list ap /* arguments */
drha18c5682000-10-08 22:20:57 +0000213){
drhe84a3062004-02-02 12:29:25 +0000214 int c; /* Next character in the format string */
215 char *bufpt; /* Pointer to the conversion buffer */
216 int precision; /* Precision of the current field */
217 int length; /* Length of the field */
218 int idx; /* A general purpose loop counter */
219 int count; /* Total number of characters output */
220 int width; /* Width of the current field */
221 etByte flag_leftjustify; /* True if "-" flag is present */
222 etByte flag_plussign; /* True if "+" flag is present */
223 etByte flag_blanksign; /* True if " " flag is present */
224 etByte flag_alternateform; /* True if "#" flag is present */
drh531fe872005-08-13 13:40:42 +0000225 etByte flag_altform2; /* True if "!" flag is present */
drhe84a3062004-02-02 12:29:25 +0000226 etByte flag_zeropad; /* True if field width constant starts with zero */
227 etByte flag_long; /* True if "l" flag is present */
drha34b6762004-05-07 13:30:42 +0000228 etByte flag_longlong; /* True if the "ll" flag is present */
drh3e9aeec2005-08-13 13:39:02 +0000229 etByte done; /* Loop termination flag */
drh27436af2006-03-28 23:57:17 +0000230 sqlite_uint64 longvalue; /* Value for integer types */
drh384eef32004-01-07 03:04:27 +0000231 LONGDOUBLE_TYPE realvalue; /* Value for real types */
drh57196282004-10-06 15:41:16 +0000232 const et_info *infop; /* Pointer to the appropriate info structure */
drhe84a3062004-02-02 12:29:25 +0000233 char buf[etBUFSIZE]; /* Conversion buffer */
234 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
235 etByte errorflag = 0; /* True if an error is encountered */
236 etByte xtype; /* Conversion paradigm */
237 char *zExtra; /* Extra memory used for etTCLESCAPE conversions */
drh57196282004-10-06 15:41:16 +0000238 static const char spaces[] =
239 " ";
drha18c5682000-10-08 22:20:57 +0000240#define etSPACESIZE (sizeof(spaces)-1)
drhb37df7b2005-10-13 02:09:49 +0000241#ifndef SQLITE_OMIT_FLOATING_POINT
drh557cc602005-08-13 12:59:14 +0000242 int exp, e2; /* exponent of real numbers */
drhe84a3062004-02-02 12:29:25 +0000243 double rounder; /* Used for rounding floating point values */
244 etByte flag_dp; /* True if decimal point should be shown */
245 etByte flag_rtz; /* True if trailing zeros should be removed */
246 etByte flag_exp; /* True to force display of the exponent */
247 int nsd; /* Number of significant digits returned */
drha18c5682000-10-08 22:20:57 +0000248#endif
249
drhe29b1a02004-07-17 21:56:09 +0000250 func(arg,"",0);
drha18c5682000-10-08 22:20:57 +0000251 count = length = 0;
252 bufpt = 0;
253 for(; (c=(*fmt))!=0; ++fmt){
254 if( c!='%' ){
drhe84a3062004-02-02 12:29:25 +0000255 int amt;
drha18c5682000-10-08 22:20:57 +0000256 bufpt = (char *)fmt;
257 amt = 1;
258 while( (c=(*++fmt))!='%' && c!=0 ) amt++;
259 (*func)(arg,bufpt,amt);
260 count += amt;
261 if( c==0 ) break;
262 }
263 if( (c=(*++fmt))==0 ){
264 errorflag = 1;
265 (*func)(arg,"%",1);
266 count++;
267 break;
268 }
269 /* Find out what flags are present */
270 flag_leftjustify = flag_plussign = flag_blanksign =
drh557cc602005-08-13 12:59:14 +0000271 flag_alternateform = flag_altform2 = flag_zeropad = 0;
drh3e9aeec2005-08-13 13:39:02 +0000272 done = 0;
drha18c5682000-10-08 22:20:57 +0000273 do{
274 switch( c ){
drh3e9aeec2005-08-13 13:39:02 +0000275 case '-': flag_leftjustify = 1; break;
276 case '+': flag_plussign = 1; break;
277 case ' ': flag_blanksign = 1; break;
278 case '#': flag_alternateform = 1; break;
279 case '!': flag_altform2 = 1; break;
280 case '0': flag_zeropad = 1; break;
281 default: done = 1; break;
drha18c5682000-10-08 22:20:57 +0000282 }
drh3e9aeec2005-08-13 13:39:02 +0000283 }while( !done && (c=(*++fmt))!=0 );
drha18c5682000-10-08 22:20:57 +0000284 /* Get the field width */
285 width = 0;
286 if( c=='*' ){
287 width = va_arg(ap,int);
288 if( width<0 ){
289 flag_leftjustify = 1;
290 width = -width;
291 }
292 c = *++fmt;
293 }else{
drh17a68932001-01-31 13:28:08 +0000294 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000295 width = width*10 + c - '0';
296 c = *++fmt;
297 }
298 }
299 if( width > etBUFSIZE-10 ){
300 width = etBUFSIZE-10;
301 }
302 /* Get the precision */
303 if( c=='.' ){
304 precision = 0;
305 c = *++fmt;
306 if( c=='*' ){
307 precision = va_arg(ap,int);
drha18c5682000-10-08 22:20:57 +0000308 if( precision<0 ) precision = -precision;
drha18c5682000-10-08 22:20:57 +0000309 c = *++fmt;
310 }else{
drh17a68932001-01-31 13:28:08 +0000311 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000312 precision = precision*10 + c - '0';
313 c = *++fmt;
314 }
315 }
drha18c5682000-10-08 22:20:57 +0000316 }else{
317 precision = -1;
318 }
319 /* Get the conversion type modifier */
320 if( c=='l' ){
321 flag_long = 1;
322 c = *++fmt;
drha34b6762004-05-07 13:30:42 +0000323 if( c=='l' ){
324 flag_longlong = 1;
325 c = *++fmt;
326 }else{
327 flag_longlong = 0;
328 }
drha18c5682000-10-08 22:20:57 +0000329 }else{
drha34b6762004-05-07 13:30:42 +0000330 flag_long = flag_longlong = 0;
drha18c5682000-10-08 22:20:57 +0000331 }
332 /* Fetch the info entry for the field */
333 infop = 0;
334 for(idx=0; idx<etNINFO; idx++){
335 if( c==fmtinfo[idx].fmttype ){
336 infop = &fmtinfo[idx];
drh5f968432004-02-21 19:02:30 +0000337 if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
338 xtype = infop->type;
drhc4413562006-05-22 22:04:00 +0000339 }else{
340 return -1;
drh5f968432004-02-21 19:02:30 +0000341 }
drha18c5682000-10-08 22:20:57 +0000342 break;
343 }
344 }
drha18c5682000-10-08 22:20:57 +0000345 zExtra = 0;
drh43617e92006-03-06 20:55:46 +0000346 if( infop==0 ){
347 return -1;
348 }
349
drha18c5682000-10-08 22:20:57 +0000350
drh4794f732004-11-05 17:17:50 +0000351 /* Limit the precision to prevent overflowing buf[] during conversion */
352 if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
353 precision = etBUFSIZE-40;
354 }
355
drha18c5682000-10-08 22:20:57 +0000356 /*
357 ** At this point, variables are initialized as follows:
358 **
359 ** flag_alternateform TRUE if a '#' is present.
drh3e9aeec2005-08-13 13:39:02 +0000360 ** flag_altform2 TRUE if a '!' is present.
drha18c5682000-10-08 22:20:57 +0000361 ** flag_plussign TRUE if a '+' is present.
362 ** flag_leftjustify TRUE if a '-' is present or if the
363 ** field width was negative.
364 ** flag_zeropad TRUE if the width began with 0.
365 ** flag_long TRUE if the letter 'l' (ell) prefixed
366 ** the conversion character.
drha34b6762004-05-07 13:30:42 +0000367 ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed
368 ** the conversion character.
drha18c5682000-10-08 22:20:57 +0000369 ** flag_blanksign TRUE if a ' ' is present.
370 ** width The specified field width. This is
371 ** always non-negative. Zero is the default.
372 ** precision The specified precision. The default
373 ** is -1.
374 ** xtype The class of the conversion.
375 ** infop Pointer to the appropriate info struct.
376 */
377 switch( xtype ){
drhfe63d1c2004-09-08 20:13:04 +0000378 case etPOINTER:
379 flag_longlong = sizeof(char*)==sizeof(i64);
380 flag_long = sizeof(char*)==sizeof(long int);
381 /* Fall through into the next case */
drha18c5682000-10-08 22:20:57 +0000382 case etRADIX:
drhe84a3062004-02-02 12:29:25 +0000383 if( infop->flags & FLAG_SIGNED ){
drhe9707672004-06-25 01:10:48 +0000384 i64 v;
385 if( flag_longlong ) v = va_arg(ap,i64);
386 else if( flag_long ) v = va_arg(ap,long int);
387 else v = va_arg(ap,int);
388 if( v<0 ){
389 longvalue = -v;
390 prefix = '-';
danielk1977cfcdaef2004-05-12 07:33:33 +0000391 }else{
drhe9707672004-06-25 01:10:48 +0000392 longvalue = v;
393 if( flag_plussign ) prefix = '+';
danielk1977cfcdaef2004-05-12 07:33:33 +0000394 else if( flag_blanksign ) prefix = ' ';
395 else prefix = 0;
396 }
drhe9707672004-06-25 01:10:48 +0000397 }else{
398 if( flag_longlong ) longvalue = va_arg(ap,u64);
399 else if( flag_long ) longvalue = va_arg(ap,unsigned long int);
400 else longvalue = va_arg(ap,unsigned int);
401 prefix = 0;
402 }
403 if( longvalue==0 ) flag_alternateform = 0;
drha18c5682000-10-08 22:20:57 +0000404 if( flag_zeropad && precision<width-(prefix!=0) ){
405 precision = width-(prefix!=0);
drh9adf9ac2002-05-15 11:44:13 +0000406 }
drh3039c0a2004-02-29 00:11:30 +0000407 bufpt = &buf[etBUFSIZE-1];
drha18c5682000-10-08 22:20:57 +0000408 {
drh76ff3a02004-09-24 22:32:30 +0000409 register const char *cset; /* Use registers for speed */
drha18c5682000-10-08 22:20:57 +0000410 register int base;
drh76ff3a02004-09-24 22:32:30 +0000411 cset = &aDigits[infop->charset];
drha18c5682000-10-08 22:20:57 +0000412 base = infop->base;
413 do{ /* Convert to ascii */
414 *(--bufpt) = cset[longvalue%base];
415 longvalue = longvalue/base;
416 }while( longvalue>0 );
drh9adf9ac2002-05-15 11:44:13 +0000417 }
drh3039c0a2004-02-29 00:11:30 +0000418 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000419 for(idx=precision-length; idx>0; idx--){
420 *(--bufpt) = '0'; /* Zero pad */
drh9adf9ac2002-05-15 11:44:13 +0000421 }
drha18c5682000-10-08 22:20:57 +0000422 if( prefix ) *(--bufpt) = prefix; /* Add sign */
423 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
drh76ff3a02004-09-24 22:32:30 +0000424 const char *pre;
425 char x;
426 pre = &aPrefix[infop->prefix];
drha18c5682000-10-08 22:20:57 +0000427 if( *bufpt!=pre[0] ){
drh76ff3a02004-09-24 22:32:30 +0000428 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
drh9adf9ac2002-05-15 11:44:13 +0000429 }
drha18c5682000-10-08 22:20:57 +0000430 }
drh3039c0a2004-02-29 00:11:30 +0000431 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000432 break;
433 case etFLOAT:
434 case etEXP:
435 case etGENERIC:
436 realvalue = va_arg(ap,double);
drhb37df7b2005-10-13 02:09:49 +0000437#ifndef SQLITE_OMIT_FLOATING_POINT
drha18c5682000-10-08 22:20:57 +0000438 if( precision<0 ) precision = 6; /* Set default precision */
drh5eba8c02005-08-19 02:26:27 +0000439 if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10;
drha18c5682000-10-08 22:20:57 +0000440 if( realvalue<0.0 ){
441 realvalue = -realvalue;
442 prefix = '-';
drh9adf9ac2002-05-15 11:44:13 +0000443 }else{
drha18c5682000-10-08 22:20:57 +0000444 if( flag_plussign ) prefix = '+';
445 else if( flag_blanksign ) prefix = ' ';
446 else prefix = 0;
drh9adf9ac2002-05-15 11:44:13 +0000447 }
drh3e9aeec2005-08-13 13:39:02 +0000448 if( xtype==etGENERIC && precision>0 ) precision--;
drh5f968432004-02-21 19:02:30 +0000449#if 0
drha18c5682000-10-08 22:20:57 +0000450 /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */
451 for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
452#else
453 /* It makes more sense to use 0.5 */
drh74161702006-02-24 02:53:49 +0000454 for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
drha18c5682000-10-08 22:20:57 +0000455#endif
drh3e9aeec2005-08-13 13:39:02 +0000456 if( xtype==etFLOAT ) realvalue += rounder;
drha18c5682000-10-08 22:20:57 +0000457 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
458 exp = 0;
drh7cd69272007-06-20 15:29:25 +0000459 if( sqlite3_isnan(realvalue) ){
drh53c14022007-05-10 17:23:11 +0000460 bufpt = "NaN";
461 length = 3;
462 break;
463 }
drha18c5682000-10-08 22:20:57 +0000464 if( realvalue>0.0 ){
drh63782852005-08-30 19:30:59 +0000465 while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
466 while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
467 while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
drhb621c232004-02-21 19:41:04 +0000468 while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; }
469 while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; }
470 if( exp>350 || exp<-350 ){
drh53c14022007-05-10 17:23:11 +0000471 if( prefix=='-' ){
472 bufpt = "-Inf";
473 }else if( prefix=='+' ){
474 bufpt = "+Inf";
475 }else{
476 bufpt = "Inf";
477 }
478 length = strlen(bufpt);
drha18c5682000-10-08 22:20:57 +0000479 break;
480 }
drh9adf9ac2002-05-15 11:44:13 +0000481 }
drha18c5682000-10-08 22:20:57 +0000482 bufpt = buf;
483 /*
484 ** If the field type is etGENERIC, then convert to either etEXP
485 ** or etFLOAT, as appropriate.
486 */
487 flag_exp = xtype==etEXP;
488 if( xtype!=etFLOAT ){
489 realvalue += rounder;
490 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
491 }
492 if( xtype==etGENERIC ){
493 flag_rtz = !flag_alternateform;
494 if( exp<-4 || exp>precision ){
495 xtype = etEXP;
496 }else{
497 precision = precision - exp;
498 xtype = etFLOAT;
499 }
drh9adf9ac2002-05-15 11:44:13 +0000500 }else{
drha18c5682000-10-08 22:20:57 +0000501 flag_rtz = 0;
drh9adf9ac2002-05-15 11:44:13 +0000502 }
drh557cc602005-08-13 12:59:14 +0000503 if( xtype==etEXP ){
504 e2 = 0;
505 }else{
506 e2 = exp;
507 }
drha18c5682000-10-08 22:20:57 +0000508 nsd = 0;
drh557cc602005-08-13 12:59:14 +0000509 flag_dp = (precision>0) | flag_alternateform | flag_altform2;
510 /* The sign in front of the number */
511 if( prefix ){
512 *(bufpt++) = prefix;
513 }
514 /* Digits prior to the decimal point */
515 if( e2<0 ){
516 *(bufpt++) = '0';
517 }else{
518 for(; e2>=0; e2--){
519 *(bufpt++) = et_getdigit(&realvalue,&nsd);
drha18c5682000-10-08 22:20:57 +0000520 }
drh9adf9ac2002-05-15 11:44:13 +0000521 }
drh557cc602005-08-13 12:59:14 +0000522 /* The decimal point */
523 if( flag_dp ){
524 *(bufpt++) = '.';
525 }
526 /* "0" digits after the decimal point but before the first
527 ** significant digit of the number */
528 for(e2++; e2<0 && precision>0; precision--, e2++){
529 *(bufpt++) = '0';
530 }
531 /* Significant digits after the decimal point */
532 while( (precision--)>0 ){
533 *(bufpt++) = et_getdigit(&realvalue,&nsd);
534 }
535 /* Remove trailing zeros and the "." if no digits follow the "." */
536 if( flag_rtz && flag_dp ){
drh3e9aeec2005-08-13 13:39:02 +0000537 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
538 assert( bufpt>buf );
539 if( bufpt[-1]=='.' ){
drh557cc602005-08-13 12:59:14 +0000540 if( flag_altform2 ){
541 *(bufpt++) = '0';
542 }else{
543 *(--bufpt) = 0;
544 }
545 }
546 }
547 /* Add the "eNNN" suffix */
548 if( flag_exp || (xtype==etEXP && exp) ){
549 *(bufpt++) = aDigits[infop->charset];
550 if( exp<0 ){
551 *(bufpt++) = '-'; exp = -exp;
552 }else{
553 *(bufpt++) = '+';
554 }
555 if( exp>=100 ){
556 *(bufpt++) = (exp/100)+'0'; /* 100's digit */
557 exp %= 100;
558 }
559 *(bufpt++) = exp/10+'0'; /* 10's digit */
560 *(bufpt++) = exp%10+'0'; /* 1's digit */
561 }
562 *bufpt = 0;
563
drha18c5682000-10-08 22:20:57 +0000564 /* The converted number is in buf[] and zero terminated. Output it.
565 ** Note that the number is in the usual order, not reversed as with
566 ** integer conversions. */
drh94ce4c12003-06-07 11:29:50 +0000567 length = bufpt-buf;
drha18c5682000-10-08 22:20:57 +0000568 bufpt = buf;
569
570 /* Special case: Add leading zeros if the flag_zeropad flag is
571 ** set and we are not left justified */
572 if( flag_zeropad && !flag_leftjustify && length < width){
573 int i;
574 int nPad = width - length;
575 for(i=width; i>=nPad; i--){
576 bufpt[i] = bufpt[i-nPad];
577 }
578 i = prefix!=0;
579 while( nPad-- ) bufpt[i++] = '0';
580 length = width;
581 }
582#endif
583 break;
584 case etSIZE:
585 *(va_arg(ap,int*)) = count;
586 length = width = 0;
587 break;
588 case etPERCENT:
589 buf[0] = '%';
590 bufpt = buf;
591 length = 1;
592 break;
593 case etCHARLIT:
594 case etCHARX:
595 c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt);
596 if( precision>=0 ){
597 for(idx=1; idx<precision; idx++) buf[idx] = c;
598 length = precision;
drh9adf9ac2002-05-15 11:44:13 +0000599 }else{
drha18c5682000-10-08 22:20:57 +0000600 length =1;
drh9adf9ac2002-05-15 11:44:13 +0000601 }
drha18c5682000-10-08 22:20:57 +0000602 bufpt = buf;
603 break;
604 case etSTRING:
drhd93d8a82003-06-16 03:08:18 +0000605 case etDYNSTRING:
drhcb485882002-08-15 13:50:48 +0000606 bufpt = va_arg(ap,char*);
drhd93d8a82003-06-16 03:08:18 +0000607 if( bufpt==0 ){
608 bufpt = "";
609 }else if( xtype==etDYNSTRING ){
610 zExtra = bufpt;
611 }
drha18c5682000-10-08 22:20:57 +0000612 length = strlen(bufpt);
613 if( precision>=0 && precision<length ) length = precision;
614 break;
615 case etSQLESCAPE:
danielk1977f3b863e2007-06-24 06:32:17 +0000616 case etSQLESCAPE2:
617 case etSQLESCAPE3: {
danielk1977f0113002006-01-24 12:09:17 +0000618 int i, j, n, ch, isnull;
drh5eba8c02005-08-19 02:26:27 +0000619 int needQuote;
danielk1977f3b863e2007-06-24 06:32:17 +0000620 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
danielk1977f0113002006-01-24 12:09:17 +0000621 char *escarg = va_arg(ap,char*);
622 isnull = escarg==0;
623 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
624 for(i=n=0; (ch=escarg[i])!=0; i++){
danielk1977f3b863e2007-06-24 06:32:17 +0000625 if( ch==q ) n++;
drha18c5682000-10-08 22:20:57 +0000626 }
drh5eba8c02005-08-19 02:26:27 +0000627 needQuote = !isnull && xtype==etSQLESCAPE2;
628 n += i + 1 + needQuote*2;
629 if( n>etBUFSIZE ){
drh17435752007-08-16 04:30:38 +0000630 bufpt = zExtra = sqlite3_malloc( n );
drh5eba8c02005-08-19 02:26:27 +0000631 if( bufpt==0 ) return -1;
632 }else{
633 bufpt = buf;
634 }
635 j = 0;
danielk1977f3b863e2007-06-24 06:32:17 +0000636 if( needQuote ) bufpt[j++] = q;
danielk1977f0113002006-01-24 12:09:17 +0000637 for(i=0; (ch=escarg[i])!=0; i++){
638 bufpt[j++] = ch;
danielk1977f3b863e2007-06-24 06:32:17 +0000639 if( ch==q ) bufpt[j++] = ch;
drh5eba8c02005-08-19 02:26:27 +0000640 }
danielk1977f3b863e2007-06-24 06:32:17 +0000641 if( needQuote ) bufpt[j++] = q;
drh5eba8c02005-08-19 02:26:27 +0000642 bufpt[j] = 0;
643 length = j;
drh05a82982006-03-19 13:00:25 +0000644 /* The precision is ignored on %q and %Q */
645 /* if( precision>=0 && precision<length ) length = precision; */
drha18c5682000-10-08 22:20:57 +0000646 break;
drh5eba8c02005-08-19 02:26:27 +0000647 }
drh5f968432004-02-21 19:02:30 +0000648 case etTOKEN: {
649 Token *pToken = va_arg(ap, Token*);
drhad6d9462004-09-19 02:15:24 +0000650 if( pToken && pToken->z ){
drh2646da72005-12-09 20:02:05 +0000651 (*func)(arg, (char*)pToken->z, pToken->n);
drhad6d9462004-09-19 02:15:24 +0000652 }
drh5f968432004-02-21 19:02:30 +0000653 length = width = 0;
654 break;
655 }
656 case etSRCLIST: {
657 SrcList *pSrc = va_arg(ap, SrcList*);
658 int k = va_arg(ap, int);
659 struct SrcList_item *pItem = &pSrc->a[k];
660 assert( k>=0 && k<pSrc->nSrc );
661 if( pItem->zDatabase && pItem->zDatabase[0] ){
662 (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase));
663 (*func)(arg, ".", 1);
664 }
665 (*func)(arg, pItem->zName, strlen(pItem->zName));
666 length = width = 0;
667 break;
668 }
drha18c5682000-10-08 22:20:57 +0000669 }/* End switch over the format type */
670 /*
671 ** The text of the conversion is pointed to by "bufpt" and is
672 ** "length" characters long. The field width is "width". Do
673 ** the output.
674 */
675 if( !flag_leftjustify ){
676 register int nspace;
677 nspace = width-length;
678 if( nspace>0 ){
drha18c5682000-10-08 22:20:57 +0000679 count += nspace;
680 while( nspace>=etSPACESIZE ){
681 (*func)(arg,spaces,etSPACESIZE);
682 nspace -= etSPACESIZE;
683 }
684 if( nspace>0 ) (*func)(arg,spaces,nspace);
685 }
686 }
687 if( length>0 ){
688 (*func)(arg,bufpt,length);
689 count += length;
690 }
691 if( flag_leftjustify ){
692 register int nspace;
693 nspace = width-length;
694 if( nspace>0 ){
695 count += nspace;
696 while( nspace>=etSPACESIZE ){
697 (*func)(arg,spaces,etSPACESIZE);
698 nspace -= etSPACESIZE;
699 }
700 if( nspace>0 ) (*func)(arg,spaces,nspace);
701 }
702 }
703 if( zExtra ){
danielk19771e536952007-08-16 10:09:01 +0000704 sqlite3_free(zExtra);
drha18c5682000-10-08 22:20:57 +0000705 }
706 }/* End for loop over the format string */
707 return errorflag ? -1 : count;
708} /* End of function */
709
710
711/* This structure is used to store state information about the
712** write to memory that is currently in progress.
713*/
714struct sgMprintf {
715 char *zBase; /* A base allocation */
716 char *zText; /* The string collected so far */
717 int nChar; /* Length of the string so far */
drh5f968432004-02-21 19:02:30 +0000718 int nTotal; /* Output size if unconstrained */
drha18c5682000-10-08 22:20:57 +0000719 int nAlloc; /* Amount of space allocated in zText */
drhf3a65f72007-08-22 20:18:21 +0000720 void *(*xRealloc)(void*,int); /* Function used to realloc memory */
drha18c5682000-10-08 22:20:57 +0000721};
722
723/*
724** This function implements the callback from vxprintf.
725**
726** This routine add nNewChar characters of text in zNewText to
727** the sgMprintf structure pointed to by "arg".
728*/
drh5f968432004-02-21 19:02:30 +0000729static void mout(void *arg, const char *zNewText, int nNewChar){
drha18c5682000-10-08 22:20:57 +0000730 struct sgMprintf *pM = (struct sgMprintf*)arg;
drh5f968432004-02-21 19:02:30 +0000731 pM->nTotal += nNewChar;
drhe29b1a02004-07-17 21:56:09 +0000732 if( pM->zText ){
drh17435752007-08-16 04:30:38 +0000733 if( pM->nChar + nNewChar + 1 > pM->nAlloc ){
734 if( pM->xRealloc==0 ){
735 nNewChar = pM->nAlloc - pM->nChar - 1;
736 }else{
737 int nAlloc = pM->nChar + nNewChar*2 + 1;
738 if( pM->zText==pM->zBase ){
739 pM->zText = pM->xRealloc(0, nAlloc);
740 if( pM->zText==0 ){
741 return;
742 }else if( pM->nChar ){
743 memcpy(pM->zText, pM->zBase, pM->nChar);
744 }
745 }else{
746 char *zNew;
747 zNew = pM->xRealloc(pM->zText, nAlloc);
748 if( zNew ){
749 pM->zText = zNew;
750 }else{
751 pM->xRealloc(pM->zText, 0);
752 pM->zText = 0;
753 return;
754 }
755 }
756 pM->nAlloc = nAlloc;
757 }
758 }
drhe29b1a02004-07-17 21:56:09 +0000759 if( nNewChar>0 ){
760 memcpy(&pM->zText[pM->nChar], zNewText, nNewChar);
761 pM->nChar += nNewChar;
762 }
drha18c5682000-10-08 22:20:57 +0000763 pM->zText[pM->nChar] = 0;
764 }
765}
766
767/*
drh5f968432004-02-21 19:02:30 +0000768** This routine is a wrapper around xprintf() that invokes mout() as
769** the consumer.
drha18c5682000-10-08 22:20:57 +0000770*/
drh5f968432004-02-21 19:02:30 +0000771static char *base_vprintf(
drhf3a65f72007-08-22 20:18:21 +0000772 void *(*xRealloc)(void*, int), /* realloc() function. May be NULL */
drh5f968432004-02-21 19:02:30 +0000773 int useInternal, /* Use internal %-conversions if true */
774 char *zInitBuf, /* Initially write here, before mallocing */
775 int nInitBuf, /* Size of zInitBuf[] */
776 const char *zFormat, /* format string */
777 va_list ap /* arguments */
778){
779 struct sgMprintf sM;
780 sM.zBase = sM.zText = zInitBuf;
781 sM.nChar = sM.nTotal = 0;
782 sM.nAlloc = nInitBuf;
783 sM.xRealloc = xRealloc;
784 vxprintf(mout, &sM, useInternal, zFormat, ap);
785 if( xRealloc ){
786 if( sM.zText==sM.zBase ){
787 sM.zText = xRealloc(0, sM.nChar+1);
danielk19778def5ea2004-06-16 10:39:52 +0000788 if( sM.zText ){
789 memcpy(sM.zText, sM.zBase, sM.nChar+1);
790 }
drh5f968432004-02-21 19:02:30 +0000791 }else if( sM.nAlloc>sM.nChar+10 ){
drh53f733c2005-09-16 02:38:09 +0000792 char *zNew = xRealloc(sM.zText, sM.nChar+1);
793 if( zNew ){
794 sM.zText = zNew;
795 }
drh5f968432004-02-21 19:02:30 +0000796 }
797 }
798 return sM.zText;
drh483750b2003-01-29 18:46:51 +0000799}
800
801/*
drh5f968432004-02-21 19:02:30 +0000802** Realloc that is a real function, not a macro.
803*/
drhf3a65f72007-08-22 20:18:21 +0000804static void *printf_realloc(void *old, int size){
danielk19771e536952007-08-16 10:09:01 +0000805 return sqlite3_realloc(old, size);
drh5f968432004-02-21 19:02:30 +0000806}
807
808/*
809** Print into memory obtained from sqliteMalloc(). Use the internal
810** %-conversion extensions.
811*/
drh17435752007-08-16 04:30:38 +0000812char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
813 char *z;
drh79158e12005-09-06 21:40:45 +0000814 char zBase[SQLITE_PRINT_BUF_SIZE];
drh17435752007-08-16 04:30:38 +0000815 z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
816 if( z==0 && db!=0 ){
817 db->mallocFailed = 1;
818 }
819 return z;
drh5f968432004-02-21 19:02:30 +0000820}
821
822/*
823** Print into memory obtained from sqliteMalloc(). Use the internal
824** %-conversion extensions.
825*/
drh17435752007-08-16 04:30:38 +0000826char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000827 va_list ap;
828 char *z;
drh79158e12005-09-06 21:40:45 +0000829 char zBase[SQLITE_PRINT_BUF_SIZE];
drh5f968432004-02-21 19:02:30 +0000830 va_start(ap, zFormat);
831 z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
832 va_end(ap);
drh17435752007-08-16 04:30:38 +0000833 if( z==0 && db!=0 ){
834 db->mallocFailed = 1;
835 }
drh5f968432004-02-21 19:02:30 +0000836 return z;
837}
838
839/*
drh28dd4792006-06-26 21:35:44 +0000840** Print into memory obtained from sqlite3_malloc(). Omit the internal
841** %-conversion extensions.
842*/
843char *sqlite3_vmprintf(const char *zFormat, va_list ap){
844 char zBase[SQLITE_PRINT_BUF_SIZE];
845 return base_vprintf(sqlite3_realloc, 0, zBase, sizeof(zBase), zFormat, ap);
846}
847
848/*
849** Print into memory obtained from sqlite3_malloc()(). Omit the internal
850** %-conversion extensions.
drh483750b2003-01-29 18:46:51 +0000851*/
danielk19776f8a5032004-05-10 10:34:51 +0000852char *sqlite3_mprintf(const char *zFormat, ...){
drha18c5682000-10-08 22:20:57 +0000853 va_list ap;
drh5f968432004-02-21 19:02:30 +0000854 char *z;
drh28dd4792006-06-26 21:35:44 +0000855 va_start(ap, zFormat);
drhb3738b62007-03-31 15:02:49 +0000856 z = sqlite3_vmprintf(zFormat, ap);
drha18c5682000-10-08 22:20:57 +0000857 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000858 return z;
drha18c5682000-10-08 22:20:57 +0000859}
860
drh93a5c6b2003-12-23 02:17:35 +0000861/*
danielk19776f8a5032004-05-10 10:34:51 +0000862** sqlite3_snprintf() works like snprintf() except that it ignores the
drh93a5c6b2003-12-23 02:17:35 +0000863** current locale settings. This is important for SQLite because we
864** are not able to use a "," as the decimal point in place of "." as
865** specified by some locales.
866*/
danielk19776f8a5032004-05-10 10:34:51 +0000867char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000868 char *z;
drh93a5c6b2003-12-23 02:17:35 +0000869 va_list ap;
drh93a5c6b2003-12-23 02:17:35 +0000870
drh68853902007-05-07 11:24:30 +0000871 if( n<=0 ){
872 return zBuf;
873 }
874 zBuf[0] = 0;
drh93a5c6b2003-12-23 02:17:35 +0000875 va_start(ap,zFormat);
drh5f968432004-02-21 19:02:30 +0000876 z = base_vprintf(0, 0, zBuf, n, zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +0000877 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000878 return z;
drh93a5c6b2003-12-23 02:17:35 +0000879}
880
drh7d7f17b2007-06-15 20:29:20 +0000881#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) || defined(SQLITE_MEMDEBUG)
drhe54ca3f2004-06-07 01:52:14 +0000882/*
883** A version of printf() that understands %lld. Used for debugging.
884** The printf() built into some versions of windows does not understand %lld
885** and segfaults if you give it a long long int.
886*/
887void sqlite3DebugPrintf(const char *zFormat, ...){
drh76ff3a02004-09-24 22:32:30 +0000888 extern int getpid(void);
drhe54ca3f2004-06-07 01:52:14 +0000889 va_list ap;
890 char zBuf[500];
891 va_start(ap, zFormat);
892 base_vprintf(0, 0, zBuf, sizeof(zBuf), zFormat, ap);
893 va_end(ap);
drh485f0032007-01-26 19:23:33 +0000894 fprintf(stdout,"%s", zBuf);
drh2ac3ee92004-06-07 16:27:46 +0000895 fflush(stdout);
drhe54ca3f2004-06-07 01:52:14 +0000896}
897#endif