From f220fc9b146e1e1705d6dc93cefd258ed5004143 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Tue, 20 Feb 2024 15:30:37 +0100 Subject: [PATCH 1/3] GDALDeserializeGCPListFromXML(): make it take a const CPLXMLNode* --- gcore/gdal_misc.cpp | 6 +++--- gcore/gdal_priv.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/gcore/gdal_misc.cpp b/gcore/gdal_misc.cpp index 63a23e5edc20..b219a9437154 100644 --- a/gcore/gdal_misc.cpp +++ b/gcore/gdal_misc.cpp @@ -4125,7 +4125,7 @@ void GDALSerializeGCPListToXML(CPLXMLNode *psParentNode, GDAL_GCP *pasGCPList, /* GDALDeserializeGCPListFromXML() */ /************************************************************************/ -void GDALDeserializeGCPListFromXML(CPLXMLNode *psGCPList, +void GDALDeserializeGCPListFromXML(const CPLXMLNode *psGCPList, GDAL_GCP **ppasGCPList, int *pnGCPCount, OGRSpatialReference **ppoGCP_SRS) { @@ -4168,7 +4168,7 @@ void GDALDeserializeGCPListFromXML(CPLXMLNode *psGCPList, // Count GCPs. int nGCPMax = 0; - for (CPLXMLNode *psXMLGCP = psGCPList->psChild; psXMLGCP != nullptr; + for (const CPLXMLNode *psXMLGCP = psGCPList->psChild; psXMLGCP != nullptr; psXMLGCP = psXMLGCP->psNext) { @@ -4185,7 +4185,7 @@ void GDALDeserializeGCPListFromXML(CPLXMLNode *psGCPList, if (nGCPMax == 0) return; - for (CPLXMLNode *psXMLGCP = psGCPList->psChild; + for (const CPLXMLNode *psXMLGCP = psGCPList->psChild; *ppasGCPList != nullptr && psXMLGCP != nullptr; psXMLGCP = psXMLGCP->psNext) { diff --git a/gcore/gdal_priv.h b/gcore/gdal_priv.h index 60f0be90536b..0368de11c838 100644 --- a/gcore/gdal_priv.h +++ b/gcore/gdal_priv.h @@ -4056,7 +4056,7 @@ double GDALAdjustNoDataCloseToFloatMax(double dfVal); void GDALSerializeGCPListToXML(CPLXMLNode *psParentNode, GDAL_GCP *pasGCPList, int nGCPCount, const OGRSpatialReference *poGCP_SRS); -void GDALDeserializeGCPListFromXML(CPLXMLNode *psGCPList, +void GDALDeserializeGCPListFromXML(const CPLXMLNode *psGCPList, GDAL_GCP **ppasGCPList, int *pnGCPCount, OGRSpatialReference **ppoGCP_SRS); From 187541adc48ccc6d592331591399d5d690bf0514 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Tue, 20 Feb 2024 15:30:53 +0100 Subject: [PATCH 2/3] VRT: make XMLInit() method take a const CPLXMLNode* --- frmts/vrt/vrtdataset.cpp | 28 +++++++++------------ frmts/vrt/vrtdataset.h | 40 +++++++++++++++--------------- frmts/vrt/vrtderivedrasterband.cpp | 7 +++--- frmts/vrt/vrtdriver.cpp | 2 +- frmts/vrt/vrtfilters.cpp | 4 +-- frmts/vrt/vrtmultidim.cpp | 6 ++--- frmts/vrt/vrtpansharpened.cpp | 15 +++++------ frmts/vrt/vrtrasterband.cpp | 19 +++++++------- frmts/vrt/vrtrawrasterband.cpp | 2 +- frmts/vrt/vrtsourcedrasterband.cpp | 4 +-- frmts/vrt/vrtsources.cpp | 30 ++++++++++++---------- frmts/vrt/vrtwarped.cpp | 12 ++++++--- 12 files changed, 87 insertions(+), 82 deletions(-) diff --git a/frmts/vrt/vrtdataset.cpp b/frmts/vrt/vrtdataset.cpp index 716137dd58ee..5e99d7437ed6 100644 --- a/frmts/vrt/vrtdataset.cpp +++ b/frmts/vrt/vrtdataset.cpp @@ -434,7 +434,7 @@ VRTRasterBand *VRTDataset::InitBand(const char *pszSubclass, int nBand, /* XMLInit() */ /************************************************************************/ -CPLErr VRTDataset::XMLInit(CPLXMLNode *psTree, const char *pszVRTPathIn) +CPLErr VRTDataset::XMLInit(const CPLXMLNode *psTree, const char *pszVRTPathIn) { if (pszVRTPathIn != nullptr) @@ -443,7 +443,7 @@ CPLErr VRTDataset::XMLInit(CPLXMLNode *psTree, const char *pszVRTPathIn) /* -------------------------------------------------------------------- */ /* Check for an SRS node. */ /* -------------------------------------------------------------------- */ - CPLXMLNode *psSRSNode = CPLGetXMLNode(psTree, "SRS"); + const CPLXMLNode *psSRSNode = CPLGetXMLNode(psTree, "SRS"); if (psSRSNode) { if (m_poSRS) @@ -480,11 +480,12 @@ CPLErr VRTDataset::XMLInit(CPLXMLNode *psTree, const char *pszVRTPathIn) /* -------------------------------------------------------------------- */ /* Check for a GeoTransform node. */ /* -------------------------------------------------------------------- */ - if (strlen(CPLGetXMLValue(psTree, "GeoTransform", "")) > 0) + const char *pszGT = CPLGetXMLValue(psTree, "GeoTransform", ""); + if (strlen(pszGT) > 0) { - const char *pszGT = CPLGetXMLValue(psTree, "GeoTransform", ""); - char **papszTokens = CSLTokenizeStringComplex(pszGT, ",", FALSE, FALSE); - if (CSLCount(papszTokens) != 6) + const CPLStringList aosTokens( + CSLTokenizeStringComplex(pszGT, ",", FALSE, FALSE)); + if (aosTokens.size() != 6) { CPLError(CE_Warning, CPLE_AppDefined, "GeoTransform node does not have expected six values."); @@ -492,19 +493,15 @@ CPLErr VRTDataset::XMLInit(CPLXMLNode *psTree, const char *pszVRTPathIn) else { for (int iTA = 0; iTA < 6; iTA++) - m_adfGeoTransform[iTA] = CPLAtof(papszTokens[iTA]); + m_adfGeoTransform[iTA] = CPLAtof(aosTokens[iTA]); m_bGeoTransformSet = TRUE; } - - CSLDestroy(papszTokens); } /* -------------------------------------------------------------------- */ /* Check for GCPs. */ /* -------------------------------------------------------------------- */ - CPLXMLNode *psGCPList = CPLGetXMLNode(psTree, "GCPList"); - - if (psGCPList != nullptr) + if (const CPLXMLNode *psGCPList = CPLGetXMLNode(psTree, "GCPList")) { GDALDeserializeGCPListFromXML(psGCPList, &m_pasGCPList, &m_nGCPCount, &m_poGCP_SRS); @@ -520,9 +517,9 @@ CPLErr VRTDataset::XMLInit(CPLXMLNode *psTree, const char *pszVRTPathIn) /* -------------------------------------------------------------------- */ /* Parse dataset mask band first */ - CPLXMLNode *psMaskBandNode = CPLGetXMLNode(psTree, "MaskBand"); + const CPLXMLNode *psMaskBandNode = CPLGetXMLNode(psTree, "MaskBand"); - CPLXMLNode *psChild = nullptr; + const CPLXMLNode *psChild = nullptr; if (psMaskBandNode) psChild = psMaskBandNode->psChild; else @@ -581,8 +578,7 @@ CPLErr VRTDataset::XMLInit(CPLXMLNode *psTree, const char *pszVRTPathIn) } } - CPLXMLNode *psGroup = CPLGetXMLNode(psTree, "Group"); - if (psGroup) + if (const CPLXMLNode *psGroup = CPLGetXMLNode(psTree, "Group")) { const char *pszName = CPLGetXMLValue(psGroup, "name", nullptr); if (pszName == nullptr || !EQUAL(pszName, "/")) diff --git a/frmts/vrt/vrtdataset.h b/frmts/vrt/vrtdataset.h index 439f3341f0db..3319366bf5eb 100644 --- a/frmts/vrt/vrtdataset.h +++ b/frmts/vrt/vrtdataset.h @@ -154,7 +154,7 @@ class CPL_DLL VRTSource int bApproxOK, GDALProgressFunc pfnProgress, void *pProgressData) = 0; - virtual CPLErr XMLInit(CPLXMLNode *psTree, const char *, + virtual CPLErr XMLInit(const CPLXMLNode *psTree, const char *, std::map &) = 0; virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath) = 0; @@ -172,17 +172,17 @@ class CPL_DLL VRTSource }; typedef VRTSource *(*VRTSourceParser)( - CPLXMLNode *, const char *, + const CPLXMLNode *, const char *, std::map &oMapSharedSources); VRTSource * -VRTParseCoreSources(CPLXMLNode *psTree, const char *, +VRTParseCoreSources(const CPLXMLNode *psTree, const char *, std::map &oMapSharedSources); VRTSource * -VRTParseFilterSources(CPLXMLNode *psTree, const char *, +VRTParseFilterSources(const CPLXMLNode *psTree, const char *, std::map &oMapSharedSources); VRTSource * -VRTParseArraySource(CPLXMLNode *psTree, const char *, +VRTParseArraySource(const CPLXMLNode *psTree, const char *, std::map &oMapSharedSources); /************************************************************************/ @@ -335,7 +335,7 @@ class CPL_DLL VRTDataset CPL_NON_FINAL : public GDALDataset char **papszOptions) override; virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath); - virtual CPLErr XMLInit(CPLXMLNode *, const char *); + virtual CPLErr XMLInit(const CPLXMLNode *, const char *); virtual CPLErr IBuildOverviews(const char *, int, const int *, int, const int *, GDALProgressFunc, void *, @@ -419,7 +419,7 @@ class CPL_DLL VRTWarpedDataset final : public VRTDataset const char *pszDomain = "") override; virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath) override; - virtual CPLErr XMLInit(CPLXMLNode *, const char *) override; + virtual CPLErr XMLInit(const CPLXMLNode *, const char *) override; virtual CPLErr AddBand(GDALDataType eType, char **papszOptions = nullptr) override; @@ -481,10 +481,10 @@ class VRTPansharpenedDataset final : public VRTDataset virtual CPLErr FlushCache(bool bAtClosing) override; - virtual CPLErr XMLInit(CPLXMLNode *, const char *) override; + virtual CPLErr XMLInit(const CPLXMLNode *, const char *) override; virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath) override; - CPLErr XMLInit(CPLXMLNode *psTree, const char *pszVRTPath, + CPLErr XMLInit(const CPLXMLNode *psTree, const char *pszVRTPath, GDALRasterBandH hPanchroBandIn, int nInputSpectralBandsIn, GDALRasterBandH *pahInputSpectralBandsIn); @@ -567,7 +567,7 @@ class CPL_DLL VRTRasterBand CPL_NON_FINAL : public GDALRasterBand VRTRasterBand(); virtual ~VRTRasterBand(); - virtual CPLErr XMLInit(CPLXMLNode *, const char *, + virtual CPLErr XMLInit(const CPLXMLNode *, const char *, std::map &); virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath, bool &bHasWarnedAboutRAMUsage, @@ -709,7 +709,7 @@ class CPL_DLL VRTSourcedRasterBand CPL_NON_FINAL : public VRTRasterBand virtual CPLErr SetMetadataItem(const char *pszName, const char *pszValue, const char *pszDomain = "") override; - virtual CPLErr XMLInit(CPLXMLNode *, const char *, + virtual CPLErr XMLInit(const CPLXMLNode *, const char *, std::map &) override; virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath, bool &bHasWarnedAboutRAMUsage, @@ -916,7 +916,7 @@ class CPL_DLL VRTDerivedRasterBand CPL_NON_FINAL : public VRTSourcedRasterBand void SetSourceTransferType(GDALDataType eDataType); void SetPixelFunctionLanguage(const char *pszLanguage); - virtual CPLErr XMLInit(CPLXMLNode *, const char *, + virtual CPLErr XMLInit(const CPLXMLNode *, const char *, std::map &) override; virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath, bool &bHasWarnedAboutRAMUsage, @@ -959,7 +959,7 @@ class CPL_DLL VRTRawRasterBand CPL_NON_FINAL : public VRTRasterBand GDALDataType eType = GDT_Unknown); virtual ~VRTRawRasterBand(); - virtual CPLErr XMLInit(CPLXMLNode *, const char *, + virtual CPLErr XMLInit(const CPLXMLNode *, const char *, std::map &) override; virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath, bool &bHasWarnedAboutRAMUsage, @@ -1010,7 +1010,7 @@ class VRTDriver final : public GDALDriver const char *pszDomain = "") override; VRTSource * - ParseSource(CPLXMLNode *psSrc, const char *pszVRTPath, + ParseSource(const CPLXMLNode *psSrc, const char *pszVRTPath, std::map &oMapSharedSources); void AddSourceParser(const char *pszElementName, VRTSourceParser pfnParser); }; @@ -1091,7 +1091,7 @@ class CPL_DLL VRTSimpleSource CPL_NON_FINAL : public VRTSource double dfYDstRatio); virtual ~VRTSimpleSource(); - virtual CPLErr XMLInit(CPLXMLNode *psTree, const char *, + virtual CPLErr XMLInit(const CPLXMLNode *psTree, const char *, std::map &) override; virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath) override; @@ -1240,7 +1240,7 @@ class VRTNoDataFromMaskSource final : public VRTSimpleSource void SetParameters(double dfNoDataValue, double dfMaskValueThreshold, double dfRemappedValue); - virtual CPLErr XMLInit(CPLXMLNode *psTree, const char *, + virtual CPLErr XMLInit(const CPLXMLNode *psTree, const char *, std::map &) override; virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath) override; virtual const char *GetType() override @@ -1332,7 +1332,7 @@ class CPL_DLL VRTComplexSource CPL_NON_FINAL : public VRTSimpleSource void *pProgressData) override; virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath) override; - virtual CPLErr XMLInit(CPLXMLNode *, const char *, + virtual CPLErr XMLInit(const CPLXMLNode *, const char *, std::map &) override; virtual const char *GetType() override { @@ -1415,7 +1415,7 @@ class VRTKernelFilteredSource CPL_NON_FINAL : public VRTFilteredSource VRTKernelFilteredSource(); virtual ~VRTKernelFilteredSource(); - virtual CPLErr XMLInit(CPLXMLNode *psTree, const char *, + virtual CPLErr XMLInit(const CPLXMLNode *psTree, const char *, std::map &) override; virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath) override; @@ -1438,7 +1438,7 @@ class VRTAverageFilteredSource final : public VRTKernelFilteredSource explicit VRTAverageFilteredSource(int nKernelSize); virtual ~VRTAverageFilteredSource(); - virtual CPLErr XMLInit(CPLXMLNode *psTree, const char *, + virtual CPLErr XMLInit(const CPLXMLNode *psTree, const char *, std::map &) override; virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath) override; }; @@ -1454,7 +1454,7 @@ class VRTFuncSource final : public VRTSource VRTFuncSource(); virtual ~VRTFuncSource(); - virtual CPLErr XMLInit(CPLXMLNode *, const char *, + virtual CPLErr XMLInit(const CPLXMLNode *, const char *, std::map &) override { return CE_Failure; diff --git a/frmts/vrt/vrtderivedrasterband.cpp b/frmts/vrt/vrtderivedrasterband.cpp index b69360ef58ad..7bf09532618b 100644 --- a/frmts/vrt/vrtderivedrasterband.cpp +++ b/frmts/vrt/vrtderivedrasterband.cpp @@ -1406,7 +1406,7 @@ int VRTDerivedRasterBand::IGetDataCoverageStatus( /************************************************************************/ CPLErr VRTDerivedRasterBand::XMLInit( - CPLXMLNode *psTree, const char *pszVRTPath, + const CPLXMLNode *psTree, const char *pszVRTPath, std::map &oMapSharedSources) { @@ -1457,10 +1457,11 @@ CPLErr VRTDerivedRasterBand::XMLInit( return CE_Failure; } - CPLXMLNode *psArgs = CPLGetXMLNode(psTree, "PixelFunctionArguments"); + const CPLXMLNode *const psArgs = + CPLGetXMLNode(psTree, "PixelFunctionArguments"); if (psArgs != nullptr) { - for (CPLXMLNode *psIter = psArgs->psChild; psIter != nullptr; + for (const CPLXMLNode *psIter = psArgs->psChild; psIter; psIter = psIter->psNext) { if (psIter->eType == CXT_Attribute) diff --git a/frmts/vrt/vrtdriver.cpp b/frmts/vrt/vrtdriver.cpp index 87068b12246c..2883944b4e13 100644 --- a/frmts/vrt/vrtdriver.cpp +++ b/frmts/vrt/vrtdriver.cpp @@ -136,7 +136,7 @@ void VRTDriver::AddSourceParser(const char *pszElementName, /************************************************************************/ VRTSource * -VRTDriver::ParseSource(CPLXMLNode *psSrc, const char *pszVRTPath, +VRTDriver::ParseSource(const CPLXMLNode *psSrc, const char *pszVRTPath, std::map &oMapSharedSources) { diff --git a/frmts/vrt/vrtfilters.cpp b/frmts/vrt/vrtfilters.cpp index dfd41b7a1137..3350afeff354 100644 --- a/frmts/vrt/vrtfilters.cpp +++ b/frmts/vrt/vrtfilters.cpp @@ -592,7 +592,7 @@ CPLErr VRTKernelFilteredSource::FilterData(int nXSize, int nYSize, /************************************************************************/ CPLErr VRTKernelFilteredSource::XMLInit( - CPLXMLNode *psTree, const char *pszVRTPath, + const CPLXMLNode *psTree, const char *pszVRTPath, std::map &oMapSharedSources) { @@ -705,7 +705,7 @@ CPLXMLNode *VRTKernelFilteredSource::SerializeToXML(const char *pszVRTPath) /************************************************************************/ VRTSource * -VRTParseFilterSources(CPLXMLNode *psChild, const char *pszVRTPath, +VRTParseFilterSources(const CPLXMLNode *psChild, const char *pszVRTPath, std::map &oMapSharedSources) { diff --git a/frmts/vrt/vrtmultidim.cpp b/frmts/vrt/vrtmultidim.cpp index d4305915a126..d4706b081659 100644 --- a/frmts/vrt/vrtmultidim.cpp +++ b/frmts/vrt/vrtmultidim.cpp @@ -2627,7 +2627,7 @@ class VRTArraySource : public VRTSource } CPLErr - XMLInit(CPLXMLNode *psTree, const char *pszVRTPath, + XMLInit(const CPLXMLNode *psTree, const char *pszVRTPath, std::map &oMapSharedSources) override; CPLXMLNode *SerializeToXML(const char *pszVRTPath) override; }; @@ -2706,7 +2706,7 @@ ParseSingleSourceArray(const CPLXMLNode *psSingleSourceArray, /************************************************************************/ CPLErr VRTArraySource::XMLInit( - CPLXMLNode *psTree, const char *pszVRTPath, + const CPLXMLNode *psTree, const char *pszVRTPath, std::map & /*oMapSharedSources*/) { const auto poArray = ParseArray(psTree, pszVRTPath, "ArraySource"); @@ -2978,7 +2978,7 @@ static std::shared_ptr ParseArray(const CPLXMLNode *psTree, /************************************************************************/ VRTSource * -VRTParseArraySource(CPLXMLNode *psChild, const char *pszVRTPath, +VRTParseArraySource(const CPLXMLNode *psChild, const char *pszVRTPath, std::map &oMapSharedSources) { VRTSource *poSource = nullptr; diff --git a/frmts/vrt/vrtpansharpened.cpp b/frmts/vrt/vrtpansharpened.cpp index cecf614d0710..e055848eb2fd 100644 --- a/frmts/vrt/vrtpansharpened.cpp +++ b/frmts/vrt/vrtpansharpened.cpp @@ -271,14 +271,14 @@ char **VRTPansharpenedDataset::GetFileList() /* XMLInit() */ /************************************************************************/ -CPLErr VRTPansharpenedDataset::XMLInit(CPLXMLNode *psTree, +CPLErr VRTPansharpenedDataset::XMLInit(const CPLXMLNode *psTree, const char *pszVRTPathIn) { return XMLInit(psTree, pszVRTPathIn, nullptr, 0, nullptr); } -CPLErr VRTPansharpenedDataset::XMLInit(CPLXMLNode *psTree, +CPLErr VRTPansharpenedDataset::XMLInit(const CPLXMLNode *psTree, const char *pszVRTPathIn, GDALRasterBandH hPanchroBandIn, int nInputSpectralBandsIn, @@ -299,7 +299,7 @@ CPLErr VRTPansharpenedDataset::XMLInit(CPLXMLNode *psTree, /* Parse PansharpeningOptions */ /* -------------------------------------------------------------------- */ - CPLXMLNode *psOptions = CPLGetXMLNode(psTree, "PansharpeningOptions"); + const CPLXMLNode *psOptions = CPLGetXMLNode(psTree, "PansharpeningOptions"); if (psOptions == nullptr) { CPLError(CE_Failure, CPLE_AppDefined, "Missing PansharpeningOptions"); @@ -325,7 +325,8 @@ CPLErr VRTPansharpenedDataset::XMLInit(CPLXMLNode *psTree, if (hPanchroBandIn == nullptr) { - CPLXMLNode *psPanchroBand = CPLGetXMLNode(psOptions, "PanchroBand"); + const CPLXMLNode *psPanchroBand = + CPLGetXMLNode(psOptions, "PanchroBand"); if (psPanchroBand == nullptr) { CPLError(CE_Failure, CPLE_AppDefined, "PanchroBand missing"); @@ -433,8 +434,8 @@ CPLErr VRTPansharpenedDataset::XMLInit(CPLXMLNode *psTree, } std::vector adfWeights; - CPLXMLNode *psAlgOptions = CPLGetXMLNode(psOptions, "AlgorithmOptions"); - if (psAlgOptions != nullptr) + if (const CPLXMLNode *psAlgOptions = + CPLGetXMLNode(psOptions, "AlgorithmOptions")) { const char *pszWeights = CPLGetXMLValue(psAlgOptions, "Weights", nullptr); @@ -499,7 +500,7 @@ CPLErr VRTPansharpenedDataset::XMLInit(CPLXMLNode *psTree, /* First pass on spectral datasets to check their georeferencing. */ /* -------------------------------------------------------------------- */ int iSpectralBand = 0; - for (CPLXMLNode *psIter = psOptions->psChild; psIter; + for (const CPLXMLNode *psIter = psOptions->psChild; psIter; psIter = psIter->psNext) { GDALDataset *poDataset; diff --git a/frmts/vrt/vrtrasterband.cpp b/frmts/vrt/vrtrasterband.cpp index 37bfd0c18312..c4f3df26d2bb 100644 --- a/frmts/vrt/vrtrasterband.cpp +++ b/frmts/vrt/vrtrasterband.cpp @@ -343,7 +343,7 @@ VRTParseColorTable(const CPLXMLNode *psColorTable) /************************************************************************/ CPLErr -VRTRasterBand::XMLInit(CPLXMLNode *psTree, const char *pszVRTPath, +VRTRasterBand::XMLInit(const CPLXMLNode *psTree, const char *pszVRTPath, std::map &oMapSharedSources) { @@ -481,20 +481,18 @@ VRTRasterBand::XMLInit(CPLXMLNode *psTree, const char *pszVRTPath, /* -------------------------------------------------------------------- */ /* Histograms */ /* -------------------------------------------------------------------- */ - CPLXMLNode *psHist = CPLGetXMLNode(psTree, "Histograms"); + const CPLXMLNode *psHist = CPLGetXMLNode(psTree, "Histograms"); if (psHist != nullptr) { - CPLXMLNode *psNext = psHist->psNext; - psHist->psNext = nullptr; - - m_psSavedHistograms = CPLCloneXMLTree(psHist); - psHist->psNext = psNext; + CPLXMLNode sHistTemp = *psHist; + sHistTemp.psNext = nullptr; + m_psSavedHistograms = CPLCloneXMLTree(&sHistTemp); } /* ==================================================================== */ /* Overviews */ /* ==================================================================== */ - CPLXMLNode *psNode = psTree->psChild; + const CPLXMLNode *psNode = psTree->psChild; for (; psNode != nullptr; psNode = psNode->psNext) { @@ -507,7 +505,8 @@ VRTRasterBand::XMLInit(CPLXMLNode *psTree, const char *pszVRTPath, /* Prepare filename. */ /* -------------------------------------------------------------------- */ - CPLXMLNode *psFileNameNode = CPLGetXMLNode(psNode, "SourceFilename"); + const CPLXMLNode *psFileNameNode = + CPLGetXMLNode(psNode, "SourceFilename"); const char *pszFilename = psFileNameNode ? CPLGetXMLValue(psFileNameNode, nullptr, nullptr) : nullptr; @@ -557,7 +556,7 @@ VRTRasterBand::XMLInit(CPLXMLNode *psTree, const char *pszVRTPath, /* ==================================================================== */ /* Mask band (specific to that raster band) */ /* ==================================================================== */ - CPLXMLNode *psMaskBandNode = CPLGetXMLNode(psTree, "MaskBand"); + const CPLXMLNode *psMaskBandNode = CPLGetXMLNode(psTree, "MaskBand"); if (psMaskBandNode) psNode = psMaskBandNode->psChild; else diff --git a/frmts/vrt/vrtrawrasterband.cpp b/frmts/vrt/vrtrawrasterband.cpp index 0c7086effe10..e5c25753a518 100644 --- a/frmts/vrt/vrtrawrasterband.cpp +++ b/frmts/vrt/vrtrawrasterband.cpp @@ -344,7 +344,7 @@ CPLVirtualMem *VRTRawRasterBand::GetVirtualMemAuto(GDALRWFlag eRWFlag, /************************************************************************/ CPLErr -VRTRawRasterBand::XMLInit(CPLXMLNode *psTree, const char *pszVRTPath, +VRTRawRasterBand::XMLInit(const CPLXMLNode *psTree, const char *pszVRTPath, std::map &oMapSharedSources) { diff --git a/frmts/vrt/vrtsourcedrasterband.cpp b/frmts/vrt/vrtsourcedrasterband.cpp index 0067f27abfac..b790c1a7e129 100644 --- a/frmts/vrt/vrtsourcedrasterband.cpp +++ b/frmts/vrt/vrtsourcedrasterband.cpp @@ -1813,7 +1813,7 @@ CPLErr CPL_STDCALL VRTAddSource(VRTSourcedRasterBandH hVRTBand, /************************************************************************/ CPLErr VRTSourcedRasterBand::XMLInit( - CPLXMLNode *psTree, const char *pszVRTPath, + const CPLXMLNode *psTree, const char *pszVRTPath, std::map &oMapSharedSources) { @@ -1830,7 +1830,7 @@ CPLErr VRTSourcedRasterBand::XMLInit( VRTDriver *const poDriver = static_cast(GDALGetDriverByName("VRT")); - for (CPLXMLNode *psChild = psTree->psChild; + for (const CPLXMLNode *psChild = psTree->psChild; psChild != nullptr && poDriver != nullptr; psChild = psChild->psNext) { if (psChild->eType != CXT_Element) diff --git a/frmts/vrt/vrtsources.cpp b/frmts/vrt/vrtsources.cpp index 722d9e36a66c..f3a090bed7a9 100644 --- a/frmts/vrt/vrtsources.cpp +++ b/frmts/vrt/vrtsources.cpp @@ -512,7 +512,7 @@ CPLXMLNode *VRTSimpleSource::SerializeToXML(const char *pszVRTPath) /************************************************************************/ CPLErr -VRTSimpleSource::XMLInit(CPLXMLNode *psSrc, const char *pszVRTPath, +VRTSimpleSource::XMLInit(const CPLXMLNode *psSrc, const char *pszVRTPath, std::map &oMapSharedSources) { @@ -523,7 +523,8 @@ VRTSimpleSource::XMLInit(CPLXMLNode *psSrc, const char *pszVRTPath, /* -------------------------------------------------------------------- */ /* Prepare filename. */ /* -------------------------------------------------------------------- */ - CPLXMLNode *psSourceFileNameNode = CPLGetXMLNode(psSrc, "SourceFilename"); + const CPLXMLNode *psSourceFileNameNode = + CPLGetXMLNode(psSrc, "SourceFilename"); const char *pszFilename = psSourceFileNameNode ? CPLGetXMLValue(psSourceFileNameNode, nullptr, "") : ""; @@ -2019,7 +2020,7 @@ VRTNoDataFromMaskSource::VRTNoDataFromMaskSource() /************************************************************************/ CPLErr VRTNoDataFromMaskSource::XMLInit( - CPLXMLNode *psSrc, const char *pszVRTPath, + const CPLXMLNode *psSrc, const char *pszVRTPath, std::map &oMapSharedSources) { @@ -2033,10 +2034,10 @@ CPLErr VRTNoDataFromMaskSource::XMLInit( return eErr; } - if (CPLGetXMLValue(psSrc, "NODATA", nullptr) != nullptr) + if (const char *pszNODATA = CPLGetXMLValue(psSrc, "NODATA", nullptr)) { m_bNoDataSet = true; - m_dfNoDataValue = CPLAtofM(CPLGetXMLValue(psSrc, "NODATA", "0")); + m_dfNoDataValue = CPLAtofM(pszNODATA); } m_dfMaskValueThreshold = @@ -2628,7 +2629,7 @@ CPLXMLNode *VRTComplexSource::SerializeToXML(const char *pszVRTPath) /************************************************************************/ CPLErr -VRTComplexSource::XMLInit(CPLXMLNode *psSrc, const char *pszVRTPath, +VRTComplexSource::XMLInit(const CPLXMLNode *psSrc, const char *pszVRTPath, std::map &oMapSharedSources) { @@ -2645,12 +2646,15 @@ VRTComplexSource::XMLInit(CPLXMLNode *psSrc, const char *pszVRTPath, /* -------------------------------------------------------------------- */ /* Complex parameters. */ /* -------------------------------------------------------------------- */ - if (CPLGetXMLValue(psSrc, "ScaleOffset", nullptr) != nullptr || - CPLGetXMLValue(psSrc, "ScaleRatio", nullptr) != nullptr) + const char *pszScaleOffset = CPLGetXMLValue(psSrc, "ScaleOffset", nullptr); + const char *pszScaleRatio = CPLGetXMLValue(psSrc, "ScaleRatio", nullptr); + if (pszScaleOffset || pszScaleRatio) { m_nProcessingFlags |= PROCESSING_FLAG_SCALING_LINEAR; - m_dfScaleOff = CPLAtof(CPLGetXMLValue(psSrc, "ScaleOffset", "0")); - m_dfScaleRatio = CPLAtof(CPLGetXMLValue(psSrc, "ScaleRatio", "1")); + if (pszScaleOffset) + m_dfScaleOff = CPLAtof(pszScaleOffset); + if (pszScaleRatio) + m_dfScaleRatio = CPLAtof(pszScaleRatio); } else if (CPLGetXMLValue(psSrc, "Exponent", nullptr) != nullptr && CPLGetXMLValue(psSrc, "DstMin", nullptr) != nullptr && @@ -2672,10 +2676,10 @@ VRTComplexSource::XMLInit(CPLXMLNode *psSrc, const char *pszVRTPath, m_dfDstMax = CPLAtof(CPLGetXMLValue(psSrc, "DstMax", "0.0")); } - if (CPLGetXMLValue(psSrc, "NODATA", nullptr) != nullptr) + if (const char *pszNODATA = CPLGetXMLValue(psSrc, "NODATA", nullptr)) { m_nProcessingFlags |= PROCESSING_FLAG_NODATA; - m_osNoDataValueOri = CPLGetXMLValue(psSrc, "NODATA", "0"); + m_osNoDataValueOri = pszNODATA; m_dfNoDataValue = CPLAtofM(m_osNoDataValueOri.c_str()); } @@ -3649,7 +3653,7 @@ CPLErr VRTFuncSource::GetHistogram( /************************************************************************/ VRTSource * -VRTParseCoreSources(CPLXMLNode *psChild, const char *pszVRTPath, +VRTParseCoreSources(const CPLXMLNode *psChild, const char *pszVRTPath, std::map &oMapSharedSources) { diff --git a/frmts/vrt/vrtwarped.cpp b/frmts/vrt/vrtwarped.cpp index 84a95d63e8ac..642241b7392b 100644 --- a/frmts/vrt/vrtwarped.cpp +++ b/frmts/vrt/vrtwarped.cpp @@ -1283,7 +1283,8 @@ CPLErr CPL_STDCALL GDALInitializeWarpedVRT(GDALDatasetH hDS, /* XMLInit() */ /************************************************************************/ -CPLErr VRTWarpedDataset::XMLInit(CPLXMLNode *psTree, const char *pszVRTPathIn) +CPLErr VRTWarpedDataset::XMLInit(const CPLXMLNode *psTree, + const char *pszVRTPathIn) { @@ -1330,7 +1331,8 @@ CPLErr VRTWarpedDataset::XMLInit(CPLXMLNode *psTree, const char *pszVRTPathIn) /* -------------------------------------------------------------------- */ /* Find the GDALWarpOptions XML tree. */ /* -------------------------------------------------------------------- */ - CPLXMLNode *const psOptionsTree = CPLGetXMLNode(psTree, "GDALWarpOptions"); + const CPLXMLNode *const psOptionsTree = + CPLGetXMLNode(psTree, "GDALWarpOptions"); if (psOptionsTree == nullptr) { CPLError(CE_Failure, CPLE_AppDefined, @@ -1355,14 +1357,16 @@ CPLErr VRTWarpedDataset::XMLInit(CPLXMLNode *psTree, const char *pszVRTPathIn) else pszAbsolutePath = CPLStrdup(pszRelativePath); - CPLSetXMLValue(psOptionsTree, "SourceDataset", pszAbsolutePath); + CPLXMLNode *psOptionsTreeCloned = CPLCloneXMLTree(psOptionsTree); + CPLSetXMLValue(psOptionsTreeCloned, "SourceDataset", pszAbsolutePath); CPLFree(pszAbsolutePath); /* -------------------------------------------------------------------- */ /* And instantiate the warp options, and corresponding warp */ /* operation. */ /* -------------------------------------------------------------------- */ - GDALWarpOptions *psWO = GDALDeserializeWarpOptions(psOptionsTree); + GDALWarpOptions *psWO = GDALDeserializeWarpOptions(psOptionsTreeCloned); + CPLDestroyXMLNode(psOptionsTreeCloned); if (psWO == nullptr) return CE_Failure; From e2743637a1856ef5397dfb6c9cf767ce74f33a33 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Tue, 20 Feb 2024 15:58:50 +0100 Subject: [PATCH 3/3] GDALPamDataset/GDALPamRasterBand: make XMLInit() accept a const CPLXMLNode* argument --- gcore/gdal_pam.h | 4 ++-- gcore/gdalpamdataset.cpp | 48 ++++++++++++++++--------------------- gcore/gdalpamrasterband.cpp | 43 +++++++++++++-------------------- 3 files changed, 40 insertions(+), 55 deletions(-) diff --git a/gcore/gdal_pam.h b/gcore/gdal_pam.h index b40ae21826a7..73ae1e25a939 100644 --- a/gcore/gdal_pam.h +++ b/gcore/gdal_pam.h @@ -129,7 +129,7 @@ class CPL_DLL GDALPamDataset : public GDALDataset GDALDatasetPamInfo *psPam = nullptr; virtual CPLXMLNode *SerializeToXML(const char *); - virtual CPLErr XMLInit(CPLXMLNode *, const char *); + virtual CPLErr XMLInit(const CPLXMLNode *, const char *); virtual CPLErr TryLoadXML(char **papszSiblingFiles = nullptr); virtual CPLErr TrySaveXML(); @@ -278,7 +278,7 @@ class CPL_DLL GDALPamRasterBand : public GDALRasterBand protected: //! @cond Doxygen_Suppress virtual CPLXMLNode *SerializeToXML(const char *pszVRTPath); - virtual CPLErr XMLInit(CPLXMLNode *, const char *); + virtual CPLErr XMLInit(const CPLXMLNode *, const char *); void PamInitialize(); void PamClear(); diff --git a/gcore/gdalpamdataset.cpp b/gcore/gdalpamdataset.cpp index e672bef224fc..1f0c326762e1 100644 --- a/gcore/gdalpamdataset.cpp +++ b/gcore/gdalpamdataset.cpp @@ -420,14 +420,13 @@ void GDALPamDataset::PamClear() /* XMLInit() */ /************************************************************************/ -CPLErr GDALPamDataset::XMLInit(CPLXMLNode *psTree, const char *pszUnused) +CPLErr GDALPamDataset::XMLInit(const CPLXMLNode *psTree, const char *pszUnused) { /* -------------------------------------------------------------------- */ /* Check for an SRS node. */ /* -------------------------------------------------------------------- */ - CPLXMLNode *psSRSNode = CPLGetXMLNode(psTree, "SRS"); - if (psSRSNode) + if (const CPLXMLNode *psSRSNode = CPLGetXMLNode(psTree, "SRS")) { if (psPam->poSRS) psPam->poSRS->Release(); @@ -463,12 +462,12 @@ CPLErr GDALPamDataset::XMLInit(CPLXMLNode *psTree, const char *pszUnused) /* -------------------------------------------------------------------- */ /* Check for a GeoTransform node. */ /* -------------------------------------------------------------------- */ - if (strlen(CPLGetXMLValue(psTree, "GeoTransform", "")) > 0) + const char *pszGT = CPLGetXMLValue(psTree, "GeoTransform", ""); + if (strlen(pszGT) > 0) { - const char *pszGT = CPLGetXMLValue(psTree, "GeoTransform", ""); - - char **papszTokens = CSLTokenizeStringComplex(pszGT, ",", FALSE, FALSE); - if (CSLCount(papszTokens) != 6) + const CPLStringList aosTokens( + CSLTokenizeStringComplex(pszGT, ",", FALSE, FALSE)); + if (aosTokens.size() != 6) { CPLError(CE_Warning, CPLE_AppDefined, "GeoTransform node does not have expected six values."); @@ -476,19 +475,15 @@ CPLErr GDALPamDataset::XMLInit(CPLXMLNode *psTree, const char *pszUnused) else { for (int iTA = 0; iTA < 6; iTA++) - psPam->adfGeoTransform[iTA] = CPLAtof(papszTokens[iTA]); + psPam->adfGeoTransform[iTA] = CPLAtof(aosTokens[iTA]); psPam->bHaveGeoTransform = TRUE; } - - CSLDestroy(papszTokens); } /* -------------------------------------------------------------------- */ /* Check for GCPs. */ /* -------------------------------------------------------------------- */ - CPLXMLNode *psGCPList = CPLGetXMLNode(psTree, "GCPList"); - - if (psGCPList != nullptr) + if (const CPLXMLNode *psGCPList = CPLGetXMLNode(psTree, "GCPList")) { if (psPam->poGCP_SRS) psPam->poGCP_SRS->Release(); @@ -527,8 +522,9 @@ CPLErr GDALPamDataset::XMLInit(CPLXMLNode *psTree, const char *pszUnused) // over the root PAMDataset SRS node. // ArcGIS 9.3: GeodataXform as a root element - CPLXMLNode *psGeodataXform = CPLGetXMLNode(psTree, "=GeodataXform"); - CPLXMLNode *psValueAsXML = nullptr; + const CPLXMLNode *psGeodataXform = + CPLGetXMLNode(psTree, "=GeodataXform"); + CPLXMLTreeCloser oTreeValueAsXML(nullptr); if (psGeodataXform != nullptr) { char *apszMD[2]; @@ -543,10 +539,10 @@ CPLErr GDALPamDataset::XMLInit(CPLXMLNode *psTree, const char *pszUnused) char **papszXML = oMDMD.GetMetadata("xml:ESRI"); if (CSLCount(papszXML) == 1) { - psValueAsXML = CPLParseXMLString(papszXML[0]); - if (psValueAsXML) + oTreeValueAsXML.reset(CPLParseXMLString(papszXML[0])); + if (oTreeValueAsXML) psGeodataXform = - CPLGetXMLNode(psValueAsXML, "=GeodataXform"); + CPLGetXMLNode(oTreeValueAsXML.get(), "=GeodataXform"); } } @@ -686,14 +682,12 @@ CPLErr GDALPamDataset::XMLInit(CPLXMLNode *psTree, const char *pszUnused) } } } - if (psValueAsXML) - CPLDestroyXMLNode(psValueAsXML); } /* -------------------------------------------------------------------- */ /* Process bands. */ /* -------------------------------------------------------------------- */ - for (CPLXMLNode *psBandTree = psTree->psChild; psBandTree != nullptr; + for (const CPLXMLNode *psBandTree = psTree->psChild; psBandTree; psBandTree = psBandTree->psNext) { if (psBandTree->eType != CXT_Element || @@ -719,16 +713,16 @@ CPLErr GDALPamDataset::XMLInit(CPLXMLNode *psTree, const char *pszUnused) /* -------------------------------------------------------------------- */ /* Preserve Array information. */ /* -------------------------------------------------------------------- */ - for (CPLXMLNode *psIter = psTree->psChild; psIter; psIter = psIter->psNext) + for (const CPLXMLNode *psIter = psTree->psChild; psIter; + psIter = psIter->psNext) { if (psIter->eType == CXT_Element && strcmp(psIter->pszValue, "Array") == 0) { - CPLXMLNode *psNextBackup = psIter->psNext; - psIter->psNext = nullptr; + CPLXMLNode sArrayTmp = *psIter; + sArrayTmp.psNext = nullptr; psPam->m_apoOtherNodes.emplace_back( - CPLXMLTreeCloser(CPLCloneXMLTree(psIter))); - psIter->psNext = psNextBackup; + CPLXMLTreeCloser(CPLCloneXMLTree(&sArrayTmp))); } } diff --git a/gcore/gdalpamrasterband.cpp b/gcore/gdalpamrasterband.cpp index 880452399310..a75f1cc3df70 100644 --- a/gcore/gdalpamrasterband.cpp +++ b/gcore/gdalpamrasterband.cpp @@ -372,7 +372,7 @@ void GDALPamRasterBand::PamClear() /* XMLInit() */ /************************************************************************/ -CPLErr GDALPamRasterBand::XMLInit(CPLXMLNode *psTree, +CPLErr GDALPamRasterBand::XMLInit(const CPLXMLNode *psTree, const char * /* pszUnused */) { PamInitialize(); @@ -387,8 +387,8 @@ CPLErr GDALPamRasterBand::XMLInit(CPLXMLNode *psTree, /* -------------------------------------------------------------------- */ GDALMajorObject::SetDescription(CPLGetXMLValue(psTree, "Description", "")); - const char *pszNoDataValue = CPLGetXMLValue(psTree, "NoDataValue", nullptr); - if (pszNoDataValue != nullptr) + if (const char *pszNoDataValue = + CPLGetXMLValue(psTree, "NoDataValue", nullptr)) { const char *pszLEHex = CPLGetXMLValue(psTree, "NoDataValue.le_hex_equiv", nullptr); @@ -436,12 +436,10 @@ CPLErr GDALPamRasterBand::XMLInit(CPLXMLNode *psTree, GDALPamRasterBand::SetScale(pszScale ? CPLAtof(pszScale) : 1.0); } - const char *pszUnitType = CPLGetXMLValue(psTree, "UnitType", nullptr); - if (pszUnitType) + if (const char *pszUnitType = CPLGetXMLValue(psTree, "UnitType", nullptr)) GDALPamRasterBand::SetUnitType(pszUnitType); - const char *pszInterp = CPLGetXMLValue(psTree, "ColorInterp", nullptr); - if (pszInterp) + if (const char *pszInterp = CPLGetXMLValue(psTree, "ColorInterp", nullptr)) { GDALPamRasterBand::SetColorInterpretation( GDALGetColorInterpretationByName(pszInterp)); @@ -450,12 +448,11 @@ CPLErr GDALPamRasterBand::XMLInit(CPLXMLNode *psTree, /* -------------------------------------------------------------------- */ /* Category names. */ /* -------------------------------------------------------------------- */ - const auto psCategoryNames = CPLGetXMLNode(psTree, "CategoryNames"); - if (psCategoryNames) + if (const auto psCategoryNames = CPLGetXMLNode(psTree, "CategoryNames")) { CPLStringList oCategoryNames; - for (CPLXMLNode *psEntry = psCategoryNames->psChild; psEntry != nullptr; + for (const CPLXMLNode *psEntry = psCategoryNames->psChild; psEntry; psEntry = psEntry->psNext) { /* Don't skip tag with empty content */ @@ -475,13 +472,12 @@ CPLErr GDALPamRasterBand::XMLInit(CPLXMLNode *psTree, /* -------------------------------------------------------------------- */ /* Collect a color table. */ /* -------------------------------------------------------------------- */ - const auto psColorTable = CPLGetXMLNode(psTree, "ColorTable"); - if (psColorTable) + if (const auto psColorTable = CPLGetXMLNode(psTree, "ColorTable")) { GDALColorTable oTable; int iEntry = 0; - for (CPLXMLNode *psEntry = psColorTable->psChild; psEntry != nullptr; + for (const CPLXMLNode *psEntry = psColorTable->psChild; psEntry; psEntry = psEntry->psNext) { if (!(psEntry->eType == CXT_Element && @@ -505,8 +501,7 @@ CPLErr GDALPamRasterBand::XMLInit(CPLXMLNode *psTree, /* -------------------------------------------------------------------- */ /* Do we have a complete set of stats? */ /* -------------------------------------------------------------------- */ - const char *pszMinimum = CPLGetXMLValue(psTree, "Minimum", nullptr); - if (pszMinimum) + if (const char *pszMinimum = CPLGetXMLValue(psTree, "Minimum", nullptr)) { const char *pszMaximum = CPLGetXMLValue(psTree, "Maximum", nullptr); if (pszMaximum) @@ -517,8 +512,7 @@ CPLErr GDALPamRasterBand::XMLInit(CPLXMLNode *psTree, } } - const char *pszMean = CPLGetXMLValue(psTree, "Mean", nullptr); - if (pszMean) + if (const char *pszMean = CPLGetXMLValue(psTree, "Mean", nullptr)) { const char *pszStandardDeviation = CPLGetXMLValue(psTree, "StandardDeviation", nullptr); @@ -533,26 +527,23 @@ CPLErr GDALPamRasterBand::XMLInit(CPLXMLNode *psTree, /* -------------------------------------------------------------------- */ /* Histograms */ /* -------------------------------------------------------------------- */ - CPLXMLNode *psHist = CPLGetXMLNode(psTree, "Histograms"); - if (psHist != nullptr) + if (const CPLXMLNode *psHist = CPLGetXMLNode(psTree, "Histograms")) { - CPLXMLNode *psNext = psHist->psNext; - psHist->psNext = nullptr; - + CPLXMLNode sHistTemp = *psHist; + sHistTemp.psNext = nullptr; if (psPam->psSavedHistograms != nullptr) { CPLDestroyXMLNode(psPam->psSavedHistograms); psPam->psSavedHistograms = nullptr; } - psPam->psSavedHistograms = CPLCloneXMLTree(psHist); - psHist->psNext = psNext; + psPam->psSavedHistograms = CPLCloneXMLTree(&sHistTemp); } /* -------------------------------------------------------------------- */ /* Raster Attribute Table */ /* -------------------------------------------------------------------- */ - CPLXMLNode *psRAT = CPLGetXMLNode(psTree, "GDALRasterAttributeTable"); - if (psRAT != nullptr) + if (const CPLXMLNode *psRAT = + CPLGetXMLNode(psTree, "GDALRasterAttributeTable")) { delete psPam->poDefaultRAT; auto poNewRAT = new GDALDefaultRasterAttributeTable();