blob: 801db5a837f281d8790624293172c7470b0777f1 [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 */
drhe84a3062004-02-02 12:29:25 +000076
77
78/*
79** An "etByte" is an 8-bit unsigned value.
80*/
81typedef unsigned char etByte;
drha18c5682000-10-08 22:20:57 +000082
83/*
84** Each builtin conversion character (ex: the 'd' in "%d") is described
85** by an instance of the following structure
86*/
87typedef struct et_info { /* Information about each format field */
drhe84a3062004-02-02 12:29:25 +000088 char fmttype; /* The format field code letter */
89 etByte base; /* The base for radix conversion */
90 etByte flags; /* One or more of FLAG_ constants below */
91 etByte type; /* Conversion paradigm */
drh76ff3a02004-09-24 22:32:30 +000092 etByte charset; /* Offset into aDigits[] of the digits string */
93 etByte prefix; /* Offset into aPrefix[] of the prefix string */
drha18c5682000-10-08 22:20:57 +000094} et_info;
95
96/*
drhe84a3062004-02-02 12:29:25 +000097** Allowed values for et_info.flags
98*/
99#define FLAG_SIGNED 1 /* True if the value to convert is signed */
100#define FLAG_INTERN 2 /* True if for internal use only */
drh4794f732004-11-05 17:17:50 +0000101#define FLAG_STRING 4 /* Allow infinity precision */
drhe84a3062004-02-02 12:29:25 +0000102
103
104/*
drha18c5682000-10-08 22:20:57 +0000105** The following table is searched linearly, so it is good to put the
106** most frequently used conversion types first.
107*/
drh76ff3a02004-09-24 22:32:30 +0000108static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
109static const char aPrefix[] = "-x0\000X0";
drh57196282004-10-06 15:41:16 +0000110static const et_info fmtinfo[] = {
drh76ff3a02004-09-24 22:32:30 +0000111 { 'd', 10, 1, etRADIX, 0, 0 },
drh4794f732004-11-05 17:17:50 +0000112 { 's', 0, 4, etSTRING, 0, 0 },
drh557cc602005-08-13 12:59:14 +0000113 { 'g', 0, 1, etGENERIC, 30, 0 },
drh4794f732004-11-05 17:17:50 +0000114 { 'z', 0, 6, etDYNSTRING, 0, 0 },
115 { 'q', 0, 4, etSQLESCAPE, 0, 0 },
116 { 'Q', 0, 4, etSQLESCAPE2, 0, 0 },
drh76ff3a02004-09-24 22:32:30 +0000117 { 'c', 0, 0, etCHARX, 0, 0 },
118 { 'o', 8, 0, etRADIX, 0, 2 },
119 { 'u', 10, 0, etRADIX, 0, 0 },
120 { 'x', 16, 0, etRADIX, 16, 1 },
121 { 'X', 16, 0, etRADIX, 0, 4 },
drhb37df7b2005-10-13 02:09:49 +0000122#ifndef SQLITE_OMIT_FLOATING_POINT
drh76ff3a02004-09-24 22:32:30 +0000123 { 'f', 0, 1, etFLOAT, 0, 0 },
124 { 'e', 0, 1, etEXP, 30, 0 },
125 { 'E', 0, 1, etEXP, 14, 0 },
drh76ff3a02004-09-24 22:32:30 +0000126 { 'G', 0, 1, etGENERIC, 14, 0 },
drhb37df7b2005-10-13 02:09:49 +0000127#endif
drh76ff3a02004-09-24 22:32:30 +0000128 { 'i', 10, 1, etRADIX, 0, 0 },
129 { 'n', 0, 0, etSIZE, 0, 0 },
130 { '%', 0, 0, etPERCENT, 0, 0 },
131 { 'p', 16, 0, etPOINTER, 0, 1 },
132 { 'T', 0, 2, etTOKEN, 0, 0 },
133 { 'S', 0, 2, etSRCLIST, 0, 0 },
drha18c5682000-10-08 22:20:57 +0000134};
135#define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0]))
136
137/*
drhb37df7b2005-10-13 02:09:49 +0000138** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
drha18c5682000-10-08 22:20:57 +0000139** conversions will work.
140*/
drhb37df7b2005-10-13 02:09:49 +0000141#ifndef SQLITE_OMIT_FLOATING_POINT
drha18c5682000-10-08 22:20:57 +0000142/*
143** "*val" is a double such that 0.1 <= *val < 10.0
144** Return the ascii code for the leading digit of *val, then
145** multiply "*val" by 10.0 to renormalize.
146**
147** Example:
148** input: *val = 3.14159
149** output: *val = 1.4159 function return = '3'
150**
151** The counter *cnt is incremented each time. After counter exceeds
152** 16 (the number of significant digits in a 64-bit float) '0' is
153** always returned.
154*/
drh384eef32004-01-07 03:04:27 +0000155static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
drha18c5682000-10-08 22:20:57 +0000156 int digit;
drh384eef32004-01-07 03:04:27 +0000157 LONGDOUBLE_TYPE d;
drha18c5682000-10-08 22:20:57 +0000158 if( (*cnt)++ >= 16 ) return '0';
159 digit = (int)*val;
160 d = digit;
161 digit += '0';
162 *val = (*val - d)*10.0;
163 return digit;
164}
drhb37df7b2005-10-13 02:09:49 +0000165#endif /* SQLITE_OMIT_FLOATING_POINT */
drha18c5682000-10-08 22:20:57 +0000166
drh79158e12005-09-06 21:40:45 +0000167/*
168** On machines with a small stack size, you can redefine the
169** SQLITE_PRINT_BUF_SIZE to be less than 350. But beware - for
170** smaller values some %f conversions may go into an infinite loop.
171*/
172#ifndef SQLITE_PRINT_BUF_SIZE
173# define SQLITE_PRINT_BUF_SIZE 350
174#endif
175#define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
drha18c5682000-10-08 22:20:57 +0000176
177/*
178** The root program. All variations call this core.
179**
180** INPUTS:
181** func This is a pointer to a function taking three arguments
182** 1. A pointer to anything. Same as the "arg" parameter.
183** 2. A pointer to the list of characters to be output
184** (Note, this list is NOT null terminated.)
185** 3. An integer number of characters to be output.
186** (Note: This number might be zero.)
187**
188** arg This is the pointer to anything which will be passed as the
189** first argument to "func". Use it for whatever you like.
190**
191** fmt This is the format string, as in the usual print.
192**
193** ap This is a pointer to a list of arguments. Same as in
194** vfprint.
195**
196** OUTPUTS:
197** The return value is the total number of characters sent to
198** the function "func". Returns -1 on a error.
199**
200** Note that the order in which automatic variables are declared below
201** seems to make a big difference in determining how fast this beast
202** will run.
203*/
204static int vxprintf(
drh5f968432004-02-21 19:02:30 +0000205 void (*func)(void*,const char*,int), /* Consumer of text */
206 void *arg, /* First argument to the consumer */
207 int useExtended, /* Allow extended %-conversions */
208 const char *fmt, /* Format string */
209 va_list ap /* arguments */
drha18c5682000-10-08 22:20:57 +0000210){
drhe84a3062004-02-02 12:29:25 +0000211 int c; /* Next character in the format string */
212 char *bufpt; /* Pointer to the conversion buffer */
213 int precision; /* Precision of the current field */
214 int length; /* Length of the field */
215 int idx; /* A general purpose loop counter */
216 int count; /* Total number of characters output */
217 int width; /* Width of the current field */
218 etByte flag_leftjustify; /* True if "-" flag is present */
219 etByte flag_plussign; /* True if "+" flag is present */
220 etByte flag_blanksign; /* True if " " flag is present */
221 etByte flag_alternateform; /* True if "#" flag is present */
drh531fe872005-08-13 13:40:42 +0000222 etByte flag_altform2; /* True if "!" flag is present */
drhe84a3062004-02-02 12:29:25 +0000223 etByte flag_zeropad; /* True if field width constant starts with zero */
224 etByte flag_long; /* True if "l" flag is present */
drha34b6762004-05-07 13:30:42 +0000225 etByte flag_longlong; /* True if the "ll" flag is present */
drh3e9aeec2005-08-13 13:39:02 +0000226 etByte done; /* Loop termination flag */
drha34b6762004-05-07 13:30:42 +0000227 UINT64_TYPE longvalue; /* Value for integer types */
drh384eef32004-01-07 03:04:27 +0000228 LONGDOUBLE_TYPE realvalue; /* Value for real types */
drh57196282004-10-06 15:41:16 +0000229 const et_info *infop; /* Pointer to the appropriate info structure */
drhe84a3062004-02-02 12:29:25 +0000230 char buf[etBUFSIZE]; /* Conversion buffer */
231 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
232 etByte errorflag = 0; /* True if an error is encountered */
233 etByte xtype; /* Conversion paradigm */
234 char *zExtra; /* Extra memory used for etTCLESCAPE conversions */
drh57196282004-10-06 15:41:16 +0000235 static const char spaces[] =
236 " ";
drha18c5682000-10-08 22:20:57 +0000237#define etSPACESIZE (sizeof(spaces)-1)
drhb37df7b2005-10-13 02:09:49 +0000238#ifndef SQLITE_OMIT_FLOATING_POINT
drh557cc602005-08-13 12:59:14 +0000239 int exp, e2; /* exponent of real numbers */
drhe84a3062004-02-02 12:29:25 +0000240 double rounder; /* Used for rounding floating point values */
241 etByte flag_dp; /* True if decimal point should be shown */
242 etByte flag_rtz; /* True if trailing zeros should be removed */
243 etByte flag_exp; /* True to force display of the exponent */
244 int nsd; /* Number of significant digits returned */
drha18c5682000-10-08 22:20:57 +0000245#endif
246
drhe29b1a02004-07-17 21:56:09 +0000247 func(arg,"",0);
drha18c5682000-10-08 22:20:57 +0000248 count = length = 0;
249 bufpt = 0;
250 for(; (c=(*fmt))!=0; ++fmt){
251 if( c!='%' ){
drhe84a3062004-02-02 12:29:25 +0000252 int amt;
drha18c5682000-10-08 22:20:57 +0000253 bufpt = (char *)fmt;
254 amt = 1;
255 while( (c=(*++fmt))!='%' && c!=0 ) amt++;
256 (*func)(arg,bufpt,amt);
257 count += amt;
258 if( c==0 ) break;
259 }
260 if( (c=(*++fmt))==0 ){
261 errorflag = 1;
262 (*func)(arg,"%",1);
263 count++;
264 break;
265 }
266 /* Find out what flags are present */
267 flag_leftjustify = flag_plussign = flag_blanksign =
drh557cc602005-08-13 12:59:14 +0000268 flag_alternateform = flag_altform2 = flag_zeropad = 0;
drh3e9aeec2005-08-13 13:39:02 +0000269 done = 0;
drha18c5682000-10-08 22:20:57 +0000270 do{
271 switch( c ){
drh3e9aeec2005-08-13 13:39:02 +0000272 case '-': flag_leftjustify = 1; break;
273 case '+': flag_plussign = 1; break;
274 case ' ': flag_blanksign = 1; break;
275 case '#': flag_alternateform = 1; break;
276 case '!': flag_altform2 = 1; break;
277 case '0': flag_zeropad = 1; break;
278 default: done = 1; break;
drha18c5682000-10-08 22:20:57 +0000279 }
drh3e9aeec2005-08-13 13:39:02 +0000280 }while( !done && (c=(*++fmt))!=0 );
drha18c5682000-10-08 22:20:57 +0000281 /* Get the field width */
282 width = 0;
283 if( c=='*' ){
284 width = va_arg(ap,int);
285 if( width<0 ){
286 flag_leftjustify = 1;
287 width = -width;
288 }
289 c = *++fmt;
290 }else{
drh17a68932001-01-31 13:28:08 +0000291 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000292 width = width*10 + c - '0';
293 c = *++fmt;
294 }
295 }
296 if( width > etBUFSIZE-10 ){
297 width = etBUFSIZE-10;
298 }
299 /* Get the precision */
300 if( c=='.' ){
301 precision = 0;
302 c = *++fmt;
303 if( c=='*' ){
304 precision = va_arg(ap,int);
drha18c5682000-10-08 22:20:57 +0000305 if( precision<0 ) precision = -precision;
drha18c5682000-10-08 22:20:57 +0000306 c = *++fmt;
307 }else{
drh17a68932001-01-31 13:28:08 +0000308 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000309 precision = precision*10 + c - '0';
310 c = *++fmt;
311 }
312 }
drha18c5682000-10-08 22:20:57 +0000313 }else{
314 precision = -1;
315 }
316 /* Get the conversion type modifier */
317 if( c=='l' ){
318 flag_long = 1;
319 c = *++fmt;
drha34b6762004-05-07 13:30:42 +0000320 if( c=='l' ){
321 flag_longlong = 1;
322 c = *++fmt;
323 }else{
324 flag_longlong = 0;
325 }
drha18c5682000-10-08 22:20:57 +0000326 }else{
drha34b6762004-05-07 13:30:42 +0000327 flag_long = flag_longlong = 0;
drha18c5682000-10-08 22:20:57 +0000328 }
329 /* Fetch the info entry for the field */
330 infop = 0;
331 for(idx=0; idx<etNINFO; idx++){
332 if( c==fmtinfo[idx].fmttype ){
333 infop = &fmtinfo[idx];
drh5f968432004-02-21 19:02:30 +0000334 if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
335 xtype = infop->type;
336 }
drha18c5682000-10-08 22:20:57 +0000337 break;
338 }
339 }
drha18c5682000-10-08 22:20:57 +0000340 zExtra = 0;
drh43617e92006-03-06 20:55:46 +0000341 if( infop==0 ){
342 return -1;
343 }
344
drha18c5682000-10-08 22:20:57 +0000345
drh4794f732004-11-05 17:17:50 +0000346 /* Limit the precision to prevent overflowing buf[] during conversion */
347 if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
348 precision = etBUFSIZE-40;
349 }
350
drha18c5682000-10-08 22:20:57 +0000351 /*
352 ** At this point, variables are initialized as follows:
353 **
354 ** flag_alternateform TRUE if a '#' is present.
drh3e9aeec2005-08-13 13:39:02 +0000355 ** flag_altform2 TRUE if a '!' is present.
drha18c5682000-10-08 22:20:57 +0000356 ** flag_plussign TRUE if a '+' is present.
357 ** flag_leftjustify TRUE if a '-' is present or if the
358 ** field width was negative.
359 ** flag_zeropad TRUE if the width began with 0.
360 ** flag_long TRUE if the letter 'l' (ell) prefixed
361 ** the conversion character.
drha34b6762004-05-07 13:30:42 +0000362 ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed
363 ** the conversion character.
drha18c5682000-10-08 22:20:57 +0000364 ** flag_blanksign TRUE if a ' ' is present.
365 ** width The specified field width. This is
366 ** always non-negative. Zero is the default.
367 ** precision The specified precision. The default
368 ** is -1.
369 ** xtype The class of the conversion.
370 ** infop Pointer to the appropriate info struct.
371 */
372 switch( xtype ){
drhfe63d1c2004-09-08 20:13:04 +0000373 case etPOINTER:
374 flag_longlong = sizeof(char*)==sizeof(i64);
375 flag_long = sizeof(char*)==sizeof(long int);
376 /* Fall through into the next case */
drha18c5682000-10-08 22:20:57 +0000377 case etRADIX:
drhe84a3062004-02-02 12:29:25 +0000378 if( infop->flags & FLAG_SIGNED ){
drhe9707672004-06-25 01:10:48 +0000379 i64 v;
380 if( flag_longlong ) v = va_arg(ap,i64);
381 else if( flag_long ) v = va_arg(ap,long int);
382 else v = va_arg(ap,int);
383 if( v<0 ){
384 longvalue = -v;
385 prefix = '-';
danielk1977cfcdaef2004-05-12 07:33:33 +0000386 }else{
drhe9707672004-06-25 01:10:48 +0000387 longvalue = v;
388 if( flag_plussign ) prefix = '+';
danielk1977cfcdaef2004-05-12 07:33:33 +0000389 else if( flag_blanksign ) prefix = ' ';
390 else prefix = 0;
391 }
drhe9707672004-06-25 01:10:48 +0000392 }else{
393 if( flag_longlong ) longvalue = va_arg(ap,u64);
394 else if( flag_long ) longvalue = va_arg(ap,unsigned long int);
395 else longvalue = va_arg(ap,unsigned int);
396 prefix = 0;
397 }
398 if( longvalue==0 ) flag_alternateform = 0;
drha18c5682000-10-08 22:20:57 +0000399 if( flag_zeropad && precision<width-(prefix!=0) ){
400 precision = width-(prefix!=0);
drh9adf9ac2002-05-15 11:44:13 +0000401 }
drh3039c0a2004-02-29 00:11:30 +0000402 bufpt = &buf[etBUFSIZE-1];
drha18c5682000-10-08 22:20:57 +0000403 {
drh76ff3a02004-09-24 22:32:30 +0000404 register const char *cset; /* Use registers for speed */
drha18c5682000-10-08 22:20:57 +0000405 register int base;
drh76ff3a02004-09-24 22:32:30 +0000406 cset = &aDigits[infop->charset];
drha18c5682000-10-08 22:20:57 +0000407 base = infop->base;
408 do{ /* Convert to ascii */
409 *(--bufpt) = cset[longvalue%base];
410 longvalue = longvalue/base;
411 }while( longvalue>0 );
drh9adf9ac2002-05-15 11:44:13 +0000412 }
drh3039c0a2004-02-29 00:11:30 +0000413 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000414 for(idx=precision-length; idx>0; idx--){
415 *(--bufpt) = '0'; /* Zero pad */
drh9adf9ac2002-05-15 11:44:13 +0000416 }
drha18c5682000-10-08 22:20:57 +0000417 if( prefix ) *(--bufpt) = prefix; /* Add sign */
418 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
drh76ff3a02004-09-24 22:32:30 +0000419 const char *pre;
420 char x;
421 pre = &aPrefix[infop->prefix];
drha18c5682000-10-08 22:20:57 +0000422 if( *bufpt!=pre[0] ){
drh76ff3a02004-09-24 22:32:30 +0000423 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
drh9adf9ac2002-05-15 11:44:13 +0000424 }
drha18c5682000-10-08 22:20:57 +0000425 }
drh3039c0a2004-02-29 00:11:30 +0000426 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000427 break;
428 case etFLOAT:
429 case etEXP:
430 case etGENERIC:
431 realvalue = va_arg(ap,double);
drhb37df7b2005-10-13 02:09:49 +0000432#ifndef SQLITE_OMIT_FLOATING_POINT
drha18c5682000-10-08 22:20:57 +0000433 if( precision<0 ) precision = 6; /* Set default precision */
drh5eba8c02005-08-19 02:26:27 +0000434 if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10;
drha18c5682000-10-08 22:20:57 +0000435 if( realvalue<0.0 ){
436 realvalue = -realvalue;
437 prefix = '-';
drh9adf9ac2002-05-15 11:44:13 +0000438 }else{
drha18c5682000-10-08 22:20:57 +0000439 if( flag_plussign ) prefix = '+';
440 else if( flag_blanksign ) prefix = ' ';
441 else prefix = 0;
drh9adf9ac2002-05-15 11:44:13 +0000442 }
drh3e9aeec2005-08-13 13:39:02 +0000443 if( xtype==etGENERIC && precision>0 ) precision--;
drh5f968432004-02-21 19:02:30 +0000444#if 0
drha18c5682000-10-08 22:20:57 +0000445 /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */
446 for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
447#else
448 /* It makes more sense to use 0.5 */
drh74161702006-02-24 02:53:49 +0000449 for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
drha18c5682000-10-08 22:20:57 +0000450#endif
drh3e9aeec2005-08-13 13:39:02 +0000451 if( xtype==etFLOAT ) realvalue += rounder;
drha18c5682000-10-08 22:20:57 +0000452 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
453 exp = 0;
454 if( realvalue>0.0 ){
drh63782852005-08-30 19:30:59 +0000455 while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
456 while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
457 while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
drhb621c232004-02-21 19:41:04 +0000458 while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; }
459 while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; }
460 if( exp>350 || exp<-350 ){
drha18c5682000-10-08 22:20:57 +0000461 bufpt = "NaN";
462 length = 3;
463 break;
464 }
drh9adf9ac2002-05-15 11:44:13 +0000465 }
drha18c5682000-10-08 22:20:57 +0000466 bufpt = buf;
467 /*
468 ** If the field type is etGENERIC, then convert to either etEXP
469 ** or etFLOAT, as appropriate.
470 */
471 flag_exp = xtype==etEXP;
472 if( xtype!=etFLOAT ){
473 realvalue += rounder;
474 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
475 }
476 if( xtype==etGENERIC ){
477 flag_rtz = !flag_alternateform;
478 if( exp<-4 || exp>precision ){
479 xtype = etEXP;
480 }else{
481 precision = precision - exp;
482 xtype = etFLOAT;
483 }
drh9adf9ac2002-05-15 11:44:13 +0000484 }else{
drha18c5682000-10-08 22:20:57 +0000485 flag_rtz = 0;
drh9adf9ac2002-05-15 11:44:13 +0000486 }
drh557cc602005-08-13 12:59:14 +0000487 if( xtype==etEXP ){
488 e2 = 0;
489 }else{
490 e2 = exp;
491 }
drha18c5682000-10-08 22:20:57 +0000492 nsd = 0;
drh557cc602005-08-13 12:59:14 +0000493 flag_dp = (precision>0) | flag_alternateform | flag_altform2;
494 /* The sign in front of the number */
495 if( prefix ){
496 *(bufpt++) = prefix;
497 }
498 /* Digits prior to the decimal point */
499 if( e2<0 ){
500 *(bufpt++) = '0';
501 }else{
502 for(; e2>=0; e2--){
503 *(bufpt++) = et_getdigit(&realvalue,&nsd);
drha18c5682000-10-08 22:20:57 +0000504 }
drh9adf9ac2002-05-15 11:44:13 +0000505 }
drh557cc602005-08-13 12:59:14 +0000506 /* The decimal point */
507 if( flag_dp ){
508 *(bufpt++) = '.';
509 }
510 /* "0" digits after the decimal point but before the first
511 ** significant digit of the number */
512 for(e2++; e2<0 && precision>0; precision--, e2++){
513 *(bufpt++) = '0';
514 }
515 /* Significant digits after the decimal point */
516 while( (precision--)>0 ){
517 *(bufpt++) = et_getdigit(&realvalue,&nsd);
518 }
519 /* Remove trailing zeros and the "." if no digits follow the "." */
520 if( flag_rtz && flag_dp ){
drh3e9aeec2005-08-13 13:39:02 +0000521 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
522 assert( bufpt>buf );
523 if( bufpt[-1]=='.' ){
drh557cc602005-08-13 12:59:14 +0000524 if( flag_altform2 ){
525 *(bufpt++) = '0';
526 }else{
527 *(--bufpt) = 0;
528 }
529 }
530 }
531 /* Add the "eNNN" suffix */
532 if( flag_exp || (xtype==etEXP && exp) ){
533 *(bufpt++) = aDigits[infop->charset];
534 if( exp<0 ){
535 *(bufpt++) = '-'; exp = -exp;
536 }else{
537 *(bufpt++) = '+';
538 }
539 if( exp>=100 ){
540 *(bufpt++) = (exp/100)+'0'; /* 100's digit */
541 exp %= 100;
542 }
543 *(bufpt++) = exp/10+'0'; /* 10's digit */
544 *(bufpt++) = exp%10+'0'; /* 1's digit */
545 }
546 *bufpt = 0;
547
drha18c5682000-10-08 22:20:57 +0000548 /* The converted number is in buf[] and zero terminated. Output it.
549 ** Note that the number is in the usual order, not reversed as with
550 ** integer conversions. */
drh94ce4c12003-06-07 11:29:50 +0000551 length = bufpt-buf;
drha18c5682000-10-08 22:20:57 +0000552 bufpt = buf;
553
554 /* Special case: Add leading zeros if the flag_zeropad flag is
555 ** set and we are not left justified */
556 if( flag_zeropad && !flag_leftjustify && length < width){
557 int i;
558 int nPad = width - length;
559 for(i=width; i>=nPad; i--){
560 bufpt[i] = bufpt[i-nPad];
561 }
562 i = prefix!=0;
563 while( nPad-- ) bufpt[i++] = '0';
564 length = width;
565 }
566#endif
567 break;
568 case etSIZE:
569 *(va_arg(ap,int*)) = count;
570 length = width = 0;
571 break;
572 case etPERCENT:
573 buf[0] = '%';
574 bufpt = buf;
575 length = 1;
576 break;
577 case etCHARLIT:
578 case etCHARX:
579 c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt);
580 if( precision>=0 ){
581 for(idx=1; idx<precision; idx++) buf[idx] = c;
582 length = precision;
drh9adf9ac2002-05-15 11:44:13 +0000583 }else{
drha18c5682000-10-08 22:20:57 +0000584 length =1;
drh9adf9ac2002-05-15 11:44:13 +0000585 }
drha18c5682000-10-08 22:20:57 +0000586 bufpt = buf;
587 break;
588 case etSTRING:
drhd93d8a82003-06-16 03:08:18 +0000589 case etDYNSTRING:
drhcb485882002-08-15 13:50:48 +0000590 bufpt = va_arg(ap,char*);
drhd93d8a82003-06-16 03:08:18 +0000591 if( bufpt==0 ){
592 bufpt = "";
593 }else if( xtype==etDYNSTRING ){
594 zExtra = bufpt;
595 }
drha18c5682000-10-08 22:20:57 +0000596 length = strlen(bufpt);
597 if( precision>=0 && precision<length ) length = precision;
598 break;
599 case etSQLESCAPE:
drh5eba8c02005-08-19 02:26:27 +0000600 case etSQLESCAPE2: {
danielk1977f0113002006-01-24 12:09:17 +0000601 int i, j, n, ch, isnull;
drh5eba8c02005-08-19 02:26:27 +0000602 int needQuote;
danielk1977f0113002006-01-24 12:09:17 +0000603 char *escarg = va_arg(ap,char*);
604 isnull = escarg==0;
605 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
606 for(i=n=0; (ch=escarg[i])!=0; i++){
607 if( ch=='\'' ) n++;
drha18c5682000-10-08 22:20:57 +0000608 }
drh5eba8c02005-08-19 02:26:27 +0000609 needQuote = !isnull && xtype==etSQLESCAPE2;
610 n += i + 1 + needQuote*2;
611 if( n>etBUFSIZE ){
612 bufpt = zExtra = sqliteMalloc( n );
613 if( bufpt==0 ) return -1;
614 }else{
615 bufpt = buf;
616 }
617 j = 0;
618 if( needQuote ) bufpt[j++] = '\'';
danielk1977f0113002006-01-24 12:09:17 +0000619 for(i=0; (ch=escarg[i])!=0; i++){
620 bufpt[j++] = ch;
621 if( ch=='\'' ) bufpt[j++] = ch;
drh5eba8c02005-08-19 02:26:27 +0000622 }
623 if( needQuote ) bufpt[j++] = '\'';
624 bufpt[j] = 0;
625 length = j;
drh05a82982006-03-19 13:00:25 +0000626 /* The precision is ignored on %q and %Q */
627 /* if( precision>=0 && precision<length ) length = precision; */
drha18c5682000-10-08 22:20:57 +0000628 break;
drh5eba8c02005-08-19 02:26:27 +0000629 }
drh5f968432004-02-21 19:02:30 +0000630 case etTOKEN: {
631 Token *pToken = va_arg(ap, Token*);
drhad6d9462004-09-19 02:15:24 +0000632 if( pToken && pToken->z ){
drh2646da72005-12-09 20:02:05 +0000633 (*func)(arg, (char*)pToken->z, pToken->n);
drhad6d9462004-09-19 02:15:24 +0000634 }
drh5f968432004-02-21 19:02:30 +0000635 length = width = 0;
636 break;
637 }
638 case etSRCLIST: {
639 SrcList *pSrc = va_arg(ap, SrcList*);
640 int k = va_arg(ap, int);
641 struct SrcList_item *pItem = &pSrc->a[k];
642 assert( k>=0 && k<pSrc->nSrc );
643 if( pItem->zDatabase && pItem->zDatabase[0] ){
644 (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase));
645 (*func)(arg, ".", 1);
646 }
647 (*func)(arg, pItem->zName, strlen(pItem->zName));
648 length = width = 0;
649 break;
650 }
drha18c5682000-10-08 22:20:57 +0000651 }/* End switch over the format type */
652 /*
653 ** The text of the conversion is pointed to by "bufpt" and is
654 ** "length" characters long. The field width is "width". Do
655 ** the output.
656 */
657 if( !flag_leftjustify ){
658 register int nspace;
659 nspace = width-length;
660 if( nspace>0 ){
drha18c5682000-10-08 22:20:57 +0000661 count += nspace;
662 while( nspace>=etSPACESIZE ){
663 (*func)(arg,spaces,etSPACESIZE);
664 nspace -= etSPACESIZE;
665 }
666 if( nspace>0 ) (*func)(arg,spaces,nspace);
667 }
668 }
669 if( length>0 ){
670 (*func)(arg,bufpt,length);
671 count += length;
672 }
673 if( flag_leftjustify ){
674 register int nspace;
675 nspace = width-length;
676 if( nspace>0 ){
677 count += nspace;
678 while( nspace>=etSPACESIZE ){
679 (*func)(arg,spaces,etSPACESIZE);
680 nspace -= etSPACESIZE;
681 }
682 if( nspace>0 ) (*func)(arg,spaces,nspace);
683 }
684 }
685 if( zExtra ){
drh5f968432004-02-21 19:02:30 +0000686 sqliteFree(zExtra);
drha18c5682000-10-08 22:20:57 +0000687 }
688 }/* End for loop over the format string */
689 return errorflag ? -1 : count;
690} /* End of function */
691
692
693/* This structure is used to store state information about the
694** write to memory that is currently in progress.
695*/
696struct sgMprintf {
697 char *zBase; /* A base allocation */
698 char *zText; /* The string collected so far */
699 int nChar; /* Length of the string so far */
drh5f968432004-02-21 19:02:30 +0000700 int nTotal; /* Output size if unconstrained */
drha18c5682000-10-08 22:20:57 +0000701 int nAlloc; /* Amount of space allocated in zText */
drh5f968432004-02-21 19:02:30 +0000702 void *(*xRealloc)(void*,int); /* Function used to realloc memory */
drha18c5682000-10-08 22:20:57 +0000703};
704
705/*
706** This function implements the callback from vxprintf.
707**
708** This routine add nNewChar characters of text in zNewText to
709** the sgMprintf structure pointed to by "arg".
710*/
drh5f968432004-02-21 19:02:30 +0000711static void mout(void *arg, const char *zNewText, int nNewChar){
drha18c5682000-10-08 22:20:57 +0000712 struct sgMprintf *pM = (struct sgMprintf*)arg;
drh5f968432004-02-21 19:02:30 +0000713 pM->nTotal += nNewChar;
drha18c5682000-10-08 22:20:57 +0000714 if( pM->nChar + nNewChar + 1 > pM->nAlloc ){
drh5f968432004-02-21 19:02:30 +0000715 if( pM->xRealloc==0 ){
716 nNewChar = pM->nAlloc - pM->nChar - 1;
drha18c5682000-10-08 22:20:57 +0000717 }else{
drh5f968432004-02-21 19:02:30 +0000718 pM->nAlloc = pM->nChar + nNewChar*2 + 1;
719 if( pM->zText==pM->zBase ){
720 pM->zText = pM->xRealloc(0, pM->nAlloc);
721 if( pM->zText && pM->nChar ){
722 memcpy(pM->zText, pM->zBase, pM->nChar);
723 }
724 }else{
drh53f733c2005-09-16 02:38:09 +0000725 char *zNew;
726 zNew = pM->xRealloc(pM->zText, pM->nAlloc);
727 if( zNew ){
728 pM->zText = zNew;
729 }
drh6d4abfb2001-10-22 02:58:08 +0000730 }
drha18c5682000-10-08 22:20:57 +0000731 }
732 }
drhe29b1a02004-07-17 21:56:09 +0000733 if( pM->zText ){
734 if( nNewChar>0 ){
735 memcpy(&pM->zText[pM->nChar], zNewText, nNewChar);
736 pM->nChar += nNewChar;
737 }
drha18c5682000-10-08 22:20:57 +0000738 pM->zText[pM->nChar] = 0;
739 }
740}
741
742/*
drh5f968432004-02-21 19:02:30 +0000743** This routine is a wrapper around xprintf() that invokes mout() as
744** the consumer.
drha18c5682000-10-08 22:20:57 +0000745*/
drh5f968432004-02-21 19:02:30 +0000746static char *base_vprintf(
747 void *(*xRealloc)(void*,int), /* Routine to realloc memory. May be NULL */
748 int useInternal, /* Use internal %-conversions if true */
749 char *zInitBuf, /* Initially write here, before mallocing */
750 int nInitBuf, /* Size of zInitBuf[] */
751 const char *zFormat, /* format string */
752 va_list ap /* arguments */
753){
754 struct sgMprintf sM;
755 sM.zBase = sM.zText = zInitBuf;
756 sM.nChar = sM.nTotal = 0;
757 sM.nAlloc = nInitBuf;
758 sM.xRealloc = xRealloc;
759 vxprintf(mout, &sM, useInternal, zFormat, ap);
760 if( xRealloc ){
761 if( sM.zText==sM.zBase ){
762 sM.zText = xRealloc(0, sM.nChar+1);
danielk19778def5ea2004-06-16 10:39:52 +0000763 if( sM.zText ){
764 memcpy(sM.zText, sM.zBase, sM.nChar+1);
765 }
drh5f968432004-02-21 19:02:30 +0000766 }else if( sM.nAlloc>sM.nChar+10 ){
drh53f733c2005-09-16 02:38:09 +0000767 char *zNew = xRealloc(sM.zText, sM.nChar+1);
768 if( zNew ){
769 sM.zText = zNew;
770 }
drh5f968432004-02-21 19:02:30 +0000771 }
772 }
773 return sM.zText;
drh483750b2003-01-29 18:46:51 +0000774}
775
776/*
drh5f968432004-02-21 19:02:30 +0000777** Realloc that is a real function, not a macro.
778*/
779static void *printf_realloc(void *old, int size){
780 return sqliteRealloc(old,size);
781}
782
783/*
784** Print into memory obtained from sqliteMalloc(). Use the internal
785** %-conversion extensions.
786*/
danielk19774adee202004-05-08 08:23:19 +0000787char *sqlite3VMPrintf(const char *zFormat, va_list ap){
drh79158e12005-09-06 21:40:45 +0000788 char zBase[SQLITE_PRINT_BUF_SIZE];
drh5f968432004-02-21 19:02:30 +0000789 return base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
790}
791
792/*
793** Print into memory obtained from sqliteMalloc(). Use the internal
794** %-conversion extensions.
795*/
danielk19774adee202004-05-08 08:23:19 +0000796char *sqlite3MPrintf(const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000797 va_list ap;
798 char *z;
drh79158e12005-09-06 21:40:45 +0000799 char zBase[SQLITE_PRINT_BUF_SIZE];
drh5f968432004-02-21 19:02:30 +0000800 va_start(ap, zFormat);
801 z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
802 va_end(ap);
803 return z;
804}
805
806/*
807** Print into memory obtained from malloc(). Do not use the internal
808** %-conversion extensions. This routine is for use by external users.
drh483750b2003-01-29 18:46:51 +0000809*/
danielk19776f8a5032004-05-10 10:34:51 +0000810char *sqlite3_mprintf(const char *zFormat, ...){
drha18c5682000-10-08 22:20:57 +0000811 va_list ap;
drh5f968432004-02-21 19:02:30 +0000812 char *z;
drha18c5682000-10-08 22:20:57 +0000813 char zBuf[200];
814
drha18c5682000-10-08 22:20:57 +0000815 va_start(ap,zFormat);
drh5f968432004-02-21 19:02:30 +0000816 z = base_vprintf((void*(*)(void*,int))realloc, 0,
817 zBuf, sizeof(zBuf), zFormat, ap);
drha18c5682000-10-08 22:20:57 +0000818 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000819 return z;
drha18c5682000-10-08 22:20:57 +0000820}
821
danielk19776f8a5032004-05-10 10:34:51 +0000822/* This is the varargs version of sqlite3_mprintf.
drha18c5682000-10-08 22:20:57 +0000823*/
danielk19776f8a5032004-05-10 10:34:51 +0000824char *sqlite3_vmprintf(const char *zFormat, va_list ap){
drha18c5682000-10-08 22:20:57 +0000825 char zBuf[200];
drh5f968432004-02-21 19:02:30 +0000826 return base_vprintf((void*(*)(void*,int))realloc, 0,
827 zBuf, sizeof(zBuf), zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +0000828}
829
830/*
danielk19776f8a5032004-05-10 10:34:51 +0000831** sqlite3_snprintf() works like snprintf() except that it ignores the
drh93a5c6b2003-12-23 02:17:35 +0000832** current locale settings. This is important for SQLite because we
833** are not able to use a "," as the decimal point in place of "." as
834** specified by some locales.
835*/
danielk19776f8a5032004-05-10 10:34:51 +0000836char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000837 char *z;
drh93a5c6b2003-12-23 02:17:35 +0000838 va_list ap;
drh93a5c6b2003-12-23 02:17:35 +0000839
drh93a5c6b2003-12-23 02:17:35 +0000840 va_start(ap,zFormat);
drh5f968432004-02-21 19:02:30 +0000841 z = base_vprintf(0, 0, zBuf, n, zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +0000842 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000843 return z;
drh93a5c6b2003-12-23 02:17:35 +0000844}
845
drh1b743be2004-06-22 22:04:46 +0000846#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
drhe54ca3f2004-06-07 01:52:14 +0000847/*
848** A version of printf() that understands %lld. Used for debugging.
849** The printf() built into some versions of windows does not understand %lld
850** and segfaults if you give it a long long int.
851*/
852void sqlite3DebugPrintf(const char *zFormat, ...){
drh76ff3a02004-09-24 22:32:30 +0000853 extern int getpid(void);
drhe54ca3f2004-06-07 01:52:14 +0000854 va_list ap;
855 char zBuf[500];
856 va_start(ap, zFormat);
857 base_vprintf(0, 0, zBuf, sizeof(zBuf), zFormat, ap);
858 va_end(ap);
drh59c98a62004-09-24 19:39:26 +0000859 fprintf(stdout,"%d: %s", getpid(), zBuf);
drh2ac3ee92004-06-07 16:27:46 +0000860 fflush(stdout);
drhe54ca3f2004-06-07 01:52:14 +0000861}
862#endif