From 5f53d882e0af5775f944360869dd1a1bd6b04428 Mon Sep 17 00:00:00 2001 From: Nicklas Larsson Date: Tue, 30 Jan 2024 16:26:17 +0100 Subject: [PATCH] Modernise C++ code --- grass.cpp | 572 ++++++++++++++++++++--------------------- ogrgrass.h | 87 ++++--- ogrgrassdatasource.cpp | 133 ++++------ ogrgrassdriver.cpp | 2 - ogrgrasslayer.cpp | 190 +++++++------- 5 files changed, 462 insertions(+), 522 deletions(-) diff --git a/grass.cpp b/grass.cpp index 06068ce..5f1421e 100644 --- a/grass.cpp +++ b/grass.cpp @@ -30,7 +30,7 @@ * DEALINGS IN THE SOFTWARE. ****************************************************************************/ -#include +#include #include "cpl_string.h" #include "gdal_frmts.h" @@ -51,22 +51,23 @@ extern "C" #include #include - char *GPJ_grass_to_wkt(const struct Key_Value *, const struct Key_Value *, - int, int); + auto GPJ_grass_to_wkt(const struct Key_Value *, const struct Key_Value *, + int, int) -> char *; void GDALRegister_GRASS(); } -#define GRASS_MAX_COLORS 100000 // what is the right value - -CPL_CVSID("$Id$") +enum +{ + BUFF_SIZE = 200, + GRASS_MAX_COLORS = 100000 +}; /************************************************************************/ /* Grass2CPLErrorHook() */ /************************************************************************/ -static int Grass2CPLErrorHook(char *pszMessage, int bFatal) - +static auto Grass2CPLErrorHook(char *pszMessage, int bFatal) -> int { if (!bFatal) //CPLDebug( "GRASS", "%s", pszMessage ); @@ -78,6 +79,19 @@ static int Grass2CPLErrorHook(char *pszMessage, int bFatal) return 0; } +struct GRASSRasterPath +{ + std::string gisdbase; + std::string location; + std::string mapset; + std::string element; + std::string name; + + explicit GRASSRasterPath(const char *path); + auto isValid() -> bool; + auto isCellHD() -> bool; +}; + /************************************************************************/ /* ==================================================================== */ /* GRASSDataset */ @@ -90,27 +104,25 @@ class GRASSDataset final : public GDALDataset { friend class GRASSRasterBand; - char *pszGisdbase; - char *pszLocation; /* LOCATION_NAME */ - char *pszElement; /* cellhd or group */ + std::string osGisdbase; + std::string osLocation; /* LOCATION_NAME */ + std::string osElement; /* cellhd or group */ - struct Cell_head sCellInfo; /* raster region */ + struct Cell_head sCellInfo + { + }; /* raster region */ OGRSpatialReference m_oSRS{}; - double adfGeoTransform[6]; + std::array adfGeoTransform{0.0, 1.0, 0.0, 0.0, 0.0, 1.0}; public: - GRASSDataset(); - ~GRASSDataset() override; + explicit GRASSDataset(GRASSRasterPath &); - const OGRSpatialReference *GetSpatialRef() const override; - CPLErr GetGeoTransform(double *) override; + auto GetSpatialRef() const -> const OGRSpatialReference * override; + auto GetGeoTransform(double *) -> CPLErr override; - static GDALDataset *Open(GDALOpenInfo *); - - private: - static bool SplitPath(char *, char **, char **, char **, char **, char **); + static auto Open(GDALOpenInfo *) -> GDALDataset *; }; /************************************************************************/ @@ -123,74 +135,76 @@ class GRASSRasterBand final : public GDALRasterBand { friend class GRASSDataset; - char *pszCellName; - char *pszMapset; + std::string osCellName; + std::string osMapset; int hCell; int nGRSType; // GRASS raster type: CELL_TYPE, FCELL_TYPE, DCELL_TYPE bool nativeNulls; // use GRASS native NULL values - struct Colors sGrassColors; + struct Colors sGrassColors + { + }; GDALColorTable *poCT; - struct Cell_head sOpenWindow; /* the region when the raster was opened */ + struct Cell_head sOpenWindow + { + }; /* the region when the raster was opened */ int bHaveMinMax; - double dfCellMin; - double dfCellMax; + double dfCellMin{0.0}; + double dfCellMax{0.0}; double dfNoData; - bool valid; + bool valid{false}; public: - GRASSRasterBand(GRASSDataset *, int, const char *, const char *); + GRASSRasterBand(GRASSDataset *, int, std::string &, std::string &); ~GRASSRasterBand() override; - CPLErr IReadBlock(int, int, void *) override; - CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int, - GDALDataType, GSpacing nPixelSpace, GSpacing nLineSpace, - GDALRasterIOExtraArg *psExtraArg) override; - GDALColorInterp GetColorInterpretation() override; - GDALColorTable *GetColorTable() override; - double GetMinimum(int *pbSuccess = NULL) override; - double GetMaximum(int *pbSuccess = NULL) override; - double GetNoDataValue(int *pbSuccess = NULL) override; + auto IReadBlock(int, int, void *) -> CPLErr override; + auto IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int, + GDALDataType, GSpacing nPixelSpace, GSpacing nLineSpace, + GDALRasterIOExtraArg *psExtraArg) -> CPLErr override; + auto GetColorInterpretation() -> GDALColorInterp override; + auto GetColorTable() -> GDALColorTable * override; + auto GetMinimum(int *pbSuccess = nullptr) -> double override; + auto GetMaximum(int *pbSuccess = nullptr) -> double override; + auto GetNoDataValue(int *pbSuccess = nullptr) -> double override; private: void SetWindow(struct Cell_head *); - CPLErr ResetReading(struct Cell_head *); + auto ResetReading(struct Cell_head *) -> CPLErr; }; /************************************************************************/ /* GRASSRasterBand() */ /************************************************************************/ - GRASSRasterBand::GRASSRasterBand(GRASSDataset *poDSIn, int nBandIn, - const char *pszMapsetIn, - const char *pszCellNameIn) - + std::string &pszMapsetIn, + std::string &pszCellNameIn) + : osCellName(pszCellNameIn), osMapset(pszMapsetIn), + nGRSType(Rast_map_type(osCellName.c_str(), osMapset.c_str())) { - struct Cell_head sCellInfo; + struct Cell_head sCellInfo + { + }; // Note: GISDBASE, LOCATION_NAME ans MAPSET was set in GRASSDataset::Open this->poDS = poDSIn; this->nBand = nBandIn; - this->valid = false; - this->pszCellName = G_store((char *)pszCellNameIn); - this->pszMapset = G_store((char *)pszMapsetIn); - - Rast_get_cellhd((char *)pszCellName, (char *)pszMapset, &sCellInfo); - nGRSType = Rast_map_type((char *)pszCellName, (char *)pszMapset); + Rast_get_cellhd(osCellName.c_str(), osMapset.c_str(), &sCellInfo); /* -------------------------------------------------------------------- */ /* Get min/max values. */ /* -------------------------------------------------------------------- */ - struct FPRange sRange; + struct FPRange sRange + { + }; - if (Rast_read_fp_range((char *)pszCellName, (char *)pszMapset, &sRange) == - -1) + if (Rast_read_fp_range(osCellName.c_str(), osMapset.c_str(), &sRange) == -1) { bHaveMinMax = FALSE; } @@ -244,7 +258,7 @@ GRASSRasterBand::GRASSRasterBand(GRASSDataset *poDSIn, int nBandIn, } else { // maximum is not known or full range is used - CELL cval; + CELL cval = 0; this->eDataType = GDT_Int32; Rast_set_c_null_value(&cval, 1); dfNoData = (double)cval; @@ -253,7 +267,7 @@ GRASSRasterBand::GRASSRasterBand(GRASSDataset *poDSIn, int nBandIn, } else { // 3-4 bytes - CELL cval; + CELL cval = 0; this->eDataType = GDT_Int32; Rast_set_c_null_value(&cval, 1); dfNoData = (double)cval; @@ -262,7 +276,7 @@ GRASSRasterBand::GRASSRasterBand(GRASSDataset *poDSIn, int nBandIn, } else if (nGRSType == FCELL_TYPE) { - FCELL fval; + FCELL fval = NAN; this->eDataType = GDT_Float32; Rast_set_f_null_value(&fval, 1); dfNoData = (double)fval; @@ -270,7 +284,7 @@ GRASSRasterBand::GRASSRasterBand(GRASSDataset *poDSIn, int nBandIn, } else if (nGRSType == DCELL_TYPE) { - DCELL dval; + DCELL dval = NAN; this->eDataType = GDT_Float64; Rast_set_d_null_value(&dval, 1); dfNoData = (double)dval; @@ -283,18 +297,18 @@ GRASSRasterBand::GRASSRasterBand(GRASSDataset *poDSIn, int nBandIn, Rast_set_window(&(poDSIn->sCellInfo)); // open the raster only for actual reading hCell = -1; - memcpy((void *)&sOpenWindow, (void *)&(poDSIn->sCellInfo), - sizeof(struct Cell_head)); + memcpy(static_cast(&sOpenWindow), + static_cast(&(poDSIn->sCellInfo)), sizeof(struct Cell_head)); /* -------------------------------------------------------------------- */ /* Do we have a color table? */ /* -------------------------------------------------------------------- */ - poCT = NULL; - if (Rast_read_colors((char *)pszCellName, (char *)pszMapset, - &sGrassColors) == 1) + poCT = nullptr; + if (Rast_read_colors(osCellName.c_str(), osMapset.c_str(), &sGrassColors) == + 1) { - int maxcolor; - CELL min, max; + int maxcolor = 0; + CELL min = 0, max = 0; Rast_get_c_color_range(&min, &max, &sGrassColors); @@ -334,15 +348,15 @@ GRASSRasterBand::GRASSRasterBand(GRASSDataset *poDSIn, int nBandIn, poCT = new GDALColorTable(); for (int iColor = 0; iColor <= maxcolor; iColor++) { - int nRed, nGreen, nBlue; + int nRed = 0, nGreen = 0, nBlue = 0; GDALColorEntry sColor; if (Rast_get_c_color(&iColor, &nRed, &nGreen, &nBlue, &sGrassColors)) { - sColor.c1 = nRed; - sColor.c2 = nGreen; - sColor.c3 = nBlue; + sColor.c1 = (short)nRed; + sColor.c2 = (short)nGreen; + sColor.c3 = (short)nBlue; sColor.c4 = 255; poCT->SetColorEntry(iColor, &sColor); @@ -359,26 +373,28 @@ GRASSRasterBand::GRASSRasterBand(GRASSDataset *poDSIn, int nBandIn, } /* Create metadata entries for color table rules */ - char key[200], value[200]; + std::array value{}; + std::array key{}; int rcount = Rast_colors_count(&sGrassColors); - snprintf(value, sizeof(value), "%d", rcount); - this->SetMetadataItem("COLOR_TABLE_RULES_COUNT", value); + (void)std::snprintf(value.data(), BUFF_SIZE, "%d", rcount); + this->SetMetadataItem("COLOR_TABLE_RULES_COUNT", value.data()); /* Add the rules in reverse order */ for (int i = rcount - 1; i >= 0; i--) { - DCELL val1, val2; - unsigned char r1, g1, b1, r2, g2, b2; + DCELL val1 = NAN, val2 = NAN; + unsigned char r1 = 0, g1 = 0, b1 = 0, r2 = 0, g2 = 0, b2 = 0; Rast_get_fp_color_rule(&val1, &r1, &g1, &b1, &val2, &r2, &g2, &b2, &sGrassColors, i); - snprintf(key, sizeof(key), "COLOR_TABLE_RULE_RGB_%d", - rcount - i - 1); - snprintf(value, sizeof(value), "%e %e %d %d %d %d %d %d", val1, - val2, r1, g1, b1, r2, g2, b2); - this->SetMetadataItem(key, value); + (void)std::snprintf(key.data(), key.size(), + "COLOR_TABLE_RULE_RGB_%d", rcount - i - 1); + (void)std::snprintf(value.data(), value.size(), + "%e %e %d %d %d %d %d %d", val1, val2, r1, g1, + b1, r2, g2, b2); + this->SetMetadataItem(key.data(), value.data()); } } else @@ -395,7 +411,7 @@ GRASSRasterBand::GRASSRasterBand(GRASSDataset *poDSIn, int nBandIn, GRASSRasterBand::~GRASSRasterBand() { - if (poCT != NULL) + if (poCT != nullptr) { Rast_free_colors(&sGrassColors); delete poCT; @@ -403,12 +419,6 @@ GRASSRasterBand::~GRASSRasterBand() if (hCell >= 0) Rast_close(hCell); - - if (pszCellName) - G_free(pszCellName); - - if (pszMapset) - G_free(pszMapset); } /************************************************************************/ @@ -432,11 +442,13 @@ void GRASSRasterBand::SetWindow(struct Cell_head *sNewWindow) Rast_set_window(sNewWindow); /* Set GRASS env to the current raster, don't open the raster */ - G_setenv_nogisrc("GISDBASE", ((GRASSDataset *)poDS)->pszGisdbase); - G_setenv_nogisrc("LOCATION_NAME", ((GRASSDataset *)poDS)->pszLocation); - G_setenv_nogisrc("MAPSET", pszMapset); + G_setenv_nogisrc("GISDBASE", + (dynamic_cast(poDS))->osGisdbase.c_str()); + G_setenv_nogisrc("LOCATION_NAME", + (dynamic_cast(poDS))->osLocation.c_str()); + G_setenv_nogisrc("MAPSET", osMapset.c_str()); G_reset_mapsets(); - G_add_mapset_to_search_path(pszMapset); + G_add_mapset_to_search_path(osMapset.c_str()); } /************************************************************************/ @@ -447,7 +459,7 @@ void GRASSRasterBand::SetWindow(struct Cell_head *sNewWindow) /* */ /* Returns CE_Failure if fails, otherwise CE_None */ /************************************************************************/ -CPLErr GRASSRasterBand::ResetReading(struct Cell_head *sNewWindow) +auto GRASSRasterBand::ResetReading(struct Cell_head *sNewWindow) -> CPLErr { /* Check if the window has changed */ @@ -461,13 +473,15 @@ CPLErr GRASSRasterBand::ResetReading(struct Cell_head *sNewWindow) sNewWindow->cols != sOpenWindow.cols) { SetWindow(sNewWindow); - memcpy((void *)&sOpenWindow, (void *)sNewWindow, - sizeof(struct Cell_head)); + memcpy(static_cast(&sOpenWindow), + static_cast(sNewWindow), sizeof(struct Cell_head)); } else { /* The windows are identical, check current window */ - struct Cell_head sCurrentWindow; + struct Cell_head sCurrentWindow + { + }; Rast_get_window(&sCurrentWindow); @@ -492,25 +506,26 @@ CPLErr GRASSRasterBand::ResetReading(struct Cell_head *sNewWindow) /* */ /************************************************************************/ -CPLErr GRASSRasterBand::IReadBlock(int /*nBlockXOff*/, int nBlockYOff, - void *pImage) - +auto GRASSRasterBand::IReadBlock(int /*nBlockXOff*/, int nBlockYOff, + void *pImage) -> CPLErr { if (!this->valid) return CE_Failure; // Reset window because IRasterIO could be previously called. - if (ResetReading(&(((GRASSDataset *)poDS)->sCellInfo)) != CE_None) + if (ResetReading(&((dynamic_cast(poDS))->sCellInfo)) != + CE_None) { return CE_Failure; } // open for reading if (hCell < 0) { - if ((hCell = Rast_open_old((char *)pszCellName, (char *)pszMapset)) < 0) + hCell = Rast_open_old(osCellName.c_str(), osMapset.c_str()); + if (hCell < 0) { CPLError(CE_Failure, CPLE_AppDefined, - "GRASS: Cannot open raster '%s'", pszCellName); + "GRASS: Cannot open raster '%s'", osCellName.c_str()); return CE_Failure; } } @@ -527,22 +542,23 @@ CPLErr GRASSRasterBand::IReadBlock(int /*nBlockXOff*/, int nBlockYOff, cbuf[col] = (CELL)dfNoData; } - GDALCopyWords((void *)cbuf, GDT_Int32, sizeof(CELL), pImage, eDataType, - GDALGetDataTypeSize(eDataType) / 8, nBlockXSize); + GDALCopyWords(static_cast(cbuf), GDT_Int32, sizeof(CELL), + pImage, eDataType, GDALGetDataTypeSize(eDataType) / 8, + nBlockXSize); G_free(cbuf); } else if (eDataType == GDT_Int32) { - Rast_get_c_row(hCell, (CELL *)pImage, nBlockYOff); + Rast_get_c_row(hCell, static_cast(pImage), nBlockYOff); } else if (eDataType == GDT_Float32) { - Rast_get_f_row(hCell, (FCELL *)pImage, nBlockYOff); + Rast_get_f_row(hCell, static_cast(pImage), nBlockYOff); } else if (eDataType == GDT_Float64) { - Rast_get_d_row(hCell, (DCELL *)pImage, nBlockYOff); + Rast_get_d_row(hCell, static_cast(pImage), nBlockYOff); } // close to avoid confusion with other GRASS raster bands @@ -557,26 +573,28 @@ CPLErr GRASSRasterBand::IReadBlock(int /*nBlockXOff*/, int nBlockYOff, /* */ /************************************************************************/ -CPLErr GRASSRasterBand::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, - int nXSize, int nYSize, void *pData, - int nBufXSize, int nBufYSize, - GDALDataType eBufType, GSpacing nPixelSpace, - GSpacing nLineSpace, - GDALRasterIOExtraArg * /*psExtraArg*/) +auto GRASSRasterBand::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, + int nXSize, int nYSize, void *pData, + int nBufXSize, int nBufYSize, + GDALDataType eBufType, GSpacing nPixelSpace, + GSpacing nLineSpace, + GDALRasterIOExtraArg * /*psExtraArg*/) -> CPLErr { /* GRASS library does that, we have only calculate and reset the region in map units * and if the region has changed, reopen the raster */ /* Calculate the region */ - struct Cell_head sWindow; - struct Cell_head *psDsWindow; + struct Cell_head sWindow + { + }; + struct Cell_head *psDsWindow = nullptr; if (eRWFlag != GF_Read) return CE_Failure; if (!this->valid) return CE_Failure; - psDsWindow = &(((GRASSDataset *)poDS)->sCellInfo); + psDsWindow = &((dynamic_cast(poDS))->sCellInfo); sWindow.north = psDsWindow->north - nYOff * psDsWindow->ns_res; sWindow.south = sWindow.north - nYSize * psDsWindow->ns_res; @@ -598,18 +616,19 @@ CPLErr GRASSRasterBand::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, // open for reading if (hCell < 0) { - if ((hCell = Rast_open_old((char *)pszCellName, (char *)pszMapset)) < 0) + hCell = Rast_open_old(osCellName.c_str(), osMapset.c_str()); + if (hCell < 0) { CPLError(CE_Failure, CPLE_AppDefined, - "GRASS: Cannot open raster '%s'", pszCellName); + "GRASS: Cannot open raster '%s'", osCellName.c_str()); return CE_Failure; } } /* Read Data */ - CELL *cbuf = NULL; - FCELL *fbuf = NULL; - DCELL *dbuf = NULL; + CELL *cbuf = nullptr; + FCELL *fbuf = nullptr; + DCELL *dbuf = nullptr; bool direct = false; /* Reset space if default (0) */ @@ -642,55 +661,58 @@ CPLErr GRASSRasterBand::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, for (int row = 0; row < nBufYSize; row++) { - char *pnt = (char *)pData + row * nLineSpace; + char *pnt = static_cast(pData) + row * nLineSpace; if (nGRSType == CELL_TYPE) { if (direct) { - Rast_get_c_row(hCell, (CELL *)pnt, row); + Rast_get_c_row(hCell, reinterpret_cast(pnt), row); } else { Rast_get_c_row(hCell, cbuf, row); - /* Reset NULLs */ + /* Reset nullptrs */ for (int col = 0; col < nBufXSize; col++) { if (Rast_is_c_null_value(&(cbuf[col]))) cbuf[col] = (CELL)dfNoData; } - GDALCopyWords((void *)cbuf, GDT_Int32, sizeof(CELL), - (void *)pnt, eBufType, nPixelSpace, nBufXSize); + GDALCopyWords(static_cast(cbuf), GDT_Int32, + sizeof(CELL), static_cast(pnt), eBufType, + (int)nPixelSpace, nBufXSize); } } else if (nGRSType == FCELL_TYPE) { if (direct) { - Rast_get_f_row(hCell, (FCELL *)pnt, row); + Rast_get_f_row(hCell, reinterpret_cast(pnt), row); } else { Rast_get_f_row(hCell, fbuf, row); - GDALCopyWords((void *)fbuf, GDT_Float32, sizeof(FCELL), - (void *)pnt, eBufType, nPixelSpace, nBufXSize); + GDALCopyWords(static_cast(fbuf), GDT_Float32, + sizeof(FCELL), static_cast(pnt), eBufType, + (int)nPixelSpace, nBufXSize); } } else if (nGRSType == DCELL_TYPE) { if (direct) { - Rast_get_d_row(hCell, (DCELL *)pnt, row); + Rast_get_d_row(hCell, reinterpret_cast(pnt), row); } else { Rast_get_d_row(hCell, dbuf, row); - GDALCopyWords((void *)dbuf, GDT_Float64, sizeof(DCELL), - (void *)pnt, eBufType, nPixelSpace, nBufXSize); + GDALCopyWords(static_cast(dbuf), GDT_Float64, + sizeof(DCELL), static_cast(pnt), eBufType, + (int)nPixelSpace, nBufXSize); } } } @@ -713,10 +735,9 @@ CPLErr GRASSRasterBand::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, /* GetColorInterpretation() */ /************************************************************************/ -GDALColorInterp GRASSRasterBand::GetColorInterpretation() - +auto GRASSRasterBand::GetColorInterpretation() -> GDALColorInterp { - if (poCT != NULL) + if (poCT != nullptr) return GCI_PaletteIndex; else return GCI_GrayIndex; @@ -726,8 +747,7 @@ GDALColorInterp GRASSRasterBand::GetColorInterpretation() /* GetColorTable() */ /************************************************************************/ -GDALColorTable *GRASSRasterBand::GetColorTable() - +auto GRASSRasterBand::GetColorTable() -> GDALColorTable * { return poCT; } @@ -736,8 +756,7 @@ GDALColorTable *GRASSRasterBand::GetColorTable() /* GetMinimum() */ /************************************************************************/ -double GRASSRasterBand::GetMinimum(int *pbSuccess) - +auto GRASSRasterBand::GetMinimum(int *pbSuccess) -> double { if (pbSuccess) *pbSuccess = bHaveMinMax; @@ -755,8 +774,7 @@ double GRASSRasterBand::GetMinimum(int *pbSuccess) /* GetMaximum() */ /************************************************************************/ -double GRASSRasterBand::GetMaximum(int *pbSuccess) - +auto GRASSRasterBand::GetMaximum(int *pbSuccess) -> double { if (pbSuccess) *pbSuccess = bHaveMinMax; @@ -764,9 +782,8 @@ double GRASSRasterBand::GetMaximum(int *pbSuccess) if (bHaveMinMax) return dfCellMax; - else if (eDataType == GDT_Float32 || eDataType == GDT_Float64) - return 4294967295.0; - else if (eDataType == GDT_UInt32) + else if (eDataType == GDT_Float32 || eDataType == GDT_Float64 || + eDataType == GDT_UInt32) return 4294967295.0; else if (eDataType == GDT_UInt16) return 65535; @@ -778,8 +795,7 @@ double GRASSRasterBand::GetMaximum(int *pbSuccess) /* GetNoDataValue() */ /************************************************************************/ -double GRASSRasterBand::GetNoDataValue(int *pbSuccess) - +auto GRASSRasterBand::GetNoDataValue(int *pbSuccess) -> double { if (pbSuccess) *pbSuccess = TRUE; @@ -797,43 +813,18 @@ double GRASSRasterBand::GetNoDataValue(int *pbSuccess) /* GRASSDataset() */ /************************************************************************/ -GRASSDataset::GRASSDataset() +GRASSDataset::GRASSDataset(GRASSRasterPath &gpath) + : osGisdbase(gpath.gisdbase), osLocation(gpath.location), + osElement(gpath.element) { m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER); - - adfGeoTransform[0] = 0.0; - adfGeoTransform[1] = 1.0; - adfGeoTransform[2] = 0.0; - adfGeoTransform[3] = 0.0; - adfGeoTransform[4] = 0.0; - adfGeoTransform[5] = 1.0; - pszGisdbase = NULL; - pszLocation = NULL; - pszElement = NULL; -} - -/************************************************************************/ -/* ~GRASSDataset() */ -/************************************************************************/ - -GRASSDataset::~GRASSDataset() -{ - - if (pszGisdbase) - G_free(pszGisdbase); - - if (pszLocation) - G_free(pszLocation); - - if (pszElement) - G_free(pszElement); } /************************************************************************/ /* GetSpatialRef() */ /************************************************************************/ -const OGRSpatialReference *GRASSDataset::GetSpatialRef() const +auto GRASSDataset::GetSpatialRef() const -> const OGRSpatialReference * { return m_oSRS.IsEmpty() ? nullptr : &m_oSRS; } @@ -842,88 +833,30 @@ const OGRSpatialReference *GRASSDataset::GetSpatialRef() const /* GetGeoTransform() */ /************************************************************************/ -CPLErr GRASSDataset::GetGeoTransform(double *padfGeoTransform) +auto GRASSDataset::GetGeoTransform(double *padfGeoTransform) -> CPLErr { - memcpy(padfGeoTransform, adfGeoTransform, sizeof(double) * 6); + memcpy(padfGeoTransform, adfGeoTransform.data(), sizeof(double) * 6); return CE_None; } -/************************************************************************/ -/* SplitPath() */ -/* Split full path to cell or group to: */ -/* gisdbase, location, mapset, element, name */ -/* New string are allocated and should be freed when no longer needed. */ -/* */ -/* Returns: true - OK */ -/* false - failed */ -/************************************************************************/ -bool GRASSDataset::SplitPath(char *path, char **gisdbase, char **location, - char **mapset, char **element, char **name) -{ - char *p; - char *ptr[5]; - char *tmp; - int i = 0; - - *gisdbase = NULL; - *location = NULL; - *mapset = NULL; - *element = NULL; - *name = NULL; - - if (!path || strlen(path) == 0) - return false; - - tmp = G_store(path); - - while ((p = strrchr(tmp, '/')) != NULL && i < 4) - { - *p = '\0'; - - if (strlen(p + 1) == 0) /* repeated '/' */ - continue; - - ptr[i++] = p + 1; - } - - /* Note: empty GISDBASE == 0 is not accepted (relative path) */ - if (i != 4) - { - G_free(tmp); - return false; - } - - *gisdbase = G_store(tmp); - *location = G_store(ptr[3]); - *mapset = G_store(ptr[2]); - *element = G_store(ptr[1]); - *name = G_store(ptr[0]); - - G_free(tmp); - return true; -} - /************************************************************************/ /* Open() */ /************************************************************************/ -typedef int (*GrassErrorHandler)(const char *, int); - -GDALDataset *GRASSDataset::Open(GDALOpenInfo *poOpenInfo) +using GrassErrorHandler = auto(*)(const char *, int) -> int; +auto GRASSDataset::Open(GDALOpenInfo *poOpenInfo) -> GDALDataset * { - char *pszGisdb = NULL, *pszLoc = NULL; - char *pszMapset = NULL, *pszElem = NULL, *pszName = NULL; - char **papszCells = NULL; - char **papszMapsets = NULL; + char **papszCells = nullptr; + char **papszMapsets = nullptr; /* -------------------------------------------------------------------- */ /* Does this even look like a grass file path? */ /* -------------------------------------------------------------------- */ - if (strstr(poOpenInfo->pszFilename, "/cellhd/") == NULL && - strstr(poOpenInfo->pszFilename, "/group/") == NULL) - return NULL; + if (strstr(poOpenInfo->pszFilename, "/cellhd/") == nullptr && + strstr(poOpenInfo->pszFilename, "/group/") == nullptr) + return nullptr; /* Always init, if no rasters are opened G_no_gisinit resets the projection and * rasters in different projection may be then opened */ @@ -935,91 +868,83 @@ GDALDataset *GRASSDataset::Open(GDALOpenInfo *poOpenInfo) G_no_gisinit(); // Doesn't check write permissions for mapset compare to G_gisinit // Set error function - G_set_error_routine((GrassErrorHandler)Grass2CPLErrorHook); + G_set_error_routine(GrassErrorHandler(Grass2CPLErrorHook)); // GISBASE is path to the directory where GRASS is installed, if (!getenv("GISBASE")) { - static char *gisbaseEnv = NULL; + static char *gisbaseEnv = nullptr; const char *gisbase = GRASS_GISBASE; CPLError(CE_Warning, CPLE_AppDefined, "GRASS warning: GISBASE " "environment variable was not set, using:\n%s", gisbase); - char buf[2000]; - snprintf(buf, sizeof(buf), "GISBASE=%s", gisbase); - buf[sizeof(buf) - 1] = '\0'; + std::array buf{}; + int res = std::snprintf(buf.data(), BUFF_SIZE, "GISBASE=%s", gisbase); + if (res >= BUFF_SIZE) + { + CPLError( + CE_Warning, CPLE_AppDefined, + "GRASS warning: GISBASE environment variable was too long.\n"); + return nullptr; + } CPLFree(gisbaseEnv); - gisbaseEnv = CPLStrdup(buf); + gisbaseEnv = CPLStrdup(buf.data()); putenv(gisbaseEnv); } - if (!SplitPath(poOpenInfo->pszFilename, &pszGisdb, &pszLoc, &pszMapset, - &pszElem, &pszName)) - { - return NULL; - } + GRASSRasterPath gp = GRASSRasterPath(poOpenInfo->pszFilename); /* -------------------------------------------------------------------- */ /* Check element name */ /* -------------------------------------------------------------------- */ - if (strcmp(pszElem, "cellhd") != 0 && strcmp(pszElem, "group") != 0) + if (!gp.isValid()) { - G_free(pszGisdb); - G_free(pszLoc); - G_free(pszMapset); - G_free(pszElem); - G_free(pszName); - return NULL; + return nullptr; } /* -------------------------------------------------------------------- */ /* Set GRASS variables */ /* -------------------------------------------------------------------- */ - G_setenv_nogisrc("GISDBASE", pszGisdb); - G_setenv_nogisrc("LOCATION_NAME", pszLoc); - G_setenv_nogisrc("MAPSET", - pszMapset); // group is searched only in current mapset + G_setenv_nogisrc("GISDBASE", gp.gisdbase.c_str()); + G_setenv_nogisrc("LOCATION_NAME", gp.location.c_str()); + G_setenv_nogisrc( + "MAPSET", + gp.mapset.c_str()); // group is searched only in current mapset G_reset_mapsets(); - G_add_mapset_to_search_path(pszMapset); + G_add_mapset_to_search_path(gp.mapset.c_str()); /* -------------------------------------------------------------------- */ /* Check if this is a valid grass cell. */ /* -------------------------------------------------------------------- */ - if (strcmp(pszElem, "cellhd") == 0) + if (gp.isCellHD()) { - if (G_find_file2("cell", pszName, pszMapset) == NULL) + if (G_find_file2("cell", gp.name.c_str(), gp.mapset.c_str()) == nullptr) { - G_free(pszGisdb); - G_free(pszLoc); - G_free(pszMapset); - G_free(pszElem); - G_free(pszName); - return NULL; + return nullptr; } - papszMapsets = CSLAddString(papszMapsets, pszMapset); - papszCells = CSLAddString(papszCells, pszName); + papszMapsets = CSLAddString(papszMapsets, gp.mapset.c_str()); + papszCells = CSLAddString(papszCells, gp.name.c_str()); } + /* -------------------------------------------------------------------- */ /* Check if this is a valid GRASS imagery group. */ /* -------------------------------------------------------------------- */ else { - struct Ref ref; + struct Ref ref + { + }; I_init_group_ref(&ref); - if (I_get_group_ref(pszName, &ref) == 0) + bool has_group_ref = I_get_group_ref(gp.name.c_str(), &ref); + if (!has_group_ref || ref.nfiles <= 0) { - G_free(pszGisdb); - G_free(pszLoc); - G_free(pszMapset); - G_free(pszElem); - G_free(pszName); - return NULL; + return nullptr; } for (int iRef = 0; iRef < ref.nfiles; iRef++) @@ -1032,20 +957,18 @@ GDALDataset *GRASSDataset::Open(GDALOpenInfo *poOpenInfo) I_free_group_ref(&ref); } - G_free(pszMapset); - G_free(pszName); - /* -------------------------------------------------------------------- */ /* Create a corresponding GDALDataset. */ /* -------------------------------------------------------------------- */ - GRASSDataset *poDS = new GRASSDataset(); + auto poDS = new GRASSDataset(gp); /* notdef: should only allow read access to an existing cell, right? */ poDS->eAccess = poOpenInfo->eAccess; - poDS->pszGisdbase = pszGisdb; - poDS->pszLocation = pszLoc; - poDS->pszElement = pszElem; + if (!papszCells) + { + return nullptr; + } /* -------------------------------------------------------------------- */ /* Capture some information from the file that is of interest. */ @@ -1066,10 +989,9 @@ GDALDataset *GRASSDataset::Open(GDALOpenInfo *poOpenInfo) /* -------------------------------------------------------------------- */ /* Try to get a projection definition. */ /* -------------------------------------------------------------------- */ - struct Key_Value *projinfo, *projunits; + struct Key_Value *projinfo = G_get_projinfo(); + struct Key_Value *projunits = G_get_projunits(); - projinfo = G_get_projinfo(); - projunits = G_get_projunits(); char *pszWKT = GPJ_grass_to_wkt(projinfo, projunits, 0, 0); if (projinfo) G_free_key_value(projinfo); @@ -1082,10 +1004,11 @@ GDALDataset *GRASSDataset::Open(GDALOpenInfo *poOpenInfo) /* -------------------------------------------------------------------- */ /* Create band information objects. */ /* -------------------------------------------------------------------- */ - for (int iBand = 0; papszCells[iBand] != NULL; iBand++) + for (int iBand = 0; papszCells[iBand] != nullptr; iBand++) { - GRASSRasterBand *rb = new GRASSRasterBand( - poDS, iBand + 1, papszMapsets[iBand], papszCells[iBand]); + std::string msets = std::string(papszMapsets[iBand]); + std::string cells = std::string(papszCells[iBand]); + auto rb = new GRASSRasterBand(poDS, iBand + 1, msets, cells); if (!rb->valid) { @@ -1093,7 +1016,7 @@ GDALDataset *GRASSDataset::Open(GDALOpenInfo *poOpenInfo) "GRASS: Cannot open raster band %d", iBand); delete rb; delete poDS; - return NULL; + return nullptr; } poDS->SetBand(iBand + 1, rb); @@ -1111,12 +1034,67 @@ GDALDataset *GRASSDataset::Open(GDALOpenInfo *poOpenInfo) CPLError(CE_Failure, CPLE_NotSupported, "The GRASS driver does not support update access to existing" " datasets.\n"); - return NULL; + return nullptr; } return poDS; } +/************************************************************************/ +/* GRASSRasterPath */ +/************************************************************************/ + +GRASSRasterPath::GRASSRasterPath(const char *path) +{ + if (!path || std::strlen(path) == 0) + return; + + char *p = nullptr; + std::array ptr{}; + int i = 0; + auto tmp = std::unique_ptr(new char[std::strlen(path) + 1]); + + std::strcpy(tmp.get(), path); + + while ((p = std::strrchr(tmp.get(), '/')) != nullptr && i < 4) + { + *p = '\0'; + + if (std::strlen(p + 1) == 0) /* repeated '/' */ + continue; + + ptr[i++] = p + 1; + } + + /* Note: empty GISDBASE == 0 is not accepted (relative path) */ + if (i != 4) + { + return; + } + + gisdbase = std::string(tmp.get()); + location = std::string(ptr[3]); + mapset = std::string(ptr[2]); + element = std::string(ptr[1]); + name = std::string(ptr[0]); + + return; +} + +auto GRASSRasterPath::isValid() -> bool +{ + if (name.empty() || (element != "cellhd" && element != "group")) + { + return false; + } + return true; +} + +auto GRASSRasterPath::isCellHD() -> bool +{ + return element == "cellhd"; +} + /************************************************************************/ /* GDALRegister_GRASS() */ /************************************************************************/ @@ -1126,10 +1104,10 @@ void GDALRegister_GRASS() if (!GDAL_CHECK_VERSION("GDAL/GRASS driver")) return; - if (GDALGetDriverByName("GRASS") != NULL) + if (GDALGetDriverByName("GRASS") != nullptr) return; - GDALDriver *poDriver = new GDALDriver(); + auto poDriver = new GDALDriver(); poDriver->SetDescription("GRASS"); poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES"); diff --git a/ogrgrass.h b/ogrgrass.h index b1855a2..1ea5a69 100644 --- a/ogrgrass.h +++ b/ogrgrass.h @@ -1,5 +1,4 @@ /****************************************************************************** - * $Id$ * * Project: OpenGIS Simple Features Reference Implementation * Purpose: Private definitions for OGR/GRASS driver. @@ -51,28 +50,32 @@ class OGRGRASSLayer final : public OGRLayer virtual ~OGRGRASSLayer(); // Layer info - OGRFeatureDefn *GetLayerDefn() override + auto GetName() -> const char * override + { + return osName.c_str(); + } + auto GetLayerDefn() -> OGRFeatureDefn * override { return poFeatureDefn; } - GIntBig GetFeatureCount(int) override; - OGRErr GetExtent(OGREnvelope *psExtent, int bForce) override; - virtual OGRErr GetExtent(int iGeomField, OGREnvelope *psExtent, - int bForce) override + auto GetFeatureCount(int) -> GIntBig override; + auto GetExtent(OGREnvelope *psExtent, int bForce) -> OGRErr override; + virtual auto GetExtent(int iGeomField, OGREnvelope *psExtent, int bForce) + -> OGRErr override { return OGRLayer::GetExtent(iGeomField, psExtent, bForce); } - virtual OGRSpatialReference *GetSpatialRef() override; - int TestCapability(const char *) override; + virtual auto GetSpatialRef() -> OGRSpatialReference * override; + auto TestCapability(const char *) -> int override; // Reading void ResetReading() override; - virtual OGRErr SetNextByIndex(GIntBig nIndex) override; - OGRFeature *GetNextFeature() override; - OGRFeature *GetFeature(GIntBig nFeatureId) override; + virtual auto SetNextByIndex(GIntBig nIndex) -> OGRErr override; + auto GetNextFeature() -> OGRFeature * override; + auto GetFeature(GIntBig nFeatureId) -> OGRFeature * override; // Filters - virtual OGRErr SetAttributeFilter(const char *query) override; + virtual auto SetAttributeFilter(const char *query) -> OGRErr override; virtual void SetSpatialFilter(OGRGeometry *poGeomIn) override; virtual void SetSpatialFilter(int iGeomField, OGRGeometry *poGeom) override { @@ -80,12 +83,12 @@ class OGRGRASSLayer final : public OGRLayer } private: - char *pszName; + std::string osName; OGRSpatialReference *poSRS; OGRFeatureDefn *poFeatureDefn; char *pszQuery; // Attribute filter string - int iNextId; + GIntBig iNextId; int nTotalCount; int iLayer; // Layer number int iLayerIndex; // Layer index (in GRASS category index) @@ -110,21 +113,21 @@ class OGRGRASSLayer final : public OGRLayer struct line_pnts *poPoints; struct line_cats *poCats; - bool StartDbDriver(); - bool StopDbDriver(); + auto StartDbDriver() -> bool; + auto StopDbDriver() -> bool; - OGRGeometry *GetFeatureGeometry(long nFeatureId, int *cat); - bool SetAttributes(OGRFeature *feature, dbTable *table); + auto GetFeatureGeometry(long nFeatureId, int *cat) -> OGRGeometry *; + auto SetAttributes(OGRFeature *feature, dbTable *table) -> bool; // Features matching spatial filter for ALL features/elements in GRASS char *paSpatialMatch; - bool SetSpatialMatch(); + auto SetSpatialMatch() -> bool; // Features matching attribute filter for ALL features/elements in GRASS char *paQueryMatch; - bool OpenSequentialCursor(); - bool ResetSequentialCursor(); - bool SetQueryMatch(); + auto OpenSequentialCursor() -> bool; + auto ResetSequentialCursor() -> bool; + auto SetQueryMatch() -> bool; }; /************************************************************************/ @@ -133,38 +136,40 @@ class OGRGRASSLayer final : public OGRLayer class OGRGRASSDataSource final : public OGRDataSource { public: - OGRGRASSDataSource(); + OGRGRASSDataSource() = default; virtual ~OGRGRASSDataSource(); - bool Open(const char *, bool bUpdate, bool bTestOpen, - bool bSingleNewFile = false); + auto Open(const char *, bool bUpdate, bool bTestOpen, + bool bSingleNewFile = false) -> bool; - const char *GetName() override + auto GetName() -> const char * override { - return pszName; + return osName.c_str(); } - int GetLayerCount() override + auto GetLayerCount() -> int override { return nLayers; } - OGRLayer *GetLayer(int) override; + auto GetLayer(int) -> OGRLayer * override; - int TestCapability(const char *) override; + auto TestCapability(const char *) -> int override; private: - OGRGRASSLayer **papoLayers; - char *pszName; // Date source name - char *pszGisdbase; // GISBASE - char *pszLocation; // location name - char *pszMapset; // mapset name - char *pszMap; // name of vector map - - struct Map_info map; - int nLayers; + OGRGRASSLayer **papoLayers{nullptr}; + std::string osName; // Date source name + std::string osGisdbase; // GISBASE + std::string osLocation; // location name + std::string osMapset; // mapset name + std::string osMap; // name of vector map + + struct Map_info map + { + }; + int nLayers{0}; - bool bOpened; + bool bOpened{false}; - static bool SplitPath(char *, char **, char **, char **, char **); + auto SetPath(const char *) -> bool; }; #endif /* ndef OGRGRASS_H_INCLUDED */ diff --git a/ogrgrassdatasource.cpp b/ogrgrassdatasource.cpp index 4220a52..fd70b34 100644 --- a/ogrgrassdatasource.cpp +++ b/ogrgrassdatasource.cpp @@ -27,16 +27,16 @@ * DEALINGS IN THE SOFTWARE. ****************************************************************************/ +#include + #include "ogrgrass.h" #include "cpl_conv.h" #include "cpl_string.h" -CPL_CVSID("$Id$") - /************************************************************************/ /* Grass2CPLErrorHook() */ /************************************************************************/ -static int Grass2OGRErrorHook(char *pszMessage, int bFatal) +static auto Grass2CPLErrorHook(char *pszMessage, int bFatal) -> int { if (!bFatal) CPLError(CE_Warning, CPLE_AppDefined, "GRASS warning: %s", pszMessage); @@ -47,21 +47,6 @@ static int Grass2OGRErrorHook(char *pszMessage, int bFatal) return 0; } -/************************************************************************/ -/* OGRGRASSDataSource() */ -/************************************************************************/ -OGRGRASSDataSource::OGRGRASSDataSource() -{ - pszName = NULL; - pszGisdbase = NULL; - pszLocation = NULL; - pszMapset = NULL; - pszMap = NULL; - papoLayers = NULL; - nLayers = 0; - bOpened = FALSE; -} - /************************************************************************/ /* ~OGRGRASSDataSource() */ /************************************************************************/ @@ -70,19 +55,6 @@ OGRGRASSDataSource::~OGRGRASSDataSource() for (int i = 0; i < nLayers; i++) delete papoLayers[i]; - if (pszName) - CPLFree(pszName); - if (papoLayers) - CPLFree(papoLayers); - if (pszGisdbase) - G_free(pszGisdbase); - if (pszLocation) - G_free(pszLocation); - if (pszMapset) - G_free(pszMapset); - if (pszMap) - G_free(pszMap); - if (bOpened) Vect_close(&map); } @@ -91,27 +63,25 @@ OGRGRASSDataSource::~OGRGRASSDataSource() /* Open() */ /************************************************************************/ -typedef int (*GrassErrorHandler)(const char *, int); +using GrassErrorHandler = auto(*)(const char *, int) -> int; -bool OGRGRASSDataSource::Open(const char *pszNewName, bool /*bUpdate*/, - bool bTestOpen, bool /*bSingleNewFileIn*/) +auto OGRGRASSDataSource::Open(const char *pszNewName, bool /*bUpdate*/, + bool bTestOpen, bool /*bSingleNewFileIn*/) -> bool { VSIStatBuf stat; CPLAssert(nLayers == 0); - pszName = CPLStrdup(pszNewName); // Released by destructor - /* -------------------------------------------------------------------- */ /* Do the given path contains 'vector' and 'head'? */ /* -------------------------------------------------------------------- */ - if (strstr(pszName, "vector") == nullptr || - strstr(pszName, "head") == nullptr) + if (std::strstr(pszNewName, "vector") == nullptr || + std::strstr(pszNewName, "head") == nullptr) { if (!bTestOpen) { CPLError(CE_Failure, CPLE_AppDefined, - "%s is not GRASS vector, access failed.\n", pszName); + "%s is not GRASS vector, access failed.\n", pszNewName); } return false; } @@ -119,12 +89,12 @@ bool OGRGRASSDataSource::Open(const char *pszNewName, bool /*bUpdate*/, /* -------------------------------------------------------------------- */ /* Is the given a regular file? */ /* -------------------------------------------------------------------- */ - if (CPLStat(pszName, &stat) != 0 || !VSI_ISREG(stat.st_mode)) + if (CPLStat(pszNewName, &stat) != 0 || !VSI_ISREG(stat.st_mode)) { if (!bTestOpen) { CPLError(CE_Failure, CPLE_AppDefined, - "%s is not GRASS vector, access failed.\n", pszName); + "%s is not GRASS vector, access failed.\n", pszNewName); } return false; @@ -133,21 +103,21 @@ bool OGRGRASSDataSource::Open(const char *pszNewName, bool /*bUpdate*/, /* -------------------------------------------------------------------- */ /* Parse datasource name */ /* -------------------------------------------------------------------- */ - if (!SplitPath(pszName, &pszGisdbase, &pszLocation, &pszMapset, &pszMap)) + if (!SetPath(pszNewName)) { if (!bTestOpen) { CPLError(CE_Failure, CPLE_AppDefined, "%s is not GRASS datasource name, access failed.\n", - pszName); + pszNewName); } return false; } - CPLDebug("GRASS", "Gisdbase: %s", pszGisdbase); - CPLDebug("GRASS", "Location: %s", pszLocation); - CPLDebug("GRASS", "Mapset: %s", pszMapset); - CPLDebug("GRASS", "Map: %s", pszMap); + CPLDebug("GRASS", "Gisdbase: %s", osGisdbase.c_str()); + CPLDebug("GRASS", "Location: %s", osLocation.c_str()); + CPLDebug("GRASS", "Mapset: %s", osMapset.c_str()); + CPLDebug("GRASS", "Map: %s", osMap.c_str()); /* -------------------------------------------------------------------- */ /* Init GRASS library */ @@ -162,12 +132,11 @@ bool OGRGRASSDataSource::Open(const char *pszNewName, bool /*bUpdate*/, "GRASS warning: GISBASE " "environment variable was not set, using:\n%s", gisbase); - char buf[2000]; - snprintf(buf, sizeof(buf), "GISBASE=%s", gisbase); - buf[sizeof(buf) - 1] = '\0'; + std::array buf{}; + (void)snprintf(buf.data(), buf.size(), "GISBASE=%s", gisbase); CPLFree(gisbaseEnv); - gisbaseEnv = CPLStrdup(buf); + gisbaseEnv = CPLStrdup(buf.data()); putenv(gisbaseEnv); } @@ -180,27 +149,27 @@ bool OGRGRASSDataSource::Open(const char *pszNewName, bool /*bUpdate*/, G_no_gisinit(); // Set error function - G_set_error_routine((GrassErrorHandler)Grass2OGRErrorHook); + G_set_error_routine(GrassErrorHandler(Grass2CPLErrorHook)); /* -------------------------------------------------------------------- */ /* Set GRASS variables */ /* -------------------------------------------------------------------- */ - G_setenv_nogisrc("GISDBASE", pszGisdbase); - G_setenv_nogisrc("LOCATION_NAME", pszLocation); - G_setenv_nogisrc("MAPSET", pszMapset); + G_setenv_nogisrc("GISDBASE", osGisdbase.c_str()); + G_setenv_nogisrc("LOCATION_NAME", osLocation.c_str()); + G_setenv_nogisrc("MAPSET", osMapset.c_str()); G_reset_mapsets(); - G_add_mapset_to_search_path(pszMapset); + G_add_mapset_to_search_path(osMapset.c_str()); /* -------------------------------------------------------------------- */ /* Open GRASS vector map */ /* -------------------------------------------------------------------- */ Vect_set_open_level(2); - int level = Vect_open_old(&map, pszMap, pszMapset); + int level = Vect_open_old(&map, osMap.c_str(), osMapset.c_str()); if (level < 2) { CPLError(CE_Failure, CPLE_AppDefined, - "Cannot open GRASS vector %s on level 2.\n", pszName); + "Cannot open GRASS vector %s on level 2.\n", osName.c_str()); return false; } @@ -215,11 +184,11 @@ bool OGRGRASSDataSource::Open(const char *pszNewName, bool /*bUpdate*/, for (int i = 0; i < ncidx; i++) { // Create the layer object - OGRGRASSLayer *poLayer = new OGRGRASSLayer(i, &map); + auto poLayer = new OGRGRASSLayer(i, &map); // Add layer to data source layer list - papoLayers = (OGRGRASSLayer **)CPLRealloc( - papoLayers, sizeof(OGRGRASSLayer *) * (nLayers + 1)); + papoLayers = reinterpret_cast( + CPLRealloc(papoLayers, sizeof(OGRGRASSLayer *) * (nLayers + 1))); papoLayers[nLayers++] = poLayer; } @@ -231,7 +200,7 @@ bool OGRGRASSDataSource::Open(const char *pszNewName, bool /*bUpdate*/, /************************************************************************/ /* TestCapability() */ /************************************************************************/ -int OGRGRASSDataSource::TestCapability(const char * /* pszCap*/) +auto OGRGRASSDataSource::TestCapability(const char * /* pszCap*/) -> int { return FALSE; } @@ -239,10 +208,10 @@ int OGRGRASSDataSource::TestCapability(const char * /* pszCap*/) /************************************************************************/ /* GetLayer() */ /************************************************************************/ -OGRLayer *OGRGRASSDataSource::GetLayer(int iLayer) +auto OGRGRASSDataSource::GetLayer(int iLayer) -> OGRLayer * { if (iLayer < 0 || iLayer >= nLayers) - return NULL; + return nullptr; else return papoLayers[iLayer]; } @@ -256,26 +225,25 @@ OGRLayer *OGRGRASSDataSource::GetLayer(int iLayer) /* Returns: true - OK */ /* false - failed */ /************************************************************************/ -bool OGRGRASSDataSource::SplitPath(char *path, char **gisdbase, char **location, - char **mapset, char **map) +auto OGRGRASSDataSource::SetPath(const char *path) -> bool { - char *p, *ptr[5], *tmp; - int i = 0; - - CPLDebug("GRASS", "OGRGRASSDataSource::SplitPath"); + CPLDebug("GRASS", "OGRGRASSDataSource::SetPath"); - *gisdbase = *location = *mapset = *map = NULL; - - if (!path || strlen(path) == 0) + if (!path || std::strlen(path) == 0) return false; - tmp = G_store(path); + char *p = nullptr; + std::array ptr{}; + int i = 0; + auto tmp = std::unique_ptr(new char[std::strlen(path) + 1]); + + std::strcpy(tmp.get(), path); - while ((p = strrchr(tmp, '/')) != NULL && i < 5) + while ((p = std::strrchr(tmp.get(), '/')) != nullptr && i < 5) { *p = '\0'; - if (strlen(p + 1) == 0) /* repeated '/' */ + if (std::strlen(p + 1) == 0) /* repeated '/' */ continue; ptr[i++] = p + 1; @@ -284,20 +252,19 @@ bool OGRGRASSDataSource::SplitPath(char *path, char **gisdbase, char **location, /* Note: empty GISDBASE == 0 is not accepted (relative path) */ if (i != 5) { - free(tmp); return false; } - if (strcmp(ptr[0], "head") != 0 || strcmp(ptr[2], "vector") != 0) + if (std::strcmp(ptr[0], "head") != 0 || std::strcmp(ptr[2], "vector") != 0) { return false; } - *gisdbase = G_store(tmp); - *location = G_store(ptr[4]); - *mapset = G_store(ptr[3]); - *map = G_store(ptr[1]); + osName = std::string(path); + osGisdbase = std::string(tmp.get()); + osLocation = std::string(ptr[4]); + osMapset = std::string(ptr[3]); + osMap = std::string(ptr[1]); - free(tmp); return true; } diff --git a/ogrgrassdriver.cpp b/ogrgrassdriver.cpp index bc83cad..a33fde4 100644 --- a/ogrgrassdriver.cpp +++ b/ogrgrassdriver.cpp @@ -35,8 +35,6 @@ extern "C" void RegisterOGRGRASS(); } -CPL_CVSID("$Id$") - /************************************************************************/ /* Open() */ /************************************************************************/ diff --git a/ogrgrasslayer.cpp b/ogrgrasslayer.cpp index 7e7938e..3a4d223 100644 --- a/ogrgrasslayer.cpp +++ b/ogrgrasslayer.cpp @@ -27,46 +27,37 @@ * DEALINGS IN THE SOFTWARE. ****************************************************************************/ -#include +#include + #include "ogrgrass.h" #include "cpl_conv.h" -CPL_CVSID("$Id$") - /************************************************************************/ /* OGRGRASSLayer() */ /************************************************************************/ OGRGRASSLayer::OGRGRASSLayer(int layerIndex, struct Map_info *map) + : poSRS(nullptr), pszQuery(nullptr), iNextId(0), + iLayer(Vect_cidx_get_field_number(map, layerIndex)), + iLayerIndex(layerIndex), poMap(map), + poLink(Vect_get_field(poMap, iLayer)), iCurrentCat(0), + poPoints(Vect_new_line_struct()), poCats(Vect_new_cats_struct()), + paSpatialMatch(nullptr), paQueryMatch(nullptr) { CPLDebug("GRASS", "OGRGRASSLayer::OGRGRASSLayer layerIndex = %d", layerIndex); - iLayerIndex = layerIndex; - poMap = map; - poSRS = NULL; - iNextId = 0; - poPoints = Vect_new_line_struct(); - poCats = Vect_new_cats_struct(); - pszQuery = NULL; - paQueryMatch = NULL; - paSpatialMatch = NULL; - iCurrentCat = 0; - - iLayer = Vect_cidx_get_field_number(poMap, iLayerIndex); CPLDebug("GRASS", "iLayer = %d", iLayer); - poLink = Vect_get_field(poMap, iLayer); // May be NULL if not defined + // poLink may be NULL if not defined // Layer name if (poLink && poLink->name) { - pszName = CPLStrdup(poLink->name); + osName = std::string(poLink->name); } else { - char buf[20]; - snprintf(buf, sizeof(buf), "%d", iLayer); - pszName = CPLStrdup(buf); + osName = std::to_string(iLayer); } // Because we don't represent centroids as any simple feature, we have to scan @@ -74,14 +65,14 @@ OGRGRASSLayer::OGRGRASSLayer(int layerIndex, struct Map_info *map) nTotalCount = Vect_cidx_get_type_count(poMap, iLayer, GV_POINT | GV_LINES | GV_AREA); CPLDebug("GRASS", "nTotalCount = %d", nTotalCount); - paFeatureIndex = (int *)CPLMalloc(nTotalCount * sizeof(int)); + paFeatureIndex = static_cast(CPLMalloc(nTotalCount * sizeof(int))); int n = Vect_cidx_get_type_count(poMap, iLayer, GV_POINTS | GV_LINES | GV_AREA); int cnt = 0; for (int i = 0; i < n; i++) { - int cat, type, id; + int cat = 0, type = 0, id = 0; Vect_cidx_get_cat_by_index(poMap, iLayerIndex, i, &cat, &type, &id); @@ -90,7 +81,7 @@ OGRGRASSLayer::OGRGRASSLayer(int layerIndex, struct Map_info *map) paFeatureIndex[cnt++] = i; } - poFeatureDefn = new OGRFeatureDefn(pszName); + poFeatureDefn = new OGRFeatureDefn(osName.c_str()); SetDescription(poFeatureDefn->GetName()); poFeatureDefn->Reference(); @@ -99,7 +90,7 @@ OGRGRASSLayer::OGRGRASSLayer(int layerIndex, struct Map_info *map) int types = 0; for (int i = 0; i < nTypes; i++) { - int type, count; + int type = 0, count = 0; Vect_cidx_get_type_count_by_index(poMap, iLayerIndex, i, &type, &count); if (!(type & (GV_POINT | GV_LINES | GV_AREA))) continue; @@ -128,11 +119,11 @@ OGRGRASSLayer::OGRGRASSLayer(int layerIndex, struct Map_info *map) poFeatureDefn->SetGeomType(eGeomType); // Get attributes definition - poDbString = (dbString *)CPLMalloc(sizeof(dbString)); - poCursor = (dbCursor *)CPLMalloc(sizeof(dbCursor)); + poDbString = reinterpret_cast(CPLMalloc(sizeof(dbString))); + poCursor = reinterpret_cast(CPLMalloc(sizeof(dbCursor))); bCursorOpened = FALSE; - poDriver = NULL; + poDriver = nullptr; bHaveAttributes = false; db_init_string(poDbString); if (poLink) @@ -140,7 +131,7 @@ OGRGRASSLayer::OGRGRASSLayer(int layerIndex, struct Map_info *map) if (StartDbDriver()) { db_set_string(poDbString, poLink->table); - dbTable *table = NULL; + dbTable *table = nullptr; if (db_describe_table(poDriver, poDbString, &table) == DB_OK) { nFields = db_get_table_number_of_columns(table); @@ -189,7 +180,7 @@ OGRGRASSLayer::OGRGRASSLayer(int layerIndex, struct Map_info *map) CPLError(CE_Failure, CPLE_AppDefined, "Cannot find key field"); db_close_database_shutdown_driver(poDriver); - poDriver = NULL; + poDriver = nullptr; } } else @@ -198,7 +189,7 @@ OGRGRASSLayer::OGRGRASSLayer(int layerIndex, struct Map_info *map) "Cannot describe table %s", poLink->table); } db_close_database_shutdown_driver(poDriver); - poDriver = NULL; + poDriver = nullptr; } } @@ -211,14 +202,12 @@ OGRGRASSLayer::OGRGRASSLayer(int layerIndex, struct Map_info *map) if (getenv("GISBASE")) // We have some projection info in GISBASE { - struct Key_Value *projinfo, *projunits; - // Note: we do not have to reset GISDBASE and LOCATION_NAME because // OGRGRASSLayer constructor is called from OGRGRASSDataSource::Open // where those variables are set - projinfo = G_get_projinfo(); - projunits = G_get_projunits(); + struct Key_Value *projinfo = G_get_projinfo(); + struct Key_Value *projunits = G_get_projunits(); char *srsWkt = GPJ_grass_to_wkt(projinfo, projunits, 0, 0); if (srsWkt) @@ -248,8 +237,6 @@ OGRGRASSLayer::~OGRGRASSLayer() StopDbDriver(); } - if (pszName) - CPLFree(pszName); if (poFeatureDefn) poFeatureDefn->Release(); if (poSRS) @@ -280,7 +267,7 @@ OGRGRASSLayer::~OGRGRASSLayer() /************************************************************************/ /* StartDbDriver */ /************************************************************************/ -bool OGRGRASSLayer::StartDbDriver() +auto OGRGRASSLayer::StartDbDriver() -> bool { CPLDebug("GRASS", "StartDbDriver()"); @@ -292,7 +279,7 @@ bool OGRGRASSLayer::StartDbDriver() } poDriver = db_start_driver_open_database(poLink->driver, poLink->database); - if (poDriver == NULL) + if (poDriver == nullptr) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot open database %s by driver %s, " @@ -308,7 +295,7 @@ bool OGRGRASSLayer::StartDbDriver() /************************************************************************/ /* StopDbDriver */ /************************************************************************/ -bool OGRGRASSLayer::StopDbDriver() +auto OGRGRASSLayer::StopDbDriver() -> bool { if (!poDriver) { @@ -360,9 +347,9 @@ void OGRGRASSLayer::ResetReading() /* If we already have an FID list, we can easily reposition */ /* ourselves in it. */ /************************************************************************/ -OGRErr OGRGRASSLayer::SetNextByIndex(GIntBig nIndex) +auto OGRGRASSLayer::SetNextByIndex(GIntBig nIndex) -> OGRErr { - if (m_poFilterGeom != NULL || m_poAttrQuery != NULL) + if (m_poFilterGeom != nullptr || m_poAttrQuery != nullptr) { iNextId = 0; int count = 0; @@ -375,7 +362,7 @@ OGRErr OGRGRASSLayer::SetNextByIndex(GIntBig nIndex) break; // Attributes - if (pszQuery != NULL && !paQueryMatch[iNextId]) + if (pszQuery != nullptr && !paQueryMatch[iNextId]) { iNextId++; continue; @@ -391,7 +378,7 @@ OGRErr OGRGRASSLayer::SetNextByIndex(GIntBig nIndex) } } - iNextId = nIndex; + iNextId = (int)nIndex; return OGRERR_NONE; } @@ -399,27 +386,27 @@ OGRErr OGRGRASSLayer::SetNextByIndex(GIntBig nIndex) /************************************************************************/ /* SetAttributeFilter */ /************************************************************************/ -OGRErr OGRGRASSLayer::SetAttributeFilter(const char *query) +auto OGRGRASSLayer::SetAttributeFilter(const char *query) -> OGRErr { CPLDebug("GRASS", "SetAttributeFilter: %s", query); - if (query == NULL) + if (query == nullptr) { // Release old if any if (pszQuery) { CPLFree(pszQuery); - pszQuery = NULL; + pszQuery = nullptr; } if (paQueryMatch) { CPLFree(paQueryMatch); - paQueryMatch = NULL; + paQueryMatch = nullptr; } return OGRERR_NONE; } - paQueryMatch = (char *)CPLMalloc(nTotalCount); + paQueryMatch = reinterpret_cast(CPLMalloc(nTotalCount)); memset(paQueryMatch, 0x0, nTotalCount); pszQuery = CPLStrdup(query); @@ -450,16 +437,16 @@ OGRErr OGRGRASSLayer::SetAttributeFilter(const char *query) else { CPLFree(pszQuery); - pszQuery = NULL; + pszQuery = nullptr; return OGRERR_FAILURE; } db_close_database_shutdown_driver(poDriver); - poDriver = NULL; + poDriver = nullptr; } else { CPLFree(pszQuery); - pszQuery = NULL; + pszQuery = nullptr; return OGRERR_FAILURE; } } @@ -484,7 +471,7 @@ OGRErr OGRGRASSLayer::SetAttributeFilter(const char *query) /************************************************************************/ /* SetQueryMatch */ /************************************************************************/ -bool OGRGRASSLayer::SetQueryMatch() +auto OGRGRASSLayer::SetQueryMatch() -> bool { CPLDebug("GRASS", "SetQueryMatch"); @@ -497,7 +484,7 @@ bool OGRGRASSLayer::SetQueryMatch() return false; } - int more; + int more = 0; int cidx = 0; // index to category index int fidx = 0; // index to feature index (paFeatureIndex) // number of categories in category index @@ -575,7 +562,7 @@ bool OGRGRASSLayer::SetQueryMatch() /************************************************************************/ /* OpenSequentialCursor */ /************************************************************************/ -bool OGRGRASSLayer::OpenSequentialCursor() +auto OGRGRASSLayer::OpenSequentialCursor() -> bool { CPLDebug("GRASS", "OpenSequentialCursor: %s", pszQuery); @@ -591,18 +578,18 @@ bool OGRGRASSLayer::OpenSequentialCursor() bCursorOpened = false; } - char buf[2000]; - snprintf(buf, sizeof(buf), "SELECT * FROM %s ", poLink->table); - db_set_string(poDbString, buf); + std::array buf{}; + (void)snprintf(buf.data(), buf.size(), "SELECT * FROM %s ", poLink->table); + db_set_string(poDbString, buf.data()); if (pszQuery) { - snprintf(buf, sizeof(buf), "WHERE %s ", pszQuery); - db_append_string(poDbString, buf); + (void)snprintf(buf.data(), buf.size(), "WHERE %s ", pszQuery); + db_append_string(poDbString, buf.data()); } - snprintf(buf, sizeof(buf), "ORDER BY %s", poLink->key); - db_append_string(poDbString, buf); + (void)snprintf(buf.data(), buf.size(), "ORDER BY %s", poLink->key); + db_append_string(poDbString, buf.data()); CPLDebug("GRASS", "Query: %s", db_get_string(poDbString)); @@ -624,11 +611,11 @@ bool OGRGRASSLayer::OpenSequentialCursor() /************************************************************************/ /* ResetSequentialCursor */ /************************************************************************/ -bool OGRGRASSLayer::ResetSequentialCursor() +auto OGRGRASSLayer::ResetSequentialCursor() -> bool { CPLDebug("GRASS", "ResetSequentialCursor"); - int more; + int more = 0; if (db_fetch(poCursor, DB_FIRST, &more) != DB_OK) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot reset cursor."); @@ -651,13 +638,13 @@ void OGRGRASSLayer::SetSpatialFilter(OGRGeometry *poGeomIn) OGRLayer::SetSpatialFilter(poGeomIn); - if (poGeomIn == NULL) + if (poGeomIn == nullptr) { // Release old if any if (paSpatialMatch) { CPLFree(paSpatialMatch); - paSpatialMatch = NULL; + paSpatialMatch = nullptr; } return; } @@ -668,17 +655,17 @@ void OGRGRASSLayer::SetSpatialFilter(OGRGeometry *poGeomIn) /************************************************************************/ /* SetSpatialMatch */ /************************************************************************/ -bool OGRGRASSLayer::SetSpatialMatch() +auto OGRGRASSLayer::SetSpatialMatch() -> bool { CPLDebug("GRASS", "SetSpatialMatch"); if (!paSpatialMatch) { - paSpatialMatch = (char *)CPLMalloc(nTotalCount); + paSpatialMatch = static_cast(CPLMalloc(nTotalCount)); } memset(paSpatialMatch, 0x0, nTotalCount); - OGRLineString *lstring = new OGRLineString(); + auto lstring = new OGRLineString(); lstring->setNumPoints(5); OGRGeometry *geom = lstring; @@ -686,11 +673,13 @@ bool OGRGRASSLayer::SetSpatialMatch() { int cidx = paFeatureIndex[i]; - int cat, type, id; + int cat = 0, type = 0, id = 0; Vect_cidx_get_cat_by_index(poMap, iLayerIndex, cidx, &cat, &type, &id); - struct bound_box box; + struct bound_box box + { + }; switch (type) { @@ -724,12 +713,12 @@ bool OGRGRASSLayer::SetSpatialMatch() /************************************************************************/ /* GetNextFeature() */ /************************************************************************/ -OGRFeature *OGRGRASSLayer::GetNextFeature() +auto OGRGRASSLayer::GetNextFeature() -> OGRFeature * { CPLDebug("GRASS", "OGRGRASSLayer::GetNextFeature"); - OGRFeature *poFeature = NULL; + OGRFeature *poFeature = nullptr; - int cat; + int cat = 0; // Get next iNextId while (true) @@ -745,14 +734,14 @@ OGRFeature *OGRGRASSLayer::GetNextFeature() if (poDriver) { db_close_database_shutdown_driver(poDriver); - poDriver = NULL; + poDriver = nullptr; } - return NULL; + return nullptr; } // Attributes - if (pszQuery != NULL && !paQueryMatch[iNextId]) + if (pszQuery != nullptr && !paQueryMatch[iNextId]) { iNextId++; continue; @@ -796,7 +785,7 @@ OGRFeature *OGRGRASSLayer::GetNextFeature() { while (true) { - int more; + int more = 0; if (db_fetch(poCursor, DB_NEXT, &more) != DB_OK) { CPLError(CE_Failure, CPLE_AppDefined, @@ -838,16 +827,16 @@ OGRFeature *OGRGRASSLayer::GetNextFeature() /************************************************************************/ /* GetFeature() */ /************************************************************************/ -OGRFeature *OGRGRASSLayer::GetFeature(GIntBig nFeatureId) +auto OGRGRASSLayer::GetFeature(GIntBig nFeatureId) -> OGRFeature * { CPLDebug("GRASS", "OGRGRASSLayer::GetFeature nFeatureId = " CPL_FRMT_GIB, nFeatureId); - int cat; + int cat = 0; OGRGeometry *poOGR = GetFeatureGeometry(nFeatureId, &cat); - OGRFeature *poFeature = new OGRFeature(poFeatureDefn); + auto poFeature = new OGRFeature(poFeatureDefn); poFeature->SetGeometryDirectly(poOGR); poFeature->SetFID(nFeatureId); @@ -864,10 +853,10 @@ OGRFeature *OGRGRASSLayer::GetFeature(GIntBig nFeatureId) bCursorOpened = false; } CPLDebug("GRASS", "Open cursor for key = %d", cat); - char buf[2000]; - snprintf(buf, sizeof(buf), "SELECT * FROM %s WHERE %s = %d", - poLink->table, poLink->key, cat); - db_set_string(poDbString, buf); + std::array buf{}; + (void)snprintf(buf.data(), buf.size(), "SELECT * FROM %s WHERE %s = %d", + poLink->table, poLink->key, cat); + db_set_string(poDbString, buf.data()); if (db_open_select_cursor(poDriver, poDbString, poCursor, DB_SEQUENTIAL) == DB_OK) { @@ -881,7 +870,7 @@ OGRFeature *OGRGRASSLayer::GetFeature(GIntBig nFeatureId) if (bCursorOpened) { - int more; + int more = 0; if (db_fetch(poCursor, DB_NEXT, &more) != DB_OK) { CPLError(CE_Failure, CPLE_AppDefined, @@ -916,19 +905,20 @@ OGRFeature *OGRGRASSLayer::GetFeature(GIntBig nFeatureId) /************************************************************************/ /* GetFeatureGeometry() */ /************************************************************************/ -OGRGeometry *OGRGRASSLayer::GetFeatureGeometry(long nFeatureId, int *cat) +auto OGRGRASSLayer::GetFeatureGeometry(long nFeatureId, int *cat) + -> OGRGeometry * { CPLDebug("GRASS", "OGRGRASSLayer::GetFeatureGeometry nFeatureId = %ld", nFeatureId); int cidx = paFeatureIndex[(int)nFeatureId]; - int type, id; + int type = 0, id = 0; Vect_cidx_get_cat_by_index(poMap, iLayerIndex, cidx, cat, &type, &id); //CPLDebug ( "GRASS", "cat = %d type = %d id = %d", *cat, type, id ); - OGRGeometry *poOGR = NULL; + OGRGeometry *poOGR = nullptr; int bIs3D = Vect_is_3d(poMap); switch (type) @@ -948,7 +938,7 @@ OGRGeometry *OGRGRASSLayer::GetFeatureGeometry(long nFeatureId, int *cat) case GV_BOUNDARY: { Vect_read_line(poMap, poPoints, poCats, id); - OGRLineString *poOGRLine = new OGRLineString(); + auto poOGRLine = new OGRLineString(); if (bIs3D) poOGRLine->setPoints(poPoints->n_points, poPoints->x, poPoints->y, poPoints->z); @@ -964,9 +954,9 @@ OGRGeometry *OGRGRASSLayer::GetFeatureGeometry(long nFeatureId, int *cat) { Vect_get_area_points(poMap, id, poPoints); - OGRPolygon *poOGRPoly = new OGRPolygon(); + auto poOGRPoly = new OGRPolygon(); - OGRLinearRing *poRing = new OGRLinearRing(); + auto poRing = new OGRLinearRing(); if (bIs3D) poRing->setPoints(poPoints->n_points, poPoints->x, poPoints->y, poPoints->z); @@ -1001,7 +991,7 @@ OGRGeometry *OGRGRASSLayer::GetFeatureGeometry(long nFeatureId, int *cat) { CPLError(CE_Failure, CPLE_AppDefined, "Unknown GRASS feature type."); - return NULL; + return nullptr; } } @@ -1011,7 +1001,7 @@ OGRGeometry *OGRGRASSLayer::GetFeatureGeometry(long nFeatureId, int *cat) /************************************************************************/ /* SetAttributes() */ /************************************************************************/ -bool OGRGRASSLayer::SetAttributes(OGRFeature *poFeature, dbTable *table) +auto OGRGRASSLayer::SetAttributes(OGRFeature *poFeature, dbTable *table) -> bool { CPLDebug("GRASS", "OGRGRASSLayer::SetAttributes"); @@ -1056,9 +1046,9 @@ bool OGRGRASSLayer::SetAttributes(OGRFeature *poFeature, dbTable *table) /* Eventually we should consider implementing a more efficient */ /* way of counting features matching a spatial query. */ /************************************************************************/ -GIntBig OGRGRASSLayer::GetFeatureCount(int bForce) +auto OGRGRASSLayer::GetFeatureCount(int bForce) -> GIntBig { - if (m_poFilterGeom != NULL || m_poAttrQuery != NULL) + if (m_poFilterGeom != nullptr || m_poAttrQuery != nullptr) return OGRLayer::GetFeatureCount(bForce); return nTotalCount; @@ -1073,9 +1063,11 @@ GIntBig OGRGRASSLayer::GetFeatureCount(int bForce) /* */ /* Returns OGRERR_NONE/OGRRERR_FAILURE. */ /************************************************************************/ -OGRErr OGRGRASSLayer::GetExtent(OGREnvelope *psExtent, int /*bForce*/) +auto OGRGRASSLayer::GetExtent(OGREnvelope *psExtent, int /*bForce*/) -> OGRErr { - struct bound_box box; + struct bound_box box + { + }; Vect_get_map_box(poMap, &box); @@ -1090,7 +1082,7 @@ OGRErr OGRGRASSLayer::GetExtent(OGREnvelope *psExtent, int /*bForce*/) /************************************************************************/ /* TestCapability() */ /************************************************************************/ -int OGRGRASSLayer::TestCapability(const char *pszCap) +auto OGRGRASSLayer::TestCapability(const char *pszCap) -> int { if (EQUAL(pszCap, OLCRandomRead)) return TRUE; @@ -1114,7 +1106,7 @@ int OGRGRASSLayer::TestCapability(const char *pszCap) /************************************************************************/ /* GetSpatialRef() */ /************************************************************************/ -OGRSpatialReference *OGRGRASSLayer::GetSpatialRef() +auto OGRGRASSLayer::GetSpatialRef() -> OGRSpatialReference * { return poSRS; }