LCOV - code coverage report
Current view: top level - apps - gdalbuildvrt_bin.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 32 43 74.4 %
Date: 2024-11-21 22:18:42 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GDAL Utilities
       4             :  * Purpose:  Command line application to build VRT datasets from raster products
       5             :  *or content of SHP tile index Author:   Even Rouault, <even dot rouault at
       6             :  *spatialys dot com>
       7             :  *
       8             :  ******************************************************************************
       9             :  * Copyright (c) 2007-2016, Even Rouault <even dot rouault at spatialys dot com>
      10             :  *
      11             :  * SPDX-License-Identifier: MIT
      12             :  ****************************************************************************/
      13             : 
      14             : #include "cpl_string.h"
      15             : #include "cpl_error.h"
      16             : #include "commonutils.h"
      17             : #include "gdal_version.h"
      18             : #include "gdal_utils_priv.h"
      19             : 
      20             : /************************************************************************/
      21             : /*                               Usage()                                */
      22             : /************************************************************************/
      23             : 
      24             : static void Usage() CPL_NO_RETURN;
      25             : 
      26           1 : static void Usage()
      27             : 
      28             : {
      29           1 :     fprintf(stderr, "%s\n", GDALBuildVRTGetParserUsage().c_str());
      30           1 :     exit(1);
      31             : }
      32             : 
      33             : /************************************************************************/
      34             : /*                                main()                                */
      35             : /************************************************************************/
      36             : 
      37          20 : MAIN_START(argc, argv)
      38             : 
      39             : {
      40          20 :     EarlySetConfigOptions(argc, argv);
      41             : 
      42             :     /* -------------------------------------------------------------------- */
      43             :     /*      Register standard GDAL drivers, and process generic GDAL        */
      44             :     /*      command options.                                                */
      45             :     /* -------------------------------------------------------------------- */
      46          20 :     GDALAllRegister();
      47          20 :     argc = GDALGeneralCmdLineProcessor(argc, &argv, 0);
      48          20 :     if (argc < 1)
      49           0 :         exit(-argc);
      50             : 
      51          20 :     GDALBuildVRTOptionsForBinary sOptionsForBinary;
      52             :     /* coverity[tainted_data] */
      53             :     GDALBuildVRTOptions *psOptions =
      54          20 :         GDALBuildVRTOptionsNew(argv + 1, &sOptionsForBinary);
      55          19 :     CSLDestroy(argv);
      56             : 
      57          19 :     if (psOptions == nullptr)
      58             :     {
      59           0 :         Usage();
      60             :     }
      61             : 
      62          19 :     if (!(sOptionsForBinary.bQuiet))
      63             :     {
      64          19 :         GDALBuildVRTOptionsSetProgress(psOptions, GDALTermProgress, nullptr);
      65             :     }
      66             : 
      67             :     /* Avoid overwriting a non VRT dataset if the user did not put the */
      68             :     /* filenames in the right order */
      69             :     VSIStatBuf sBuf;
      70          19 :     if (!sOptionsForBinary.bOverwrite)
      71             :     {
      72             :         int bExists =
      73          19 :             (VSIStat(sOptionsForBinary.osDstFilename.c_str(), &sBuf) == 0);
      74          19 :         if (bExists)
      75             :         {
      76           0 :             GDALDriverH hDriver = GDALIdentifyDriver(
      77             :                 sOptionsForBinary.osDstFilename.c_str(), nullptr);
      78           0 :             if (hDriver &&
      79           0 :                 !(EQUAL(GDALGetDriverShortName(hDriver), "VRT") ||
      80           0 :                   (EQUAL(GDALGetDriverShortName(hDriver), "API_PROXY") &&
      81           0 :                    EQUAL(
      82             :                        CPLGetExtension(sOptionsForBinary.osDstFilename.c_str()),
      83             :                        "VRT"))))
      84             :             {
      85           0 :                 fprintf(
      86             :                     stderr,
      87             :                     "'%s' is an existing GDAL dataset managed by %s driver.\n"
      88             :                     "There is an high chance you did not put filenames in the "
      89             :                     "right order.\n"
      90             :                     "If you want to overwrite %s, add -overwrite option to the "
      91             :                     "command line.\n\n",
      92             :                     sOptionsForBinary.osDstFilename.c_str(),
      93             :                     GDALGetDriverShortName(hDriver),
      94             :                     sOptionsForBinary.osDstFilename.c_str());
      95           0 :                 Usage();
      96             :             }
      97             :         }
      98             :     }
      99             : 
     100          19 :     int bUsageError = FALSE;
     101          19 :     GDALDatasetH hOutDS = GDALBuildVRT(
     102             :         sOptionsForBinary.osDstFilename.c_str(),
     103             :         sOptionsForBinary.aosSrcFiles.size(), nullptr,
     104          19 :         sOptionsForBinary.aosSrcFiles.List(), psOptions, &bUsageError);
     105          19 :     if (bUsageError)
     106           1 :         Usage();
     107          18 :     int nRetCode = (hOutDS) ? 0 : 1;
     108             : 
     109          18 :     GDALBuildVRTOptionsFree(psOptions);
     110             : 
     111          18 :     CPLErrorReset();
     112             :     // The flush to disk is only done at that stage, so check if any error has
     113             :     // happened
     114          18 :     if (GDALClose(hOutDS) != CE_None)
     115           0 :         nRetCode = 1;
     116          18 :     if (CPLGetLastErrorType() != CE_None)
     117           1 :         nRetCode = 1;
     118             : 
     119          18 :     GDALDumpOpenDatasets(stderr);
     120             : 
     121          18 :     GDALDestroyDriverManager();
     122             : 
     123          18 :     OGRCleanupAll();
     124             : 
     125          18 :     return nRetCode;
     126             : }
     127             : 
     128           0 : MAIN_END

Generated by: LCOV version 1.14