printf.c 29 KB

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