diff --git a/backend-shared/exportfuncs.cpp b/backend-shared/exportfuncs.cpp index 9946d3197..f5b36e613 100644 --- a/backend-shared/exportfuncs.cpp +++ b/backend-shared/exportfuncs.cpp @@ -193,10 +193,10 @@ void export_TeX(const char *filename, bool selected_only, bool plain, ExportCall put_format(&buf, "\\def\\%srating{%s}\n", ssrf, qPrintable(rating)); put_format(&buf, "\\def\\%splot{\\includegraphics[width=9cm,height=4cm]{profile%d}}\n", ssrf, dive->number); put_format(&buf, "\\def\\%sprofilename{profile%d}\n", ssrf, dive->number); - put_format(&buf, "\\def\\%scomment{%s}\n", ssrf, dive->notes ? dive->notes : ""); - put_format(&buf, "\\def\\%sbuddy{%s}\n", ssrf, dive->buddy ? dive->buddy : ""); - put_format(&buf, "\\def\\%sdivemaster{%s}\n", ssrf, dive->diveguide ? dive->diveguide : ""); - put_format(&buf, "\\def\\%ssuit{%s}\n", ssrf, dive->suit ? dive->suit : ""); + put_format(&buf, "\\def\\%scomment{%s}\n", ssrf, dive->notes.c_str()); + put_format(&buf, "\\def\\%sbuddy{%s}\n", ssrf, dive->buddy.c_str()); + put_format(&buf, "\\def\\%sdivemaster{%s}\n", ssrf, dive->diveguide.c_str()); + put_format(&buf, "\\def\\%ssuit{%s}\n", ssrf, dive->suit.c_str()); // Print cylinder data put_format(&buf, "\n%% Gas use information:\n"); diff --git a/commands/command_edit.cpp b/commands/command_edit.cpp index f33cfaa59..664745829 100644 --- a/commands/command_edit.cpp +++ b/commands/command_edit.cpp @@ -43,17 +43,16 @@ T EditDefaultSetter::data(struct dive *d) const return d->*PTR; } -template +template void EditStringSetter::set(struct dive *d, QString v) const { - free(d->*PTR); - d->*PTR = copy_qstring(v); + d->*PTR = v.toStdString(); } -template +template QString EditStringSetter::data(struct dive *d) const { - return QString(d->*PTR); + return QString::fromStdString(d->*PTR); } static std::vector getDives(bool currentDiveOnly) @@ -585,14 +584,13 @@ QString EditTags::fieldName() const // ***** Buddies ***** QStringList EditBuddies::data(struct dive *d) const { - return stringToList(d->buddy); + return stringToList(QString::fromStdString(d->buddy)); } void EditBuddies::set(struct dive *d, const QStringList &v) const { QString text = v.join(", "); - free(d->buddy); - d->buddy = copy_qstring(text); + d->buddy = text.toStdString(); } QString EditBuddies::fieldName() const @@ -603,14 +601,13 @@ QString EditBuddies::fieldName() const // ***** DiveGuide ***** QStringList EditDiveGuide::data(struct dive *d) const { - return stringToList(d->diveguide); + return stringToList(QString::fromStdString(d->diveguide)); } void EditDiveGuide::set(struct dive *d, const QStringList &v) const { QString text = v.join(", "); - free(d->diveguide); - d->diveguide = copy_qstring(text); + d->diveguide = text.toStdString(); } QString EditDiveGuide::fieldName() const @@ -618,14 +615,6 @@ QString EditDiveGuide::fieldName() const return Command::Base::tr("dive guide"); } -static void swapCandQString(QString &q, char *&c) -{ - QString tmp(c); - free(c); - c = copy_qstring(q); - q = std::move(tmp); -} - PasteState::PasteState(dive *dIn, const dive *data, dive_components what) : d(dIn) { if (what.notes) @@ -698,13 +687,13 @@ PasteState::~PasteState() void PasteState::swap(dive_components what) { if (what.notes) - swapCandQString(notes, d->notes); + std::swap(notes, d->notes); if (what.diveguide) - swapCandQString(diveguide, d->diveguide); + std::swap(diveguide, d->diveguide); if (what.buddy) - swapCandQString(buddy, d->buddy); + std::swap(buddy, d->buddy); if (what.suit) - swapCandQString(suit, d->suit); + std::swap(suit, d->suit); if (what.rating) std::swap(rating, d->rating); if (what.visibility) @@ -791,7 +780,6 @@ ReplanDive::ReplanDive(dive *source) : d(current_dive), when(0), maxdepth({0}), meandepth({0}), - notes(nullptr), surface_pressure({0}), duration({0}), salinity(0) @@ -806,7 +794,7 @@ ReplanDive::ReplanDive(dive *source) : d(current_dive), when = source->when; maxdepth = source->maxdepth; meandepth = source->meandepth; - notes = copy_string(source->notes); + notes = source->notes; duration = source->duration; salinity = source->salinity; surface_pressure = source->surface_pressure; @@ -820,7 +808,6 @@ ReplanDive::ReplanDive(dive *source) : d(current_dive), ReplanDive::~ReplanDive() { - free(notes); } bool ReplanDive::workToBeDone() @@ -1376,9 +1363,9 @@ EditDive::EditDive(dive *oldDiveIn, dive *newDiveIn, dive_site *createDs, dive_s changedFields |= DiveField::ATM_PRESS; if (oldDive->dive_site != newDive->dive_site) changedFields |= DiveField::DIVESITE; - if (!same_string(oldDive->diveguide, newDive->diveguide)) + if (oldDive->diveguide != newDive->diveguide) changedFields |= DiveField::DIVEGUIDE; - if (!same_string(oldDive->buddy, newDive->buddy)) + if (oldDive->buddy != newDive->buddy) changedFields |= DiveField::BUDDY; if (oldDive->rating != newDive->rating) changedFields |= DiveField::RATING; @@ -1392,13 +1379,13 @@ EditDive::EditDive(dive *oldDiveIn, dive *newDiveIn, dive_site *createDs, dive_s changedFields |= DiveField::SURGE; if (oldDive->chill != newDive->chill) changedFields |= DiveField::CHILL; - if (!same_string(oldDive->suit, newDive->suit)) + if (oldDive->suit != newDive->suit) changedFields |= DiveField::SUIT; if (taglist_get_tagstring(oldDive->tags) != taglist_get_tagstring(newDive->tags)) // This is cheating. Do we have a taglist comparison function? changedFields |= DiveField::TAGS; if (oldDive->dcs[0].divemode != newDive->dcs[0].divemode) changedFields |= DiveField::MODE; - if (!same_string(oldDive->notes, newDive->notes)) + if (oldDive->notes != newDive->notes) changedFields |= DiveField::NOTES; if (oldDive->salinity != newDive->salinity) changedFields |= DiveField::SALINITY; diff --git a/commands/command_edit.h b/commands/command_edit.h index 15c9c128d..26bdabe7d 100644 --- a/commands/command_edit.h +++ b/commands/command_edit.h @@ -88,7 +88,7 @@ private: // Automatically generate getter and setter in the case for string assignments. // The third parameter is a pointer to a C-style string in the dive structure. -template +template class EditStringSetter : public EditTemplate { private: using EditTemplate::EditTemplate; @@ -289,10 +289,10 @@ public: struct PasteState { dive *d; dive_site *divesite; - QString notes; - QString diveguide; - QString buddy; - QString suit; + std::string notes; + std::string diveguide; + std::string buddy; + std::string suit; int rating; int wavesize; int visibility; @@ -329,7 +329,7 @@ class ReplanDive : public Base { depth_t maxdepth, meandepth; struct cylinder_table cylinders; struct divecomputer dc; - char *notes; + std::string notes; pressure_t surface_pressure; duration_t duration; int salinity; diff --git a/core/datatrak.cpp b/core/datatrak.cpp index 6976bbac2..41707bd30 100644 --- a/core/datatrak.cpp +++ b/core/datatrak.cpp @@ -15,6 +15,7 @@ #include "units.h" #include "device.h" #include "file.h" +#include "format.h" #include "divesite.h" #include "dive.h" #include "divelog.h" @@ -159,16 +160,15 @@ static dc_status_t dt_libdc_buffer(unsigned char *ptr, int prf_length, int dc_mo static char *dt_dive_parser(unsigned char *runner, struct dive *dt_dive, struct divelog *log, char *maxbuf) { int rc, profile_length, libdc_model; - char *tmp_notes_str = NULL; unsigned char *tmp_string1 = NULL, *locality = NULL, *dive_point = NULL, *compl_buffer, *membuf = runner; - char buffer[1024]; unsigned char tmp_1byte; unsigned int tmp_2bytes; unsigned long tmp_4bytes; + std::string tmp_notes_str; char is_nitrox = 0, is_O2 = 0, is_SCR = 0; device_data_t devdata; @@ -305,22 +305,22 @@ static char *dt_dive_parser(unsigned char *runner, struct dive *dt_dive, struct read_bytes(1); switch (tmp_1byte) { case 1: - dt_dive->suit = strdup(translate("gettextFromC", "No suit")); + dt_dive->suit = "No suit"; break; case 2: - dt_dive->suit = strdup(translate("gettextFromC", "Shorty")); + dt_dive->suit = "Shorty"; break; case 3: - dt_dive->suit = strdup(translate("gettextFromC", "Combi")); + dt_dive->suit = "Combi"; break; case 4: - dt_dive->suit = strdup(translate("gettextFromC", "Wet suit")); + dt_dive->suit = "Wet suit"; break; case 5: - dt_dive->suit = strdup(translate("gettextFromC", "Semidry suit")); + dt_dive->suit = "Semidry suit"; break; case 6: - dt_dive->suit = strdup(translate("gettextFromC", "Dry suit")); + dt_dive->suit = "Dry suit"; break; default: // unknown, do nothing @@ -450,10 +450,9 @@ static char *dt_dive_parser(unsigned char *runner, struct dive *dt_dive, struct read_bytes(1); if (tmp_1byte != 0) { read_string(tmp_string1); - snprintf(buffer, sizeof(buffer), "%s: %s\n", + tmp_notes_str= format_string_std("%s: %s\n", translate("gettextFromC", "Other activities"), tmp_string1); - tmp_notes_str = strdup(buffer); free(tmp_string1); } @@ -463,7 +462,7 @@ static char *dt_dive_parser(unsigned char *runner, struct dive *dt_dive, struct read_bytes(1); if (tmp_1byte != 0) { read_string(tmp_string1); - dt_dive->buddy = strdup((char *)tmp_string1); + dt_dive->buddy = (const char *)tmp_string1; free(tmp_string1); } @@ -473,15 +472,12 @@ static char *dt_dive_parser(unsigned char *runner, struct dive *dt_dive, struct read_bytes(1); if (tmp_1byte != 0) { read_string(tmp_string1); - int len = snprintf(buffer, sizeof(buffer), "%s%s:\n%s", - tmp_notes_str ? tmp_notes_str : "", + dt_dive->notes = format_string_std("%s%s:\n%s", + tmp_notes_str.c_str(), translate("gettextFromC", "Datatrak/Wlog notes"), tmp_string1); - dt_dive->notes = (char *)calloc((len +1), 1); - memcpy(dt_dive->notes, buffer, len); free(tmp_string1); } - free(tmp_notes_str); /* * Alarms 1 and Alarms2 - Bit tables - Not in Subsurface, we use the profile @@ -519,7 +515,7 @@ static char *dt_dive_parser(unsigned char *runner, struct dive *dt_dive, struct libdc_model = dtrak_prepare_data(tmp_1byte, devdata); if (!libdc_model) report_error(translate("gettextFromC", "[Warning] Manual dive # %d\n"), dt_dive->number); - dt_dive->dcs[0].model = copy_string(devdata.model.c_str()); + dt_dive->dcs[0].model = devdata.model; /* * Air usage, unknown use. Probably allows or deny manually entering gas @@ -603,7 +599,7 @@ static void wlog_compl_parser(std::string &wl_mem, struct dive *dt_dive, int dco pos_viz = offset + 258, pos_tank_init = offset + 266, pos_suit = offset + 268; - char *wlog_notes = NULL, *wlog_suit = NULL, *buffer = NULL; + char *wlog_notes = NULL, *wlog_suit = NULL; unsigned char *runner = (unsigned char *) wl_mem.data(); /* @@ -615,16 +611,8 @@ static void wlog_compl_parser(std::string &wl_mem, struct dive *dt_dive, int dco (void)memcpy(wlog_notes_temp, runner + offset, NOTES_LENGTH); wlog_notes = to_utf8((unsigned char *) wlog_notes_temp); } - if (dt_dive->notes && wlog_notes) { - buffer = (char *)calloc (strlen(dt_dive->notes) + strlen(wlog_notes) + 1, 1); - sprintf(buffer, "%s%s", dt_dive->notes, wlog_notes); - free(dt_dive->notes); - dt_dive->notes = copy_string(buffer); - } else if (wlog_notes) { - dt_dive->notes = copy_string(wlog_notes); - } - free(buffer); - free(wlog_notes); + if (wlog_notes) + dt_dive->notes += wlog_notes; /* * Weight in Kg * 100 @@ -666,7 +654,7 @@ static void wlog_compl_parser(std::string &wl_mem, struct dive *dt_dive, int dco wlog_suit = to_utf8((unsigned char *) wlog_suit_temp); } if (wlog_suit) - dt_dive->suit = copy_string(wlog_suit); + dt_dive->suit = wlog_suit; free(wlog_suit); } diff --git a/core/dive.cpp b/core/dive.cpp index 59f437be1..1ab0d88f0 100644 --- a/core/dive.cpp +++ b/core/dive.cpp @@ -170,10 +170,10 @@ static void free_dive_structures(struct dive *d) return; fulltext_unregister(d); /* free the strings */ - free(d->buddy); - free(d->diveguide); - free(d->notes); - free(d->suit); + d->buddy.clear(); + d->diveguide.clear(); + d->notes.clear(); + d->suit.clear(); /* free tags, additional dive computers, and pictures */ d->tags.clear(); d->cylinders.clear(); @@ -207,10 +207,6 @@ void copy_dive(const struct dive *s, struct dive *d) memset(&d->pictures, 0, sizeof(d->pictures)); d->full_text = NULL; invalidate_dive_cache(d); - d->buddy = copy_string(s->buddy); - d->diveguide = copy_string(s->diveguide); - d->notes = copy_string(s->notes); - d->suit = copy_string(s->suit); copy_pictures(&s->pictures, &d->pictures); } @@ -233,7 +229,7 @@ struct std::unique_ptr move_dive(struct dive *s) #define CONDITIONAL_COPY_STRING(_component) \ if (what._component) \ - d->_component = copy_string(s->_component) + d->_component = s->_component // copy elements, depending on bits in what that are set void selective_copy_dive(const struct dive *s, struct dive *d, struct dive_components what, bool clear) @@ -1317,22 +1313,15 @@ static void merge_extra_data(struct divecomputer *res, } } -static char *merge_text(const char *a, const char *b, const char *sep) +static std::string merge_text(const std::string &a, const std::string &b, const char *sep) { - char *res; - if (!a && !b) - return NULL; - if (!a || !*a) - return copy_string(b); - if (!b || !*b) - return strdup(a); - if (!strcmp(a, b)) - return copy_string(a); - res = (char *)malloc(strlen(a) + strlen(b) + 32); - if (!res) - return (char *)a; - sprintf(res, "%s%s%s", a, sep, b); - return res; + if (a.empty()) + return b; + if (b.empty()) + return a; + if (a == b) + return a; + return a + sep + b; } #define SORT(a, b) \ diff --git a/core/dive.h b/core/dive.h index 97439fee3..e85bbeedd 100644 --- a/core/dive.h +++ b/core/dive.h @@ -29,11 +29,11 @@ struct dive { struct dive_trip *divetrip = nullptr; timestamp_t when = 0; struct dive_site *dive_site = nullptr; - char *notes = nullptr; - char *diveguide = nullptr, *buddy = nullptr; + std::string notes; + std::string diveguide, buddy; + std::string suit; cylinder_table cylinders; weightsystem_table weightsystems; - char *suit = nullptr; int number = 0; int rating = 0; int wavesize = 0, current = 0, visibility = 0, surge = 0, chill = 0; /* 0 - 5 star ratings */ diff --git a/core/filterconstraint.cpp b/core/filterconstraint.cpp index ed116d883..a2dddfddd 100644 --- a/core/filterconstraint.cpp +++ b/core/filterconstraint.cpp @@ -827,9 +827,9 @@ static bool has_tags(const filter_constraint &c, const struct dive *d) static bool has_people(const filter_constraint &c, const struct dive *d) { QStringList dive_people; - for (const QString &s: QString(d->buddy).split(",", SKIP_EMPTY)) + for (const QString &s: QString::fromStdString(d->buddy).split(",", SKIP_EMPTY)) dive_people.push_back(s.trimmed()); - for (const QString &s: QString(d->diveguide).split(",", SKIP_EMPTY)) + for (const QString &s: QString::fromStdString(d->diveguide).split(",", SKIP_EMPTY)) dive_people.push_back(s.trimmed()); return check(c, dive_people); } @@ -867,16 +867,16 @@ static bool has_cylinder_type(const filter_constraint &c, const struct dive *d) static bool has_suits(const filter_constraint &c, const struct dive *d) { QStringList diveSuits; - if (d->suit) - diveSuits.push_back(QString(d->suit)); + if (!d->suit.empty()) + diveSuits.push_back(QString::fromStdString(d->suit)); return check(c, diveSuits); } static bool has_notes(const filter_constraint &c, const struct dive *d) { QStringList diveNotes; - if (d->notes) - diveNotes.push_back(QString(d->notes)); + if (!d->notes.empty()) + diveNotes.push_back(QString::fromStdString(d->notes)); return check(c, diveNotes); } diff --git a/core/fulltext.cpp b/core/fulltext.cpp index 39fdc763c..57510a1a9 100644 --- a/core/fulltext.cpp +++ b/core/fulltext.cpp @@ -119,10 +119,10 @@ static void tokenize(QString s, std::vector &res) static std::vector getWords(const dive *d) { std::vector res; - tokenize(QString(d->notes), res); - tokenize(QString(d->diveguide), res); - tokenize(QString(d->buddy), res); - tokenize(QString(d->suit), res); + tokenize(QString::fromStdString(d->notes), res); + tokenize(QString::fromStdString(d->diveguide), res); + tokenize(QString::fromStdString(d->buddy), res); + tokenize(QString::fromStdString(d->suit), res); for (const divetag *tag: d->tags) tokenize(QString::fromStdString(tag->name), res); for (auto &cyl: d->cylinders) diff --git a/core/import-cobalt.cpp b/core/import-cobalt.cpp index 43e8c0e14..4c599301c 100644 --- a/core/import-cobalt.cpp +++ b/core/import-cobalt.cpp @@ -63,7 +63,7 @@ static int cobalt_buddies(void *param, int, char **data, char **) struct parser_state *state = (struct parser_state *)param; if (data[0]) - utf8_string(data[0], &state->cur_dive->buddy); + utf8_string_std(data[0], &state->cur_dive->buddy); return 0; } @@ -106,7 +106,7 @@ static int cobalt_dive(void *param, int, char **data, char **) state->cur_dive->when = (time_t)(atol(data[1])); if (data[4]) - utf8_string(data[4], &state->cur_dive->notes); + utf8_string_std(data[4], &state->cur_dive->notes); /* data[5] should have information on Units used, but I cannot * parse it at all based on the sample log I have received. The diff --git a/core/import-divinglog.cpp b/core/import-divinglog.cpp index 509a9d0cf..578c13904 100644 --- a/core/import-divinglog.cpp +++ b/core/import-divinglog.cpp @@ -279,10 +279,10 @@ static int divinglog_dive(void *param, int, char **data, char **) state->log->sites->find_or_create(std::string(data[2]))->add_dive(state->cur_dive.get()); if (data[3]) - utf8_string(data[3], &state->cur_dive->buddy); + utf8_string_std(data[3], &state->cur_dive->buddy); if (data[4]) - utf8_string(data[4], &state->cur_dive->notes); + utf8_string_std(data[4], &state->cur_dive->notes); if (data[5]) state->cur_dive->dcs[0].maxdepth.mm = lrint(permissive_strtod(data[5], NULL) * 1000); @@ -291,7 +291,7 @@ static int divinglog_dive(void *param, int, char **data, char **) state->cur_dive->dcs[0].duration.seconds = atoi(data[6]) * 60; if (data[7]) - utf8_string(data[7], &state->cur_dive->diveguide); + utf8_string_std(data[7], &state->cur_dive->diveguide); if (data[8]) state->cur_dive->airtemp.mkelvin = C_to_mkelvin(atol(data[8])); @@ -305,7 +305,7 @@ static int divinglog_dive(void *param, int, char **data, char **) } if (data[11]) - state->cur_dive->suit = strdup(data[11]); + state->cur_dive->suit = data[11]; /* Divinglog has following visibility options: good, medium, bad */ if (data[14]) { diff --git a/core/import-seac.cpp b/core/import-seac.cpp index 1ff8bfba7..eddeed247 100644 --- a/core/import-seac.cpp +++ b/core/import-seac.cpp @@ -150,7 +150,7 @@ static int seac_dive(void *param, int, char **data, char **) // 9 = comments from seac app if (data[9]) { - utf8_string(data[9], &state->cur_dive->notes); + utf8_string_std(data[9], &state->cur_dive->notes); } // 10 = dive duration diff --git a/core/import-shearwater.cpp b/core/import-shearwater.cpp index 077feec09..e977ee6de 100644 --- a/core/import-shearwater.cpp +++ b/core/import-shearwater.cpp @@ -236,9 +236,9 @@ static int shearwater_dive(void *param, int, char **data, char **) if (data[2]) add_dive_site(data[2], state->cur_dive.get(), state); if (data[3]) - utf8_string(data[3], &state->cur_dive->buddy); + utf8_string_std(data[3], &state->cur_dive->buddy); if (data[4]) - utf8_string(data[4], &state->cur_dive->notes); + utf8_string_std(data[4], &state->cur_dive->notes); state->metric = atoi(data[5]) == 1 ? 0 : 1; @@ -366,9 +366,9 @@ static int shearwater_cloud_dive(void *param, int, char **data, char **) if (data[2]) add_dive_site(data[2], state->cur_dive.get(), state); if (data[3]) - utf8_string(data[3], &state->cur_dive->buddy); + utf8_string_std(data[3], &state->cur_dive->buddy); if (data[4]) - utf8_string(data[4], &state->cur_dive->notes); + utf8_string_std(data[4], &state->cur_dive->notes); state->metric = atoi(data[5]) == 1 ? 0 : 1; diff --git a/core/import-suunto.cpp b/core/import-suunto.cpp index 22d8956bf..d05993586 100644 --- a/core/import-suunto.cpp +++ b/core/import-suunto.cpp @@ -179,7 +179,7 @@ static int dm4_dive(void *param, int, char **data, char **) state->cur_dive->when = (time_t)(atol(data[1])); if (data[2]) - utf8_string(data[2], &state->cur_dive->notes); + utf8_string_std(data[2], &state->cur_dive->notes); /* * DM4 stores Duration and DiveTime. It looks like DiveTime is @@ -367,7 +367,7 @@ static int dm5_dive(void *param, int, char **data, char **) state->cur_dive->when = (time_t)(atol(data[1])); if (data[2]) - utf8_string(data[2], &state->cur_dive->notes); + utf8_string_std(data[2], &state->cur_dive->notes); if (data[3]) state->cur_dive->duration.seconds = atoi(data[3]); diff --git a/core/liquivision.cpp b/core/liquivision.cpp index 0147b9468..cda12b1d2 100644 --- a/core/liquivision.cpp +++ b/core/liquivision.cpp @@ -199,7 +199,7 @@ static void parse_dives(int log_version, const unsigned char *buf, unsigned int // Blank notes are better than the default text std::string notes((char *)buf + ptr, len); if (!starts_with(notes, "Comment ...")) - dive->notes = strdup(notes.c_str()); + dive->notes = notes; ptr += len; dive->id = array_uint32_le(buf + ptr); diff --git a/core/load-git.cpp b/core/load-git.cpp index 0910adc8e..c7d681e90 100644 --- a/core/load-git.cpp +++ b/core/load-git.cpp @@ -240,16 +240,16 @@ static void parse_dive_location(char *, struct git_parser_state *state) } static void parse_dive_diveguide(char *, struct git_parser_state *state) -{ state->active_dive->diveguide = get_first_converted_string_c(state); } +{ state->active_dive->diveguide = get_first_converted_string(state); } static void parse_dive_buddy(char *, struct git_parser_state *state) -{ state->active_dive->buddy = get_first_converted_string_c(state); } +{ state->active_dive->buddy = get_first_converted_string(state); } static void parse_dive_suit(char *, struct git_parser_state *state) -{ state->active_dive->suit = get_first_converted_string_c(state); } +{ state->active_dive->suit = get_first_converted_string(state); } static void parse_dive_notes(char *, struct git_parser_state *state) -{ state->active_dive->notes = get_first_converted_string_c(state); } +{ state->active_dive->notes = get_first_converted_string(state); } static void parse_dive_divesiteid(char *line, struct git_parser_state *state) { state->log->sites->get_by_uuid(get_hex(line))->add_dive(state->active_dive.get()); } @@ -312,10 +312,10 @@ static void parse_site_description(char *, struct git_parser_state *state) { state->active_site->description = get_first_converted_string(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(state); } static void parse_site_notes(char *, struct git_parser_state *state) -{ state->active_site->notes = get_first_converted_string_c(state); } +{ state->active_site->notes = get_first_converted_string(state); } static void parse_site_gps(char *line, struct git_parser_state *state) { diff --git a/core/parse-xml.cpp b/core/parse-xml.cpp index 356c40d84..691715d5a 100644 --- a/core/parse-xml.cpp +++ b/core/parse-xml.cpp @@ -1016,8 +1016,8 @@ static int divinglog_dive_match(struct dive *dive, const char *name, char *buf, MATCH("divetime", duration, &dive->dcs[0].duration) || MATCH_STATE("depth", depth, &dive->dcs[0].maxdepth) || MATCH_STATE("depthavg", depth, &dive->dcs[0].meandepth) || - MATCH("comments", utf8_string, &dive->notes) || - MATCH("names.buddy", utf8_string, &dive->buddy) || + MATCH("comments", utf8_string_std, &dive->notes) || + MATCH("names.buddy", utf8_string_std, &dive->buddy) || MATCH("name.country", utf8_string_std, &state->country) || MATCH("name.city", utf8_string_std, &state->city) || MATCH_STATE("name.place", divinglog_place, dive) || @@ -1309,18 +1309,18 @@ static void try_to_fill_dive(struct dive *dive, const char *name, char *buf, str return; if (MATCH_STATE("name.dive", add_dive_site, dive)) return; - if (MATCH("suit", utf8_string, &dive->suit)) + if (MATCH("suit", utf8_string_std, &dive->suit)) return; - if (MATCH("divesuit", utf8_string, &dive->suit)) + if (MATCH("divesuit", utf8_string_std, &dive->suit)) return; - if (MATCH("notes", utf8_string, &dive->notes)) + if (MATCH("notes", utf8_string_std, &dive->notes)) return; // For historic reasons, we accept dive guide as well as dive master - if (MATCH("diveguide", utf8_string, &dive->diveguide)) + if (MATCH("diveguide", utf8_string_std, &dive->diveguide)) return; - if (MATCH("divemaster", utf8_string, &dive->diveguide)) + if (MATCH("divemaster", utf8_string_std, &dive->diveguide)) return; - if (MATCH("buddy", utf8_string, &dive->buddy)) + if (MATCH("buddy", utf8_string_std, &dive->buddy)) return; if (MATCH("watersalinity", salinity, &dive->user_salinity)) return; diff --git a/core/plannernotes.cpp b/core/plannernotes.cpp index 71f1577c8..0ee3febf8 100644 --- a/core/plannernotes.cpp +++ b/core/plannernotes.cpp @@ -126,8 +126,7 @@ void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool show_d translate("gettextFromC", "Warning:"), translate("gettextFromC", "Decompression calculation aborted due to excessive time")); // TODO: avoid copy - free(dive->notes); - dive->notes = strdup(buf.c_str()); + dive->notes = buf; return; } @@ -144,8 +143,7 @@ void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool show_d subsurface_canonical_version(), translate("gettextFromC", "dive plan (overlapping dives detected)")); // TODO: avoid copy - free(dive->notes); - dive->notes = strdup(buf.c_str()); + dive->notes = buf; return; } else if (diveplan->surface_interval >= 48 * 60 *60) { buf += format_string_std("%s (%s) %s %s", @@ -613,8 +611,7 @@ void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool show_d buf += "\n"; } // TODO: avoid copy - free(dive->notes); - dive->notes = strdup(buf.c_str()); + dive->notes = buf; #ifdef DEBUG_PLANNER_NOTES printf("\n\n\tplannernotes\n\t\n%s\t\n\n", dive->notes); #endif diff --git a/core/save-git.cpp b/core/save-git.cpp index 57c3d4294..402cee1f2 100644 --- a/core/save-git.cpp +++ b/core/save-git.cpp @@ -98,10 +98,10 @@ static void show_utf8(struct membuffer *b, const char *prefix, const char *value static void save_overview(struct membuffer *b, struct dive *dive) { - show_utf8(b, "divemaster ", dive->diveguide, "\n"); - show_utf8(b, "buddy ", dive->buddy, "\n"); - show_utf8(b, "suit ", dive->suit, "\n"); - show_utf8(b, "notes ", dive->notes, "\n"); + show_utf8(b, "divemaster ", dive->diveguide.c_str(), "\n"); + show_utf8(b, "buddy ", dive->buddy.c_str(), "\n"); + show_utf8(b, "suit ", dive->suit.c_str(), "\n"); + show_utf8(b, "notes ", dive->notes.c_str(), "\n"); } static void save_tags(struct membuffer *b, const tag_list &tags) diff --git a/core/save-html.cpp b/core/save-html.cpp index 8e07509df..d7f7212a9 100644 --- a/core/save-html.cpp +++ b/core/save-html.cpp @@ -210,11 +210,10 @@ void put_HTML_quoted(struct membuffer *b, const char *text) void put_HTML_notes(struct membuffer *b, const struct dive *dive, const char *pre, const char *post) { put_string(b, pre); - if (dive->notes) { - put_HTML_quoted(b, dive->notes); - } else { + if (!dive->notes.empty()) + put_HTML_quoted(b, dive->notes.c_str()); + else put_string(b, "--"); - } put_string(b, post); } @@ -348,9 +347,9 @@ static void write_one_dive(struct membuffer *b, const struct dive *dive, const c put_HTML_airtemp(b, dive, "\"air\":\"", "\","); put_HTML_watertemp(b, dive, "\"water\":\"", "\""); put_string(b, " },"); - write_attribute(b, "buddy", dive->buddy, ", "); - write_attribute(b, "diveguide", dive->diveguide, ", "); - write_attribute(b, "suit", dive->suit, ", "); + write_attribute(b, "buddy", dive->buddy.c_str(), ", "); + write_attribute(b, "diveguide", dive->diveguide.c_str(), ", "); + write_attribute(b, "suit", dive->suit.c_str(), ", "); put_HTML_tags(b, dive, "\"tags\":", ","); if (!list_only) { put_cylinder_HTML(b, dive); diff --git a/core/save-xml.cpp b/core/save-xml.cpp index 811d57d35..b72238946 100644 --- a/core/save-xml.cpp +++ b/core/save-xml.cpp @@ -157,10 +157,10 @@ static void save_salinity(struct membuffer *b, struct divecomputer *dc) static void save_overview(struct membuffer *b, struct dive *dive, bool anonymize) { - show_utf8_blanked(b, dive->diveguide, " ", "\n", 0, anonymize); - show_utf8_blanked(b, dive->buddy, " ", "\n", 0, anonymize); - show_utf8_blanked(b, dive->notes, " ", "\n", 0, anonymize); - show_utf8_blanked(b, dive->suit, " ", "\n", 0, anonymize); + show_utf8_blanked(b, dive->diveguide.c_str(), " ", "\n", 0, anonymize); + show_utf8_blanked(b, dive->buddy.c_str(), " ", "\n", 0, anonymize); + show_utf8_blanked(b, dive->notes.c_str(), " ", "\n", 0, anonymize); + show_utf8_blanked(b, dive->suit.c_str(), " ", "\n", 0, anonymize); } static void put_gasmix(struct membuffer *b, struct gasmix mix) diff --git a/core/string-format.cpp b/core/string-format.cpp index c95417a3f..ff4ac1889 100644 --- a/core/string-format.cpp +++ b/core/string-format.cpp @@ -43,7 +43,7 @@ QString formatSac(const dive *d) QString formatNotes(const dive *d) { - QString tmp = d->notes ? QString::fromUtf8(d->notes) : QString(); + QString tmp = QString::fromStdString(d->notes); if (is_dc_planner(&d->dcs[0])) { QTextDocument notes; #define _NOTES_BR "\n" diff --git a/core/uemis-downloader.cpp b/core/uemis-downloader.cpp index 2998dc23d..609c5416a 100644 --- a/core/uemis-downloader.cpp +++ b/core/uemis-downloader.cpp @@ -158,20 +158,15 @@ static void uemis_get_index(std::string_view buffer, int &idx) } /* space separated */ -static void uemis_add_string(std::string_view buffer, char **text, const char *delimit) +static void uemis_add_string(std::string_view buffer, std::string &text, const char *delimit) { /* do nothing if this is an empty buffer (Uemis sometimes returns a single * space for empty buffers) */ if (buffer.empty() || buffer == " ") return; - if (!*text) { - *text = strdup(std::string(buffer).c_str()); - } else { - std::string res = std::string(*text) + delimit; - res += buffer; - free(*text); - *text = strdup(res.c_str()); - } + if (!text.empty()) + text += delimit; + text += buffer; } /* still unclear if it ever reports lbs */ @@ -749,24 +744,24 @@ static void parse_tag(struct dive *dive, std::string_view tag, std::string_view uemis_get_weight(val, ws, dive->dcs[0].diveid); dive->weightsystems.push_back(std::move(ws)); } else if (tag == "notes") { - uemis_add_string(val, &dive->notes, " "); + uemis_add_string(val, dive->notes, " "); } else if (tag == "u8DiveSuit") { int idx = 0; uemis_get_index(val, idx); if (idx > 0 && idx < (int)std::size(suit)) - uemis_add_string(translate("gettextFromC", suit[idx]), &dive->suit, " "); + uemis_add_string(translate("gettextFromC", suit[idx]), dive->suit, " "); } else if (tag == "u8DiveSuitType") { int idx = 0; uemis_get_index(val, idx); if (idx > 0 && idx < (int)std::size(suit_type)) - uemis_add_string(translate("gettextFromC", suit_type[idx]), &dive->suit, " "); + uemis_add_string(translate("gettextFromC", suit_type[idx]), dive->suit, " "); } else if (tag == "u8SuitThickness") { int idx = 0; uemis_get_index(val, idx); if (idx > 0 && idx < (int)std::size(suit_thickness)) - uemis_add_string(translate("gettextFromC", suit_thickness[idx]), &dive->suit, " "); + uemis_add_string(translate("gettextFromC", suit_thickness[idx]), dive->suit, " "); } else if (tag == "nickname") { - uemis_add_string(val, &dive->buddy, ","); + uemis_add_string(val, dive->buddy, ","); } } diff --git a/desktop-widgets/simplewidgets.cpp b/desktop-widgets/simplewidgets.cpp index 581df22ec..85f38ebae 100644 --- a/desktop-widgets/simplewidgets.cpp +++ b/desktop-widgets/simplewidgets.cpp @@ -328,17 +328,17 @@ void DiveComponentSelection::buttonClicked(QAbstractButton *button) if (what->divesite && current_dive->dive_site) text << tr("Dive site: ") << QString::fromStdString(current_dive->dive_site->name) << "\n"; if (what->diveguide) - text << tr("Dive guide: ") << current_dive->diveguide << "\n"; + text << tr("Dive guide: ") << QString::fromStdString(current_dive->diveguide) << "\n"; if (what->buddy) - text << tr("Buddy: ") << current_dive->buddy << "\n"; + text << tr("Buddy: ") << QString::fromStdString(current_dive->buddy) << "\n"; if (what->rating) text << tr("Rating: ") + QString("*").repeated(current_dive->rating) << "\n"; if (what->visibility) text << tr("Visibility: ") + QString("*").repeated(current_dive->visibility) << "\n"; if (what->notes) - text << tr("Notes:\n") << current_dive->notes << "\n"; + text << tr("Notes:\n") << QString::fromStdString(current_dive->notes) << "\n"; if (what->suit) - text << tr("Suit: ") << current_dive->suit << "\n"; + text << tr("Suit: ") << QString::fromStdString(current_dive->suit) << "\n"; if (what-> tags) { text << tr("Tags: "); for (const divetag *tag: current_dive->tags) diff --git a/desktop-widgets/tab-widgets/TabDiveEquipment.cpp b/desktop-widgets/tab-widgets/TabDiveEquipment.cpp index b0453f110..488f424d2 100644 --- a/desktop-widgets/tab-widgets/TabDiveEquipment.cpp +++ b/desktop-widgets/tab-widgets/TabDiveEquipment.cpp @@ -118,7 +118,7 @@ void TabDiveEquipment::divesChanged(const QVector &dives, DiveField fiel return; if (field.suit) - ui.suit->setText(QString(parent.currentDive->suit)); + ui.suit->setText(QString::fromStdString(parent.currentDive->suit)); } void TabDiveEquipment::toggleTriggeredColumn() @@ -145,8 +145,8 @@ void TabDiveEquipment::updateData(const std::vector &, dive *currentDive sensorDelegate.setCurrentDC(dc); tankUseDelegate.setCurrentDC(dc); - if (currentDive && currentDive->suit) - ui.suit->setText(QString(currentDive->suit)); + if (currentDive && !currentDive->suit.empty()) + ui.suit->setText(QString::fromStdString(currentDive->suit)); else ui.suit->clear(); } diff --git a/desktop-widgets/tab-widgets/TabDiveNotes.cpp b/desktop-widgets/tab-widgets/TabDiveNotes.cpp index 61387ea1a..2db4beace 100644 --- a/desktop-widgets/tab-widgets/TabDiveNotes.cpp +++ b/desktop-widgets/tab-widgets/TabDiveNotes.cpp @@ -120,9 +120,9 @@ void TabDiveNotes::divesChanged(const QVector &dives, DiveField field) if (field.tags) ui.tagWidget->setText(QString::fromStdString(taglist_get_tagstring(currentDive->tags))); if (field.buddy) - ui.buddy->setText(currentDive->buddy); + ui.buddy->setText(QString::fromStdString(currentDive->buddy)); if (field.diveguide) - ui.diveguide->setText(currentDive->diveguide); + ui.diveguide->setText(QString::fromStdString(currentDive->diveguide)); } void TabDiveNotes::diveSiteEdited(dive_site *ds, int) @@ -152,7 +152,7 @@ static bool isHtml(const QString &s) void TabDiveNotes::updateNotes(const struct dive *d) { - QString tmp(d->notes); + QString tmp = QString::fromStdString(d->notes); if (isHtml(tmp)) { ui.notes->setHtml(tmp); } else { @@ -263,8 +263,8 @@ void TabDiveNotes::updateData(const std::vector &, dive *currentDive, in updateNotes(currentDive); updateDiveSite(currentDive); updateDateTime(currentDive); - ui.diveguide->setText(currentDive->diveguide); - ui.buddy->setText(currentDive->buddy); + ui.diveguide->setText(QString::fromStdString(currentDive->diveguide)); + ui.buddy->setText(QString::fromStdString(currentDive->buddy)); } ui.duration->setText(render_seconds_to_string(currentDive->duration.seconds)); ui.depth->setText(get_depth_string(currentDive->maxdepth, true)); diff --git a/desktop-widgets/templatelayout.cpp b/desktop-widgets/templatelayout.cpp index 59ab75548..3fc8a105c 100644 --- a/desktop-widgets/templatelayout.cpp +++ b/desktop-widgets/templatelayout.cpp @@ -540,11 +540,11 @@ QVariant TemplateLayout::getValue(QString list, QString property, const State &s } else if (property == "meandepth") { return get_depth_string(d->dcs[0].meandepth.mm, true, true); } else if (property == "divemaster") { - return d->diveguide; + return QString::fromStdString(d->diveguide); } else if (property == "diveguide") { - return d->diveguide; + return QString::fromStdString(d->diveguide); } else if (property == "buddy") { - return d->buddy; + return QString::fromStdString(d->buddy); } else if (property == "airTemp") { return get_temperature_string(d->airtemp, true); } else if (property == "waterTemp") { @@ -564,7 +564,7 @@ QVariant TemplateLayout::getValue(QString list, QString property, const State &s } else if (property == "singleWeight") { return d->weightsystems.size() <= 1; } else if (property == "suit") { - return d->suit; + return QString::fromStdString(d->suit); } else if (property == "cylinderList") { return formatFullCylinderList(); } else if (property == "cylinders") { diff --git a/mobile-widgets/qmlmanager.cpp b/mobile-widgets/qmlmanager.cpp index c52115f04..af8d8f1a6 100644 --- a/mobile-widgets/qmlmanager.cpp +++ b/mobile-widgets/qmlmanager.cpp @@ -1301,26 +1301,21 @@ void QMLManager::commitChanges(QString diveId, QString number, QString date, QSt k++; } } - if (d->suit != suit) { + if (d->suit != suit.toStdString()) { diveChanged = true; - free(d->suit); - d->suit = copy_qstring(suit); + d->suit = suit.toStdString(); } - if (d->buddy != buddy) { - if (buddy.contains(",")){ + if (d->buddy != buddy.toStdString()) { + if (buddy.contains(",")) buddy = buddy.replace(QRegularExpression("\\s*,\\s*"), ", "); - } diveChanged = true; - free(d->buddy); - d->buddy = copy_qstring(buddy); + d->buddy = buddy.toStdString(); } - if (d->diveguide != diveGuide) { - if (diveGuide.contains(",")){ + if (d->diveguide != diveGuide.toStdString()) { + if (diveGuide.contains(",")) diveGuide = diveGuide.replace(QRegularExpression("\\s*,\\s*"), ", "); - } diveChanged = true; - free(d->diveguide); - d->diveguide = copy_qstring(diveGuide); + d->diveguide = diveGuide.toStdString(); } // normalize the tag list we have and the one we get from the UI // try hard to deal with accidental white space issues @@ -1349,8 +1344,7 @@ void QMLManager::commitChanges(QString diveId, QString number, QString date, QSt } if (formatNotes(d) != notes) { diveChanged = true; - free(d->notes); - d->notes = copy_qstring(notes); + d->notes = notes.toStdString(); } // now that we have it all figured out, let's see what we need // to update diff --git a/qt-models/completionmodels.cpp b/qt-models/completionmodels.cpp index 3f5210b73..89c374e5f 100644 --- a/qt-models/completionmodels.cpp +++ b/qt-models/completionmodels.cpp @@ -28,13 +28,13 @@ void CompletionModelBase::divesChanged(const QVector &, DiveField field) updateModel(); } -static QStringList getCSVList(char *dive::*item) +static QStringList getCSVList(const std::string dive::*item) { QSet set; struct dive *dive; int i = 0; for_each_dive (i, dive) { - QString str(dive->*item); + QString str = QString::fromStdString(dive->*item); for (const QString &value: str.split(",", SKIP_EMPTY)) set.insert(value.trimmed()); } @@ -69,7 +69,7 @@ QStringList SuitCompletionModel::getStrings() struct dive *dive; int i = 0; for_each_dive (i, dive) { - QString suit(dive->suit); + QString suit = QString::fromStdString(dive->suit); if (!list.contains(suit)) list.append(suit); } diff --git a/qt-models/diveplannermodel.cpp b/qt-models/diveplannermodel.cpp index 00a310438..61e80abdf 100644 --- a/qt-models/diveplannermodel.cpp +++ b/qt-models/diveplannermodel.cpp @@ -1093,7 +1093,7 @@ void DivePlannerPointsModel::updateDiveProfile() #endif final_deco_state = plan_deco_state; } - emit calculatedPlanNotes(QString(d->notes)); + emit calculatedPlanNotes(QString::fromStdString(d->notes)); #if DEBUG_PLAN @@ -1268,10 +1268,10 @@ finish: void DivePlannerPointsModel::computeVariationsDone(QString variations) { - QString notes = QString(d->notes); - free(d->notes); - d->notes = copy_qstring(notes.replace("VARIATIONS", variations)); - emit calculatedPlanNotes(QString(d->notes)); + QString notes = QString::fromStdString(d->notes); + notes = notes.replace("VARIATIONS", variations); + d->notes = notes.toStdString(); + emit calculatedPlanNotes(notes); } void DivePlannerPointsModel::createPlan(bool saveAsNew) @@ -1298,7 +1298,7 @@ void DivePlannerPointsModel::createPlan(bool saveAsNew) // Try to identify old planner output and remove only this part // Treat user provided text as plain text. QTextDocument notesDocument; - notesDocument.setHtml(current_dive->notes); + notesDocument.setHtml(QString::fromStdString(current_dive->notes)); QString oldnotes(notesDocument.toPlainText()); QString disclaimer = get_planner_disclaimer(); int disclaimerMid = disclaimer.indexOf("%s"); @@ -1320,8 +1320,8 @@ void DivePlannerPointsModel::createPlan(bool saveAsNew) } // Deal with line breaks oldnotes.replace("\n", "
"); - oldnotes.append(d->notes); - d->notes = copy_qstring(oldnotes); + oldnotes.append(QString::fromStdString(d->notes)); + d->notes = oldnotes.toStdString(); // If we save as new create a copy of the dive here } diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp index 8c14e1bc8..ce61af8ce 100644 --- a/qt-models/divetripmodel.cpp +++ b/qt-models/divetripmodel.cpp @@ -287,13 +287,13 @@ QVariant DiveTripModelBase::diveData(const struct dive *d, int column, int role) formatDiveDuration(d)); case MobileListModel::RatingRole: return d->rating; case MobileListModel::VizRole: return d->visibility; - case MobileListModel::SuitRole: return QString(d->suit); + case MobileListModel::SuitRole: return QString::fromStdString(d->suit); case MobileListModel::AirTempRole: return get_temperature_string(d->airtemp, true); case MobileListModel::WaterTempRole: return get_temperature_string(d->watertemp, true); case MobileListModel::SacRole: return formatSac(d); case MobileListModel::SumWeightRole: return formatSumWeight(d); - case MobileListModel::DiveGuideRole: return QString(d->diveguide); - case MobileListModel::BuddyRole: return QString(d->buddy); + case MobileListModel::DiveGuideRole: return QString::fromStdString(d->diveguide); + case MobileListModel::BuddyRole: return QString::fromStdString(d->buddy); case MobileListModel::TagsRole: return QString::fromStdString(taglist_get_tagstring(d->tags)); case MobileListModel::NotesRole: return formatNotes(d); case MobileListModel::GpsRole: return formatDiveGPS(d); @@ -334,7 +334,7 @@ QVariant DiveTripModelBase::diveData(const struct dive *d, int column, int role) case TOTALWEIGHT: return displayWeight(d, prefs.units.show_units_table); case SUIT: - return QString(d->suit); + return QString::fromStdString(d->suit); case CYLINDER: return !d->cylinders.empty() ? QString::fromStdString(d->cylinders[0].type.description) : QString(); case SAC: @@ -353,15 +353,15 @@ QVariant DiveTripModelBase::diveData(const struct dive *d, int column, int role) case COUNTRY: return QString::fromStdString(get_dive_country(d)); case BUDDIES: - return QString(d->buddy); + return QString::fromStdString(d->buddy); case DIVEGUIDE: - return QString(d->diveguide); + return QString::fromStdString(d->diveguide); case LOCATION: return QString::fromStdString(get_dive_location(d)); case GAS: return formatDiveGasString(d); case NOTES: - return QString(d->notes); + return QString::fromStdString(d->notes); case DIVEMODE: return QString(divemode_text_ui[(int)d->dcs[0].divemode]); } @@ -1712,15 +1712,6 @@ static bool lessThanHelper(int diff1, int diff2) return diff1 < 0 || (diff1 == 0 && diff2 < 0); } -static int strCmp(const char *s1, const char *s2) -{ - if (!s1) - return !s2 ? 0 : -1; - if (!s2) - return 1; - return QString::localeAwareCompare(QString(s1), QString(s2)); // TODO: avoid copy -} - static int strCmp(const std::string &s1, const std::string &s2) { return QString::localeAwareCompare(QString::fromStdString(s1), QString::fromStdString(s2)); // TODO: avoid copy diff --git a/smtk-import/smartrak.cpp b/smtk-import/smartrak.cpp index a0f966997..73ece6ff3 100644 --- a/smtk-import/smartrak.cpp +++ b/smtk-import/smartrak.cpp @@ -264,26 +264,6 @@ static void concat(std::string &orig, const char *sep, std::string_view s) orig += s; } -/* - * Temporary funcion as long as core still has C-strings. - * Equivalent to concat, but takes a C-string as first argument and - * frees it. Returns a newly allocated copy. - * - * Note: taking "const char * const *" is an ugly hack to - * allow passing pointer to "const char *" as well as - * "char *". Which in turn is necessary, as this function currently - * replaces "const char *" strings. - */ -static void concat(const char * const *orig_in, const char *sep, std::string_view s) -{ - char **orig = const_cast(orig_in); - char *to_free = *orig; - std::string orig_std(*orig ? *orig : ""); - concat(orig_std, sep, s); - *orig = strdup(orig_std.c_str()); - free(to_free); -} - /* * A site may be a wreck, which has its own table. * Parse this table referred by the site idx. If found, put the different info items in @@ -700,7 +680,7 @@ static void smtk_parse_relations(MdbHandle *mdb, struct dive *dive, char *dive_i dive->dcs[0].divemode = CCR; } if (!tmp.empty()) - concat(&dive->notes, "\n", format_string_std("Smartrak %s: %s", table_name, tmp.c_str())); + concat(dive->notes, "\n", format_string_std("Smartrak %s: %s", table_name, tmp.c_str())); } /* @@ -719,7 +699,7 @@ static void smtk_parse_other(struct dive *dive, const std::vector & if (tag) taglist_add_tag(dive->tags, str); else - concat(&dive->notes, "\n", format_string_std("Smartrak %s: %s", data_name, str.c_str())); + concat(dive->notes, "\n", format_string_std("Smartrak %s: %s", data_name, str.c_str())); } } @@ -1023,9 +1003,9 @@ void smartrak_import(const char *file, struct divelog *log) smtkdive->visibility = strtod((char *)col[coln(VISIBILITY)]->bind_ptr, NULL) > 25 ? 5 : lrint(strtod((char *)col[13]->bind_ptr, NULL) / 5); weightsystem_t ws = { {(int)lrint(strtod((char *)col[coln(WEIGHT)]->bind_ptr, NULL) * 1000)}, std::string(), false }; smtkdive->weightsystems.push_back(std::move(ws)); - smtkdive->suit = strdup(get(suit_list, atoi((char *)col[coln(SUITIDX)]->bind_ptr) - 1).c_str()); + smtkdive->suit = get(suit_list, atoi((char *)col[coln(SUITIDX)]->bind_ptr) - 1); smtk_build_location(mdb_clon, (char *)col[coln(SITEIDX)]->bind_ptr, &smtkdive->dive_site, log); - smtkdive->buddy = strdup(smtk_locate_buddy(mdb_clon, (char *)col[0]->bind_ptr, buddy_list).c_str()); + smtkdive->buddy = smtk_locate_buddy(mdb_clon, (char *)col[0]->bind_ptr, buddy_list); smtk_parse_relations(mdb_clon, smtkdive.get(), (char *)col[0]->bind_ptr, "Type", "TypeRelation", type_list, true); smtk_parse_relations(mdb_clon, smtkdive.get(), (char *)col[0]->bind_ptr, "Activity", "ActivityRelation", activity_list, false); smtk_parse_relations(mdb_clon, smtkdive.get(), (char *)col[0]->bind_ptr, "Gear", "GearRelation", gear_list, false); @@ -1034,7 +1014,7 @@ void smartrak_import(const char *file, struct divelog *log) smtk_parse_other(smtkdive.get(), underwater_list, "Underwater", (char *)col[coln(UNDERWATERIDX)]->bind_ptr, false); smtk_parse_other(smtkdive.get(), surface_list, "Surface", (char *)col[coln(SURFACEIDX)]->bind_ptr, false); smtk_parse_bookmarks(mdb_clon, smtkdive.get(), (char *)col[0]->bind_ptr); - concat(&smtkdive->notes, "\n", std::string((char *)col[coln(REMARKS)]->bind_ptr)); + concat(smtkdive->notes, "\n", std::string((char *)col[coln(REMARKS)]->bind_ptr)); record_dive_to_table(smtkdive.release(), log->dives.get()); } diff --git a/stats/statsvariables.cpp b/stats/statsvariables.cpp index 01a2995fe..dddbd69d7 100644 --- a/stats/statsvariables.cpp +++ b/stats/statsvariables.cpp @@ -1427,9 +1427,9 @@ struct DiveModeVariable : public StatsVariableTemplate { std::vector to_bin_values(const dive *d) const { std::vector dive_people; - for (const QString &s: QString(d->buddy).split(",", SKIP_EMPTY)) + for (const QString &s: QString::fromStdString(d->buddy).split(",", SKIP_EMPTY)) dive_people.push_back(s.trimmed()); - for (const QString &s: QString(d->diveguide).split(",", SKIP_EMPTY)) + for (const QString &s: QString::fromStdString(d->diveguide).split(",", SKIP_EMPTY)) dive_people.push_back(s.trimmed()); return dive_people; } @@ -1441,8 +1441,8 @@ struct PeopleVariable : public StatsVariableTemplatebuddy).trimmed(); - QString diveguide = QString(d->diveguide).trimmed(); + QString buddy = QString::fromStdString(d->buddy).trimmed(); + QString diveguide = QString::fromStdString(d->diveguide).trimmed(); if (!buddy.isEmpty() && !diveguide.isEmpty()) buddy += ", "; return buddy + diveguide; @@ -1455,7 +1455,7 @@ struct PeopleVariable : public StatsVariableTemplate { std::vector to_bin_values(const dive *d) const { std::vector buddies; - for (const QString &s: QString(d->buddy).split(",", SKIP_EMPTY)) + for (const QString &s: QString::fromStdString(d->buddy).split(",", SKIP_EMPTY)) buddies.push_back(s.trimmed()); return buddies; } @@ -1467,7 +1467,7 @@ struct BuddyVariable : public StatsVariableTemplatebuddy).trimmed(); + return QString::fromStdString(d->buddy).trimmed(); } std::vector binners() const override { return { &buddy_binner }; @@ -1477,7 +1477,7 @@ struct BuddyVariable : public StatsVariableTemplate { std::vector to_bin_values(const dive *d) const { std::vector dive_guides; - for (const QString &s: QString(d->diveguide).split(",", SKIP_EMPTY)) + for (const QString &s: QString::fromStdString(d->diveguide).split(",", SKIP_EMPTY)) dive_guides.push_back(s.trimmed()); return dive_guides; } @@ -1489,7 +1489,7 @@ struct DiveGuideVariable : public StatsVariableTemplatediveguide).trimmed(); + return QString::fromStdString(d->diveguide).trimmed(); } std::vector binners() const override { return { &dive_guide_binner }; @@ -1746,7 +1746,7 @@ struct GasContentHeVariable : GasContentVariable { struct SuitBinner : public StringBinner { std::vector to_bin_values(const dive *d) const { - return { QString(d->suit) }; + return { QString::fromStdString(d->suit) }; } }; @@ -1756,7 +1756,7 @@ struct SuitVariable : public StatsVariableTemplatesuit); + return QString::fromStdString(d->suit); } std::vector binners() const override { return { &suit_binner }; diff --git a/tests/testgitstorage.cpp b/tests/testgitstorage.cpp index 5a03015ce..7a1cdec64 100644 --- a/tests/testgitstorage.cpp +++ b/tests/testgitstorage.cpp @@ -378,8 +378,7 @@ void TestGitStorage::testGitStorageCloudMerge2() process_loaded_dives(); dive = get_dive(1); QVERIFY(dive != NULL); - free(dive->notes); - dive->notes = strdup("These notes have been modified by TestGitStorage"); + dive->notes = "These notes have been modified by TestGitStorage"; QCOMPARE(save_dives(cloudTestRepo.c_str()), 0); clear_dive_file_data(); @@ -413,14 +412,11 @@ void TestGitStorage::testGitStorageCloudMerge3() process_loaded_dives(); struct dive *dive; QVERIFY((dive = get_dive(0)) != 0); - free(dive->notes); - dive->notes = strdup("Create multi line dive notes\nLine 2\nLine 3\nLine 4\nLine 5\nThat should be enough"); + dive->notes = "Create multi line dive notes\nLine 2\nLine 3\nLine 4\nLine 5\nThat should be enough"; QVERIFY((dive = get_dive(1)) != 0); - free(dive->notes); - dive->notes = strdup("Create multi line dive notes\nLine 2\nLine 3\nLine 4\nLine 5\nThat should be enough"); + dive->notes = "Create multi line dive notes\nLine 2\nLine 3\nLine 4\nLine 5\nThat should be enough"; QVERIFY((dive = get_dive(2)) != 0); - free(dive->notes); - dive->notes = strdup("Create multi line dive notes\nLine 2\nLine 3\nLine 4\nLine 5\nThat should be enough"); + dive->notes = "Create multi line dive notes\nLine 2\nLine 3\nLine 4\nLine 5\nThat should be enough"; QCOMPARE(save_dives(cloudTestRepo.c_str()), 0); clear_dive_file_data(); @@ -428,14 +424,11 @@ void TestGitStorage::testGitStorageCloudMerge3() QCOMPARE(parse_file(cloudTestRepo.c_str(), &divelog), 0); process_loaded_dives(); QVERIFY((dive = get_dive(0)) != 0); - free(dive->notes); - dive->notes = strdup("Create multi line dive notes\nDifferent line 2 and removed 3-5\n\nThat should be enough"); + dive->notes = "Create multi line dive notes\nDifferent line 2 and removed 3-5\n\nThat should be enough"; QVERIFY((dive = get_dive(1)) != 0); - free(dive->notes); - dive->notes = strdup("Line 2\nLine 3\nLine 4\nLine 5"); // keep the middle, remove first and last"); + dive->notes = "Line 2\nLine 3\nLine 4\nLine 5"; // keep the middle, remove first and last"); QVERIFY((dive = get_dive(2)) != 0); - free(dive->notes); - dive->notes = strdup("single line dive notes"); + dive->notes = "single line dive notes"; git_local_only = true; QCOMPARE(save_dives(cloudTestRepo.c_str()), 0); git_local_only = false; @@ -447,14 +440,11 @@ void TestGitStorage::testGitStorageCloudMerge3() QCOMPARE(parse_file(cloudTestRepo.c_str(), &divelog), 0); process_loaded_dives(); QVERIFY((dive = get_dive(0)) != 0); - free(dive->notes); - dive->notes = strdup("Completely different dive notes\nBut also multi line"); + dive->notes = "Completely different dive notes\nBut also multi line"; QVERIFY((dive = get_dive(1)) != 0); - free(dive->notes); - dive->notes = strdup("single line dive notes"); + dive->notes = "single line dive notes"; QVERIFY((dive = get_dive(2)) != 0); - free(dive->notes); - dive->notes = strdup("Line 2\nLine 3\nLine 4\nLine 5"); // keep the middle, remove first and last"); + dive->notes = "Line 2\nLine 3\nLine 4\nLine 5"; // keep the middle, remove first and last"); QCOMPARE(save_dives(cloudTestRepo.c_str()), 0); clear_dive_file_data(); diff --git a/tests/testplan.cpp b/tests/testplan.cpp index 849efb82a..22b46d7b1 100644 --- a/tests/testplan.cpp +++ b/tests/testplan.cpp @@ -486,8 +486,7 @@ void TestPlan::testMetric() plan(&test_deco_state, &testPlan, &dive, 0, 60, stoptable, cache, 1, 0); #if DEBUG - free(dive.notes); - dive.notes = NULL; + dive.notes.clear(); save_dive(stdout, &dive, false); #endif @@ -527,8 +526,7 @@ void TestPlan::testImperial() plan(&test_deco_state, &testPlan, &dive, 0, 60, stoptable, cache, 1, 0); #if DEBUG - free(dive.notes); - dive.notes = NULL; + dive.notes.clear(); save_dive(stdout, &dive, false); #endif @@ -567,8 +565,7 @@ void TestPlan::testVpmbMetric45m30minTx() plan(&test_deco_state, &testPlan, &dive, 0, 60, stoptable, cache, 1, 0); #if DEBUG - free(dive.notes); - dive.notes = NULL; + dive.notes.clear(); save_dive(stdout, &dive, false); #endif @@ -597,8 +594,7 @@ void TestPlan::testVpmbMetric60m10minTx() plan(&test_deco_state, &testPlan, &dive, 0, 60, stoptable, cache, 1, 0); #if DEBUG - free(dive.notes); - dive.notes = NULL; + dive.notes.clear(); save_dive(stdout, &dive, false); #endif @@ -627,8 +623,7 @@ void TestPlan::testVpmbMetric60m30minAir() plan(&test_deco_state, &testPlan, &dive, 0, 60, stoptable, cache, 1, 0); #if DEBUG - free(dive.notes); - dive.notes = NULL; + dive.notes.clear(); save_dive(stdout, &dive, false); #endif @@ -657,8 +652,7 @@ void TestPlan::testVpmbMetric60m30minEan50() plan(&test_deco_state, &testPlan, &dive, 0, 60, stoptable, cache, 1, 0); #if DEBUG - free(dive.notes); - dive.notes = NULL; + dive.notes.clear(); save_dive(stdout, &dive, false); #endif @@ -693,8 +687,7 @@ void TestPlan::testVpmbMetric60m30minTx() plan(&test_deco_state, &testPlan, &dive, 0, 60, stoptable, cache, 1, 0); #if DEBUG - free(dive.notes); - dive.notes = NULL; + dive.notes.clear(); save_dive(stdout, &dive, false); #endif @@ -729,8 +722,7 @@ void TestPlan::testVpmbMetric100m60min() plan(&test_deco_state, &testPlan, &dive, 0, 60, stoptable, cache, 1, 0); #if DEBUG - free(dive.notes); - dive.notes = NULL; + dive.notes.clear(); save_dive(stdout, &dive, false); #endif @@ -772,8 +764,7 @@ void TestPlan::testMultipleGases() plan(&test_deco_state, &testPlan, &dive, 0, 60, stoptable, cache, 1, 0); #if DEBUG - free(dive.notes); - dive.notes = NULL; + dive.notes.clear(); save_dive(stdout, &dive, false); #endif @@ -797,8 +788,7 @@ void TestPlan::testVpmbMetricMultiLevelAir() plan(&test_deco_state, &testPlan, &dive, 0, 60, stoptable, cache, 1, 0); #if DEBUG - free(dive.notes); - dive.notes = NULL; + dive.notes.clear(); save_dive(stdout, &dive, false); #endif @@ -827,8 +817,7 @@ void TestPlan::testVpmbMetric100m10min() plan(&test_deco_state, &testPlan, &dive, 0, 60, stoptable, cache, 1, 0); #if DEBUG - free(dive.notes); - dive.notes = NULL; + dive.notes.clear(); save_dive(stdout, &dive, false); #endif @@ -874,8 +863,7 @@ void TestPlan::testVpmbMetricRepeat() plan(&test_deco_state, &testPlan, &dive, 0, 60, stoptable, cache, 1, 0); #if DEBUG - free(dive.notes); - dive.notes = NULL; + dive.notes.clear(); save_dive(stdout, &dive, false); #endif @@ -895,8 +883,7 @@ void TestPlan::testVpmbMetricRepeat() plan(&test_deco_state, &testPlan, &dive, 0, 60, stoptable, cache, 1, 0); #if DEBUG - free(dive.notes); - dive.notes = NULL; + dive.notes.clear(); save_dive(stdout, &dive, false); #endif @@ -933,8 +920,7 @@ void TestPlan::testVpmbMetricRepeat() plan(&test_deco_state, &testPlan, &dive, 0, 60, stoptable, cache, 1, 0); #if DEBUG - free(dive.notes); - dive.notes = NULL; + dive.notes.clear(); save_dive(stdout, &dive, false); #endif @@ -972,8 +958,7 @@ void TestPlan::testCcrBailoutGasSelection() plan(&test_deco_state, &testPlan, &dive, 0, 60, stoptable, cache, true, false); #if DEBUG - free(dive.notes); - dive.notes = NULL; + dive.notes.clear(); save_dive(stdout, &dive, false); #endif