| 303 | */ |
| 304 | |
| 305 | char **CSLLoad2(const char *pszFname, int nMaxLines, int nMaxCols, |
| 306 | CSLConstList papszOptions) |
| 307 | { |
| 308 | VSILFILE *fp = VSIFOpenL(pszFname, "rb"); |
| 309 | |
| 310 | if (!fp) |
| 311 | { |
| 312 | if (CPLFetchBool(papszOptions, "EMIT_ERROR_IF_CANNOT_OPEN_FILE", true)) |
| 313 | { |
| 314 | // Unable to open file. |
| 315 | CPLError(CE_Failure, CPLE_OpenFailed, |
| 316 | "CSLLoad2(\"%s\") failed: unable to open file.", pszFname); |
| 317 | } |
| 318 | return nullptr; |
| 319 | } |
| 320 | |
| 321 | char **papszStrList = nullptr; |
| 322 | int nLines = 0; |
| 323 | int nAllocatedLines = 0; |
| 324 | |
| 325 | while (!VSIFEofL(fp) && (nMaxLines == -1 || nLines < nMaxLines)) |
| 326 | { |
| 327 | const char *pszLine = CPLReadLine2L(fp, nMaxCols, papszOptions); |
| 328 | if (pszLine == nullptr) |
| 329 | break; |
| 330 | |
| 331 | if (nLines + 1 >= nAllocatedLines) |
| 332 | { |
| 333 | nAllocatedLines = 16 + nAllocatedLines * 2; |
| 334 | char **papszStrListNew = static_cast<char **>( |
| 335 | VSIRealloc(papszStrList, nAllocatedLines * sizeof(char *))); |
| 336 | if (papszStrListNew == nullptr) |
| 337 | { |
| 338 | CPL_IGNORE_RET_VAL(VSIFCloseL(fp)); |
| 339 | CPLReadLineL(nullptr); |
| 340 | CPLError(CE_Failure, CPLE_OutOfMemory, |
| 341 | "CSLLoad2(\"%s\") " |
| 342 | "failed: not enough memory to allocate lines.", |
| 343 | pszFname); |
| 344 | return papszStrList; |
| 345 | } |
| 346 | papszStrList = papszStrListNew; |
| 347 | } |
| 348 | papszStrList[nLines] = CPLStrdup(pszLine); |
| 349 | papszStrList[nLines + 1] = nullptr; |
| 350 | ++nLines; |
| 351 | } |
| 352 | |
| 353 | CPL_IGNORE_RET_VAL(VSIFCloseL(fp)); |
| 354 | |
| 355 | // Free the internal thread local line buffer. |
| 356 | CPLReadLineL(nullptr); |
| 357 | |
| 358 | return papszStrList; |
| 359 | } |
| 360 | |
| 361 | /************************************************************************/ |
| 362 | /* CSLLoad() */ |
no test coverage detected