123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- #include <kopano/zcdefs.h>
- #include <kopano/lockhelper.hpp>
- #include <kopano/platform.h>
- #include <kopano/ECGetText.h>
- #include <kopano/charset/convert.h>
- #include <map>
- #include <mutex>
- #include <string>
- #include <cassert>
- namespace KC {
- namespace detail {
-
- class converter _kc_final {
- public:
-
- static converter *getInstance() {
- scoped_lock locker(s_hInstanceLock);
- if (!s_lpInstance) {
- s_lpInstance = new converter;
- atexit(&destroy);
- }
- return s_lpInstance;
- }
-
- const wchar_t *convert(const char *lpsz) {
- scoped_lock l_cache(m_hCacheLock);
- auto insResult = m_cache.insert(cache_type::value_type(lpsz, std::wstring()));
- if (insResult.second == true)
- insResult.first->second.assign(m_converter.convert_to<std::wstring>(lpsz));
-
- const wchar_t *lpszW = insResult.first->second.c_str();
- return lpszW;
- }
- private:
-
- static void destroy() {
- assert(s_lpInstance);
- delete s_lpInstance;
- s_lpInstance = NULL;
- }
- private:
- static converter *s_lpInstance;
- static std::mutex s_hInstanceLock;
- typedef std::map<const char *, std::wstring> cache_type;
- convert_context m_converter;
- cache_type m_cache;
- std::mutex m_hCacheLock;
- };
- std::mutex converter::s_hInstanceLock;
- converter* converter::s_lpInstance = NULL;
- }
- LPWSTR kopano_dcgettext_wide(const char *domainname, const char *msgid)
- {
- const char *lpsz = msgid;
- #ifndef NO_GETTEXT
- lpsz = dcgettext(domainname, msgid, LC_MESSAGES);
- #endif
- return const_cast<wchar_t *>(detail::converter::getInstance()->convert(lpsz));
- }
- }
|