From 264adacba1df1db60be6754c2ec486aaaf4cf2b0 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Fri, 17 Mar 2023 08:21:47 +0100 Subject: [PATCH] cleanup: move formatting of gas type to string-format.cpp Since the only caller was C++ code, this can be done in C++ code, which removes memory-management headaches. Signed-off-by: Berthold Stoeger --- core/divelist.c | 30 ------------------------------ core/divelist.h | 1 - core/string-format.cpp | 24 ++++++++++++++++++++++++ core/string-format.h | 1 + qt-models/divetripmodel.cpp | 7 +------ 5 files changed, 26 insertions(+), 37 deletions(-) diff --git a/core/divelist.c b/core/divelist.c index 16fe685e7..811a80f00 100644 --- a/core/divelist.c +++ b/core/divelist.c @@ -625,36 +625,6 @@ void update_cylinder_related_info(struct dive *dive) } } -#define MAX_GAS_STRING 80 - -/* callers needs to free the string */ -char *get_dive_gas_string(const struct dive *dive) -{ - int o2, he, o2max; - char *buffer = malloc(MAX_GAS_STRING); - - if (buffer) { - get_dive_gas(dive, &o2, &he, &o2max); - o2 = (o2 + 5) / 10; - he = (he + 5) / 10; - o2max = (o2max + 5) / 10; - - if (he) - if (o2 == o2max) - snprintf(buffer, MAX_GAS_STRING, "%d/%d", o2, he); - else - snprintf(buffer, MAX_GAS_STRING, "%d/%d…%d%%", o2, he, o2max); - else if (o2) - if (o2 == o2max) - snprintf(buffer, MAX_GAS_STRING, "%d%%", o2); - else - snprintf(buffer, MAX_GAS_STRING, "%d…%d%%", o2, o2max); - else - strcpy(buffer, translate("gettextFromC", "air")); - } - return buffer; -} - /* Like strcmp(), but don't crash on null-pointers */ static int safe_strcmp(const char *s1, const char *s2) { diff --git a/core/divelist.h b/core/divelist.h index e65e94dfc..2876407ab 100644 --- a/core/divelist.h +++ b/core/divelist.h @@ -44,7 +44,6 @@ extern void process_imported_dives(struct dive_table *import_table, struct trip_ struct dive_table *dives_to_add, struct dive_table *dives_to_remove, struct trip_table *trips_to_add, struct dive_site_table *sites_to_add, struct device_table *devices_to_add); -extern char *get_dive_gas_string(const struct dive *dive); extern int dive_table_get_insertion_index(struct dive_table *table, struct dive *dive); extern void add_to_dive_table(struct dive_table *table, int idx, struct dive *dive); diff --git a/core/string-format.cpp b/core/string-format.cpp index 8c8264cc5..b9ccc02dd 100644 --- a/core/string-format.cpp +++ b/core/string-format.cpp @@ -1,6 +1,7 @@ #include "string-format.h" #include "dive.h" #include "divesite.h" +#include "format.h" #include "qthelper.h" #include "subsurface-string.h" #include "trip.h" @@ -261,6 +262,29 @@ QString formatDiveDateTime(const dive *d) localTime.time().toString(prefs.time_format)); } +QString formatDiveGasString(const dive *d) +{ + int o2, he, o2max; + get_dive_gas(d, &o2, &he, &o2max); + o2 = (o2 + 5) / 10; + he = (he + 5) / 10; + o2max = (o2max + 5) / 10; + + if (he) { + if (o2 == o2max) + return qasprintf_loc("%d/%d", o2, he); + else + return qasprintf_loc("%d/%d…%d%%", o2, he, o2max); + } else if (o2) { + if (o2 == o2max) + return qasprintf_loc("%d%%", o2); + else + return qasprintf_loc("%d…%d%%", o2, o2max); + } else { + return gettextFromC::tr("air"); + } +} + QString formatDayOfWeek(int day) { // I can't wrap my head around the fact that Sunday is the diff --git a/core/string-format.h b/core/string-format.h index 51ae70df0..404e7f9cc 100644 --- a/core/string-format.h +++ b/core/string-format.h @@ -26,6 +26,7 @@ QString formatDiveGPS(const dive *d); QString formatDiveDate(const dive *d); QString formatDiveTime(const dive *d); QString formatDiveDateTime(const dive *d); +QString formatDiveGasString(const dive *d); QString formatDayOfWeek(int day); QString formatMinutes(int seconds); QString formatTripTitle(const dive_trip *trip); diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp index 1613087c4..965575484 100644 --- a/qt-models/divetripmodel.cpp +++ b/qt-models/divetripmodel.cpp @@ -355,12 +355,7 @@ QVariant DiveTripModelBase::diveData(const struct dive *d, int column, int role) case LOCATION: return QString(get_dive_location(d)); case GAS: - { - char *gas_string = get_dive_gas_string(d); - QString ret(gas_string); - free(gas_string); - return ret; - } + return formatDiveGasString(d); case NOTES: return QString(d->notes); }