Combine two strings as file paths. Trailing and leading separators for @a lhs and @a rhs respectively are handled to yield exactly one separator. @return A newly @x ats_malloc string of the combined paths. */
| 585 | @return A newly @x ats_malloc string of the combined paths. |
| 586 | */ |
| 587 | inline char * |
| 588 | path_join(ats_scoped_str const &lhs, ats_scoped_str const &rhs) |
| 589 | { |
| 590 | size_t ln = strlen(lhs); |
| 591 | size_t rn = strlen(rhs); |
| 592 | const char *rptr = rhs; // May need to be modified. |
| 593 | |
| 594 | if (ln && lhs[ln - 1] == '/') { |
| 595 | --ln; // drop trailing separator. |
| 596 | } |
| 597 | if (rn && *rptr == '/') { |
| 598 | --rn, ++rptr; // drop leading separator. |
| 599 | } |
| 600 | |
| 601 | ats_scoped_str x(ln + rn + 2); |
| 602 | |
| 603 | memcpy(x, lhs, ln); |
| 604 | x[ln] = '/'; |
| 605 | memcpy(x + ln + 1, rptr, rn); |
| 606 | x[ln + rn + 1] = 0; // terminate string. |
| 607 | |
| 608 | return x.release(); |
| 609 | } |
| 610 | |
| 611 | struct ats_unique_buf_deleter { |
| 612 | void |