From a8a8fc43effb757b5a8fbe4e86fae1cb0b83ef57 Mon Sep 17 00:00:00 2001 From: lethosor Date: Fri, 13 Feb 2015 17:56:29 -0500 Subject: [PATCH] Add df2console() wrapper Closes #522 --- library/LuaApi.cpp | 2 ++ library/MiscUtils.cpp | 16 ++++++++++++++++ library/include/MiscUtils.h | 1 + 3 files changed, 19 insertions(+) diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index 64d27038c..26e158368 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -1282,6 +1282,7 @@ static bool isMapLoaded() { return Core::getInstance().isMapLoaded(); } static std::string df2utf(std::string s) { return DF2UTF(s); } static std::string utf2df(std::string s) { return UTF2DF(s); } +static std::string df2console(std::string s) { return DF2CONSOLE(s); } static const LuaWrapper::FunctionReg dfhack_module[] = { WRAP(getOSType), @@ -1294,6 +1295,7 @@ static const LuaWrapper::FunctionReg dfhack_module[] = { WRAPM(Translation, TranslateName), WRAP(df2utf), WRAP(utf2df), + WRAP(df2console), { NULL, NULL } }; diff --git a/library/MiscUtils.cpp b/library/MiscUtils.cpp index 53c403026..823b676eb 100644 --- a/library/MiscUtils.cpp +++ b/library/MiscUtils.cpp @@ -347,3 +347,19 @@ std::string UTF2DF(const std::string &in) out.resize(pos); return out; } + +DFHACK_EXPORT std::string DF2CONSOLE(const std::string &in) +{ + bool is_utf = false; +#ifdef LINUX_BUILD + std::string locale = ""; + if (getenv("LANG")) + locale += getenv("LANG"); + if (getenv("LC_CTYPE")) + locale += getenv("LC_CTYPE"); + locale = toUpper(locale); + is_utf = (locale.find("UTF-8") != std::string::npos) || + (locale.find("UTF8") != std::string::npos); +#endif + return is_utf ? DF2UTF(in) : in; +} diff --git a/library/include/MiscUtils.h b/library/include/MiscUtils.h index 660699d7a..653e37f47 100644 --- a/library/include/MiscUtils.h +++ b/library/include/MiscUtils.h @@ -348,3 +348,4 @@ DFHACK_EXPORT std::string stl_vsprintf(const char *fmt, va_list args); // Conversion between CP437 and UTF-8 DFHACK_EXPORT std::string UTF2DF(const std::string &in); DFHACK_EXPORT std::string DF2UTF(const std::string &in); +DFHACK_EXPORT std::string DF2CONSOLE(const std::string &in);