io.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. /*
  2. * Copyright (c) 2008-2010 Stefan Krah. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #include "mpdecimal.h"
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <ctype.h>
  32. #include <limits.h>
  33. #include <assert.h>
  34. #ifdef _WIN32_WCE
  35. static int errno = 0;
  36. #define EINVAL -1
  37. #else
  38. #include <errno.h>
  39. #include <locale.h>
  40. #endif
  41. #include "bits.h"
  42. #include "constants.h"
  43. #include "memory.h"
  44. #include "typearith.h"
  45. #include "io.h"
  46. /*
  47. * Work around the behavior of tolower() and strcasecmp() in certain
  48. * locales. For example, in tr_TR.utf8:
  49. *
  50. * tolower((unsigned char)'I') == 'I'
  51. *
  52. * u is the exact uppercase version of l; n is strlen(l) or strlen(l)+1
  53. */
  54. static inline int
  55. _mpd_strneq(const char *s, const char *l, const char *u, size_t n)
  56. {
  57. while (--n != SIZE_MAX) {
  58. if (*s != *l && *s != *u) {
  59. return 0;
  60. }
  61. s++; u++; l++;
  62. }
  63. return 1;
  64. }
  65. static mpd_ssize_t
  66. strtoexp(const char *s)
  67. {
  68. char *end;
  69. mpd_ssize_t retval;
  70. errno = 0;
  71. retval = mpd_strtossize(s, &end, 10);
  72. if (errno == 0 && !(*s != '\0' && *end == '\0'))
  73. errno = EINVAL;
  74. return retval;
  75. }
  76. /*
  77. * Scan 'len' words. The most significant word contains 'r' digits,
  78. * the remaining words are full words. Skip dpoint. The string 's' must
  79. * consist of digits and an optional single decimal point at 'dpoint'.
  80. */
  81. static void
  82. string_to_coeff(mpd_uint_t *data, const char *s, const char *dpoint, int r,
  83. size_t len)
  84. {
  85. int j;
  86. if (r > 0) {
  87. data[--len] = 0;
  88. for (j = 0; j < r; j++, s++) {
  89. if (s == dpoint) s++;
  90. data[len] = 10 * data[len] + (*s - '0');
  91. }
  92. }
  93. while (--len != SIZE_MAX) {
  94. data[len] = 0;
  95. for (j = 0; j < MPD_RDIGITS; j++, s++) {
  96. if (s == dpoint) s++;
  97. data[len] = 10 * data[len] + (*s - '0');
  98. }
  99. }
  100. }
  101. /*
  102. * Scan for at most one decimal point and at most one indicator.
  103. * Remove unneeded zeros before and after the decimal point.
  104. * The first relevant digit will be the start of the coefficient.
  105. * A decimal point may occur before an indicator.
  106. * Plus or minus may occur directly after the indicator.
  107. * The rest of the characters must be decimal digits.
  108. *
  109. * Return start of the coefficient or NULL or error. The end of
  110. * the string is stored in 'end'.
  111. */
  112. static const char *
  113. scan_dpoint_exp(const char *s, const char **dpoint, const char **exp,
  114. const char **end)
  115. {
  116. const char *coeff = NULL;
  117. *dpoint = NULL;
  118. *exp = NULL;
  119. for (; *s != '\0'; s++) {
  120. switch (*s) {
  121. case '.':
  122. if (*dpoint != NULL || *exp != NULL)
  123. return NULL;
  124. *dpoint = s;
  125. break;
  126. case 'E': case 'e':
  127. if (*exp != NULL)
  128. return NULL;
  129. *exp = s;
  130. if (*(s+1) == '+' || *(s+1) == '-')
  131. s++;
  132. break;
  133. default:
  134. if (!isdigit((uchar)*s))
  135. return NULL;
  136. if (coeff == NULL && *exp == NULL) {
  137. if (*s == '0') {
  138. if (!isdigit((uchar)*(s+1)))
  139. if (!(*(s+1) == '.' &&
  140. isdigit((uchar)*(s+2))))
  141. coeff = s;
  142. }
  143. else {
  144. coeff = s;
  145. }
  146. }
  147. break;
  148. }
  149. }
  150. *end = s;
  151. return coeff;
  152. }
  153. /* scan the payload of a NaN */
  154. static const char *
  155. scan_payload(const char *s, const char **end)
  156. {
  157. const char *coeff;
  158. while (*s == '0')
  159. s++;
  160. coeff = s;
  161. while (isdigit((uchar)*s))
  162. s++;
  163. *end = s;
  164. return (*s == '\0') ? coeff : NULL;
  165. }
  166. /* convert a character string to a decimal */
  167. void
  168. mpd_qset_string(mpd_t *dec, const char *s, const mpd_context_t *ctx,
  169. uint32_t *status)
  170. {
  171. mpd_ssize_t q, r, len;
  172. const char *coeff, *end;
  173. const char *dpoint = NULL, *exp = NULL;
  174. size_t digits;
  175. uint8_t sign = MPD_POS;
  176. mpd_set_flags(dec, 0);
  177. dec->len = 0;
  178. dec->exp = 0;
  179. /* [flags] */
  180. if (*s == '+') {
  181. s++;
  182. }
  183. else if (*s == '-') {
  184. mpd_set_negative(dec);
  185. sign = MPD_NEG;
  186. s++;
  187. }
  188. if (_mpd_strneq(s, "nan", "NAN", 3)) { /* NaN */
  189. s += 3;
  190. mpd_setspecial(dec, sign, MPD_NAN);
  191. if (*s == '\0')
  192. return;
  193. /* only digits for the diagnostic code */
  194. if ((coeff = scan_payload(s, &end)) == NULL)
  195. goto conversion_error;
  196. /* payload consists entirely of zeros */
  197. if (*coeff == '\0')
  198. return;
  199. digits = end - coeff;
  200. /* prec >= 1, clamp is 0 or 1 */
  201. if (digits > (size_t)(ctx->prec-ctx->clamp))
  202. goto conversion_error;
  203. } /* sNaN */
  204. else if (_mpd_strneq(s, "snan", "SNAN", 4)) {
  205. s += 4;
  206. mpd_setspecial(dec, sign, MPD_SNAN);
  207. if (*s == '\0')
  208. return;
  209. /* only digits for the diagnostic code */
  210. if ((coeff = scan_payload(s, &end)) == NULL)
  211. goto conversion_error;
  212. /* payload consists entirely of zeros */
  213. if (*coeff == '\0')
  214. return;
  215. digits = end - coeff;
  216. if (digits > (size_t)(ctx->prec-ctx->clamp))
  217. goto conversion_error;
  218. }
  219. else if (_mpd_strneq(s, "inf", "INF", 3)) {
  220. s += 3;
  221. if (*s == '\0' || _mpd_strneq(s, "inity", "INITY", 6)) {
  222. /* numeric-value: infinity */
  223. mpd_setspecial(dec, sign, MPD_INF);
  224. return;
  225. }
  226. goto conversion_error;
  227. }
  228. else {
  229. /* scan for start of coefficient, decimal point, indicator, end */
  230. if ((coeff = scan_dpoint_exp(s, &dpoint, &exp, &end)) == NULL)
  231. goto conversion_error;
  232. /* numeric-value: [exponent-part] */
  233. if (exp) {
  234. /* exponent-part */
  235. end = exp; exp++;
  236. dec->exp = strtoexp(exp);
  237. if (errno) {
  238. if (!(errno == ERANGE &&
  239. (dec->exp == MPD_SSIZE_MAX ||
  240. dec->exp == MPD_SSIZE_MIN)))
  241. goto conversion_error;
  242. }
  243. }
  244. digits = end - coeff;
  245. if (dpoint) {
  246. size_t fracdigits = end-dpoint-1;
  247. if (dpoint > coeff) digits--;
  248. if (fracdigits > MPD_MAX_PREC) {
  249. goto conversion_error;
  250. }
  251. if (dec->exp < (MPD_SSIZE_MIN+1)+(mpd_ssize_t)fracdigits) {
  252. dec->exp = MPD_SSIZE_MIN+1;
  253. }
  254. else {
  255. dec->exp -= (mpd_ssize_t)fracdigits;
  256. }
  257. }
  258. if (digits > MPD_MAX_PREC) {
  259. goto conversion_error;
  260. }
  261. if (dec->exp > MPD_EXP_INF) {
  262. dec->exp = MPD_EXP_INF;
  263. }
  264. }
  265. _mpd_idiv_word(&q, &r, (mpd_ssize_t)digits, MPD_RDIGITS);
  266. len = (r == 0) ? q : q+1;
  267. if (len == 0) {
  268. goto conversion_error; /* GCOV_NOT_REACHED */
  269. }
  270. if (!mpd_qresize(dec, len, status)) {
  271. mpd_seterror(dec, MPD_Malloc_error, status);
  272. return;
  273. }
  274. dec->len = len;
  275. string_to_coeff(dec->data, coeff, dpoint, (int)r, len);
  276. mpd_setdigits(dec);
  277. mpd_qfinalize(dec, ctx, status);
  278. return;
  279. conversion_error:
  280. /* standard wants a positive NaN */
  281. mpd_seterror(dec, MPD_Conversion_syntax, status);
  282. }
  283. /* print the exponent to a string */
  284. static inline char *
  285. exp_to_string(char *s, mpd_ssize_t x)
  286. {
  287. mpd_ssize_t q, d;
  288. char sign = '+';
  289. int j;
  290. if (x < 0) {
  291. sign = '-';
  292. x = -x;
  293. }
  294. *s++ = sign;
  295. j = mpd_exp_digits(x) - 1;
  296. for (; j != 0; --j) {
  297. d = mpd_pow10[j];
  298. q = x / d;
  299. x -= d * q;
  300. *s++ = '0' + (char)q;
  301. }
  302. *s++ = '0' + (char)x;
  303. return s;
  304. }
  305. /* print coefficient to string, len(dec) > 0 */
  306. static inline char *
  307. coeff_to_string(char *s, const mpd_t *dec)
  308. {
  309. mpd_uint_t x, q, d;
  310. mpd_ssize_t i;
  311. int j;
  312. /* most significant word */
  313. x = mpd_msword(dec);
  314. j = mpd_word_digits(x) - 1;
  315. for (; j != 0; --j) {
  316. d = mpd_pow10[j];
  317. q = x / d;
  318. x -= d * q;
  319. *s++ = '0' + (char)q;
  320. }
  321. *s++ = '0' + (char)x;
  322. /* remaining full words */
  323. for (i=dec->len-2; i >= 0; --i) {
  324. x = dec->data[i];
  325. for (j=MPD_RDIGITS-1; j != 0; --j) {
  326. d = mpd_pow10[j];
  327. q = x / d;
  328. x -= d * q;
  329. *s++ = '0' + (char)q;
  330. }
  331. *s++ = '0' + (char)x;
  332. }
  333. return s;
  334. }
  335. /* print coefficient to string, len(dec) > 0 */
  336. static inline char *
  337. coeff_to_string_dot(char *s, char *dot, const mpd_t *dec)
  338. {
  339. mpd_uint_t x, q, d;
  340. mpd_ssize_t i;
  341. int j;
  342. /* most significant word */
  343. x = mpd_msword(dec);
  344. j = mpd_word_digits(x) - 1;
  345. for (; j >= 0; --j) {
  346. if (s == dot)
  347. *s++ = '.';
  348. d = mpd_pow10[j];
  349. q = x / d;
  350. x -= d * q;
  351. *s++ = '0' + (char)q;
  352. }
  353. /* remaining full words */
  354. for (i=dec->len-2; i >= 0; --i) {
  355. x = dec->data[i];
  356. for (j=MPD_RDIGITS-1; j >= 0; --j) {
  357. if (s == dot)
  358. *s++ = '.';
  359. d = mpd_pow10[j];
  360. q = x / d;
  361. x -= d * q;
  362. *s++ = '0' + (char)q;
  363. }
  364. }
  365. return s;
  366. }
  367. /* Format type */
  368. #define MPD_FMT_LOWER 0x00000000
  369. #define MPD_FMT_UPPER 0x00000001
  370. #define MPD_FMT_TOSCI 0x00000002
  371. #define MPD_FMT_TOENG 0x00000004
  372. #define MPD_FMT_EXP 0x00000008
  373. #define MPD_FMT_FIXED 0x00000010
  374. #define MPD_FMT_PERCENT 0x00000020
  375. #define MPD_FMT_SIGN_SPACE 0x00000040
  376. #define MPD_FMT_SIGN_PLUS 0x00000080
  377. /*
  378. * Return the string representation of a decimal. Formatting is done according
  379. * to 'flags'. A return value of NULL indicates MPD_Malloc_error.
  380. *
  381. * To allow formatting like [0e15, '.6e' -> 0.000000e-9], 'zeroexp' is used
  382. * in combination with MPD_FMT_FIXED (see mpd_qformat for details).
  383. */
  384. static char *
  385. _mpd_to_string(const mpd_t *dec, int flags, mpd_ssize_t zeroexp)
  386. {
  387. char *decstring = NULL, *cp = NULL;
  388. mpd_ssize_t ldigits, dplace;
  389. mpd_ssize_t mem = 0, k;
  390. if (mpd_isspecial(dec)) {
  391. mem = sizeof "-Infinity";
  392. if (mpd_isnan(dec) && dec->len > 0) {
  393. /* diagnostic code */
  394. mem += dec->digits;
  395. }
  396. cp = decstring = mpd_alloc(mem, sizeof *decstring);
  397. if (cp == NULL) {
  398. return NULL;
  399. }
  400. if (mpd_isnegative(dec)) {
  401. *cp++ = '-';
  402. }
  403. else if (flags&MPD_FMT_SIGN_SPACE) {
  404. *cp++ = ' ';
  405. }
  406. else if (flags&MPD_FMT_SIGN_PLUS) {
  407. *cp++ = '+';
  408. }
  409. if (mpd_isnan(dec)) {
  410. if (mpd_isqnan(dec)) {
  411. strcpy(cp, "NaN");
  412. cp += 3;
  413. }
  414. else {
  415. strcpy(cp, "sNaN");
  416. cp += 4;
  417. }
  418. if (dec->len > 0) { /* diagnostic code */
  419. cp = coeff_to_string(cp, dec);
  420. }
  421. }
  422. else if (mpd_isinfinite(dec)) {
  423. strcpy(cp, "Infinity");
  424. cp += 8;
  425. }
  426. else { /* debug */
  427. abort(); /* GCOV_NOT_REACHED */
  428. }
  429. }
  430. else {
  431. assert(dec->len > 0);
  432. /*
  433. * ldigits: Digits to the left of the decimal point, as if the
  434. * number was written without exponent notation.
  435. *
  436. * dplace: Position of the decimal point relative to the first
  437. * member of the coefficient.
  438. *
  439. * 0.00000_.____._____000000.
  440. * ^ ^ ^ ^
  441. * | | | |
  442. * | | | `- dplace in the last position
  443. * | | `- dplace in the middle of the coefficient
  444. * | ` dplace = 1 (after the first coefficient digit)
  445. * `- dplace is negative
  446. */
  447. ldigits = dec->digits + dec->exp;
  448. dplace = 1; /* default for MPD_FMT_TOSCI, MPD_FMT_EXP */
  449. if (flags&MPD_FMT_EXP) {
  450. ;
  451. }
  452. else if (flags&MPD_FMT_FIXED || (dec->exp <= 0 && ldigits > -6)) {
  453. /* MPD_FMT_FIXED: always use fixed point notation.
  454. * MPD_FMT_TOSCI, MPD_FMT_TOENG: for a certain range,
  455. * override exponent notation. */
  456. dplace = ldigits;
  457. }
  458. else if (flags&MPD_FMT_TOENG) {
  459. if (mpd_iszero(dec)) {
  460. /* If the exponent is divisible by three,
  461. * dplace = 1. Otherwise, move dplace one
  462. * or two places to the left. */
  463. dplace = -1 + mod_mpd_ssize_t(dec->exp+2, 3);
  464. }
  465. else { /* ldigits-1 is the adjusted exponent, which
  466. * should be divisible by three. If not, move
  467. * dplace one or two places to the right. */
  468. dplace += mod_mpd_ssize_t(ldigits-1, 3);
  469. }
  470. }
  471. /*
  472. * Basic space requirements:
  473. *
  474. * [-][.][coeffdigits][E][-][expdigits+1][%]['\0']
  475. *
  476. * If the decimal point lies outside of the coefficient digits,
  477. * space is adjusted accordingly.
  478. */
  479. if (dplace <= 0) {
  480. mem = -dplace + dec->digits + 2;
  481. }
  482. else if (dplace >= dec->digits) {
  483. mem = dplace;
  484. }
  485. else {
  486. mem = dec->digits;
  487. }
  488. mem += (MPD_EXPDIGITS+1+6);
  489. cp = decstring = mpd_alloc(mem, sizeof *decstring);
  490. if (cp == NULL) {
  491. return NULL;
  492. }
  493. if (mpd_isnegative(dec)) {
  494. *cp++ = '-';
  495. }
  496. else if (flags&MPD_FMT_SIGN_SPACE) {
  497. *cp++ = ' ';
  498. }
  499. else if (flags&MPD_FMT_SIGN_PLUS) {
  500. *cp++ = '+';
  501. }
  502. if (dplace <= 0) {
  503. /* space: -dplace+dec->digits+2 */
  504. *cp++ = '0';
  505. *cp++ = '.';
  506. for (k = 0; k < -dplace; k++) {
  507. *cp++ = '0';
  508. }
  509. cp = coeff_to_string(cp, dec);
  510. }
  511. else if (dplace >= dec->digits) {
  512. /* space: dplace */
  513. cp = coeff_to_string(cp, dec);
  514. for (k = 0; k < dplace-dec->digits; k++) {
  515. *cp++ = '0';
  516. }
  517. }
  518. else {
  519. /* space: dec->digits+1 */
  520. cp = coeff_to_string_dot(cp, cp+dplace, dec);
  521. }
  522. /*
  523. * Conditions for printing an exponent:
  524. *
  525. * MPD_FMT_TOSCI, MPD_FMT_TOENG: only if ldigits != dplace
  526. * MPD_FMT_FIXED: never (ldigits == dplace)
  527. * MPD_FMT_EXP: always
  528. * MPD_FMT_FIXED with zeroexp: always
  529. */
  530. if (ldigits != dplace || flags&MPD_FMT_EXP || zeroexp != MPD_SSIZE_MAX) {
  531. /* space: expdigits+2 */
  532. mpd_ssize_t x = (zeroexp != MPD_SSIZE_MAX) ? zeroexp : ldigits-dplace;
  533. *cp++ = (flags&MPD_FMT_UPPER) ? 'E' : 'e';
  534. cp = exp_to_string(cp, x);
  535. }
  536. if (flags&MPD_FMT_PERCENT) {
  537. *cp++ = '%';
  538. }
  539. }
  540. assert(cp < decstring+mem);
  541. *cp = '\0';
  542. return decstring;
  543. }
  544. char *
  545. mpd_to_sci(const mpd_t *dec, int fmt)
  546. {
  547. int flags = MPD_FMT_TOSCI;
  548. flags |= fmt ? MPD_FMT_UPPER : MPD_FMT_LOWER;
  549. return _mpd_to_string(dec, flags, MPD_SSIZE_MAX);
  550. }
  551. char *
  552. mpd_to_eng(const mpd_t *dec, int fmt)
  553. {
  554. int flags = MPD_FMT_TOENG;
  555. flags |= fmt ? MPD_FMT_UPPER : MPD_FMT_LOWER;
  556. return _mpd_to_string(dec, flags, MPD_SSIZE_MAX);
  557. }
  558. /* Copy a single UTF-8 char to dest. */
  559. static int
  560. _mpd_copy_utf8(char dest[5], const char *s)
  561. {
  562. const uchar *cp = (const uchar *)s;
  563. uchar lb, ub;
  564. int count, i;
  565. if (*cp == 0) {
  566. /* empty string */
  567. dest[0] = '\0';
  568. return 0;
  569. }
  570. else if (*cp <= 0x7f) {
  571. /* ascii */
  572. dest[0] = *cp;
  573. dest[1] = '\0';
  574. return 1;
  575. }
  576. else if (0xc2 <= *cp && *cp <= 0xdf) {
  577. lb = 0x80; ub = 0xbf;
  578. count = 2;
  579. }
  580. else if (*cp == 0xe0) {
  581. lb = 0xa0; ub = 0xbf;
  582. count = 3;
  583. }
  584. else if (*cp <= 0xec) {
  585. lb = 0x80; ub = 0xbf;
  586. count = 3;
  587. }
  588. else if (*cp == 0xed) {
  589. lb = 0x80; ub = 0x9f;
  590. count = 3;
  591. }
  592. else if (*cp <= 0xef) {
  593. lb = 0x80; ub = 0xbf;
  594. count = 3;
  595. }
  596. else if (*cp == 0xf0) {
  597. lb = 0x90; ub = 0xbf;
  598. count = 4;
  599. }
  600. else if (*cp <= 0xf3) {
  601. lb = 0x80; ub = 0xbf;
  602. count = 4;
  603. }
  604. else if (*cp == 0xf4) {
  605. lb = 0x80; ub = 0x8f;
  606. count = 4;
  607. }
  608. else {
  609. /* invalid */
  610. goto error;
  611. }
  612. dest[0] = *cp++;
  613. if (*cp < lb || ub < *cp) {
  614. goto error;
  615. }
  616. dest[1] = *cp++;
  617. for (i = 2; i < count; i++) {
  618. if (*cp < 0x80 || 0xbf < *cp) {
  619. goto error;
  620. }
  621. dest[i] = *cp++;
  622. }
  623. dest[i] = '\0';
  624. return count;
  625. error:
  626. dest[0] = '\0';
  627. return -1;
  628. }
  629. int
  630. mpd_parse_fmt_str(mpd_spec_t *spec, const char *fmt, int caps)
  631. {
  632. char *cp = (char *)fmt;
  633. int have_align = 0, n;
  634. /* defaults */
  635. spec->min_width = 0;
  636. spec->prec = -1;
  637. spec->type = caps ? 'G' : 'g';
  638. spec->align = '>';
  639. spec->sign = '-';
  640. spec->dot = "";
  641. spec->sep = "";
  642. spec->grouping = "";
  643. /* presume that the first character is a UTF-8 fill character */
  644. if ((n = _mpd_copy_utf8(spec->fill, cp)) < 0) {
  645. return 0;
  646. }
  647. /* alignment directive, prefixed by a fill character */
  648. if (*cp && (*(cp+n) == '<' || *(cp+n) == '>' ||
  649. *(cp+n) == '=' || *(cp+n) == '^')) {
  650. cp += n;
  651. spec->align = *cp++;
  652. have_align = 1;
  653. } /* alignment directive */
  654. else {
  655. /* default fill character */
  656. spec->fill[0] = ' ';
  657. spec->fill[1] = '\0';
  658. if (*cp == '<' || *cp == '>' ||
  659. *cp == '=' || *cp == '^') {
  660. spec->align = *cp++;
  661. have_align = 1;
  662. }
  663. }
  664. /* sign formatting */
  665. if (*cp == '+' || *cp == '-' || *cp == ' ') {
  666. spec->sign = *cp++;
  667. }
  668. /* zero padding */
  669. if (*cp == '0') {
  670. /* zero padding implies alignment, which should not be
  671. * specified twice. */
  672. if (have_align) {
  673. return 0;
  674. }
  675. spec->align = 'z';
  676. spec->fill[0] = *cp++;
  677. spec->fill[1] = '\0';
  678. }
  679. /* minimum width */
  680. if (isdigit((uchar)*cp)) {
  681. if (*cp == '0') {
  682. return 0;
  683. }
  684. errno = 0;
  685. spec->min_width = mpd_strtossize(cp, &cp, 10);
  686. if (errno == ERANGE || errno == EINVAL) {
  687. return 0;
  688. }
  689. }
  690. /* thousands separator */
  691. if (*cp == ',') {
  692. spec->dot = ".";
  693. spec->sep = ",";
  694. spec->grouping = "\003\003";
  695. cp++;
  696. }
  697. /* fraction digits or significant digits */
  698. if (*cp == '.') {
  699. cp++;
  700. if (!isdigit((uchar)*cp)) {
  701. return 0;
  702. }
  703. errno = 0;
  704. spec->prec = mpd_strtossize(cp, &cp, 10);
  705. if (errno == ERANGE || errno == EINVAL) {
  706. return 0;
  707. }
  708. }
  709. /* type */
  710. if (*cp == 'E' || *cp == 'e' || *cp == 'F' || *cp == 'f' ||
  711. *cp == 'G' || *cp == 'g' || *cp == '%') {
  712. spec->type = *cp++;
  713. }
  714. else if (*cp == 'N' || *cp == 'n') {
  715. /* locale specific conversion */
  716. struct lconv *lc;
  717. spec->type = *cp++;
  718. /* separator has already been specified */
  719. if (*spec->sep) return 0;
  720. spec->type = (spec->type == 'N') ? 'G' : 'g';
  721. lc = localeconv();
  722. spec->dot = lc->decimal_point;
  723. spec->sep = lc->thousands_sep;
  724. spec->grouping = lc->grouping;
  725. }
  726. /* check correctness */
  727. if (*cp != '\0') {
  728. return 0;
  729. }
  730. return 1;
  731. }
  732. /*
  733. * The following functions assume that spec->min_width <= MPD_MAX_PREC, which
  734. * is made sure in mpd_qformat_spec. Then, even with a spec that inserts a
  735. * four-byte separator after each digit, nbytes in the following struct
  736. * cannot overflow.
  737. */
  738. /* Multibyte string */
  739. typedef struct {
  740. mpd_ssize_t nbytes; /* length in bytes */
  741. mpd_ssize_t nchars; /* length in chars */
  742. mpd_ssize_t cur; /* current write index */
  743. char *data;
  744. } mpd_mbstr_t;
  745. static inline void
  746. _mpd_bcopy(char *dest, const char *src, mpd_ssize_t n)
  747. {
  748. while (--n >= 0) {
  749. dest[n] = src[n];
  750. }
  751. }
  752. static inline void
  753. _mbstr_copy_char(mpd_mbstr_t *dest, const char *src, mpd_ssize_t n)
  754. {
  755. dest->nbytes += n;
  756. dest->nchars += 1;
  757. dest->cur -= n;
  758. if (dest->data != NULL) {
  759. _mpd_bcopy(dest->data+dest->cur, src, n);
  760. }
  761. }
  762. static inline void
  763. _mbstr_copy_ascii(mpd_mbstr_t *dest, const char *src, mpd_ssize_t n)
  764. {
  765. dest->nbytes += n;
  766. dest->nchars += n;
  767. dest->cur -= n;
  768. if (dest->data != NULL) {
  769. _mpd_bcopy(dest->data+dest->cur, src, n);
  770. }
  771. }
  772. static inline void
  773. _mbstr_copy_pad(mpd_mbstr_t *dest, mpd_ssize_t n)
  774. {
  775. dest->nbytes += n;
  776. dest->nchars += n;
  777. dest->cur -= n;
  778. if (dest->data != NULL) {
  779. char*cp = dest->data + dest->cur;
  780. while (--n >= 0) {
  781. cp[n] = '0';
  782. }
  783. }
  784. }
  785. /*
  786. * Copy the decimal to dest, adding separators according to
  787. * spec->grouping. If leading zero padding is enabled and the
  788. * result is smaller than spec->min_width, continue adding zeros
  789. * and separators until min_width is reached.
  790. */
  791. static void
  792. _mpd_add_sep_dot(mpd_mbstr_t *dest,
  793. const char *src, mpd_ssize_t n_src, /* integer part and length */
  794. const char *sign, const char *dot, const char *rest,
  795. mpd_spec_t *spec)
  796. {
  797. mpd_ssize_t n_sep, n_sign, consume;
  798. const char *g;
  799. int pad = 0;
  800. n_sign = sign ? 1 : 0;
  801. n_sep = (mpd_ssize_t)strlen(spec->sep);
  802. g = spec->grouping;
  803. dest->cur = dest->nbytes;
  804. dest->nbytes = dest->nchars = 0;
  805. /* rest <= MPD_MAX_PREC */
  806. _mbstr_copy_ascii(dest, rest, (mpd_ssize_t)strlen(rest));
  807. if (dot) {
  808. _mbstr_copy_char(dest, dot, (mpd_ssize_t)strlen(dot));
  809. }
  810. consume = *g;
  811. while (1) {
  812. if (*g == 0 || *g == CHAR_MAX || consume > n_src) {
  813. consume = n_src;
  814. }
  815. n_src -= consume;
  816. if (pad) {
  817. _mbstr_copy_pad(dest, consume);
  818. }
  819. else {
  820. _mbstr_copy_ascii(dest, src+n_src, consume);
  821. }
  822. if (n_src == 0) {
  823. if (spec->align == 'z' &&
  824. dest->nchars + n_sign < spec->min_width) {
  825. n_src = spec->min_width - (dest->nchars + n_sign);
  826. consume = *g - consume;
  827. pad = 1;
  828. continue;
  829. }
  830. break;
  831. }
  832. if (n_sep > 0) {
  833. if (pad && n_src > 1) n_src -= 1;
  834. _mbstr_copy_char(dest, spec->sep, n_sep);
  835. }
  836. if (*g && *(g+1)) g++;
  837. consume = *g;
  838. }
  839. if (sign) {
  840. _mbstr_copy_ascii(dest, sign, 1);
  841. }
  842. if (dest->data) {
  843. dest->data[dest->nbytes] = '\0';
  844. }
  845. }
  846. /* Change decstring to locale-specific appearance. */
  847. static void
  848. _mpd_apply_lconv(mpd_mbstr_t *result, char *decstring, mpd_spec_t *spec,
  849. uint32_t *status)
  850. {
  851. const char *sign = NULL, *intpart = NULL;
  852. const char *dot = NULL, *rest = NULL;
  853. const char *dp;
  854. mpd_ssize_t n_int;
  855. assert(result->data == NULL);
  856. dp = decstring;
  857. n_int = 0;
  858. if (!isdigit((uchar)*dp)) {
  859. sign = dp++;
  860. }
  861. if (isdigit((uchar)*dp)) {
  862. intpart = dp++;
  863. while (isdigit((uchar)*dp)) {
  864. dp++;
  865. }
  866. n_int = (mpd_ssize_t)(dp-intpart);
  867. }
  868. if (*dp == '.') {
  869. if (*spec->dot == '\0') {
  870. /* decimal point must be present */
  871. *status |= MPD_Invalid_operation; /* GCOV_NOT_REACHED */
  872. mpd_free(decstring); /* GCOV_NOT_REACHED */
  873. return; /* GCOV_NOT_REACHED */
  874. }
  875. dp++; dot = spec->dot;
  876. }
  877. rest = dp;
  878. if (!dot && !(intpart && *spec->sep && *spec->grouping)) {
  879. result->data = decstring;
  880. result->nbytes = result->nchars = (mpd_ssize_t)strlen(decstring);
  881. return;
  882. }
  883. /* Get the size of the new decimal string after inserting dot and
  884. * separators. */
  885. _mpd_add_sep_dot(result, intpart, n_int, sign, dot, rest, spec);
  886. if ((result->data = mpd_alloc(result->nbytes+1, 1)) == NULL) {
  887. *status |= MPD_Malloc_error;
  888. mpd_free(decstring);
  889. return;
  890. }
  891. /* Perform actual writes. */
  892. _mpd_add_sep_dot(result, intpart, n_int, sign, dot, rest, spec);
  893. mpd_free(decstring);
  894. }
  895. /* Add padding to the formatted string if necessary. */
  896. static void
  897. _mpd_add_pad(mpd_mbstr_t *result, mpd_spec_t *spec, uint32_t *status)
  898. {
  899. if (result->nchars < spec->min_width) {
  900. mpd_ssize_t add_chars, add_bytes;
  901. size_t lpad = 0, rpad = 0;
  902. size_t n_fill, len, i, j;
  903. uint8_t err = 0;
  904. char *cp;
  905. n_fill = strlen(spec->fill);
  906. add_chars = (spec->min_width - result->nchars);
  907. /* max value: MPD_MAX_PREC * 4 */
  908. add_bytes = add_chars * (mpd_ssize_t)n_fill;
  909. cp = result->data = mpd_realloc(result->data,
  910. result->nbytes+add_bytes+1,
  911. sizeof *result->data, &err);
  912. if (err) {
  913. *status |= MPD_Malloc_error;
  914. mpd_free(result->data);
  915. result->data = NULL;
  916. return;
  917. }
  918. if (spec->align == 'z') {
  919. spec->align = '=';
  920. }
  921. if (spec->align == '<') {
  922. rpad = add_chars;
  923. }
  924. else if (spec->align == '>' || spec->align == '=') {
  925. lpad = add_chars;
  926. }
  927. else { /* align == '^' */
  928. lpad = add_chars/2;
  929. rpad = add_chars-lpad;
  930. }
  931. len = result->nbytes;
  932. if (spec->align == '=' && (*cp == '-' || *cp == '+' ||
  933. *cp == ' ')) {
  934. /* leave sign in the leading position */
  935. cp++; len--;
  936. }
  937. memmove(cp+n_fill*lpad, cp, len);
  938. for (i = 0; i < lpad; i++) {
  939. for (j = 0; j < n_fill; j++) {
  940. cp[i*n_fill+j] = spec->fill[j];
  941. }
  942. }
  943. cp += (n_fill*lpad + len);
  944. for (i = 0; i < rpad; i++) {
  945. for (j = 0; j < n_fill; j++) {
  946. cp[i*n_fill+j] = spec->fill[j];
  947. }
  948. }
  949. result->nbytes += add_bytes;
  950. result->nchars += add_chars;
  951. result->data[result->nbytes] = '\0';
  952. }
  953. }
  954. /*
  955. * Return the string representation of an mpd_t, formatted according to 'spec'.
  956. * 'spec' is modified. The format specification is assumed to be valid. Memory
  957. * errors are indicated as usual. This function is quiet.
  958. */
  959. char *
  960. mpd_qformat_spec(const mpd_t *dec, mpd_spec_t *spec, const mpd_context_t *ctx,
  961. uint32_t *status)
  962. {
  963. mpd_context_t workctx;
  964. mpd_uint_t dt[MPD_MINALLOC_MAX];
  965. mpd_t tmp = {MPD_STATIC|MPD_STATIC_DATA,0,0,0,MPD_MINALLOC_MAX,dt};
  966. mpd_ssize_t prec, fracdigits, exp;
  967. mpd_ssize_t zeroexp = MPD_SSIZE_MAX;
  968. mpd_mbstr_t result;
  969. char *decstring;
  970. int flags = 0;
  971. if (spec->min_width > MPD_MAX_PREC) {
  972. *status |= MPD_Invalid_operation;
  973. return NULL;
  974. }
  975. if (!mpd_qcopy(&tmp, dec, status)) {
  976. return NULL;
  977. }
  978. if (spec->type == '%') {
  979. tmp.exp += 2;
  980. spec->type = 'f';
  981. flags |= MPD_FMT_PERCENT;
  982. }
  983. if (isupper((uchar)spec->type)) {
  984. spec->type = tolower((uchar)spec->type);
  985. flags |= MPD_FMT_UPPER;
  986. }
  987. if (spec->sign == ' ') {
  988. flags |= MPD_FMT_SIGN_SPACE;
  989. }
  990. else if (spec->sign == '+') {
  991. flags |= MPD_FMT_SIGN_PLUS;
  992. }
  993. mpd_maxcontext_plus(&workctx, ctx);
  994. workctx.round = ctx->round;
  995. if (mpd_isspecial(&tmp)) {
  996. /* no percent formatting */
  997. flags |= MPD_FMT_TOSCI;
  998. if (spec->align == 'z') {
  999. spec->fill[0] = ' ';
  1000. spec->fill[1] = '\0';
  1001. spec->align = '>';
  1002. }
  1003. }
  1004. else if (spec->type == 'g') {
  1005. /* spec->prec: significant digits */
  1006. prec = (spec->prec < 0) ? dec->digits : spec->prec;
  1007. workctx.prec = (prec == 0) ? 1 : prec;
  1008. flags |= MPD_FMT_TOSCI;
  1009. mpd_qfinalize(&tmp, &workctx, &workctx.status);
  1010. }
  1011. else {
  1012. /* spec->prec: fraction digits */
  1013. if (spec->type == 'e') {
  1014. fracdigits = (spec->prec < 0) ? tmp.digits-1 : spec->prec;
  1015. if (mpd_iszero(&tmp)) {
  1016. zeroexp = tmp.exp+fracdigits;
  1017. exp = -fracdigits;
  1018. flags |= MPD_FMT_FIXED;
  1019. }
  1020. else {
  1021. exp = tmp.exp + tmp.digits-(fracdigits+1);
  1022. flags |= MPD_FMT_EXP;
  1023. }
  1024. }
  1025. else { /* 'f' */
  1026. fracdigits = spec->prec;
  1027. if (fracdigits < 0) {
  1028. fracdigits = (tmp.exp < 0) ? -tmp.exp : 0;
  1029. }
  1030. exp = -fracdigits;
  1031. fracdigits += (tmp.exp+tmp.digits);
  1032. fracdigits = (fracdigits < 0) ? 0 : fracdigits;
  1033. flags |= MPD_FMT_FIXED;
  1034. }
  1035. workctx.prec = fracdigits+1;
  1036. mpd_qrescale(&tmp, &tmp, exp, &workctx, &workctx.status);
  1037. if (tmp.digits > workctx.prec) {
  1038. mpd_qfinalize(&tmp, &workctx, &workctx.status);
  1039. }
  1040. }
  1041. if ((decstring = _mpd_to_string(&tmp, flags, zeroexp)) == NULL) {
  1042. *status |= MPD_Malloc_error;
  1043. mpd_del(&tmp);
  1044. return NULL;
  1045. }
  1046. result.data = decstring;
  1047. result.nbytes = result.nchars = 0;
  1048. if (!mpd_isspecial(&tmp) && *spec->dot != '\0') {
  1049. result.data = NULL;
  1050. _mpd_apply_lconv(&result, decstring, spec, status);
  1051. if (result.data == NULL) {
  1052. goto finish;
  1053. }
  1054. }
  1055. if (spec->min_width) {
  1056. if (result.nbytes == 0) {
  1057. result.nbytes = result.nchars =
  1058. (mpd_ssize_t)strlen(result.data);
  1059. }
  1060. _mpd_add_pad(&result, spec, status);
  1061. }
  1062. finish:
  1063. mpd_del(&tmp);
  1064. return result.data;
  1065. }
  1066. char *
  1067. mpd_qformat(const mpd_t *dec, const char *fmt, const mpd_context_t *ctx,
  1068. uint32_t *status)
  1069. {
  1070. mpd_spec_t spec;
  1071. if (!mpd_parse_fmt_str(&spec, fmt, 1)) {
  1072. *status |= MPD_Invalid_operation;
  1073. return NULL;
  1074. }
  1075. return mpd_qformat_spec(dec, &spec, ctx, status);
  1076. }
  1077. /*
  1078. * The specification has a *condition* called Invalid_operation and an
  1079. * IEEE *signal* called Invalid_operation. The former corresponds to
  1080. * MPD_Invalid_operation, the latter to MPD_IEEE_Invalid_operation.
  1081. * MPD_IEEE_Invalid_operation comprises the following conditions:
  1082. *
  1083. * [MPD_Conversion_syntax, MPD_Division_impossible, MPD_Division_undefined,
  1084. * MPD_Fpu_error, MPD_Invalid_context, MPD_Invalid_operation,
  1085. * MPD_Malloc_error]
  1086. *
  1087. * In the following functions, 'flag' denotes the condition, 'signal'
  1088. * denotes the IEEE signal.
  1089. */
  1090. static const char *mpd_flag_string[MPD_NUM_FLAGS] = {
  1091. "Clamped",
  1092. "Conversion_syntax",
  1093. "Division_by_zero",
  1094. "Division_impossible",
  1095. "Division_undefined",
  1096. "Fpu_error",
  1097. "Inexact",
  1098. "Invalid_context",
  1099. "Invalid_operation",
  1100. "Malloc_error",
  1101. "Not_implemented",
  1102. "Overflow",
  1103. "Rounded",
  1104. "Subnormal",
  1105. "Underflow",
  1106. };
  1107. static const char *mpd_signal_string[MPD_NUM_FLAGS] = {
  1108. "Clamped",
  1109. "IEEE_Invalid_operation",
  1110. "Division_by_zero",
  1111. "IEEE_Invalid_operation",
  1112. "IEEE_Invalid_operation",
  1113. "IEEE_Invalid_operation",
  1114. "Inexact",
  1115. "IEEE_Invalid_operation",
  1116. "IEEE_Invalid_operation",
  1117. "IEEE_Invalid_operation",
  1118. "Not_implemented",
  1119. "Overflow",
  1120. "Rounded",
  1121. "Subnormal",
  1122. "Underflow",
  1123. };
  1124. /* print conditions to buffer, separated by spaces */
  1125. int
  1126. mpd_snprint_flags(char *dest, int nmemb, uint32_t flags)
  1127. {
  1128. char *cp;
  1129. int n, j;
  1130. assert(nmemb >= MPD_MAX_FLAG_STRING);
  1131. *dest = '\0'; cp = dest;
  1132. for (j = 0; j < MPD_NUM_FLAGS; j++) {
  1133. if (flags & (1U<<j)) {
  1134. n = snprintf(cp, nmemb, "%s ", mpd_flag_string[j]);
  1135. if (n < 0 || n >= nmemb) return -1;
  1136. cp += n; nmemb -= n;
  1137. }
  1138. }
  1139. if (cp != dest) {
  1140. *(--cp) = '\0';
  1141. }
  1142. return (int)(cp-dest);
  1143. }
  1144. /* print conditions to buffer, in list form */
  1145. int
  1146. mpd_lsnprint_flags(char *dest, int nmemb, uint32_t flags, const char *flag_string[])
  1147. {
  1148. char *cp;
  1149. int n, j;
  1150. assert(nmemb >= MPD_MAX_FLAG_LIST);
  1151. if (flag_string == NULL) {
  1152. flag_string = mpd_flag_string;
  1153. }
  1154. *dest = '[';
  1155. *(dest+1) = '\0';
  1156. cp = dest+1;
  1157. --nmemb;
  1158. for (j = 0; j < MPD_NUM_FLAGS; j++) {
  1159. if (flags & (1U<<j)) {
  1160. n = snprintf(cp, nmemb, "%s, ", flag_string[j]);
  1161. if (n < 0 || n >= nmemb) return -1;
  1162. cp += n; nmemb -= n;
  1163. }
  1164. }
  1165. /* erase the last ", " */
  1166. if (cp != dest+1) {
  1167. cp -= 2;
  1168. }
  1169. *cp++ = ']';
  1170. *cp = '\0';
  1171. return (int)(cp-dest); /* strlen, without NUL terminator */
  1172. }
  1173. /* print signals to buffer, in list form */
  1174. int
  1175. mpd_lsnprint_signals(char *dest, int nmemb, uint32_t flags, const char *signal_string[])
  1176. {
  1177. char *cp;
  1178. int n, j;
  1179. int ieee_invalid_done = 0;
  1180. assert(nmemb >= MPD_MAX_SIGNAL_LIST);
  1181. if (signal_string == NULL) {
  1182. signal_string = mpd_signal_string;
  1183. }
  1184. *dest = '[';
  1185. *(dest+1) = '\0';
  1186. cp = dest+1;
  1187. --nmemb;
  1188. for (j = 0; j < MPD_NUM_FLAGS; j++) {
  1189. uint32_t f = flags & (1U<<j);
  1190. if (f) {
  1191. if (f&MPD_IEEE_Invalid_operation) {
  1192. if (ieee_invalid_done) {
  1193. continue;
  1194. }
  1195. ieee_invalid_done = 1;
  1196. }
  1197. n = snprintf(cp, nmemb, "%s, ", signal_string[j]);
  1198. if (n < 0 || n >= nmemb) return -1;
  1199. cp += n; nmemb -= n;
  1200. }
  1201. }
  1202. /* erase the last ", " */
  1203. if (cp != dest+1) {
  1204. cp -= 2;
  1205. }
  1206. *cp++ = ']';
  1207. *cp = '\0';
  1208. return (int)(cp-dest); /* strlen, without NUL terminator */
  1209. }
  1210. /* The following two functions are mainly intended for debugging. */
  1211. void
  1212. mpd_fprint(FILE *file, const mpd_t *dec)
  1213. {
  1214. char *decstring;
  1215. decstring = mpd_to_sci(dec, 1);
  1216. if (decstring != NULL) {
  1217. fprintf(file, "%s\n", decstring);
  1218. mpd_free(decstring);
  1219. }
  1220. else {
  1221. fputs("mpd_fprint: output error\n", file); /* GCOV_NOT_REACHED */
  1222. }
  1223. }
  1224. void
  1225. mpd_print(const mpd_t *dec)
  1226. {
  1227. char *decstring;
  1228. decstring = mpd_to_sci(dec, 1);
  1229. if (decstring != NULL) {
  1230. printf("%s\n", decstring);
  1231. mpd_free(decstring);
  1232. }
  1233. else {
  1234. fputs("mpd_fprint: output error\n", stderr); /* GCOV_NOT_REACHED */
  1235. }
  1236. }