From b9a2eff3c9dfea85e66976530f5b6b50fc8b0775 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 18 May 2024 17:03:19 +0200 Subject: [PATCH] core: turn string data in struct divecomputer into std::string Simplifies memory management. Signed-off-by: Berthold Stoeger --- backend-shared/exportfuncs.cpp | 2 +- commands/command_device.cpp | 2 +- core/device.cpp | 28 ++++++++++++++-------------- core/device.h | 10 +++++----- core/dive.cpp | 22 ++++++++-------------- core/divecomputer.cpp | 29 ++++++++--------------------- core/divecomputer.h | 2 +- core/divelist.cpp | 2 +- core/import-cobalt.cpp | 4 ++-- core/import-csv.cpp | 2 +- core/import-divinglog.cpp | 8 ++++---- core/import-seac.cpp | 7 ++----- core/import-shearwater.cpp | 24 ++++++++++++------------ core/import-suunto.cpp | 4 +--- core/libdivecomputer.cpp | 10 +++++----- core/liquivision.cpp | 8 ++++---- core/load-git.cpp | 4 ++-- core/ostctools.cpp | 12 ++++++------ core/parse-xml.cpp | 6 +++--- core/parse.cpp | 8 ++++---- core/save-git.cpp | 24 +++++++++++------------- core/save-html.cpp | 2 +- core/save-xml.cpp | 26 +++++++++----------------- core/uemis-downloader.cpp | 4 ++-- core/uemis.cpp | 2 +- profile-widget/profilescene.cpp | 2 +- profile-widget/profilewidget2.cpp | 5 +++-- smtk-import/smartrak.cpp | 2 +- 28 files changed, 114 insertions(+), 147 deletions(-) diff --git a/backend-shared/exportfuncs.cpp b/backend-shared/exportfuncs.cpp index 5cb3d2be0..29a9452bf 100644 --- a/backend-shared/exportfuncs.cpp +++ b/backend-shared/exportfuncs.cpp @@ -179,7 +179,7 @@ void export_TeX(const char *filename, bool selected_only, bool plain, ExportCall 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\\%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.c_str()); put_format(&buf, "\\def\\%scountry{%s}\n", ssrf, country.c_str()); put_format(&buf, "\\def\\%stime{%u:%02u}\n", ssrf, FRACTION_TUPLE(dive->duration.seconds, 60)); diff --git a/commands/command_device.cpp b/commands/command_device.cpp index cef91c5d2..13b4ebc7a 100644 --- a/commands/command_device.cpp +++ b/commands/command_device.cpp @@ -13,7 +13,7 @@ EditDeviceNickname::EditDeviceNickname(const struct divecomputer *dc, const QStr if (index == -1) return; - setText(Command::Base::tr("Set nickname of device %1 (serial %2) to %3").arg(dc->model, dc->serial, nicknameIn)); + setText(Command::Base::tr("Set nickname of device %1 (serial %2) to %3").arg(dc->model.c_str(), dc->serial.c_str(), nicknameIn)); } bool EditDeviceNickname::workToBeDone() diff --git a/core/device.cpp b/core/device.cpp index 4eb3a2acc..4b4362794 100644 --- a/core/device.cpp +++ b/core/device.cpp @@ -29,7 +29,7 @@ bool device::operator<(const device &a) const const struct device *get_device_for_dc(const struct device_table *table, const struct divecomputer *dc) { - if (!dc->model || !dc->serial) + if (dc->model.empty() || dc->serial.empty()) return NULL; const std::vector &dcs = table->devices; @@ -40,14 +40,14 @@ const struct device *get_device_for_dc(const struct device_table *table, const s int get_or_add_device_for_dc(struct device_table *table, const struct divecomputer *dc) { - if (!dc->model || !dc->serial) + if (dc->model.empty() || dc->serial.empty()) return -1; const struct device *dev = get_device_for_dc(table, dc); if (dev) { auto it = std::lower_bound(table->devices.begin(), table->devices.end(), *dev); return it - table->devices.begin(); } - return create_device_node(table, dc->model, dc->serial, ""); + return create_device_node(table, dc->model, dc->serial, std::string()); } bool device_exists(const struct device_table *device_table, const struct device *dev) @@ -86,14 +86,14 @@ static int addDC(std::vector &dcs, const std::string &m, const std::stri } } -int create_device_node(struct device_table *device_table, const char *model, const char *serial, const char *nickname) +int create_device_node(struct device_table *device_table, const std::string &model, const std::string &serial, const std::string &nickname) { - return addDC(device_table->devices, model ?: "", serial ?: "", nickname ?: ""); + return addDC(device_table->devices, model, serial, nickname); } int add_to_device_table(struct device_table *device_table, const struct device *dev) { - return create_device_node(device_table, dev->model.c_str(), dev->serialNumber.c_str(), dev->nickName.c_str()); + return create_device_node(device_table, dev->model, dev->serialNumber, dev->nickName); } int remove_device(struct device_table *device_table, const struct device *dev) @@ -138,12 +138,12 @@ int is_default_dive_computer_device(const char *name) return qPrefDiveComputer::device() == name; } -const char *get_dc_nickname(const struct divecomputer *dc) +std::string get_dc_nickname(const struct divecomputer *dc) { const device *existNode = get_device_for_dc(divelog.devices.get(), dc); if (existNode && !existNode->nickName.empty()) - return existNode->nickName.c_str(); + return existNode->nickName; else return dc->model; } @@ -167,19 +167,19 @@ struct device *get_device_mutable(struct device_table *table, int i) return &table->devices[i]; } -const char *device_get_model(const struct device *dev) +std::string device_get_model(const struct device *dev) { - return dev ? dev->model.c_str() : NULL; + return dev ? dev->model : std::string(); } -const char *device_get_serial(const struct device *dev) +std::string device_get_serial(const struct device *dev) { - return dev ? dev->serialNumber.c_str() : NULL; + return dev ? dev->serialNumber : std::string(); } -const char *device_get_nickname(const struct device *dev) +std::string device_get_nickname(const struct device *dev) { - return dev ? dev->nickName.c_str() : NULL; + return dev ? dev->nickName : std::string(); } struct device_table *alloc_device_table() diff --git a/core/device.h b/core/device.h index 5c9f042b1..d155588d0 100644 --- a/core/device.h +++ b/core/device.h @@ -14,12 +14,12 @@ struct dive_table; // global device table extern struct fingerprint_table fingerprint_table; -extern int create_device_node(struct device_table *table, const char *model, const char *serial, const char *nickname); +extern int create_device_node(struct device_table *table, const std::string &model, const std::string &serial, const std::string &nickname); extern int nr_devices(const struct device_table *table); extern const struct device *get_device(const struct device_table *table, int i); extern struct device *get_device_mutable(struct device_table *table, int i); extern void clear_device_table(struct device_table *table); -const char *get_dc_nickname(const struct divecomputer *dc); +std::string get_dc_nickname(const struct divecomputer *dc); extern bool device_used_by_selected_dive(const struct device *dev); extern const struct device *get_device_for_dc(const struct device_table *table, const struct divecomputer *dc); @@ -30,9 +30,9 @@ extern int remove_device(struct device_table *table, const struct device *dev); extern void remove_from_device_table(struct device_table *table, int idx); // struct device accessors for C-code. The returned strings are not stable! -const char *device_get_model(const struct device *dev); -const char *device_get_serial(const struct device *dev); -const char *device_get_nickname(const struct device *dev); +std::string device_get_model(const struct device *dev); +std::string device_get_serial(const struct device *dev); +std::string device_get_nickname(const struct device *dev); // for C code that needs to alloc/free a device table. (Let's try to get rid of those) extern struct device_table *alloc_device_table(); diff --git a/core/dive.cpp b/core/dive.cpp index a91d0d455..7304b5eff 100644 --- a/core/dive.cpp +++ b/core/dive.cpp @@ -181,14 +181,11 @@ static void copy_extra_data(struct extra_data *sed, struct extra_data *ded) ded->value = copy_string(sed->value); } -/* this is very different from the copy_divecomputer later in this file; +/* this is very different from the copy_dive_computer later in this file; * this function actually makes full copies of the content */ static void copy_dc(const struct divecomputer *sdc, struct divecomputer *ddc) { *ddc = *sdc; - ddc->model = copy_string(sdc->model); - ddc->serial = copy_string(sdc->serial); - ddc->fw_version = copy_string(sdc->fw_version); copy_samples(sdc, ddc); copy_events(sdc, ddc); STRUCTURED_LIST_COPY(struct extra_data, sdc->extra_data, ddc->extra_data, copy_extra_data); @@ -616,9 +613,9 @@ void update_setpoint_events(const struct dive *dive, struct divecomputer *dc) new_setpoint = prefs.defaultsetpoint; if (dc->divemode == OC && - (same_string(dc->model, "Shearwater Predator") || - same_string(dc->model, "Shearwater Petrel") || - same_string(dc->model, "Shearwater Nerd"))) { + (dc->model == "Shearwater Predator" || + dc->model == "Shearwater Petrel" || + dc->model == "Shearwater Nerd")) { // make sure there's no setpoint in the samples // this is an irreversible change - so switching a dive to OC // by mistake when it's actually CCR is _bad_ @@ -2399,11 +2396,11 @@ static int same_dc(struct divecomputer *a, struct divecomputer *b) static int might_be_same_device(const struct divecomputer *a, const struct divecomputer *b) { /* No dive computer model? That matches anything */ - if (!a->model || !b->model) + if (a->model.empty() || b->model.empty()) return 1; /* Otherwise at least the model names have to match */ - if (strcasecmp(a->model, b->model)) + if (strcasecmp(a->model.c_str(), b->model.c_str())) return 0; /* No device ID? Match */ @@ -2452,9 +2449,6 @@ static const struct divecomputer *find_matching_computer(const struct divecomput static void copy_dive_computer(struct divecomputer *res, const struct divecomputer *a) { *res = *a; - res->model = copy_string(a->model); - res->serial = copy_string(a->serial); - res->fw_version = copy_string(a->fw_version); STRUCTURED_LIST_COPY(struct extra_data, a->extra_data, res->extra_data, copy_extra_data); res->samples = res->alloc_samples = 0; res->sample = NULL; @@ -2520,11 +2514,11 @@ static void join_dive_computers(struct dive *d, struct divecomputer *res, { struct divecomputer *tmp; - if (a->model && !b->model) { + if (!a->model.empty() && b->model.empty()) { copy_dc_renumber(d, a, res, cylinders_map_a); return; } - if (b->model && !a->model) { + if (!b->model.empty() && a->model.empty()) { copy_dc_renumber(d, b, res, cylinders_map_b); return; } diff --git a/core/divecomputer.cpp b/core/divecomputer.cpp index 204162f75..e42f75654 100644 --- a/core/divecomputer.cpp +++ b/core/divecomputer.cpp @@ -489,10 +489,10 @@ void add_extra_data(struct divecomputer *dc, const char *key, const char *value) if (!strcasecmp(key, "Serial")) { dc->deviceid = calculate_string_hash(value); - dc->serial = strdup(value); + dc->serial = value; } if (!strcmp(key, "FW Version")) { - dc->fw_version = strdup(value); + dc->fw_version = value; } while (*ed) @@ -505,14 +505,6 @@ void add_extra_data(struct divecomputer *dc, const char *key, const char *value) } } -<<<<<<< HEAD -======= -bool is_dc_planner(const struct divecomputer *dc) -{ - return same_string(dc->model, "planned dive"); -} - ->>>>>>> ed6cdf19f (build: remove extern "C" linkage) /* * Match two dive computer entries against each other, and * tell if it's the same dive. Return 0 if "don't know", @@ -522,9 +514,9 @@ bool is_dc_planner(const struct divecomputer *dc) int match_one_dc(const struct divecomputer *a, const struct divecomputer *b) { /* Not same model? Don't know if matching.. */ - if (!a->model || !b->model) + if (a->model.empty() || b->model.empty()) return 0; - if (strcasecmp(a->model, b->model)) + if (strcasecmp(a->model.c_str(), b->model.c_str())) return 0; /* Different device ID's? Don't know */ @@ -551,9 +543,6 @@ static void free_extra_data(struct extra_data *ed) void free_dc_contents(struct divecomputer *dc) { free(dc->sample); - free((void *)dc->model); - free((void *)dc->serial); - free((void *)dc->fw_version); free_events(dc->events); STRUCTURED_LIST_FREE(struct extra_data, dc->extra_data, free_extra_data); } @@ -562,24 +551,22 @@ static const char *planner_dc_name = "planned dive"; bool is_dc_planner(const struct divecomputer *dc) { - return same_string(dc->model, planner_dc_name); + return dc->model == planner_dc_name; } void make_planner_dc(struct divecomputer *dc) { - free((void *)dc->model); - dc->model = strdup(planner_dc_name); + dc->model = planner_dc_name; } const char *manual_dc_name = "manually added dive"; bool is_dc_manually_added_dive(const struct divecomputer *dc) { - return dc && same_string(dc->model, manual_dc_name); + return dc->model == manual_dc_name; } void make_manually_added_dive_dc(struct divecomputer *dc) { - free((void *)dc->model); - dc->model = strdup(manual_dc_name); + dc->model = manual_dc_name; } diff --git a/core/divecomputer.h b/core/divecomputer.h index 9a08d9a88..eb1c914bc 100644 --- a/core/divecomputer.h +++ b/core/divecomputer.h @@ -34,7 +34,7 @@ struct divecomputer { enum divemode_t divemode = OC; // dive computer type: OC(default) or CCR uint8_t no_o2sensors = 0; // rebreathers: number of O2 sensors used int salinity = 0; // kg per 10000 l - const char *model = nullptr, *serial = nullptr, *fw_version = nullptr; + std::string model, serial, fw_version; uint32_t deviceid = 0, diveid = 0; int samples = 0, alloc_samples = 0; struct sample *sample = nullptr; diff --git a/core/divelist.cpp b/core/divelist.cpp index a48f83663..a76ed0f99 100644 --- a/core/divelist.cpp +++ b/core/divelist.cpp @@ -631,7 +631,7 @@ static int comp_dc(const struct divecomputer *dc1, const struct divecomputer *dc return -1; if (!dc2) return 1; - if ((cmp = safe_strcmp(dc1->model, dc2->model)) != 0) + if ((cmp = dc1->model.compare(dc2->model)) != 0) return cmp; dc1 = dc1->next; dc2 = dc2->next; diff --git a/core/import-cobalt.cpp b/core/import-cobalt.cpp index 98954209f..bae68b9e9 100644 --- a/core/import-cobalt.cpp +++ b/core/import-cobalt.cpp @@ -133,7 +133,7 @@ static int cobalt_dive(void *param, int, char **data, char **) if (data[9]) { utf8_string_std(data[9], &state->cur_settings.dc.serial_nr); state->cur_settings.dc.deviceid = atoi(data[9]); - state->cur_settings.dc.model = strdup("Cobalt import"); + state->cur_settings.dc.model = "Cobalt import"; } dc_settings_end(state); @@ -141,7 +141,7 @@ static int cobalt_dive(void *param, int, char **data, char **) if (data[9]) { state->cur_dive->dc.deviceid = atoi(data[9]); - state->cur_dive->dc.model = strdup("Cobalt import"); + state->cur_dive->dc.model = "Cobalt import"; } snprintf(get_buffer, sizeof(get_buffer) - 1, get_cylinder_template, state->cur_dive->number); diff --git a/core/import-csv.cpp b/core/import-csv.cpp index 5b30c5fc2..6c8208fb5 100644 --- a/core/import-csv.cpp +++ b/core/import-csv.cpp @@ -512,7 +512,7 @@ int parse_txt_file(const char *filename, const char *csv, struct divelog *log) auto dive = std::make_unique(); dive->when = utc_mktime(&cur_tm);; - dive->dc.model = strdup("Poseidon MkVI Discovery"); + dive->dc.model = "Poseidon MkVI Discovery"; value = parse_mkvi_value(memtxt.data(), "Rig Serial number"); dive->dc.deviceid = atoi(value.c_str()); dive->dc.divemode = CCR; diff --git a/core/import-divinglog.cpp b/core/import-divinglog.cpp index a286cde84..224bb7ef8 100644 --- a/core/import-divinglog.cpp +++ b/core/import-divinglog.cpp @@ -330,9 +330,9 @@ static int divinglog_dive(void *param, int, char **data, char **) dc_settings_start(state); if (data[12]) { - state->cur_dive->dc.model = strdup(data[12]); + state->cur_dive->dc.model = data[12]; } else { - state->cur_settings.dc.model = strdup("Divinglog import"); + state->cur_settings.dc.model = "Divinglog import"; } snprintf(get_buffer, sizeof(get_buffer) - 1, get_cylinder0_template, diveid); @@ -367,9 +367,9 @@ static int divinglog_dive(void *param, int, char **data, char **) settings_end(state); if (data[12]) { - state->cur_dive->dc.model = strdup(data[12]); + state->cur_dive->dc.model = data[12]; } else { - state->cur_dive->dc.model = strdup("Divinglog import"); + state->cur_dive->dc.model = "Divinglog import"; } snprintf(get_buffer, sizeof(get_buffer) - 1, get_profile_template, diveid); diff --git a/core/import-seac.cpp b/core/import-seac.cpp index 451c2dfa6..d681f0b5e 100644 --- a/core/import-seac.cpp +++ b/core/import-seac.cpp @@ -205,10 +205,8 @@ static int seac_dive(void *param, int, char **data, char **) settings_start(state); dc_settings_start(state); - // These dc values are const char *, therefore we have to cast. - // Will be fixed by converting to std::string - utf8_string(data[1], (char **)&state->cur_dive->dc.serial); - utf8_string(data[12], (char **)&state->cur_dive->dc.fw_version); + utf8_string_std(data[1], &state->cur_dive->dc.serial); + utf8_string_std(data[12],&state->cur_dive->dc.fw_version); state->cur_dive->dc.model = strdup("Seac Action"); state->cur_dive->dc.deviceid = calculate_string_hash(data[1]); @@ -225,7 +223,6 @@ static int seac_dive(void *param, int, char **data, char **) curcyl->gasmix.o2.permille = 10 * sqlite3_column_int(sqlstmt, 4); - // Track gasses to tell when switch occurs lastgas = curcyl->gasmix; curgas = curcyl->gasmix; diff --git a/core/import-shearwater.cpp b/core/import-shearwater.cpp index 898991bbd..96f47300e 100644 --- a/core/import-shearwater.cpp +++ b/core/import-shearwater.cpp @@ -266,13 +266,13 @@ static int shearwater_dive(void *param, int, char **data, char **) if (data[10]) { switch (atoi(data[10])) { case 2: - state->cur_settings.dc.model = strdup("Shearwater Petrel/Perdix"); + state->cur_settings.dc.model = "Shearwater Petrel/Perdix"; break; case 4: - state->cur_settings.dc.model = strdup("Shearwater Predator"); + state->cur_settings.dc.model = "Shearwater Predator"; break; default: - state->cur_settings.dc.model = strdup("Shearwater import"); + state->cur_settings.dc.model = "Shearwater import"; break; } } @@ -285,13 +285,13 @@ static int shearwater_dive(void *param, int, char **data, char **) if (data[10]) { switch (atoi(data[10])) { case 2: - state->cur_dive->dc.model = strdup("Shearwater Petrel/Perdix"); + state->cur_dive->dc.model = "Shearwater Petrel/Perdix"; break; case 4: - state->cur_dive->dc.model = strdup("Shearwater Predator"); + state->cur_dive->dc.model = "Shearwater Predator"; break; default: - state->cur_dive->dc.model = strdup("Shearwater import"); + state->cur_dive->dc.model = "Shearwater import"; break; } } @@ -396,13 +396,13 @@ static int shearwater_cloud_dive(void *param, int, char **data, char **) if (data[10]) { switch (atoi(data[10])) { case 2: - state->cur_settings.dc.model = strdup("Shearwater Petrel/Perdix"); + state->cur_settings.dc.model = "Shearwater Petrel/Perdix"; break; case 4: - state->cur_settings.dc.model = strdup("Shearwater Predator"); + state->cur_settings.dc.model = "Shearwater Predator"; break; default: - state->cur_settings.dc.model = strdup("Shearwater import"); + state->cur_settings.dc.model = "Shearwater import"; break; } } @@ -415,13 +415,13 @@ static int shearwater_cloud_dive(void *param, int, char **data, char **) if (data[10]) { switch (atoi(data[10])) { case 2: - state->cur_dive->dc.model = strdup("Shearwater Petrel/Perdix"); + state->cur_dive->dc.model = "Shearwater Petrel/Perdix"; break; case 4: - state->cur_dive->dc.model = strdup("Shearwater Predator"); + state->cur_dive->dc.model = "Shearwater Predator"; break; default: - state->cur_dive->dc.model = strdup("Shearwater import"); + state->cur_dive->dc.model = "Shearwater import"; break; } } diff --git a/core/import-suunto.cpp b/core/import-suunto.cpp index 80aa137c7..1dc23a3a1 100644 --- a/core/import-suunto.cpp +++ b/core/import-suunto.cpp @@ -399,10 +399,8 @@ static int dm5_dive(void *param, int, char **data, char **) if (data[4]) { state->cur_dive->dc.deviceid = atoi(data[4]); } - // Ugh. dc.model is const char * -> we are not supposed to write into it. This will - // change when we convert to std::string. if (data[5]) - utf8_string(data[5], (char **)&state->cur_dive->dc.model); + utf8_string_std(data[5], &state->cur_dive->dc.model); if (data[25]) { switch(atoi(data[25])) { diff --git a/core/libdivecomputer.cpp b/core/libdivecomputer.cpp index 3647fa253..2bf09147e 100644 --- a/core/libdivecomputer.cpp +++ b/core/libdivecomputer.cpp @@ -436,7 +436,7 @@ sample_cb(dc_sample_type_t type, const dc_sample_value_t *pvalue, void *userdata handle_event(dc, sample, value); break; case DC_SAMPLE_RBT: - sample->rbt.seconds = (!strncasecmp(dc->model, "suunto", 6)) ? value.rbt : value.rbt * 60; + sample->rbt.seconds = (!strncasecmp(dc->model.c_str(), "suunto", 6)) ? value.rbt : value.rbt * 60; break; #ifdef DC_SAMPLE_TTS case DC_SAMPLE_TTS: @@ -534,9 +534,9 @@ static dc_status_t parse_samples(device_data_t *, struct divecomputer *dc, dc_pa static int might_be_same_dc(struct divecomputer *a, struct divecomputer *b) { - if (!a->model || !b->model) + if (a->model.empty() || b->model.empty()) return 1; - if (strcasecmp(a->model, b->model)) + if (strcasecmp(a->model.c_str(), b->model.c_str())) return 0; if (!a->deviceid || !b->deviceid) return 1; @@ -827,7 +827,7 @@ static int dive_cb(const unsigned char *data, unsigned int size, auto dive = std::make_unique(); // Fill in basic fields - dive->dc.model = strdup(devdata->model.c_str()); + dive->dc.model = devdata->model; dive->dc.diveid = calculate_diveid(fingerprint, fsize); // Parse the dive's header data @@ -878,7 +878,7 @@ static int dive_cb(const unsigned char *data, unsigned int size, /* special case for bug in Tecdiving DiveComputer.eu * often the first sample has a water temperature of 0C, followed by the correct * temperature in the next sample */ - if (same_string(dive->dc.model, "Tecdiving DiveComputer.eu") && + if (dive->dc.model == "Tecdiving DiveComputer.eu" && dive->dc.sample[0].temperature.mkelvin == ZERO_C_IN_MKELVIN && dive->dc.sample[1].temperature.mkelvin > dive->dc.sample[0].temperature.mkelvin) dive->dc.sample[0].temperature.mkelvin = dive->dc.sample[1].temperature.mkelvin; diff --git a/core/liquivision.cpp b/core/liquivision.cpp index 4cdc3b901..a8d482548 100644 --- a/core/liquivision.cpp +++ b/core/liquivision.cpp @@ -156,17 +156,17 @@ static void parse_dives(int log_version, const unsigned char *buf, unsigned int model = *(buf + ptr); switch (model) { case 0: - dc->model = strdup("Xen"); + dc->model = "Xen"; break; case 1: case 2: - dc->model = strdup("Xeo"); + dc->model = "Xeo"; break; case 4: - dc->model = strdup("Lynx"); + dc->model = "Lynx"; break; default: - dc->model = strdup("Liquivision"); + dc->model = "Liquivision"; break; } ptr++; diff --git a/core/load-git.cpp b/core/load-git.cpp index 4ec309a30..a1702f02e 100644 --- a/core/load-git.cpp +++ b/core/load-git.cpp @@ -754,7 +754,7 @@ static void parse_dc_meandepth(char *line, struct git_parser_state *state) { state->active_dc->meandepth = get_depth(line); } static void parse_dc_model(char *, struct git_parser_state *state) -{ state->active_dc->model = get_first_converted_string_c(state); } +{ state->active_dc->model = get_first_converted_string(state); } static void parse_dc_numberofoxygensensors(char *line, struct git_parser_state *state) { state->active_dc->no_o2sensors = get_index(line); } @@ -1649,7 +1649,7 @@ static struct divecomputer *create_new_dc(struct dive *dive) while (dc->next) dc = dc->next; /* Did we already fill that in? */ - if (dc->samples || dc->model || dc->when) { + if (dc->samples || !dc->model.empty() || dc->when) { struct divecomputer *newdc = new divecomputer; dc->next = newdc; dc = newdc; diff --git a/core/ostctools.cpp b/core/ostctools.cpp index 2cdf1845b..4355c0e82 100644 --- a/core/ostctools.cpp +++ b/core/ostctools.cpp @@ -28,8 +28,8 @@ static int ostc_prepare_data(int data_model, dc_family_t dc_fam, device_data_t & data_descriptor = get_descriptor(dc_fam, data_model); if (data_descriptor) { dev_data.descriptor = data_descriptor; - dev_data.vendor = copy_string(dc_descriptor_get_vendor(data_descriptor)); - dev_data.model = copy_string(dc_descriptor_get_product(data_descriptor)); + dev_data.vendor = dc_descriptor_get_vendor(data_descriptor); + dev_data.model = dc_descriptor_get_product(data_descriptor); } else { return 0; } @@ -153,7 +153,7 @@ void ostctools_import(const char *file, struct divelog *log) return; } std::string tmp = devdata.vendor + " " + devdata.model + " (Imported from OSTCTools)"; - ostcdive->dc.model = copy_string(tmp.c_str()); + ostcdive->dc.model = tmp.c_str(); // Parse the dive data rc = libdc_buffer_parser(ostcdive.get(), &devdata, buffer.data(), i + 1); @@ -163,18 +163,18 @@ void ostctools_import(const char *file, struct divelog *log) // Serial number is not part of the header nor the profile, so libdc won't // catch it. If Serial is part of the extra_data, and set to zero, remove // it from the list and add again. - ostcdive->dc.serial = copy_string(std::to_string(serial).c_str()); + ostcdive->dc.serial = std::to_string(serial); if (ostcdive->dc.extra_data) { ptr = ostcdive->dc.extra_data; while (strcmp(ptr->key, "Serial")) ptr = ptr->next; if (!strcmp(ptr->value, "0")) { - add_extra_data(&ostcdive->dc, "Serial", ostcdive->dc.serial); + add_extra_data(&ostcdive->dc, "Serial", ostcdive->dc.serial.c_str()); *ptr = *(ptr)->next; } } else { - add_extra_data(&ostcdive->dc, "Serial", ostcdive->dc.serial); + add_extra_data(&ostcdive->dc, "Serial", ostcdive->dc.serial.c_str()); } record_dive_to_table(ostcdive.release(), log->dives.get()); sort_dive_table(log->dives.get()); diff --git a/core/parse-xml.cpp b/core/parse-xml.cpp index 304f4be62..9e84bc1ba 100644 --- a/core/parse-xml.cpp +++ b/core/parse-xml.cpp @@ -835,7 +835,7 @@ static void try_to_fill_dc(struct divecomputer *dc, const char *name, char *buf, return; if (MATCH_STATE("time", divetime, &dc->when)) return; - if (MATCH("model", utf8_string, (char **)&dc->model)) + if (MATCH("model", utf8_string_std, &dc->model)) return; if (MATCH("deviceid", hex_value, &deviceid)) return; @@ -1821,10 +1821,10 @@ int parse_dlf_buffer(unsigned char *buffer, size_t size, struct divelog *log) dive_start(&state); divecomputer_start(&state); - state.cur_dc->model = strdup("DLF import"); + state.cur_dc->model = "DLF import"; // (ptr[7] << 8) + ptr[6] Is "Serial" snprintf(serial, sizeof(serial), "%d", (ptr[7] << 8) + ptr[6]); - state.cur_dc->serial = strdup(serial); + state.cur_dc->serial = serial; state.cur_dc->when = parse_dlf_timestamp(ptr + 8); state.cur_dive->when = state.cur_dc->when; diff --git a/core/parse.cpp b/core/parse.cpp index a1c1a0f78..d3cc16e50 100644 --- a/core/parse.cpp +++ b/core/parse.cpp @@ -178,9 +178,9 @@ void dc_settings_start(struct parser_state *state) void dc_settings_end(struct parser_state *state) { create_device_node(state->log->devices.get(), - state->cur_settings.dc.model.c_str(), - state->cur_settings.dc.serial_nr.c_str(), - state->cur_settings.dc.nickname.c_str()); + state->cur_settings.dc.model, + state->cur_settings.dc.serial_nr, + state->cur_settings.dc.nickname); reset_dc_settings(state); } @@ -392,7 +392,7 @@ void divecomputer_start(struct parser_state *state) dc = dc->next; /* Did we already fill that in? */ - if (dc->samples || dc->model || dc->when) { + if (dc->samples || !dc->model.empty() || dc->when) { struct divecomputer *newdc = new divecomputer; if (newdc) { dc->next = newdc; diff --git a/core/save-git.cpp b/core/save-git.cpp index 64a033631..d8d85ef0b 100644 --- a/core/save-git.cpp +++ b/core/save-git.cpp @@ -420,7 +420,7 @@ static void save_events(struct membuffer *b, struct dive *dive, struct event *ev static void save_dc(struct membuffer *b, struct dive *dive, struct divecomputer *dc) { - show_utf8(b, "model ", dc->model, "\n"); + show_utf8(b, "model ", dc->model.c_str(), "\n"); if (dc->last_manual_time.seconds) put_duration(b, dc->last_manual_time, "lastmanualtime ", "min\n"); if (dc->deviceid) @@ -862,19 +862,17 @@ static void save_units(void *_b) static void save_one_device(struct membuffer *b, const struct device *d) { - const char *model = device_get_model(d); - const char *nickname = device_get_nickname(d); - const char *serial = device_get_serial(d); + std::string model = device_get_model(d); + std::string nickname = device_get_nickname(d); + std::string serial = device_get_serial(d); - if (empty_string(serial)) serial = NULL; - if (empty_string(nickname)) nickname = NULL; - if (!nickname || !serial) + if (nickname.empty() || serial.empty()) return; - show_utf8(b, "divecomputerid ", model, ""); - put_format(b, " deviceid=%08x", calculate_string_hash(serial)); - show_utf8(b, " serial=", serial, ""); - show_utf8(b, " nickname=", nickname, ""); + show_utf8(b, "divecomputerid ", model.c_str(), ""); + put_format(b, " deviceid=%08x", calculate_string_hash(serial.c_str())); + show_utf8(b, " serial=", serial.c_str(), ""); + show_utf8(b, " nickname=", nickname.c_str(), ""); put_string(b, "\n"); } @@ -1154,8 +1152,8 @@ static void create_commit_message(struct membuffer *msg, bool create_empty) put_format(msg, " (%s)", trip->location); put_format(msg, "\n"); do { - if (!empty_string(dc->model)) { - put_format(msg, "%s%s", sep, dc->model); + if (!dc->model.empty()) { + put_format(msg, "%s%s", sep, dc->model.c_str()); sep = ", "; } } while ((dc = dc->next) != NULL); diff --git a/core/save-html.cpp b/core/save-html.cpp index b3625cdc9..36d91dde1 100644 --- a/core/save-html.cpp +++ b/core/save-html.cpp @@ -58,7 +58,7 @@ static void write_divecomputers(struct membuffer *b, struct dive *dive) put_string(b, separator); separator = ", "; put_format(b, "{"); - write_attribute(b, "model", dc->model, ", "); + write_attribute(b, "model", dc->model.c_str(), ", "); if (dc->deviceid) put_format(b, "\"deviceid\":\"%08x\", ", dc->deviceid); else diff --git a/core/save-xml.cpp b/core/save-xml.cpp index 8d7b0596f..2530df97e 100644 --- a/core/save-xml.cpp +++ b/core/save-xml.cpp @@ -447,7 +447,7 @@ static void save_samples(struct membuffer *b, struct dive *dive, struct divecomp static void save_dc(struct membuffer *b, struct dive *dive, struct divecomputer *dc) { put_format(b, " model, " model='", "'", 1); + show_utf8(b, dc->model.c_str(), " model='", "'", 1); if (dc->last_manual_time.seconds) put_duration(b, dc->last_manual_time, " last-manual-time='", " min'"); if (dc->deviceid) @@ -593,27 +593,19 @@ static void save_trip(struct membuffer *b, dive_trip_t *trip, bool anonymize) static void save_one_device(struct membuffer *b, const struct device *d) { - const char *model = device_get_model(d); - const char *nickname = device_get_nickname(d); - const char *serial_nr = device_get_serial(d); + std::string model = device_get_model(d); + std::string nickname = device_get_nickname(d); + std::string serial_nr = device_get_serial(d); /* Nicknames that are empty or the same as the device model are not interesting */ - if (empty_string(nickname) || !strcmp(model, nickname)) - nickname = NULL; - - /* Serial numbers that are empty are not interesting */ - if (empty_string(serial_nr)) - serial_nr = NULL; - - /* Do we have anything interesting about this dive computer to save? */ - if (!serial_nr || !nickname) + if (nickname.empty() || serial_nr.empty() || model == nickname) return; put_format(b, "\n"); } diff --git a/core/uemis-downloader.cpp b/core/uemis-downloader.cpp index bbd099921..9bbe5e028 100644 --- a/core/uemis-downloader.cpp +++ b/core/uemis-downloader.cpp @@ -188,7 +188,7 @@ static void uemis_get_weight(std::string_view buffer, weightsystem_t &weight, in static std::unique_ptr uemis_start_dive(uint32_t deviceid) { auto dive = std::make_unique(); - dive->dc.model = strdup("Uemis Zurich"); + dive->dc.model = "Uemis Zurich"; dive->dc.deviceid = deviceid; return dive; } @@ -985,7 +985,7 @@ static std::pair uemis_get_divenr(uint32_t deviceid, struct if (!d) continue; for_each_dc (d, dc) { - if (dc->model && !strcmp(dc->model, "Uemis Zurich") && + if (dc->model == "Uemis Zurich" && (dc->deviceid == 0 || dc->deviceid == 0x7fffffff || dc->deviceid == deviceid)) { if (dc->diveid > maxdiveid) maxdiveid = dc->diveid; diff --git a/core/uemis.cpp b/core/uemis.cpp index 1957d68c5..22ce6359a 100644 --- a/core/uemis.cpp +++ b/core/uemis.cpp @@ -295,7 +295,7 @@ void uemis::parse_divelog_binary(std::string_view base64, struct dive *dive) dive->dc.salinity = FRESHWATER_SALINITY; /* grams per 10l fresh water */ /* this will allow us to find the last dive read so far from this computer */ - dc->model = strdup("Uemis Zurich"); + dc->model = "Uemis Zurich"; dc->deviceid = *(uint32_t *)(data.data() + 9); dc->diveid = *(uint16_t *)(data.data() + 7); /* remember the weight units used in this dive - we may need this later when diff --git a/profile-widget/profilescene.cpp b/profile-widget/profilescene.cpp index d7feb8c70..4ae91c378 100644 --- a/profile-widget/profilescene.cpp +++ b/profile-widget/profilescene.cpp @@ -577,7 +577,7 @@ void ProfileScene::plotDive(const struct dive *dIn, int dcIn, DivePlannerPointsM event = event->next; } - QString dcText = get_dc_nickname(currentdc); + QString dcText = QString::fromStdString(get_dc_nickname(currentdc)); if (is_dc_planner(currentdc)) dcText = tr("Planned dive"); else if (is_dc_manually_added_dive(currentdc)) diff --git a/profile-widget/profilewidget2.cpp b/profile-widget/profilewidget2.cpp index 6535400d9..b4f603b98 100644 --- a/profile-widget/profilewidget2.cpp +++ b/profile-widget/profilewidget2.cpp @@ -681,8 +681,9 @@ void ProfileWidget2::renameCurrentDC() if (!currentdc) return; QString newName = QInputDialog::getText(this, tr("Edit nickname"), - tr("Set new nickname for %1 (serial %2):").arg(currentdc->model).arg(currentdc->serial), - QLineEdit::Normal, get_dc_nickname(currentdc), &ok); + tr("Set new nickname for %1 (serial %2):").arg(QString::fromStdString(currentdc->model)). + arg(QString::fromStdString(currentdc->serial)), + QLineEdit::Normal, QString::fromStdString(get_dc_nickname(currentdc)), &ok); if (ok) Command::editDeviceNickname(currentdc, newName); } diff --git a/smtk-import/smartrak.cpp b/smtk-import/smartrak.cpp index 367ae6790..dd2f10b2c 100644 --- a/smtk-import/smartrak.cpp +++ b/smtk-import/smartrak.cpp @@ -945,7 +945,7 @@ void smartrak_import(const char *file, struct divelog *log) dc_fam = DC_FAMILY_UWATEC_ALADIN; } rc = prepare_data(dc_model, (char *)col[coln(DCNUMBER)]->bind_ptr, dc_fam, devdata); - smtkdive->dc.model = copy_string(devdata.model.c_str()); + smtkdive->dc.model = devdata.model; if (rc == DC_STATUS_SUCCESS && mdb_table.get_len(coln(PROFILE))) { prf_buffer = static_cast(mdb_ole_read_full(mdb, col[coln(PROFILE)], &prf_length)); if (prf_length > 0) {