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