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