core: convert pref.c and units.c to C++

Convert both files simultanously, because the SI_UNITS define works
either under C or under C++.

This was painful, because initialization of struct-members has to
be done in order of definition in C++. And it was completely out
of order. However, as long as not all is C++, we can't use
default initialization directly in the struct definition. :(

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-05-02 07:56:47 +02:00
parent 782d54a6e9
commit 98693c6ccb
5 changed files with 138 additions and 114 deletions

View file

@ -45,7 +45,7 @@ SOURCES += subsurface-mobile-main.cpp \
core/fulltext.cpp \ core/fulltext.cpp \
core/subsurfacestartup.cpp \ core/subsurfacestartup.cpp \
core/subsurface-string.cpp \ core/subsurface-string.cpp \
core/pref.c \ core/pref.cpp \
core/profile.cpp \ core/profile.cpp \
core/device.cpp \ core/device.cpp \
core/dive.cpp \ core/dive.cpp \
@ -98,7 +98,7 @@ SOURCES += subsurface-mobile-main.cpp \
core/taxonomy.c \ core/taxonomy.c \
core/time.cpp \ core/time.cpp \
core/trip.c \ core/trip.c \
core/units.c \ core/units.cpp \
core/uemis.cpp \ core/uemis.cpp \
core/btdiscovery.cpp \ core/btdiscovery.cpp \
core/connectionlistmodel.cpp \ core/connectionlistmodel.cpp \

View file

@ -145,7 +145,7 @@ set(SUBSURFACE_CORE_LIB_SRCS
planner.h planner.h
plannernotes.cpp plannernotes.cpp
pref.h pref.h
pref.c pref.cpp
profile.cpp profile.cpp
profile.h profile.h
qt-gui.h qt-gui.h
@ -188,7 +188,7 @@ set(SUBSURFACE_CORE_LIB_SRCS
uemis.cpp uemis.cpp
uemis.h uemis.h
units.h units.h
units.c units.cpp
uploadDiveShare.cpp uploadDiveShare.cpp
uploadDiveShare.h uploadDiveShare.h
uploadDiveLogsDE.cpp uploadDiveLogsDE.cpp

View file

@ -5,10 +5,96 @@
struct preferences prefs, git_prefs; struct preferences prefs, git_prefs;
struct preferences default_prefs = { struct preferences default_prefs = {
.animation_speed = 500,
.cloud_base_url = "https://" CLOUD_HOST_EU "/", // if we don't know any better, use the European host .cloud_base_url = "https://" CLOUD_HOST_EU "/", // if we don't know any better, use the European host
.units = SI_UNITS, #if defined(SUBSURFACE_MOBILE)
.unit_system = METRIC, .cloud_timeout = 10,
.coordinates_traditional = true, #else
.cloud_timeout = 5,
#endif
.sync_dc_time = false,
.display_invalid_dives = false,
.divelist_font = NULL,
.font_size = -1,
.mobile_scale = 1.0,
.show_developer = true,
.three_m_based_grid = false,
.map_short_names = false,
.default_cylinder = NULL,
.include_unused_tanks = false,
.display_default_tank_infos = true,
.auto_recalculate_thumbnails = true,
.extract_video_thumbnails = true,
.extract_video_thumbnails_position = 20, // The first fifth seems like a reasonable place
.ffmpeg_executable = NULL,
.defaultsetpoint = 1100,
.default_filename = NULL,
.default_file_behavior = LOCAL_DEFAULT_FILE,
.o2consumption = 720,
.pscr_ratio = 100,
.use_default_file = true,
.extraEnvironmentalDefault = false,
.salinityEditDefault = false,
.geocoding = {
.category = { TC_NONE, TC_NONE, TC_NONE }
},
.date_format = NULL,
.date_format_override = false,
.date_format_short = NULL,
.locale = {
.use_system_language = true,
},
.time_format = NULL,
.time_format_override = false,
.proxy_auth = false,
.proxy_host = NULL,
.proxy_port = 0,
.proxy_type = 0,
.proxy_user = NULL,
.proxy_pass = NULL,
.ascratelast6m = 9000 / 60,
.ascratestops = 9000 / 60,
.ascrate50 = 9000 / 60,
.ascrate75 = 9000 / 60,
.bestmixend = { 30000 },
.bottompo2 = 1400,
.bottomsac = 20000,
.decopo2 = 1600,
.decosac = 17000,
.descrate = 18000 / 60,
.display_duration = true,
.display_runtime = true,
.display_transitions = true,
.display_variations = false,
.doo2breaks = false,
.dobailout = false,
.o2narcotic = true,
.drop_stone_mode = false,
.last_stop = false,
.min_switch_duration = 60,
.surface_segment = 0,
.planner_deco_mode = BUEHLMANN,
.problemsolvingtime = 4,
.reserve_gas=40000,
.sacfactor = 400,
.safetystop = true,
.switch_at_req_stop = false,
.verbatim_plan = false,
.calcalltissues = false,
.calcceiling = false,
.calcceiling3m = false,
.calcndltts = false,
.decoinfo = true,
.dcceiling = true,
.display_deco_mode = BUEHLMANN,
.ead = false,
.gfhigh = 75,
.gflow = 30,
.gf_low_at_maxdepth = false,
.hrgraph = false,
.mod = false,
.modpO2 = 1.6,
.percentagegraph = false,
.pp_graphs = { .pp_graphs = {
.po2 = false, .po2 = false,
.pn2 = false, .pn2 = false,
@ -18,86 +104,27 @@ struct preferences default_prefs = {
.pn2_threshold = 4.0, .pn2_threshold = 4.0,
.phe_threshold = 13.0, .phe_threshold = 13.0,
}, },
.mod = false,
.modpO2 = 1.6,
.ead = false,
.hrgraph = false,
.percentagegraph = false,
.dcceiling = true,
.redceiling = false, .redceiling = false,
.calcceiling = false, .rulergraph = false,
.calcceiling3m = false,
.calcndltts = false,
.decoinfo = true,
.gflow = 30,
.gfhigh = 75,
.animation_speed = 500,
.gf_low_at_maxdepth = false,
.show_ccr_setpoint = false,
.show_ccr_sensors = false,
.show_scr_ocpo2 = false,
.font_size = -1,
.mobile_scale = 1.0,
.display_invalid_dives = false,
.show_sac = false,
.include_unused_tanks = false,
.display_default_tank_infos = true,
.show_average_depth = true, .show_average_depth = true,
.show_ccr_sensors = false,
.show_ccr_setpoint = false,
.show_icd = false, .show_icd = false,
.ascrate75 = 9000 / 60,
.ascrate50 = 9000 / 60,
.ascratestops = 9000 / 60,
.ascratelast6m = 9000 / 60,
.descrate = 18000 / 60,
.sacfactor = 400,
.problemsolvingtime = 4,
.bottompo2 = 1400,
.decopo2 = 1600,
.bestmixend.mm = 30000,
.doo2breaks = false,
.dobailout = false,
.drop_stone_mode = false,
.switch_at_req_stop = false,
.min_switch_duration = 60,
.surface_segment = 0,
.last_stop = false,
.verbatim_plan = false,
.display_runtime = true,
.display_duration = true,
.display_transitions = true,
.display_variations = false,
.o2narcotic = true,
.safetystop = true,
.bottomsac = 20000,
.decosac = 17000,
.reserve_gas=40000,
.o2consumption = 720,
.pscr_ratio = 100,
.show_pictures_in_profile = true, .show_pictures_in_profile = true,
.show_sac = false,
.show_scr_ocpo2 = false,
.tankbar = false, .tankbar = false,
.defaultsetpoint = 1100,
.geocoding = {
.category = { 0 }
},
.locale = {
.use_system_language = true,
},
.planner_deco_mode = BUEHLMANN,
.vpmb_conservatism = 3, .vpmb_conservatism = 3,
#if defined(SUBSURFACE_MOBILE) .zoomed_plot = false,
.cloud_timeout = 10, .infobox = true,
#else .coordinates_traditional = true,
.cloud_timeout = 5, .unit_system = METRIC,
#endif .units = SI_UNITS,
.auto_recalculate_thumbnails = true, .update_manager = { false, NULL, 0 }
.extract_video_thumbnails = true,
.extract_video_thumbnails_position = 20, // The first fifth seems like a reasonable place
.three_m_based_grid = false,
.sync_dc_time = false,
}; };
/* copy a preferences block, including making copies of all included strings */ /* copy a preferences block, including making copies of all included strings */
void copy_prefs(struct preferences *src, struct preferences *dest) extern "C" void copy_prefs(struct preferences *src, struct preferences *dest)
{ {
*dest = *src; *dest = *src;
dest->divelist_font = copy_string(src->divelist_font); dest->divelist_font = copy_string(src->divelist_font);
@ -122,7 +149,7 @@ void copy_prefs(struct preferences *src, struct preferences *dest)
* These are not real leaks but they plug the holes found by eg. * These are not real leaks but they plug the holes found by eg.
* valgrind so you can find the real leaks. * valgrind so you can find the real leaks.
*/ */
void free_prefs(void) extern "C" void free_prefs(void)
{ {
// nop // nop
} }

View file

@ -3,28 +3,25 @@
#include "gettext.h" #include "gettext.h"
#include "pref.h" #include "pref.h"
#define IMPERIAL_UNITS \
{ \
.length = FEET, .volume = CUFT, .pressure = PSI, .temperature = FAHRENHEIT, .weight = LBS, \
.vertical_speed_time = MINUTES, .duration_units = MIXED, .show_units_table = false \
}
const struct units SI_units = SI_UNITS; const struct units SI_units = SI_UNITS;
const struct units IMPERIAL_units = IMPERIAL_UNITS; const struct units IMPERIAL_units = {
.length = units::FEET, .volume = units::CUFT, .pressure = units::PSI, .temperature = units::FAHRENHEIT, .weight = units::LBS,
.vertical_speed_time = units::MINUTES, .duration_units = units::MIXED, .show_units_table = false
};
int get_pressure_units(int mb, const char **units) extern "C" int get_pressure_units(int mb, const char **units)
{ {
int pressure; int pressure;
const char *unit; const char *unit;
const struct units *units_p = get_units(); const struct units *units_p = get_units();
switch (units_p->pressure) { switch (units_p->pressure) {
case BAR: case units::BAR:
default: default:
pressure = (mb + 500) / 1000; pressure = (mb + 500) / 1000;
unit = translate("gettextFromC", "bar"); unit = translate("gettextFromC", "bar");
break; break;
case PSI: case units::PSI:
pressure = (int)lrint(mbar_to_PSI(mb)); pressure = (int)lrint(mbar_to_PSI(mb));
unit = translate("gettextFromC", "psi"); unit = translate("gettextFromC", "psi");
break; break;
@ -34,13 +31,13 @@ int get_pressure_units(int mb, const char **units)
return pressure; return pressure;
} }
double get_temp_units(unsigned int mk, const char **units) extern "C" double get_temp_units(unsigned int mk, const char **units)
{ {
double deg; double deg;
const char *unit; const char *unit;
const struct units *units_p = get_units(); const struct units *units_p = get_units();
if (units_p->temperature == FAHRENHEIT) { if (units_p->temperature == units::FAHRENHEIT) {
deg = mkelvin_to_F(mk); deg = mkelvin_to_F(mk);
unit = "°F"; unit = "°F";
} else { } else {
@ -52,7 +49,7 @@ double get_temp_units(unsigned int mk, const char **units)
return deg; return deg;
} }
double get_volume_units(unsigned int ml, int *frac, const char **units) extern "C" double get_volume_units(unsigned int ml, int *frac, const char **units)
{ {
int decimals; int decimals;
double vol; double vol;
@ -60,13 +57,13 @@ double get_volume_units(unsigned int ml, int *frac, const char **units)
const struct units *units_p = get_units(); const struct units *units_p = get_units();
switch (units_p->volume) { switch (units_p->volume) {
case LITER: case units::LITER:
default: default:
vol = ml / 1000.0; vol = ml / 1000.0;
unit = translate("gettextFromC", ""); unit = translate("gettextFromC", "");
decimals = 1; decimals = 1;
break; break;
case CUFT: case units::CUFT:
vol = ml_to_cuft(ml); vol = ml_to_cuft(ml);
unit = translate("gettextFromC", "cuft"); unit = translate("gettextFromC", "cuft");
decimals = 2; decimals = 2;
@ -79,18 +76,18 @@ double get_volume_units(unsigned int ml, int *frac, const char **units)
return vol; return vol;
} }
int units_to_sac(double volume) extern "C" int units_to_sac(double volume)
{ {
if (get_units()->volume == CUFT) if (get_units()->volume == units::CUFT)
return lrint(cuft_to_l(volume) * 1000.0); return lrint(cuft_to_l(volume) * 1000.0);
else else
return lrint(volume * 1000); return lrint(volume * 1000);
} }
depth_t units_to_depth(double depth) extern "C" depth_t units_to_depth(double depth)
{ {
depth_t internaldepth; depth_t internaldepth;
if (get_units()->length == METERS) { if (get_units()->length == units::METERS) {
internaldepth.mm = lrint(depth * 1000); internaldepth.mm = lrint(depth * 1000);
} else { } else {
internaldepth.mm = feet_to_mm(depth); internaldepth.mm = feet_to_mm(depth);
@ -98,7 +95,7 @@ depth_t units_to_depth(double depth)
return internaldepth; return internaldepth;
} }
double get_depth_units(int mm, int *frac, const char **units) extern "C" double get_depth_units(int mm, int *frac, const char **units)
{ {
int decimals; int decimals;
double d; double d;
@ -106,13 +103,13 @@ double get_depth_units(int mm, int *frac, const char **units)
const struct units *units_p = get_units(); const struct units *units_p = get_units();
switch (units_p->length) { switch (units_p->length) {
case METERS: case units::METERS:
default: default:
d = mm / 1000.0; d = mm / 1000.0;
unit = translate("gettextFromC", "m"); unit = translate("gettextFromC", "m");
decimals = d < 20; decimals = d < 20;
break; break;
case FEET: case units::FEET:
d = mm_to_feet(mm); d = mm_to_feet(mm);
unit = translate("gettextFromC", "ft"); unit = translate("gettextFromC", "ft");
decimals = 0; decimals = 0;
@ -125,25 +122,25 @@ double get_depth_units(int mm, int *frac, const char **units)
return d; return d;
} }
double get_vertical_speed_units(unsigned int mms, int *frac, const char **units) extern "C" double get_vertical_speed_units(unsigned int mms, int *frac, const char **units)
{ {
double d; double d;
const char *unit; const char *unit;
const struct units *units_p = get_units(); const struct units *units_p = get_units();
const double time_factor = units_p->vertical_speed_time == MINUTES ? 60.0 : 1.0; const double time_factor = units_p->vertical_speed_time == units::MINUTES ? 60.0 : 1.0;
switch (units_p->length) { switch (units_p->length) {
case METERS: case units::METERS:
default: default:
d = mms / 1000.0 * time_factor; d = mms / 1000.0 * time_factor;
if (units_p->vertical_speed_time == MINUTES) if (units_p->vertical_speed_time == units::MINUTES)
unit = translate("gettextFromC", "m/min"); unit = translate("gettextFromC", "m/min");
else else
unit = translate("gettextFromC", "m/s"); unit = translate("gettextFromC", "m/s");
break; break;
case FEET: case units::FEET:
d = mm_to_feet(mms) * time_factor; d = mm_to_feet(mms) * time_factor;
if (units_p->vertical_speed_time == MINUTES) if (units_p->vertical_speed_time == units::MINUTES)
unit = translate("gettextFromC", "ft/min"); unit = translate("gettextFromC", "ft/min");
else else
unit = translate("gettextFromC", "ft/s"); unit = translate("gettextFromC", "ft/s");
@ -156,14 +153,14 @@ double get_vertical_speed_units(unsigned int mms, int *frac, const char **units)
return d; return d;
} }
double get_weight_units(unsigned int grams, int *frac, const char **units) extern "C" double get_weight_units(unsigned int grams, int *frac, const char **units)
{ {
int decimals; int decimals;
double value; double value;
const char *unit; const char *unit;
const struct units *units_p = get_units(); const struct units *units_p = get_units();
if (units_p->weight == LBS) { if (units_p->weight == units::LBS) {
value = grams_to_lbs(grams); value = grams_to_lbs(grams);
unit = translate("gettextFromC", "lbs"); unit = translate("gettextFromC", "lbs");
decimals = 0; decimals = 0;
@ -179,7 +176,7 @@ double get_weight_units(unsigned int grams, int *frac, const char **units)
return value; return value;
} }
const struct units *get_units() extern "C" const struct units *get_units()
{ {
return &prefs.units; return &prefs.units;
} }

View file

@ -327,10 +327,10 @@ struct units {
* actually use. Similarly, C instead of Kelvin. * actually use. Similarly, C instead of Kelvin.
* And kg instead of g. * And kg instead of g.
*/ */
#define SI_UNITS \ #define SI_UNITS \
{ \ { \
.length = METERS, .volume = LITER, .pressure = BAR, .temperature = CELSIUS, .weight = KG, \ .length = units::METERS, .volume = units::LITER, .pressure = units::BAR, .temperature = units::CELSIUS, .weight = units::KG, \
.vertical_speed_time = MINUTES, .duration_units = MIXED, .show_units_table = false \ .vertical_speed_time = units::MINUTES, .duration_units = units::MIXED, .show_units_table = false \
} }
extern const struct units SI_units, IMPERIAL_units; extern const struct units SI_units, IMPERIAL_units;