From 1f749b064da030140aeec4a331b20f0274bdae15 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Sun, 17 Mar 2024 23:52:44 +0100 Subject: [PATCH] apps: use new CSLConstList/CPLStringList facilities --- apps/commonutils.cpp | 33 +++++++------------ apps/commonutils.h | 4 +-- apps/gdal_contour.cpp | 2 +- apps/gdal_footprint_lib.cpp | 3 +- apps/gdal_rasterize_lib.cpp | 30 +++++++---------- apps/gdalinfo_lib.cpp | 64 ++++++++++++++----------------------- apps/gdaltindex_lib.cpp | 3 +- apps/gdalwarp_lib.cpp | 16 ++++------ apps/ogr2ogr_lib.cpp | 59 +++++++++++++--------------------- apps/ogrinfo_lib.cpp | 54 ++++++++++++------------------- apps/ogrtindex.cpp | 2 +- apps/sozip.cpp | 22 ++++++------- apps/test_ogrsf.cpp | 29 ++++++++--------- 13 files changed, 125 insertions(+), 196 deletions(-) diff --git a/apps/commonutils.cpp b/apps/commonutils.cpp index b7df3fb6179a..8e0d215f22d3 100644 --- a/apps/commonutils.cpp +++ b/apps/commonutils.cpp @@ -41,17 +41,12 @@ /* GetOutputDriversFor() */ /* -------------------------------------------------------------------- */ -std::vector GetOutputDriversFor(const char *pszDestFilename, - int nFlagRasterVector) +std::vector GetOutputDriversFor(const char *pszDestFilename, + int nFlagRasterVector) { - std::vector 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)); } /* -------------------------------------------------------------------- */ @@ -60,15 +55,13 @@ std::vector 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(); } @@ -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) @@ -158,7 +150,6 @@ std::string GDALRemoveSQLComments(const std::string &osInput) } osSQL += ' '; } - CSLDestroy(papszLines); return osSQL; } diff --git a/apps/commonutils.h b/apps/commonutils.h index 8165ffc2cf53..dae2f5dc0b88 100644 --- a/apps/commonutils.h +++ b/apps/commonutils.h @@ -93,8 +93,8 @@ CPL_C_END #include "cpl_string.h" #include -std::vector CPL_DLL GetOutputDriversFor(const char *pszDestFilename, - int nFlagRasterVector); +std::vector 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); diff --git a/apps/gdal_contour.cpp b/apps/gdal_contour.cpp index e127e1a7791d..d1c418537aab 100644 --- a/apps/gdal_contour.cpp +++ b/apps/gdal_contour.cpp @@ -320,7 +320,7 @@ MAIN_START(argc, argv) CPLString osFormat; if (pszFormat == nullptr) { - std::vector aoDrivers = + const auto aoDrivers = GetOutputDriversFor(pszDstFilename, GDAL_OF_VECTOR); if (aoDrivers.empty()) { diff --git a/apps/gdal_footprint_lib.cpp b/apps/gdal_footprint_lib.cpp index a81c185abf8b..41f2812d2b68 100644 --- a/apps/gdal_footprint_lib.cpp +++ b/apps/gdal_footprint_lib.cpp @@ -404,8 +404,7 @@ GetOutputLayerAndUpdateDstDS(const char *pszDest, GDALDatasetH &hDstDS, std::string osFormat(psOptions->osFormat); if (osFormat.empty()) { - std::vector aoDrivers = - GetOutputDriversFor(pszDest, GDAL_OF_VECTOR); + const auto aoDrivers = GetOutputDriversFor(pszDest, GDAL_OF_VECTOR); if (aoDrivers.empty()) { CPLError(CE_Failure, CPLE_AppDefined, diff --git a/apps/gdal_rasterize_lib.cpp b/apps/gdal_rasterize_lib.cpp index 08c13e60fd0b..39913ad306cb 100644 --- a/apps/gdal_rasterize_lib.cpp +++ b/apps/gdal_rasterize_lib.cpp @@ -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 @@ -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 @@ -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 diff --git a/apps/gdalinfo_lib.cpp b/apps/gdalinfo_lib.cpp index 2ca2466dde8c..38aa60f4faeb 100644 --- a/apps/gdalinfo_lib.cpp +++ b/apps/gdalinfo_lib.cpp @@ -1975,25 +1975,24 @@ 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); @@ -2001,18 +2000,16 @@ static void GDALInfoReportMetadata(const GDALInfoOptions *psOptions, 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) @@ -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); } /* -------------------------------------------------------------------- */ diff --git a/apps/gdaltindex_lib.cpp b/apps/gdaltindex_lib.cpp index 79770cd2030b..82f6c6855963 100644 --- a/apps/gdaltindex_lib.cpp +++ b/apps/gdaltindex_lib.cpp @@ -394,8 +394,7 @@ GDALDatasetH GDALTileIndex(const char *pszDest, int nSrcCount, { if (psOptions->osFormat.empty()) { - std::vector aoDrivers = - GetOutputDriversFor(pszDest, GDAL_OF_VECTOR); + const auto aoDrivers = GetOutputDriversFor(pszDest, GDAL_OF_VECTOR); if (aoDrivers.empty()) { CPLError(CE_Failure, CPLE_AppDefined, diff --git a/apps/gdalwarp_lib.cpp b/apps/gdalwarp_lib.cpp index 289929f981fc..645fd39842b6 100644 --- a/apps/gdalwarp_lib.cpp +++ b/apps/gdalwarp_lib.cpp @@ -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(); } @@ -3668,11 +3666,10 @@ 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()) @@ -3680,7 +3677,6 @@ static GDALDatasetH GDALWarpCreateOutput( oSetExistingDestFilesFoundInSource.insert(osFilename); } } - CSLDestroy(papszFileList); } } diff --git a/apps/ogr2ogr_lib.cpp b/apps/ogr2ogr_lib.cpp index 57f961f17c93..b2b505af097b 100644 --- a/apps/ogr2ogr_lib.cpp +++ b/apps/ogr2ogr_lib.cpp @@ -2023,10 +2023,9 @@ GDALVectorTranslateCreateCopy(GDALDriver *poDriver, const char *pszDest, } else { - CSLConstList papszIter = psOptions->aosLayers.List(); - for (; *papszIter != nullptr; ++papszIter) + for (const char *pszLayer : psOptions->aosLayers) { - OGRLayer *poSrcLayer = poDS->GetLayerByName(*papszIter); + OGRLayer *poSrcLayer = poDS->GetLayerByName(pszLayer); if (poSrcLayer != nullptr) { poSrcLayer->SetAttributeFilter( @@ -2085,12 +2084,11 @@ GDALVectorTranslateCreateCopy(GDALDriver *poDriver, const char *pszDest, if (EQUAL(poDriver->GetDescription(), "GMLAS")) { CPLString osLayers; - CSLConstList papszIter = psOptions->aosLayers.List(); - for (; *papszIter != nullptr; ++papszIter) + for (const char *pszLayer : psOptions->aosLayers) { if (!osLayers.empty()) osLayers += ","; - osLayers += *papszIter; + osLayers += pszLayer; } aosDSCO.SetNameValue("LAYERS", osLayers); } @@ -2399,13 +2397,13 @@ GDALDatasetH GDALVectorTranslate(const char *pszDest, GDALDatasetH hDstDS, /* -------------------------------------------------------------------- */ /* Try opening the output datasource as an existing, writable */ /* -------------------------------------------------------------------- */ - std::vector aoDrivers; + std::vector aoDrivers; if (poODS == nullptr && psOptions->osFormat.empty()) { aoDrivers = GetOutputDriversFor(pszDest, GDAL_OF_VECTOR); if (!bUpdate && aoDrivers.size() == 1) { - GDALDriverH hDriver = GDALGetDriverByName(aoDrivers[0]); + GDALDriverH hDriver = GDALGetDriverByName(aoDrivers[0].c_str()); const char *pszPrefix = GDALGetMetadataItem( hDriver, GDAL_DMD_CONNECTION_PREFIX, nullptr); if (pszPrefix && STARTS_WITH_CI(pszDest, pszPrefix)) @@ -2624,26 +2622,17 @@ GDALDatasetH GDALVectorTranslate(const char *pszDest, GDALDatasetH hDstDS, if (psOptions->bCopyMD) { - char **papszDomains = poDS->GetMetadataDomainList(); - for (char **papszIter = papszDomains; papszIter && *papszIter; - ++papszIter) + const CPLStringList aosDomains(poDS->GetMetadataDomainList()); + for (const char *pszMD : aosDomains) { - char **papszMD = poDS->GetMetadata(*papszIter); - if (papszMD) - poODS->SetMetadata(papszMD, *papszIter); + if (char **papszMD = poDS->GetMetadata(pszMD)) + poODS->SetMetadata(papszMD, pszMD); } - CSLDestroy(papszDomains); } - for (char **papszIter = psOptions->aosMetadataOptions.List(); - papszIter && *papszIter; ++papszIter) + for (const auto &[pszKey, pszValue] : + cpl::IterateNameValue(psOptions->aosMetadataOptions)) { - char *pszKey = nullptr; - const char *pszValue = CPLParseNameValue(*papszIter, &pszKey); - if (pszKey) - { - poODS->SetMetadataItem(pszKey, pszValue); - CPLFree(pszKey); - } + poODS->SetMetadataItem(pszKey, pszValue); } // When writing to GeoJSON and using -nln, set the @NAME layer @@ -3000,15 +2989,14 @@ GDALDatasetH GDALVectorTranslate(const char *pszDest, GDALDatasetH hDstDS, } // Make sure to probe all layers in case some are by default invisible - for (char **papszIter = psOptions->aosLayers.List(); - papszIter && *papszIter; ++papszIter) + for (const char *pszLayer : psOptions->aosLayers) { - OGRLayer *poLayer = poDS->GetLayerByName(*papszIter); + OGRLayer *poLayer = poDS->GetLayerByName(pszLayer); if (poLayer == nullptr) { CPLError(CE_Failure, CPLE_AppDefined, - "Couldn't fetch requested layer %s!", *papszIter); + "Couldn't fetch requested layer %s!", pszLayer); if (hDstDS == nullptr) GDALClose(poODS); delete poGCPCoordTrans; @@ -4602,19 +4590,16 @@ SetupTargetLayer::Setup(OGRLayer *poSrcLayer, const char *pszNewLayerName, if (m_bCopyMD) { - char **papszDomains = poSrcLayer->GetMetadataDomainList(); - for (char **papszIter = papszDomains; papszIter && *papszIter; - ++papszIter) + const CPLStringList aosDomains(poSrcLayer->GetMetadataDomainList()); + for (const char *pszMD : aosDomains) { - if (!EQUAL(*papszIter, "IMAGE_STRUCTURE") && - !EQUAL(*papszIter, "SUBDATASETS")) + if (!EQUAL(pszMD, "IMAGE_STRUCTURE") && + !EQUAL(pszMD, "SUBDATASETS")) { - char **papszMD = poSrcLayer->GetMetadata(*papszIter); - if (papszMD) - poDstLayer->SetMetadata(papszMD, *papszIter); + if (char **papszMD = poSrcLayer->GetMetadata(pszMD)) + poDstLayer->SetMetadata(papszMD, pszMD); } } - CSLDestroy(papszDomains); } if (anRequestedGeomFields.empty() && nSrcGeomFieldCount > 1 && diff --git a/apps/ogrinfo_lib.cpp b/apps/ogrinfo_lib.cpp index 1450cb307945..2cb6e57cb428 100644 --- a/apps/ogrinfo_lib.cpp +++ b/apps/ogrinfo_lib.cpp @@ -713,17 +713,16 @@ static void GDALVectorInfoReportMetadata(CPLString &osRet, CPLJSONObject &oRoot, /* -------------------------------------------------------------------- */ if (bListMDD) { - char **papszMDDList = GDALGetMetadataDomainList(hObject); - char **papszIter = papszMDDList; + const CPLStringList aosMDDList(GDALGetMetadataDomainList(hObject)); CPLJSONArray metadataDomains; - if (papszMDDList != nullptr && !bJson) + if (!aosMDDList.empty() && !bJson) Concat(osRet, psOptions->bStdoutOutput, "%sMetadata domains:\n", pszIndent); - while (papszIter != nullptr && *papszIter != nullptr) + for (const char *pszDomain : aosMDDList) { - if (EQUAL(*papszIter, "")) + if (EQUAL(pszDomain, "")) { if (bJson) metadataDomains.Add(""); @@ -734,14 +733,12 @@ static void GDALVectorInfoReportMetadata(CPLString &osRet, CPLJSONObject &oRoot, else { if (bJson) - metadataDomains.Add(*papszIter); + metadataDomains.Add(pszDomain); else Concat(osRet, psOptions->bStdoutOutput, "%s %s\n", - pszIndent, *papszIter); + pszIndent, pszDomain); } - papszIter++; } - CSLDestroy(papszMDDList); if (bJson) oRoot.Add("metadataDomains", metadataDomains); @@ -763,43 +760,33 @@ static void GDALVectorInfoReportMetadata(CPLString &osRet, CPLJSONObject &oRoot, /* -------------------------------------------------------------------- */ if (papszExtraMDDomains != nullptr) { - char **papszExtraMDDomainsExpanded = nullptr; + CPLStringList aosExtraMDDomainsExpanded; if (EQUAL(papszExtraMDDomains[0], "all") && papszExtraMDDomains[1] == nullptr) { - char **papszMDDList = GDALGetMetadataDomainList(hObject); - char **papszIter = papszMDDList; - - while (papszIter != nullptr && *papszIter != nullptr) + const CPLStringList aosMDDList(GDALGetMetadataDomainList(hObject)); + for (const char *pszDomain : aosMDDList) { - if (!EQUAL(*papszIter, "") && !EQUAL(*papszIter, "SUBDATASETS")) + if (!EQUAL(pszDomain, "") && !EQUAL(pszDomain, "SUBDATASETS")) { - papszExtraMDDomainsExpanded = - CSLAddString(papszExtraMDDomainsExpanded, *papszIter); + aosExtraMDDomainsExpanded.AddString(pszDomain); } - papszIter++; } - CSLDestroy(papszMDDList); } else { - papszExtraMDDomainsExpanded = CSLDuplicate(papszExtraMDDomains); + aosExtraMDDomainsExpanded = CSLDuplicate(papszExtraMDDomains); } - for (int iMDD = 0; papszExtraMDDomainsExpanded != nullptr && - papszExtraMDDomainsExpanded[iMDD] != nullptr; - iMDD++) + for (const char *pszDomain : aosExtraMDDomainsExpanded) { - char pszDisplayedname[256]; - snprintf(pszDisplayedname, 256, "Metadata (%s)", - papszExtraMDDomainsExpanded[iMDD]); + const std::string osDisplayedName = + std::string("Metadata (").append(pszDomain).append(")"); GDALVectorInfoPrintMetadata(osRet, oMetadata, psOptions, hObject, - papszExtraMDDomainsExpanded[iMDD], - pszDisplayedname, pszIndent); + pszDomain, osDisplayedName.c_str(), + pszIndent); } - - CSLDestroy(papszExtraMDDomainsExpanded); } GDALVectorInfoPrintMetadata(osRet, oMetadata, psOptions, hObject, "SUBDATASETS", "Subdatasets", pszIndent); @@ -2139,15 +2126,14 @@ char *GDALVectorInfo(GDALDatasetH hDataset, /* -------------------------------------------------------------------- */ - for (CSLConstList papszIter = papszLayers; *papszIter != nullptr; - ++papszIter) + for (const char *pszLayer : cpl::Iterate(papszLayers)) { - OGRLayer *poLayer = poDS->GetLayerByName(*papszIter); + OGRLayer *poLayer = poDS->GetLayerByName(pszLayer); if (poLayer == nullptr) { CPLError(CE_Failure, CPLE_AppDefined, - "Couldn't fetch requested layer %s.", *papszIter); + "Couldn't fetch requested layer %s.", pszLayer); return nullptr; } diff --git a/apps/ogrtindex.cpp b/apps/ogrtindex.cpp index 92bbfb7f8f84..58e36348eb2f 100644 --- a/apps/ogrtindex.cpp +++ b/apps/ogrtindex.cpp @@ -278,7 +278,7 @@ MAIN_START(nArgc, papszArgv) CPLString osFormat; if (pszFormat == nullptr) { - const std::vector aoDrivers = + const auto aoDrivers = GetOutputDriversFor(pszOutputName, GDAL_OF_VECTOR); if (aoDrivers.empty()) { diff --git a/apps/sozip.cpp b/apps/sozip.cpp index 5ad596f2ebcc..a44c02511dc8 100644 --- a/apps/sozip.cpp +++ b/apps/sozip.cpp @@ -562,22 +562,21 @@ MAIN_START(nArgc, papszArgv) pszZipFilename + "}/" + psEntry->pszName; std::string osProperties; - char **papszMDGeneric = - VSIGetFileMetadata(osFilename.c_str(), nullptr, nullptr); - for (char **papszIter = papszMDGeneric; - papszIter && papszIter[0]; ++papszIter) + const CPLStringList aosMDGeneric( + VSIGetFileMetadata(osFilename.c_str(), nullptr, nullptr)); + for (const char *pszMDGeneric : aosMDGeneric) { if (!osProperties.empty()) osProperties += ','; - osProperties += *papszIter; + osProperties += pszMDGeneric; } - CSLDestroy(papszMDGeneric); - char **papszMD = - VSIGetFileMetadata(osFilename.c_str(), "ZIP", nullptr); - bool bSeekOptimized = - CSLFetchNameValue(papszMD, "SOZIP_VALID") != nullptr; + + const CPLStringList aosMD( + VSIGetFileMetadata(osFilename.c_str(), "ZIP", nullptr)); + const bool bSeekOptimized = + aosMD.FetchNameValue("SOZIP_VALID") != nullptr; const char *pszChunkSize = - CSLFetchNameValue(papszMD, "SOZIP_CHUNK_SIZE"); + aosMD.FetchNameValue("SOZIP_CHUNK_SIZE"); printf("%11" CPL_FRMT_GB_WITHOUT_PREFIX "u %04d-%02d-%02d %02d:%02d:%02d %s %s " "%s\n", @@ -589,7 +588,6 @@ MAIN_START(nArgc, papszArgv) ? CPLSPrintf(" yes (%9s bytes) ", pszChunkSize) : " ", psEntry->pszName, osProperties.c_str()); - CSLDestroy(papszMD); } } VSICloseDir(psDir); diff --git a/apps/test_ogrsf.cpp b/apps/test_ogrsf.cpp index ec3a0a1d5841..ff5fd60a332b 100644 --- a/apps/test_ogrsf.cpp +++ b/apps/test_ogrsf.cpp @@ -4537,22 +4537,21 @@ static int TestVirtualIO(GDALDataset *poDS) return TRUE; } - char **papszFileList = LOG_ACTION(poDS->GetFileList()); - char **papszIter = papszFileList; + const CPLStringList aosFileList(LOG_ACTION(poDS->GetFileList())); CPLString osPath; int bAllPathIdentical = TRUE; - for (; *papszIter != nullptr; papszIter++) + for (const char *pszFilename : aosFileList) { - if (papszIter == papszFileList) - osPath = CPLGetPath(*papszIter); - else if (strcmp(osPath, CPLGetPath(*papszIter)) != 0) + if (pszFilename == aosFileList[0]) + osPath = CPLGetPath(pszFilename); + else if (strcmp(osPath, CPLGetPath(pszFilename)) != 0) { bAllPathIdentical = FALSE; break; } } CPLString osVirtPath; - if (bAllPathIdentical && CSLCount(papszFileList) > 1) + if (bAllPathIdentical && aosFileList.size() > 1) { osVirtPath = CPLFormFilename("/vsimem", CPLGetFilename(osPath), nullptr); @@ -4560,14 +4559,14 @@ static int TestVirtualIO(GDALDataset *poDS) } else osVirtPath = "/vsimem"; - papszIter = papszFileList; - for (; *papszIter != nullptr; papszIter++) + + for (const char *pszFilename : aosFileList) { const char *pszDestFile = - CPLFormFilename(osVirtPath, CPLGetFilename(*papszIter), nullptr); - /* CPLDebug("test_ogrsf", "Copying %s to %s", *papszIter, pszDestFile); + CPLFormFilename(osVirtPath, CPLGetFilename(pszFilename), nullptr); + /* CPLDebug("test_ogrsf", "Copying %s to %s", pszFilename, pszDestFile); */ - CPLCopyFile(pszDestFile, *papszIter); + CPLCopyFile(pszDestFile, pszFilename); } const char *pszVirtFile; @@ -4611,13 +4610,11 @@ static int TestVirtualIO(GDALDataset *poDS) } } - papszIter = papszFileList; - for (; *papszIter != nullptr; papszIter++) + for (const char *pszFilename : aosFileList) { VSIUnlink( - CPLFormFilename(osVirtPath, CPLGetFilename(*papszIter), nullptr)); + CPLFormFilename(osVirtPath, CPLGetFilename(pszFilename), nullptr)); } - CSLDestroy(papszFileList); return bRet; }