* Return the index of a given leaf in the c_leaf[] array, where we * record leaf values. If the leaf is new and we haven't stopped recording * leafs, then make a new slot for it and record the name. */
| 490 | * leafs, then make a new slot for it and record the name. |
| 491 | */ |
| 492 | static int |
| 493 | csv_leaf_num (xo_handle_t *xop UNUSED, csv_private_t *csv, |
| 494 | const char *name, xo_xff_flags_t flags) |
| 495 | { |
| 496 | ssize_t fnum; |
| 497 | leaf_t *lp; |
| 498 | xo_buffer_t *xbp = &csv->c_name_buf; |
| 499 | |
| 500 | for (fnum = 0; fnum < csv->c_leaf_depth; fnum++) { |
| 501 | lp = &csv->c_leaf[fnum]; |
| 502 | |
| 503 | const char *fname = xo_buf_data(xbp, lp->f_name); |
| 504 | if (xo_streq(fname, name)) |
| 505 | return fnum; |
| 506 | } |
| 507 | |
| 508 | /* If we're done with adding new leafs, then bail */ |
| 509 | if (csv->c_flags & CF_LEAFS_DONE) |
| 510 | return -1; |
| 511 | |
| 512 | /* This leaf does not exist yet, so we need to create it */ |
| 513 | /* Start by checking if there's enough room */ |
| 514 | if (csv->c_leaf_depth + 1 >= csv->c_leaf_max) { |
| 515 | /* Out of room; realloc it */ |
| 516 | ssize_t new_max = csv->c_leaf_max * 2; |
| 517 | if (new_max == 0) |
| 518 | new_max = C_LEAF_MAX; |
| 519 | |
| 520 | lp = xo_realloc(csv->c_leaf, new_max * sizeof(*lp)); |
| 521 | if (lp == NULL) |
| 522 | return -1; /* No luck; bail */ |
| 523 | |
| 524 | /* Zero out the new portion */ |
| 525 | bzero(&lp[csv->c_leaf_max], csv->c_leaf_max * sizeof(*lp)); |
| 526 | |
| 527 | /* Update csv data */ |
| 528 | csv->c_leaf = lp; |
| 529 | csv->c_leaf_max = new_max; |
| 530 | } |
| 531 | |
| 532 | lp = &csv->c_leaf[csv->c_leaf_depth++]; |
| 533 | #ifdef CSV_STACK_IS_NEEDED |
| 534 | lp->f_depth = csv->c_stack_depth; |
| 535 | #endif /* CSV_STACK_IS_NEEDED */ |
| 536 | |
| 537 | lp->f_name = xo_buf_offset(xbp); |
| 538 | |
| 539 | char *cp = xo_buf_cur(xbp); |
| 540 | xo_buf_append(xbp, name, strlen(name) + 1); |
| 541 | |
| 542 | if (flags & XFF_KEY) |
| 543 | lp->f_flags |= LF_KEY; |
| 544 | |
| 545 | csv_dbg(xop, csv, "csv: leaf: name: %zd [%s] [%s] %x\n", |
| 546 | fnum, name, cp, lp->f_flags); |
| 547 | |
| 548 | return fnum; |
| 549 | } |
no test coverage detected