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