Skip to content

Commit

Permalink
[gdalmdimtranslate] Use GDALArgumentParser (OSGeo#9912)
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso authored May 14, 2024
1 parent 6d3b13a commit cae1d97
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 159 deletions.
2 changes: 2 additions & 0 deletions apps/gdal_utils_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ std::string CPL_DLL GDALVectorInfoGetParserUsage();

std::string CPL_DLL GDALTranslateGetParserUsage();

std::string CPL_DLL GDALMultiDimTranslateAppGetParserUsage();

std::string CPL_DLL GDALVectorTranslateGetParserUsage();

std::string CPL_DLL GDALWarpAppGetParserUsage();
Expand Down
94 changes: 39 additions & 55 deletions apps/gdalmdimtranslate_bin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,25 @@
#include "gdal_utils_priv.h"
#include "gdal_priv.h"

/**
* @brief Makes sure the GDAL library is properly cleaned up before exiting.
* @param nCode exit code
* @todo Move to API
*/
static void GDALExit(int nCode)
{
GDALDestroy();
exit(nCode);
}

/************************************************************************/
/* Usage() */
/************************************************************************/

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

static void Usage()
{
fprintf(bIsError ? stderr : stdout,
"Usage: gdalmdimtranslate [--help] [--help-general]\n"
" [-if <format>]... [-of <format>]\n"
" [-co <NAME>=<VALUE>]...\n"
" [-array <array_spec>]...\n"
" [-arrayoption <NAME>=<VALUE>]...\n"
" [-group <group_spec>]...\n"
" [-subset <subset_spec>]...\n"
" [-scaleaxes <scaleaxes_spec>]\n"
" [-oo <NAME>=<VALUE>]...\n"
" <src_filename> <dst_filename>\n");

if (pszErrorMsg != nullptr)
fprintf(stderr, "\nFAILURE: %s\n", pszErrorMsg);

exit(bIsError ? 1 : 0);
fprintf(stderr, "%s\n", GDALMultiDimTranslateAppGetParserUsage().c_str());
GDALExit(1);
}

/************************************************************************/
Expand All @@ -65,57 +61,41 @@ MAIN_START(argc, argv)
{
/* Check strict compilation and runtime library version as we use C++ API */
if (!GDAL_CHECK_VERSION(argv[0]))
exit(1);
GDALExit(1);

EarlySetConfigOptions(argc, argv);

/* -------------------------------------------------------------------- */
/* Generic arg processing. */
/* -------------------------------------------------------------------- */
GDALAllRegister();

argc = GDALGeneralCmdLineProcessor(argc, &argv, 0);
if (argc < 1)
exit(-argc);
GDALExit(-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);
}
}
/* -------------------------------------------------------------------- */
/* Parse command line */
/* -------------------------------------------------------------------- */

GDALMultiDimTranslateOptionsForBinary sOptionsForBinary;
// coverity[tainted_data]
GDALMultiDimTranslateOptions *psOptions =
GDALMultiDimTranslateOptionsNew(argv + 1, &sOptionsForBinary);
CSLDestroy(argv);

if (psOptions == nullptr)
std::unique_ptr<GDALMultiDimTranslateOptions,
decltype(&GDALMultiDimTranslateOptionsFree)>
psOptions{GDALMultiDimTranslateOptionsNew(argv + 1, &sOptionsForBinary),
GDALMultiDimTranslateOptionsFree};
CSLDestroy(argv);
if (!psOptions)
{
Usage(true);
Usage();
}

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

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

if (sOptionsForBinary.osDest.empty())
Usage(true, "No output file specified.");

/* -------------------------------------------------------------------- */
/* Open input file. */
/* -------------------------------------------------------------------- */
Expand All @@ -126,12 +106,14 @@ MAIN_START(argc, argv)
sOptionsForBinary.aosOpenOptions.List(), nullptr);

if (hInDS == nullptr)
exit(1);
GDALExit(1);

/* -------------------------------------------------------------------- */
/* Open output file if in update mode. */
/* -------------------------------------------------------------------- */
GDALDatasetH hDstDS = nullptr;
// Note: since bUpdate is never changed and defaults to false this block
// will never be executed
if (sOptionsForBinary.bUpdate)
{
CPLPushErrorHandler(CPLQuietErrorHandler);
Expand All @@ -145,18 +127,20 @@ MAIN_START(argc, argv)
int bUsageError = FALSE;
GDALDatasetH hRetDS =
GDALMultiDimTranslate(sOptionsForBinary.osDest.c_str(), hDstDS, 1,
&hInDS, psOptions, &bUsageError);
&hInDS, psOptions.get(), &bUsageError);

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

int nRetCode = hRetDS ? 0 : 1;

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

GDALClose(hInDS);
GDALMultiDimTranslateOptionsFree(psOptions);
if (GDALClose(hInDS) != CE_None)
nRetCode = 1;

GDALDestroyDriverManager();
GDALDestroy();

return nRetCode;
}
Expand Down
Loading

0 comments on commit cae1d97

Please sign in to comment.