]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/jansson/strconv.c
7 #undef __NO_ISOCEXT /* ensure stdlib.h will declare prototypes for mingw own 'strtod' replacement, called '__strtod' */
9 #include "jansson_private.h"
10 #include "strbuffer.h"
12 /* need jansson_private_config.h to get the correct snprintf */
14 #include <jansson_private_config.h>
18 #define strtod __strtod
21 #if JSON_HAVE_LOCALECONV
25 - This code assumes that the decimal separator is exactly one
28 - If setlocale() is called by another thread between the call to
29 localeconv() and the call to sprintf() or strtod(), the result may
30 be wrong. setlocale() is not thread-safe and should not be used
31 this way. Multi-threaded programs should use uselocale() instead.
34 static void to_locale(strbuffer_t
*strbuffer
)
39 point
= localeconv()->decimal_point
;
41 /* No conversion needed */
45 pos
= strchr(strbuffer
->value
, '.');
50 static void from_locale(char *buffer
)
55 point
= localeconv()->decimal_point
;
57 /* No conversion needed */
61 pos
= strchr(buffer
, *point
);
67 int jsonp_strtod(strbuffer_t
*strbuffer
, double *out
)
72 #if JSON_HAVE_LOCALECONV
77 value
= strtod(strbuffer
->value
, &end
);
78 assert(end
== strbuffer
->value
+ strbuffer
->length
);
80 if((value
== HUGE_VAL
|| value
== -HUGE_VAL
) && errno
== ERANGE
) {
89 int jsonp_dtostr(char *buffer
, size_t size
, double value
, int precision
)
98 ret
= snprintf(buffer
, size
, "%.*g", precision
, value
);
102 length
= (size_t)ret
;
106 #if JSON_HAVE_LOCALECONV
110 /* Make sure there's a dot or 'e' in the output. Otherwise
111 a real is converted to an integer when decoding */
112 if(strchr(buffer
, '.') == NULL
&&
113 strchr(buffer
, 'e') == NULL
)
115 if(length
+ 3 >= size
) {
116 /* No space to append ".0" */
119 buffer
[length
] = '.';
120 buffer
[length
+ 1] = '0';
121 buffer
[length
+ 2] = '\0';
125 /* Remove leading '+' from positive exponent. Also remove leading
126 zeros from exponents (added by some printf() implementations) */
127 start
= strchr(buffer
, 'e');
139 memmove(start
, end
, length
- (size_t)(end
- buffer
));
140 length
-= (size_t)(end
- start
);