MCPcopy Create free account
hub / github.com/OSGeo/gdal / CSLLoad2

Function CSLLoad2

port/cpl_string.cpp:305–359  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

303 */
304
305char **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() */

Callers 13

Detect_AVL_ADCMethod · 0.85
OpenMethod · 0.85
_AVCBinReadOpenPrjFunction · 0.85
GetSpatialRefMethod · 0.85
GDALLoadRPCFileFunction · 0.85
LoadPythonDriverFunction · 0.85
CSLLoadFunction · 0.85
OpenInternalMethod · 0.85
OpenMethod · 0.85

Calls 10

VSIFOpenLFunction · 0.85
CPLErrorFunction · 0.85
VSIFEofLFunction · 0.85
CPLReadLine2LFunction · 0.85
VSIReallocFunction · 0.85
VSIFCloseLFunction · 0.85
CPLReadLineLFunction · 0.85
CPLStrdupFunction · 0.85
CPLFetchBoolFunction · 0.70
CPL_IGNORE_RET_VALFunction · 0.70

Tested by

no test coverage detected