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 */ |
| 68 | #define etERROR 10 /* Used to indicate no such conversion type */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 69 | /* The rest are extensions, not normally found in printf() */ |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 70 | #define etCHARLIT 11 /* Literal characters. %' */ |
| 71 | #define etSQLESCAPE 12 /* Strings with '\'' doubled. %q */ |
| 72 | #define etSQLESCAPE2 13 /* Strings with '\'' doubled and enclosed in '', |
chw | 0cfcf3f | 2002-06-16 04:55:48 +0000 | [diff] [blame] | 73 | NULL pointers replaced by SQL NULL. %Q */ |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 74 | #define etTOKEN 14 /* a pointer to a Token structure */ |
| 75 | #define etSRCLIST 15 /* a pointer to a SrcList */ |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 76 | #define etPOINTER 16 /* The %p conversion */ |
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 | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 115 | { 'z', 0, 6, etDYNSTRING, 0, 0 }, |
| 116 | { 'q', 0, 4, etSQLESCAPE, 0, 0 }, |
| 117 | { 'Q', 0, 4, etSQLESCAPE2, 0, 0 }, |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 118 | { 'c', 0, 0, etCHARX, 0, 0 }, |
| 119 | { 'o', 8, 0, etRADIX, 0, 2 }, |
| 120 | { 'u', 10, 0, etRADIX, 0, 0 }, |
| 121 | { 'x', 16, 0, etRADIX, 16, 1 }, |
| 122 | { 'X', 16, 0, etRADIX, 0, 4 }, |
drh | b37df7b | 2005-10-13 02:09:49 +0000 | [diff] [blame^] | 123 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 124 | { 'f', 0, 1, etFLOAT, 0, 0 }, |
| 125 | { 'e', 0, 1, etEXP, 30, 0 }, |
| 126 | { 'E', 0, 1, etEXP, 14, 0 }, |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 127 | { 'G', 0, 1, etGENERIC, 14, 0 }, |
drh | b37df7b | 2005-10-13 02:09:49 +0000 | [diff] [blame^] | 128 | #endif |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 129 | { 'i', 10, 1, etRADIX, 0, 0 }, |
| 130 | { 'n', 0, 0, etSIZE, 0, 0 }, |
| 131 | { '%', 0, 0, etPERCENT, 0, 0 }, |
| 132 | { 'p', 16, 0, etPOINTER, 0, 1 }, |
| 133 | { 'T', 0, 2, etTOKEN, 0, 0 }, |
| 134 | { 'S', 0, 2, etSRCLIST, 0, 0 }, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 135 | }; |
| 136 | #define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0])) |
| 137 | |
| 138 | /* |
drh | b37df7b | 2005-10-13 02:09:49 +0000 | [diff] [blame^] | 139 | ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 140 | ** conversions will work. |
| 141 | */ |
drh | b37df7b | 2005-10-13 02:09:49 +0000 | [diff] [blame^] | 142 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 143 | /* |
| 144 | ** "*val" is a double such that 0.1 <= *val < 10.0 |
| 145 | ** Return the ascii code for the leading digit of *val, then |
| 146 | ** multiply "*val" by 10.0 to renormalize. |
| 147 | ** |
| 148 | ** Example: |
| 149 | ** input: *val = 3.14159 |
| 150 | ** output: *val = 1.4159 function return = '3' |
| 151 | ** |
| 152 | ** The counter *cnt is incremented each time. After counter exceeds |
| 153 | ** 16 (the number of significant digits in a 64-bit float) '0' is |
| 154 | ** always returned. |
| 155 | */ |
drh | 384eef3 | 2004-01-07 03:04:27 +0000 | [diff] [blame] | 156 | static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 157 | int digit; |
drh | 384eef3 | 2004-01-07 03:04:27 +0000 | [diff] [blame] | 158 | LONGDOUBLE_TYPE d; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 159 | if( (*cnt)++ >= 16 ) return '0'; |
| 160 | digit = (int)*val; |
| 161 | d = digit; |
| 162 | digit += '0'; |
| 163 | *val = (*val - d)*10.0; |
| 164 | return digit; |
| 165 | } |
drh | b37df7b | 2005-10-13 02:09:49 +0000 | [diff] [blame^] | 166 | #endif /* SQLITE_OMIT_FLOATING_POINT */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 167 | |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 168 | /* |
| 169 | ** On machines with a small stack size, you can redefine the |
| 170 | ** SQLITE_PRINT_BUF_SIZE to be less than 350. But beware - for |
| 171 | ** smaller values some %f conversions may go into an infinite loop. |
| 172 | */ |
| 173 | #ifndef SQLITE_PRINT_BUF_SIZE |
| 174 | # define SQLITE_PRINT_BUF_SIZE 350 |
| 175 | #endif |
| 176 | #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 177 | |
| 178 | /* |
| 179 | ** The root program. All variations call this core. |
| 180 | ** |
| 181 | ** INPUTS: |
| 182 | ** func This is a pointer to a function taking three arguments |
| 183 | ** 1. A pointer to anything. Same as the "arg" parameter. |
| 184 | ** 2. A pointer to the list of characters to be output |
| 185 | ** (Note, this list is NOT null terminated.) |
| 186 | ** 3. An integer number of characters to be output. |
| 187 | ** (Note: This number might be zero.) |
| 188 | ** |
| 189 | ** arg This is the pointer to anything which will be passed as the |
| 190 | ** first argument to "func". Use it for whatever you like. |
| 191 | ** |
| 192 | ** fmt This is the format string, as in the usual print. |
| 193 | ** |
| 194 | ** ap This is a pointer to a list of arguments. Same as in |
| 195 | ** vfprint. |
| 196 | ** |
| 197 | ** OUTPUTS: |
| 198 | ** The return value is the total number of characters sent to |
| 199 | ** the function "func". Returns -1 on a error. |
| 200 | ** |
| 201 | ** Note that the order in which automatic variables are declared below |
| 202 | ** seems to make a big difference in determining how fast this beast |
| 203 | ** will run. |
| 204 | */ |
| 205 | static int vxprintf( |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 206 | void (*func)(void*,const char*,int), /* Consumer of text */ |
| 207 | void *arg, /* First argument to the consumer */ |
| 208 | int useExtended, /* Allow extended %-conversions */ |
| 209 | const char *fmt, /* Format string */ |
| 210 | va_list ap /* arguments */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 211 | ){ |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 212 | int c; /* Next character in the format string */ |
| 213 | char *bufpt; /* Pointer to the conversion buffer */ |
| 214 | int precision; /* Precision of the current field */ |
| 215 | int length; /* Length of the field */ |
| 216 | int idx; /* A general purpose loop counter */ |
| 217 | int count; /* Total number of characters output */ |
| 218 | int width; /* Width of the current field */ |
| 219 | etByte flag_leftjustify; /* True if "-" flag is present */ |
| 220 | etByte flag_plussign; /* True if "+" flag is present */ |
| 221 | etByte flag_blanksign; /* True if " " flag is present */ |
| 222 | etByte flag_alternateform; /* True if "#" flag is present */ |
drh | 531fe87 | 2005-08-13 13:40:42 +0000 | [diff] [blame] | 223 | etByte flag_altform2; /* True if "!" flag is present */ |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 224 | etByte flag_zeropad; /* True if field width constant starts with zero */ |
| 225 | etByte flag_long; /* True if "l" flag is present */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 226 | etByte flag_longlong; /* True if the "ll" flag is present */ |
drh | 3e9aeec | 2005-08-13 13:39:02 +0000 | [diff] [blame] | 227 | etByte done; /* Loop termination flag */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 228 | UINT64_TYPE longvalue; /* Value for integer types */ |
drh | 384eef3 | 2004-01-07 03:04:27 +0000 | [diff] [blame] | 229 | LONGDOUBLE_TYPE realvalue; /* Value for real types */ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 230 | const et_info *infop; /* Pointer to the appropriate info structure */ |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 231 | char buf[etBUFSIZE]; /* Conversion buffer */ |
| 232 | char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ |
| 233 | etByte errorflag = 0; /* True if an error is encountered */ |
| 234 | etByte xtype; /* Conversion paradigm */ |
| 235 | char *zExtra; /* Extra memory used for etTCLESCAPE conversions */ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 236 | static const char spaces[] = |
| 237 | " "; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 238 | #define etSPACESIZE (sizeof(spaces)-1) |
drh | b37df7b | 2005-10-13 02:09:49 +0000 | [diff] [blame^] | 239 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 557cc60 | 2005-08-13 12:59:14 +0000 | [diff] [blame] | 240 | int exp, e2; /* exponent of real numbers */ |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 241 | double rounder; /* Used for rounding floating point values */ |
| 242 | etByte flag_dp; /* True if decimal point should be shown */ |
| 243 | etByte flag_rtz; /* True if trailing zeros should be removed */ |
| 244 | etByte flag_exp; /* True to force display of the exponent */ |
| 245 | int nsd; /* Number of significant digits returned */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 246 | #endif |
| 247 | |
drh | e29b1a0 | 2004-07-17 21:56:09 +0000 | [diff] [blame] | 248 | func(arg,"",0); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 249 | count = length = 0; |
| 250 | bufpt = 0; |
| 251 | for(; (c=(*fmt))!=0; ++fmt){ |
| 252 | if( c!='%' ){ |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 253 | int amt; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 254 | bufpt = (char *)fmt; |
| 255 | amt = 1; |
| 256 | while( (c=(*++fmt))!='%' && c!=0 ) amt++; |
| 257 | (*func)(arg,bufpt,amt); |
| 258 | count += amt; |
| 259 | if( c==0 ) break; |
| 260 | } |
| 261 | if( (c=(*++fmt))==0 ){ |
| 262 | errorflag = 1; |
| 263 | (*func)(arg,"%",1); |
| 264 | count++; |
| 265 | break; |
| 266 | } |
| 267 | /* Find out what flags are present */ |
| 268 | flag_leftjustify = flag_plussign = flag_blanksign = |
drh | 557cc60 | 2005-08-13 12:59:14 +0000 | [diff] [blame] | 269 | flag_alternateform = flag_altform2 = flag_zeropad = 0; |
drh | 3e9aeec | 2005-08-13 13:39:02 +0000 | [diff] [blame] | 270 | done = 0; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 271 | do{ |
| 272 | switch( c ){ |
drh | 3e9aeec | 2005-08-13 13:39:02 +0000 | [diff] [blame] | 273 | case '-': flag_leftjustify = 1; break; |
| 274 | case '+': flag_plussign = 1; break; |
| 275 | case ' ': flag_blanksign = 1; break; |
| 276 | case '#': flag_alternateform = 1; break; |
| 277 | case '!': flag_altform2 = 1; break; |
| 278 | case '0': flag_zeropad = 1; break; |
| 279 | default: done = 1; break; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 280 | } |
drh | 3e9aeec | 2005-08-13 13:39:02 +0000 | [diff] [blame] | 281 | }while( !done && (c=(*++fmt))!=0 ); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 282 | /* Get the field width */ |
| 283 | width = 0; |
| 284 | if( c=='*' ){ |
| 285 | width = va_arg(ap,int); |
| 286 | if( width<0 ){ |
| 287 | flag_leftjustify = 1; |
| 288 | width = -width; |
| 289 | } |
| 290 | c = *++fmt; |
| 291 | }else{ |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 292 | while( c>='0' && c<='9' ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 293 | width = width*10 + c - '0'; |
| 294 | c = *++fmt; |
| 295 | } |
| 296 | } |
| 297 | if( width > etBUFSIZE-10 ){ |
| 298 | width = etBUFSIZE-10; |
| 299 | } |
| 300 | /* Get the precision */ |
| 301 | if( c=='.' ){ |
| 302 | precision = 0; |
| 303 | c = *++fmt; |
| 304 | if( c=='*' ){ |
| 305 | precision = va_arg(ap,int); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 306 | if( precision<0 ) precision = -precision; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 307 | c = *++fmt; |
| 308 | }else{ |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 309 | while( c>='0' && c<='9' ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 310 | precision = precision*10 + c - '0'; |
| 311 | c = *++fmt; |
| 312 | } |
| 313 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 314 | }else{ |
| 315 | precision = -1; |
| 316 | } |
| 317 | /* Get the conversion type modifier */ |
| 318 | if( c=='l' ){ |
| 319 | flag_long = 1; |
| 320 | c = *++fmt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 321 | if( c=='l' ){ |
| 322 | flag_longlong = 1; |
| 323 | c = *++fmt; |
| 324 | }else{ |
| 325 | flag_longlong = 0; |
| 326 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 327 | }else{ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 328 | flag_long = flag_longlong = 0; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 329 | } |
| 330 | /* Fetch the info entry for the field */ |
| 331 | infop = 0; |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 332 | xtype = etERROR; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 333 | for(idx=0; idx<etNINFO; idx++){ |
| 334 | if( c==fmtinfo[idx].fmttype ){ |
| 335 | infop = &fmtinfo[idx]; |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 336 | if( useExtended || (infop->flags & FLAG_INTERN)==0 ){ |
| 337 | xtype = infop->type; |
| 338 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 339 | break; |
| 340 | } |
| 341 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 342 | zExtra = 0; |
| 343 | |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 344 | /* Limit the precision to prevent overflowing buf[] during conversion */ |
| 345 | if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){ |
| 346 | precision = etBUFSIZE-40; |
| 347 | } |
| 348 | |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 349 | /* |
| 350 | ** At this point, variables are initialized as follows: |
| 351 | ** |
| 352 | ** flag_alternateform TRUE if a '#' is present. |
drh | 3e9aeec | 2005-08-13 13:39:02 +0000 | [diff] [blame] | 353 | ** flag_altform2 TRUE if a '!' is present. |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 354 | ** flag_plussign TRUE if a '+' is present. |
| 355 | ** flag_leftjustify TRUE if a '-' is present or if the |
| 356 | ** field width was negative. |
| 357 | ** flag_zeropad TRUE if the width began with 0. |
| 358 | ** flag_long TRUE if the letter 'l' (ell) prefixed |
| 359 | ** the conversion character. |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 360 | ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed |
| 361 | ** the conversion character. |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 362 | ** flag_blanksign TRUE if a ' ' is present. |
| 363 | ** width The specified field width. This is |
| 364 | ** always non-negative. Zero is the default. |
| 365 | ** precision The specified precision. The default |
| 366 | ** is -1. |
| 367 | ** xtype The class of the conversion. |
| 368 | ** infop Pointer to the appropriate info struct. |
| 369 | */ |
| 370 | switch( xtype ){ |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 371 | case etPOINTER: |
| 372 | flag_longlong = sizeof(char*)==sizeof(i64); |
| 373 | flag_long = sizeof(char*)==sizeof(long int); |
| 374 | /* Fall through into the next case */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 375 | case etRADIX: |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 376 | if( infop->flags & FLAG_SIGNED ){ |
drh | e970767 | 2004-06-25 01:10:48 +0000 | [diff] [blame] | 377 | i64 v; |
| 378 | if( flag_longlong ) v = va_arg(ap,i64); |
| 379 | else if( flag_long ) v = va_arg(ap,long int); |
| 380 | else v = va_arg(ap,int); |
| 381 | if( v<0 ){ |
| 382 | longvalue = -v; |
| 383 | prefix = '-'; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 384 | }else{ |
drh | e970767 | 2004-06-25 01:10:48 +0000 | [diff] [blame] | 385 | longvalue = v; |
| 386 | if( flag_plussign ) prefix = '+'; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 387 | else if( flag_blanksign ) prefix = ' '; |
| 388 | else prefix = 0; |
| 389 | } |
drh | e970767 | 2004-06-25 01:10:48 +0000 | [diff] [blame] | 390 | }else{ |
| 391 | if( flag_longlong ) longvalue = va_arg(ap,u64); |
| 392 | else if( flag_long ) longvalue = va_arg(ap,unsigned long int); |
| 393 | else longvalue = va_arg(ap,unsigned int); |
| 394 | prefix = 0; |
| 395 | } |
| 396 | if( longvalue==0 ) flag_alternateform = 0; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 397 | if( flag_zeropad && precision<width-(prefix!=0) ){ |
| 398 | precision = width-(prefix!=0); |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 399 | } |
drh | 3039c0a | 2004-02-29 00:11:30 +0000 | [diff] [blame] | 400 | bufpt = &buf[etBUFSIZE-1]; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 401 | { |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 402 | register const char *cset; /* Use registers for speed */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 403 | register int base; |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 404 | cset = &aDigits[infop->charset]; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 405 | base = infop->base; |
| 406 | do{ /* Convert to ascii */ |
| 407 | *(--bufpt) = cset[longvalue%base]; |
| 408 | longvalue = longvalue/base; |
| 409 | }while( longvalue>0 ); |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 410 | } |
drh | 3039c0a | 2004-02-29 00:11:30 +0000 | [diff] [blame] | 411 | length = &buf[etBUFSIZE-1]-bufpt; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 412 | for(idx=precision-length; idx>0; idx--){ |
| 413 | *(--bufpt) = '0'; /* Zero pad */ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 414 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 415 | if( prefix ) *(--bufpt) = prefix; /* Add sign */ |
| 416 | if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 417 | const char *pre; |
| 418 | char x; |
| 419 | pre = &aPrefix[infop->prefix]; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 420 | if( *bufpt!=pre[0] ){ |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 421 | for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 422 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 423 | } |
drh | 3039c0a | 2004-02-29 00:11:30 +0000 | [diff] [blame] | 424 | length = &buf[etBUFSIZE-1]-bufpt; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 425 | break; |
| 426 | case etFLOAT: |
| 427 | case etEXP: |
| 428 | case etGENERIC: |
| 429 | realvalue = va_arg(ap,double); |
drh | b37df7b | 2005-10-13 02:09:49 +0000 | [diff] [blame^] | 430 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 431 | if( precision<0 ) precision = 6; /* Set default precision */ |
drh | 5eba8c0 | 2005-08-19 02:26:27 +0000 | [diff] [blame] | 432 | if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 433 | if( realvalue<0.0 ){ |
| 434 | realvalue = -realvalue; |
| 435 | prefix = '-'; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 436 | }else{ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 437 | if( flag_plussign ) prefix = '+'; |
| 438 | else if( flag_blanksign ) prefix = ' '; |
| 439 | else prefix = 0; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 440 | } |
drh | 3e9aeec | 2005-08-13 13:39:02 +0000 | [diff] [blame] | 441 | if( xtype==etGENERIC && precision>0 ) precision--; |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 442 | #if 0 |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 443 | /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */ |
| 444 | for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1); |
| 445 | #else |
| 446 | /* It makes more sense to use 0.5 */ |
| 447 | for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1); |
| 448 | #endif |
drh | 3e9aeec | 2005-08-13 13:39:02 +0000 | [diff] [blame] | 449 | if( xtype==etFLOAT ) realvalue += rounder; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 450 | /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ |
| 451 | exp = 0; |
| 452 | if( realvalue>0.0 ){ |
drh | 6378285 | 2005-08-30 19:30:59 +0000 | [diff] [blame] | 453 | while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; } |
| 454 | while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; } |
| 455 | while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; } |
drh | b621c23 | 2004-02-21 19:41:04 +0000 | [diff] [blame] | 456 | while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; } |
| 457 | while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; } |
| 458 | if( exp>350 || exp<-350 ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 459 | bufpt = "NaN"; |
| 460 | length = 3; |
| 461 | break; |
| 462 | } |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 463 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 464 | bufpt = buf; |
| 465 | /* |
| 466 | ** If the field type is etGENERIC, then convert to either etEXP |
| 467 | ** or etFLOAT, as appropriate. |
| 468 | */ |
| 469 | flag_exp = xtype==etEXP; |
| 470 | if( xtype!=etFLOAT ){ |
| 471 | realvalue += rounder; |
| 472 | if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } |
| 473 | } |
| 474 | if( xtype==etGENERIC ){ |
| 475 | flag_rtz = !flag_alternateform; |
| 476 | if( exp<-4 || exp>precision ){ |
| 477 | xtype = etEXP; |
| 478 | }else{ |
| 479 | precision = precision - exp; |
| 480 | xtype = etFLOAT; |
| 481 | } |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 482 | }else{ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 483 | flag_rtz = 0; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 484 | } |
drh | 557cc60 | 2005-08-13 12:59:14 +0000 | [diff] [blame] | 485 | if( xtype==etEXP ){ |
| 486 | e2 = 0; |
| 487 | }else{ |
| 488 | e2 = exp; |
| 489 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 490 | nsd = 0; |
drh | 557cc60 | 2005-08-13 12:59:14 +0000 | [diff] [blame] | 491 | flag_dp = (precision>0) | flag_alternateform | flag_altform2; |
| 492 | /* The sign in front of the number */ |
| 493 | if( prefix ){ |
| 494 | *(bufpt++) = prefix; |
| 495 | } |
| 496 | /* Digits prior to the decimal point */ |
| 497 | if( e2<0 ){ |
| 498 | *(bufpt++) = '0'; |
| 499 | }else{ |
| 500 | for(; e2>=0; e2--){ |
| 501 | *(bufpt++) = et_getdigit(&realvalue,&nsd); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 502 | } |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 503 | } |
drh | 557cc60 | 2005-08-13 12:59:14 +0000 | [diff] [blame] | 504 | /* The decimal point */ |
| 505 | if( flag_dp ){ |
| 506 | *(bufpt++) = '.'; |
| 507 | } |
| 508 | /* "0" digits after the decimal point but before the first |
| 509 | ** significant digit of the number */ |
| 510 | for(e2++; e2<0 && precision>0; precision--, e2++){ |
| 511 | *(bufpt++) = '0'; |
| 512 | } |
| 513 | /* Significant digits after the decimal point */ |
| 514 | while( (precision--)>0 ){ |
| 515 | *(bufpt++) = et_getdigit(&realvalue,&nsd); |
| 516 | } |
| 517 | /* Remove trailing zeros and the "." if no digits follow the "." */ |
| 518 | if( flag_rtz && flag_dp ){ |
drh | 3e9aeec | 2005-08-13 13:39:02 +0000 | [diff] [blame] | 519 | while( bufpt[-1]=='0' ) *(--bufpt) = 0; |
| 520 | assert( bufpt>buf ); |
| 521 | if( bufpt[-1]=='.' ){ |
drh | 557cc60 | 2005-08-13 12:59:14 +0000 | [diff] [blame] | 522 | if( flag_altform2 ){ |
| 523 | *(bufpt++) = '0'; |
| 524 | }else{ |
| 525 | *(--bufpt) = 0; |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | /* Add the "eNNN" suffix */ |
| 530 | if( flag_exp || (xtype==etEXP && exp) ){ |
| 531 | *(bufpt++) = aDigits[infop->charset]; |
| 532 | if( exp<0 ){ |
| 533 | *(bufpt++) = '-'; exp = -exp; |
| 534 | }else{ |
| 535 | *(bufpt++) = '+'; |
| 536 | } |
| 537 | if( exp>=100 ){ |
| 538 | *(bufpt++) = (exp/100)+'0'; /* 100's digit */ |
| 539 | exp %= 100; |
| 540 | } |
| 541 | *(bufpt++) = exp/10+'0'; /* 10's digit */ |
| 542 | *(bufpt++) = exp%10+'0'; /* 1's digit */ |
| 543 | } |
| 544 | *bufpt = 0; |
| 545 | |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 546 | /* The converted number is in buf[] and zero terminated. Output it. |
| 547 | ** Note that the number is in the usual order, not reversed as with |
| 548 | ** integer conversions. */ |
drh | 94ce4c1 | 2003-06-07 11:29:50 +0000 | [diff] [blame] | 549 | length = bufpt-buf; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 550 | bufpt = buf; |
| 551 | |
| 552 | /* Special case: Add leading zeros if the flag_zeropad flag is |
| 553 | ** set and we are not left justified */ |
| 554 | if( flag_zeropad && !flag_leftjustify && length < width){ |
| 555 | int i; |
| 556 | int nPad = width - length; |
| 557 | for(i=width; i>=nPad; i--){ |
| 558 | bufpt[i] = bufpt[i-nPad]; |
| 559 | } |
| 560 | i = prefix!=0; |
| 561 | while( nPad-- ) bufpt[i++] = '0'; |
| 562 | length = width; |
| 563 | } |
| 564 | #endif |
| 565 | break; |
| 566 | case etSIZE: |
| 567 | *(va_arg(ap,int*)) = count; |
| 568 | length = width = 0; |
| 569 | break; |
| 570 | case etPERCENT: |
| 571 | buf[0] = '%'; |
| 572 | bufpt = buf; |
| 573 | length = 1; |
| 574 | break; |
| 575 | case etCHARLIT: |
| 576 | case etCHARX: |
| 577 | c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt); |
| 578 | if( precision>=0 ){ |
| 579 | for(idx=1; idx<precision; idx++) buf[idx] = c; |
| 580 | length = precision; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 581 | }else{ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 582 | length =1; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 583 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 584 | bufpt = buf; |
| 585 | break; |
| 586 | case etSTRING: |
drh | d93d8a8 | 2003-06-16 03:08:18 +0000 | [diff] [blame] | 587 | case etDYNSTRING: |
drh | cb48588 | 2002-08-15 13:50:48 +0000 | [diff] [blame] | 588 | bufpt = va_arg(ap,char*); |
drh | d93d8a8 | 2003-06-16 03:08:18 +0000 | [diff] [blame] | 589 | if( bufpt==0 ){ |
| 590 | bufpt = ""; |
| 591 | }else if( xtype==etDYNSTRING ){ |
| 592 | zExtra = bufpt; |
| 593 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 594 | length = strlen(bufpt); |
| 595 | if( precision>=0 && precision<length ) length = precision; |
| 596 | break; |
| 597 | case etSQLESCAPE: |
drh | 5eba8c0 | 2005-08-19 02:26:27 +0000 | [diff] [blame] | 598 | case etSQLESCAPE2: { |
| 599 | int i, j, n, c, isnull; |
| 600 | int needQuote; |
| 601 | char *arg = va_arg(ap,char*); |
| 602 | isnull = arg==0; |
| 603 | if( isnull ) arg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); |
| 604 | for(i=n=0; (c=arg[i])!=0; i++){ |
| 605 | if( c=='\'' ) n++; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 606 | } |
drh | 5eba8c0 | 2005-08-19 02:26:27 +0000 | [diff] [blame] | 607 | needQuote = !isnull && xtype==etSQLESCAPE2; |
| 608 | n += i + 1 + needQuote*2; |
| 609 | if( n>etBUFSIZE ){ |
| 610 | bufpt = zExtra = sqliteMalloc( n ); |
| 611 | if( bufpt==0 ) return -1; |
| 612 | }else{ |
| 613 | bufpt = buf; |
| 614 | } |
| 615 | j = 0; |
| 616 | if( needQuote ) bufpt[j++] = '\''; |
| 617 | for(i=0; (c=arg[i])!=0; i++){ |
| 618 | bufpt[j++] = c; |
| 619 | if( c=='\'' ) bufpt[j++] = c; |
| 620 | } |
| 621 | if( needQuote ) bufpt[j++] = '\''; |
| 622 | bufpt[j] = 0; |
| 623 | length = j; |
| 624 | if( precision>=0 && precision<length ) length = precision; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 625 | break; |
drh | 5eba8c0 | 2005-08-19 02:26:27 +0000 | [diff] [blame] | 626 | } |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 627 | case etTOKEN: { |
| 628 | Token *pToken = va_arg(ap, Token*); |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 629 | if( pToken && pToken->z ){ |
| 630 | (*func)(arg, pToken->z, pToken->n); |
| 631 | } |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 632 | length = width = 0; |
| 633 | break; |
| 634 | } |
| 635 | case etSRCLIST: { |
| 636 | SrcList *pSrc = va_arg(ap, SrcList*); |
| 637 | int k = va_arg(ap, int); |
| 638 | struct SrcList_item *pItem = &pSrc->a[k]; |
| 639 | assert( k>=0 && k<pSrc->nSrc ); |
| 640 | if( pItem->zDatabase && pItem->zDatabase[0] ){ |
| 641 | (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase)); |
| 642 | (*func)(arg, ".", 1); |
| 643 | } |
| 644 | (*func)(arg, pItem->zName, strlen(pItem->zName)); |
| 645 | length = width = 0; |
| 646 | break; |
| 647 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 648 | case etERROR: |
| 649 | buf[0] = '%'; |
| 650 | buf[1] = c; |
| 651 | errorflag = 0; |
| 652 | idx = 1+(c!=0); |
| 653 | (*func)(arg,"%",idx); |
| 654 | count += idx; |
| 655 | if( c==0 ) fmt--; |
| 656 | break; |
| 657 | }/* End switch over the format type */ |
| 658 | /* |
| 659 | ** The text of the conversion is pointed to by "bufpt" and is |
| 660 | ** "length" characters long. The field width is "width". Do |
| 661 | ** the output. |
| 662 | */ |
| 663 | if( !flag_leftjustify ){ |
| 664 | register int nspace; |
| 665 | nspace = width-length; |
| 666 | if( nspace>0 ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 667 | count += nspace; |
| 668 | while( nspace>=etSPACESIZE ){ |
| 669 | (*func)(arg,spaces,etSPACESIZE); |
| 670 | nspace -= etSPACESIZE; |
| 671 | } |
| 672 | if( nspace>0 ) (*func)(arg,spaces,nspace); |
| 673 | } |
| 674 | } |
| 675 | if( length>0 ){ |
| 676 | (*func)(arg,bufpt,length); |
| 677 | count += length; |
| 678 | } |
| 679 | if( flag_leftjustify ){ |
| 680 | register int nspace; |
| 681 | nspace = width-length; |
| 682 | if( nspace>0 ){ |
| 683 | count += nspace; |
| 684 | while( nspace>=etSPACESIZE ){ |
| 685 | (*func)(arg,spaces,etSPACESIZE); |
| 686 | nspace -= etSPACESIZE; |
| 687 | } |
| 688 | if( nspace>0 ) (*func)(arg,spaces,nspace); |
| 689 | } |
| 690 | } |
| 691 | if( zExtra ){ |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 692 | sqliteFree(zExtra); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 693 | } |
| 694 | }/* End for loop over the format string */ |
| 695 | return errorflag ? -1 : count; |
| 696 | } /* End of function */ |
| 697 | |
| 698 | |
| 699 | /* This structure is used to store state information about the |
| 700 | ** write to memory that is currently in progress. |
| 701 | */ |
| 702 | struct sgMprintf { |
| 703 | char *zBase; /* A base allocation */ |
| 704 | char *zText; /* The string collected so far */ |
| 705 | int nChar; /* Length of the string so far */ |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 706 | int nTotal; /* Output size if unconstrained */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 707 | int nAlloc; /* Amount of space allocated in zText */ |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 708 | void *(*xRealloc)(void*,int); /* Function used to realloc memory */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 709 | }; |
| 710 | |
| 711 | /* |
| 712 | ** This function implements the callback from vxprintf. |
| 713 | ** |
| 714 | ** This routine add nNewChar characters of text in zNewText to |
| 715 | ** the sgMprintf structure pointed to by "arg". |
| 716 | */ |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 717 | static void mout(void *arg, const char *zNewText, int nNewChar){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 718 | struct sgMprintf *pM = (struct sgMprintf*)arg; |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 719 | pM->nTotal += nNewChar; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 720 | if( pM->nChar + nNewChar + 1 > pM->nAlloc ){ |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 721 | if( pM->xRealloc==0 ){ |
| 722 | nNewChar = pM->nAlloc - pM->nChar - 1; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 723 | }else{ |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 724 | pM->nAlloc = pM->nChar + nNewChar*2 + 1; |
| 725 | if( pM->zText==pM->zBase ){ |
| 726 | pM->zText = pM->xRealloc(0, pM->nAlloc); |
| 727 | if( pM->zText && pM->nChar ){ |
| 728 | memcpy(pM->zText, pM->zBase, pM->nChar); |
| 729 | } |
| 730 | }else{ |
drh | 53f733c | 2005-09-16 02:38:09 +0000 | [diff] [blame] | 731 | char *zNew; |
| 732 | zNew = pM->xRealloc(pM->zText, pM->nAlloc); |
| 733 | if( zNew ){ |
| 734 | pM->zText = zNew; |
| 735 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 736 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 737 | } |
| 738 | } |
drh | e29b1a0 | 2004-07-17 21:56:09 +0000 | [diff] [blame] | 739 | if( pM->zText ){ |
| 740 | if( nNewChar>0 ){ |
| 741 | memcpy(&pM->zText[pM->nChar], zNewText, nNewChar); |
| 742 | pM->nChar += nNewChar; |
| 743 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 744 | pM->zText[pM->nChar] = 0; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | /* |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 749 | ** This routine is a wrapper around xprintf() that invokes mout() as |
| 750 | ** the consumer. |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 751 | */ |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 752 | static char *base_vprintf( |
| 753 | void *(*xRealloc)(void*,int), /* Routine to realloc memory. May be NULL */ |
| 754 | int useInternal, /* Use internal %-conversions if true */ |
| 755 | char *zInitBuf, /* Initially write here, before mallocing */ |
| 756 | int nInitBuf, /* Size of zInitBuf[] */ |
| 757 | const char *zFormat, /* format string */ |
| 758 | va_list ap /* arguments */ |
| 759 | ){ |
| 760 | struct sgMprintf sM; |
| 761 | sM.zBase = sM.zText = zInitBuf; |
| 762 | sM.nChar = sM.nTotal = 0; |
| 763 | sM.nAlloc = nInitBuf; |
| 764 | sM.xRealloc = xRealloc; |
| 765 | vxprintf(mout, &sM, useInternal, zFormat, ap); |
| 766 | if( xRealloc ){ |
| 767 | if( sM.zText==sM.zBase ){ |
| 768 | sM.zText = xRealloc(0, sM.nChar+1); |
danielk1977 | 8def5ea | 2004-06-16 10:39:52 +0000 | [diff] [blame] | 769 | if( sM.zText ){ |
| 770 | memcpy(sM.zText, sM.zBase, sM.nChar+1); |
| 771 | } |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 772 | }else if( sM.nAlloc>sM.nChar+10 ){ |
drh | 53f733c | 2005-09-16 02:38:09 +0000 | [diff] [blame] | 773 | char *zNew = xRealloc(sM.zText, sM.nChar+1); |
| 774 | if( zNew ){ |
| 775 | sM.zText = zNew; |
| 776 | } |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | return sM.zText; |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | /* |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 783 | ** Realloc that is a real function, not a macro. |
| 784 | */ |
| 785 | static void *printf_realloc(void *old, int size){ |
| 786 | return sqliteRealloc(old,size); |
| 787 | } |
| 788 | |
| 789 | /* |
| 790 | ** Print into memory obtained from sqliteMalloc(). Use the internal |
| 791 | ** %-conversion extensions. |
| 792 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 793 | char *sqlite3VMPrintf(const char *zFormat, va_list ap){ |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 794 | char zBase[SQLITE_PRINT_BUF_SIZE]; |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 795 | return base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap); |
| 796 | } |
| 797 | |
| 798 | /* |
| 799 | ** Print into memory obtained from sqliteMalloc(). Use the internal |
| 800 | ** %-conversion extensions. |
| 801 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 802 | char *sqlite3MPrintf(const char *zFormat, ...){ |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 803 | va_list ap; |
| 804 | char *z; |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 805 | char zBase[SQLITE_PRINT_BUF_SIZE]; |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 806 | va_start(ap, zFormat); |
| 807 | z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap); |
| 808 | va_end(ap); |
| 809 | return z; |
| 810 | } |
| 811 | |
| 812 | /* |
| 813 | ** Print into memory obtained from malloc(). Do not use the internal |
| 814 | ** %-conversion extensions. This routine is for use by external users. |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 815 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 816 | char *sqlite3_mprintf(const char *zFormat, ...){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 817 | va_list ap; |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 818 | char *z; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 819 | char zBuf[200]; |
| 820 | |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 821 | va_start(ap,zFormat); |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 822 | z = base_vprintf((void*(*)(void*,int))realloc, 0, |
| 823 | zBuf, sizeof(zBuf), zFormat, ap); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 824 | va_end(ap); |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 825 | return z; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 826 | } |
| 827 | |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 828 | /* This is the varargs version of sqlite3_mprintf. |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 829 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 830 | char *sqlite3_vmprintf(const char *zFormat, va_list ap){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 831 | char zBuf[200]; |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 832 | return base_vprintf((void*(*)(void*,int))realloc, 0, |
| 833 | zBuf, sizeof(zBuf), zFormat, ap); |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 837 | ** sqlite3_snprintf() works like snprintf() except that it ignores the |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 838 | ** current locale settings. This is important for SQLite because we |
| 839 | ** are not able to use a "," as the decimal point in place of "." as |
| 840 | ** specified by some locales. |
| 841 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 842 | char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 843 | char *z; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 844 | va_list ap; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 845 | |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 846 | va_start(ap,zFormat); |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 847 | z = base_vprintf(0, 0, zBuf, n, zFormat, ap); |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 848 | va_end(ap); |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 849 | return z; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 850 | } |
| 851 | |
drh | 1b743be | 2004-06-22 22:04:46 +0000 | [diff] [blame] | 852 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 853 | /* |
| 854 | ** A version of printf() that understands %lld. Used for debugging. |
| 855 | ** The printf() built into some versions of windows does not understand %lld |
| 856 | ** and segfaults if you give it a long long int. |
| 857 | */ |
| 858 | void sqlite3DebugPrintf(const char *zFormat, ...){ |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 859 | extern int getpid(void); |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 860 | va_list ap; |
| 861 | char zBuf[500]; |
| 862 | va_start(ap, zFormat); |
| 863 | base_vprintf(0, 0, zBuf, sizeof(zBuf), zFormat, ap); |
| 864 | va_end(ap); |
drh | 59c98a6 | 2004-09-24 19:39:26 +0000 | [diff] [blame] | 865 | fprintf(stdout,"%d: %s", getpid(), zBuf); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 866 | fflush(stdout); |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 867 | } |
| 868 | #endif |