diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2019-06-04 09:05:17 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2019-06-19 13:11:10 -0700 |
commit | bff80cf4625c4a97182e8333633403d06725a613 (patch) | |
tree | 0154f660c6e7db88ff79ad434f5a1df5bf6e076f | |
parent | 7f4d9db962e73aa5d5089c43c99b78b3690ffb87 (diff) | |
download | subsurface-bff80cf4625c4a97182e8333633403d06725a613.tar.gz |
Cleanup: move unit-related functions to own translation unit
Create a units.c translation unit.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
-rw-r--r-- | core/CMakeLists.txt | 1 | ||||
-rw-r--r-- | core/dive.c | 171 | ||||
-rw-r--r-- | core/dive.h | 13 | ||||
-rw-r--r-- | core/units.c | 174 | ||||
-rw-r--r-- | core/units.h | 17 | ||||
-rw-r--r-- | packaging/ios/Subsurface-mobile.pro | 1 |
6 files changed, 191 insertions, 186 deletions
diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 6a0a1ad40..5121222cc 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -156,6 +156,7 @@ set(SUBSURFACE_CORE_LIB_SRCS uemis.c uemis.h units.h + units.c version.c version.h videoframeextractor.cpp diff --git a/core/dive.c b/core/dive.c index b64d2654e..2d82f5565 100644 --- a/core/dive.c +++ b/core/dive.c @@ -273,177 +273,6 @@ struct gasmix get_gasmix_from_event(const struct dive *dive, const struct event return dummy; } -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 PASCAL: - pressure = mb * 100; - unit = translate("gettextFromC", "pascal"); - break; - case BAR: - default: - pressure = (mb + 500) / 1000; - unit = translate("gettextFromC", "bar"); - break; - case PSI: - pressure = mbar_to_PSI(mb); - unit = translate("gettextFromC", "psi"); - break; - } - if (units) - *units = unit; - return pressure; -} - -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) { - deg = mkelvin_to_F(mk); - unit = "°F"; - } else { - deg = mkelvin_to_C(mk); - unit = "°C"; - } - if (units) - *units = unit; - return deg; -} - -double get_volume_units(unsigned int ml, int *frac, const char **units) -{ - int decimals; - double vol; - const char *unit; - const struct units *units_p = get_units(); - - switch (units_p->volume) { - case LITER: - default: - vol = ml / 1000.0; - unit = translate("gettextFromC", "ℓ"); - decimals = 1; - break; - case CUFT: - vol = ml_to_cuft(ml); - unit = translate("gettextFromC", "cuft"); - decimals = 2; - break; - } - if (frac) - *frac = decimals; - if (units) - *units = unit; - return vol; -} - -int units_to_sac(double volume) -{ - if (get_units()->volume == CUFT) - return lrint(cuft_to_l(volume) * 1000.0); - else - return lrint(volume * 1000); -} - -depth_t units_to_depth(double depth) -{ - depth_t internaldepth; - if (get_units()->length == METERS) { - internaldepth.mm = lrint(depth * 1000); - } else { - internaldepth.mm = feet_to_mm(depth); - } - return internaldepth; -} - -double get_depth_units(int mm, int *frac, const char **units) -{ - int decimals; - double d; - const char *unit; - const struct units *units_p = get_units(); - - switch (units_p->length) { - case METERS: - default: - d = mm / 1000.0; - unit = translate("gettextFromC", "m"); - decimals = d < 20; - break; - case FEET: - d = mm_to_feet(mm); - unit = translate("gettextFromC", "ft"); - decimals = 0; - break; - } - if (frac) - *frac = decimals; - if (units) - *units = unit; - return d; -} - -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; - - switch (units_p->length) { - case METERS: - default: - d = mms / 1000.0 * time_factor; - if (units_p->vertical_speed_time == MINUTES) - unit = translate("gettextFromC", "m/min"); - else - unit = translate("gettextFromC", "m/s"); - break; - case FEET: - d = mm_to_feet(mms) * time_factor; - if (units_p->vertical_speed_time == MINUTES) - unit = translate("gettextFromC", "ft/min"); - else - unit = translate("gettextFromC", "ft/s"); - break; - } - if (frac) - *frac = d < 10; - if (units) - *units = unit; - return d; -} - -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) { - value = grams_to_lbs(grams); - unit = translate("gettextFromC", "lbs"); - decimals = 0; - } else { - value = grams / 1000.0; - unit = translate("gettextFromC", "kg"); - decimals = 1; - } - if (frac) - *frac = decimals; - if (units) - *units = unit; - return value; -} - // we need this to be uniq. oh, and it has no meaning whatsoever // - that's why we have the silly initial number and increment by 3 :-) int dive_getUniqID() diff --git a/core/dive.h b/core/dive.h index 6fd78d2dc..093046cf4 100644 --- a/core/dive.h +++ b/core/dive.h @@ -63,16 +63,6 @@ struct event { extern int event_is_gaschange(const struct event *ev); -extern int get_pressure_units(int mb, const char **units); -extern double get_depth_units(int mm, int *frac, const char **units); -extern double get_volume_units(unsigned int ml, int *frac, const char **units); -extern double get_temp_units(unsigned int mk, const char **units); -extern double get_weight_units(unsigned int grams, int *frac, const char **units); -extern double get_vertical_speed_units(unsigned int mms, int *frac, const char **units); - -extern depth_t units_to_depth(double depth); -extern int units_to_sac(double volume); - /* Volume in mliter of a cylinder at pressure 'p' */ extern int gas_volume(const cylinder_t *cyl, pressure_t p); extern double gas_compressibility_factor(struct gasmix gas, double bar); @@ -294,9 +284,6 @@ extern bool autogroup; struct dive *unregister_dive(int idx); extern void delete_single_dive(int idx); -extern const struct units SI_units, IMPERIAL_units; - -extern const struct units *get_units(void); extern int run_survey, verbose, quit, force_root; extern struct dive_table dive_table; diff --git a/core/units.c b/core/units.c new file mode 100644 index 000000000..f0dba064b --- /dev/null +++ b/core/units.c @@ -0,0 +1,174 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "units.h" +#include "gettext.h" + +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 PASCAL: + pressure = mb * 100; + unit = translate("gettextFromC", "pascal"); + break; + case BAR: + default: + pressure = (mb + 500) / 1000; + unit = translate("gettextFromC", "bar"); + break; + case PSI: + pressure = mbar_to_PSI(mb); + unit = translate("gettextFromC", "psi"); + break; + } + if (units) + *units = unit; + return pressure; +} + +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) { + deg = mkelvin_to_F(mk); + unit = "°F"; + } else { + deg = mkelvin_to_C(mk); + unit = "°C"; + } + if (units) + *units = unit; + return deg; +} + +double get_volume_units(unsigned int ml, int *frac, const char **units) +{ + int decimals; + double vol; + const char *unit; + const struct units *units_p = get_units(); + + switch (units_p->volume) { + case LITER: + default: + vol = ml / 1000.0; + unit = translate("gettextFromC", "ℓ"); + decimals = 1; + break; + case CUFT: + vol = ml_to_cuft(ml); + unit = translate("gettextFromC", "cuft"); + decimals = 2; + break; + } + if (frac) + *frac = decimals; + if (units) + *units = unit; + return vol; +} + +int units_to_sac(double volume) +{ + if (get_units()->volume == CUFT) + return lrint(cuft_to_l(volume) * 1000.0); + else + return lrint(volume * 1000); +} + +depth_t units_to_depth(double depth) +{ + depth_t internaldepth; + if (get_units()->length == METERS) { + internaldepth.mm = lrint(depth * 1000); + } else { + internaldepth.mm = feet_to_mm(depth); + } + return internaldepth; +} + +double get_depth_units(int mm, int *frac, const char **units) +{ + int decimals; + double d; + const char *unit; + const struct units *units_p = get_units(); + + switch (units_p->length) { + case METERS: + default: + d = mm / 1000.0; + unit = translate("gettextFromC", "m"); + decimals = d < 20; + break; + case FEET: + d = mm_to_feet(mm); + unit = translate("gettextFromC", "ft"); + decimals = 0; + break; + } + if (frac) + *frac = decimals; + if (units) + *units = unit; + return d; +} + +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; + + switch (units_p->length) { + case METERS: + default: + d = mms / 1000.0 * time_factor; + if (units_p->vertical_speed_time == MINUTES) + unit = translate("gettextFromC", "m/min"); + else + unit = translate("gettextFromC", "m/s"); + break; + case FEET: + d = mm_to_feet(mms) * time_factor; + if (units_p->vertical_speed_time == MINUTES) + unit = translate("gettextFromC", "ft/min"); + else + unit = translate("gettextFromC", "ft/s"); + break; + } + if (frac) + *frac = d < 10; + if (units) + *units = unit; + return d; +} + +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) { + value = grams_to_lbs(grams); + unit = translate("gettextFromC", "lbs"); + decimals = 0; + } else { + value = grams / 1000.0; + unit = translate("gettextFromC", "kg"); + decimals = 1; + } + if (frac) + *frac = decimals; + if (units) + *units = unit; + return value; +} diff --git a/core/units.h b/core/units.h index 2307622e9..565b15d58 100644 --- a/core/units.h +++ b/core/units.h @@ -319,15 +319,28 @@ struct units { #define SI_UNITS \ { \ .length = METERS, .volume = LITER, .pressure = BAR, .temperature = CELSIUS, .weight = KG, \ - .vertical_speed_time = MINUTES, .duration_units = MIXED, .show_units_table = false \ + .vertical_speed_time = MINUTES, .duration_units = MIXED, .show_units_table = false \ } #define IMPERIAL_UNITS \ { \ .length = FEET, .volume = CUFT, .pressure = PSI, .temperature = FAHRENHEIT, .weight = LBS, \ - .vertical_speed_time = MINUTES, .duration_units = MIXED, .show_units_table = false \ + .vertical_speed_time = MINUTES, .duration_units = MIXED, .show_units_table = false \ } +extern const struct units SI_units, IMPERIAL_units; + +extern const struct units *get_units(void); + +extern int get_pressure_units(int mb, const char **units); +extern double get_depth_units(int mm, int *frac, const char **units); +extern double get_volume_units(unsigned int ml, int *frac, const char **units); +extern double get_temp_units(unsigned int mk, const char **units); +extern double get_weight_units(unsigned int grams, int *frac, const char **units); +extern double get_vertical_speed_units(unsigned int mms, int *frac, const char **units); + +extern depth_t units_to_depth(double depth); +extern int units_to_sac(double volume); #ifdef __cplusplus } #endif diff --git a/packaging/ios/Subsurface-mobile.pro b/packaging/ios/Subsurface-mobile.pro index c7d067698..7d78ade94 100644 --- a/packaging/ios/Subsurface-mobile.pro +++ b/packaging/ios/Subsurface-mobile.pro @@ -74,6 +74,7 @@ SOURCES += ../../subsurface-mobile-main.cpp \ ../../core/taxonomy.c \ ../../core/time.c \ ../../core/trip.c \ + ../../core/units.c \ ../../core/uemis.c \ ../../core/btdiscovery.cpp \ ../../core/connectionlistmodel.cpp \ |