Skip to content

Commit

Permalink
frmts/ ogr/ .cpp: replace all remaining uses of CPLGetBasename() with…
Browse files Browse the repository at this point in the history
… CPLGetBasenameSafe()
  • Loading branch information
rouault committed Jan 13, 2025
1 parent 3e1327c commit 85eb32a
Show file tree
Hide file tree
Showing 38 changed files with 210 additions and 183 deletions.
4 changes: 2 additions & 2 deletions frmts/fits/fitsdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2543,7 +2543,7 @@ GDALDataset *FITSDataset::Open(GDALOpenInfo *poOpenInfo)
osPath.resize(1024);
if (CPLGetExecPath(&osPath[0], static_cast<int>(osPath.size())))
{
osPath = CPLGetBasename(osPath.c_str());
osPath = CPLGetBasenameSafe(osPath.c_str());
}
if (osPath == "gdalinfo")
{
Expand Down Expand Up @@ -2578,7 +2578,7 @@ GDALDataset *FITSDataset::Open(GDALOpenInfo *poOpenInfo)
osPath.resize(1024);
if (CPLGetExecPath(&osPath[0], static_cast<int>(osPath.size())))
{
osPath = CPLGetBasename(osPath.c_str());
osPath = CPLGetBasenameSafe(osPath.c_str());
}
if (osPath == "ogrinfo")
{
Expand Down
2 changes: 1 addition & 1 deletion frmts/grib/gribdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ static bool IsGdalinfoInteractive()
osPath.resize(1024);
if (CPLGetExecPath(&osPath[0], static_cast<int>(osPath.size())))
{
osPath = CPLGetBasename(osPath.c_str());
osPath = CPLGetBasenameSafe(osPath.c_str());
}
return osPath == "gdalinfo";
}
Expand Down
12 changes: 7 additions & 5 deletions frmts/hfa/hfaopen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2908,13 +2908,15 @@ const char *HFAGetIGEFilename(HFAHandle hHFA)
const CPLString osExtension =
CPLGetExtensionSafe(pszRawFilename);
const CPLString osBasename =
CPLGetBasename(hHFA->pszFilename);
osFullFilename =
CPLFormFilename(hHFA->pszPath, osBasename, osExtension);
CPLGetBasenameSafe(hHFA->pszFilename);
osFullFilename = CPLFormFilenameSafe(
hHFA->pszPath, osBasename, osExtension);

if (VSIStatL(osFullFilename, &sStatBuf) == 0)
hHFA->pszIGEFilename = CPLStrdup(
CPLFormFilename(nullptr, osBasename, osExtension));
hHFA->pszIGEFilename =
CPLStrdup(CPLFormFilenameSafe(nullptr, osBasename,
osExtension)
.c_str());
else
hHFA->pszIGEFilename = CPLStrdup(pszRawFilename);
}
Expand Down
5 changes: 3 additions & 2 deletions frmts/jaxapalsar/jaxapalsardataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,9 @@ int PALSARJaxaDataset::Identify(GDALOpenInfo *poOpenInfo)

/* First, check that this is a PALSAR image indeed */
if (!STARTS_WITH_CI((char *)(poOpenInfo->pabyHeader + 60), "AL") ||
!STARTS_WITH_CI(CPLGetBasename((char *)(poOpenInfo->pszFilename)) + 4,
"ALPSR"))
!STARTS_WITH_CI(
CPLGetBasenameSafe(poOpenInfo->pszFilename).substr(4).c_str(),
"ALPSR"))
{
return 0;
}
Expand Down
8 changes: 4 additions & 4 deletions frmts/kmlsuperoverlay/kmlsuperoverlaydataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,15 @@ static int GenerateRootKml(const char *filename, const char *kmlfilename,
}
int minlodpixels = tilesize / 2;

const char *tmpfilename = CPLGetBasename(kmlfilename);
if (pszOverlayName == nullptr)
pszOverlayName = tmpfilename;
const std::string osOverlayName = pszOverlayName
? std::string(pszOverlayName)
: CPLGetBasenameSafe(kmlfilename);

// If we have not written any features yet, output the layer's schema.
VSIFPrintfL(fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
VSIFPrintfL(fp, "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n");
VSIFPrintfL(fp, "\t<Document>\n");
char *pszEncoded = CPLEscapeString(pszOverlayName, -1, CPLES_XML);
char *pszEncoded = CPLEscapeString(osOverlayName.c_str(), -1, CPLES_XML);
VSIFPrintfL(fp, "\t\t<name>%s</name>\n", pszEncoded);
CPLFree(pszEncoded);
if (pszOverlayDescription == nullptr)
Expand Down
23 changes: 12 additions & 11 deletions frmts/nitf/nitfdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1422,23 +1422,24 @@ NITFDataset *NITFDataset::OpenInternal(GDALOpenInfo *poOpenInfo,
/* -------------------------------------------------------------------- */

// get _rpc.txt file
const char *pszDirName = CPLGetDirname(pszFilename);
const char *pszBaseName = CPLGetBasename(pszFilename);
const char *pszRPCTXTFilename =
CPLFormFilename(pszDirName, CPLSPrintf("%s_rpc", pszBaseName), "txt");
if (CPLCheckForFile((char *)pszRPCTXTFilename,
poOpenInfo->GetSiblingFiles()))
const std::string osDirName = CPLGetDirnameSafe(pszFilename);
const std::string osBaseName = CPLGetBasenameSafe(pszFilename);
std::string osRPCTXTFilename = CPLFormFilenameSafe(
osDirName.c_str(), std::string(osBaseName).append("_rpc").c_str(),
"txt");
if (CPLCheckForFile(osRPCTXTFilename.data(), poOpenInfo->GetSiblingFiles()))
{
poDS->m_osRPCTXTFilename = pszRPCTXTFilename;
poDS->m_osRPCTXTFilename = osRPCTXTFilename;
}
else
{
pszRPCTXTFilename = CPLFormFilename(
pszDirName, CPLSPrintf("%s_RPC", pszBaseName), "TXT");
if (CPLCheckForFile((char *)pszRPCTXTFilename,
osRPCTXTFilename = CPLFormFilenameSafe(
osDirName.c_str(), std::string(osBaseName).append("_RPC").c_str(),
"TXT");
if (CPLCheckForFile(osRPCTXTFilename.data(),
poOpenInfo->GetSiblingFiles()))
{
poDS->m_osRPCTXTFilename = pszRPCTXTFilename;
poDS->m_osRPCTXTFilename = osRPCTXTFilename;
}
}
bool bHasLoadedRPCTXT = false;
Expand Down
12 changes: 6 additions & 6 deletions frmts/pds/pds4dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4169,8 +4169,8 @@ OGRLayer *PDS4Dataset::ICreateLayer(const char *pszName,
if (bSameDirectory)
{
osFullFilename =
CPLFormFilename(CPLGetPathSafe(m_osXMLFilename.c_str()).c_str(),
osBasename.c_str(), pszExt);
CPLFormFilenameSafe(CPLGetPathSafe(m_osXMLFilename.c_str()).c_str(),
osBasename.c_str(), pszExt);
VSIStatBufL sStat;
if (VSIStatL(osFullFilename, &sStat) == 0)
{
Expand All @@ -4183,9 +4183,9 @@ OGRLayer *PDS4Dataset::ICreateLayer(const char *pszName,
}
else
{
CPLString osDirectory =
CPLFormFilename(CPLGetPathSafe(m_osXMLFilename).c_str(),
CPLGetBasename(m_osXMLFilename), nullptr);
CPLString osDirectory = CPLFormFilenameSafe(
CPLGetPathSafe(m_osXMLFilename).c_str(),
CPLGetBasenameSafe(m_osXMLFilename).c_str(), nullptr);
VSIStatBufL sStat;
if (VSIStatL(osDirectory, &sStat) != 0 &&
VSIMkdir(osDirectory, 0755) != 0)
Expand All @@ -4195,7 +4195,7 @@ OGRLayer *PDS4Dataset::ICreateLayer(const char *pszName,
return nullptr;
}
osFullFilename =
CPLFormFilename(osDirectory, osBasename.c_str(), pszExt);
CPLFormFilenameSafe(osDirectory, osBasename.c_str(), pszExt);
}

if (EQUAL(pszTableType, "DELIMITED"))
Expand Down
2 changes: 1 addition & 1 deletion frmts/prf/phprfdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ GDALDataset *PhPrfDataset::Open(GDALOpenInfo *poOpenInfo)
GDALDataType eResultDatatype = GDT_Unknown;
CPLString osPartsBasePath(CPLGetPathSafe(poOpenInfo->pszFilename));
CPLString osPartsPath(osPartsBasePath + "/" +
CPLGetBasename(poOpenInfo->pszFilename));
CPLGetBasenameSafe(poOpenInfo->pszFilename));
CPLString osPartsExt;
double adfGeoTrans[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
bool bGeoTransOk = false;
Expand Down
4 changes: 2 additions & 2 deletions frmts/rasterlite/rasterlitecreatecopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ GDALDataset *RasterliteCreateCopy(const char *pszFilename, GDALDataset *poSrcDS,
if (nTokens == 0)
{
osDBName = pszFilenameWithoutPrefix;
osTableName = CPLGetBasename(pszFilenameWithoutPrefix);
osTableName = CPLGetBasenameSafe(pszFilenameWithoutPrefix);
}
else
{
Expand Down Expand Up @@ -439,7 +439,7 @@ GDALDataset *RasterliteCreateCopy(const char *pszFilename, GDALDataset *poSrcDS,
"specified");
return nullptr;
}
osTableName = CPLGetBasename(osDBName.c_str());
osTableName = CPLGetBasenameSafe(osDBName.c_str());
}

CPLString osRasterLayer;
Expand Down
3 changes: 2 additions & 1 deletion frmts/raw/ace2dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ GDALDataset *ACE2Dataset::Open(GDALOpenInfo *poOpenInfo)
if (!Identify(poOpenInfo))
return nullptr;

const char *pszBasename = CPLGetBasename(poOpenInfo->pszFilename);
const std::string osBasename = CPLGetBasenameSafe(poOpenInfo->pszFilename);
const char *pszBasename = osBasename.c_str();

if (strlen(pszBasename) < 7)
return nullptr;
Expand Down
4 changes: 2 additions & 2 deletions frmts/raw/iscedataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,10 @@ CPLErr ISCEDataset::FlushCache(bool bAtClosing)
CPLAddXMLAttributeAndValue(psTmpNode, "name", "ACCESS_MODE");
CPLCreateXMLElementAndValue(psTmpNode, "value", "read");

const char *pszFilename = CPLGetBasename(pszXMLFilename);
psTmpNode = CPLCreateXMLNode(psDocNode, CXT_Element, "property");
CPLAddXMLAttributeAndValue(psTmpNode, "name", "FILE_NAME");
CPLCreateXMLElementAndValue(psTmpNode, "value", pszFilename);
CPLCreateXMLElementAndValue(psTmpNode, "value",
CPLGetBasenameSafe(pszXMLFilename).c_str());

/* -------------------------------------------------------------------- */
/* Then, add the ISCE domain metadata. */
Expand Down
14 changes: 7 additions & 7 deletions frmts/saga/sagadataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,17 +444,17 @@ GDALDataset *SAGADataset::Open(GDALOpenInfo *poOpenInfo)

CSLDestroy(filesinzip);

osFullname = CPLFormFilename(osPath, file, nullptr);
osName = CPLGetBasename(file);
osHDRFilename =
CPLFormFilename(osPath, CPLGetBasenameSafe(file).c_str(), "sgrd");
osFullname = CPLFormFilenameSafe(osPath, file, nullptr);
osName = CPLGetBasenameSafe(file);
osHDRFilename = CPLFormFilenameSafe(
osPath, CPLGetBasenameSafe(file).c_str(), "sgrd");
}
else
{
osFullname = poOpenInfo->pszFilename;
osPath = CPLGetPath(poOpenInfo->pszFilename);
osName = CPLGetBasename(poOpenInfo->pszFilename);
osHDRFilename = CPLFormCIFilename(
osPath = CPLGetPathSafe(poOpenInfo->pszFilename);
osName = CPLGetBasenameSafe(poOpenInfo->pszFilename);
osHDRFilename = CPLFormCIFilenameSafe(
osPath, CPLGetBasenameSafe(poOpenInfo->pszFilename).c_str(),
"sgrd");
}
Expand Down
12 changes: 6 additions & 6 deletions frmts/sentinel2/sentinel2dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1816,9 +1816,9 @@ static void SENTINEL2GetResolutionSetAndMainMDFromGranule(
if (atoi(osBandName) < 10)
osBandName = "0" + osBandName;

CPLString osTile(
SENTINEL2GetTilename(CPLGetPathSafe(pszFilename).c_str(),
CPLGetBasename(pszFilename), osBandName));
CPLString osTile(SENTINEL2GetTilename(
CPLGetPathSafe(pszFilename).c_str(),
CPLGetBasenameSafe(pszFilename).c_str(), osBandName));
VSIStatBufL sStat;
if (VSIStatExL(osTile, &sStat, VSI_STAT_EXISTS_FLAG) == 0)
{
Expand Down Expand Up @@ -2050,9 +2050,9 @@ GDALDataset *SENTINEL2Dataset::OpenL1BSubdataset(GDALOpenInfo *poOpenInfo)
{
for (size_t i = 0; i < aosBands.size(); i++)
{
CPLString osTile(
SENTINEL2GetTilename(CPLGetPathSafe(osFilename).c_str(),
CPLGetBasename(osFilename), aosBands[i]));
CPLString osTile(SENTINEL2GetTilename(
CPLGetPathSafe(osFilename).c_str(),
CPLGetBasenameSafe(osFilename).c_str(), aosBands[i]));
if (SENTINEL2GetTileInfo(osTile, &nCols, &nRows, &nBits))
{
if (nBits <= 16)
Expand Down
27 changes: 12 additions & 15 deletions frmts/tiledb/tiledbdense.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,8 @@ CPLErr TileDBRasterBand::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,

if (eErr == CE_None)
{
CPLString osName = CPLString().Printf(
"%s", CPLGetBasenameSafe(poAttrDS->GetDescription())
.c_str());
CPLString osName =
CPLGetBasenameSafe(poAttrDS->GetDescription());

SetBuffer(poQuery.get(), eAttrType, osName, pAttrBlock,
nValues);
Expand Down Expand Up @@ -1353,7 +1352,6 @@ GDALDataset *TileDBRasterDataset::OpenInternal(GDALOpenInfo *poOpenInfo,
poDS->nTimestamp = std::strtoull(pszTimestamp, nullptr, 10);

CPLString osURI;
CPLString osAux;
CPLString osSubdataset;

std::string osAttrNameTmp;
Expand Down Expand Up @@ -1388,8 +1386,8 @@ GDALDataset *TileDBRasterDataset::OpenInternal(GDALOpenInfo *poOpenInfo,
osURI = TileDBDataset::VSI_to_tiledb_uri(poOpenInfo->pszFilename);
}

const char *pszArrayName = CPLGetBasename(osURI);
osAux.Printf("%s.tdb", pszArrayName);
CPLString osAux = CPLGetBasenameSafe(osURI);
osAux += ".tdb";

// aux file is in array folder
poDS->SetPhysicalFilename(
Expand Down Expand Up @@ -2159,12 +2157,11 @@ TileDBRasterDataset *TileDBRasterDataset::CreateLL(const char *pszFilename,
}
}

CPLString osAux;
const char *pszArrayName = CPLGetBasename(pszFilename);
osAux.Printf("%s.tdb", pszArrayName);
CPLString osAux = CPLGetBasenameSafe(pszFilename);
osAux += ".tdb";

poDS->SetPhysicalFilename(
CPLFormFilename(pszFilename, osAux.c_str(), nullptr));
CPLFormFilenameSafe(pszFilename, osAux.c_str(), nullptr).c_str());

// Initialize PAM information.
poDS->SetDescription(pszFilename);
Expand Down Expand Up @@ -2287,13 +2284,13 @@ bool TileDBRasterDataset::DeferredCreate(bool bCreateArray)
// be reported as subdatasets on future reads
for (const auto &poAttrDS : m_lpoAttributeDS)
{
const char *pszAttrName =
CPLGetBasename(poAttrDS->GetDescription());
const std::string osAttrName =
CPLGetBasenameSafe(poAttrDS->GetDescription());
GDALRasterBand *poAttrBand = poAttrDS->GetRasterBand(1);
int bHasNoData = false;
const double dfNoData = poAttrBand->GetNoDataValue(&bHasNoData);
CreateAttribute(poAttrBand->GetRasterDataType(), pszAttrName, 1,
CPL_TO_BOOL(bHasNoData), dfNoData);
CreateAttribute(poAttrBand->GetRasterDataType(), osAttrName.c_str(),
1, CPL_TO_BOOL(bHasNoData), dfNoData);
}

if (bCreateArray)
Expand Down Expand Up @@ -2604,7 +2601,7 @@ TileDBRasterDataset *TileDBRasterDataset::Create(const char *pszFilename,
{
aosImageStruct.SetNameValue(
CPLString().Printf("TILEDB_ATTRIBUTE_%i", ++i),
CPLGetBasename(poAttrDS->GetDescription()));
CPLGetBasenameSafe(poAttrDS->GetDescription()).c_str());
}
}
poDS->SetMetadata(aosImageStruct.List(), "IMAGE_STRUCTURE");
Expand Down
5 changes: 3 additions & 2 deletions frmts/tiledb/tiledbsparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,9 @@ GDALDataset *OGRTileDBDataset::Open(GDALOpenInfo *poOpenInfo,
{
auto poLayer = std::make_unique<OGRTileDBLayer>(
poDS.get(), osLayerFilename.c_str(),
osLayerName.has_value() ? (*osLayerName).c_str()
: CPLGetBasename(osLayerFilename.c_str()),
osLayerName.has_value()
? (*osLayerName).c_str()
: CPLGetBasenameSafe(osLayerFilename.c_str()).c_str(),
wkbUnknown, nullptr);
poLayer->m_bUpdatable = poOpenInfo->eAccess == GA_Update;
if (!poLayer->InitFromStorage(poDS->m_ctx.get(), nTimestamp,
Expand Down
2 changes: 1 addition & 1 deletion frmts/xpm/xpmdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ static GDALDataset *XPMCreateCopy(const char *pszFilename, GDALDataset *poSrcDS,
/* -------------------------------------------------------------------- */
bool bOK = VSIFPrintfL(fpPBM, "/* XPM */\n") >= 0;
bOK &= VSIFPrintfL(fpPBM, "static char *%s[] = {\n",
CPLGetBasename(pszFilename)) >= 0;
CPLGetBasenameSafe(pszFilename).c_str()) >= 0;
bOK &= VSIFPrintfL(fpPBM,
"/* width height num_colors chars_per_pixel */\n") >= 0;

Expand Down
4 changes: 2 additions & 2 deletions frmts/zarr/zarr_sharedresource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ std::shared_ptr<ZarrGroupBase> ZarrSharedResource::OpenRootGroup()
}
}
const std::string osArrayName(
CPLGetBasename(m_osRootDirectoryName.c_str()));
CPLGetBasenameSafe(m_osRootDirectoryName.c_str()));
if (!poRG->LoadArray(osArrayName, osZarrayFilename, oRoot, false,
CPLJSONObject()))
return nullptr;
Expand Down Expand Up @@ -165,7 +165,7 @@ std::shared_ptr<ZarrGroupBase> ZarrSharedResource::OpenRootGroup()
if (osNodeType == "array")
{
const std::string osArrayName(
CPLGetBasename(m_osRootDirectoryName.c_str()));
CPLGetBasenameSafe(m_osRootDirectoryName.c_str()));
poRG_V3->SetExplored();
if (!poRG_V3->LoadArray(osArrayName, osZarrJsonFilename, oRoot))
return nullptr;
Expand Down
10 changes: 5 additions & 5 deletions frmts/zarr/zarrdriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1180,8 +1180,8 @@ GDALDataset *ZarrDataset::Create(const char *pszName, int nXSize, int nYSize,
nBandsIn)
: nullptr);

const char *pszNonNullArrayName =
pszArrayName ? pszArrayName : CPLGetBasename(pszName);
const std::string osNonNullArrayName =
pszArrayName ? std::string(pszArrayName) : CPLGetBasenameSafe(pszName);
if (poBandDim)
{
const std::vector<std::shared_ptr<GDALDimension>> apoDims(
Expand All @@ -1192,8 +1192,8 @@ GDALDataset *ZarrDataset::Create(const char *pszName, int nXSize, int nYSize,
: std::vector<std::shared_ptr<GDALDimension>>{
poDS->m_poDimY, poDS->m_poDimX, poBandDim});
poDS->m_poSingleArray = poRG->CreateMDArray(
pszNonNullArrayName, apoDims, GDALExtendedDataType::Create(eType),
papszOptions);
osNonNullArrayName.c_str(), apoDims,
GDALExtendedDataType::Create(eType), papszOptions);
if (!poDS->m_poSingleArray)
{
CleanupCreatedFiles();
Expand All @@ -1215,7 +1215,7 @@ GDALDataset *ZarrDataset::Create(const char *pszName, int nXSize, int nYSize,
for (int i = 0; i < nBandsIn; i++)
{
auto poArray = poRG->CreateMDArray(
nBandsIn == 1 ? pszNonNullArrayName
nBandsIn == 1 ? osNonNullArrayName.c_str()
: pszArrayName ? CPLSPrintf("%s_band%d", pszArrayName, i + 1)
: CPLSPrintf("Band%d", i + 1),
apoDims, GDALExtendedDataType::Create(eType), papszOptions);
Expand Down
Loading

0 comments on commit 85eb32a

Please sign in to comment.