summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDirk-Jan C. Binnema <djcb@djcbsoftware.nl>2022-05-17 22:31:03 +0300
committerDirk-Jan C. Binnema <djcb@djcbsoftware.nl>2022-05-17 22:31:03 +0300
commitc4cc9795dcf7ecac318dd00f7704fb4e45fd8174 (patch)
tree203c7f5ff1defc9ba0a90f915059cf208ba714c8 /lib
parent7f8260d5edfd2f1ca788ca03715d94bea90afc2e (diff)
utils: add locale_workaround
Attempt to work around some locale handling throwing in some systems. "locale::facet::_S_create_c_locale name not valid" Ugly, but maybe it helps.
Diffstat (limited to 'lib')
-rw-r--r--lib/utils/mu-utils.cc19
-rw-r--r--lib/utils/mu-utils.hh23
2 files changed, 26 insertions, 16 deletions
diff --git a/lib/utils/mu-utils.cc b/lib/utils/mu-utils.cc
index 0cc34a5..28abc43 100644
--- a/lib/utils/mu-utils.cc
+++ b/lib/utils/mu-utils.cc
@@ -19,6 +19,7 @@
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE
+#include <stdexcept>
#endif /*_XOPEN_SOURCE*/
#include <array>
@@ -606,3 +607,21 @@ Mu::TempDir::~TempDir()
g_debug("removed '%s'", path_.c_str());
}
+
+bool
+Mu::locale_workaround()
+{
+ // quite horrible... but some systems break otherwise with
+ // https://github.com/djcb/mu/issues/2252
+
+ for (auto&& loc : {"", "en_US.UTF-8", "C" }) {
+ try {
+ std::locale::global(std::locale(loc));
+ return true;
+ } catch (const std::runtime_error& re) {
+ continue;
+ }
+ }
+
+ return false;
+}
diff --git a/lib/utils/mu-utils.hh b/lib/utils/mu-utils.hh
index 95d6d82..8018a49 100644
--- a/lib/utils/mu-utils.hh
+++ b/lib/utils/mu-utils.hh
@@ -147,22 +147,13 @@ std::string date_to_time_t_string(int64_t t);
std::string time_to_string(const std::string& frm, time_t t, bool utc = false) G_GNUC_CONST;
-// /**
-// * Create a std::string by consuming a gchar* array; this takes ownership
-// * of str which should no longer be used.
-// *
-// * @param str a gchar* or NULL (latter taken as "")
-// *
-// * @return a std::string
-// */
-// static inline std::string
-// from_gchars(gchar*&& str)
-// {
-// std::string s{str ? str : ""};
-// g_free(str);
-
-// return s;
-// }
+/**
+ * Hack to avoid locale crashes
+ *
+ * @return true if setting locale worked; false otherwise
+ */
+bool locale_workaround();
+
// https://stackoverflow.com/questions/19053351/how-do-i-use-a-custom-deleter-with-a-stdunique-ptr-member
template <auto fn>