mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-03-26 22:49:01 +08:00
[c] Replaced call to locale dependent strtof with customer float parser. Closes #1009
This commit is contained in:
parent
4d7d448197
commit
377aee58ec
@ -92,29 +92,65 @@ void Json_dispose (Json *c) {
|
|||||||
|
|
||||||
/* Parse the input text to generate a number, and populate the result into item. */
|
/* Parse the input text to generate a number, and populate the result into item. */
|
||||||
static const char* parse_number (Json *item, const char* num) {
|
static const char* parse_number (Json *item, const char* num) {
|
||||||
char * endptr;
|
double result = 0.0;
|
||||||
float n;
|
int negative = 0;
|
||||||
|
char* ptr = (char*)num;
|
||||||
|
|
||||||
/* Using strtod and strtof is slightly more permissive than RFC4627,
|
if (*ptr == '-') {
|
||||||
* accepting for example hex-encoded floating point, but either
|
negative = -1;
|
||||||
* is often leagues faster than any manual implementation.
|
++ptr;
|
||||||
*
|
}
|
||||||
* We also already know that this starts with [-0-9] from parse_value.
|
|
||||||
*/
|
|
||||||
#if __STDC_VERSION__ >= 199901L
|
|
||||||
n = strtof(num, &endptr);
|
|
||||||
#else
|
|
||||||
n = (float)strtod( num, &endptr );
|
|
||||||
#endif
|
|
||||||
/* ignore errno's ERANGE, which returns +/-HUGE_VAL */
|
|
||||||
/* n is 0 on any other error */
|
|
||||||
|
|
||||||
if (endptr != num) {
|
while (*ptr >= '0' && *ptr <= '9') {
|
||||||
|
result = result * 10.0 + (*ptr - '0');
|
||||||
|
++ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*ptr == '.') {
|
||||||
|
double fraction = 0.0;
|
||||||
|
int n = 0;
|
||||||
|
++ptr;
|
||||||
|
|
||||||
|
while (*ptr >= '0' && *ptr <= '9') {
|
||||||
|
fraction = (fraction * 10.0) + (*ptr - '0');
|
||||||
|
++ptr;
|
||||||
|
++n;
|
||||||
|
}
|
||||||
|
result += fraction / POW(10.0, n);
|
||||||
|
}
|
||||||
|
if (negative) result = -result;
|
||||||
|
|
||||||
|
if (*ptr == 'e' || *ptr == 'E') {
|
||||||
|
double exponent = 0;
|
||||||
|
int expNegative = 0;
|
||||||
|
int n = 0;
|
||||||
|
++ptr;
|
||||||
|
|
||||||
|
if (*ptr == '-') {
|
||||||
|
expNegative = -1;
|
||||||
|
++ptr;
|
||||||
|
} else if (*ptr == '+') {
|
||||||
|
++ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (*ptr >= '0' && *ptr <= '9') {
|
||||||
|
exponent = (exponent * 10.0) + (*ptr - '0');
|
||||||
|
++ptr;
|
||||||
|
++n;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (expNegative)
|
||||||
|
result = result / POW(10, exponent);
|
||||||
|
else
|
||||||
|
result = result * POW(10, exponent);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ptr != num) {
|
||||||
/* Parse success, number found. */
|
/* Parse success, number found. */
|
||||||
item->valueFloat = n;
|
item->valueFloat = result;
|
||||||
item->valueInt = (int)n;
|
item->valueInt = (int)result;
|
||||||
item->type = Json_Number;
|
item->type = Json_Number;
|
||||||
return endptr;
|
return ptr;
|
||||||
} else {
|
} else {
|
||||||
/* Parse failure, ep is set. */
|
/* Parse failure, ep is set. */
|
||||||
ep = num;
|
ep = num;
|
||||||
|
|||||||
@ -30,7 +30,6 @@
|
|||||||
|
|
||||||
#include <spine/SkeletonJson.h>
|
#include <spine/SkeletonJson.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <locale.h>
|
|
||||||
#include "Json.h"
|
#include "Json.h"
|
||||||
#include <spine/extension.h>
|
#include <spine/extension.h>
|
||||||
#include <spine/AtlasAttachmentLoader.h>
|
#include <spine/AtlasAttachmentLoader.h>
|
||||||
@ -579,25 +578,14 @@ spSkeletonData* spSkeletonJson_readSkeletonData (spSkeletonJson* self, const cha
|
|||||||
int i, ii;
|
int i, ii;
|
||||||
spSkeletonData* skeletonData;
|
spSkeletonData* skeletonData;
|
||||||
Json *root, *skeleton, *bones, *boneMap, *ik, *transform, *path, *slots, *skins, *animations, *events;
|
Json *root, *skeleton, *bones, *boneMap, *ik, *transform, *path, *slots, *skins, *animations, *events;
|
||||||
char* oldLocale;
|
|
||||||
_spSkeletonJson* internal = SUB_CAST(_spSkeletonJson, self);
|
_spSkeletonJson* internal = SUB_CAST(_spSkeletonJson, self);
|
||||||
|
|
||||||
FREE(self->error);
|
FREE(self->error);
|
||||||
CONST_CAST(char*, self->error) = 0;
|
CONST_CAST(char*, self->error) = 0;
|
||||||
internal->linkedMeshCount = 0;
|
internal->linkedMeshCount = 0;
|
||||||
|
|
||||||
#ifndef __ANDROID__
|
|
||||||
oldLocale = strdup(setlocale(LC_NUMERIC, NULL));
|
|
||||||
setlocale(LC_NUMERIC, "C");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
root = Json_create(json);
|
root = Json_create(json);
|
||||||
|
|
||||||
#ifndef __ANDROID__
|
|
||||||
setlocale(LC_NUMERIC, oldLocale);
|
|
||||||
free(oldLocale);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (!root) {
|
if (!root) {
|
||||||
_spSkeletonJson_setError(self, 0, "Invalid skeleton JSON: ", Json_getError());
|
_spSkeletonJson_setError(self, 0, "Invalid skeleton JSON: ", Json_getError());
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user