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