Skip to content

Commit

Permalink
gcore: use new CSLConstList/CPLStringList facilities
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Mar 17, 2024
1 parent b4bc273 commit dcff935
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 187 deletions.
35 changes: 11 additions & 24 deletions gcore/gdaldataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,8 @@ static std::string
GDALSharedDatasetConcatenateOpenOptions(CSLConstList papszOpenOptions)
{
std::string osStr;
for (CSLConstList papszIter = papszOpenOptions; papszIter && *papszIter;
++papszIter)
osStr += *papszIter;
for (const char *pszOption : cpl::Iterate(papszOpenOptions))
osStr += *pszOption;
return osStr;
}

Expand Down Expand Up @@ -2105,18 +2104,10 @@ CPLErr GDALDataset::BuildOverviews(const char *pszResampling, int nOverviews,
// At time of writing, all overview generation options are actually
// expected to be passed as configuration options.
std::vector<std::unique_ptr<CPLConfigOptionSetter>> apoConfigOptionSetter;
for (CSLConstList papszIter = papszOptions; papszIter && *papszIter;
++papszIter)
for (const auto &[pszKey, pszValue] : cpl::IterateNameValue(papszOptions))
{
char *pszKey = nullptr;
const char *pszValue = CPLParseNameValue(*papszIter, &pszKey);
if (pszKey && pszValue)
{
apoConfigOptionSetter.emplace_back(
std::make_unique<CPLConfigOptionSetter>(pszKey, pszValue,
false));
}
CPLFree(pszKey);
apoConfigOptionSetter.emplace_back(
std::make_unique<CPLConfigOptionSetter>(pszKey, pszValue, false));
}

const CPLErr eErr =
Expand Down Expand Up @@ -3244,15 +3235,12 @@ char **GDALDataset::GetFileList()
if (oOvManager.HaveMaskFile())
{
auto iter = aosDatasetList.insert(datasetCtxt).first;
char **papszMskList = oOvManager.poMaskDS->GetFileList();
char **papszIter = papszMskList;
while (papszIter && *papszIter)
for (const char *pszFile :
CPLStringList(oOvManager.poMaskDS->GetFileList()))
{
if (CSLFindString(papszList, *papszIter) < 0)
papszList = CSLAddString(papszList, *papszIter);
++papszIter;
if (CSLFindString(papszList, pszFile) < 0)
papszList = CSLAddString(papszList, pszFile);
}
CSLDestroy(papszMskList);
aosDatasetList.erase(iter);
}

Expand Down Expand Up @@ -3594,9 +3582,8 @@ GDALDatasetH CPL_STDCALL GDALOpenEx(const char *pszFilename,
}

std::string osAllowedDrivers;
for (CSLConstList papszIter = papszAllowedDrivers; papszIter && *papszIter;
++papszIter)
osAllowedDrivers += *papszIter;
for (const char *pszDriverName : cpl::Iterate(papszAllowedDrivers))
osAllowedDrivers += pszDriverName;
auto dsCtxt = GDALAntiRecursionStruct::DatasetContext(
std::string(pszFilename), nOpenFlags, osAllowedDrivers);
if (sAntiRecursion.aosDatasetNamesWithFlags.find(dsCtxt) !=
Expand Down
68 changes: 27 additions & 41 deletions gcore/gdaldriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,11 +575,10 @@ GDALDataset *GDALDriver::DefaultCreateCopy(const char *pszFilename,
if (poSrcGroup != nullptr && GetMetadataItem(GDAL_DCAP_MULTIDIM_RASTER))
{
CPLStringList aosDatasetCO;
for (CSLConstList papszIter = papszOptions; papszIter && *papszIter;
++papszIter)
for (const char *pszOption : cpl::Iterate(papszOptions))
{
if (!STARTS_WITH_CI(*papszIter, "ARRAY:"))
aosDatasetCO.AddString(*papszIter);
if (!STARTS_WITH_CI(pszOption, "ARRAY:"))
aosDatasetCO.AddString(pszOption);
}
auto poDstDS = std::unique_ptr<GDALDataset>(
CreateMultiDimensional(pszFilename, nullptr, aosDatasetCO.List()));
Expand Down Expand Up @@ -932,13 +931,9 @@ void GDALDriver::DefaultCopyMetadata(GDALDataset *poSrcDS, GDALDataset *poDstDS,
if ((!EQUAL(pszCopySrcMDD, "AUTO") && CPLTestBool(pszCopySrcMDD)) ||
papszSrcMDD)
{
char **papszDomainList = poSrcDS->GetMetadataDomainList();
constexpr const char *apszReservedDomains[] = {
"IMAGE_STRUCTURE", "DERIVED_SUBDATASETS"};
for (char **papszIter = papszDomainList; papszIter && *papszIter;
++papszIter)
for (const char *pszDomain :
CPLStringList(poSrcDS->GetMetadataDomainList()))
{
const char *pszDomain = *papszIter;
if (pszDomain[0] != 0 &&
(!papszSrcMDD ||
CSLFindString(papszSrcMDD, pszDomain) >= 0))
Expand All @@ -960,6 +955,8 @@ void GDALDriver::DefaultCopyMetadata(GDALDataset *poSrcDS, GDALDataset *poDstDS,
}
if (!papszSrcMDD)
{
constexpr const char *const apszReservedDomains[] =
{"IMAGE_STRUCTURE", "DERIVED_SUBDATASETS"};
for (const char *pszOtherDomain :
apszReservedDomains)
{
Expand All @@ -978,7 +975,6 @@ void GDALDriver::DefaultCopyMetadata(GDALDataset *poSrcDS, GDALDataset *poDstDS,
}
}
}
CSLDestroy(papszDomainList);
}
}
CSLDestroy(papszSrcMDD);
Expand Down Expand Up @@ -1018,14 +1014,12 @@ CPLErr GDALDriver::QuietDeleteForCreateCopy(const char *pszFilename,
pszFilename, GDAL_OF_RASTER, apszAllowedDrivers));
if (poExistingOutputDS)
{
char **papszFileList = poExistingOutputDS->GetFileList();
for (char **papszIter = papszFileList; papszIter && *papszIter;
++papszIter)
for (const char *pszFileInList :
CPLStringList(poExistingOutputDS->GetFileList()))
{
oSetExistingDestFiles.insert(
CPLString(*papszIter).replaceAll('\\', '/'));
CPLString(pszFileInList).replaceAll('\\', '/'));
}
CSLDestroy(papszFileList);
}
CPLPopErrorHandler();
}
Expand Down Expand Up @@ -1053,19 +1047,17 @@ CPLErr GDALDriver::QuietDeleteForCreateCopy(const char *pszFilename,
poSrcDS->papszOpenOptions));
if (poSrcDSTmp)
{
char **papszFileList = poSrcDSTmp->GetFileList();
for (char **papszIter = papszFileList; papszIter && *papszIter;
++papszIter)
for (const char *pszFileInList :
CPLStringList(poSrcDSTmp->GetFileList()))
{
CPLString osFilename(*papszIter);
CPLString osFilename(pszFileInList);
osFilename.replaceAll('\\', '/');
if (oSetExistingDestFiles.find(osFilename) !=
oSetExistingDestFiles.end())
{
oSetExistingDestFilesFoundInSource.insert(osFilename);
}
}
CSLDestroy(papszFileList);
}
CPLPopErrorHandler();
}
Expand Down Expand Up @@ -1323,11 +1315,10 @@ GDALDataset *GDALDriver::CreateCopy(const char *pszFilename,
if (poSrcGroup != nullptr && GetMetadataItem(GDAL_DCAP_MULTIDIM_RASTER))
{
CPLStringList aosDatasetCO;
for (CSLConstList papszIter = papszOptions; papszIter && *papszIter;
++papszIter)
for (const char *pszOption : cpl::Iterate(papszOptions))
{
if (!STARTS_WITH_CI(*papszIter, "ARRAY:"))
aosDatasetCO.AddString(*papszIter);
if (!STARTS_WITH_CI(pszOption, "ARRAY:"))
aosDatasetCO.AddString(pszOption);
}
GDALValidateCreationOptions(this, aosDatasetCO.List());
}
Expand Down Expand Up @@ -1462,10 +1453,9 @@ bool GDALDriver::CanVectorTranslateFrom(
ppapszFailureReasons ? ppapszFailureReasons : &papszFailureReasons);
if (!ppapszFailureReasons)
{
for (CSLConstList papszIter = papszFailureReasons;
papszIter && *papszIter; ++papszIter)
for (const char *pszReason : cpl::Iterate(papszFailureReasons))
{
CPLDebug("GDAL", "%s", *papszIter);
CPLDebug("GDAL", "%s", pszReason);
}
CSLDestroy(papszFailureReasons);
}
Expand Down Expand Up @@ -1560,11 +1550,10 @@ CPLErr GDALDriver::QuietDelete(const char *pszName,
if (papszAllowedDrivers)
{
GDALOpenInfo oOpenInfo(pszName, GDAL_OF_ALL);
for (CSLConstList papszIter = papszAllowedDrivers; *papszIter;
++papszIter)
for (const char *pszDriverName : cpl::Iterate(papszAllowedDrivers))
{
GDALDriver *poTmpDriver =
GDALDriver::FromHandle(GDALGetDriverByName(*papszIter));
GDALDriver::FromHandle(GDALGetDriverByName(pszDriverName));
if (poTmpDriver)
{
const bool bIdentifyRes =
Expand Down Expand Up @@ -2118,14 +2107,13 @@ int CPL_STDCALL GDALValidateCreationOptions(GDALDriverH hDriver,
osDriver.Printf("driver %s",
GDALDriver::FromHandle(hDriver)->GetDescription());
bool bFoundOptionToRemove = false;
for (CSLConstList papszIter = papszCreationOptions; papszIter && *papszIter;
++papszIter)
for (const char *pszCO : cpl::Iterate(papszCreationOptions))
{
for (const char *pszExcludedOptions :
{"APPEND_SUBDATASET", "COPY_SRC_MDD", "SRC_MDD"})
{
if (STARTS_WITH_CI(*papszIter, pszExcludedOptions) &&
(*papszIter)[strlen(pszExcludedOptions)] == '=')
if (STARTS_WITH_CI(pszCO, pszExcludedOptions) &&
pszCO[strlen(pszExcludedOptions)] == '=')
{
bFoundOptionToRemove = true;
break;
Expand All @@ -2138,23 +2126,21 @@ int CPL_STDCALL GDALValidateCreationOptions(GDALDriverH hDriver,
char **papszOptionsToFree = nullptr;
if (bFoundOptionToRemove)
{
for (CSLConstList papszIter = papszCreationOptions;
papszIter && *papszIter; ++papszIter)
for (const char *pszCO : cpl::Iterate(papszCreationOptions))
{
bool bMatch = false;
for (const char *pszExcludedOptions :
{"APPEND_SUBDATASET", "COPY_SRC_MDD", "SRC_MDD"})
{
if (STARTS_WITH_CI(*papszIter, pszExcludedOptions) &&
(*papszIter)[strlen(pszExcludedOptions)] == '=')
if (STARTS_WITH_CI(pszCO, pszExcludedOptions) &&
pszCO[strlen(pszExcludedOptions)] == '=')
{
bMatch = true;
break;
}
}
if (!bMatch)
papszOptionsToFree =
CSLAddString(papszOptionsToFree, *papszIter);
papszOptionsToFree = CSLAddString(papszOptionsToFree, pszCO);
}
papszOptionsToValidate = papszOptionsToFree;
}
Expand Down
28 changes: 13 additions & 15 deletions gcore/gdaljp2abstractdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ void GDALJP2AbstractDataset::LoadJP2Metadata(GDALOpenInfo *poOpenInfo,

if (bGeorefSourcesConfigOption)
{
for (char **papszIter = papszTokens; *papszIter; ++papszIter)
for (const char *pszToken : cpl::Iterate(papszTokens))
{
if (!EQUAL(*papszIter, "PAM") && !EQUAL(*papszIter, "GEOJP2") &&
!EQUAL(*papszIter, "GMLJP2") && !EQUAL(*papszIter, "MSIG") &&
!EQUAL(*papszIter, "WORLDFILE") && !EQUAL(*papszIter, "NONE"))
if (!EQUAL(pszToken, "PAM") && !EQUAL(pszToken, "GEOJP2") &&
!EQUAL(pszToken, "GMLJP2") && !EQUAL(pszToken, "MSIG") &&
!EQUAL(pszToken, "WORLDFILE") && !EQUAL(pszToken, "NONE"))
{
CPLError(CE_Warning, CPLE_AppDefined,
"Unhandled value %s in GEOREF_SOURCES", *papszIter);
"Unhandled value %s in GEOREF_SOURCES", pszToken);
}
}
}
Expand Down Expand Up @@ -224,26 +224,24 @@ void GDALJP2AbstractDataset::LoadJP2Metadata(GDALOpenInfo *poOpenInfo,
{
GDALMultiDomainMetadata oLocalMDMD;
oLocalMDMD.XMLInit(psXMLNode, FALSE);
char **papszDomainList = oLocalMDMD.GetDomainList();
char **papszIter = papszDomainList;
GDALDataset::SetMetadata(oLocalMDMD.GetMetadata());
while (papszIter && *papszIter)
for (const char *pszDomain :
cpl::Iterate(oLocalMDMD.GetDomainList()))
{
if (!EQUAL(*papszIter, "") &&
!EQUAL(*papszIter, "IMAGE_STRUCTURE"))
if (!EQUAL(pszDomain, "") &&
!EQUAL(pszDomain, "IMAGE_STRUCTURE"))
{
if (GDALDataset::GetMetadata(*papszIter) != nullptr)
if (GDALDataset::GetMetadata(pszDomain) != nullptr)
{
CPLDebug(
"GDALJP2",
"GDAL metadata overrides metadata in %s domain "
"over metadata read from other boxes",
*papszIter);
pszDomain);
}
GDALDataset::SetMetadata(oLocalMDMD.GetMetadata(*papszIter),
*papszIter);
GDALDataset::SetMetadata(oLocalMDMD.GetMetadata(pszDomain),
pszDomain);
}
++papszIter;
}
CPLDestroyXMLNode(psXMLNode);
}
Expand Down
7 changes: 3 additions & 4 deletions gcore/gdalmultidim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,12 +913,11 @@ bool GDALGroup::CopyFrom(const std::shared_ptr<GDALGroup> &poDstRootGroup,
CPLStringList aosArrayCO;
bool bAutoScale = false;
GDALDataType eAutoScaleType = GDT_UInt16;
for (CSLConstList papszIter = papszOptions; papszIter && *papszIter;
++papszIter)
for (const char *pszItem : cpl::Iterate(papszOptions))
{
if (STARTS_WITH_CI(*papszIter, "ARRAY:"))
if (STARTS_WITH_CI(pszItem, "ARRAY:"))
{
const char *pszOption = *papszIter + strlen("ARRAY:");
const char *pszOption = pszItem + strlen("ARRAY:");
if (STARTS_WITH_CI(pszOption, "IF(DIM="))
{
const char *pszNext = strchr(pszOption, ':');
Expand Down
13 changes: 5 additions & 8 deletions gcore/gdalpamdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1800,26 +1800,23 @@ void GDALPamDataset::ClearStatistics()
{
bool bChanged = false;
GDALRasterBand *poBand = GetRasterBand(i);
char **papszOldMD = poBand->GetMetadata();
char **papszNewMD = nullptr;
for (char **papszIter = papszOldMD; papszIter && papszIter[0];
++papszIter)
CPLStringList aosNewMD;
for (const char *pszStr : cpl::Iterate(poBand->GetMetadata()))
{
if (STARTS_WITH_CI(*papszIter, "STATISTICS_"))
if (STARTS_WITH_CI(pszStr, "STATISTICS_"))
{
MarkPamDirty();
bChanged = true;
}
else
{
papszNewMD = CSLAddString(papszNewMD, *papszIter);
aosNewMD.AddString(pszStr);
}
}
if (bChanged)
{
poBand->SetMetadata(papszNewMD);
poBand->SetMetadata(aosNewMD.List());
}
CSLDestroy(papszNewMD);
}

GDALDataset::ClearStatistics();
Expand Down
Loading

0 comments on commit dcff935

Please sign in to comment.