Coverage Report

Created: 2025-06-13 06:18

/src/gdal/ogr/ogrsf_frmts/generic/ogrsfdriver.cpp
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 *
3
 * Project:  OpenGIS Simple Features Reference Implementation
4
 * Purpose:  The generic portions of the OGRSFDriver class.
5
 * Author:   Frank Warmerdam, warmerdam@pobox.com
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 1999,  Les Technologies SoftMap Inc.
9
 * Copyright (c) 2009-2011, Even Rouault <even dot rouault at spatialys.com>
10
 *
11
 * SPDX-License-Identifier: MIT
12
 ****************************************************************************/
13
14
#include "ogrsf_frmts.h"
15
#include "ogr_api.h"
16
#include "ogr_p.h"
17
#include "ograpispy.h"
18
19
//! @cond Doxygen_Suppress
20
21
/************************************************************************/
22
/*                            ~OGRSFDriver()                            */
23
/************************************************************************/
24
25
OGRSFDriver::~OGRSFDriver()
26
27
0
{
28
0
}
29
30
/************************************************************************/
31
/*                          CreateDataSource()                          */
32
/************************************************************************/
33
34
OGRDataSource *OGRSFDriver::CreateDataSource(const char *, char **)
35
36
0
{
37
0
    CPLError(CE_Failure, CPLE_NotSupported,
38
0
             "CreateDataSource() not supported by this driver.\n");
39
40
0
    return nullptr;
41
0
}
42
43
/************************************************************************/
44
/*                      OGR_Dr_CreateDataSource()                       */
45
/************************************************************************/
46
47
OGRDataSourceH OGR_Dr_CreateDataSource(OGRSFDriverH hDriver,
48
                                       const char *pszName, char **papszOptions)
49
50
0
{
51
0
    VALIDATE_POINTER1(hDriver, "OGR_Dr_CreateDataSource", nullptr);
52
53
0
    GDALDriver *poDriver = reinterpret_cast<GDALDriver *>(hDriver);
54
55
    /* MapServer had the bad habit of calling with NULL name for a memory
56
     * datasource */
57
0
    if (pszName == nullptr)
58
0
        pszName = "";
59
60
0
    OGRDataSourceH hDS = reinterpret_cast<OGRDataSourceH>(
61
0
        poDriver->Create(pszName, 0, 0, 0, GDT_Unknown, papszOptions));
62
63
0
#ifdef OGRAPISPY_ENABLED
64
0
    OGRAPISpyCreateDataSource(hDriver, pszName, papszOptions,
65
0
                              reinterpret_cast<OGRDataSourceH>(hDS));
66
0
#endif
67
68
0
    return hDS;
69
0
}
70
71
/************************************************************************/
72
/*                          DeleteDataSource()                          */
73
/************************************************************************/
74
75
OGRErr OGRSFDriver::DeleteDataSource(const char *pszDataSource)
76
77
0
{
78
0
    (void)pszDataSource;
79
0
    CPLError(CE_Failure, CPLE_NotSupported,
80
0
             "DeleteDataSource() not supported by this driver.");
81
82
0
    return OGRERR_UNSUPPORTED_OPERATION;
83
0
}
84
85
/************************************************************************/
86
/*                      OGR_Dr_DeleteDataSource()                       */
87
/************************************************************************/
88
89
OGRErr OGR_Dr_DeleteDataSource(OGRSFDriverH hDriver, const char *pszDataSource)
90
91
0
{
92
0
    VALIDATE_POINTER1(hDriver, "OGR_Dr_DeleteDataSource",
93
0
                      OGRERR_INVALID_HANDLE);
94
95
0
#ifdef OGRAPISPY_ENABLED
96
0
    OGRAPISpyDeleteDataSource(hDriver, pszDataSource);
97
0
#endif
98
99
0
    CPLErr eErr =
100
0
        reinterpret_cast<GDALDriver *>(hDriver)->Delete(pszDataSource);
101
0
    if (eErr == CE_None)
102
0
        return OGRERR_NONE;
103
0
    else
104
0
        return OGRERR_FAILURE;
105
0
}
106
107
/************************************************************************/
108
/*                           OGR_Dr_GetName()                           */
109
/************************************************************************/
110
111
const char *OGR_Dr_GetName(OGRSFDriverH hDriver)
112
113
0
{
114
0
    VALIDATE_POINTER1(hDriver, "OGR_Dr_GetName", nullptr);
115
116
0
    return reinterpret_cast<GDALDriver *>(hDriver)->GetDescription();
117
0
}
118
119
/************************************************************************/
120
/*                            OGR_Dr_Open()                             */
121
/************************************************************************/
122
123
OGRDataSourceH OGR_Dr_Open(OGRSFDriverH hDriver, const char *pszName,
124
                           int bUpdate)
125
126
0
{
127
0
    VALIDATE_POINTER1(hDriver, "OGR_Dr_Open", nullptr);
128
129
0
    const char *const apszDrivers[] = {
130
0
        reinterpret_cast<GDALDriver *>(hDriver)->GetDescription(), nullptr};
131
132
0
#ifdef OGRAPISPY_ENABLED
133
0
    int iSnapshot = OGRAPISpyOpenTakeSnapshot(pszName, bUpdate);
134
0
#endif
135
136
0
    GDALDatasetH hDS =
137
0
        GDALOpenEx(pszName, GDAL_OF_VECTOR | ((bUpdate) ? GDAL_OF_UPDATE : 0),
138
0
                   apszDrivers, nullptr, nullptr);
139
140
0
#ifdef OGRAPISPY_ENABLED
141
0
    OGRAPISpyOpen(pszName, bUpdate, iSnapshot, &hDS);
142
0
#endif
143
144
0
    return reinterpret_cast<OGRDataSourceH>(hDS);
145
0
}
146
147
/************************************************************************/
148
/*                       OGR_Dr_TestCapability()                        */
149
/************************************************************************/
150
151
int OGR_Dr_TestCapability(OGRSFDriverH hDriver, const char *pszCap)
152
153
0
{
154
0
    VALIDATE_POINTER1(hDriver, "OGR_Dr_TestCapability", 0);
155
0
    VALIDATE_POINTER1(pszCap, "OGR_Dr_TestCapability", 0);
156
157
0
    GDALDriver *poDriver = reinterpret_cast<GDALDriver *>(hDriver);
158
0
    if (EQUAL(pszCap, ODrCCreateDataSource))
159
0
    {
160
0
        return poDriver->GetMetadataItem(GDAL_DCAP_CREATE) ||
161
0
               poDriver->pfnCreate != nullptr ||
162
0
               poDriver->pfnCreateVectorOnly != nullptr;
163
0
    }
164
0
    else if (EQUAL(pszCap, ODrCDeleteDataSource))
165
0
    {
166
0
        return poDriver->pfnDelete != nullptr ||
167
0
               poDriver->pfnDeleteDataSource != nullptr;
168
0
    }
169
0
    else
170
0
        return FALSE;
171
0
}
172
173
/************************************************************************/
174
/*                       OGR_Dr_CopyDataSource()                        */
175
/************************************************************************/
176
177
OGRDataSourceH OGR_Dr_CopyDataSource(OGRSFDriverH hDriver,
178
                                     OGRDataSourceH hSrcDS,
179
                                     const char *pszNewName,
180
                                     char **papszOptions)
181
182
0
{
183
0
    VALIDATE_POINTER1(hDriver, "OGR_Dr_CopyDataSource", nullptr);
184
0
    VALIDATE_POINTER1(hSrcDS, "OGR_Dr_CopyDataSource", nullptr);
185
0
    VALIDATE_POINTER1(pszNewName, "OGR_Dr_CopyDataSource", nullptr);
186
187
0
    GDALDriver *poDriver = reinterpret_cast<GDALDriver *>(hDriver);
188
0
    if (!poDriver->GetMetadataItem(GDAL_DCAP_CREATE))
189
0
    {
190
0
        CPLError(CE_Failure, CPLE_NotSupported,
191
0
                 "%s driver does not support data source creation.",
192
0
                 poDriver->GetDescription());
193
0
        return nullptr;
194
0
    }
195
196
0
    GDALDataset *poSrcDS = GDALDataset::FromHandle(hSrcDS);
197
0
    GDALDataset *poODS =
198
0
        poDriver->Create(pszNewName, 0, 0, 0, GDT_Unknown, papszOptions);
199
0
    if (poODS == nullptr)
200
0
        return nullptr;
201
202
    /* -------------------------------------------------------------------- */
203
    /*      Process each data source layer.                                 */
204
    /* -------------------------------------------------------------------- */
205
0
    for (int iLayer = 0; iLayer < poSrcDS->GetLayerCount(); iLayer++)
206
0
    {
207
0
        OGRLayer *poLayer = poSrcDS->GetLayer(iLayer);
208
209
0
        if (poLayer == nullptr)
210
0
            continue;
211
212
0
        poODS->CopyLayer(poLayer, poLayer->GetLayerDefn()->GetName(),
213
0
                         papszOptions);
214
0
    }
215
216
0
    return reinterpret_cast<OGRDataSourceH>(poODS);
217
0
}
218
219
//! @endcond