Skip to content

Commit

Permalink
apps: use cpl::Iterate
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Mar 19, 2024
1 parent 180bc15 commit cd48932
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 80 deletions.
20 changes: 5 additions & 15 deletions apps/gdal_translate_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2582,18 +2582,10 @@ static void AttachMetadata(GDALDatasetH hDS,
const CPLStringList &aosMetadataOptions)

{
const int nCount = aosMetadataOptions.size();

for (int i = 0; i < nCount; i++)
for (const auto &[pszKey, pszValue] :
cpl::IterateNameValue(aosMetadataOptions))
{
char *pszKey = nullptr;
const char *pszValue =
CPLParseNameValue(aosMetadataOptions[i], &pszKey);
if (pszKey && pszValue)
{
GDALSetMetadataItem(hDS, pszKey, pszValue, nullptr);
}
CPLFree(pszKey);
GDALSetMetadataItem(hDS, pszKey, pszValue, nullptr);
}
}

Expand All @@ -2605,17 +2597,15 @@ static void AttachDomainMetadata(GDALDatasetH hDS,
const CPLStringList &aosDomainMetadataOptions)

{
const int nCount = aosDomainMetadataOptions.size();

for (int i = 0; i < nCount; i++)
for (const char *pszStr : aosDomainMetadataOptions)
{

char *pszKey = nullptr;
char *pszDomain = nullptr;

// parse the DOMAIN:KEY=value, Remainder is KEY=value
const char *pszRemainder =
CPLParseNameValueSep(aosDomainMetadataOptions[i], &pszDomain, ':');
CPLParseNameValueSep(pszStr, &pszDomain, ':');

if (pszDomain && pszRemainder)
{
Expand Down
11 changes: 5 additions & 6 deletions apps/gdalmdiminfo_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,21 +715,20 @@ static void DumpStructuralInfo(CSLConstList papszStructuralInfo,
CPLJSonStreamingWriter &serializer)
{
auto objectContext(serializer.MakeObjectContext());
for (int i = 0; papszStructuralInfo && papszStructuralInfo[i]; ++i)
int i = 1;
for (const auto &[pszKey, pszValue] : cpl::IterateNameValue(
papszStructuralInfo, /* bReturnNullKeyIfNotNameValue = */ true))
{
char *pszKey = nullptr;
const char *pszValue =
CPLParseNameValue(papszStructuralInfo[i], &pszKey);
if (pszKey)
{
serializer.AddObjKey(pszKey);
}
else
{
serializer.AddObjKey(CPLSPrintf("metadata_%d", i + 1));
serializer.AddObjKey(CPLSPrintf("metadata_%d", i));
++i;
}
serializer.Add(pszValue);
CPLFree(pszKey);
}
}

Expand Down
13 changes: 5 additions & 8 deletions apps/gdaltindex_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,14 +699,11 @@ GDALDatasetH GDALTileIndex(const char *pszDest, int nSrcCount,
{
poLayer->SetMetadataItem("MASK_BAND", "YES");
}
for (const auto &osNameValue : psOptions->aosMetadata)
{
char *pszKey = nullptr;
const char *pszValue =
CPLParseNameValue(osNameValue.c_str(), &pszKey);
if (pszKey && pszValue)
poLayer->SetMetadataItem(pszKey, pszValue);
CPLFree(pszKey);
const CPLStringList aosMetadata(psOptions->aosMetadata);
for (const auto &[pszKey, pszValue] :
cpl::IterateNameValue(aosMetadata))
{
poLayer->SetMetadataItem(pszKey, pszValue);
}
}

Expand Down
37 changes: 11 additions & 26 deletions apps/gdalwarp_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ static GDALDatasetH GDALWarpCreateOutput(
GDALWarpAppOptions *psOptions);

static void RemoveConflictingMetadata(GDALMajorObjectH hObj,
char **papszMetadata,
CSLConstList papszMetadata,
const char *pszValueConflict);

static bool GetResampleAlg(const char *pszResampling,
Expand Down Expand Up @@ -5267,40 +5267,25 @@ static CPLErr TransformCutlineToSource(GDALDataset *poSrcDS,
}

static void RemoveConflictingMetadata(GDALMajorObjectH hObj,
char **papszMetadata,
CSLConstList papszSrcMetadata,
const char *pszValueConflict)
{
if (hObj == nullptr)
return;

char **papszMetadataRef = CSLDuplicate(papszMetadata);
int nCount = CSLCount(papszMetadataRef);

for (int i = 0; i < nCount; i++)
for (const auto &[pszKey, pszValue] :
cpl::IterateNameValue(papszSrcMetadata))
{
char *pszKey = nullptr;
const char *pszValueRef =
CPLParseNameValue(papszMetadataRef[i], &pszKey);
if (pszKey != nullptr)
const char *pszValueComp = GDALGetMetadataItem(hObj, pszKey, nullptr);
if (pszValueComp == nullptr || (!EQUAL(pszValue, pszValueComp) &&
!EQUAL(pszValueComp, pszValueConflict)))
{
const char *pszValueComp =
GDALGetMetadataItem(hObj, pszKey, nullptr);
if ((pszValueRef == nullptr || pszValueComp == nullptr ||
!EQUAL(pszValueRef, pszValueComp)) &&
(pszValueComp == nullptr ||
!EQUAL(pszValueComp, pszValueConflict)))
{
if (STARTS_WITH(pszKey, "STATISTICS_"))
GDALSetMetadataItem(hObj, pszKey, nullptr, nullptr);
else
GDALSetMetadataItem(hObj, pszKey, pszValueConflict,
nullptr);
}
CPLFree(pszKey);
if (STARTS_WITH(pszKey, "STATISTICS_"))
GDALSetMetadataItem(hObj, pszKey, nullptr, nullptr);
else
GDALSetMetadataItem(hObj, pszKey, pszValueConflict, nullptr);
}
}

CSLDestroy(papszMetadataRef);
}

/************************************************************************/
Expand Down
44 changes: 19 additions & 25 deletions apps/ogrinfo_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ GDALVectorInfoPrintMetadata(CPLString &osRet, CPLJSONObject &oMetadata,
else if (pszDomain != nullptr && STARTS_WITH_CI(pszDomain, "json:"))
bMDIsJson = true;

char **papszMetadata = GDALGetMetadata(hObject, pszDomain);
CSLConstList papszMetadata = GDALGetMetadata(hObject, pszDomain);
if (CSLCount(papszMetadata) > 0)
{
CPLJSONObject oMetadataDomain;
Expand Down Expand Up @@ -1051,33 +1051,27 @@ static void ReportOnLayer(CPLString &osRet, CPLJSONObject &oLayer,
oCoordPrec.oFormatSpecificOptions)
{
CPLJSONObject oThisFormatSpecificOptions;
for (int i = 0; i < formatOptionsPair.second.size();
++i)
for (const auto &[pszKey, pszValue] :
cpl::IterateNameValue(
formatOptionsPair.second))
{
char *pszKey = nullptr;
const char *pszValue = CPLParseNameValue(
formatOptionsPair.second[i], &pszKey);
if (pszKey && pszValue)
const auto eValueType =
CPLGetValueType(pszValue);
if (eValueType == CPL_VALUE_INTEGER)
{
const auto eValueType =
CPLGetValueType(pszValue);
if (eValueType == CPL_VALUE_INTEGER)
{
oThisFormatSpecificOptions.Add(
pszKey, CPLAtoGIntBig(pszValue));
}
else if (eValueType == CPL_VALUE_REAL)
{
oThisFormatSpecificOptions.Add(
pszKey, CPLAtof(pszValue));
}
else
{
oThisFormatSpecificOptions.Add(
pszKey, pszValue);
}
oThisFormatSpecificOptions.Add(
pszKey, CPLAtoGIntBig(pszValue));
}
else if (eValueType == CPL_VALUE_REAL)
{
oThisFormatSpecificOptions.Add(
pszKey, CPLAtof(pszValue));
}
else
{
oThisFormatSpecificOptions.Add(pszKey,
pszValue);
}
CPLFree(pszKey);
}
oFormatSpecificOptions.Add(
formatOptionsPair.first,
Expand Down

0 comments on commit cd48932

Please sign in to comment.