From 77e8c3655edd3ad65ff8870aaca7b5b0526792a1 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Thu, 7 Mar 2024 11:09:40 +0100 Subject: [PATCH] core: compile planner source files with C++ Another relatively easily converted part of the code. Signed-off-by: Berthold Stoeger --- Subsurface-mobile.pro | 8 +- core/CMakeLists.txt | 8 +- core/{deco.c => deco.cpp} | 41 ++++--- core/gas.c | 2 +- core/gaspressures.c | 6 +- core/{planner.c => planner.cpp} | 31 +++--- core/{plannernotes.c => plannernotes.cpp} | 130 +++++++++++----------- core/{profile.c => profile.cpp} | 76 ++++++------- 8 files changed, 152 insertions(+), 150 deletions(-) rename core/{deco.c => deco.cpp} (93%) rename core/{planner.c => planner.cpp} (97%) rename core/{plannernotes.c => plannernotes.cpp} (89%) rename core/{profile.c => profile.cpp} (95%) diff --git a/Subsurface-mobile.pro b/Subsurface-mobile.pro index 70eef0d06..5846536ff 100644 --- a/Subsurface-mobile.pro +++ b/Subsurface-mobile.pro @@ -25,7 +25,7 @@ SOURCES += subsurface-mobile-main.cpp \ core/devicedetails.cpp \ core/downloadfromdcthread.cpp \ core/qtserialbluetooth.cpp \ - core/plannernotes.c \ + core/plannernotes.cpp \ core/uemis-downloader.cpp \ core/qthelper.cpp \ core/checkcloudconnection.cpp \ @@ -45,7 +45,7 @@ SOURCES += subsurface-mobile-main.cpp \ core/fulltext.cpp \ core/subsurfacestartup.cpp \ core/pref.c \ - core/profile.c \ + core/profile.cpp \ core/device.cpp \ core/dive.cpp \ core/divecomputer.c \ @@ -81,10 +81,10 @@ SOURCES += subsurface-mobile-main.cpp \ core/save-git.cpp \ core/datatrak.cpp \ core/ostctools.c \ - core/planner.c \ + core/planner.cpp \ core/save-xml.cpp \ core/cochran.cpp \ - core/deco.c \ + core/deco.cpp \ core/divesite.c \ core/equipment.c \ core/gas.c \ diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 3e42257e5..88e1ea312 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -53,7 +53,7 @@ set(SUBSURFACE_CORE_LIB_SRCS connectionlistmodel.h datatrak.cpp datatrak.h - deco.c + deco.cpp deco.h device.cpp device.h @@ -141,12 +141,12 @@ set(SUBSURFACE_CORE_LIB_SRCS picture.h pictureobj.cpp pictureobj.h - planner.c + planner.cpp planner.h - plannernotes.c + plannernotes.cpp pref.h pref.c - profile.c + profile.cpp profile.h qt-gui.h qt-init.cpp diff --git a/core/deco.c b/core/deco.cpp similarity index 93% rename from core/deco.c rename to core/deco.cpp index f422afa3d..7cb550f12 100644 --- a/core/deco.c +++ b/core/deco.cpp @@ -218,7 +218,7 @@ static double vpmb_tolerated_ambient_pressure(struct deco_state *ds, double refe return ds->tissue_n2_sat[ci] + ds->tissue_he_sat[ci] + vpmb_config.other_gases_pressure - total_gradient; } -double tissue_tolerance_calc(struct deco_state *ds, const struct dive *dive, double pressure, bool in_planner) +extern "C" double tissue_tolerance_calc(struct deco_state *ds, const struct dive *dive, double pressure, bool in_planner) { int ci = -1; double ret_tolerance_limit_ambient_pressure = 0.0; @@ -325,7 +325,7 @@ static double calc_surface_phase(double surface_pressure, double he_pressure, do return 0; } -void vpmb_start_gradient(struct deco_state *ds) +extern "C" void vpmb_start_gradient(struct deco_state *ds) { int ci; @@ -335,7 +335,7 @@ void vpmb_start_gradient(struct deco_state *ds) } } -void vpmb_next_gradient(struct deco_state *ds, double deco_time, double surface_pressure, bool in_planner) +extern "C" void vpmb_next_gradient(struct deco_state *ds, double deco_time, double surface_pressure, bool in_planner) { int ci; double n2_b, n2_c; @@ -381,7 +381,7 @@ static double solve_cubic(double A, double B, double C) } -void nuclear_regeneration(struct deco_state *ds, double time) +extern "C" void nuclear_regeneration(struct deco_state *ds, double time) { time /= 60.0; int ci; @@ -413,7 +413,7 @@ static double calc_inner_pressure(double crit_radius, double onset_tension, doub } // Calculates the crushing pressure in the given moment. Updates crushing_onset_tension and critical radius if needed -void calc_crushing_pressure(struct deco_state *ds, double pressure) +extern "C" void calc_crushing_pressure(struct deco_state *ds, double pressure) { int ci; double gradient; @@ -445,9 +445,8 @@ void calc_crushing_pressure(struct deco_state *ds, double pressure) } /* add period_in_seconds at the given pressure and gas to the deco calculation */ -void add_segment(struct deco_state *ds, double pressure, struct gasmix gasmix, int period_in_seconds, int ccpo2, enum divemode_t divemode, int sac, bool in_planner) +extern "C" void add_segment(struct deco_state *ds, double pressure, struct gasmix gasmix, int period_in_seconds, int ccpo2, enum divemode_t divemode, int, bool in_planner) { - UNUSED(sac); int ci; struct gas_pressures pressures; bool icd = false; @@ -479,7 +478,7 @@ void add_segment(struct deco_state *ds, double pressure, struct gasmix gasmix, i } #if DECO_CALC_DEBUG -void dump_tissues(struct deco_state *ds) +extern "C" void dump_tissues(struct deco_state *ds) { int ci; printf("N2 tissues:"); @@ -492,7 +491,7 @@ void dump_tissues(struct deco_state *ds) } #endif -void clear_vpmb_state(struct deco_state *ds) +extern "C" void clear_vpmb_state(struct deco_state *ds) { int ci; for (ci = 0; ci < 16; ci++) { @@ -504,7 +503,7 @@ void clear_vpmb_state(struct deco_state *ds) ds->max_bottom_ceiling_pressure.mbar = 0; } -void clear_deco(struct deco_state *ds, double surface_pressure, bool in_planner) +extern "C" void clear_deco(struct deco_state *ds, double surface_pressure, bool in_planner) { int ci; @@ -523,18 +522,18 @@ void clear_deco(struct deco_state *ds, double surface_pressure, bool in_planner) ds->ci_pointing_to_guiding_tissue = -1; } -void cache_deco_state(struct deco_state *src, struct deco_state **cached_datap) +extern "C" void cache_deco_state(struct deco_state *src, struct deco_state **cached_datap) { struct deco_state *data = *cached_datap; if (!data) { - data = malloc(sizeof(struct deco_state)); + data = (deco_state *)malloc(sizeof(struct deco_state)); *cached_datap = data; } *data = *src; } -void restore_deco_state(struct deco_state *data, struct deco_state *target, bool keep_vpmb_state) +extern "C" void restore_deco_state(struct deco_state *data, struct deco_state *target, bool keep_vpmb_state) { if (keep_vpmb_state) { int ci; @@ -551,7 +550,7 @@ void restore_deco_state(struct deco_state *data, struct deco_state *target, bool } -int deco_allowed_depth(double tissues_tolerance, double surface_pressure, const struct dive *dive, bool smooth) +extern "C" int deco_allowed_depth(double tissues_tolerance, double surface_pressure, const struct dive *dive, bool smooth) { int depth; double pressure_delta; @@ -570,7 +569,7 @@ int deco_allowed_depth(double tissues_tolerance, double surface_pressure, const return depth; } -void set_gf(short gflow, short gfhigh) +extern "C" void set_gf(short gflow, short gfhigh) { if (gflow != -1) buehlmann_config.gf_low = (double)gflow / 100.0; @@ -578,7 +577,7 @@ void set_gf(short gflow, short gfhigh) buehlmann_config.gf_high = (double)gfhigh / 100.0; } -void set_vpmb_conservatism(short conservatism) +extern "C" void set_vpmb_conservatism(short conservatism) { if (conservatism < 0) vpmb_config.conservatism = 0; @@ -588,7 +587,7 @@ void set_vpmb_conservatism(short conservatism) vpmb_config.conservatism = conservatism; } -double get_gf(struct deco_state *ds, double ambpressure_bar, const struct dive *dive) +extern "C" double get_gf(struct deco_state *ds, double ambpressure_bar, const struct dive *dive) { double surface_pressure_bar = get_surface_pressure_in_mbar(dive, true) / 1000.0; double gf_low = buehlmann_config.gf_low; @@ -602,7 +601,7 @@ double get_gf(struct deco_state *ds, double ambpressure_bar, const struct dive * return gf; } -double regressiona(const struct deco_state *ds) +extern "C" double regressiona(const struct deco_state *ds) { if (ds->sum1 > 1) { double avxy = ds->sumxy / ds->sum1; @@ -615,7 +614,7 @@ double regressiona(const struct deco_state *ds) return 0.0; } -double regressionb(const struct deco_state *ds) +extern "C" double regressionb(const struct deco_state *ds) { if (ds->sum1) return ds->sumy / ds->sum1 - ds->sumx * regressiona(ds) / ds->sum1; @@ -623,14 +622,14 @@ double regressionb(const struct deco_state *ds) return 0.0; } -void reset_regression(struct deco_state *ds) +extern "C" void reset_regression(struct deco_state *ds) { ds->sum1 = 0; ds->sumxx = ds->sumx = 0L; ds->sumy = ds->sumxy = 0.0; } -void update_regression(struct deco_state *ds, const struct dive *dive) +extern "C" void update_regression(struct deco_state *ds, const struct dive *dive) { if (!ds->plot_depth) return; diff --git a/core/gas.c b/core/gas.c index 96dc60479..7e8a98309 100644 --- a/core/gas.c +++ b/core/gas.c @@ -117,7 +117,7 @@ int pscr_o2(const double amb_pressure, struct gasmix mix) * *pressures = structure for communicating o2 sensor values from and gas pressures to the calling function. * *mix = structure containing cylinder gas mixture information. * divemode = the dive mode pertaining to this point in the dive profile. - * This function called by: calculate_gas_information_new() in profile.c; add_segment() in deco.c. + * This function called by: calculate_gas_information_new() in profile.cpp; add_segment() in deco.cpp. */ void fill_pressures(struct gas_pressures *pressures, const double amb_pressure, struct gasmix mix, double po2, enum divemode_t divemode) { diff --git a/core/gaspressures.c b/core/gaspressures.c index 7661e40c6..8bbc869e4 100644 --- a/core/gaspressures.c +++ b/core/gaspressures.c @@ -2,9 +2,9 @@ /* gaspressures.c * --------------- * This file contains the routines to calculate the gas pressures in the cylinders. - * The functions below support the code in profile.c. + * The functions below support the code in profile.cpp. * The high-level function is populate_pressure_information(), called by function - * create_plot_info_new() in profile.c. The other functions below are, in turn, + * create_plot_info_new() in profile.cpp. The other functions below are, in turn, * called by populate_pressure_information(). The calling sequence is as follows: * * populate_pressure_information() -> calc_pressure_time() @@ -362,7 +362,7 @@ static void debug_print_pressures(struct plot_info *pi) * pr_track_alloc structures for each cylinder. These pr_track_alloc structures ultimately allow for filling * the missing tank pressure values on the dive profile using the depth_pressure of the dive. To do this, it * calculates the summed pressure-time value for the duration of the dive and stores these * in the pr_track_alloc - * structures. This function is called by create_plot_info_new() in profile.c + * structures. This function is called by create_plot_info_new() in profile.cpp */ void populate_pressure_information(const struct dive *dive, const struct divecomputer *dc, struct plot_info *pi, int sensor) { diff --git a/core/planner.c b/core/planner.cpp similarity index 97% rename from core/planner.c rename to core/planner.cpp index 6d37d3101..926d976b8 100644 --- a/core/planner.c +++ b/core/planner.cpp @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-2.0 -/* planner.c +/* planner.cpp * * code that allows us to plan future dives * @@ -42,7 +42,7 @@ static int decostoplevels_imperial[] = { 0, 3048, 6096, 9144, 12192, 15240, 1828 325120, 345440, 365760, 386080 }; #if DEBUG_PLAN -void dump_plan(struct diveplan *diveplan) +extern "C" void dump_plan(struct diveplan *diveplan) { struct divedatapoint *dp; struct tm tm; @@ -65,7 +65,7 @@ void dump_plan(struct diveplan *diveplan) } #endif -bool diveplan_empty(struct diveplan *diveplan) +extern "C" bool diveplan_empty(struct diveplan *diveplan) { struct divedatapoint *dp; if (!diveplan || !diveplan->dp) @@ -80,7 +80,7 @@ bool diveplan_empty(struct diveplan *diveplan) } /* get the cylinder index at a certain time during the dive */ -int get_cylinderid_at_time(struct dive *dive, struct divecomputer *dc, duration_t time) +extern "C" int get_cylinderid_at_time(struct dive *dive, struct divecomputer *dc, duration_t time) { // we start with the first cylinder unless an event tells us otherwise int cylinder_idx = 0; @@ -327,7 +327,7 @@ static void create_dive_from_plan(struct diveplan *diveplan, struct dive *dive, return; } -void free_dps(struct diveplan *diveplan) +extern "C" void free_dps(struct diveplan *diveplan) { if (!diveplan) return; @@ -344,7 +344,7 @@ static struct divedatapoint *create_dp(int time_incr, int depth, int cylinderid, { struct divedatapoint *dp; - dp = malloc(sizeof(struct divedatapoint)); + dp = (divedatapoint *)malloc(sizeof(struct divedatapoint)); dp->time = time_incr; dp->depth.mm = depth; dp->cylinderid = cylinderid; @@ -371,7 +371,7 @@ static void add_to_end_of_diveplan(struct diveplan *diveplan, struct divedatapoi dp->time += lasttime; } -struct divedatapoint *plan_add_segment(struct diveplan *diveplan, int duration, int depth, int cylinderid, int po2, bool entered, enum divemode_t divemode) +extern "C" struct divedatapoint *plan_add_segment(struct diveplan *diveplan, int duration, int depth, int cylinderid, int po2, bool entered, enum divemode_t divemode) { struct divedatapoint *dp = create_dp(duration, depth, cylinderid, divemode == CCR ? po2 : 0); dp->entered = entered; @@ -413,7 +413,7 @@ static struct gaschanges *analyze_gaslist(struct diveplan *diveplan, struct dive if (dp->depth.mm <= depth) { int i = 0; nr++; - gaschanges = realloc(gaschanges, nr * sizeof(struct gaschanges)); + gaschanges = (struct gaschanges *)realloc(gaschanges, nr * sizeof(struct gaschanges)); while (i < nr - 1) { if (dp->depth.mm < gaschanges[i].depth) { memmove(gaschanges + i + 1, gaschanges + i, (nr - i - 1) * sizeof(struct gaschanges)); @@ -454,7 +454,7 @@ static int *sort_stops(int *dstops, int dnr, struct gaschanges *gstops, int gnr) { int i, gi, di; int total = dnr + gnr; - int *stoplevels = malloc(total * sizeof(int)); + int *stoplevels = (int *)malloc(total * sizeof(int)); /* no gaschanges */ if (gnr == 0) { @@ -502,9 +502,8 @@ static int *sort_stops(int *dstops, int dnr, struct gaschanges *gstops, int gnr) return stoplevels; } -int ascent_velocity(int depth, int avg_depth, int bottom_time) +extern "C" int ascent_velocity(int depth, int avg_depth, int) { - UNUSED(bottom_time); /* We need to make this configurable */ /* As an example (and possibly reasonable default) this is the Tech 1 provedure according @@ -655,7 +654,7 @@ static void average_max_depth(struct diveplan *dive, int *avg_depth, int *max_de *avg_depth = *max_depth = 0; } -bool plan(struct deco_state *ds, struct diveplan *diveplan, struct dive *dive, int timestep, struct decostop *decostoptable, struct deco_state **cached_datap, bool is_planner, bool show_disclaimer) +extern "C" bool plan(struct deco_state *ds, struct diveplan *diveplan, struct dive *dive, int timestep, struct decostop *decostoptable, struct deco_state **cached_datap, bool is_planner, bool show_disclaimer) { int bottom_depth; @@ -715,7 +714,7 @@ bool plan(struct deco_state *ds, struct diveplan *diveplan, struct dive *dive, i create_dive_from_plan(diveplan, dive, is_planner); // Do we want deco stop array in metres or feet? - if (prefs.units.length == METERS ) { + if (prefs.units.length == units::METERS ) { decostoplevels = decostoplevels_metric; decostoplevelcount = sizeof(decostoplevels_metric) / sizeof(int); } else { @@ -929,7 +928,7 @@ bool plan(struct deco_state *ds, struct diveplan *diveplan, struct dive *dive, i last_segment_min_switch = false; clock += TIMESTEP; depth -= deltad; - /* Print VPM-Gradient as gradient factor, this has to be done from within deco.c */ + /* Print VPM-Gradient as gradient factor, this has to be done from within deco.cpp */ if (decodive) ds->plot_depth = depth; } while (depth > 0 && depth > stoplevels[stopidx]); @@ -1179,7 +1178,7 @@ static int get_permille(const char *begin, const char **end) return value; } -int validate_gas(const char *text, struct gasmix *gas) +extern "C" int validate_gas(const char *text, struct gasmix *gas) { int o2, he; @@ -1226,7 +1225,7 @@ int validate_gas(const char *text, struct gasmix *gas) return 1; } -int validate_po2(const char *text, int *mbar_po2) +extern "C" int validate_po2(const char *text, int *mbar_po2) { int po2; diff --git a/core/plannernotes.c b/core/plannernotes.cpp similarity index 89% rename from core/plannernotes.c rename to core/plannernotes.cpp index e2f84284c..494f24348 100644 --- a/core/plannernotes.c +++ b/core/plannernotes.cpp @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-2.0 -/* planner.c +/* plannernotes.cpp * * code that allows us to plan future dives * @@ -75,7 +75,7 @@ static void add_icd_entry(struct membuffer *b, struct icd_data *icdvalues, bool ambientpressure_mbar * -icdvalues->dHe / 5e6f, translate("gettextFromC", "bar")); } -const char *get_planner_disclaimer() +extern "C" const char *get_planner_disclaimer() { return translate("gettextFromC", "DISCLAIMER / WARNING: THIS IMPLEMENTATION OF THE %s " "ALGORITHM AND A DIVE PLANNER IMPLEMENTATION BASED ON THAT HAS " @@ -84,7 +84,7 @@ const char *get_planner_disclaimer() } /* Returns newly allocated buffer. Must be freed by caller */ -char *get_planner_disclaimer_formatted() +extern "C" char *get_planner_disclaimer_formatted() { struct membuffer buf = { 0 }; const char *deco = decoMode(true) == VPMB ? translate("gettextFromC", "VPM-B") @@ -93,7 +93,7 @@ char *get_planner_disclaimer_formatted() return detach_cstring(&buf); } -void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool show_disclaimer, int error) +extern "C" void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool show_disclaimer, int error) { struct membuffer buf = { 0 }; struct membuffer icdbuf = { 0 }; @@ -436,30 +436,34 @@ void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool show_d } put_string(&buf, "
\n"); - const char *depth_unit; - int altitude = (int) get_depth_units((int) (pressure_to_altitude(diveplan->surface_pressure)), NULL, &depth_unit); + { + const char *depth_unit; + int altitude = (int) get_depth_units((int) (pressure_to_altitude(diveplan->surface_pressure)), NULL, &depth_unit); - put_format_loc(&buf, translate("gettextFromC", "ATM pressure: %dmbar (%d%s)
\n\n"), diveplan->surface_pressure, altitude, depth_unit); + put_format_loc(&buf, translate("gettextFromC", "ATM pressure: %dmbar (%d%s)
\n\n"), diveplan->surface_pressure, altitude, depth_unit); + } /* Get SAC values and units for printing it in gas consumption */ - double bottomsacvalue, decosacvalue; - int sacdecimals; - const char* sacunit; + { + double bottomsacvalue, decosacvalue; + int sacdecimals; + const char* sacunit; - bottomsacvalue = get_volume_units(prefs.bottomsac, &sacdecimals, &sacunit); - decosacvalue = get_volume_units(prefs.decosac, NULL, NULL); + bottomsacvalue = get_volume_units(prefs.bottomsac, &sacdecimals, &sacunit); + decosacvalue = get_volume_units(prefs.decosac, NULL, NULL); - /* Reduce number of decimals from 1 to 0 for bar/min, keep 2 for cuft/min */ - if (sacdecimals==1) sacdecimals--; + /* Reduce number of decimals from 1 to 0 for bar/min, keep 2 for cuft/min */ + if (sacdecimals==1) sacdecimals--; - /* Print the gas consumption next.*/ - if (dive->dc.divemode == CCR) - temp = strdup(translate("gettextFromC", "Gas consumption (CCR legs excluded):")); - else - asprintf_loc(&temp, "%s %.*f|%.*f%s/min):", translate("gettextFromC", "Gas consumption (based on SAC"), - sacdecimals, bottomsacvalue, sacdecimals, decosacvalue, sacunit); - put_format(&buf, "
\n%s
\n", temp); - free(temp); + /* Print the gas consumption next.*/ + if (dive->dc.divemode == CCR) + temp = strdup(translate("gettextFromC", "Gas consumption (CCR legs excluded):")); + else + asprintf_loc(&temp, "%s %.*f|%.*f%s/min):", translate("gettextFromC", "Gas consumption (based on SAC"), + sacdecimals, bottomsacvalue, sacdecimals, decosacvalue, sacunit); + put_format(&buf, "
\n%s
\n", temp); + free(temp); + } /* Print gas consumption: This loop covers all cylinders */ for (int gasidx = 0; gasidx < dive->cylinders.nr; gasidx++) { @@ -570,53 +574,55 @@ void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool show_d } free_buffer(&icdbuf); - /* Print warnings for pO2 */ - dp = diveplan->dp; - bool o2warning_exist = false; - enum divemode_t current_divemode; - double amb; - const struct event *evd = NULL; - current_divemode = UNDEF_COMP_TYPE; + /* Print warnings for pO2 (move into separate function?) */ + { + dp = diveplan->dp; + bool o2warning_exist = false; + enum divemode_t current_divemode; + double amb; + const struct event *evd = NULL; + current_divemode = UNDEF_COMP_TYPE; - if (dive->dc.divemode != CCR) { - while (dp) { - if (dp->time != 0) { - struct gas_pressures pressures; - struct gasmix gasmix = get_cylinder(dive, dp->cylinderid)->gasmix; + if (dive->dc.divemode != CCR) { + while (dp) { + if (dp->time != 0) { + struct gas_pressures pressures; + struct gasmix gasmix = get_cylinder(dive, dp->cylinderid)->gasmix; - current_divemode = get_current_divemode(&dive->dc, dp->time, &evd, ¤t_divemode); - amb = depth_to_atm(dp->depth.mm, dive); - fill_pressures(&pressures, amb, gasmix, (current_divemode == OC) ? 0.0 : amb * gasmix.o2.permille / 1000.0, current_divemode); + current_divemode = get_current_divemode(&dive->dc, dp->time, &evd, ¤t_divemode); + amb = depth_to_atm(dp->depth.mm, dive); + fill_pressures(&pressures, amb, gasmix, (current_divemode == OC) ? 0.0 : amb * gasmix.o2.permille / 1000.0, current_divemode); - if (pressures.o2 > (dp->entered ? prefs.bottompo2 : prefs.decopo2) / 1000.0) { - const char *depth_unit; - int decimals; - double depth_value = get_depth_units(dp->depth.mm, &decimals, &depth_unit); - if (!o2warning_exist) - put_string(&buf, "
\n"); - o2warning_exist = true; - asprintf_loc(&temp, translate("gettextFromC", "high pO₂ value %.2f at %d:%02u with gas %s at depth %.*f %s"), - pressures.o2, FRACTION(dp->time, 60), gasname(gasmix), decimals, depth_value, depth_unit); - put_format(&buf, "%s %s
\n", translate("gettextFromC", "Warning:"), temp); - free(temp); - } else if (pressures.o2 < 0.16) { - const char *depth_unit; - int decimals; - double depth_value = get_depth_units(dp->depth.mm, &decimals, &depth_unit); - if (!o2warning_exist) - put_string(&buf, "
"); - o2warning_exist = true; - asprintf_loc(&temp, translate("gettextFromC", "low pO₂ value %.2f at %d:%02u with gas %s at depth %.*f %s"), - pressures.o2, FRACTION(dp->time, 60), gasname(gasmix), decimals, depth_value, depth_unit); - put_format(&buf, "%s %s
\n", translate("gettextFromC", "Warning:"), temp); - free(temp); + if (pressures.o2 > (dp->entered ? prefs.bottompo2 : prefs.decopo2) / 1000.0) { + const char *depth_unit; + int decimals; + double depth_value = get_depth_units(dp->depth.mm, &decimals, &depth_unit); + if (!o2warning_exist) + put_string(&buf, "
\n"); + o2warning_exist = true; + asprintf_loc(&temp, translate("gettextFromC", "high pO₂ value %.2f at %d:%02u with gas %s at depth %.*f %s"), + pressures.o2, FRACTION(dp->time, 60), gasname(gasmix), decimals, depth_value, depth_unit); + put_format(&buf, "%s %s
\n", translate("gettextFromC", "Warning:"), temp); + free(temp); + } else if (pressures.o2 < 0.16) { + const char *depth_unit; + int decimals; + double depth_value = get_depth_units(dp->depth.mm, &decimals, &depth_unit); + if (!o2warning_exist) + put_string(&buf, "
"); + o2warning_exist = true; + asprintf_loc(&temp, translate("gettextFromC", "low pO₂ value %.2f at %d:%02u with gas %s at depth %.*f %s"), + pressures.o2, FRACTION(dp->time, 60), gasname(gasmix), decimals, depth_value, depth_unit); + put_format(&buf, "%s %s
\n", translate("gettextFromC", "Warning:"), temp); + free(temp); + } } + dp = dp->next; } - dp = dp->next; } + if (o2warning_exist) + put_string(&buf, "
\n"); } - if (o2warning_exist) - put_string(&buf, "
\n"); finished: free(dive->notes); dive->notes = detach_cstring(&buf); diff --git a/core/profile.c b/core/profile.cpp similarity index 95% rename from core/profile.c rename to core/profile.cpp index dc292d7b1..b38eaca09 100644 --- a/core/profile.c +++ b/core/profile.cpp @@ -30,7 +30,7 @@ #define MAX_PROFILE_DECO 7200 -extern int ascent_velocity(int depth, int avg_depth, int bottom_time); +extern "C" int ascent_velocity(int depth, int avg_depth, int bottom_time); #ifdef DEBUG_PI /* debugging tool - not normally used */ @@ -66,7 +66,7 @@ static void dump_pi(struct plot_info *pi) * 30 minutes or 90 ft, just so that small dives show * up as such unless zoom is enabled. */ -int get_maxtime(const struct plot_info *pi) +extern "C" int get_maxtime(const struct plot_info *pi) { int seconds = pi->maxtime; int min = prefs.zoomed_plot ? 30 : 30 * 60; @@ -74,7 +74,7 @@ int get_maxtime(const struct plot_info *pi) } /* get the maximum depth to which we want to plot */ -int get_maxdepth(const struct plot_info *pi) +extern "C" int get_maxdepth(const struct plot_info *pi) { /* 3m to spare */ int mm = pi->maxdepth + 3000; @@ -185,7 +185,7 @@ static void analyze_plot_info(struct plot_info *pi) * Some dive computers give cylinder indices, some * give just the gas mix. */ -int get_cylinder_index(const struct dive *dive, const struct event *ev) +extern "C" int get_cylinder_index(const struct dive *dive, const struct event *ev) { int best; struct gasmix mix; @@ -206,7 +206,7 @@ int get_cylinder_index(const struct dive *dive, const struct event *ev) return best < 0 ? 0 : best; } -struct event *get_next_event_mutable(struct event *event, const char *name) +extern "C" struct event *get_next_event_mutable(struct event *event, const char *name) { if (!name || !*name) return NULL; @@ -218,7 +218,7 @@ struct event *get_next_event_mutable(struct event *event, const char *name) return event; } -const struct event *get_next_event(const struct event *event, const char *name) +extern "C" const struct event *get_next_event(const struct event *event, const char *name) { return get_next_event_mutable((struct event *)event, name); } @@ -246,9 +246,8 @@ static int set_setpoint(struct plot_info *pi, int i, int setpoint, int end) return i; } -static void check_setpoint_events(const struct dive *dive, const struct divecomputer *dc, struct plot_info *pi) +static void check_setpoint_events(const struct dive *, const struct divecomputer *dc, struct plot_info *pi) { - UNUSED(dive); int i = 0; pressure_t setpoint; setpoint.mbar = 0; @@ -381,7 +380,7 @@ static void insert_entry(struct plot_info *pi, int idx, int time, int depth, int entry->bearing = -1; } -void free_plot_info_data(struct plot_info *pi) +extern "C" void free_plot_info_data(struct plot_info *pi) { free(pi->entry); free(pi->pressures); @@ -390,7 +389,6 @@ void free_plot_info_data(struct plot_info *pi) static void populate_plot_entries(const struct dive *dive, const struct divecomputer *dc, struct plot_info *pi) { - UNUSED(dive); int idx, maxtime, nr, i; int lastdepth, lasttime, lasttemp = 0; struct plot_data *plot_data; @@ -407,10 +405,10 @@ static void populate_plot_entries(const struct dive *dive, const struct divecomp * past "maxtime" in the original sample data) */ nr = dc->samples + 6 + maxtime / 10 + count_events(dc); - plot_data = calloc(nr, sizeof(struct plot_data)); + plot_data = (struct plot_data *)calloc(nr, sizeof(struct plot_data)); pi->entry = plot_data; pi->nr_cylinders = dive->cylinders.nr; - pi->pressures = calloc(nr * (size_t)pi->nr_cylinders, sizeof(struct plot_pressure_data)); + pi->pressures = (struct plot_pressure_data *)calloc(nr * (size_t)pi->nr_cylinders, sizeof(struct plot_pressure_data)); if (!plot_data) return; pi->nr = nr; @@ -684,11 +682,11 @@ static void calculate_sac(const struct dive *dive, const struct divecomputer *dc const struct event *ev = NULL; bool *gases, *gases_scratch; - gases = calloc(pi->nr_cylinders, sizeof(*gases)); + gases = (bool *)calloc(pi->nr_cylinders, sizeof(*gases)); /* This might be premature optimization, but let's allocate the gas array for * the fill_sac function only once an not once per sample */ - gases_scratch = malloc(pi->nr_cylinders * sizeof(*gases)); + gases_scratch = (bool *)malloc(pi->nr_cylinders * sizeof(*gases)); for (int i = 0; i < pi->nr; i++) { struct plot_data *entry = pi->entry + i; @@ -707,7 +705,7 @@ static void calculate_sac(const struct dive *dive, const struct divecomputer *dc static void populate_secondary_sensor_data(const struct divecomputer *dc, struct plot_info *pi) { - int *seen = calloc(pi->nr_cylinders, sizeof(*seen)); + int *seen = (int *)calloc(pi->nr_cylinders, sizeof(*seen)); for (int idx = 0; idx < pi->nr; ++idx) for (int c = 0; c < pi->nr_cylinders; ++c) if (get_plot_pressure_data(pi, idx, SENSOR_PR, c)) @@ -754,9 +752,9 @@ static void setup_gas_sensor_pressure(const struct dive *dive, const struct dive /* FIXME: The planner uses a dummy one-past-end cylinder for surface air! */ int num_cyl = pi->nr_cylinders + 1; - int *seen = malloc(num_cyl * sizeof(*seen)); - int *first = malloc(num_cyl * sizeof(*first)); - int *last = malloc(num_cyl * sizeof(*last)); + int *seen = (int *)malloc(num_cyl * sizeof(*seen)); + int *first = (int *)malloc(num_cyl * sizeof(*first)); + int *last = (int *)malloc(num_cyl * sizeof(*last)); const struct divecomputer *secondary; for (i = 0; i < num_cyl; i++) { @@ -1127,7 +1125,7 @@ static void calculate_deco_information(struct deco_state *ds, const struct deco_ /* Sort the o2 pressure values. There are so few that a simple bubble sort * will do */ -void sort_o2_pressures(int *sensorn, int np, struct plot_data *entry) +extern "C" void sort_o2_pressures(int *sensorn, int np, struct plot_data *entry) { int smallest, position, old; @@ -1308,7 +1306,7 @@ static void debug_print_profiledata(struct plot_info *pi) /* * Initialize a plot_info structure to all-zeroes */ -void init_plot_info(struct plot_info *pi) +extern "C" void init_plot_info(struct plot_info *pi) { memset(pi, 0, sizeof(*pi)); } @@ -1323,7 +1321,7 @@ void init_plot_info(struct plot_info *pi) * The old data will be freed. Before the first call, the plot * info must be initialized with init_plot_info(). */ -void create_plot_info_new(const struct dive *dive, const struct divecomputer *dc, struct plot_info *pi, const struct deco_state *planner_ds) +extern "C" void create_plot_info_new(const struct dive *dive, const struct divecomputer *dc, struct plot_info *pi, const struct deco_state *planner_ds) { int o2, he, o2max; struct deco_state plot_deco_state; @@ -1333,14 +1331,14 @@ void create_plot_info_new(const struct dive *dive, const struct divecomputer *dc calculate_max_limits_new(dive, dc, pi, in_planner); get_dive_gas(dive, &o2, &he, &o2max); if (dc->divemode == FREEDIVE) { - pi->dive_type = FREEDIVING; + pi->dive_type = plot_info::FREEDIVING; } else if (he > 0) { - pi->dive_type = TRIMIX; + pi->dive_type = plot_info::TRIMIX; } else { if (o2) - pi->dive_type = NITROX; + pi->dive_type = plot_info::NITROX; else - pi->dive_type = AIR; + pi->dive_type = plot_info::AIR; } populate_plot_entries(dive, dc, pi); @@ -1414,23 +1412,23 @@ static void plot_string(const struct dive *d, const struct plot_info *pi, int id if (prefs.ead) { switch (pi->dive_type) { - case NITROX: + case plot_info::NITROX: if (entry->ead > 0) { ead = lrint(get_depth_units(entry->ead, NULL, &depth_unit)); put_format_loc(b, translate("gettextFromC", "EAD: %d%s\nEADD: %d%s / %.1fg/ℓ\n"), ead, depth_unit, eadd, depth_unit, entry->density); break; } - case TRIMIX: + case plot_info::TRIMIX: if (entry->end > 0) { end = lrint(get_depth_units(entry->end, NULL, &depth_unit)); put_format_loc(b, translate("gettextFromC", "END: %d%s\nEADD: %d%s / %.1fg/ℓ\n"), end, depth_unit, eadd, depth_unit, entry->density); break; } - case AIR: + case plot_info::AIR: if (entry->density > 0) { put_format_loc(b, translate("gettextFromC", "Density: %.1fg/ℓ\n"), entry->density); } - case FREEDIVING: + case plot_info::FREEDIVING: /* nothing */ break; } @@ -1519,7 +1517,7 @@ static void plot_string(const struct dive *d, const struct plot_info *pi, int id strip_mb(b); } -int get_plot_details_new(const struct dive *d, const struct plot_info *pi, int time, struct membuffer *mb) +extern "C" int get_plot_details_new(const struct dive *d, const struct plot_info *pi, int time, struct membuffer *mb) { int i; @@ -1535,11 +1533,11 @@ int get_plot_details_new(const struct dive *d, const struct plot_info *pi, int t } /* Compare two plot_data entries and writes the results into a string */ -void compare_samples(const struct dive *d, const struct plot_info *pi, int idx1, int idx2, char *buf, int bufsize, bool sum) +extern "C" void compare_samples(const struct dive *d, const struct plot_info *pi, int idx1, int idx2, char *buf, int bufsize, bool sum) { struct plot_data *start, *stop, *data; const char *depth_unit, *pressure_unit, *vertical_speed_unit; - char *buf2 = malloc(bufsize); + char *buf2 = (char *)malloc(bufsize); int avg_speed, max_asc_speed, max_desc_speed; int delta_depth, avg_depth, max_depth, min_depth; int pressurevalue; @@ -1578,11 +1576,11 @@ void compare_samples(const struct dive *d, const struct plot_info *pi, int idx1, last_sec = start->sec; volume_t cylinder_volume = { .mliter = 0, }; - int *start_pressures = calloc((size_t)pi->nr_cylinders, sizeof(int)); - int *last_pressures = calloc((size_t)pi->nr_cylinders, sizeof(int)); - int *bar_used = calloc((size_t)pi->nr_cylinders, sizeof(int)); - int *volumes_used = calloc((size_t)pi->nr_cylinders, sizeof(int)); - bool *cylinder_is_used = calloc((size_t)pi->nr_cylinders, sizeof(bool)); + int *start_pressures = (int *)calloc((size_t)pi->nr_cylinders, sizeof(int)); + int *last_pressures = (int *)calloc((size_t)pi->nr_cylinders, sizeof(int)); + int *bar_used = (int *)calloc((size_t)pi->nr_cylinders, sizeof(int)); + int *volumes_used = (int *)calloc((size_t)pi->nr_cylinders, sizeof(int)); + bool *cylinder_is_used = (bool *)calloc((size_t)pi->nr_cylinders, sizeof(bool)); data = start; for (int i = idx1; i < idx2; ++i) { @@ -1603,7 +1601,7 @@ void compare_samples(const struct dive *d, const struct plot_info *pi, int idx1, if (data->depth > max_depth) max_depth = data->depth; - for (unsigned cylinder_index = 0; cylinder_index < pi->nr_cylinders; cylinder_index++) { + for (int cylinder_index = 0; cylinder_index < pi->nr_cylinders; cylinder_index++) { int next_pressure = get_plot_pressure(pi, i, cylinder_index); if (next_pressure && !start_pressures[cylinder_index]) start_pressures[cylinder_index] = next_pressure; @@ -1669,7 +1667,7 @@ void compare_samples(const struct dive *d, const struct plot_info *pi, int idx1, int total_volume_used = 0; bool cylindersizes_are_identical = true; bool sac_is_determinable = true; - for (unsigned cylinder_index = 0; cylinder_index < pi->nr_cylinders; cylinder_index++) + for (int cylinder_index = 0; cylinder_index < pi->nr_cylinders; cylinder_index++) if (cylinder_is_used[cylinder_index]) { total_bar_used += bar_used[cylinder_index]; total_volume_used += volumes_used[cylinder_index];