blob: e910650fe70428ec30973559bfdd6a20603a4bce [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 */
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 */
drh27436af2006-03-28 23:57:17 +0000228 sqlite_uint64 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;
332 for(idx=0; idx<etNINFO; idx++){
333 if( c==fmtinfo[idx].fmttype ){
334 infop = &fmtinfo[idx];
drh5f968432004-02-21 19:02:30 +0000335 if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
336 xtype = infop->type;
drhc4413562006-05-22 22:04:00 +0000337 }else{
338 return -1;
drh5f968432004-02-21 19:02:30 +0000339 }
drha18c5682000-10-08 22:20:57 +0000340 break;
341 }
342 }
drha18c5682000-10-08 22:20:57 +0000343 zExtra = 0;
drh43617e92006-03-06 20:55:46 +0000344 if( infop==0 ){
345 return -1;
346 }
347
drha18c5682000-10-08 22:20:57 +0000348
drh4794f732004-11-05 17:17:50 +0000349 /* Limit the precision to prevent overflowing buf[] during conversion */
350 if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
351 precision = etBUFSIZE-40;
352 }
353
drha18c5682000-10-08 22:20:57 +0000354 /*
355 ** At this point, variables are initialized as follows:
356 **
357 ** flag_alternateform TRUE if a '#' is present.
drh3e9aeec2005-08-13 13:39:02 +0000358 ** flag_altform2 TRUE if a '!' is present.
drha18c5682000-10-08 22:20:57 +0000359 ** flag_plussign TRUE if a '+' is present.
360 ** flag_leftjustify TRUE if a '-' is present or if the
361 ** field width was negative.
362 ** flag_zeropad TRUE if the width began with 0.
363 ** flag_long TRUE if the letter 'l' (ell) prefixed
364 ** the conversion character.
drha34b6762004-05-07 13:30:42 +0000365 ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed
366 ** the conversion character.
drha18c5682000-10-08 22:20:57 +0000367 ** flag_blanksign TRUE if a ' ' is present.
368 ** width The specified field width. This is
369 ** always non-negative. Zero is the default.
370 ** precision The specified precision. The default
371 ** is -1.
372 ** xtype The class of the conversion.
373 ** infop Pointer to the appropriate info struct.
374 */
375 switch( xtype ){
drhfe63d1c2004-09-08 20:13:04 +0000376 case etPOINTER:
377 flag_longlong = sizeof(char*)==sizeof(i64);
378 flag_long = sizeof(char*)==sizeof(long int);
379 /* Fall through into the next case */
drha18c5682000-10-08 22:20:57 +0000380 case etRADIX:
drhe84a3062004-02-02 12:29:25 +0000381 if( infop->flags & FLAG_SIGNED ){
drhe9707672004-06-25 01:10:48 +0000382 i64 v;
383 if( flag_longlong ) v = va_arg(ap,i64);
384 else if( flag_long ) v = va_arg(ap,long int);
385 else v = va_arg(ap,int);
386 if( v<0 ){
387 longvalue = -v;
388 prefix = '-';
danielk1977cfcdaef2004-05-12 07:33:33 +0000389 }else{
drhe9707672004-06-25 01:10:48 +0000390 longvalue = v;
391 if( flag_plussign ) prefix = '+';
danielk1977cfcdaef2004-05-12 07:33:33 +0000392 else if( flag_blanksign ) prefix = ' ';
393 else prefix = 0;
394 }
drhe9707672004-06-25 01:10:48 +0000395 }else{
396 if( flag_longlong ) longvalue = va_arg(ap,u64);
397 else if( flag_long ) longvalue = va_arg(ap,unsigned long int);
398 else longvalue = va_arg(ap,unsigned int);
399 prefix = 0;
400 }
401 if( longvalue==0 ) flag_alternateform = 0;
drha18c5682000-10-08 22:20:57 +0000402 if( flag_zeropad && precision<width-(prefix!=0) ){
403 precision = width-(prefix!=0);
drh9adf9ac2002-05-15 11:44:13 +0000404 }
drh3039c0a2004-02-29 00:11:30 +0000405 bufpt = &buf[etBUFSIZE-1];
drha18c5682000-10-08 22:20:57 +0000406 {
drh76ff3a02004-09-24 22:32:30 +0000407 register const char *cset; /* Use registers for speed */
drha18c5682000-10-08 22:20:57 +0000408 register int base;
drh76ff3a02004-09-24 22:32:30 +0000409 cset = &aDigits[infop->charset];
drha18c5682000-10-08 22:20:57 +0000410 base = infop->base;
411 do{ /* Convert to ascii */
412 *(--bufpt) = cset[longvalue%base];
413 longvalue = longvalue/base;
414 }while( longvalue>0 );
drh9adf9ac2002-05-15 11:44:13 +0000415 }
drh3039c0a2004-02-29 00:11:30 +0000416 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000417 for(idx=precision-length; idx>0; idx--){
418 *(--bufpt) = '0'; /* Zero pad */
drh9adf9ac2002-05-15 11:44:13 +0000419 }
drha18c5682000-10-08 22:20:57 +0000420 if( prefix ) *(--bufpt) = prefix; /* Add sign */
421 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
drh76ff3a02004-09-24 22:32:30 +0000422 const char *pre;
423 char x;
424 pre = &aPrefix[infop->prefix];
drha18c5682000-10-08 22:20:57 +0000425 if( *bufpt!=pre[0] ){
drh76ff3a02004-09-24 22:32:30 +0000426 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
drh9adf9ac2002-05-15 11:44:13 +0000427 }
drha18c5682000-10-08 22:20:57 +0000428 }
drh3039c0a2004-02-29 00:11:30 +0000429 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000430 break;
431 case etFLOAT:
432 case etEXP:
433 case etGENERIC:
434 realvalue = va_arg(ap,double);
drhb37df7b2005-10-13 02:09:49 +0000435#ifndef SQLITE_OMIT_FLOATING_POINT
drha18c5682000-10-08 22:20:57 +0000436 if( precision<0 ) precision = 6; /* Set default precision */
drh5eba8c02005-08-19 02:26:27 +0000437 if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10;
drha18c5682000-10-08 22:20:57 +0000438 if( realvalue<0.0 ){
439 realvalue = -realvalue;
440 prefix = '-';
drh9adf9ac2002-05-15 11:44:13 +0000441 }else{
drha18c5682000-10-08 22:20:57 +0000442 if( flag_plussign ) prefix = '+';
443 else if( flag_blanksign ) prefix = ' ';
444 else prefix = 0;
drh9adf9ac2002-05-15 11:44:13 +0000445 }
drh3e9aeec2005-08-13 13:39:02 +0000446 if( xtype==etGENERIC && precision>0 ) precision--;
drh5f968432004-02-21 19:02:30 +0000447#if 0
drha18c5682000-10-08 22:20:57 +0000448 /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */
449 for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
450#else
451 /* It makes more sense to use 0.5 */
drh74161702006-02-24 02:53:49 +0000452 for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
drha18c5682000-10-08 22:20:57 +0000453#endif
drh3e9aeec2005-08-13 13:39:02 +0000454 if( xtype==etFLOAT ) realvalue += rounder;
drha18c5682000-10-08 22:20:57 +0000455 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
456 exp = 0;
drh53c14022007-05-10 17:23:11 +0000457 if( isnan(realvalue) ){
458 bufpt = "NaN";
459 length = 3;
460 break;
461 }
drha18c5682000-10-08 22:20:57 +0000462 if( realvalue>0.0 ){
drh63782852005-08-30 19:30:59 +0000463 while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
464 while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
465 while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
drhb621c232004-02-21 19:41:04 +0000466 while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; }
467 while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; }
468 if( exp>350 || exp<-350 ){
drh53c14022007-05-10 17:23:11 +0000469 if( prefix=='-' ){
470 bufpt = "-Inf";
471 }else if( prefix=='+' ){
472 bufpt = "+Inf";
473 }else{
474 bufpt = "Inf";
475 }
476 length = strlen(bufpt);
drha18c5682000-10-08 22:20:57 +0000477 break;
478 }
drh9adf9ac2002-05-15 11:44:13 +0000479 }
drha18c5682000-10-08 22:20:57 +0000480 bufpt = buf;
481 /*
482 ** If the field type is etGENERIC, then convert to either etEXP
483 ** or etFLOAT, as appropriate.
484 */
485 flag_exp = xtype==etEXP;
486 if( xtype!=etFLOAT ){
487 realvalue += rounder;
488 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
489 }
490 if( xtype==etGENERIC ){
491 flag_rtz = !flag_alternateform;
492 if( exp<-4 || exp>precision ){
493 xtype = etEXP;
494 }else{
495 precision = precision - exp;
496 xtype = etFLOAT;
497 }
drh9adf9ac2002-05-15 11:44:13 +0000498 }else{
drha18c5682000-10-08 22:20:57 +0000499 flag_rtz = 0;
drh9adf9ac2002-05-15 11:44:13 +0000500 }
drh557cc602005-08-13 12:59:14 +0000501 if( xtype==etEXP ){
502 e2 = 0;
503 }else{
504 e2 = exp;
505 }
drha18c5682000-10-08 22:20:57 +0000506 nsd = 0;
drh557cc602005-08-13 12:59:14 +0000507 flag_dp = (precision>0) | flag_alternateform | flag_altform2;
508 /* The sign in front of the number */
509 if( prefix ){
510 *(bufpt++) = prefix;
511 }
512 /* Digits prior to the decimal point */
513 if( e2<0 ){
514 *(bufpt++) = '0';
515 }else{
516 for(; e2>=0; e2--){
517 *(bufpt++) = et_getdigit(&realvalue,&nsd);
drha18c5682000-10-08 22:20:57 +0000518 }
drh9adf9ac2002-05-15 11:44:13 +0000519 }
drh557cc602005-08-13 12:59:14 +0000520 /* The decimal point */
521 if( flag_dp ){
522 *(bufpt++) = '.';
523 }
524 /* "0" digits after the decimal point but before the first
525 ** significant digit of the number */
526 for(e2++; e2<0 && precision>0; precision--, e2++){
527 *(bufpt++) = '0';
528 }
529 /* Significant digits after the decimal point */
530 while( (precision--)>0 ){
531 *(bufpt++) = et_getdigit(&realvalue,&nsd);
532 }
533 /* Remove trailing zeros and the "." if no digits follow the "." */
534 if( flag_rtz && flag_dp ){
drh3e9aeec2005-08-13 13:39:02 +0000535 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
536 assert( bufpt>buf );
537 if( bufpt[-1]=='.' ){
drh557cc602005-08-13 12:59:14 +0000538 if( flag_altform2 ){
539 *(bufpt++) = '0';
540 }else{
541 *(--bufpt) = 0;
542 }
543 }
544 }
545 /* Add the "eNNN" suffix */
546 if( flag_exp || (xtype==etEXP && exp) ){
547 *(bufpt++) = aDigits[infop->charset];
548 if( exp<0 ){
549 *(bufpt++) = '-'; exp = -exp;
550 }else{
551 *(bufpt++) = '+';
552 }
553 if( exp>=100 ){
554 *(bufpt++) = (exp/100)+'0'; /* 100's digit */
555 exp %= 100;
556 }
557 *(bufpt++) = exp/10+'0'; /* 10's digit */
558 *(bufpt++) = exp%10+'0'; /* 1's digit */
559 }
560 *bufpt = 0;
561
drha18c5682000-10-08 22:20:57 +0000562 /* The converted number is in buf[] and zero terminated. Output it.
563 ** Note that the number is in the usual order, not reversed as with
564 ** integer conversions. */
drh94ce4c12003-06-07 11:29:50 +0000565 length = bufpt-buf;
drha18c5682000-10-08 22:20:57 +0000566 bufpt = buf;
567
568 /* Special case: Add leading zeros if the flag_zeropad flag is
569 ** set and we are not left justified */
570 if( flag_zeropad && !flag_leftjustify && length < width){
571 int i;
572 int nPad = width - length;
573 for(i=width; i>=nPad; i--){
574 bufpt[i] = bufpt[i-nPad];
575 }
576 i = prefix!=0;
577 while( nPad-- ) bufpt[i++] = '0';
578 length = width;
579 }
580#endif
581 break;
582 case etSIZE:
583 *(va_arg(ap,int*)) = count;
584 length = width = 0;
585 break;
586 case etPERCENT:
587 buf[0] = '%';
588 bufpt = buf;
589 length = 1;
590 break;
591 case etCHARLIT:
592 case etCHARX:
593 c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt);
594 if( precision>=0 ){
595 for(idx=1; idx<precision; idx++) buf[idx] = c;
596 length = precision;
drh9adf9ac2002-05-15 11:44:13 +0000597 }else{
drha18c5682000-10-08 22:20:57 +0000598 length =1;
drh9adf9ac2002-05-15 11:44:13 +0000599 }
drha18c5682000-10-08 22:20:57 +0000600 bufpt = buf;
601 break;
602 case etSTRING:
drhd93d8a82003-06-16 03:08:18 +0000603 case etDYNSTRING:
drhcb485882002-08-15 13:50:48 +0000604 bufpt = va_arg(ap,char*);
drhd93d8a82003-06-16 03:08:18 +0000605 if( bufpt==0 ){
606 bufpt = "";
607 }else if( xtype==etDYNSTRING ){
608 zExtra = bufpt;
609 }
drha18c5682000-10-08 22:20:57 +0000610 length = strlen(bufpt);
611 if( precision>=0 && precision<length ) length = precision;
612 break;
613 case etSQLESCAPE:
drh5eba8c02005-08-19 02:26:27 +0000614 case etSQLESCAPE2: {
danielk1977f0113002006-01-24 12:09:17 +0000615 int i, j, n, ch, isnull;
drh5eba8c02005-08-19 02:26:27 +0000616 int needQuote;
danielk1977f0113002006-01-24 12:09:17 +0000617 char *escarg = va_arg(ap,char*);
618 isnull = escarg==0;
619 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
620 for(i=n=0; (ch=escarg[i])!=0; i++){
621 if( ch=='\'' ) n++;
drha18c5682000-10-08 22:20:57 +0000622 }
drh5eba8c02005-08-19 02:26:27 +0000623 needQuote = !isnull && xtype==etSQLESCAPE2;
624 n += i + 1 + needQuote*2;
625 if( n>etBUFSIZE ){
626 bufpt = zExtra = sqliteMalloc( n );
627 if( bufpt==0 ) return -1;
628 }else{
629 bufpt = buf;
630 }
631 j = 0;
632 if( needQuote ) bufpt[j++] = '\'';
danielk1977f0113002006-01-24 12:09:17 +0000633 for(i=0; (ch=escarg[i])!=0; i++){
634 bufpt[j++] = ch;
635 if( ch=='\'' ) bufpt[j++] = ch;
drh5eba8c02005-08-19 02:26:27 +0000636 }
637 if( needQuote ) bufpt[j++] = '\'';
638 bufpt[j] = 0;
639 length = j;
drh05a82982006-03-19 13:00:25 +0000640 /* The precision is ignored on %q and %Q */
641 /* if( precision>=0 && precision<length ) length = precision; */
drha18c5682000-10-08 22:20:57 +0000642 break;
drh5eba8c02005-08-19 02:26:27 +0000643 }
drh5f968432004-02-21 19:02:30 +0000644 case etTOKEN: {
645 Token *pToken = va_arg(ap, Token*);
drhad6d9462004-09-19 02:15:24 +0000646 if( pToken && pToken->z ){
drh2646da72005-12-09 20:02:05 +0000647 (*func)(arg, (char*)pToken->z, pToken->n);
drhad6d9462004-09-19 02:15:24 +0000648 }
drh5f968432004-02-21 19:02:30 +0000649 length = width = 0;
650 break;
651 }
652 case etSRCLIST: {
653 SrcList *pSrc = va_arg(ap, SrcList*);
654 int k = va_arg(ap, int);
655 struct SrcList_item *pItem = &pSrc->a[k];
656 assert( k>=0 && k<pSrc->nSrc );
657 if( pItem->zDatabase && pItem->zDatabase[0] ){
658 (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase));
659 (*func)(arg, ".", 1);
660 }
661 (*func)(arg, pItem->zName, strlen(pItem->zName));
662 length = width = 0;
663 break;
664 }
drha18c5682000-10-08 22:20:57 +0000665 }/* End switch over the format type */
666 /*
667 ** The text of the conversion is pointed to by "bufpt" and is
668 ** "length" characters long. The field width is "width". Do
669 ** the output.
670 */
671 if( !flag_leftjustify ){
672 register int nspace;
673 nspace = width-length;
674 if( nspace>0 ){
drha18c5682000-10-08 22:20:57 +0000675 count += nspace;
676 while( nspace>=etSPACESIZE ){
677 (*func)(arg,spaces,etSPACESIZE);
678 nspace -= etSPACESIZE;
679 }
680 if( nspace>0 ) (*func)(arg,spaces,nspace);
681 }
682 }
683 if( length>0 ){
684 (*func)(arg,bufpt,length);
685 count += length;
686 }
687 if( flag_leftjustify ){
688 register int nspace;
689 nspace = width-length;
690 if( nspace>0 ){
691 count += nspace;
692 while( nspace>=etSPACESIZE ){
693 (*func)(arg,spaces,etSPACESIZE);
694 nspace -= etSPACESIZE;
695 }
696 if( nspace>0 ) (*func)(arg,spaces,nspace);
697 }
698 }
699 if( zExtra ){
drh5f968432004-02-21 19:02:30 +0000700 sqliteFree(zExtra);
drha18c5682000-10-08 22:20:57 +0000701 }
702 }/* End for loop over the format string */
703 return errorflag ? -1 : count;
704} /* End of function */
705
706
707/* This structure is used to store state information about the
708** write to memory that is currently in progress.
709*/
710struct sgMprintf {
711 char *zBase; /* A base allocation */
712 char *zText; /* The string collected so far */
713 int nChar; /* Length of the string so far */
drh5f968432004-02-21 19:02:30 +0000714 int nTotal; /* Output size if unconstrained */
drha18c5682000-10-08 22:20:57 +0000715 int nAlloc; /* Amount of space allocated in zText */
drh5f968432004-02-21 19:02:30 +0000716 void *(*xRealloc)(void*,int); /* Function used to realloc memory */
drha18c5682000-10-08 22:20:57 +0000717};
718
719/*
720** This function implements the callback from vxprintf.
721**
722** This routine add nNewChar characters of text in zNewText to
723** the sgMprintf structure pointed to by "arg".
724*/
drh5f968432004-02-21 19:02:30 +0000725static void mout(void *arg, const char *zNewText, int nNewChar){
drha18c5682000-10-08 22:20:57 +0000726 struct sgMprintf *pM = (struct sgMprintf*)arg;
drh5f968432004-02-21 19:02:30 +0000727 pM->nTotal += nNewChar;
drha18c5682000-10-08 22:20:57 +0000728 if( pM->nChar + nNewChar + 1 > pM->nAlloc ){
drh5f968432004-02-21 19:02:30 +0000729 if( pM->xRealloc==0 ){
730 nNewChar = pM->nAlloc - pM->nChar - 1;
drha18c5682000-10-08 22:20:57 +0000731 }else{
drheaad32b2007-05-15 02:34:09 +0000732 int nAlloc = pM->nChar + nNewChar*2 + 1;
drh5f968432004-02-21 19:02:30 +0000733 if( pM->zText==pM->zBase ){
drheaad32b2007-05-15 02:34:09 +0000734 pM->zText = pM->xRealloc(0, nAlloc);
drh5f968432004-02-21 19:02:30 +0000735 if( pM->zText && pM->nChar ){
736 memcpy(pM->zText, pM->zBase, pM->nChar);
737 }
738 }else{
drh53f733c2005-09-16 02:38:09 +0000739 char *zNew;
drheaad32b2007-05-15 02:34:09 +0000740 zNew = pM->xRealloc(pM->zText, nAlloc);
drh53f733c2005-09-16 02:38:09 +0000741 if( zNew ){
742 pM->zText = zNew;
drheaad32b2007-05-15 02:34:09 +0000743 }else{
744 return;
drh53f733c2005-09-16 02:38:09 +0000745 }
drh6d4abfb2001-10-22 02:58:08 +0000746 }
drheaad32b2007-05-15 02:34:09 +0000747 pM->nAlloc = nAlloc;
drha18c5682000-10-08 22:20:57 +0000748 }
749 }
drhe29b1a02004-07-17 21:56:09 +0000750 if( pM->zText ){
751 if( nNewChar>0 ){
752 memcpy(&pM->zText[pM->nChar], zNewText, nNewChar);
753 pM->nChar += nNewChar;
754 }
drha18c5682000-10-08 22:20:57 +0000755 pM->zText[pM->nChar] = 0;
756 }
757}
758
759/*
drh5f968432004-02-21 19:02:30 +0000760** This routine is a wrapper around xprintf() that invokes mout() as
761** the consumer.
drha18c5682000-10-08 22:20:57 +0000762*/
drh5f968432004-02-21 19:02:30 +0000763static char *base_vprintf(
764 void *(*xRealloc)(void*,int), /* Routine to realloc memory. May be NULL */
765 int useInternal, /* Use internal %-conversions if true */
766 char *zInitBuf, /* Initially write here, before mallocing */
767 int nInitBuf, /* Size of zInitBuf[] */
768 const char *zFormat, /* format string */
769 va_list ap /* arguments */
770){
771 struct sgMprintf sM;
772 sM.zBase = sM.zText = zInitBuf;
773 sM.nChar = sM.nTotal = 0;
774 sM.nAlloc = nInitBuf;
775 sM.xRealloc = xRealloc;
776 vxprintf(mout, &sM, useInternal, zFormat, ap);
777 if( xRealloc ){
778 if( sM.zText==sM.zBase ){
779 sM.zText = xRealloc(0, sM.nChar+1);
danielk19778def5ea2004-06-16 10:39:52 +0000780 if( sM.zText ){
781 memcpy(sM.zText, sM.zBase, sM.nChar+1);
782 }
drh5f968432004-02-21 19:02:30 +0000783 }else if( sM.nAlloc>sM.nChar+10 ){
drh53f733c2005-09-16 02:38:09 +0000784 char *zNew = xRealloc(sM.zText, sM.nChar+1);
785 if( zNew ){
786 sM.zText = zNew;
787 }
drh5f968432004-02-21 19:02:30 +0000788 }
789 }
790 return sM.zText;
drh483750b2003-01-29 18:46:51 +0000791}
792
793/*
drh5f968432004-02-21 19:02:30 +0000794** Realloc that is a real function, not a macro.
795*/
796static void *printf_realloc(void *old, int size){
797 return sqliteRealloc(old,size);
798}
799
800/*
801** Print into memory obtained from sqliteMalloc(). Use the internal
802** %-conversion extensions.
803*/
danielk19774adee202004-05-08 08:23:19 +0000804char *sqlite3VMPrintf(const char *zFormat, va_list ap){
drh79158e12005-09-06 21:40:45 +0000805 char zBase[SQLITE_PRINT_BUF_SIZE];
drh5f968432004-02-21 19:02:30 +0000806 return base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
807}
808
809/*
810** Print into memory obtained from sqliteMalloc(). Use the internal
811** %-conversion extensions.
812*/
danielk19774adee202004-05-08 08:23:19 +0000813char *sqlite3MPrintf(const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000814 va_list ap;
815 char *z;
drh79158e12005-09-06 21:40:45 +0000816 char zBase[SQLITE_PRINT_BUF_SIZE];
drh5f968432004-02-21 19:02:30 +0000817 va_start(ap, zFormat);
818 z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
819 va_end(ap);
820 return z;
821}
822
823/*
drh28dd4792006-06-26 21:35:44 +0000824** Print into memory obtained from sqlite3_malloc(). Omit the internal
825** %-conversion extensions.
826*/
827char *sqlite3_vmprintf(const char *zFormat, va_list ap){
828 char zBase[SQLITE_PRINT_BUF_SIZE];
829 return base_vprintf(sqlite3_realloc, 0, zBase, sizeof(zBase), zFormat, ap);
830}
831
832/*
833** Print into memory obtained from sqlite3_malloc()(). Omit the internal
834** %-conversion extensions.
drh483750b2003-01-29 18:46:51 +0000835*/
danielk19776f8a5032004-05-10 10:34:51 +0000836char *sqlite3_mprintf(const char *zFormat, ...){
drha18c5682000-10-08 22:20:57 +0000837 va_list ap;
drh5f968432004-02-21 19:02:30 +0000838 char *z;
drh28dd4792006-06-26 21:35:44 +0000839 va_start(ap, zFormat);
drhb3738b62007-03-31 15:02:49 +0000840 z = sqlite3_vmprintf(zFormat, ap);
drha18c5682000-10-08 22:20:57 +0000841 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000842 return z;
drha18c5682000-10-08 22:20:57 +0000843}
844
drh93a5c6b2003-12-23 02:17:35 +0000845/*
danielk19776f8a5032004-05-10 10:34:51 +0000846** sqlite3_snprintf() works like snprintf() except that it ignores the
drh93a5c6b2003-12-23 02:17:35 +0000847** current locale settings. This is important for SQLite because we
848** are not able to use a "," as the decimal point in place of "." as
849** specified by some locales.
850*/
danielk19776f8a5032004-05-10 10:34:51 +0000851char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000852 char *z;
drh93a5c6b2003-12-23 02:17:35 +0000853 va_list ap;
drh93a5c6b2003-12-23 02:17:35 +0000854
drh68853902007-05-07 11:24:30 +0000855 if( n<=0 ){
856 return zBuf;
857 }
858 zBuf[0] = 0;
drh93a5c6b2003-12-23 02:17:35 +0000859 va_start(ap,zFormat);
drh5f968432004-02-21 19:02:30 +0000860 z = base_vprintf(0, 0, zBuf, n, zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +0000861 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000862 return z;
drh93a5c6b2003-12-23 02:17:35 +0000863}
864
drh7d7f17b2007-06-15 20:29:20 +0000865#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) || defined(SQLITE_MEMDEBUG)
drhe54ca3f2004-06-07 01:52:14 +0000866/*
867** A version of printf() that understands %lld. Used for debugging.
868** The printf() built into some versions of windows does not understand %lld
869** and segfaults if you give it a long long int.
870*/
871void sqlite3DebugPrintf(const char *zFormat, ...){
drh76ff3a02004-09-24 22:32:30 +0000872 extern int getpid(void);
drhe54ca3f2004-06-07 01:52:14 +0000873 va_list ap;
874 char zBuf[500];
875 va_start(ap, zFormat);
876 base_vprintf(0, 0, zBuf, sizeof(zBuf), zFormat, ap);
877 va_end(ap);
drh485f0032007-01-26 19:23:33 +0000878 fprintf(stdout,"%s", zBuf);
drh2ac3ee92004-06-07 16:27:46 +0000879 fflush(stdout);
drhe54ca3f2004-06-07 01:52:14 +0000880}
881#endif