blob: 54d80eef86691f8f7ba94e7debe0fc87bc6b5acd [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/*
drha18c5682000-10-08 22:20:57 +000018** Conversion types fall into various categories as defined by the
19** following enumeration.
20*/
drhe84a3062004-02-02 12:29:25 +000021#define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */
22#define etFLOAT 2 /* Floating point. %f */
23#define etEXP 3 /* Exponentional notation. %e and %E */
24#define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */
25#define etSIZE 5 /* Return number of characters processed so far. %n */
26#define etSTRING 6 /* Strings. %s */
27#define etDYNSTRING 7 /* Dynamically allocated strings. %z */
28#define etPERCENT 8 /* Percent symbol. %% */
29#define etCHARX 9 /* Characters. %c */
drha18c5682000-10-08 22:20:57 +000030/* The rest are extensions, not normally found in printf() */
drhaf005fb2008-07-09 16:51:51 +000031#define etSQLESCAPE 10 /* Strings with '\'' doubled. %q */
32#define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
chw0cfcf3f2002-06-16 04:55:48 +000033 NULL pointers replaced by SQL NULL. %Q */
drhaf005fb2008-07-09 16:51:51 +000034#define etTOKEN 12 /* a pointer to a Token structure */
35#define etSRCLIST 13 /* a pointer to a SrcList */
36#define etPOINTER 14 /* The %p conversion */
37#define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */
38#define etORDINAL 16 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
drhe84a3062004-02-02 12:29:25 +000039
drh874ba042009-04-08 16:10:04 +000040#define etINVALID 0 /* Any unrecognized conversion type */
41
drhe84a3062004-02-02 12:29:25 +000042
43/*
44** An "etByte" is an 8-bit unsigned value.
45*/
46typedef unsigned char etByte;
drha18c5682000-10-08 22:20:57 +000047
48/*
49** Each builtin conversion character (ex: the 'd' in "%d") is described
50** by an instance of the following structure
51*/
52typedef struct et_info { /* Information about each format field */
drhe84a3062004-02-02 12:29:25 +000053 char fmttype; /* The format field code letter */
54 etByte base; /* The base for radix conversion */
55 etByte flags; /* One or more of FLAG_ constants below */
56 etByte type; /* Conversion paradigm */
drh76ff3a02004-09-24 22:32:30 +000057 etByte charset; /* Offset into aDigits[] of the digits string */
58 etByte prefix; /* Offset into aPrefix[] of the prefix string */
drha18c5682000-10-08 22:20:57 +000059} et_info;
60
61/*
drhe84a3062004-02-02 12:29:25 +000062** Allowed values for et_info.flags
63*/
64#define FLAG_SIGNED 1 /* True if the value to convert is signed */
65#define FLAG_INTERN 2 /* True if for internal use only */
drh4794f732004-11-05 17:17:50 +000066#define FLAG_STRING 4 /* Allow infinity precision */
drhe84a3062004-02-02 12:29:25 +000067
68
69/*
drha18c5682000-10-08 22:20:57 +000070** The following table is searched linearly, so it is good to put the
71** most frequently used conversion types first.
72*/
drh76ff3a02004-09-24 22:32:30 +000073static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
74static const char aPrefix[] = "-x0\000X0";
drh57196282004-10-06 15:41:16 +000075static const et_info fmtinfo[] = {
drh76ff3a02004-09-24 22:32:30 +000076 { 'd', 10, 1, etRADIX, 0, 0 },
drh4794f732004-11-05 17:17:50 +000077 { 's', 0, 4, etSTRING, 0, 0 },
drh557cc602005-08-13 12:59:14 +000078 { 'g', 0, 1, etGENERIC, 30, 0 },
drh153c62c2007-08-24 03:51:33 +000079 { 'z', 0, 4, etDYNSTRING, 0, 0 },
drh4794f732004-11-05 17:17:50 +000080 { 'q', 0, 4, etSQLESCAPE, 0, 0 },
81 { 'Q', 0, 4, etSQLESCAPE2, 0, 0 },
danielk1977f3b863e2007-06-24 06:32:17 +000082 { 'w', 0, 4, etSQLESCAPE3, 0, 0 },
drh76ff3a02004-09-24 22:32:30 +000083 { 'c', 0, 0, etCHARX, 0, 0 },
84 { 'o', 8, 0, etRADIX, 0, 2 },
85 { 'u', 10, 0, etRADIX, 0, 0 },
86 { 'x', 16, 0, etRADIX, 16, 1 },
87 { 'X', 16, 0, etRADIX, 0, 4 },
drhb37df7b2005-10-13 02:09:49 +000088#ifndef SQLITE_OMIT_FLOATING_POINT
drh76ff3a02004-09-24 22:32:30 +000089 { 'f', 0, 1, etFLOAT, 0, 0 },
90 { 'e', 0, 1, etEXP, 30, 0 },
91 { 'E', 0, 1, etEXP, 14, 0 },
drh76ff3a02004-09-24 22:32:30 +000092 { 'G', 0, 1, etGENERIC, 14, 0 },
drhb37df7b2005-10-13 02:09:49 +000093#endif
drh76ff3a02004-09-24 22:32:30 +000094 { 'i', 10, 1, etRADIX, 0, 0 },
95 { 'n', 0, 0, etSIZE, 0, 0 },
96 { '%', 0, 0, etPERCENT, 0, 0 },
97 { 'p', 16, 0, etPOINTER, 0, 1 },
drh7e3ff5d2009-04-08 11:49:42 +000098
99/* All the rest have the FLAG_INTERN bit set and are thus for internal
100** use only */
drh76ff3a02004-09-24 22:32:30 +0000101 { 'T', 0, 2, etTOKEN, 0, 0 },
102 { 'S', 0, 2, etSRCLIST, 0, 0 },
drh9a993342007-12-13 02:45:31 +0000103 { 'r', 10, 3, etORDINAL, 0, 0 },
drha18c5682000-10-08 22:20:57 +0000104};
drha18c5682000-10-08 22:20:57 +0000105
106/*
drhb37df7b2005-10-13 02:09:49 +0000107** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
drha18c5682000-10-08 22:20:57 +0000108** conversions will work.
109*/
drhb37df7b2005-10-13 02:09:49 +0000110#ifndef SQLITE_OMIT_FLOATING_POINT
drha18c5682000-10-08 22:20:57 +0000111/*
112** "*val" is a double such that 0.1 <= *val < 10.0
113** Return the ascii code for the leading digit of *val, then
114** multiply "*val" by 10.0 to renormalize.
115**
116** Example:
117** input: *val = 3.14159
118** output: *val = 1.4159 function return = '3'
119**
120** The counter *cnt is incremented each time. After counter exceeds
121** 16 (the number of significant digits in a 64-bit float) '0' is
122** always returned.
123*/
drhea678832008-12-10 19:26:22 +0000124static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
drha18c5682000-10-08 22:20:57 +0000125 int digit;
drh384eef32004-01-07 03:04:27 +0000126 LONGDOUBLE_TYPE d;
drh72b3fbc2012-06-19 03:11:25 +0000127 if( (*cnt)<=0 ) return '0';
128 (*cnt)--;
drha18c5682000-10-08 22:20:57 +0000129 digit = (int)*val;
130 d = digit;
131 digit += '0';
132 *val = (*val - d)*10.0;
drhea678832008-12-10 19:26:22 +0000133 return (char)digit;
drha18c5682000-10-08 22:20:57 +0000134}
drhb37df7b2005-10-13 02:09:49 +0000135#endif /* SQLITE_OMIT_FLOATING_POINT */
drha18c5682000-10-08 22:20:57 +0000136
drh79158e12005-09-06 21:40:45 +0000137/*
drha6353a32013-12-09 19:03:26 +0000138** Set the StrAccum object to an error mode.
139*/
drha5c14162013-12-17 15:03:06 +0000140static void setStrAccumError(StrAccum *p, u8 eError){
drhc0490572015-05-02 11:45:53 +0000141 assert( eError==STRACCUM_NOMEM || eError==STRACCUM_TOOBIG );
drha6353a32013-12-09 19:03:26 +0000142 p->accError = eError;
143 p->nAlloc = 0;
144}
145
146/*
drha5c14162013-12-17 15:03:06 +0000147** Extra argument values from a PrintfArguments object
148*/
149static sqlite3_int64 getIntArg(PrintfArguments *p){
150 if( p->nArg<=p->nUsed ) return 0;
151 return sqlite3_value_int64(p->apArg[p->nUsed++]);
152}
153static double getDoubleArg(PrintfArguments *p){
154 if( p->nArg<=p->nUsed ) return 0.0;
155 return sqlite3_value_double(p->apArg[p->nUsed++]);
156}
157static char *getTextArg(PrintfArguments *p){
158 if( p->nArg<=p->nUsed ) return 0;
159 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
160}
161
162
163/*
drh79158e12005-09-06 21:40:45 +0000164** On machines with a small stack size, you can redefine the
drhed1fddf2011-10-12 18:52:59 +0000165** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
drh79158e12005-09-06 21:40:45 +0000166*/
167#ifndef SQLITE_PRINT_BUF_SIZE
drh59eedf72011-10-11 17:54:54 +0000168# define SQLITE_PRINT_BUF_SIZE 70
drh79158e12005-09-06 21:40:45 +0000169#endif
170#define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
drha18c5682000-10-08 22:20:57 +0000171
172/*
drhed1fddf2011-10-12 18:52:59 +0000173** Render a string given by "fmt" into the StrAccum object.
drha18c5682000-10-08 22:20:57 +0000174*/
drhf089aa42008-07-08 19:34:06 +0000175void sqlite3VXPrintf(
drha5c14162013-12-17 15:03:06 +0000176 StrAccum *pAccum, /* Accumulate results here */
177 u32 bFlags, /* SQLITE_PRINTF_* flags */
178 const char *fmt, /* Format string */
179 va_list ap /* arguments */
drha18c5682000-10-08 22:20:57 +0000180){
drhe84a3062004-02-02 12:29:25 +0000181 int c; /* Next character in the format string */
182 char *bufpt; /* Pointer to the conversion buffer */
183 int precision; /* Precision of the current field */
184 int length; /* Length of the field */
185 int idx; /* A general purpose loop counter */
drhe84a3062004-02-02 12:29:25 +0000186 int width; /* Width of the current field */
187 etByte flag_leftjustify; /* True if "-" flag is present */
188 etByte flag_plussign; /* True if "+" flag is present */
189 etByte flag_blanksign; /* True if " " flag is present */
190 etByte flag_alternateform; /* True if "#" flag is present */
drh531fe872005-08-13 13:40:42 +0000191 etByte flag_altform2; /* True if "!" flag is present */
drhe84a3062004-02-02 12:29:25 +0000192 etByte flag_zeropad; /* True if field width constant starts with zero */
193 etByte flag_long; /* True if "l" flag is present */
drha34b6762004-05-07 13:30:42 +0000194 etByte flag_longlong; /* True if the "ll" flag is present */
drh3e9aeec2005-08-13 13:39:02 +0000195 etByte done; /* Loop termination flag */
drhed1fddf2011-10-12 18:52:59 +0000196 etByte xtype = 0; /* Conversion paradigm */
drha5c14162013-12-17 15:03:06 +0000197 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
198 u8 useIntern; /* Ok to use internal conversions (ex: %T) */
drhed1fddf2011-10-12 18:52:59 +0000199 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
drh27436af2006-03-28 23:57:17 +0000200 sqlite_uint64 longvalue; /* Value for integer types */
drh384eef32004-01-07 03:04:27 +0000201 LONGDOUBLE_TYPE realvalue; /* Value for real types */
drh57196282004-10-06 15:41:16 +0000202 const et_info *infop; /* Pointer to the appropriate info structure */
drh59eedf72011-10-11 17:54:54 +0000203 char *zOut; /* Rendering buffer */
204 int nOut; /* Size of the rendering buffer */
drhaf8f5132014-10-29 18:20:18 +0000205 char *zExtra = 0; /* Malloced memory used by some conversion */
drhb37df7b2005-10-13 02:09:49 +0000206#ifndef SQLITE_OMIT_FLOATING_POINT
drh557cc602005-08-13 12:59:14 +0000207 int exp, e2; /* exponent of real numbers */
drhed1fddf2011-10-12 18:52:59 +0000208 int nsd; /* Number of significant digits returned */
drhe84a3062004-02-02 12:29:25 +0000209 double rounder; /* Used for rounding floating point values */
210 etByte flag_dp; /* True if decimal point should be shown */
211 etByte flag_rtz; /* True if trailing zeros should be removed */
drha18c5682000-10-08 22:20:57 +0000212#endif
drha5c14162013-12-17 15:03:06 +0000213 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
drhed1fddf2011-10-12 18:52:59 +0000214 char buf[etBUFSIZE]; /* Conversion buffer */
drha18c5682000-10-08 22:20:57 +0000215
drha18c5682000-10-08 22:20:57 +0000216 bufpt = 0;
drha5c14162013-12-17 15:03:06 +0000217 if( bFlags ){
218 if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){
219 pArgList = va_arg(ap, PrintfArguments*);
220 }
221 useIntern = bFlags & SQLITE_PRINTF_INTERNAL;
222 }else{
223 bArgList = useIntern = 0;
224 }
drha18c5682000-10-08 22:20:57 +0000225 for(; (c=(*fmt))!=0; ++fmt){
226 if( c!='%' ){
drha18c5682000-10-08 22:20:57 +0000227 bufpt = (char *)fmt;
drh760b1592014-09-18 01:50:09 +0000228#if HAVE_STRCHRNUL
229 fmt = strchrnul(fmt, '%');
230#else
231 do{ fmt++; }while( *fmt && *fmt != '%' );
232#endif
drha70a0732014-03-17 14:24:27 +0000233 sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt));
drh760b1592014-09-18 01:50:09 +0000234 if( *fmt==0 ) break;
drha18c5682000-10-08 22:20:57 +0000235 }
236 if( (c=(*++fmt))==0 ){
drhade86482007-11-28 22:36:40 +0000237 sqlite3StrAccumAppend(pAccum, "%", 1);
drha18c5682000-10-08 22:20:57 +0000238 break;
239 }
240 /* Find out what flags are present */
241 flag_leftjustify = flag_plussign = flag_blanksign =
drh557cc602005-08-13 12:59:14 +0000242 flag_alternateform = flag_altform2 = flag_zeropad = 0;
drh3e9aeec2005-08-13 13:39:02 +0000243 done = 0;
drha18c5682000-10-08 22:20:57 +0000244 do{
245 switch( c ){
drh3e9aeec2005-08-13 13:39:02 +0000246 case '-': flag_leftjustify = 1; break;
247 case '+': flag_plussign = 1; break;
248 case ' ': flag_blanksign = 1; break;
249 case '#': flag_alternateform = 1; break;
250 case '!': flag_altform2 = 1; break;
251 case '0': flag_zeropad = 1; break;
252 default: done = 1; break;
drha18c5682000-10-08 22:20:57 +0000253 }
drh3e9aeec2005-08-13 13:39:02 +0000254 }while( !done && (c=(*++fmt))!=0 );
drha18c5682000-10-08 22:20:57 +0000255 /* Get the field width */
256 width = 0;
257 if( c=='*' ){
drha5c14162013-12-17 15:03:06 +0000258 if( bArgList ){
259 width = (int)getIntArg(pArgList);
260 }else{
261 width = va_arg(ap,int);
262 }
drha18c5682000-10-08 22:20:57 +0000263 if( width<0 ){
264 flag_leftjustify = 1;
drhb6f47de2015-04-07 15:39:29 +0000265 width = width >= -2147483647 ? -width : 0;
drha18c5682000-10-08 22:20:57 +0000266 }
267 c = *++fmt;
268 }else{
drhb6f47de2015-04-07 15:39:29 +0000269 unsigned wx = 0;
drh17a68932001-01-31 13:28:08 +0000270 while( c>='0' && c<='9' ){
drhb6f47de2015-04-07 15:39:29 +0000271 wx = wx*10 + c - '0';
drha18c5682000-10-08 22:20:57 +0000272 c = *++fmt;
273 }
drhb6f47de2015-04-07 15:39:29 +0000274 testcase( wx>0x7fffffff );
275 width = wx & 0x7fffffff;
drha18c5682000-10-08 22:20:57 +0000276 }
dan8c069142015-04-07 14:38:57 +0000277
drha18c5682000-10-08 22:20:57 +0000278 /* Get the precision */
279 if( c=='.' ){
280 precision = 0;
281 c = *++fmt;
282 if( c=='*' ){
drha5c14162013-12-17 15:03:06 +0000283 if( bArgList ){
284 precision = (int)getIntArg(pArgList);
285 }else{
286 precision = va_arg(ap,int);
287 }
drha18c5682000-10-08 22:20:57 +0000288 c = *++fmt;
drhb6f47de2015-04-07 15:39:29 +0000289 if( precision<0 ){
290 precision = precision >= -2147483647 ? -precision : -1;
291 }
drha18c5682000-10-08 22:20:57 +0000292 }else{
drhb6f47de2015-04-07 15:39:29 +0000293 unsigned px = 0;
drh17a68932001-01-31 13:28:08 +0000294 while( c>='0' && c<='9' ){
drhb6f47de2015-04-07 15:39:29 +0000295 px = px*10 + c - '0';
drha18c5682000-10-08 22:20:57 +0000296 c = *++fmt;
297 }
drhb6f47de2015-04-07 15:39:29 +0000298 testcase( px>0x7fffffff );
299 precision = px & 0x7fffffff;
drha18c5682000-10-08 22:20:57 +0000300 }
drha18c5682000-10-08 22:20:57 +0000301 }else{
302 precision = -1;
303 }
drhb092b032015-05-02 18:25:25 +0000304
305 /* Check for over-size width or precision and error-out if found */
306 if( bArgList ){
307 int iLimit = pAccum->db->aLimit[SQLITE_LIMIT_PRINTF_WIDTH];
308 if( width>iLimit || precision>iLimit ){
309 setStrAccumError(pAccum, STRACCUM_TOOBIG);
310 return;
311 }
312 }
313
drha18c5682000-10-08 22:20:57 +0000314 /* Get the conversion type modifier */
315 if( c=='l' ){
316 flag_long = 1;
317 c = *++fmt;
drha34b6762004-05-07 13:30:42 +0000318 if( c=='l' ){
319 flag_longlong = 1;
320 c = *++fmt;
321 }else{
322 flag_longlong = 0;
323 }
drha18c5682000-10-08 22:20:57 +0000324 }else{
drha34b6762004-05-07 13:30:42 +0000325 flag_long = flag_longlong = 0;
drha18c5682000-10-08 22:20:57 +0000326 }
327 /* Fetch the info entry for the field */
drh874ba042009-04-08 16:10:04 +0000328 infop = &fmtinfo[0];
329 xtype = etINVALID;
danielk197700e13612008-11-17 19:18:54 +0000330 for(idx=0; idx<ArraySize(fmtinfo); idx++){
drha18c5682000-10-08 22:20:57 +0000331 if( c==fmtinfo[idx].fmttype ){
332 infop = &fmtinfo[idx];
drha5c14162013-12-17 15:03:06 +0000333 if( useIntern || (infop->flags & FLAG_INTERN)==0 ){
drh5f968432004-02-21 19:02:30 +0000334 xtype = infop->type;
drhc4413562006-05-22 22:04:00 +0000335 }else{
drhade86482007-11-28 22:36:40 +0000336 return;
drh5f968432004-02-21 19:02:30 +0000337 }
drha18c5682000-10-08 22:20:57 +0000338 break;
339 }
340 }
drh43617e92006-03-06 20:55:46 +0000341
drha18c5682000-10-08 22:20:57 +0000342 /*
343 ** At this point, variables are initialized as follows:
344 **
345 ** flag_alternateform TRUE if a '#' is present.
drh3e9aeec2005-08-13 13:39:02 +0000346 ** flag_altform2 TRUE if a '!' is present.
drha18c5682000-10-08 22:20:57 +0000347 ** flag_plussign TRUE if a '+' is present.
348 ** flag_leftjustify TRUE if a '-' is present or if the
349 ** field width was negative.
350 ** flag_zeropad TRUE if the width began with 0.
351 ** flag_long TRUE if the letter 'l' (ell) prefixed
352 ** the conversion character.
drha34b6762004-05-07 13:30:42 +0000353 ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed
354 ** the conversion character.
drha18c5682000-10-08 22:20:57 +0000355 ** flag_blanksign TRUE if a ' ' is present.
356 ** width The specified field width. This is
357 ** always non-negative. Zero is the default.
358 ** precision The specified precision. The default
359 ** is -1.
360 ** xtype The class of the conversion.
361 ** infop Pointer to the appropriate info struct.
362 */
363 switch( xtype ){
drhfe63d1c2004-09-08 20:13:04 +0000364 case etPOINTER:
365 flag_longlong = sizeof(char*)==sizeof(i64);
366 flag_long = sizeof(char*)==sizeof(long int);
367 /* Fall through into the next case */
drh9a993342007-12-13 02:45:31 +0000368 case etORDINAL:
drha18c5682000-10-08 22:20:57 +0000369 case etRADIX:
drhe84a3062004-02-02 12:29:25 +0000370 if( infop->flags & FLAG_SIGNED ){
drhe9707672004-06-25 01:10:48 +0000371 i64 v;
drha5c14162013-12-17 15:03:06 +0000372 if( bArgList ){
373 v = getIntArg(pArgList);
374 }else if( flag_longlong ){
drheeb23a42009-05-04 20:20:16 +0000375 v = va_arg(ap,i64);
376 }else if( flag_long ){
377 v = va_arg(ap,long int);
378 }else{
379 v = va_arg(ap,int);
380 }
drhe9707672004-06-25 01:10:48 +0000381 if( v<0 ){
drh158b9cb2011-03-05 20:59:46 +0000382 if( v==SMALLEST_INT64 ){
383 longvalue = ((u64)1)<<63;
384 }else{
385 longvalue = -v;
386 }
drhe9707672004-06-25 01:10:48 +0000387 prefix = '-';
danielk1977cfcdaef2004-05-12 07:33:33 +0000388 }else{
drhe9707672004-06-25 01:10:48 +0000389 longvalue = v;
390 if( flag_plussign ) prefix = '+';
danielk1977cfcdaef2004-05-12 07:33:33 +0000391 else if( flag_blanksign ) prefix = ' ';
392 else prefix = 0;
393 }
drhe9707672004-06-25 01:10:48 +0000394 }else{
drha5c14162013-12-17 15:03:06 +0000395 if( bArgList ){
396 longvalue = (u64)getIntArg(pArgList);
397 }else if( flag_longlong ){
drheeb23a42009-05-04 20:20:16 +0000398 longvalue = va_arg(ap,u64);
399 }else if( flag_long ){
400 longvalue = va_arg(ap,unsigned long int);
401 }else{
402 longvalue = va_arg(ap,unsigned int);
403 }
drhe9707672004-06-25 01:10:48 +0000404 prefix = 0;
405 }
406 if( longvalue==0 ) flag_alternateform = 0;
drha18c5682000-10-08 22:20:57 +0000407 if( flag_zeropad && precision<width-(prefix!=0) ){
408 precision = width-(prefix!=0);
drh9adf9ac2002-05-15 11:44:13 +0000409 }
drh59eedf72011-10-11 17:54:54 +0000410 if( precision<etBUFSIZE-10 ){
411 nOut = etBUFSIZE;
412 zOut = buf;
413 }else{
414 nOut = precision + 10;
415 zOut = zExtra = sqlite3Malloc( nOut );
416 if( zOut==0 ){
drha6353a32013-12-09 19:03:26 +0000417 setStrAccumError(pAccum, STRACCUM_NOMEM);
drh59eedf72011-10-11 17:54:54 +0000418 return;
419 }
420 }
421 bufpt = &zOut[nOut-1];
drh9a993342007-12-13 02:45:31 +0000422 if( xtype==etORDINAL ){
drh43f6e062007-12-13 17:50:22 +0000423 static const char zOrd[] = "thstndrd";
drhea678832008-12-10 19:26:22 +0000424 int x = (int)(longvalue % 10);
drh43f6e062007-12-13 17:50:22 +0000425 if( x>=4 || (longvalue/10)%10==1 ){
426 x = 0;
427 }
drh59eedf72011-10-11 17:54:54 +0000428 *(--bufpt) = zOrd[x*2+1];
429 *(--bufpt) = zOrd[x*2];
drh9a993342007-12-13 02:45:31 +0000430 }
drha18c5682000-10-08 22:20:57 +0000431 {
drh0e682092014-03-17 15:06:57 +0000432 const char *cset = &aDigits[infop->charset];
433 u8 base = infop->base;
drha18c5682000-10-08 22:20:57 +0000434 do{ /* Convert to ascii */
435 *(--bufpt) = cset[longvalue%base];
436 longvalue = longvalue/base;
437 }while( longvalue>0 );
drh9adf9ac2002-05-15 11:44:13 +0000438 }
drh59eedf72011-10-11 17:54:54 +0000439 length = (int)(&zOut[nOut-1]-bufpt);
drha18c5682000-10-08 22:20:57 +0000440 for(idx=precision-length; idx>0; idx--){
441 *(--bufpt) = '0'; /* Zero pad */
drh9adf9ac2002-05-15 11:44:13 +0000442 }
drha18c5682000-10-08 22:20:57 +0000443 if( prefix ) *(--bufpt) = prefix; /* Add sign */
444 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
drh76ff3a02004-09-24 22:32:30 +0000445 const char *pre;
446 char x;
447 pre = &aPrefix[infop->prefix];
drhaf005fb2008-07-09 16:51:51 +0000448 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
drha18c5682000-10-08 22:20:57 +0000449 }
drh59eedf72011-10-11 17:54:54 +0000450 length = (int)(&zOut[nOut-1]-bufpt);
drha18c5682000-10-08 22:20:57 +0000451 break;
452 case etFLOAT:
453 case etEXP:
454 case etGENERIC:
drha5c14162013-12-17 15:03:06 +0000455 if( bArgList ){
456 realvalue = getDoubleArg(pArgList);
457 }else{
458 realvalue = va_arg(ap,double);
459 }
drh69ef7032010-03-04 17:11:31 +0000460#ifdef SQLITE_OMIT_FLOATING_POINT
461 length = 0;
462#else
drha18c5682000-10-08 22:20:57 +0000463 if( precision<0 ) precision = 6; /* Set default precision */
drha18c5682000-10-08 22:20:57 +0000464 if( realvalue<0.0 ){
465 realvalue = -realvalue;
466 prefix = '-';
drh9adf9ac2002-05-15 11:44:13 +0000467 }else{
drha18c5682000-10-08 22:20:57 +0000468 if( flag_plussign ) prefix = '+';
469 else if( flag_blanksign ) prefix = ' ';
470 else prefix = 0;
drh9adf9ac2002-05-15 11:44:13 +0000471 }
drh3e9aeec2005-08-13 13:39:02 +0000472 if( xtype==etGENERIC && precision>0 ) precision--;
drha30d22a2015-04-07 13:28:41 +0000473 testcase( precision>0xfff );
drh74b42272015-04-07 12:41:17 +0000474 for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){}
drh3e9aeec2005-08-13 13:39:02 +0000475 if( xtype==etFLOAT ) realvalue += rounder;
drha18c5682000-10-08 22:20:57 +0000476 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
477 exp = 0;
drhea678832008-12-10 19:26:22 +0000478 if( sqlite3IsNaN((double)realvalue) ){
drh53c14022007-05-10 17:23:11 +0000479 bufpt = "NaN";
480 length = 3;
481 break;
482 }
drha18c5682000-10-08 22:20:57 +0000483 if( realvalue>0.0 ){
drh72b3fbc2012-06-19 03:11:25 +0000484 LONGDOUBLE_TYPE scale = 1.0;
drh4ef94132012-06-19 03:35:05 +0000485 while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
drh72b3fbc2012-06-19 03:11:25 +0000486 while( realvalue>=1e64*scale && exp<=350 ){ scale *= 1e64; exp+=64; }
487 while( realvalue>=1e8*scale && exp<=350 ){ scale *= 1e8; exp+=8; }
488 while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
489 realvalue /= scale;
drh93a960a2008-07-10 00:32:42 +0000490 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
491 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
drhaf005fb2008-07-09 16:51:51 +0000492 if( exp>350 ){
drh53c14022007-05-10 17:23:11 +0000493 if( prefix=='-' ){
494 bufpt = "-Inf";
495 }else if( prefix=='+' ){
496 bufpt = "+Inf";
497 }else{
498 bufpt = "Inf";
499 }
drhea678832008-12-10 19:26:22 +0000500 length = sqlite3Strlen30(bufpt);
drha18c5682000-10-08 22:20:57 +0000501 break;
502 }
drh9adf9ac2002-05-15 11:44:13 +0000503 }
drha18c5682000-10-08 22:20:57 +0000504 bufpt = buf;
505 /*
506 ** If the field type is etGENERIC, then convert to either etEXP
507 ** or etFLOAT, as appropriate.
508 */
drha18c5682000-10-08 22:20:57 +0000509 if( xtype!=etFLOAT ){
510 realvalue += rounder;
511 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
512 }
513 if( xtype==etGENERIC ){
514 flag_rtz = !flag_alternateform;
515 if( exp<-4 || exp>precision ){
516 xtype = etEXP;
517 }else{
518 precision = precision - exp;
519 xtype = etFLOAT;
520 }
drh9adf9ac2002-05-15 11:44:13 +0000521 }else{
drh72b3fbc2012-06-19 03:11:25 +0000522 flag_rtz = flag_altform2;
drh9adf9ac2002-05-15 11:44:13 +0000523 }
drh557cc602005-08-13 12:59:14 +0000524 if( xtype==etEXP ){
525 e2 = 0;
526 }else{
527 e2 = exp;
528 }
drh74b42272015-04-07 12:41:17 +0000529 if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){
530 bufpt = zExtra
531 = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 );
drh59eedf72011-10-11 17:54:54 +0000532 if( bufpt==0 ){
drha6353a32013-12-09 19:03:26 +0000533 setStrAccumError(pAccum, STRACCUM_NOMEM);
drh59eedf72011-10-11 17:54:54 +0000534 return;
535 }
536 }
537 zOut = bufpt;
drh72b3fbc2012-06-19 03:11:25 +0000538 nsd = 16 + flag_altform2*10;
drhea678832008-12-10 19:26:22 +0000539 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
drh557cc602005-08-13 12:59:14 +0000540 /* The sign in front of the number */
541 if( prefix ){
542 *(bufpt++) = prefix;
543 }
544 /* Digits prior to the decimal point */
545 if( e2<0 ){
546 *(bufpt++) = '0';
547 }else{
548 for(; e2>=0; e2--){
549 *(bufpt++) = et_getdigit(&realvalue,&nsd);
drha18c5682000-10-08 22:20:57 +0000550 }
drh9adf9ac2002-05-15 11:44:13 +0000551 }
drh557cc602005-08-13 12:59:14 +0000552 /* The decimal point */
553 if( flag_dp ){
554 *(bufpt++) = '.';
555 }
556 /* "0" digits after the decimal point but before the first
557 ** significant digit of the number */
drhaf005fb2008-07-09 16:51:51 +0000558 for(e2++; e2<0; precision--, e2++){
559 assert( precision>0 );
drh557cc602005-08-13 12:59:14 +0000560 *(bufpt++) = '0';
561 }
562 /* Significant digits after the decimal point */
563 while( (precision--)>0 ){
564 *(bufpt++) = et_getdigit(&realvalue,&nsd);
565 }
566 /* Remove trailing zeros and the "." if no digits follow the "." */
567 if( flag_rtz && flag_dp ){
drh3e9aeec2005-08-13 13:39:02 +0000568 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
drh59eedf72011-10-11 17:54:54 +0000569 assert( bufpt>zOut );
drh3e9aeec2005-08-13 13:39:02 +0000570 if( bufpt[-1]=='.' ){
drh557cc602005-08-13 12:59:14 +0000571 if( flag_altform2 ){
572 *(bufpt++) = '0';
573 }else{
574 *(--bufpt) = 0;
575 }
576 }
577 }
578 /* Add the "eNNN" suffix */
drh59eedf72011-10-11 17:54:54 +0000579 if( xtype==etEXP ){
drh557cc602005-08-13 12:59:14 +0000580 *(bufpt++) = aDigits[infop->charset];
581 if( exp<0 ){
582 *(bufpt++) = '-'; exp = -exp;
583 }else{
584 *(bufpt++) = '+';
585 }
586 if( exp>=100 ){
drhea678832008-12-10 19:26:22 +0000587 *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */
drh557cc602005-08-13 12:59:14 +0000588 exp %= 100;
589 }
drhea678832008-12-10 19:26:22 +0000590 *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */
591 *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */
drh557cc602005-08-13 12:59:14 +0000592 }
593 *bufpt = 0;
594
drha18c5682000-10-08 22:20:57 +0000595 /* The converted number is in buf[] and zero terminated. Output it.
596 ** Note that the number is in the usual order, not reversed as with
597 ** integer conversions. */
drh59eedf72011-10-11 17:54:54 +0000598 length = (int)(bufpt-zOut);
599 bufpt = zOut;
drha18c5682000-10-08 22:20:57 +0000600
601 /* Special case: Add leading zeros if the flag_zeropad flag is
602 ** set and we are not left justified */
603 if( flag_zeropad && !flag_leftjustify && length < width){
604 int i;
605 int nPad = width - length;
606 for(i=width; i>=nPad; i--){
607 bufpt[i] = bufpt[i-nPad];
608 }
609 i = prefix!=0;
610 while( nPad-- ) bufpt[i++] = '0';
611 length = width;
612 }
drh69ef7032010-03-04 17:11:31 +0000613#endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
drha18c5682000-10-08 22:20:57 +0000614 break;
615 case etSIZE:
drhfc6ee9d2013-12-17 15:58:42 +0000616 if( !bArgList ){
617 *(va_arg(ap,int*)) = pAccum->nChar;
618 }
drha18c5682000-10-08 22:20:57 +0000619 length = width = 0;
620 break;
621 case etPERCENT:
622 buf[0] = '%';
623 bufpt = buf;
624 length = 1;
625 break;
drha18c5682000-10-08 22:20:57 +0000626 case etCHARX:
drha5c14162013-12-17 15:03:06 +0000627 if( bArgList ){
drhfc6ee9d2013-12-17 15:58:42 +0000628 bufpt = getTextArg(pArgList);
629 c = bufpt ? bufpt[0] : 0;
drha5c14162013-12-17 15:03:06 +0000630 }else{
631 c = va_arg(ap,int);
632 }
drhaf8f5132014-10-29 18:20:18 +0000633 if( precision>1 ){
634 width -= precision-1;
635 if( width>1 && !flag_leftjustify ){
636 sqlite3AppendChar(pAccum, width-1, ' ');
637 width = 0;
638 }
639 sqlite3AppendChar(pAccum, precision-1, c);
drh9adf9ac2002-05-15 11:44:13 +0000640 }
drhaf8f5132014-10-29 18:20:18 +0000641 length = 1;
642 buf[0] = c;
drha18c5682000-10-08 22:20:57 +0000643 bufpt = buf;
644 break;
645 case etSTRING:
drhd93d8a82003-06-16 03:08:18 +0000646 case etDYNSTRING:
drha5c14162013-12-17 15:03:06 +0000647 if( bArgList ){
648 bufpt = getTextArg(pArgList);
649 }else{
650 bufpt = va_arg(ap,char*);
651 }
drhd93d8a82003-06-16 03:08:18 +0000652 if( bufpt==0 ){
653 bufpt = "";
drha5c14162013-12-17 15:03:06 +0000654 }else if( xtype==etDYNSTRING && !bArgList ){
drhd93d8a82003-06-16 03:08:18 +0000655 zExtra = bufpt;
656 }
drhe5090942008-04-29 15:22:27 +0000657 if( precision>=0 ){
658 for(length=0; length<precision && bufpt[length]; length++){}
659 }else{
drhea678832008-12-10 19:26:22 +0000660 length = sqlite3Strlen30(bufpt);
drhe5090942008-04-29 15:22:27 +0000661 }
drha18c5682000-10-08 22:20:57 +0000662 break;
663 case etSQLESCAPE:
danielk1977f3b863e2007-06-24 06:32:17 +0000664 case etSQLESCAPE2:
665 case etSQLESCAPE3: {
drh8965b502009-11-25 16:53:37 +0000666 int i, j, k, n, isnull;
drh5eba8c02005-08-19 02:26:27 +0000667 int needQuote;
drhea678832008-12-10 19:26:22 +0000668 char ch;
danielk1977f3b863e2007-06-24 06:32:17 +0000669 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
drha5c14162013-12-17 15:03:06 +0000670 char *escarg;
671
672 if( bArgList ){
673 escarg = getTextArg(pArgList);
674 }else{
675 escarg = va_arg(ap,char*);
676 }
danielk1977f0113002006-01-24 12:09:17 +0000677 isnull = escarg==0;
678 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
drh8965b502009-11-25 16:53:37 +0000679 k = precision;
dan60d4a302010-03-04 17:58:45 +0000680 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
danielk1977f3b863e2007-06-24 06:32:17 +0000681 if( ch==q ) n++;
drha18c5682000-10-08 22:20:57 +0000682 }
drh5eba8c02005-08-19 02:26:27 +0000683 needQuote = !isnull && xtype==etSQLESCAPE2;
684 n += i + 1 + needQuote*2;
685 if( n>etBUFSIZE ){
drhe5ae5732008-06-15 02:51:47 +0000686 bufpt = zExtra = sqlite3Malloc( n );
drhd164fd32008-11-20 18:20:28 +0000687 if( bufpt==0 ){
drha6353a32013-12-09 19:03:26 +0000688 setStrAccumError(pAccum, STRACCUM_NOMEM);
drhd164fd32008-11-20 18:20:28 +0000689 return;
690 }
drh5eba8c02005-08-19 02:26:27 +0000691 }else{
692 bufpt = buf;
693 }
694 j = 0;
danielk1977f3b863e2007-06-24 06:32:17 +0000695 if( needQuote ) bufpt[j++] = q;
drh8965b502009-11-25 16:53:37 +0000696 k = i;
697 for(i=0; i<k; i++){
698 bufpt[j++] = ch = escarg[i];
danielk1977f3b863e2007-06-24 06:32:17 +0000699 if( ch==q ) bufpt[j++] = ch;
drh5eba8c02005-08-19 02:26:27 +0000700 }
danielk1977f3b863e2007-06-24 06:32:17 +0000701 if( needQuote ) bufpt[j++] = q;
drh5eba8c02005-08-19 02:26:27 +0000702 bufpt[j] = 0;
703 length = j;
drh8965b502009-11-25 16:53:37 +0000704 /* The precision in %q and %Q means how many input characters to
705 ** consume, not the length of the output...
706 ** if( precision>=0 && precision<length ) length = precision; */
drha18c5682000-10-08 22:20:57 +0000707 break;
drh5eba8c02005-08-19 02:26:27 +0000708 }
drh5f968432004-02-21 19:02:30 +0000709 case etTOKEN: {
710 Token *pToken = va_arg(ap, Token*);
drha5c14162013-12-17 15:03:06 +0000711 assert( bArgList==0 );
drha9ab4812013-12-11 11:00:44 +0000712 if( pToken && pToken->n ){
drhade86482007-11-28 22:36:40 +0000713 sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
drhad6d9462004-09-19 02:15:24 +0000714 }
drh5f968432004-02-21 19:02:30 +0000715 length = width = 0;
716 break;
717 }
718 case etSRCLIST: {
719 SrcList *pSrc = va_arg(ap, SrcList*);
720 int k = va_arg(ap, int);
721 struct SrcList_item *pItem = &pSrc->a[k];
drha5c14162013-12-17 15:03:06 +0000722 assert( bArgList==0 );
drh5f968432004-02-21 19:02:30 +0000723 assert( k>=0 && k<pSrc->nSrc );
drh93a960a2008-07-10 00:32:42 +0000724 if( pItem->zDatabase ){
drha6353a32013-12-09 19:03:26 +0000725 sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
drhade86482007-11-28 22:36:40 +0000726 sqlite3StrAccumAppend(pAccum, ".", 1);
drh5f968432004-02-21 19:02:30 +0000727 }
drha6353a32013-12-09 19:03:26 +0000728 sqlite3StrAccumAppendAll(pAccum, pItem->zName);
drh5f968432004-02-21 19:02:30 +0000729 length = width = 0;
730 break;
731 }
drh874ba042009-04-08 16:10:04 +0000732 default: {
733 assert( xtype==etINVALID );
734 return;
735 }
drha18c5682000-10-08 22:20:57 +0000736 }/* End switch over the format type */
737 /*
738 ** The text of the conversion is pointed to by "bufpt" and is
739 ** "length" characters long. The field width is "width". Do
740 ** the output.
741 */
drha70a0732014-03-17 14:24:27 +0000742 width -= length;
drhaf8f5132014-10-29 18:20:18 +0000743 if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
drha70a0732014-03-17 14:24:27 +0000744 sqlite3StrAccumAppend(pAccum, bufpt, length);
drhaf8f5132014-10-29 18:20:18 +0000745 if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
drha70a0732014-03-17 14:24:27 +0000746
drhaf8f5132014-10-29 18:20:18 +0000747 if( zExtra ){
748 sqlite3_free(zExtra);
749 zExtra = 0;
750 }
drha18c5682000-10-08 22:20:57 +0000751 }/* End for loop over the format string */
drha18c5682000-10-08 22:20:57 +0000752} /* End of function */
753
drhade86482007-11-28 22:36:40 +0000754/*
drha70a0732014-03-17 14:24:27 +0000755** Enlarge the memory allocation on a StrAccum object so that it is
756** able to accept at least N more bytes of text.
757**
758** Return the number of bytes of text that StrAccum is able to accept
759** after the attempted enlargement. The value returned might be zero.
760*/
761static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
762 char *zNew;
drha30d22a2015-04-07 13:28:41 +0000763 assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
drha70a0732014-03-17 14:24:27 +0000764 if( p->accError ){
765 testcase(p->accError==STRACCUM_TOOBIG);
766 testcase(p->accError==STRACCUM_NOMEM);
767 return 0;
768 }
drhc0490572015-05-02 11:45:53 +0000769 if( p->mxAlloc==0 ){
drha70a0732014-03-17 14:24:27 +0000770 N = p->nAlloc - p->nChar - 1;
771 setStrAccumError(p, STRACCUM_TOOBIG);
772 return N;
773 }else{
774 char *zOld = (p->zText==p->zBase ? 0 : p->zText);
775 i64 szNew = p->nChar;
776 szNew += N + 1;
drh7b4d7802014-11-03 14:46:29 +0000777 if( szNew+p->nChar<=p->mxAlloc ){
778 /* Force exponential buffer size growth as long as it does not overflow,
779 ** to avoid having to call this routine too often */
780 szNew += p->nChar;
781 }
drha70a0732014-03-17 14:24:27 +0000782 if( szNew > p->mxAlloc ){
783 sqlite3StrAccumReset(p);
784 setStrAccumError(p, STRACCUM_TOOBIG);
785 return 0;
786 }else{
787 p->nAlloc = (int)szNew;
788 }
drhc0490572015-05-02 11:45:53 +0000789 if( p->db ){
drha70a0732014-03-17 14:24:27 +0000790 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
791 }else{
drhf3cdcdc2015-04-29 16:50:28 +0000792 zNew = sqlite3_realloc64(zOld, p->nAlloc);
drha70a0732014-03-17 14:24:27 +0000793 }
794 if( zNew ){
drh7ef4d1c2014-05-31 15:39:53 +0000795 assert( p->zText!=0 || p->nChar==0 );
drha70a0732014-03-17 14:24:27 +0000796 if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
797 p->zText = zNew;
drh7f5a7ec2014-11-03 13:24:12 +0000798 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
drha70a0732014-03-17 14:24:27 +0000799 }else{
800 sqlite3StrAccumReset(p);
801 setStrAccumError(p, STRACCUM_NOMEM);
802 return 0;
803 }
804 }
805 return N;
806}
807
808/*
drhaf8f5132014-10-29 18:20:18 +0000809** Append N copies of character c to the given string buffer.
drha70a0732014-03-17 14:24:27 +0000810*/
drhaf8f5132014-10-29 18:20:18 +0000811void sqlite3AppendChar(StrAccum *p, int N, char c){
drha30d22a2015-04-07 13:28:41 +0000812 testcase( p->nChar + (i64)N > 0x7fffffff );
813 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
814 return;
815 }
drhaf8f5132014-10-29 18:20:18 +0000816 while( (N--)>0 ) p->zText[p->nChar++] = c;
drha70a0732014-03-17 14:24:27 +0000817}
818
819/*
820** The StrAccum "p" is not large enough to accept N new bytes of z[].
821** So enlarge if first, then do the append.
822**
823** This is a helper routine to sqlite3StrAccumAppend() that does special-case
824** work (enlarging the buffer) using tail recursion, so that the
825** sqlite3StrAccumAppend() routine can use fast calling semantics.
826*/
drh172087f2014-08-22 15:40:20 +0000827static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
drha70a0732014-03-17 14:24:27 +0000828 N = sqlite3StrAccumEnlarge(p, N);
829 if( N>0 ){
830 memcpy(&p->zText[p->nChar], z, N);
831 p->nChar += N;
832 }
833}
834
835/*
836** Append N bytes of text from z to the StrAccum object. Increase the
837** size of the memory allocation for StrAccum if necessary.
drha18c5682000-10-08 22:20:57 +0000838*/
drhade86482007-11-28 22:36:40 +0000839void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
drh34573382015-04-15 05:38:35 +0000840 assert( z!=0 || N==0 );
drha6353a32013-12-09 19:03:26 +0000841 assert( p->zText!=0 || p->nChar==0 || p->accError );
842 assert( N>=0 );
843 assert( p->accError==0 || p->nAlloc==0 );
drhade86482007-11-28 22:36:40 +0000844 if( p->nChar+N >= p->nAlloc ){
drha70a0732014-03-17 14:24:27 +0000845 enlargeAndAppend(p,z,N);
drh172087f2014-08-22 15:40:20 +0000846 }else{
847 assert( p->zText );
848 p->nChar += N;
849 memcpy(&p->zText[p->nChar-N], z, N);
drh5f968432004-02-21 19:02:30 +0000850 }
drh483750b2003-01-29 18:46:51 +0000851}
852
853/*
drha6353a32013-12-09 19:03:26 +0000854** Append the complete text of zero-terminated string z[] to the p string.
855*/
856void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){
mistachkin53cd9642013-12-11 02:21:19 +0000857 sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z));
drha6353a32013-12-09 19:03:26 +0000858}
859
860
861/*
drhade86482007-11-28 22:36:40 +0000862** Finish off a string by making sure it is zero-terminated.
863** Return a pointer to the resulting string. Return a NULL
864** pointer if any kind of error was encountered.
drh5f968432004-02-21 19:02:30 +0000865*/
drhade86482007-11-28 22:36:40 +0000866char *sqlite3StrAccumFinish(StrAccum *p){
867 if( p->zText ){
868 p->zText[p->nChar] = 0;
drhc0490572015-05-02 11:45:53 +0000869 if( p->mxAlloc>0 && p->zText==p->zBase ){
870 p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
drhade86482007-11-28 22:36:40 +0000871 if( p->zText ){
872 memcpy(p->zText, p->zBase, p->nChar+1);
873 }else{
drha6353a32013-12-09 19:03:26 +0000874 setStrAccumError(p, STRACCUM_NOMEM);
drhade86482007-11-28 22:36:40 +0000875 }
876 }
877 }
878 return p->zText;
879}
880
881/*
882** Reset an StrAccum string. Reclaim all malloced memory.
883*/
884void sqlite3StrAccumReset(StrAccum *p){
885 if( p->zText!=p->zBase ){
drhc0490572015-05-02 11:45:53 +0000886 sqlite3DbFree(p->db, p->zText);
drhade86482007-11-28 22:36:40 +0000887 }
drhf089aa42008-07-08 19:34:06 +0000888 p->zText = 0;
drhade86482007-11-28 22:36:40 +0000889}
890
891/*
drhc0490572015-05-02 11:45:53 +0000892** Initialize a string accumulator.
893**
894** p: The accumulator to be initialized.
895** db: Pointer to a database connection. May be NULL. Lookaside
896** memory is used if not NULL. db->mallocFailed is set appropriately
897** when not NULL.
898** zBase: An initial buffer. May be NULL in which case the initial buffer
899** is malloced.
900** n: Size of zBase in bytes. If total space requirements never exceed
901** n then no memory allocations ever occur.
902** mx: Maximum number of bytes to accumulate. If mx==0 then no memory
903** allocations will ever occur.
drhade86482007-11-28 22:36:40 +0000904*/
drhc0490572015-05-02 11:45:53 +0000905void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
drhade86482007-11-28 22:36:40 +0000906 p->zText = p->zBase = zBase;
drhc0490572015-05-02 11:45:53 +0000907 p->db = db;
drhade86482007-11-28 22:36:40 +0000908 p->nChar = 0;
909 p->nAlloc = n;
drhbb4957f2008-03-20 14:03:29 +0000910 p->mxAlloc = mx;
drhb49bc862013-08-21 21:12:10 +0000911 p->accError = 0;
drh5f968432004-02-21 19:02:30 +0000912}
913
914/*
915** Print into memory obtained from sqliteMalloc(). Use the internal
916** %-conversion extensions.
917*/
drh17435752007-08-16 04:30:38 +0000918char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
919 char *z;
drh79158e12005-09-06 21:40:45 +0000920 char zBase[SQLITE_PRINT_BUF_SIZE];
drhade86482007-11-28 22:36:40 +0000921 StrAccum acc;
drhbc6160b2009-04-08 15:45:31 +0000922 assert( db!=0 );
drhc0490572015-05-02 11:45:53 +0000923 sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
drhbc6160b2009-04-08 15:45:31 +0000924 db->aLimit[SQLITE_LIMIT_LENGTH]);
drha5c14162013-12-17 15:03:06 +0000925 sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap);
drhade86482007-11-28 22:36:40 +0000926 z = sqlite3StrAccumFinish(&acc);
drhb49bc862013-08-21 21:12:10 +0000927 if( acc.accError==STRACCUM_NOMEM ){
drh17435752007-08-16 04:30:38 +0000928 db->mallocFailed = 1;
929 }
930 return z;
drh5f968432004-02-21 19:02:30 +0000931}
932
933/*
934** Print into memory obtained from sqliteMalloc(). Use the internal
935** %-conversion extensions.
936*/
drh17435752007-08-16 04:30:38 +0000937char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +0000938 va_list ap;
939 char *z;
drh5f968432004-02-21 19:02:30 +0000940 va_start(ap, zFormat);
drhade86482007-11-28 22:36:40 +0000941 z = sqlite3VMPrintf(db, zFormat, ap);
drh5f968432004-02-21 19:02:30 +0000942 va_end(ap);
943 return z;
944}
945
946/*
drh633e6d52008-07-28 19:34:53 +0000947** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
peter.d.reid60ec9142014-09-06 16:39:46 +0000948** the string and before returning. This routine is intended to be used
drh633e6d52008-07-28 19:34:53 +0000949** to modify an existing string. For example:
950**
951** x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
952**
953*/
954char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
955 va_list ap;
956 char *z;
957 va_start(ap, zFormat);
958 z = sqlite3VMPrintf(db, zFormat, ap);
959 va_end(ap);
960 sqlite3DbFree(db, zStr);
961 return z;
962}
963
964/*
drh28dd4792006-06-26 21:35:44 +0000965** Print into memory obtained from sqlite3_malloc(). Omit the internal
966** %-conversion extensions.
967*/
968char *sqlite3_vmprintf(const char *zFormat, va_list ap){
drhade86482007-11-28 22:36:40 +0000969 char *z;
drh28dd4792006-06-26 21:35:44 +0000970 char zBase[SQLITE_PRINT_BUF_SIZE];
drhade86482007-11-28 22:36:40 +0000971 StrAccum acc;
drh9ca95732014-10-24 00:35:58 +0000972
973#ifdef SQLITE_ENABLE_API_ARMOR
974 if( zFormat==0 ){
975 (void)SQLITE_MISUSE_BKPT;
976 return 0;
977 }
978#endif
drhff1590e2008-07-14 12:52:53 +0000979#ifndef SQLITE_OMIT_AUTOINIT
980 if( sqlite3_initialize() ) return 0;
981#endif
drhc0490572015-05-02 11:45:53 +0000982 sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
drhf089aa42008-07-08 19:34:06 +0000983 sqlite3VXPrintf(&acc, 0, zFormat, ap);
drhade86482007-11-28 22:36:40 +0000984 z = sqlite3StrAccumFinish(&acc);
985 return z;
drh28dd4792006-06-26 21:35:44 +0000986}
987
988/*
989** Print into memory obtained from sqlite3_malloc()(). Omit the internal
990** %-conversion extensions.
drh483750b2003-01-29 18:46:51 +0000991*/
danielk19776f8a5032004-05-10 10:34:51 +0000992char *sqlite3_mprintf(const char *zFormat, ...){
drha18c5682000-10-08 22:20:57 +0000993 va_list ap;
drh5f968432004-02-21 19:02:30 +0000994 char *z;
drhff1590e2008-07-14 12:52:53 +0000995#ifndef SQLITE_OMIT_AUTOINIT
996 if( sqlite3_initialize() ) return 0;
997#endif
drh28dd4792006-06-26 21:35:44 +0000998 va_start(ap, zFormat);
drhb3738b62007-03-31 15:02:49 +0000999 z = sqlite3_vmprintf(zFormat, ap);
drha18c5682000-10-08 22:20:57 +00001000 va_end(ap);
drh5f968432004-02-21 19:02:30 +00001001 return z;
drha18c5682000-10-08 22:20:57 +00001002}
1003
drh93a5c6b2003-12-23 02:17:35 +00001004/*
danielk19776f8a5032004-05-10 10:34:51 +00001005** sqlite3_snprintf() works like snprintf() except that it ignores the
drh93a5c6b2003-12-23 02:17:35 +00001006** current locale settings. This is important for SQLite because we
1007** are not able to use a "," as the decimal point in place of "." as
1008** specified by some locales.
drhdb26d4c2011-01-05 12:20:09 +00001009**
1010** Oops: The first two arguments of sqlite3_snprintf() are backwards
1011** from the snprintf() standard. Unfortunately, it is too late to change
1012** this without breaking compatibility, so we just have to live with the
1013** mistake.
1014**
1015** sqlite3_vsnprintf() is the varargs version.
drh93a5c6b2003-12-23 02:17:35 +00001016*/
drhdb26d4c2011-01-05 12:20:09 +00001017char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
1018 StrAccum acc;
1019 if( n<=0 ) return zBuf;
drh9ca95732014-10-24 00:35:58 +00001020#ifdef SQLITE_ENABLE_API_ARMOR
1021 if( zBuf==0 || zFormat==0 ) {
1022 (void)SQLITE_MISUSE_BKPT;
drh96c707a2015-02-13 16:36:14 +00001023 if( zBuf ) zBuf[0] = 0;
drh9ca95732014-10-24 00:35:58 +00001024 return zBuf;
1025 }
1026#endif
drhc0490572015-05-02 11:45:53 +00001027 sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
drhdb26d4c2011-01-05 12:20:09 +00001028 sqlite3VXPrintf(&acc, 0, zFormat, ap);
1029 return sqlite3StrAccumFinish(&acc);
1030}
danielk19776f8a5032004-05-10 10:34:51 +00001031char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +00001032 char *z;
drh93a5c6b2003-12-23 02:17:35 +00001033 va_list ap;
drh93a5c6b2003-12-23 02:17:35 +00001034 va_start(ap,zFormat);
drhdb26d4c2011-01-05 12:20:09 +00001035 z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +00001036 va_end(ap);
drh5f968432004-02-21 19:02:30 +00001037 return z;
drh93a5c6b2003-12-23 02:17:35 +00001038}
1039
drh3f280702010-02-18 18:45:09 +00001040/*
drh7c0c4602010-03-03 22:25:18 +00001041** This is the routine that actually formats the sqlite3_log() message.
1042** We house it in a separate routine from sqlite3_log() to avoid using
1043** stack space on small-stack systems when logging is disabled.
1044**
1045** sqlite3_log() must render into a static buffer. It cannot dynamically
1046** allocate memory because it might be called while the memory allocator
1047** mutex is held.
1048*/
1049static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
drha64fa912010-03-04 00:53:32 +00001050 StrAccum acc; /* String accumulator */
1051 char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */
drh7c0c4602010-03-03 22:25:18 +00001052
drhc0490572015-05-02 11:45:53 +00001053 sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
drh7c0c4602010-03-03 22:25:18 +00001054 sqlite3VXPrintf(&acc, 0, zFormat, ap);
1055 sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
1056 sqlite3StrAccumFinish(&acc));
1057}
1058
1059/*
drh3f280702010-02-18 18:45:09 +00001060** Format and write a message to the log if logging is enabled.
1061*/
drha7564662010-02-22 19:32:31 +00001062void sqlite3_log(int iErrCode, const char *zFormat, ...){
drh3f280702010-02-18 18:45:09 +00001063 va_list ap; /* Vararg list */
drh7c0c4602010-03-03 22:25:18 +00001064 if( sqlite3GlobalConfig.xLog ){
drh3f280702010-02-18 18:45:09 +00001065 va_start(ap, zFormat);
drh7c0c4602010-03-03 22:25:18 +00001066 renderLogMsg(iErrCode, zFormat, ap);
drh3f280702010-02-18 18:45:09 +00001067 va_end(ap);
drh3f280702010-02-18 18:45:09 +00001068 }
1069}
1070
mistachkin02b0e262015-04-16 03:37:19 +00001071#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
drhe54ca3f2004-06-07 01:52:14 +00001072/*
1073** A version of printf() that understands %lld. Used for debugging.
1074** The printf() built into some versions of windows does not understand %lld
1075** and segfaults if you give it a long long int.
1076*/
1077void sqlite3DebugPrintf(const char *zFormat, ...){
1078 va_list ap;
drhade86482007-11-28 22:36:40 +00001079 StrAccum acc;
drhe54ca3f2004-06-07 01:52:14 +00001080 char zBuf[500];
drhc0490572015-05-02 11:45:53 +00001081 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
drhade86482007-11-28 22:36:40 +00001082 va_start(ap,zFormat);
drhf089aa42008-07-08 19:34:06 +00001083 sqlite3VXPrintf(&acc, 0, zFormat, ap);
drhe54ca3f2004-06-07 01:52:14 +00001084 va_end(ap);
drhbed8e7e2007-12-08 17:55:35 +00001085 sqlite3StrAccumFinish(&acc);
drh485f0032007-01-26 19:23:33 +00001086 fprintf(stdout,"%s", zBuf);
drh2ac3ee92004-06-07 16:27:46 +00001087 fflush(stdout);
drhe54ca3f2004-06-07 01:52:14 +00001088}
1089#endif
drhc7bc4fd2009-11-25 18:03:42 +00001090
drh4fa4a542014-09-30 12:33:33 +00001091#ifdef SQLITE_DEBUG
1092/*************************************************************************
1093** Routines for implementing the "TreeView" display of hierarchical
1094** data structures for debugging.
1095**
1096** The main entry points (coded elsewhere) are:
1097** sqlite3TreeViewExpr(0, pExpr, 0);
1098** sqlite3TreeViewExprList(0, pList, 0, 0);
1099** sqlite3TreeViewSelect(0, pSelect, 0);
1100** Insert calls to those routines while debugging in order to display
1101** a diagram of Expr, ExprList, and Select objects.
1102**
1103*/
1104/* Add a new subitem to the tree. The moreToFollow flag indicates that this
1105** is not the last item in the tree. */
1106TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
1107 if( p==0 ){
drhf3cdcdc2015-04-29 16:50:28 +00001108 p = sqlite3_malloc64( sizeof(*p) );
drh4fa4a542014-09-30 12:33:33 +00001109 if( p==0 ) return 0;
1110 memset(p, 0, sizeof(*p));
1111 }else{
1112 p->iLevel++;
1113 }
1114 assert( moreToFollow==0 || moreToFollow==1 );
drhb08cd3f2014-09-30 19:04:41 +00001115 if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
drh4fa4a542014-09-30 12:33:33 +00001116 return p;
1117}
1118/* Finished with one layer of the tree */
1119void sqlite3TreeViewPop(TreeView *p){
1120 if( p==0 ) return;
1121 p->iLevel--;
1122 if( p->iLevel<0 ) sqlite3_free(p);
1123}
1124/* Generate a single line of output for the tree, with a prefix that contains
1125** all the appropriate tree lines */
1126void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
1127 va_list ap;
1128 int i;
1129 StrAccum acc;
1130 char zBuf[500];
drhc0490572015-05-02 11:45:53 +00001131 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
drh4fa4a542014-09-30 12:33:33 +00001132 if( p ){
drhb08cd3f2014-09-30 19:04:41 +00001133 for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
1134 sqlite3StrAccumAppend(&acc, p->bLine[i] ? "| " : " ", 4);
drh4fa4a542014-09-30 12:33:33 +00001135 }
drhb08cd3f2014-09-30 19:04:41 +00001136 sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
drh4fa4a542014-09-30 12:33:33 +00001137 }
1138 va_start(ap, zFormat);
1139 sqlite3VXPrintf(&acc, 0, zFormat, ap);
1140 va_end(ap);
1141 if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
1142 sqlite3StrAccumFinish(&acc);
1143 fprintf(stdout,"%s", zBuf);
1144 fflush(stdout);
1145}
1146/* Shorthand for starting a new tree item that consists of a single label */
1147void sqlite3TreeViewItem(TreeView *p, const char *zLabel, u8 moreToFollow){
1148 p = sqlite3TreeViewPush(p, moreToFollow);
1149 sqlite3TreeViewLine(p, "%s", zLabel);
1150}
1151#endif /* SQLITE_DEBUG */
1152
drhc7bc4fd2009-11-25 18:03:42 +00001153/*
1154** variable-argument wrapper around sqlite3VXPrintf().
1155*/
drha5c14162013-12-17 15:03:06 +00001156void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
drhc7bc4fd2009-11-25 18:03:42 +00001157 va_list ap;
1158 va_start(ap,zFormat);
drha5c14162013-12-17 15:03:06 +00001159 sqlite3VXPrintf(p, bFlags, zFormat, ap);
drhc7bc4fd2009-11-25 18:03:42 +00001160 va_end(ap);
1161}