diff options
author | Nikias Bassen | 2019-11-07 01:52:15 +0100 |
---|---|---|
committer | Nikias Bassen | 2019-11-07 01:52:15 +0100 |
commit | b7c802f1b7cdf8a271b148d4375c4309aefc5e0d (patch) | |
tree | cead322dc4f9ea7d5860c9d592ed16ccf9507955 /src | |
parent | 072a31d6fdcc03b1eebb1d5ea1ebe4be36597253 (diff) | |
download | libplist-b7c802f1b7cdf8a271b148d4375c4309aefc5e0d.tar.gz libplist-b7c802f1b7cdf8a271b148d4375c4309aefc5e0d.tar.bz2 |
xplist: Increase precision when converting PLIST_REAL nodes to XML
Diffstat (limited to 'src')
-rw-r--r-- | src/xplist.c | 53 |
1 files 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 <time.h> #include <inttypes.h> +#include <float.h> #include <math.h> #include <limits.h> @@ -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) |