Skip to content

Commit

Permalink
frmts/: manual replacement of last uses of CPLGetExtension() by CPLGe…
Browse files Browse the repository at this point in the history
…tExtensionSafe()
  • Loading branch information
rouault committed Jan 11, 2025
1 parent fb1f6a5 commit 7474f93
Show file tree
Hide file tree
Showing 30 changed files with 93 additions and 85 deletions.
7 changes: 4 additions & 3 deletions frmts/adrg/adrgdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,8 @@ char **ADRGDataset::GetGENListFromTHF(const char *pszFileName)
char *c = (char *)strchr(osSubFileName.c_str(), ' ');
if (c)
*c = 0;
if (EQUAL(CPLGetExtension(osSubFileName.c_str()), "GEN"))
if (EQUAL(CPLGetExtensionSafe(osSubFileName.c_str()).c_str(),
"GEN"))
{
CPLDebug("ADRG", "Found GEN file in THF : %s",
osSubFileName.c_str());
Expand Down Expand Up @@ -1551,7 +1552,7 @@ GDALDataset *ADRGDataset::Open(GDALOpenInfo *poOpenInfo)
return nullptr;

CPLString osFileName(poOpenInfo->pszFilename);
if (EQUAL(CPLGetExtension(osFileName.c_str()), "THF"))
if (EQUAL(CPLGetExtensionSafe(osFileName.c_str()).c_str(), "THF"))
{
char **papszFileNames = GetGENListFromTHF(osFileName.c_str());
if (papszFileNames == nullptr)
Expand Down Expand Up @@ -1583,7 +1584,7 @@ GDALDataset *ADRGDataset::Open(GDALOpenInfo *poOpenInfo)
}
}

if (EQUAL(CPLGetExtension(osFileName.c_str()), "GEN"))
if (EQUAL(CPLGetExtensionSafe(osFileName.c_str()).c_str(), "GEN"))
{
osGENFileName = osFileName;

Expand Down
12 changes: 7 additions & 5 deletions frmts/adrg/srpdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,8 @@ char **SRPDataset::GetGENListFromTHF(const char *pszFileName)
{
while (*ptrDir)
{
if (EQUAL(CPLGetExtension(*ptrDir), "GEN"))
if (EQUAL(CPLGetExtensionSafe(*ptrDir).c_str(),
"GEN"))
{
bFound = 1;
osGENFileName = CPLFormFilename(
Expand All @@ -1160,7 +1161,8 @@ char **SRPDataset::GetGENListFromTHF(const char *pszFileName)
{
while (*ptrDir)
{
if (EQUAL(CPLGetExtension(*ptrDir), "GEN") &&
if (EQUAL(CPLGetExtensionSafe(*ptrDir).c_str(),
"GEN") &&
EQUALN(CPLGetBasename(*ptrDir), osName, 6))
{
bFound = 1;
Expand Down Expand Up @@ -1480,7 +1482,7 @@ GDALDataset *SRPDataset::Open(GDALOpenInfo *poOpenInfo)
return nullptr;
CPLString osFileName(poOpenInfo->pszFilename);

if (EQUAL(CPLGetExtension(osFileName.c_str()), "THF"))
if (EQUAL(CPLGetExtensionSafe(osFileName.c_str()).c_str(), "THF"))
{

CPLDebug("SRP", "Read THF");
Expand Down Expand Up @@ -1521,7 +1523,7 @@ GDALDataset *SRPDataset::Open(GDALOpenInfo *poOpenInfo)

if (bTHFWithSingleGEN
#ifdef OPEN_GEN
|| EQUAL(CPLGetExtension(osFileName.c_str()), "GEN")
|| EQUAL(CPLGetExtensionSafe(osFileName.c_str()).c_str(), "GEN")
#endif
)
{
Expand Down Expand Up @@ -1550,7 +1552,7 @@ GDALDataset *SRPDataset::Open(GDALOpenInfo *poOpenInfo)
}
}

if (EQUAL(CPLGetExtension(osFileName.c_str()), "IMG"))
if (EQUAL(CPLGetExtensionSafe(osFileName.c_str()).c_str(), "IMG"))
{

osIMGFileName = osFileName;
Expand Down
8 changes: 4 additions & 4 deletions frmts/aigrid/aigdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,8 +659,8 @@ GDALDataset *AIGDataset::Open(GDALOpenInfo *poOpenInfo)
for (int iFile = 0; papszFiles != nullptr && papszFiles[iFile] != nullptr;
iFile++)
{
if (!EQUAL(CPLGetExtension(papszFiles[iFile]), "clr") &&
!EQUAL(CPLGetExtension(papszFiles[iFile]), "CLR"))
const std::string osExt = CPLGetExtensionSafe(papszFiles[iFile]);
if (!EQUAL(osExt.c_str(), "clr") && !EQUAL(osExt.c_str(), "CLR"))
continue;

osClrFilename =
Expand Down Expand Up @@ -874,12 +874,12 @@ static CPLErr AIGRename(const char *pszNewName, const char *pszOldName)
/* -------------------------------------------------------------------- */
CPLString osOldPath, osNewPath;

if (strlen(CPLGetExtension(pszNewName)) > 0)
if (!CPLGetExtensionSafe(pszNewName).empty())
osNewPath = CPLGetPath(pszNewName);
else
osNewPath = pszNewName;

if (strlen(CPLGetExtension(pszOldName)) > 0)
if (!CPLGetExtensionSafe(pszOldName).empty())
osOldPath = CPLGetPath(pszOldName);
else
osOldPath = pszOldName;
Expand Down
4 changes: 2 additions & 2 deletions frmts/bsb/bsbdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,11 +568,11 @@ void BSBDataset::ScanForGCPs(bool isNos, const char *pszFilename)

void BSBDataset::ScanForGCPsNos(const char *pszFilename)
{
const char *extension = CPLGetExtension(pszFilename);
const std::string extension = CPLGetExtensionSafe(pszFilename);

// pseudointelligently try and guess whether we want a .geo or a .GEO
const char *geofile = nullptr;
if (extension[1] == 'O')
if (extension.size() >= 2 && extension[1] == 'O')
{
geofile = CPLResetExtension(pszFilename, "GEO");
}
Expand Down
3 changes: 2 additions & 1 deletion frmts/ceos2/sar_ceosdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1824,7 +1824,8 @@ GDALDataset *SAR_CEOSDataset::Open(GDALOpenInfo *poOpenInfo)
/* -------------------------------------------------------------------- */
char *pszPath = CPLStrdup(CPLGetPath(poOpenInfo->pszFilename));
char *pszBasename = CPLStrdup(CPLGetBasename(poOpenInfo->pszFilename));
char *pszExtension = CPLStrdup(CPLGetExtension(poOpenInfo->pszFilename));
char *pszExtension =
CPLStrdup(CPLGetExtensionSafe(poOpenInfo->pszFilename).c_str());

int nBand;
if (strlen(pszBasename) > 4)
Expand Down
5 changes: 3 additions & 2 deletions frmts/ecw/gdal_ecw.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,11 @@ class VSIIOStream final : public CNCSJPCIOStream
{
osFilenameUsed = CPLGenerateTempFilename(nullptr);
// try to preserve the extension.
if (strlen(CPLGetExtension(pszFilename)) > 0)
const auto osExt = CPLGetExtensionSafe(pszFilename);
if (!osExt.empty())
{
osFilenameUsed += ".";
osFilenameUsed += CPLGetExtension(pszFilename);
osFilenameUsed += osExt;
}
CPLDebug("ECW",
"Using filename '%s' for temporary directory "
Expand Down
2 changes: 1 addition & 1 deletion frmts/gtiff/gtiffdataset_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6586,7 +6586,7 @@ void GTiffDataset::LoadMetadata()
return;
m_bIMDRPCMetadataLoaded = true;

if (EQUAL(CPLGetExtension(GetDescription()), "ovr"))
if (EQUAL(CPLGetExtensionSafe(GetDescription()).c_str(), "ovr"))
{
// Do not attempt to retrieve metadata files on .tif.ovr files.
// For example the Pleiades metadata reader might wrongly associate a
Expand Down
2 changes: 1 addition & 1 deletion frmts/hdf5/hdf5drivercore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ int HDF5DatasetIdentify(GDALOpenInfo *poOpenInfo)
if (!poOpenInfo->pabyHeader)
return FALSE;

const CPLString osExt(CPLGetExtension(poOpenInfo->pszFilename));
const CPLString osExt(poOpenInfo->osExtension);

const auto IsRecognizedByNetCDFDriver = [&osExt, poOpenInfo]()
{
Expand Down
3 changes: 2 additions & 1 deletion frmts/hfa/hfaband.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ CPLErr HFABand::LoadOverviews()
HFAEntry *poBandProxyNode = poNode;
HFAInfo_t *psOvHFA = psInfo;

if (nOverviews == 0 && EQUAL(CPLGetExtension(psInfo->pszFilename), "aux"))
if (nOverviews == 0 &&
EQUAL(CPLGetExtensionSafe(psInfo->pszFilename).c_str(), "aux"))
{
const CPLString osRRDFilename =
CPLResetExtension(psInfo->pszFilename, "rrd");
Expand Down
9 changes: 5 additions & 4 deletions frmts/hfa/hfaopen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1873,7 +1873,7 @@ HFAHandle HFACreateLL(const char *pszFilename)

// If an .ige or .rrd file exists with the same base name,
// delete them. (#1784)
CPLString osExtension = CPLGetExtension(pszFilename);
CPLString osExtension = CPLGetExtensionSafe(pszFilename);
if (!EQUAL(osExtension, "rrd") && !EQUAL(osExtension, "aux"))
{
CPLString osPath = CPLGetPath(pszFilename);
Expand Down Expand Up @@ -2906,7 +2906,7 @@ const char *HFAGetIGEFilename(HFAHandle hHFA)
if (VSIStatL(osFullFilename, &sStatBuf) != 0)
{
const CPLString osExtension =
CPLGetExtension(pszRawFilename);
CPLGetExtensionSafe(pszRawFilename);
const CPLString osBasename =
CPLGetBasename(hHFA->pszFilename);
osFullFilename =
Expand Down Expand Up @@ -2955,10 +2955,11 @@ bool HFACreateSpillStack(HFAInfo_t *psInfo, int nXSize, int nYSize, int nLayers,

if (psInfo->pszIGEFilename == nullptr)
{
if (EQUAL(CPLGetExtension(psInfo->pszFilename), "rrd"))
const auto osExt = CPLGetExtensionSafe(psInfo->pszFilename);
if (EQUAL(osExt.c_str(), "rrd"))
psInfo->pszIGEFilename =
CPLStrdup(CPLResetExtension(psInfo->pszFilename, "rde"));
else if (EQUAL(CPLGetExtension(psInfo->pszFilename), "aux"))
else if (EQUAL(osExt.c_str(), "aux"))
psInfo->pszIGEFilename =
CPLStrdup(CPLResetExtension(psInfo->pszFilename, "axe"));
else
Expand Down
12 changes: 7 additions & 5 deletions frmts/ilwis/ilwisdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -684,9 +684,11 @@ GDALDataset *ILWISDataset::Open(GDALOpenInfo *poOpenInfo)
if (poOpenInfo->nHeaderBytes < 1)
return nullptr;

std::string sExt = CPLGetExtension(poOpenInfo->pszFilename);
if (!EQUAL(sExt.c_str(), "mpr") && !EQUAL(sExt.c_str(), "mpl"))
return nullptr;
{
const std::string &sExt = poOpenInfo->osExtension;
if (!EQUAL(sExt.c_str(), "mpr") && !EQUAL(sExt.c_str(), "mpl"))
return nullptr;
}

if (!CheckASCII(poOpenInfo->pabyHeader, poOpenInfo->nHeaderBytes))
return nullptr;
Expand Down Expand Up @@ -736,8 +738,8 @@ GDALDataset *ILWISDataset::Open(GDALOpenInfo *poOpenInfo)
// of ILWIS raster maps,
std::string sMapStoreName =
ReadElement("MapStore", "Data", sBandName);
sExt = CPLGetExtension(sMapStoreName.c_str());
if (!STARTS_WITH_CI(sExt.c_str(), "mp#"))
if (!STARTS_WITH_CI(
CPLGetExtensionSafe(sMapStoreName.c_str()).c_str(), "mp#"))
{
CPLError(CE_Failure, CPLE_AppDefined,
"Unsupported ILWIS data file. \n"
Expand Down
2 changes: 1 addition & 1 deletion frmts/jp2kak/jp2kakdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ GDALDataset *JP2KAKDataset::Open(GDALOpenInfo *poOpenInfo)
KakaduInitialize();

// Handle setting up datasource for JPIP.
const char *pszExtension = CPLGetExtension(poOpenInfo->pszFilename);
const char *pszExtension = poOpenInfo->osExtension.c_str();
std::vector<GByte> abySubfileHeader(16); // leave in this scope
if (poOpenInfo->nHeaderBytes < 16)
{
Expand Down
3 changes: 1 addition & 2 deletions frmts/jp2kak/jp2kakdrivercore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ int JP2KAKDatasetIdentify(GDALOpenInfo *poOpenInfo)
else if (memcmp(poOpenInfo->pabyHeader, jpc_header, sizeof(jpc_header)) ==
0)
{
const char *const pszExtension =
CPLGetExtension(poOpenInfo->pszFilename);
const char *const pszExtension = poOpenInfo->osExtension.c_str();
if (EQUAL(pszExtension, "jpc") || EQUAL(pszExtension, "j2k") ||
EQUAL(pszExtension, "jp2") || EQUAL(pszExtension, "jpx") ||
EQUAL(pszExtension, "j2c") || EQUAL(pszExtension, "jhc"))
Expand Down
17 changes: 8 additions & 9 deletions frmts/kmlsuperoverlay/kmlsuperoverlaydataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,8 @@ KmlSuperOverlayCreateCopy(const char *pszFilename, GDALDataset *poSrcDS,
}
else
{
const char *extension = CPLGetExtension(pszFilename);
const std::string osExtension = CPLGetExtensionSafe(pszFilename);
const char *extension = osExtension.c_str();
if (!EQUAL(extension, "kml") && !EQUAL(extension, "kmz"))
{
CPLError(CE_Failure, CPLE_None,
Expand Down Expand Up @@ -1681,7 +1682,7 @@ static int KmlSuperOverlayFindRegionStart(CPLXMLNode *psNode,
int KmlSuperOverlayReadDataset::Identify(GDALOpenInfo *poOpenInfo)

{
const char *pszExt = CPLGetExtension(poOpenInfo->pszFilename);
const char *pszExt = poOpenInfo->osExtension.c_str();
if (EQUAL(pszExt, "kmz"))
return -1;
if (poOpenInfo->nHeaderBytes == 0)
Expand Down Expand Up @@ -1746,7 +1747,8 @@ GDALDataset *KmlSuperOverlayReadDataset::Open(GDALOpenInfo *poOpenInfo)
static std::unique_ptr<GDALDataset>
KmlSuperOverlayLoadIcon(const char *pszBaseFilename, const char *pszIcon)
{
const char *pszExt = CPLGetExtension(pszIcon);
const std::string osExt = CPLGetExtensionSafe(pszIcon);
const char *pszExt = osExt.c_str();
if (!EQUAL(pszExt, "png") && !EQUAL(pszExt, "jpg") &&
!EQUAL(pszExt, "jpeg"))
{
Expand Down Expand Up @@ -1812,8 +1814,7 @@ static bool KmlSuperOverlayComputeDepth(const std::string &osFilename,
CPLGetXMLNode(psIter, "Region") != nullptr &&
(pszHref = CPLGetXMLValue(psIter, "Link.href", nullptr)) != nullptr)
{
const char *pszExt = CPLGetExtension(pszHref);
if (EQUAL(pszExt, "kml"))
if (EQUAL(CPLGetExtensionSafe(pszHref).c_str(), "kml"))
{
CPLString osSubFilename;
if (STARTS_WITH(pszHref, "http"))
Expand Down Expand Up @@ -2580,8 +2581,7 @@ KmlSuperOverlayReadDataset::Open(const char *pszFilename,
if (nRec == 2)
return nullptr;
CPLString osFilename(pszFilename);
const char *pszExt = CPLGetExtension(pszFilename);
if (EQUAL(pszExt, "kmz"))
if (EQUAL(CPLGetExtensionSafe(pszFilename).c_str(), "kmz"))
{
if (!STARTS_WITH(pszFilename, "/vsizip/"))
osFilename = CPLSPrintf("/vsizip/%s", pszFilename);
Expand All @@ -2591,8 +2591,7 @@ KmlSuperOverlayReadDataset::Open(const char *pszFilename,
char **papszIter = papszFiles;
for (; *papszIter != nullptr; papszIter++)
{
pszExt = CPLGetExtension(*papszIter);
if (EQUAL(pszExt, "kml"))
if (EQUAL(CPLGetExtensionSafe(*papszIter).c_str(), "kml"))
{
osFilename = CPLFormFilename(osFilename, *papszIter, nullptr);
osFilename = KMLRemoveSlash(osFilename);
Expand Down
2 changes: 1 addition & 1 deletion frmts/mrsid/mrsiddataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,7 @@ CPLErr MrSIDDataset::OpenZoomLevel(lt_int32 iZoom)
/* projection */
/* -------------------------------------------------------------------- */
if (iZoom == 0 && m_oSRS.IsEmpty() &&
EQUAL(CPLGetExtension(GetDescription()), "sid"))
EQUAL(CPLGetExtensionSafe(GetDescription()).c_str(), "sid"))
{
const char *pszMETFilename = CPLResetExtension(GetDescription(), "met");
VSILFILE *fp = VSIFOpenL(pszMETFilename, "rb");
Expand Down
2 changes: 1 addition & 1 deletion frmts/mrsid/mrsiddrivercore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int MrSIDJP2Identify(GDALOpenInfo *poOpenInfo)

if (memcmp(poOpenInfo->pabyHeader, jpc_header, sizeof(jpc_header)) == 0)
{
const char *pszExtension = CPLGetExtension(poOpenInfo->pszFilename);
const char *pszExtension = poOpenInfo->osExtension.c_str();

if (!EQUAL(pszExtension, "jpc") && !EQUAL(pszExtension, "j2k") &&
!EQUAL(pszExtension, "jp2") && !EQUAL(pszExtension, "jpx") &&
Expand Down
4 changes: 2 additions & 2 deletions frmts/netcdf/netcdfdrivercore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ NetCDFFormatEnum netCDFIdentifyFormat(GDALOpenInfo *poOpenInfo, bool bCheckExt)
if (bCheckExt)
{
// Check by default.
const char *pszExtension = CPLGetExtension(poOpenInfo->pszFilename);
const char *pszExtension = poOpenInfo->osExtension.c_str();
if (!(EQUAL(pszExtension, "nc") || EQUAL(pszExtension, "cdf") ||
EQUAL(pszExtension, "nc2") || EQUAL(pszExtension, "nc4") ||
EQUAL(pszExtension, "nc3") || EQUAL(pszExtension, "grd") ||
Expand Down Expand Up @@ -154,7 +154,7 @@ NetCDFFormatEnum netCDFIdentifyFormat(GDALOpenInfo *poOpenInfo, bool bCheckExt)

// The HDF5 signature of netCDF 4 files can be at offsets 512, 1024, 2048,
// etc.
const char *pszExtension = CPLGetExtension(poOpenInfo->pszFilename);
const char *pszExtension = poOpenInfo->osExtension.c_str();
if (poOpenInfo->fpL != nullptr &&
(!bCheckExt || EQUAL(pszExtension, "nc") ||
EQUAL(pszExtension, "cdf") || EQUAL(pszExtension, "nc4") ||
Expand Down
5 changes: 3 additions & 2 deletions frmts/nitf/rpftocfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,9 @@ RPFToc *RPFTOCReadFromBuffer(const char *pszFilename, VSILFILE *fp,
// some CADRG maps have legend name smaller than 8.3 then the extension
// has blanks (0x20) at the end -> check only the first 3 letters of the
// extension.
const char *fileExt = CPLGetExtension(frameEntry->filename);
if (EQUALN(fileExt, "ovr", 3) || EQUALN(fileExt, "lgd", 3))
const std::string fileExt = CPLGetExtensionSafe(frameEntry->filename);
if (EQUALN(fileExt.c_str(), "ovr", 3) ||
EQUALN(fileExt.c_str(), "lgd", 3))
{
entry->isOverviewOrLegend = TRUE;
}
Expand Down
2 changes: 1 addition & 1 deletion frmts/pds/pds4dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1405,7 +1405,7 @@ static CPLString FixupTableFilename(const CPLString &osFilename)
{
return osFilename;
}
CPLString osExt = CPLGetExtension(osFilename);
CPLString osExt = CPLGetExtensionSafe(osFilename);
if (!osExt.empty())
{
CPLString osTry(osFilename);
Expand Down
9 changes: 3 additions & 6 deletions frmts/raw/byndataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ int BYNDataset::Identify(GDALOpenInfo *poOpenInfo)
/* Check file extension (.byn/.err) */
/* -------------------------------------------------------------------- */
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
const char *pszFileExtension = CPLGetExtension(poOpenInfo->pszFilename);
const char *pszFileExtension = poOpenInfo->osExtension.c_str();

if (!EQUAL(pszFileExtension, "byn") && !EQUAL(pszFileExtension, "err"))
{
Expand Down Expand Up @@ -720,19 +720,16 @@ GDALDataset *BYNDataset::Create(const char *pszFilename, int nXSize, int nYSize,
/* Check file extension (.byn/.err) */
/* -------------------------------------------------------------------- */

char *pszFileExtension = CPLStrdup(CPLGetExtension(pszFilename));
const std::string osExt = CPLGetExtensionSafe(pszFilename);

if (!EQUAL(pszFileExtension, "byn") && !EQUAL(pszFileExtension, "err"))
if (!EQUAL(osExt.c_str(), "byn") && !EQUAL(osExt.c_str(), "err"))
{
CPLError(
CE_Failure, CPLE_AppDefined,
"Attempt to create byn file with extension other than byn/err.");
CPLFree(pszFileExtension);
return nullptr;
}

CPLFree(pszFileExtension);

/* -------------------------------------------------------------------- */
/* Try to create the file. */
/* -------------------------------------------------------------------- */
Expand Down
2 changes: 1 addition & 1 deletion frmts/raw/envidataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2119,7 +2119,7 @@ ENVIDataset *ENVIDataset::Open(GDALOpenInfo *poOpenInfo, bool bFileSizeCheck)
// In case, there is no interleave keyword, we try to derive it from the
// file extension.
CPLString osInterleave = poDS->m_aosHeader.FetchNameValueDef(
"interleave", CPLGetExtension(poOpenInfo->pszFilename));
"interleave", poOpenInfo->osExtension.c_str());

if (!STARTS_WITH_CI(osInterleave, "BSQ") &&
!STARTS_WITH_CI(osInterleave, "BIP") &&
Expand Down
Loading

0 comments on commit 7474f93

Please sign in to comment.