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