| 1 | // |
| 2 | // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh) |
| 3 | // Copyright (c) 2022-2023 Alexander Grund |
| 4 | // |
| 5 | // Distributed under the Boost Software License, Version 1.0. |
| 6 | // https://www.boost.org/LICENSE_1_0.txt |
| 7 | |
| 8 | #include "boost/locale/util/encoding.hpp" |
| 9 | #include "boost/locale/util/string.hpp" |
| 10 | #if BOOST_LOCALE_USE_WIN32_API |
| 11 | # include "boost/locale/util/win_codepages.hpp" |
| 12 | # ifndef NOMINMAX |
| 13 | # define NOMINMAX |
| 14 | # endif |
| 15 | # include <windows.h> |
| 16 | #endif |
| 17 | #include <algorithm> |
| 18 | #include <cstring> |
| 19 | |
| 20 | namespace boost { namespace locale { namespace util { |
| 21 | std::string normalize_encoding(const string_view encoding) |
| 22 | { |
| 23 | std::string result; |
| 24 | result.reserve(res_arg: encoding.length()); |
| 25 | for(char c : encoding) { |
| 26 | if(is_lower_ascii(c) || is_numeric_ascii(c)) |
| 27 | result += c; |
| 28 | else if(is_upper_ascii(c)) |
| 29 | result += char(c - 'A' + 'a'); |
| 30 | } |
| 31 | return result; |
| 32 | } |
| 33 | |
| 34 | #if BOOST_LOCALE_USE_WIN32_API |
| 35 | static int normalized_encoding_to_windows_codepage(const std::string& encoding) |
| 36 | { |
| 37 | windows_encoding* end = std::end(all_windows_encodings); |
| 38 | |
| 39 | windows_encoding* ptr = std::lower_bound(all_windows_encodings, end, encoding.c_str()); |
| 40 | while(ptr != end && ptr->name == encoding) { |
| 41 | if(ptr->was_tested) |
| 42 | return ptr->codepage; |
| 43 | else if(IsValidCodePage(ptr->codepage)) { |
| 44 | // the thread safety is not an issue, maximum |
| 45 | // it would be checked more then once |
| 46 | ptr->was_tested = 1; |
| 47 | return ptr->codepage; |
| 48 | } else |
| 49 | ++ptr; |
| 50 | } |
| 51 | return -1; |
| 52 | } |
| 53 | |
| 54 | int encoding_to_windows_codepage(const string_view encoding) |
| 55 | { |
| 56 | return normalized_encoding_to_windows_codepage(normalize_encoding(encoding)); |
| 57 | } |
| 58 | |
| 59 | #endif |
| 60 | }}} // namespace boost::locale::util |
| 61 | |