1a18c5681Sdrh /* 2a18c5681Sdrh ** The "printf" code that follows dates from the 1980's. It is in 3a18c5681Sdrh ** the public domain. The original comments are included here for 4e84a306bSdrh ** completeness. They are very out-of-date but might be useful as 5e84a306bSdrh ** an historical reference. Most of the "enhancements" have been backed 6e84a306bSdrh ** out so that the functionality is now the same as standard printf(). 7e84a306bSdrh ** 8e84a306bSdrh ************************************************************************** 9a18c5681Sdrh ** 10e78e8284Sdrh ** The following modules is an enhanced replacement for the "printf" subroutines 11e78e8284Sdrh ** found in the standard C library. The following enhancements are 12a18c5681Sdrh ** supported: 13a18c5681Sdrh ** 14a18c5681Sdrh ** + Additional functions. The standard set of "printf" functions 15a18c5681Sdrh ** includes printf, fprintf, sprintf, vprintf, vfprintf, and 16a18c5681Sdrh ** vsprintf. This module adds the following: 17a18c5681Sdrh ** 18a18c5681Sdrh ** * snprintf -- Works like sprintf, but has an extra argument 19a18c5681Sdrh ** which is the size of the buffer written to. 20a18c5681Sdrh ** 21a18c5681Sdrh ** * mprintf -- Similar to sprintf. Writes output to memory 22a18c5681Sdrh ** obtained from malloc. 23a18c5681Sdrh ** 24a18c5681Sdrh ** * xprintf -- Calls a function to dispose of output. 25a18c5681Sdrh ** 26a18c5681Sdrh ** * nprintf -- No output, but returns the number of characters 27a18c5681Sdrh ** that would have been output by printf. 28a18c5681Sdrh ** 29a18c5681Sdrh ** * A v- version (ex: vsnprintf) of every function is also 30a18c5681Sdrh ** supplied. 31a18c5681Sdrh ** 32a18c5681Sdrh ** + A few extensions to the formatting notation are supported: 33a18c5681Sdrh ** 34a18c5681Sdrh ** * The "=" flag (similar to "-") causes the output to be 35a18c5681Sdrh ** be centered in the appropriately sized field. 36a18c5681Sdrh ** 37a18c5681Sdrh ** * The %b field outputs an integer in binary notation. 38a18c5681Sdrh ** 39a18c5681Sdrh ** * The %c field now accepts a precision. The character output 40a18c5681Sdrh ** is repeated by the number of times the precision specifies. 41a18c5681Sdrh ** 42a18c5681Sdrh ** * The %' field works like %c, but takes as its character the 43a18c5681Sdrh ** next character of the format string, instead of the next 44a18c5681Sdrh ** argument. For example, printf("%.78'-") prints 78 minus 45a18c5681Sdrh ** signs, the same as printf("%.78c",'-'). 46a18c5681Sdrh ** 47a18c5681Sdrh ** + When compiled using GCC on a SPARC, this version of printf is 48a18c5681Sdrh ** faster than the library printf for SUN OS 4.1. 49a18c5681Sdrh ** 50a18c5681Sdrh ** + All functions are fully reentrant. 51a18c5681Sdrh ** 52a18c5681Sdrh */ 53a18c5681Sdrh #include "sqliteInt.h" 547c68d60bSdrh 55a18c5681Sdrh /* 56a18c5681Sdrh ** Conversion types fall into various categories as defined by the 57a18c5681Sdrh ** following enumeration. 58a18c5681Sdrh */ 59e84a306bSdrh #define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */ 60e84a306bSdrh #define etFLOAT 2 /* Floating point. %f */ 61e84a306bSdrh #define etEXP 3 /* Exponentional notation. %e and %E */ 62e84a306bSdrh #define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */ 63e84a306bSdrh #define etSIZE 5 /* Return number of characters processed so far. %n */ 64e84a306bSdrh #define etSTRING 6 /* Strings. %s */ 65e84a306bSdrh #define etDYNSTRING 7 /* Dynamically allocated strings. %z */ 66e84a306bSdrh #define etPERCENT 8 /* Percent symbol. %% */ 67e84a306bSdrh #define etCHARX 9 /* Characters. %c */ 68e84a306bSdrh #define etERROR 10 /* Used to indicate no such conversion type */ 69a18c5681Sdrh /* The rest are extensions, not normally found in printf() */ 70e84a306bSdrh #define etCHARLIT 11 /* Literal characters. %' */ 71e84a306bSdrh #define etSQLESCAPE 12 /* Strings with '\'' doubled. %q */ 72e84a306bSdrh #define etSQLESCAPE2 13 /* Strings with '\'' doubled and enclosed in '', 730cfcf3fbSchw NULL pointers replaced by SQL NULL. %Q */ 745f968436Sdrh #define etTOKEN 14 /* a pointer to a Token structure */ 755f968436Sdrh #define etSRCLIST 15 /* a pointer to a SrcList */ 76e84a306bSdrh 77e84a306bSdrh 78e84a306bSdrh /* 79e84a306bSdrh ** An "etByte" is an 8-bit unsigned value. 80e84a306bSdrh */ 81e84a306bSdrh typedef unsigned char etByte; 82a18c5681Sdrh 83a18c5681Sdrh /* 84a18c5681Sdrh ** Each builtin conversion character (ex: the 'd' in "%d") is described 85a18c5681Sdrh ** by an instance of the following structure 86a18c5681Sdrh */ 87a18c5681Sdrh typedef struct et_info { /* Information about each format field */ 88e84a306bSdrh char fmttype; /* The format field code letter */ 89e84a306bSdrh etByte base; /* The base for radix conversion */ 90e84a306bSdrh etByte flags; /* One or more of FLAG_ constants below */ 91e84a306bSdrh etByte type; /* Conversion paradigm */ 92a18c5681Sdrh char *charset; /* The character set for conversion */ 93a18c5681Sdrh char *prefix; /* Prefix on non-zero values in alt format */ 94a18c5681Sdrh } et_info; 95a18c5681Sdrh 96a18c5681Sdrh /* 97e84a306bSdrh ** Allowed values for et_info.flags 98e84a306bSdrh */ 99e84a306bSdrh #define FLAG_SIGNED 1 /* True if the value to convert is signed */ 100e84a306bSdrh #define FLAG_INTERN 2 /* True if for internal use only */ 101e84a306bSdrh 102e84a306bSdrh 103e84a306bSdrh /* 104a18c5681Sdrh ** The following table is searched linearly, so it is good to put the 105a18c5681Sdrh ** most frequently used conversion types first. 106a18c5681Sdrh */ 107a18c5681Sdrh static et_info fmtinfo[] = { 108e84a306bSdrh { 'd', 10, 1, etRADIX, "0123456789", 0 }, 109e84a306bSdrh { 's', 0, 0, etSTRING, 0, 0 }, 110e84a306bSdrh { 'z', 0, 2, etDYNSTRING, 0, 0 }, 111e84a306bSdrh { 'q', 0, 0, etSQLESCAPE, 0, 0 }, 112e84a306bSdrh { 'Q', 0, 0, etSQLESCAPE2, 0, 0 }, 113e84a306bSdrh { 'c', 0, 0, etCHARX, 0, 0 }, 114e84a306bSdrh { 'o', 8, 0, etRADIX, "01234567", "0" }, 115e84a306bSdrh { 'u', 10, 0, etRADIX, "0123456789", 0 }, 116e84a306bSdrh { 'x', 16, 0, etRADIX, "0123456789abcdef", "x0" }, 117e84a306bSdrh { 'X', 16, 0, etRADIX, "0123456789ABCDEF", "X0" }, 118e84a306bSdrh { 'f', 0, 1, etFLOAT, 0, 0 }, 119e84a306bSdrh { 'e', 0, 1, etEXP, "e", 0 }, 120e84a306bSdrh { 'E', 0, 1, etEXP, "E", 0 }, 121e84a306bSdrh { 'g', 0, 1, etGENERIC, "e", 0 }, 122e84a306bSdrh { 'G', 0, 1, etGENERIC, "E", 0 }, 123e84a306bSdrh { 'i', 10, 1, etRADIX, "0123456789", 0 }, 124e84a306bSdrh { 'n', 0, 0, etSIZE, 0, 0 }, 125e84a306bSdrh { '%', 0, 0, etPERCENT, 0, 0 }, 126e84a306bSdrh { 'p', 10, 0, etRADIX, "0123456789", 0 }, 1275f968436Sdrh { 'T', 0, 2, etTOKEN, 0, 0 }, 1285f968436Sdrh { 'S', 0, 2, etSRCLIST, 0, 0 }, 129a18c5681Sdrh }; 130a18c5681Sdrh #define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0])) 131a18c5681Sdrh 132a18c5681Sdrh /* 133a18c5681Sdrh ** If NOFLOATINGPOINT is defined, then none of the floating point 134a18c5681Sdrh ** conversions will work. 135a18c5681Sdrh */ 136a18c5681Sdrh #ifndef etNOFLOATINGPOINT 137a18c5681Sdrh /* 138a18c5681Sdrh ** "*val" is a double such that 0.1 <= *val < 10.0 139a18c5681Sdrh ** Return the ascii code for the leading digit of *val, then 140a18c5681Sdrh ** multiply "*val" by 10.0 to renormalize. 141a18c5681Sdrh ** 142a18c5681Sdrh ** Example: 143a18c5681Sdrh ** input: *val = 3.14159 144a18c5681Sdrh ** output: *val = 1.4159 function return = '3' 145a18c5681Sdrh ** 146a18c5681Sdrh ** The counter *cnt is incremented each time. After counter exceeds 147a18c5681Sdrh ** 16 (the number of significant digits in a 64-bit float) '0' is 148a18c5681Sdrh ** always returned. 149a18c5681Sdrh */ 150384eef32Sdrh static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ 151a18c5681Sdrh int digit; 152384eef32Sdrh LONGDOUBLE_TYPE d; 153a18c5681Sdrh if( (*cnt)++ >= 16 ) return '0'; 154a18c5681Sdrh digit = (int)*val; 155a18c5681Sdrh d = digit; 156a18c5681Sdrh digit += '0'; 157a18c5681Sdrh *val = (*val - d)*10.0; 158a18c5681Sdrh return digit; 159a18c5681Sdrh } 160a18c5681Sdrh #endif 161a18c5681Sdrh 162a18c5681Sdrh #define etBUFSIZE 1000 /* Size of the output buffer */ 163a18c5681Sdrh 164a18c5681Sdrh /* 165a18c5681Sdrh ** The root program. All variations call this core. 166a18c5681Sdrh ** 167a18c5681Sdrh ** INPUTS: 168a18c5681Sdrh ** func This is a pointer to a function taking three arguments 169a18c5681Sdrh ** 1. A pointer to anything. Same as the "arg" parameter. 170a18c5681Sdrh ** 2. A pointer to the list of characters to be output 171a18c5681Sdrh ** (Note, this list is NOT null terminated.) 172a18c5681Sdrh ** 3. An integer number of characters to be output. 173a18c5681Sdrh ** (Note: This number might be zero.) 174a18c5681Sdrh ** 175a18c5681Sdrh ** arg This is the pointer to anything which will be passed as the 176a18c5681Sdrh ** first argument to "func". Use it for whatever you like. 177a18c5681Sdrh ** 178a18c5681Sdrh ** fmt This is the format string, as in the usual print. 179a18c5681Sdrh ** 180a18c5681Sdrh ** ap This is a pointer to a list of arguments. Same as in 181a18c5681Sdrh ** vfprint. 182a18c5681Sdrh ** 183a18c5681Sdrh ** OUTPUTS: 184a18c5681Sdrh ** The return value is the total number of characters sent to 185a18c5681Sdrh ** the function "func". Returns -1 on a error. 186a18c5681Sdrh ** 187a18c5681Sdrh ** Note that the order in which automatic variables are declared below 188a18c5681Sdrh ** seems to make a big difference in determining how fast this beast 189a18c5681Sdrh ** will run. 190a18c5681Sdrh */ 191a18c5681Sdrh static int vxprintf( 1925f968436Sdrh void (*func)(void*,const char*,int), /* Consumer of text */ 1935f968436Sdrh void *arg, /* First argument to the consumer */ 1945f968436Sdrh int useExtended, /* Allow extended %-conversions */ 1955f968436Sdrh const char *fmt, /* Format string */ 1965f968436Sdrh va_list ap /* arguments */ 197a18c5681Sdrh ){ 198e84a306bSdrh int c; /* Next character in the format string */ 199e84a306bSdrh char *bufpt; /* Pointer to the conversion buffer */ 200e84a306bSdrh int precision; /* Precision of the current field */ 201e84a306bSdrh int length; /* Length of the field */ 202e84a306bSdrh int idx; /* A general purpose loop counter */ 203a18c5681Sdrh int count; /* Total number of characters output */ 204a18c5681Sdrh int width; /* Width of the current field */ 205e84a306bSdrh etByte flag_leftjustify; /* True if "-" flag is present */ 206e84a306bSdrh etByte flag_plussign; /* True if "+" flag is present */ 207e84a306bSdrh etByte flag_blanksign; /* True if " " flag is present */ 208e84a306bSdrh etByte flag_alternateform; /* True if "#" flag is present */ 209e84a306bSdrh etByte flag_zeropad; /* True if field width constant starts with zero */ 210e84a306bSdrh etByte flag_long; /* True if "l" flag is present */ 211a34b6764Sdrh etByte flag_longlong; /* True if the "ll" flag is present */ 212a34b6764Sdrh UINT64_TYPE longvalue; /* Value for integer types */ 213384eef32Sdrh LONGDOUBLE_TYPE realvalue; /* Value for real types */ 214a18c5681Sdrh et_info *infop; /* Pointer to the appropriate info structure */ 215a18c5681Sdrh char buf[etBUFSIZE]; /* Conversion buffer */ 216a18c5681Sdrh char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ 217e84a306bSdrh etByte errorflag = 0; /* True if an error is encountered */ 218e84a306bSdrh etByte xtype; /* Conversion paradigm */ 219a18c5681Sdrh char *zExtra; /* Extra memory used for etTCLESCAPE conversions */ 220905793e2Sdrh static char spaces[] = " "; 221a18c5681Sdrh #define etSPACESIZE (sizeof(spaces)-1) 222a18c5681Sdrh #ifndef etNOFLOATINGPOINT 223a18c5681Sdrh int exp; /* exponent of real numbers */ 224a18c5681Sdrh double rounder; /* Used for rounding floating point values */ 225e84a306bSdrh etByte flag_dp; /* True if decimal point should be shown */ 226e84a306bSdrh etByte flag_rtz; /* True if trailing zeros should be removed */ 227e84a306bSdrh etByte flag_exp; /* True to force display of the exponent */ 228a18c5681Sdrh int nsd; /* Number of significant digits returned */ 229a18c5681Sdrh #endif 230a18c5681Sdrh 231a18c5681Sdrh count = length = 0; 232a18c5681Sdrh bufpt = 0; 233a18c5681Sdrh for(; (c=(*fmt))!=0; ++fmt){ 234a18c5681Sdrh if( c!='%' ){ 235e84a306bSdrh int amt; 236a18c5681Sdrh bufpt = (char *)fmt; 237a18c5681Sdrh amt = 1; 238a18c5681Sdrh while( (c=(*++fmt))!='%' && c!=0 ) amt++; 239a18c5681Sdrh (*func)(arg,bufpt,amt); 240a18c5681Sdrh count += amt; 241a18c5681Sdrh if( c==0 ) break; 242a18c5681Sdrh } 243a18c5681Sdrh if( (c=(*++fmt))==0 ){ 244a18c5681Sdrh errorflag = 1; 245a18c5681Sdrh (*func)(arg,"%",1); 246a18c5681Sdrh count++; 247a18c5681Sdrh break; 248a18c5681Sdrh } 249a18c5681Sdrh /* Find out what flags are present */ 250a18c5681Sdrh flag_leftjustify = flag_plussign = flag_blanksign = 251e84a306bSdrh flag_alternateform = flag_zeropad = 0; 252a18c5681Sdrh do{ 253a18c5681Sdrh switch( c ){ 254a18c5681Sdrh case '-': flag_leftjustify = 1; c = 0; break; 255a18c5681Sdrh case '+': flag_plussign = 1; c = 0; break; 256a18c5681Sdrh case ' ': flag_blanksign = 1; c = 0; break; 257a18c5681Sdrh case '#': flag_alternateform = 1; c = 0; break; 258a18c5681Sdrh case '0': flag_zeropad = 1; c = 0; break; 259a18c5681Sdrh default: break; 260a18c5681Sdrh } 261a18c5681Sdrh }while( c==0 && (c=(*++fmt))!=0 ); 262a18c5681Sdrh /* Get the field width */ 263a18c5681Sdrh width = 0; 264a18c5681Sdrh if( c=='*' ){ 265a18c5681Sdrh width = va_arg(ap,int); 266a18c5681Sdrh if( width<0 ){ 267a18c5681Sdrh flag_leftjustify = 1; 268a18c5681Sdrh width = -width; 269a18c5681Sdrh } 270a18c5681Sdrh c = *++fmt; 271a18c5681Sdrh }else{ 27217a68934Sdrh while( c>='0' && c<='9' ){ 273a18c5681Sdrh width = width*10 + c - '0'; 274a18c5681Sdrh c = *++fmt; 275a18c5681Sdrh } 276a18c5681Sdrh } 277a18c5681Sdrh if( width > etBUFSIZE-10 ){ 278a18c5681Sdrh width = etBUFSIZE-10; 279a18c5681Sdrh } 280a18c5681Sdrh /* Get the precision */ 281a18c5681Sdrh if( c=='.' ){ 282a18c5681Sdrh precision = 0; 283a18c5681Sdrh c = *++fmt; 284a18c5681Sdrh if( c=='*' ){ 285a18c5681Sdrh precision = va_arg(ap,int); 286a18c5681Sdrh if( precision<0 ) precision = -precision; 287a18c5681Sdrh c = *++fmt; 288a18c5681Sdrh }else{ 28917a68934Sdrh while( c>='0' && c<='9' ){ 290a18c5681Sdrh precision = precision*10 + c - '0'; 291a18c5681Sdrh c = *++fmt; 292a18c5681Sdrh } 293a18c5681Sdrh } 294a18c5681Sdrh /* Limit the precision to prevent overflowing buf[] during conversion */ 295a18c5681Sdrh if( precision>etBUFSIZE-40 ) precision = etBUFSIZE-40; 296a18c5681Sdrh }else{ 297a18c5681Sdrh precision = -1; 298a18c5681Sdrh } 299a18c5681Sdrh /* Get the conversion type modifier */ 300a18c5681Sdrh if( c=='l' ){ 301a18c5681Sdrh flag_long = 1; 302a18c5681Sdrh c = *++fmt; 303a34b6764Sdrh if( c=='l' ){ 304a34b6764Sdrh flag_longlong = 1; 305a34b6764Sdrh c = *++fmt; 306a18c5681Sdrh }else{ 307a34b6764Sdrh flag_longlong = 0; 308a34b6764Sdrh } 309a34b6764Sdrh }else{ 310a34b6764Sdrh flag_long = flag_longlong = 0; 311a18c5681Sdrh } 312a18c5681Sdrh /* Fetch the info entry for the field */ 313a18c5681Sdrh infop = 0; 314e84a306bSdrh xtype = etERROR; 315a18c5681Sdrh for(idx=0; idx<etNINFO; idx++){ 316a18c5681Sdrh if( c==fmtinfo[idx].fmttype ){ 317a18c5681Sdrh infop = &fmtinfo[idx]; 3185f968436Sdrh if( useExtended || (infop->flags & FLAG_INTERN)==0 ){ 319e84a306bSdrh xtype = infop->type; 3205f968436Sdrh } 321a18c5681Sdrh break; 322a18c5681Sdrh } 323a18c5681Sdrh } 324a18c5681Sdrh zExtra = 0; 325a18c5681Sdrh 326a18c5681Sdrh /* 327a18c5681Sdrh ** At this point, variables are initialized as follows: 328a18c5681Sdrh ** 329a18c5681Sdrh ** flag_alternateform TRUE if a '#' is present. 330a18c5681Sdrh ** flag_plussign TRUE if a '+' is present. 331a18c5681Sdrh ** flag_leftjustify TRUE if a '-' is present or if the 332a18c5681Sdrh ** field width was negative. 333a18c5681Sdrh ** flag_zeropad TRUE if the width began with 0. 334a18c5681Sdrh ** flag_long TRUE if the letter 'l' (ell) prefixed 335a18c5681Sdrh ** the conversion character. 336a34b6764Sdrh ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed 337a34b6764Sdrh ** the conversion character. 338a18c5681Sdrh ** flag_blanksign TRUE if a ' ' is present. 339a18c5681Sdrh ** width The specified field width. This is 340a18c5681Sdrh ** always non-negative. Zero is the default. 341a18c5681Sdrh ** precision The specified precision. The default 342a18c5681Sdrh ** is -1. 343a18c5681Sdrh ** xtype The class of the conversion. 344a18c5681Sdrh ** infop Pointer to the appropriate info struct. 345a18c5681Sdrh */ 346a18c5681Sdrh switch( xtype ){ 347a18c5681Sdrh case etRADIX: 348a34b6764Sdrh if( flag_longlong ) longvalue = va_arg(ap,INT64_TYPE); 349*4adee20fSdanielk1977 else if( flag_long ) longvalue = va_arg(ap,long int); 350a18c5681Sdrh else longvalue = va_arg(ap,int); 3515f968436Sdrh #if 1 352a18c5681Sdrh /* For the format %#x, the value zero is printed "0" not "0x0". 353a18c5681Sdrh ** I think this is stupid. */ 354a18c5681Sdrh if( longvalue==0 ) flag_alternateform = 0; 355a18c5681Sdrh #else 356a18c5681Sdrh /* More sensible: turn off the prefix for octal (to prevent "00"), 357a18c5681Sdrh ** but leave the prefix for hex. */ 358a18c5681Sdrh if( longvalue==0 && infop->base==8 ) flag_alternateform = 0; 359a18c5681Sdrh #endif 360e84a306bSdrh if( infop->flags & FLAG_SIGNED ){ 361a18c5681Sdrh if( *(long*)&longvalue<0 ){ 362a18c5681Sdrh longvalue = -*(long*)&longvalue; 363a18c5681Sdrh prefix = '-'; 364a18c5681Sdrh }else if( flag_plussign ) prefix = '+'; 365a18c5681Sdrh else if( flag_blanksign ) prefix = ' '; 366a18c5681Sdrh else prefix = 0; 367a18c5681Sdrh }else prefix = 0; 368a18c5681Sdrh if( flag_zeropad && precision<width-(prefix!=0) ){ 369a18c5681Sdrh precision = width-(prefix!=0); 370a18c5681Sdrh } 3713039c0a8Sdrh bufpt = &buf[etBUFSIZE-1]; 372a18c5681Sdrh { 373a18c5681Sdrh register char *cset; /* Use registers for speed */ 374a18c5681Sdrh register int base; 375a18c5681Sdrh cset = infop->charset; 376a18c5681Sdrh base = infop->base; 377a18c5681Sdrh do{ /* Convert to ascii */ 378a18c5681Sdrh *(--bufpt) = cset[longvalue%base]; 379a18c5681Sdrh longvalue = longvalue/base; 380a18c5681Sdrh }while( longvalue>0 ); 381a18c5681Sdrh } 3823039c0a8Sdrh length = &buf[etBUFSIZE-1]-bufpt; 383a18c5681Sdrh for(idx=precision-length; idx>0; idx--){ 384a18c5681Sdrh *(--bufpt) = '0'; /* Zero pad */ 385a18c5681Sdrh } 386a18c5681Sdrh if( prefix ) *(--bufpt) = prefix; /* Add sign */ 387a18c5681Sdrh if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ 388a18c5681Sdrh char *pre, x; 389a18c5681Sdrh pre = infop->prefix; 390a18c5681Sdrh if( *bufpt!=pre[0] ){ 391a18c5681Sdrh for(pre=infop->prefix; (x=(*pre))!=0; pre++) *(--bufpt) = x; 392a18c5681Sdrh } 393a18c5681Sdrh } 3943039c0a8Sdrh length = &buf[etBUFSIZE-1]-bufpt; 395a18c5681Sdrh break; 396a18c5681Sdrh case etFLOAT: 397a18c5681Sdrh case etEXP: 398a18c5681Sdrh case etGENERIC: 399a18c5681Sdrh realvalue = va_arg(ap,double); 400a18c5681Sdrh #ifndef etNOFLOATINGPOINT 401a18c5681Sdrh if( precision<0 ) precision = 6; /* Set default precision */ 402a18c5681Sdrh if( precision>etBUFSIZE-10 ) precision = etBUFSIZE-10; 403a18c5681Sdrh if( realvalue<0.0 ){ 404a18c5681Sdrh realvalue = -realvalue; 405a18c5681Sdrh prefix = '-'; 406a18c5681Sdrh }else{ 407a18c5681Sdrh if( flag_plussign ) prefix = '+'; 408a18c5681Sdrh else if( flag_blanksign ) prefix = ' '; 409a18c5681Sdrh else prefix = 0; 410a18c5681Sdrh } 411a18c5681Sdrh if( infop->type==etGENERIC && precision>0 ) precision--; 412a18c5681Sdrh rounder = 0.0; 4135f968436Sdrh #if 0 414a18c5681Sdrh /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */ 415a18c5681Sdrh for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1); 416a18c5681Sdrh #else 417a18c5681Sdrh /* It makes more sense to use 0.5 */ 418a18c5681Sdrh for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1); 419a18c5681Sdrh #endif 420a18c5681Sdrh if( infop->type==etFLOAT ) realvalue += rounder; 421a18c5681Sdrh /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ 422a18c5681Sdrh exp = 0; 423a18c5681Sdrh if( realvalue>0.0 ){ 424b621c237Sdrh while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; } 425b621c237Sdrh while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; } 426b621c237Sdrh while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; } 427b621c237Sdrh while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; } 428b621c237Sdrh if( exp>350 || exp<-350 ){ 429a18c5681Sdrh bufpt = "NaN"; 430a18c5681Sdrh length = 3; 431a18c5681Sdrh break; 432a18c5681Sdrh } 433a18c5681Sdrh } 434a18c5681Sdrh bufpt = buf; 435a18c5681Sdrh /* 436a18c5681Sdrh ** If the field type is etGENERIC, then convert to either etEXP 437a18c5681Sdrh ** or etFLOAT, as appropriate. 438a18c5681Sdrh */ 439a18c5681Sdrh flag_exp = xtype==etEXP; 440a18c5681Sdrh if( xtype!=etFLOAT ){ 441a18c5681Sdrh realvalue += rounder; 442a18c5681Sdrh if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } 443a18c5681Sdrh } 444a18c5681Sdrh if( xtype==etGENERIC ){ 445a18c5681Sdrh flag_rtz = !flag_alternateform; 446a18c5681Sdrh if( exp<-4 || exp>precision ){ 447a18c5681Sdrh xtype = etEXP; 448a18c5681Sdrh }else{ 449a18c5681Sdrh precision = precision - exp; 450a18c5681Sdrh xtype = etFLOAT; 451a18c5681Sdrh } 452a18c5681Sdrh }else{ 453a18c5681Sdrh flag_rtz = 0; 454a18c5681Sdrh } 455a18c5681Sdrh /* 456a18c5681Sdrh ** The "exp+precision" test causes output to be of type etEXP if 457a18c5681Sdrh ** the precision is too large to fit in buf[]. 458a18c5681Sdrh */ 459a18c5681Sdrh nsd = 0; 460a18c5681Sdrh if( xtype==etFLOAT && exp+precision<etBUFSIZE-30 ){ 461a18c5681Sdrh flag_dp = (precision>0 || flag_alternateform); 462a18c5681Sdrh if( prefix ) *(bufpt++) = prefix; /* Sign */ 463a18c5681Sdrh if( exp<0 ) *(bufpt++) = '0'; /* Digits before "." */ 464a18c5681Sdrh else for(; exp>=0; exp--) *(bufpt++) = et_getdigit(&realvalue,&nsd); 465a18c5681Sdrh if( flag_dp ) *(bufpt++) = '.'; /* The decimal point */ 466a18c5681Sdrh for(exp++; exp<0 && precision>0; precision--, exp++){ 467a18c5681Sdrh *(bufpt++) = '0'; 468a18c5681Sdrh } 469a18c5681Sdrh while( (precision--)>0 ) *(bufpt++) = et_getdigit(&realvalue,&nsd); 470a18c5681Sdrh *(bufpt--) = 0; /* Null terminate */ 471a18c5681Sdrh if( flag_rtz && flag_dp ){ /* Remove trailing zeros and "." */ 472a18c5681Sdrh while( bufpt>=buf && *bufpt=='0' ) *(bufpt--) = 0; 473a18c5681Sdrh if( bufpt>=buf && *bufpt=='.' ) *(bufpt--) = 0; 474a18c5681Sdrh } 475a18c5681Sdrh bufpt++; /* point to next free slot */ 476a18c5681Sdrh }else{ /* etEXP or etGENERIC */ 477a18c5681Sdrh flag_dp = (precision>0 || flag_alternateform); 478a18c5681Sdrh if( prefix ) *(bufpt++) = prefix; /* Sign */ 479a18c5681Sdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); /* First digit */ 480a18c5681Sdrh if( flag_dp ) *(bufpt++) = '.'; /* Decimal point */ 481a18c5681Sdrh while( (precision--)>0 ) *(bufpt++) = et_getdigit(&realvalue,&nsd); 482a18c5681Sdrh bufpt--; /* point to last digit */ 483a18c5681Sdrh if( flag_rtz && flag_dp ){ /* Remove tail zeros */ 484a18c5681Sdrh while( bufpt>=buf && *bufpt=='0' ) *(bufpt--) = 0; 485a18c5681Sdrh if( bufpt>=buf && *bufpt=='.' ) *(bufpt--) = 0; 486a18c5681Sdrh } 487a18c5681Sdrh bufpt++; /* point to next free slot */ 488a18c5681Sdrh if( exp || flag_exp ){ 489a18c5681Sdrh *(bufpt++) = infop->charset[0]; 490a18c5681Sdrh if( exp<0 ){ *(bufpt++) = '-'; exp = -exp; } /* sign of exp */ 491a18c5681Sdrh else { *(bufpt++) = '+'; } 492a18c5681Sdrh if( exp>=100 ){ 493a18c5681Sdrh *(bufpt++) = (exp/100)+'0'; /* 100's digit */ 494a18c5681Sdrh exp %= 100; 495a18c5681Sdrh } 496a18c5681Sdrh *(bufpt++) = exp/10+'0'; /* 10's digit */ 497a18c5681Sdrh *(bufpt++) = exp%10+'0'; /* 1's digit */ 498a18c5681Sdrh } 499a18c5681Sdrh } 500a18c5681Sdrh /* The converted number is in buf[] and zero terminated. Output it. 501a18c5681Sdrh ** Note that the number is in the usual order, not reversed as with 502a18c5681Sdrh ** integer conversions. */ 50394ce4c1eSdrh length = bufpt-buf; 504a18c5681Sdrh bufpt = buf; 505a18c5681Sdrh 506a18c5681Sdrh /* Special case: Add leading zeros if the flag_zeropad flag is 507a18c5681Sdrh ** set and we are not left justified */ 508a18c5681Sdrh if( flag_zeropad && !flag_leftjustify && length < width){ 509a18c5681Sdrh int i; 510a18c5681Sdrh int nPad = width - length; 511a18c5681Sdrh for(i=width; i>=nPad; i--){ 512a18c5681Sdrh bufpt[i] = bufpt[i-nPad]; 513a18c5681Sdrh } 514a18c5681Sdrh i = prefix!=0; 515a18c5681Sdrh while( nPad-- ) bufpt[i++] = '0'; 516a18c5681Sdrh length = width; 517a18c5681Sdrh } 518a18c5681Sdrh #endif 519a18c5681Sdrh break; 520a18c5681Sdrh case etSIZE: 521a18c5681Sdrh *(va_arg(ap,int*)) = count; 522a18c5681Sdrh length = width = 0; 523a18c5681Sdrh break; 524a18c5681Sdrh case etPERCENT: 525a18c5681Sdrh buf[0] = '%'; 526a18c5681Sdrh bufpt = buf; 527a18c5681Sdrh length = 1; 528a18c5681Sdrh break; 529a18c5681Sdrh case etCHARLIT: 530a18c5681Sdrh case etCHARX: 531a18c5681Sdrh c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt); 532a18c5681Sdrh if( precision>=0 ){ 533a18c5681Sdrh for(idx=1; idx<precision; idx++) buf[idx] = c; 534a18c5681Sdrh length = precision; 535a18c5681Sdrh }else{ 536a18c5681Sdrh length =1; 537a18c5681Sdrh } 538a18c5681Sdrh bufpt = buf; 539a18c5681Sdrh break; 540a18c5681Sdrh case etSTRING: 541d93d8a81Sdrh case etDYNSTRING: 542cb485882Sdrh bufpt = va_arg(ap,char*); 543d93d8a81Sdrh if( bufpt==0 ){ 544d93d8a81Sdrh bufpt = ""; 545d93d8a81Sdrh }else if( xtype==etDYNSTRING ){ 546d93d8a81Sdrh zExtra = bufpt; 547d93d8a81Sdrh } 548a18c5681Sdrh length = strlen(bufpt); 549a18c5681Sdrh if( precision>=0 && precision<length ) length = precision; 550a18c5681Sdrh break; 551a18c5681Sdrh case etSQLESCAPE: 5520cfcf3fbSchw case etSQLESCAPE2: 553a18c5681Sdrh { 5540cfcf3fbSchw int i, j, n, c, isnull; 555a18c5681Sdrh char *arg = va_arg(ap,char*); 5560cfcf3fbSchw isnull = arg==0; 5570cfcf3fbSchw if( isnull ) arg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); 558a18c5681Sdrh for(i=n=0; (c=arg[i])!=0; i++){ 559a18c5681Sdrh if( c=='\'' ) n++; 560a18c5681Sdrh } 5610cfcf3fbSchw n += i + 1 + ((!isnull && xtype==etSQLESCAPE2) ? 2 : 0); 562a18c5681Sdrh if( n>etBUFSIZE ){ 563a18c5681Sdrh bufpt = zExtra = sqliteMalloc( n ); 564daffd0e5Sdrh if( bufpt==0 ) return -1; 565a18c5681Sdrh }else{ 566a18c5681Sdrh bufpt = buf; 567a18c5681Sdrh } 5680cfcf3fbSchw j = 0; 5690cfcf3fbSchw if( !isnull && xtype==etSQLESCAPE2 ) bufpt[j++] = '\''; 5700cfcf3fbSchw for(i=0; (c=arg[i])!=0; i++){ 571a18c5681Sdrh bufpt[j++] = c; 572a18c5681Sdrh if( c=='\'' ) bufpt[j++] = c; 573a18c5681Sdrh } 5740cfcf3fbSchw if( !isnull && xtype==etSQLESCAPE2 ) bufpt[j++] = '\''; 575a18c5681Sdrh bufpt[j] = 0; 576a18c5681Sdrh length = j; 577a18c5681Sdrh if( precision>=0 && precision<length ) length = precision; 578a18c5681Sdrh } 579a18c5681Sdrh break; 5805f968436Sdrh case etTOKEN: { 5815f968436Sdrh Token *pToken = va_arg(ap, Token*); 5825f968436Sdrh (*func)(arg, pToken->z, pToken->n); 5835f968436Sdrh length = width = 0; 5845f968436Sdrh break; 5855f968436Sdrh } 5865f968436Sdrh case etSRCLIST: { 5875f968436Sdrh SrcList *pSrc = va_arg(ap, SrcList*); 5885f968436Sdrh int k = va_arg(ap, int); 5895f968436Sdrh struct SrcList_item *pItem = &pSrc->a[k]; 5905f968436Sdrh assert( k>=0 && k<pSrc->nSrc ); 5915f968436Sdrh if( pItem->zDatabase && pItem->zDatabase[0] ){ 5925f968436Sdrh (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase)); 5935f968436Sdrh (*func)(arg, ".", 1); 5945f968436Sdrh } 5955f968436Sdrh (*func)(arg, pItem->zName, strlen(pItem->zName)); 5965f968436Sdrh length = width = 0; 5975f968436Sdrh break; 5985f968436Sdrh } 599a18c5681Sdrh case etERROR: 600a18c5681Sdrh buf[0] = '%'; 601a18c5681Sdrh buf[1] = c; 602a18c5681Sdrh errorflag = 0; 603a18c5681Sdrh idx = 1+(c!=0); 604a18c5681Sdrh (*func)(arg,"%",idx); 605a18c5681Sdrh count += idx; 606a18c5681Sdrh if( c==0 ) fmt--; 607a18c5681Sdrh break; 608a18c5681Sdrh }/* End switch over the format type */ 609a18c5681Sdrh /* 610a18c5681Sdrh ** The text of the conversion is pointed to by "bufpt" and is 611a18c5681Sdrh ** "length" characters long. The field width is "width". Do 612a18c5681Sdrh ** the output. 613a18c5681Sdrh */ 614a18c5681Sdrh if( !flag_leftjustify ){ 615a18c5681Sdrh register int nspace; 616a18c5681Sdrh nspace = width-length; 617a18c5681Sdrh if( nspace>0 ){ 618a18c5681Sdrh count += nspace; 619a18c5681Sdrh while( nspace>=etSPACESIZE ){ 620a18c5681Sdrh (*func)(arg,spaces,etSPACESIZE); 621a18c5681Sdrh nspace -= etSPACESIZE; 622a18c5681Sdrh } 623a18c5681Sdrh if( nspace>0 ) (*func)(arg,spaces,nspace); 624a18c5681Sdrh } 625a18c5681Sdrh } 626a18c5681Sdrh if( length>0 ){ 627a18c5681Sdrh (*func)(arg,bufpt,length); 628a18c5681Sdrh count += length; 629a18c5681Sdrh } 630a18c5681Sdrh if( flag_leftjustify ){ 631a18c5681Sdrh register int nspace; 632a18c5681Sdrh nspace = width-length; 633a18c5681Sdrh if( nspace>0 ){ 634a18c5681Sdrh count += nspace; 635a18c5681Sdrh while( nspace>=etSPACESIZE ){ 636a18c5681Sdrh (*func)(arg,spaces,etSPACESIZE); 637a18c5681Sdrh nspace -= etSPACESIZE; 638a18c5681Sdrh } 639a18c5681Sdrh if( nspace>0 ) (*func)(arg,spaces,nspace); 640a18c5681Sdrh } 641a18c5681Sdrh } 642a18c5681Sdrh if( zExtra ){ 643a18c5681Sdrh sqliteFree(zExtra); 644a18c5681Sdrh } 645a18c5681Sdrh }/* End for loop over the format string */ 646a18c5681Sdrh return errorflag ? -1 : count; 647a18c5681Sdrh } /* End of function */ 648a18c5681Sdrh 649a18c5681Sdrh 650a18c5681Sdrh /* This structure is used to store state information about the 651a18c5681Sdrh ** write to memory that is currently in progress. 652a18c5681Sdrh */ 653a18c5681Sdrh struct sgMprintf { 654a18c5681Sdrh char *zBase; /* A base allocation */ 655a18c5681Sdrh char *zText; /* The string collected so far */ 656a18c5681Sdrh int nChar; /* Length of the string so far */ 6575f968436Sdrh int nTotal; /* Output size if unconstrained */ 658a18c5681Sdrh int nAlloc; /* Amount of space allocated in zText */ 6595f968436Sdrh void *(*xRealloc)(void*,int); /* Function used to realloc memory */ 660a18c5681Sdrh }; 661a18c5681Sdrh 662a18c5681Sdrh /* 663a18c5681Sdrh ** This function implements the callback from vxprintf. 664a18c5681Sdrh ** 665a18c5681Sdrh ** This routine add nNewChar characters of text in zNewText to 666a18c5681Sdrh ** the sgMprintf structure pointed to by "arg". 667a18c5681Sdrh */ 6685f968436Sdrh static void mout(void *arg, const char *zNewText, int nNewChar){ 669a18c5681Sdrh struct sgMprintf *pM = (struct sgMprintf*)arg; 6705f968436Sdrh pM->nTotal += nNewChar; 671a18c5681Sdrh if( pM->nChar + nNewChar + 1 > pM->nAlloc ){ 6725f968436Sdrh if( pM->xRealloc==0 ){ 6735f968436Sdrh nNewChar = pM->nAlloc - pM->nChar - 1; 6745f968436Sdrh }else{ 675a18c5681Sdrh pM->nAlloc = pM->nChar + nNewChar*2 + 1; 676a18c5681Sdrh if( pM->zText==pM->zBase ){ 6775f968436Sdrh pM->zText = pM->xRealloc(0, pM->nAlloc); 6785f968436Sdrh if( pM->zText && pM->nChar ){ 6795f968436Sdrh memcpy(pM->zText, pM->zBase, pM->nChar); 6805f968436Sdrh } 681a18c5681Sdrh }else{ 6825f968436Sdrh pM->zText = pM->xRealloc(pM->zText, pM->nAlloc); 683a18c5681Sdrh } 684a18c5681Sdrh } 6855f968436Sdrh } 6865f968436Sdrh if( pM->zText && nNewChar>0 ){ 687a18c5681Sdrh memcpy(&pM->zText[pM->nChar], zNewText, nNewChar); 688a18c5681Sdrh pM->nChar += nNewChar; 689a18c5681Sdrh pM->zText[pM->nChar] = 0; 690a18c5681Sdrh } 691a18c5681Sdrh } 692a18c5681Sdrh 693a18c5681Sdrh /* 6945f968436Sdrh ** This routine is a wrapper around xprintf() that invokes mout() as 6955f968436Sdrh ** the consumer. 696a18c5681Sdrh */ 6975f968436Sdrh static char *base_vprintf( 6985f968436Sdrh void *(*xRealloc)(void*,int), /* Routine to realloc memory. May be NULL */ 6995f968436Sdrh int useInternal, /* Use internal %-conversions if true */ 7005f968436Sdrh char *zInitBuf, /* Initially write here, before mallocing */ 7015f968436Sdrh int nInitBuf, /* Size of zInitBuf[] */ 7025f968436Sdrh const char *zFormat, /* format string */ 7035f968436Sdrh va_list ap /* arguments */ 7045f968436Sdrh ){ 7055f968436Sdrh struct sgMprintf sM; 7065f968436Sdrh sM.zBase = sM.zText = zInitBuf; 7075f968436Sdrh sM.nChar = sM.nTotal = 0; 7085f968436Sdrh sM.nAlloc = nInitBuf; 7095f968436Sdrh sM.xRealloc = xRealloc; 7105f968436Sdrh vxprintf(mout, &sM, useInternal, zFormat, ap); 7115f968436Sdrh if( xRealloc ){ 7125f968436Sdrh if( sM.zText==sM.zBase ){ 7135f968436Sdrh sM.zText = xRealloc(0, sM.nChar+1); 7145f968436Sdrh memcpy(sM.zText, sM.zBase, sM.nChar+1); 7155f968436Sdrh }else if( sM.nAlloc>sM.nChar+10 ){ 7165f968436Sdrh sM.zText = xRealloc(sM.zText, sM.nChar+1); 7175f968436Sdrh } 7185f968436Sdrh } 7195f968436Sdrh return sM.zText; 720483750baSdrh } 721483750baSdrh 722483750baSdrh /* 7235f968436Sdrh ** Realloc that is a real function, not a macro. 7245f968436Sdrh */ 7255f968436Sdrh static void *printf_realloc(void *old, int size){ 7265f968436Sdrh return sqliteRealloc(old,size); 7275f968436Sdrh } 7285f968436Sdrh 7295f968436Sdrh /* 7305f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 7315f968436Sdrh ** %-conversion extensions. 7325f968436Sdrh */ 733*4adee20fSdanielk1977 char *sqlite3VMPrintf(const char *zFormat, va_list ap){ 7345f968436Sdrh char zBase[1000]; 7355f968436Sdrh return base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap); 7365f968436Sdrh } 7375f968436Sdrh 7385f968436Sdrh /* 7395f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 7405f968436Sdrh ** %-conversion extensions. 7415f968436Sdrh */ 742*4adee20fSdanielk1977 char *sqlite3MPrintf(const char *zFormat, ...){ 7435f968436Sdrh va_list ap; 7445f968436Sdrh char *z; 7455f968436Sdrh char zBase[1000]; 7465f968436Sdrh va_start(ap, zFormat); 7475f968436Sdrh z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap); 7485f968436Sdrh va_end(ap); 7495f968436Sdrh return z; 7505f968436Sdrh } 7515f968436Sdrh 7525f968436Sdrh /* 7535f968436Sdrh ** Print into memory obtained from malloc(). Do not use the internal 7545f968436Sdrh ** %-conversion extensions. This routine is for use by external users. 755483750baSdrh */ 756a18c5681Sdrh char *sqlite_mprintf(const char *zFormat, ...){ 757a18c5681Sdrh va_list ap; 7585f968436Sdrh char *z; 759a18c5681Sdrh char zBuf[200]; 760a18c5681Sdrh 761a18c5681Sdrh va_start(ap,zFormat); 7625f968436Sdrh z = base_vprintf((void*(*)(void*,int))realloc, 0, 7635f968436Sdrh zBuf, sizeof(zBuf), zFormat, ap); 764a18c5681Sdrh va_end(ap); 7655f968436Sdrh return z; 766a18c5681Sdrh } 767a18c5681Sdrh 768a18c5681Sdrh /* This is the varargs version of sqlite_mprintf. 769a18c5681Sdrh */ 770a18c5681Sdrh char *sqlite_vmprintf(const char *zFormat, va_list ap){ 771a18c5681Sdrh char zBuf[200]; 7725f968436Sdrh return base_vprintf((void*(*)(void*,int))realloc, 0, 7735f968436Sdrh zBuf, sizeof(zBuf), zFormat, ap); 774a18c5681Sdrh } 775a18c5681Sdrh 776a18c5681Sdrh /* 7775f968436Sdrh ** sqlite_snprintf() works like snprintf() except that it ignores the 77893a5c6bdSdrh ** current locale settings. This is important for SQLite because we 77993a5c6bdSdrh ** are not able to use a "," as the decimal point in place of "." as 78093a5c6bdSdrh ** specified by some locales. 78193a5c6bdSdrh */ 7825f968436Sdrh char *sqlite_snprintf(int n, char *zBuf, const char *zFormat, ...){ 7835f968436Sdrh char *z; 78493a5c6bdSdrh va_list ap; 78593a5c6bdSdrh 78693a5c6bdSdrh va_start(ap,zFormat); 7875f968436Sdrh z = base_vprintf(0, 0, zBuf, n, zFormat, ap); 78893a5c6bdSdrh va_end(ap); 7895f968436Sdrh return z; 79093a5c6bdSdrh } 79193a5c6bdSdrh 79293a5c6bdSdrh /* 793a18c5681Sdrh ** The following four routines implement the varargs versions of the 794a18c5681Sdrh ** sqlite_exec() and sqlite_get_table() interfaces. See the sqlite.h 795a18c5681Sdrh ** header files for a more detailed description of how these interfaces 796a18c5681Sdrh ** work. 797a18c5681Sdrh ** 798a18c5681Sdrh ** These routines are all just simple wrappers. 799a18c5681Sdrh */ 800a18c5681Sdrh int sqlite_exec_printf( 801a18c5681Sdrh sqlite *db, /* An open database */ 8029f71c2e0Sdrh const char *sqlFormat, /* printf-style format string for the SQL */ 803a18c5681Sdrh sqlite_callback xCallback, /* Callback function */ 804a18c5681Sdrh void *pArg, /* 1st argument to callback function */ 805a18c5681Sdrh char **errmsg, /* Error msg written here */ 806a18c5681Sdrh ... /* Arguments to the format string. */ 807a18c5681Sdrh ){ 808a18c5681Sdrh va_list ap; 809a18c5681Sdrh int rc; 810a18c5681Sdrh 811a18c5681Sdrh va_start(ap, errmsg); 812a18c5681Sdrh rc = sqlite_exec_vprintf(db, sqlFormat, xCallback, pArg, errmsg, ap); 813a18c5681Sdrh va_end(ap); 814a18c5681Sdrh return rc; 815a18c5681Sdrh } 816a18c5681Sdrh int sqlite_exec_vprintf( 817a18c5681Sdrh sqlite *db, /* An open database */ 8189f71c2e0Sdrh const char *sqlFormat, /* printf-style format string for the SQL */ 819a18c5681Sdrh sqlite_callback xCallback, /* Callback function */ 820a18c5681Sdrh void *pArg, /* 1st argument to callback function */ 821a18c5681Sdrh char **errmsg, /* Error msg written here */ 822a18c5681Sdrh va_list ap /* Arguments to the format string. */ 823a18c5681Sdrh ){ 824a18c5681Sdrh char *zSql; 825a18c5681Sdrh int rc; 826a18c5681Sdrh 827a18c5681Sdrh zSql = sqlite_vmprintf(sqlFormat, ap); 828a18c5681Sdrh rc = sqlite_exec(db, zSql, xCallback, pArg, errmsg); 829fa173a76Sdrh free(zSql); 830a18c5681Sdrh return rc; 831a18c5681Sdrh } 832a18c5681Sdrh int sqlite_get_table_printf( 833a18c5681Sdrh sqlite *db, /* An open database */ 8349f71c2e0Sdrh const char *sqlFormat, /* printf-style format string for the SQL */ 835a18c5681Sdrh char ***resultp, /* Result written to a char *[] that this points to */ 836a18c5681Sdrh int *nrow, /* Number of result rows written here */ 837a18c5681Sdrh int *ncol, /* Number of result columns written here */ 838a18c5681Sdrh char **errmsg, /* Error msg written here */ 839a18c5681Sdrh ... /* Arguments to the format string */ 840a18c5681Sdrh ){ 841a18c5681Sdrh va_list ap; 842a18c5681Sdrh int rc; 843a18c5681Sdrh 844a18c5681Sdrh va_start(ap, errmsg); 845a18c5681Sdrh rc = sqlite_get_table_vprintf(db, sqlFormat, resultp, nrow, ncol, errmsg, ap); 846a18c5681Sdrh va_end(ap); 847a18c5681Sdrh return rc; 848a18c5681Sdrh } 849a18c5681Sdrh int sqlite_get_table_vprintf( 850a18c5681Sdrh sqlite *db, /* An open database */ 8519f71c2e0Sdrh const char *sqlFormat, /* printf-style format string for the SQL */ 852a18c5681Sdrh char ***resultp, /* Result written to a char *[] that this points to */ 853a18c5681Sdrh int *nrow, /* Number of result rows written here */ 854a18c5681Sdrh int *ncolumn, /* Number of result columns written here */ 855a18c5681Sdrh char **errmsg, /* Error msg written here */ 856a18c5681Sdrh va_list ap /* Arguments to the format string */ 857a18c5681Sdrh ){ 858a18c5681Sdrh char *zSql; 859a18c5681Sdrh int rc; 860a18c5681Sdrh 861a18c5681Sdrh zSql = sqlite_vmprintf(sqlFormat, ap); 862a18c5681Sdrh rc = sqlite_get_table(db, zSql, resultp, nrow, ncolumn, errmsg); 863fa173a76Sdrh free(zSql); 864a18c5681Sdrh return rc; 865a18c5681Sdrh } 866*4adee20fSdanielk1977 867*4adee20fSdanielk1977 868*4adee20fSdanielk1977 869