From 98693c6ccb52af43f8592b792eb59ab07fe0f338 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Thu, 2 May 2024 07:56:47 +0200 Subject: [PATCH] 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 --- Subsurface-mobile.pro | 4 +- core/CMakeLists.txt | 4 +- core/{pref.c => pref.cpp} | 177 +++++++++++++++++++++--------------- core/{units.c => units.cpp} | 59 ++++++------ core/units.h | 8 +- 5 files changed, 138 insertions(+), 114 deletions(-) rename core/{pref.c => pref.cpp} (79%) rename core/{units.c => units.cpp} (64%) diff --git a/Subsurface-mobile.pro b/Subsurface-mobile.pro index f773abcd1..447484236 100644 --- a/Subsurface-mobile.pro +++ b/Subsurface-mobile.pro @@ -45,7 +45,7 @@ SOURCES += subsurface-mobile-main.cpp \ core/fulltext.cpp \ core/subsurfacestartup.cpp \ core/subsurface-string.cpp \ - core/pref.c \ + core/pref.cpp \ core/profile.cpp \ core/device.cpp \ core/dive.cpp \ @@ -98,7 +98,7 @@ SOURCES += subsurface-mobile-main.cpp \ core/taxonomy.c \ core/time.cpp \ core/trip.c \ - core/units.c \ + core/units.cpp \ core/uemis.cpp \ core/btdiscovery.cpp \ core/connectionlistmodel.cpp \ diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 1a5f0aa5b..dbefcd7b2 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -145,7 +145,7 @@ set(SUBSURFACE_CORE_LIB_SRCS planner.h plannernotes.cpp pref.h - pref.c + pref.cpp profile.cpp profile.h qt-gui.h @@ -188,7 +188,7 @@ set(SUBSURFACE_CORE_LIB_SRCS uemis.cpp uemis.h units.h - units.c + units.cpp uploadDiveShare.cpp uploadDiveShare.h uploadDiveLogsDE.cpp diff --git a/core/pref.c b/core/pref.cpp similarity index 79% rename from core/pref.c rename to core/pref.cpp index 17ca1c652..865de9369 100644 --- a/core/pref.c +++ b/core/pref.cpp @@ -5,10 +5,96 @@ struct preferences prefs, git_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 - .units = SI_UNITS, - .unit_system = METRIC, - .coordinates_traditional = true, +#if defined(SUBSURFACE_MOBILE) + .cloud_timeout = 10, +#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 = { .po2 = false, .pn2 = false, @@ -18,86 +104,27 @@ struct preferences default_prefs = { .pn2_threshold = 4.0, .phe_threshold = 13.0, }, - .mod = false, - .modpO2 = 1.6, - .ead = false, - .hrgraph = false, - .percentagegraph = false, - .dcceiling = true, .redceiling = false, - .calcceiling = 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, + .rulergraph = false, .show_average_depth = true, + .show_ccr_sensors = false, + .show_ccr_setpoint = 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_sac = false, + .show_scr_ocpo2 = false, .tankbar = false, - .defaultsetpoint = 1100, - .geocoding = { - .category = { 0 } - }, - .locale = { - .use_system_language = true, - }, - .planner_deco_mode = BUEHLMANN, .vpmb_conservatism = 3, -#if defined(SUBSURFACE_MOBILE) - .cloud_timeout = 10, -#else - .cloud_timeout = 5, -#endif - .auto_recalculate_thumbnails = true, - .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, + .zoomed_plot = false, + .infobox = true, + .coordinates_traditional = true, + .unit_system = METRIC, + .units = SI_UNITS, + .update_manager = { false, NULL, 0 } }; /* 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->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. * valgrind so you can find the real leaks. */ -void free_prefs(void) +extern "C" void free_prefs(void) { // nop } diff --git a/core/units.c b/core/units.cpp similarity index 64% rename from core/units.c rename to core/units.cpp index 6d4c5fbbb..142bfbb47 100644 --- a/core/units.c +++ b/core/units.cpp @@ -3,28 +3,25 @@ #include "gettext.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 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; const char *unit; const struct units *units_p = get_units(); switch (units_p->pressure) { - case BAR: + case units::BAR: default: pressure = (mb + 500) / 1000; unit = translate("gettextFromC", "bar"); break; - case PSI: + case units::PSI: pressure = (int)lrint(mbar_to_PSI(mb)); unit = translate("gettextFromC", "psi"); break; @@ -34,13 +31,13 @@ int get_pressure_units(int mb, const char **units) 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; const char *unit; const struct units *units_p = get_units(); - if (units_p->temperature == FAHRENHEIT) { + if (units_p->temperature == units::FAHRENHEIT) { deg = mkelvin_to_F(mk); unit = "°F"; } else { @@ -52,7 +49,7 @@ double get_temp_units(unsigned int mk, const char **units) 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; 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(); switch (units_p->volume) { - case LITER: + case units::LITER: default: vol = ml / 1000.0; unit = translate("gettextFromC", "ℓ"); decimals = 1; break; - case CUFT: + case units::CUFT: vol = ml_to_cuft(ml); unit = translate("gettextFromC", "cuft"); decimals = 2; @@ -79,18 +76,18 @@ double get_volume_units(unsigned int ml, int *frac, const char **units) 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); else return lrint(volume * 1000); } -depth_t units_to_depth(double depth) +extern "C" depth_t units_to_depth(double depth) { depth_t internaldepth; - if (get_units()->length == METERS) { + if (get_units()->length == units::METERS) { internaldepth.mm = lrint(depth * 1000); } else { internaldepth.mm = feet_to_mm(depth); @@ -98,7 +95,7 @@ depth_t units_to_depth(double depth) 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; double d; @@ -106,13 +103,13 @@ double get_depth_units(int mm, int *frac, const char **units) const struct units *units_p = get_units(); switch (units_p->length) { - case METERS: + case units::METERS: default: d = mm / 1000.0; unit = translate("gettextFromC", "m"); decimals = d < 20; break; - case FEET: + case units::FEET: d = mm_to_feet(mm); unit = translate("gettextFromC", "ft"); decimals = 0; @@ -125,25 +122,25 @@ double get_depth_units(int mm, int *frac, const char **units) 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; const char *unit; 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) { - case METERS: + case units::METERS: default: 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"); else unit = translate("gettextFromC", "m/s"); break; - case FEET: + case units::FEET: 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"); else unit = translate("gettextFromC", "ft/s"); @@ -156,14 +153,14 @@ double get_vertical_speed_units(unsigned int mms, int *frac, const char **units) 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; double value; const char *unit; const struct units *units_p = get_units(); - if (units_p->weight == LBS) { + if (units_p->weight == units::LBS) { value = grams_to_lbs(grams); unit = translate("gettextFromC", "lbs"); decimals = 0; @@ -179,7 +176,7 @@ double get_weight_units(unsigned int grams, int *frac, const char **units) return value; } -const struct units *get_units() +extern "C" const struct units *get_units() { return &prefs.units; } diff --git a/core/units.h b/core/units.h index c18f6f414..f781ab8df 100644 --- a/core/units.h +++ b/core/units.h @@ -327,10 +327,10 @@ struct units { * actually use. Similarly, C instead of Kelvin. * And kg instead of g. */ -#define SI_UNITS \ - { \ - .length = METERS, .volume = LITER, .pressure = BAR, .temperature = CELSIUS, .weight = KG, \ - .vertical_speed_time = MINUTES, .duration_units = MIXED, .show_units_table = false \ +#define SI_UNITS \ + { \ + .length = units::METERS, .volume = units::LITER, .pressure = units::BAR, .temperature = units::CELSIUS, .weight = units::KG, \ + .vertical_speed_time = units::MINUTES, .duration_units = units::MIXED, .show_units_table = false \ } extern const struct units SI_units, IMPERIAL_units;