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-05-03 15:49:35 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             :  * Permission is hereby granted, free of charge, to any person obtaining a
      12             :  * copy of this software and associated documentation files (the "Software"),
      13             :  * to deal in the Software without restriction, including without limitation
      14             :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      15             :  * and/or sell copies of the Software, and to permit persons to whom the
      16             :  * Software is furnished to do so, subject to the following conditions:
      17             :  *
      18             :  * The above copyright notice and this permission notice shall be included
      19             :  * in all copies or substantial portions of the Software.
      20             :  *
      21             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      22             :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      23             :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      24             :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      25             :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      26             :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      27             :  * DEALINGS IN THE SOFTWARE.
      28             :  ****************************************************************************/
      29             : 
      30             : #include "cpl_string.h"
      31             : #include "cpl_error.h"
      32             : #include "commonutils.h"
      33             : #include "gdal_version.h"
      34             : #include "gdal_utils_priv.h"
      35             : 
      36             : /************************************************************************/
      37             : /*                               Usage()                                */
      38             : /************************************************************************/
      39             : 
      40             : static void Usage() CPL_NO_RETURN;
      41             : 
      42           1 : static void Usage()
      43             : 
      44             : {
      45           1 :     fprintf(stderr, "%s\n", GDALBuildVRTGetParserUsage().c_str());
      46           1 :     exit(1);
      47             : }
      48             : 
      49             : /************************************************************************/
      50             : /*                                main()                                */
      51             : /************************************************************************/
      52             : 
      53          20 : MAIN_START(argc, argv)
      54             : 
      55             : {
      56          20 :     EarlySetConfigOptions(argc, argv);
      57             : 
      58             :     /* -------------------------------------------------------------------- */
      59             :     /*      Register standard GDAL drivers, and process generic GDAL        */
      60             :     /*      command options.                                                */
      61             :     /* -------------------------------------------------------------------- */
      62          20 :     GDALAllRegister();
      63          20 :     argc = GDALGeneralCmdLineProcessor(argc, &argv, 0);
      64          20 :     if (argc < 1)
      65           0 :         exit(-argc);
      66             : 
      67          20 :     GDALBuildVRTOptionsForBinary sOptionsForBinary;
      68             :     /* coverity[tainted_data] */
      69             :     GDALBuildVRTOptions *psOptions =
      70          20 :         GDALBuildVRTOptionsNew(argv + 1, &sOptionsForBinary);
      71          19 :     CSLDestroy(argv);
      72             : 
      73          19 :     if (psOptions == nullptr)
      74             :     {
      75           0 :         Usage();
      76             :     }
      77             : 
      78          19 :     if (!(sOptionsForBinary.bQuiet))
      79             :     {
      80          19 :         GDALBuildVRTOptionsSetProgress(psOptions, GDALTermProgress, nullptr);
      81             :     }
      82             : 
      83             :     /* Avoid overwriting a non VRT dataset if the user did not put the */
      84             :     /* filenames in the right order */
      85             :     VSIStatBuf sBuf;
      86          19 :     if (!sOptionsForBinary.bOverwrite)
      87             :     {
      88             :         int bExists =
      89          19 :             (VSIStat(sOptionsForBinary.osDstFilename.c_str(), &sBuf) == 0);
      90          19 :         if (bExists)
      91             :         {
      92           0 :             GDALDriverH hDriver = GDALIdentifyDriver(
      93             :                 sOptionsForBinary.osDstFilename.c_str(), nullptr);
      94           0 :             if (hDriver &&
      95           0 :                 !(EQUAL(GDALGetDriverShortName(hDriver), "VRT") ||
      96           0 :                   (EQUAL(GDALGetDriverShortName(hDriver), "API_PROXY") &&
      97           0 :                    EQUAL(
      98             :                        CPLGetExtension(sOptionsForBinary.osDstFilename.c_str()),
      99             :                        "VRT"))))
     100             :             {
     101           0 :                 fprintf(
     102             :                     stderr,
     103             :                     "'%s' is an existing GDAL dataset managed by %s driver.\n"
     104             :                     "There is an high chance you did not put filenames in the "
     105             :                     "right order.\n"
     106             :                     "If you want to overwrite %s, add -overwrite option to the "
     107             :                     "command line.\n\n",
     108             :                     sOptionsForBinary.osDstFilename.c_str(),
     109             :                     GDALGetDriverShortName(hDriver),
     110             :                     sOptionsForBinary.osDstFilename.c_str());
     111           0 :                 Usage();
     112             :             }
     113             :         }
     114             :     }
     115             : 
     116          19 :     int bUsageError = FALSE;
     117          19 :     GDALDatasetH hOutDS = GDALBuildVRT(
     118             :         sOptionsForBinary.osDstFilename.c_str(),
     119             :         sOptionsForBinary.aosSrcFiles.size(), nullptr,
     120          19 :         sOptionsForBinary.aosSrcFiles.List(), psOptions, &bUsageError);
     121          19 :     if (bUsageError)
     122           1 :         Usage();
     123          18 :     int nRetCode = (hOutDS) ? 0 : 1;
     124             : 
     125          18 :     GDALBuildVRTOptionsFree(psOptions);
     126             : 
     127          18 :     CPLErrorReset();
     128             :     // The flush to disk is only done at that stage, so check if any error has
     129             :     // happened
     130          18 :     if (GDALClose(hOutDS) != CE_None)
     131           0 :         nRetCode = 1;
     132          18 :     if (CPLGetLastErrorType() != CE_None)
     133           1 :         nRetCode = 1;
     134             : 
     135          18 :     GDALDumpOpenDatasets(stderr);
     136             : 
     137          18 :     GDALDestroyDriverManager();
     138             : 
     139          18 :     OGRCleanupAll();
     140             : 
     141          18 :     return nRetCode;
     142             : }
     143             : 
     144           0 : MAIN_END

Generated by: LCOV version 1.14