* Simplified line reading from text file. * * Similar to CPLReadLine(), but reading from a large file API handle. * * @param fp file pointer opened with VSIFOpenL(). * @param nMaxCars maximum number of characters allowed, or -1 for no limit. * @param papszOptions NULL-terminated array of options. Unused for now. * @param[out] pnBufLength size of output string (must be non-NULL) * @return
| 713 | * |
| 714 | */ |
| 715 | const char *CPLReadLine3L(VSILFILE *fp, int nMaxCars, int *pnBufLength, |
| 716 | CPL_UNUSED CSLConstList papszOptions) |
| 717 | { |
| 718 | /* -------------------------------------------------------------------- */ |
| 719 | /* Cleanup case. */ |
| 720 | /* -------------------------------------------------------------------- */ |
| 721 | if (fp == nullptr) |
| 722 | { |
| 723 | CPLReadLineBuffer(-1); |
| 724 | return nullptr; |
| 725 | } |
| 726 | |
| 727 | /* -------------------------------------------------------------------- */ |
| 728 | /* Loop reading chunks of the line till we get to the end of */ |
| 729 | /* the line. */ |
| 730 | /* -------------------------------------------------------------------- */ |
| 731 | char *pszRLBuffer = nullptr; |
| 732 | const size_t nChunkSize = 40; |
| 733 | char szChunk[nChunkSize] = {}; |
| 734 | size_t nChunkBytesRead = 0; |
| 735 | size_t nChunkBytesConsumed = 0; |
| 736 | |
| 737 | *pnBufLength = 0; |
| 738 | szChunk[0] = 0; |
| 739 | |
| 740 | while (true) |
| 741 | { |
| 742 | /* -------------------------------------------------------------------- |
| 743 | */ |
| 744 | /* Read a chunk from the input file. */ |
| 745 | /* -------------------------------------------------------------------- |
| 746 | */ |
| 747 | if (*pnBufLength > INT_MAX - static_cast<int>(nChunkSize) - 1) |
| 748 | { |
| 749 | CPLError(CE_Failure, CPLE_AppDefined, |
| 750 | "Too big line : more than 2 billion characters!."); |
| 751 | CPLReadLineBuffer(-1); |
| 752 | return nullptr; |
| 753 | } |
| 754 | |
| 755 | pszRLBuffer = |
| 756 | CPLReadLineBuffer(static_cast<int>(*pnBufLength + nChunkSize + 1)); |
| 757 | if (pszRLBuffer == nullptr) |
| 758 | return nullptr; |
| 759 | |
| 760 | if (nChunkBytesRead == nChunkBytesConsumed + 1) |
| 761 | { |
| 762 | |
| 763 | // case where one character is left over from last read. |
| 764 | szChunk[0] = szChunk[nChunkBytesConsumed]; |
| 765 | |
| 766 | nChunkBytesConsumed = 0; |
| 767 | nChunkBytesRead = VSIFReadL(szChunk + 1, 1, nChunkSize - 1, fp) + 1; |
| 768 | } |
| 769 | else |
| 770 | { |
| 771 | nChunkBytesConsumed = 0; |
| 772 |
no test coverage detected