Skip to content

Commit

Permalink
GDALPamDataset/GDALPamRasterBand: make XMLInit() accept a const CPLXM…
Browse files Browse the repository at this point in the history
…LNode* argument
  • Loading branch information
rouault committed Feb 20, 2024
1 parent a062a06 commit cd177b2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 57 deletions.
4 changes: 2 additions & 2 deletions gcore/gdal_pam.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
50 changes: 23 additions & 27 deletions gcore/gdalpamdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -463,32 +462,28 @@ 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.");
}
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();
Expand Down Expand Up @@ -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];
Expand All @@ -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");
}
}

Expand Down Expand Up @@ -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 != nullptr;
psBandTree = psBandTree->psNext)
{
if (psBandTree->eType != CXT_Element ||
Expand All @@ -719,16 +713,18 @@ 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 = {.eType = psIter->eType,
.pszValue = psIter->pszValue,
.psNext = nullptr,
.psChild = psIter->psChild};
psPam->m_apoOtherNodes.emplace_back(
CPLXMLTreeCloser(CPLCloneXMLTree(psIter)));
psIter->psNext = psNextBackup;
CPLXMLTreeCloser(CPLCloneXMLTree(&sArrayTmp)));
}
}

Expand Down
51 changes: 23 additions & 28 deletions gcore/gdalpamrasterband.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ void GDALPamRasterBand::PamClear()
/* XMLInit() */
/************************************************************************/

CPLErr GDALPamRasterBand::XMLInit(CPLXMLNode *psTree,
CPLErr GDALPamRasterBand::XMLInit(const CPLXMLNode *psTree,
const char * /* pszUnused */)
{
PamInitialize();
Expand All @@ -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);
Expand Down Expand Up @@ -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));
Expand All @@ -450,13 +448,12 @@ 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;
psEntry = psEntry->psNext)
for (const CPLXMLNode *psEntry = psCategoryNames->psChild;
psEntry != nullptr; psEntry = psEntry->psNext)
{
/* Don't skip <Category> tag with empty content */
if (psEntry->eType != CXT_Element ||
Expand All @@ -475,14 +472,13 @@ 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;
psEntry = psEntry->psNext)
for (const CPLXMLNode *psEntry = psColorTable->psChild;
psEntry != nullptr; psEntry = psEntry->psNext)
{
if (!(psEntry->eType == CXT_Element &&
EQUAL(psEntry->pszValue, "Entry")))
Expand All @@ -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)
Expand All @@ -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);
Expand All @@ -533,26 +527,27 @@ 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 = {
.eType = psHist->eType,
.pszValue = psHist->pszValue,
.psNext = nullptr,
.psChild = psHist->psChild,
};
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();
Expand Down

0 comments on commit cd177b2

Please sign in to comment.