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 |
| 4 | ** completeness. They are slightly out-of-date. |
| 5 | ** |
drh | e78e828 | 2003-01-19 03:59:45 +0000 | [diff] [blame] | 6 | ** The following modules is an enhanced replacement for the "printf" subroutines |
| 7 | ** found in the standard C library. The following enhancements are |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 8 | ** supported: |
| 9 | ** |
| 10 | ** + Additional functions. The standard set of "printf" functions |
| 11 | ** includes printf, fprintf, sprintf, vprintf, vfprintf, and |
| 12 | ** vsprintf. This module adds the following: |
| 13 | ** |
| 14 | ** * snprintf -- Works like sprintf, but has an extra argument |
| 15 | ** which is the size of the buffer written to. |
| 16 | ** |
| 17 | ** * mprintf -- Similar to sprintf. Writes output to memory |
| 18 | ** obtained from malloc. |
| 19 | ** |
| 20 | ** * xprintf -- Calls a function to dispose of output. |
| 21 | ** |
| 22 | ** * nprintf -- No output, but returns the number of characters |
| 23 | ** that would have been output by printf. |
| 24 | ** |
| 25 | ** * A v- version (ex: vsnprintf) of every function is also |
| 26 | ** supplied. |
| 27 | ** |
| 28 | ** + A few extensions to the formatting notation are supported: |
| 29 | ** |
| 30 | ** * The "=" flag (similar to "-") causes the output to be |
| 31 | ** be centered in the appropriately sized field. |
| 32 | ** |
| 33 | ** * The %b field outputs an integer in binary notation. |
| 34 | ** |
| 35 | ** * The %c field now accepts a precision. The character output |
| 36 | ** is repeated by the number of times the precision specifies. |
| 37 | ** |
| 38 | ** * The %' field works like %c, but takes as its character the |
| 39 | ** next character of the format string, instead of the next |
| 40 | ** argument. For example, printf("%.78'-") prints 78 minus |
| 41 | ** signs, the same as printf("%.78c",'-'). |
| 42 | ** |
| 43 | ** + When compiled using GCC on a SPARC, this version of printf is |
| 44 | ** faster than the library printf for SUN OS 4.1. |
| 45 | ** |
| 46 | ** + All functions are fully reentrant. |
| 47 | ** |
| 48 | */ |
| 49 | #include "sqliteInt.h" |
drh | 7c68d60 | 2000-10-11 19:28:51 +0000 | [diff] [blame] | 50 | |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 51 | /* |
| 52 | ** Undefine COMPATIBILITY to make some slight changes in the way things |
| 53 | ** work. I think the changes are an improvement, but they are not |
| 54 | ** backwards compatible. |
| 55 | */ |
| 56 | /* #define COMPATIBILITY / * Compatible with SUN OS 4.1 */ |
| 57 | |
| 58 | /* |
| 59 | ** Conversion types fall into various categories as defined by the |
| 60 | ** following enumeration. |
| 61 | */ |
| 62 | enum et_type { /* The type of the format field */ |
| 63 | etRADIX, /* Integer types. %d, %x, %o, and so forth */ |
| 64 | etFLOAT, /* Floating point. %f */ |
| 65 | etEXP, /* Exponentional notation. %e and %E */ |
| 66 | etGENERIC, /* Floating or exponential, depending on exponent. %g */ |
| 67 | etSIZE, /* Return number of characters processed so far. %n */ |
| 68 | etSTRING, /* Strings. %s */ |
| 69 | etPERCENT, /* Percent symbol. %% */ |
| 70 | etCHARX, /* Characters. %c */ |
| 71 | etERROR, /* Used to indicate no such conversion type */ |
| 72 | /* The rest are extensions, not normally found in printf() */ |
| 73 | etCHARLIT, /* Literal characters. %' */ |
| 74 | etSQLESCAPE, /* Strings with '\'' doubled. %q */ |
chw | 0cfcf3f | 2002-06-16 04:55:48 +0000 | [diff] [blame] | 75 | etSQLESCAPE2, /* Strings with '\'' doubled and enclosed in '', |
| 76 | NULL pointers replaced by SQL NULL. %Q */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 77 | etORDINAL /* 1st, 2nd, 3rd and so forth */ |
| 78 | }; |
| 79 | |
| 80 | /* |
| 81 | ** Each builtin conversion character (ex: the 'd' in "%d") is described |
| 82 | ** by an instance of the following structure |
| 83 | */ |
| 84 | typedef struct et_info { /* Information about each format field */ |
| 85 | int fmttype; /* The format field code letter */ |
| 86 | int base; /* The base for radix conversion */ |
| 87 | char *charset; /* The character set for conversion */ |
| 88 | int flag_signed; /* Is the quantity signed? */ |
| 89 | char *prefix; /* Prefix on non-zero values in alt format */ |
| 90 | enum et_type type; /* Conversion paradigm */ |
| 91 | } et_info; |
| 92 | |
| 93 | /* |
| 94 | ** The following table is searched linearly, so it is good to put the |
| 95 | ** most frequently used conversion types first. |
| 96 | */ |
| 97 | static et_info fmtinfo[] = { |
| 98 | { 'd', 10, "0123456789", 1, 0, etRADIX, }, |
| 99 | { 's', 0, 0, 0, 0, etSTRING, }, |
| 100 | { 'q', 0, 0, 0, 0, etSQLESCAPE, }, |
chw | 0cfcf3f | 2002-06-16 04:55:48 +0000 | [diff] [blame] | 101 | { 'Q', 0, 0, 0, 0, etSQLESCAPE2, }, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 102 | { 'c', 0, 0, 0, 0, etCHARX, }, |
| 103 | { 'o', 8, "01234567", 0, "0", etRADIX, }, |
| 104 | { 'u', 10, "0123456789", 0, 0, etRADIX, }, |
| 105 | { 'x', 16, "0123456789abcdef", 0, "x0", etRADIX, }, |
| 106 | { 'X', 16, "0123456789ABCDEF", 0, "X0", etRADIX, }, |
| 107 | { 'r', 10, "0123456789", 0, 0, etORDINAL, }, |
| 108 | { 'f', 0, 0, 1, 0, etFLOAT, }, |
| 109 | { 'e', 0, "e", 1, 0, etEXP, }, |
| 110 | { 'E', 0, "E", 1, 0, etEXP, }, |
| 111 | { 'g', 0, "e", 1, 0, etGENERIC, }, |
| 112 | { 'G', 0, "E", 1, 0, etGENERIC, }, |
| 113 | { 'i', 10, "0123456789", 1, 0, etRADIX, }, |
| 114 | { 'n', 0, 0, 0, 0, etSIZE, }, |
| 115 | { '%', 0, 0, 0, 0, etPERCENT, }, |
| 116 | { 'b', 2, "01", 0, "b0", etRADIX, }, /* Binary */ |
| 117 | { 'p', 10, "0123456789", 0, 0, etRADIX, }, /* Pointers */ |
| 118 | { '\'', 0, 0, 0, 0, etCHARLIT, }, /* Literal char */ |
| 119 | }; |
| 120 | #define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0])) |
| 121 | |
| 122 | /* |
| 123 | ** If NOFLOATINGPOINT is defined, then none of the floating point |
| 124 | ** conversions will work. |
| 125 | */ |
| 126 | #ifndef etNOFLOATINGPOINT |
| 127 | /* |
| 128 | ** "*val" is a double such that 0.1 <= *val < 10.0 |
| 129 | ** Return the ascii code for the leading digit of *val, then |
| 130 | ** multiply "*val" by 10.0 to renormalize. |
| 131 | ** |
| 132 | ** Example: |
| 133 | ** input: *val = 3.14159 |
| 134 | ** output: *val = 1.4159 function return = '3' |
| 135 | ** |
| 136 | ** The counter *cnt is incremented each time. After counter exceeds |
| 137 | ** 16 (the number of significant digits in a 64-bit float) '0' is |
| 138 | ** always returned. |
| 139 | */ |
| 140 | static int et_getdigit(double *val, int *cnt){ |
| 141 | int digit; |
| 142 | double d; |
| 143 | if( (*cnt)++ >= 16 ) return '0'; |
| 144 | digit = (int)*val; |
| 145 | d = digit; |
| 146 | digit += '0'; |
| 147 | *val = (*val - d)*10.0; |
| 148 | return digit; |
| 149 | } |
| 150 | #endif |
| 151 | |
| 152 | #define etBUFSIZE 1000 /* Size of the output buffer */ |
| 153 | |
| 154 | /* |
| 155 | ** The root program. All variations call this core. |
| 156 | ** |
| 157 | ** INPUTS: |
| 158 | ** func This is a pointer to a function taking three arguments |
| 159 | ** 1. A pointer to anything. Same as the "arg" parameter. |
| 160 | ** 2. A pointer to the list of characters to be output |
| 161 | ** (Note, this list is NOT null terminated.) |
| 162 | ** 3. An integer number of characters to be output. |
| 163 | ** (Note: This number might be zero.) |
| 164 | ** |
| 165 | ** arg This is the pointer to anything which will be passed as the |
| 166 | ** first argument to "func". Use it for whatever you like. |
| 167 | ** |
| 168 | ** fmt This is the format string, as in the usual print. |
| 169 | ** |
| 170 | ** ap This is a pointer to a list of arguments. Same as in |
| 171 | ** vfprint. |
| 172 | ** |
| 173 | ** OUTPUTS: |
| 174 | ** The return value is the total number of characters sent to |
| 175 | ** the function "func". Returns -1 on a error. |
| 176 | ** |
| 177 | ** Note that the order in which automatic variables are declared below |
| 178 | ** seems to make a big difference in determining how fast this beast |
| 179 | ** will run. |
| 180 | */ |
| 181 | static int vxprintf( |
| 182 | void (*func)(void*,char*,int), |
| 183 | void *arg, |
| 184 | const char *format, |
| 185 | va_list ap |
| 186 | ){ |
| 187 | register const char *fmt; /* The format string. */ |
| 188 | register int c; /* Next character in the format string */ |
| 189 | register char *bufpt; /* Pointer to the conversion buffer */ |
| 190 | register int precision; /* Precision of the current field */ |
| 191 | register int length; /* Length of the field */ |
| 192 | register int idx; /* A general purpose loop counter */ |
| 193 | int count; /* Total number of characters output */ |
| 194 | int width; /* Width of the current field */ |
| 195 | int flag_leftjustify; /* True if "-" flag is present */ |
| 196 | int flag_plussign; /* True if "+" flag is present */ |
| 197 | int flag_blanksign; /* True if " " flag is present */ |
| 198 | int flag_alternateform; /* True if "#" flag is present */ |
| 199 | int flag_zeropad; /* True if field width constant starts with zero */ |
| 200 | int flag_long; /* True if "l" flag is present */ |
| 201 | int flag_center; /* True if "=" flag is present */ |
| 202 | unsigned long longvalue; /* Value for integer types */ |
| 203 | double realvalue; /* Value for real types */ |
| 204 | et_info *infop; /* Pointer to the appropriate info structure */ |
| 205 | char buf[etBUFSIZE]; /* Conversion buffer */ |
| 206 | char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ |
| 207 | int errorflag = 0; /* True if an error is encountered */ |
| 208 | enum et_type xtype; /* Conversion paradigm */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 209 | char *zExtra; /* Extra memory used for etTCLESCAPE conversions */ |
| 210 | static char spaces[] = " " |
| 211 | " "; |
| 212 | #define etSPACESIZE (sizeof(spaces)-1) |
| 213 | #ifndef etNOFLOATINGPOINT |
| 214 | int exp; /* exponent of real numbers */ |
| 215 | double rounder; /* Used for rounding floating point values */ |
| 216 | int flag_dp; /* True if decimal point should be shown */ |
| 217 | int flag_rtz; /* True if trailing zeros should be removed */ |
| 218 | int flag_exp; /* True to force display of the exponent */ |
| 219 | int nsd; /* Number of significant digits returned */ |
| 220 | #endif |
| 221 | |
| 222 | fmt = format; /* Put in a register for speed */ |
| 223 | count = length = 0; |
| 224 | bufpt = 0; |
| 225 | for(; (c=(*fmt))!=0; ++fmt){ |
| 226 | if( c!='%' ){ |
| 227 | register int amt; |
| 228 | bufpt = (char *)fmt; |
| 229 | amt = 1; |
| 230 | while( (c=(*++fmt))!='%' && c!=0 ) amt++; |
| 231 | (*func)(arg,bufpt,amt); |
| 232 | count += amt; |
| 233 | if( c==0 ) break; |
| 234 | } |
| 235 | if( (c=(*++fmt))==0 ){ |
| 236 | errorflag = 1; |
| 237 | (*func)(arg,"%",1); |
| 238 | count++; |
| 239 | break; |
| 240 | } |
| 241 | /* Find out what flags are present */ |
| 242 | flag_leftjustify = flag_plussign = flag_blanksign = |
| 243 | flag_alternateform = flag_zeropad = flag_center = 0; |
| 244 | do{ |
| 245 | switch( c ){ |
| 246 | case '-': flag_leftjustify = 1; c = 0; break; |
| 247 | case '+': flag_plussign = 1; c = 0; break; |
| 248 | case ' ': flag_blanksign = 1; c = 0; break; |
| 249 | case '#': flag_alternateform = 1; c = 0; break; |
| 250 | case '0': flag_zeropad = 1; c = 0; break; |
| 251 | case '=': flag_center = 1; c = 0; break; |
| 252 | default: break; |
| 253 | } |
| 254 | }while( c==0 && (c=(*++fmt))!=0 ); |
| 255 | if( flag_center ) flag_leftjustify = 0; |
| 256 | /* Get the field width */ |
| 257 | width = 0; |
| 258 | if( c=='*' ){ |
| 259 | width = va_arg(ap,int); |
| 260 | if( width<0 ){ |
| 261 | flag_leftjustify = 1; |
| 262 | width = -width; |
| 263 | } |
| 264 | c = *++fmt; |
| 265 | }else{ |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 266 | while( c>='0' && c<='9' ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 267 | width = width*10 + c - '0'; |
| 268 | c = *++fmt; |
| 269 | } |
| 270 | } |
| 271 | if( width > etBUFSIZE-10 ){ |
| 272 | width = etBUFSIZE-10; |
| 273 | } |
| 274 | /* Get the precision */ |
| 275 | if( c=='.' ){ |
| 276 | precision = 0; |
| 277 | c = *++fmt; |
| 278 | if( c=='*' ){ |
| 279 | precision = va_arg(ap,int); |
| 280 | #ifndef etCOMPATIBILITY |
| 281 | /* This is sensible, but SUN OS 4.1 doesn't do it. */ |
| 282 | if( precision<0 ) precision = -precision; |
| 283 | #endif |
| 284 | c = *++fmt; |
| 285 | }else{ |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 286 | while( c>='0' && c<='9' ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 287 | precision = precision*10 + c - '0'; |
| 288 | c = *++fmt; |
| 289 | } |
| 290 | } |
| 291 | /* Limit the precision to prevent overflowing buf[] during conversion */ |
| 292 | if( precision>etBUFSIZE-40 ) precision = etBUFSIZE-40; |
| 293 | }else{ |
| 294 | precision = -1; |
| 295 | } |
| 296 | /* Get the conversion type modifier */ |
| 297 | if( c=='l' ){ |
| 298 | flag_long = 1; |
| 299 | c = *++fmt; |
| 300 | }else{ |
| 301 | flag_long = 0; |
| 302 | } |
| 303 | /* Fetch the info entry for the field */ |
| 304 | infop = 0; |
| 305 | for(idx=0; idx<etNINFO; idx++){ |
| 306 | if( c==fmtinfo[idx].fmttype ){ |
| 307 | infop = &fmtinfo[idx]; |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | /* No info entry found. It must be an error. */ |
| 312 | if( infop==0 ){ |
| 313 | xtype = etERROR; |
| 314 | }else{ |
| 315 | xtype = infop->type; |
| 316 | } |
| 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 ){ |
| 338 | case etORDINAL: |
| 339 | case etRADIX: |
| 340 | if( flag_long ) longvalue = va_arg(ap,long); |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 341 | else longvalue = va_arg(ap,int); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 342 | #ifdef etCOMPATIBILITY |
| 343 | /* For the format %#x, the value zero is printed "0" not "0x0". |
| 344 | ** I think this is stupid. */ |
| 345 | if( longvalue==0 ) flag_alternateform = 0; |
| 346 | #else |
| 347 | /* More sensible: turn off the prefix for octal (to prevent "00"), |
| 348 | ** but leave the prefix for hex. */ |
| 349 | if( longvalue==0 && infop->base==8 ) flag_alternateform = 0; |
| 350 | #endif |
| 351 | if( infop->flag_signed ){ |
| 352 | if( *(long*)&longvalue<0 ){ |
| 353 | longvalue = -*(long*)&longvalue; |
| 354 | prefix = '-'; |
| 355 | }else if( flag_plussign ) prefix = '+'; |
| 356 | else if( flag_blanksign ) prefix = ' '; |
| 357 | else prefix = 0; |
| 358 | }else prefix = 0; |
| 359 | if( flag_zeropad && precision<width-(prefix!=0) ){ |
| 360 | precision = width-(prefix!=0); |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 361 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 362 | bufpt = &buf[etBUFSIZE]; |
| 363 | if( xtype==etORDINAL ){ |
| 364 | long a,b; |
| 365 | a = longvalue%10; |
| 366 | b = longvalue%100; |
| 367 | bufpt -= 2; |
| 368 | if( a==0 || a>3 || (b>10 && b<14) ){ |
| 369 | bufpt[0] = 't'; |
| 370 | bufpt[1] = 'h'; |
| 371 | }else if( a==1 ){ |
| 372 | bufpt[0] = 's'; |
| 373 | bufpt[1] = 't'; |
| 374 | }else if( a==2 ){ |
| 375 | bufpt[0] = 'n'; |
| 376 | bufpt[1] = 'd'; |
| 377 | }else if( a==3 ){ |
| 378 | bufpt[0] = 'r'; |
| 379 | bufpt[1] = 'd'; |
| 380 | } |
| 381 | } |
| 382 | { |
| 383 | register char *cset; /* Use registers for speed */ |
| 384 | register int base; |
| 385 | cset = infop->charset; |
| 386 | base = infop->base; |
| 387 | do{ /* Convert to ascii */ |
| 388 | *(--bufpt) = cset[longvalue%base]; |
| 389 | longvalue = longvalue/base; |
| 390 | }while( longvalue>0 ); |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 391 | } |
drh | 94ce4c1 | 2003-06-07 11:29:50 +0000 | [diff] [blame^] | 392 | length = &buf[etBUFSIZE]-bufpt; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 393 | for(idx=precision-length; idx>0; idx--){ |
| 394 | *(--bufpt) = '0'; /* Zero pad */ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 395 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 396 | if( prefix ) *(--bufpt) = prefix; /* Add sign */ |
| 397 | if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ |
| 398 | char *pre, x; |
| 399 | pre = infop->prefix; |
| 400 | if( *bufpt!=pre[0] ){ |
| 401 | for(pre=infop->prefix; (x=(*pre))!=0; pre++) *(--bufpt) = x; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 402 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 403 | } |
drh | 94ce4c1 | 2003-06-07 11:29:50 +0000 | [diff] [blame^] | 404 | length = &buf[etBUFSIZE]-bufpt; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 405 | break; |
| 406 | case etFLOAT: |
| 407 | case etEXP: |
| 408 | case etGENERIC: |
| 409 | realvalue = va_arg(ap,double); |
| 410 | #ifndef etNOFLOATINGPOINT |
| 411 | if( precision<0 ) precision = 6; /* Set default precision */ |
| 412 | if( precision>etBUFSIZE-10 ) precision = etBUFSIZE-10; |
| 413 | if( realvalue<0.0 ){ |
| 414 | realvalue = -realvalue; |
| 415 | prefix = '-'; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 416 | }else{ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 417 | if( flag_plussign ) prefix = '+'; |
| 418 | else if( flag_blanksign ) prefix = ' '; |
| 419 | else prefix = 0; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 420 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 421 | if( infop->type==etGENERIC && precision>0 ) precision--; |
| 422 | rounder = 0.0; |
| 423 | #ifdef COMPATIBILITY |
| 424 | /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */ |
| 425 | for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1); |
| 426 | #else |
| 427 | /* It makes more sense to use 0.5 */ |
| 428 | for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1); |
| 429 | #endif |
| 430 | if( infop->type==etFLOAT ) realvalue += rounder; |
| 431 | /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ |
| 432 | exp = 0; |
| 433 | if( realvalue>0.0 ){ |
| 434 | int k = 0; |
| 435 | while( realvalue>=1e8 && k++<100 ){ realvalue *= 1e-8; exp+=8; } |
| 436 | while( realvalue>=10.0 && k++<100 ){ realvalue *= 0.1; exp++; } |
| 437 | while( realvalue<1e-8 && k++<100 ){ realvalue *= 1e8; exp-=8; } |
| 438 | while( realvalue<1.0 && k++<100 ){ realvalue *= 10.0; exp--; } |
| 439 | if( k>=100 ){ |
| 440 | bufpt = "NaN"; |
| 441 | length = 3; |
| 442 | break; |
| 443 | } |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 444 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 445 | bufpt = buf; |
| 446 | /* |
| 447 | ** If the field type is etGENERIC, then convert to either etEXP |
| 448 | ** or etFLOAT, as appropriate. |
| 449 | */ |
| 450 | flag_exp = xtype==etEXP; |
| 451 | if( xtype!=etFLOAT ){ |
| 452 | realvalue += rounder; |
| 453 | if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } |
| 454 | } |
| 455 | if( xtype==etGENERIC ){ |
| 456 | flag_rtz = !flag_alternateform; |
| 457 | if( exp<-4 || exp>precision ){ |
| 458 | xtype = etEXP; |
| 459 | }else{ |
| 460 | precision = precision - exp; |
| 461 | xtype = etFLOAT; |
| 462 | } |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 463 | }else{ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 464 | flag_rtz = 0; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 465 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 466 | /* |
| 467 | ** The "exp+precision" test causes output to be of type etEXP if |
| 468 | ** the precision is too large to fit in buf[]. |
| 469 | */ |
| 470 | nsd = 0; |
| 471 | if( xtype==etFLOAT && exp+precision<etBUFSIZE-30 ){ |
| 472 | flag_dp = (precision>0 || flag_alternateform); |
| 473 | if( prefix ) *(bufpt++) = prefix; /* Sign */ |
| 474 | if( exp<0 ) *(bufpt++) = '0'; /* Digits before "." */ |
| 475 | else for(; exp>=0; exp--) *(bufpt++) = et_getdigit(&realvalue,&nsd); |
| 476 | if( flag_dp ) *(bufpt++) = '.'; /* The decimal point */ |
| 477 | for(exp++; exp<0 && precision>0; precision--, exp++){ |
| 478 | *(bufpt++) = '0'; |
| 479 | } |
| 480 | while( (precision--)>0 ) *(bufpt++) = et_getdigit(&realvalue,&nsd); |
| 481 | *(bufpt--) = 0; /* Null terminate */ |
| 482 | if( flag_rtz && flag_dp ){ /* Remove trailing zeros and "." */ |
| 483 | while( bufpt>=buf && *bufpt=='0' ) *(bufpt--) = 0; |
| 484 | if( bufpt>=buf && *bufpt=='.' ) *(bufpt--) = 0; |
| 485 | } |
| 486 | bufpt++; /* point to next free slot */ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 487 | }else{ /* etEXP or etGENERIC */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 488 | flag_dp = (precision>0 || flag_alternateform); |
| 489 | if( prefix ) *(bufpt++) = prefix; /* Sign */ |
| 490 | *(bufpt++) = et_getdigit(&realvalue,&nsd); /* First digit */ |
| 491 | if( flag_dp ) *(bufpt++) = '.'; /* Decimal point */ |
| 492 | while( (precision--)>0 ) *(bufpt++) = et_getdigit(&realvalue,&nsd); |
| 493 | bufpt--; /* point to last digit */ |
| 494 | if( flag_rtz && flag_dp ){ /* Remove tail zeros */ |
| 495 | while( bufpt>=buf && *bufpt=='0' ) *(bufpt--) = 0; |
| 496 | if( bufpt>=buf && *bufpt=='.' ) *(bufpt--) = 0; |
| 497 | } |
| 498 | bufpt++; /* point to next free slot */ |
| 499 | if( exp || flag_exp ){ |
| 500 | *(bufpt++) = infop->charset[0]; |
| 501 | if( exp<0 ){ *(bufpt++) = '-'; exp = -exp; } /* sign of exp */ |
| 502 | else { *(bufpt++) = '+'; } |
| 503 | if( exp>=100 ){ |
| 504 | *(bufpt++) = (exp/100)+'0'; /* 100's digit */ |
| 505 | exp %= 100; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 506 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 507 | *(bufpt++) = exp/10+'0'; /* 10's digit */ |
| 508 | *(bufpt++) = exp%10+'0'; /* 1's digit */ |
| 509 | } |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 510 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 511 | /* The converted number is in buf[] and zero terminated. Output it. |
| 512 | ** Note that the number is in the usual order, not reversed as with |
| 513 | ** integer conversions. */ |
drh | 94ce4c1 | 2003-06-07 11:29:50 +0000 | [diff] [blame^] | 514 | length = bufpt-buf; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 515 | bufpt = buf; |
| 516 | |
| 517 | /* Special case: Add leading zeros if the flag_zeropad flag is |
| 518 | ** set and we are not left justified */ |
| 519 | if( flag_zeropad && !flag_leftjustify && length < width){ |
| 520 | int i; |
| 521 | int nPad = width - length; |
| 522 | for(i=width; i>=nPad; i--){ |
| 523 | bufpt[i] = bufpt[i-nPad]; |
| 524 | } |
| 525 | i = prefix!=0; |
| 526 | while( nPad-- ) bufpt[i++] = '0'; |
| 527 | length = width; |
| 528 | } |
| 529 | #endif |
| 530 | break; |
| 531 | case etSIZE: |
| 532 | *(va_arg(ap,int*)) = count; |
| 533 | length = width = 0; |
| 534 | break; |
| 535 | case etPERCENT: |
| 536 | buf[0] = '%'; |
| 537 | bufpt = buf; |
| 538 | length = 1; |
| 539 | break; |
| 540 | case etCHARLIT: |
| 541 | case etCHARX: |
| 542 | c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt); |
| 543 | if( precision>=0 ){ |
| 544 | for(idx=1; idx<precision; idx++) buf[idx] = c; |
| 545 | length = precision; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 546 | }else{ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 547 | length =1; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 548 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 549 | bufpt = buf; |
| 550 | break; |
| 551 | case etSTRING: |
drh | cb48588 | 2002-08-15 13:50:48 +0000 | [diff] [blame] | 552 | bufpt = va_arg(ap,char*); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 553 | if( bufpt==0 ) bufpt = "(null)"; |
| 554 | length = strlen(bufpt); |
| 555 | if( precision>=0 && precision<length ) length = precision; |
| 556 | break; |
| 557 | case etSQLESCAPE: |
chw | 0cfcf3f | 2002-06-16 04:55:48 +0000 | [diff] [blame] | 558 | case etSQLESCAPE2: |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 559 | { |
chw | 0cfcf3f | 2002-06-16 04:55:48 +0000 | [diff] [blame] | 560 | int i, j, n, c, isnull; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 561 | char *arg = va_arg(ap,char*); |
chw | 0cfcf3f | 2002-06-16 04:55:48 +0000 | [diff] [blame] | 562 | isnull = arg==0; |
| 563 | if( isnull ) arg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 564 | for(i=n=0; (c=arg[i])!=0; i++){ |
| 565 | if( c=='\'' ) n++; |
| 566 | } |
chw | 0cfcf3f | 2002-06-16 04:55:48 +0000 | [diff] [blame] | 567 | n += i + 1 + ((!isnull && xtype==etSQLESCAPE2) ? 2 : 0); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 568 | if( n>etBUFSIZE ){ |
| 569 | bufpt = zExtra = sqliteMalloc( n ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 570 | if( bufpt==0 ) return -1; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 571 | }else{ |
| 572 | bufpt = buf; |
| 573 | } |
chw | 0cfcf3f | 2002-06-16 04:55:48 +0000 | [diff] [blame] | 574 | j = 0; |
| 575 | if( !isnull && xtype==etSQLESCAPE2 ) bufpt[j++] = '\''; |
| 576 | for(i=0; (c=arg[i])!=0; i++){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 577 | bufpt[j++] = c; |
| 578 | if( c=='\'' ) bufpt[j++] = c; |
| 579 | } |
chw | 0cfcf3f | 2002-06-16 04:55:48 +0000 | [diff] [blame] | 580 | if( !isnull && xtype==etSQLESCAPE2 ) bufpt[j++] = '\''; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 581 | bufpt[j] = 0; |
| 582 | length = j; |
| 583 | if( precision>=0 && precision<length ) length = precision; |
| 584 | } |
| 585 | break; |
| 586 | case etERROR: |
| 587 | buf[0] = '%'; |
| 588 | buf[1] = c; |
| 589 | errorflag = 0; |
| 590 | idx = 1+(c!=0); |
| 591 | (*func)(arg,"%",idx); |
| 592 | count += idx; |
| 593 | if( c==0 ) fmt--; |
| 594 | break; |
| 595 | }/* End switch over the format type */ |
| 596 | /* |
| 597 | ** The text of the conversion is pointed to by "bufpt" and is |
| 598 | ** "length" characters long. The field width is "width". Do |
| 599 | ** the output. |
| 600 | */ |
| 601 | if( !flag_leftjustify ){ |
| 602 | register int nspace; |
| 603 | nspace = width-length; |
| 604 | if( nspace>0 ){ |
| 605 | if( flag_center ){ |
| 606 | nspace = nspace/2; |
| 607 | width -= nspace; |
| 608 | flag_leftjustify = 1; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 609 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 610 | count += nspace; |
| 611 | while( nspace>=etSPACESIZE ){ |
| 612 | (*func)(arg,spaces,etSPACESIZE); |
| 613 | nspace -= etSPACESIZE; |
| 614 | } |
| 615 | if( nspace>0 ) (*func)(arg,spaces,nspace); |
| 616 | } |
| 617 | } |
| 618 | if( length>0 ){ |
| 619 | (*func)(arg,bufpt,length); |
| 620 | count += length; |
| 621 | } |
| 622 | if( flag_leftjustify ){ |
| 623 | register int nspace; |
| 624 | nspace = width-length; |
| 625 | if( nspace>0 ){ |
| 626 | count += nspace; |
| 627 | while( nspace>=etSPACESIZE ){ |
| 628 | (*func)(arg,spaces,etSPACESIZE); |
| 629 | nspace -= etSPACESIZE; |
| 630 | } |
| 631 | if( nspace>0 ) (*func)(arg,spaces,nspace); |
| 632 | } |
| 633 | } |
| 634 | if( zExtra ){ |
| 635 | sqliteFree(zExtra); |
| 636 | } |
| 637 | }/* End for loop over the format string */ |
| 638 | return errorflag ? -1 : count; |
| 639 | } /* End of function */ |
| 640 | |
| 641 | |
| 642 | /* This structure is used to store state information about the |
| 643 | ** write to memory that is currently in progress. |
| 644 | */ |
| 645 | struct sgMprintf { |
| 646 | char *zBase; /* A base allocation */ |
| 647 | char *zText; /* The string collected so far */ |
| 648 | int nChar; /* Length of the string so far */ |
| 649 | int nAlloc; /* Amount of space allocated in zText */ |
| 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 | */ |
| 658 | static void mout(void *arg, char *zNewText, int nNewChar){ |
| 659 | struct sgMprintf *pM = (struct sgMprintf*)arg; |
| 660 | if( pM->nChar + nNewChar + 1 > pM->nAlloc ){ |
| 661 | pM->nAlloc = pM->nChar + nNewChar*2 + 1; |
| 662 | if( pM->zText==pM->zBase ){ |
| 663 | pM->zText = sqliteMalloc(pM->nAlloc); |
| 664 | if( pM->zText && pM->nChar ) memcpy(pM->zText,pM->zBase,pM->nChar); |
| 665 | }else{ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 666 | char *z = sqliteRealloc(pM->zText, pM->nAlloc); |
| 667 | if( z==0 ){ |
| 668 | sqliteFree(pM->zText); |
| 669 | pM->nChar = 0; |
| 670 | pM->nAlloc = 0; |
| 671 | } |
| 672 | pM->zText = z; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | if( pM->zText ){ |
| 676 | memcpy(&pM->zText[pM->nChar], zNewText, nNewChar); |
| 677 | pM->nChar += nNewChar; |
| 678 | pM->zText[pM->nChar] = 0; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | /* |
| 683 | ** sqlite_mprintf() works like printf(), but allocations memory to hold the |
| 684 | ** resulting string and returns a pointer to the allocated memory. Use |
| 685 | ** sqliteFree() to release the memory allocated. |
| 686 | */ |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 687 | char *sqliteMPrintf(const char *zFormat, ...){ |
| 688 | va_list ap; |
| 689 | struct sgMprintf sMprintf; |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 690 | char zBuf[200]; |
| 691 | |
| 692 | sMprintf.nChar = 0; |
| 693 | sMprintf.nAlloc = sizeof(zBuf); |
| 694 | sMprintf.zText = zBuf; |
| 695 | sMprintf.zBase = zBuf; |
| 696 | va_start(ap,zFormat); |
| 697 | vxprintf(mout,&sMprintf,zFormat,ap); |
| 698 | va_end(ap); |
| 699 | sMprintf.zText[sMprintf.nChar] = 0; |
| 700 | return sqliteRealloc(sMprintf.zText, sMprintf.nChar+1); |
| 701 | } |
| 702 | |
| 703 | /* |
| 704 | ** sqlite_mprintf() works like printf(), but allocations memory to hold the |
| 705 | ** resulting string and returns a pointer to the allocated memory. Use |
| 706 | ** sqliteFree() to release the memory allocated. |
| 707 | */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 708 | char *sqlite_mprintf(const char *zFormat, ...){ |
| 709 | va_list ap; |
| 710 | struct sgMprintf sMprintf; |
| 711 | char *zNew; |
| 712 | char zBuf[200]; |
| 713 | |
| 714 | sMprintf.nChar = 0; |
| 715 | sMprintf.nAlloc = sizeof(zBuf); |
| 716 | sMprintf.zText = zBuf; |
| 717 | sMprintf.zBase = zBuf; |
| 718 | va_start(ap,zFormat); |
| 719 | vxprintf(mout,&sMprintf,zFormat,ap); |
| 720 | va_end(ap); |
| 721 | sMprintf.zText[sMprintf.nChar] = 0; |
drh | fa173a7 | 2002-07-10 21:26:00 +0000 | [diff] [blame] | 722 | zNew = malloc( sMprintf.nChar+1 ); |
| 723 | if( zNew ) strcpy(zNew,sMprintf.zText); |
| 724 | if( sMprintf.zText!=sMprintf.zBase ){ |
| 725 | sqliteFree(sMprintf.zText); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 726 | } |
| 727 | return zNew; |
| 728 | } |
| 729 | |
| 730 | /* This is the varargs version of sqlite_mprintf. |
| 731 | */ |
| 732 | char *sqlite_vmprintf(const char *zFormat, va_list ap){ |
| 733 | struct sgMprintf sMprintf; |
drh | fa173a7 | 2002-07-10 21:26:00 +0000 | [diff] [blame] | 734 | char *zNew; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 735 | char zBuf[200]; |
| 736 | sMprintf.nChar = 0; |
| 737 | sMprintf.zText = zBuf; |
| 738 | sMprintf.nAlloc = sizeof(zBuf); |
| 739 | sMprintf.zBase = zBuf; |
| 740 | vxprintf(mout,&sMprintf,zFormat,ap); |
| 741 | sMprintf.zText[sMprintf.nChar] = 0; |
drh | fa173a7 | 2002-07-10 21:26:00 +0000 | [diff] [blame] | 742 | zNew = malloc( sMprintf.nChar+1 ); |
| 743 | if( zNew ) strcpy(zNew,sMprintf.zText); |
| 744 | if( sMprintf.zText!=sMprintf.zBase ){ |
| 745 | sqliteFree(sMprintf.zText); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 746 | } |
drh | fa173a7 | 2002-07-10 21:26:00 +0000 | [diff] [blame] | 747 | return zNew; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | /* |
| 751 | ** The following four routines implement the varargs versions of the |
| 752 | ** sqlite_exec() and sqlite_get_table() interfaces. See the sqlite.h |
| 753 | ** header files for a more detailed description of how these interfaces |
| 754 | ** work. |
| 755 | ** |
| 756 | ** These routines are all just simple wrappers. |
| 757 | */ |
| 758 | int sqlite_exec_printf( |
| 759 | sqlite *db, /* An open database */ |
drh | 9f71c2e | 2001-11-03 23:57:09 +0000 | [diff] [blame] | 760 | const char *sqlFormat, /* printf-style format string for the SQL */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 761 | sqlite_callback xCallback, /* Callback function */ |
| 762 | void *pArg, /* 1st argument to callback function */ |
| 763 | char **errmsg, /* Error msg written here */ |
| 764 | ... /* Arguments to the format string. */ |
| 765 | ){ |
| 766 | va_list ap; |
| 767 | int rc; |
| 768 | |
| 769 | va_start(ap, errmsg); |
| 770 | rc = sqlite_exec_vprintf(db, sqlFormat, xCallback, pArg, errmsg, ap); |
| 771 | va_end(ap); |
| 772 | return rc; |
| 773 | } |
| 774 | int sqlite_exec_vprintf( |
| 775 | sqlite *db, /* An open database */ |
drh | 9f71c2e | 2001-11-03 23:57:09 +0000 | [diff] [blame] | 776 | const char *sqlFormat, /* printf-style format string for the SQL */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 777 | sqlite_callback xCallback, /* Callback function */ |
| 778 | void *pArg, /* 1st argument to callback function */ |
| 779 | char **errmsg, /* Error msg written here */ |
| 780 | va_list ap /* Arguments to the format string. */ |
| 781 | ){ |
| 782 | char *zSql; |
| 783 | int rc; |
| 784 | |
| 785 | zSql = sqlite_vmprintf(sqlFormat, ap); |
| 786 | rc = sqlite_exec(db, zSql, xCallback, pArg, errmsg); |
drh | fa173a7 | 2002-07-10 21:26:00 +0000 | [diff] [blame] | 787 | free(zSql); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 788 | return rc; |
| 789 | } |
| 790 | int sqlite_get_table_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 | char ***resultp, /* Result written to a char *[] that this points to */ |
| 794 | int *nrow, /* Number of result rows written here */ |
| 795 | int *ncol, /* Number of result columns written here */ |
| 796 | char **errmsg, /* Error msg written here */ |
| 797 | ... /* Arguments to the format string */ |
| 798 | ){ |
| 799 | va_list ap; |
| 800 | int rc; |
| 801 | |
| 802 | va_start(ap, errmsg); |
| 803 | rc = sqlite_get_table_vprintf(db, sqlFormat, resultp, nrow, ncol, errmsg, ap); |
| 804 | va_end(ap); |
| 805 | return rc; |
| 806 | } |
| 807 | int sqlite_get_table_vprintf( |
| 808 | sqlite *db, /* An open database */ |
drh | 9f71c2e | 2001-11-03 23:57:09 +0000 | [diff] [blame] | 809 | const char *sqlFormat, /* printf-style format string for the SQL */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 810 | char ***resultp, /* Result written to a char *[] that this points to */ |
| 811 | int *nrow, /* Number of result rows written here */ |
| 812 | int *ncolumn, /* Number of result columns written here */ |
| 813 | char **errmsg, /* Error msg written here */ |
| 814 | va_list ap /* Arguments to the format string */ |
| 815 | ){ |
| 816 | char *zSql; |
| 817 | int rc; |
| 818 | |
| 819 | zSql = sqlite_vmprintf(sqlFormat, ap); |
| 820 | rc = sqlite_get_table(db, zSql, resultp, nrow, ncolumn, errmsg); |
drh | fa173a7 | 2002-07-10 21:26:00 +0000 | [diff] [blame] | 821 | free(zSql); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 822 | return rc; |
| 823 | } |