blob: ac7b0516314450947899517fc47d0ad07967ba8c [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**
8**************************************************************************
drha18c5682000-10-08 22:20:57 +00009**
drhed1fddf2011-10-12 18:52:59 +000010** This file contains code for a set of "printf"-like routines. These
11** routines format strings much like the printf() from the standard C
12** library, though the implementation here has enhancements to support
13** SQLlite.
drha18c5682000-10-08 22:20:57 +000014*/
15#include "sqliteInt.h"
drh7c68d602000-10-11 19:28:51 +000016
drha18c5682000-10-08 22:20:57 +000017/*
drh760b1592014-09-18 01:50:09 +000018** If the strchrnul() library function is available, then set
19** HAVE_STRCHRNUL. If that routine is not available, this module
20** will supply its own. The built-in version is slower than
21** the glibc version so the glibc version is definitely preferred.
22*/
23#if !defined(HAVE_STRCHRNUL)
drhb7288e22014-10-22 20:07:19 +000024# define HAVE_STRCHRNUL 0
drh760b1592014-09-18 01:50:09 +000025#endif
26
27
28/*
drha18c5682000-10-08 22:20:57 +000029** Conversion types fall into various categories as defined by the
30** following enumeration.
31*/
drhe84a3062004-02-02 12:29:25 +000032#define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */
33#define etFLOAT 2 /* Floating point. %f */
34#define etEXP 3 /* Exponentional notation. %e and %E */
35#define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */
36#define etSIZE 5 /* Return number of characters processed so far. %n */
37#define etSTRING 6 /* Strings. %s */
38#define etDYNSTRING 7 /* Dynamically allocated strings. %z */
39#define etPERCENT 8 /* Percent symbol. %% */
40#define etCHARX 9 /* Characters. %c */
drha18c5682000-10-08 22:20:57 +000041/* The rest are extensions, not normally found in printf() */
drhaf005fb2008-07-09 16:51:51 +000042#define etSQLESCAPE 10 /* Strings with '\'' doubled. %q */
43#define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
chw0cfcf3f2002-06-16 04:55:48 +000044 NULL pointers replaced by SQL NULL. %Q */
drhaf005fb2008-07-09 16:51:51 +000045#define etTOKEN 12 /* a pointer to a Token structure */
46#define etSRCLIST 13 /* a pointer to a SrcList */
47#define etPOINTER 14 /* The %p conversion */
48#define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */
49#define etORDINAL 16 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
drhe84a3062004-02-02 12:29:25 +000050
drh874ba042009-04-08 16:10:04 +000051#define etINVALID 0 /* Any unrecognized conversion type */
52
drhe84a3062004-02-02 12:29:25 +000053
54/*
55** An "etByte" is an 8-bit unsigned value.
56*/
57typedef unsigned char etByte;
drha18c5682000-10-08 22:20:57 +000058
59/*
60** Each builtin conversion character (ex: the 'd' in "%d") is described
61** by an instance of the following structure
62*/
63typedef struct et_info { /* Information about each format field */
drhe84a3062004-02-02 12:29:25 +000064 char fmttype; /* The format field code letter */
65 etByte base; /* The base for radix conversion */
66 etByte flags; /* One or more of FLAG_ constants below */
67 etByte type; /* Conversion paradigm */
drh76ff3a02004-09-24 22:32:30 +000068 etByte charset; /* Offset into aDigits[] of the digits string */
69 etByte prefix; /* Offset into aPrefix[] of the prefix string */
drha18c5682000-10-08 22:20:57 +000070} et_info;
71
72/*
drhe84a3062004-02-02 12:29:25 +000073** Allowed values for et_info.flags
74*/
75#define FLAG_SIGNED 1 /* True if the value to convert is signed */
76#define FLAG_INTERN 2 /* True if for internal use only */
drh4794f732004-11-05 17:17:50 +000077#define FLAG_STRING 4 /* Allow infinity precision */
drhe84a3062004-02-02 12:29:25 +000078
79
80/*
drha18c5682000-10-08 22:20:57 +000081** The following table is searched linearly, so it is good to put the
82** most frequently used conversion types first.
83*/
drh76ff3a02004-09-24 22:32:30 +000084static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
85static const char aPrefix[] = "-x0\000X0";
drh57196282004-10-06 15:41:16 +000086static const et_info fmtinfo[] = {
drh76ff3a02004-09-24 22:32:30 +000087 { 'd', 10, 1, etRADIX, 0, 0 },
drh4794f732004-11-05 17:17:50 +000088 { 's', 0, 4, etSTRING, 0, 0 },
drh557cc602005-08-13 12:59:14 +000089 { 'g', 0, 1, etGENERIC, 30, 0 },
drh153c62c2007-08-24 03:51:33 +000090 { 'z', 0, 4, etDYNSTRING, 0, 0 },
drh4794f732004-11-05 17:17:50 +000091 { 'q', 0, 4, etSQLESCAPE, 0, 0 },
92 { 'Q', 0, 4, etSQLESCAPE2, 0, 0 },
danielk1977f3b863e2007-06-24 06:32:17 +000093 { 'w', 0, 4, etSQLESCAPE3, 0, 0 },
drh76ff3a02004-09-24 22:32:30 +000094 { 'c', 0, 0, etCHARX, 0, 0 },
95 { 'o', 8, 0, etRADIX, 0, 2 },
96 { 'u', 10, 0, etRADIX, 0, 0 },
97 { 'x', 16, 0, etRADIX, 16, 1 },
98 { 'X', 16, 0, etRADIX, 0, 4 },
drhb37df7b2005-10-13 02:09:49 +000099#ifndef SQLITE_OMIT_FLOATING_POINT
drh76ff3a02004-09-24 22:32:30 +0000100 { 'f', 0, 1, etFLOAT, 0, 0 },
101 { 'e', 0, 1, etEXP, 30, 0 },
102 { 'E', 0, 1, etEXP, 14, 0 },
drh76ff3a02004-09-24 22:32:30 +0000103 { 'G', 0, 1, etGENERIC, 14, 0 },
drhb37df7b2005-10-13 02:09:49 +0000104#endif
drh76ff3a02004-09-24 22:32:30 +0000105 { 'i', 10, 1, etRADIX, 0, 0 },
106 { 'n', 0, 0, etSIZE, 0, 0 },
107 { '%', 0, 0, etPERCENT, 0, 0 },
108 { 'p', 16, 0, etPOINTER, 0, 1 },
drh7e3ff5d2009-04-08 11:49:42 +0000109
110/* All the rest have the FLAG_INTERN bit set and are thus for internal
111** use only */
drh76ff3a02004-09-24 22:32:30 +0000112 { 'T', 0, 2, etTOKEN, 0, 0 },
113 { 'S', 0, 2, etSRCLIST, 0, 0 },
drh9a993342007-12-13 02:45:31 +0000114 { 'r', 10, 3, etORDINAL, 0, 0 },
drha18c5682000-10-08 22:20:57 +0000115};
drha18c5682000-10-08 22:20:57 +0000116
117/*
drhb37df7b2005-10-13 02:09:49 +0000118** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
drha18c5682000-10-08 22:20:57 +0000119** conversions will work.
120*/
drhb37df7b2005-10-13 02:09:49 +0000121#ifndef SQLITE_OMIT_FLOATING_POINT
drha18c5682000-10-08 22:20:57 +0000122/*
123** "*val" is a double such that 0.1 <= *val < 10.0
124** Return the ascii code for the leading digit of *val, then
125** multiply "*val" by 10.0 to renormalize.
126**
127** Example:
128** input: *val = 3.14159
129** output: *val = 1.4159 function return = '3'
130**
131** The counter *cnt is incremented each time. After counter exceeds
132** 16 (the number of significant digits in a 64-bit float) '0' is
133** always returned.
134*/
drhea678832008-12-10 19:26:22 +0000135static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
drha18c5682000-10-08 22:20:57 +0000136 int digit;
drh384eef32004-01-07 03:04:27 +0000137 LONGDOUBLE_TYPE d;
drh72b3fbc2012-06-19 03:11:25 +0000138 if( (*cnt)<=0 ) return '0';
139 (*cnt)--;
drha18c5682000-10-08 22:20:57 +0000140 digit = (int)*val;
141 d = digit;
142 digit += '0';
143 *val = (*val - d)*10.0;
drhea678832008-12-10 19:26:22 +0000144 return (char)digit;
drha18c5682000-10-08 22:20:57 +0000145}
drhb37df7b2005-10-13 02:09:49 +0000146#endif /* SQLITE_OMIT_FLOATING_POINT */
drha18c5682000-10-08 22:20:57 +0000147
drh79158e12005-09-06 21:40:45 +0000148/*
drha6353a32013-12-09 19:03:26 +0000149** Set the StrAccum object to an error mode.
150*/
drha5c14162013-12-17 15:03:06 +0000151static void setStrAccumError(StrAccum *p, u8 eError){
drha6353a32013-12-09 19:03:26 +0000152 p->accError = eError;
153 p->nAlloc = 0;
154}
155
156/*
drha5c14162013-12-17 15:03:06 +0000157** Extra argument values from a PrintfArguments object
158*/
159static sqlite3_int64 getIntArg(PrintfArguments *p){
160 if( p->nArg<=p->nUsed ) return 0;
161 return sqlite3_value_int64(p->apArg[p->nUsed++]);
162}
163static double getDoubleArg(PrintfArguments *p){
164 if( p->nArg<=p->nUsed ) return 0.0;
165 return sqlite3_value_double(p->apArg[p->nUsed++]);
166}
167static char *getTextArg(PrintfArguments *p){
168 if( p->nArg<=p->nUsed ) return 0;
169 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
170}
171
172
173/*
drh79158e12005-09-06 21:40:45 +0000174** On machines with a small stack size, you can redefine the
drhed1fddf2011-10-12 18:52:59 +0000175** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
drh79158e12005-09-06 21:40:45 +0000176*/
177#ifndef SQLITE_PRINT_BUF_SIZE
drh59eedf72011-10-11 17:54:54 +0000178# define SQLITE_PRINT_BUF_SIZE 70
drh79158e12005-09-06 21:40:45 +0000179#endif
180#define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
drha18c5682000-10-08 22:20:57 +0000181
182/*
drhed1fddf2011-10-12 18:52:59 +0000183** Render a string given by "fmt" into the StrAccum object.
drha18c5682000-10-08 22:20:57 +0000184*/
drhf089aa42008-07-08 19:34:06 +0000185void sqlite3VXPrintf(
drha5c14162013-12-17 15:03:06 +0000186 StrAccum *pAccum, /* Accumulate results here */
187 u32 bFlags, /* SQLITE_PRINTF_* flags */
188 const char *fmt, /* Format string */
189 va_list ap /* arguments */
drha18c5682000-10-08 22:20:57 +0000190){
drhe84a3062004-02-02 12:29:25 +0000191 int c; /* Next character in the format string */
192 char *bufpt; /* Pointer to the conversion buffer */
193 int precision; /* Precision of the current field */
194 int length; /* Length of the field */
195 int idx; /* A general purpose loop counter */
drhe84a3062004-02-02 12:29:25 +0000196 int width; /* Width of the current field */
197 etByte flag_leftjustify; /* True if "-" flag is present */
198 etByte flag_plussign; /* True if "+" flag is present */
199 etByte flag_blanksign; /* True if " " flag is present */
200 etByte flag_alternateform; /* True if "#" flag is present */
drh531fe872005-08-13 13:40:42 +0000201 etByte flag_altform2; /* True if "!" flag is present */
drhe84a3062004-02-02 12:29:25 +0000202 etByte flag_zeropad; /* True if field width constant starts with zero */
203 etByte flag_long; /* True if "l" flag is present */
drha34b6762004-05-07 13:30:42 +0000204 etByte flag_longlong; /* True if the "ll" flag is present */
drh3e9aeec2005-08-13 13:39:02 +0000205 etByte done; /* Loop termination flag */
drhed1fddf2011-10-12 18:52:59 +0000206 etByte xtype = 0; /* Conversion paradigm */
drha5c14162013-12-17 15:03:06 +0000207 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
208 u8 useIntern; /* Ok to use internal conversions (ex: %T) */
drhed1fddf2011-10-12 18:52:59 +0000209 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
drh27436af2006-03-28 23:57:17 +0000210 sqlite_uint64 longvalue; /* Value for integer types */
drh384eef32004-01-07 03:04:27 +0000211 LONGDOUBLE_TYPE realvalue; /* Value for real types */
drh57196282004-10-06 15:41:16 +0000212 const et_info *infop; /* Pointer to the appropriate info structure */
drh59eedf72011-10-11 17:54:54 +0000213 char *zOut; /* Rendering buffer */
214 int nOut; /* Size of the rendering buffer */
drhaf8f5132014-10-29 18:20:18 +0000215 char *zExtra = 0; /* Malloced memory used by some conversion */
drhb37df7b2005-10-13 02:09:49 +0000216#ifndef SQLITE_OMIT_FLOATING_POINT
drh557cc602005-08-13 12:59:14 +0000217 int exp, e2; /* exponent of real numbers */
drhed1fddf2011-10-12 18:52:59 +0000218 int nsd; /* Number of significant digits returned */
drhe84a3062004-02-02 12:29:25 +0000219 double rounder; /* Used for rounding floating point values */
220 etByte flag_dp; /* True if decimal point should be shown */
221 etByte flag_rtz; /* True if trailing zeros should be removed */
drha18c5682000-10-08 22:20:57 +0000222#endif
drha5c14162013-12-17 15:03:06 +0000223 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
drhed1fddf2011-10-12 18:52:59 +0000224 char buf[etBUFSIZE]; /* Conversion buffer */
drha18c5682000-10-08 22:20:57 +0000225
drh9ca95732014-10-24 00:35:58 +0000226#ifdef SQLITE_ENABLE_API_ARMOR
227 if( ap==0 ){
228 (void)SQLITE_MISUSE_BKPT;
229 sqlite3StrAccumReset(pAccum);
230 return;
231 }
232#endif
drha18c5682000-10-08 22:20:57 +0000233 bufpt = 0;
drha5c14162013-12-17 15:03:06 +0000234 if( bFlags ){
235 if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){
236 pArgList = va_arg(ap, PrintfArguments*);
237 }
238 useIntern = bFlags & SQLITE_PRINTF_INTERNAL;
239 }else{
240 bArgList = useIntern = 0;
241 }
drha18c5682000-10-08 22:20:57 +0000242 for(; (c=(*fmt))!=0; ++fmt){
243 if( c!='%' ){
drha18c5682000-10-08 22:20:57 +0000244 bufpt = (char *)fmt;
drh760b1592014-09-18 01:50:09 +0000245#if HAVE_STRCHRNUL
246 fmt = strchrnul(fmt, '%');
247#else
248 do{ fmt++; }while( *fmt && *fmt != '%' );
249#endif
drha70a0732014-03-17 14:24:27 +0000250 sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt));
drh760b1592014-09-18 01:50:09 +0000251 if( *fmt==0 ) break;
drha18c5682000-10-08 22:20:57 +0000252 }
253 if( (c=(*++fmt))==0 ){
drhade86482007-11-28 22:36:40 +0000254 sqlite3StrAccumAppend(pAccum, "%", 1);
drha18c5682000-10-08 22:20:57 +0000255 break;
256 }
257 /* Find out what flags are present */
258 flag_leftjustify = flag_plussign = flag_blanksign =
drh557cc602005-08-13 12:59:14 +0000259 flag_alternateform = flag_altform2 = flag_zeropad = 0;
drh3e9aeec2005-08-13 13:39:02 +0000260 done = 0;
drha18c5682000-10-08 22:20:57 +0000261 do{
262 switch( c ){
drh3e9aeec2005-08-13 13:39:02 +0000263 case '-': flag_leftjustify = 1; break;
264 case '+': flag_plussign = 1; break;
265 case ' ': flag_blanksign = 1; break;
266 case '#': flag_alternateform = 1; break;
267 case '!': flag_altform2 = 1; break;
268 case '0': flag_zeropad = 1; break;
269 default: done = 1; break;
drha18c5682000-10-08 22:20:57 +0000270 }
drh3e9aeec2005-08-13 13:39:02 +0000271 }while( !done && (c=(*++fmt))!=0 );
drha18c5682000-10-08 22:20:57 +0000272 /* Get the field width */
273 width = 0;
274 if( c=='*' ){
drha5c14162013-12-17 15:03:06 +0000275 if( bArgList ){
276 width = (int)getIntArg(pArgList);
277 }else{
278 width = va_arg(ap,int);
279 }
drha18c5682000-10-08 22:20:57 +0000280 if( width<0 ){
281 flag_leftjustify = 1;
282 width = -width;
283 }
284 c = *++fmt;
285 }else{
drh17a68932001-01-31 13:28:08 +0000286 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000287 width = width*10 + c - '0';
288 c = *++fmt;
289 }
290 }
drha18c5682000-10-08 22:20:57 +0000291 /* Get the precision */
292 if( c=='.' ){
293 precision = 0;
294 c = *++fmt;
295 if( c=='*' ){
drha5c14162013-12-17 15:03:06 +0000296 if( bArgList ){
297 precision = (int)getIntArg(pArgList);
298 }else{
299 precision = va_arg(ap,int);
300 }
drha18c5682000-10-08 22:20:57 +0000301 if( precision<0 ) precision = -precision;
drha18c5682000-10-08 22:20:57 +0000302 c = *++fmt;
303 }else{
drh17a68932001-01-31 13:28:08 +0000304 while( c>='0' && c<='9' ){
drha18c5682000-10-08 22:20:57 +0000305 precision = precision*10 + c - '0';
306 c = *++fmt;
307 }
308 }
drha18c5682000-10-08 22:20:57 +0000309 }else{
310 precision = -1;
311 }
312 /* Get the conversion type modifier */
313 if( c=='l' ){
314 flag_long = 1;
315 c = *++fmt;
drha34b6762004-05-07 13:30:42 +0000316 if( c=='l' ){
317 flag_longlong = 1;
318 c = *++fmt;
319 }else{
320 flag_longlong = 0;
321 }
drha18c5682000-10-08 22:20:57 +0000322 }else{
drha34b6762004-05-07 13:30:42 +0000323 flag_long = flag_longlong = 0;
drha18c5682000-10-08 22:20:57 +0000324 }
325 /* Fetch the info entry for the field */
drh874ba042009-04-08 16:10:04 +0000326 infop = &fmtinfo[0];
327 xtype = etINVALID;
danielk197700e13612008-11-17 19:18:54 +0000328 for(idx=0; idx<ArraySize(fmtinfo); idx++){
drha18c5682000-10-08 22:20:57 +0000329 if( c==fmtinfo[idx].fmttype ){
330 infop = &fmtinfo[idx];
drha5c14162013-12-17 15:03:06 +0000331 if( useIntern || (infop->flags & FLAG_INTERN)==0 ){
drh5f968432004-02-21 19:02:30 +0000332 xtype = infop->type;
drhc4413562006-05-22 22:04:00 +0000333 }else{
drhade86482007-11-28 22:36:40 +0000334 return;
drh5f968432004-02-21 19:02:30 +0000335 }
drha18c5682000-10-08 22:20:57 +0000336 break;
337 }
338 }
drh43617e92006-03-06 20:55:46 +0000339
drha18c5682000-10-08 22:20:57 +0000340 /*
341 ** At this point, variables are initialized as follows:
342 **
343 ** flag_alternateform TRUE if a '#' is present.
drh3e9aeec2005-08-13 13:39:02 +0000344 ** flag_altform2 TRUE if a '!' is present.
drha18c5682000-10-08 22:20:57 +0000345 ** flag_plussign TRUE if a '+' is present.
346 ** flag_leftjustify TRUE if a '-' is present or if the
347 ** field width was negative.
348 ** flag_zeropad TRUE if the width began with 0.
349 ** flag_long TRUE if the letter 'l' (ell) prefixed
350 ** the conversion character.
drha34b6762004-05-07 13:30:42 +0000351 ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed
352 ** the conversion character.
drha18c5682000-10-08 22:20:57 +0000353 ** flag_blanksign TRUE if a ' ' is present.
354 ** width The specified field width. This is
355 ** always non-negative. Zero is the default.
356 ** precision The specified precision. The default
357 ** is -1.
358 ** xtype The class of the conversion.
359 ** infop Pointer to the appropriate info struct.
360 */
361 switch( xtype ){
drhfe63d1c2004-09-08 20:13:04 +0000362 case etPOINTER:
363 flag_longlong = sizeof(char*)==sizeof(i64);
364 flag_long = sizeof(char*)==sizeof(long int);
365 /* Fall through into the next case */
drh9a993342007-12-13 02:45:31 +0000366 case etORDINAL:
drha18c5682000-10-08 22:20:57 +0000367 case etRADIX:
drhe84a3062004-02-02 12:29:25 +0000368 if( infop->flags & FLAG_SIGNED ){
drhe9707672004-06-25 01:10:48 +0000369 i64 v;
drha5c14162013-12-17 15:03:06 +0000370 if( bArgList ){
371 v = getIntArg(pArgList);
372 }else if( flag_longlong ){
drheeb23a42009-05-04 20:20:16 +0000373 v = va_arg(ap,i64);
374 }else if( flag_long ){
375 v = va_arg(ap,long int);
376 }else{
377 v = va_arg(ap,int);
378 }
drhe9707672004-06-25 01:10:48 +0000379 if( v<0 ){
drh158b9cb2011-03-05 20:59:46 +0000380 if( v==SMALLEST_INT64 ){
381 longvalue = ((u64)1)<<63;
382 }else{
383 longvalue = -v;
384 }
drhe9707672004-06-25 01:10:48 +0000385 prefix = '-';
danielk1977cfcdaef2004-05-12 07:33:33 +0000386 }else{
drhe9707672004-06-25 01:10:48 +0000387 longvalue = v;
388 if( flag_plussign ) prefix = '+';
danielk1977cfcdaef2004-05-12 07:33:33 +0000389 else if( flag_blanksign ) prefix = ' ';
390 else prefix = 0;
391 }
drhe9707672004-06-25 01:10:48 +0000392 }else{
drha5c14162013-12-17 15:03:06 +0000393 if( bArgList ){
394 longvalue = (u64)getIntArg(pArgList);
395 }else if( flag_longlong ){
drheeb23a42009-05-04 20:20:16 +0000396 longvalue = va_arg(ap,u64);
397 }else if( flag_long ){
398 longvalue = va_arg(ap,unsigned long int);
399 }else{
400 longvalue = va_arg(ap,unsigned int);
401 }
drhe9707672004-06-25 01:10:48 +0000402 prefix = 0;
403 }
404 if( longvalue==0 ) flag_alternateform = 0;
drha18c5682000-10-08 22:20:57 +0000405 if( flag_zeropad && precision<width-(prefix!=0) ){
406 precision = width-(prefix!=0);
drh9adf9ac2002-05-15 11:44:13 +0000407 }
drh59eedf72011-10-11 17:54:54 +0000408 if( precision<etBUFSIZE-10 ){
409 nOut = etBUFSIZE;
410 zOut = buf;
411 }else{
412 nOut = precision + 10;
413 zOut = zExtra = sqlite3Malloc( nOut );
414 if( zOut==0 ){
drha6353a32013-12-09 19:03:26 +0000415 setStrAccumError(pAccum, STRACCUM_NOMEM);
drh59eedf72011-10-11 17:54:54 +0000416 return;
417 }
418 }
419 bufpt = &zOut[nOut-1];
drh9a993342007-12-13 02:45:31 +0000420 if( xtype==etORDINAL ){
drh43f6e062007-12-13 17:50:22 +0000421 static const char zOrd[] = "thstndrd";
drhea678832008-12-10 19:26:22 +0000422 int x = (int)(longvalue % 10);
drh43f6e062007-12-13 17:50:22 +0000423 if( x>=4 || (longvalue/10)%10==1 ){
424 x = 0;
425 }
drh59eedf72011-10-11 17:54:54 +0000426 *(--bufpt) = zOrd[x*2+1];
427 *(--bufpt) = zOrd[x*2];
drh9a993342007-12-13 02:45:31 +0000428 }
drha18c5682000-10-08 22:20:57 +0000429 {
drh0e682092014-03-17 15:06:57 +0000430 const char *cset = &aDigits[infop->charset];
431 u8 base = infop->base;
drha18c5682000-10-08 22:20:57 +0000432 do{ /* Convert to ascii */
433 *(--bufpt) = cset[longvalue%base];
434 longvalue = longvalue/base;
435 }while( longvalue>0 );
drh9adf9ac2002-05-15 11:44:13 +0000436 }
drh59eedf72011-10-11 17:54:54 +0000437 length = (int)(&zOut[nOut-1]-bufpt);
drha18c5682000-10-08 22:20:57 +0000438 for(idx=precision-length; idx>0; idx--){
439 *(--bufpt) = '0'; /* Zero pad */
drh9adf9ac2002-05-15 11:44:13 +0000440 }
drha18c5682000-10-08 22:20:57 +0000441 if( prefix ) *(--bufpt) = prefix; /* Add sign */
442 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
drh76ff3a02004-09-24 22:32:30 +0000443 const char *pre;
444 char x;
445 pre = &aPrefix[infop->prefix];
drhaf005fb2008-07-09 16:51:51 +0000446 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
drha18c5682000-10-08 22:20:57 +0000447 }
drh59eedf72011-10-11 17:54:54 +0000448 length = (int)(&zOut[nOut-1]-bufpt);
drha18c5682000-10-08 22:20:57 +0000449 break;
450 case etFLOAT:
451 case etEXP:
452 case etGENERIC:
drha5c14162013-12-17 15:03:06 +0000453 if( bArgList ){
454 realvalue = getDoubleArg(pArgList);
455 }else{
456 realvalue = va_arg(ap,double);
457 }
drh69ef7032010-03-04 17:11:31 +0000458#ifdef SQLITE_OMIT_FLOATING_POINT
459 length = 0;
460#else
drha18c5682000-10-08 22:20:57 +0000461 if( precision<0 ) precision = 6; /* Set default precision */
drha18c5682000-10-08 22:20:57 +0000462 if( realvalue<0.0 ){
463 realvalue = -realvalue;
464 prefix = '-';
drh9adf9ac2002-05-15 11:44:13 +0000465 }else{
drha18c5682000-10-08 22:20:57 +0000466 if( flag_plussign ) prefix = '+';
467 else if( flag_blanksign ) prefix = ' ';
468 else prefix = 0;
drh9adf9ac2002-05-15 11:44:13 +0000469 }
drh3e9aeec2005-08-13 13:39:02 +0000470 if( xtype==etGENERIC && precision>0 ) precision--;
drh74161702006-02-24 02:53:49 +0000471 for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
drh3e9aeec2005-08-13 13:39:02 +0000472 if( xtype==etFLOAT ) realvalue += rounder;
drha18c5682000-10-08 22:20:57 +0000473 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
474 exp = 0;
drhea678832008-12-10 19:26:22 +0000475 if( sqlite3IsNaN((double)realvalue) ){
drh53c14022007-05-10 17:23:11 +0000476 bufpt = "NaN";
477 length = 3;
478 break;
479 }
drha18c5682000-10-08 22:20:57 +0000480 if( realvalue>0.0 ){
drh72b3fbc2012-06-19 03:11:25 +0000481 LONGDOUBLE_TYPE scale = 1.0;
drh4ef94132012-06-19 03:35:05 +0000482 while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
drh72b3fbc2012-06-19 03:11:25 +0000483 while( realvalue>=1e64*scale && exp<=350 ){ scale *= 1e64; exp+=64; }
484 while( realvalue>=1e8*scale && exp<=350 ){ scale *= 1e8; exp+=8; }
485 while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
486 realvalue /= scale;
drh93a960a2008-07-10 00:32:42 +0000487 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
488 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
drhaf005fb2008-07-09 16:51:51 +0000489 if( exp>350 ){
drh53c14022007-05-10 17:23:11 +0000490 if( prefix=='-' ){
491 bufpt = "-Inf";
492 }else if( prefix=='+' ){
493 bufpt = "+Inf";
494 }else{
495 bufpt = "Inf";
496 }
drhea678832008-12-10 19:26:22 +0000497 length = sqlite3Strlen30(bufpt);
drha18c5682000-10-08 22:20:57 +0000498 break;
499 }
drh9adf9ac2002-05-15 11:44:13 +0000500 }
drha18c5682000-10-08 22:20:57 +0000501 bufpt = buf;
502 /*
503 ** If the field type is etGENERIC, then convert to either etEXP
504 ** or etFLOAT, as appropriate.
505 */
drha18c5682000-10-08 22:20:57 +0000506 if( xtype!=etFLOAT ){
507 realvalue += rounder;
508 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
509 }
510 if( xtype==etGENERIC ){
511 flag_rtz = !flag_alternateform;
512 if( exp<-4 || exp>precision ){
513 xtype = etEXP;
514 }else{
515 precision = precision - exp;
516 xtype = etFLOAT;
517 }
drh9adf9ac2002-05-15 11:44:13 +0000518 }else{
drh72b3fbc2012-06-19 03:11:25 +0000519 flag_rtz = flag_altform2;
drh9adf9ac2002-05-15 11:44:13 +0000520 }
drh557cc602005-08-13 12:59:14 +0000521 if( xtype==etEXP ){
522 e2 = 0;
523 }else{
524 e2 = exp;
525 }
drhe8c13bf2013-07-08 22:33:20 +0000526 if( MAX(e2,0)+precision+width > etBUFSIZE - 15 ){
527 bufpt = zExtra = sqlite3Malloc( MAX(e2,0)+precision+width+15 );
drh59eedf72011-10-11 17:54:54 +0000528 if( bufpt==0 ){
drha6353a32013-12-09 19:03:26 +0000529 setStrAccumError(pAccum, STRACCUM_NOMEM);
drh59eedf72011-10-11 17:54:54 +0000530 return;
531 }
532 }
533 zOut = bufpt;
drh72b3fbc2012-06-19 03:11:25 +0000534 nsd = 16 + flag_altform2*10;
drhea678832008-12-10 19:26:22 +0000535 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
drh557cc602005-08-13 12:59:14 +0000536 /* The sign in front of the number */
537 if( prefix ){
538 *(bufpt++) = prefix;
539 }
540 /* Digits prior to the decimal point */
541 if( e2<0 ){
542 *(bufpt++) = '0';
543 }else{
544 for(; e2>=0; e2--){
545 *(bufpt++) = et_getdigit(&realvalue,&nsd);
drha18c5682000-10-08 22:20:57 +0000546 }
drh9adf9ac2002-05-15 11:44:13 +0000547 }
drh557cc602005-08-13 12:59:14 +0000548 /* The decimal point */
549 if( flag_dp ){
550 *(bufpt++) = '.';
551 }
552 /* "0" digits after the decimal point but before the first
553 ** significant digit of the number */
drhaf005fb2008-07-09 16:51:51 +0000554 for(e2++; e2<0; precision--, e2++){
555 assert( precision>0 );
drh557cc602005-08-13 12:59:14 +0000556 *(bufpt++) = '0';
557 }
558 /* Significant digits after the decimal point */
559 while( (precision--)>0 ){
560 *(bufpt++) = et_getdigit(&realvalue,&nsd);
561 }
562 /* Remove trailing zeros and the "." if no digits follow the "." */
563 if( flag_rtz && flag_dp ){
drh3e9aeec2005-08-13 13:39:02 +0000564 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
drh59eedf72011-10-11 17:54:54 +0000565 assert( bufpt>zOut );
drh3e9aeec2005-08-13 13:39:02 +0000566 if( bufpt[-1]=='.' ){
drh557cc602005-08-13 12:59:14 +0000567 if( flag_altform2 ){
568 *(bufpt++) = '0';
569 }else{
570 *(--bufpt) = 0;
571 }
572 }
573 }
574 /* Add the "eNNN" suffix */
drh59eedf72011-10-11 17:54:54 +0000575 if( xtype==etEXP ){
drh557cc602005-08-13 12:59:14 +0000576 *(bufpt++) = aDigits[infop->charset];
577 if( exp<0 ){
578 *(bufpt++) = '-'; exp = -exp;
579 }else{
580 *(bufpt++) = '+';
581 }
582 if( exp>=100 ){
drhea678832008-12-10 19:26:22 +0000583 *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */
drh557cc602005-08-13 12:59:14 +0000584 exp %= 100;
585 }
drhea678832008-12-10 19:26:22 +0000586 *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */
587 *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */
drh557cc602005-08-13 12:59:14 +0000588 }
589 *bufpt = 0;
590
drha18c5682000-10-08 22:20:57 +0000591 /* The converted number is in buf[] and zero terminated. Output it.
592 ** Note that the number is in the usual order, not reversed as with
593 ** integer conversions. */
drh59eedf72011-10-11 17:54:54 +0000594 length = (int)(bufpt-zOut);
595 bufpt = zOut;
drha18c5682000-10-08 22:20:57 +0000596
597 /* Special case: Add leading zeros if the flag_zeropad flag is
598 ** set and we are not left justified */
599 if( flag_zeropad && !flag_leftjustify && length < width){
600 int i;
601 int nPad = width - length;
602 for(i=width; i>=nPad; i--){
603 bufpt[i] = bufpt[i-nPad];
604 }
605 i = prefix!=0;
606 while( nPad-- ) bufpt[i++] = '0';
607 length = width;
608 }
drh69ef7032010-03-04 17:11:31 +0000609#endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
drha18c5682000-10-08 22:20:57 +0000610 break;
611 case etSIZE:
drhfc6ee9d2013-12-17 15:58:42 +0000612 if( !bArgList ){
613 *(va_arg(ap,int*)) = pAccum->nChar;
614 }
drha18c5682000-10-08 22:20:57 +0000615 length = width = 0;
616 break;
617 case etPERCENT:
618 buf[0] = '%';
619 bufpt = buf;
620 length = 1;
621 break;
drha18c5682000-10-08 22:20:57 +0000622 case etCHARX:
drha5c14162013-12-17 15:03:06 +0000623 if( bArgList ){
drhfc6ee9d2013-12-17 15:58:42 +0000624 bufpt = getTextArg(pArgList);
625 c = bufpt ? bufpt[0] : 0;
drha5c14162013-12-17 15:03:06 +0000626 }else{
627 c = va_arg(ap,int);
628 }
drhaf8f5132014-10-29 18:20:18 +0000629 if( precision>1 ){
630 width -= precision-1;
631 if( width>1 && !flag_leftjustify ){
632 sqlite3AppendChar(pAccum, width-1, ' ');
633 width = 0;
634 }
635 sqlite3AppendChar(pAccum, precision-1, c);
drh9adf9ac2002-05-15 11:44:13 +0000636 }
drhaf8f5132014-10-29 18:20:18 +0000637 length = 1;
638 buf[0] = c;
drha18c5682000-10-08 22:20:57 +0000639 bufpt = buf;
640 break;
641 case etSTRING:
drhd93d8a82003-06-16 03:08:18 +0000642 case etDYNSTRING:
drha5c14162013-12-17 15:03:06 +0000643 if( bArgList ){
644 bufpt = getTextArg(pArgList);
645 }else{
646 bufpt = va_arg(ap,char*);
647 }
drhd93d8a82003-06-16 03:08:18 +0000648 if( bufpt==0 ){
649 bufpt = "";
drha5c14162013-12-17 15:03:06 +0000650 }else if( xtype==etDYNSTRING && !bArgList ){
drhd93d8a82003-06-16 03:08:18 +0000651 zExtra = bufpt;
652 }
drhe5090942008-04-29 15:22:27 +0000653 if( precision>=0 ){
654 for(length=0; length<precision && bufpt[length]; length++){}
655 }else{
drhea678832008-12-10 19:26:22 +0000656 length = sqlite3Strlen30(bufpt);
drhe5090942008-04-29 15:22:27 +0000657 }
drha18c5682000-10-08 22:20:57 +0000658 break;
659 case etSQLESCAPE:
danielk1977f3b863e2007-06-24 06:32:17 +0000660 case etSQLESCAPE2:
661 case etSQLESCAPE3: {
drh8965b502009-11-25 16:53:37 +0000662 int i, j, k, n, isnull;
drh5eba8c02005-08-19 02:26:27 +0000663 int needQuote;
drhea678832008-12-10 19:26:22 +0000664 char ch;
danielk1977f3b863e2007-06-24 06:32:17 +0000665 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
drha5c14162013-12-17 15:03:06 +0000666 char *escarg;
667
668 if( bArgList ){
669 escarg = getTextArg(pArgList);
670 }else{
671 escarg = va_arg(ap,char*);
672 }
danielk1977f0113002006-01-24 12:09:17 +0000673 isnull = escarg==0;
674 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
drh8965b502009-11-25 16:53:37 +0000675 k = precision;
dan60d4a302010-03-04 17:58:45 +0000676 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
danielk1977f3b863e2007-06-24 06:32:17 +0000677 if( ch==q ) n++;
drha18c5682000-10-08 22:20:57 +0000678 }
drh5eba8c02005-08-19 02:26:27 +0000679 needQuote = !isnull && xtype==etSQLESCAPE2;
680 n += i + 1 + needQuote*2;
681 if( n>etBUFSIZE ){
drhe5ae5732008-06-15 02:51:47 +0000682 bufpt = zExtra = sqlite3Malloc( n );
drhd164fd32008-11-20 18:20:28 +0000683 if( bufpt==0 ){
drha6353a32013-12-09 19:03:26 +0000684 setStrAccumError(pAccum, STRACCUM_NOMEM);
drhd164fd32008-11-20 18:20:28 +0000685 return;
686 }
drh5eba8c02005-08-19 02:26:27 +0000687 }else{
688 bufpt = buf;
689 }
690 j = 0;
danielk1977f3b863e2007-06-24 06:32:17 +0000691 if( needQuote ) bufpt[j++] = q;
drh8965b502009-11-25 16:53:37 +0000692 k = i;
693 for(i=0; i<k; i++){
694 bufpt[j++] = ch = escarg[i];
danielk1977f3b863e2007-06-24 06:32:17 +0000695 if( ch==q ) bufpt[j++] = ch;
drh5eba8c02005-08-19 02:26:27 +0000696 }
danielk1977f3b863e2007-06-24 06:32:17 +0000697 if( needQuote ) bufpt[j++] = q;
drh5eba8c02005-08-19 02:26:27 +0000698 bufpt[j] = 0;
699 length = j;
drh8965b502009-11-25 16:53:37 +0000700 /* The precision in %q and %Q means how many input characters to
701 ** consume, not the length of the output...
702 ** if( precision>=0 && precision<length ) length = precision; */
drha18c5682000-10-08 22:20:57 +0000703 break;
drh5eba8c02005-08-19 02:26:27 +0000704 }
drh5f968432004-02-21 19:02:30 +0000705 case etTOKEN: {
706 Token *pToken = va_arg(ap, Token*);
drha5c14162013-12-17 15:03:06 +0000707 assert( bArgList==0 );
drha9ab4812013-12-11 11:00:44 +0000708 if( pToken && pToken->n ){
drhade86482007-11-28 22:36:40 +0000709 sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
drhad6d9462004-09-19 02:15:24 +0000710 }
drh5f968432004-02-21 19:02:30 +0000711 length = width = 0;
712 break;
713 }
714 case etSRCLIST: {
715 SrcList *pSrc = va_arg(ap, SrcList*);
716 int k = va_arg(ap, int);
717 struct SrcList_item *pItem = &pSrc->a[k];
drha5c14162013-12-17 15:03:06 +0000718 assert( bArgList==0 );
drh5f968432004-02-21 19:02:30 +0000719 assert( k>=0 && k<pSrc->nSrc );
drh93a960a2008-07-10 00:32:42 +0000720 if( pItem->zDatabase ){
drha6353a32013-12-09 19:03:26 +0000721 sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
drhade86482007-11-28 22:36:40 +0000722 sqlite3StrAccumAppend(pAccum, ".", 1);
drh5f968432004-02-21 19:02:30 +0000723 }
drha6353a32013-12-09 19:03:26 +0000724 sqlite3StrAccumAppendAll(pAccum, pItem->zName);
drh5f968432004-02-21 19:02:30 +0000725 length = width = 0;
726 break;
727 }
drh874ba042009-04-08 16:10:04 +0000728 default: {
729 assert( xtype==etINVALID );
730 return;
731 }
drha18c5682000-10-08 22:20:57 +0000732 }/* End switch over the format type */
733 /*
734 ** The text of the conversion is pointed to by "bufpt" and is
735 ** "length" characters long. The field width is "width". Do
736 ** the output.
737 */
drha70a0732014-03-17 14:24:27 +0000738 width -= length;
drhaf8f5132014-10-29 18:20:18 +0000739 if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
drha70a0732014-03-17 14:24:27 +0000740 sqlite3StrAccumAppend(pAccum, bufpt, length);
drhaf8f5132014-10-29 18:20:18 +0000741 if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
drha70a0732014-03-17 14:24:27 +0000742
drhaf8f5132014-10-29 18:20:18 +0000743 if( zExtra ){
744 sqlite3_free(zExtra);
745 zExtra = 0;
746 }
drha18c5682000-10-08 22:20:57 +0000747 }/* End for loop over the format string */
drha18c5682000-10-08 22:20:57 +0000748} /* End of function */
749
drhade86482007-11-28 22:36:40 +0000750/*
drha70a0732014-03-17 14:24:27 +0000751** Enlarge the memory allocation on a StrAccum object so that it is
752** able to accept at least N more bytes of text.
753**
754** Return the number of bytes of text that StrAccum is able to accept
755** after the attempted enlargement. The value returned might be zero.
756*/
757static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
758 char *zNew;
759 assert( p->nChar+N >= p->nAlloc ); /* Only called if really needed */
760 if( p->accError ){
761 testcase(p->accError==STRACCUM_TOOBIG);
762 testcase(p->accError==STRACCUM_NOMEM);
763 return 0;
764 }
765 if( !p->useMalloc ){
766 N = p->nAlloc - p->nChar - 1;
767 setStrAccumError(p, STRACCUM_TOOBIG);
768 return N;
769 }else{
770 char *zOld = (p->zText==p->zBase ? 0 : p->zText);
771 i64 szNew = p->nChar;
772 szNew += N + 1;
drh7b4d7802014-11-03 14:46:29 +0000773 if( szNew+p->nChar<=p->mxAlloc ){
774 /* Force exponential buffer size growth as long as it does not overflow,
775 ** to avoid having to call this routine too often */
776 szNew += p->nChar;
777 }
drha70a0732014-03-17 14:24:27 +0000778 if( szNew > p->mxAlloc ){
779 sqlite3StrAccumReset(p);
780 setStrAccumError(p, STRACCUM_TOOBIG);
781 return 0;
782 }else{
783 p->nAlloc = (int)szNew;
784 }
785 if( p->useMalloc==1 ){
786 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
787 }else{
788 zNew = sqlite3_realloc(zOld, p->nAlloc);
789 }
790 if( zNew ){
drh7ef4d1c2014-05-31 15:39:53 +0000791 assert( p->zText!=0 || p->nChar==0 );
drha70a0732014-03-17 14:24:27 +0000792 if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
793 p->zText = zNew;
drh7f5a7ec2014-11-03 13:24:12 +0000794 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
drha70a0732014-03-17 14:24:27 +0000795 }else{
796 sqlite3StrAccumReset(p);
797 setStrAccumError(p, STRACCUM_NOMEM);
798 return 0;
799 }
800 }
801 return N;
802}
803
804/*
drhaf8f5132014-10-29 18:20:18 +0000805** Append N copies of character c to the given string buffer.
drha70a0732014-03-17 14:24:27 +0000806*/
drhaf8f5132014-10-29 18:20:18 +0000807void sqlite3AppendChar(StrAccum *p, int N, char c){
drha70a0732014-03-17 14:24:27 +0000808 if( p->nChar+N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ) return;
drhaf8f5132014-10-29 18:20:18 +0000809 while( (N--)>0 ) p->zText[p->nChar++] = c;
drha70a0732014-03-17 14:24:27 +0000810}
811
812/*
813** The StrAccum "p" is not large enough to accept N new bytes of z[].
814** So enlarge if first, then do the append.
815**
816** This is a helper routine to sqlite3StrAccumAppend() that does special-case
817** work (enlarging the buffer) using tail recursion, so that the
818** sqlite3StrAccumAppend() routine can use fast calling semantics.
819*/
drh172087f2014-08-22 15:40:20 +0000820static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
drha70a0732014-03-17 14:24:27 +0000821 N = sqlite3StrAccumEnlarge(p, N);
822 if( N>0 ){
823 memcpy(&p->zText[p->nChar], z, N);
824 p->nChar += N;
825 }
826}
827
828/*
829** Append N bytes of text from z to the StrAccum object. Increase the
830** size of the memory allocation for StrAccum if necessary.
drha18c5682000-10-08 22:20:57 +0000831*/
drhade86482007-11-28 22:36:40 +0000832void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
drha9ab4812013-12-11 11:00:44 +0000833 assert( z!=0 );
drha6353a32013-12-09 19:03:26 +0000834 assert( p->zText!=0 || p->nChar==0 || p->accError );
835 assert( N>=0 );
836 assert( p->accError==0 || p->nAlloc==0 );
drhade86482007-11-28 22:36:40 +0000837 if( p->nChar+N >= p->nAlloc ){
drha70a0732014-03-17 14:24:27 +0000838 enlargeAndAppend(p,z,N);
drh172087f2014-08-22 15:40:20 +0000839 }else{
840 assert( p->zText );
841 p->nChar += N;
842 memcpy(&p->zText[p->nChar-N], z, N);
drh5f968432004-02-21 19:02:30 +0000843 }
drh483750b2003-01-29 18:46:51 +0000844}
845
846/*
drha6353a32013-12-09 19:03:26 +0000847** Append the complete text of zero-terminated string z[] to the p string.
848*/
849void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){
mistachkin53cd9642013-12-11 02:21:19 +0000850 sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z));
drha6353a32013-12-09 19:03:26 +0000851}
852
853
854/*
drhade86482007-11-28 22:36:40 +0000855** Finish off a string by making sure it is zero-terminated.
856** Return a pointer to the resulting string. Return a NULL
857** pointer if any kind of error was encountered.
drh5f968432004-02-21 19:02:30 +0000858*/
drhade86482007-11-28 22:36:40 +0000859char *sqlite3StrAccumFinish(StrAccum *p){
860 if( p->zText ){
861 p->zText[p->nChar] = 0;
862 if( p->useMalloc && p->zText==p->zBase ){
drhb9755982010-07-24 16:34:37 +0000863 if( p->useMalloc==1 ){
864 p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
865 }else{
866 p->zText = sqlite3_malloc(p->nChar+1);
867 }
drhade86482007-11-28 22:36:40 +0000868 if( p->zText ){
869 memcpy(p->zText, p->zBase, p->nChar+1);
870 }else{
drha6353a32013-12-09 19:03:26 +0000871 setStrAccumError(p, STRACCUM_NOMEM);
drhade86482007-11-28 22:36:40 +0000872 }
873 }
874 }
875 return p->zText;
876}
877
878/*
879** Reset an StrAccum string. Reclaim all malloced memory.
880*/
881void sqlite3StrAccumReset(StrAccum *p){
882 if( p->zText!=p->zBase ){
drhb9755982010-07-24 16:34:37 +0000883 if( p->useMalloc==1 ){
884 sqlite3DbFree(p->db, p->zText);
885 }else{
886 sqlite3_free(p->zText);
887 }
drhade86482007-11-28 22:36:40 +0000888 }
drhf089aa42008-07-08 19:34:06 +0000889 p->zText = 0;
drhade86482007-11-28 22:36:40 +0000890}
891
892/*
893** Initialize a string accumulator
894*/
drhf089aa42008-07-08 19:34:06 +0000895void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
drhade86482007-11-28 22:36:40 +0000896 p->zText = p->zBase = zBase;
drh633e6d52008-07-28 19:34:53 +0000897 p->db = 0;
drhade86482007-11-28 22:36:40 +0000898 p->nChar = 0;
899 p->nAlloc = n;
drhbb4957f2008-03-20 14:03:29 +0000900 p->mxAlloc = mx;
drhade86482007-11-28 22:36:40 +0000901 p->useMalloc = 1;
drhb49bc862013-08-21 21:12:10 +0000902 p->accError = 0;
drh5f968432004-02-21 19:02:30 +0000903}
904
905/*
906** Print into memory obtained from sqliteMalloc(). Use the internal
907** %-conversion extensions.
908*/
drh17435752007-08-16 04:30:38 +0000909char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
910 char *z;
drh79158e12005-09-06 21:40:45 +0000911 char zBase[SQLITE_PRINT_BUF_SIZE];
drhade86482007-11-28 22:36:40 +0000912 StrAccum acc;
drhbc6160b2009-04-08 15:45:31 +0000913 assert( db!=0 );
drhbb4957f2008-03-20 14:03:29 +0000914 sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
drhbc6160b2009-04-08 15:45:31 +0000915 db->aLimit[SQLITE_LIMIT_LENGTH]);
drh633e6d52008-07-28 19:34:53 +0000916 acc.db = db;
drha5c14162013-12-17 15:03:06 +0000917 sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap);
drhade86482007-11-28 22:36:40 +0000918 z = sqlite3StrAccumFinish(&acc);
drhb49bc862013-08-21 21:12:10 +0000919 if( acc.accError==STRACCUM_NOMEM ){
drh17435752007-08-16 04:30:38 +0000920 db->mallocFailed = 1;
921 }
922 return z;
drh5f968432004-02-21 19:02:30 +0000923}
924
925/*
926** Print into memory obtained from sqliteMalloc(). Use the internal
927** %-conversion extensions.
928*/
drh17435752007-08-16 04:30:38 +0000929char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000930 va_list ap;
931 char *z;
drh5f968432004-02-21 19:02:30 +0000932 va_start(ap, zFormat);
drhade86482007-11-28 22:36:40 +0000933 z = sqlite3VMPrintf(db, zFormat, ap);
drh5f968432004-02-21 19:02:30 +0000934 va_end(ap);
935 return z;
936}
937
938/*
drh633e6d52008-07-28 19:34:53 +0000939** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
peter.d.reid60ec9142014-09-06 16:39:46 +0000940** the string and before returning. This routine is intended to be used
drh633e6d52008-07-28 19:34:53 +0000941** to modify an existing string. For example:
942**
943** x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
944**
945*/
946char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
947 va_list ap;
948 char *z;
949 va_start(ap, zFormat);
950 z = sqlite3VMPrintf(db, zFormat, ap);
951 va_end(ap);
952 sqlite3DbFree(db, zStr);
953 return z;
954}
955
956/*
drh28dd4792006-06-26 21:35:44 +0000957** Print into memory obtained from sqlite3_malloc(). Omit the internal
958** %-conversion extensions.
959*/
960char *sqlite3_vmprintf(const char *zFormat, va_list ap){
drhade86482007-11-28 22:36:40 +0000961 char *z;
drh28dd4792006-06-26 21:35:44 +0000962 char zBase[SQLITE_PRINT_BUF_SIZE];
drhade86482007-11-28 22:36:40 +0000963 StrAccum acc;
drh9ca95732014-10-24 00:35:58 +0000964
965#ifdef SQLITE_ENABLE_API_ARMOR
966 if( zFormat==0 ){
967 (void)SQLITE_MISUSE_BKPT;
968 return 0;
969 }
970#endif
drhff1590e2008-07-14 12:52:53 +0000971#ifndef SQLITE_OMIT_AUTOINIT
972 if( sqlite3_initialize() ) return 0;
973#endif
drhbb4957f2008-03-20 14:03:29 +0000974 sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
drhb9755982010-07-24 16:34:37 +0000975 acc.useMalloc = 2;
drhf089aa42008-07-08 19:34:06 +0000976 sqlite3VXPrintf(&acc, 0, zFormat, ap);
drhade86482007-11-28 22:36:40 +0000977 z = sqlite3StrAccumFinish(&acc);
978 return z;
drh28dd4792006-06-26 21:35:44 +0000979}
980
981/*
982** Print into memory obtained from sqlite3_malloc()(). Omit the internal
983** %-conversion extensions.
drh483750b2003-01-29 18:46:51 +0000984*/
danielk19776f8a5032004-05-10 10:34:51 +0000985char *sqlite3_mprintf(const char *zFormat, ...){
drha18c5682000-10-08 22:20:57 +0000986 va_list ap;
drh5f968432004-02-21 19:02:30 +0000987 char *z;
drhff1590e2008-07-14 12:52:53 +0000988#ifndef SQLITE_OMIT_AUTOINIT
989 if( sqlite3_initialize() ) return 0;
990#endif
drh28dd4792006-06-26 21:35:44 +0000991 va_start(ap, zFormat);
drhb3738b62007-03-31 15:02:49 +0000992 z = sqlite3_vmprintf(zFormat, ap);
drha18c5682000-10-08 22:20:57 +0000993 va_end(ap);
drh5f968432004-02-21 19:02:30 +0000994 return z;
drha18c5682000-10-08 22:20:57 +0000995}
996
drh93a5c6b2003-12-23 02:17:35 +0000997/*
danielk19776f8a5032004-05-10 10:34:51 +0000998** sqlite3_snprintf() works like snprintf() except that it ignores the
drh93a5c6b2003-12-23 02:17:35 +0000999** current locale settings. This is important for SQLite because we
1000** are not able to use a "," as the decimal point in place of "." as
1001** specified by some locales.
drhdb26d4c2011-01-05 12:20:09 +00001002**
1003** Oops: The first two arguments of sqlite3_snprintf() are backwards
1004** from the snprintf() standard. Unfortunately, it is too late to change
1005** this without breaking compatibility, so we just have to live with the
1006** mistake.
1007**
1008** sqlite3_vsnprintf() is the varargs version.
drh93a5c6b2003-12-23 02:17:35 +00001009*/
drhdb26d4c2011-01-05 12:20:09 +00001010char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
1011 StrAccum acc;
1012 if( n<=0 ) return zBuf;
drh9ca95732014-10-24 00:35:58 +00001013#ifdef SQLITE_ENABLE_API_ARMOR
1014 if( zBuf==0 || zFormat==0 ) {
1015 (void)SQLITE_MISUSE_BKPT;
1016 if( zBuf && n>0 ) zBuf[0] = 0;
1017 return zBuf;
1018 }
1019#endif
drhdb26d4c2011-01-05 12:20:09 +00001020 sqlite3StrAccumInit(&acc, zBuf, n, 0);
1021 acc.useMalloc = 0;
1022 sqlite3VXPrintf(&acc, 0, zFormat, ap);
1023 return sqlite3StrAccumFinish(&acc);
1024}
danielk19776f8a5032004-05-10 10:34:51 +00001025char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +00001026 char *z;
drh93a5c6b2003-12-23 02:17:35 +00001027 va_list ap;
drh93a5c6b2003-12-23 02:17:35 +00001028 va_start(ap,zFormat);
drhdb26d4c2011-01-05 12:20:09 +00001029 z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +00001030 va_end(ap);
drh5f968432004-02-21 19:02:30 +00001031 return z;
drh93a5c6b2003-12-23 02:17:35 +00001032}
1033
drh3f280702010-02-18 18:45:09 +00001034/*
drh7c0c4602010-03-03 22:25:18 +00001035** This is the routine that actually formats the sqlite3_log() message.
1036** We house it in a separate routine from sqlite3_log() to avoid using
1037** stack space on small-stack systems when logging is disabled.
1038**
1039** sqlite3_log() must render into a static buffer. It cannot dynamically
1040** allocate memory because it might be called while the memory allocator
1041** mutex is held.
1042*/
1043static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
drha64fa912010-03-04 00:53:32 +00001044 StrAccum acc; /* String accumulator */
1045 char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */
drh7c0c4602010-03-03 22:25:18 +00001046
1047 sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0);
1048 acc.useMalloc = 0;
1049 sqlite3VXPrintf(&acc, 0, zFormat, ap);
1050 sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
1051 sqlite3StrAccumFinish(&acc));
1052}
1053
1054/*
drh3f280702010-02-18 18:45:09 +00001055** Format and write a message to the log if logging is enabled.
1056*/
drha7564662010-02-22 19:32:31 +00001057void sqlite3_log(int iErrCode, const char *zFormat, ...){
drh3f280702010-02-18 18:45:09 +00001058 va_list ap; /* Vararg list */
drh7c0c4602010-03-03 22:25:18 +00001059 if( sqlite3GlobalConfig.xLog ){
drh3f280702010-02-18 18:45:09 +00001060 va_start(ap, zFormat);
drh7c0c4602010-03-03 22:25:18 +00001061 renderLogMsg(iErrCode, zFormat, ap);
drh3f280702010-02-18 18:45:09 +00001062 va_end(ap);
drh3f280702010-02-18 18:45:09 +00001063 }
1064}
1065
drh85e9e222008-07-15 00:27:34 +00001066#if defined(SQLITE_DEBUG)
drhe54ca3f2004-06-07 01:52:14 +00001067/*
1068** A version of printf() that understands %lld. Used for debugging.
1069** The printf() built into some versions of windows does not understand %lld
1070** and segfaults if you give it a long long int.
1071*/
1072void sqlite3DebugPrintf(const char *zFormat, ...){
1073 va_list ap;
drhade86482007-11-28 22:36:40 +00001074 StrAccum acc;
drhe54ca3f2004-06-07 01:52:14 +00001075 char zBuf[500];
drhbb4957f2008-03-20 14:03:29 +00001076 sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
drhade86482007-11-28 22:36:40 +00001077 acc.useMalloc = 0;
1078 va_start(ap,zFormat);
drhf089aa42008-07-08 19:34:06 +00001079 sqlite3VXPrintf(&acc, 0, zFormat, ap);
drhe54ca3f2004-06-07 01:52:14 +00001080 va_end(ap);
drhbed8e7e2007-12-08 17:55:35 +00001081 sqlite3StrAccumFinish(&acc);
drh485f0032007-01-26 19:23:33 +00001082 fprintf(stdout,"%s", zBuf);
drh2ac3ee92004-06-07 16:27:46 +00001083 fflush(stdout);
drhe54ca3f2004-06-07 01:52:14 +00001084}
1085#endif
drhc7bc4fd2009-11-25 18:03:42 +00001086
drh4fa4a542014-09-30 12:33:33 +00001087#ifdef SQLITE_DEBUG
1088/*************************************************************************
1089** Routines for implementing the "TreeView" display of hierarchical
1090** data structures for debugging.
1091**
1092** The main entry points (coded elsewhere) are:
1093** sqlite3TreeViewExpr(0, pExpr, 0);
1094** sqlite3TreeViewExprList(0, pList, 0, 0);
1095** sqlite3TreeViewSelect(0, pSelect, 0);
1096** Insert calls to those routines while debugging in order to display
1097** a diagram of Expr, ExprList, and Select objects.
1098**
1099*/
1100/* Add a new subitem to the tree. The moreToFollow flag indicates that this
1101** is not the last item in the tree. */
1102TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
1103 if( p==0 ){
1104 p = sqlite3_malloc( sizeof(*p) );
1105 if( p==0 ) return 0;
1106 memset(p, 0, sizeof(*p));
1107 }else{
1108 p->iLevel++;
1109 }
1110 assert( moreToFollow==0 || moreToFollow==1 );
drhb08cd3f2014-09-30 19:04:41 +00001111 if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
drh4fa4a542014-09-30 12:33:33 +00001112 return p;
1113}
1114/* Finished with one layer of the tree */
1115void sqlite3TreeViewPop(TreeView *p){
1116 if( p==0 ) return;
1117 p->iLevel--;
1118 if( p->iLevel<0 ) sqlite3_free(p);
1119}
1120/* Generate a single line of output for the tree, with a prefix that contains
1121** all the appropriate tree lines */
1122void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
1123 va_list ap;
1124 int i;
1125 StrAccum acc;
1126 char zBuf[500];
1127 sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
1128 acc.useMalloc = 0;
1129 if( p ){
drhb08cd3f2014-09-30 19:04:41 +00001130 for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
1131 sqlite3StrAccumAppend(&acc, p->bLine[i] ? "| " : " ", 4);
drh4fa4a542014-09-30 12:33:33 +00001132 }
drhb08cd3f2014-09-30 19:04:41 +00001133 sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
drh4fa4a542014-09-30 12:33:33 +00001134 }
1135 va_start(ap, zFormat);
1136 sqlite3VXPrintf(&acc, 0, zFormat, ap);
1137 va_end(ap);
1138 if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
1139 sqlite3StrAccumFinish(&acc);
1140 fprintf(stdout,"%s", zBuf);
1141 fflush(stdout);
1142}
1143/* Shorthand for starting a new tree item that consists of a single label */
1144void sqlite3TreeViewItem(TreeView *p, const char *zLabel, u8 moreToFollow){
1145 p = sqlite3TreeViewPush(p, moreToFollow);
1146 sqlite3TreeViewLine(p, "%s", zLabel);
1147}
1148#endif /* SQLITE_DEBUG */
1149
drhc7bc4fd2009-11-25 18:03:42 +00001150/*
1151** variable-argument wrapper around sqlite3VXPrintf().
1152*/
drha5c14162013-12-17 15:03:06 +00001153void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
drhc7bc4fd2009-11-25 18:03:42 +00001154 va_list ap;
1155 va_start(ap,zFormat);
drha5c14162013-12-17 15:03:06 +00001156 sqlite3VXPrintf(p, bFlags, zFormat, ap);
drhc7bc4fd2009-11-25 18:03:42 +00001157 va_end(ap);
1158}