diff options
author | Nikias Bassen | 2015-11-13 03:19:00 +0100 |
---|---|---|
committer | Nikias Bassen | 2015-11-13 03:19:00 +0100 |
commit | 9834d85ca473ef9b229dcf4c0df758e18a4149ad (patch) | |
tree | 1846b0867cc56321d071a642e70b6b5269e5ed90 | |
parent | 9ca25d293fe7f8aca8d952fc7bb91464fe2d34ab (diff) | |
download | libplist-9834d85ca473ef9b229dcf4c0df758e18a4149ad.tar.gz libplist-9834d85ca473ef9b229dcf4c0df758e18a4149ad.tar.bz2 |
xplist: Get rid of setlocale() and use custom function to print floating point values
-rw-r--r-- | src/xplist.c | 46 |
1 files changed, 32 insertions, 14 deletions
diff --git a/src/xplist.c b/src/xplist.c index 1ee0138..09c1a11 100644 --- a/src/xplist.c +++ b/src/xplist.c @@ -29,7 +29,7 @@ #include <time.h> #include <inttypes.h> -#include <locale.h> +#include <math.h> #include <libxml/xmlIO.h> #include <libxml/parser.h> @@ -147,6 +147,36 @@ static struct node_t* new_uint_node(uint64_t value) return node_create(NULL, data); } +static void dtostr(char *buf, size_t bufsize, double realval) +{ + double f = realval; + double ip = 0.0; + int64_t v; + size_t len; + size_t p; + + f = modf(f, &ip); + len = snprintf(buf, bufsize, "%s%"PRIi64, ((f < 0) && (ip >= 0)) ? "-" : "", (int64_t)ip); + if (len >= bufsize) { + return; + } + + if (f < 0) { + f *= -1; + } + f += 0.0000004; + + 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'; +} + static void node_to_xml(node_t* node, void *xml_struct) { struct xml_node *xstruct = NULL; @@ -194,7 +224,7 @@ static void node_to_xml(node_t* node, void *xml_struct) case PLIST_REAL: tag = XPLIST_REAL; val = (char*)malloc(64); - (void)snprintf(val, 64, "%f", node_data->realval); + dtostr(val, 64, node_data->realval); break; case PLIST_STRING: @@ -529,14 +559,6 @@ PLIST_API void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length) root_node = xmlDocGetRootElement(plist_doc); root.xml = root_node; - char *current_locale = setlocale(LC_NUMERIC, NULL); - char *saved_locale = NULL; - if (current_locale) { - saved_locale = strdup(current_locale); - } - if (saved_locale) { - setlocale(LC_NUMERIC, "POSIX"); - } node_to_xml(plist, &root); xmlChar* tmp = NULL; @@ -552,10 +574,6 @@ PLIST_API void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length) } xmlFreeDoc(plist_doc); - if (saved_locale) { - setlocale(LC_NUMERIC, saved_locale); - free(saved_locale); - } /* free memory from parser initialization */ xmlCleanupCharEncodingHandlers(); |