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 <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2023-03-17 08:21:47 +01:00 committed by Dirk Hohndel
parent 468c1d8d61
commit 264adacba1
5 changed files with 26 additions and 37 deletions

View file

@ -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)
{

View file

@ -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);

View file

@ -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

View file

@ -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);

View file

@ -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);
}