| 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Project: OpenGIS Simple Features Reference Implementation |
| 4 | * Purpose: Implements OGRGPSBabelWriteDataSource 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 <cstring> |
| 14 | #include "cpl_conv.h"cxx . . $ `<file>/port/cpl_conv.h`/ |
| 15 | #include "cpl_string.h"cxx . . $ `<file>/port/cpl_string.h`/ |
| 16 | #include "cpl_error.h"cxx . . $ `<file>/port/cpl_error.h`/ |
| 17 | #include "cpl_spawn.h"cxx . . $ `<file>/port/cpl_spawn.h`/ |
| 18 | |
| 19 | #include "ogr_gpsbabel.h"cxx . . $ `<file>/ogr/ogrsf_frmts/gpsbabel/ogr_gpsbabel.h`/ |
| 20 | |
| 21 | /************************************************************************/ |
| 22 | /* OGRGPSBabelWriteDataSource() */ |
| 23 | /************************************************************************/ |
| 24 | |
| 25 | OGRGPSBabelWriteDataSourcecxx . . $ OGRGPSBabelWriteDataSource#::OGRGPSBabelWriteDataSourcecxx . . $ OGRGPSBabelWriteDataSource#OGRGPSBabelWriteDataSource(49f6e7a06ebc5aa8).() = default; |
| 26 | |
| 27 | /************************************************************************/ |
| 28 | /* ~OGRGPSBabelWriteDataSource() */ |
| 29 | /************************************************************************/ |
| 30 | |
| 31 | OGRGPSBabelWriteDataSourcecxx . . $ OGRGPSBabelWriteDataSource#::~cxx . . $ OGRGPSBabelWriteDataSource#`~OGRGPSBabelWriteDataSource`(ced63f7c635d850d).OGRGPSBabelWriteDataSourcecxx . . $ OGRGPSBabelWriteDataSource#() |
| 32 | |
| 33 | { |
| 34 | if (poGPXDScxx . . $ OGRGPSBabelWriteDataSource#poGPXDS.) |
| 35 | GDALClosecxx . . $ GDALClose(3d5aeed3fac2555e).(poGPXDScxx . . $ OGRGPSBabelWriteDataSource#poGPXDS.); |
| 36 | |
| 37 | Convertcxx . . $ OGRGPSBabelWriteDataSource#Convert(c9857a73d68e59ba).(); |
| 38 | |
| 39 | CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(pszGPSBabelDriverNamecxx . . $ OGRGPSBabelWriteDataSource#pszGPSBabelDriverName.); |
| 40 | CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(pszFilenamecxx . . $ OGRGPSBabelWriteDataSource#pszFilename.); |
| 41 | } |
| 42 | |
| 43 | /************************************************************************/ |
| 44 | /* Convert() */ |
| 45 | /************************************************************************/ |
| 46 | |
| 47 | bool OGRGPSBabelWriteDataSourcecxx . . $ OGRGPSBabelWriteDataSource#::Convertcxx . . $ OGRGPSBabelWriteDataSource#Convert(c9857a73d68e59ba).() |
| 48 | { |
| 49 | int nRet = -1; |
| 50 | if (!osTmpFileNamecxx . . $ OGRGPSBabelWriteDataSource#osTmpFileName..empty() && pszFilenamecxx . . $ OGRGPSBabelWriteDataSource#pszFilename. != nullptr && |
| 51 | pszGPSBabelDriverNamecxx . . $ OGRGPSBabelWriteDataSource#pszGPSBabelDriverName. != nullptr) |
| 52 | { |
| 53 | if (OGRGPSBabelDataSourcecxx . . $ OGRGPSBabelDataSource#::IsSpecialFilecxx . . $ OGRGPSBabelDataSource#IsSpecialFile(c5f7da30cbc0f4e9).(pszFilenamecxx . . $ OGRGPSBabelWriteDataSource#pszFilename.)) |
| 54 | { |
| 55 | /* Special file : don't try to open it */ |
| 56 | VSILFILE *tmpfp = VSIFOpenL(osTmpFileNamecxx . . $ OGRGPSBabelWriteDataSource#osTmpFileName..c_str(), "rb"); |
| 57 | if (tmpfp) |
| 58 | { |
| 59 | const char *const argv[] = {"gpsbabel", |
| 60 | "-i", |
| 61 | "gpx", |
| 62 | "-f", |
| 63 | "-", |
| 64 | "-o", |
| 65 | pszGPSBabelDriverNamecxx . . $ OGRGPSBabelWriteDataSource#pszGPSBabelDriverName., |
| 66 | "-F", |
| 67 | pszFilenamecxx . . $ OGRGPSBabelWriteDataSource#pszFilename., |
| 68 | nullptr}; |
| 69 | nRet = CPLSpawn(argv, tmpfp, nullptr, TRUEcxx . . $ `port/cpl_port.h:1184:9`!); |
| 70 | |
| 71 | VSIFCloseL(tmpfp); |
| 72 | tmpfp = nullptr; |
| 73 | } |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | VSILFILE *fp = VSIFOpenL(pszFilenamecxx . . $ OGRGPSBabelWriteDataSource#pszFilename., "wb"); |
| 78 | if (fp == nullptr) |
| 79 | { |
| 80 | CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, "Cannot open file %s", |
| 81 | pszFilenamecxx . . $ OGRGPSBabelWriteDataSource#pszFilename.); |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | VSILFILE *tmpfp = VSIFOpenL(osTmpFileNamecxx . . $ OGRGPSBabelWriteDataSource#osTmpFileName..c_str(), "rb"); |
| 86 | if (tmpfp) |
| 87 | { |
| 88 | const char *const argv[] = {"gpsbabel", |
| 89 | "-i", |
| 90 | "gpx", |
| 91 | "-f", |
| 92 | "-", |
| 93 | "-o", |
| 94 | pszGPSBabelDriverNamecxx . . $ OGRGPSBabelWriteDataSource#pszGPSBabelDriverName., |
| 95 | "-F", |
| 96 | "-", |
| 97 | nullptr}; |
| 98 | nRet = CPLSpawn(argv, tmpfp, fp, TRUEcxx . . $ `port/cpl_port.h:1184:9`!); |
| 99 | |
| 100 | VSIFCloseL(tmpfp); |
| 101 | tmpfp = nullptr; |
| 102 | } |
| 103 | |
| 104 | VSIFCloseL(fp); |
| 105 | fp = nullptr; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | VSIUnlink(osTmpFileNamecxx . . $ OGRGPSBabelWriteDataSource#osTmpFileName..c_str()); |
| 110 | osTmpFileNamecxx . . $ OGRGPSBabelWriteDataSource#osTmpFileName. = ""; |
| 111 | } |
| 112 | |
| 113 | return nRet == 0; |
| 114 | } |
| 115 | |
| 116 | /************************************************************************/ |
| 117 | /* Create() */ |
| 118 | /************************************************************************/ |
| 119 | |
| 120 | int OGRGPSBabelWriteDataSourcecxx . . $ OGRGPSBabelWriteDataSource#::Createcxx . . $ OGRGPSBabelWriteDataSource#Create(5a7b06e3ce74da13).(const char *pszNameIn, |
| 121 | CSLConstListcxx . . $ CSLConstList# papszOptions) |
| 122 | { |
| 123 | GDALDrivercxx . . $ GDALDriver# *poGPXDriver = |
| 124 | OGRSFDriverRegistrarcxx . . $ OGRSFDriverRegistrar#::GetRegistrarcxx . . $ OGRSFDriverRegistrar#GetRegistrar(4e9952cdeba0aa35).()->GetDriverByNamecxx . . $ OGRSFDriverRegistrar#GetDriverByName(715d9ac900aae6d5).("GPX"); |
| 125 | if (poGPXDriver == nullptr) |
| 126 | { |
| 127 | CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, |
| 128 | "GPX driver is necessary for GPSBabel write support"); |
| 129 | return FALSEcxx . . $ `port/cpl_port.h:1180:9`!; |
| 130 | } |
| 131 | |
| 132 | if (!STARTS_WITH_CIcxx . . $ `port/cpl_port.h:543:9`!(pszNameIn, "GPSBABEL:")) |
| 133 | { |
| 134 | const char *pszOptionGPSBabelDriverName = |
| 135 | CSLFetchNameValue(papszOptions, "GPSBABEL_DRIVER"); |
| 136 | if (pszOptionGPSBabelDriverName != nullptr) |
| 137 | pszGPSBabelDriverNamecxx . . $ OGRGPSBabelWriteDataSource#pszGPSBabelDriverName. = CPLStrdup(pszOptionGPSBabelDriverName); |
| 138 | else |
| 139 | { |
| 140 | CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, |
| 141 | "GPSBABEL_DRIVER dataset creation option expected"); |
| 142 | return FALSEcxx . . $ `port/cpl_port.h:1180:9`!; |
| 143 | } |
| 144 | |
| 145 | pszFilenamecxx . . $ OGRGPSBabelWriteDataSource#pszFilename. = CPLStrdup(pszNameIn); |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | const char *pszSep = strchr(pszNameIn + 9, ':'); |
| 150 | if (pszSep == nullptr) |
| 151 | { |
| 152 | CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, |
| 153 | "Wrong syntax. Expected GPSBabel:driver_name[,options]*:" |
| 154 | "file_name"); |
| 155 | return FALSEcxx . . $ `port/cpl_port.h:1180:9`!; |
| 156 | } |
| 157 | |
| 158 | pszGPSBabelDriverNamecxx . . $ OGRGPSBabelWriteDataSource#pszGPSBabelDriverName. = CPLStrdup(pszNameIn + 9); |
| 159 | char *nextColon = strchr(pszGPSBabelDriverNamecxx . . $ OGRGPSBabelWriteDataSource#pszGPSBabelDriverName., ':'); |
| 160 | if (nextColon) |
| 161 | *nextColon = 0; |
| 162 | |
| 163 | pszFilenamecxx . . $ OGRGPSBabelWriteDataSource#pszFilename. = CPLStrdup(pszSep + 1); |
| 164 | } |
| 165 | |
| 166 | /* A bit of validation to avoid command line injection */ |
| 167 | if (!OGRGPSBabelDataSourcecxx . . $ OGRGPSBabelDataSource#::IsValidDriverNamecxx . . $ OGRGPSBabelDataSource#IsValidDriverName(c5f7da30cbc0f4e9).(pszGPSBabelDriverNamecxx . . $ OGRGPSBabelWriteDataSource#pszGPSBabelDriverName.)) |
| 168 | return FALSEcxx . . $ `port/cpl_port.h:1180:9`!; |
| 169 | |
| 170 | const char *pszOptionUseTempFile = |
| 171 | CSLFetchNameValue(papszOptions, "USE_TEMPFILE"); |
| 172 | if (pszOptionUseTempFile == nullptr) |
| 173 | pszOptionUseTempFile = CPLGetConfigOption("USE_TEMPFILE", nullptr); |
| 174 | if (pszOptionUseTempFile && CPLTestBool(pszOptionUseTempFile)) |
| 175 | osTmpFileNamecxx . . $ OGRGPSBabelWriteDataSource#osTmpFileName. = CPLGenerateTempFilenameSafe(nullptr); |
| 176 | else |
| 177 | osTmpFileNamecxx . . $ OGRGPSBabelWriteDataSource#osTmpFileName. = VSIMemGenerateHiddenFilename("gpsbabel"); |
| 178 | |
| 179 | poGPXDScxx . . $ OGRGPSBabelWriteDataSource#poGPXDS. = poGPXDriver->Createcxx . . $ GDALDriver#Create(2a509bdc7a404bec).(osTmpFileNamecxx . . $ OGRGPSBabelWriteDataSource#osTmpFileName..c_str(), 0, 0, 0, GDT_Unknown, |
| 180 | papszOptions); |
| 181 | if (poGPXDScxx . . $ OGRGPSBabelWriteDataSource#poGPXDS. == nullptr) |
| 182 | return FALSEcxx . . $ `port/cpl_port.h:1180:9`!; |
| 183 | |
| 184 | return TRUEcxx . . $ `port/cpl_port.h:1184:9`!; |
| 185 | } |
| 186 | |
| 187 | /************************************************************************/ |
| 188 | /* ICreateLayer() */ |
| 189 | /************************************************************************/ |
| 190 | |
| 191 | OGRLayercxx . . $ OGRLayer# *OGRGPSBabelWriteDataSourcecxx . . $ OGRGPSBabelWriteDataSource#::ICreateLayercxx . . $ OGRGPSBabelWriteDataSource#ICreateLayer(d0342318947b6f88).( |
| 192 | const char *pszLayerName, const OGRGeomFieldDefncxx . . $ OGRGeomFieldDefn# *poGeomFieldDefn, |
| 193 | CSLConstListcxx . . $ CSLConstList# papszOptions) |
| 194 | { |
| 195 | if (poGPXDScxx . . $ OGRGPSBabelWriteDataSource#poGPXDS.) |
| 196 | return poGPXDScxx . . $ OGRGPSBabelWriteDataSource#poGPXDS.->CreateLayercxx . . $ GDALDataset#CreateLayer(d0342318947b6f88).(pszLayerName, poGeomFieldDefn, |
| 197 | papszOptions); |
| 198 | return nullptr; |
| 199 | } |
| 200 | |
| 201 | /************************************************************************/ |
| 202 | /* TestCapability() */ |
| 203 | /************************************************************************/ |
| 204 | |
| 205 | int OGRGPSBabelWriteDataSourcecxx . . $ OGRGPSBabelWriteDataSource#::TestCapabilitycxx . . $ OGRGPSBabelWriteDataSource#TestCapability(78f035161ffa9fe2).(const char *pszCap) const |
| 206 | |
| 207 | { |
| 208 | if (EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszCap, ODsCCreateLayercxx . . $ `ogr/ogr_core.h:1040:9`!)) |
| 209 | return TRUEcxx . . $ `port/cpl_port.h:1184:9`!; |
| 210 | |
| 211 | return FALSEcxx . . $ `port/cpl_port.h:1180:9`!; |
| 212 | } |
| 213 | |
| 214 | /************************************************************************/ |
| 215 | /* GetLayer() */ |
| 216 | /************************************************************************/ |
| 217 | |
| 218 | const OGRLayercxx . . $ OGRLayer# *OGRGPSBabelWriteDataSourcecxx . . $ OGRGPSBabelWriteDataSource#::GetLayercxx . . $ OGRGPSBabelWriteDataSource#GetLayer(3ead67c79a3185c4).(int iLayer) const |
| 219 | |
| 220 | { |
| 221 | if (poGPXDScxx . . $ OGRGPSBabelWriteDataSource#poGPXDS.) |
| 222 | return poGPXDScxx . . $ OGRGPSBabelWriteDataSource#poGPXDS.->GetLayercxx . . $ GDALDataset#GetLayer(7a9909270c12ab7d).(iLayer); |
| 223 | |
| 224 | return nullptr; |
| 225 | } |
| 226 | |
| 227 | /************************************************************************/ |
| 228 | /* GetLayerCount() */ |
| 229 | /************************************************************************/ |
| 230 | |
| 231 | int OGRGPSBabelWriteDataSourcecxx . . $ OGRGPSBabelWriteDataSource#::GetLayerCountcxx . . $ OGRGPSBabelWriteDataSource#GetLayerCount(455f465bc33b4cdf).() const |
| 232 | |
| 233 | { |
| 234 | if (poGPXDScxx . . $ OGRGPSBabelWriteDataSource#poGPXDS.) |
| 235 | return poGPXDScxx . . $ OGRGPSBabelWriteDataSource#poGPXDS.->GetLayerCountcxx . . $ GDALDataset#GetLayerCount(455f465bc33b4cdf).(); |
| 236 | |
| 237 | return 0; |
| 238 | } |