Skip to content

Commit

Permalink
apps: use new CSLConstList/CPLStringList facilities
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Mar 17, 2024
1 parent 11160f2 commit 1f749b0
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 196 deletions.
33 changes: 12 additions & 21 deletions apps/commonutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,12 @@
/* GetOutputDriversFor() */
/* -------------------------------------------------------------------- */

std::vector<CPLString> GetOutputDriversFor(const char *pszDestFilename,
int nFlagRasterVector)
std::vector<std::string> GetOutputDriversFor(const char *pszDestFilename,
int nFlagRasterVector)
{
std::vector<CPLString> aoDriverList;
char **papszList = GDALGetOutputDriversForDatasetName(
return CPLStringList(GDALGetOutputDriversForDatasetName(
pszDestFilename, nFlagRasterVector, /* bSingleMatch = */ false,
/* bEmitWarning = */ false);
for (char **papszIter = papszList; papszIter && *papszIter; ++papszIter)
aoDriverList.push_back(*papszIter);
CSLDestroy(papszList);
return aoDriverList;
/* bEmitWarning = */ false));
}

/* -------------------------------------------------------------------- */
Expand All @@ -60,15 +55,13 @@ std::vector<CPLString> GetOutputDriversFor(const char *pszDestFilename,

CPLString GetOutputDriverForRaster(const char *pszDestFilename)
{
char **papszList = GDALGetOutputDriversForDatasetName(
const CPLStringList aosList(GDALGetOutputDriversForDatasetName(
pszDestFilename, GDAL_OF_RASTER, /* bSingleMatch = */ true,
/* bEmitWarning = */ true);
if (papszList)
/* bEmitWarning = */ true));
if (!aosList.empty())
{
CPLDebug("GDAL", "Using %s driver", papszList[0]);
const std::string osRet = papszList[0];
CSLDestroy(papszList);
return osRet;
CPLDebug("GDAL", "Using %s driver", aosList[0]);
return aosList[0];
}
return CPLString();
}
Expand Down Expand Up @@ -119,12 +112,11 @@ void GDALRemoveBOM(GByte *pabyData)

std::string GDALRemoveSQLComments(const std::string &osInput)
{
char **papszLines =
CSLTokenizeStringComplex(osInput.c_str(), "\r\n", FALSE, FALSE);
const CPLStringList aosLines(
CSLTokenizeStringComplex(osInput.c_str(), "\r\n", FALSE, FALSE));
std::string osSQL;
for (char **papszIter = papszLines; papszIter && *papszIter; ++papszIter)
for (const char *pszLine : aosLines)
{
const char *pszLine = *papszIter;
char chQuote = 0;
int i = 0;
for (; pszLine[i] != '\0'; ++i)
Expand Down Expand Up @@ -158,7 +150,6 @@ std::string GDALRemoveSQLComments(const std::string &osInput)
}
osSQL += ' ';
}
CSLDestroy(papszLines);
return osSQL;
}

Expand Down
4 changes: 2 additions & 2 deletions apps/commonutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ CPL_C_END
#include "cpl_string.h"
#include <vector>

std::vector<CPLString> CPL_DLL GetOutputDriversFor(const char *pszDestFilename,
int nFlagRasterVector);
std::vector<std::string> CPL_DLL
GetOutputDriversFor(const char *pszDestFilename, int nFlagRasterVector);
CPLString CPL_DLL GetOutputDriverForRaster(const char *pszDestFilename);
void GDALRemoveBOM(GByte *pabyData);
std::string GDALRemoveSQLComments(const std::string &osInput);
Expand Down
2 changes: 1 addition & 1 deletion apps/gdal_contour.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ MAIN_START(argc, argv)
CPLString osFormat;
if (pszFormat == nullptr)
{
std::vector<CPLString> aoDrivers =
const auto aoDrivers =
GetOutputDriversFor(pszDstFilename, GDAL_OF_VECTOR);
if (aoDrivers.empty())
{
Expand Down
3 changes: 1 addition & 2 deletions apps/gdal_footprint_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,7 @@ GetOutputLayerAndUpdateDstDS(const char *pszDest, GDALDatasetH &hDstDS,
std::string osFormat(psOptions->osFormat);
if (osFormat.empty())
{
std::vector<CPLString> aoDrivers =
GetOutputDriversFor(pszDest, GDAL_OF_VECTOR);
const auto aoDrivers = GetOutputDriversFor(pszDest, GDAL_OF_VECTOR);
if (aoDrivers.empty())
{
CPLError(CE_Failure, CPLE_AppDefined,
Expand Down
30 changes: 12 additions & 18 deletions apps/gdal_rasterize_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1067,14 +1067,12 @@ GDALRasterizeOptionsNew(char **papszArgv,
{
if (strchr(papszArgv[i + 1], ' '))
{
char **papszTokens = CSLTokenizeString(papszArgv[i + 1]);
char **papszIter = papszTokens;
while (papszIter && *papszIter)
const CPLStringList aosTokens(
CSLTokenizeString(papszArgv[i + 1]));
for (const char *pszToken : aosTokens)
{
psOptions->anBandList.push_back(atoi(*papszIter));
papszIter++;
psOptions->anBandList.push_back(atoi(pszToken));
}
CSLDestroy(papszTokens);
i += 1;
}
else
Expand Down Expand Up @@ -1120,14 +1118,12 @@ GDALRasterizeOptionsNew(char **papszArgv,
{
if (strchr(papszArgv[i + 1], ' '))
{
char **papszTokens = CSLTokenizeString(papszArgv[i + 1]);
char **papszIter = papszTokens;
while (papszIter && *papszIter)
const CPLStringList aosTokens(
CSLTokenizeString(papszArgv[i + 1]));
for (const char *pszToken : aosTokens)
{
psOptions->adfBurnValues.push_back(CPLAtof(*papszIter));
papszIter++;
psOptions->adfBurnValues.push_back(CPLAtof(pszToken));
}
CSLDestroy(papszTokens);
i += 1;
}
else
Expand Down Expand Up @@ -1179,14 +1175,12 @@ GDALRasterizeOptionsNew(char **papszArgv,
{
if (strchr(papszArgv[i + 1], ' '))
{
char **papszTokens = CSLTokenizeString(papszArgv[i + 1]);
char **papszIter = papszTokens;
while (papszIter && *papszIter)
const CPLStringList aosTokens(
CSLTokenizeString(papszArgv[i + 1]));
for (const char *pszToken : aosTokens)
{
psOptions->adfInitVals.push_back(CPLAtof(*papszIter));
papszIter++;
psOptions->adfInitVals.push_back(CPLAtof(pszToken));
}
CSLDestroy(papszTokens);
i += 1;
}
else
Expand Down
64 changes: 24 additions & 40 deletions apps/gdalinfo_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1975,44 +1975,41 @@ static void GDALInfoReportMetadata(const GDALInfoOptions *psOptions,
/* -------------------------------------------------------------------- */
if (psOptions->bListMDD)
{
char **papszMDDList = GDALGetMetadataDomainList(hObject);
char **papszIter = papszMDDList;
const CPLStringList aosDomainList(GDALGetMetadataDomainList(hObject));
json_object *poMDD = nullptr;
json_object *const poListMDD =
bJson ? json_object_new_array() : nullptr;

if (papszMDDList != nullptr)
if (!aosDomainList.empty())
{
if (!bJson)
Concat(osStr, psOptions->bStdoutOutput, "%sMetadata domains:\n",
pszIndent);
}

while (papszIter != nullptr && *papszIter != nullptr)
for (const char *pszDomain : aosDomainList)
{
if (EQUAL(*papszIter, ""))
if (EQUAL(pszDomain, ""))
{
if (bJson)
poMDD = json_object_new_string(*papszIter);
poMDD = json_object_new_string(pszDomain);
else
Concat(osStr, psOptions->bStdoutOutput, "%s (default)\n",
pszIndent);
}
else
{
if (bJson)
poMDD = json_object_new_string(*papszIter);
poMDD = json_object_new_string(pszDomain);
else
Concat(osStr, psOptions->bStdoutOutput, "%s %s\n",
pszIndent, *papszIter);
pszIndent, pszDomain);
}
if (bJson)
json_object_array_add(poListMDD, poMDD);
papszIter++;
}
if (bJson)
json_object_object_add(poMetadata, "metadataDomains", poListMDD);
CSLDestroy(papszMDDList);
}

if (!psOptions->bShowMetadata)
Expand All @@ -2029,61 +2026,48 @@ static void GDALInfoReportMetadata(const GDALInfoOptions *psOptions,
/* -------------------------------------------------------------------- */
if (psOptions->papszExtraMDDomains != nullptr)
{
char **papszExtraMDDomainsExpanded = nullptr;
CPLStringList aosExtraMDDomainsExpanded;

if (EQUAL(psOptions->papszExtraMDDomains[0], "all") &&
psOptions->papszExtraMDDomains[1] == nullptr)
{
char **papszMDDList = GDALGetMetadataDomainList(hObject);
char *const *papszIter = papszMDDList;

while (papszIter != nullptr && *papszIter != nullptr)
const CPLStringList aosMDDList(GDALGetMetadataDomainList(hObject));
for (const char *pszDomain : aosMDDList)
{
if (!EQUAL(*papszIter, "") &&
!EQUAL(*papszIter, "IMAGE_STRUCTURE") &&
!EQUAL(*papszIter, "TILING_SCHEME") &&
!EQUAL(*papszIter, "SUBDATASETS") &&
!EQUAL(*papszIter, "GEOLOCATION") &&
!EQUAL(*papszIter, "RPC"))
if (!EQUAL(pszDomain, "") &&
!EQUAL(pszDomain, "IMAGE_STRUCTURE") &&
!EQUAL(pszDomain, "TILING_SCHEME") &&
!EQUAL(pszDomain, "SUBDATASETS") &&
!EQUAL(pszDomain, "GEOLOCATION") &&
!EQUAL(pszDomain, "RPC"))
{
papszExtraMDDomainsExpanded =
CSLAddString(papszExtraMDDomainsExpanded, *papszIter);
aosExtraMDDomainsExpanded.AddString(pszDomain);
}
papszIter++;
}
CSLDestroy(papszMDDList);
}
else
{
papszExtraMDDomainsExpanded =
aosExtraMDDomainsExpanded =
CSLDuplicate(psOptions->papszExtraMDDomains);
}

for (int iMDD = 0; papszExtraMDDomainsExpanded != nullptr &&
papszExtraMDDomainsExpanded[iMDD] != nullptr;
iMDD++)
for (const char *pszDomain : aosExtraMDDomainsExpanded)
{
if (bJson)
{
GDALInfoPrintMetadata(psOptions, hObject,
papszExtraMDDomainsExpanded[iMDD],
papszExtraMDDomainsExpanded[iMDD],
GDALInfoPrintMetadata(psOptions, hObject, pszDomain, pszDomain,
pszIndent, bJson, poMetadata, osStr);
}
else
{
CPLString osDisplayedname =
"Metadata (" +
CPLString(papszExtraMDDomainsExpanded[iMDD]) + ")";
const std::string osDisplayedName =
std::string("Metadata (").append(pszDomain).append(")");

GDALInfoPrintMetadata(psOptions, hObject,
papszExtraMDDomainsExpanded[iMDD],
osDisplayedname.c_str(), pszIndent, bJson,
GDALInfoPrintMetadata(psOptions, hObject, pszDomain,
osDisplayedName.c_str(), pszIndent, bJson,
poMetadata, osStr);
}
}

CSLDestroy(papszExtraMDDomainsExpanded);
}

/* -------------------------------------------------------------------- */
Expand Down
3 changes: 1 addition & 2 deletions apps/gdaltindex_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,7 @@ GDALDatasetH GDALTileIndex(const char *pszDest, int nSrcCount,
{
if (psOptions->osFormat.empty())
{
std::vector<CPLString> aoDrivers =
GetOutputDriversFor(pszDest, GDAL_OF_VECTOR);
const auto aoDrivers = GetOutputDriversFor(pszDest, GDAL_OF_VECTOR);
if (aoDrivers.empty())
{
CPLError(CE_Failure, CPLE_AppDefined,
Expand Down
16 changes: 6 additions & 10 deletions apps/gdalwarp_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3616,14 +3616,12 @@ static GDALDatasetH GDALWarpCreateOutput(
GDALDataset::Open(pszFilename, GDAL_OF_RASTER, apszAllowedDrivers));
if (poExistingOutputDS)
{
char **papszFileList = poExistingOutputDS->GetFileList();
for (char **papszIter = papszFileList; papszIter && *papszIter;
++papszIter)
for (const char *pszFilenameInList :
CPLStringList(poExistingOutputDS->GetFileList()))
{
oSetExistingDestFiles.insert(
CPLString(*papszIter).replaceAll('\\', '/'));
CPLString(pszFilenameInList).replaceAll('\\', '/'));
}
CSLDestroy(papszFileList);
}
CPLPopErrorHandler();
}
Expand Down Expand Up @@ -3668,19 +3666,17 @@ static GDALDatasetH GDALWarpCreateOutput(
poSrcDS->GetDescription(), GDAL_OF_RASTER, apszAllowedDrivers));
if (poSrcDSTmp)
{
char **papszFileList = poSrcDSTmp->GetFileList();
for (char **papszIter = papszFileList; papszIter && *papszIter;
++papszIter)
for (const char *pszFilenameInList :
CPLStringList(poSrcDSTmp->GetFileList()))
{
CPLString osFilename(*papszIter);
CPLString osFilename(pszFilenameInList);
osFilename.replaceAll('\\', '/');
if (oSetExistingDestFiles.find(osFilename) !=
oSetExistingDestFiles.end())
{
oSetExistingDestFilesFoundInSource.insert(osFilename);
}
}
CSLDestroy(papszFileList);
}
}

Expand Down
Loading

0 comments on commit 1f749b0

Please sign in to comment.