blob: 634391611cbc70d04651d2f59a9682c6018fae31 [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 */
drh557cc602005-08-13 12:59:14 +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 */
217 UINT64_TYPE longvalue; /* Value for integer types */
drh384eef32004-01-07 03:04:27 +0000218 LONGDOUBLE_TYPE realvalue; /* Value for real types */
drh57196282004-10-06 15:41:16 +0000219 const et_info *infop; /* Pointer to the appropriate info structure */
drhe84a3062004-02-02 12:29:25 +0000220 char buf[etBUFSIZE]; /* Conversion buffer */
221 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
222 etByte errorflag = 0; /* True if an error is encountered */
223 etByte xtype; /* Conversion paradigm */
224 char *zExtra; /* Extra memory used for etTCLESCAPE conversions */
drh57196282004-10-06 15:41:16 +0000225 static const char spaces[] =
226 " ";
drha18c5682000-10-08 22:20:57 +0000227#define etSPACESIZE (sizeof(spaces)-1)
228#ifndef etNOFLOATINGPOINT
drh557cc602005-08-13 12:59:14 +0000229 int exp, e2; /* exponent of real numbers */
drhe84a3062004-02-02 12:29:25 +0000230 double rounder; /* Used for rounding floating point values */
231 etByte flag_dp; /* True if decimal point should be shown */
232 etByte flag_rtz; /* True if trailing zeros should be removed */
233 etByte flag_exp; /* True to force display of the exponent */
234 int nsd; /* Number of significant digits returned */
drha18c5682000-10-08 22:20:57 +0000235#endif
236
drhe29b1a02004-07-17 21:56:09 +0000237 func(arg,"",0);
drha18c5682000-10-08 22:20:57 +0000238 count = length = 0;
239 bufpt = 0;
240 for(; (c=(*fmt))!=0; ++fmt){
241 if( c!='%' ){
drhe84a3062004-02-02 12:29:25 +0000242 int amt;
drha18c5682000-10-08 22:20:57 +0000243 bufpt = (char *)fmt;
244 amt = 1;
245 while( (c=(*++fmt))!='%' && c!=0 ) amt++;
246 (*func)(arg,bufpt,amt);
247 count += amt;
248 if( c==0 ) break;
249 }
250 if( (c=(*++fmt))==0 ){
251 errorflag = 1;
252 (*func)(arg,"%",1);
253 count++;
254 break;
255 }
256 /* Find out what flags are present */
257 flag_leftjustify = flag_plussign = flag_blanksign =
drh557cc602005-08-13 12:59:14 +0000258 flag_alternateform = flag_altform2 = flag_zeropad = 0;
drha18c5682000-10-08 22:20:57 +0000259 do{
260 switch( c ){
261 case '-': flag_leftjustify = 1; c = 0; break;
262 case '+': flag_plussign = 1; c = 0; break;
263 case ' ': flag_blanksign = 1; c = 0; break;
264 case '#': flag_alternateform = 1; c = 0; break;
drh557cc602005-08-13 12:59:14 +0000265 case '!': flag_altform2 = 1; c = 0; break;
drha18c5682000-10-08 22:20:57 +0000266 case '0': flag_zeropad = 1; c = 0; break;
drha18c5682000-10-08 22:20:57 +0000267 default: break;
268 }
269 }while( c==0 && (c=(*++fmt))!=0 );
drha18c5682000-10-08 22:20:57 +0000270 /* Get the field width */
271 width = 0;
272 if( c=='*' ){
273 width = va_arg(ap,int);
274 if( width<0 ){
275 flag_leftjustify = 1;
276 width = -width;
277 }
278 c = *++fmt;
279 }else{
drh17a68932001-01-31 13:28:08 +0000280 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000281 width = width*10 + c - '0';
282 c = *++fmt;
283 }
284 }
285 if( width > etBUFSIZE-10 ){
286 width = etBUFSIZE-10;
287 }
288 /* Get the precision */
289 if( c=='.' ){
290 precision = 0;
291 c = *++fmt;
292 if( c=='*' ){
293 precision = va_arg(ap,int);
drha18c5682000-10-08 22:20:57 +0000294 if( precision<0 ) precision = -precision;
drha18c5682000-10-08 22:20:57 +0000295 c = *++fmt;
296 }else{
drh17a68932001-01-31 13:28:08 +0000297 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000298 precision = precision*10 + c - '0';
299 c = *++fmt;
300 }
301 }
drha18c5682000-10-08 22:20:57 +0000302 }else{
303 precision = -1;
304 }
305 /* Get the conversion type modifier */
306 if( c=='l' ){
307 flag_long = 1;
308 c = *++fmt;
drha34b6762004-05-07 13:30:42 +0000309 if( c=='l' ){
310 flag_longlong = 1;
311 c = *++fmt;
312 }else{
313 flag_longlong = 0;
314 }
drha18c5682000-10-08 22:20:57 +0000315 }else{
drha34b6762004-05-07 13:30:42 +0000316 flag_long = flag_longlong = 0;
drha18c5682000-10-08 22:20:57 +0000317 }
318 /* Fetch the info entry for the field */
319 infop = 0;
drhe84a3062004-02-02 12:29:25 +0000320 xtype = etERROR;
drha18c5682000-10-08 22:20:57 +0000321 for(idx=0; idx<etNINFO; idx++){
322 if( c==fmtinfo[idx].fmttype ){
323 infop = &fmtinfo[idx];
drh5f968432004-02-21 19:02:30 +0000324 if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
325 xtype = infop->type;
326 }
drha18c5682000-10-08 22:20:57 +0000327 break;
328 }
329 }
drha18c5682000-10-08 22:20:57 +0000330 zExtra = 0;
331
drh4794f732004-11-05 17:17:50 +0000332 /* Limit the precision to prevent overflowing buf[] during conversion */
333 if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
334 precision = etBUFSIZE-40;
335 }
336
drha18c5682000-10-08 22:20:57 +0000337 /*
338 ** At this point, variables are initialized as follows:
339 **
340 ** flag_alternateform TRUE if a '#' is present.
341 ** flag_plussign TRUE if a '+' is present.
342 ** flag_leftjustify TRUE if a '-' is present or if the
343 ** field width was negative.
344 ** flag_zeropad TRUE if the width began with 0.
345 ** flag_long TRUE if the letter 'l' (ell) prefixed
346 ** the conversion character.
drha34b6762004-05-07 13:30:42 +0000347 ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed
348 ** the conversion character.
drha18c5682000-10-08 22:20:57 +0000349 ** flag_blanksign TRUE if a ' ' is present.
350 ** width The specified field width. This is
351 ** always non-negative. Zero is the default.
352 ** precision The specified precision. The default
353 ** is -1.
354 ** xtype The class of the conversion.
355 ** infop Pointer to the appropriate info struct.
356 */
357 switch( xtype ){
drhfe63d1c2004-09-08 20:13:04 +0000358 case etPOINTER:
359 flag_longlong = sizeof(char*)==sizeof(i64);
360 flag_long = sizeof(char*)==sizeof(long int);
361 /* Fall through into the next case */
drha18c5682000-10-08 22:20:57 +0000362 case etRADIX:
drhe84a3062004-02-02 12:29:25 +0000363 if( infop->flags & FLAG_SIGNED ){
drhe9707672004-06-25 01:10:48 +0000364 i64 v;
365 if( flag_longlong ) v = va_arg(ap,i64);
366 else if( flag_long ) v = va_arg(ap,long int);
367 else v = va_arg(ap,int);
368 if( v<0 ){
369 longvalue = -v;
370 prefix = '-';
danielk1977cfcdaef2004-05-12 07:33:33 +0000371 }else{
drhe9707672004-06-25 01:10:48 +0000372 longvalue = v;
373 if( flag_plussign ) prefix = '+';
danielk1977cfcdaef2004-05-12 07:33:33 +0000374 else if( flag_blanksign ) prefix = ' ';
375 else prefix = 0;
376 }
drhe9707672004-06-25 01:10:48 +0000377 }else{
378 if( flag_longlong ) longvalue = va_arg(ap,u64);
379 else if( flag_long ) longvalue = va_arg(ap,unsigned long int);
380 else longvalue = va_arg(ap,unsigned int);
381 prefix = 0;
382 }
383 if( longvalue==0 ) flag_alternateform = 0;
drha18c5682000-10-08 22:20:57 +0000384 if( flag_zeropad && precision<width-(prefix!=0) ){
385 precision = width-(prefix!=0);
drh9adf9ac2002-05-15 11:44:13 +0000386 }
drh3039c0a2004-02-29 00:11:30 +0000387 bufpt = &buf[etBUFSIZE-1];
drha18c5682000-10-08 22:20:57 +0000388 {
drh76ff3a02004-09-24 22:32:30 +0000389 register const char *cset; /* Use registers for speed */
drha18c5682000-10-08 22:20:57 +0000390 register int base;
drh76ff3a02004-09-24 22:32:30 +0000391 cset = &aDigits[infop->charset];
drha18c5682000-10-08 22:20:57 +0000392 base = infop->base;
393 do{ /* Convert to ascii */
394 *(--bufpt) = cset[longvalue%base];
395 longvalue = longvalue/base;
396 }while( longvalue>0 );
drh9adf9ac2002-05-15 11:44:13 +0000397 }
drh3039c0a2004-02-29 00:11:30 +0000398 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000399 for(idx=precision-length; idx>0; idx--){
400 *(--bufpt) = '0'; /* Zero pad */
drh9adf9ac2002-05-15 11:44:13 +0000401 }
drha18c5682000-10-08 22:20:57 +0000402 if( prefix ) *(--bufpt) = prefix; /* Add sign */
403 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
drh76ff3a02004-09-24 22:32:30 +0000404 const char *pre;
405 char x;
406 pre = &aPrefix[infop->prefix];
drha18c5682000-10-08 22:20:57 +0000407 if( *bufpt!=pre[0] ){
drh76ff3a02004-09-24 22:32:30 +0000408 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
drh9adf9ac2002-05-15 11:44:13 +0000409 }
drha18c5682000-10-08 22:20:57 +0000410 }
drh3039c0a2004-02-29 00:11:30 +0000411 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000412 break;
413 case etFLOAT:
414 case etEXP:
415 case etGENERIC:
416 realvalue = va_arg(ap,double);
417#ifndef etNOFLOATINGPOINT
418 if( precision<0 ) precision = 6; /* Set default precision */
419 if( precision>etBUFSIZE-10 ) precision = etBUFSIZE-10;
420 if( realvalue<0.0 ){
421 realvalue = -realvalue;
422 prefix = '-';
drh9adf9ac2002-05-15 11:44:13 +0000423 }else{
drha18c5682000-10-08 22:20:57 +0000424 if( flag_plussign ) prefix = '+';
425 else if( flag_blanksign ) prefix = ' ';
426 else prefix = 0;
drh9adf9ac2002-05-15 11:44:13 +0000427 }
drha18c5682000-10-08 22:20:57 +0000428 if( infop->type==etGENERIC && precision>0 ) precision--;
429 rounder = 0.0;
drh5f968432004-02-21 19:02:30 +0000430#if 0
drha18c5682000-10-08 22:20:57 +0000431 /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */
432 for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
433#else
434 /* It makes more sense to use 0.5 */
435 for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1);
436#endif
437 if( infop->type==etFLOAT ) realvalue += rounder;
438 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
439 exp = 0;
440 if( realvalue>0.0 ){
drh557cc602005-08-13 12:59:14 +0000441 while( realvalue>1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
442 while( realvalue>1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
443 while( realvalue>10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
drhb621c232004-02-21 19:41:04 +0000444 while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; }
445 while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; }
446 if( exp>350 || exp<-350 ){
drha18c5682000-10-08 22:20:57 +0000447 bufpt = "NaN";
448 length = 3;
449 break;
450 }
drh9adf9ac2002-05-15 11:44:13 +0000451 }
drha18c5682000-10-08 22:20:57 +0000452 bufpt = buf;
453 /*
454 ** If the field type is etGENERIC, then convert to either etEXP
455 ** or etFLOAT, as appropriate.
456 */
457 flag_exp = xtype==etEXP;
458 if( xtype!=etFLOAT ){
459 realvalue += rounder;
460 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
461 }
462 if( xtype==etGENERIC ){
463 flag_rtz = !flag_alternateform;
464 if( exp<-4 || exp>precision ){
465 xtype = etEXP;
466 }else{
467 precision = precision - exp;
468 xtype = etFLOAT;
469 }
drh9adf9ac2002-05-15 11:44:13 +0000470 }else{
drha18c5682000-10-08 22:20:57 +0000471 flag_rtz = 0;
drh9adf9ac2002-05-15 11:44:13 +0000472 }
drh557cc602005-08-13 12:59:14 +0000473 /* If exp+precision causes the output to be too big for etFLOAT, then
474 ** do etEXP instead
drha18c5682000-10-08 22:20:57 +0000475 */
drh557cc602005-08-13 12:59:14 +0000476 if( xtype==etFLOAT && exp+precision>=etBUFSIZE-30 ){
477 xtype = etEXP;
478 }
479 if( xtype==etEXP ){
480 e2 = 0;
481 }else{
482 e2 = exp;
483 }
drha18c5682000-10-08 22:20:57 +0000484 nsd = 0;
drh557cc602005-08-13 12:59:14 +0000485 flag_dp = (precision>0) | flag_alternateform | flag_altform2;
486 /* The sign in front of the number */
487 if( prefix ){
488 *(bufpt++) = prefix;
489 }
490 /* Digits prior to the decimal point */
491 if( e2<0 ){
492 *(bufpt++) = '0';
493 }else{
494 for(; e2>=0; e2--){
495 *(bufpt++) = et_getdigit(&realvalue,&nsd);
drha18c5682000-10-08 22:20:57 +0000496 }
drh9adf9ac2002-05-15 11:44:13 +0000497 }
drh557cc602005-08-13 12:59:14 +0000498 /* The decimal point */
499 if( flag_dp ){
500 *(bufpt++) = '.';
501 }
502 /* "0" digits after the decimal point but before the first
503 ** significant digit of the number */
504 for(e2++; e2<0 && precision>0; precision--, e2++){
505 *(bufpt++) = '0';
506 }
507 /* Significant digits after the decimal point */
508 while( (precision--)>0 ){
509 *(bufpt++) = et_getdigit(&realvalue,&nsd);
510 }
511 /* Remove trailing zeros and the "." if no digits follow the "." */
512 if( flag_rtz && flag_dp ){
513 while( bufpt>buf && bufpt[-1]=='0' ) *(--bufpt) = 0;
514 if( bufpt>buf && bufpt[-1]=='.' ){
515 if( flag_altform2 ){
516 *(bufpt++) = '0';
517 }else{
518 *(--bufpt) = 0;
519 }
520 }
521 }
522 /* Add the "eNNN" suffix */
523 if( flag_exp || (xtype==etEXP && exp) ){
524 *(bufpt++) = aDigits[infop->charset];
525 if( exp<0 ){
526 *(bufpt++) = '-'; exp = -exp;
527 }else{
528 *(bufpt++) = '+';
529 }
530 if( exp>=100 ){
531 *(bufpt++) = (exp/100)+'0'; /* 100's digit */
532 exp %= 100;
533 }
534 *(bufpt++) = exp/10+'0'; /* 10's digit */
535 *(bufpt++) = exp%10+'0'; /* 1's digit */
536 }
537 *bufpt = 0;
538
drha18c5682000-10-08 22:20:57 +0000539 /* The converted number is in buf[] and zero terminated. Output it.
540 ** Note that the number is in the usual order, not reversed as with
541 ** integer conversions. */
drh94ce4c12003-06-07 11:29:50 +0000542 length = bufpt-buf;
drha18c5682000-10-08 22:20:57 +0000543 bufpt = buf;
544
545 /* Special case: Add leading zeros if the flag_zeropad flag is
546 ** set and we are not left justified */
547 if( flag_zeropad && !flag_leftjustify && length < width){
548 int i;
549 int nPad = width - length;
550 for(i=width; i>=nPad; i--){
551 bufpt[i] = bufpt[i-nPad];
552 }
553 i = prefix!=0;
554 while( nPad-- ) bufpt[i++] = '0';
555 length = width;
556 }
557#endif
558 break;
559 case etSIZE:
560 *(va_arg(ap,int*)) = count;
561 length = width = 0;
562 break;
563 case etPERCENT:
564 buf[0] = '%';
565 bufpt = buf;
566 length = 1;
567 break;
568 case etCHARLIT:
569 case etCHARX:
570 c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt);
571 if( precision>=0 ){
572 for(idx=1; idx<precision; idx++) buf[idx] = c;
573 length = precision;
drh9adf9ac2002-05-15 11:44:13 +0000574 }else{
drha18c5682000-10-08 22:20:57 +0000575 length =1;
drh9adf9ac2002-05-15 11:44:13 +0000576 }
drha18c5682000-10-08 22:20:57 +0000577 bufpt = buf;
578 break;
579 case etSTRING:
drhd93d8a82003-06-16 03:08:18 +0000580 case etDYNSTRING:
drhcb485882002-08-15 13:50:48 +0000581 bufpt = va_arg(ap,char*);
drhd93d8a82003-06-16 03:08:18 +0000582 if( bufpt==0 ){
583 bufpt = "";
584 }else if( xtype==etDYNSTRING ){
585 zExtra = bufpt;
586 }
drha18c5682000-10-08 22:20:57 +0000587 length = strlen(bufpt);
588 if( precision>=0 && precision<length ) length = precision;
589 break;
590 case etSQLESCAPE:
chw0cfcf3f2002-06-16 04:55:48 +0000591 case etSQLESCAPE2:
drha18c5682000-10-08 22:20:57 +0000592 {
chw0cfcf3f2002-06-16 04:55:48 +0000593 int i, j, n, c, isnull;
drh4794f732004-11-05 17:17:50 +0000594 int needQuote;
drha18c5682000-10-08 22:20:57 +0000595 char *arg = va_arg(ap,char*);
chw0cfcf3f2002-06-16 04:55:48 +0000596 isnull = arg==0;
597 if( isnull ) arg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
drha18c5682000-10-08 22:20:57 +0000598 for(i=n=0; (c=arg[i])!=0; i++){
599 if( c=='\'' ) n++;
600 }
drh4794f732004-11-05 17:17:50 +0000601 needQuote = !isnull && xtype==etSQLESCAPE2;
602 n += i + 1 + needQuote*2;
drha18c5682000-10-08 22:20:57 +0000603 if( n>etBUFSIZE ){
604 bufpt = zExtra = sqliteMalloc( n );
drhdaffd0e2001-04-11 14:28:42 +0000605 if( bufpt==0 ) return -1;
drha18c5682000-10-08 22:20:57 +0000606 }else{
607 bufpt = buf;
608 }
chw0cfcf3f2002-06-16 04:55:48 +0000609 j = 0;
drh4794f732004-11-05 17:17:50 +0000610 if( needQuote ) bufpt[j++] = '\'';
chw0cfcf3f2002-06-16 04:55:48 +0000611 for(i=0; (c=arg[i])!=0; i++){
drha18c5682000-10-08 22:20:57 +0000612 bufpt[j++] = c;
613 if( c=='\'' ) bufpt[j++] = c;
614 }
drh4794f732004-11-05 17:17:50 +0000615 if( needQuote ) bufpt[j++] = '\'';
drha18c5682000-10-08 22:20:57 +0000616 bufpt[j] = 0;
617 length = j;
618 if( precision>=0 && precision<length ) length = precision;
619 }
620 break;
drh5f968432004-02-21 19:02:30 +0000621 case etTOKEN: {
622 Token *pToken = va_arg(ap, Token*);
drhad6d9462004-09-19 02:15:24 +0000623 if( pToken && pToken->z ){
624 (*func)(arg, pToken->z, pToken->n);
625 }
drh5f968432004-02-21 19:02:30 +0000626 length = width = 0;
627 break;
628 }
629 case etSRCLIST: {
630 SrcList *pSrc = va_arg(ap, SrcList*);
631 int k = va_arg(ap, int);
632 struct SrcList_item *pItem = &pSrc->a[k];
633 assert( k>=0 && k<pSrc->nSrc );
634 if( pItem->zDatabase && pItem->zDatabase[0] ){
635 (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase));
636 (*func)(arg, ".", 1);
637 }
638 (*func)(arg, pItem->zName, strlen(pItem->zName));
639 length = width = 0;
640 break;
641 }
drha18c5682000-10-08 22:20:57 +0000642 case etERROR:
643 buf[0] = '%';
644 buf[1] = c;
645 errorflag = 0;
646 idx = 1+(c!=0);
647 (*func)(arg,"%",idx);
648 count += idx;
649 if( c==0 ) fmt--;
650 break;
651 }/* 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{
725 pM->zText = pM->xRealloc(pM->zText, pM->nAlloc);
drh6d4abfb2001-10-22 02:58:08 +0000726 }
drha18c5682000-10-08 22:20:57 +0000727 }
728 }
drhe29b1a02004-07-17 21:56:09 +0000729 if( pM->zText ){
730 if( nNewChar>0 ){
731 memcpy(&pM->zText[pM->nChar], zNewText, nNewChar);
732 pM->nChar += nNewChar;
733 }
drha18c5682000-10-08 22:20:57 +0000734 pM->zText[pM->nChar] = 0;
735 }
736}
737
738/*
drh5f968432004-02-21 19:02:30 +0000739** This routine is a wrapper around xprintf() that invokes mout() as
740** the consumer.
drha18c5682000-10-08 22:20:57 +0000741*/
drh5f968432004-02-21 19:02:30 +0000742static char *base_vprintf(
743 void *(*xRealloc)(void*,int), /* Routine to realloc memory. May be NULL */
744 int useInternal, /* Use internal %-conversions if true */
745 char *zInitBuf, /* Initially write here, before mallocing */
746 int nInitBuf, /* Size of zInitBuf[] */
747 const char *zFormat, /* format string */
748 va_list ap /* arguments */
749){
750 struct sgMprintf sM;
751 sM.zBase = sM.zText = zInitBuf;
752 sM.nChar = sM.nTotal = 0;
753 sM.nAlloc = nInitBuf;
754 sM.xRealloc = xRealloc;
755 vxprintf(mout, &sM, useInternal, zFormat, ap);
756 if( xRealloc ){
757 if( sM.zText==sM.zBase ){
758 sM.zText = xRealloc(0, sM.nChar+1);
danielk19778def5ea2004-06-16 10:39:52 +0000759 if( sM.zText ){
760 memcpy(sM.zText, sM.zBase, sM.nChar+1);
761 }
drh5f968432004-02-21 19:02:30 +0000762 }else if( sM.nAlloc>sM.nChar+10 ){
763 sM.zText = xRealloc(sM.zText, sM.nChar+1);
764 }
765 }
766 return sM.zText;
drh483750b2003-01-29 18:46:51 +0000767}
768
769/*
drh5f968432004-02-21 19:02:30 +0000770** Realloc that is a real function, not a macro.
771*/
772static void *printf_realloc(void *old, int size){
773 return sqliteRealloc(old,size);
774}
775
776/*
777** Print into memory obtained from sqliteMalloc(). Use the internal
778** %-conversion extensions.
779*/
danielk19774adee202004-05-08 08:23:19 +0000780char *sqlite3VMPrintf(const char *zFormat, va_list ap){
drh5f968432004-02-21 19:02:30 +0000781 char zBase[1000];
782 return base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
783}
784
785/*
786** Print into memory obtained from sqliteMalloc(). Use the internal
787** %-conversion extensions.
788*/
danielk19774adee202004-05-08 08:23:19 +0000789char *sqlite3MPrintf(const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000790 va_list ap;
791 char *z;
792 char zBase[1000];
793 va_start(ap, zFormat);
794 z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
795 va_end(ap);
796 return z;
797}
798
799/*
800** Print into memory obtained from malloc(). Do not use the internal
801** %-conversion extensions. This routine is for use by external users.
drh483750b2003-01-29 18:46:51 +0000802*/
danielk19776f8a5032004-05-10 10:34:51 +0000803char *sqlite3_mprintf(const char *zFormat, ...){
drha18c5682000-10-08 22:20:57 +0000804 va_list ap;
drh5f968432004-02-21 19:02:30 +0000805 char *z;
drha18c5682000-10-08 22:20:57 +0000806 char zBuf[200];
807
drha18c5682000-10-08 22:20:57 +0000808 va_start(ap,zFormat);
drh5f968432004-02-21 19:02:30 +0000809 z = base_vprintf((void*(*)(void*,int))realloc, 0,
810 zBuf, sizeof(zBuf), zFormat, ap);
drha18c5682000-10-08 22:20:57 +0000811 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000812 return z;
drha18c5682000-10-08 22:20:57 +0000813}
814
danielk19776f8a5032004-05-10 10:34:51 +0000815/* This is the varargs version of sqlite3_mprintf.
drha18c5682000-10-08 22:20:57 +0000816*/
danielk19776f8a5032004-05-10 10:34:51 +0000817char *sqlite3_vmprintf(const char *zFormat, va_list ap){
drha18c5682000-10-08 22:20:57 +0000818 char zBuf[200];
drh5f968432004-02-21 19:02:30 +0000819 return base_vprintf((void*(*)(void*,int))realloc, 0,
820 zBuf, sizeof(zBuf), zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +0000821}
822
823/*
danielk19776f8a5032004-05-10 10:34:51 +0000824** sqlite3_snprintf() works like snprintf() except that it ignores the
drh93a5c6b2003-12-23 02:17:35 +0000825** current locale settings. This is important for SQLite because we
826** are not able to use a "," as the decimal point in place of "." as
827** specified by some locales.
828*/
danielk19776f8a5032004-05-10 10:34:51 +0000829char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000830 char *z;
drh93a5c6b2003-12-23 02:17:35 +0000831 va_list ap;
drh93a5c6b2003-12-23 02:17:35 +0000832
drh93a5c6b2003-12-23 02:17:35 +0000833 va_start(ap,zFormat);
drh5f968432004-02-21 19:02:30 +0000834 z = base_vprintf(0, 0, zBuf, n, zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +0000835 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000836 return z;
drh93a5c6b2003-12-23 02:17:35 +0000837}
838
drh1b743be2004-06-22 22:04:46 +0000839#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
drhe54ca3f2004-06-07 01:52:14 +0000840/*
841** A version of printf() that understands %lld. Used for debugging.
842** The printf() built into some versions of windows does not understand %lld
843** and segfaults if you give it a long long int.
844*/
845void sqlite3DebugPrintf(const char *zFormat, ...){
drh76ff3a02004-09-24 22:32:30 +0000846 extern int getpid(void);
drhe54ca3f2004-06-07 01:52:14 +0000847 va_list ap;
848 char zBuf[500];
849 va_start(ap, zFormat);
850 base_vprintf(0, 0, zBuf, sizeof(zBuf), zFormat, ap);
851 va_end(ap);
drh59c98a62004-09-24 19:39:26 +0000852 fprintf(stdout,"%d: %s", getpid(), zBuf);
drh2ac3ee92004-06-07 16:27:46 +0000853 fflush(stdout);
drhe54ca3f2004-06-07 01:52:14 +0000854}
855#endif