| 570 | */ |
| 571 | |
| 572 | char **CSLRemoveStrings(char **papszStrList, int nFirstLineToDelete, |
| 573 | int nNumToRemove, char ***ppapszRetStrings) |
| 574 | { |
| 575 | const int nSrcLines = CSLCount(papszStrList); |
| 576 | |
| 577 | if (nNumToRemove < 1 || nSrcLines == 0) |
| 578 | return papszStrList; // Nothing to do! |
| 579 | |
| 580 | // If operation will result in an empty StringList, don't waste |
| 581 | // time here. |
| 582 | const int nDstLines = nSrcLines - nNumToRemove; |
| 583 | if (nDstLines < 1) |
| 584 | { |
| 585 | CSLDestroy(papszStrList); |
| 586 | return nullptr; |
| 587 | } |
| 588 | |
| 589 | // Remove lines from the source StringList. |
| 590 | // Either free() each line or store them to a new StringList depending on |
| 591 | // the caller's choice. |
| 592 | char **ppszDst = papszStrList + nFirstLineToDelete; |
| 593 | |
| 594 | if (ppapszRetStrings == nullptr) |
| 595 | { |
| 596 | // free() all the strings that will be removed. |
| 597 | for (int i = 0; i < nNumToRemove; ++i) |
| 598 | { |
| 599 | CPLFree(*ppszDst); |
| 600 | *ppszDst = nullptr; |
| 601 | } |
| 602 | } |
| 603 | else |
| 604 | { |
| 605 | // Store the strings to remove in a new StringList. |
| 606 | *ppapszRetStrings = |
| 607 | static_cast<char **>(CPLCalloc(nNumToRemove + 1, sizeof(char *))); |
| 608 | |
| 609 | for (int i = 0; i < nNumToRemove; ++i) |
| 610 | { |
| 611 | (*ppapszRetStrings)[i] = *ppszDst; |
| 612 | *ppszDst = nullptr; |
| 613 | ++ppszDst; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | // Shift down all the lines that follow the lines to remove. |
| 618 | if (nFirstLineToDelete == -1 || nFirstLineToDelete > nSrcLines) |
| 619 | nFirstLineToDelete = nDstLines; |
| 620 | |
| 621 | char **ppszSrc = papszStrList + nFirstLineToDelete + nNumToRemove; |
| 622 | ppszDst = papszStrList + nFirstLineToDelete; |
| 623 | |
| 624 | for (; *ppszSrc != nullptr; ++ppszSrc, ++ppszDst) |
| 625 | { |
| 626 | *ppszDst = *ppszSrc; |
| 627 | } |
| 628 | // Move the NULL pointer at the end of the StringList. |
| 629 | *ppszDst = *ppszSrc; |
no test coverage detected