mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
reformatting: parse_array
This commit is contained in:
parent
a9f752e034
commit
ad711e6fab
77
cJSON.c
77
cJSON.c
@ -981,28 +981,67 @@ static char *print_value(cJSON *item, int depth, int fmt, printbuffer *p)
|
|||||||
static const char *parse_array(cJSON *item,const char *value,const char **ep)
|
static const char *parse_array(cJSON *item,const char *value,const char **ep)
|
||||||
{
|
{
|
||||||
cJSON *child;
|
cJSON *child;
|
||||||
if (*value!='[') {*ep=value;return 0;} /* not an array! */
|
if (*value != '[')
|
||||||
|
|
||||||
item->type=cJSON_Array;
|
|
||||||
value=skip(value+1);
|
|
||||||
if (*value==']') return value+1; /* empty array. */
|
|
||||||
|
|
||||||
item->child=child=cJSON_New_Item();
|
|
||||||
if (!item->child) return 0; /* memory fail */
|
|
||||||
value=skip(parse_value(child,skip(value),ep)); /* skip any spacing, get the value. */
|
|
||||||
if (!value) return 0;
|
|
||||||
|
|
||||||
while (*value==',')
|
|
||||||
{
|
{
|
||||||
cJSON *new_item;
|
/* not an array! */
|
||||||
if (!(new_item=cJSON_New_Item())) return 0; /* memory fail */
|
*ep = value;
|
||||||
child->next=new_item;new_item->prev=child;child=new_item;
|
return 0;
|
||||||
value=skip(parse_value(child,skip(value+1),ep));
|
|
||||||
if (!value) return 0; /* memory fail */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (*value==']') return value+1; /* end of array */
|
item->type = cJSON_Array;
|
||||||
*ep=value;return 0; /* malformed. */
|
value = skip(value + 1);
|
||||||
|
if (*value == ']')
|
||||||
|
{
|
||||||
|
/* empty array. */
|
||||||
|
return value + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
item->child = child = cJSON_New_Item();
|
||||||
|
if (!item->child)
|
||||||
|
{
|
||||||
|
/* memory fail */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/* skip any spacing, get the value. */
|
||||||
|
value = skip(parse_value(child, skip(value), ep));
|
||||||
|
if (!value)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* loop through the comma separated array elements */
|
||||||
|
while (*value == ',')
|
||||||
|
{
|
||||||
|
cJSON *new_item;
|
||||||
|
if (!(new_item = cJSON_New_Item()))
|
||||||
|
{
|
||||||
|
/* memory fail */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/* add new item to end of the linked list */
|
||||||
|
child->next = new_item;
|
||||||
|
new_item->prev = child;
|
||||||
|
child = new_item;
|
||||||
|
|
||||||
|
/* go to the next comma */
|
||||||
|
value = skip(parse_value(child, skip(value + 1), ep));
|
||||||
|
if (!value)
|
||||||
|
{
|
||||||
|
/* memory fail */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*value == ']')
|
||||||
|
{
|
||||||
|
/* end of array */
|
||||||
|
return value + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* malformed. */
|
||||||
|
*ep = value;
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Render an array to text */
|
/* Render an array to text */
|
||||||
|
Loading…
Reference in New Issue
Block a user