85 int valueint = (int)d;
86 if (fabs(((
double)valueint)-d)<=
DBL_EPSILON && d<=INT_MAX && d>=INT_MIN)
97 if (fabs(floor(d)-d)<=
DBL_EPSILON && fabs(d)<1.0e60)
99 else if (fabs(d)<1.0e-6 || fabs(d)>1.0e9)
109 static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
112 const char*
AssignError(
const char** perror,
const char *errorMessage)
115 *perror = errorMessage;
123 : Type(itemType), dValue(0.0)
143 const char* num_start = num;
144 double n=0, sign=1, scale=0;
154 if (*num>=
'1' && *num<=
'9')
158 n=(n*10.0)+(*num++ -
'0');
160 while (*num>=
'0' && *num<=
'9');
163 if (*num==
'.' && num[1]>=
'0' && num[1]<=
'9')
168 n=(n*10.0)+(*num++ -
'0');
171 while (*num>=
'0' && *num<=
'9');
174 if (*num==
'e' || *num==
'E')
185 while (*num>=
'0' && *num<=
'9')
186 subscale=(subscale*10)+(*num++ -
'0');
190 n = sign*n*pow(10.0,(scale+subscale*signsubscale));
202 const char*
ParseHex(
unsigned* val,
unsigned digits,
const char* str)
206 for(
unsigned digitCount = 0; digitCount < digits; digitCount++, str++)
210 if ((v >=
'0') && (v <=
'9'))
212 else if ((v >=
'a') && (v <=
'f'))
214 else if ((v >=
'A') && (v <=
'F'))
219 *val = *val * 16 + v;
230 const char* ptr = str+1;
239 return AssignError(perror,
"Syntax Error: Missing quote");
242 while (*ptr!=
'\"' && *ptr && ++len)
244 if (*ptr++ ==
'\\') ptr++;
255 while (*ptr!=
'\"' && *ptr)
266 case 'b': *ptr2++ =
'\b';
break;
267 case 'f': *ptr2++ =
'\f';
break;
268 case 'n': *ptr2++ =
'\n';
break;
269 case 'r': *ptr2++ =
'\r';
break;
270 case 't': *ptr2++ =
'\t';
break;
280 if ((uc>=0xDC00 && uc<=0xDFFF) || uc==0)
284 if (uc>=0xD800 && uc<=0xDBFF)
286 if (ptr[1]!=
'\\' || ptr[2]!=
'u')
293 if (uc2<0xDC00 || uc2>0xDFFF)
296 uc = 0x10000 + (((uc&0x3FF)<<10) | (uc2&0x3FF));
312 case 4: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
313 case 3: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
314 case 2: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
354 while (token && ++len)\
356 if (
strchr(
"\"\\\b\f\n\r\t",token))
364 int buff_size = len+3;
375 if ((
unsigned char)*ptr>31 && *ptr!=
'\"' && *ptr!=
'\\')
380 switch (token=*ptr++)
382 case '\\': *ptr2++=
'\\';
break;
383 case '\"': *ptr2++=
'\"';
break;
384 case '\b': *ptr2++=
'b';
break;
385 case '\f': *ptr2++=
'f';
break;
386 case '\n': *ptr2++=
'n';
break;
387 case '\r': *ptr2++=
'r';
break;
388 case '\t': *ptr2++=
't';
break;
390 OVR_sprintf(ptr2, buff_size - (ptr2-out),
"u%04x",token);
403 static const char*
skip(
const char* in)
405 while (in && *in && (
unsigned char)*in<=
' ')
420 AssignError(perror,
"Error: Failed to allocate memory");
440 char *termStr =
new char[len + 1];
441 memcpy(termStr, buff, len);
484 if (*buff==
'-' || (*buff>=
'0' && *buff<=
'9'))
497 return AssignError(perror,
"Syntax Error: Invalid syntax");
533 return AssignError(perror,
"Syntax Error: Missing opening bracket");
555 return AssignError(perror,
"Error: Failed to allocate memory");
561 return AssignError(perror,
"Error: Failed to allocate memory");
567 return AssignError(perror,
"Syntax Error: Missing ending bracket");
575 char * out = 0,*ptr,*ret;
590 entries=(
char**)
OVR_ALLOC(numentries*
sizeof(
char*));
593 memset(entries,0,numentries*
sizeof(
char*));
597 for (
int i=0; i<numentries; i++)
622 for (
int i=0; i<numentries; i++)
635 for (
int i=0; i<numentries; i++)
661 return AssignError(perror,
"Syntax Error: Missing opening brace");
680 return AssignError(perror,
"Syntax Error: Missing colon");
704 return AssignError(perror,
"Syntax Error: Missing colon");
716 return AssignError(perror,
"Syntax Error: Missing closing brace");
723 char** entries = 0, **names = 0;
725 char* ptr, *ret, *str;
726 SPInt len = 7, i = 0, j;
744 for (i=0;i<depth-1;i++)
752 entries=(
char**)
OVR_ALLOC(numentries*
sizeof(
char*));
755 names=(
char**)
OVR_ALLOC(numentries*
sizeof(
char*));
762 memset(entries,0,
sizeof(
char*)*numentries);
763 memset(names,0,
sizeof(
char*)*numentries);
774 entries[i++] = ret = child->
PrintValue(depth, fmt);
798 for (i=0;i<numentries;i++)
818 for (i=0; i<numentries; i++)
822 for (j=0; j<depth; j++)
851 for (i=0;i<depth-1;i++)
888 child = child->pNext;
912 child = child->pNext;
988 item->
Value = strVal;
1013 return (
int)item->
dValue;
1024 return (
int)item->
dValue != 0;
1063 while (iter && i<index)
1090 return number ? number->
dValue : 0.0;
1104 return number ? number->
Value : 0;
1143 int bytes = f.
Read(buff, len);
1146 if (bytes == 0 || bytes != len)
1177 return (bytes == len);
double GetArrayNumber(int index)
static JSON * createHelper(JSONItemType itemType, double dval, const char *strVal=0)
bool Save(const char *path)
#define OVR_ASSERT_LOG(c, args)
unsigned GetItemCount() const
__BEGIN_NAMESPACE_STD void * memcpy(void *__restrict __dest, const void *__restrict __src, size_t __n) __THROW __nonnull((1
int GetIntByName(const char *name, int defValue=0)
char * PrintValue(int depth, bool fmt)
static const unsigned char firstByteMark[7]
static char * PrintNumber(double d)
void InsertArrayElement(int index, JSON *item)
static JSON * ParseBuffer(const char *buff, int len, const char **perror=0)
const char * parseString(const char *str, const char **perror)
const char * parseValue(const char *buff, const char **perror)
double GetNumberByName(const char *name, double defValue=0.0)
char * PrintString(const char *str)
virtual int Read(UByte *pbuffer, int numBytes)
char *OVR_CDECL OVR_strcpy(char *dest, UPInt destsize, const char *src)
const char * GetArrayString(int index)
const char * ParseHex(unsigned *val, unsigned digits, const char *str)
static JSON * Parse(const char *buff, const char **perror=0)
static const char * skip(const char *in)
String GetStringByName(const char *name, const String &defValue="")
static JSON * Load(const char *path, const char **perror=0)
virtual int Write(const UByte *pbuffer, int numBytes)
__BEGIN_NAMESPACE_STD char * strchr(const char *__s, int __c) __THROW __attribute_pure__ __nonnull((1))
__END_NAMESPACE_STD __BEGIN_NAMESPACE_STD char char char char int int strncmp(const char *__s1, const char *__s2, size_t __n) __THROW __attribute_pure__ __nonnull((1
const char * parseObject(const char *value, const char **perror)
void AddArrayElement(JSON *item)
void AddItem(const char *string, JSON *item)
JSON * GetItemByIndex(unsigned i)
int OVR_CDECL OVR_strcmp(const char *dest, const char *src)
void InsertNodeBefore(T *p)
bool Open(const String &path, int flags=Open_Read|Open_Buffered, int mode=Mode_ReadWrite)
__BEGIN_NAMESPACE_STD void void __END_NAMESPACE_STD void __BEGIN_NAMESPACE_STD void * memset(void *__s, int __c, size_t __n) __THROW __nonnull((1))
const char * parseNumber(const char *num)
UPInt OVR_CDECL OVR_sprintf(char *dest, UPInt destsize, const char *format,...)
int char * index(const char *__s, int __c) __THROW __attribute_pure__ __nonnull((1))
char * PrintArray(int depth, bool fmt)
char * PrintObject(int depth, bool fmt)
const char * parseArray(const char *value, const char **perror)
const char * AssignError(const char **perror, const char *errorMessage)
UPInt OVR_CDECL OVR_strlen(const char *str)
void AssignString(const InitStruct &src, UPInt size)
static char * JSON_strdup(const char *str)
JSON * GetItemByName(const char *name)
JSON(JSONItemType itemType=JSON_Object)
bool GetBoolByName(const char *name, bool defValue=false)