File: gdalalg_dataset_identify.cpp

package info (click to toggle)
gdal 3.12.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 92,292 kB
  • sloc: cpp: 1,223,498; ansic: 206,434; python: 26,278; java: 6,001; xml: 4,769; sh: 3,855; cs: 2,513; yacc: 1,306; makefile: 214
file content (196 lines) | stat: -rw-r--r-- 7,121 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/******************************************************************************
 *
 * Project:  GDAL
 * Purpose:  gdal "dataset identify" subcommand
 * Author:   Even Rouault <even dot rouault at spatialys.com>
 *
 ******************************************************************************
 * Copyright (c) 2025, Even Rouault <even dot rouault at spatialys.com>
 *
 * SPDX-License-Identifier: MIT
 ****************************************************************************/

#include "gdalalg_dataset_identify.h"

#include "cpl_string.h"

//! @cond Doxygen_Suppress

#ifndef _
#define _(x) (x)
#endif

/************************************************************************/
/*                       GDALDatasetIdentifyAlgorithm()                 */
/************************************************************************/

GDALDatasetIdentifyAlgorithm::GDALDatasetIdentifyAlgorithm()
    : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL), m_oWriter(JSONPrint, this)
{
    AddProgressArg();

    auto &arg = AddArg("filename", 0, _("File or directory name"), &m_filename)
                    .SetPositional()
                    .SetRequired();
    SetAutoCompleteFunctionForFilename(arg, 0);

    AddOutputFormatArg(&m_format).SetChoices("json", "text");

    AddArg("recursive", 'r', _("Recursively scan files/folders for datasets"),
           &m_recursive);

    AddArg("force-recursive", 0,
           _("Recursively scan folders for datasets, forcing "
             "recursion in folders recognized as valid formats"),
           &m_forceRecursive);

    AddArg("report-failures", 0,
           _("Report failures if file type is unidentified"),
           &m_reportFailures);

    AddOutputStringArg(&m_output);
    AddStdoutArg(&m_stdout);
}

/************************************************************************/
/*                GDALDatasetIdentifyAlgorithm::Print()                 */
/************************************************************************/

void GDALDatasetIdentifyAlgorithm::Print(const char *str)
{
    if (m_stdout)
        fwrite(str, 1, strlen(str), stdout);
    else
        m_output += str;
}

/************************************************************************/
/*                 GDALDatasetIdentifyAlgorithm::JSONPrint()            */
/************************************************************************/

/* static */ void GDALDatasetIdentifyAlgorithm::JSONPrint(const char *pszTxt,
                                                          void *pUserData)
{
    static_cast<GDALDatasetIdentifyAlgorithm *>(pUserData)->Print(pszTxt);
}

/************************************************************************/
/*                              Process()                               */
/************************************************************************/

bool GDALDatasetIdentifyAlgorithm::Process(const char *pszTarget,
                                           CSLConstList papszSiblingList,
                                           GDALProgressFunc pfnProgress,
                                           void *pProgressData)

{
    if (IsCalledFromCommandLine())
        pfnProgress = nullptr;

    if (m_format.empty())
        m_format = IsCalledFromCommandLine() ? "text" : "json";

    GDALDriverH hDriver = nullptr;
    {
        CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
        hDriver = GDALIdentifyDriver(pszTarget, papszSiblingList);
    }

    if (m_format == "json")
    {
        if (hDriver)
        {
            m_oWriter.StartObj();
            m_oWriter.AddObjKey("name");
            m_oWriter.Add(pszTarget);
            m_oWriter.AddObjKey("driver");
            m_oWriter.Add(GDALGetDriverShortName(hDriver));
            m_oWriter.EndObj();
        }
        else if (m_reportFailures)
        {
            m_oWriter.StartObj();
            m_oWriter.AddObjKey("name");
            m_oWriter.Add(pszTarget);
            m_oWriter.AddObjKey("driver");
            m_oWriter.AddNull();
            m_oWriter.EndObj();
        }
    }
    else
    {
        if (hDriver)
            Print(CPLSPrintf("%s: %s\n", pszTarget,
                             GDALGetDriverShortName(hDriver)));
        else if (m_reportFailures)
            Print(CPLSPrintf("%s: unrecognized\n", pszTarget));
    }

    bool ret = true;
    VSIStatBufL sStatBuf;
    if ((m_forceRecursive || (m_recursive && hDriver == nullptr)) &&
        VSIStatL(pszTarget, &sStatBuf) == 0 && VSI_ISDIR(sStatBuf.st_mode))
    {
        const CPLStringList aosSiblingList(VSIReadDir(pszTarget));
        const int nListSize = aosSiblingList.size();
        for (int i = 0; i < nListSize; ++i)
        {
            const char *pszSubTarget = aosSiblingList[i];
            if (!(EQUAL(pszSubTarget, "..") || EQUAL(pszSubTarget, ".")))
            {
                const std::string osSubTarget =
                    CPLFormFilenameSafe(pszTarget, pszSubTarget, nullptr);

                std::unique_ptr<void, decltype(&GDALDestroyScaledProgress)>
                    pScaledProgress(GDALCreateScaledProgress(
                                        static_cast<double>(i) / nListSize,
                                        static_cast<double>(i + 1) / nListSize,
                                        pfnProgress, pProgressData),
                                    GDALDestroyScaledProgress);
                ret = ret &&
                      Process(osSubTarget.c_str(), aosSiblingList.List(),
                              pScaledProgress ? GDALScaledProgress : nullptr,
                              pScaledProgress.get());
            }
        }
    }

    return ret && (!pfnProgress || pfnProgress(1.0, "", pProgressData));
}

/************************************************************************/
/*                  GDALDatasetIdentifyAlgorithm::RunImpl()             */
/************************************************************************/

bool GDALDatasetIdentifyAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
                                           void *pProgressData)
{
    if (m_format.empty())
        m_format = IsCalledFromCommandLine() ? "text" : "json";

    if (m_format == "json")
        m_oWriter.StartArray();
    int i = 0;
    bool ret = true;
    for (const std::string &osPath : m_filename)
    {
        std::unique_ptr<void, decltype(&GDALDestroyScaledProgress)>
            pScaledProgress(GDALCreateScaledProgress(
                                static_cast<double>(i) /
                                    static_cast<int>(m_filename.size()),
                                static_cast<double>(i + 1) /
                                    static_cast<int>(m_filename.size()),
                                pfnProgress, pProgressData),
                            GDALDestroyScaledProgress);
        ret = ret && Process(osPath.c_str(), nullptr,
                             pScaledProgress ? GDALScaledProgress : nullptr,
                             pScaledProgress.get());
        ++i;
    }
    if (m_format == "json")
        m_oWriter.EndArray();

    return ret;
}

//! @endcond