blob: aaf1450096bd559fd8e7e71267ffce1019fb6717 [file] [log] [blame]
drha18c5682000-10-08 22:20:57 +00001/*
2** The "printf" code that follows dates from the 1980's. It is in
3** the public domain. The original comments are included here for
drhe84a3062004-02-02 12:29:25 +00004** completeness. They are very out-of-date but might be useful as
5** an historical reference. Most of the "enhancements" have been backed
6** out so that the functionality is now the same as standard printf().
7**
8**************************************************************************
drha18c5682000-10-08 22:20:57 +00009**
drhe78e8282003-01-19 03:59:45 +000010** The following modules is an enhanced replacement for the "printf" subroutines
11** found in the standard C library. The following enhancements are
drha18c5682000-10-08 22:20:57 +000012** supported:
13**
14** + Additional functions. The standard set of "printf" functions
15** includes printf, fprintf, sprintf, vprintf, vfprintf, and
16** vsprintf. This module adds the following:
17**
18** * snprintf -- Works like sprintf, but has an extra argument
19** which is the size of the buffer written to.
20**
21** * mprintf -- Similar to sprintf. Writes output to memory
22** obtained from malloc.
23**
24** * xprintf -- Calls a function to dispose of output.
25**
26** * nprintf -- No output, but returns the number of characters
27** that would have been output by printf.
28**
29** * A v- version (ex: vsnprintf) of every function is also
30** supplied.
31**
32** + A few extensions to the formatting notation are supported:
33**
34** * The "=" flag (similar to "-") causes the output to be
35** be centered in the appropriately sized field.
36**
37** * The %b field outputs an integer in binary notation.
38**
39** * The %c field now accepts a precision. The character output
40** is repeated by the number of times the precision specifies.
41**
42** * The %' field works like %c, but takes as its character the
43** next character of the format string, instead of the next
44** argument. For example, printf("%.78'-") prints 78 minus
45** signs, the same as printf("%.78c",'-').
46**
47** + When compiled using GCC on a SPARC, this version of printf is
48** faster than the library printf for SUN OS 4.1.
49**
50** + All functions are fully reentrant.
51**
52*/
53#include "sqliteInt.h"
drh7c68d602000-10-11 19:28:51 +000054
drha18c5682000-10-08 22:20:57 +000055/*
drha18c5682000-10-08 22:20:57 +000056** Conversion types fall into various categories as defined by the
57** following enumeration.
58*/
drhe84a3062004-02-02 12:29:25 +000059#define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */
60#define etFLOAT 2 /* Floating point. %f */
61#define etEXP 3 /* Exponentional notation. %e and %E */
62#define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */
63#define etSIZE 5 /* Return number of characters processed so far. %n */
64#define etSTRING 6 /* Strings. %s */
65#define etDYNSTRING 7 /* Dynamically allocated strings. %z */
66#define etPERCENT 8 /* Percent symbol. %% */
67#define etCHARX 9 /* Characters. %c */
68#define etERROR 10 /* Used to indicate no such conversion type */
drha18c5682000-10-08 22:20:57 +000069/* The rest are extensions, not normally found in printf() */
drhe84a3062004-02-02 12:29:25 +000070#define etCHARLIT 11 /* Literal characters. %' */
71#define etSQLESCAPE 12 /* Strings with '\'' doubled. %q */
72#define etSQLESCAPE2 13 /* Strings with '\'' doubled and enclosed in '',
chw0cfcf3f2002-06-16 04:55:48 +000073 NULL pointers replaced by SQL NULL. %Q */
drh5f968432004-02-21 19:02:30 +000074#define etTOKEN 14 /* a pointer to a Token structure */
75#define etSRCLIST 15 /* a pointer to a SrcList */
drhfe63d1c2004-09-08 20:13:04 +000076#define etPOINTER 16 /* The %p conversion */
drhe84a3062004-02-02 12:29:25 +000077
78
79/*
80** An "etByte" is an 8-bit unsigned value.
81*/
82typedef unsigned char etByte;
drha18c5682000-10-08 22:20:57 +000083
84/*
85** Each builtin conversion character (ex: the 'd' in "%d") is described
86** by an instance of the following structure
87*/
88typedef struct et_info { /* Information about each format field */
drhe84a3062004-02-02 12:29:25 +000089 char fmttype; /* The format field code letter */
90 etByte base; /* The base for radix conversion */
91 etByte flags; /* One or more of FLAG_ constants below */
92 etByte type; /* Conversion paradigm */
drh76ff3a02004-09-24 22:32:30 +000093 etByte charset; /* Offset into aDigits[] of the digits string */
94 etByte prefix; /* Offset into aPrefix[] of the prefix string */
drha18c5682000-10-08 22:20:57 +000095} et_info;
96
97/*
drhe84a3062004-02-02 12:29:25 +000098** Allowed values for et_info.flags
99*/
100#define FLAG_SIGNED 1 /* True if the value to convert is signed */
101#define FLAG_INTERN 2 /* True if for internal use only */
drh4794f732004-11-05 17:17:50 +0000102#define FLAG_STRING 4 /* Allow infinity precision */
drhe84a3062004-02-02 12:29:25 +0000103
104
105/*
drha18c5682000-10-08 22:20:57 +0000106** The following table is searched linearly, so it is good to put the
107** most frequently used conversion types first.
108*/
drh76ff3a02004-09-24 22:32:30 +0000109static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
110static const char aPrefix[] = "-x0\000X0";
drh57196282004-10-06 15:41:16 +0000111static const et_info fmtinfo[] = {
drh76ff3a02004-09-24 22:32:30 +0000112 { 'd', 10, 1, etRADIX, 0, 0 },
drh4794f732004-11-05 17:17:50 +0000113 { 's', 0, 4, etSTRING, 0, 0 },
drh557cc602005-08-13 12:59:14 +0000114 { 'g', 0, 1, etGENERIC, 30, 0 },
drh4794f732004-11-05 17:17:50 +0000115 { 'z', 0, 6, etDYNSTRING, 0, 0 },
116 { 'q', 0, 4, etSQLESCAPE, 0, 0 },
117 { 'Q', 0, 4, etSQLESCAPE2, 0, 0 },
drh76ff3a02004-09-24 22:32:30 +0000118 { 'c', 0, 0, etCHARX, 0, 0 },
119 { 'o', 8, 0, etRADIX, 0, 2 },
120 { 'u', 10, 0, etRADIX, 0, 0 },
121 { 'x', 16, 0, etRADIX, 16, 1 },
122 { 'X', 16, 0, etRADIX, 0, 4 },
123 { '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 },
127 { 'i', 10, 1, etRADIX, 0, 0 },
128 { 'n', 0, 0, etSIZE, 0, 0 },
129 { '%', 0, 0, etPERCENT, 0, 0 },
130 { 'p', 16, 0, etPOINTER, 0, 1 },
131 { 'T', 0, 2, etTOKEN, 0, 0 },
132 { 'S', 0, 2, etSRCLIST, 0, 0 },
drha18c5682000-10-08 22:20:57 +0000133};
134#define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0]))
135
136/*
137** If NOFLOATINGPOINT is defined, then none of the floating point
138** conversions will work.
139*/
140#ifndef etNOFLOATINGPOINT
141/*
142** "*val" is a double such that 0.1 <= *val < 10.0
143** Return the ascii code for the leading digit of *val, then
144** multiply "*val" by 10.0 to renormalize.
145**
146** Example:
147** input: *val = 3.14159
148** output: *val = 1.4159 function return = '3'
149**
150** The counter *cnt is incremented each time. After counter exceeds
151** 16 (the number of significant digits in a 64-bit float) '0' is
152** always returned.
153*/
drh384eef32004-01-07 03:04:27 +0000154static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
drha18c5682000-10-08 22:20:57 +0000155 int digit;
drh384eef32004-01-07 03:04:27 +0000156 LONGDOUBLE_TYPE d;
drha18c5682000-10-08 22:20:57 +0000157 if( (*cnt)++ >= 16 ) return '0';
158 digit = (int)*val;
159 d = digit;
160 digit += '0';
161 *val = (*val - d)*10.0;
162 return digit;
163}
164#endif
165
166#define etBUFSIZE 1000 /* Size of the output buffer */
167
168/*
169** The root program. All variations call this core.
170**
171** INPUTS:
172** func This is a pointer to a function taking three arguments
173** 1. A pointer to anything. Same as the "arg" parameter.
174** 2. A pointer to the list of characters to be output
175** (Note, this list is NOT null terminated.)
176** 3. An integer number of characters to be output.
177** (Note: This number might be zero.)
178**
179** arg This is the pointer to anything which will be passed as the
180** first argument to "func". Use it for whatever you like.
181**
182** fmt This is the format string, as in the usual print.
183**
184** ap This is a pointer to a list of arguments. Same as in
185** vfprint.
186**
187** OUTPUTS:
188** The return value is the total number of characters sent to
189** the function "func". Returns -1 on a error.
190**
191** Note that the order in which automatic variables are declared below
192** seems to make a big difference in determining how fast this beast
193** will run.
194*/
195static int vxprintf(
drh5f968432004-02-21 19:02:30 +0000196 void (*func)(void*,const char*,int), /* Consumer of text */
197 void *arg, /* First argument to the consumer */
198 int useExtended, /* Allow extended %-conversions */
199 const char *fmt, /* Format string */
200 va_list ap /* arguments */
drha18c5682000-10-08 22:20:57 +0000201){
drhe84a3062004-02-02 12:29:25 +0000202 int c; /* Next character in the format string */
203 char *bufpt; /* Pointer to the conversion buffer */
204 int precision; /* Precision of the current field */
205 int length; /* Length of the field */
206 int idx; /* A general purpose loop counter */
207 int count; /* Total number of characters output */
208 int width; /* Width of the current field */
209 etByte flag_leftjustify; /* True if "-" flag is present */
210 etByte flag_plussign; /* True if "+" flag is present */
211 etByte flag_blanksign; /* True if " " flag is present */
212 etByte flag_alternateform; /* True if "#" flag is present */
drh531fe872005-08-13 13:40:42 +0000213 etByte flag_altform2; /* True if "!" flag is present */
drhe84a3062004-02-02 12:29:25 +0000214 etByte flag_zeropad; /* True if field width constant starts with zero */
215 etByte flag_long; /* True if "l" flag is present */
drha34b6762004-05-07 13:30:42 +0000216 etByte flag_longlong; /* True if the "ll" flag is present */
drh3e9aeec2005-08-13 13:39:02 +0000217 etByte done; /* Loop termination flag */
drha34b6762004-05-07 13:30:42 +0000218 UINT64_TYPE longvalue; /* Value for integer types */
drh384eef32004-01-07 03:04:27 +0000219 LONGDOUBLE_TYPE realvalue; /* Value for real types */
drh57196282004-10-06 15:41:16 +0000220 const et_info *infop; /* Pointer to the appropriate info structure */
drhe84a3062004-02-02 12:29:25 +0000221 char buf[etBUFSIZE]; /* Conversion buffer */
222 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
223 etByte errorflag = 0; /* True if an error is encountered */
224 etByte xtype; /* Conversion paradigm */
225 char *zExtra; /* Extra memory used for etTCLESCAPE conversions */
drh57196282004-10-06 15:41:16 +0000226 static const char spaces[] =
227 " ";
drha18c5682000-10-08 22:20:57 +0000228#define etSPACESIZE (sizeof(spaces)-1)
229#ifndef etNOFLOATINGPOINT
drh557cc602005-08-13 12:59:14 +0000230 int exp, e2; /* exponent of real numbers */
drhe84a3062004-02-02 12:29:25 +0000231 double rounder; /* Used for rounding floating point values */
232 etByte flag_dp; /* True if decimal point should be shown */
233 etByte flag_rtz; /* True if trailing zeros should be removed */
234 etByte flag_exp; /* True to force display of the exponent */
235 int nsd; /* Number of significant digits returned */
drha18c5682000-10-08 22:20:57 +0000236#endif
237
drhe29b1a02004-07-17 21:56:09 +0000238 func(arg,"",0);
drha18c5682000-10-08 22:20:57 +0000239 count = length = 0;
240 bufpt = 0;
241 for(; (c=(*fmt))!=0; ++fmt){
242 if( c!='%' ){
drhe84a3062004-02-02 12:29:25 +0000243 int amt;
drha18c5682000-10-08 22:20:57 +0000244 bufpt = (char *)fmt;
245 amt = 1;
246 while( (c=(*++fmt))!='%' && c!=0 ) amt++;
247 (*func)(arg,bufpt,amt);
248 count += amt;
249 if( c==0 ) break;
250 }
251 if( (c=(*++fmt))==0 ){
252 errorflag = 1;
253 (*func)(arg,"%",1);
254 count++;
255 break;
256 }
257 /* Find out what flags are present */
258 flag_leftjustify = flag_plussign = flag_blanksign =
drh557cc602005-08-13 12:59:14 +0000259 flag_alternateform = flag_altform2 = flag_zeropad = 0;
drh3e9aeec2005-08-13 13:39:02 +0000260 done = 0;
drha18c5682000-10-08 22:20:57 +0000261 do{
262 switch( c ){
drh3e9aeec2005-08-13 13:39:02 +0000263 case '-': flag_leftjustify = 1; break;
264 case '+': flag_plussign = 1; break;
265 case ' ': flag_blanksign = 1; break;
266 case '#': flag_alternateform = 1; break;
267 case '!': flag_altform2 = 1; break;
268 case '0': flag_zeropad = 1; break;
269 default: done = 1; break;
drha18c5682000-10-08 22:20:57 +0000270 }
drh3e9aeec2005-08-13 13:39:02 +0000271 }while( !done && (c=(*++fmt))!=0 );
drha18c5682000-10-08 22:20:57 +0000272 /* Get the field width */
273 width = 0;
274 if( c=='*' ){
275 width = va_arg(ap,int);
276 if( width<0 ){
277 flag_leftjustify = 1;
278 width = -width;
279 }
280 c = *++fmt;
281 }else{
drh17a68932001-01-31 13:28:08 +0000282 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000283 width = width*10 + c - '0';
284 c = *++fmt;
285 }
286 }
287 if( width > etBUFSIZE-10 ){
288 width = etBUFSIZE-10;
289 }
290 /* Get the precision */
291 if( c=='.' ){
292 precision = 0;
293 c = *++fmt;
294 if( c=='*' ){
295 precision = va_arg(ap,int);
drha18c5682000-10-08 22:20:57 +0000296 if( precision<0 ) precision = -precision;
drha18c5682000-10-08 22:20:57 +0000297 c = *++fmt;
298 }else{
drh17a68932001-01-31 13:28:08 +0000299 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000300 precision = precision*10 + c - '0';
301 c = *++fmt;
302 }
303 }
drha18c5682000-10-08 22:20:57 +0000304 }else{
305 precision = -1;
306 }
307 /* Get the conversion type modifier */
308 if( c=='l' ){
309 flag_long = 1;
310 c = *++fmt;
drha34b6762004-05-07 13:30:42 +0000311 if( c=='l' ){
312 flag_longlong = 1;
313 c = *++fmt;
314 }else{
315 flag_longlong = 0;
316 }
drha18c5682000-10-08 22:20:57 +0000317 }else{
drha34b6762004-05-07 13:30:42 +0000318 flag_long = flag_longlong = 0;
drha18c5682000-10-08 22:20:57 +0000319 }
320 /* Fetch the info entry for the field */
321 infop = 0;
drhe84a3062004-02-02 12:29:25 +0000322 xtype = etERROR;
drha18c5682000-10-08 22:20:57 +0000323 for(idx=0; idx<etNINFO; idx++){
324 if( c==fmtinfo[idx].fmttype ){
325 infop = &fmtinfo[idx];
drh5f968432004-02-21 19:02:30 +0000326 if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
327 xtype = infop->type;
328 }
drha18c5682000-10-08 22:20:57 +0000329 break;
330 }
331 }
drha18c5682000-10-08 22:20:57 +0000332 zExtra = 0;
333
drh4794f732004-11-05 17:17:50 +0000334 /* Limit the precision to prevent overflowing buf[] during conversion */
335 if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
336 precision = etBUFSIZE-40;
337 }
338
drha18c5682000-10-08 22:20:57 +0000339 /*
340 ** At this point, variables are initialized as follows:
341 **
342 ** flag_alternateform TRUE if a '#' is present.
drh3e9aeec2005-08-13 13:39:02 +0000343 ** flag_altform2 TRUE if a '!' is present.
drha18c5682000-10-08 22:20:57 +0000344 ** flag_plussign TRUE if a '+' is present.
345 ** flag_leftjustify TRUE if a '-' is present or if the
346 ** field width was negative.
347 ** flag_zeropad TRUE if the width began with 0.
348 ** flag_long TRUE if the letter 'l' (ell) prefixed
349 ** the conversion character.
drha34b6762004-05-07 13:30:42 +0000350 ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed
351 ** the conversion character.
drha18c5682000-10-08 22:20:57 +0000352 ** flag_blanksign TRUE if a ' ' is present.
353 ** width The specified field width. This is
354 ** always non-negative. Zero is the default.
355 ** precision The specified precision. The default
356 ** is -1.
357 ** xtype The class of the conversion.
358 ** infop Pointer to the appropriate info struct.
359 */
360 switch( xtype ){
drhfe63d1c2004-09-08 20:13:04 +0000361 case etPOINTER:
362 flag_longlong = sizeof(char*)==sizeof(i64);
363 flag_long = sizeof(char*)==sizeof(long int);
364 /* Fall through into the next case */
drha18c5682000-10-08 22:20:57 +0000365 case etRADIX:
drhe84a3062004-02-02 12:29:25 +0000366 if( infop->flags & FLAG_SIGNED ){
drhe9707672004-06-25 01:10:48 +0000367 i64 v;
368 if( flag_longlong ) v = va_arg(ap,i64);
369 else if( flag_long ) v = va_arg(ap,long int);
370 else v = va_arg(ap,int);
371 if( v<0 ){
372 longvalue = -v;
373 prefix = '-';
danielk1977cfcdaef2004-05-12 07:33:33 +0000374 }else{
drhe9707672004-06-25 01:10:48 +0000375 longvalue = v;
376 if( flag_plussign ) prefix = '+';
danielk1977cfcdaef2004-05-12 07:33:33 +0000377 else if( flag_blanksign ) prefix = ' ';
378 else prefix = 0;
379 }
drhe9707672004-06-25 01:10:48 +0000380 }else{
381 if( flag_longlong ) longvalue = va_arg(ap,u64);
382 else if( flag_long ) longvalue = va_arg(ap,unsigned long int);
383 else longvalue = va_arg(ap,unsigned int);
384 prefix = 0;
385 }
386 if( longvalue==0 ) flag_alternateform = 0;
drha18c5682000-10-08 22:20:57 +0000387 if( flag_zeropad && precision<width-(prefix!=0) ){
388 precision = width-(prefix!=0);
drh9adf9ac2002-05-15 11:44:13 +0000389 }
drh3039c0a2004-02-29 00:11:30 +0000390 bufpt = &buf[etBUFSIZE-1];
drha18c5682000-10-08 22:20:57 +0000391 {
drh76ff3a02004-09-24 22:32:30 +0000392 register const char *cset; /* Use registers for speed */
drha18c5682000-10-08 22:20:57 +0000393 register int base;
drh76ff3a02004-09-24 22:32:30 +0000394 cset = &aDigits[infop->charset];
drha18c5682000-10-08 22:20:57 +0000395 base = infop->base;
396 do{ /* Convert to ascii */
397 *(--bufpt) = cset[longvalue%base];
398 longvalue = longvalue/base;
399 }while( longvalue>0 );
drh9adf9ac2002-05-15 11:44:13 +0000400 }
drh3039c0a2004-02-29 00:11:30 +0000401 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000402 for(idx=precision-length; idx>0; idx--){
403 *(--bufpt) = '0'; /* Zero pad */
drh9adf9ac2002-05-15 11:44:13 +0000404 }
drha18c5682000-10-08 22:20:57 +0000405 if( prefix ) *(--bufpt) = prefix; /* Add sign */
406 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
drh76ff3a02004-09-24 22:32:30 +0000407 const char *pre;
408 char x;
409 pre = &aPrefix[infop->prefix];
drha18c5682000-10-08 22:20:57 +0000410 if( *bufpt!=pre[0] ){
drh76ff3a02004-09-24 22:32:30 +0000411 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
drh9adf9ac2002-05-15 11:44:13 +0000412 }
drha18c5682000-10-08 22:20:57 +0000413 }
drh3039c0a2004-02-29 00:11:30 +0000414 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000415 break;
416 case etFLOAT:
417 case etEXP:
418 case etGENERIC:
419 realvalue = va_arg(ap,double);
420#ifndef etNOFLOATINGPOINT
421 if( precision<0 ) precision = 6; /* Set default precision */
drh5eba8c02005-08-19 02:26:27 +0000422 if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10;
drha18c5682000-10-08 22:20:57 +0000423 if( realvalue<0.0 ){
424 realvalue = -realvalue;
425 prefix = '-';
drh9adf9ac2002-05-15 11:44:13 +0000426 }else{
drha18c5682000-10-08 22:20:57 +0000427 if( flag_plussign ) prefix = '+';
428 else if( flag_blanksign ) prefix = ' ';
429 else prefix = 0;
drh9adf9ac2002-05-15 11:44:13 +0000430 }
drh3e9aeec2005-08-13 13:39:02 +0000431 if( xtype==etGENERIC && precision>0 ) precision--;
drh5f968432004-02-21 19:02:30 +0000432#if 0
drha18c5682000-10-08 22:20:57 +0000433 /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */
434 for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
435#else
436 /* It makes more sense to use 0.5 */
437 for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1);
438#endif
drh3e9aeec2005-08-13 13:39:02 +0000439 if( xtype==etFLOAT ) realvalue += rounder;
drha18c5682000-10-08 22:20:57 +0000440 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
441 exp = 0;
442 if( realvalue>0.0 ){
drh63782852005-08-30 19:30:59 +0000443 while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
444 while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
445 while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
drhb621c232004-02-21 19:41:04 +0000446 while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; }
447 while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; }
448 if( exp>350 || exp<-350 ){
drha18c5682000-10-08 22:20:57 +0000449 bufpt = "NaN";
450 length = 3;
451 break;
452 }
drh9adf9ac2002-05-15 11:44:13 +0000453 }
drha18c5682000-10-08 22:20:57 +0000454 bufpt = buf;
455 /*
456 ** If the field type is etGENERIC, then convert to either etEXP
457 ** or etFLOAT, as appropriate.
458 */
459 flag_exp = xtype==etEXP;
460 if( xtype!=etFLOAT ){
461 realvalue += rounder;
462 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
463 }
464 if( xtype==etGENERIC ){
465 flag_rtz = !flag_alternateform;
466 if( exp<-4 || exp>precision ){
467 xtype = etEXP;
468 }else{
469 precision = precision - exp;
470 xtype = etFLOAT;
471 }
drh9adf9ac2002-05-15 11:44:13 +0000472 }else{
drha18c5682000-10-08 22:20:57 +0000473 flag_rtz = 0;
drh9adf9ac2002-05-15 11:44:13 +0000474 }
drh557cc602005-08-13 12:59:14 +0000475 if( xtype==etEXP ){
476 e2 = 0;
477 }else{
478 e2 = exp;
479 }
drha18c5682000-10-08 22:20:57 +0000480 nsd = 0;
drh557cc602005-08-13 12:59:14 +0000481 flag_dp = (precision>0) | flag_alternateform | flag_altform2;
482 /* The sign in front of the number */
483 if( prefix ){
484 *(bufpt++) = prefix;
485 }
486 /* Digits prior to the decimal point */
487 if( e2<0 ){
488 *(bufpt++) = '0';
489 }else{
490 for(; e2>=0; e2--){
491 *(bufpt++) = et_getdigit(&realvalue,&nsd);
drha18c5682000-10-08 22:20:57 +0000492 }
drh9adf9ac2002-05-15 11:44:13 +0000493 }
drh557cc602005-08-13 12:59:14 +0000494 /* The decimal point */
495 if( flag_dp ){
496 *(bufpt++) = '.';
497 }
498 /* "0" digits after the decimal point but before the first
499 ** significant digit of the number */
500 for(e2++; e2<0 && precision>0; precision--, e2++){
501 *(bufpt++) = '0';
502 }
503 /* Significant digits after the decimal point */
504 while( (precision--)>0 ){
505 *(bufpt++) = et_getdigit(&realvalue,&nsd);
506 }
507 /* Remove trailing zeros and the "." if no digits follow the "." */
508 if( flag_rtz && flag_dp ){
drh3e9aeec2005-08-13 13:39:02 +0000509 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
510 assert( bufpt>buf );
511 if( bufpt[-1]=='.' ){
drh557cc602005-08-13 12:59:14 +0000512 if( flag_altform2 ){
513 *(bufpt++) = '0';
514 }else{
515 *(--bufpt) = 0;
516 }
517 }
518 }
519 /* Add the "eNNN" suffix */
520 if( flag_exp || (xtype==etEXP && exp) ){
521 *(bufpt++) = aDigits[infop->charset];
522 if( exp<0 ){
523 *(bufpt++) = '-'; exp = -exp;
524 }else{
525 *(bufpt++) = '+';
526 }
527 if( exp>=100 ){
528 *(bufpt++) = (exp/100)+'0'; /* 100's digit */
529 exp %= 100;
530 }
531 *(bufpt++) = exp/10+'0'; /* 10's digit */
532 *(bufpt++) = exp%10+'0'; /* 1's digit */
533 }
534 *bufpt = 0;
535
drha18c5682000-10-08 22:20:57 +0000536 /* The converted number is in buf[] and zero terminated. Output it.
537 ** Note that the number is in the usual order, not reversed as with
538 ** integer conversions. */
drh94ce4c12003-06-07 11:29:50 +0000539 length = bufpt-buf;
drha18c5682000-10-08 22:20:57 +0000540 bufpt = buf;
541
542 /* Special case: Add leading zeros if the flag_zeropad flag is
543 ** set and we are not left justified */
544 if( flag_zeropad && !flag_leftjustify && length < width){
545 int i;
546 int nPad = width - length;
547 for(i=width; i>=nPad; i--){
548 bufpt[i] = bufpt[i-nPad];
549 }
550 i = prefix!=0;
551 while( nPad-- ) bufpt[i++] = '0';
552 length = width;
553 }
554#endif
555 break;
556 case etSIZE:
557 *(va_arg(ap,int*)) = count;
558 length = width = 0;
559 break;
560 case etPERCENT:
561 buf[0] = '%';
562 bufpt = buf;
563 length = 1;
564 break;
565 case etCHARLIT:
566 case etCHARX:
567 c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt);
568 if( precision>=0 ){
569 for(idx=1; idx<precision; idx++) buf[idx] = c;
570 length = precision;
drh9adf9ac2002-05-15 11:44:13 +0000571 }else{
drha18c5682000-10-08 22:20:57 +0000572 length =1;
drh9adf9ac2002-05-15 11:44:13 +0000573 }
drha18c5682000-10-08 22:20:57 +0000574 bufpt = buf;
575 break;
576 case etSTRING:
drhd93d8a82003-06-16 03:08:18 +0000577 case etDYNSTRING:
drhcb485882002-08-15 13:50:48 +0000578 bufpt = va_arg(ap,char*);
drhd93d8a82003-06-16 03:08:18 +0000579 if( bufpt==0 ){
580 bufpt = "";
581 }else if( xtype==etDYNSTRING ){
582 zExtra = bufpt;
583 }
drha18c5682000-10-08 22:20:57 +0000584 length = strlen(bufpt);
585 if( precision>=0 && precision<length ) length = precision;
586 break;
587 case etSQLESCAPE:
drh5eba8c02005-08-19 02:26:27 +0000588 case etSQLESCAPE2: {
589 int i, j, n, c, isnull;
590 int needQuote;
591 char *arg = va_arg(ap,char*);
592 isnull = arg==0;
593 if( isnull ) arg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
594 for(i=n=0; (c=arg[i])!=0; i++){
595 if( c=='\'' ) n++;
drha18c5682000-10-08 22:20:57 +0000596 }
drh5eba8c02005-08-19 02:26:27 +0000597 needQuote = !isnull && xtype==etSQLESCAPE2;
598 n += i + 1 + needQuote*2;
599 if( n>etBUFSIZE ){
600 bufpt = zExtra = sqliteMalloc( n );
601 if( bufpt==0 ) return -1;
602 }else{
603 bufpt = buf;
604 }
605 j = 0;
606 if( needQuote ) bufpt[j++] = '\'';
607 for(i=0; (c=arg[i])!=0; i++){
608 bufpt[j++] = c;
609 if( c=='\'' ) bufpt[j++] = c;
610 }
611 if( needQuote ) bufpt[j++] = '\'';
612 bufpt[j] = 0;
613 length = j;
614 if( precision>=0 && precision<length ) length = precision;
drha18c5682000-10-08 22:20:57 +0000615 break;
drh5eba8c02005-08-19 02:26:27 +0000616 }
drh5f968432004-02-21 19:02:30 +0000617 case etTOKEN: {
618 Token *pToken = va_arg(ap, Token*);
drhad6d9462004-09-19 02:15:24 +0000619 if( pToken && pToken->z ){
620 (*func)(arg, pToken->z, pToken->n);
621 }
drh5f968432004-02-21 19:02:30 +0000622 length = width = 0;
623 break;
624 }
625 case etSRCLIST: {
626 SrcList *pSrc = va_arg(ap, SrcList*);
627 int k = va_arg(ap, int);
628 struct SrcList_item *pItem = &pSrc->a[k];
629 assert( k>=0 && k<pSrc->nSrc );
630 if( pItem->zDatabase && pItem->zDatabase[0] ){
631 (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase));
632 (*func)(arg, ".", 1);
633 }
634 (*func)(arg, pItem->zName, strlen(pItem->zName));
635 length = width = 0;
636 break;
637 }
drha18c5682000-10-08 22:20:57 +0000638 case etERROR:
639 buf[0] = '%';
640 buf[1] = c;
641 errorflag = 0;
642 idx = 1+(c!=0);
643 (*func)(arg,"%",idx);
644 count += idx;
645 if( c==0 ) fmt--;
646 break;
647 }/* End switch over the format type */
648 /*
649 ** The text of the conversion is pointed to by "bufpt" and is
650 ** "length" characters long. The field width is "width". Do
651 ** the output.
652 */
653 if( !flag_leftjustify ){
654 register int nspace;
655 nspace = width-length;
656 if( nspace>0 ){
drha18c5682000-10-08 22:20:57 +0000657 count += nspace;
658 while( nspace>=etSPACESIZE ){
659 (*func)(arg,spaces,etSPACESIZE);
660 nspace -= etSPACESIZE;
661 }
662 if( nspace>0 ) (*func)(arg,spaces,nspace);
663 }
664 }
665 if( length>0 ){
666 (*func)(arg,bufpt,length);
667 count += length;
668 }
669 if( flag_leftjustify ){
670 register int nspace;
671 nspace = width-length;
672 if( nspace>0 ){
673 count += nspace;
674 while( nspace>=etSPACESIZE ){
675 (*func)(arg,spaces,etSPACESIZE);
676 nspace -= etSPACESIZE;
677 }
678 if( nspace>0 ) (*func)(arg,spaces,nspace);
679 }
680 }
681 if( zExtra ){
drh5f968432004-02-21 19:02:30 +0000682 sqliteFree(zExtra);
drha18c5682000-10-08 22:20:57 +0000683 }
684 }/* End for loop over the format string */
685 return errorflag ? -1 : count;
686} /* End of function */
687
688
689/* This structure is used to store state information about the
690** write to memory that is currently in progress.
691*/
692struct sgMprintf {
693 char *zBase; /* A base allocation */
694 char *zText; /* The string collected so far */
695 int nChar; /* Length of the string so far */
drh5f968432004-02-21 19:02:30 +0000696 int nTotal; /* Output size if unconstrained */
drha18c5682000-10-08 22:20:57 +0000697 int nAlloc; /* Amount of space allocated in zText */
drh5f968432004-02-21 19:02:30 +0000698 void *(*xRealloc)(void*,int); /* Function used to realloc memory */
drha18c5682000-10-08 22:20:57 +0000699};
700
701/*
702** This function implements the callback from vxprintf.
703**
704** This routine add nNewChar characters of text in zNewText to
705** the sgMprintf structure pointed to by "arg".
706*/
drh5f968432004-02-21 19:02:30 +0000707static void mout(void *arg, const char *zNewText, int nNewChar){
drha18c5682000-10-08 22:20:57 +0000708 struct sgMprintf *pM = (struct sgMprintf*)arg;
drh5f968432004-02-21 19:02:30 +0000709 pM->nTotal += nNewChar;
drha18c5682000-10-08 22:20:57 +0000710 if( pM->nChar + nNewChar + 1 > pM->nAlloc ){
drh5f968432004-02-21 19:02:30 +0000711 if( pM->xRealloc==0 ){
712 nNewChar = pM->nAlloc - pM->nChar - 1;
drha18c5682000-10-08 22:20:57 +0000713 }else{
drh5f968432004-02-21 19:02:30 +0000714 pM->nAlloc = pM->nChar + nNewChar*2 + 1;
715 if( pM->zText==pM->zBase ){
716 pM->zText = pM->xRealloc(0, pM->nAlloc);
717 if( pM->zText && pM->nChar ){
718 memcpy(pM->zText, pM->zBase, pM->nChar);
719 }
720 }else{
721 pM->zText = pM->xRealloc(pM->zText, pM->nAlloc);
drh6d4abfb2001-10-22 02:58:08 +0000722 }
drha18c5682000-10-08 22:20:57 +0000723 }
724 }
drhe29b1a02004-07-17 21:56:09 +0000725 if( pM->zText ){
726 if( nNewChar>0 ){
727 memcpy(&pM->zText[pM->nChar], zNewText, nNewChar);
728 pM->nChar += nNewChar;
729 }
drha18c5682000-10-08 22:20:57 +0000730 pM->zText[pM->nChar] = 0;
731 }
732}
733
734/*
drh5f968432004-02-21 19:02:30 +0000735** This routine is a wrapper around xprintf() that invokes mout() as
736** the consumer.
drha18c5682000-10-08 22:20:57 +0000737*/
drh5f968432004-02-21 19:02:30 +0000738static char *base_vprintf(
739 void *(*xRealloc)(void*,int), /* Routine to realloc memory. May be NULL */
740 int useInternal, /* Use internal %-conversions if true */
741 char *zInitBuf, /* Initially write here, before mallocing */
742 int nInitBuf, /* Size of zInitBuf[] */
743 const char *zFormat, /* format string */
744 va_list ap /* arguments */
745){
746 struct sgMprintf sM;
747 sM.zBase = sM.zText = zInitBuf;
748 sM.nChar = sM.nTotal = 0;
749 sM.nAlloc = nInitBuf;
750 sM.xRealloc = xRealloc;
751 vxprintf(mout, &sM, useInternal, zFormat, ap);
752 if( xRealloc ){
753 if( sM.zText==sM.zBase ){
754 sM.zText = xRealloc(0, sM.nChar+1);
danielk19778def5ea2004-06-16 10:39:52 +0000755 if( sM.zText ){
756 memcpy(sM.zText, sM.zBase, sM.nChar+1);
757 }
drh5f968432004-02-21 19:02:30 +0000758 }else if( sM.nAlloc>sM.nChar+10 ){
759 sM.zText = xRealloc(sM.zText, sM.nChar+1);
760 }
761 }
762 return sM.zText;
drh483750b2003-01-29 18:46:51 +0000763}
764
765/*
drh5f968432004-02-21 19:02:30 +0000766** Realloc that is a real function, not a macro.
767*/
768static void *printf_realloc(void *old, int size){
769 return sqliteRealloc(old,size);
770}
771
772/*
773** Print into memory obtained from sqliteMalloc(). Use the internal
774** %-conversion extensions.
775*/
danielk19774adee202004-05-08 08:23:19 +0000776char *sqlite3VMPrintf(const char *zFormat, va_list ap){
drh5f968432004-02-21 19:02:30 +0000777 char zBase[1000];
778 return base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
779}
780
781/*
782** Print into memory obtained from sqliteMalloc(). Use the internal
783** %-conversion extensions.
784*/
danielk19774adee202004-05-08 08:23:19 +0000785char *sqlite3MPrintf(const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000786 va_list ap;
787 char *z;
788 char zBase[1000];
789 va_start(ap, zFormat);
790 z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
791 va_end(ap);
792 return z;
793}
794
795/*
796** Print into memory obtained from malloc(). Do not use the internal
797** %-conversion extensions. This routine is for use by external users.
drh483750b2003-01-29 18:46:51 +0000798*/
danielk19776f8a5032004-05-10 10:34:51 +0000799char *sqlite3_mprintf(const char *zFormat, ...){
drha18c5682000-10-08 22:20:57 +0000800 va_list ap;
drh5f968432004-02-21 19:02:30 +0000801 char *z;
drha18c5682000-10-08 22:20:57 +0000802 char zBuf[200];
803
drha18c5682000-10-08 22:20:57 +0000804 va_start(ap,zFormat);
drh5f968432004-02-21 19:02:30 +0000805 z = base_vprintf((void*(*)(void*,int))realloc, 0,
806 zBuf, sizeof(zBuf), zFormat, ap);
drha18c5682000-10-08 22:20:57 +0000807 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000808 return z;
drha18c5682000-10-08 22:20:57 +0000809}
810
danielk19776f8a5032004-05-10 10:34:51 +0000811/* This is the varargs version of sqlite3_mprintf.
drha18c5682000-10-08 22:20:57 +0000812*/
danielk19776f8a5032004-05-10 10:34:51 +0000813char *sqlite3_vmprintf(const char *zFormat, va_list ap){
drha18c5682000-10-08 22:20:57 +0000814 char zBuf[200];
drh5f968432004-02-21 19:02:30 +0000815 return base_vprintf((void*(*)(void*,int))realloc, 0,
816 zBuf, sizeof(zBuf), zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +0000817}
818
819/*
danielk19776f8a5032004-05-10 10:34:51 +0000820** sqlite3_snprintf() works like snprintf() except that it ignores the
drh93a5c6b2003-12-23 02:17:35 +0000821** current locale settings. This is important for SQLite because we
822** are not able to use a "," as the decimal point in place of "." as
823** specified by some locales.
824*/
danielk19776f8a5032004-05-10 10:34:51 +0000825char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000826 char *z;
drh93a5c6b2003-12-23 02:17:35 +0000827 va_list ap;
drh93a5c6b2003-12-23 02:17:35 +0000828
drh93a5c6b2003-12-23 02:17:35 +0000829 va_start(ap,zFormat);
drh5f968432004-02-21 19:02:30 +0000830 z = base_vprintf(0, 0, zBuf, n, zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +0000831 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000832 return z;
drh93a5c6b2003-12-23 02:17:35 +0000833}
834
drh1b743be2004-06-22 22:04:46 +0000835#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
drhe54ca3f2004-06-07 01:52:14 +0000836/*
837** A version of printf() that understands %lld. Used for debugging.
838** The printf() built into some versions of windows does not understand %lld
839** and segfaults if you give it a long long int.
840*/
841void sqlite3DebugPrintf(const char *zFormat, ...){
drh76ff3a02004-09-24 22:32:30 +0000842 extern int getpid(void);
drhe54ca3f2004-06-07 01:52:14 +0000843 va_list ap;
844 char zBuf[500];
845 va_start(ap, zFormat);
846 base_vprintf(0, 0, zBuf, sizeof(zBuf), zFormat, ap);
847 va_end(ap);
drh59c98a62004-09-24 19:39:26 +0000848 fprintf(stdout,"%d: %s", getpid(), zBuf);
drh2ac3ee92004-06-07 16:27:46 +0000849 fflush(stdout);
drhe54ca3f2004-06-07 01:52:14 +0000850}
851#endif