| 3523 | | |
|---|
| 3524 | | |
|---|
| 3525 | | #if 0 |
|---|
| 3526 | | |
|---|
| 3527 | | /* |
|---|
| 3528 | | * Replace the first instance of "target" in "buf" with "insert" |
|---|
| 3529 | | * If "insert" is NULL, just remove the first instance of "target" |
|---|
| 3530 | | * In either case, return TRUE if "target" is found. |
|---|
| 3531 | | * |
|---|
| 3532 | | * Could be made more efficient, especially in the case where "insert" |
|---|
| 3533 | | * is smaller than "target". |
|---|
| 3534 | | */ |
|---|
| 3535 | | static bool insert_str(char *buf, cptr target, cptr insert) |
|---|
| 3536 | | { |
|---|
| 3537 | | int i, len; |
|---|
| 3538 | | int b_len, t_len, i_len; |
|---|
| 3539 | | |
|---|
| 3540 | | /* Attempt to find the target (modify "buf") */ |
|---|
| 3541 | | buf = strstr(buf, target); |
|---|
| 3542 | | |
|---|
| 3543 | | /* No target found */ |
|---|
| 3544 | | if (!buf) return (FALSE); |
|---|
| 3545 | | |
|---|
| 3546 | | /* Be sure we have an insertion string */ |
|---|
| 3547 | | if (!insert) insert = ""; |
|---|
| 3548 | | |
|---|
| 3549 | | /* Extract some lengths */ |
|---|
| 3550 | | t_len = strlen(target); |
|---|
| 3551 | | i_len = strlen(insert); |
|---|
| 3552 | | b_len = strlen(buf); |
|---|
| 3553 | | |
|---|
| 3554 | | /* How much "movement" do we need? */ |
|---|
| 3555 | | len = i_len - t_len; |
|---|
| 3556 | | |
|---|
| 3557 | | /* We need less space (for insert) */ |
|---|
| 3558 | | if (len < 0) |
|---|
| 3559 | | { |
|---|
| 3560 | | for (i = t_len; i < b_len; ++i) buf[i+len] = buf[i]; |
|---|
| 3561 | | } |
|---|
| 3562 | | |
|---|
| 3563 | | /* We need more space (for insert) */ |
|---|
| 3564 | | else if (len > 0) |
|---|
| 3565 | | { |
|---|
| 3566 | | for (i = b_len-1; i >= t_len; --i) buf[i+len] = buf[i]; |
|---|
| 3567 | | } |
|---|
| 3568 | | |
|---|
| 3569 | | /* If movement occured, we need a new terminator */ |
|---|
| 3570 | | if (len) buf[b_len+len] = '\0'; |
|---|
| 3571 | | |
|---|
| 3572 | | /* Now copy the insertion string */ |
|---|
| 3573 | | for (i = 0; i < i_len; ++i) buf[i] = insert[i]; |
|---|
| 3574 | | |
|---|
| 3575 | | /* Successful operation */ |
|---|
| 3576 | | return (TRUE); |
|---|
| 3577 | | } |
|---|
| 3578 | | |
|---|
| 3579 | | |
|---|
| 3580 | | #endif |
|---|