monotone

monotone Mtn Source Tree

Root/sqlite/printf.c

1/*
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 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**************************************************************************
9**
10** The following modules is an enhanced replacement for the "printf" subroutines
11** found in the standard C library. The following enhancements are
12** 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"
54#include <math.h>
55
56/*
57** Conversion types fall into various categories as defined by the
58** following enumeration.
59*/
60#define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */
61#define etFLOAT 2 /* Floating point. %f */
62#define etEXP 3 /* Exponentional notation. %e and %E */
63#define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */
64#define etSIZE 5 /* Return number of characters processed so far. %n */
65#define etSTRING 6 /* Strings. %s */
66#define etDYNSTRING 7 /* Dynamically allocated strings. %z */
67#define etPERCENT 8 /* Percent symbol. %% */
68#define etCHARX 9 /* Characters. %c */
69/* The rest are extensions, not normally found in printf() */
70#define etCHARLIT 10 /* Literal characters. %' */
71#define etSQLESCAPE 11 /* Strings with '\'' doubled. %q */
72#define etSQLESCAPE2 12 /* Strings with '\'' doubled and enclosed in '',
73 NULL pointers replaced by SQL NULL. %Q */
74#define etTOKEN 13 /* a pointer to a Token structure */
75#define etSRCLIST 14 /* a pointer to a SrcList */
76#define etPOINTER 15 /* The %p conversion */
77
78
79/*
80** An "etByte" is an 8-bit unsigned value.
81*/
82typedef unsigned char etByte;
83
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 */
89 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 */
93 etByte charset; /* Offset into aDigits[] of the digits string */
94 etByte prefix; /* Offset into aPrefix[] of the prefix string */
95} et_info;
96
97/*
98** 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 */
102#define FLAG_STRING 4 /* Allow infinity precision */
103
104
105/*
106** The following table is searched linearly, so it is good to put the
107** most frequently used conversion types first.
108*/
109static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
110static const char aPrefix[] = "-x0\000X0";
111static const et_info fmtinfo[] = {
112 { 'd', 10, 1, etRADIX, 0, 0 },
113 { 's', 0, 4, etSTRING, 0, 0 },
114 { 'g', 0, 1, etGENERIC, 30, 0 },
115 { 'z', 0, 6, etDYNSTRING, 0, 0 },
116 { 'q', 0, 4, etSQLESCAPE, 0, 0 },
117 { 'Q', 0, 4, etSQLESCAPE2, 0, 0 },
118 { '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#ifndef SQLITE_OMIT_FLOATING_POINT
124 { 'f', 0, 1, etFLOAT, 0, 0 },
125 { 'e', 0, 1, etEXP, 30, 0 },
126 { 'E', 0, 1, etEXP, 14, 0 },
127 { 'G', 0, 1, etGENERIC, 14, 0 },
128#endif
129 { 'i', 10, 1, etRADIX, 0, 0 },
130 { 'n', 0, 0, etSIZE, 0, 0 },
131 { '%', 0, 0, etPERCENT, 0, 0 },
132 { 'p', 16, 0, etPOINTER, 0, 1 },
133 { 'T', 0, 2, etTOKEN, 0, 0 },
134 { 'S', 0, 2, etSRCLIST, 0, 0 },
135};
136#define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0]))
137
138/*
139** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
140** conversions will work.
141*/
142#ifndef SQLITE_OMIT_FLOATING_POINT
143/*
144** "*val" is a double such that 0.1 <= *val < 10.0
145** Return the ascii code for the leading digit of *val, then
146** multiply "*val" by 10.0 to renormalize.
147**
148** Example:
149** input: *val = 3.14159
150** output: *val = 1.4159 function return = '3'
151**
152** The counter *cnt is incremented each time. After counter exceeds
153** 16 (the number of significant digits in a 64-bit float) '0' is
154** always returned.
155*/
156static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
157 int digit;
158 LONGDOUBLE_TYPE d;
159 if( (*cnt)++ >= 16 ) return '0';
160 digit = (int)*val;
161 d = digit;
162 digit += '0';
163 *val = (*val - d)*10.0;
164 return digit;
165}
166#endif /* SQLITE_OMIT_FLOATING_POINT */
167
168/*
169** On machines with a small stack size, you can redefine the
170** SQLITE_PRINT_BUF_SIZE to be less than 350. But beware - for
171** smaller values some %f conversions may go into an infinite loop.
172*/
173#ifndef SQLITE_PRINT_BUF_SIZE
174# define SQLITE_PRINT_BUF_SIZE 350
175#endif
176#define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
177
178/*
179** The root program. All variations call this core.
180**
181** INPUTS:
182** func This is a pointer to a function taking three arguments
183** 1. A pointer to anything. Same as the "arg" parameter.
184** 2. A pointer to the list of characters to be output
185** (Note, this list is NOT null terminated.)
186** 3. An integer number of characters to be output.
187** (Note: This number might be zero.)
188**
189** arg This is the pointer to anything which will be passed as the
190** first argument to "func". Use it for whatever you like.
191**
192** fmt This is the format string, as in the usual print.
193**
194** ap This is a pointer to a list of arguments. Same as in
195** vfprint.
196**
197** OUTPUTS:
198** The return value is the total number of characters sent to
199** the function "func". Returns -1 on a error.
200**
201** Note that the order in which automatic variables are declared below
202** seems to make a big difference in determining how fast this beast
203** will run.
204*/
205static int vxprintf(
206 void (*func)(void*,const char*,int), /* Consumer of text */
207 void *arg, /* First argument to the consumer */
208 int useExtended, /* Allow extended %-conversions */
209 const char *fmt, /* Format string */
210 va_list ap /* arguments */
211){
212 int c; /* Next character in the format string */
213 char *bufpt; /* Pointer to the conversion buffer */
214 int precision; /* Precision of the current field */
215 int length; /* Length of the field */
216 int idx; /* A general purpose loop counter */
217 int count; /* Total number of characters output */
218 int width; /* Width of the current field */
219 etByte flag_leftjustify; /* True if "-" flag is present */
220 etByte flag_plussign; /* True if "+" flag is present */
221 etByte flag_blanksign; /* True if " " flag is present */
222 etByte flag_alternateform; /* True if "#" flag is present */
223 etByte flag_altform2; /* True if "!" flag is present */
224 etByte flag_zeropad; /* True if field width constant starts with zero */
225 etByte flag_long; /* True if "l" flag is present */
226 etByte flag_longlong; /* True if the "ll" flag is present */
227 etByte done; /* Loop termination flag */
228 sqlite_uint64 longvalue; /* Value for integer types */
229 LONGDOUBLE_TYPE realvalue; /* Value for real types */
230 const et_info *infop; /* Pointer to the appropriate info structure */
231 char buf[etBUFSIZE]; /* Conversion buffer */
232 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
233 etByte errorflag = 0; /* True if an error is encountered */
234 etByte xtype; /* Conversion paradigm */
235 char *zExtra; /* Extra memory used for etTCLESCAPE conversions */
236 static const char spaces[] =
237 " ";
238#define etSPACESIZE (sizeof(spaces)-1)
239#ifndef SQLITE_OMIT_FLOATING_POINT
240 int exp, e2; /* exponent of real numbers */
241 double rounder; /* Used for rounding floating point values */
242 etByte flag_dp; /* True if decimal point should be shown */
243 etByte flag_rtz; /* True if trailing zeros should be removed */
244 etByte flag_exp; /* True to force display of the exponent */
245 int nsd; /* Number of significant digits returned */
246#endif
247
248 func(arg,"",0);
249 count = length = 0;
250 bufpt = 0;
251 for(; (c=(*fmt))!=0; ++fmt){
252 if( c!='%' ){
253 int amt;
254 bufpt = (char *)fmt;
255 amt = 1;
256 while( (c=(*++fmt))!='%' && c!=0 ) amt++;
257 (*func)(arg,bufpt,amt);
258 count += amt;
259 if( c==0 ) break;
260 }
261 if( (c=(*++fmt))==0 ){
262 errorflag = 1;
263 (*func)(arg,"%",1);
264 count++;
265 break;
266 }
267 /* Find out what flags are present */
268 flag_leftjustify = flag_plussign = flag_blanksign =
269 flag_alternateform = flag_altform2 = flag_zeropad = 0;
270 done = 0;
271 do{
272 switch( c ){
273 case '-': flag_leftjustify = 1; break;
274 case '+': flag_plussign = 1; break;
275 case ' ': flag_blanksign = 1; break;
276 case '#': flag_alternateform = 1; break;
277 case '!': flag_altform2 = 1; break;
278 case '0': flag_zeropad = 1; break;
279 default: done = 1; break;
280 }
281 }while( !done && (c=(*++fmt))!=0 );
282 /* Get the field width */
283 width = 0;
284 if( c=='*' ){
285 width = va_arg(ap,int);
286 if( width<0 ){
287 flag_leftjustify = 1;
288 width = -width;
289 }
290 c = *++fmt;
291 }else{
292 while( c>='0' && c<='9' ){
293 width = width*10 + c - '0';
294 c = *++fmt;
295 }
296 }
297 if( width > etBUFSIZE-10 ){
298 width = etBUFSIZE-10;
299 }
300 /* Get the precision */
301 if( c=='.' ){
302 precision = 0;
303 c = *++fmt;
304 if( c=='*' ){
305 precision = va_arg(ap,int);
306 if( precision<0 ) precision = -precision;
307 c = *++fmt;
308 }else{
309 while( c>='0' && c<='9' ){
310 precision = precision*10 + c - '0';
311 c = *++fmt;
312 }
313 }
314 }else{
315 precision = -1;
316 }
317 /* Get the conversion type modifier */
318 if( c=='l' ){
319 flag_long = 1;
320 c = *++fmt;
321 if( c=='l' ){
322 flag_longlong = 1;
323 c = *++fmt;
324 }else{
325 flag_longlong = 0;
326 }
327 }else{
328 flag_long = flag_longlong = 0;
329 }
330 /* Fetch the info entry for the field */
331 infop = 0;
332 for(idx=0; idx<etNINFO; idx++){
333 if( c==fmtinfo[idx].fmttype ){
334 infop = &fmtinfo[idx];
335 if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
336 xtype = infop->type;
337 }else{
338 return -1;
339 }
340 break;
341 }
342 }
343 zExtra = 0;
344 if( infop==0 ){
345 return -1;
346 }
347
348
349 /* Limit the precision to prevent overflowing buf[] during conversion */
350 if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
351 precision = etBUFSIZE-40;
352 }
353
354 /*
355 ** At this point, variables are initialized as follows:
356 **
357 ** flag_alternateform TRUE if a '#' is present.
358 ** flag_altform2 TRUE if a '!' is present.
359 ** flag_plussign TRUE if a '+' is present.
360 ** flag_leftjustify TRUE if a '-' is present or if the
361 ** field width was negative.
362 ** flag_zeropad TRUE if the width began with 0.
363 ** flag_long TRUE if the letter 'l' (ell) prefixed
364 ** the conversion character.
365 ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed
366 ** the conversion character.
367 ** flag_blanksign TRUE if a ' ' is present.
368 ** width The specified field width. This is
369 ** always non-negative. Zero is the default.
370 ** precision The specified precision. The default
371 ** is -1.
372 ** xtype The class of the conversion.
373 ** infop Pointer to the appropriate info struct.
374 */
375 switch( xtype ){
376 case etPOINTER:
377 flag_longlong = sizeof(char*)==sizeof(i64);
378 flag_long = sizeof(char*)==sizeof(long int);
379 /* Fall through into the next case */
380 case etRADIX:
381 if( infop->flags & FLAG_SIGNED ){
382 i64 v;
383 if( flag_longlong ) v = va_arg(ap,i64);
384 else if( flag_long ) v = va_arg(ap,long int);
385 else v = va_arg(ap,int);
386 if( v<0 ){
387 longvalue = -v;
388 prefix = '-';
389 }else{
390 longvalue = v;
391 if( flag_plussign ) prefix = '+';
392 else if( flag_blanksign ) prefix = ' ';
393 else prefix = 0;
394 }
395 }else{
396 if( flag_longlong ) longvalue = va_arg(ap,u64);
397 else if( flag_long ) longvalue = va_arg(ap,unsigned long int);
398 else longvalue = va_arg(ap,unsigned int);
399 prefix = 0;
400 }
401 if( longvalue==0 ) flag_alternateform = 0;
402 if( flag_zeropad && precision<width-(prefix!=0) ){
403 precision = width-(prefix!=0);
404 }
405 bufpt = &buf[etBUFSIZE-1];
406 {
407 register const char *cset; /* Use registers for speed */
408 register int base;
409 cset = &aDigits[infop->charset];
410 base = infop->base;
411 do{ /* Convert to ascii */
412 *(--bufpt) = cset[longvalue%base];
413 longvalue = longvalue/base;
414 }while( longvalue>0 );
415 }
416 length = &buf[etBUFSIZE-1]-bufpt;
417 for(idx=precision-length; idx>0; idx--){
418 *(--bufpt) = '0'; /* Zero pad */
419 }
420 if( prefix ) *(--bufpt) = prefix; /* Add sign */
421 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
422 const char *pre;
423 char x;
424 pre = &aPrefix[infop->prefix];
425 if( *bufpt!=pre[0] ){
426 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
427 }
428 }
429 length = &buf[etBUFSIZE-1]-bufpt;
430 break;
431 case etFLOAT:
432 case etEXP:
433 case etGENERIC:
434 realvalue = va_arg(ap,double);
435#ifndef SQLITE_OMIT_FLOATING_POINT
436 if( precision<0 ) precision = 6; /* Set default precision */
437 if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10;
438 if( realvalue<0.0 ){
439 realvalue = -realvalue;
440 prefix = '-';
441 }else{
442 if( flag_plussign ) prefix = '+';
443 else if( flag_blanksign ) prefix = ' ';
444 else prefix = 0;
445 }
446 if( xtype==etGENERIC && precision>0 ) precision--;
447#if 0
448 /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */
449 for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
450#else
451 /* It makes more sense to use 0.5 */
452 for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
453#endif
454 if( xtype==etFLOAT ) realvalue += rounder;
455 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
456 exp = 0;
457 if( isnan(realvalue) ){
458 bufpt = "NaN";
459 length = 3;
460 break;
461 }
462 if( realvalue>0.0 ){
463 while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
464 while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
465 while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
466 while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; }
467 while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; }
468 if( exp>350 || exp<-350 ){
469 if( prefix=='-' ){
470 bufpt = "-Inf";
471 }else if( prefix=='+' ){
472 bufpt = "+Inf";
473 }else{
474 bufpt = "Inf";
475 }
476 length = strlen(bufpt);
477 break;
478 }
479 }
480 bufpt = buf;
481 /*
482 ** If the field type is etGENERIC, then convert to either etEXP
483 ** or etFLOAT, as appropriate.
484 */
485 flag_exp = xtype==etEXP;
486 if( xtype!=etFLOAT ){
487 realvalue += rounder;
488 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
489 }
490 if( xtype==etGENERIC ){
491 flag_rtz = !flag_alternateform;
492 if( exp<-4 || exp>precision ){
493 xtype = etEXP;
494 }else{
495 precision = precision - exp;
496 xtype = etFLOAT;
497 }
498 }else{
499 flag_rtz = 0;
500 }
501 if( xtype==etEXP ){
502 e2 = 0;
503 }else{
504 e2 = exp;
505 }
506 nsd = 0;
507 flag_dp = (precision>0) | flag_alternateform | flag_altform2;
508 /* The sign in front of the number */
509 if( prefix ){
510 *(bufpt++) = prefix;
511 }
512 /* Digits prior to the decimal point */
513 if( e2<0 ){
514 *(bufpt++) = '0';
515 }else{
516 for(; e2>=0; e2--){
517 *(bufpt++) = et_getdigit(&realvalue,&nsd);
518 }
519 }
520 /* The decimal point */
521 if( flag_dp ){
522 *(bufpt++) = '.';
523 }
524 /* "0" digits after the decimal point but before the first
525 ** significant digit of the number */
526 for(e2++; e2<0 && precision>0; precision--, e2++){
527 *(bufpt++) = '0';
528 }
529 /* Significant digits after the decimal point */
530 while( (precision--)>0 ){
531 *(bufpt++) = et_getdigit(&realvalue,&nsd);
532 }
533 /* Remove trailing zeros and the "." if no digits follow the "." */
534 if( flag_rtz && flag_dp ){
535 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
536 assert( bufpt>buf );
537 if( bufpt[-1]=='.' ){
538 if( flag_altform2 ){
539 *(bufpt++) = '0';
540 }else{
541 *(--bufpt) = 0;
542 }
543 }
544 }
545 /* Add the "eNNN" suffix */
546 if( flag_exp || (xtype==etEXP && exp) ){
547 *(bufpt++) = aDigits[infop->charset];
548 if( exp<0 ){
549 *(bufpt++) = '-'; exp = -exp;
550 }else{
551 *(bufpt++) = '+';
552 }
553 if( exp>=100 ){
554 *(bufpt++) = (exp/100)+'0'; /* 100's digit */
555 exp %= 100;
556 }
557 *(bufpt++) = exp/10+'0'; /* 10's digit */
558 *(bufpt++) = exp%10+'0'; /* 1's digit */
559 }
560 *bufpt = 0;
561
562 /* The converted number is in buf[] and zero terminated. Output it.
563 ** Note that the number is in the usual order, not reversed as with
564 ** integer conversions. */
565 length = bufpt-buf;
566 bufpt = buf;
567
568 /* Special case: Add leading zeros if the flag_zeropad flag is
569 ** set and we are not left justified */
570 if( flag_zeropad && !flag_leftjustify && length < width){
571 int i;
572 int nPad = width - length;
573 for(i=width; i>=nPad; i--){
574 bufpt[i] = bufpt[i-nPad];
575 }
576 i = prefix!=0;
577 while( nPad-- ) bufpt[i++] = '0';
578 length = width;
579 }
580#endif
581 break;
582 case etSIZE:
583 *(va_arg(ap,int*)) = count;
584 length = width = 0;
585 break;
586 case etPERCENT:
587 buf[0] = '%';
588 bufpt = buf;
589 length = 1;
590 break;
591 case etCHARLIT:
592 case etCHARX:
593 c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt);
594 if( precision>=0 ){
595 for(idx=1; idx<precision; idx++) buf[idx] = c;
596 length = precision;
597 }else{
598 length =1;
599 }
600 bufpt = buf;
601 break;
602 case etSTRING:
603 case etDYNSTRING:
604 bufpt = va_arg(ap,char*);
605 if( bufpt==0 ){
606 bufpt = "";
607 }else if( xtype==etDYNSTRING ){
608 zExtra = bufpt;
609 }
610 length = strlen(bufpt);
611 if( precision>=0 && precision<length ) length = precision;
612 break;
613 case etSQLESCAPE:
614 case etSQLESCAPE2: {
615 int i, j, n, ch, isnull;
616 int needQuote;
617 char *escarg = va_arg(ap,char*);
618 isnull = escarg==0;
619 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
620 for(i=n=0; (ch=escarg[i])!=0; i++){
621 if( ch=='\'' ) n++;
622 }
623 needQuote = !isnull && xtype==etSQLESCAPE2;
624 n += i + 1 + needQuote*2;
625 if( n>etBUFSIZE ){
626 bufpt = zExtra = sqliteMalloc( n );
627 if( bufpt==0 ) return -1;
628 }else{
629 bufpt = buf;
630 }
631 j = 0;
632 if( needQuote ) bufpt[j++] = '\'';
633 for(i=0; (ch=escarg[i])!=0; i++){
634 bufpt[j++] = ch;
635 if( ch=='\'' ) bufpt[j++] = ch;
636 }
637 if( needQuote ) bufpt[j++] = '\'';
638 bufpt[j] = 0;
639 length = j;
640 /* The precision is ignored on %q and %Q */
641 /* if( precision>=0 && precision<length ) length = precision; */
642 break;
643 }
644 case etTOKEN: {
645 Token *pToken = va_arg(ap, Token*);
646 if( pToken && pToken->z ){
647 (*func)(arg, (char*)pToken->z, pToken->n);
648 }
649 length = width = 0;
650 break;
651 }
652 case etSRCLIST: {
653 SrcList *pSrc = va_arg(ap, SrcList*);
654 int k = va_arg(ap, int);
655 struct SrcList_item *pItem = &pSrc->a[k];
656 assert( k>=0 && k<pSrc->nSrc );
657 if( pItem->zDatabase && pItem->zDatabase[0] ){
658 (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase));
659 (*func)(arg, ".", 1);
660 }
661 (*func)(arg, pItem->zName, strlen(pItem->zName));
662 length = width = 0;
663 break;
664 }
665 }/* End switch over the format type */
666 /*
667 ** The text of the conversion is pointed to by "bufpt" and is
668 ** "length" characters long. The field width is "width". Do
669 ** the output.
670 */
671 if( !flag_leftjustify ){
672 register int nspace;
673 nspace = width-length;
674 if( nspace>0 ){
675 count += nspace;
676 while( nspace>=etSPACESIZE ){
677 (*func)(arg,spaces,etSPACESIZE);
678 nspace -= etSPACESIZE;
679 }
680 if( nspace>0 ) (*func)(arg,spaces,nspace);
681 }
682 }
683 if( length>0 ){
684 (*func)(arg,bufpt,length);
685 count += length;
686 }
687 if( flag_leftjustify ){
688 register int nspace;
689 nspace = width-length;
690 if( nspace>0 ){
691 count += nspace;
692 while( nspace>=etSPACESIZE ){
693 (*func)(arg,spaces,etSPACESIZE);
694 nspace -= etSPACESIZE;
695 }
696 if( nspace>0 ) (*func)(arg,spaces,nspace);
697 }
698 }
699 if( zExtra ){
700 sqliteFree(zExtra);
701 }
702 }/* End for loop over the format string */
703 return errorflag ? -1 : count;
704} /* End of function */
705
706
707/* This structure is used to store state information about the
708** write to memory that is currently in progress.
709*/
710struct sgMprintf {
711 char *zBase; /* A base allocation */
712 char *zText; /* The string collected so far */
713 int nChar; /* Length of the string so far */
714 int nTotal; /* Output size if unconstrained */
715 int nAlloc; /* Amount of space allocated in zText */
716 void *(*xRealloc)(void*,int); /* Function used to realloc memory */
717};
718
719/*
720** This function implements the callback from vxprintf.
721**
722** This routine add nNewChar characters of text in zNewText to
723** the sgMprintf structure pointed to by "arg".
724*/
725static void mout(void *arg, const char *zNewText, int nNewChar){
726 struct sgMprintf *pM = (struct sgMprintf*)arg;
727 pM->nTotal += nNewChar;
728 if( pM->nChar + nNewChar + 1 > pM->nAlloc ){
729 if( pM->xRealloc==0 ){
730 nNewChar = pM->nAlloc - pM->nChar - 1;
731 }else{
732 int nAlloc = pM->nChar + nNewChar*2 + 1;
733 if( pM->zText==pM->zBase ){
734 pM->zText = pM->xRealloc(0, nAlloc);
735 if( pM->zText && pM->nChar ){
736 memcpy(pM->zText, pM->zBase, pM->nChar);
737 }
738 }else{
739 char *zNew;
740 zNew = pM->xRealloc(pM->zText, nAlloc);
741 if( zNew ){
742 pM->zText = zNew;
743 }else{
744 return;
745 }
746 }
747 pM->nAlloc = nAlloc;
748 }
749 }
750 if( pM->zText ){
751 if( nNewChar>0 ){
752 memcpy(&pM->zText[pM->nChar], zNewText, nNewChar);
753 pM->nChar += nNewChar;
754 }
755 pM->zText[pM->nChar] = 0;
756 }
757}
758
759/*
760** This routine is a wrapper around xprintf() that invokes mout() as
761** the consumer.
762*/
763static char *base_vprintf(
764 void *(*xRealloc)(void*,int), /* Routine to realloc memory. May be NULL */
765 int useInternal, /* Use internal %-conversions if true */
766 char *zInitBuf, /* Initially write here, before mallocing */
767 int nInitBuf, /* Size of zInitBuf[] */
768 const char *zFormat, /* format string */
769 va_list ap /* arguments */
770){
771 struct sgMprintf sM;
772 sM.zBase = sM.zText = zInitBuf;
773 sM.nChar = sM.nTotal = 0;
774 sM.nAlloc = nInitBuf;
775 sM.xRealloc = xRealloc;
776 vxprintf(mout, &sM, useInternal, zFormat, ap);
777 if( xRealloc ){
778 if( sM.zText==sM.zBase ){
779 sM.zText = xRealloc(0, sM.nChar+1);
780 if( sM.zText ){
781 memcpy(sM.zText, sM.zBase, sM.nChar+1);
782 }
783 }else if( sM.nAlloc>sM.nChar+10 ){
784 char *zNew = xRealloc(sM.zText, sM.nChar+1);
785 if( zNew ){
786 sM.zText = zNew;
787 }
788 }
789 }
790 return sM.zText;
791}
792
793/*
794** Realloc that is a real function, not a macro.
795*/
796static void *printf_realloc(void *old, int size){
797 return sqliteRealloc(old,size);
798}
799
800/*
801** Print into memory obtained from sqliteMalloc(). Use the internal
802** %-conversion extensions.
803*/
804char *sqlite3VMPrintf(const char *zFormat, va_list ap){
805 char zBase[SQLITE_PRINT_BUF_SIZE];
806 return base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
807}
808
809/*
810** Print into memory obtained from sqliteMalloc(). Use the internal
811** %-conversion extensions.
812*/
813char *sqlite3MPrintf(const char *zFormat, ...){
814 va_list ap;
815 char *z;
816 char zBase[SQLITE_PRINT_BUF_SIZE];
817 va_start(ap, zFormat);
818 z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
819 va_end(ap);
820 return z;
821}
822
823/*
824** Print into memory obtained from sqlite3_malloc(). Omit the internal
825** %-conversion extensions.
826*/
827char *sqlite3_vmprintf(const char *zFormat, va_list ap){
828 char zBase[SQLITE_PRINT_BUF_SIZE];
829 return base_vprintf(sqlite3_realloc, 0, zBase, sizeof(zBase), zFormat, ap);
830}
831
832/*
833** Print into memory obtained from sqlite3_malloc()(). Omit the internal
834** %-conversion extensions.
835*/
836char *sqlite3_mprintf(const char *zFormat, ...){
837 va_list ap;
838 char *z;
839 va_start(ap, zFormat);
840 z = sqlite3_vmprintf(zFormat, ap);
841 va_end(ap);
842 return z;
843}
844
845/*
846** sqlite3_snprintf() works like snprintf() except that it ignores the
847** current locale settings. This is important for SQLite because we
848** are not able to use a "," as the decimal point in place of "." as
849** specified by some locales.
850*/
851char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
852 char *z;
853 va_list ap;
854
855 if( n<=0 ){
856 return zBuf;
857 }
858 zBuf[0] = 0;
859 va_start(ap,zFormat);
860 z = base_vprintf(0, 0, zBuf, n, zFormat, ap);
861 va_end(ap);
862 return z;
863}
864
865#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) || defined(SQLITE_MEMDEBUG)
866/*
867** A version of printf() that understands %lld. Used for debugging.
868** The printf() built into some versions of windows does not understand %lld
869** and segfaults if you give it a long long int.
870*/
871void sqlite3DebugPrintf(const char *zFormat, ...){
872 extern int getpid(void);
873 va_list ap;
874 char zBuf[500];
875 va_start(ap, zFormat);
876 base_vprintf(0, 0, zBuf, sizeof(zBuf), zFormat, ap);
877 va_end(ap);
878 fprintf(stdout,"%s", zBuf);
879 fflush(stdout);
880}
881#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status