blob: bce1770b7fb42c06f7b55006a8e7f2568276ef3b [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 */
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 */
92 char *charset; /* The character set for conversion */
93 char *prefix; /* Prefix on non-zero values in alt format */
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 */
101
102
103/*
drha18c5682000-10-08 22:20:57 +0000104** The following table is searched linearly, so it is good to put the
105** most frequently used conversion types first.
106*/
107static et_info fmtinfo[] = {
drhe84a3062004-02-02 12:29:25 +0000108 { 'd', 10, 1, etRADIX, "0123456789", 0 },
109 { 's', 0, 0, etSTRING, 0, 0 },
110 { 'z', 0, 2, etDYNSTRING, 0, 0 },
111 { 'q', 0, 0, etSQLESCAPE, 0, 0 },
112 { 'Q', 0, 0, etSQLESCAPE2, 0, 0 },
113 { 'c', 0, 0, etCHARX, 0, 0 },
114 { 'o', 8, 0, etRADIX, "01234567", "0" },
115 { 'u', 10, 0, etRADIX, "0123456789", 0 },
116 { 'x', 16, 0, etRADIX, "0123456789abcdef", "x0" },
117 { 'X', 16, 0, etRADIX, "0123456789ABCDEF", "X0" },
118 { 'f', 0, 1, etFLOAT, 0, 0 },
119 { 'e', 0, 1, etEXP, "e", 0 },
120 { 'E', 0, 1, etEXP, "E", 0 },
121 { 'g', 0, 1, etGENERIC, "e", 0 },
122 { 'G', 0, 1, etGENERIC, "E", 0 },
123 { 'i', 10, 1, etRADIX, "0123456789", 0 },
124 { 'n', 0, 0, etSIZE, 0, 0 },
125 { '%', 0, 0, etPERCENT, 0, 0 },
126 { 'p', 10, 0, etRADIX, "0123456789", 0 },
drh5f968432004-02-21 19:02:30 +0000127 { 'T', 0, 2, etTOKEN, 0, 0 },
128 { 'S', 0, 2, etSRCLIST, 0, 0 },
drha18c5682000-10-08 22:20:57 +0000129};
130#define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0]))
131
132/*
133** If NOFLOATINGPOINT is defined, then none of the floating point
134** conversions will work.
135*/
136#ifndef etNOFLOATINGPOINT
137/*
138** "*val" is a double such that 0.1 <= *val < 10.0
139** Return the ascii code for the leading digit of *val, then
140** multiply "*val" by 10.0 to renormalize.
141**
142** Example:
143** input: *val = 3.14159
144** output: *val = 1.4159 function return = '3'
145**
146** The counter *cnt is incremented each time. After counter exceeds
147** 16 (the number of significant digits in a 64-bit float) '0' is
148** always returned.
149*/
drh384eef32004-01-07 03:04:27 +0000150static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
drha18c5682000-10-08 22:20:57 +0000151 int digit;
drh384eef32004-01-07 03:04:27 +0000152 LONGDOUBLE_TYPE d;
drha18c5682000-10-08 22:20:57 +0000153 if( (*cnt)++ >= 16 ) return '0';
154 digit = (int)*val;
155 d = digit;
156 digit += '0';
157 *val = (*val - d)*10.0;
158 return digit;
159}
160#endif
161
162#define etBUFSIZE 1000 /* Size of the output buffer */
163
164/*
165** The root program. All variations call this core.
166**
167** INPUTS:
168** func This is a pointer to a function taking three arguments
169** 1. A pointer to anything. Same as the "arg" parameter.
170** 2. A pointer to the list of characters to be output
171** (Note, this list is NOT null terminated.)
172** 3. An integer number of characters to be output.
173** (Note: This number might be zero.)
174**
175** arg This is the pointer to anything which will be passed as the
176** first argument to "func". Use it for whatever you like.
177**
178** fmt This is the format string, as in the usual print.
179**
180** ap This is a pointer to a list of arguments. Same as in
181** vfprint.
182**
183** OUTPUTS:
184** The return value is the total number of characters sent to
185** the function "func". Returns -1 on a error.
186**
187** Note that the order in which automatic variables are declared below
188** seems to make a big difference in determining how fast this beast
189** will run.
190*/
191static int vxprintf(
drh5f968432004-02-21 19:02:30 +0000192 void (*func)(void*,const char*,int), /* Consumer of text */
193 void *arg, /* First argument to the consumer */
194 int useExtended, /* Allow extended %-conversions */
195 const char *fmt, /* Format string */
196 va_list ap /* arguments */
drha18c5682000-10-08 22:20:57 +0000197){
drhe84a3062004-02-02 12:29:25 +0000198 int c; /* Next character in the format string */
199 char *bufpt; /* Pointer to the conversion buffer */
200 int precision; /* Precision of the current field */
201 int length; /* Length of the field */
202 int idx; /* A general purpose loop counter */
203 int count; /* Total number of characters output */
204 int width; /* Width of the current field */
205 etByte flag_leftjustify; /* True if "-" flag is present */
206 etByte flag_plussign; /* True if "+" flag is present */
207 etByte flag_blanksign; /* True if " " flag is present */
208 etByte flag_alternateform; /* True if "#" flag is present */
209 etByte flag_zeropad; /* True if field width constant starts with zero */
210 etByte flag_long; /* True if "l" flag is present */
drha34b6762004-05-07 13:30:42 +0000211 etByte flag_longlong; /* True if the "ll" flag is present */
212 UINT64_TYPE longvalue; /* Value for integer types */
drh384eef32004-01-07 03:04:27 +0000213 LONGDOUBLE_TYPE realvalue; /* Value for real types */
drhe84a3062004-02-02 12:29:25 +0000214 et_info *infop; /* Pointer to the appropriate info structure */
215 char buf[etBUFSIZE]; /* Conversion buffer */
216 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
217 etByte errorflag = 0; /* True if an error is encountered */
218 etByte xtype; /* Conversion paradigm */
219 char *zExtra; /* Extra memory used for etTCLESCAPE conversions */
drh905793e2004-02-21 13:31:09 +0000220 static char spaces[] = " ";
drha18c5682000-10-08 22:20:57 +0000221#define etSPACESIZE (sizeof(spaces)-1)
222#ifndef etNOFLOATINGPOINT
drhe84a3062004-02-02 12:29:25 +0000223 int exp; /* exponent of real numbers */
224 double rounder; /* Used for rounding floating point values */
225 etByte flag_dp; /* True if decimal point should be shown */
226 etByte flag_rtz; /* True if trailing zeros should be removed */
227 etByte flag_exp; /* True to force display of the exponent */
228 int nsd; /* Number of significant digits returned */
drha18c5682000-10-08 22:20:57 +0000229#endif
230
drha18c5682000-10-08 22:20:57 +0000231 count = length = 0;
232 bufpt = 0;
233 for(; (c=(*fmt))!=0; ++fmt){
234 if( c!='%' ){
drhe84a3062004-02-02 12:29:25 +0000235 int amt;
drha18c5682000-10-08 22:20:57 +0000236 bufpt = (char *)fmt;
237 amt = 1;
238 while( (c=(*++fmt))!='%' && c!=0 ) amt++;
239 (*func)(arg,bufpt,amt);
240 count += amt;
241 if( c==0 ) break;
242 }
243 if( (c=(*++fmt))==0 ){
244 errorflag = 1;
245 (*func)(arg,"%",1);
246 count++;
247 break;
248 }
249 /* Find out what flags are present */
250 flag_leftjustify = flag_plussign = flag_blanksign =
drhe84a3062004-02-02 12:29:25 +0000251 flag_alternateform = flag_zeropad = 0;
drha18c5682000-10-08 22:20:57 +0000252 do{
253 switch( c ){
254 case '-': flag_leftjustify = 1; c = 0; break;
255 case '+': flag_plussign = 1; c = 0; break;
256 case ' ': flag_blanksign = 1; c = 0; break;
257 case '#': flag_alternateform = 1; c = 0; break;
258 case '0': flag_zeropad = 1; c = 0; break;
drha18c5682000-10-08 22:20:57 +0000259 default: break;
260 }
261 }while( c==0 && (c=(*++fmt))!=0 );
drha18c5682000-10-08 22:20:57 +0000262 /* Get the field width */
263 width = 0;
264 if( c=='*' ){
265 width = va_arg(ap,int);
266 if( width<0 ){
267 flag_leftjustify = 1;
268 width = -width;
269 }
270 c = *++fmt;
271 }else{
drh17a68932001-01-31 13:28:08 +0000272 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000273 width = width*10 + c - '0';
274 c = *++fmt;
275 }
276 }
277 if( width > etBUFSIZE-10 ){
278 width = etBUFSIZE-10;
279 }
280 /* Get the precision */
281 if( c=='.' ){
282 precision = 0;
283 c = *++fmt;
284 if( c=='*' ){
285 precision = va_arg(ap,int);
drha18c5682000-10-08 22:20:57 +0000286 if( precision<0 ) precision = -precision;
drha18c5682000-10-08 22:20:57 +0000287 c = *++fmt;
288 }else{
drh17a68932001-01-31 13:28:08 +0000289 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000290 precision = precision*10 + c - '0';
291 c = *++fmt;
292 }
293 }
294 /* Limit the precision to prevent overflowing buf[] during conversion */
295 if( precision>etBUFSIZE-40 ) precision = etBUFSIZE-40;
296 }else{
297 precision = -1;
298 }
299 /* Get the conversion type modifier */
300 if( c=='l' ){
301 flag_long = 1;
302 c = *++fmt;
drha34b6762004-05-07 13:30:42 +0000303 if( c=='l' ){
304 flag_longlong = 1;
305 c = *++fmt;
306 }else{
307 flag_longlong = 0;
308 }
drha18c5682000-10-08 22:20:57 +0000309 }else{
drha34b6762004-05-07 13:30:42 +0000310 flag_long = flag_longlong = 0;
drha18c5682000-10-08 22:20:57 +0000311 }
312 /* Fetch the info entry for the field */
313 infop = 0;
drhe84a3062004-02-02 12:29:25 +0000314 xtype = etERROR;
drha18c5682000-10-08 22:20:57 +0000315 for(idx=0; idx<etNINFO; idx++){
316 if( c==fmtinfo[idx].fmttype ){
317 infop = &fmtinfo[idx];
drh5f968432004-02-21 19:02:30 +0000318 if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
319 xtype = infop->type;
320 }
drha18c5682000-10-08 22:20:57 +0000321 break;
322 }
323 }
drha18c5682000-10-08 22:20:57 +0000324 zExtra = 0;
325
326 /*
327 ** At this point, variables are initialized as follows:
328 **
329 ** flag_alternateform TRUE if a '#' is present.
330 ** flag_plussign TRUE if a '+' is present.
331 ** flag_leftjustify TRUE if a '-' is present or if the
332 ** field width was negative.
333 ** flag_zeropad TRUE if the width began with 0.
334 ** flag_long TRUE if the letter 'l' (ell) prefixed
335 ** the conversion character.
drha34b6762004-05-07 13:30:42 +0000336 ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed
337 ** the conversion character.
drha18c5682000-10-08 22:20:57 +0000338 ** flag_blanksign TRUE if a ' ' is present.
339 ** width The specified field width. This is
340 ** always non-negative. Zero is the default.
341 ** precision The specified precision. The default
342 ** is -1.
343 ** xtype The class of the conversion.
344 ** infop Pointer to the appropriate info struct.
345 */
346 switch( xtype ){
drha18c5682000-10-08 22:20:57 +0000347 case etRADIX:
drha34b6762004-05-07 13:30:42 +0000348 if( flag_longlong ) longvalue = va_arg(ap,INT64_TYPE);
danielk19774adee202004-05-08 08:23:19 +0000349 else if( flag_long ) longvalue = va_arg(ap,long int);
drha34b6762004-05-07 13:30:42 +0000350 else longvalue = va_arg(ap,int);
drh5f968432004-02-21 19:02:30 +0000351#if 1
drha18c5682000-10-08 22:20:57 +0000352 /* For the format %#x, the value zero is printed "0" not "0x0".
353 ** I think this is stupid. */
354 if( longvalue==0 ) flag_alternateform = 0;
355#else
356 /* More sensible: turn off the prefix for octal (to prevent "00"),
357 ** but leave the prefix for hex. */
358 if( longvalue==0 && infop->base==8 ) flag_alternateform = 0;
359#endif
drhe84a3062004-02-02 12:29:25 +0000360 if( infop->flags & FLAG_SIGNED ){
drha18c5682000-10-08 22:20:57 +0000361 if( *(long*)&longvalue<0 ){
362 longvalue = -*(long*)&longvalue;
363 prefix = '-';
364 }else if( flag_plussign ) prefix = '+';
365 else if( flag_blanksign ) prefix = ' ';
366 else prefix = 0;
367 }else prefix = 0;
368 if( flag_zeropad && precision<width-(prefix!=0) ){
369 precision = width-(prefix!=0);
drh9adf9ac2002-05-15 11:44:13 +0000370 }
drh3039c0a2004-02-29 00:11:30 +0000371 bufpt = &buf[etBUFSIZE-1];
drha18c5682000-10-08 22:20:57 +0000372 {
373 register char *cset; /* Use registers for speed */
374 register int base;
375 cset = infop->charset;
376 base = infop->base;
377 do{ /* Convert to ascii */
378 *(--bufpt) = cset[longvalue%base];
379 longvalue = longvalue/base;
380 }while( longvalue>0 );
drh9adf9ac2002-05-15 11:44:13 +0000381 }
drh3039c0a2004-02-29 00:11:30 +0000382 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000383 for(idx=precision-length; idx>0; idx--){
384 *(--bufpt) = '0'; /* Zero pad */
drh9adf9ac2002-05-15 11:44:13 +0000385 }
drha18c5682000-10-08 22:20:57 +0000386 if( prefix ) *(--bufpt) = prefix; /* Add sign */
387 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
388 char *pre, x;
389 pre = infop->prefix;
390 if( *bufpt!=pre[0] ){
391 for(pre=infop->prefix; (x=(*pre))!=0; pre++) *(--bufpt) = x;
drh9adf9ac2002-05-15 11:44:13 +0000392 }
drha18c5682000-10-08 22:20:57 +0000393 }
drh3039c0a2004-02-29 00:11:30 +0000394 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000395 break;
396 case etFLOAT:
397 case etEXP:
398 case etGENERIC:
399 realvalue = va_arg(ap,double);
400#ifndef etNOFLOATINGPOINT
401 if( precision<0 ) precision = 6; /* Set default precision */
402 if( precision>etBUFSIZE-10 ) precision = etBUFSIZE-10;
403 if( realvalue<0.0 ){
404 realvalue = -realvalue;
405 prefix = '-';
drh9adf9ac2002-05-15 11:44:13 +0000406 }else{
drha18c5682000-10-08 22:20:57 +0000407 if( flag_plussign ) prefix = '+';
408 else if( flag_blanksign ) prefix = ' ';
409 else prefix = 0;
drh9adf9ac2002-05-15 11:44:13 +0000410 }
drha18c5682000-10-08 22:20:57 +0000411 if( infop->type==etGENERIC && precision>0 ) precision--;
412 rounder = 0.0;
drh5f968432004-02-21 19:02:30 +0000413#if 0
drha18c5682000-10-08 22:20:57 +0000414 /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */
415 for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
416#else
417 /* It makes more sense to use 0.5 */
418 for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1);
419#endif
420 if( infop->type==etFLOAT ) realvalue += rounder;
421 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
422 exp = 0;
423 if( realvalue>0.0 ){
drhb621c232004-02-21 19:41:04 +0000424 while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
425 while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
426 while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; }
427 while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; }
428 if( exp>350 || exp<-350 ){
drha18c5682000-10-08 22:20:57 +0000429 bufpt = "NaN";
430 length = 3;
431 break;
432 }
drh9adf9ac2002-05-15 11:44:13 +0000433 }
drha18c5682000-10-08 22:20:57 +0000434 bufpt = buf;
435 /*
436 ** If the field type is etGENERIC, then convert to either etEXP
437 ** or etFLOAT, as appropriate.
438 */
439 flag_exp = xtype==etEXP;
440 if( xtype!=etFLOAT ){
441 realvalue += rounder;
442 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
443 }
444 if( xtype==etGENERIC ){
445 flag_rtz = !flag_alternateform;
446 if( exp<-4 || exp>precision ){
447 xtype = etEXP;
448 }else{
449 precision = precision - exp;
450 xtype = etFLOAT;
451 }
drh9adf9ac2002-05-15 11:44:13 +0000452 }else{
drha18c5682000-10-08 22:20:57 +0000453 flag_rtz = 0;
drh9adf9ac2002-05-15 11:44:13 +0000454 }
drha18c5682000-10-08 22:20:57 +0000455 /*
456 ** The "exp+precision" test causes output to be of type etEXP if
457 ** the precision is too large to fit in buf[].
458 */
459 nsd = 0;
460 if( xtype==etFLOAT && exp+precision<etBUFSIZE-30 ){
461 flag_dp = (precision>0 || flag_alternateform);
462 if( prefix ) *(bufpt++) = prefix; /* Sign */
463 if( exp<0 ) *(bufpt++) = '0'; /* Digits before "." */
464 else for(; exp>=0; exp--) *(bufpt++) = et_getdigit(&realvalue,&nsd);
465 if( flag_dp ) *(bufpt++) = '.'; /* The decimal point */
466 for(exp++; exp<0 && precision>0; precision--, exp++){
467 *(bufpt++) = '0';
468 }
469 while( (precision--)>0 ) *(bufpt++) = et_getdigit(&realvalue,&nsd);
470 *(bufpt--) = 0; /* Null terminate */
471 if( flag_rtz && flag_dp ){ /* Remove trailing zeros and "." */
472 while( bufpt>=buf && *bufpt=='0' ) *(bufpt--) = 0;
473 if( bufpt>=buf && *bufpt=='.' ) *(bufpt--) = 0;
474 }
475 bufpt++; /* point to next free slot */
drh9adf9ac2002-05-15 11:44:13 +0000476 }else{ /* etEXP or etGENERIC */
drha18c5682000-10-08 22:20:57 +0000477 flag_dp = (precision>0 || flag_alternateform);
478 if( prefix ) *(bufpt++) = prefix; /* Sign */
479 *(bufpt++) = et_getdigit(&realvalue,&nsd); /* First digit */
480 if( flag_dp ) *(bufpt++) = '.'; /* Decimal point */
481 while( (precision--)>0 ) *(bufpt++) = et_getdigit(&realvalue,&nsd);
482 bufpt--; /* point to last digit */
483 if( flag_rtz && flag_dp ){ /* Remove tail zeros */
484 while( bufpt>=buf && *bufpt=='0' ) *(bufpt--) = 0;
485 if( bufpt>=buf && *bufpt=='.' ) *(bufpt--) = 0;
486 }
487 bufpt++; /* point to next free slot */
488 if( exp || flag_exp ){
489 *(bufpt++) = infop->charset[0];
490 if( exp<0 ){ *(bufpt++) = '-'; exp = -exp; } /* sign of exp */
491 else { *(bufpt++) = '+'; }
492 if( exp>=100 ){
493 *(bufpt++) = (exp/100)+'0'; /* 100's digit */
494 exp %= 100;
drh9adf9ac2002-05-15 11:44:13 +0000495 }
drha18c5682000-10-08 22:20:57 +0000496 *(bufpt++) = exp/10+'0'; /* 10's digit */
497 *(bufpt++) = exp%10+'0'; /* 1's digit */
498 }
drh9adf9ac2002-05-15 11:44:13 +0000499 }
drha18c5682000-10-08 22:20:57 +0000500 /* The converted number is in buf[] and zero terminated. Output it.
501 ** Note that the number is in the usual order, not reversed as with
502 ** integer conversions. */
drh94ce4c12003-06-07 11:29:50 +0000503 length = bufpt-buf;
drha18c5682000-10-08 22:20:57 +0000504 bufpt = buf;
505
506 /* Special case: Add leading zeros if the flag_zeropad flag is
507 ** set and we are not left justified */
508 if( flag_zeropad && !flag_leftjustify && length < width){
509 int i;
510 int nPad = width - length;
511 for(i=width; i>=nPad; i--){
512 bufpt[i] = bufpt[i-nPad];
513 }
514 i = prefix!=0;
515 while( nPad-- ) bufpt[i++] = '0';
516 length = width;
517 }
518#endif
519 break;
520 case etSIZE:
521 *(va_arg(ap,int*)) = count;
522 length = width = 0;
523 break;
524 case etPERCENT:
525 buf[0] = '%';
526 bufpt = buf;
527 length = 1;
528 break;
529 case etCHARLIT:
530 case etCHARX:
531 c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt);
532 if( precision>=0 ){
533 for(idx=1; idx<precision; idx++) buf[idx] = c;
534 length = precision;
drh9adf9ac2002-05-15 11:44:13 +0000535 }else{
drha18c5682000-10-08 22:20:57 +0000536 length =1;
drh9adf9ac2002-05-15 11:44:13 +0000537 }
drha18c5682000-10-08 22:20:57 +0000538 bufpt = buf;
539 break;
540 case etSTRING:
drhd93d8a82003-06-16 03:08:18 +0000541 case etDYNSTRING:
drhcb485882002-08-15 13:50:48 +0000542 bufpt = va_arg(ap,char*);
drhd93d8a82003-06-16 03:08:18 +0000543 if( bufpt==0 ){
544 bufpt = "";
545 }else if( xtype==etDYNSTRING ){
546 zExtra = bufpt;
547 }
drha18c5682000-10-08 22:20:57 +0000548 length = strlen(bufpt);
549 if( precision>=0 && precision<length ) length = precision;
550 break;
551 case etSQLESCAPE:
chw0cfcf3f2002-06-16 04:55:48 +0000552 case etSQLESCAPE2:
drha18c5682000-10-08 22:20:57 +0000553 {
chw0cfcf3f2002-06-16 04:55:48 +0000554 int i, j, n, c, isnull;
drha18c5682000-10-08 22:20:57 +0000555 char *arg = va_arg(ap,char*);
chw0cfcf3f2002-06-16 04:55:48 +0000556 isnull = arg==0;
557 if( isnull ) arg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
drha18c5682000-10-08 22:20:57 +0000558 for(i=n=0; (c=arg[i])!=0; i++){
559 if( c=='\'' ) n++;
560 }
chw0cfcf3f2002-06-16 04:55:48 +0000561 n += i + 1 + ((!isnull && xtype==etSQLESCAPE2) ? 2 : 0);
drha18c5682000-10-08 22:20:57 +0000562 if( n>etBUFSIZE ){
563 bufpt = zExtra = sqliteMalloc( n );
drhdaffd0e2001-04-11 14:28:42 +0000564 if( bufpt==0 ) return -1;
drha18c5682000-10-08 22:20:57 +0000565 }else{
566 bufpt = buf;
567 }
chw0cfcf3f2002-06-16 04:55:48 +0000568 j = 0;
569 if( !isnull && xtype==etSQLESCAPE2 ) bufpt[j++] = '\'';
570 for(i=0; (c=arg[i])!=0; i++){
drha18c5682000-10-08 22:20:57 +0000571 bufpt[j++] = c;
572 if( c=='\'' ) bufpt[j++] = c;
573 }
chw0cfcf3f2002-06-16 04:55:48 +0000574 if( !isnull && xtype==etSQLESCAPE2 ) bufpt[j++] = '\'';
drha18c5682000-10-08 22:20:57 +0000575 bufpt[j] = 0;
576 length = j;
577 if( precision>=0 && precision<length ) length = precision;
578 }
579 break;
drh5f968432004-02-21 19:02:30 +0000580 case etTOKEN: {
581 Token *pToken = va_arg(ap, Token*);
582 (*func)(arg, pToken->z, pToken->n);
583 length = width = 0;
584 break;
585 }
586 case etSRCLIST: {
587 SrcList *pSrc = va_arg(ap, SrcList*);
588 int k = va_arg(ap, int);
589 struct SrcList_item *pItem = &pSrc->a[k];
590 assert( k>=0 && k<pSrc->nSrc );
591 if( pItem->zDatabase && pItem->zDatabase[0] ){
592 (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase));
593 (*func)(arg, ".", 1);
594 }
595 (*func)(arg, pItem->zName, strlen(pItem->zName));
596 length = width = 0;
597 break;
598 }
drha18c5682000-10-08 22:20:57 +0000599 case etERROR:
600 buf[0] = '%';
601 buf[1] = c;
602 errorflag = 0;
603 idx = 1+(c!=0);
604 (*func)(arg,"%",idx);
605 count += idx;
606 if( c==0 ) fmt--;
607 break;
608 }/* End switch over the format type */
609 /*
610 ** The text of the conversion is pointed to by "bufpt" and is
611 ** "length" characters long. The field width is "width". Do
612 ** the output.
613 */
614 if( !flag_leftjustify ){
615 register int nspace;
616 nspace = width-length;
617 if( nspace>0 ){
drha18c5682000-10-08 22:20:57 +0000618 count += nspace;
619 while( nspace>=etSPACESIZE ){
620 (*func)(arg,spaces,etSPACESIZE);
621 nspace -= etSPACESIZE;
622 }
623 if( nspace>0 ) (*func)(arg,spaces,nspace);
624 }
625 }
626 if( length>0 ){
627 (*func)(arg,bufpt,length);
628 count += length;
629 }
630 if( flag_leftjustify ){
631 register int nspace;
632 nspace = width-length;
633 if( nspace>0 ){
634 count += nspace;
635 while( nspace>=etSPACESIZE ){
636 (*func)(arg,spaces,etSPACESIZE);
637 nspace -= etSPACESIZE;
638 }
639 if( nspace>0 ) (*func)(arg,spaces,nspace);
640 }
641 }
642 if( zExtra ){
drh5f968432004-02-21 19:02:30 +0000643 sqliteFree(zExtra);
drha18c5682000-10-08 22:20:57 +0000644 }
645 }/* End for loop over the format string */
646 return errorflag ? -1 : count;
647} /* End of function */
648
649
650/* This structure is used to store state information about the
651** write to memory that is currently in progress.
652*/
653struct sgMprintf {
654 char *zBase; /* A base allocation */
655 char *zText; /* The string collected so far */
656 int nChar; /* Length of the string so far */
drh5f968432004-02-21 19:02:30 +0000657 int nTotal; /* Output size if unconstrained */
drha18c5682000-10-08 22:20:57 +0000658 int nAlloc; /* Amount of space allocated in zText */
drh5f968432004-02-21 19:02:30 +0000659 void *(*xRealloc)(void*,int); /* Function used to realloc memory */
drha18c5682000-10-08 22:20:57 +0000660};
661
662/*
663** This function implements the callback from vxprintf.
664**
665** This routine add nNewChar characters of text in zNewText to
666** the sgMprintf structure pointed to by "arg".
667*/
drh5f968432004-02-21 19:02:30 +0000668static void mout(void *arg, const char *zNewText, int nNewChar){
drha18c5682000-10-08 22:20:57 +0000669 struct sgMprintf *pM = (struct sgMprintf*)arg;
drh5f968432004-02-21 19:02:30 +0000670 pM->nTotal += nNewChar;
drha18c5682000-10-08 22:20:57 +0000671 if( pM->nChar + nNewChar + 1 > pM->nAlloc ){
drh5f968432004-02-21 19:02:30 +0000672 if( pM->xRealloc==0 ){
673 nNewChar = pM->nAlloc - pM->nChar - 1;
drha18c5682000-10-08 22:20:57 +0000674 }else{
drh5f968432004-02-21 19:02:30 +0000675 pM->nAlloc = pM->nChar + nNewChar*2 + 1;
676 if( pM->zText==pM->zBase ){
677 pM->zText = pM->xRealloc(0, pM->nAlloc);
678 if( pM->zText && pM->nChar ){
679 memcpy(pM->zText, pM->zBase, pM->nChar);
680 }
681 }else{
682 pM->zText = pM->xRealloc(pM->zText, pM->nAlloc);
drh6d4abfb2001-10-22 02:58:08 +0000683 }
drha18c5682000-10-08 22:20:57 +0000684 }
685 }
drh5f968432004-02-21 19:02:30 +0000686 if( pM->zText && nNewChar>0 ){
drha18c5682000-10-08 22:20:57 +0000687 memcpy(&pM->zText[pM->nChar], zNewText, nNewChar);
688 pM->nChar += nNewChar;
689 pM->zText[pM->nChar] = 0;
690 }
691}
692
693/*
drh5f968432004-02-21 19:02:30 +0000694** This routine is a wrapper around xprintf() that invokes mout() as
695** the consumer.
drha18c5682000-10-08 22:20:57 +0000696*/
drh5f968432004-02-21 19:02:30 +0000697static char *base_vprintf(
698 void *(*xRealloc)(void*,int), /* Routine to realloc memory. May be NULL */
699 int useInternal, /* Use internal %-conversions if true */
700 char *zInitBuf, /* Initially write here, before mallocing */
701 int nInitBuf, /* Size of zInitBuf[] */
702 const char *zFormat, /* format string */
703 va_list ap /* arguments */
704){
705 struct sgMprintf sM;
706 sM.zBase = sM.zText = zInitBuf;
707 sM.nChar = sM.nTotal = 0;
708 sM.nAlloc = nInitBuf;
709 sM.xRealloc = xRealloc;
710 vxprintf(mout, &sM, useInternal, zFormat, ap);
711 if( xRealloc ){
712 if( sM.zText==sM.zBase ){
713 sM.zText = xRealloc(0, sM.nChar+1);
714 memcpy(sM.zText, sM.zBase, sM.nChar+1);
715 }else if( sM.nAlloc>sM.nChar+10 ){
716 sM.zText = xRealloc(sM.zText, sM.nChar+1);
717 }
718 }
719 return sM.zText;
drh483750b2003-01-29 18:46:51 +0000720}
721
722/*
drh5f968432004-02-21 19:02:30 +0000723** Realloc that is a real function, not a macro.
724*/
725static void *printf_realloc(void *old, int size){
726 return sqliteRealloc(old,size);
727}
728
729/*
730** Print into memory obtained from sqliteMalloc(). Use the internal
731** %-conversion extensions.
732*/
danielk19774adee202004-05-08 08:23:19 +0000733char *sqlite3VMPrintf(const char *zFormat, va_list ap){
drh5f968432004-02-21 19:02:30 +0000734 char zBase[1000];
735 return base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
736}
737
738/*
739** Print into memory obtained from sqliteMalloc(). Use the internal
740** %-conversion extensions.
741*/
danielk19774adee202004-05-08 08:23:19 +0000742char *sqlite3MPrintf(const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000743 va_list ap;
744 char *z;
745 char zBase[1000];
746 va_start(ap, zFormat);
747 z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
748 va_end(ap);
749 return z;
750}
751
752/*
753** Print into memory obtained from malloc(). Do not use the internal
754** %-conversion extensions. This routine is for use by external users.
drh483750b2003-01-29 18:46:51 +0000755*/
danielk19776f8a5032004-05-10 10:34:51 +0000756char *sqlite3_mprintf(const char *zFormat, ...){
drha18c5682000-10-08 22:20:57 +0000757 va_list ap;
drh5f968432004-02-21 19:02:30 +0000758 char *z;
drha18c5682000-10-08 22:20:57 +0000759 char zBuf[200];
760
drha18c5682000-10-08 22:20:57 +0000761 va_start(ap,zFormat);
drh5f968432004-02-21 19:02:30 +0000762 z = base_vprintf((void*(*)(void*,int))realloc, 0,
763 zBuf, sizeof(zBuf), zFormat, ap);
drha18c5682000-10-08 22:20:57 +0000764 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000765 return z;
drha18c5682000-10-08 22:20:57 +0000766}
767
danielk19776f8a5032004-05-10 10:34:51 +0000768/* This is the varargs version of sqlite3_mprintf.
drha18c5682000-10-08 22:20:57 +0000769*/
danielk19776f8a5032004-05-10 10:34:51 +0000770char *sqlite3_vmprintf(const char *zFormat, va_list ap){
drha18c5682000-10-08 22:20:57 +0000771 char zBuf[200];
drh5f968432004-02-21 19:02:30 +0000772 return base_vprintf((void*(*)(void*,int))realloc, 0,
773 zBuf, sizeof(zBuf), zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +0000774}
775
776/*
danielk19776f8a5032004-05-10 10:34:51 +0000777** sqlite3_snprintf() works like snprintf() except that it ignores the
drh93a5c6b2003-12-23 02:17:35 +0000778** current locale settings. This is important for SQLite because we
779** are not able to use a "," as the decimal point in place of "." as
780** specified by some locales.
781*/
danielk19776f8a5032004-05-10 10:34:51 +0000782char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000783 char *z;
drh93a5c6b2003-12-23 02:17:35 +0000784 va_list ap;
drh93a5c6b2003-12-23 02:17:35 +0000785
drh93a5c6b2003-12-23 02:17:35 +0000786 va_start(ap,zFormat);
drh5f968432004-02-21 19:02:30 +0000787 z = base_vprintf(0, 0, zBuf, n, zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +0000788 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000789 return z;
drh93a5c6b2003-12-23 02:17:35 +0000790}
791
drha18c5682000-10-08 22:20:57 +0000792/*
793** The following four routines implement the varargs versions of the
danielk19776f8a5032004-05-10 10:34:51 +0000794** sqlite3_exec() and sqlite3_get_table() interfaces. See the sqlite.h
drha18c5682000-10-08 22:20:57 +0000795** header files for a more detailed description of how these interfaces
796** work.
797**
798** These routines are all just simple wrappers.
799*/
danielk19776f8a5032004-05-10 10:34:51 +0000800int sqlite3_exec_printf(
drha18c5682000-10-08 22:20:57 +0000801 sqlite *db, /* An open database */
drh9f71c2e2001-11-03 23:57:09 +0000802 const char *sqlFormat, /* printf-style format string for the SQL */
drha18c5682000-10-08 22:20:57 +0000803 sqlite_callback xCallback, /* Callback function */
804 void *pArg, /* 1st argument to callback function */
805 char **errmsg, /* Error msg written here */
806 ... /* Arguments to the format string. */
807){
808 va_list ap;
809 int rc;
810
811 va_start(ap, errmsg);
danielk19776f8a5032004-05-10 10:34:51 +0000812 rc = sqlite3_exec_vprintf(db, sqlFormat, xCallback, pArg, errmsg, ap);
drha18c5682000-10-08 22:20:57 +0000813 va_end(ap);
814 return rc;
815}
danielk19776f8a5032004-05-10 10:34:51 +0000816int sqlite3_exec_vprintf(
drha18c5682000-10-08 22:20:57 +0000817 sqlite *db, /* An open database */
drh9f71c2e2001-11-03 23:57:09 +0000818 const char *sqlFormat, /* printf-style format string for the SQL */
drha18c5682000-10-08 22:20:57 +0000819 sqlite_callback xCallback, /* Callback function */
820 void *pArg, /* 1st argument to callback function */
821 char **errmsg, /* Error msg written here */
822 va_list ap /* Arguments to the format string. */
823){
824 char *zSql;
825 int rc;
826
danielk19776f8a5032004-05-10 10:34:51 +0000827 zSql = sqlite3_vmprintf(sqlFormat, ap);
828 rc = sqlite3_exec(db, zSql, xCallback, pArg, errmsg);
drhfa173a72002-07-10 21:26:00 +0000829 free(zSql);
drha18c5682000-10-08 22:20:57 +0000830 return rc;
831}
danielk19776f8a5032004-05-10 10:34:51 +0000832int sqlite3_get_table_printf(
drha18c5682000-10-08 22:20:57 +0000833 sqlite *db, /* An open database */
drh9f71c2e2001-11-03 23:57:09 +0000834 const char *sqlFormat, /* printf-style format string for the SQL */
drha18c5682000-10-08 22:20:57 +0000835 char ***resultp, /* Result written to a char *[] that this points to */
836 int *nrow, /* Number of result rows written here */
837 int *ncol, /* Number of result columns written here */
838 char **errmsg, /* Error msg written here */
839 ... /* Arguments to the format string */
840){
841 va_list ap;
842 int rc;
843
844 va_start(ap, errmsg);
danielk19776f8a5032004-05-10 10:34:51 +0000845 rc = sqlite3_get_table_vprintf(db, sqlFormat, resultp, nrow, ncol, errmsg, ap);
drha18c5682000-10-08 22:20:57 +0000846 va_end(ap);
847 return rc;
848}
danielk19776f8a5032004-05-10 10:34:51 +0000849int sqlite3_get_table_vprintf(
drha18c5682000-10-08 22:20:57 +0000850 sqlite *db, /* An open database */
drh9f71c2e2001-11-03 23:57:09 +0000851 const char *sqlFormat, /* printf-style format string for the SQL */
drha18c5682000-10-08 22:20:57 +0000852 char ***resultp, /* Result written to a char *[] that this points to */
853 int *nrow, /* Number of result rows written here */
854 int *ncolumn, /* Number of result columns written here */
855 char **errmsg, /* Error msg written here */
856 va_list ap /* Arguments to the format string */
857){
858 char *zSql;
859 int rc;
860
danielk19776f8a5032004-05-10 10:34:51 +0000861 zSql = sqlite3_vmprintf(sqlFormat, ap);
862 rc = sqlite3_get_table(db, zSql, resultp, nrow, ncolumn, errmsg);
drhfa173a72002-07-10 21:26:00 +0000863 free(zSql);
drha18c5682000-10-08 22:20:57 +0000864 return rc;
865}
danielk19774adee202004-05-08 08:23:19 +0000866
867
868