Skip to content

Commit

Permalink
Merge branch 'miramon_clean_history' of https://github.com/AbelPau/gdal
Browse files Browse the repository at this point in the history
… into miramon_clean_history
  • Loading branch information
AbelPau committed Apr 18, 2024
2 parents 21b7707 + 4b71cd2 commit f30137f
Show file tree
Hide file tree
Showing 21 changed files with 546 additions and 755 deletions.
12 changes: 7 additions & 5 deletions apps/gdal_utils_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ CPL_C_START
struct GDALInfoOptionsForBinary
{
/* Filename to open. */
char *pszFilename;
std::string osFilename{};

/* Open options. */
char **papszOpenOptions;
CPLStringList aosOpenOptions{};

/* > for reporting on a particular subdataset */
int nSubdataset;
/* For reporting on a particular subdataset */
int nSubdataset = 0;

/* Allowed input drivers. */
char **papszAllowInputDrivers;
CPLStringList aosAllowedInputDrivers{};
};

struct GDALDEMProcessingOptionsForBinary
Expand Down Expand Up @@ -239,6 +239,8 @@ std::string CPL_DLL GDALVectorTranslateGetParserUsage();

std::string CPL_DLL GDALWarpAppGetParserUsage();

std::string CPL_DLL GDALInfoAppGetParserUsage();

#endif /* #ifndef DOXYGEN_SKIP */

#endif /* GDAL_UTILS_PRIV_H_INCLUDED */
16 changes: 16 additions & 0 deletions apps/gdalargumentparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,22 @@ GDALArgumentParser::get_non_positional_arguments(const CPLStringList &aosArgs)
return args;
}

Argument &GDALArgumentParser::add_inverted_logic_flag(const std::string &name,
bool *store_into,
const std::string &help)
{
return add_argument(name)
.default_value(true)
.implicit_value(false)
.action(
[store_into](const auto &)
{
if (store_into)
*store_into = false;
})
.help(help);
}

/************************************************************************/
/* parse_args() */
/************************************************************************/
Expand Down
10 changes: 10 additions & 0 deletions apps/gdalargumentparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ class GDALArgumentParser : public ArgumentParser
//! Return the non positional arguments.
CPLStringList get_non_positional_arguments(const CPLStringList &aosArgs);

/**
* Add an inverted logic (default true, false when set) flag
* @param name flag name
* @param store_into optional pointer to a bool variable where to store the value
* @param help optional help text
*/
Argument &add_inverted_logic_flag(const std::string &name,
bool *store_into = nullptr,
const std::string &help = "");

private:
std::map<std::string, ArgumentParser::argument_it>::iterator
find_argument(const std::string &name);
Expand Down
147 changes: 55 additions & 92 deletions apps/gdalinfo_bin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,53 +35,36 @@
#include "gdal_utils_priv.h"

/************************************************************************/
/* Usage() */
/* GDALExit() */
/* This function exits and cleans up GDAL and OGR resources */
/* Perhaps it should be added to C api and used in all apps? */
/************************************************************************/

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

static int GDALExit(int nCode)
{
fprintf(
bIsError ? stderr : stdout,
"Usage: gdalinfo [--help] [--help-general]\n"
" [-json] [-mm] [-stats | -approx_stats] [-hist]\n"
" [-nogcp] [-nomd] [-norat] [-noct] [-nofl]\n"
" [-checksum] [-listmdd] [-mdd <domain>|all]\n"
" [-proj4] [-wkt_format {WKT1|WKT2|<other_format>}]...\n"
" [-sd <subdataset>] [-oo <NAME>=<VALUE>]... [-if "
"<format>]...\n"
" <datasetname>\n");

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

exit(bIsError ? 1 : 0);
}
const char *pszDebug = CPLGetConfigOption("CPL_DEBUG", nullptr);
if (pszDebug && (EQUAL(pszDebug, "ON") || EQUAL(pszDebug, "")))
{
GDALDumpOpenDatasets(stderr);
CPLDumpSharedList(nullptr);
}

/************************************************************************/
/* GDALInfoOptionsForBinary() */
/************************************************************************/
GDALDestroyDriverManager();

static GDALInfoOptionsForBinary *GDALInfoOptionsForBinaryNew(void)
{
return static_cast<GDALInfoOptionsForBinary *>(
CPLCalloc(1, sizeof(GDALInfoOptionsForBinary)));
OGRCleanupAll();

exit(nCode);
}

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

static void
GDALInfoOptionsForBinaryFree(GDALInfoOptionsForBinary *psOptionsForBinary)
static void Usage()

{
if (psOptionsForBinary)
{
CPLFree(psOptionsForBinary->pszFilename);
CSLDestroy(psOptionsForBinary->papszOpenOptions);
CSLDestroy(psOptionsForBinary->papszAllowInputDrivers);
CPLFree(psOptionsForBinary);
}
fprintf(stderr, "%s\n", GDALInfoAppGetParserUsage().c_str());
GDALExit(1);
}

/************************************************************************/
Expand All @@ -93,39 +76,30 @@ MAIN_START(argc, argv)
{
EarlySetConfigOptions(argc, argv);

GDALAllRegister();
/* -------------------------------------------------------------------- */
/* Register standard GDAL drivers, and process generic GDAL */
/* command options. */
/* -------------------------------------------------------------------- */

GDALAllRegister();
argc = GDALGeneralCmdLineProcessor(argc, &argv, 0);
if (argc < 1)
exit(-argc);
GDALExit(-argc);

for (int i = 0; argv != nullptr && argv[i] != nullptr; 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);
}
}
argv = CSLAddString(argv, "-stdout");
/* -------------------------------------------------------------------- */
/* Parse command line */
/* -------------------------------------------------------------------- */

GDALInfoOptionsForBinary *psOptionsForBinary =
GDALInfoOptionsForBinaryNew();
GDALInfoOptionsForBinary sOptionsForBinary;

GDALInfoOptions *psOptions =
GDALInfoOptionsNew(argv + 1, psOptionsForBinary);
if (psOptions == nullptr)
Usage(true);
std::unique_ptr<GDALInfoOptions, decltype(&GDALInfoOptionsFree)> psOptions{
GDALInfoOptionsNew(argv + 1, &sOptionsForBinary), GDALInfoOptionsFree};
CSLDestroy(argv);

if (psOptionsForBinary->pszFilename == nullptr)
Usage(true, "No datasource specified.");
if (!psOptions)
{
Usage();
}

/* -------------------------------------------------------------------- */
/* Open dataset. */
Expand All @@ -138,10 +112,10 @@ MAIN_START(argc, argv)
#endif

GDALDatasetH hDataset = GDALOpenEx(
psOptionsForBinary->pszFilename,
sOptionsForBinary.osFilename.c_str(),
GDAL_OF_READONLY | GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR,
psOptionsForBinary->papszAllowInputDrivers,
psOptionsForBinary->papszOpenOptions, nullptr);
sOptionsForBinary.aosAllowedInputDrivers,
sOptionsForBinary.aosOpenOptions, nullptr);

if (hDataset == nullptr)
{
Expand All @@ -151,11 +125,11 @@ MAIN_START(argc, argv)
VSIStatBuf sStat;
CPLString message;
message.Printf("gdalinfo failed - unable to open '%s'.",
psOptionsForBinary->pszFilename);
if (VSIStat(psOptionsForBinary->pszFilename, &sStat) == 0)
sOptionsForBinary.osFilename.c_str());
if (VSIStat(sOptionsForBinary.osFilename.c_str(), &sStat) == 0)
{
GDALDriverH drv =
GDALIdentifyDriverEx(psOptionsForBinary->pszFilename,
GDALIdentifyDriverEx(sOptionsForBinary.osFilename.c_str(),
GDAL_OF_VECTOR, nullptr, nullptr);
if (drv)
{
Expand All @@ -169,33 +143,33 @@ MAIN_START(argc, argv)
/* If argument is a VSIFILE, then print its contents */
/* --------------------------------------------------------------------
*/
if (STARTS_WITH(psOptionsForBinary->pszFilename, "/vsizip/") ||
STARTS_WITH(psOptionsForBinary->pszFilename, "/vsitar/"))
if (STARTS_WITH(sOptionsForBinary.osFilename.c_str(), "/vsizip/") ||
STARTS_WITH(sOptionsForBinary.osFilename.c_str(), "/vsitar/"))
{
const char *const apszOptions[] = {"NAME_AND_TYPE_ONLY=YES",
nullptr};
VSIDIR *psDir =
VSIOpenDir(psOptionsForBinary->pszFilename, -1, apszOptions);
VSIDIR *psDir = VSIOpenDir(sOptionsForBinary.osFilename.c_str(), -1,
apszOptions);
if (psDir)
{
fprintf(stdout,
"Unable to open source `%s' directly.\n"
"The archive contains several files:\n",
psOptionsForBinary->pszFilename);
sOptionsForBinary.osFilename.c_str());
int nCount = 0;
while (auto psEntry = VSIGetNextDirEntry(psDir))
{
if (VSI_ISDIR(psEntry->nMode) && psEntry->pszName[0] &&
psEntry->pszName[strlen(psEntry->pszName) - 1] != '/')
{
fprintf(stdout, " %s/%s/\n",
psOptionsForBinary->pszFilename,
sOptionsForBinary.osFilename.c_str(),
psEntry->pszName);
}
else
{
fprintf(stdout, " %s/%s\n",
psOptionsForBinary->pszFilename,
sOptionsForBinary.osFilename.c_str(),
psEntry->pszName);
}
nCount++;
Expand All @@ -209,12 +183,6 @@ MAIN_START(argc, argv)
}
}

CSLDestroy(argv);

GDALInfoOptionsForBinaryFree(psOptionsForBinary);

GDALInfoOptionsFree(psOptions);

GDALDumpOpenDatasets(stderr);

GDALDestroyDriverManager();
Expand All @@ -230,19 +198,19 @@ MAIN_START(argc, argv)
/* Read specified subdataset if requested. */
/* --------------------------------------------------------------------
*/
if (psOptionsForBinary->nSubdataset > 0)
if (sOptionsForBinary.nSubdataset > 0)
{
char **papszSubdatasets = GDALGetMetadata(hDataset, "SUBDATASETS");
int nSubdatasets = CSLCount(papszSubdatasets);

if (nSubdatasets > 0 &&
psOptionsForBinary->nSubdataset <= nSubdatasets)
sOptionsForBinary.nSubdataset <= nSubdatasets)
{
char szKeyName[1024];
char *pszSubdatasetName;

snprintf(szKeyName, sizeof(szKeyName), "SUBDATASET_%d_NAME",
psOptionsForBinary->nSubdataset);
sOptionsForBinary.nSubdataset);
szKeyName[sizeof(szKeyName) - 1] = '\0';
pszSubdatasetName =
CPLStrdup(CSLFetchNameValue(papszSubdatasets, szKeyName));
Expand All @@ -255,11 +223,11 @@ MAIN_START(argc, argv)
fprintf(stderr,
"gdalinfo warning: subdataset %d of %d requested. "
"Reading the main dataset.\n",
psOptionsForBinary->nSubdataset, nSubdatasets);
sOptionsForBinary.nSubdataset, nSubdatasets);
}
}

char *pszGDALInfoOutput = GDALInfo(hDataset, psOptions);
char *pszGDALInfoOutput = GDALInfo(hDataset, psOptions.get());

if (pszGDALInfoOutput)
printf("%s", pszGDALInfoOutput);
Expand All @@ -271,17 +239,12 @@ MAIN_START(argc, argv)
}
#endif

GDALInfoOptionsForBinaryFree(psOptionsForBinary);

GDALInfoOptionsFree(psOptions);

CSLDestroy(argv);

GDALDumpOpenDatasets(stderr);

GDALDestroyDriverManager();

CPLDumpSharedList(nullptr);

GDALDestroy();

exit(0);
Expand Down
Loading

0 comments on commit f30137f

Please sign in to comment.