core: convert divesite strings to std::string

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-05-04 17:18:08 +02:00
parent 3394ce7931
commit 160f06db8d
38 changed files with 181 additions and 222 deletions

View file

@ -174,9 +174,9 @@ void export_TeX(const char *filename, bool selected_only, bool plain, ExportCall
put_format(&buf, "\\def\\%shour{%02u}\n", ssrf, tm.tm_hour); put_format(&buf, "\\def\\%shour{%02u}\n", ssrf, tm.tm_hour);
put_format(&buf, "\\def\\%sminute{%02u}\n", ssrf, tm.tm_min); put_format(&buf, "\\def\\%sminute{%02u}\n", ssrf, tm.tm_min);
put_format(&buf, "\\def\\%snumber{%d}\n", ssrf, dive->number); put_format(&buf, "\\def\\%snumber{%d}\n", ssrf, dive->number);
put_format(&buf, "\\def\\%splace{%s}\n", ssrf, site ? site->name : ""); put_format(&buf, "\\def\\%splace{%s}\n", ssrf, site ? site->name.c_str() : "");
put_format(&buf, "\\def\\%sspot{}\n", ssrf); put_format(&buf, "\\def\\%sspot{}\n", ssrf);
put_format(&buf, "\\def\\%ssitename{%s}\n", ssrf, site ? site->name : ""); put_format(&buf, "\\def\\%ssitename{%s}\n", ssrf, site ? site->name.c_str() : "");
site ? put_format(&buf, "\\def\\%sgpslat{%f}\n", ssrf, site->location.lat.udeg / 1000000.0) : put_format(&buf, "\\def\\%sgpslat{}\n", ssrf); site ? put_format(&buf, "\\def\\%sgpslat{%f}\n", ssrf, site->location.lat.udeg / 1000000.0) : put_format(&buf, "\\def\\%sgpslat{}\n", ssrf);
site ? put_format(&buf, "\\def\\%sgpslon{%f}\n", ssrf, site->location.lon.udeg / 1000000.0) : put_format(&buf, "\\def\\gpslon{}\n"); site ? put_format(&buf, "\\def\\%sgpslon{%f}\n", ssrf, site->location.lon.udeg / 1000000.0) : put_format(&buf, "\\def\\gpslon{}\n");
put_format(&buf, "\\def\\%scomputer{%s}\n", ssrf, dive->dc.model); put_format(&buf, "\\def\\%scomputer{%s}\n", ssrf, dive->dc.model);
@ -251,7 +251,7 @@ void export_TeX(const char *filename, bool selected_only, bool plain, ExportCall
// Legacy fields // Legacy fields
put_format(&buf, "\\def\\%sspot{}\n", ssrf); put_format(&buf, "\\def\\%sspot{}\n", ssrf);
put_format(&buf, "\\def\\%sentrance{}\n", ssrf); put_format(&buf, "\\def\\%sentrance{}\n", ssrf);
put_format(&buf, "\\def\\%splace{%s}\n", ssrf, site ? site->name : ""); put_format(&buf, "\\def\\%splace{%s}\n", ssrf, site ? site->name.c_str() : "");
dive->maxdepth.mm ? put_format(&buf, "\\def\\%sdepth{%.1f\\%sdepthunit}\n", ssrf, get_depth_units(dive->maxdepth.mm, NULL, &unit), ssrf) : put_format(&buf, "\\def\\%sdepth{}\n", ssrf); dive->maxdepth.mm ? put_format(&buf, "\\def\\%sdepth{%.1f\\%sdepthunit}\n", ssrf, get_depth_units(dive->maxdepth.mm, NULL, &unit), ssrf) : put_format(&buf, "\\def\\%sdepth{}\n", ssrf);
put_format(&buf, "\\%spage\n", ssrf); put_format(&buf, "\\%spage\n", ssrf);

View file

@ -76,7 +76,7 @@ AddDiveSite::AddDiveSite(const QString &name)
{ {
setText(Command::Base::tr("add dive site")); setText(Command::Base::tr("add dive site"));
sitesToAdd.push_back(std::make_unique<dive_site>()); sitesToAdd.push_back(std::make_unique<dive_site>());
sitesToAdd.back()->name = copy_qstring(name); sitesToAdd.back()->name = name.toStdString();
} }
bool AddDiveSite::workToBeDone() bool AddDiveSite::workToBeDone()
@ -175,24 +175,15 @@ void PurgeUnusedDiveSites::undo()
sitesToRemove = addDiveSites(sitesToAdd); sitesToRemove = addDiveSites(sitesToAdd);
} }
// Helper function: swap C and Qt string
static void swap(char *&c, QString &q)
{
QString s = c;
free(c);
c = copy_qstring(q);
q = s;
}
EditDiveSiteName::EditDiveSiteName(dive_site *dsIn, const QString &name) : ds(dsIn), EditDiveSiteName::EditDiveSiteName(dive_site *dsIn, const QString &name) : ds(dsIn),
value(name) value(name.toStdString())
{ {
setText(Command::Base::tr("Edit dive site name")); setText(Command::Base::tr("Edit dive site name"));
} }
bool EditDiveSiteName::workToBeDone() bool EditDiveSiteName::workToBeDone()
{ {
return value != QString(ds->name); return value != ds->name;
} }
void EditDiveSiteName::redo() void EditDiveSiteName::redo()
@ -208,14 +199,14 @@ void EditDiveSiteName::undo()
} }
EditDiveSiteDescription::EditDiveSiteDescription(dive_site *dsIn, const QString &description) : ds(dsIn), EditDiveSiteDescription::EditDiveSiteDescription(dive_site *dsIn, const QString &description) : ds(dsIn),
value(description) value(description.toStdString())
{ {
setText(Command::Base::tr("Edit dive site description")); setText(Command::Base::tr("Edit dive site description"));
} }
bool EditDiveSiteDescription::workToBeDone() bool EditDiveSiteDescription::workToBeDone()
{ {
return value != QString(ds->description); return value != ds->description;
} }
void EditDiveSiteDescription::redo() void EditDiveSiteDescription::redo()
@ -231,14 +222,14 @@ void EditDiveSiteDescription::undo()
} }
EditDiveSiteNotes::EditDiveSiteNotes(dive_site *dsIn, const QString &notes) : ds(dsIn), EditDiveSiteNotes::EditDiveSiteNotes(dive_site *dsIn, const QString &notes) : ds(dsIn),
value(notes) value(notes.toStdString())
{ {
setText(Command::Base::tr("Edit dive site notes")); setText(Command::Base::tr("Edit dive site notes"));
} }
bool EditDiveSiteNotes::workToBeDone() bool EditDiveSiteNotes::workToBeDone()
{ {
return value != QString(ds->notes); return value != ds->notes;
} }
void EditDiveSiteNotes::redo() void EditDiveSiteNotes::redo()
@ -400,7 +391,7 @@ ApplyGPSFixes::ApplyGPSFixes(const std::vector<DiveAndLocation> &fixes)
siteLocations.push_back({ ds, dl.location }); siteLocations.push_back({ ds, dl.location });
} }
} else { } else {
ds = create_dive_site(qPrintable(dl.name), divelog.sites); ds = create_dive_site(dl.name.toStdString(), divelog.sites);
ds->location = dl.location; ds->location = dl.location;
add_dive_to_dive_site(dl.d, ds); add_dive_to_dive_site(dl.d, ds);
dl.d->dive_site = nullptr; // This will be set on redo() dl.d->dive_site = nullptr; // This will be set on redo()

View file

@ -89,7 +89,7 @@ private:
void redo() override; void redo() override;
dive_site *ds; dive_site *ds;
QString value; // Value to be set std::string value; // Value to be set
}; };
class EditDiveSiteDescription : public Base { class EditDiveSiteDescription : public Base {
@ -101,7 +101,7 @@ private:
void redo() override; void redo() override;
dive_site *ds; dive_site *ds;
QString value; // Value to be set std::string value; // Value to be set
}; };
class EditDiveSiteNotes : public Base { class EditDiveSiteNotes : public Base {
@ -113,7 +113,7 @@ private:
void redo() override; void redo() override;
dive_site *ds; dive_site *ds;
QString value; // Value to be set std::string value; // Value to be set
}; };
class EditDiveSiteCountry : public Base { class EditDiveSiteCountry : public Base {

View file

@ -374,14 +374,11 @@ void EditDiveSite::redo()
EditDiveSite::undo(); // Undo and redo do the same EditDiveSite::undo(); // Undo and redo do the same
} }
static struct dive_site *createDiveSite(const QString &name) static struct dive_site *createDiveSite(const std::string &name)
{ {
struct dive_site *ds = new dive_site; struct dive_site *ds = new dive_site;
struct dive_site *old = current_dive ? current_dive->dive_site : nullptr; if (current_dive && current_dive->dive_site)
if (old) { *ds = *current_dive->dive_site;
copy_dive_site(old, ds);
free(ds->name); // Free name, as we will overwrite it with our own version
}
// If the current dive has a location, use that as location for the new dive site // If the current dive has a location, use that as location for the new dive site
if (current_dive) { if (current_dive) {
@ -390,12 +387,12 @@ static struct dive_site *createDiveSite(const QString &name)
ds->location = loc; ds->location = loc;
} }
ds->name = copy_qstring(name); ds->name = name;
return ds; return ds;
} }
EditDiveSiteNew::EditDiveSiteNew(const QString &newName, bool currentDiveOnly) : EditDiveSiteNew::EditDiveSiteNew(const QString &newName, bool currentDiveOnly) :
EditDiveSite(createDiveSite(newName), currentDiveOnly), EditDiveSite(createDiveSite(newName.toStdString()), currentDiveOnly),
diveSiteToAdd(value), diveSiteToAdd(value),
diveSiteToRemove(nullptr) diveSiteToRemove(nullptr)
{ {

View file

@ -170,7 +170,6 @@ static char *dt_dive_parser(unsigned char *runner, struct dive *dt_dive, struct
unsigned char tmp_1byte; unsigned char tmp_1byte;
unsigned int tmp_2bytes; unsigned int tmp_2bytes;
unsigned long tmp_4bytes; unsigned long tmp_4bytes;
struct dive_site *ds;
char is_nitrox = 0, is_O2 = 0, is_SCR = 0; char is_nitrox = 0, is_O2 = 0, is_SCR = 0;
device_data_t devdata; device_data_t devdata;
@ -212,11 +211,13 @@ static char *dt_dive_parser(unsigned char *runner, struct dive *dt_dive, struct
* Subsurface only have a location variable, so we have to merge DTrak's * Subsurface only have a location variable, so we have to merge DTrak's
* Locality and Dive points. * Locality and Dive points.
*/ */
snprintf(buffer, sizeof(buffer), "%s, %s", locality, dive_point); {
ds = get_dive_site_by_name(buffer, log->sites); std::string buffer2 = std::string((char *)locality) + " " + (char *)dive_point;
if (!ds) struct dive_site *ds = get_dive_site_by_name(buffer2, log->sites);
ds = create_dive_site(buffer, log->sites); if (!ds)
add_dive_to_dive_site(dt_dive, ds); ds = create_dive_site(buffer2, log->sites);
add_dive_to_dive_site(dt_dive, ds);
}
free(locality); free(locality);
locality = NULL; locality = NULL;
free(dive_point); free(dive_point);

View file

@ -3341,12 +3341,10 @@ std::string get_dive_country(const struct dive *dive)
return ds ? taxonomy_get_country(ds->taxonomy) : std::string(); return ds ? taxonomy_get_country(ds->taxonomy) : std::string();
} }
extern "C" const char *get_dive_location(const struct dive *dive) std::string get_dive_location(const struct dive *dive)
{ {
const struct dive_site *ds = dive->dive_site; const struct dive_site *ds = dive->dive_site;
if (ds && ds->name) return ds ? ds->name : std::string();
return ds->name;
return NULL;
} }
extern "C" unsigned int number_of_computers(const struct dive *dive) extern "C" unsigned int number_of_computers(const struct dive *dive)

View file

@ -118,9 +118,9 @@ extern struct dive_site *get_dive_site_for_dive(const struct dive *dive);
#ifdef __cplusplus #ifdef __cplusplus
} // TODO: remove } // TODO: remove
extern std::string get_dive_country(const struct dive *dive); extern std::string get_dive_country(const struct dive *dive);
extern std::string get_dive_location(const struct dive *dive);
extern "C" { extern "C" {
#endif #endif
extern const char *get_dive_location(const struct dive *dive);
extern unsigned int number_of_computers(const struct dive *dive); extern unsigned int number_of_computers(const struct dive *dive);
extern struct divecomputer *get_dive_dc(struct dive *dive, int nr); extern struct divecomputer *get_dive_dc(struct dive *dive, int nr);
extern const struct divecomputer *get_dive_dc_const(const struct dive *dive, int nr); extern const struct divecomputer *get_dive_dc_const(const struct dive *dive, int nr);

View file

@ -5,6 +5,7 @@
#include "divelist.h" #include "divelist.h"
#include "divelog.h" #include "divelog.h"
#include "errorhelper.h" #include "errorhelper.h"
#include "format.h"
#include "gettextfromc.h" #include "gettextfromc.h"
#include "membuffer.h" #include "membuffer.h"
#include "pref.h" #include "pref.h"
@ -39,12 +40,12 @@ struct dive_site *get_dive_site_by_uuid(uint32_t uuid, struct dive_site_table *d
} }
/* there could be multiple sites of the same name - return the first one */ /* there could be multiple sites of the same name - return the first one */
struct dive_site *get_dive_site_by_name(const char *name, struct dive_site_table *ds_table) struct dive_site *get_dive_site_by_name(const std::string &name, struct dive_site_table *ds_table)
{ {
int i; int i;
struct dive_site *ds; struct dive_site *ds;
for_each_dive_site (i, ds, ds_table) { for_each_dive_site (i, ds, ds_table) {
if (same_string(ds->name, name)) if (ds->name == name)
return ds; return ds;
} }
return NULL; return NULL;
@ -65,12 +66,12 @@ struct dive_site *get_dive_site_by_gps(const location_t *loc, struct dive_site_t
/* to avoid a bug where we have two dive sites with different name and the same GPS coordinates /* to avoid a bug where we have two dive sites with different name and the same GPS coordinates
* and first get the gps coordinates (reading a V2 file) and happen to get back "the other" name, * and first get the gps coordinates (reading a V2 file) and happen to get back "the other" name,
* this function allows us to verify if a very specific name/GPS combination already exists */ * this function allows us to verify if a very specific name/GPS combination already exists */
struct dive_site *get_dive_site_by_gps_and_name(const char *name, const location_t *loc, struct dive_site_table *ds_table) struct dive_site *get_dive_site_by_gps_and_name(const std::string &name, const location_t *loc, struct dive_site_table *ds_table)
{ {
int i; int i;
struct dive_site *ds; struct dive_site *ds;
for_each_dive_site (i, ds, ds_table) { for_each_dive_site (i, ds, ds_table) {
if (same_location(loc, &ds->location) && same_string(ds->name, name)) if (same_location(loc, &ds->location) && ds->name == name)
return ds; return ds;
} }
return NULL; return NULL;
@ -146,12 +147,12 @@ int add_dive_site_to_table(struct dive_site *ds, struct dive_site_table *ds_tabl
* Make this deterministic for testing. */ * Make this deterministic for testing. */
if (!ds->uuid) { if (!ds->uuid) {
SHA1 sha; SHA1 sha;
if (ds->name) if (!ds->name.empty())
sha.update(ds->name, strlen(ds->name)); sha.update(ds->name);
if (ds->description) if (!ds->description.empty())
sha.update(ds->description, strlen(ds->description)); sha.update(ds->description);
if (ds->notes) if (!ds->notes.empty())
sha.update(ds->notes, strlen(ds->notes)); sha.update(ds->notes);
ds->uuid = sha.hash_uint32(); ds->uuid = sha.hash_uint32();
} }
@ -169,19 +170,16 @@ dive_site::dive_site()
{ {
} }
dive_site::dive_site(const char *name) : name(copy_string(name)) dive_site::dive_site(const std::string &name) : name(name)
{ {
} }
dive_site::dive_site(const char *name, const location_t *loc) : name(copy_string(name)), location(*loc) dive_site::dive_site(const std::string &name, const location_t *loc) : name(name), location(*loc)
{ {
} }
dive_site::~dive_site() dive_site::~dive_site()
{ {
free(name);
free(notes);
free(description);
} }
/* when parsing, dive sites are identified by uuid */ /* when parsing, dive sites are identified by uuid */
@ -225,7 +223,7 @@ void delete_dive_site(struct dive_site *ds, struct dive_site_table *ds_table)
} }
/* allocate a new site and add it to the table */ /* allocate a new site and add it to the table */
struct dive_site *create_dive_site(const char *name, struct dive_site_table *ds_table) struct dive_site *create_dive_site(const std::string &name, struct dive_site_table *ds_table)
{ {
struct dive_site *ds = new dive_site(name); struct dive_site *ds = new dive_site(name);
add_dive_site_to_table(ds, ds_table); add_dive_site_to_table(ds, ds_table);
@ -233,7 +231,7 @@ struct dive_site *create_dive_site(const char *name, struct dive_site_table *ds_
} }
/* same as before, but with GPS data */ /* same as before, but with GPS data */
struct dive_site *create_dive_site_with_gps(const char *name, const location_t *loc, struct dive_site_table *ds_table) struct dive_site *create_dive_site_with_gps(const std::string &name, const location_t *loc, struct dive_site_table *ds_table)
{ {
struct dive_site *ds = new dive_site(name, loc); struct dive_site *ds = new dive_site(name, loc);
add_dive_site_to_table(ds, ds_table); add_dive_site_to_table(ds, ds_table);
@ -244,42 +242,26 @@ struct dive_site *create_dive_site_with_gps(const char *name, const location_t *
bool dive_site_is_empty(struct dive_site *ds) bool dive_site_is_empty(struct dive_site *ds)
{ {
return !ds || return !ds ||
(empty_string(ds->name) && (ds->name.empty() &&
empty_string(ds->description) && ds->description.empty() &&
empty_string(ds->notes) && ds->notes.empty() &&
!has_location(&ds->location)); !has_location(&ds->location));
} }
void copy_dive_site(struct dive_site *orig, struct dive_site *copy) static void merge_string(std::string &a, const std::string &b)
{ {
free(copy->name); if (b.empty())
free(copy->notes);
free(copy->description);
copy->location = orig->location;
copy->name = copy_string(orig->name);
copy->notes = copy_string(orig->notes);
copy->description = copy_string(orig->description);
copy->taxonomy = orig->taxonomy;
}
static void merge_string(char **a, char **b)
{
char *s1 = *a, *s2 = *b;
if (!s2)
return; return;
if (same_string(s1, s2)) if (a == b)
return; return;
if (!s1) { if (a.empty()) {
*a = strdup(s2); a = b;
return; return;
} }
*a = format_string("(%s) or (%s)", s1, s2); a = format_string_std("(%s) or (%s)", a.c_str(), b.c_str());
free(s1);
} }
/* Used to check on import if two dive sites are equivalent. /* Used to check on import if two dive sites are equivalent.
@ -290,10 +272,10 @@ static void merge_string(char **a, char **b)
*/ */
static bool same_dive_site(const struct dive_site *a, const struct dive_site *b) static bool same_dive_site(const struct dive_site *a, const struct dive_site *b)
{ {
return same_string(a->name, b->name) return a->name == b->name
&& same_location(&a->location, &b->location) && same_location(&a->location, &b->location)
&& same_string(a->description, b->description) && a->description == b->description
&& same_string(a->notes, b->notes); && a->notes == b->notes;
} }
struct dive_site *get_same_dive_site(const struct dive_site *site) struct dive_site *get_same_dive_site(const struct dive_site *site)
@ -309,20 +291,20 @@ struct dive_site *get_same_dive_site(const struct dive_site *site)
void merge_dive_site(struct dive_site *a, struct dive_site *b) void merge_dive_site(struct dive_site *a, struct dive_site *b)
{ {
if (!has_location(&a->location)) a->location = b->location; if (!has_location(&a->location)) a->location = b->location;
merge_string(&a->name, &b->name); merge_string(a->name, b->name);
merge_string(&a->notes, &b->notes); merge_string(a->notes, b->notes);
merge_string(&a->description, &b->description); merge_string(a->description, b->description);
if (a->taxonomy.empty()) if (a->taxonomy.empty())
a->taxonomy = std::move(b->taxonomy); a->taxonomy = std::move(b->taxonomy);
} }
struct dive_site *find_or_create_dive_site_with_name(const char *name, struct dive_site_table *ds_table) struct dive_site *find_or_create_dive_site_with_name(const std::string &name, struct dive_site_table *ds_table)
{ {
int i; int i;
struct dive_site *ds; struct dive_site *ds;
for_each_dive_site(i,ds, ds_table) { for_each_dive_site(i,ds, ds_table) {
if (same_string(name, ds->name)) if (name == ds->name)
break; break;
} }
if (ds) if (ds)

View file

@ -13,15 +13,15 @@
struct dive_site struct dive_site
{ {
uint32_t uuid = 0; uint32_t uuid = 0;
char *name = nullptr; std::string name;
std::vector<dive *> dives; std::vector<dive *> dives;
location_t location = { { 9 }, { 0 } }; location_t location = { { 9 }, { 0 } };
char *description = nullptr; std::string description;
char *notes = nullptr; std::string notes;
taxonomy_data taxonomy; taxonomy_data taxonomy;
dive_site(); dive_site();
dive_site(const char *name); dive_site(const std::string &name);
dive_site(const char *name, const location_t *loc); dive_site(const std::string &name, const location_t *loc);
~dive_site(); ~dive_site();
}; };
@ -54,19 +54,17 @@ bool is_dive_site_selected(const struct dive_site &ds);
int unregister_dive_site(struct dive_site *ds); int unregister_dive_site(struct dive_site *ds);
int register_dive_site(struct dive_site *ds); int register_dive_site(struct dive_site *ds);
void delete_dive_site(struct dive_site *ds, struct dive_site_table *ds_table); void delete_dive_site(struct dive_site *ds, struct dive_site_table *ds_table);
struct dive_site *create_dive_site(const char *name, struct dive_site_table *ds_table); struct dive_site *create_dive_site(const std::string &name, struct dive_site_table *ds_table);
struct dive_site *create_dive_site_with_gps(const char *name, const location_t *, struct dive_site_table *ds_table); struct dive_site *create_dive_site_with_gps(const std::string &name, const location_t *, struct dive_site_table *ds_table);
struct dive_site *get_dive_site_by_name(const char *name, struct dive_site_table *ds_table); struct dive_site *get_dive_site_by_name(const std::string &name, struct dive_site_table *ds_table);
struct dive_site *get_dive_site_by_gps(const location_t *, struct dive_site_table *ds_table); struct dive_site *get_dive_site_by_gps(const location_t *, struct dive_site_table *ds_table);
struct dive_site *get_dive_site_by_gps_and_name(const char *name, const location_t *, struct dive_site_table *ds_table); struct dive_site *get_dive_site_by_gps_and_name(const std::string &name, const location_t *, struct dive_site_table *ds_table);
struct dive_site *get_dive_site_by_gps_proximity(const location_t *, int distance, struct dive_site_table *ds_table); struct dive_site *get_dive_site_by_gps_proximity(const location_t *, int distance, struct dive_site_table *ds_table);
struct dive_site *get_same_dive_site(const struct dive_site *); struct dive_site *get_same_dive_site(const struct dive_site *);
bool dive_site_is_empty(struct dive_site *ds); bool dive_site_is_empty(struct dive_site *ds);
void copy_dive_site_taxonomy(struct dive_site *orig, struct dive_site *copy);
void copy_dive_site(struct dive_site *orig, struct dive_site *copy);
void merge_dive_site(struct dive_site *a, struct dive_site *b); void merge_dive_site(struct dive_site *a, struct dive_site *b);
unsigned int get_distance(const location_t *loc1, const location_t *loc2); unsigned int get_distance(const location_t *loc1, const location_t *loc2);
struct dive_site *find_or_create_dive_site_with_name(const char *name, struct dive_site_table *ds_table); struct dive_site *find_or_create_dive_site_with_name(const std::string &name, struct dive_site_table *ds_table);
void purge_empty_dive_sites(struct dive_site_table *ds_table); void purge_empty_dive_sites(struct dive_site_table *ds_table);
void clear_dive_site_table(struct dive_site_table *ds_table); void clear_dive_site_table(struct dive_site_table *ds_table);
void move_dive_site_table(struct dive_site_table *src, struct dive_site_table *dst); void move_dive_site_table(struct dive_site_table *src, struct dive_site_table *dst);

View file

@ -841,7 +841,7 @@ static bool has_locations(const filter_constraint &c, const struct dive *d)
diveLocations.push_back(QString(d->divetrip->location).trimmed()); diveLocations.push_back(QString(d->divetrip->location).trimmed());
if (d->dive_site) if (d->dive_site)
diveLocations.push_back(QString(d->dive_site->name).trimmed()); diveLocations.push_back(QString::fromStdString(d->dive_site->name).trimmed());
return check(c, diveLocations); return check(c, diveLocations);
} }

View file

@ -140,7 +140,7 @@ static std::vector<QString> getWords(const dive *d)
// TODO: We should tokenize all dive-sites and trips first and then // TODO: We should tokenize all dive-sites and trips first and then
// take the tokens from a cache. // take the tokens from a cache.
if (d->dive_site) { if (d->dive_site) {
tokenize(d->dive_site->name, res); tokenize(QString::fromStdString(d->dive_site->name), res);
std::string country = taxonomy_get_country(d->dive_site->taxonomy); std::string country = taxonomy_get_country(d->dive_site->taxonomy);
if (!country.empty()) if (!country.empty())
tokenize(country.c_str(), res); tokenize(country.c_str(), res);

View file

@ -181,15 +181,8 @@ static int cobalt_dive(void *param, int, char **data, char **)
} }
if (location && location_site) { if (location && location_site) {
char *tmp = (char *)malloc(strlen(location) + strlen(location_site) + 4); std::string tmp = std::string(location) + " / " + location_site;
if (!tmp) {
free(location);
free(location_site);
return 1;
}
sprintf(tmp, "%s / %s", location, location_site);
add_dive_to_dive_site(state->cur_dive, find_or_create_dive_site_with_name(tmp, state->log->sites)); add_dive_to_dive_site(state->cur_dive, find_or_create_dive_site_with_name(tmp, state->log->sites));
free(tmp);
} }
free(location); free(location);
free(location_site); free(location_site);
@ -206,7 +199,6 @@ static int cobalt_dive(void *param, int, char **data, char **)
return SQLITE_OK; return SQLITE_OK;
} }
extern "C" int parse_cobalt_buffer(sqlite3 *handle, const char *url, const char *, int, struct divelog *log) extern "C" int parse_cobalt_buffer(sqlite3 *handle, const char *url, const char *, int, struct divelog *log)
{ {
int retval; int retval;

View file

@ -275,7 +275,7 @@ static int divinglog_dive(void *param, int, char **data, char **)
state->cur_dive->when = (time_t)(atol(data[1])); state->cur_dive->when = (time_t)(atol(data[1]));
if (data[2]) if (data[2])
add_dive_to_dive_site(state->cur_dive, find_or_create_dive_site_with_name(data[2], state->log->sites)); add_dive_to_dive_site(state->cur_dive, find_or_create_dive_site_with_name(std::string(data[2]), state->log->sites));
if (data[3]) if (data[3])
utf8_string(data[3], &state->cur_dive->buddy); utf8_string(data[3], &state->cur_dive->buddy);

View file

@ -637,7 +637,7 @@ static void parse_string_field(device_data_t *devdata, struct dive *dive, dc_fie
if (location.lat.udeg && location.lon.udeg) { if (location.lat.udeg && location.lon.udeg) {
unregister_dive_from_dive_site(dive); unregister_dive_from_dive_site(dive);
add_dive_to_dive_site(dive, create_dive_site_with_gps(str->value, &location, devdata->log->sites)); add_dive_to_dive_site(dive, create_dive_site_with_gps(std::string(str->value), &location, devdata->log->sites));
} }
} }
} }

View file

@ -191,7 +191,7 @@ static void parse_dives(int log_version, const unsigned char *buf, unsigned int
/* Store the location only if we have one */ /* Store the location only if we have one */
if (!location.empty()) if (!location.empty())
add_dive_to_dive_site(dive, find_or_create_dive_site_with_name(location.c_str(), sites)); add_dive_to_dive_site(dive, find_or_create_dive_site_with_name(location, sites));
ptr += len + 4 + place_len; ptr += len + 4 + place_len;

View file

@ -178,7 +178,7 @@ static void parse_dive_gps(char *line, struct git_parser_state *state)
if (!ds) { if (!ds) {
ds = get_dive_site_by_gps(&location, state->log->sites); ds = get_dive_site_by_gps(&location, state->log->sites);
if (!ds) if (!ds)
ds = create_dive_site_with_gps("", &location, state->log->sites); ds = create_dive_site_with_gps(std::string(), &location, state->log->sites);
add_dive_to_dive_site(state->active_dive, ds); add_dive_to_dive_site(state->active_dive, ds);
} else { } else {
if (dive_site_has_gps_location(ds) && !same_location(&ds->location, &location)) { if (dive_site_has_gps_location(ds) && !same_location(&ds->location, &location)) {
@ -189,10 +189,8 @@ static void parse_dive_gps(char *line, struct git_parser_state *state)
// note 2: we could include the first newline in the // note 2: we could include the first newline in the
// translation string, but that would be weird and cause // translation string, but that would be weird and cause
// a new string. // a new string.
std::string new_text = std::string(ds->notes) + '\n' + ds->notes += '\n';
format_string_std(translate("gettextFromC", "multiple GPS locations for this dive site; also %s\n"), coords.c_str()); ds->notes += format_string_std(translate("gettextFromC", "multiple GPS locations for this dive site; also %s\n"), coords.c_str());
free(ds->notes);
ds->notes = strdup(new_text.c_str());
} }
ds->location = location; ds->location = location;
} }
@ -224,21 +222,19 @@ static void parse_dive_location(char *, struct git_parser_state *state)
std::string name = get_first_converted_string(state); std::string name = get_first_converted_string(state);
struct dive_site *ds = get_dive_site_for_dive(state->active_dive); struct dive_site *ds = get_dive_site_for_dive(state->active_dive);
if (!ds) { if (!ds) {
ds = get_dive_site_by_name(name.c_str(), state->log->sites); ds = get_dive_site_by_name(name, state->log->sites);
if (!ds) if (!ds)
ds = create_dive_site(name.c_str(), state->log->sites); ds = create_dive_site(name, state->log->sites);
add_dive_to_dive_site(state->active_dive, ds); add_dive_to_dive_site(state->active_dive, ds);
} else { } else {
// we already had a dive site linked to the dive // we already had a dive site linked to the dive
if (empty_string(ds->name)) { if (ds->name.empty()) {
free(ds->name); // empty_string could mean pointer to a 0-byte! ds->name = name.c_str();
ds->name = strdup(name.c_str());
} else { } else {
// and that dive site had a name. that's weird - if our name is different, add it to the notes // and that dive site had a name. that's weird - if our name is different, add it to the notes
if (!same_string(ds->name, name.c_str())) { if (ds->name == name) {
std::string new_string = std::string(ds->notes) + '\n' + ds->notes += '\n';
format_string_std(translate("gettextFromC", "additional name for site: %s\n"), name.c_str()); ds->notes += format_string_std(translate("gettextFromC", "additional name for site: %s\n"), name.c_str());
ds->notes = strdup(new_string.c_str());
} }
} }
} }
@ -314,7 +310,7 @@ static void parse_dive_invalid(char *, struct git_parser_state *state)
} }
static void parse_site_description(char *, struct git_parser_state *state) static void parse_site_description(char *, struct git_parser_state *state)
{ state->active_site->description = get_first_converted_string_c(state); } { state->active_site->description = get_first_converted_string(state); }
static void parse_site_name(char *, struct git_parser_state *state) static void parse_site_name(char *, struct git_parser_state *state)
{ state->active_site->name = get_first_converted_string_c(state); } { state->active_site->name = get_first_converted_string_c(state); }

View file

@ -978,10 +978,9 @@ static void try_to_fill_sample(struct sample *sample, const char *name, char *bu
static void divinglog_place(const char *place, struct dive *d, struct parser_state *state) static void divinglog_place(const char *place, struct dive *d, struct parser_state *state)
{ {
char buffer[1024];
struct dive_site *ds; struct dive_site *ds;
snprintf(buffer, sizeof(buffer), std::string buffer = format_string_std(
"%s%s%s%s%s", "%s%s%s%s%s",
place, place,
!state->city.empty() ? ", " : "", !state->city.empty() ? ", " : "",
@ -1160,10 +1159,11 @@ static void gps_lat(const char *buffer, struct dive *dive, struct parser_state *
location.lat = parse_degrees(buffer, &end); location.lat = parse_degrees(buffer, &end);
if (!ds) { if (!ds) {
add_dive_to_dive_site(dive, create_dive_site_with_gps(NULL, &location, state->log->sites)); add_dive_to_dive_site(dive, create_dive_site_with_gps(std::string(), &location, state->log->sites));
} else { } else {
if (ds->location.lat.udeg && ds->location.lat.udeg != location.lat.udeg) if (ds->location.lat.udeg && ds->location.lat.udeg != location.lat.udeg)
report_info("Oops, changing the latitude of existing dive site id %8x name %s; not good", ds->uuid, ds->name ?: "(unknown)"); report_info("Oops, changing the latitude of existing dive site id %8x name %s; not good", ds->uuid,
ds->name.empty() ? "(unknown)" : ds->name.c_str());
ds->location.lat = location.lat; ds->location.lat = location.lat;
} }
} }
@ -1176,10 +1176,11 @@ static void gps_long(const char *buffer, struct dive *dive, struct parser_state
location.lon = parse_degrees(buffer, &end); location.lon = parse_degrees(buffer, &end);
if (!ds) { if (!ds) {
add_dive_to_dive_site(dive, create_dive_site_with_gps(NULL, &location, state->log->sites)); add_dive_to_dive_site(dive, create_dive_site_with_gps(std::string(), &location, state->log->sites));
} else { } else {
if (ds->location.lon.udeg && ds->location.lon.udeg != location.lon.udeg) if (ds->location.lon.udeg && ds->location.lon.udeg != location.lon.udeg)
report_info("Oops, changing the longitude of existing dive site id %8x name %s; not good", ds->uuid, ds->name ?: "(unknown)"); report_info("Oops, changing the longitude of existing dive site id %8x name %s; not good", ds->uuid,
ds->name.empty() ? "(unknown)" : ds->name.c_str());
ds->location.lon = location.lon; ds->location.lon = location.lon;
} }
} }
@ -1213,7 +1214,7 @@ static void gps_in_dive(const char *buffer, struct dive *dive, struct parser_sta
// remember the original coordinates so we can create the correct dive site later // remember the original coordinates so we can create the correct dive site later
state->cur_location = location; state->cur_location = location;
} else { } else {
ds = create_dive_site_with_gps("", &location, state->log->sites); ds = create_dive_site_with_gps(std::string(), &location, state->log->sites);
} }
add_dive_to_dive_site(dive, ds); add_dive_to_dive_site(dive, ds);
} else { } else {
@ -1224,7 +1225,8 @@ static void gps_in_dive(const char *buffer, struct dive *dive, struct parser_sta
ds->location.lat.udeg / 1000000.0, ds->location.lon.udeg / 1000000.0, ds->location.lat.udeg / 1000000.0, ds->location.lon.udeg / 1000000.0,
location.lat.udeg / 1000000.0, location.lon.udeg / 1000000.0); location.lat.udeg / 1000000.0, location.lon.udeg / 1000000.0);
std::string coords = printGPSCoordsC(&location); std::string coords = printGPSCoordsC(&location);
ds->notes = add_to_string(ds->notes, translate("gettextFromC", "multiple GPS locations for this dive site; also %s\n"), coords.c_str()); ds->notes += '\n';
ds->notes += format_string_std(translate("gettextFromC", "multiple GPS locations for this dive site; also %s\n"), coords.c_str());
} else { } else {
ds->location = location; ds->location = location;
} }
@ -1417,11 +1419,11 @@ static void try_to_fill_dive_site(struct parser_state *state, const char *name,
if (MATCH("uuid", hex_value, &ds->uuid)) if (MATCH("uuid", hex_value, &ds->uuid))
return; return;
if (MATCH("name", utf8_string, &ds->name)) if (MATCH("name", utf8_string_std, &ds->name))
return; return;
if (MATCH("description", utf8_string, &ds->description)) if (MATCH("description", utf8_string_std, &ds->description))
return; return;
if (MATCH("notes", utf8_string, &ds->notes)) if (MATCH("notes", utf8_string_std, &ds->notes))
return; return;
if (MATCH("gps", gps_location, ds.get())) if (MATCH("gps", gps_location, ds.get()))
return; return;
@ -1802,6 +1804,7 @@ static timestamp_t parse_dlf_timestamp(unsigned char *buffer)
extern "C" int parse_dlf_buffer(unsigned char *buffer, size_t size, struct divelog *log) extern "C" int parse_dlf_buffer(unsigned char *buffer, size_t size, struct divelog *log)
{ {
using namespace std::string_literals;
unsigned char *ptr = buffer; unsigned char *ptr = buffer;
unsigned char event; unsigned char event;
bool found; bool found;
@ -2232,7 +2235,7 @@ extern "C" int parse_dlf_buffer(unsigned char *buffer, size_t size, struct divel
/* Measure GPS */ /* Measure GPS */
state.cur_location.lat.udeg = (int)((ptr[7] << 24) + (ptr[6] << 16) + (ptr[5] << 8) + (ptr[4] << 0)); state.cur_location.lat.udeg = (int)((ptr[7] << 24) + (ptr[6] << 16) + (ptr[5] << 8) + (ptr[4] << 0));
state.cur_location.lon.udeg = (int)((ptr[11] << 24) + (ptr[10] << 16) + (ptr[9] << 8) + (ptr[8] << 0)); state.cur_location.lon.udeg = (int)((ptr[11] << 24) + (ptr[10] << 16) + (ptr[9] << 8) + (ptr[8] << 0));
add_dive_to_dive_site(state.cur_dive, create_dive_site_with_gps("DLF imported", &state.cur_location, state.log->sites)); add_dive_to_dive_site(state.cur_dive, create_dive_site_with_gps("DLF imported"s, &state.cur_location, state.log->sites));
break; break;
default: default:
break; break;

View file

@ -12,6 +12,7 @@
#include "divelog.h" #include "divelog.h"
#include "divesite.h" #include "divesite.h"
#include "errorhelper.h" #include "errorhelper.h"
#include "format.h"
#include "sample.h" #include "sample.h"
#include "subsurface-string.h" #include "subsurface-string.h"
#include "picture.h" #include "picture.h"
@ -203,7 +204,7 @@ void dive_site_end(struct parser_state *state)
merge_dive_site(ds, state->cur_dive_site.get()); merge_dive_site(ds, state->cur_dive_site.get());
if (verbose > 3) if (verbose > 3)
printf("completed dive site uuid %x8 name {%s}\n", ds->uuid, ds->name); printf("completed dive site uuid %x8 name {%s}\n", ds->uuid, ds->name.c_str());
state->cur_dive_site.reset(); state->cur_dive_site.reset();
} }
@ -470,18 +471,18 @@ void add_dive_site(const char *ds_name, struct dive *dive, struct parser_state *
struct dive_site *ds = dive->dive_site; struct dive_site *ds = dive->dive_site;
if (!ds) { if (!ds) {
// if the dive doesn't have a dive site, check if there's already a dive site by this name // if the dive doesn't have a dive site, check if there's already a dive site by this name
ds = get_dive_site_by_name(trimmed.c_str(), state->log->sites); ds = get_dive_site_by_name(trimmed, state->log->sites);
} }
if (ds) { if (ds) {
// we have a dive site, let's hope there isn't a different name // we have a dive site, let's hope there isn't a different name
if (empty_string(ds->name)) { if (ds->name.empty()) {
ds->name = copy_string(trimmed.c_str()); ds->name = trimmed;
} else if (trimmed != ds->name) { } else if (trimmed != ds->name) {
// if it's not the same name, it's not the same dive site // if it's not the same name, it's not the same dive site
// but wait, we could have gotten this one based on GPS coords and could // but wait, we could have gotten this one based on GPS coords and could
// have had two different names for the same site... so let's search the other // have had two different names for the same site... so let's search the other
// way around // way around
struct dive_site *exact_match = get_dive_site_by_gps_and_name(trimmed.c_str(), &ds->location, state->log->sites); struct dive_site *exact_match = get_dive_site_by_gps_and_name(trimmed, &ds->location, state->log->sites);
if (exact_match) { if (exact_match) {
unregister_dive_from_dive_site(dive); unregister_dive_from_dive_site(dive);
add_dive_to_dive_site(dive, exact_match); add_dive_to_dive_site(dive, exact_match);
@ -495,7 +496,8 @@ void add_dive_site(const char *ds_name, struct dive *dive, struct parser_state *
} else { } else {
newds->location = ds->location; newds->location = ds->location;
} }
newds->notes = add_to_string(newds->notes, translate("gettextFromC", "additional name for site: %s\n"), ds->name); newds->notes += '\n';
newds->notes += format_string_std(translate("gettextFromC", "additional name for site: %s\n"), ds->name.c_str());
} }
} else if (dive->dive_site != ds) { } else if (dive->dive_site != ds) {
// add the existing dive site to the current dive // add the existing dive site to the current dive
@ -503,7 +505,7 @@ void add_dive_site(const char *ds_name, struct dive *dive, struct parser_state *
add_dive_to_dive_site(dive, ds); add_dive_to_dive_site(dive, ds);
} }
} else { } else {
add_dive_to_dive_site(dive, create_dive_site(trimmed.c_str(), state->log->sites)); add_dive_to_dive_site(dive, create_dive_site(trimmed, state->log->sites));
} }
} }
} }

View file

@ -929,9 +929,9 @@ static void save_divesites(git_repository *repo, struct dir *tree)
struct dive_site *ds = get_dive_site(i, divelog.sites); struct dive_site *ds = get_dive_site(i, divelog.sites);
struct membufferpp site_file_name; struct membufferpp site_file_name;
put_format(&site_file_name, "Site-%08x", ds->uuid); put_format(&site_file_name, "Site-%08x", ds->uuid);
show_utf8(&b, "name ", ds->name, "\n"); show_utf8(&b, "name ", ds->name.c_str(), "\n");
show_utf8(&b, "description ", ds->description, "\n"); show_utf8(&b, "description ", ds->description.c_str(), "\n");
show_utf8(&b, "notes ", ds->notes, "\n"); show_utf8(&b, "notes ", ds->notes.c_str(), "\n");
put_location(&b, &ds->location, "gps ", "\n"); put_location(&b, &ds->location, "gps ", "\n");
for (const auto &t: ds->taxonomy) { for (const auto &t: ds->taxonomy) {
if (t.category != TC_NONE && !t.value.empty()) { if (t.category != TC_NONE && !t.value.empty()) {
@ -1142,15 +1142,17 @@ static void create_commit_message(struct membuffer *msg, bool create_empty)
put_format(msg, "Changes made: \n\n%s\n", changes_made.c_str()); put_format(msg, "Changes made: \n\n%s\n", changes_made.c_str());
} else if (dive) { } else if (dive) {
dive_trip_t *trip = dive->divetrip; dive_trip_t *trip = dive->divetrip;
const char *location = get_dive_location(dive) ? : "no location"; std::string location = get_dive_location(dive);
if (location.empty())
location = "no location";
struct divecomputer *dc = &dive->dc; struct divecomputer *dc = &dive->dc;
const char *sep = "\n"; const char *sep = "\n";
if (dive->number) if (dive->number)
nr = dive->number; nr = dive->number;
put_format(msg, "dive %d: %s", nr, location); put_format(msg, "dive %d: %s", nr, location.c_str());
if (trip && !empty_string(trip->location) && strcmp(trip->location, location)) if (trip && !empty_string(trip->location) && location != trip->location)
put_format(msg, " (%s)", trip->location); put_format(msg, " (%s)", trip->location);
put_format(msg, "\n"); put_format(msg, "\n");
do { do {

View file

@ -355,7 +355,7 @@ static void write_one_dive(struct membuffer *b, struct dive *dive, const char *p
put_format(b, "\"subsurface_number\":%d,", dive->number); put_format(b, "\"subsurface_number\":%d,", dive->number);
put_HTML_date(b, dive, "\"date\":\"", "\","); put_HTML_date(b, dive, "\"date\":\"", "\",");
put_HTML_time(b, dive, "\"time\":\"", "\","); put_HTML_time(b, dive, "\"time\":\"", "\",");
write_attribute(b, "location", get_dive_location(dive), ", "); write_attribute(b, "location", get_dive_location(dive).c_str(), ", ");
put_HTML_coordinates(b, dive); put_HTML_coordinates(b, dive);
put_format(b, "\"rating\":%d,", dive->rating); put_format(b, "\"rating\":%d,", dive->rating);
put_format(b, "\"visibility\":%d,", dive->visibility); put_format(b, "\"visibility\":%d,", dive->visibility);

View file

@ -711,11 +711,11 @@ static void save_dives_buffer(struct membuffer *b, bool select_only, bool anonym
continue; continue;
put_format(b, "<site uuid='%8x'", ds->uuid); put_format(b, "<site uuid='%8x'", ds->uuid);
show_utf8_blanked(b, ds->name, " name='", "'", 1, anonymize); show_utf8_blanked(b, ds->name.c_str(), " name='", "'", 1, anonymize);
put_location(b, &ds->location, " gps='", "'"); put_location(b, &ds->location, " gps='", "'");
show_utf8_blanked(b, ds->description, " description='", "'", 1, anonymize); show_utf8_blanked(b, ds->description.c_str(), " description='", "'", 1, anonymize);
put_format(b, ">\n"); put_format(b, ">\n");
show_utf8_blanked(b, ds->notes, " <notes>", " </notes>\n", 0, anonymize); show_utf8_blanked(b, ds->notes.c_str(), " <notes>", " </notes>\n", 0, anonymize);
for (auto const &t: ds->taxonomy) { for (auto const &t: ds->taxonomy) {
if (t.category != TC_NONE && !t.value.empty()) { if (t.category != TC_NONE && !t.value.empty()) {
put_format(b, " <geo cat='%d'", t.category); put_format(b, " <geo cat='%d'", t.category);
@ -913,11 +913,11 @@ static void save_dive_sites_buffer(struct membuffer *b, const struct dive_site *
const struct dive_site *ds = sites[i]; const struct dive_site *ds = sites[i];
put_format(b, "<site uuid='%8x'", ds->uuid); put_format(b, "<site uuid='%8x'", ds->uuid);
show_utf8_blanked(b, ds->name, " name='", "'", 1, anonymize); show_utf8_blanked(b, ds->name.c_str(), " name='", "'", 1, anonymize);
put_location(b, &ds->location, " gps='", "'"); put_location(b, &ds->location, " gps='", "'");
show_utf8_blanked(b, ds->description, " description='", "'", 1, anonymize); show_utf8_blanked(b, ds->description.c_str(), " description='", "'", 1, anonymize);
put_format(b, ">\n"); put_format(b, ">\n");
show_utf8_blanked(b, ds->notes, " <notes>", " </notes>\n", 0, anonymize); show_utf8_blanked(b, ds->notes.c_str(), " <notes>", " </notes>\n", 0, anonymize);
for (const auto &t: ds->taxonomy) { for (const auto &t: ds->taxonomy) {
if (t.category != TC_NONE && !t.value.empty()) { if (t.category != TC_NONE && !t.value.empty()) {
put_format(b, " <geo cat='%d'", t.category); put_format(b, " <geo cat='%d'", t.category);

View file

@ -148,7 +148,7 @@ dive_trip_t *create_trip_from_dive(struct dive *dive)
dive_trip_t *trip; dive_trip_t *trip;
trip = alloc_trip(); trip = alloc_trip();
trip->location = copy_string(get_dive_location(dive)); trip->location = copy_string(get_dive_location(dive).c_str());
return trip; return trip;
} }
@ -265,8 +265,9 @@ dive_trip_t *get_dives_to_autogroup(struct dive_table *table, int start, int *fr
if (dive->divetrip || dive->notrip || if (dive->divetrip || dive->notrip ||
dive->when >= lastdive->when + TRIP_THRESHOLD) dive->when >= lastdive->when + TRIP_THRESHOLD)
break; break;
if (get_dive_location(dive) && !trip->location) std::string location = get_dive_location(dive);
trip->location = copy_string(get_dive_location(dive)); if (!location.empty() && !trip->location)
trip->location = copy_string(get_dive_location(dive).c_str());
lastdive = dive; lastdive = dive;
} }
return trip; return trip;

View file

@ -808,6 +808,7 @@ static bool uemis_delete_dive(device_data_t *devdata, uint32_t diveid)
* the addresses of those fields for every dive that references the dive spot. */ * the addresses of those fields for every dive that references the dive spot. */
static bool process_raw_buffer(device_data_t *devdata, uint32_t deviceid, std::string_view buf, int &max_divenr, int *for_dive) static bool process_raw_buffer(device_data_t *devdata, uint32_t deviceid, std::string_view buf, int &max_divenr, int *for_dive)
{ {
using namespace std::string_literals;
bool done = false; bool done = false;
bool is_log = false, is_dive = false; bool is_log = false, is_dive = false;
std::vector<std::string_view> sections; std::vector<std::string_view> sections;
@ -914,7 +915,7 @@ static bool process_raw_buffer(device_data_t *devdata, uint32_t deviceid, std::s
} else if (!is_log && dive && tag == "divespot_id") { } else if (!is_log && dive && tag == "divespot_id") {
int divespot_id; int divespot_id;
if (from_chars(val, divespot_id).ec != std::errc::invalid_argument) { if (from_chars(val, divespot_id).ec != std::errc::invalid_argument) {
struct dive_site *ds = create_dive_site("from Uemis", devdata->log->sites); struct dive_site *ds = create_dive_site("from Uemis"s, devdata->log->sites);
unregister_dive_from_dive_site(dive); unregister_dive_from_dive_site(dive);
add_dive_to_dive_site(dive, ds); add_dive_to_dive_site(dive, ds);
uemis_obj.mark_divelocation(dive->dc.diveid, divespot_id, ds); uemis_obj.mark_divelocation(dive->dc.diveid, divespot_id, ds);
@ -1099,17 +1100,16 @@ static void get_uemis_divespot(device_data_t *devdata, const std::string &mountp
struct dive_site *ds = it->second; struct dive_site *ds = it->second;
unregister_dive_from_dive_site(dive); unregister_dive_from_dive_site(dive);
add_dive_to_dive_site(dive, ds); add_dive_to_dive_site(dive, ds);
} else if (nds && nds->name && strstr(nds->name,"from Uemis")) { } else if (nds && !nds->name.empty() && nds->name.find("from Uemis") != std::string::npos) {
if (load_uemis_divespot(mountpath, divespot_id)) { if (load_uemis_divespot(mountpath, divespot_id)) {
/* get the divesite based on the diveid, this should give us /* get the divesite based on the diveid, this should give us
* the newly created site * the newly created site
*/ */
struct dive_site *ods;
/* with the divesite name we got from parse_dive, that is called on load_uemis_divespot /* with the divesite name we got from parse_dive, that is called on load_uemis_divespot
* we search all existing divesites if we have one with the same name already. The function * we search all existing divesites if we have one with the same name already. The function
* returns the first found which is luckily not the newly created. * returns the first found which is luckily not the newly created.
*/ */
ods = get_dive_site_by_name(nds->name, devdata->log->sites); struct dive_site *ods = get_dive_site_by_name(nds->name, devdata->log->sites);
if (ods) { if (ods) {
/* if the uuid's are the same, the new site is a duplicate and can be deleted */ /* if the uuid's are the same, the new site is a duplicate and can be deleted */
if (nds->uuid != ods->uuid) { if (nds->uuid != ods->uuid) {

View file

@ -165,8 +165,7 @@ void uemis::set_divelocation(int divespot, const std::string &text, double longi
if (it.second.divespot == divespot) { if (it.second.divespot == divespot) {
struct dive_site *ds = it.second.dive_site; struct dive_site *ds = it.second.dive_site;
if (ds) { if (ds) {
free(ds->name); ds->name = text;
ds->name = strdup(text.c_str());
ds->location = create_location(latitude, longitude); ds->location = create_location(latitude, longitude);
} }
} }

View file

@ -112,7 +112,7 @@ bool uploadDiveLogsDE::prepareDives(const QString &tempfile, bool selected)
if (ds) { if (ds) {
put_format(&mb, "<divelog><divesites><site uuid='%8x' name='", ds->uuid); put_format(&mb, "<divelog><divesites><site uuid='%8x' name='", ds->uuid);
put_quoted(&mb, ds->name, 1, 0); put_quoted(&mb, ds->name.c_str(), 1, 0);
put_format(&mb, "'"); put_format(&mb, "'");
put_location(&mb, &ds->location, " gps='", "'"); put_location(&mb, &ds->location, " gps='", "'");
put_format(&mb, ">\n"); put_format(&mb, ">\n");

View file

@ -61,7 +61,7 @@ static void writeMarkers(struct membuffer *b, bool selected_only)
put_HTML_watertemp(b, dive, " ", "</p>"); put_HTML_watertemp(b, dive, " ", "</p>");
pre = format_string_std("<p>%s <b>", translate("gettextFromC", "Location:")); pre = format_string_std("<p>%s <b>", translate("gettextFromC", "Location:"));
put_string(b, pre.c_str()); put_string(b, pre.c_str());
put_HTML_quoted(b, get_dive_location(dive)); put_HTML_quoted(b, get_dive_location(dive).c_str());
put_string(b, "</b></p>"); put_string(b, "</b></p>");
pre = format_string_std("<p> %s ", translate("gettextFromC", "Notes:")); pre = format_string_std("<p> %s ", translate("gettextFromC", "Notes:"));
put_HTML_notes(b, dive, pre.c_str(), " </p>"); put_HTML_notes(b, dive, pre.c_str(), " </p>");

View file

@ -68,7 +68,7 @@ void DivesiteImportDialog::on_ok_clicked()
for (int i = 0; i < importedSites.nr; i++) for (int i = 0; i < importedSites.nr; i++)
if (divesiteImportedModel->data(divesiteImportedModel->index(i, 0), Qt::CheckStateRole) == Qt::Checked) { if (divesiteImportedModel->data(divesiteImportedModel->index(i, 0), Qt::CheckStateRole) == Qt::Checked) {
struct dive_site *newSite = new dive_site; struct dive_site *newSite = new dive_site;
copy_dive_site(importedSites.dive_sites[i], newSite); *newSite = *importedSites.dive_sites[i];
add_dive_site_to_table(newSite, &selectedSites); add_dive_site_to_table(newSite, &selectedSites);
} }

View file

@ -129,8 +129,8 @@ void LocationInformationWidget::updateLabels()
clearLabels(); clearLabels();
return; return;
} }
if (diveSite->name) if (!diveSite->name.empty())
ui.diveSiteName->setText(diveSite->name); ui.diveSiteName->setText(QString::fromStdString(diveSite->name));
else else
ui.diveSiteName->clear(); ui.diveSiteName->clear();
std::string country = taxonomy_get_country(diveSite->taxonomy); std::string country = taxonomy_get_country(diveSite->taxonomy);
@ -138,12 +138,12 @@ void LocationInformationWidget::updateLabels()
ui.diveSiteCountry->setText(QString::fromStdString(country)); ui.diveSiteCountry->setText(QString::fromStdString(country));
else else
ui.diveSiteCountry->clear(); ui.diveSiteCountry->clear();
if (diveSite->description) if (!diveSite->description.empty())
ui.diveSiteDescription->setText(diveSite->description); ui.diveSiteDescription->setText(QString::fromStdString(diveSite->description));
else else
ui.diveSiteDescription->clear(); ui.diveSiteDescription->clear();
if (diveSite->notes) if (!diveSite->notes.empty())
ui.diveSiteNotes->setPlainText(diveSite->notes); ui.diveSiteNotes->setPlainText(QString::fromStdString(diveSite->notes));
else else
ui.diveSiteNotes->clear(); ui.diveSiteNotes->clear();
if (has_location(&diveSite->location)) if (has_location(&diveSite->location))
@ -172,13 +172,13 @@ void LocationInformationWidget::diveSiteChanged(struct dive_site *ds, int field)
return; // A different dive site was changed -> do nothing. return; // A different dive site was changed -> do nothing.
switch (field) { switch (field) {
case LocationInformationModel::NAME: case LocationInformationModel::NAME:
ui.diveSiteName->setText(diveSite->name); ui.diveSiteName->setText(QString::fromStdString(diveSite->name));
return; return;
case LocationInformationModel::DESCRIPTION: case LocationInformationModel::DESCRIPTION:
ui.diveSiteDescription->setText(diveSite->description); ui.diveSiteDescription->setText(QString::fromStdString(diveSite->description));
return; return;
case LocationInformationModel::NOTES: case LocationInformationModel::NOTES:
ui.diveSiteNotes->setText(diveSite->notes); ui.diveSiteNotes->setText(QString::fromStdString(diveSite->notes));
return; return;
case LocationInformationModel::TAXONOMY: case LocationInformationModel::TAXONOMY:
ui.diveSiteCountry->setText(QString::fromStdString(taxonomy_get_country(diveSite->taxonomy))); ui.diveSiteCountry->setText(QString::fromStdString(taxonomy_get_country(diveSite->taxonomy)));
@ -563,7 +563,7 @@ static struct dive_site *get_dive_site_name_start_which_str(const QString &str)
struct dive_site *ds; struct dive_site *ds;
int i; int i;
for_each_dive_site (i, ds, divelog.sites) { for_each_dive_site (i, ds, divelog.sites) {
QString dsName(ds->name); QString dsName = QString::fromStdString(ds->name);
if (dsName.toLower().startsWith(str.toLower())) if (dsName.toLower().startsWith(str.toLower()))
return ds; return ds;
} }
@ -585,10 +585,10 @@ void DiveLocationLineEdit::setTemporaryDiveSiteName(const QString &name)
// the user entered text. // the user entered text.
QString i1_name; QString i1_name;
if (struct dive_site *ds = get_dive_site_name_start_which_str(name)) { if (struct dive_site *ds = get_dive_site_name_start_which_str(name)) {
const QString orig_name = QString(ds->name).toLower(); const QString orig_name = QString::fromStdString(ds->name).toLower();
const QString new_name = name.toLower(); const QString new_name = name.toLower();
if (new_name != orig_name) if (new_name != orig_name)
i1_name = QString(ds->name); i1_name = QString::fromStdString(ds->name);
} }
model->setData(i1, i1_name); model->setData(i1, i1_name);
@ -674,7 +674,7 @@ void DiveLocationLineEdit::setCurrentDiveSite(struct dive *d)
if (!currDs) if (!currDs)
clear(); clear();
else else
setText(currDs->name); setText(QString::fromStdString(currDs->name));
proxy->setCurrentLocation(currentLocation); proxy->setCurrentLocation(currentLocation);
delegate.setCurrentLocation(currentLocation); delegate.setCurrentLocation(currentLocation);
} }

View file

@ -325,7 +325,7 @@ void DiveComponentSelection::buttonClicked(QAbstractButton *button)
QString cliptext; QString cliptext;
text.setString(&cliptext); text.setString(&cliptext);
if (what->divesite && current_dive->dive_site) if (what->divesite && current_dive->dive_site)
text << tr("Dive site: ") << current_dive->dive_site->name << "\n"; text << tr("Dive site: ") << QString::fromStdString(current_dive->dive_site->name) << "\n";
if (what->diveguide) if (what->diveguide)
text << tr("Dive guide: ") << current_dive->diveguide << "\n"; text << tr("Dive guide: ") << current_dive->diveguide << "\n";
if (what->buddy) if (what->buddy)

View file

@ -526,7 +526,7 @@ QVariant TemplateLayout::getValue(QString list, QString property, const State &s
} else if (property == "timestamp") { } else if (property == "timestamp") {
return QVariant::fromValue(d->when); return QVariant::fromValue(d->when);
} else if (property == "location") { } else if (property == "location") {
return get_dive_location(d); return QString::fromStdString(get_dive_location(d));
} else if (property == "gps") { } else if (property == "gps") {
return formatDiveGPS(d); return formatDiveGPS(d);
} else if (property == "gps_decimal") { } else if (property == "gps_decimal") {

View file

@ -1068,9 +1068,9 @@ bool QMLManager::checkLocation(DiveSiteChange &res, struct dive *d, QString loca
{ {
struct dive_site *ds = get_dive_site_for_dive(d); struct dive_site *ds = get_dive_site_for_dive(d);
bool changed = false; bool changed = false;
QString oldLocation = get_dive_location(d); QString oldLocation = QString::fromStdString(get_dive_location(d));
if (oldLocation != location) { if (oldLocation != location) {
ds = get_dive_site_by_name(qPrintable(location), divelog.sites); ds = get_dive_site_by_name(location.toStdString(), divelog.sites);
if (!ds && !location.isEmpty()) { if (!ds && !location.isEmpty()) {
res.createdDs = std::make_unique<dive_site>(qPrintable(location)); res.createdDs = std::make_unique<dive_site>(qPrintable(location));
res.changed = true; res.changed = true;
@ -1817,9 +1817,7 @@ QString QMLManager::getVersion() const
QString QMLManager::getGpsFromSiteName(const QString &siteName) QString QMLManager::getGpsFromSiteName(const QString &siteName)
{ {
struct dive_site *ds; struct dive_site *ds = get_dive_site_by_name(siteName.toStdString(), divelog.sites);
ds = get_dive_site_by_name(qPrintable(siteName), divelog.sites);
if (!ds) if (!ds)
return QString(); return QString();
return printGPSCoords(&ds->location); return printGPSCoords(&ds->location);

View file

@ -91,11 +91,11 @@ QVariant LocationInformationModel::getDiveSiteData(const struct dive_site *ds, i
case Qt::DisplayRole: case Qt::DisplayRole:
switch(column) { switch(column) {
case DIVESITE: return QVariant::fromValue<dive_site *>((dive_site *)ds); // Not nice: casting away const case DIVESITE: return QVariant::fromValue<dive_site *>((dive_site *)ds); // Not nice: casting away const
case NAME: return QString(ds->name); case NAME: return QString::fromStdString(ds->name);
case NUM_DIVES: return static_cast<int>(ds->dives.size()); case NUM_DIVES: return static_cast<int>(ds->dives.size());
case LOCATION: return "TODO"; case LOCATION: return "TODO";
case DESCRIPTION: return QString(ds->description); case DESCRIPTION: return QString::fromStdString(ds->description);
case NOTES: return QString(ds->name); case NOTES: return QString::fromStdString(ds->notes);
case TAXONOMY: return "TODO"; case TAXONOMY: return "TODO";
} }
break; break;
@ -184,7 +184,7 @@ bool DiveSiteSortedModel::filterAcceptsRow(int sourceRow, const QModelIndex &sou
if (sourceRow < 0 || sourceRow > divelog.sites->nr) if (sourceRow < 0 || sourceRow > divelog.sites->nr)
return false; return false;
struct dive_site *ds = divelog.sites->dive_sites[sourceRow]; struct dive_site *ds = divelog.sites->dive_sites[sourceRow];
QString text = QString(ds->name) + QString(ds->description) + QString(ds->notes); QString text = QString::fromStdString(ds->name + ds->description + ds->notes);
return text.contains(fullText, Qt::CaseInsensitive); return text.contains(fullText, Qt::CaseInsensitive);
} }
@ -200,18 +200,18 @@ bool DiveSiteSortedModel::lessThan(const QModelIndex &i1, const QModelIndex &i2)
switch (i1.column()) { switch (i1.column()) {
case LocationInformationModel::NAME: case LocationInformationModel::NAME:
default: default:
return QString::localeAwareCompare(QString(ds1->name), QString(ds2->name)) < 0; // TODO: avoid copy return QString::localeAwareCompare(QString::fromStdString(ds1->name), QString::fromStdString(ds2->name)) < 0; // TODO: avoid copy
case LocationInformationModel::DESCRIPTION: { case LocationInformationModel::DESCRIPTION: {
int cmp = QString::localeAwareCompare(QString(ds1->description), QString(ds2->description)); // TODO: avoid copy int cmp = QString::localeAwareCompare(QString::fromStdString(ds1->description), QString::fromStdString(ds2->description)); // TODO: avoid copy
return cmp != 0 ? cmp < 0 : return cmp != 0 ? cmp < 0 :
QString::localeAwareCompare(QString(ds1->name), QString(ds2->name)) < 0; // TODO: avoid copy QString::localeAwareCompare(QString::fromStdString(ds1->name), QString::fromStdString(ds2->name)) < 0; // TODO: avoid copy
} }
case LocationInformationModel::NUM_DIVES: { case LocationInformationModel::NUM_DIVES: {
int cmp = static_cast<int>(ds1->dives.size()) - static_cast<int>(ds2->dives.size()); int cmp = static_cast<int>(ds1->dives.size()) - static_cast<int>(ds2->dives.size());
// Since by default nr dives is descending, invert sort direction of names, such that // Since by default nr dives is descending, invert sort direction of names, such that
// the names are listed as ascending. // the names are listed as ascending.
return cmp != 0 ? cmp < 0 : return cmp != 0 ? cmp < 0 :
QString::localeAwareCompare(QString(ds1->name), QString(ds2->name)) < 0; // TODO: avoid copy QString::localeAwareCompare(QString::fromStdString(ds1->name), QString::fromStdString(ds2->name)) < 0; // TODO: avoid copy
} }
} }
} }
@ -234,7 +234,7 @@ QStringList DiveSiteSortedModel::allSiteNames() const
report_info("DiveSiteSortedModel::allSiteNames(): invalid index"); report_info("DiveSiteSortedModel::allSiteNames(): invalid index");
continue; continue;
} }
locationNames << QString(divelog.sites->dive_sites[idx]->name); locationNames << QString::fromStdString(divelog.sites->dive_sites[idx]->name);
} }
return locationNames; return locationNames;
} }

View file

@ -59,7 +59,7 @@ QVariant DivesiteImportedModel::data(const QModelIndex &index, int role) const
if (role == Qt::DisplayRole) { if (role == Qt::DisplayRole) {
switch (index.column()) { switch (index.column()) {
case NAME: case NAME:
return QString(ds->name); return QString::fromStdString(ds->name);
case LOCATION: case LOCATION:
return printGPSCoords(&ds->location); return printGPSCoords(&ds->location);
case COUNTRY: case COUNTRY:
@ -70,7 +70,7 @@ QVariant DivesiteImportedModel::data(const QModelIndex &index, int role) const
get_dive_site_by_gps_proximity(&ds->location, get_dive_site_by_gps_proximity(&ds->location,
40075000, divelog.sites); 40075000, divelog.sites);
if (nearest_ds) if (nearest_ds)
return QString(nearest_ds->name); return QString::fromStdString(nearest_ds->name);
else else
return QString(); return QString();
} }

View file

@ -280,7 +280,7 @@ QVariant DiveTripModelBase::diveData(const struct dive *d, int column, int role)
case MobileListModel::DateTimeRole: return formatDiveDateTime(d); case MobileListModel::DateTimeRole: return formatDiveDateTime(d);
case MobileListModel::IdRole: return d->id; case MobileListModel::IdRole: return d->id;
case MobileListModel::NumberRole: return d->number; case MobileListModel::NumberRole: return d->number;
case MobileListModel::LocationRole: return get_dive_location(d); case MobileListModel::LocationRole: return QString::fromStdString(get_dive_location(d));
case MobileListModel::DepthRole: return get_depth_string(d->dc.maxdepth.mm, true, true); case MobileListModel::DepthRole: return get_depth_string(d->dc.maxdepth.mm, true, true);
case MobileListModel::DurationRole: return formatDiveDuration(d); case MobileListModel::DurationRole: return formatDiveDuration(d);
case MobileListModel::DepthDurationRole: return QStringLiteral("%1 / %2").arg(get_depth_string(d->dc.maxdepth.mm, true, true), case MobileListModel::DepthDurationRole: return QStringLiteral("%1 / %2").arg(get_depth_string(d->dc.maxdepth.mm, true, true),
@ -357,7 +357,7 @@ QVariant DiveTripModelBase::diveData(const struct dive *d, int column, int role)
case DIVEGUIDE: case DIVEGUIDE:
return QString(d->diveguide); return QString(d->diveguide);
case LOCATION: case LOCATION:
return QString(get_dive_location(d)); return QString::fromStdString(get_dive_location(d));
case GAS: case GAS:
return formatDiveGasString(d); return formatDiveGasString(d);
case NOTES: case NOTES:
@ -1776,7 +1776,7 @@ bool DiveTripModelList::lessThan(const QModelIndex &i1, const QModelIndex &i2) c
case DIVEGUIDE: case DIVEGUIDE:
return lessThanHelper(strCmp(d1->diveguide, d2->diveguide), row_diff); return lessThanHelper(strCmp(d1->diveguide, d2->diveguide), row_diff);
case LOCATION: case LOCATION:
return lessThanHelper(strCmp(get_dive_location(d1), get_dive_location(d2)), row_diff); return lessThanHelper(strCmp(get_dive_location(d1).c_str(), get_dive_location(d2).c_str()), row_diff);
case NOTES: case NOTES:
return lessThanHelper(strCmp(d1->notes, d2->notes), row_diff); return lessThanHelper(strCmp(d1->notes, d2->notes), row_diff);
case DIVEMODE: case DIVEMODE:

View file

@ -17,10 +17,10 @@
// Example: // Example:
// Japan/Izu Peninsula/Atami/Chinsen-Aft // Japan/Izu Peninsula/Atami/Chinsen-Aft
// Short name: Chinsen-Aft // Short name: Chinsen-Aft
static QString siteMapDisplayName(const char *sitename) static QString siteMapDisplayName(const std::string &sitename)
{ {
const char Separator = '/'; const char Separator = '/';
QString fullname(sitename); QString fullname = QString::fromStdString(sitename);
if (!qPrefDisplay::map_short_names() ) if (!qPrefDisplay::map_short_names() )
return fullname; return fullname;

View file

@ -347,7 +347,7 @@ static void smtk_wreck_site(MdbHandle *mdb, char *site_idx, struct dive_site *ds
break; break;
} }
} }
concat(&ds->notes, "\n", notes); concat(ds->notes, "\n", notes);
break; break;
} }
} }
@ -423,18 +423,17 @@ static void smtk_build_location(MdbHandle *mdb, char *idx, struct dive_site **lo
concat(str, ", ", table.get_string_view(1)); // Locality concat(str, ", ", table.get_string_view(1)); // Locality
concat(str, ", ", site); concat(str, ", ", site);
ds = get_dive_site_by_name(str.c_str(), log->sites); ds = get_dive_site_by_name(str, log->sites);
if (!ds) { if (!ds) {
if (!has_location(&loc)) if (!has_location(&loc))
ds = create_dive_site(str.c_str(), log->sites); ds = create_dive_site(str, log->sites);
else else
ds = create_dive_site_with_gps(str.c_str(), &loc, log->sites); ds = create_dive_site_with_gps(str, &loc, log->sites);
} }
*location = ds; *location = ds;
/* Insert site notes */ /* Insert site notes */
free(ds->notes); ds->notes = notes.c_str();
ds->notes = strdup(notes.c_str());
/* Check if we have a wreck */ /* Check if we have a wreck */
smtk_wreck_site(mdb, idx, ds); smtk_wreck_site(mdb, idx, ds);

View file

@ -47,7 +47,7 @@ struct DiveSiteWrapper {
const dive_site *ds; const dive_site *ds;
QString name; QString name;
DiveSiteWrapper(const dive_site *ds) : ds(ds), DiveSiteWrapper(const dive_site *ds) : ds(ds),
name(ds ? ds->name : "") name(ds ? QString::fromStdString(ds->name) : QString())
{ {
} }
bool operator<(const DiveSiteWrapper &d2) const { bool operator<(const DiveSiteWrapper &d2) const {

View file

@ -93,7 +93,7 @@ int TestParse::parseDivingLog()
{ {
// Parsing of DivingLog import from SQLite database // Parsing of DivingLog import from SQLite database
struct dive_site *ds = alloc_or_get_dive_site(0xdeadbeef, divelog.sites); struct dive_site *ds = alloc_or_get_dive_site(0xdeadbeef, divelog.sites);
ds->name = copy_string("Suomi - - Hälvälä"); ds->name = "Suomi - - Hälvälä";
int ret = sqlite3_open(SUBSURFACE_TEST_DATA "/dives/TestDivingLog4.1.1.sql", &_sqlite3_handle); int ret = sqlite3_open(SUBSURFACE_TEST_DATA "/dives/TestDivingLog4.1.1.sql", &_sqlite3_handle);
if (ret == 0) if (ret == 0)