From b7c802f1b7cdf8a271b148d4375c4309aefc5e0d Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Thu, 7 Nov 2019 01:52:15 +0100 Subject: xplist: Increase precision when converting PLIST_REAL nodes to XML --- src/xplist.c | 53 ++++++++++++++++------------------------------------- 1 file changed, 16 insertions(+), 37 deletions(-) diff --git a/src/xplist.c b/src/xplist.c index fc8fd61..0cf4663 100644 --- a/src/xplist.c +++ b/src/xplist.c @@ -36,6 +36,7 @@ #include #include +#include #include #include @@ -102,46 +103,24 @@ void plist_xml_deinit(void) static size_t dtostr(char *buf, size_t bufsize, double realval) { - double f = realval; - double ip = 0.0; - int64_t v; - size_t len; - size_t p; - double CORR = 0.0000005; - - f = modf(f, &ip); - v = (int64_t)ip; - if (f < 0) { - if (((int)((f - CORR) * -10.0f)) >= 10) { - v--; - f = 0; - } + size_t len = 0; + if (isnan(realval)) { + len = snprintf(buf, bufsize, "nan"); + } else if (isinf(realval)) { + len = snprintf(buf, bufsize, "%cinfinity", (realval > 0.0) ? '+' : '-'); + } else if (realval == 0.0f) { + len = snprintf(buf, bufsize, "0.0"); } else { - if (((int)((f + CORR) * 10.0f)) >= 10) { - v++; - f = 0; + size_t i = 0; + len = snprintf(buf, bufsize, "%.*g", 17, realval); + for (i = 0; i < len; i++) { + if (buf[i] == ',') { + buf[i] = '.'; + break; + } } } - len = snprintf(buf, bufsize, "%s%"PRIi64, ((f < 0) && (ip >= 0)) ? "-" : "", v); - if (len >= bufsize) { - return 0; - } - - if (f < 0) { - f *= -1; - } - f += CORR; - - p = len; - buf[p++] = '.'; - - while (p < bufsize && (p <= len+6)) { - f = modf(f*10, &ip); - v = (int)ip; - buf[p++] = (v + 0x30); - } - buf[p] = '\0'; - return p; + return len; } static void node_to_xml(node_t* node, bytearray_t **outbuf, uint32_t depth) -- cgit v1.1-32-gdbae