Parse the input text to generate a number, and populate the result into item. */
| 103 | |
| 104 | /* Parse the input text to generate a number, and populate the result into item. */ |
| 105 | static const char *parse_number(cJSON *item,const char *num) |
| 106 | { |
| 107 | double n=0,sign=1,scale=0;int subscale=0,signsubscale=1; |
| 108 | |
| 109 | /* Could use sscanf for this? */ |
| 110 | if (*num=='-') sign=-1,num++; /* Has sign? */ |
| 111 | if (*num=='0') num++; /* is zero */ |
| 112 | if (*num>='1' && *num<='9') do n=(n*10.0)+(*num++ -'0'); while (*num>='0' && *num<='9'); /* Number? */ |
| 113 | if (*num=='.') {num++; do n=(n*10.0)+(*num++ -'0'),scale--; while (*num>='0' && *num<='9');} /* Fractional part? */ |
| 114 | if (*num=='e' || *num=='E') /* Exponent? */ |
| 115 | { num++;if (*num=='+') num++; else if (*num=='-') signsubscale=-1,num++; /* With sign? */ |
| 116 | while (*num>='0' && *num<='9') subscale=(subscale*10)+(*num++ - '0'); /* Number? */ |
| 117 | } |
| 118 | |
| 119 | n=sign*n*pow(10.0,(scale+subscale*signsubscale)); /* number = +/- number.fraction * 10^+/- exponent */ |
| 120 | |
| 121 | item->valuedouble=n; |
| 122 | item->valueint=static_cast<int> (n); |
| 123 | item->type=cJSON_Number; |
| 124 | return num; |
| 125 | } |
| 126 | |
| 127 | /* Render the number nicely from the given item into a string. */ |
| 128 | static char *print_number(cJSON *item) |