summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-03-25 09:05:47 +0100
committerGravatar Robert C. Helling <helling@atdotde.de>2019-03-27 13:58:15 +0100
commit40a3e562b01de2827fa6c2c5fd9d46631f91dc36 (patch)
tree194fa05c48b087220cabd206125cc82eaf439d0d
parent04593e8ec4bac2606dec54605c72a6a49cc83f9b (diff)
downloadsubsurface-40a3e562b01de2827fa6c2c5fd9d46631f91dc36.tar.gz
Cleanup: provide printGPSCoords in C and C++ versions
printGPSCoords() returned a newly allocated C-style string. Most callers simply made a QString out of it and freed the C-style string. This is paradoxical, as printGPSCoords internally works with QStrings and converts them to C-style on return. Therefore, let printGPSCoords() return a QString and create a printGPSCoordsC() wrapper for the two C-callers. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
-rw-r--r--core/gpslocation.cpp4
-rw-r--r--core/load-git.c2
-rw-r--r--core/parse-xml.c2
-rw-r--r--core/qthelper.cpp11
-rw-r--r--core/qthelper.h3
-rw-r--r--core/subsurface-qt/DiveObjectHelper.cpp5
-rw-r--r--desktop-widgets/locationinformation.cpp13
-rw-r--r--desktop-widgets/modeldelegates.cpp7
-rw-r--r--desktop-widgets/tab-widgets/maintab.cpp7
-rw-r--r--map-widget/qmlmapwidgethelper.cpp4
-rw-r--r--mobile-widgets/qmlmanager.cpp5
11 files changed, 24 insertions, 39 deletions
diff --git a/core/gpslocation.cpp b/core/gpslocation.cpp
index d0bbcdc8f..e0b176f58 100644
--- a/core/gpslocation.cpp
+++ b/core/gpslocation.cpp
@@ -137,9 +137,7 @@ QString GpsLocation::currentPosition()
if (delta < 300) {
// we can simply use the last position that we tracked
gpsTracker gt = m_trackers.last();
- char *gps = printGPSCoords(&gt.location);
- QString gpsString = gps;
- free(gps);
+ QString gpsString = printGPSCoords(&gt.location);
qDebug() << "returning last position" << gpsString;
return gpsString;
} else {
diff --git a/core/load-git.c b/core/load-git.c
index 8417fc2e4..28b33f036 100644
--- a/core/load-git.c
+++ b/core/load-git.c
@@ -164,7 +164,7 @@ static void parse_dive_gps(char *line, struct membuffer *str, void *_dive)
dive->dive_site = ds;
} else {
if (dive_site_has_gps_location(ds) && !same_location(&ds->location, &location)) {
- char *coords = printGPSCoords(&location);
+ char *coords = printGPSCoordsC(&location);
// we have a dive site that already has GPS coordinates
ds->notes = add_to_string(ds->notes, translate("gettextFromC", "multiple GPS locations for this dive site; also %s\n"), coords);
free(coords);
diff --git a/core/parse-xml.c b/core/parse-xml.c
index c118c89cc..3081e1351 100644
--- a/core/parse-xml.c
+++ b/core/parse-xml.c
@@ -1201,7 +1201,7 @@ static void gps_in_dive(char *buffer, struct dive *dive, struct parser_state *st
fprintf(stderr, "dive site uuid in dive, but gps location (%10.6f/%10.6f) different from dive location (%10.6f/%10.6f)\n",
ds->location.lat.udeg / 1000000.0, ds->location.lon.udeg / 1000000.0,
location.lat.udeg / 1000000.0, location.lon.udeg / 1000000.0);
- char *coords = printGPSCoords(&location);
+ char *coords = printGPSCoordsC(&location);
ds->notes = add_to_string(ds->notes, translate("gettextFromC", "multiple GPS locations for this dive site; also %s\n"), coords);
free(coords);
} else {
diff --git a/core/qthelper.cpp b/core/qthelper.cpp
index 26a8a68f9..1a85d35a7 100644
--- a/core/qthelper.cpp
+++ b/core/qthelper.cpp
@@ -72,7 +72,7 @@ QString distance_string(int distanceInMeters)
return str;
}
-extern "C" char *printGPSCoords(const location_t *location)
+QString printGPSCoords(const location_t *location)
{
int lat = location->lat.udeg;
int lon = location->lon.udeg;
@@ -82,7 +82,7 @@ extern "C" char *printGPSCoords(const location_t *location)
QString lath, lonh, result;
if (!has_location(location))
- return strdup("");
+ return QString();
if (prefs.coordinates_traditional) {
lath = lat >= 0 ? gettextFromC::tr("N") : gettextFromC::tr("S");
@@ -101,7 +101,12 @@ extern "C" char *printGPSCoords(const location_t *location)
} else {
result.sprintf("%f %f", (double) lat / 1000000.0, (double) lon / 1000000.0);
}
- return copy_qstring(result);
+ return result;
+}
+
+extern "C" char *printGPSCoordsC(const location_t *location)
+{
+ return copy_qstring(printGPSCoords(location));
}
/**
diff --git a/core/qthelper.h b/core/qthelper.h
index ed09d70fb..2cc7dfec4 100644
--- a/core/qthelper.h
+++ b/core/qthelper.h
@@ -82,6 +82,7 @@ QString uiLanguage(QLocale *callerLoc);
QLocale getLocale();
void selectedDivesGasUsed(QVector<QPair<QString, int> > &gasUsed);
QString getUserAgent();
+QString printGPSCoords(const location_t *loc);
#if defined __APPLE__
#define TITLE_OR_TEXT(_t, _m) "", _t + "\n" + _m
@@ -123,7 +124,7 @@ void moveInVector(Vector &v, int rangeBegin, int rangeEnd, int destination)
extern "C" {
#endif
-char *printGPSCoords(const location_t *loc);
+char *printGPSCoordsC(const location_t *loc);
bool in_planner();
bool getProxyString(char **buffer);
bool canReachCloudServer();
diff --git a/core/subsurface-qt/DiveObjectHelper.cpp b/core/subsurface-qt/DiveObjectHelper.cpp
index 921d55bea..e126298ac 100644
--- a/core/subsurface-qt/DiveObjectHelper.cpp
+++ b/core/subsurface-qt/DiveObjectHelper.cpp
@@ -114,10 +114,7 @@ QString DiveObjectHelper::gps() const
struct dive_site *ds = m_dive->dive_site;
if (!ds)
return QString();
- char *gps = printGPSCoords(&ds->location);
- QString res = gps;
- free(gps);
- return res;
+ return printGPSCoords(&ds->location);
}
QString DiveObjectHelper::gps_decimal() const
diff --git a/desktop-widgets/locationinformation.cpp b/desktop-widgets/locationinformation.cpp
index f4da411af..af3fa513f 100644
--- a/desktop-widgets/locationinformation.cpp
+++ b/desktop-widgets/locationinformation.cpp
@@ -113,13 +113,10 @@ void LocationInformationWidget::updateLabels()
ui.diveSiteNotes->setPlainText(diveSite->notes);
else
ui.diveSiteNotes->clear();
- if (has_location(&diveSite->location)) {
- char *coords = printGPSCoords(&diveSite->location);
- ui.diveSiteCoordinates->setText(coords);
- free(coords);
- } else {
+ if (has_location(&diveSite->location))
+ ui.diveSiteCoordinates->setText(printGPSCoords(&diveSite->location));
+ else
ui.diveSiteCoordinates->clear();
- }
ui.locationTags->setText(constructLocationTags(&taxonomy, false));
}
@@ -138,10 +135,8 @@ void LocationInformationWidget::updateGpsCoordinates(const location_t &location)
{
QString oldText = ui.diveSiteCoordinates->text();
- char *coords = printGPSCoords(&location);
- ui.diveSiteCoordinates->setText(coords);
+ ui.diveSiteCoordinates->setText(printGPSCoords(&location));
enableLocationButtons(has_location(&location));
- free(coords);
if (oldText != ui.diveSiteCoordinates->text())
markChangedWidget(ui.diveSiteCoordinates);
}
diff --git a/desktop-widgets/modeldelegates.cpp b/desktop-widgets/modeldelegates.cpp
index c6405a433..fd6bb2e47 100644
--- a/desktop-widgets/modeldelegates.cpp
+++ b/desktop-widgets/modeldelegates.cpp
@@ -483,11 +483,8 @@ void LocationFilterDelegate::paint(QPainter *painter, const QStyleOptionViewItem
bottomText += QString(ds->taxonomy.category[idx].value);
}
- if (bottomText.isEmpty()) {
- char *gpsCoords = printGPSCoords(&ds->location);
- bottomText = QString(gpsCoords);
- free(gpsCoords);
- }
+ if (bottomText.isEmpty())
+ bottomText = printGPSCoords(&ds->location);
if (dive_site_has_gps_location(ds) && currentDiveSiteHasGPS) {
// so we are showing a completion and both the current dive site and the completion
diff --git a/desktop-widgets/tab-widgets/maintab.cpp b/desktop-widgets/tab-widgets/maintab.cpp
index a2ff15368..98b8904c2 100644
--- a/desktop-widgets/tab-widgets/maintab.cpp
+++ b/desktop-widgets/tab-widgets/maintab.cpp
@@ -410,11 +410,8 @@ void MainTab::updateDiveInfo(bool clear)
ui.location->setCurrentDiveSite(ds);
ui.locationTags->setText(constructLocationTags(&ds->taxonomy, true));
- if (ui.locationTags->text().isEmpty() && has_location(&ds->location)) {
- char *coords = printGPSCoords(&ds->location);
- ui.locationTags->setText(coords);
- free(coords);
- }
+ if (ui.locationTags->text().isEmpty() && has_location(&ds->location))
+ ui.locationTags->setText(printGPSCoords(&ds->location));
} else {
ui.location->clear();
ui.locationTags->clear();
diff --git a/map-widget/qmlmapwidgethelper.cpp b/map-widget/qmlmapwidgethelper.cpp
index 61b8f0abe..b13c047e7 100644
--- a/map-widget/qmlmapwidgethelper.cpp
+++ b/map-widget/qmlmapwidgethelper.cpp
@@ -246,10 +246,8 @@ void MapWidgetHelper::copyToClipboardCoordinates(QGeoCoordinate coord, bool form
bool savep = prefs.coordinates_traditional;
prefs.coordinates_traditional = formatTraditional;
location_t location = mk_location(coord);
- char *coordinates = printGPSCoords(&location);
- QApplication::clipboard()->setText(QString(coordinates), QClipboard::Clipboard);
+ QApplication::clipboard()->setText(printGPSCoords(&location), QClipboard::Clipboard);
- free(coordinates);
prefs.coordinates_traditional = savep;
}
diff --git a/mobile-widgets/qmlmanager.cpp b/mobile-widgets/qmlmanager.cpp
index d9da5e562..1e2b6e132 100644
--- a/mobile-widgets/qmlmanager.cpp
+++ b/mobile-widgets/qmlmanager.cpp
@@ -1604,10 +1604,7 @@ QString QMLManager::getGpsFromSiteName(const QString& siteName)
ds = get_dive_site_by_name(qPrintable(siteName));
if (!ds)
return QString();
- char *gps = printGPSCoords(&ds->location);
- QString res = gps;
- free(gps);
- return res;
+ return printGPSCoords(&ds->location);
}
void QMLManager::setNotificationText(QString text)