LCOV - code coverage report
Current view: top level - apps - gdalmanage.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 37 91 40.7 %
Date: 2024-05-03 15:49:35 Functions: 3 6 50.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GDAL Utilities
       4             :  * Purpose:  Command line utility for GDAL identify, delete, rename and copy
       5             :  *           (by file) operations.
       6             :  * Author:   Frank Warmerdam, warmerdam@pobox.com
       7             :  *
       8             :  * ****************************************************************************
       9             :  * Copyright (c) 2007, Frank Warmerdam
      10             :  * Copyright (c) 2008-2009, Even Rouault <even dot rouault at spatialys.com>
      11             :  *
      12             :  * Permission is hereby granted, free of charge, to any person obtaining a
      13             :  * copy of this software and associated documentation files (the "Software"),
      14             :  * to deal in the Software without restriction, including without limitation
      15             :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      16             :  * and/or sell copies of the Software, and to permit persons to whom the
      17             :  * Software is furnished to do so, subject to the following conditions:
      18             :  *
      19             :  * The above copyright notice and this permission notice shall be included
      20             :  * in all copies or substantial portions of the Software.
      21             :  *
      22             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      23             :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      24             :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      25             :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      26             :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      27             :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      28             :  * DEALINGS IN THE SOFTWARE.
      29             :  ****************************************************************************/
      30             : 
      31             : #include "cpl_string.h"
      32             : #include "cpl_conv.h"
      33             : #include "gdal_version.h"
      34             : #include "gdal.h"
      35             : #include "commonutils.h"
      36             : 
      37             : /************************************************************************/
      38             : /*                               Usage()                                */
      39             : /************************************************************************/
      40             : 
      41           0 : static void Usage(bool bIsError)
      42             : 
      43             : {
      44           0 :     fprintf(bIsError ? stderr : stdout,
      45             :             "Usage: gdalmanage [--help] [--help-general]\n"
      46             :             "    or gdalmanage identify [-r|-fr] [-u] <files>*\n"
      47             :             "    or gdalmanage copy [-f <driver>] <oldname> <newname>\n"
      48             :             "    or gdalmanage rename [-f <driver>] <oldname> <newname>\n"
      49             :             "    or gdalmanage delete [-f <driver>] <datasetname>\n");
      50           0 :     exit(bIsError ? 1 : 0);
      51             : }
      52             : 
      53             : /************************************************************************/
      54             : /*                       ProcessIdentifyTarget()                        */
      55             : /************************************************************************/
      56             : 
      57           1 : static void ProcessIdentifyTarget(const char *pszTarget,
      58             :                                   char **papszSiblingList, bool bRecursive,
      59             :                                   bool bReportFailures, bool bForceRecurse)
      60             : 
      61             : {
      62             :     GDALDriverH hDriver;
      63             :     VSIStatBufL sStatBuf;
      64             :     int i;
      65             : 
      66           1 :     hDriver = GDALIdentifyDriver(pszTarget, papszSiblingList);
      67             : 
      68           1 :     if (hDriver != nullptr)
      69           1 :         printf("%s: %s\n", pszTarget, GDALGetDriverShortName(hDriver));
      70           0 :     else if (bReportFailures)
      71           0 :         printf("%s: unrecognized\n", pszTarget);
      72             : 
      73           1 :     if (!bForceRecurse && (!bRecursive || hDriver != nullptr))
      74           1 :         return;
      75             : 
      76           0 :     if (VSIStatL(pszTarget, &sStatBuf) != 0 || !VSI_ISDIR(sStatBuf.st_mode))
      77           0 :         return;
      78             : 
      79           0 :     papszSiblingList = VSIReadDir(pszTarget);
      80           0 :     for (i = 0; papszSiblingList && papszSiblingList[i]; i++)
      81             :     {
      82           0 :         if (EQUAL(papszSiblingList[i], "..") || EQUAL(papszSiblingList[i], "."))
      83           0 :             continue;
      84             : 
      85             :         CPLString osSubTarget =
      86           0 :             CPLFormFilename(pszTarget, papszSiblingList[i], nullptr);
      87             : 
      88           0 :         ProcessIdentifyTarget(osSubTarget, papszSiblingList, bRecursive,
      89             :                               bReportFailures, bForceRecurse);
      90             :     }
      91           0 :     CSLDestroy(papszSiblingList);
      92             : }
      93             : 
      94             : /************************************************************************/
      95             : /*                              Identify()                              */
      96             : /************************************************************************/
      97             : 
      98           1 : static void Identify(int nArgc, char **papszArgv)
      99             : 
     100             : {
     101             :     /* -------------------------------------------------------------------- */
     102             :     /*      Scan for command line switches                                   */
     103             :     /* -------------------------------------------------------------------- */
     104           1 :     bool bRecursive = false;
     105           1 :     bool bForceRecurse = false;
     106           1 :     bool bReportFailures = false;
     107             : 
     108           1 :     int i = 0;
     109           1 :     for (; i < nArgc && papszArgv[i][0] == '-'; ++i)
     110             :     {
     111           0 :         if (EQUAL(papszArgv[i], "-r"))
     112           0 :             bRecursive = true;
     113           0 :         else if (EQUAL(papszArgv[i], "-fr"))
     114             :         {
     115           0 :             bForceRecurse = true;
     116           0 :             bRecursive = true;
     117             :         }
     118           0 :         else if (EQUAL(papszArgv[i], "-u"))
     119           0 :             bReportFailures = true;
     120             :         else
     121           0 :             Usage(true);
     122             :     }
     123             : 
     124             :     /* -------------------------------------------------------------------- */
     125             :     /*      Process given files.                                            */
     126             :     /* -------------------------------------------------------------------- */
     127           2 :     for (; i < nArgc; ++i)
     128             :     {
     129           1 :         ProcessIdentifyTarget(papszArgv[i], nullptr, bRecursive,
     130             :                               bReportFailures, bForceRecurse);
     131             :     }
     132           1 : }
     133             : 
     134             : /************************************************************************/
     135             : /*                               Delete()                               */
     136             : /************************************************************************/
     137             : 
     138           0 : static void Delete(GDALDriverH hDriver, int nArgc, char **papszArgv)
     139             : 
     140             : {
     141           0 :     if (nArgc != 1)
     142           0 :         Usage(true);
     143             : 
     144           0 :     GDALDeleteDataset(hDriver, papszArgv[0]);
     145           0 : }
     146             : 
     147             : /************************************************************************/
     148             : /*                                Copy()                                */
     149             : /************************************************************************/
     150             : 
     151           0 : static void Copy(GDALDriverH hDriver, int nArgc, char **papszArgv,
     152             :                  const char *pszOperation)
     153             : 
     154             : {
     155           0 :     if (nArgc != 2)
     156           0 :         Usage(true);
     157             : 
     158           0 :     if (EQUAL(pszOperation, "copy"))
     159           0 :         GDALCopyDatasetFiles(hDriver, papszArgv[1], papszArgv[0]);
     160             :     else
     161           0 :         GDALRenameDataset(hDriver, papszArgv[1], papszArgv[0]);
     162           0 : }
     163             : 
     164             : /************************************************************************/
     165             : /*                                main()                                */
     166             : /************************************************************************/
     167             : 
     168           2 : MAIN_START(argc, argv)
     169             : 
     170             : {
     171           2 :     char *pszDriver = nullptr;
     172           2 :     GDALDriverH hDriver = nullptr;
     173             : 
     174             :     /* Check that we are running against at least GDAL 1.5 */
     175             :     /* Note to developers : if we use newer API, please change the requirement
     176             :      */
     177           2 :     if (atoi(GDALVersionInfo("VERSION_NUM")) < 1500)
     178             :     {
     179           0 :         fprintf(stderr,
     180             :                 "At least, GDAL >= 1.5.0 is required for this version of %s, "
     181             :                 "which was compiled against GDAL %s\n",
     182             :                 argv[0], GDAL_RELEASE_NAME);
     183           0 :         exit(1);
     184             :     }
     185             : 
     186           2 :     GDALAllRegister();
     187             : 
     188           2 :     argc = GDALGeneralCmdLineProcessor(argc, &argv, 0);
     189           2 :     if (argc < 1)
     190           0 :         exit(-argc);
     191             : 
     192           6 :     for (int i = 0; i < argc; i++)
     193             :     {
     194           5 :         if (EQUAL(argv[i], "--utility_version"))
     195             :         {
     196           1 :             printf("%s was compiled against GDAL %s and is running against "
     197             :                    "GDAL %s\n",
     198             :                    argv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME"));
     199           1 :             return 0;
     200             :         }
     201           4 :         else if (EQUAL(argv[i], "--help"))
     202             :         {
     203           0 :             Usage(false);
     204             :         }
     205             :     }
     206             : 
     207           1 :     if (argc < 3)
     208           0 :         Usage(true);
     209             : 
     210             :     /* -------------------------------------------------------------------- */
     211             :     /*      Do we have a driver specifier?                                  */
     212             :     /* -------------------------------------------------------------------- */
     213           1 :     char **papszRemainingArgv = argv + 2;
     214           1 :     int nRemainingArgc = argc - 2;
     215             : 
     216           1 :     if (EQUAL(papszRemainingArgv[0], "-f") && nRemainingArgc > 1)
     217             :     {
     218           0 :         pszDriver = papszRemainingArgv[1];
     219           0 :         papszRemainingArgv += 2;
     220           0 :         nRemainingArgc -= 2;
     221             :     }
     222             : 
     223           1 :     if (pszDriver != nullptr)
     224             :     {
     225           0 :         hDriver = GDALGetDriverByName(pszDriver);
     226           0 :         if (hDriver == nullptr)
     227             :         {
     228           0 :             fprintf(stderr, "Unable to find driver named '%s'.\n", pszDriver);
     229           0 :             exit(1);
     230             :         }
     231             :     }
     232             : 
     233             :     /* -------------------------------------------------------------------- */
     234             :     /*      Split out based on operation.                                   */
     235             :     /* -------------------------------------------------------------------- */
     236           1 :     if (STARTS_WITH_CI(argv[1], "ident" /* identify" */))
     237           1 :         Identify(nRemainingArgc, papszRemainingArgv);
     238             : 
     239           0 :     else if (EQUAL(argv[1], "copy"))
     240           0 :         Copy(hDriver, nRemainingArgc, papszRemainingArgv, "copy");
     241             : 
     242           0 :     else if (EQUAL(argv[1], "rename"))
     243           0 :         Copy(hDriver, nRemainingArgc, papszRemainingArgv, "rename");
     244             : 
     245           0 :     else if (EQUAL(argv[1], "delete"))
     246           0 :         Delete(hDriver, nRemainingArgc, papszRemainingArgv);
     247             : 
     248             :     else
     249           0 :         Usage(true);
     250             : 
     251             :     /* -------------------------------------------------------------------- */
     252             :     /*      Cleanup                                                         */
     253             :     /* -------------------------------------------------------------------- */
     254           1 :     CSLDestroy(argv);
     255           1 :     GDALDestroyDriverManager();
     256             : 
     257           1 :     exit(0);
     258             : }
     259             : 
     260           0 : MAIN_END

Generated by: LCOV version 1.14