diff --git a/src/common/utility.c b/src/common/utility.c index a591c9bad1af..a07273c1b889 100644 --- a/src/common/utility.c +++ b/src/common/utility.c @@ -97,6 +97,27 @@ guint dt_util_str_occurence(const gchar *haystack, const gchar *needle) return o; } +gchar *dt_util_float_to_str(const gchar *format, const double value) +{ +#if defined(WIN32) + _configthreadlocale(_ENABLE_PER_THREAD_LOCALE); + setlocale (LC_NUMERIC, "C"); +#else + locale_t nlocale = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0); + locale_t locale = uselocale(nlocale); +#endif + + gchar *txt = g_strdup_printf(format, value); + +#if defined(WIN32) + _configthreadlocale(_DISABLE_PER_THREAD_LOCALE); +#else + uselocale(locale); + freelocale(nlocale); +#endif + return txt; +} + gchar *dt_util_str_replace(const gchar *string, const gchar *pattern, const gchar *substitute) { const gint occurrences = dt_util_str_occurence(string, pattern); diff --git a/src/common/utility.h b/src/common/utility.h index 9e5cd091b36f..9e42438ea63b 100644 --- a/src/common/utility.h +++ b/src/common/utility.h @@ -33,6 +33,8 @@ gchar *dt_util_dstrcat(gchar *str, const gchar *format, ...) __attribute__((form gchar *dt_util_str_replace(const gchar *string, const gchar *pattern, const gchar *substitute); /** count the number of occurrences of needle in haystack */ guint dt_util_str_occurence(const gchar *haystack, const gchar *needle); +/** format a floating point number string with dot separator locale independent */ +gchar *dt_util_float_to_str(const gchar *format, const double value); /** generate a string from the elements of the list, separated by separator. the result has to be freed. */ gchar *dt_util_glist_to_str(const gchar *separator, GList *items); /** generate a GList from the elements of a string, separated by separator. the result has to be freed. */ diff --git a/src/libs/filters/aperture.c b/src/libs/filters/aperture.c index 6f1dfeb192a1..5d28e744f942 100644 --- a/src/libs/filters/aperture.c +++ b/src/libs/filters/aperture.c @@ -20,6 +20,7 @@ This file contains the necessary routines to implement a filter for the filtering module */ +#include "common/utility.h" static gboolean _aperture_update(dt_lib_filtering_rule_t *rule) { @@ -67,7 +68,14 @@ static gboolean _aperture_update(dt_lib_filtering_rule_t *rule) static gchar *_aperture_print_func(const double value, const gboolean detailled) { - return g_strdup_printf("%s%.1lf", detailled ? "f/" : "", value); + if(detailled) + { + return g_strdup_printf("f/%.1lf", value); + } + else + { + return dt_util_float_to_str("%.1f", value); + } } static void _aperture_widget_init(dt_lib_filtering_rule_t *rule, const dt_collection_properties_t prop, diff --git a/src/libs/filters/exposure.c b/src/libs/filters/exposure.c index 69c154bc0f43..854cd2a65182 100644 --- a/src/libs/filters/exposure.c +++ b/src/libs/filters/exposure.c @@ -20,6 +20,7 @@ This file contains the necessary routines to implement a filter for the filtering module */ +#include "common/utility.h" static gboolean _exposure_update(dt_lib_filtering_rule_t *rule) { @@ -84,12 +85,7 @@ static gchar *_exposure_print_func(const double value, const gboolean detailled) } else { - gchar *locale = g_strdup(setlocale(LC_ALL, NULL)); - setlocale(LC_NUMERIC, "C"); - gchar *txt = g_strdup_printf("%.6lf", value); - setlocale(LC_NUMERIC, locale); - g_free(locale); - return txt; + return dt_util_float_to_str("%.6lf", value); } } @@ -139,4 +135,4 @@ static void _exposure_widget_init(dt_lib_filtering_rule_t *rule, const dt_collec // modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py // vim: shiftwidth=2 expandtab tabstop=2 cindent // kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified; -// clang-format on \ No newline at end of file +// clang-format on diff --git a/src/libs/filters/iso.c b/src/libs/filters/iso.c index ee9fb156d460..81bab03cea2e 100644 --- a/src/libs/filters/iso.c +++ b/src/libs/filters/iso.c @@ -20,6 +20,7 @@ This file contains the necessary routines to implement a filter for the filtering module */ +#include "common/utility.h" static gboolean _iso_update(dt_lib_filtering_rule_t *rule) { @@ -91,12 +92,7 @@ static gchar *_iso_print_func(const double value, const gboolean detailled) } else { - gchar *locale = g_strdup(setlocale(LC_ALL, NULL)); - setlocale(LC_NUMERIC, "C"); - gchar *txt = g_strdup_printf("%.0lf", value); - setlocale(LC_NUMERIC, locale); - g_free(locale); - return txt; + return dt_util_float_to_str("%.0lf", value); } } @@ -140,4 +136,4 @@ static void _iso_widget_init(dt_lib_filtering_rule_t *rule, const dt_collection_ // modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py // vim: shiftwidth=2 expandtab tabstop=2 cindent // kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified; -// clang-format on \ No newline at end of file +// clang-format on diff --git a/src/libs/filters/ratio.c b/src/libs/filters/ratio.c index 4927b30c4378..65c3515b4a91 100644 --- a/src/libs/filters/ratio.c +++ b/src/libs/filters/ratio.c @@ -20,6 +20,8 @@ This file contains the necessary routines to implement a filter for the filtering module */ +#include "common/utility.h" + static gboolean _ratio_update(dt_lib_filtering_rule_t *rule) { if(!rule->w_specific) return FALSE; @@ -110,11 +112,7 @@ static double _ratio_value_from_band_func(const double value) static gchar *_ratio_print_func(const double value, const gboolean detailled) { - gchar *locale = g_strdup(setlocale(LC_ALL, NULL)); - setlocale(LC_NUMERIC, "C"); - gchar *txt = g_strdup_printf("%.2lf", value); - setlocale(LC_NUMERIC, locale); - g_free(locale); + gchar *txt = dt_util_float_to_str("%.2lf", value); if(detailled) { @@ -169,4 +167,4 @@ static void _ratio_widget_init(dt_lib_filtering_rule_t *rule, const dt_collectio // modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py // vim: shiftwidth=2 expandtab tabstop=2 cindent // kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified; -// clang-format on \ No newline at end of file +// clang-format on