blob: 2558c07c5ec98c874761667a15fda8221cae9045 [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 */
drha18c5682000-10-08 22:20:57 +000068/* The rest are extensions, not normally found in printf() */
drh05a82982006-03-19 13:00:25 +000069#define etCHARLIT 10 /* Literal characters. %' */
70#define etSQLESCAPE 11 /* Strings with '\'' doubled. %q */
71#define etSQLESCAPE2 12 /* Strings with '\'' doubled and enclosed in '',
chw0cfcf3f2002-06-16 04:55:48 +000072 NULL pointers replaced by SQL NULL. %Q */
drh05a82982006-03-19 13:00:25 +000073#define etTOKEN 13 /* a pointer to a Token structure */
74#define etSRCLIST 14 /* a pointer to a SrcList */
75#define etPOINTER 15 /* The %p conversion */
danielk1977f3b863e2007-06-24 06:32:17 +000076#define etSQLESCAPE3 16 /* %w -> Strings with '\"' doubled */
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 },
drh153c62c2007-08-24 03:51:33 +0000115 { 'z', 0, 4, etDYNSTRING, 0, 0 },
drh4794f732004-11-05 17:17:50 +0000116 { 'q', 0, 4, etSQLESCAPE, 0, 0 },
117 { 'Q', 0, 4, etSQLESCAPE2, 0, 0 },
danielk1977f3b863e2007-06-24 06:32:17 +0000118 { 'w', 0, 4, etSQLESCAPE3, 0, 0 },
drh76ff3a02004-09-24 22:32:30 +0000119 { 'c', 0, 0, etCHARX, 0, 0 },
120 { 'o', 8, 0, etRADIX, 0, 2 },
121 { 'u', 10, 0, etRADIX, 0, 0 },
122 { 'x', 16, 0, etRADIX, 16, 1 },
123 { 'X', 16, 0, etRADIX, 0, 4 },
drhb37df7b2005-10-13 02:09:49 +0000124#ifndef SQLITE_OMIT_FLOATING_POINT
drh76ff3a02004-09-24 22:32:30 +0000125 { 'f', 0, 1, etFLOAT, 0, 0 },
126 { 'e', 0, 1, etEXP, 30, 0 },
127 { 'E', 0, 1, etEXP, 14, 0 },
drh76ff3a02004-09-24 22:32:30 +0000128 { 'G', 0, 1, etGENERIC, 14, 0 },
drhb37df7b2005-10-13 02:09:49 +0000129#endif
drh76ff3a02004-09-24 22:32:30 +0000130 { 'i', 10, 1, etRADIX, 0, 0 },
131 { 'n', 0, 0, etSIZE, 0, 0 },
132 { '%', 0, 0, etPERCENT, 0, 0 },
133 { 'p', 16, 0, etPOINTER, 0, 1 },
134 { 'T', 0, 2, etTOKEN, 0, 0 },
135 { 'S', 0, 2, etSRCLIST, 0, 0 },
drha18c5682000-10-08 22:20:57 +0000136};
137#define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0]))
138
139/*
drhb37df7b2005-10-13 02:09:49 +0000140** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
drha18c5682000-10-08 22:20:57 +0000141** conversions will work.
142*/
drhb37df7b2005-10-13 02:09:49 +0000143#ifndef SQLITE_OMIT_FLOATING_POINT
drha18c5682000-10-08 22:20:57 +0000144/*
145** "*val" is a double such that 0.1 <= *val < 10.0
146** Return the ascii code for the leading digit of *val, then
147** multiply "*val" by 10.0 to renormalize.
148**
149** Example:
150** input: *val = 3.14159
151** output: *val = 1.4159 function return = '3'
152**
153** The counter *cnt is incremented each time. After counter exceeds
154** 16 (the number of significant digits in a 64-bit float) '0' is
155** always returned.
156*/
drh384eef32004-01-07 03:04:27 +0000157static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
drha18c5682000-10-08 22:20:57 +0000158 int digit;
drh384eef32004-01-07 03:04:27 +0000159 LONGDOUBLE_TYPE d;
drha18c5682000-10-08 22:20:57 +0000160 if( (*cnt)++ >= 16 ) return '0';
161 digit = (int)*val;
162 d = digit;
163 digit += '0';
164 *val = (*val - d)*10.0;
165 return digit;
166}
drhb37df7b2005-10-13 02:09:49 +0000167#endif /* SQLITE_OMIT_FLOATING_POINT */
drha18c5682000-10-08 22:20:57 +0000168
drh79158e12005-09-06 21:40:45 +0000169/*
drhade86482007-11-28 22:36:40 +0000170** Append N space characters to the given string buffer.
171*/
172static void appendSpace(StrAccum *pAccum, int N){
173 static const char zSpaces[] = " ";
174 while( N>=sizeof(zSpaces)-1 ){
175 sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
176 N -= sizeof(zSpaces)-1;
177 }
178 if( N>0 ){
179 sqlite3StrAccumAppend(pAccum, zSpaces, N);
180 }
181}
182
183/*
drh79158e12005-09-06 21:40:45 +0000184** On machines with a small stack size, you can redefine the
185** SQLITE_PRINT_BUF_SIZE to be less than 350. But beware - for
186** smaller values some %f conversions may go into an infinite loop.
187*/
188#ifndef SQLITE_PRINT_BUF_SIZE
189# define SQLITE_PRINT_BUF_SIZE 350
190#endif
191#define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
drha18c5682000-10-08 22:20:57 +0000192
193/*
194** The root program. All variations call this core.
195**
196** INPUTS:
197** func This is a pointer to a function taking three arguments
198** 1. A pointer to anything. Same as the "arg" parameter.
199** 2. A pointer to the list of characters to be output
200** (Note, this list is NOT null terminated.)
201** 3. An integer number of characters to be output.
202** (Note: This number might be zero.)
203**
204** arg This is the pointer to anything which will be passed as the
205** first argument to "func". Use it for whatever you like.
206**
207** fmt This is the format string, as in the usual print.
208**
209** ap This is a pointer to a list of arguments. Same as in
210** vfprint.
211**
212** OUTPUTS:
213** The return value is the total number of characters sent to
214** the function "func". Returns -1 on a error.
215**
216** Note that the order in which automatic variables are declared below
217** seems to make a big difference in determining how fast this beast
218** will run.
219*/
drhade86482007-11-28 22:36:40 +0000220static void vxprintf(
221 StrAccum *pAccum, /* Accumulate results here */
drh5f968432004-02-21 19:02:30 +0000222 int useExtended, /* Allow extended %-conversions */
223 const char *fmt, /* Format string */
224 va_list ap /* arguments */
drha18c5682000-10-08 22:20:57 +0000225){
drhe84a3062004-02-02 12:29:25 +0000226 int c; /* Next character in the format string */
227 char *bufpt; /* Pointer to the conversion buffer */
228 int precision; /* Precision of the current field */
229 int length; /* Length of the field */
230 int idx; /* A general purpose loop counter */
drhe84a3062004-02-02 12:29:25 +0000231 int width; /* Width of the current field */
232 etByte flag_leftjustify; /* True if "-" flag is present */
233 etByte flag_plussign; /* True if "+" flag is present */
234 etByte flag_blanksign; /* True if " " flag is present */
235 etByte flag_alternateform; /* True if "#" flag is present */
drh531fe872005-08-13 13:40:42 +0000236 etByte flag_altform2; /* True if "!" flag is present */
drhe84a3062004-02-02 12:29:25 +0000237 etByte flag_zeropad; /* True if field width constant starts with zero */
238 etByte flag_long; /* True if "l" flag is present */
drha34b6762004-05-07 13:30:42 +0000239 etByte flag_longlong; /* True if the "ll" flag is present */
drh3e9aeec2005-08-13 13:39:02 +0000240 etByte done; /* Loop termination flag */
drh27436af2006-03-28 23:57:17 +0000241 sqlite_uint64 longvalue; /* Value for integer types */
drh384eef32004-01-07 03:04:27 +0000242 LONGDOUBLE_TYPE realvalue; /* Value for real types */
drh57196282004-10-06 15:41:16 +0000243 const et_info *infop; /* Pointer to the appropriate info structure */
drhe84a3062004-02-02 12:29:25 +0000244 char buf[etBUFSIZE]; /* Conversion buffer */
245 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
246 etByte errorflag = 0; /* True if an error is encountered */
247 etByte xtype; /* Conversion paradigm */
248 char *zExtra; /* Extra memory used for etTCLESCAPE conversions */
drhb37df7b2005-10-13 02:09:49 +0000249#ifndef SQLITE_OMIT_FLOATING_POINT
drh557cc602005-08-13 12:59:14 +0000250 int exp, e2; /* exponent of real numbers */
drhe84a3062004-02-02 12:29:25 +0000251 double rounder; /* Used for rounding floating point values */
252 etByte flag_dp; /* True if decimal point should be shown */
253 etByte flag_rtz; /* True if trailing zeros should be removed */
254 etByte flag_exp; /* True to force display of the exponent */
255 int nsd; /* Number of significant digits returned */
drha18c5682000-10-08 22:20:57 +0000256#endif
257
drhade86482007-11-28 22:36:40 +0000258 length = 0;
drha18c5682000-10-08 22:20:57 +0000259 bufpt = 0;
260 for(; (c=(*fmt))!=0; ++fmt){
261 if( c!='%' ){
drhe84a3062004-02-02 12:29:25 +0000262 int amt;
drha18c5682000-10-08 22:20:57 +0000263 bufpt = (char *)fmt;
264 amt = 1;
265 while( (c=(*++fmt))!='%' && c!=0 ) amt++;
drhade86482007-11-28 22:36:40 +0000266 sqlite3StrAccumAppend(pAccum, bufpt, amt);
drha18c5682000-10-08 22:20:57 +0000267 if( c==0 ) break;
268 }
269 if( (c=(*++fmt))==0 ){
270 errorflag = 1;
drhade86482007-11-28 22:36:40 +0000271 sqlite3StrAccumAppend(pAccum, "%", 1);
drha18c5682000-10-08 22:20:57 +0000272 break;
273 }
274 /* Find out what flags are present */
275 flag_leftjustify = flag_plussign = flag_blanksign =
drh557cc602005-08-13 12:59:14 +0000276 flag_alternateform = flag_altform2 = flag_zeropad = 0;
drh3e9aeec2005-08-13 13:39:02 +0000277 done = 0;
drha18c5682000-10-08 22:20:57 +0000278 do{
279 switch( c ){
drh3e9aeec2005-08-13 13:39:02 +0000280 case '-': flag_leftjustify = 1; break;
281 case '+': flag_plussign = 1; break;
282 case ' ': flag_blanksign = 1; break;
283 case '#': flag_alternateform = 1; break;
284 case '!': flag_altform2 = 1; break;
285 case '0': flag_zeropad = 1; break;
286 default: done = 1; break;
drha18c5682000-10-08 22:20:57 +0000287 }
drh3e9aeec2005-08-13 13:39:02 +0000288 }while( !done && (c=(*++fmt))!=0 );
drha18c5682000-10-08 22:20:57 +0000289 /* Get the field width */
290 width = 0;
291 if( c=='*' ){
292 width = va_arg(ap,int);
293 if( width<0 ){
294 flag_leftjustify = 1;
295 width = -width;
296 }
297 c = *++fmt;
298 }else{
drh17a68932001-01-31 13:28:08 +0000299 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000300 width = width*10 + c - '0';
301 c = *++fmt;
302 }
303 }
304 if( width > etBUFSIZE-10 ){
305 width = etBUFSIZE-10;
306 }
307 /* Get the precision */
308 if( c=='.' ){
309 precision = 0;
310 c = *++fmt;
311 if( c=='*' ){
312 precision = va_arg(ap,int);
drha18c5682000-10-08 22:20:57 +0000313 if( precision<0 ) precision = -precision;
drha18c5682000-10-08 22:20:57 +0000314 c = *++fmt;
315 }else{
drh17a68932001-01-31 13:28:08 +0000316 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000317 precision = precision*10 + c - '0';
318 c = *++fmt;
319 }
320 }
drha18c5682000-10-08 22:20:57 +0000321 }else{
322 precision = -1;
323 }
324 /* Get the conversion type modifier */
325 if( c=='l' ){
326 flag_long = 1;
327 c = *++fmt;
drha34b6762004-05-07 13:30:42 +0000328 if( c=='l' ){
329 flag_longlong = 1;
330 c = *++fmt;
331 }else{
332 flag_longlong = 0;
333 }
drha18c5682000-10-08 22:20:57 +0000334 }else{
drha34b6762004-05-07 13:30:42 +0000335 flag_long = flag_longlong = 0;
drha18c5682000-10-08 22:20:57 +0000336 }
337 /* Fetch the info entry for the field */
338 infop = 0;
339 for(idx=0; idx<etNINFO; idx++){
340 if( c==fmtinfo[idx].fmttype ){
341 infop = &fmtinfo[idx];
drh5f968432004-02-21 19:02:30 +0000342 if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
343 xtype = infop->type;
drhc4413562006-05-22 22:04:00 +0000344 }else{
drhade86482007-11-28 22:36:40 +0000345 return;
drh5f968432004-02-21 19:02:30 +0000346 }
drha18c5682000-10-08 22:20:57 +0000347 break;
348 }
349 }
drha18c5682000-10-08 22:20:57 +0000350 zExtra = 0;
drh43617e92006-03-06 20:55:46 +0000351 if( infop==0 ){
drhade86482007-11-28 22:36:40 +0000352 return;
drh43617e92006-03-06 20:55:46 +0000353 }
354
drha18c5682000-10-08 22:20:57 +0000355
drh4794f732004-11-05 17:17:50 +0000356 /* Limit the precision to prevent overflowing buf[] during conversion */
357 if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
358 precision = etBUFSIZE-40;
359 }
360
drha18c5682000-10-08 22:20:57 +0000361 /*
362 ** At this point, variables are initialized as follows:
363 **
364 ** flag_alternateform TRUE if a '#' is present.
drh3e9aeec2005-08-13 13:39:02 +0000365 ** flag_altform2 TRUE if a '!' is present.
drha18c5682000-10-08 22:20:57 +0000366 ** flag_plussign TRUE if a '+' is present.
367 ** flag_leftjustify TRUE if a '-' is present or if the
368 ** field width was negative.
369 ** flag_zeropad TRUE if the width began with 0.
370 ** flag_long TRUE if the letter 'l' (ell) prefixed
371 ** the conversion character.
drha34b6762004-05-07 13:30:42 +0000372 ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed
373 ** the conversion character.
drha18c5682000-10-08 22:20:57 +0000374 ** flag_blanksign TRUE if a ' ' is present.
375 ** width The specified field width. This is
376 ** always non-negative. Zero is the default.
377 ** precision The specified precision. The default
378 ** is -1.
379 ** xtype The class of the conversion.
380 ** infop Pointer to the appropriate info struct.
381 */
382 switch( xtype ){
drhfe63d1c2004-09-08 20:13:04 +0000383 case etPOINTER:
384 flag_longlong = sizeof(char*)==sizeof(i64);
385 flag_long = sizeof(char*)==sizeof(long int);
386 /* Fall through into the next case */
drha18c5682000-10-08 22:20:57 +0000387 case etRADIX:
drhe84a3062004-02-02 12:29:25 +0000388 if( infop->flags & FLAG_SIGNED ){
drhe9707672004-06-25 01:10:48 +0000389 i64 v;
390 if( flag_longlong ) v = va_arg(ap,i64);
391 else if( flag_long ) v = va_arg(ap,long int);
392 else v = va_arg(ap,int);
393 if( v<0 ){
394 longvalue = -v;
395 prefix = '-';
danielk1977cfcdaef2004-05-12 07:33:33 +0000396 }else{
drhe9707672004-06-25 01:10:48 +0000397 longvalue = v;
398 if( flag_plussign ) prefix = '+';
danielk1977cfcdaef2004-05-12 07:33:33 +0000399 else if( flag_blanksign ) prefix = ' ';
400 else prefix = 0;
401 }
drhe9707672004-06-25 01:10:48 +0000402 }else{
403 if( flag_longlong ) longvalue = va_arg(ap,u64);
404 else if( flag_long ) longvalue = va_arg(ap,unsigned long int);
405 else longvalue = va_arg(ap,unsigned int);
406 prefix = 0;
407 }
408 if( longvalue==0 ) flag_alternateform = 0;
drha18c5682000-10-08 22:20:57 +0000409 if( flag_zeropad && precision<width-(prefix!=0) ){
410 precision = width-(prefix!=0);
drh9adf9ac2002-05-15 11:44:13 +0000411 }
drh3039c0a2004-02-29 00:11:30 +0000412 bufpt = &buf[etBUFSIZE-1];
drha18c5682000-10-08 22:20:57 +0000413 {
drh76ff3a02004-09-24 22:32:30 +0000414 register const char *cset; /* Use registers for speed */
drha18c5682000-10-08 22:20:57 +0000415 register int base;
drh76ff3a02004-09-24 22:32:30 +0000416 cset = &aDigits[infop->charset];
drha18c5682000-10-08 22:20:57 +0000417 base = infop->base;
418 do{ /* Convert to ascii */
419 *(--bufpt) = cset[longvalue%base];
420 longvalue = longvalue/base;
421 }while( longvalue>0 );
drh9adf9ac2002-05-15 11:44:13 +0000422 }
drh3039c0a2004-02-29 00:11:30 +0000423 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000424 for(idx=precision-length; idx>0; idx--){
425 *(--bufpt) = '0'; /* Zero pad */
drh9adf9ac2002-05-15 11:44:13 +0000426 }
drha18c5682000-10-08 22:20:57 +0000427 if( prefix ) *(--bufpt) = prefix; /* Add sign */
428 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
drh76ff3a02004-09-24 22:32:30 +0000429 const char *pre;
430 char x;
431 pre = &aPrefix[infop->prefix];
drha18c5682000-10-08 22:20:57 +0000432 if( *bufpt!=pre[0] ){
drh76ff3a02004-09-24 22:32:30 +0000433 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
drh9adf9ac2002-05-15 11:44:13 +0000434 }
drha18c5682000-10-08 22:20:57 +0000435 }
drh3039c0a2004-02-29 00:11:30 +0000436 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000437 break;
438 case etFLOAT:
439 case etEXP:
440 case etGENERIC:
441 realvalue = va_arg(ap,double);
drhb37df7b2005-10-13 02:09:49 +0000442#ifndef SQLITE_OMIT_FLOATING_POINT
drha18c5682000-10-08 22:20:57 +0000443 if( precision<0 ) precision = 6; /* Set default precision */
drh5eba8c02005-08-19 02:26:27 +0000444 if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10;
drha18c5682000-10-08 22:20:57 +0000445 if( realvalue<0.0 ){
446 realvalue = -realvalue;
447 prefix = '-';
drh9adf9ac2002-05-15 11:44:13 +0000448 }else{
drha18c5682000-10-08 22:20:57 +0000449 if( flag_plussign ) prefix = '+';
450 else if( flag_blanksign ) prefix = ' ';
451 else prefix = 0;
drh9adf9ac2002-05-15 11:44:13 +0000452 }
drh3e9aeec2005-08-13 13:39:02 +0000453 if( xtype==etGENERIC && precision>0 ) precision--;
drh5f968432004-02-21 19:02:30 +0000454#if 0
drha18c5682000-10-08 22:20:57 +0000455 /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */
456 for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
457#else
458 /* It makes more sense to use 0.5 */
drh74161702006-02-24 02:53:49 +0000459 for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
drha18c5682000-10-08 22:20:57 +0000460#endif
drh3e9aeec2005-08-13 13:39:02 +0000461 if( xtype==etFLOAT ) realvalue += rounder;
drha18c5682000-10-08 22:20:57 +0000462 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
463 exp = 0;
drh7cd69272007-06-20 15:29:25 +0000464 if( sqlite3_isnan(realvalue) ){
drh53c14022007-05-10 17:23:11 +0000465 bufpt = "NaN";
466 length = 3;
467 break;
468 }
drha18c5682000-10-08 22:20:57 +0000469 if( realvalue>0.0 ){
drh63782852005-08-30 19:30:59 +0000470 while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
471 while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
472 while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
drhb621c232004-02-21 19:41:04 +0000473 while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; }
474 while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; }
475 if( exp>350 || exp<-350 ){
drh53c14022007-05-10 17:23:11 +0000476 if( prefix=='-' ){
477 bufpt = "-Inf";
478 }else if( prefix=='+' ){
479 bufpt = "+Inf";
480 }else{
481 bufpt = "Inf";
482 }
483 length = strlen(bufpt);
drha18c5682000-10-08 22:20:57 +0000484 break;
485 }
drh9adf9ac2002-05-15 11:44:13 +0000486 }
drha18c5682000-10-08 22:20:57 +0000487 bufpt = buf;
488 /*
489 ** If the field type is etGENERIC, then convert to either etEXP
490 ** or etFLOAT, as appropriate.
491 */
492 flag_exp = xtype==etEXP;
493 if( xtype!=etFLOAT ){
494 realvalue += rounder;
495 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
496 }
497 if( xtype==etGENERIC ){
498 flag_rtz = !flag_alternateform;
499 if( exp<-4 || exp>precision ){
500 xtype = etEXP;
501 }else{
502 precision = precision - exp;
503 xtype = etFLOAT;
504 }
drh9adf9ac2002-05-15 11:44:13 +0000505 }else{
drha18c5682000-10-08 22:20:57 +0000506 flag_rtz = 0;
drh9adf9ac2002-05-15 11:44:13 +0000507 }
drh557cc602005-08-13 12:59:14 +0000508 if( xtype==etEXP ){
509 e2 = 0;
510 }else{
511 e2 = exp;
512 }
drha18c5682000-10-08 22:20:57 +0000513 nsd = 0;
drh557cc602005-08-13 12:59:14 +0000514 flag_dp = (precision>0) | flag_alternateform | flag_altform2;
515 /* The sign in front of the number */
516 if( prefix ){
517 *(bufpt++) = prefix;
518 }
519 /* Digits prior to the decimal point */
520 if( e2<0 ){
521 *(bufpt++) = '0';
522 }else{
523 for(; e2>=0; e2--){
524 *(bufpt++) = et_getdigit(&realvalue,&nsd);
drha18c5682000-10-08 22:20:57 +0000525 }
drh9adf9ac2002-05-15 11:44:13 +0000526 }
drh557cc602005-08-13 12:59:14 +0000527 /* The decimal point */
528 if( flag_dp ){
529 *(bufpt++) = '.';
530 }
531 /* "0" digits after the decimal point but before the first
532 ** significant digit of the number */
533 for(e2++; e2<0 && precision>0; precision--, e2++){
534 *(bufpt++) = '0';
535 }
536 /* Significant digits after the decimal point */
537 while( (precision--)>0 ){
538 *(bufpt++) = et_getdigit(&realvalue,&nsd);
539 }
540 /* Remove trailing zeros and the "." if no digits follow the "." */
541 if( flag_rtz && flag_dp ){
drh3e9aeec2005-08-13 13:39:02 +0000542 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
543 assert( bufpt>buf );
544 if( bufpt[-1]=='.' ){
drh557cc602005-08-13 12:59:14 +0000545 if( flag_altform2 ){
546 *(bufpt++) = '0';
547 }else{
548 *(--bufpt) = 0;
549 }
550 }
551 }
552 /* Add the "eNNN" suffix */
553 if( flag_exp || (xtype==etEXP && exp) ){
554 *(bufpt++) = aDigits[infop->charset];
555 if( exp<0 ){
556 *(bufpt++) = '-'; exp = -exp;
557 }else{
558 *(bufpt++) = '+';
559 }
560 if( exp>=100 ){
561 *(bufpt++) = (exp/100)+'0'; /* 100's digit */
562 exp %= 100;
563 }
564 *(bufpt++) = exp/10+'0'; /* 10's digit */
565 *(bufpt++) = exp%10+'0'; /* 1's digit */
566 }
567 *bufpt = 0;
568
drha18c5682000-10-08 22:20:57 +0000569 /* The converted number is in buf[] and zero terminated. Output it.
570 ** Note that the number is in the usual order, not reversed as with
571 ** integer conversions. */
drh94ce4c12003-06-07 11:29:50 +0000572 length = bufpt-buf;
drha18c5682000-10-08 22:20:57 +0000573 bufpt = buf;
574
575 /* Special case: Add leading zeros if the flag_zeropad flag is
576 ** set and we are not left justified */
577 if( flag_zeropad && !flag_leftjustify && length < width){
578 int i;
579 int nPad = width - length;
580 for(i=width; i>=nPad; i--){
581 bufpt[i] = bufpt[i-nPad];
582 }
583 i = prefix!=0;
584 while( nPad-- ) bufpt[i++] = '0';
585 length = width;
586 }
587#endif
588 break;
589 case etSIZE:
drhade86482007-11-28 22:36:40 +0000590 *(va_arg(ap,int*)) = pAccum->nChar;
drha18c5682000-10-08 22:20:57 +0000591 length = width = 0;
592 break;
593 case etPERCENT:
594 buf[0] = '%';
595 bufpt = buf;
596 length = 1;
597 break;
598 case etCHARLIT:
599 case etCHARX:
600 c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt);
601 if( precision>=0 ){
602 for(idx=1; idx<precision; idx++) buf[idx] = c;
603 length = precision;
drh9adf9ac2002-05-15 11:44:13 +0000604 }else{
drha18c5682000-10-08 22:20:57 +0000605 length =1;
drh9adf9ac2002-05-15 11:44:13 +0000606 }
drha18c5682000-10-08 22:20:57 +0000607 bufpt = buf;
608 break;
609 case etSTRING:
drhd93d8a82003-06-16 03:08:18 +0000610 case etDYNSTRING:
drhcb485882002-08-15 13:50:48 +0000611 bufpt = va_arg(ap,char*);
drhd93d8a82003-06-16 03:08:18 +0000612 if( bufpt==0 ){
613 bufpt = "";
614 }else if( xtype==etDYNSTRING ){
615 zExtra = bufpt;
616 }
drha18c5682000-10-08 22:20:57 +0000617 length = strlen(bufpt);
618 if( precision>=0 && precision<length ) length = precision;
619 break;
620 case etSQLESCAPE:
danielk1977f3b863e2007-06-24 06:32:17 +0000621 case etSQLESCAPE2:
622 case etSQLESCAPE3: {
danielk1977f0113002006-01-24 12:09:17 +0000623 int i, j, n, ch, isnull;
drh5eba8c02005-08-19 02:26:27 +0000624 int needQuote;
danielk1977f3b863e2007-06-24 06:32:17 +0000625 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
danielk1977f0113002006-01-24 12:09:17 +0000626 char *escarg = va_arg(ap,char*);
627 isnull = escarg==0;
628 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
629 for(i=n=0; (ch=escarg[i])!=0; i++){
danielk1977f3b863e2007-06-24 06:32:17 +0000630 if( ch==q ) n++;
drha18c5682000-10-08 22:20:57 +0000631 }
drh5eba8c02005-08-19 02:26:27 +0000632 needQuote = !isnull && xtype==etSQLESCAPE2;
633 n += i + 1 + needQuote*2;
634 if( n>etBUFSIZE ){
drh17435752007-08-16 04:30:38 +0000635 bufpt = zExtra = sqlite3_malloc( n );
drhade86482007-11-28 22:36:40 +0000636 if( bufpt==0 ) return;
drh5eba8c02005-08-19 02:26:27 +0000637 }else{
638 bufpt = buf;
639 }
640 j = 0;
danielk1977f3b863e2007-06-24 06:32:17 +0000641 if( needQuote ) bufpt[j++] = q;
danielk1977f0113002006-01-24 12:09:17 +0000642 for(i=0; (ch=escarg[i])!=0; i++){
643 bufpt[j++] = ch;
danielk1977f3b863e2007-06-24 06:32:17 +0000644 if( ch==q ) bufpt[j++] = ch;
drh5eba8c02005-08-19 02:26:27 +0000645 }
danielk1977f3b863e2007-06-24 06:32:17 +0000646 if( needQuote ) bufpt[j++] = q;
drh5eba8c02005-08-19 02:26:27 +0000647 bufpt[j] = 0;
648 length = j;
drh05a82982006-03-19 13:00:25 +0000649 /* The precision is ignored on %q and %Q */
650 /* if( precision>=0 && precision<length ) length = precision; */
drha18c5682000-10-08 22:20:57 +0000651 break;
drh5eba8c02005-08-19 02:26:27 +0000652 }
drh5f968432004-02-21 19:02:30 +0000653 case etTOKEN: {
654 Token *pToken = va_arg(ap, Token*);
drhad6d9462004-09-19 02:15:24 +0000655 if( pToken && pToken->z ){
drhade86482007-11-28 22:36:40 +0000656 sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
drhad6d9462004-09-19 02:15:24 +0000657 }
drh5f968432004-02-21 19:02:30 +0000658 length = width = 0;
659 break;
660 }
661 case etSRCLIST: {
662 SrcList *pSrc = va_arg(ap, SrcList*);
663 int k = va_arg(ap, int);
664 struct SrcList_item *pItem = &pSrc->a[k];
665 assert( k>=0 && k<pSrc->nSrc );
666 if( pItem->zDatabase && pItem->zDatabase[0] ){
drhade86482007-11-28 22:36:40 +0000667 sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1);
668 sqlite3StrAccumAppend(pAccum, ".", 1);
drh5f968432004-02-21 19:02:30 +0000669 }
drhade86482007-11-28 22:36:40 +0000670 sqlite3StrAccumAppend(pAccum, pItem->zName, -1);
drh5f968432004-02-21 19:02:30 +0000671 length = width = 0;
672 break;
673 }
drha18c5682000-10-08 22:20:57 +0000674 }/* End switch over the format type */
675 /*
676 ** The text of the conversion is pointed to by "bufpt" and is
677 ** "length" characters long. The field width is "width". Do
678 ** the output.
679 */
680 if( !flag_leftjustify ){
681 register int nspace;
682 nspace = width-length;
683 if( nspace>0 ){
drhade86482007-11-28 22:36:40 +0000684 appendSpace(pAccum, nspace);
drha18c5682000-10-08 22:20:57 +0000685 }
686 }
687 if( length>0 ){
drhade86482007-11-28 22:36:40 +0000688 sqlite3StrAccumAppend(pAccum, bufpt, length);
drha18c5682000-10-08 22:20:57 +0000689 }
690 if( flag_leftjustify ){
691 register int nspace;
692 nspace = width-length;
693 if( nspace>0 ){
drhade86482007-11-28 22:36:40 +0000694 appendSpace(pAccum, nspace);
drha18c5682000-10-08 22:20:57 +0000695 }
696 }
697 if( zExtra ){
danielk19771e536952007-08-16 10:09:01 +0000698 sqlite3_free(zExtra);
drha18c5682000-10-08 22:20:57 +0000699 }
700 }/* End for loop over the format string */
drha18c5682000-10-08 22:20:57 +0000701} /* End of function */
702
drhade86482007-11-28 22:36:40 +0000703/*
704** Append N bytes of text from z to the StrAccum object.
drha18c5682000-10-08 22:20:57 +0000705*/
drhade86482007-11-28 22:36:40 +0000706void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
707 if( p->tooBig | p->mallocFailed ){
708 return;
709 }
710 if( N<0 ){
711 N = strlen(z);
712 }
713 if( N==0 ){
714 return;
715 }
716 if( p->nChar+N >= p->nAlloc ){
717 char *zNew;
718 if( !p->useMalloc ){
719 p->tooBig = 1;
720 N = p->nAlloc - p->nChar - 1;
721 if( N<=0 ){
722 return;
723 }
724 }else{
725 p->nAlloc += p->nAlloc + N + 1;
726 if( p->nAlloc > SQLITE_MAX_LENGTH ){
727 p->nAlloc = SQLITE_MAX_LENGTH;
728 if( p->nChar+N >= p->nAlloc ){
729 sqlite3StrAccumReset(p);
730 p->tooBig = 1;
731 return;
drh17435752007-08-16 04:30:38 +0000732 }
drh17435752007-08-16 04:30:38 +0000733 }
drhade86482007-11-28 22:36:40 +0000734 zNew = sqlite3_malloc( p->nAlloc );
drh53f733c2005-09-16 02:38:09 +0000735 if( zNew ){
drhade86482007-11-28 22:36:40 +0000736 memcpy(zNew, p->zText, p->nChar);
737 sqlite3StrAccumReset(p);
738 p->zText = zNew;
739 }else{
740 p->mallocFailed = 1;
741 sqlite3StrAccumReset(p);
742 return;
drh53f733c2005-09-16 02:38:09 +0000743 }
drh5f968432004-02-21 19:02:30 +0000744 }
745 }
drhade86482007-11-28 22:36:40 +0000746 memcpy(&p->zText[p->nChar], z, N);
747 p->nChar += N;
drh483750b2003-01-29 18:46:51 +0000748}
749
750/*
drhade86482007-11-28 22:36:40 +0000751** Finish off a string by making sure it is zero-terminated.
752** Return a pointer to the resulting string. Return a NULL
753** pointer if any kind of error was encountered.
drh5f968432004-02-21 19:02:30 +0000754*/
drhade86482007-11-28 22:36:40 +0000755char *sqlite3StrAccumFinish(StrAccum *p){
756 if( p->zText ){
757 p->zText[p->nChar] = 0;
758 if( p->useMalloc && p->zText==p->zBase ){
759 p->zText = sqlite3_malloc( p->nChar+1 );
760 if( p->zText ){
761 memcpy(p->zText, p->zBase, p->nChar+1);
762 }else{
763 p->mallocFailed = 1;
764 }
765 }
766 }
767 return p->zText;
768}
769
770/*
771** Reset an StrAccum string. Reclaim all malloced memory.
772*/
773void sqlite3StrAccumReset(StrAccum *p){
774 if( p->zText!=p->zBase ){
775 sqlite3_free(p->zText);
776 p->zText = 0;
777 }
778}
779
780/*
781** Initialize a string accumulator
782*/
783static void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n){
784 p->zText = p->zBase = zBase;
785 p->nChar = 0;
786 p->nAlloc = n;
787 p->useMalloc = 1;
788 p->tooBig = 0;
789 p->mallocFailed = 0;
drh5f968432004-02-21 19:02:30 +0000790}
791
792/*
793** Print into memory obtained from sqliteMalloc(). Use the internal
794** %-conversion extensions.
795*/
drh17435752007-08-16 04:30:38 +0000796char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
797 char *z;
drh79158e12005-09-06 21:40:45 +0000798 char zBase[SQLITE_PRINT_BUF_SIZE];
drhade86482007-11-28 22:36:40 +0000799 StrAccum acc;
800 sqlite3StrAccumInit(&acc, zBase, sizeof(zBase));
801 vxprintf(&acc, 1, zFormat, ap);
802 z = sqlite3StrAccumFinish(&acc);
803 if( acc.mallocFailed && db ){
drh17435752007-08-16 04:30:38 +0000804 db->mallocFailed = 1;
805 }
806 return z;
drh5f968432004-02-21 19:02:30 +0000807}
808
809/*
810** Print into memory obtained from sqliteMalloc(). Use the internal
811** %-conversion extensions.
812*/
drh17435752007-08-16 04:30:38 +0000813char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000814 va_list ap;
815 char *z;
drh5f968432004-02-21 19:02:30 +0000816 va_start(ap, zFormat);
drhade86482007-11-28 22:36:40 +0000817 z = sqlite3VMPrintf(db, zFormat, ap);
drh5f968432004-02-21 19:02:30 +0000818 va_end(ap);
819 return z;
820}
821
822/*
drh28dd4792006-06-26 21:35:44 +0000823** Print into memory obtained from sqlite3_malloc(). Omit the internal
824** %-conversion extensions.
825*/
826char *sqlite3_vmprintf(const char *zFormat, va_list ap){
drhade86482007-11-28 22:36:40 +0000827 char *z;
drh28dd4792006-06-26 21:35:44 +0000828 char zBase[SQLITE_PRINT_BUF_SIZE];
drhade86482007-11-28 22:36:40 +0000829 StrAccum acc;
830 sqlite3StrAccumInit(&acc, zBase, sizeof(zBase));
831 vxprintf(&acc, 0, zFormat, ap);
832 z = sqlite3StrAccumFinish(&acc);
833 return z;
drh28dd4792006-06-26 21:35:44 +0000834}
835
836/*
837** Print into memory obtained from sqlite3_malloc()(). Omit the internal
838** %-conversion extensions.
drh483750b2003-01-29 18:46:51 +0000839*/
danielk19776f8a5032004-05-10 10:34:51 +0000840char *sqlite3_mprintf(const char *zFormat, ...){
drha18c5682000-10-08 22:20:57 +0000841 va_list ap;
drh5f968432004-02-21 19:02:30 +0000842 char *z;
drh28dd4792006-06-26 21:35:44 +0000843 va_start(ap, zFormat);
drhb3738b62007-03-31 15:02:49 +0000844 z = sqlite3_vmprintf(zFormat, ap);
drha18c5682000-10-08 22:20:57 +0000845 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000846 return z;
drha18c5682000-10-08 22:20:57 +0000847}
848
drh93a5c6b2003-12-23 02:17:35 +0000849/*
danielk19776f8a5032004-05-10 10:34:51 +0000850** sqlite3_snprintf() works like snprintf() except that it ignores the
drh93a5c6b2003-12-23 02:17:35 +0000851** current locale settings. This is important for SQLite because we
852** are not able to use a "," as the decimal point in place of "." as
853** specified by some locales.
854*/
danielk19776f8a5032004-05-10 10:34:51 +0000855char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000856 char *z;
drh93a5c6b2003-12-23 02:17:35 +0000857 va_list ap;
drhade86482007-11-28 22:36:40 +0000858 StrAccum acc;
drh93a5c6b2003-12-23 02:17:35 +0000859
drh68853902007-05-07 11:24:30 +0000860 if( n<=0 ){
861 return zBuf;
862 }
drhade86482007-11-28 22:36:40 +0000863 sqlite3StrAccumInit(&acc, zBuf, n);
864 acc.useMalloc = 0;
drh93a5c6b2003-12-23 02:17:35 +0000865 va_start(ap,zFormat);
drhade86482007-11-28 22:36:40 +0000866 vxprintf(&acc, 0, zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +0000867 va_end(ap);
drhade86482007-11-28 22:36:40 +0000868 z = sqlite3StrAccumFinish(&acc);
drh5f968432004-02-21 19:02:30 +0000869 return z;
drh93a5c6b2003-12-23 02:17:35 +0000870}
871
drh7d7f17b2007-06-15 20:29:20 +0000872#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) || defined(SQLITE_MEMDEBUG)
drhe54ca3f2004-06-07 01:52:14 +0000873/*
874** A version of printf() that understands %lld. Used for debugging.
875** The printf() built into some versions of windows does not understand %lld
876** and segfaults if you give it a long long int.
877*/
878void sqlite3DebugPrintf(const char *zFormat, ...){
879 va_list ap;
drhade86482007-11-28 22:36:40 +0000880 StrAccum acc;
drhe54ca3f2004-06-07 01:52:14 +0000881 char zBuf[500];
drhade86482007-11-28 22:36:40 +0000882 sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf));
883 acc.useMalloc = 0;
884 va_start(ap,zFormat);
885 vxprintf(&acc, 0, zFormat, ap);
drhe54ca3f2004-06-07 01:52:14 +0000886 va_end(ap);
drhbed8e7e2007-12-08 17:55:35 +0000887 sqlite3StrAccumFinish(&acc);
drh485f0032007-01-26 19:23:33 +0000888 fprintf(stdout,"%s", zBuf);
drh2ac3ee92004-06-07 16:27:46 +0000889 fflush(stdout);
drhe54ca3f2004-06-07 01:52:14 +0000890}
891#endif