Parse inline properties: {key: "value", ...} */
| 516 | |
| 517 | /* Parse inline properties: {key: "value", ...} */ |
| 518 | static int parse_props(parser_t *p, cbm_prop_filter_t **out, int *count) { |
| 519 | *out = NULL; |
| 520 | *count = 0; |
| 521 | if (!match(p, TOK_LBRACE)) { |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | int cap = CYP_INIT_CAP4; |
| 526 | int n = 0; |
| 527 | cbm_prop_filter_t *arr = malloc(cap * sizeof(cbm_prop_filter_t)); |
| 528 | if (!arr) { |
| 529 | return CBM_NOT_FOUND; |
| 530 | } |
| 531 | |
| 532 | while (!check(p, TOK_RBRACE) && !check(p, TOK_EOF)) { |
| 533 | /* Every failure frees the entries already parsed, not only the array. */ |
| 534 | const cbm_token_t *key = expect(p, TOK_IDENT); |
| 535 | if (!key) { |
| 536 | free_prop_entries(arr, n); |
| 537 | return CBM_NOT_FOUND; |
| 538 | } |
| 539 | if (!expect(p, TOK_COLON)) { |
| 540 | free_prop_entries(arr, n); |
| 541 | return CBM_NOT_FOUND; |
| 542 | } |
| 543 | const cbm_token_t *val = expect(p, TOK_STRING); |
| 544 | if (!val) { |
| 545 | free_prop_entries(arr, n); |
| 546 | return CBM_NOT_FOUND; |
| 547 | } |
| 548 | |
| 549 | if (n >= cap) { |
| 550 | int new_cap = cap * PAIR_LEN; |
| 551 | void *tmp = realloc(arr, new_cap * sizeof(cbm_prop_filter_t)); |
| 552 | if (!tmp) { |
| 553 | free_prop_entries(arr, n); |
| 554 | return CBM_NOT_FOUND; |
| 555 | } |
| 556 | arr = tmp; |
| 557 | cap = new_cap; |
| 558 | } |
| 559 | const char *new_key = heap_strdup(key->text); |
| 560 | const char *new_val = heap_strdup(val->text); |
| 561 | if (!new_key || !new_val) { |
| 562 | safe_str_free(&new_key); |
| 563 | safe_str_free(&new_val); |
| 564 | free_prop_entries(arr, n); |
| 565 | return CBM_NOT_FOUND; |
| 566 | } |
| 567 | arr[n].key = new_key; |
| 568 | arr[n].value = new_val; |
| 569 | n++; |
| 570 | |
| 571 | match(p, TOK_COMMA); /* optional comma */ |
| 572 | } |
| 573 | expect(p, TOK_RBRACE); |
| 574 | |
| 575 | *out = arr; |
no test coverage detected