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