Commit b7c802f1 authored by Nikias Bassen's avatar Nikias Bassen

xplist: Increase precision when converting PLIST_REAL nodes to XML

parent 072a31d6
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#include <time.h> #include <time.h>
#include <inttypes.h> #include <inttypes.h>
#include <float.h>
#include <math.h> #include <math.h>
#include <limits.h> #include <limits.h>
...@@ -102,46 +103,24 @@ void plist_xml_deinit(void) ...@@ -102,46 +103,24 @@ void plist_xml_deinit(void)
static size_t dtostr(char *buf, size_t bufsize, double realval) static size_t dtostr(char *buf, size_t bufsize, double realval)
{ {
double f = realval; size_t len = 0;
double ip = 0.0; if (isnan(realval)) {
int64_t v; len = snprintf(buf, bufsize, "nan");
size_t len; } else if (isinf(realval)) {
size_t p; len = snprintf(buf, bufsize, "%cinfinity", (realval > 0.0) ? '+' : '-');
double CORR = 0.0000005; } else if (realval == 0.0f) {
len = snprintf(buf, bufsize, "0.0");
f = modf(f, &ip);
v = (int64_t)ip;
if (f < 0) {
if (((int)((f - CORR) * -10.0f)) >= 10) {
v--;
f = 0;
}
} else { } else {
if (((int)((f + CORR) * 10.0f)) >= 10) { size_t i = 0;
v++; len = snprintf(buf, bufsize, "%.*g", 17, realval);
f = 0; for (i = 0; i < len; i++) {
if (buf[i] == ',') {
buf[i] = '.';
break;
}
} }
} }
len = snprintf(buf, bufsize, "%s%"PRIi64, ((f < 0) && (ip >= 0)) ? "-" : "", v); return len;
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;
} }
static void node_to_xml(node_t* node, bytearray_t **outbuf, uint32_t depth) static void node_to_xml(node_t* node, bytearray_t **outbuf, uint32_t depth)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment