// SPDX-License-Identifier: GPL-2.0 #include "core/parse-gpx.h" #include "core/dive.h" #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, 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 (gpxReader.name() == "trkpt") { trkpt_found = true; line++; foreach (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 (gpxReader.name() == "time" && trkpt_found) { // Ignore the