Build an array from input text. */
| 1355 | |
| 1356 | /* Build an array from input text. */ |
| 1357 | static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer) |
| 1358 | { |
| 1359 | cJSON *head = NULL; /* head of the linked list */ |
| 1360 | cJSON *current_item = NULL; |
| 1361 | |
| 1362 | if (input_buffer->depth >= CJSON_NESTING_LIMIT) |
| 1363 | { |
| 1364 | return false; /* to deeply nested */ |
| 1365 | } |
| 1366 | input_buffer->depth++; |
| 1367 | |
| 1368 | if (buffer_at_offset(input_buffer)[0] != '[') |
| 1369 | { |
| 1370 | /* not an array */ |
| 1371 | goto fail; |
| 1372 | } |
| 1373 | |
| 1374 | input_buffer->offset++; |
| 1375 | buffer_skip_whitespace(input_buffer); |
| 1376 | if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ']')) |
| 1377 | { |
| 1378 | /* empty array */ |
| 1379 | goto success; |
| 1380 | } |
| 1381 | |
| 1382 | /* check if we skipped to the end of the buffer */ |
| 1383 | if (cannot_access_at_index(input_buffer, 0)) |
| 1384 | { |
| 1385 | input_buffer->offset--; |
| 1386 | goto fail; |
| 1387 | } |
| 1388 | |
| 1389 | /* step back to character in front of the first element */ |
| 1390 | input_buffer->offset--; |
| 1391 | /* loop through the comma separated array elements */ |
| 1392 | do |
| 1393 | { |
| 1394 | /* allocate next item */ |
| 1395 | cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); |
| 1396 | if (new_item == NULL) |
| 1397 | { |
| 1398 | goto fail; /* allocation failure */ |
| 1399 | } |
| 1400 | |
| 1401 | /* attach next item to list */ |
| 1402 | if (head == NULL) |
| 1403 | { |
| 1404 | /* start the linked list */ |
| 1405 | current_item = head = new_item; |
| 1406 | } |
| 1407 | else |
| 1408 | { |
| 1409 | /* add to the end and advance */ |
| 1410 | current_item->next = new_item; |
| 1411 | new_item->prev = current_item; |
| 1412 | current_item = new_item; |
| 1413 | } |
| 1414 |
no test coverage detected