You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

823 lines
29 KiB

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