|
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.94 2008/08/22 14:08:36 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 xtype; /* Conversion paradigm */ |
|
250 char *zExtra; /* Extra memory used for etTCLESCAPE conversions */ |
|
251 #ifndef SQLITE_OMIT_FLOATING_POINT |
|
252 int exp, e2; /* exponent of real numbers */ |
|
253 double rounder; /* Used for rounding floating point values */ |
|
254 etByte flag_dp; /* True if decimal point should be shown */ |
|
255 etByte flag_rtz; /* True if trailing zeros should be removed */ |
|
256 etByte flag_exp; /* True to force display of the exponent */ |
|
257 int nsd; /* Number of significant digits returned */ |
|
258 #endif |
|
259 |
|
260 length = 0; |
|
261 bufpt = 0; |
|
262 for(; (c=(*fmt))!=0; ++fmt){ |
|
263 if( c!='%' ){ |
|
264 int amt; |
|
265 bufpt = (char *)fmt; |
|
266 amt = 1; |
|
267 while( (c=(*++fmt))!='%' && c!=0 ) amt++; |
|
268 sqlite3StrAccumAppend(pAccum, bufpt, amt); |
|
269 if( c==0 ) break; |
|
270 } |
|
271 if( (c=(*++fmt))==0 ){ |
|
272 sqlite3StrAccumAppend(pAccum, "%", 1); |
|
273 break; |
|
274 } |
|
275 /* Find out what flags are present */ |
|
276 flag_leftjustify = flag_plussign = flag_blanksign = |
|
277 flag_alternateform = flag_altform2 = flag_zeropad = 0; |
|
278 done = 0; |
|
279 do{ |
|
280 switch( c ){ |
|
281 case '-': flag_leftjustify = 1; break; |
|
282 case '+': flag_plussign = 1; break; |
|
283 case ' ': flag_blanksign = 1; break; |
|
284 case '#': flag_alternateform = 1; break; |
|
285 case '!': flag_altform2 = 1; break; |
|
286 case '0': flag_zeropad = 1; break; |
|
287 default: done = 1; break; |
|
288 } |
|
289 }while( !done && (c=(*++fmt))!=0 ); |
|
290 /* Get the field width */ |
|
291 width = 0; |
|
292 if( c=='*' ){ |
|
293 width = va_arg(ap,int); |
|
294 if( width<0 ){ |
|
295 flag_leftjustify = 1; |
|
296 width = -width; |
|
297 } |
|
298 c = *++fmt; |
|
299 }else{ |
|
300 while( c>='0' && c<='9' ){ |
|
301 width = width*10 + c - '0'; |
|
302 c = *++fmt; |
|
303 } |
|
304 } |
|
305 if( width > etBUFSIZE-10 ){ |
|
306 width = etBUFSIZE-10; |
|
307 } |
|
308 /* Get the precision */ |
|
309 if( c=='.' ){ |
|
310 precision = 0; |
|
311 c = *++fmt; |
|
312 if( c=='*' ){ |
|
313 precision = va_arg(ap,int); |
|
314 if( precision<0 ) precision = -precision; |
|
315 c = *++fmt; |
|
316 }else{ |
|
317 while( c>='0' && c<='9' ){ |
|
318 precision = precision*10 + c - '0'; |
|
319 c = *++fmt; |
|
320 } |
|
321 } |
|
322 }else{ |
|
323 precision = -1; |
|
324 } |
|
325 /* Get the conversion type modifier */ |
|
326 if( c=='l' ){ |
|
327 flag_long = 1; |
|
328 c = *++fmt; |
|
329 if( c=='l' ){ |
|
330 flag_longlong = 1; |
|
331 c = *++fmt; |
|
332 }else{ |
|
333 flag_longlong = 0; |
|
334 } |
|
335 }else{ |
|
336 flag_long = flag_longlong = 0; |
|
337 } |
|
338 /* Fetch the info entry for the field */ |
|
339 infop = 0; |
|
340 for(idx=0; idx<etNINFO; idx++){ |
|
341 if( c==fmtinfo[idx].fmttype ){ |
|
342 infop = &fmtinfo[idx]; |
|
343 if( useExtended || (infop->flags & FLAG_INTERN)==0 ){ |
|
344 xtype = infop->type; |
|
345 }else{ |
|
346 return; |
|
347 } |
|
348 break; |
|
349 } |
|
350 } |
|
351 zExtra = 0; |
|
352 if( infop==0 ){ |
|
353 return; |
|
354 } |
|
355 |
|
356 |
|
357 /* Limit the precision to prevent overflowing buf[] during conversion */ |
|
358 if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){ |
|
359 precision = etBUFSIZE-40; |
|
360 } |
|
361 |
|
362 /* |
|
363 ** At this point, variables are initialized as follows: |
|
364 ** |
|
365 ** flag_alternateform TRUE if a '#' is present. |
|
366 ** flag_altform2 TRUE if a '!' is present. |
|
367 ** flag_plussign TRUE if a '+' is present. |
|
368 ** flag_leftjustify TRUE if a '-' is present or if the |
|
369 ** field width was negative. |
|
370 ** flag_zeropad TRUE if the width began with 0. |
|
371 ** flag_long TRUE if the letter 'l' (ell) prefixed |
|
372 ** the conversion character. |
|
373 ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed |
|
374 ** the conversion character. |
|
375 ** flag_blanksign TRUE if a ' ' is present. |
|
376 ** width The specified field width. This is |
|
377 ** always non-negative. Zero is the default. |
|
378 ** precision The specified precision. The default |
|
379 ** is -1. |
|
380 ** xtype The class of the conversion. |
|
381 ** infop Pointer to the appropriate info struct. |
|
382 */ |
|
383 switch( xtype ){ |
|
384 case etPOINTER: |
|
385 flag_longlong = sizeof(char*)==sizeof(i64); |
|
386 flag_long = sizeof(char*)==sizeof(long int); |
|
387 /* Fall through into the next case */ |
|
388 case etORDINAL: |
|
389 case etRADIX: |
|
390 if( infop->flags & FLAG_SIGNED ){ |
|
391 i64 v; |
|
392 if( flag_longlong ) v = va_arg(ap,i64); |
|
393 else if( flag_long ) v = va_arg(ap,long int); |
|
394 else v = va_arg(ap,int); |
|
395 if( v<0 ){ |
|
396 longvalue = -v; |
|
397 prefix = '-'; |
|
398 }else{ |
|
399 longvalue = v; |
|
400 if( flag_plussign ) prefix = '+'; |
|
401 else if( flag_blanksign ) prefix = ' '; |
|
402 else prefix = 0; |
|
403 } |
|
404 }else{ |
|
405 if( flag_longlong ) longvalue = va_arg(ap,u64); |
|
406 else if( flag_long ) longvalue = va_arg(ap,unsigned long int); |
|
407 else longvalue = va_arg(ap,unsigned int); |
|
408 prefix = 0; |
|
409 } |
|
410 if( longvalue==0 ) flag_alternateform = 0; |
|
411 if( flag_zeropad && precision<width-(prefix!=0) ){ |
|
412 precision = width-(prefix!=0); |
|
413 } |
|
414 bufpt = &buf[etBUFSIZE-1]; |
|
415 if( xtype==etORDINAL ){ |
|
416 static const char zOrd[] = "thstndrd"; |
|
417 int x = longvalue % 10; |
|
418 if( x>=4 || (longvalue/10)%10==1 ){ |
|
419 x = 0; |
|
420 } |
|
421 buf[etBUFSIZE-3] = zOrd[x*2]; |
|
422 buf[etBUFSIZE-2] = zOrd[x*2+1]; |
|
423 bufpt -= 2; |
|
424 } |
|
425 { |
|
426 register const char *cset; /* Use registers for speed */ |
|
427 register int base; |
|
428 cset = &aDigits[infop->charset]; |
|
429 base = infop->base; |
|
430 do{ /* Convert to ascii */ |
|
431 *(--bufpt) = cset[longvalue%base]; |
|
432 longvalue = longvalue/base; |
|
433 }while( longvalue>0 ); |
|
434 } |
|
435 length = &buf[etBUFSIZE-1]-bufpt; |
|
436 for(idx=precision-length; idx>0; idx--){ |
|
437 *(--bufpt) = '0'; /* Zero pad */ |
|
438 } |
|
439 if( prefix ) *(--bufpt) = prefix; /* Add sign */ |
|
440 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ |
|
441 const char *pre; |
|
442 char x; |
|
443 pre = &aPrefix[infop->prefix]; |
|
444 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; |
|
445 } |
|
446 length = &buf[etBUFSIZE-1]-bufpt; |
|
447 break; |
|
448 case etFLOAT: |
|
449 case etEXP: |
|
450 case etGENERIC: |
|
451 realvalue = va_arg(ap,double); |
|
452 #ifndef SQLITE_OMIT_FLOATING_POINT |
|
453 if( precision<0 ) precision = 6; /* Set default precision */ |
|
454 if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10; |
|
455 if( realvalue<0.0 ){ |
|
456 realvalue = -realvalue; |
|
457 prefix = '-'; |
|
458 }else{ |
|
459 if( flag_plussign ) prefix = '+'; |
|
460 else if( flag_blanksign ) prefix = ' '; |
|
461 else prefix = 0; |
|
462 } |
|
463 if( xtype==etGENERIC && precision>0 ) precision--; |
|
464 #if 0 |
|
465 /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */ |
|
466 for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1); |
|
467 #else |
|
468 /* It makes more sense to use 0.5 */ |
|
469 for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){} |
|
470 #endif |
|
471 if( xtype==etFLOAT ) realvalue += rounder; |
|
472 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ |
|
473 exp = 0; |
|
474 if( sqlite3IsNaN(realvalue) ){ |
|
475 bufpt = "NaN"; |
|
476 length = 3; |
|
477 break; |
|
478 } |
|
479 if( realvalue>0.0 ){ |
|
480 while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; } |
|
481 while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; } |
|
482 while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; } |
|
483 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } |
|
484 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } |
|
485 if( exp>350 ){ |
|
486 if( prefix=='-' ){ |
|
487 bufpt = "-Inf"; |
|
488 }else if( prefix=='+' ){ |
|
489 bufpt = "+Inf"; |
|
490 }else{ |
|
491 bufpt = "Inf"; |
|
492 } |
|
493 length = strlen(bufpt); |
|
494 break; |
|
495 } |
|
496 } |
|
497 bufpt = buf; |
|
498 /* |
|
499 ** If the field type is etGENERIC, then convert to either etEXP |
|
500 ** or etFLOAT, as appropriate. |
|
501 */ |
|
502 flag_exp = xtype==etEXP; |
|
503 if( xtype!=etFLOAT ){ |
|
504 realvalue += rounder; |
|
505 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } |
|
506 } |
|
507 if( xtype==etGENERIC ){ |
|
508 flag_rtz = !flag_alternateform; |
|
509 if( exp<-4 || exp>precision ){ |
|
510 xtype = etEXP; |
|
511 }else{ |
|
512 precision = precision - exp; |
|
513 xtype = etFLOAT; |
|
514 } |
|
515 }else{ |
|
516 flag_rtz = 0; |
|
517 } |
|
518 if( xtype==etEXP ){ |
|
519 e2 = 0; |
|
520 }else{ |
|
521 e2 = exp; |
|
522 } |
|
523 nsd = 0; |
|
524 flag_dp = (precision>0) | flag_alternateform | flag_altform2; |
|
525 /* The sign in front of the number */ |
|
526 if( prefix ){ |
|
527 *(bufpt++) = prefix; |
|
528 } |
|
529 /* Digits prior to the decimal point */ |
|
530 if( e2<0 ){ |
|
531 *(bufpt++) = '0'; |
|
532 }else{ |
|
533 for(; e2>=0; e2--){ |
|
534 *(bufpt++) = et_getdigit(&realvalue,&nsd); |
|
535 } |
|
536 } |
|
537 /* The decimal point */ |
|
538 if( flag_dp ){ |
|
539 *(bufpt++) = '.'; |
|
540 } |
|
541 /* "0" digits after the decimal point but before the first |
|
542 ** significant digit of the number */ |
|
543 for(e2++; e2<0; precision--, e2++){ |
|
544 assert( precision>0 ); |
|
545 *(bufpt++) = '0'; |
|
546 } |
|
547 /* Significant digits after the decimal point */ |
|
548 while( (precision--)>0 ){ |
|
549 *(bufpt++) = et_getdigit(&realvalue,&nsd); |
|
550 } |
|
551 /* Remove trailing zeros and the "." if no digits follow the "." */ |
|
552 if( flag_rtz && flag_dp ){ |
|
553 while( bufpt[-1]=='0' ) *(--bufpt) = 0; |
|
554 assert( bufpt>buf ); |
|
555 if( bufpt[-1]=='.' ){ |
|
556 if( flag_altform2 ){ |
|
557 *(bufpt++) = '0'; |
|
558 }else{ |
|
559 *(--bufpt) = 0; |
|
560 } |
|
561 } |
|
562 } |
|
563 /* Add the "eNNN" suffix */ |
|
564 if( flag_exp || xtype==etEXP ){ |
|
565 *(bufpt++) = aDigits[infop->charset]; |
|
566 if( exp<0 ){ |
|
567 *(bufpt++) = '-'; exp = -exp; |
|
568 }else{ |
|
569 *(bufpt++) = '+'; |
|
570 } |
|
571 if( exp>=100 ){ |
|
572 *(bufpt++) = (exp/100)+'0'; /* 100's digit */ |
|
573 exp %= 100; |
|
574 } |
|
575 *(bufpt++) = exp/10+'0'; /* 10's digit */ |
|
576 *(bufpt++) = exp%10+'0'; /* 1's digit */ |
|
577 } |
|
578 *bufpt = 0; |
|
579 |
|
580 /* The converted number is in buf[] and zero terminated. Output it. |
|
581 ** Note that the number is in the usual order, not reversed as with |
|
582 ** integer conversions. */ |
|
583 length = bufpt-buf; |
|
584 bufpt = buf; |
|
585 |
|
586 /* Special case: Add leading zeros if the flag_zeropad flag is |
|
587 ** set and we are not left justified */ |
|
588 if( flag_zeropad && !flag_leftjustify && length < width){ |
|
589 int i; |
|
590 int nPad = width - length; |
|
591 for(i=width; i>=nPad; i--){ |
|
592 bufpt[i] = bufpt[i-nPad]; |
|
593 } |
|
594 i = prefix!=0; |
|
595 while( nPad-- ) bufpt[i++] = '0'; |
|
596 length = width; |
|
597 } |
|
598 #endif |
|
599 break; |
|
600 case etSIZE: |
|
601 *(va_arg(ap,int*)) = pAccum->nChar; |
|
602 length = width = 0; |
|
603 break; |
|
604 case etPERCENT: |
|
605 buf[0] = '%'; |
|
606 bufpt = buf; |
|
607 length = 1; |
|
608 break; |
|
609 case etCHARX: |
|
610 c = buf[0] = va_arg(ap,int); |
|
611 if( precision>=0 ){ |
|
612 for(idx=1; idx<precision; idx++) buf[idx] = c; |
|
613 length = precision; |
|
614 }else{ |
|
615 length =1; |
|
616 } |
|
617 bufpt = buf; |
|
618 break; |
|
619 case etSTRING: |
|
620 case etDYNSTRING: |
|
621 bufpt = va_arg(ap,char*); |
|
622 if( bufpt==0 ){ |
|
623 bufpt = ""; |
|
624 }else if( xtype==etDYNSTRING ){ |
|
625 zExtra = bufpt; |
|
626 } |
|
627 if( precision>=0 ){ |
|
628 for(length=0; length<precision && bufpt[length]; length++){} |
|
629 }else{ |
|
630 length = strlen(bufpt); |
|
631 } |
|
632 break; |
|
633 case etSQLESCAPE: |
|
634 case etSQLESCAPE2: |
|
635 case etSQLESCAPE3: { |
|
636 int i, j, n, ch, isnull; |
|
637 int needQuote; |
|
638 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */ |
|
639 char *escarg = va_arg(ap,char*); |
|
640 isnull = escarg==0; |
|
641 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); |
|
642 for(i=n=0; (ch=escarg[i])!=0; i++){ |
|
643 if( ch==q ) n++; |
|
644 } |
|
645 needQuote = !isnull && xtype==etSQLESCAPE2; |
|
646 n += i + 1 + needQuote*2; |
|
647 if( n>etBUFSIZE ){ |
|
648 bufpt = zExtra = sqlite3Malloc( n ); |
|
649 if( bufpt==0 ) return; |
|
650 }else{ |
|
651 bufpt = buf; |
|
652 } |
|
653 j = 0; |
|
654 if( needQuote ) bufpt[j++] = q; |
|
655 for(i=0; (ch=escarg[i])!=0; i++){ |
|
656 bufpt[j++] = ch; |
|
657 if( ch==q ) bufpt[j++] = ch; |
|
658 } |
|
659 if( needQuote ) bufpt[j++] = q; |
|
660 bufpt[j] = 0; |
|
661 length = j; |
|
662 /* The precision is ignored on %q and %Q */ |
|
663 /* if( precision>=0 && precision<length ) length = precision; */ |
|
664 break; |
|
665 } |
|
666 case etTOKEN: { |
|
667 Token *pToken = va_arg(ap, Token*); |
|
668 if( pToken ){ |
|
669 sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n); |
|
670 } |
|
671 length = width = 0; |
|
672 break; |
|
673 } |
|
674 case etSRCLIST: { |
|
675 SrcList *pSrc = va_arg(ap, SrcList*); |
|
676 int k = va_arg(ap, int); |
|
677 struct SrcList_item *pItem = &pSrc->a[k]; |
|
678 assert( k>=0 && k<pSrc->nSrc ); |
|
679 if( pItem->zDatabase ){ |
|
680 sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1); |
|
681 sqlite3StrAccumAppend(pAccum, ".", 1); |
|
682 } |
|
683 sqlite3StrAccumAppend(pAccum, pItem->zName, -1); |
|
684 length = width = 0; |
|
685 break; |
|
686 } |
|
687 }/* End switch over the format type */ |
|
688 /* |
|
689 ** The text of the conversion is pointed to by "bufpt" and is |
|
690 ** "length" characters long. The field width is "width". Do |
|
691 ** the output. |
|
692 */ |
|
693 if( !flag_leftjustify ){ |
|
694 register int nspace; |
|
695 nspace = width-length; |
|
696 if( nspace>0 ){ |
|
697 appendSpace(pAccum, nspace); |
|
698 } |
|
699 } |
|
700 if( length>0 ){ |
|
701 sqlite3StrAccumAppend(pAccum, bufpt, length); |
|
702 } |
|
703 if( flag_leftjustify ){ |
|
704 register int nspace; |
|
705 nspace = width-length; |
|
706 if( nspace>0 ){ |
|
707 appendSpace(pAccum, nspace); |
|
708 } |
|
709 } |
|
710 if( zExtra ){ |
|
711 sqlite3_free(zExtra); |
|
712 } |
|
713 }/* End for loop over the format string */ |
|
714 } /* End of function */ |
|
715 |
|
716 /* |
|
717 ** Append N bytes of text from z to the StrAccum object. |
|
718 */ |
|
719 void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){ |
|
720 if( p->tooBig | p->mallocFailed ){ |
|
721 return; |
|
722 } |
|
723 if( N<0 ){ |
|
724 N = strlen(z); |
|
725 } |
|
726 if( N==0 ){ |
|
727 return; |
|
728 } |
|
729 if( p->nChar+N >= p->nAlloc ){ |
|
730 char *zNew; |
|
731 if( !p->useMalloc ){ |
|
732 p->tooBig = 1; |
|
733 N = p->nAlloc - p->nChar - 1; |
|
734 if( N<=0 ){ |
|
735 return; |
|
736 } |
|
737 }else{ |
|
738 i64 szNew = p->nChar; |
|
739 szNew += N + 1; |
|
740 if( szNew > p->mxAlloc ){ |
|
741 sqlite3StrAccumReset(p); |
|
742 p->tooBig = 1; |
|
743 return; |
|
744 }else{ |
|
745 p->nAlloc = szNew; |
|
746 } |
|
747 zNew = sqlite3DbMallocRaw(p->db, p->nAlloc ); |
|
748 if( zNew ){ |
|
749 memcpy(zNew, p->zText, p->nChar); |
|
750 sqlite3StrAccumReset(p); |
|
751 p->zText = zNew; |
|
752 }else{ |
|
753 p->mallocFailed = 1; |
|
754 sqlite3StrAccumReset(p); |
|
755 return; |
|
756 } |
|
757 } |
|
758 } |
|
759 memcpy(&p->zText[p->nChar], z, N); |
|
760 p->nChar += N; |
|
761 } |
|
762 |
|
763 /* |
|
764 ** Finish off a string by making sure it is zero-terminated. |
|
765 ** Return a pointer to the resulting string. Return a NULL |
|
766 ** pointer if any kind of error was encountered. |
|
767 */ |
|
768 char *sqlite3StrAccumFinish(StrAccum *p){ |
|
769 if( p->zText ){ |
|
770 p->zText[p->nChar] = 0; |
|
771 if( p->useMalloc && p->zText==p->zBase ){ |
|
772 p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); |
|
773 if( p->zText ){ |
|
774 memcpy(p->zText, p->zBase, p->nChar+1); |
|
775 }else{ |
|
776 p->mallocFailed = 1; |
|
777 } |
|
778 } |
|
779 } |
|
780 return p->zText; |
|
781 } |
|
782 |
|
783 /* |
|
784 ** Reset an StrAccum string. Reclaim all malloced memory. |
|
785 */ |
|
786 void sqlite3StrAccumReset(StrAccum *p){ |
|
787 if( p->zText!=p->zBase ){ |
|
788 sqlite3DbFree(p->db, p->zText); |
|
789 } |
|
790 p->zText = 0; |
|
791 } |
|
792 |
|
793 /* |
|
794 ** Initialize a string accumulator |
|
795 */ |
|
796 void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){ |
|
797 p->zText = p->zBase = zBase; |
|
798 p->db = 0; |
|
799 p->nChar = 0; |
|
800 p->nAlloc = n; |
|
801 p->mxAlloc = mx; |
|
802 p->useMalloc = 1; |
|
803 p->tooBig = 0; |
|
804 p->mallocFailed = 0; |
|
805 } |
|
806 |
|
807 /* |
|
808 ** Print into memory obtained from sqliteMalloc(). Use the internal |
|
809 ** %-conversion extensions. |
|
810 */ |
|
811 char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ |
|
812 char *z; |
|
813 char zBase[SQLITE_PRINT_BUF_SIZE]; |
|
814 StrAccum acc; |
|
815 sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), |
|
816 db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH); |
|
817 acc.db = db; |
|
818 sqlite3VXPrintf(&acc, 1, zFormat, ap); |
|
819 z = sqlite3StrAccumFinish(&acc); |
|
820 if( acc.mallocFailed && db ){ |
|
821 db->mallocFailed = 1; |
|
822 } |
|
823 return z; |
|
824 } |
|
825 |
|
826 /* |
|
827 ** Print into memory obtained from sqliteMalloc(). Use the internal |
|
828 ** %-conversion extensions. |
|
829 */ |
|
830 char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ |
|
831 va_list ap; |
|
832 char *z; |
|
833 va_start(ap, zFormat); |
|
834 z = sqlite3VMPrintf(db, zFormat, ap); |
|
835 va_end(ap); |
|
836 return z; |
|
837 } |
|
838 |
|
839 /* |
|
840 ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting |
|
841 ** the string and before returnning. This routine is intended to be used |
|
842 ** to modify an existing string. For example: |
|
843 ** |
|
844 ** x = sqlite3MPrintf(db, x, "prefix %s suffix", x); |
|
845 ** |
|
846 */ |
|
847 char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){ |
|
848 va_list ap; |
|
849 char *z; |
|
850 va_start(ap, zFormat); |
|
851 z = sqlite3VMPrintf(db, zFormat, ap); |
|
852 va_end(ap); |
|
853 sqlite3DbFree(db, zStr); |
|
854 return z; |
|
855 } |
|
856 |
|
857 /* |
|
858 ** Print into memory obtained from sqlite3_malloc(). Omit the internal |
|
859 ** %-conversion extensions. |
|
860 */ |
|
861 char *sqlite3_vmprintf(const char *zFormat, va_list ap){ |
|
862 char *z; |
|
863 char zBase[SQLITE_PRINT_BUF_SIZE]; |
|
864 StrAccum acc; |
|
865 #ifndef SQLITE_OMIT_AUTOINIT |
|
866 if( sqlite3_initialize() ) return 0; |
|
867 #endif |
|
868 sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); |
|
869 sqlite3VXPrintf(&acc, 0, zFormat, ap); |
|
870 z = sqlite3StrAccumFinish(&acc); |
|
871 return z; |
|
872 } |
|
873 |
|
874 /* |
|
875 ** Print into memory obtained from sqlite3_malloc()(). Omit the internal |
|
876 ** %-conversion extensions. |
|
877 */ |
|
878 char *sqlite3_mprintf(const char *zFormat, ...){ |
|
879 va_list ap; |
|
880 char *z; |
|
881 #ifndef SQLITE_OMIT_AUTOINIT |
|
882 if( sqlite3_initialize() ) return 0; |
|
883 #endif |
|
884 va_start(ap, zFormat); |
|
885 z = sqlite3_vmprintf(zFormat, ap); |
|
886 va_end(ap); |
|
887 return z; |
|
888 } |
|
889 |
|
890 /* |
|
891 ** sqlite3_snprintf() works like snprintf() except that it ignores the |
|
892 ** current locale settings. This is important for SQLite because we |
|
893 ** are not able to use a "," as the decimal point in place of "." as |
|
894 ** specified by some locales. |
|
895 */ |
|
896 char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ |
|
897 char *z; |
|
898 va_list ap; |
|
899 StrAccum acc; |
|
900 |
|
901 if( n<=0 ){ |
|
902 return zBuf; |
|
903 } |
|
904 sqlite3StrAccumInit(&acc, zBuf, n, 0); |
|
905 acc.useMalloc = 0; |
|
906 va_start(ap,zFormat); |
|
907 sqlite3VXPrintf(&acc, 0, zFormat, ap); |
|
908 va_end(ap); |
|
909 z = sqlite3StrAccumFinish(&acc); |
|
910 return z; |
|
911 } |
|
912 |
|
913 #if defined(SQLITE_DEBUG) |
|
914 /* |
|
915 ** A version of printf() that understands %lld. Used for debugging. |
|
916 ** The printf() built into some versions of windows does not understand %lld |
|
917 ** and segfaults if you give it a long long int. |
|
918 */ |
|
919 void sqlite3DebugPrintf(const char *zFormat, ...){ |
|
920 va_list ap; |
|
921 StrAccum acc; |
|
922 char zBuf[500]; |
|
923 sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0); |
|
924 acc.useMalloc = 0; |
|
925 va_start(ap,zFormat); |
|
926 sqlite3VXPrintf(&acc, 0, zFormat, ap); |
|
927 va_end(ap); |
|
928 sqlite3StrAccumFinish(&acc); |
|
929 fprintf(stdout,"%s", zBuf); |
|
930 fflush(stdout); |
|
931 } |
|
932 #endif |