blob: e1ae39df7fb49b93ec221ae8391063cfc382d512 [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
4** completeness. They are slightly out-of-date.
5**
6** The following modules is an enhanced replacement for the "printf" programs
7** found in the standard library. The following enhancements are
8** supported:
9**
10** + Additional functions. The standard set of "printf" functions
11** includes printf, fprintf, sprintf, vprintf, vfprintf, and
12** vsprintf. This module adds the following:
13**
14** * snprintf -- Works like sprintf, but has an extra argument
15** which is the size of the buffer written to.
16**
17** * mprintf -- Similar to sprintf. Writes output to memory
18** obtained from malloc.
19**
20** * xprintf -- Calls a function to dispose of output.
21**
22** * nprintf -- No output, but returns the number of characters
23** that would have been output by printf.
24**
25** * A v- version (ex: vsnprintf) of every function is also
26** supplied.
27**
28** + A few extensions to the formatting notation are supported:
29**
30** * The "=" flag (similar to "-") causes the output to be
31** be centered in the appropriately sized field.
32**
33** * The %b field outputs an integer in binary notation.
34**
35** * The %c field now accepts a precision. The character output
36** is repeated by the number of times the precision specifies.
37**
38** * The %' field works like %c, but takes as its character the
39** next character of the format string, instead of the next
40** argument. For example, printf("%.78'-") prints 78 minus
41** signs, the same as printf("%.78c",'-').
42**
43** + When compiled using GCC on a SPARC, this version of printf is
44** faster than the library printf for SUN OS 4.1.
45**
46** + All functions are fully reentrant.
47**
48*/
49#include "sqliteInt.h"
drh7c68d602000-10-11 19:28:51 +000050
drha18c5682000-10-08 22:20:57 +000051/*
52** Undefine COMPATIBILITY to make some slight changes in the way things
53** work. I think the changes are an improvement, but they are not
54** backwards compatible.
55*/
56/* #define COMPATIBILITY / * Compatible with SUN OS 4.1 */
57
58/*
59** Conversion types fall into various categories as defined by the
60** following enumeration.
61*/
62enum et_type { /* The type of the format field */
63 etRADIX, /* Integer types. %d, %x, %o, and so forth */
64 etFLOAT, /* Floating point. %f */
65 etEXP, /* Exponentional notation. %e and %E */
66 etGENERIC, /* Floating or exponential, depending on exponent. %g */
67 etSIZE, /* Return number of characters processed so far. %n */
68 etSTRING, /* Strings. %s */
69 etPERCENT, /* Percent symbol. %% */
70 etCHARX, /* Characters. %c */
71 etERROR, /* Used to indicate no such conversion type */
72/* The rest are extensions, not normally found in printf() */
73 etCHARLIT, /* Literal characters. %' */
74 etSQLESCAPE, /* Strings with '\'' doubled. %q */
chw0cfcf3f2002-06-16 04:55:48 +000075 etSQLESCAPE2, /* Strings with '\'' doubled and enclosed in '',
76 NULL pointers replaced by SQL NULL. %Q */
drha18c5682000-10-08 22:20:57 +000077 etORDINAL /* 1st, 2nd, 3rd and so forth */
78};
79
80/*
81** Each builtin conversion character (ex: the 'd' in "%d") is described
82** by an instance of the following structure
83*/
84typedef struct et_info { /* Information about each format field */
85 int fmttype; /* The format field code letter */
86 int base; /* The base for radix conversion */
87 char *charset; /* The character set for conversion */
88 int flag_signed; /* Is the quantity signed? */
89 char *prefix; /* Prefix on non-zero values in alt format */
90 enum et_type type; /* Conversion paradigm */
91} et_info;
92
93/*
94** The following table is searched linearly, so it is good to put the
95** most frequently used conversion types first.
96*/
97static et_info fmtinfo[] = {
98 { 'd', 10, "0123456789", 1, 0, etRADIX, },
99 { 's', 0, 0, 0, 0, etSTRING, },
100 { 'q', 0, 0, 0, 0, etSQLESCAPE, },
chw0cfcf3f2002-06-16 04:55:48 +0000101 { 'Q', 0, 0, 0, 0, etSQLESCAPE2, },
drha18c5682000-10-08 22:20:57 +0000102 { 'c', 0, 0, 0, 0, etCHARX, },
103 { 'o', 8, "01234567", 0, "0", etRADIX, },
104 { 'u', 10, "0123456789", 0, 0, etRADIX, },
105 { 'x', 16, "0123456789abcdef", 0, "x0", etRADIX, },
106 { 'X', 16, "0123456789ABCDEF", 0, "X0", etRADIX, },
107 { 'r', 10, "0123456789", 0, 0, etORDINAL, },
108 { 'f', 0, 0, 1, 0, etFLOAT, },
109 { 'e', 0, "e", 1, 0, etEXP, },
110 { 'E', 0, "E", 1, 0, etEXP, },
111 { 'g', 0, "e", 1, 0, etGENERIC, },
112 { 'G', 0, "E", 1, 0, etGENERIC, },
113 { 'i', 10, "0123456789", 1, 0, etRADIX, },
114 { 'n', 0, 0, 0, 0, etSIZE, },
115 { '%', 0, 0, 0, 0, etPERCENT, },
116 { 'b', 2, "01", 0, "b0", etRADIX, }, /* Binary */
117 { 'p', 10, "0123456789", 0, 0, etRADIX, }, /* Pointers */
118 { '\'', 0, 0, 0, 0, etCHARLIT, }, /* Literal char */
119};
120#define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0]))
121
122/*
123** If NOFLOATINGPOINT is defined, then none of the floating point
124** conversions will work.
125*/
126#ifndef etNOFLOATINGPOINT
127/*
128** "*val" is a double such that 0.1 <= *val < 10.0
129** Return the ascii code for the leading digit of *val, then
130** multiply "*val" by 10.0 to renormalize.
131**
132** Example:
133** input: *val = 3.14159
134** output: *val = 1.4159 function return = '3'
135**
136** The counter *cnt is incremented each time. After counter exceeds
137** 16 (the number of significant digits in a 64-bit float) '0' is
138** always returned.
139*/
140static int et_getdigit(double *val, int *cnt){
141 int digit;
142 double d;
143 if( (*cnt)++ >= 16 ) return '0';
144 digit = (int)*val;
145 d = digit;
146 digit += '0';
147 *val = (*val - d)*10.0;
148 return digit;
149}
150#endif
151
152#define etBUFSIZE 1000 /* Size of the output buffer */
153
154/*
155** The root program. All variations call this core.
156**
157** INPUTS:
158** func This is a pointer to a function taking three arguments
159** 1. A pointer to anything. Same as the "arg" parameter.
160** 2. A pointer to the list of characters to be output
161** (Note, this list is NOT null terminated.)
162** 3. An integer number of characters to be output.
163** (Note: This number might be zero.)
164**
165** arg This is the pointer to anything which will be passed as the
166** first argument to "func". Use it for whatever you like.
167**
168** fmt This is the format string, as in the usual print.
169**
170** ap This is a pointer to a list of arguments. Same as in
171** vfprint.
172**
173** OUTPUTS:
174** The return value is the total number of characters sent to
175** the function "func". Returns -1 on a error.
176**
177** Note that the order in which automatic variables are declared below
178** seems to make a big difference in determining how fast this beast
179** will run.
180*/
181static int vxprintf(
182 void (*func)(void*,char*,int),
183 void *arg,
184 const char *format,
185 va_list ap
186){
187 register const char *fmt; /* The format string. */
188 register int c; /* Next character in the format string */
189 register char *bufpt; /* Pointer to the conversion buffer */
190 register int precision; /* Precision of the current field */
191 register int length; /* Length of the field */
192 register int idx; /* A general purpose loop counter */
193 int count; /* Total number of characters output */
194 int width; /* Width of the current field */
195 int flag_leftjustify; /* True if "-" flag is present */
196 int flag_plussign; /* True if "+" flag is present */
197 int flag_blanksign; /* True if " " flag is present */
198 int flag_alternateform; /* True if "#" flag is present */
199 int flag_zeropad; /* True if field width constant starts with zero */
200 int flag_long; /* True if "l" flag is present */
201 int flag_center; /* True if "=" flag is present */
202 unsigned long longvalue; /* Value for integer types */
203 double realvalue; /* Value for real types */
204 et_info *infop; /* Pointer to the appropriate info structure */
205 char buf[etBUFSIZE]; /* Conversion buffer */
206 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
207 int errorflag = 0; /* True if an error is encountered */
208 enum et_type xtype; /* Conversion paradigm */
209 char *zMem; /* String to be freed */
210 char *zExtra; /* Extra memory used for etTCLESCAPE conversions */
211 static char spaces[] = " "
212 " ";
213#define etSPACESIZE (sizeof(spaces)-1)
214#ifndef etNOFLOATINGPOINT
215 int exp; /* exponent of real numbers */
216 double rounder; /* Used for rounding floating point values */
217 int flag_dp; /* True if decimal point should be shown */
218 int flag_rtz; /* True if trailing zeros should be removed */
219 int flag_exp; /* True to force display of the exponent */
220 int nsd; /* Number of significant digits returned */
221#endif
222
223 fmt = format; /* Put in a register for speed */
224 count = length = 0;
225 bufpt = 0;
226 for(; (c=(*fmt))!=0; ++fmt){
227 if( c!='%' ){
228 register int amt;
229 bufpt = (char *)fmt;
230 amt = 1;
231 while( (c=(*++fmt))!='%' && c!=0 ) amt++;
232 (*func)(arg,bufpt,amt);
233 count += amt;
234 if( c==0 ) break;
235 }
236 if( (c=(*++fmt))==0 ){
237 errorflag = 1;
238 (*func)(arg,"%",1);
239 count++;
240 break;
241 }
242 /* Find out what flags are present */
243 flag_leftjustify = flag_plussign = flag_blanksign =
244 flag_alternateform = flag_zeropad = flag_center = 0;
245 do{
246 switch( c ){
247 case '-': flag_leftjustify = 1; c = 0; break;
248 case '+': flag_plussign = 1; c = 0; break;
249 case ' ': flag_blanksign = 1; c = 0; break;
250 case '#': flag_alternateform = 1; c = 0; break;
251 case '0': flag_zeropad = 1; c = 0; break;
252 case '=': flag_center = 1; c = 0; break;
253 default: break;
254 }
255 }while( c==0 && (c=(*++fmt))!=0 );
256 if( flag_center ) flag_leftjustify = 0;
257 /* Get the field width */
258 width = 0;
259 if( c=='*' ){
260 width = va_arg(ap,int);
261 if( width<0 ){
262 flag_leftjustify = 1;
263 width = -width;
264 }
265 c = *++fmt;
266 }else{
drh17a68932001-01-31 13:28:08 +0000267 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000268 width = width*10 + c - '0';
269 c = *++fmt;
270 }
271 }
272 if( width > etBUFSIZE-10 ){
273 width = etBUFSIZE-10;
274 }
275 /* Get the precision */
276 if( c=='.' ){
277 precision = 0;
278 c = *++fmt;
279 if( c=='*' ){
280 precision = va_arg(ap,int);
281#ifndef etCOMPATIBILITY
282 /* This is sensible, but SUN OS 4.1 doesn't do it. */
283 if( precision<0 ) precision = -precision;
284#endif
285 c = *++fmt;
286 }else{
drh17a68932001-01-31 13:28:08 +0000287 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000288 precision = precision*10 + c - '0';
289 c = *++fmt;
290 }
291 }
292 /* Limit the precision to prevent overflowing buf[] during conversion */
293 if( precision>etBUFSIZE-40 ) precision = etBUFSIZE-40;
294 }else{
295 precision = -1;
296 }
297 /* Get the conversion type modifier */
298 if( c=='l' ){
299 flag_long = 1;
300 c = *++fmt;
301 }else{
302 flag_long = 0;
303 }
304 /* Fetch the info entry for the field */
305 infop = 0;
306 for(idx=0; idx<etNINFO; idx++){
307 if( c==fmtinfo[idx].fmttype ){
308 infop = &fmtinfo[idx];
309 break;
310 }
311 }
312 /* No info entry found. It must be an error. */
313 if( infop==0 ){
314 xtype = etERROR;
315 }else{
316 xtype = infop->type;
317 }
318 zExtra = 0;
319
320 /*
321 ** At this point, variables are initialized as follows:
322 **
323 ** flag_alternateform TRUE if a '#' is present.
324 ** flag_plussign TRUE if a '+' is present.
325 ** flag_leftjustify TRUE if a '-' is present or if the
326 ** field width was negative.
327 ** flag_zeropad TRUE if the width began with 0.
328 ** flag_long TRUE if the letter 'l' (ell) prefixed
329 ** the conversion character.
330 ** flag_blanksign TRUE if a ' ' is present.
331 ** width The specified field width. This is
332 ** always non-negative. Zero is the default.
333 ** precision The specified precision. The default
334 ** is -1.
335 ** xtype The class of the conversion.
336 ** infop Pointer to the appropriate info struct.
337 */
338 switch( xtype ){
339 case etORDINAL:
340 case etRADIX:
341 if( flag_long ) longvalue = va_arg(ap,long);
drh9adf9ac2002-05-15 11:44:13 +0000342 else longvalue = va_arg(ap,int);
drha18c5682000-10-08 22:20:57 +0000343#ifdef etCOMPATIBILITY
344 /* For the format %#x, the value zero is printed "0" not "0x0".
345 ** I think this is stupid. */
346 if( longvalue==0 ) flag_alternateform = 0;
347#else
348 /* More sensible: turn off the prefix for octal (to prevent "00"),
349 ** but leave the prefix for hex. */
350 if( longvalue==0 && infop->base==8 ) flag_alternateform = 0;
351#endif
352 if( infop->flag_signed ){
353 if( *(long*)&longvalue<0 ){
354 longvalue = -*(long*)&longvalue;
355 prefix = '-';
356 }else if( flag_plussign ) prefix = '+';
357 else if( flag_blanksign ) prefix = ' ';
358 else prefix = 0;
359 }else prefix = 0;
360 if( flag_zeropad && precision<width-(prefix!=0) ){
361 precision = width-(prefix!=0);
drh9adf9ac2002-05-15 11:44:13 +0000362 }
drha18c5682000-10-08 22:20:57 +0000363 bufpt = &buf[etBUFSIZE];
364 if( xtype==etORDINAL ){
365 long a,b;
366 a = longvalue%10;
367 b = longvalue%100;
368 bufpt -= 2;
369 if( a==0 || a>3 || (b>10 && b<14) ){
370 bufpt[0] = 't';
371 bufpt[1] = 'h';
372 }else if( a==1 ){
373 bufpt[0] = 's';
374 bufpt[1] = 't';
375 }else if( a==2 ){
376 bufpt[0] = 'n';
377 bufpt[1] = 'd';
378 }else if( a==3 ){
379 bufpt[0] = 'r';
380 bufpt[1] = 'd';
381 }
382 }
383 {
384 register char *cset; /* Use registers for speed */
385 register int base;
386 cset = infop->charset;
387 base = infop->base;
388 do{ /* Convert to ascii */
389 *(--bufpt) = cset[longvalue%base];
390 longvalue = longvalue/base;
391 }while( longvalue>0 );
drh9adf9ac2002-05-15 11:44:13 +0000392 }
drha18c5682000-10-08 22:20:57 +0000393 length = (long)&buf[etBUFSIZE]-(long)bufpt;
394 for(idx=precision-length; idx>0; idx--){
395 *(--bufpt) = '0'; /* Zero pad */
drh9adf9ac2002-05-15 11:44:13 +0000396 }
drha18c5682000-10-08 22:20:57 +0000397 if( prefix ) *(--bufpt) = prefix; /* Add sign */
398 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
399 char *pre, x;
400 pre = infop->prefix;
401 if( *bufpt!=pre[0] ){
402 for(pre=infop->prefix; (x=(*pre))!=0; pre++) *(--bufpt) = x;
drh9adf9ac2002-05-15 11:44:13 +0000403 }
drha18c5682000-10-08 22:20:57 +0000404 }
405 length = (long)&buf[etBUFSIZE]-(long)bufpt;
406 break;
407 case etFLOAT:
408 case etEXP:
409 case etGENERIC:
410 realvalue = va_arg(ap,double);
411#ifndef etNOFLOATINGPOINT
412 if( precision<0 ) precision = 6; /* Set default precision */
413 if( precision>etBUFSIZE-10 ) precision = etBUFSIZE-10;
414 if( realvalue<0.0 ){
415 realvalue = -realvalue;
416 prefix = '-';
drh9adf9ac2002-05-15 11:44:13 +0000417 }else{
drha18c5682000-10-08 22:20:57 +0000418 if( flag_plussign ) prefix = '+';
419 else if( flag_blanksign ) prefix = ' ';
420 else prefix = 0;
drh9adf9ac2002-05-15 11:44:13 +0000421 }
drha18c5682000-10-08 22:20:57 +0000422 if( infop->type==etGENERIC && precision>0 ) precision--;
423 rounder = 0.0;
424#ifdef COMPATIBILITY
425 /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */
426 for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
427#else
428 /* It makes more sense to use 0.5 */
429 for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1);
430#endif
431 if( infop->type==etFLOAT ) realvalue += rounder;
432 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
433 exp = 0;
434 if( realvalue>0.0 ){
435 int k = 0;
436 while( realvalue>=1e8 && k++<100 ){ realvalue *= 1e-8; exp+=8; }
437 while( realvalue>=10.0 && k++<100 ){ realvalue *= 0.1; exp++; }
438 while( realvalue<1e-8 && k++<100 ){ realvalue *= 1e8; exp-=8; }
439 while( realvalue<1.0 && k++<100 ){ realvalue *= 10.0; exp--; }
440 if( k>=100 ){
441 bufpt = "NaN";
442 length = 3;
443 break;
444 }
drh9adf9ac2002-05-15 11:44:13 +0000445 }
drha18c5682000-10-08 22:20:57 +0000446 bufpt = buf;
447 /*
448 ** If the field type is etGENERIC, then convert to either etEXP
449 ** or etFLOAT, as appropriate.
450 */
451 flag_exp = xtype==etEXP;
452 if( xtype!=etFLOAT ){
453 realvalue += rounder;
454 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
455 }
456 if( xtype==etGENERIC ){
457 flag_rtz = !flag_alternateform;
458 if( exp<-4 || exp>precision ){
459 xtype = etEXP;
460 }else{
461 precision = precision - exp;
462 xtype = etFLOAT;
463 }
drh9adf9ac2002-05-15 11:44:13 +0000464 }else{
drha18c5682000-10-08 22:20:57 +0000465 flag_rtz = 0;
drh9adf9ac2002-05-15 11:44:13 +0000466 }
drha18c5682000-10-08 22:20:57 +0000467 /*
468 ** The "exp+precision" test causes output to be of type etEXP if
469 ** the precision is too large to fit in buf[].
470 */
471 nsd = 0;
472 if( xtype==etFLOAT && exp+precision<etBUFSIZE-30 ){
473 flag_dp = (precision>0 || flag_alternateform);
474 if( prefix ) *(bufpt++) = prefix; /* Sign */
475 if( exp<0 ) *(bufpt++) = '0'; /* Digits before "." */
476 else for(; exp>=0; exp--) *(bufpt++) = et_getdigit(&realvalue,&nsd);
477 if( flag_dp ) *(bufpt++) = '.'; /* The decimal point */
478 for(exp++; exp<0 && precision>0; precision--, exp++){
479 *(bufpt++) = '0';
480 }
481 while( (precision--)>0 ) *(bufpt++) = et_getdigit(&realvalue,&nsd);
482 *(bufpt--) = 0; /* Null terminate */
483 if( flag_rtz && flag_dp ){ /* Remove trailing zeros and "." */
484 while( bufpt>=buf && *bufpt=='0' ) *(bufpt--) = 0;
485 if( bufpt>=buf && *bufpt=='.' ) *(bufpt--) = 0;
486 }
487 bufpt++; /* point to next free slot */
drh9adf9ac2002-05-15 11:44:13 +0000488 }else{ /* etEXP or etGENERIC */
drha18c5682000-10-08 22:20:57 +0000489 flag_dp = (precision>0 || flag_alternateform);
490 if( prefix ) *(bufpt++) = prefix; /* Sign */
491 *(bufpt++) = et_getdigit(&realvalue,&nsd); /* First digit */
492 if( flag_dp ) *(bufpt++) = '.'; /* Decimal point */
493 while( (precision--)>0 ) *(bufpt++) = et_getdigit(&realvalue,&nsd);
494 bufpt--; /* point to last digit */
495 if( flag_rtz && flag_dp ){ /* Remove tail zeros */
496 while( bufpt>=buf && *bufpt=='0' ) *(bufpt--) = 0;
497 if( bufpt>=buf && *bufpt=='.' ) *(bufpt--) = 0;
498 }
499 bufpt++; /* point to next free slot */
500 if( exp || flag_exp ){
501 *(bufpt++) = infop->charset[0];
502 if( exp<0 ){ *(bufpt++) = '-'; exp = -exp; } /* sign of exp */
503 else { *(bufpt++) = '+'; }
504 if( exp>=100 ){
505 *(bufpt++) = (exp/100)+'0'; /* 100's digit */
506 exp %= 100;
drh9adf9ac2002-05-15 11:44:13 +0000507 }
drha18c5682000-10-08 22:20:57 +0000508 *(bufpt++) = exp/10+'0'; /* 10's digit */
509 *(bufpt++) = exp%10+'0'; /* 1's digit */
510 }
drh9adf9ac2002-05-15 11:44:13 +0000511 }
drha18c5682000-10-08 22:20:57 +0000512 /* The converted number is in buf[] and zero terminated. Output it.
513 ** Note that the number is in the usual order, not reversed as with
514 ** integer conversions. */
515 length = (long)bufpt-(long)buf;
516 bufpt = buf;
517
518 /* Special case: Add leading zeros if the flag_zeropad flag is
519 ** set and we are not left justified */
520 if( flag_zeropad && !flag_leftjustify && length < width){
521 int i;
522 int nPad = width - length;
523 for(i=width; i>=nPad; i--){
524 bufpt[i] = bufpt[i-nPad];
525 }
526 i = prefix!=0;
527 while( nPad-- ) bufpt[i++] = '0';
528 length = width;
529 }
530#endif
531 break;
532 case etSIZE:
533 *(va_arg(ap,int*)) = count;
534 length = width = 0;
535 break;
536 case etPERCENT:
537 buf[0] = '%';
538 bufpt = buf;
539 length = 1;
540 break;
541 case etCHARLIT:
542 case etCHARX:
543 c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt);
544 if( precision>=0 ){
545 for(idx=1; idx<precision; idx++) buf[idx] = c;
546 length = precision;
drh9adf9ac2002-05-15 11:44:13 +0000547 }else{
drha18c5682000-10-08 22:20:57 +0000548 length =1;
drh9adf9ac2002-05-15 11:44:13 +0000549 }
drha18c5682000-10-08 22:20:57 +0000550 bufpt = buf;
551 break;
552 case etSTRING:
553 zMem = bufpt = va_arg(ap,char*);
554 if( bufpt==0 ) bufpt = "(null)";
555 length = strlen(bufpt);
556 if( precision>=0 && precision<length ) length = precision;
557 break;
558 case etSQLESCAPE:
chw0cfcf3f2002-06-16 04:55:48 +0000559 case etSQLESCAPE2:
drha18c5682000-10-08 22:20:57 +0000560 {
chw0cfcf3f2002-06-16 04:55:48 +0000561 int i, j, n, c, isnull;
drha18c5682000-10-08 22:20:57 +0000562 char *arg = va_arg(ap,char*);
chw0cfcf3f2002-06-16 04:55:48 +0000563 isnull = arg==0;
564 if( isnull ) arg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
drha18c5682000-10-08 22:20:57 +0000565 for(i=n=0; (c=arg[i])!=0; i++){
566 if( c=='\'' ) n++;
567 }
chw0cfcf3f2002-06-16 04:55:48 +0000568 n += i + 1 + ((!isnull && xtype==etSQLESCAPE2) ? 2 : 0);
drha18c5682000-10-08 22:20:57 +0000569 if( n>etBUFSIZE ){
570 bufpt = zExtra = sqliteMalloc( n );
drhdaffd0e2001-04-11 14:28:42 +0000571 if( bufpt==0 ) return -1;
drha18c5682000-10-08 22:20:57 +0000572 }else{
573 bufpt = buf;
574 }
chw0cfcf3f2002-06-16 04:55:48 +0000575 j = 0;
576 if( !isnull && xtype==etSQLESCAPE2 ) bufpt[j++] = '\'';
577 for(i=0; (c=arg[i])!=0; i++){
drha18c5682000-10-08 22:20:57 +0000578 bufpt[j++] = c;
579 if( c=='\'' ) bufpt[j++] = c;
580 }
chw0cfcf3f2002-06-16 04:55:48 +0000581 if( !isnull && xtype==etSQLESCAPE2 ) bufpt[j++] = '\'';
drha18c5682000-10-08 22:20:57 +0000582 bufpt[j] = 0;
583 length = j;
584 if( precision>=0 && precision<length ) length = precision;
585 }
586 break;
587 case etERROR:
588 buf[0] = '%';
589 buf[1] = c;
590 errorflag = 0;
591 idx = 1+(c!=0);
592 (*func)(arg,"%",idx);
593 count += idx;
594 if( c==0 ) fmt--;
595 break;
596 }/* End switch over the format type */
597 /*
598 ** The text of the conversion is pointed to by "bufpt" and is
599 ** "length" characters long. The field width is "width". Do
600 ** the output.
601 */
602 if( !flag_leftjustify ){
603 register int nspace;
604 nspace = width-length;
605 if( nspace>0 ){
606 if( flag_center ){
607 nspace = nspace/2;
608 width -= nspace;
609 flag_leftjustify = 1;
drh9adf9ac2002-05-15 11:44:13 +0000610 }
drha18c5682000-10-08 22:20:57 +0000611 count += nspace;
612 while( nspace>=etSPACESIZE ){
613 (*func)(arg,spaces,etSPACESIZE);
614 nspace -= etSPACESIZE;
615 }
616 if( nspace>0 ) (*func)(arg,spaces,nspace);
617 }
618 }
619 if( length>0 ){
620 (*func)(arg,bufpt,length);
621 count += length;
622 }
623 if( flag_leftjustify ){
624 register int nspace;
625 nspace = width-length;
626 if( nspace>0 ){
627 count += nspace;
628 while( nspace>=etSPACESIZE ){
629 (*func)(arg,spaces,etSPACESIZE);
630 nspace -= etSPACESIZE;
631 }
632 if( nspace>0 ) (*func)(arg,spaces,nspace);
633 }
634 }
635 if( zExtra ){
636 sqliteFree(zExtra);
637 }
638 }/* End for loop over the format string */
639 return errorflag ? -1 : count;
640} /* End of function */
641
642
643/* This structure is used to store state information about the
644** write to memory that is currently in progress.
645*/
646struct sgMprintf {
647 char *zBase; /* A base allocation */
648 char *zText; /* The string collected so far */
649 int nChar; /* Length of the string so far */
650 int nAlloc; /* Amount of space allocated in zText */
651};
652
653/*
654** This function implements the callback from vxprintf.
655**
656** This routine add nNewChar characters of text in zNewText to
657** the sgMprintf structure pointed to by "arg".
658*/
659static void mout(void *arg, char *zNewText, int nNewChar){
660 struct sgMprintf *pM = (struct sgMprintf*)arg;
661 if( pM->nChar + nNewChar + 1 > pM->nAlloc ){
662 pM->nAlloc = pM->nChar + nNewChar*2 + 1;
663 if( pM->zText==pM->zBase ){
664 pM->zText = sqliteMalloc(pM->nAlloc);
665 if( pM->zText && pM->nChar ) memcpy(pM->zText,pM->zBase,pM->nChar);
666 }else{
drh6d4abfb2001-10-22 02:58:08 +0000667 char *z = sqliteRealloc(pM->zText, pM->nAlloc);
668 if( z==0 ){
669 sqliteFree(pM->zText);
670 pM->nChar = 0;
671 pM->nAlloc = 0;
672 }
673 pM->zText = z;
drha18c5682000-10-08 22:20:57 +0000674 }
675 }
676 if( pM->zText ){
677 memcpy(&pM->zText[pM->nChar], zNewText, nNewChar);
678 pM->nChar += nNewChar;
679 pM->zText[pM->nChar] = 0;
680 }
681}
682
683/*
684** sqlite_mprintf() works like printf(), but allocations memory to hold the
685** resulting string and returns a pointer to the allocated memory. Use
686** sqliteFree() to release the memory allocated.
687*/
688char *sqlite_mprintf(const char *zFormat, ...){
689 va_list ap;
690 struct sgMprintf sMprintf;
691 char *zNew;
692 char zBuf[200];
693
694 sMprintf.nChar = 0;
695 sMprintf.nAlloc = sizeof(zBuf);
696 sMprintf.zText = zBuf;
697 sMprintf.zBase = zBuf;
698 va_start(ap,zFormat);
699 vxprintf(mout,&sMprintf,zFormat,ap);
700 va_end(ap);
701 sMprintf.zText[sMprintf.nChar] = 0;
drhfa173a72002-07-10 21:26:00 +0000702 zNew = malloc( sMprintf.nChar+1 );
703 if( zNew ) strcpy(zNew,sMprintf.zText);
704 if( sMprintf.zText!=sMprintf.zBase ){
705 sqliteFree(sMprintf.zText);
drha18c5682000-10-08 22:20:57 +0000706 }
707 return zNew;
708}
709
710/* This is the varargs version of sqlite_mprintf.
711*/
712char *sqlite_vmprintf(const char *zFormat, va_list ap){
713 struct sgMprintf sMprintf;
drhfa173a72002-07-10 21:26:00 +0000714 char *zNew;
drha18c5682000-10-08 22:20:57 +0000715 char zBuf[200];
716 sMprintf.nChar = 0;
717 sMprintf.zText = zBuf;
718 sMprintf.nAlloc = sizeof(zBuf);
719 sMprintf.zBase = zBuf;
720 vxprintf(mout,&sMprintf,zFormat,ap);
721 sMprintf.zText[sMprintf.nChar] = 0;
drhfa173a72002-07-10 21:26:00 +0000722 zNew = malloc( sMprintf.nChar+1 );
723 if( zNew ) strcpy(zNew,sMprintf.zText);
724 if( sMprintf.zText!=sMprintf.zBase ){
725 sqliteFree(sMprintf.zText);
drha18c5682000-10-08 22:20:57 +0000726 }
drhfa173a72002-07-10 21:26:00 +0000727 return zNew;
drha18c5682000-10-08 22:20:57 +0000728}
729
730/*
731** The following four routines implement the varargs versions of the
732** sqlite_exec() and sqlite_get_table() interfaces. See the sqlite.h
733** header files for a more detailed description of how these interfaces
734** work.
735**
736** These routines are all just simple wrappers.
737*/
738int sqlite_exec_printf(
739 sqlite *db, /* An open database */
drh9f71c2e2001-11-03 23:57:09 +0000740 const char *sqlFormat, /* printf-style format string for the SQL */
drha18c5682000-10-08 22:20:57 +0000741 sqlite_callback xCallback, /* Callback function */
742 void *pArg, /* 1st argument to callback function */
743 char **errmsg, /* Error msg written here */
744 ... /* Arguments to the format string. */
745){
746 va_list ap;
747 int rc;
748
749 va_start(ap, errmsg);
750 rc = sqlite_exec_vprintf(db, sqlFormat, xCallback, pArg, errmsg, ap);
751 va_end(ap);
752 return rc;
753}
754int sqlite_exec_vprintf(
755 sqlite *db, /* An open database */
drh9f71c2e2001-11-03 23:57:09 +0000756 const char *sqlFormat, /* printf-style format string for the SQL */
drha18c5682000-10-08 22:20:57 +0000757 sqlite_callback xCallback, /* Callback function */
758 void *pArg, /* 1st argument to callback function */
759 char **errmsg, /* Error msg written here */
760 va_list ap /* Arguments to the format string. */
761){
762 char *zSql;
763 int rc;
764
765 zSql = sqlite_vmprintf(sqlFormat, ap);
766 rc = sqlite_exec(db, zSql, xCallback, pArg, errmsg);
drhfa173a72002-07-10 21:26:00 +0000767 free(zSql);
drha18c5682000-10-08 22:20:57 +0000768 return rc;
769}
770int sqlite_get_table_printf(
771 sqlite *db, /* An open database */
drh9f71c2e2001-11-03 23:57:09 +0000772 const char *sqlFormat, /* printf-style format string for the SQL */
drha18c5682000-10-08 22:20:57 +0000773 char ***resultp, /* Result written to a char *[] that this points to */
774 int *nrow, /* Number of result rows written here */
775 int *ncol, /* Number of result columns written here */
776 char **errmsg, /* Error msg written here */
777 ... /* Arguments to the format string */
778){
779 va_list ap;
780 int rc;
781
782 va_start(ap, errmsg);
783 rc = sqlite_get_table_vprintf(db, sqlFormat, resultp, nrow, ncol, errmsg, ap);
784 va_end(ap);
785 return rc;
786}
787int sqlite_get_table_vprintf(
788 sqlite *db, /* An open database */
drh9f71c2e2001-11-03 23:57:09 +0000789 const char *sqlFormat, /* printf-style format string for the SQL */
drha18c5682000-10-08 22:20:57 +0000790 char ***resultp, /* Result written to a char *[] that this points to */
791 int *nrow, /* Number of result rows written here */
792 int *ncolumn, /* Number of result columns written here */
793 char **errmsg, /* Error msg written here */
794 va_list ap /* Arguments to the format string */
795){
796 char *zSql;
797 int rc;
798
799 zSql = sqlite_vmprintf(sqlFormat, ap);
800 rc = sqlite_get_table(db, zSql, resultp, nrow, ncolumn, errmsg);
drhfa173a72002-07-10 21:26:00 +0000801 free(zSql);
drha18c5682000-10-08 22:20:57 +0000802 return rc;
803}