// SPDX-License-Identifier: GPL-2.0 #include "core/parse-gpx.h" #include "core/subsurface-time.h" #include "core/namecmp.h" #include #include // Find the coordinates at the time specified in coords.start_dive // by searching the gpx file "fileName". Here is a typical trkpt element in GPX: // -53.7 int getCoordsFromGPXFile(struct dive_coords *coords, const QString &fileName) { struct tm tm1; time_t trkpt_time = 0; time_t divetime; int64_t time_offset = coords->settingsDiff_offset + coords->timeZone_offset; double lon = 0, lat = 0; int line = 0; bool first_line = true; bool found = false; bool trkpt_found = false; divetime = coords->start_dive; QFile gpxFile; gpxFile.setFileName(fileName); if (!gpxFile.open(QIODevice::ReadOnly | QIODevice::Text)) { QByteArray local8bitBAString1 = fileName.toLocal8Bit(); char *fname = local8bitBAString1.data(); // convert QString to a C string fileName fprintf(stderr, "GPS file open error: file name = %s\n", fname); return 1; } #ifdef GPSDEBUG struct tm time; // decode the time of start of dive: utc_mkdate(divetime, &time); int dyr,dmon,dday,dhr,dmin; dyr = time.tm_year; dmon = time.tm_mon; dday = time.tm_mday; dhr = time.tm_hour; dmin = time.tm_min; #endif QXmlStreamReader gpxReader(&gpxFile); while (!gpxReader.atEnd()) { gpxReader.readNext(); if (gpxReader.isStartElement()) { if (nameCmp(gpxReader, "trkpt") == 0) { trkpt_found = true; line++; for (const QXmlStreamAttribute &attr: gpxReader.attributes()) { if (attr.name().toString() == QLatin1String("lat")) lat = attr.value().toString().toDouble(); else if (attr.name().toString() == QLatin1String("lon")) lon = attr.value().toString().toDouble(); } } if (nameCmp(gpxReader, "time") == 0 && trkpt_found) { // Ignore the