Debian Codegraph @

ogr/ogrsf_frmts/gpsbabel/ogrgpsbabeldatasource.cpp

raw symbols SCIP
1 /******************************************************************************
2 *
3 * Project: OpenGIS Simple Features Reference Implementation
4 * Purpose: Implements OGRGPSBabelDataSource class.
5 * Author: Even Rouault, <even dot rouault at spatialys.com>
6 *
7 ******************************************************************************
8 * Copyright (c) 2010-2013, Even Rouault <even dot rouault at spatialys.com>
9 *
10 * SPDX-License-Identifier: MIT
11 ****************************************************************************/
12
13 #include "cpl_conv.h"cxx . . $ `<file>/port/cpl_conv.h`/
14 #include "cpl_string.h"cxx . . $ `<file>/port/cpl_string.h`/
15 #include "cpl_error.h"cxx . . $ `<file>/port/cpl_error.h`/
16 #include "cpl_spawn.h"cxx . . $ `<file>/port/cpl_spawn.h`/
17 #include "ogr_gpsbabel.h"cxx . . $ `<file>/ogr/ogrsf_frmts/gpsbabel/ogr_gpsbabel.h`/
18
19 #include <cstring>
20 #include <algorithm>
21
22 /************************************************************************/
23 /* OGRGPSBabelDataSource() */
24 /************************************************************************/
25
26 OGRGPSBabelDataSourcecxx . . $ OGRGPSBabelDataSource#::OGRGPSBabelDataSourcecxx . . $ OGRGPSBabelDataSource#OGRGPSBabelDataSource(49f6e7a06ebc5aa8).()
27 {
28 }
29
30 /************************************************************************/
31 /* ~OGRGPSBabelDataSource() */
32 /************************************************************************/
33
34 OGRGPSBabelDataSourcecxx . . $ OGRGPSBabelDataSource#::~cxx . . $ OGRGPSBabelDataSource#`~OGRGPSBabelDataSource`(ced63f7c635d850d).OGRGPSBabelDataSourcecxx . . $ OGRGPSBabelDataSource#()
35
36 {
37 CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(pszGPSBabelDriverNamecxx . . $ OGRGPSBabelDataSource#pszGPSBabelDriverName.);
38 CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(pszFilenamecxx . . $ OGRGPSBabelDataSource#pszFilename.);
39
40 OGRGPSBabelDataSourcecxx . . $ OGRGPSBabelDataSource#::CloseDependentDatasetscxx . . $ OGRGPSBabelDataSource#CloseDependentDatasets(b126dc7c1de90089).();
41
42 if (!osTmpFileNamecxx . . $ OGRGPSBabelDataSource#osTmpFileName..empty())
43 VSIUnlink(osTmpFileNamecxx . . $ OGRGPSBabelDataSource#osTmpFileName..c_str());
44 }
45
46 /************************************************************************/
47 /* CloseDependentDatasets() */
48 /************************************************************************/
49
50 int OGRGPSBabelDataSourcecxx . . $ OGRGPSBabelDataSource#::CloseDependentDatasetscxx . . $ OGRGPSBabelDataSource#CloseDependentDatasets(b126dc7c1de90089).()
51 {
52 if (poGPXDScxx . . $ OGRGPSBabelDataSource#poGPXDS. == nullptr)
53 return FALSEcxx . . $ `port/cpl_port.h:1180:9`!;
54
55 GDALClosecxx . . $ GDALClose(3d5aeed3fac2555e).(poGPXDScxx . . $ OGRGPSBabelDataSource#poGPXDS.);
56 poGPXDScxx . . $ OGRGPSBabelDataSource#poGPXDS. = nullptr;
57 return TRUEcxx . . $ `port/cpl_port.h:1184:9`!;
58 }
59
60 /************************************************************************/
61 /* GetArgv() */
62 /************************************************************************/
63
64 static char **GetArgvcxx . . $ GetArgv(b5ee7b4a4d51078c).(int bExplicitFeatures, int bWaypoints, int bRoutes,
65 int bTracks, const char *pszGPSBabelDriverName,
66 const char *pszFilename)
67 {
68 char **argv = CSLAddString(nullptr, "gpsbabel");
69 if (bExplicitFeatures)
70 {
71 if (bWaypoints)
72 argv = CSLAddString(argv, "-w");
73 if (bRoutes)
74 argv = CSLAddString(argv, "-r");
75 if (bTracks)
76 argv = CSLAddString(argv, "-t");
77 }
78 argv = CSLAddString(argv, "-i");
79 argv = CSLAddString(argv, pszGPSBabelDriverName);
80 argv = CSLAddString(argv, "-f");
81 argv = CSLAddString(argv, pszFilename);
82 argv = CSLAddString(argv, "-o");
83 argv = CSLAddString(argv, "gpx,gpxver=1.1");
84 argv = CSLAddString(argv, "-F");
85 argv = CSLAddString(argv, "-");
86
87 return argv;
88 }
89
90 /************************************************************************/
91 /* IsSpecialFile() */
92 /************************************************************************/
93
94 bool OGRGPSBabelDataSourcecxx . . $ OGRGPSBabelDataSource#::IsSpecialFilecxx . . $ OGRGPSBabelDataSource#IsSpecialFile(c5f7da30cbc0f4e9).(const char *pszFilename)
95 {
96 return STARTS_WITHcxx . . $ `port/cpl_port.h:541:9`!(pszFilename, "/dev/") ||
97 STARTS_WITHcxx . . $ `port/cpl_port.h:541:9`!(pszFilename, "usb:") ||
98 (STARTS_WITHcxx . . $ `port/cpl_port.h:541:9`!(pszFilename, "COM") && atoicxx . . $ atoi(f5ecf3191ed0cd93).(pszFilename + 3) > 0);
99 }
100
101 /************************************************************************/
102 /* IsValidDriverName() */
103 /************************************************************************/
104
105 bool OGRGPSBabelDataSourcecxx . . $ OGRGPSBabelDataSource#::IsValidDriverNamecxx . . $ OGRGPSBabelDataSource#IsValidDriverName(c5f7da30cbc0f4e9).(const char *pszGPSBabelDriverName)
106 {
107 for (int i = 0; pszGPSBabelDriverName[i] != '\0'; i++)
108 {
109 char ch = pszGPSBabelDriverName[i];
110 if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') ||
111 (ch >= '0' && ch <= '9') || ch == '_' || ch == '=' || ch == '.' ||
112 ch == ','))
113 {
114 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!,
115 "Invalid GPSBabel driver name");
116 return false;
117 }
118 }
119 return true;
120 }
121
122 /************************************************************************/
123 /* Open() */
124 /************************************************************************/
125
126 int OGRGPSBabelDataSourcecxx . . $ OGRGPSBabelDataSource#::Opencxx . . $ OGRGPSBabelDataSource#Open(f0dc5b3d4b8a75).(const char *pszDatasourceName,
127 const char *pszGPSBabelDriverNameIn,
128 char **papszOpenOptionsIn)
129
130 {
131 constexpr const char *GPSBABEL_PREFIX = "GPSBABEL:";
132 if (!STARTS_WITH_CIcxx . . $ `port/cpl_port.h:543:9`!(pszDatasourceName, GPSBABEL_PREFIX))
133 {
134 CPLAssertcxx . . $ `port/cpl_error.h:353:9`!(pszGPSBabelDriverNameIn);
135 pszGPSBabelDriverNamecxx . . $ OGRGPSBabelDataSource#pszGPSBabelDriverName. = CPLStrdup(pszGPSBabelDriverNameIn);
136 pszFilenamecxx . . $ OGRGPSBabelDataSource#pszFilename. = CPLStrdup(pszDatasourceName);
137 }
138 else
139 {
140 if (CSLFetchNameValue(papszOpenOptionsIn, "FILENAME"))
141 pszFilenamecxx . . $ OGRGPSBabelDataSource#pszFilename. =
142 CPLStrdup(CSLFetchNameValue(papszOpenOptionsIn, "FILENAME"));
143
144 if (CSLFetchNameValue(papszOpenOptionsIn, "GPSBABEL_DRIVER"))
145 {
146 if (pszFilenamecxx . . $ OGRGPSBabelDataSource#pszFilename. == nullptr)
147 {
148 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, "Missing FILENAME");
149 return FALSEcxx . . $ `port/cpl_port.h:1180:9`!;
150 }
151
152 pszGPSBabelDriverNamecxx . . $ OGRGPSBabelDataSource#pszGPSBabelDriverName. =
153 CPLStrdup(CSLFetchNameValue(papszOpenOptionsIn, "DRIVER"));
154
155 /* A bit of validation to avoid command line injection */
156 if (!IsValidDriverNamecxx . . $ OGRGPSBabelDataSource#IsValidDriverName(c5f7da30cbc0f4e9).(pszGPSBabelDriverNamecxx . . $ OGRGPSBabelDataSource#pszGPSBabelDriverName.))
157 return FALSEcxx . . $ `port/cpl_port.h:1180:9`!;
158 }
159 }
160
161 bool bExplicitFeatures = false;
162 bool bWaypoints = true;
163 bool bTracks = true;
164 bool bRoutes = true;
165
166 if (pszGPSBabelDriverNamecxx . . $ OGRGPSBabelDataSource#pszGPSBabelDriverName. == nullptr)
167 {
168 const char *pszDatasourceNameAfterPrefix =
169 pszDatasourceName + strlen(GPSBABEL_PREFIX);
170 const char *pszSep = strchr(pszDatasourceNameAfterPrefix, ':');
171 if (pszSep == nullptr)
172 {
173 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!,
174 "Wrong syntax. Expected GPSBabel:driver_name:file_name");
175 return FALSEcxx . . $ `port/cpl_port.h:1180:9`!;
176 }
177
178 pszGPSBabelDriverNamecxx . . $ OGRGPSBabelDataSource#pszGPSBabelDriverName. = CPLStrdup(pszDatasourceNameAfterPrefix);
179 pszGPSBabelDriverNamecxx . . $ OGRGPSBabelDataSource#pszGPSBabelDriverName.[pszSep - pszDatasourceNameAfterPrefix] = '\0';
180
181 /* A bit of validation to avoid command line injection */
182 if (!IsValidDriverNamecxx . . $ OGRGPSBabelDataSource#IsValidDriverName(c5f7da30cbc0f4e9).(pszGPSBabelDriverNamecxx . . $ OGRGPSBabelDataSource#pszGPSBabelDriverName.))
183 return FALSEcxx . . $ `port/cpl_port.h:1180:9`!;
184
185 /* Parse optional features= option */
186 const char *pszAfterSep = pszSep + 1;
187 constexpr const char *FEATURES_EQUAL = "features=";
188 if (STARTS_WITH_CIcxx . . $ `port/cpl_port.h:543:9`!(pszAfterSep, FEATURES_EQUAL))
189 {
190 const char *pszAfterFeaturesEqual =
191 pszAfterSep + strlen(FEATURES_EQUAL);
192 const char *pszNextSep = strchr(pszAfterFeaturesEqual, ':');
193 if (pszNextSep == nullptr)
194 {
195 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!,
196 "Wrong syntax. Expected "
197 "GPSBabel:driver_name[,options]*:["
198 "features=waypoints,tracks,routes:]file_name");
199 return FALSEcxx . . $ `port/cpl_port.h:1180:9`!;
200 }
201
202 char *pszFeatures = CPLStrdup(pszAfterFeaturesEqual);
203 pszFeatures[pszNextSep - pszAfterFeaturesEqual] = 0;
204 char **papszTokens = CSLTokenizeString(pszFeatures);
205 char **papszIter = papszTokens;
206 bool bErr = false;
207 bExplicitFeatures = true;
208 bWaypoints = false;
209 bTracks = false;
210 bRoutes = false;
211 while (papszIter && *papszIter)
212 {
213 if (EQUALcxx . . $ `port/cpl_port.h:532:9`!(*papszIter, "waypoints"))
214 bWaypoints = true;
215 else if (EQUALcxx . . $ `port/cpl_port.h:532:9`!(*papszIter, "tracks"))
216 bTracks = true;
217 else if (EQUALcxx . . $ `port/cpl_port.h:532:9`!(*papszIter, "routes"))
218 bRoutes = true;
219 else
220 {
221 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!,
222 "Wrong value for 'features' options");
223 bErr = true;
224 }
225 papszIter++;
226 }
227 CSLDestroy(papszTokens);
228 CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(pszFeatures);
229
230 if (bErr)
231 return FALSEcxx . . $ `port/cpl_port.h:1180:9`!;
232
233 pszAfterSep = pszNextSep + 1;
234 }
235
236 if (pszFilenamecxx . . $ OGRGPSBabelDataSource#pszFilename. == nullptr)
237 pszFilenamecxx . . $ OGRGPSBabelDataSource#pszFilename. = CPLStrdup(pszAfterSep);
238 }
239
240 const char *pszOptionUseTempFile =
241 CPLGetConfigOption("USE_TEMPFILE", nullptr);
242 if (pszOptionUseTempFile && CPLTestBool(pszOptionUseTempFile))
243 osTmpFileNamecxx . . $ OGRGPSBabelDataSource#osTmpFileName. = CPLGenerateTempFilenameSafe(nullptr);
244 else
245 osTmpFileNamecxx . . $ OGRGPSBabelDataSource#osTmpFileName. = VSIMemGenerateHiddenFilename("gpsbabel");
246
247 bool bRet = false;
248 if (IsSpecialFilecxx . . $ OGRGPSBabelDataSource#IsSpecialFile(c5f7da30cbc0f4e9).(pszFilenamecxx . . $ OGRGPSBabelDataSource#pszFilename.))
249 {
250 /* Special file : don't try to open it */
251 char **argv = GetArgvcxx . . $ GetArgv(b5ee7b4a4d51078c).(bExplicitFeatures, bWaypoints, bRoutes, bTracks,
252 pszGPSBabelDriverNamecxx . . $ OGRGPSBabelDataSource#pszGPSBabelDriverName., pszFilenamecxx . . $ OGRGPSBabelDataSource#pszFilename.);
253 VSILFILE *tmpfp = VSIFOpenL(osTmpFileNamecxx . . $ OGRGPSBabelDataSource#osTmpFileName..c_str(), "wb");
254 bRet = (CPLSpawn(argv, nullptr, tmpfp, TRUEcxx . . $ `port/cpl_port.h:1184:9`!) == 0);
255 VSIFCloseL(tmpfp);
256 tmpfp = nullptr;
257 CSLDestroy(argv);
258 argv = nullptr;
259 }
260 else
261 {
262 VSILFILE *fp = VSIFOpenL(pszFilenamecxx . . $ OGRGPSBabelDataSource#pszFilename., "rb");
263 if (fp == nullptr)
264 {
265 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, "Cannot open file %s",
266 pszFilenamecxx . . $ OGRGPSBabelDataSource#pszFilename.);
267 return FALSEcxx . . $ `port/cpl_port.h:1180:9`!;
268 }
269
270 char **argv = GetArgvcxx . . $ GetArgv(b5ee7b4a4d51078c).(bExplicitFeatures, bWaypoints, bRoutes, bTracks,
271 pszGPSBabelDriverNamecxx . . $ OGRGPSBabelDataSource#pszGPSBabelDriverName., "-");
272
273 VSILFILE *tmpfp = VSIFOpenL(osTmpFileNamecxx . . $ OGRGPSBabelDataSource#osTmpFileName..c_str(), "wb");
274
275 CPLPushErrorHandler(CPLQuietErrorHandler);
276 bRet = (CPLSpawn(argv, fp, tmpfp, TRUEcxx . . $ `port/cpl_port.h:1184:9`!) == 0);
277 CPLPopErrorHandler();
278
279 CSLDestroy(argv);
280 argv = nullptr;
281
282 CPLErr nLastErrorType = CPLGetLastErrorType();
283 CPLErrorNum nLastErrorNo = CPLGetLastErrorNo();
284 CPLString osLastErrorMsg = CPLGetLastErrorMsg();
285
286 VSIFCloseL(tmpfp);
287 tmpfp = nullptr;
288
289 VSIFCloseL(fp);
290 fp = nullptr;
291
292 if (!bRet)
293 {
294 if (strstr(osLastErrorMsg.c_str(),
295 "This format cannot be used in piped commands") ==
296 nullptr)
297 {
298 CPLError(nLastErrorType, nLastErrorNo, "%s",
299 osLastErrorMsg.c_str());
300 }
301 else
302 {
303 VSIStatBuf sStatBuf;
304 if (VSIStat(pszFilenamecxx . . $ OGRGPSBabelDataSource#pszFilename., &sStatBuf) != 0)
305 {
306 CPLError(CE_Failure, CPLE_NotSupportedcxx . . $ `port/cpl_error.h:118:9`!,
307 "Driver %s only supports real (non virtual) "
308 "files",
309 pszGPSBabelDriverNamecxx . . $ OGRGPSBabelDataSource#pszGPSBabelDriverName.);
310 return FALSEcxx . . $ `port/cpl_port.h:1180:9`!;
311 }
312
313 /* Try without piping in */
314 argv = GetArgvcxx . . $ GetArgv(b5ee7b4a4d51078c).(bExplicitFeatures, bWaypoints, bRoutes, bTracks,
315 pszGPSBabelDriverNamecxx . . $ OGRGPSBabelDataSource#pszGPSBabelDriverName., pszFilenamecxx . . $ OGRGPSBabelDataSource#pszFilename.);
316 tmpfp = VSIFOpenL(osTmpFileNamecxx . . $ OGRGPSBabelDataSource#osTmpFileName..c_str(), "wb");
317 bRet = (CPLSpawn(argv, nullptr, tmpfp, TRUEcxx . . $ `port/cpl_port.h:1184:9`!) == 0);
318 VSIFCloseL(tmpfp);
319 tmpfp = nullptr;
320
321 CSLDestroy(argv);
322 argv = nullptr;
323 }
324 }
325 }
326
327 if (bRet)
328 {
329 poGPXDScxx . . $ OGRGPSBabelDataSource#poGPXDS. = static_cast<GDALDatasetcxx . . $ GDALDataset# *>(GDALOpenExcxx . . $ GDALOpenEx(5f20ed644b617ec0).(
330 osTmpFileNamecxx . . $ OGRGPSBabelDataSource#osTmpFileName..c_str(), GDAL_OF_VECTORcxx . . $ `gcore/gdal.h:1130:9`!, nullptr, nullptr, nullptr));
331 if (poGPXDScxx . . $ OGRGPSBabelDataSource#poGPXDS.)
332 {
333 if (bWaypoints)
334 {
335 OGRLayercxx . . $ OGRLayer# *poLayer = poGPXDScxx . . $ OGRGPSBabelDataSource#poGPXDS.->GetLayerByNamecxx . . $ GDALDataset#GetLayerByName(5ddf27af0841fa5b).("waypoints");
336 if (poLayer != nullptr && poLayer->GetFeatureCountcxx . . $ OGRLayer#GetFeatureCount(9ccc33c3ebad048d).() != 0)
337 apoLayerscxx . . $ OGRGPSBabelDataSource#apoLayers.[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).nLayerscxx . . $ OGRGPSBabelDataSource#nLayers.++] = poLayer;
338 }
339
340 if (bRoutes)
341 {
342 OGRLayercxx . . $ OGRLayer# *poLayer = poGPXDScxx . . $ OGRGPSBabelDataSource#poGPXDS.->GetLayerByNamecxx . . $ GDALDataset#GetLayerByName(5ddf27af0841fa5b).("routes");
343 if (poLayer != nullptr && poLayer->GetFeatureCountcxx . . $ OGRLayer#GetFeatureCount(9ccc33c3ebad048d).() != 0)
344 apoLayerscxx . . $ OGRGPSBabelDataSource#apoLayers.[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).nLayerscxx . . $ OGRGPSBabelDataSource#nLayers.++] = poLayer;
345 poLayer = poGPXDScxx . . $ OGRGPSBabelDataSource#poGPXDS.->GetLayerByNamecxx . . $ GDALDataset#GetLayerByName(5ddf27af0841fa5b).("route_points");
346 if (poLayer != nullptr && poLayer->GetFeatureCountcxx . . $ OGRLayer#GetFeatureCount(9ccc33c3ebad048d).() != 0)
347 apoLayerscxx . . $ OGRGPSBabelDataSource#apoLayers.[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).nLayerscxx . . $ OGRGPSBabelDataSource#nLayers.++] = poLayer;
348 }
349
350 if (bTracks)
351 {
352 OGRLayercxx . . $ OGRLayer# *poLayer = poGPXDScxx . . $ OGRGPSBabelDataSource#poGPXDS.->GetLayerByNamecxx . . $ GDALDataset#GetLayerByName(5ddf27af0841fa5b).("tracks");
353 if (poLayer != nullptr && poLayer->GetFeatureCountcxx . . $ OGRLayer#GetFeatureCount(9ccc33c3ebad048d).() != 0)
354 apoLayerscxx . . $ OGRGPSBabelDataSource#apoLayers.[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).nLayerscxx . . $ OGRGPSBabelDataSource#nLayers.++] = poLayer;
355 poLayer = poGPXDScxx . . $ OGRGPSBabelDataSource#poGPXDS.->GetLayerByNamecxx . . $ GDALDataset#GetLayerByName(5ddf27af0841fa5b).("track_points");
356 if (poLayer != nullptr && poLayer->GetFeatureCountcxx . . $ OGRLayer#GetFeatureCount(9ccc33c3ebad048d).() != 0)
357 apoLayerscxx . . $ OGRGPSBabelDataSource#apoLayers.[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).nLayerscxx . . $ OGRGPSBabelDataSource#nLayers.++] = poLayer;
358 }
359 }
360 }
361
362 return nLayerscxx . . $ OGRGPSBabelDataSource#nLayers. > 0;
363 }
364
365 /************************************************************************/
366 /* GetLayer() */
367 /************************************************************************/
368
369 const OGRLayercxx . . $ OGRLayer# *OGRGPSBabelDataSourcecxx . . $ OGRGPSBabelDataSource#::GetLayercxx . . $ OGRGPSBabelDataSource#GetLayer(3ead67c79a3185c4).(int iLayer) const
370
371 {
372 if (iLayer < 0 || iLayer >= nLayerscxx . . $ OGRGPSBabelDataSource#nLayers.)
373 return nullptr;
374
375 return apoLayerscxx . . $ OGRGPSBabelDataSource#apoLayers.[cxx . . $ std/array#`operator[]`(c65000e556194d6f).iLayer];
376 }