| 1 |
/* |
| 2 |
* Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2023 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-common-lib |
| 6 |
* Created on: 19 нояб. 2023 г. |
| 7 |
* |
| 8 |
* lsp-common-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-common-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-common-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef LSP_PLUG_IN_STDLIB_LOCALE_H_ |
| 23 |
#define LSP_PLUG_IN_STDLIB_LOCALE_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/common/version.h> |
| 26 |
|
| 27 |
#include <lsp-plug.in/common/finally.h> |
| 28 |
#include <lsp-plug.in/common/types.h> |
| 29 |
#include <locale.h> |
| 30 |
#include <string.h> |
| 31 |
|
| 32 |
#ifdef PLATFORM_MACOSX |
| 33 |
#include <xlocale.h> |
| 34 |
#endif /* PLATFORM_MACOSX */ |
| 35 |
|
| 36 |
#define SET_LOCALE_MANGLE2(prefix, postfix) prefix ## _ ## postfix |
| 37 |
#define SET_LOCALE_MANGLE1(prefix, postfix) SET_LOCALE_MANGLE2(prefix, postfix) |
| 38 |
#define SET_LOCALE_MANGLE(var) SET_LOCALE_MANGLE1(var, __COUNTER__) |
| 39 |
|
| 40 |
#ifdef PLATFORM_WINDOWS |
| 41 |
#define SET_LOCALE_SCOPED_VAR(state, old, old_len, buf, lc, value) \ |
| 42 |
int state = _configthreadlocale(_ENABLE_PER_THREAD_LOCALE); \ |
| 43 |
char *old = setlocale(lc, NULL); \ |
| 44 |
if (old != NULL) \ |
| 45 |
{ \ |
| 46 |
size_t old_len = strlen(old) + 1; \ |
| 47 |
char *buf = static_cast<char *>(alloca(old_len)); \ |
| 48 |
memcpy(buf, old, old_len); \ |
| 49 |
old = buf; \ |
| 50 |
} \ |
| 51 |
setlocale(lc, value); \ |
| 52 |
lsp_finally { \ |
| 53 |
if (old != NULL) \ |
| 54 |
::setlocale(lc, old); \ |
| 55 |
if (state >= 0) \ |
| 56 |
_configthreadlocale(state); \ |
| 57 |
} |
| 58 |
|
| 59 |
#define SET_LOCALE_SCOPED(lc, value) SET_LOCALE_SCOPED_VAR( \ |
| 60 |
SET_LOCALE_MANGLE(__state), \ |
| 61 |
SET_LOCALE_MANGLE(__old), \ |
| 62 |
SET_LOCALE_MANGLE(__len), \ |
| 63 |
SET_LOCALE_MANGLE(__buf), \ |
| 64 |
lc, \ |
| 65 |
value) |
| 66 |
|
| 67 |
#else |
| 68 |
|
| 69 |
#define SET_LOCALE_SCOPED_VAR(old, new, lc, value) \ |
| 70 |
locale_t old = ::lsp::detail::INVALID_LOCALE; \ |
| 71 |
locale_t new = ::lsp::detail::create_locale(lc, value); \ |
| 72 |
if (new != ::lsp::detail::INVALID_LOCALE) \ |
| 73 |
old = uselocale(new); \ |
| 74 |
lsp_finally { \ |
| 75 |
if (old != ::lsp::detail::INVALID_LOCALE) \ |
| 76 |
uselocale(old); \ |
| 77 |
if (new != ::lsp::detail::INVALID_LOCALE) \ |
| 78 |
freelocale(new); \ |
| 79 |
} |
| 80 |
|
| 81 |
#define SET_LOCALE_SCOPED(lc, value) SET_LOCALE_SCOPED_VAR( \ |
| 82 |
SET_LOCALE_MANGLE(__old), \ |
| 83 |
SET_LOCALE_MANGLE(__new), \ |
| 84 |
lc, \ |
| 85 |
value) |
| 86 |
|
| 87 |
namespace lsp |
| 88 |
{ |
| 89 |
namespace detail |
| 90 |
{ |
| 91 |
constexpr locale_t INVALID_LOCALE = (locale_t)0; |
| 92 |
|
| 93 |
/** |
| 94 |
* Create locale object based on locale type and name |
| 95 |
* @param type locale type |
| 96 |
* @param name locale name |
| 97 |
* @return created locale object |
| 98 |
*/ |
| 99 |
LSP_COMMON_LIB_PUBLIC |
| 100 |
locale_t create_locale(int type, const char *name); |
| 101 |
} /* namespace detail */ |
| 102 |
} /* namespace lsp */ |
| 103 |
|
| 104 |
#endif /* PLATFORM_WINDOWS */ |
| 105 |
|
| 106 |
|
| 107 |
#endif /* LSP_PLUG_IN_STDLIB_LOCALE_H_ */ |