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