Skip to content

Commit

Permalink
gdal_rasterize: port to argument parser (#10741)
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso authored Sep 6, 2024
1 parent 8f4571f commit 8d9ceab
Show file tree
Hide file tree
Showing 3 changed files with 500 additions and 542 deletions.
81 changes: 24 additions & 57 deletions apps/gdal_rasterize_bin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,11 @@
/* Usage() */
/************************************************************************/

static void Usage(bool bIsError, const char *pszErrorMsg = nullptr)
static void Usage()

{
fprintf(
bIsError ? stderr : stdout,
"Usage: gdal_rasterize [--help] [--help-general]\n"
" [-b <band>]... [-i] [-at]\n"
" [-oo <NAME>=<VALUE>]...\n"
" {[-burn <value>]... | [-a <attribute_name>] | [-3d]} [-add]\n"
" [-l <layername>]... [-where <expression>] "
"[-sql <select_statement>|@<filename>]\n"
" [-dialect <dialect>] [-of <format>] [-a_srs <srs_def>] [-to "
"<NAME>=<VALUE>]...\n"
" [-co <NAME>=<VALUE>]... [-a_nodata <value>] [-init "
"<value>]...\n"
" [-te <xmin> <ymin> <xmax> <ymax>] [-tr <xres> <yres>] [-tap] "
"[-ts <width> "
"<height>]\n"
" [-ot "
"{Byte/Int8/Int16/UInt16/UInt32/Int32/UInt64/Int64/Float32/Float64/\n"
" CInt16/CInt32/CFloat32/CFloat64}] [-optim "
"{AUTO|VECTOR|RASTER}] [-q]\n"
" <src_datasource> <dst_filename>\n");

if (pszErrorMsg != nullptr)
fprintf(stderr, "\nFAILURE: %s\n", pszErrorMsg);
exit(bIsError ? 1 : 0);
fprintf(stderr, "%s\n", GDALRasterizeAppGetParserUsage().c_str());
exit(1);
}

/************************************************************************/
Expand All @@ -71,58 +49,44 @@ static void Usage(bool bIsError, const char *pszErrorMsg = nullptr)

MAIN_START(argc, argv)
{

/* Check strict compilation and runtime library version as we use C++ API */
if (!GDAL_CHECK_VERSION(argv[0]))
exit(1);

EarlySetConfigOptions(argc, argv);

/* -------------------------------------------------------------------- */
/* Generic arg processing. */
/* Register standard GDAL drivers, and process generic GDAL */
/* command options. */
/* -------------------------------------------------------------------- */
GDALAllRegister();
argc = GDALGeneralCmdLineProcessor(argc, &argv, 0);
if (argc < 1)
exit(-argc);

for (int i = 0; i < argc; i++)
{
if (EQUAL(argv[i], "--utility_version"))
{
printf("%s was compiled against GDAL %s and "
"is running against GDAL %s\n",
argv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME"));
CSLDestroy(argv);
return 0;
}
else if (EQUAL(argv[i], "--help"))
{
Usage(false);
}
}
/* -------------------------------------------------------------------- */
/* Generic arg processing. */
/* -------------------------------------------------------------------- */

GDALRasterizeOptionsForBinary sOptionsForBinary;
// coverity[tainted_data]
GDALRasterizeOptions *psOptions =
GDALRasterizeOptionsNew(argv + 1, &sOptionsForBinary);
std::unique_ptr<GDALRasterizeOptions, decltype(&GDALRasterizeOptionsFree)>
psOptions{GDALRasterizeOptionsNew(argv + 1, &sOptionsForBinary),
GDALRasterizeOptionsFree};

CSLDestroy(argv);

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

if (!(sOptionsForBinary.bQuiet))
{
GDALRasterizeOptionsSetProgress(psOptions, GDALTermProgress, nullptr);
GDALRasterizeOptionsSetProgress(psOptions.get(), GDALTermProgress,
nullptr);
}

if (sOptionsForBinary.osSource.empty())
Usage(true, "No input file specified.");

if (!sOptionsForBinary.bDestSpecified)
Usage(true, "No output file specified.");

/* -------------------------------------------------------------------- */
/* Open input file. */
/* -------------------------------------------------------------------- */
Expand Down Expand Up @@ -186,16 +150,19 @@ MAIN_START(argc, argv)
}

int bUsageError = FALSE;
GDALDatasetH hRetDS = GDALRasterize(sOptionsForBinary.osDest.c_str(),
hDstDS, hInDS, psOptions, &bUsageError);
GDALDatasetH hRetDS =
GDALRasterize(sOptionsForBinary.osDest.c_str(), hDstDS, hInDS,
psOptions.get(), &bUsageError);

if (bUsageError == TRUE)
Usage(true);
Usage();

int nRetCode = hRetDS ? 0 : 1;

GDALClose(hInDS);

if (GDALClose(hRetDS) != CE_None)
nRetCode = 1;
GDALRasterizeOptionsFree(psOptions);

GDALDestroyDriverManager();

Expand Down
Loading

0 comments on commit 8d9ceab

Please sign in to comment.