| 1 | // Definitions for <text_encoding> -*- C++ -*- |
| 2 | |
| 3 | // Copyright The GNU Toolchain Authors. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // Under Section 7 of GPL version 3, you are granted additional |
| 17 | // permissions described in the GCC Runtime Library Exception, version |
| 18 | // 3.1, as published by the Free Software Foundation. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License and |
| 21 | // a copy of the GCC Runtime Library Exception along with this program; |
| 22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 | // <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | #include <text_encoding> |
| 26 | #include <locale> |
| 27 | |
| 28 | #ifdef _GLIBCXX_USE_NL_LANGINFO_L |
| 29 | #include <locale.h> |
| 30 | #if __has_include(<xlocale.h>) |
| 31 | # include <xlocale.h> |
| 32 | #endif |
| 33 | #include <langinfo.h> |
| 34 | |
| 35 | #if __CHAR_BIT__ == 8 |
| 36 | namespace std |
| 37 | { |
| 38 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 39 | namespace |
| 40 | { |
| 41 | // Attempt to determine the text_encoding used by the named locale. |
| 42 | text_encoding |
| 43 | __locale_encoding(const char* name) |
| 44 | { |
| 45 | text_encoding enc; |
| 46 | if (locale_t loc = ::newlocale(LC_CTYPE_MASK, name, (locale_t)0)) |
| 47 | { |
| 48 | if (const char* codeset = ::nl_langinfo_l(CODESET, loc)) |
| 49 | { |
| 50 | string_view s(codeset); |
| 51 | if (s.size() < text_encoding::max_name_length) |
| 52 | enc = text_encoding(s); |
| 53 | } |
| 54 | ::freelocale(loc); |
| 55 | } |
| 56 | return enc; |
| 57 | } |
| 58 | |
| 59 | } // namespace |
| 60 | _GLIBCXX_END_NAMESPACE_VERSION |
| 61 | } // namespace std |
| 62 | |
| 63 | std::text_encoding |
| 64 | std::text_encoding::environment() |
| 65 | { |
| 66 | return std::__locale_encoding(""); |
| 67 | } |
| 68 | |
| 69 | bool |
| 70 | std::text_encoding::_M_is_environment() const |
| 71 | { |
| 72 | bool matched = false; |
| 73 | if (locale_t loc = ::newlocale(LC_CTYPE_MASK, "", (locale_t)0)) |
| 74 | { |
| 75 | if (const char* codeset = ::nl_langinfo_l(CODESET, loc)) |
| 76 | { |
| 77 | string_view sv(codeset); |
| 78 | for (auto alias : aliases()) |
| 79 | if (__unicode::__charset_alias_match(alias, sv)) |
| 80 | { |
| 81 | matched = true; |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | ::freelocale(loc); |
| 86 | } |
| 87 | return matched; |
| 88 | } |
| 89 | |
| 90 | std::text_encoding |
| 91 | std::locale::encoding() const |
| 92 | { |
| 93 | string name = this->name(); |
| 94 | if (name.length() == 1) |
| 95 | { |
| 96 | if (name[0] == 'C') |
| 97 | return text_encoding(text_encoding::ASCII); |
| 98 | if (name[0] == '*') |
| 99 | return {}; |
| 100 | } |
| 101 | return __locale_encoding(name.c_str()); |
| 102 | } |
| 103 | #endif // CHAR_BIT == 8 |
| 104 | #endif // _GLIBCXX_USE_NL_LANGINFO_L |