Skip to content

Commit

Permalink
GDALBuildVRT: C++ify GDALBuildVRTOptions and GDALBuildVRTOptionsForBi…
Browse files Browse the repository at this point in the history
…nary
  • Loading branch information
rouault committed Apr 17, 2024
1 parent 324d6b0 commit 065c826
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 237 deletions.
17 changes: 8 additions & 9 deletions apps/gdal_utils_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,6 @@ struct GDALDEMProcessingOptionsForBinary
int bQuiet;
};

struct GDALBuildVRTOptionsForBinary
{
int nSrcFiles;
char **papszSrcFiles;
char *pszDstFilename;
int bQuiet;
int bOverwrite;
};

CPL_C_END

/* Access modes */
Expand Down Expand Up @@ -228,6 +219,14 @@ struct GDALWarpAppOptionsForBinary
CPLStringList aosAllowedInputDrivers{};
};

struct GDALBuildVRTOptionsForBinary
{
CPLStringList aosSrcFiles{};
std::string osDstFilename{};
bool bQuiet = false;
bool bOverwrite = false;
};

std::string CPL_DLL GDALNearblackGetParserUsage();

std::string CPL_DLL GDALVectorInfoGetParserUsage();
Expand Down
58 changes: 17 additions & 41 deletions apps/gdalbuildvrt_bin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,31 +105,6 @@ static void Usage(bool bIsError, const char *pszErrorMsg)
exit(bIsError ? 1 : 0);
}

/************************************************************************/
/* GDALBuildVRTOptionsForBinaryNew() */
/************************************************************************/

static GDALBuildVRTOptionsForBinary *GDALBuildVRTOptionsForBinaryNew(void)
{
return static_cast<GDALBuildVRTOptionsForBinary *>(
CPLCalloc(1, sizeof(GDALBuildVRTOptionsForBinary)));
}

/************************************************************************/
/* GDALBuildVRTOptionsForBinaryFree() */
/************************************************************************/

static void GDALBuildVRTOptionsForBinaryFree(
GDALBuildVRTOptionsForBinary *psOptionsForBinary)
{
if (psOptionsForBinary)
{
CSLDestroy(psOptionsForBinary->papszSrcFiles);
CPLFree(psOptionsForBinary->pszDstFilename);
CPLFree(psOptionsForBinary);
}
}

/************************************************************************/
/* main() */
/************************************************************************/
Expand Down Expand Up @@ -164,43 +139,44 @@ MAIN_START(argc, argv)
}
}

GDALBuildVRTOptionsForBinary *psOptionsForBinary =
GDALBuildVRTOptionsForBinaryNew();
GDALBuildVRTOptionsForBinary sOptionsForBinary;
/* coverity[tainted_data] */
GDALBuildVRTOptions *psOptions =
GDALBuildVRTOptionsNew(argv + 1, psOptionsForBinary);
GDALBuildVRTOptionsNew(argv + 1, &sOptionsForBinary);
CSLDestroy(argv);

if (psOptions == nullptr)
{
Usage(true, nullptr);
}

if (psOptionsForBinary->pszDstFilename == nullptr)
if (sOptionsForBinary.osDstFilename.c_str() == nullptr)
{
Usage(true, "No target filename specified.");
}

if (!(psOptionsForBinary->bQuiet))
if (!(sOptionsForBinary.bQuiet))
{
GDALBuildVRTOptionsSetProgress(psOptions, GDALTermProgress, nullptr);
}

/* Avoid overwriting a non VRT dataset if the user did not put the */
/* filenames in the right order */
VSIStatBuf sBuf;
if (!psOptionsForBinary->bOverwrite)
if (!sOptionsForBinary.bOverwrite)
{
int bExists = (VSIStat(psOptionsForBinary->pszDstFilename, &sBuf) == 0);
int bExists =
(VSIStat(sOptionsForBinary.osDstFilename.c_str(), &sBuf) == 0);
if (bExists)
{
GDALDriverH hDriver =
GDALIdentifyDriver(psOptionsForBinary->pszDstFilename, nullptr);
GDALDriverH hDriver = GDALIdentifyDriver(
sOptionsForBinary.osDstFilename.c_str(), nullptr);
if (hDriver &&
!(EQUAL(GDALGetDriverShortName(hDriver), "VRT") ||
(EQUAL(GDALGetDriverShortName(hDriver), "API_PROXY") &&
EQUAL(CPLGetExtension(psOptionsForBinary->pszDstFilename),
"VRT"))))
EQUAL(
CPLGetExtension(sOptionsForBinary.osDstFilename.c_str()),
"VRT"))))
{
fprintf(
stderr,
Expand All @@ -209,24 +185,24 @@ MAIN_START(argc, argv)
"right order.\n"
"If you want to overwrite %s, add -overwrite option to the "
"command line.\n\n",
psOptionsForBinary->pszDstFilename,
sOptionsForBinary.osDstFilename.c_str(),
GDALGetDriverShortName(hDriver),
psOptionsForBinary->pszDstFilename);
sOptionsForBinary.osDstFilename.c_str());
Usage(true);
}
}
}

int bUsageError = FALSE;
GDALDatasetH hOutDS = GDALBuildVRT(
psOptionsForBinary->pszDstFilename, psOptionsForBinary->nSrcFiles,
nullptr, psOptionsForBinary->papszSrcFiles, psOptions, &bUsageError);
sOptionsForBinary.osDstFilename.c_str(),
sOptionsForBinary.aosSrcFiles.size(), nullptr,
sOptionsForBinary.aosSrcFiles.List(), psOptions, &bUsageError);
if (bUsageError)
Usage(true);
int nRetCode = (hOutDS) ? 0 : 1;

GDALBuildVRTOptionsFree(psOptions);
GDALBuildVRTOptionsForBinaryFree(psOptionsForBinary);

CPLErrorReset();
// The flush to disk is only done at that stage, so check if any error has
Expand Down
Loading

0 comments on commit 065c826

Please sign in to comment.