print_vprintf.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. Adapted from the avr-libc vfprintf:
  3. Copyright (c) 2002, Alexander Popov (sasho@vip.bg)
  4. Copyright (c) 2002,2004,2005 Joerg Wunsch
  5. Copyright (c) 2005, Helmut Wallner
  6. Copyright (c) 2007, Dmitry Xmelkov
  7. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions are met:
  10. * Redistributions of source code must retain the above copyright
  11. notice, this list of conditions and the following disclaimer.
  12. * Redistributions in binary form must reproduce the above copyright
  13. notice, this list of conditions and the following disclaimer in
  14. the documentation and/or other materials provided with the
  15. distribution.
  16. * Neither the name of the copyright holders nor the names of
  17. contributors may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /* From: Id: printf_p_new.c,v 1.1.1.9 2002/10/15 20:10:28 joerg_wunsch Exp */
  32. /* $Id: vfprintf.c,v 1.18.2.1 2009/04/01 23:12:06 arcanum Exp $ */
  33. #include "print_vprintf.h"
  34. #include <cmath>
  35. #include <stdarg.h>
  36. #include <string.h>
  37. #include <AP_HAL/AP_HAL.h>
  38. #include "ftoa_engine.h"
  39. #include "xtoa_fast.h"
  40. #define FL_ZFILL 0x01
  41. #define FL_PLUS 0x02
  42. #define FL_SPACE 0x04
  43. #define FL_LPAD 0x08
  44. #define FL_ALT 0x10
  45. #define FL_WIDTH 0x20
  46. #define FL_PREC 0x40
  47. #define FL_LONG 0x80
  48. #define FL_LONGLONG 0x100
  49. #define FL_NEGATIVE FL_LONG
  50. #define FL_ALTUPP FL_PLUS
  51. #define FL_ALTHEX FL_SPACE
  52. #define FL_FLTUPP FL_ALT
  53. #define FL_FLTEXP FL_PREC
  54. #define FL_FLTFIX FL_LONG
  55. void print_vprintf(AP_HAL::BetterStream *s, const char *fmt, va_list ap)
  56. {
  57. unsigned char c; /* holds a char from the format string */
  58. uint16_t flags;
  59. unsigned char width;
  60. unsigned char prec;
  61. unsigned char buf[23];
  62. for (;;) {
  63. /*
  64. * Process non-format characters
  65. */
  66. for (;;) {
  67. c = *fmt++;
  68. if (!c) {
  69. return;
  70. }
  71. if (c == '%') {
  72. c = *fmt++;
  73. if (c != '%') {
  74. break;
  75. }
  76. }
  77. /* emit cr before lf to make most terminals happy */
  78. if (c == '\n') {
  79. s->write('\r');
  80. }
  81. s->write(c);
  82. }
  83. flags = 0;
  84. width = 0;
  85. prec = 0;
  86. /*
  87. * Process format adjustment characters, precision, width.
  88. */
  89. do {
  90. if (flags < FL_WIDTH) {
  91. switch (c) {
  92. case '0':
  93. flags |= FL_ZFILL;
  94. continue;
  95. case '+':
  96. flags |= FL_PLUS;
  97. FALLTHROUGH;
  98. case ' ':
  99. flags |= FL_SPACE;
  100. continue;
  101. case '-':
  102. flags |= FL_LPAD;
  103. continue;
  104. case '#':
  105. flags |= FL_ALT;
  106. continue;
  107. }
  108. }
  109. if (flags < FL_LONG) {
  110. if (c >= '0' && c <= '9') {
  111. c -= '0';
  112. if (flags & FL_PREC) {
  113. prec = 10*prec + c;
  114. continue;
  115. }
  116. width = 10*width + c;
  117. flags |= FL_WIDTH;
  118. continue;
  119. }
  120. if (c == '.') {
  121. if (flags & FL_PREC) {
  122. return;
  123. }
  124. flags |= FL_PREC;
  125. continue;
  126. }
  127. if (c == 'l') {
  128. flags |= FL_LONG;
  129. continue;
  130. }
  131. if (c == 'h') {
  132. continue;
  133. }
  134. } else if ((flags & FL_LONG) && c == 'l') {
  135. flags |= FL_LONGLONG;
  136. continue;
  137. }
  138. break;
  139. } while ((c = *fmt++) != 0);
  140. #if CONFIG_HAL_BOARD != HAL_BOARD_CHIBIOS || __FPU_PRESENT
  141. /*
  142. * Handle floating-point formats E, F, G, e, f, g.
  143. */
  144. if (c >= 'E' && c <= 'G') {
  145. flags |= FL_FLTUPP;
  146. c += 'e' - 'E';
  147. goto flt_oper;
  148. } else if (c >= 'e' && c <= 'g') {
  149. int exp; /* exponent of master decimal digit */
  150. int n;
  151. unsigned char vtype; /* result of float value parse */
  152. unsigned char sign; /* sign character (or 0) */
  153. unsigned char ndigs;
  154. flags &= ~FL_FLTUPP;
  155. flt_oper:
  156. float value = va_arg(ap,double);
  157. if (!(flags & FL_PREC)) {
  158. prec = 6;
  159. }
  160. flags &= ~(FL_FLTEXP | FL_FLTFIX);
  161. if (c == 'e') {
  162. flags |= FL_FLTEXP;
  163. } else if (c == 'f') {
  164. flags |= FL_FLTFIX;
  165. } else if (prec > 0) {
  166. prec -= 1;
  167. }
  168. if ((flags & FL_FLTFIX) && fabsf(value) > 9999999) {
  169. flags = (flags & ~FL_FLTFIX) | FL_FLTEXP;
  170. }
  171. if (flags & FL_FLTFIX) {
  172. vtype = 7; /* 'prec' arg for 'ftoa_engine' */
  173. ndigs = prec < 60 ? prec + 1 : 60;
  174. } else {
  175. if (prec > 10) {
  176. prec = 10;
  177. }
  178. vtype = prec;
  179. ndigs = 0;
  180. }
  181. memset(buf, 0, sizeof(buf));
  182. exp = ftoa_engine(value, (char *)buf, vtype, ndigs);
  183. vtype = buf[0];
  184. sign = 0;
  185. if ((vtype & FTOA_MINUS) && !(vtype & FTOA_NAN))
  186. sign = '-';
  187. else if (flags & FL_PLUS)
  188. sign = '+';
  189. else if (flags & FL_SPACE)
  190. sign = ' ';
  191. if (vtype & (FTOA_NAN | FTOA_INF)) {
  192. ndigs = sign ? 4 : 3;
  193. if (width > ndigs) {
  194. width -= ndigs;
  195. if (!(flags & FL_LPAD)) {
  196. do {
  197. s->write(' ');
  198. } while (--width);
  199. }
  200. } else {
  201. width = 0;
  202. }
  203. if (sign) {
  204. s->write(sign);
  205. }
  206. const char *p = "inf";
  207. if (vtype & FTOA_NAN)
  208. p = "nan";
  209. while ((ndigs = *p) != 0) {
  210. if (flags & FL_FLTUPP)
  211. ndigs += 'I' - 'i';
  212. s->write(ndigs);
  213. p++;
  214. }
  215. goto tail;
  216. }
  217. /* Output format adjustment, number of decimal digits in buf[] */
  218. if (flags & FL_FLTFIX) {
  219. ndigs += exp;
  220. if ((vtype & FTOA_CARRY) && buf[1] == '1') {
  221. ndigs -= 1;
  222. }
  223. if ((signed char)ndigs < 1) {
  224. ndigs = 1;
  225. } else if (ndigs > 8) {
  226. ndigs = 8;
  227. }
  228. } else if (!(flags & FL_FLTEXP)) { /* 'g(G)' format */
  229. if (exp <= prec && exp >= -4) {
  230. flags |= FL_FLTFIX;
  231. }
  232. while (prec && buf[1+prec] == '0') {
  233. prec--;
  234. }
  235. if (flags & FL_FLTFIX) {
  236. ndigs = prec + 1; /* number of digits in buf */
  237. prec = prec > exp ? prec - exp : 0; /* fractional part length */
  238. }
  239. }
  240. /* Conversion result length, width := free space length */
  241. if (flags & FL_FLTFIX) {
  242. n = (exp>0 ? exp+1 : 1);
  243. } else {
  244. n = 5; /* 1e+00 */
  245. }
  246. if (sign) {
  247. n += 1;
  248. }
  249. if (prec) {
  250. n += prec + 1;
  251. }
  252. width = width > n ? width - n : 0;
  253. /* Output before first digit */
  254. if (!(flags & (FL_LPAD | FL_ZFILL))) {
  255. while (width) {
  256. s->write(' ');
  257. width--;
  258. }
  259. }
  260. if (sign) {
  261. s->write(sign);
  262. }
  263. if (!(flags & FL_LPAD)) {
  264. while (width) {
  265. s->write('0');
  266. width--;
  267. }
  268. }
  269. if (flags & FL_FLTFIX) { /* 'f' format */
  270. n = exp > 0 ? exp : 0; /* exponent of left digit */
  271. unsigned char v = 0;
  272. do {
  273. if (n == -1) {
  274. s->write('.');
  275. }
  276. v = (n <= exp && n > exp - ndigs)
  277. ? buf[exp - n + 1] : '0';
  278. if (--n < -prec || v == 0) {
  279. break;
  280. }
  281. s->write(v);
  282. } while (1);
  283. if (n == exp
  284. && (buf[1] > '5'
  285. || (buf[1] == '5' && !(vtype & FTOA_CARRY)))) {
  286. v = '1';
  287. }
  288. if (v) {
  289. s->write(v);
  290. }
  291. } else { /* 'e(E)' format */
  292. /* mantissa */
  293. if (buf[1] != '1')
  294. vtype &= ~FTOA_CARRY;
  295. s->write(buf[1]);
  296. if (prec) {
  297. s->write('.');
  298. sign = 2;
  299. do {
  300. s->write(buf[sign++]);
  301. } while (--prec);
  302. }
  303. /* exponent */
  304. s->write(flags & FL_FLTUPP ? 'E' : 'e');
  305. ndigs = '+';
  306. if (exp < 0 || (exp == 0 && (vtype & FTOA_CARRY) != 0)) {
  307. exp = -exp;
  308. ndigs = '-';
  309. }
  310. s->write(ndigs);
  311. for (ndigs = '0'; exp >= 10; exp -= 10)
  312. ndigs += 1;
  313. s->write(ndigs);
  314. s->write('0' + exp);
  315. }
  316. goto tail;
  317. }
  318. #endif //#if CONFIG_HAL_BOARD != HAL_BOARD_CHIBIOS || __FPU_PRESENT
  319. /*
  320. * Handle string formats c, s, S.
  321. */
  322. {
  323. const char * pnt;
  324. size_t size;
  325. switch (c) {
  326. case 'c':
  327. buf[0] = va_arg (ap, int);
  328. pnt = (char *)buf;
  329. size = 1;
  330. break;
  331. case 's':
  332. pnt = va_arg (ap, char *);
  333. size = strnlen (pnt, (flags & FL_PREC) ? prec : ~0);
  334. break;
  335. default:
  336. goto non_string;
  337. }
  338. if (!(flags & FL_LPAD)) {
  339. while (size < width) {
  340. s->write(' ');
  341. width--;
  342. }
  343. }
  344. while (size) {
  345. s->write(*pnt++);
  346. if (width) width -= 1;
  347. size -= 1;
  348. }
  349. goto tail;
  350. }
  351. non_string:
  352. /*
  353. * Handle integer formats variations for d/i, u, o, p, x, X.
  354. */
  355. if (c == 'd' || c == 'i') {
  356. if (flags & FL_LONGLONG) {
  357. int64_t x = va_arg(ap,long long);
  358. flags &= ~(FL_NEGATIVE | FL_ALT);
  359. if (x < 0) {
  360. x = -x;
  361. flags |= FL_NEGATIVE;
  362. }
  363. c = ulltoa_invert (x, (char *)buf, 10) - (char *)buf;
  364. } else {
  365. long x = (flags & FL_LONG) ? va_arg(ap,long) : va_arg(ap,int);
  366. flags &= ~(FL_NEGATIVE | FL_ALT);
  367. if (x < 0) {
  368. x = -x;
  369. flags |= FL_NEGATIVE;
  370. }
  371. c = ultoa_invert (x, (char *)buf, 10) - (char *)buf;
  372. }
  373. } else {
  374. int base;
  375. if (c == 'u') {
  376. flags &= ~FL_ALT;
  377. base = 10;
  378. goto ultoa;
  379. }
  380. flags &= ~(FL_PLUS | FL_SPACE);
  381. switch (c) {
  382. case 'o':
  383. base = 8;
  384. goto ultoa;
  385. case 'p':
  386. flags |= FL_ALT;
  387. FALLTHROUGH;
  388. case 'x':
  389. if (flags & FL_ALT)
  390. flags |= FL_ALTHEX;
  391. base = 16;
  392. goto ultoa;
  393. case 'X':
  394. if (flags & FL_ALT)
  395. flags |= (FL_ALTHEX | FL_ALTUPP);
  396. base = 16 | XTOA_UPPER;
  397. ultoa:
  398. if (flags & FL_LONGLONG) {
  399. c = ulltoa_invert (va_arg(ap, unsigned long long),
  400. (char *)buf, base) - (char *)buf;
  401. } else {
  402. c = ultoa_invert ((flags & FL_LONG)
  403. ? va_arg(ap, unsigned long)
  404. : va_arg(ap, unsigned int),
  405. (char *)buf, base) - (char *)buf;
  406. }
  407. flags &= ~FL_NEGATIVE;
  408. break;
  409. default:
  410. return;
  411. }
  412. }
  413. /*
  414. * Format integers.
  415. */
  416. {
  417. unsigned char len;
  418. len = c;
  419. if (flags & FL_PREC) {
  420. flags &= ~FL_ZFILL;
  421. if (len < prec) {
  422. len = prec;
  423. if ((flags & FL_ALT) && !(flags & FL_ALTHEX)) {
  424. flags &= ~FL_ALT;
  425. }
  426. }
  427. }
  428. if (flags & FL_ALT) {
  429. if (buf[c-1] == '0') {
  430. flags &= ~(FL_ALT | FL_ALTHEX | FL_ALTUPP);
  431. } else {
  432. len += 1;
  433. if (flags & FL_ALTHEX) {
  434. len += 1;
  435. }
  436. }
  437. } else if (flags & (FL_NEGATIVE | FL_PLUS | FL_SPACE)) {
  438. len += 1;
  439. }
  440. if (!(flags & FL_LPAD)) {
  441. if (flags & FL_ZFILL) {
  442. prec = c;
  443. if (len < width) {
  444. prec += width - len;
  445. len = width;
  446. }
  447. }
  448. while (len < width) {
  449. s->write(' ');
  450. len++;
  451. }
  452. }
  453. width = (len < width) ? width - len : 0;
  454. if (flags & FL_ALT) {
  455. s->write('0');
  456. if (flags & FL_ALTHEX) {
  457. s->write(flags & FL_ALTUPP ? 'X' : 'x');
  458. }
  459. } else if (flags & (FL_NEGATIVE | FL_PLUS | FL_SPACE)) {
  460. unsigned char z = ' ';
  461. if (flags & FL_PLUS) {
  462. z = '+';
  463. }
  464. if (flags & FL_NEGATIVE) {
  465. z = '-';
  466. }
  467. s->write(z);
  468. }
  469. while (prec > c) {
  470. s->write('0');
  471. prec--;
  472. }
  473. do {
  474. s->write(buf[--c]);
  475. } while (c);
  476. }
  477. tail:
  478. /* Tail is possible. */
  479. while (width) {
  480. s->write(' ');
  481. width--;
  482. }
  483. } /* for (;;) */
  484. }