diff --git a/backend-shared/exportfuncs.cpp b/backend-shared/exportfuncs.cpp index 6a6b8ee34..842dee404 100644 --- a/backend-shared/exportfuncs.cpp +++ b/backend-shared/exportfuncs.cpp @@ -200,7 +200,7 @@ void export_TeX(const char *filename, bool selected_only, bool plain, ExportCall for (auto [i, cyl]: enumerated_range(dive->cylinders)) { if (dive->is_cylinder_used(i) || (prefs.include_unused_tanks && !cyl.type.description.empty())){ put_format(&buf, "\\def\\%scyl%cdescription{%s}\n", ssrf, 'a' + i, cyl.type.description.c_str()); - put_format(&buf, "\\def\\%scyl%cgasname{%s}\n", ssrf, 'a' + i, gasname(cyl.gasmix)); + put_format(&buf, "\\def\\%scyl%cgasname{%s}\n", ssrf, 'a' + i, cyl.gasmix.name().c_str()); put_format(&buf, "\\def\\%scyl%cmixO2{%.1f\\%%}\n", ssrf, 'a' + i, get_o2(cyl.gasmix)/10.0); put_format(&buf, "\\def\\%scyl%cmixHe{%.1f\\%%}\n", ssrf, 'a' + i, get_he(cyl.gasmix)/10.0); put_format(&buf, "\\def\\%scyl%cmixN2{%.1f\\%%}\n", ssrf, 'a' + i, (100.0 - (get_o2(cyl.gasmix)/10.0) - (get_he(cyl.gasmix)/10.0))); diff --git a/core/equipment.cpp b/core/equipment.cpp index f553acb39..3d5ecc8c1 100644 --- a/core/equipment.cpp +++ b/core/equipment.cpp @@ -178,26 +178,6 @@ bool weightsystem_t::operator==(const weightsystem_t &w2) const std::tie(w2.weight.grams, w2.description); } -void get_gas_string(struct gasmix gasmix, char *text, int len) -{ - if (gasmix_is_air(gasmix)) - snprintf(text, len, "%s", translate("gettextFromC", "air")); - else if (get_he(gasmix) == 0 && get_o2(gasmix) < 1000) - snprintf(text, len, translate("gettextFromC", "EAN%d"), (get_o2(gasmix) + 5) / 10); - else if (get_he(gasmix) == 0 && get_o2(gasmix) == 1000) - snprintf(text, len, "%s", translate("gettextFromC", "oxygen")); - else - snprintf(text, len, "(%d/%d)", (get_o2(gasmix) + 5) / 10, (get_he(gasmix) + 5) / 10); -} - -/* Returns a static char buffer - only good for immediate use by printf etc */ -const char *gasname(struct gasmix gasmix) -{ - static char gas[64]; - get_gas_string(gasmix, gas, sizeof(gas)); - return gas; -} - volume_t cylinder_t::gas_volume(pressure_t p) const { double bar = p.mbar / 1000.0; diff --git a/core/equipment.h b/core/equipment.h index bf3c1339c..cebe43e2e 100644 --- a/core/equipment.h +++ b/core/equipment.h @@ -93,9 +93,6 @@ extern void dump_cylinders(struct dive *dive, bool verbose); /* Cylinder table functions */ extern void add_cylinder(struct cylinder_table *, int idx, cylinder_t cyl); -void get_gas_string(struct gasmix gasmix, char *text, int len); -const char *gasname(struct gasmix gasmix); - struct ws_info { std::string name; weight_t weight; diff --git a/core/gas.cpp b/core/gas.cpp index 398a2127c..2033b62f9 100644 --- a/core/gas.cpp +++ b/core/gas.cpp @@ -2,6 +2,7 @@ #include "gas.h" #include "pref.h" #include "errorhelper.h" +#include "format.h" #include "gettext.h" #include #include @@ -183,3 +184,15 @@ const char *gastype_name(enum gastype type) return ""; return translate("gettextFromC", gastype_names[type]); } + +std::string gasmix::name() const +{ + if (gasmix_is_air(*this)) + return translate("gettextFromC", "air"); + else if (get_he(*this) == 0 && get_o2(*this) < 1000) + return format_string_std(translate("gettextFromC", "EAN%d"), (get_o2(*this) + 5) / 10); + else if (get_he(*this) == 0 && get_o2(*this) == 1000) + return translate("gettextFromC", "oxygen"); + else + return format_string_std("(%d/%d)", (get_o2(*this) + 5) / 10, (get_he(*this) + 5) / 10); +} diff --git a/core/gas.h b/core/gas.h index 4e6bcf193..64f2d46d1 100644 --- a/core/gas.h +++ b/core/gas.h @@ -5,6 +5,8 @@ #include "divemode.h" #include "units.h" +#include + enum gas_component { N2, HE, O2 }; // o2 == 0 && he == 0 -> air @@ -12,6 +14,7 @@ enum gas_component { N2, HE, O2 }; struct gasmix { fraction_t o2; fraction_t he; + std::string name() const; }; static const struct gasmix gasmix_invalid = { { -1 }, { -1 } }; static const struct gasmix gasmix_air = { { 0 }, { 0 } }; diff --git a/core/planner.cpp b/core/planner.cpp index 209b5690f..0d8cbdfe1 100644 --- a/core/planner.cpp +++ b/core/planner.cpp @@ -422,7 +422,7 @@ static std::vector analyze_gaslist(struct diveplan *diveplan, struct for (size_t nr = 0; nr < gaschanges.size(); nr++) { int idx = gaschanges[nr].gasidx; printf("gaschange nr %d: @ %5.2lfm gasidx %d (%s)\n", nr, gaschanges[nr].depth / 1000.0, - idx, gasname(&dive.get_cylinder(idx)->gasmix)); + idx, dive.get_cylinder(idx)->gasmix.name().c_str()); } #endif return gaschanges; @@ -737,7 +737,7 @@ bool plan(struct deco_state *ds, struct diveplan *diveplan, struct dive *dive, i } #if DEBUG_PLAN & 4 - printf("gas %s\n", gasname(&gas)); + printf("gas %s\n", gas.name().c_str()); printf("depth %5.2lfm \n", depth / 1000.0); printf("current_cylinder %i\n", current_cylinder); #endif @@ -876,7 +876,7 @@ bool plan(struct deco_state *ds, struct diveplan *diveplan, struct dive *dive, i else current_cylinder = get_gasidx(dive, gas); if (current_cylinder == -1) { - report_error(translate("gettextFromC", "Can't find gas %s"), gasname(gas)); + report_error(translate("gettextFromC", "Can't find gas %s"), gas.name().c_str()); current_cylinder = 0; } reset_regression(ds); diff --git a/core/plannernotes.cpp b/core/plannernotes.cpp index 5f2669c81..499aec2f3 100644 --- a/core/plannernotes.cpp +++ b/core/plannernotes.cpp @@ -64,7 +64,7 @@ static std::string icd_entry(struct icd_data *icdvalues, bool printheader, int t b += casprintf_loc( "%3d%s" "%s➙", - (time_seconds + 30) / 60, translate("gettextFromC", "min"), gasname(gas_from)); + (time_seconds + 30) / 60, translate("gettextFromC", "min"), gas_from.name().c_str()); b += casprintf_loc( "%s%+5.1f%%" "%+5.1f%%" @@ -72,7 +72,7 @@ static std::string icd_entry(struct icd_data *icdvalues, bool printheader, int t "%+5.2f%s" "%+5.2f%s" "%+5.2f%s", - gasname(gas_to), icdvalues->dHe / 10.0, + gas_to.name().c_str(), icdvalues->dHe / 10.0, ((5 * icdvalues->dN2) > -icdvalues->dHe) ? "red" : "#383838", icdvalues->dN2 / 10.0 , 0.2 * (-icdvalues->dHe / 10.0), ambientpressure_mbar * icdvalues->dHe / 1e6f, translate("gettextFromC", "bar"), ((5 * icdvalues->dN2) > -icdvalues->dHe) ? "red" : "#383838", ambientpressure_mbar * icdvalues->dN2 / 1e6f, translate("gettextFromC", "bar"), @@ -237,7 +237,7 @@ void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool show_d decimals, depthvalue, depth_unit, FRACTION_TUPLE(dp->time - lasttime, 60), FRACTION_TUPLE(dp->time, 60), - gasname(gasmix), + gasmix.name().c_str(), (double) dp->setpoint / 1000.0); } else { buf += casprintf_loc(translate("gettextFromC", "%s to %.*f %s in %d:%02d min - runtime %d:%02u on %s"), @@ -245,7 +245,7 @@ void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool show_d decimals, depthvalue, depth_unit, FRACTION_TUPLE(dp->time - lasttime, 60), FRACTION_TUPLE(dp->time, 60), - gasname(gasmix)); + gasmix.name().c_str()); } buf += "
\n"; @@ -259,14 +259,14 @@ void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool show_d decimals, depthvalue, depth_unit, FRACTION_TUPLE(dp->time - lasttime, 60), FRACTION_TUPLE(dp->time, 60), - gasname(gasmix), + gasmix.name().c_str(), (double) dp->setpoint / 1000.0); } else { buf += casprintf_loc(translate("gettextFromC", "Stay at %.*f %s for %d:%02d min - runtime %d:%02u on %s %s"), decimals, depthvalue, depth_unit, FRACTION_TUPLE(dp->time - lasttime, 60), FRACTION_TUPLE(dp->time, 60), - gasname(gasmix), + gasmix.name().c_str(), translate("gettextFromC", divemode_text_ui[dp->divemode])); } buf += "
\n"; @@ -327,10 +327,10 @@ void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool show_d if (nextdp->setpoint) { temp = casprintf_loc(translate("gettextFromC", "(SP = %.1fbar CCR)"), nextdp->setpoint / 1000.0); buf += format_string_std("%s %s", - gasname(newgasmix), temp.c_str()); + newgasmix.name().c_str(), temp.c_str()); } else { buf += format_string_std("%s %s", - gasname(newgasmix), dp->divemode == UNDEF_COMP_TYPE || dp->divemode == nextdp->divemode ? "" : translate("gettextFromC", divemode_text_ui[nextdp->divemode])); + newgasmix.name().c_str(), dp->divemode == UNDEF_COMP_TYPE || dp->divemode == nextdp->divemode ? "" : translate("gettextFromC", divemode_text_ui[nextdp->divemode])); if (isascent && (get_he(lastprintgasmix) > 0)) { // For a trimix gas change on ascent, save ICD info if previous cylinder had helium if (isobaric_counterdiffusion(lastprintgasmix, newgasmix, &icdvalues)) // Do icd calulations icdwarning = true; @@ -348,9 +348,9 @@ void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool show_d // If a new gas has been used for this segment, now is the time to show it if (dp->setpoint) { temp = casprintf_loc(translate("gettextFromC", "(SP = %.1fbar CCR)"), (double) dp->setpoint / 1000.0); - buf += format_string_std("%s %s", gasname(gasmix), temp.c_str()); + buf += format_string_std("%s %s", gasmix.name().c_str(), temp.c_str()); } else { - buf += format_string_std("%s %s", gasname(gasmix), + buf += format_string_std("%s %s", gasmix.name().c_str(), lastdivemode == UNDEF_COMP_TYPE || lastdivemode == dp->divemode ? "" : translate("gettextFromC", divemode_text_ui[dp->divemode])); if (get_he(lastprintgasmix) > 0) { // For a trimix gas change, save ICD info if previous cylinder had helium if (isobaric_counterdiffusion(lastprintgasmix, gasmix, &icdvalues)) // Do icd calculations @@ -380,9 +380,9 @@ void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool show_d if (plan_verbatim) { if (lastsetpoint >= 0) { if (nextdp && nextdp->setpoint) { - buf += casprintf_loc(translate("gettextFromC", "Switch gas to %s (SP = %.1fbar)"), gasname(newgasmix), (double) nextdp->setpoint / 1000.0); + buf += casprintf_loc(translate("gettextFromC", "Switch gas to %s (SP = %.1fbar)"), newgasmix.name().c_str(), (double) nextdp->setpoint / 1000.0); } else { - buf += format_string_std(translate("gettextFromC", "Switch gas to %s"), gasname(newgasmix)); + buf += format_string_std(translate("gettextFromC", "Switch gas to %s"), newgasmix.name().c_str()); if ((isascent) && (get_he(lastprintgasmix) > 0)) { // For a trimix gas change on ascent: if (isobaric_counterdiffusion(lastprintgasmix, newgasmix, &icdvalues)) // Do icd calculations icdwarning = true; @@ -535,18 +535,18 @@ void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool show_d /* Print the gas consumption for every cylinder here to temp buffer. */ if (lrint(volume) > 0) { temp = casprintf_loc(translate("gettextFromC", "%.0f%s/%.0f%s of %s (%.0f%s/%.0f%s in planned ascent)"), - volume, unit, pressure, pressure_unit, gasname(cyl.gasmix), deco_volume, unit, deco_pressure, pressure_unit); + volume, unit, pressure, pressure_unit, cyl.gasmix.name().c_str(), deco_volume, unit, deco_pressure, pressure_unit); } else { temp = casprintf_loc(translate("gettextFromC", "%.0f%s/%.0f%s of %s"), - volume, unit, pressure, pressure_unit, gasname(cyl.gasmix)); + volume, unit, pressure, pressure_unit, cyl.gasmix.name().c_str()); } } else { if (lrint(volume) > 0) { temp = casprintf_loc(translate("gettextFromC", "%.0f%s of %s (%.0f%s during planned ascent)"), - volume, unit, gasname(cyl.gasmix), deco_volume, unit); + volume, unit, cyl.gasmix.name().c_str(), deco_volume, unit); } else { temp = casprintf_loc(translate("gettextFromC", "%.0f%s of %s"), - volume, unit, gasname(cyl.gasmix)); + volume, unit, cyl.gasmix.name().c_str()); } } /* Gas consumption: Now finally print all strings to output */ @@ -591,7 +591,7 @@ void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool show_d buf += "
\n"; o2warning_exist = true; temp = casprintf_loc(translate("gettextFromC", "high pO₂ value %.2f at %d:%02u with gas %s at depth %.*f %s"), - pressures.o2, FRACTION_TUPLE(dp->time, 60), gasname(gasmix), decimals, depth_value, depth_unit); + pressures.o2, FRACTION_TUPLE(dp->time, 60), gasmix.name().c_str(), decimals, depth_value, depth_unit); buf += format_string_std("%s %s
\n", translate("gettextFromC", "Warning:"), temp.c_str()); } else if (pressures.o2 < 0.16) { const char *depth_unit; @@ -601,7 +601,7 @@ void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool show_d buf += "
"; o2warning_exist = true; temp = casprintf_loc(translate("gettextFromC", "low pO₂ value %.2f at %d:%02u with gas %s at depth %.*f %s"), - pressures.o2, FRACTION_TUPLE(dp->time, 60), gasname(gasmix), decimals, depth_value, depth_unit); + pressures.o2, FRACTION_TUPLE(dp->time, 60), gasmix.name().c_str(), decimals, depth_value, depth_unit); buf += format_string_std("%s %s
\n", translate("gettextFromC", "Warning:"), temp.c_str()); } } diff --git a/core/profile.cpp b/core/profile.cpp index f065056d4..2256780a8 100644 --- a/core/profile.cpp +++ b/core/profile.cpp @@ -1281,7 +1281,7 @@ static std::vector plot_string(const struct dive *d, const struct p continue; struct gasmix mix = d->get_cylinder(cyl)->gasmix; pressurevalue = get_pressure_units(mbar, &pressure_unit); - res.push_back(casprintf_loc(translate("gettextFromC", "P: %d%s (%s)"), pressurevalue, pressure_unit, gasname(mix))); + res.push_back(casprintf_loc(translate("gettextFromC", "P: %d%s (%s)"), pressurevalue, pressure_unit, mix.name().c_str())); } if (entry.temperature) { tempvalue = get_temp_units(entry.temperature, &temp_unit); diff --git a/core/qthelper.cpp b/core/qthelper.cpp index b3f446223..ff15c45f5 100644 --- a/core/qthelper.cpp +++ b/core/qthelper.cpp @@ -351,7 +351,7 @@ QVector> selectedDivesGasUsed() std::vector diveGases = get_gas_used(d); for (size_t j = 0; j < d->cylinders.size(); j++) { if (diveGases[j].mliter) { - QString gasName = gasname(d->get_cylinder(j)->gasmix); + QString gasName = QString::fromStdString(d->get_cylinder(j)->gasmix.name()); gasUsed[gasName] += diveGases[j].mliter; } } diff --git a/core/string-format.cpp b/core/string-format.cpp index b9b2bb1a3..07bb45f26 100644 --- a/core/string-format.cpp +++ b/core/string-format.cpp @@ -179,7 +179,7 @@ QString formatGas(const dive *d) QString gas = QString::fromStdString(cyl.type.description); if (!gas.isEmpty()) gas += QChar(' '); - gas += gasname(cyl.gasmix); + gas += QString::fromStdString(cyl.gasmix.name()); // if has a description and if such gas is not already present if (!gas.isEmpty() && gases.indexOf(gas) == -1) { if (!gases.isEmpty()) diff --git a/desktop-widgets/simplewidgets.cpp b/desktop-widgets/simplewidgets.cpp index 650db6a6e..6b80cb549 100644 --- a/desktop-widgets/simplewidgets.cpp +++ b/desktop-widgets/simplewidgets.cpp @@ -347,7 +347,8 @@ void DiveComponentSelection::buttonClicked(QAbstractButton *button) text << tr("Cylinders:\n"); for (auto [idx, cyl]: enumerated_range(current_dive->cylinders)) { if (current_dive->is_cylinder_used(idx)) - text << QString::fromStdString(cyl.type.description) << " " << gasname(cyl.gasmix) << "\n"; + text << QString::fromStdString(cyl.type.description) << " " + << QString::fromStdString(cyl.gasmix.name()) << "\n"; } } if (what->weights) { diff --git a/desktop-widgets/tab-widgets/TabDiveInformation.cpp b/desktop-widgets/tab-widgets/TabDiveInformation.cpp index 99ec535ac..9d9753032 100644 --- a/desktop-widgets/tab-widgets/TabDiveInformation.cpp +++ b/desktop-widgets/tab-widgets/TabDiveInformation.cpp @@ -139,7 +139,7 @@ void TabDiveInformation::updateProfile() gaslist.append(separator); volumes.append(separator); SACs.append(separator); separator = "\n"; - gaslist.append(gasname(currentDive->get_cylinder(i)->gasmix)); + gaslist.append(QString::fromStdString(currentDive->get_cylinder(i)->gasmix.name())); if (!gases[i].mliter) continue; volumes.append(get_volume_string(gases[i], true)); diff --git a/profile-widget/diveeventitem.cpp b/profile-widget/diveeventitem.cpp index a52090541..c5848c9ea 100644 --- a/profile-widget/diveeventitem.cpp +++ b/profile-widget/diveeventitem.cpp @@ -125,7 +125,7 @@ void DiveEventItem::setupToolTipString(struct gasmix lastgasmix) struct icd_data icd_data; struct gasmix mix = dive->get_gasmix_from_event(ev); name += ": "; - name += gasname(mix); + name += QString::fromStdString(mix.name()); /* Do we have an explicit cylinder index? Show it. */ if (ev.gas.index >= 0) diff --git a/profile-widget/tankitem.cpp b/profile-widget/tankitem.cpp index f8cc1aeb1..6906faec7 100644 --- a/profile-widget/tankitem.cpp +++ b/profile-widget/tankitem.cpp @@ -57,7 +57,7 @@ void TankItem::createBar(int startTime, int stopTime, struct gasmix gas) rect->setPen(QPen(QBrush(), 0.0)); // get rid of the thick line around the rectangle rects.push_back(rect); DiveTextItem *label = new DiveTextItem(dpr, 1.0, Qt::AlignVCenter | Qt::AlignRight, rect); - label->set(gasname(gas), Qt::black); + label->set(QString::fromStdString(gas.name()), Qt::black); label->setPos(x + 2.0 * dpr, height() / 2.0); label->setZValue(101); }