blob: 6798aa7dd2cd9a95d8b568421bff78de4e9d4b1d [file] [log] [blame]
drha18c5682000-10-08 22:20:57 +00001/*
2** The "printf" code that follows dates from the 1980's. It is in
3** the public domain. The original comments are included here for
drhe84a3062004-02-02 12:29:25 +00004** completeness. They are very out-of-date but might be useful as
5** an historical reference. Most of the "enhancements" have been backed
6** out so that the functionality is now the same as standard printf().
7**
drh17d46fc2008-11-22 18:28:50 +00008** $Id: printf.c,v 1.97 2008/11/22 18:28:51 drh Exp $
danielk1977822a5162008-05-16 04:51:54 +00009**
drhe84a3062004-02-02 12:29:25 +000010**************************************************************************
drha18c5682000-10-08 22:20:57 +000011**
drhe78e8282003-01-19 03:59:45 +000012** The following modules is an enhanced replacement for the "printf" subroutines
13** found in the standard C library. The following enhancements are
drha18c5682000-10-08 22:20:57 +000014** 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"
drh7c68d602000-10-11 19:28:51 +000056
drha18c5682000-10-08 22:20:57 +000057/*
drha18c5682000-10-08 22:20:57 +000058** Conversion types fall into various categories as defined by the
59** following enumeration.
60*/
drhe84a3062004-02-02 12:29:25 +000061#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 */
drha18c5682000-10-08 22:20:57 +000070/* The rest are extensions, not normally found in printf() */
drhaf005fb2008-07-09 16:51:51 +000071#define etSQLESCAPE 10 /* Strings with '\'' doubled. %q */
72#define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
chw0cfcf3f2002-06-16 04:55:48 +000073 NULL pointers replaced by SQL NULL. %Q */
drhaf005fb2008-07-09 16:51:51 +000074#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 */
drhe84a3062004-02-02 12:29:25 +000079
80
81/*
82** An "etByte" is an 8-bit unsigned value.
83*/
84typedef unsigned char etByte;
drha18c5682000-10-08 22:20:57 +000085
86/*
87** Each builtin conversion character (ex: the 'd' in "%d") is described
88** by an instance of the following structure
89*/
90typedef struct et_info { /* Information about each format field */
drhe84a3062004-02-02 12:29:25 +000091 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 */
drh76ff3a02004-09-24 22:32:30 +000095 etByte charset; /* Offset into aDigits[] of the digits string */
96 etByte prefix; /* Offset into aPrefix[] of the prefix string */
drha18c5682000-10-08 22:20:57 +000097} et_info;
98
99/*
drhe84a3062004-02-02 12:29:25 +0000100** 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 */
drh4794f732004-11-05 17:17:50 +0000104#define FLAG_STRING 4 /* Allow infinity precision */
drhe84a3062004-02-02 12:29:25 +0000105
106
107/*
drha18c5682000-10-08 22:20:57 +0000108** The following table is searched linearly, so it is good to put the
109** most frequently used conversion types first.
110*/
drh76ff3a02004-09-24 22:32:30 +0000111static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
112static const char aPrefix[] = "-x0\000X0";
drh57196282004-10-06 15:41:16 +0000113static const et_info fmtinfo[] = {
drh76ff3a02004-09-24 22:32:30 +0000114 { 'd', 10, 1, etRADIX, 0, 0 },
drh4794f732004-11-05 17:17:50 +0000115 { 's', 0, 4, etSTRING, 0, 0 },
drh557cc602005-08-13 12:59:14 +0000116 { 'g', 0, 1, etGENERIC, 30, 0 },
drh153c62c2007-08-24 03:51:33 +0000117 { 'z', 0, 4, etDYNSTRING, 0, 0 },
drh4794f732004-11-05 17:17:50 +0000118 { 'q', 0, 4, etSQLESCAPE, 0, 0 },
119 { 'Q', 0, 4, etSQLESCAPE2, 0, 0 },
danielk1977f3b863e2007-06-24 06:32:17 +0000120 { 'w', 0, 4, etSQLESCAPE3, 0, 0 },
drh76ff3a02004-09-24 22:32:30 +0000121 { '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 },
drhb37df7b2005-10-13 02:09:49 +0000126#ifndef SQLITE_OMIT_FLOATING_POINT
drh76ff3a02004-09-24 22:32:30 +0000127 { 'f', 0, 1, etFLOAT, 0, 0 },
128 { 'e', 0, 1, etEXP, 30, 0 },
129 { 'E', 0, 1, etEXP, 14, 0 },
drh76ff3a02004-09-24 22:32:30 +0000130 { 'G', 0, 1, etGENERIC, 14, 0 },
drhb37df7b2005-10-13 02:09:49 +0000131#endif
drh76ff3a02004-09-24 22:32:30 +0000132 { '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 },
drh9a993342007-12-13 02:45:31 +0000138 { 'r', 10, 3, etORDINAL, 0, 0 },
drha18c5682000-10-08 22:20:57 +0000139};
drha18c5682000-10-08 22:20:57 +0000140
141/*
drhb37df7b2005-10-13 02:09:49 +0000142** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
drha18c5682000-10-08 22:20:57 +0000143** conversions will work.
144*/
drhb37df7b2005-10-13 02:09:49 +0000145#ifndef SQLITE_OMIT_FLOATING_POINT
drha18c5682000-10-08 22:20:57 +0000146/*
147** "*val" is a double such that 0.1 <= *val < 10.0
148** Return the ascii code for the leading digit of *val, then
149** multiply "*val" by 10.0 to renormalize.
150**
151** Example:
152** input: *val = 3.14159
153** output: *val = 1.4159 function return = '3'
154**
155** The counter *cnt is incremented each time. After counter exceeds
156** 16 (the number of significant digits in a 64-bit float) '0' is
157** always returned.
158*/
drh384eef32004-01-07 03:04:27 +0000159static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
drha18c5682000-10-08 22:20:57 +0000160 int digit;
drh384eef32004-01-07 03:04:27 +0000161 LONGDOUBLE_TYPE d;
drha18c5682000-10-08 22:20:57 +0000162 if( (*cnt)++ >= 16 ) return '0';
163 digit = (int)*val;
164 d = digit;
165 digit += '0';
166 *val = (*val - d)*10.0;
167 return digit;
168}
drhb37df7b2005-10-13 02:09:49 +0000169#endif /* SQLITE_OMIT_FLOATING_POINT */
drha18c5682000-10-08 22:20:57 +0000170
drh79158e12005-09-06 21:40:45 +0000171/*
drhade86482007-11-28 22:36:40 +0000172** Append N space characters to the given string buffer.
173*/
174static void appendSpace(StrAccum *pAccum, int N){
175 static const char zSpaces[] = " ";
danielk197700e13612008-11-17 19:18:54 +0000176 while( N>=(int)sizeof(zSpaces)-1 ){
drhade86482007-11-28 22:36:40 +0000177 sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
178 N -= sizeof(zSpaces)-1;
179 }
180 if( N>0 ){
181 sqlite3StrAccumAppend(pAccum, zSpaces, N);
182 }
183}
184
185/*
drh79158e12005-09-06 21:40:45 +0000186** On machines with a small stack size, you can redefine the
187** SQLITE_PRINT_BUF_SIZE to be less than 350. But beware - for
188** smaller values some %f conversions may go into an infinite loop.
189*/
190#ifndef SQLITE_PRINT_BUF_SIZE
191# define SQLITE_PRINT_BUF_SIZE 350
192#endif
193#define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
drha18c5682000-10-08 22:20:57 +0000194
195/*
196** The root program. All variations call this core.
197**
198** INPUTS:
199** func This is a pointer to a function taking three arguments
200** 1. A pointer to anything. Same as the "arg" parameter.
201** 2. A pointer to the list of characters to be output
202** (Note, this list is NOT null terminated.)
203** 3. An integer number of characters to be output.
204** (Note: This number might be zero.)
205**
206** arg This is the pointer to anything which will be passed as the
207** first argument to "func". Use it for whatever you like.
208**
209** fmt This is the format string, as in the usual print.
210**
211** ap This is a pointer to a list of arguments. Same as in
212** vfprint.
213**
214** OUTPUTS:
215** The return value is the total number of characters sent to
216** the function "func". Returns -1 on a error.
217**
218** Note that the order in which automatic variables are declared below
219** seems to make a big difference in determining how fast this beast
220** will run.
221*/
drhf089aa42008-07-08 19:34:06 +0000222void sqlite3VXPrintf(
drhade86482007-11-28 22:36:40 +0000223 StrAccum *pAccum, /* Accumulate results here */
drh5f968432004-02-21 19:02:30 +0000224 int useExtended, /* Allow extended %-conversions */
225 const char *fmt, /* Format string */
226 va_list ap /* arguments */
drha18c5682000-10-08 22:20:57 +0000227){
drhe84a3062004-02-02 12:29:25 +0000228 int c; /* Next character in the format string */
229 char *bufpt; /* Pointer to the conversion buffer */
230 int precision; /* Precision of the current field */
231 int length; /* Length of the field */
232 int idx; /* A general purpose loop counter */
drhe84a3062004-02-02 12:29:25 +0000233 int width; /* Width of the current field */
234 etByte flag_leftjustify; /* True if "-" flag is present */
235 etByte flag_plussign; /* True if "+" flag is present */
236 etByte flag_blanksign; /* True if " " flag is present */
237 etByte flag_alternateform; /* True if "#" flag is present */
drh531fe872005-08-13 13:40:42 +0000238 etByte flag_altform2; /* True if "!" flag is present */
drhe84a3062004-02-02 12:29:25 +0000239 etByte flag_zeropad; /* True if field width constant starts with zero */
240 etByte flag_long; /* True if "l" flag is present */
drha34b6762004-05-07 13:30:42 +0000241 etByte flag_longlong; /* True if the "ll" flag is present */
drh3e9aeec2005-08-13 13:39:02 +0000242 etByte done; /* Loop termination flag */
drh27436af2006-03-28 23:57:17 +0000243 sqlite_uint64 longvalue; /* Value for integer types */
drh384eef32004-01-07 03:04:27 +0000244 LONGDOUBLE_TYPE realvalue; /* Value for real types */
drh57196282004-10-06 15:41:16 +0000245 const et_info *infop; /* Pointer to the appropriate info structure */
drhe84a3062004-02-02 12:29:25 +0000246 char buf[etBUFSIZE]; /* Conversion buffer */
247 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
drhe84a3062004-02-02 12:29:25 +0000248 etByte xtype; /* Conversion paradigm */
249 char *zExtra; /* Extra memory used for etTCLESCAPE conversions */
drhb37df7b2005-10-13 02:09:49 +0000250#ifndef SQLITE_OMIT_FLOATING_POINT
drh557cc602005-08-13 12:59:14 +0000251 int exp, e2; /* exponent of real numbers */
drhe84a3062004-02-02 12:29:25 +0000252 double rounder; /* Used for rounding floating point values */
253 etByte flag_dp; /* True if decimal point should be shown */
254 etByte flag_rtz; /* True if trailing zeros should be removed */
255 etByte flag_exp; /* True to force display of the exponent */
256 int nsd; /* Number of significant digits returned */
drha18c5682000-10-08 22:20:57 +0000257#endif
258
drhade86482007-11-28 22:36:40 +0000259 length = 0;
drha18c5682000-10-08 22:20:57 +0000260 bufpt = 0;
261 for(; (c=(*fmt))!=0; ++fmt){
262 if( c!='%' ){
drhe84a3062004-02-02 12:29:25 +0000263 int amt;
drha18c5682000-10-08 22:20:57 +0000264 bufpt = (char *)fmt;
265 amt = 1;
266 while( (c=(*++fmt))!='%' && c!=0 ) amt++;
drhade86482007-11-28 22:36:40 +0000267 sqlite3StrAccumAppend(pAccum, bufpt, amt);
drha18c5682000-10-08 22:20:57 +0000268 if( c==0 ) break;
269 }
270 if( (c=(*++fmt))==0 ){
drhade86482007-11-28 22:36:40 +0000271 sqlite3StrAccumAppend(pAccum, "%", 1);
drha18c5682000-10-08 22:20:57 +0000272 break;
273 }
274 /* Find out what flags are present */
275 flag_leftjustify = flag_plussign = flag_blanksign =
drh557cc602005-08-13 12:59:14 +0000276 flag_alternateform = flag_altform2 = flag_zeropad = 0;
drh3e9aeec2005-08-13 13:39:02 +0000277 done = 0;
drha18c5682000-10-08 22:20:57 +0000278 do{
279 switch( c ){
drh3e9aeec2005-08-13 13:39:02 +0000280 case '-': flag_leftjustify = 1; break;
281 case '+': flag_plussign = 1; break;
282 case ' ': flag_blanksign = 1; break;
283 case '#': flag_alternateform = 1; break;
284 case '!': flag_altform2 = 1; break;
285 case '0': flag_zeropad = 1; break;
286 default: done = 1; break;
drha18c5682000-10-08 22:20:57 +0000287 }
drh3e9aeec2005-08-13 13:39:02 +0000288 }while( !done && (c=(*++fmt))!=0 );
drha18c5682000-10-08 22:20:57 +0000289 /* Get the field width */
290 width = 0;
291 if( c=='*' ){
292 width = va_arg(ap,int);
293 if( width<0 ){
294 flag_leftjustify = 1;
295 width = -width;
296 }
297 c = *++fmt;
298 }else{
drh17a68932001-01-31 13:28:08 +0000299 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000300 width = width*10 + c - '0';
301 c = *++fmt;
302 }
303 }
304 if( width > etBUFSIZE-10 ){
305 width = etBUFSIZE-10;
306 }
307 /* Get the precision */
308 if( c=='.' ){
309 precision = 0;
310 c = *++fmt;
311 if( c=='*' ){
312 precision = va_arg(ap,int);
drha18c5682000-10-08 22:20:57 +0000313 if( precision<0 ) precision = -precision;
drha18c5682000-10-08 22:20:57 +0000314 c = *++fmt;
315 }else{
drh17a68932001-01-31 13:28:08 +0000316 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000317 precision = precision*10 + c - '0';
318 c = *++fmt;
319 }
320 }
drha18c5682000-10-08 22:20:57 +0000321 }else{
322 precision = -1;
323 }
324 /* Get the conversion type modifier */
325 if( c=='l' ){
326 flag_long = 1;
327 c = *++fmt;
drha34b6762004-05-07 13:30:42 +0000328 if( c=='l' ){
329 flag_longlong = 1;
330 c = *++fmt;
331 }else{
332 flag_longlong = 0;
333 }
drha18c5682000-10-08 22:20:57 +0000334 }else{
drha34b6762004-05-07 13:30:42 +0000335 flag_long = flag_longlong = 0;
drha18c5682000-10-08 22:20:57 +0000336 }
337 /* Fetch the info entry for the field */
338 infop = 0;
danielk197700e13612008-11-17 19:18:54 +0000339 for(idx=0; idx<ArraySize(fmtinfo); idx++){
drha18c5682000-10-08 22:20:57 +0000340 if( c==fmtinfo[idx].fmttype ){
341 infop = &fmtinfo[idx];
drh5f968432004-02-21 19:02:30 +0000342 if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
343 xtype = infop->type;
drhc4413562006-05-22 22:04:00 +0000344 }else{
drhade86482007-11-28 22:36:40 +0000345 return;
drh5f968432004-02-21 19:02:30 +0000346 }
drha18c5682000-10-08 22:20:57 +0000347 break;
348 }
349 }
drha18c5682000-10-08 22:20:57 +0000350 zExtra = 0;
drh43617e92006-03-06 20:55:46 +0000351 if( infop==0 ){
drhade86482007-11-28 22:36:40 +0000352 return;
drh43617e92006-03-06 20:55:46 +0000353 }
354
drha18c5682000-10-08 22:20:57 +0000355
drh4794f732004-11-05 17:17:50 +0000356 /* Limit the precision to prevent overflowing buf[] during conversion */
357 if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
358 precision = etBUFSIZE-40;
359 }
360
drha18c5682000-10-08 22:20:57 +0000361 /*
362 ** At this point, variables are initialized as follows:
363 **
364 ** flag_alternateform TRUE if a '#' is present.
drh3e9aeec2005-08-13 13:39:02 +0000365 ** flag_altform2 TRUE if a '!' is present.
drha18c5682000-10-08 22:20:57 +0000366 ** flag_plussign TRUE if a '+' is present.
367 ** flag_leftjustify TRUE if a '-' is present or if the
368 ** field width was negative.
369 ** flag_zeropad TRUE if the width began with 0.
370 ** flag_long TRUE if the letter 'l' (ell) prefixed
371 ** the conversion character.
drha34b6762004-05-07 13:30:42 +0000372 ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed
373 ** the conversion character.
drha18c5682000-10-08 22:20:57 +0000374 ** flag_blanksign TRUE if a ' ' is present.
375 ** width The specified field width. This is
376 ** always non-negative. Zero is the default.
377 ** precision The specified precision. The default
378 ** is -1.
379 ** xtype The class of the conversion.
380 ** infop Pointer to the appropriate info struct.
381 */
382 switch( xtype ){
drhfe63d1c2004-09-08 20:13:04 +0000383 case etPOINTER:
384 flag_longlong = sizeof(char*)==sizeof(i64);
385 flag_long = sizeof(char*)==sizeof(long int);
386 /* Fall through into the next case */
drh9a993342007-12-13 02:45:31 +0000387 case etORDINAL:
drha18c5682000-10-08 22:20:57 +0000388 case etRADIX:
drhe84a3062004-02-02 12:29:25 +0000389 if( infop->flags & FLAG_SIGNED ){
drhe9707672004-06-25 01:10:48 +0000390 i64 v;
391 if( flag_longlong ) v = va_arg(ap,i64);
392 else if( flag_long ) v = va_arg(ap,long int);
393 else v = va_arg(ap,int);
394 if( v<0 ){
395 longvalue = -v;
396 prefix = '-';
danielk1977cfcdaef2004-05-12 07:33:33 +0000397 }else{
drhe9707672004-06-25 01:10:48 +0000398 longvalue = v;
399 if( flag_plussign ) prefix = '+';
danielk1977cfcdaef2004-05-12 07:33:33 +0000400 else if( flag_blanksign ) prefix = ' ';
401 else prefix = 0;
402 }
drhe9707672004-06-25 01:10:48 +0000403 }else{
404 if( flag_longlong ) longvalue = va_arg(ap,u64);
405 else if( flag_long ) longvalue = va_arg(ap,unsigned long int);
406 else longvalue = va_arg(ap,unsigned int);
407 prefix = 0;
408 }
409 if( longvalue==0 ) flag_alternateform = 0;
drha18c5682000-10-08 22:20:57 +0000410 if( flag_zeropad && precision<width-(prefix!=0) ){
411 precision = width-(prefix!=0);
drh9adf9ac2002-05-15 11:44:13 +0000412 }
drh3039c0a2004-02-29 00:11:30 +0000413 bufpt = &buf[etBUFSIZE-1];
drh9a993342007-12-13 02:45:31 +0000414 if( xtype==etORDINAL ){
drh43f6e062007-12-13 17:50:22 +0000415 static const char zOrd[] = "thstndrd";
416 int x = longvalue % 10;
417 if( x>=4 || (longvalue/10)%10==1 ){
418 x = 0;
419 }
420 buf[etBUFSIZE-3] = zOrd[x*2];
421 buf[etBUFSIZE-2] = zOrd[x*2+1];
drh9a993342007-12-13 02:45:31 +0000422 bufpt -= 2;
423 }
drha18c5682000-10-08 22:20:57 +0000424 {
drh76ff3a02004-09-24 22:32:30 +0000425 register const char *cset; /* Use registers for speed */
drha18c5682000-10-08 22:20:57 +0000426 register int base;
drh76ff3a02004-09-24 22:32:30 +0000427 cset = &aDigits[infop->charset];
drha18c5682000-10-08 22:20:57 +0000428 base = infop->base;
429 do{ /* Convert to ascii */
430 *(--bufpt) = cset[longvalue%base];
431 longvalue = longvalue/base;
432 }while( longvalue>0 );
drh9adf9ac2002-05-15 11:44:13 +0000433 }
drh3039c0a2004-02-29 00:11:30 +0000434 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000435 for(idx=precision-length; idx>0; idx--){
436 *(--bufpt) = '0'; /* Zero pad */
drh9adf9ac2002-05-15 11:44:13 +0000437 }
drha18c5682000-10-08 22:20:57 +0000438 if( prefix ) *(--bufpt) = prefix; /* Add sign */
439 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
drh76ff3a02004-09-24 22:32:30 +0000440 const char *pre;
441 char x;
442 pre = &aPrefix[infop->prefix];
drhaf005fb2008-07-09 16:51:51 +0000443 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
drha18c5682000-10-08 22:20:57 +0000444 }
drh3039c0a2004-02-29 00:11:30 +0000445 length = &buf[etBUFSIZE-1]-bufpt;
drha18c5682000-10-08 22:20:57 +0000446 break;
447 case etFLOAT:
448 case etEXP:
449 case etGENERIC:
450 realvalue = va_arg(ap,double);
drhb37df7b2005-10-13 02:09:49 +0000451#ifndef SQLITE_OMIT_FLOATING_POINT
drha18c5682000-10-08 22:20:57 +0000452 if( precision<0 ) precision = 6; /* Set default precision */
drh5eba8c02005-08-19 02:26:27 +0000453 if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10;
drha18c5682000-10-08 22:20:57 +0000454 if( realvalue<0.0 ){
455 realvalue = -realvalue;
456 prefix = '-';
drh9adf9ac2002-05-15 11:44:13 +0000457 }else{
drha18c5682000-10-08 22:20:57 +0000458 if( flag_plussign ) prefix = '+';
459 else if( flag_blanksign ) prefix = ' ';
460 else prefix = 0;
drh9adf9ac2002-05-15 11:44:13 +0000461 }
drh3e9aeec2005-08-13 13:39:02 +0000462 if( xtype==etGENERIC && precision>0 ) precision--;
drh5f968432004-02-21 19:02:30 +0000463#if 0
drha18c5682000-10-08 22:20:57 +0000464 /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */
465 for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
466#else
467 /* It makes more sense to use 0.5 */
drh74161702006-02-24 02:53:49 +0000468 for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
drha18c5682000-10-08 22:20:57 +0000469#endif
drh3e9aeec2005-08-13 13:39:02 +0000470 if( xtype==etFLOAT ) realvalue += rounder;
drha18c5682000-10-08 22:20:57 +0000471 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
472 exp = 0;
drh0de3ae92008-04-28 16:55:26 +0000473 if( sqlite3IsNaN(realvalue) ){
drh53c14022007-05-10 17:23:11 +0000474 bufpt = "NaN";
475 length = 3;
476 break;
477 }
drha18c5682000-10-08 22:20:57 +0000478 if( realvalue>0.0 ){
drh63782852005-08-30 19:30:59 +0000479 while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
480 while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
481 while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
drh93a960a2008-07-10 00:32:42 +0000482 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
483 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
drhaf005fb2008-07-09 16:51:51 +0000484 if( exp>350 ){
drh53c14022007-05-10 17:23:11 +0000485 if( prefix=='-' ){
486 bufpt = "-Inf";
487 }else if( prefix=='+' ){
488 bufpt = "+Inf";
489 }else{
490 bufpt = "Inf";
491 }
492 length = strlen(bufpt);
drha18c5682000-10-08 22:20:57 +0000493 break;
494 }
drh9adf9ac2002-05-15 11:44:13 +0000495 }
drha18c5682000-10-08 22:20:57 +0000496 bufpt = buf;
497 /*
498 ** If the field type is etGENERIC, then convert to either etEXP
499 ** or etFLOAT, as appropriate.
500 */
501 flag_exp = xtype==etEXP;
502 if( xtype!=etFLOAT ){
503 realvalue += rounder;
504 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
505 }
506 if( xtype==etGENERIC ){
507 flag_rtz = !flag_alternateform;
508 if( exp<-4 || exp>precision ){
509 xtype = etEXP;
510 }else{
511 precision = precision - exp;
512 xtype = etFLOAT;
513 }
drh9adf9ac2002-05-15 11:44:13 +0000514 }else{
drha18c5682000-10-08 22:20:57 +0000515 flag_rtz = 0;
drh9adf9ac2002-05-15 11:44:13 +0000516 }
drh557cc602005-08-13 12:59:14 +0000517 if( xtype==etEXP ){
518 e2 = 0;
519 }else{
520 e2 = exp;
521 }
drha18c5682000-10-08 22:20:57 +0000522 nsd = 0;
drh557cc602005-08-13 12:59:14 +0000523 flag_dp = (precision>0) | flag_alternateform | flag_altform2;
524 /* The sign in front of the number */
525 if( prefix ){
526 *(bufpt++) = prefix;
527 }
528 /* Digits prior to the decimal point */
529 if( e2<0 ){
530 *(bufpt++) = '0';
531 }else{
532 for(; e2>=0; e2--){
533 *(bufpt++) = et_getdigit(&realvalue,&nsd);
drha18c5682000-10-08 22:20:57 +0000534 }
drh9adf9ac2002-05-15 11:44:13 +0000535 }
drh557cc602005-08-13 12:59:14 +0000536 /* The decimal point */
537 if( flag_dp ){
538 *(bufpt++) = '.';
539 }
540 /* "0" digits after the decimal point but before the first
541 ** significant digit of the number */
drhaf005fb2008-07-09 16:51:51 +0000542 for(e2++; e2<0; precision--, e2++){
543 assert( precision>0 );
drh557cc602005-08-13 12:59:14 +0000544 *(bufpt++) = '0';
545 }
546 /* Significant digits after the decimal point */
547 while( (precision--)>0 ){
548 *(bufpt++) = et_getdigit(&realvalue,&nsd);
549 }
550 /* Remove trailing zeros and the "." if no digits follow the "." */
551 if( flag_rtz && flag_dp ){
drh3e9aeec2005-08-13 13:39:02 +0000552 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
553 assert( bufpt>buf );
554 if( bufpt[-1]=='.' ){
drh557cc602005-08-13 12:59:14 +0000555 if( flag_altform2 ){
556 *(bufpt++) = '0';
557 }else{
558 *(--bufpt) = 0;
559 }
560 }
561 }
562 /* Add the "eNNN" suffix */
drhaf005fb2008-07-09 16:51:51 +0000563 if( flag_exp || xtype==etEXP ){
drh557cc602005-08-13 12:59:14 +0000564 *(bufpt++) = aDigits[infop->charset];
565 if( exp<0 ){
566 *(bufpt++) = '-'; exp = -exp;
567 }else{
568 *(bufpt++) = '+';
569 }
570 if( exp>=100 ){
571 *(bufpt++) = (exp/100)+'0'; /* 100's digit */
572 exp %= 100;
573 }
574 *(bufpt++) = exp/10+'0'; /* 10's digit */
575 *(bufpt++) = exp%10+'0'; /* 1's digit */
576 }
577 *bufpt = 0;
578
drha18c5682000-10-08 22:20:57 +0000579 /* The converted number is in buf[] and zero terminated. Output it.
580 ** Note that the number is in the usual order, not reversed as with
581 ** integer conversions. */
drh94ce4c12003-06-07 11:29:50 +0000582 length = bufpt-buf;
drha18c5682000-10-08 22:20:57 +0000583 bufpt = buf;
584
585 /* Special case: Add leading zeros if the flag_zeropad flag is
586 ** set and we are not left justified */
587 if( flag_zeropad && !flag_leftjustify && length < width){
588 int i;
589 int nPad = width - length;
590 for(i=width; i>=nPad; i--){
591 bufpt[i] = bufpt[i-nPad];
592 }
593 i = prefix!=0;
594 while( nPad-- ) bufpt[i++] = '0';
595 length = width;
596 }
597#endif
598 break;
599 case etSIZE:
drhade86482007-11-28 22:36:40 +0000600 *(va_arg(ap,int*)) = pAccum->nChar;
drha18c5682000-10-08 22:20:57 +0000601 length = width = 0;
602 break;
603 case etPERCENT:
604 buf[0] = '%';
605 bufpt = buf;
606 length = 1;
607 break;
drha18c5682000-10-08 22:20:57 +0000608 case etCHARX:
drhaf005fb2008-07-09 16:51:51 +0000609 c = buf[0] = va_arg(ap,int);
drha18c5682000-10-08 22:20:57 +0000610 if( precision>=0 ){
611 for(idx=1; idx<precision; idx++) buf[idx] = c;
612 length = precision;
drh9adf9ac2002-05-15 11:44:13 +0000613 }else{
drha18c5682000-10-08 22:20:57 +0000614 length =1;
drh9adf9ac2002-05-15 11:44:13 +0000615 }
drha18c5682000-10-08 22:20:57 +0000616 bufpt = buf;
617 break;
618 case etSTRING:
drhd93d8a82003-06-16 03:08:18 +0000619 case etDYNSTRING:
drhcb485882002-08-15 13:50:48 +0000620 bufpt = va_arg(ap,char*);
drhd93d8a82003-06-16 03:08:18 +0000621 if( bufpt==0 ){
622 bufpt = "";
623 }else if( xtype==etDYNSTRING ){
624 zExtra = bufpt;
625 }
drhe5090942008-04-29 15:22:27 +0000626 if( precision>=0 ){
627 for(length=0; length<precision && bufpt[length]; length++){}
628 }else{
629 length = strlen(bufpt);
630 }
drha18c5682000-10-08 22:20:57 +0000631 break;
632 case etSQLESCAPE:
danielk1977f3b863e2007-06-24 06:32:17 +0000633 case etSQLESCAPE2:
634 case etSQLESCAPE3: {
danielk1977f0113002006-01-24 12:09:17 +0000635 int i, j, n, ch, isnull;
drh5eba8c02005-08-19 02:26:27 +0000636 int needQuote;
danielk1977f3b863e2007-06-24 06:32:17 +0000637 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
danielk1977f0113002006-01-24 12:09:17 +0000638 char *escarg = va_arg(ap,char*);
639 isnull = escarg==0;
640 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
641 for(i=n=0; (ch=escarg[i])!=0; i++){
danielk1977f3b863e2007-06-24 06:32:17 +0000642 if( ch==q ) n++;
drha18c5682000-10-08 22:20:57 +0000643 }
drh5eba8c02005-08-19 02:26:27 +0000644 needQuote = !isnull && xtype==etSQLESCAPE2;
645 n += i + 1 + needQuote*2;
646 if( n>etBUFSIZE ){
drhe5ae5732008-06-15 02:51:47 +0000647 bufpt = zExtra = sqlite3Malloc( n );
drhd164fd32008-11-20 18:20:28 +0000648 if( bufpt==0 ){
649 pAccum->mallocFailed = 1;
650 return;
651 }
drh5eba8c02005-08-19 02:26:27 +0000652 }else{
653 bufpt = buf;
654 }
655 j = 0;
danielk1977f3b863e2007-06-24 06:32:17 +0000656 if( needQuote ) bufpt[j++] = q;
danielk1977f0113002006-01-24 12:09:17 +0000657 for(i=0; (ch=escarg[i])!=0; i++){
658 bufpt[j++] = ch;
danielk1977f3b863e2007-06-24 06:32:17 +0000659 if( ch==q ) bufpt[j++] = ch;
drh5eba8c02005-08-19 02:26:27 +0000660 }
danielk1977f3b863e2007-06-24 06:32:17 +0000661 if( needQuote ) bufpt[j++] = q;
drh5eba8c02005-08-19 02:26:27 +0000662 bufpt[j] = 0;
663 length = j;
drh05a82982006-03-19 13:00:25 +0000664 /* The precision is ignored on %q and %Q */
665 /* if( precision>=0 && precision<length ) length = precision; */
drha18c5682000-10-08 22:20:57 +0000666 break;
drh5eba8c02005-08-19 02:26:27 +0000667 }
drh5f968432004-02-21 19:02:30 +0000668 case etTOKEN: {
669 Token *pToken = va_arg(ap, Token*);
drhaf005fb2008-07-09 16:51:51 +0000670 if( pToken ){
drhade86482007-11-28 22:36:40 +0000671 sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
drhad6d9462004-09-19 02:15:24 +0000672 }
drh5f968432004-02-21 19:02:30 +0000673 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 );
drh93a960a2008-07-10 00:32:42 +0000681 if( pItem->zDatabase ){
drhade86482007-11-28 22:36:40 +0000682 sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1);
683 sqlite3StrAccumAppend(pAccum, ".", 1);
drh5f968432004-02-21 19:02:30 +0000684 }
drhade86482007-11-28 22:36:40 +0000685 sqlite3StrAccumAppend(pAccum, pItem->zName, -1);
drh5f968432004-02-21 19:02:30 +0000686 length = width = 0;
687 break;
688 }
drha18c5682000-10-08 22:20:57 +0000689 }/* 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 ){
drhade86482007-11-28 22:36:40 +0000699 appendSpace(pAccum, nspace);
drha18c5682000-10-08 22:20:57 +0000700 }
701 }
702 if( length>0 ){
drhade86482007-11-28 22:36:40 +0000703 sqlite3StrAccumAppend(pAccum, bufpt, length);
drha18c5682000-10-08 22:20:57 +0000704 }
705 if( flag_leftjustify ){
706 register int nspace;
707 nspace = width-length;
708 if( nspace>0 ){
drhade86482007-11-28 22:36:40 +0000709 appendSpace(pAccum, nspace);
drha18c5682000-10-08 22:20:57 +0000710 }
711 }
712 if( zExtra ){
danielk19771e536952007-08-16 10:09:01 +0000713 sqlite3_free(zExtra);
drha18c5682000-10-08 22:20:57 +0000714 }
715 }/* End for loop over the format string */
drha18c5682000-10-08 22:20:57 +0000716} /* End of function */
717
drhade86482007-11-28 22:36:40 +0000718/*
719** Append N bytes of text from z to the StrAccum object.
drha18c5682000-10-08 22:20:57 +0000720*/
drhade86482007-11-28 22:36:40 +0000721void 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 }
drh17d46fc2008-11-22 18:28:50 +0000728 if( N==0 || z==0 ){
drhade86482007-11-28 22:36:40 +0000729 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{
drh93a960a2008-07-10 00:32:42 +0000740 i64 szNew = p->nChar;
drhb1a6c3c2008-03-20 16:30:17 +0000741 szNew += N + 1;
742 if( szNew > p->mxAlloc ){
drh93a960a2008-07-10 00:32:42 +0000743 sqlite3StrAccumReset(p);
744 p->tooBig = 1;
745 return;
drhb1a6c3c2008-03-20 16:30:17 +0000746 }else{
747 p->nAlloc = szNew;
drh17435752007-08-16 04:30:38 +0000748 }
drh633e6d52008-07-28 19:34:53 +0000749 zNew = sqlite3DbMallocRaw(p->db, p->nAlloc );
drh53f733c2005-09-16 02:38:09 +0000750 if( zNew ){
drhade86482007-11-28 22:36:40 +0000751 memcpy(zNew, p->zText, p->nChar);
752 sqlite3StrAccumReset(p);
753 p->zText = zNew;
754 }else{
755 p->mallocFailed = 1;
756 sqlite3StrAccumReset(p);
757 return;
drh53f733c2005-09-16 02:38:09 +0000758 }
drh5f968432004-02-21 19:02:30 +0000759 }
760 }
drhade86482007-11-28 22:36:40 +0000761 memcpy(&p->zText[p->nChar], z, N);
762 p->nChar += N;
drh483750b2003-01-29 18:46:51 +0000763}
764
765/*
drhade86482007-11-28 22:36:40 +0000766** 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.
drh5f968432004-02-21 19:02:30 +0000769*/
drhade86482007-11-28 22:36:40 +0000770char *sqlite3StrAccumFinish(StrAccum *p){
771 if( p->zText ){
772 p->zText[p->nChar] = 0;
773 if( p->useMalloc && p->zText==p->zBase ){
drh633e6d52008-07-28 19:34:53 +0000774 p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
drhade86482007-11-28 22:36:40 +0000775 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*/
788void sqlite3StrAccumReset(StrAccum *p){
789 if( p->zText!=p->zBase ){
drh633e6d52008-07-28 19:34:53 +0000790 sqlite3DbFree(p->db, p->zText);
drhade86482007-11-28 22:36:40 +0000791 }
drhf089aa42008-07-08 19:34:06 +0000792 p->zText = 0;
drhade86482007-11-28 22:36:40 +0000793}
794
795/*
796** Initialize a string accumulator
797*/
drhf089aa42008-07-08 19:34:06 +0000798void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
drhade86482007-11-28 22:36:40 +0000799 p->zText = p->zBase = zBase;
drh633e6d52008-07-28 19:34:53 +0000800 p->db = 0;
drhade86482007-11-28 22:36:40 +0000801 p->nChar = 0;
802 p->nAlloc = n;
drhbb4957f2008-03-20 14:03:29 +0000803 p->mxAlloc = mx;
drhade86482007-11-28 22:36:40 +0000804 p->useMalloc = 1;
805 p->tooBig = 0;
806 p->mallocFailed = 0;
drh5f968432004-02-21 19:02:30 +0000807}
808
809/*
810** Print into memory obtained from sqliteMalloc(). Use the internal
811** %-conversion extensions.
812*/
drh17435752007-08-16 04:30:38 +0000813char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
814 char *z;
drh79158e12005-09-06 21:40:45 +0000815 char zBase[SQLITE_PRINT_BUF_SIZE];
drhade86482007-11-28 22:36:40 +0000816 StrAccum acc;
drhbb4957f2008-03-20 14:03:29 +0000817 sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
818 db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
drh633e6d52008-07-28 19:34:53 +0000819 acc.db = db;
drhf089aa42008-07-08 19:34:06 +0000820 sqlite3VXPrintf(&acc, 1, zFormat, ap);
drhade86482007-11-28 22:36:40 +0000821 z = sqlite3StrAccumFinish(&acc);
822 if( acc.mallocFailed && db ){
drh17435752007-08-16 04:30:38 +0000823 db->mallocFailed = 1;
824 }
825 return z;
drh5f968432004-02-21 19:02:30 +0000826}
827
828/*
829** Print into memory obtained from sqliteMalloc(). Use the internal
830** %-conversion extensions.
831*/
drh17435752007-08-16 04:30:38 +0000832char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000833 va_list ap;
834 char *z;
drh5f968432004-02-21 19:02:30 +0000835 va_start(ap, zFormat);
drhade86482007-11-28 22:36:40 +0000836 z = sqlite3VMPrintf(db, zFormat, ap);
drh5f968432004-02-21 19:02:30 +0000837 va_end(ap);
838 return z;
839}
840
841/*
drh633e6d52008-07-28 19:34:53 +0000842** 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*/
849char *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/*
drh28dd4792006-06-26 21:35:44 +0000860** Print into memory obtained from sqlite3_malloc(). Omit the internal
861** %-conversion extensions.
862*/
863char *sqlite3_vmprintf(const char *zFormat, va_list ap){
drhade86482007-11-28 22:36:40 +0000864 char *z;
drh28dd4792006-06-26 21:35:44 +0000865 char zBase[SQLITE_PRINT_BUF_SIZE];
drhade86482007-11-28 22:36:40 +0000866 StrAccum acc;
drhff1590e2008-07-14 12:52:53 +0000867#ifndef SQLITE_OMIT_AUTOINIT
868 if( sqlite3_initialize() ) return 0;
869#endif
drhbb4957f2008-03-20 14:03:29 +0000870 sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
drhf089aa42008-07-08 19:34:06 +0000871 sqlite3VXPrintf(&acc, 0, zFormat, ap);
drhade86482007-11-28 22:36:40 +0000872 z = sqlite3StrAccumFinish(&acc);
873 return z;
drh28dd4792006-06-26 21:35:44 +0000874}
875
876/*
877** Print into memory obtained from sqlite3_malloc()(). Omit the internal
878** %-conversion extensions.
drh483750b2003-01-29 18:46:51 +0000879*/
danielk19776f8a5032004-05-10 10:34:51 +0000880char *sqlite3_mprintf(const char *zFormat, ...){
drha18c5682000-10-08 22:20:57 +0000881 va_list ap;
drh5f968432004-02-21 19:02:30 +0000882 char *z;
drhff1590e2008-07-14 12:52:53 +0000883#ifndef SQLITE_OMIT_AUTOINIT
884 if( sqlite3_initialize() ) return 0;
885#endif
drh28dd4792006-06-26 21:35:44 +0000886 va_start(ap, zFormat);
drhb3738b62007-03-31 15:02:49 +0000887 z = sqlite3_vmprintf(zFormat, ap);
drha18c5682000-10-08 22:20:57 +0000888 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000889 return z;
drha18c5682000-10-08 22:20:57 +0000890}
891
drh93a5c6b2003-12-23 02:17:35 +0000892/*
danielk19776f8a5032004-05-10 10:34:51 +0000893** sqlite3_snprintf() works like snprintf() except that it ignores the
drh93a5c6b2003-12-23 02:17:35 +0000894** 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*/
danielk19776f8a5032004-05-10 10:34:51 +0000898char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000899 char *z;
drh93a5c6b2003-12-23 02:17:35 +0000900 va_list ap;
drhade86482007-11-28 22:36:40 +0000901 StrAccum acc;
drh93a5c6b2003-12-23 02:17:35 +0000902
drh68853902007-05-07 11:24:30 +0000903 if( n<=0 ){
904 return zBuf;
905 }
drhbb4957f2008-03-20 14:03:29 +0000906 sqlite3StrAccumInit(&acc, zBuf, n, 0);
drhade86482007-11-28 22:36:40 +0000907 acc.useMalloc = 0;
drh93a5c6b2003-12-23 02:17:35 +0000908 va_start(ap,zFormat);
drhf089aa42008-07-08 19:34:06 +0000909 sqlite3VXPrintf(&acc, 0, zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +0000910 va_end(ap);
drhade86482007-11-28 22:36:40 +0000911 z = sqlite3StrAccumFinish(&acc);
drh5f968432004-02-21 19:02:30 +0000912 return z;
drh93a5c6b2003-12-23 02:17:35 +0000913}
914
drh85e9e222008-07-15 00:27:34 +0000915#if defined(SQLITE_DEBUG)
drhe54ca3f2004-06-07 01:52:14 +0000916/*
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*/
921void sqlite3DebugPrintf(const char *zFormat, ...){
922 va_list ap;
drhade86482007-11-28 22:36:40 +0000923 StrAccum acc;
drhe54ca3f2004-06-07 01:52:14 +0000924 char zBuf[500];
drhbb4957f2008-03-20 14:03:29 +0000925 sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
drhade86482007-11-28 22:36:40 +0000926 acc.useMalloc = 0;
927 va_start(ap,zFormat);
drhf089aa42008-07-08 19:34:06 +0000928 sqlite3VXPrintf(&acc, 0, zFormat, ap);
drhe54ca3f2004-06-07 01:52:14 +0000929 va_end(ap);
drhbed8e7e2007-12-08 17:55:35 +0000930 sqlite3StrAccumFinish(&acc);
drh485f0032007-01-26 19:23:33 +0000931 fprintf(stdout,"%s", zBuf);
drh2ac3ee92004-06-07 16:27:46 +0000932 fflush(stdout);
drhe54ca3f2004-06-07 01:52:14 +0000933}
934#endif