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 */ |
| 211 | unsigned long longvalue; /* Value for integer types */ |
drh | 384eef3 | 2004-01-07 03:04:27 +0000 | [diff] [blame] | 212 | LONGDOUBLE_TYPE realvalue; /* Value for real types */ |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 213 | et_info *infop; /* Pointer to the appropriate info structure */ |
| 214 | char buf[etBUFSIZE]; /* Conversion buffer */ |
| 215 | char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ |
| 216 | etByte errorflag = 0; /* True if an error is encountered */ |
| 217 | etByte xtype; /* Conversion paradigm */ |
| 218 | char *zExtra; /* Extra memory used for etTCLESCAPE conversions */ |
drh | 905793e | 2004-02-21 13:31:09 +0000 | [diff] [blame] | 219 | static char spaces[] = " "; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 220 | #define etSPACESIZE (sizeof(spaces)-1) |
| 221 | #ifndef etNOFLOATINGPOINT |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 222 | int exp; /* exponent of real numbers */ |
| 223 | double rounder; /* Used for rounding floating point values */ |
| 224 | etByte flag_dp; /* True if decimal point should be shown */ |
| 225 | etByte flag_rtz; /* True if trailing zeros should be removed */ |
| 226 | etByte flag_exp; /* True to force display of the exponent */ |
| 227 | int nsd; /* Number of significant digits returned */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 228 | #endif |
| 229 | |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 230 | count = length = 0; |
| 231 | bufpt = 0; |
| 232 | for(; (c=(*fmt))!=0; ++fmt){ |
| 233 | if( c!='%' ){ |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 234 | int amt; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 235 | bufpt = (char *)fmt; |
| 236 | amt = 1; |
| 237 | while( (c=(*++fmt))!='%' && c!=0 ) amt++; |
| 238 | (*func)(arg,bufpt,amt); |
| 239 | count += amt; |
| 240 | if( c==0 ) break; |
| 241 | } |
| 242 | if( (c=(*++fmt))==0 ){ |
| 243 | errorflag = 1; |
| 244 | (*func)(arg,"%",1); |
| 245 | count++; |
| 246 | break; |
| 247 | } |
| 248 | /* Find out what flags are present */ |
| 249 | flag_leftjustify = flag_plussign = flag_blanksign = |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 250 | flag_alternateform = flag_zeropad = 0; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 251 | do{ |
| 252 | switch( c ){ |
| 253 | case '-': flag_leftjustify = 1; c = 0; break; |
| 254 | case '+': flag_plussign = 1; c = 0; break; |
| 255 | case ' ': flag_blanksign = 1; c = 0; break; |
| 256 | case '#': flag_alternateform = 1; c = 0; break; |
| 257 | case '0': flag_zeropad = 1; c = 0; break; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 258 | default: break; |
| 259 | } |
| 260 | }while( c==0 && (c=(*++fmt))!=0 ); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 261 | /* Get the field width */ |
| 262 | width = 0; |
| 263 | if( c=='*' ){ |
| 264 | width = va_arg(ap,int); |
| 265 | if( width<0 ){ |
| 266 | flag_leftjustify = 1; |
| 267 | width = -width; |
| 268 | } |
| 269 | c = *++fmt; |
| 270 | }else{ |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 271 | while( c>='0' && c<='9' ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 272 | width = width*10 + c - '0'; |
| 273 | c = *++fmt; |
| 274 | } |
| 275 | } |
| 276 | if( width > etBUFSIZE-10 ){ |
| 277 | width = etBUFSIZE-10; |
| 278 | } |
| 279 | /* Get the precision */ |
| 280 | if( c=='.' ){ |
| 281 | precision = 0; |
| 282 | c = *++fmt; |
| 283 | if( c=='*' ){ |
| 284 | precision = va_arg(ap,int); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 285 | if( precision<0 ) precision = -precision; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 286 | c = *++fmt; |
| 287 | }else{ |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 288 | while( c>='0' && c<='9' ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 289 | precision = precision*10 + c - '0'; |
| 290 | c = *++fmt; |
| 291 | } |
| 292 | } |
| 293 | /* Limit the precision to prevent overflowing buf[] during conversion */ |
| 294 | if( precision>etBUFSIZE-40 ) precision = etBUFSIZE-40; |
| 295 | }else{ |
| 296 | precision = -1; |
| 297 | } |
| 298 | /* Get the conversion type modifier */ |
| 299 | if( c=='l' ){ |
| 300 | flag_long = 1; |
| 301 | c = *++fmt; |
| 302 | }else{ |
| 303 | flag_long = 0; |
| 304 | } |
| 305 | /* Fetch the info entry for the field */ |
| 306 | infop = 0; |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 307 | xtype = etERROR; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 308 | for(idx=0; idx<etNINFO; idx++){ |
| 309 | if( c==fmtinfo[idx].fmttype ){ |
| 310 | infop = &fmtinfo[idx]; |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 311 | if( useExtended || (infop->flags & FLAG_INTERN)==0 ){ |
| 312 | xtype = infop->type; |
| 313 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 314 | break; |
| 315 | } |
| 316 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 317 | zExtra = 0; |
| 318 | |
| 319 | /* |
| 320 | ** At this point, variables are initialized as follows: |
| 321 | ** |
| 322 | ** flag_alternateform TRUE if a '#' is present. |
| 323 | ** flag_plussign TRUE if a '+' is present. |
| 324 | ** flag_leftjustify TRUE if a '-' is present or if the |
| 325 | ** field width was negative. |
| 326 | ** flag_zeropad TRUE if the width began with 0. |
| 327 | ** flag_long TRUE if the letter 'l' (ell) prefixed |
| 328 | ** the conversion character. |
| 329 | ** flag_blanksign TRUE if a ' ' is present. |
| 330 | ** width The specified field width. This is |
| 331 | ** always non-negative. Zero is the default. |
| 332 | ** precision The specified precision. The default |
| 333 | ** is -1. |
| 334 | ** xtype The class of the conversion. |
| 335 | ** infop Pointer to the appropriate info struct. |
| 336 | */ |
| 337 | switch( xtype ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 338 | case etRADIX: |
| 339 | if( flag_long ) longvalue = va_arg(ap,long); |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 340 | else longvalue = va_arg(ap,int); |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 341 | #if 1 |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 342 | /* For the format %#x, the value zero is printed "0" not "0x0". |
| 343 | ** I think this is stupid. */ |
| 344 | if( longvalue==0 ) flag_alternateform = 0; |
| 345 | #else |
| 346 | /* More sensible: turn off the prefix for octal (to prevent "00"), |
| 347 | ** but leave the prefix for hex. */ |
| 348 | if( longvalue==0 && infop->base==8 ) flag_alternateform = 0; |
| 349 | #endif |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 350 | if( infop->flags & FLAG_SIGNED ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 351 | if( *(long*)&longvalue<0 ){ |
| 352 | longvalue = -*(long*)&longvalue; |
| 353 | prefix = '-'; |
| 354 | }else if( flag_plussign ) prefix = '+'; |
| 355 | else if( flag_blanksign ) prefix = ' '; |
| 356 | else prefix = 0; |
| 357 | }else prefix = 0; |
| 358 | if( flag_zeropad && precision<width-(prefix!=0) ){ |
| 359 | precision = width-(prefix!=0); |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 360 | } |
drh | 3039c0a | 2004-02-29 00:11:30 +0000 | [diff] [blame^] | 361 | bufpt = &buf[etBUFSIZE-1]; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 362 | { |
| 363 | register char *cset; /* Use registers for speed */ |
| 364 | register int base; |
| 365 | cset = infop->charset; |
| 366 | base = infop->base; |
| 367 | do{ /* Convert to ascii */ |
| 368 | *(--bufpt) = cset[longvalue%base]; |
| 369 | longvalue = longvalue/base; |
| 370 | }while( longvalue>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 | length = &buf[etBUFSIZE-1]-bufpt; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 373 | for(idx=precision-length; idx>0; idx--){ |
| 374 | *(--bufpt) = '0'; /* Zero pad */ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 375 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 376 | if( prefix ) *(--bufpt) = prefix; /* Add sign */ |
| 377 | if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ |
| 378 | char *pre, x; |
| 379 | pre = infop->prefix; |
| 380 | if( *bufpt!=pre[0] ){ |
| 381 | for(pre=infop->prefix; (x=(*pre))!=0; pre++) *(--bufpt) = x; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 382 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 383 | } |
drh | 3039c0a | 2004-02-29 00:11:30 +0000 | [diff] [blame^] | 384 | length = &buf[etBUFSIZE-1]-bufpt; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 385 | break; |
| 386 | case etFLOAT: |
| 387 | case etEXP: |
| 388 | case etGENERIC: |
| 389 | realvalue = va_arg(ap,double); |
| 390 | #ifndef etNOFLOATINGPOINT |
| 391 | if( precision<0 ) precision = 6; /* Set default precision */ |
| 392 | if( precision>etBUFSIZE-10 ) precision = etBUFSIZE-10; |
| 393 | if( realvalue<0.0 ){ |
| 394 | realvalue = -realvalue; |
| 395 | prefix = '-'; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 396 | }else{ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 397 | if( flag_plussign ) prefix = '+'; |
| 398 | else if( flag_blanksign ) prefix = ' '; |
| 399 | else prefix = 0; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 400 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 401 | if( infop->type==etGENERIC && precision>0 ) precision--; |
| 402 | rounder = 0.0; |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 403 | #if 0 |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 404 | /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */ |
| 405 | for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1); |
| 406 | #else |
| 407 | /* It makes more sense to use 0.5 */ |
| 408 | for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1); |
| 409 | #endif |
| 410 | if( infop->type==etFLOAT ) realvalue += rounder; |
| 411 | /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ |
| 412 | exp = 0; |
| 413 | if( realvalue>0.0 ){ |
drh | b621c23 | 2004-02-21 19:41:04 +0000 | [diff] [blame] | 414 | while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; } |
| 415 | while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; } |
| 416 | while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; } |
| 417 | while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; } |
| 418 | if( exp>350 || exp<-350 ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 419 | bufpt = "NaN"; |
| 420 | length = 3; |
| 421 | break; |
| 422 | } |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 423 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 424 | bufpt = buf; |
| 425 | /* |
| 426 | ** If the field type is etGENERIC, then convert to either etEXP |
| 427 | ** or etFLOAT, as appropriate. |
| 428 | */ |
| 429 | flag_exp = xtype==etEXP; |
| 430 | if( xtype!=etFLOAT ){ |
| 431 | realvalue += rounder; |
| 432 | if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } |
| 433 | } |
| 434 | if( xtype==etGENERIC ){ |
| 435 | flag_rtz = !flag_alternateform; |
| 436 | if( exp<-4 || exp>precision ){ |
| 437 | xtype = etEXP; |
| 438 | }else{ |
| 439 | precision = precision - exp; |
| 440 | xtype = etFLOAT; |
| 441 | } |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 442 | }else{ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 443 | flag_rtz = 0; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 444 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 445 | /* |
| 446 | ** The "exp+precision" test causes output to be of type etEXP if |
| 447 | ** the precision is too large to fit in buf[]. |
| 448 | */ |
| 449 | nsd = 0; |
| 450 | if( xtype==etFLOAT && exp+precision<etBUFSIZE-30 ){ |
| 451 | flag_dp = (precision>0 || flag_alternateform); |
| 452 | if( prefix ) *(bufpt++) = prefix; /* Sign */ |
| 453 | if( exp<0 ) *(bufpt++) = '0'; /* Digits before "." */ |
| 454 | else for(; exp>=0; exp--) *(bufpt++) = et_getdigit(&realvalue,&nsd); |
| 455 | if( flag_dp ) *(bufpt++) = '.'; /* The decimal point */ |
| 456 | for(exp++; exp<0 && precision>0; precision--, exp++){ |
| 457 | *(bufpt++) = '0'; |
| 458 | } |
| 459 | while( (precision--)>0 ) *(bufpt++) = et_getdigit(&realvalue,&nsd); |
| 460 | *(bufpt--) = 0; /* Null terminate */ |
| 461 | if( flag_rtz && flag_dp ){ /* Remove trailing zeros and "." */ |
| 462 | while( bufpt>=buf && *bufpt=='0' ) *(bufpt--) = 0; |
| 463 | if( bufpt>=buf && *bufpt=='.' ) *(bufpt--) = 0; |
| 464 | } |
| 465 | bufpt++; /* point to next free slot */ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 466 | }else{ /* etEXP or etGENERIC */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 467 | flag_dp = (precision>0 || flag_alternateform); |
| 468 | if( prefix ) *(bufpt++) = prefix; /* Sign */ |
| 469 | *(bufpt++) = et_getdigit(&realvalue,&nsd); /* First digit */ |
| 470 | if( flag_dp ) *(bufpt++) = '.'; /* Decimal point */ |
| 471 | while( (precision--)>0 ) *(bufpt++) = et_getdigit(&realvalue,&nsd); |
| 472 | bufpt--; /* point to last digit */ |
| 473 | if( flag_rtz && flag_dp ){ /* Remove tail zeros */ |
| 474 | while( bufpt>=buf && *bufpt=='0' ) *(bufpt--) = 0; |
| 475 | if( bufpt>=buf && *bufpt=='.' ) *(bufpt--) = 0; |
| 476 | } |
| 477 | bufpt++; /* point to next free slot */ |
| 478 | if( exp || flag_exp ){ |
| 479 | *(bufpt++) = infop->charset[0]; |
| 480 | if( exp<0 ){ *(bufpt++) = '-'; exp = -exp; } /* sign of exp */ |
| 481 | else { *(bufpt++) = '+'; } |
| 482 | if( exp>=100 ){ |
| 483 | *(bufpt++) = (exp/100)+'0'; /* 100's digit */ |
| 484 | exp %= 100; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 485 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 486 | *(bufpt++) = exp/10+'0'; /* 10's digit */ |
| 487 | *(bufpt++) = exp%10+'0'; /* 1's digit */ |
| 488 | } |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 489 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 490 | /* The converted number is in buf[] and zero terminated. Output it. |
| 491 | ** Note that the number is in the usual order, not reversed as with |
| 492 | ** integer conversions. */ |
drh | 94ce4c1 | 2003-06-07 11:29:50 +0000 | [diff] [blame] | 493 | length = bufpt-buf; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 494 | bufpt = buf; |
| 495 | |
| 496 | /* Special case: Add leading zeros if the flag_zeropad flag is |
| 497 | ** set and we are not left justified */ |
| 498 | if( flag_zeropad && !flag_leftjustify && length < width){ |
| 499 | int i; |
| 500 | int nPad = width - length; |
| 501 | for(i=width; i>=nPad; i--){ |
| 502 | bufpt[i] = bufpt[i-nPad]; |
| 503 | } |
| 504 | i = prefix!=0; |
| 505 | while( nPad-- ) bufpt[i++] = '0'; |
| 506 | length = width; |
| 507 | } |
| 508 | #endif |
| 509 | break; |
| 510 | case etSIZE: |
| 511 | *(va_arg(ap,int*)) = count; |
| 512 | length = width = 0; |
| 513 | break; |
| 514 | case etPERCENT: |
| 515 | buf[0] = '%'; |
| 516 | bufpt = buf; |
| 517 | length = 1; |
| 518 | break; |
| 519 | case etCHARLIT: |
| 520 | case etCHARX: |
| 521 | c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt); |
| 522 | if( precision>=0 ){ |
| 523 | for(idx=1; idx<precision; idx++) buf[idx] = c; |
| 524 | length = precision; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 525 | }else{ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 526 | length =1; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 527 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 528 | bufpt = buf; |
| 529 | break; |
| 530 | case etSTRING: |
drh | d93d8a8 | 2003-06-16 03:08:18 +0000 | [diff] [blame] | 531 | case etDYNSTRING: |
drh | cb48588 | 2002-08-15 13:50:48 +0000 | [diff] [blame] | 532 | bufpt = va_arg(ap,char*); |
drh | d93d8a8 | 2003-06-16 03:08:18 +0000 | [diff] [blame] | 533 | if( bufpt==0 ){ |
| 534 | bufpt = ""; |
| 535 | }else if( xtype==etDYNSTRING ){ |
| 536 | zExtra = bufpt; |
| 537 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 538 | length = strlen(bufpt); |
| 539 | if( precision>=0 && precision<length ) length = precision; |
| 540 | break; |
| 541 | case etSQLESCAPE: |
chw | 0cfcf3f | 2002-06-16 04:55:48 +0000 | [diff] [blame] | 542 | case etSQLESCAPE2: |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 543 | { |
chw | 0cfcf3f | 2002-06-16 04:55:48 +0000 | [diff] [blame] | 544 | int i, j, n, c, isnull; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 545 | char *arg = va_arg(ap,char*); |
chw | 0cfcf3f | 2002-06-16 04:55:48 +0000 | [diff] [blame] | 546 | isnull = arg==0; |
| 547 | if( isnull ) arg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 548 | for(i=n=0; (c=arg[i])!=0; i++){ |
| 549 | if( c=='\'' ) n++; |
| 550 | } |
chw | 0cfcf3f | 2002-06-16 04:55:48 +0000 | [diff] [blame] | 551 | n += i + 1 + ((!isnull && xtype==etSQLESCAPE2) ? 2 : 0); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 552 | if( n>etBUFSIZE ){ |
| 553 | bufpt = zExtra = sqliteMalloc( n ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 554 | if( bufpt==0 ) return -1; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 555 | }else{ |
| 556 | bufpt = buf; |
| 557 | } |
chw | 0cfcf3f | 2002-06-16 04:55:48 +0000 | [diff] [blame] | 558 | j = 0; |
| 559 | if( !isnull && xtype==etSQLESCAPE2 ) bufpt[j++] = '\''; |
| 560 | for(i=0; (c=arg[i])!=0; i++){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 561 | bufpt[j++] = c; |
| 562 | if( c=='\'' ) bufpt[j++] = c; |
| 563 | } |
chw | 0cfcf3f | 2002-06-16 04:55:48 +0000 | [diff] [blame] | 564 | if( !isnull && xtype==etSQLESCAPE2 ) bufpt[j++] = '\''; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 565 | bufpt[j] = 0; |
| 566 | length = j; |
| 567 | if( precision>=0 && precision<length ) length = precision; |
| 568 | } |
| 569 | break; |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 570 | case etTOKEN: { |
| 571 | Token *pToken = va_arg(ap, Token*); |
| 572 | (*func)(arg, pToken->z, pToken->n); |
| 573 | length = width = 0; |
| 574 | break; |
| 575 | } |
| 576 | case etSRCLIST: { |
| 577 | SrcList *pSrc = va_arg(ap, SrcList*); |
| 578 | int k = va_arg(ap, int); |
| 579 | struct SrcList_item *pItem = &pSrc->a[k]; |
| 580 | assert( k>=0 && k<pSrc->nSrc ); |
| 581 | if( pItem->zDatabase && pItem->zDatabase[0] ){ |
| 582 | (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase)); |
| 583 | (*func)(arg, ".", 1); |
| 584 | } |
| 585 | (*func)(arg, pItem->zName, strlen(pItem->zName)); |
| 586 | length = width = 0; |
| 587 | break; |
| 588 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 589 | case etERROR: |
| 590 | buf[0] = '%'; |
| 591 | buf[1] = c; |
| 592 | errorflag = 0; |
| 593 | idx = 1+(c!=0); |
| 594 | (*func)(arg,"%",idx); |
| 595 | count += idx; |
| 596 | if( c==0 ) fmt--; |
| 597 | break; |
| 598 | }/* End switch over the format type */ |
| 599 | /* |
| 600 | ** The text of the conversion is pointed to by "bufpt" and is |
| 601 | ** "length" characters long. The field width is "width". Do |
| 602 | ** the output. |
| 603 | */ |
| 604 | if( !flag_leftjustify ){ |
| 605 | register int nspace; |
| 606 | nspace = width-length; |
| 607 | if( nspace>0 ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 608 | count += nspace; |
| 609 | while( nspace>=etSPACESIZE ){ |
| 610 | (*func)(arg,spaces,etSPACESIZE); |
| 611 | nspace -= etSPACESIZE; |
| 612 | } |
| 613 | if( nspace>0 ) (*func)(arg,spaces,nspace); |
| 614 | } |
| 615 | } |
| 616 | if( length>0 ){ |
| 617 | (*func)(arg,bufpt,length); |
| 618 | count += length; |
| 619 | } |
| 620 | if( flag_leftjustify ){ |
| 621 | register int nspace; |
| 622 | nspace = width-length; |
| 623 | if( nspace>0 ){ |
| 624 | count += nspace; |
| 625 | while( nspace>=etSPACESIZE ){ |
| 626 | (*func)(arg,spaces,etSPACESIZE); |
| 627 | nspace -= etSPACESIZE; |
| 628 | } |
| 629 | if( nspace>0 ) (*func)(arg,spaces,nspace); |
| 630 | } |
| 631 | } |
| 632 | if( zExtra ){ |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 633 | sqliteFree(zExtra); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 634 | } |
| 635 | }/* End for loop over the format string */ |
| 636 | return errorflag ? -1 : count; |
| 637 | } /* End of function */ |
| 638 | |
| 639 | |
| 640 | /* This structure is used to store state information about the |
| 641 | ** write to memory that is currently in progress. |
| 642 | */ |
| 643 | struct sgMprintf { |
| 644 | char *zBase; /* A base allocation */ |
| 645 | char *zText; /* The string collected so far */ |
| 646 | int nChar; /* Length of the string so far */ |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 647 | int nTotal; /* Output size if unconstrained */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 648 | int nAlloc; /* Amount of space allocated in zText */ |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 649 | void *(*xRealloc)(void*,int); /* Function used to realloc memory */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 650 | }; |
| 651 | |
| 652 | /* |
| 653 | ** This function implements the callback from vxprintf. |
| 654 | ** |
| 655 | ** This routine add nNewChar characters of text in zNewText to |
| 656 | ** the sgMprintf structure pointed to by "arg". |
| 657 | */ |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 658 | static void mout(void *arg, const char *zNewText, int nNewChar){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 659 | struct sgMprintf *pM = (struct sgMprintf*)arg; |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 660 | pM->nTotal += nNewChar; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 661 | if( pM->nChar + nNewChar + 1 > pM->nAlloc ){ |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 662 | if( pM->xRealloc==0 ){ |
| 663 | nNewChar = pM->nAlloc - pM->nChar - 1; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 664 | }else{ |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 665 | pM->nAlloc = pM->nChar + nNewChar*2 + 1; |
| 666 | if( pM->zText==pM->zBase ){ |
| 667 | pM->zText = pM->xRealloc(0, pM->nAlloc); |
| 668 | if( pM->zText && pM->nChar ){ |
| 669 | memcpy(pM->zText, pM->zBase, pM->nChar); |
| 670 | } |
| 671 | }else{ |
| 672 | pM->zText = pM->xRealloc(pM->zText, pM->nAlloc); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 673 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 674 | } |
| 675 | } |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 676 | if( pM->zText && nNewChar>0 ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 677 | memcpy(&pM->zText[pM->nChar], zNewText, nNewChar); |
| 678 | pM->nChar += nNewChar; |
| 679 | pM->zText[pM->nChar] = 0; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | /* |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 684 | ** This routine is a wrapper around xprintf() that invokes mout() as |
| 685 | ** the consumer. |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 686 | */ |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 687 | static char *base_vprintf( |
| 688 | void *(*xRealloc)(void*,int), /* Routine to realloc memory. May be NULL */ |
| 689 | int useInternal, /* Use internal %-conversions if true */ |
| 690 | char *zInitBuf, /* Initially write here, before mallocing */ |
| 691 | int nInitBuf, /* Size of zInitBuf[] */ |
| 692 | const char *zFormat, /* format string */ |
| 693 | va_list ap /* arguments */ |
| 694 | ){ |
| 695 | struct sgMprintf sM; |
| 696 | sM.zBase = sM.zText = zInitBuf; |
| 697 | sM.nChar = sM.nTotal = 0; |
| 698 | sM.nAlloc = nInitBuf; |
| 699 | sM.xRealloc = xRealloc; |
| 700 | vxprintf(mout, &sM, useInternal, zFormat, ap); |
| 701 | if( xRealloc ){ |
| 702 | if( sM.zText==sM.zBase ){ |
| 703 | sM.zText = xRealloc(0, sM.nChar+1); |
| 704 | memcpy(sM.zText, sM.zBase, sM.nChar+1); |
| 705 | }else if( sM.nAlloc>sM.nChar+10 ){ |
| 706 | sM.zText = xRealloc(sM.zText, sM.nChar+1); |
| 707 | } |
| 708 | } |
| 709 | return sM.zText; |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | /* |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 713 | ** Realloc that is a real function, not a macro. |
| 714 | */ |
| 715 | static void *printf_realloc(void *old, int size){ |
| 716 | return sqliteRealloc(old,size); |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | ** Print into memory obtained from sqliteMalloc(). Use the internal |
| 721 | ** %-conversion extensions. |
| 722 | */ |
| 723 | char *sqliteVMPrintf(const char *zFormat, va_list ap){ |
| 724 | char zBase[1000]; |
| 725 | return base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap); |
| 726 | } |
| 727 | |
| 728 | /* |
| 729 | ** Print into memory obtained from sqliteMalloc(). Use the internal |
| 730 | ** %-conversion extensions. |
| 731 | */ |
| 732 | char *sqliteMPrintf(const char *zFormat, ...){ |
| 733 | va_list ap; |
| 734 | char *z; |
| 735 | char zBase[1000]; |
| 736 | va_start(ap, zFormat); |
| 737 | z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap); |
| 738 | va_end(ap); |
| 739 | return z; |
| 740 | } |
| 741 | |
| 742 | /* |
| 743 | ** Print into memory obtained from malloc(). Do not use the internal |
| 744 | ** %-conversion extensions. This routine is for use by external users. |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 745 | */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 746 | char *sqlite_mprintf(const char *zFormat, ...){ |
| 747 | va_list ap; |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 748 | char *z; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 749 | char zBuf[200]; |
| 750 | |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 751 | va_start(ap,zFormat); |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 752 | z = base_vprintf((void*(*)(void*,int))realloc, 0, |
| 753 | zBuf, sizeof(zBuf), zFormat, ap); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 754 | va_end(ap); |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 755 | return z; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | /* This is the varargs version of sqlite_mprintf. |
| 759 | */ |
| 760 | char *sqlite_vmprintf(const char *zFormat, va_list ap){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 761 | char zBuf[200]; |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 762 | return base_vprintf((void*(*)(void*,int))realloc, 0, |
| 763 | zBuf, sizeof(zBuf), zFormat, ap); |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | /* |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 767 | ** sqlite_snprintf() works like snprintf() except that it ignores the |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 768 | ** current locale settings. This is important for SQLite because we |
| 769 | ** are not able to use a "," as the decimal point in place of "." as |
| 770 | ** specified by some locales. |
| 771 | */ |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 772 | char *sqlite_snprintf(int n, char *zBuf, const char *zFormat, ...){ |
| 773 | char *z; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 774 | va_list ap; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 775 | |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 776 | va_start(ap,zFormat); |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 777 | z = base_vprintf(0, 0, zBuf, n, zFormat, ap); |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 778 | va_end(ap); |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 779 | return z; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 780 | } |
| 781 | |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 782 | /* |
| 783 | ** The following four routines implement the varargs versions of the |
| 784 | ** sqlite_exec() and sqlite_get_table() interfaces. See the sqlite.h |
| 785 | ** header files for a more detailed description of how these interfaces |
| 786 | ** work. |
| 787 | ** |
| 788 | ** These routines are all just simple wrappers. |
| 789 | */ |
| 790 | int sqlite_exec_printf( |
| 791 | sqlite *db, /* An open database */ |
drh | 9f71c2e | 2001-11-03 23:57:09 +0000 | [diff] [blame] | 792 | const char *sqlFormat, /* printf-style format string for the SQL */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 793 | sqlite_callback xCallback, /* Callback function */ |
| 794 | void *pArg, /* 1st argument to callback function */ |
| 795 | char **errmsg, /* Error msg written here */ |
| 796 | ... /* Arguments to the format string. */ |
| 797 | ){ |
| 798 | va_list ap; |
| 799 | int rc; |
| 800 | |
| 801 | va_start(ap, errmsg); |
| 802 | rc = sqlite_exec_vprintf(db, sqlFormat, xCallback, pArg, errmsg, ap); |
| 803 | va_end(ap); |
| 804 | return rc; |
| 805 | } |
| 806 | int sqlite_exec_vprintf( |
| 807 | sqlite *db, /* An open database */ |
drh | 9f71c2e | 2001-11-03 23:57:09 +0000 | [diff] [blame] | 808 | const char *sqlFormat, /* printf-style format string for the SQL */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 809 | sqlite_callback xCallback, /* Callback function */ |
| 810 | void *pArg, /* 1st argument to callback function */ |
| 811 | char **errmsg, /* Error msg written here */ |
| 812 | va_list ap /* Arguments to the format string. */ |
| 813 | ){ |
| 814 | char *zSql; |
| 815 | int rc; |
| 816 | |
| 817 | zSql = sqlite_vmprintf(sqlFormat, ap); |
| 818 | rc = sqlite_exec(db, zSql, xCallback, pArg, errmsg); |
drh | fa173a7 | 2002-07-10 21:26:00 +0000 | [diff] [blame] | 819 | free(zSql); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 820 | return rc; |
| 821 | } |
| 822 | int sqlite_get_table_printf( |
| 823 | sqlite *db, /* An open database */ |
drh | 9f71c2e | 2001-11-03 23:57:09 +0000 | [diff] [blame] | 824 | const char *sqlFormat, /* printf-style format string for the SQL */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 825 | char ***resultp, /* Result written to a char *[] that this points to */ |
| 826 | int *nrow, /* Number of result rows written here */ |
| 827 | int *ncol, /* Number of result columns written here */ |
| 828 | char **errmsg, /* Error msg written here */ |
| 829 | ... /* Arguments to the format string */ |
| 830 | ){ |
| 831 | va_list ap; |
| 832 | int rc; |
| 833 | |
| 834 | va_start(ap, errmsg); |
| 835 | rc = sqlite_get_table_vprintf(db, sqlFormat, resultp, nrow, ncol, errmsg, ap); |
| 836 | va_end(ap); |
| 837 | return rc; |
| 838 | } |
| 839 | int sqlite_get_table_vprintf( |
| 840 | sqlite *db, /* An open database */ |
drh | 9f71c2e | 2001-11-03 23:57:09 +0000 | [diff] [blame] | 841 | const char *sqlFormat, /* printf-style format string for the SQL */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 842 | char ***resultp, /* Result written to a char *[] that this points to */ |
| 843 | int *nrow, /* Number of result rows written here */ |
| 844 | int *ncolumn, /* Number of result columns written here */ |
| 845 | char **errmsg, /* Error msg written here */ |
| 846 | va_list ap /* Arguments to the format string */ |
| 847 | ){ |
| 848 | char *zSql; |
| 849 | int rc; |
| 850 | |
| 851 | zSql = sqlite_vmprintf(sqlFormat, ap); |
| 852 | rc = sqlite_get_table(db, zSql, resultp, nrow, ncolumn, errmsg); |
drh | fa173a7 | 2002-07-10 21:26:00 +0000 | [diff] [blame] | 853 | free(zSql); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 854 | return rc; |
| 855 | } |