| 1 | // |
| 2 | // UnicodeConverter.h |
| 3 | // |
| 4 | // Library: Foundation |
| 5 | // Package: Text |
| 6 | // Module: UnicodeConverter |
| 7 | // |
| 8 | // Definition of the UnicodeConverter class. |
| 9 | // |
| 10 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
| 11 | // and Contributors. |
| 12 | // |
| 13 | // SPDX-License-Identifier: BSL-1.0 |
| 14 | // |
| 15 | |
| 16 | |
| 17 | #ifndef Foundation_UnicodeConverter_INCLUDED |
| 18 | #define Foundation_UnicodeConverter_INCLUDED |
| 19 | |
| 20 | |
| 21 | #include "Poco/Foundation.h" |
| 22 | |
| 23 | #if !defined(POCO_NO_WSTRING) |
| 24 | |
| 25 | #include "Poco/UTFString.h" |
| 26 | |
| 27 | |
| 28 | namespace Poco { |
| 29 | |
| 30 | |
| 31 | class Foundation_API UnicodeConverter |
| 32 | /// A convenience class that converts strings from |
| 33 | /// UTF-8 encoded std::strings to UTF-16 or UTF-32 encoded std::wstrings |
| 34 | /// and vice-versa. |
| 35 | /// |
| 36 | /// This class is mainly used for working with the Unicode Windows APIs |
| 37 | /// and probably won't be of much use anywhere else ??? |
| 38 | { |
| 39 | public: |
| 40 | static void convert(const std::string& utf8String, UTF32String& utf32String); |
| 41 | /// Converts the given UTF-8 encoded string into an UTF-32 encoded wide string. |
| 42 | |
| 43 | static void convert(const char* utf8String, std::size_t length, UTF32String& utf32String); |
| 44 | /// Converts the given UTF-8 encoded character sequence into an UTF-32 encoded wide string. |
| 45 | |
| 46 | static void convert(const char* utf8String, UTF32String& utf32String); |
| 47 | /// Converts the given zero-terminated UTF-8 encoded character sequence into an UTF-32 encoded wide string. |
| 48 | |
| 49 | static void convert(const std::string& utf8String, UTF16String& utf16String); |
| 50 | /// Converts the given UTF-8 encoded string into an UTF-16 encoded wide string. |
| 51 | |
| 52 | static void convert(const char* utf8String, std::size_t length, UTF16String& utf16String); |
| 53 | /// Converts the given UTF-8 encoded character sequence into an UTF-16 encoded wide string. |
| 54 | |
| 55 | static void convert(const char* utf8String, UTF16String& utf16String); |
| 56 | /// Converts the given zero-terminated UTF-8 encoded character sequence into an UTF-16 encoded wide string. |
| 57 | |
| 58 | static void convert(const UTF16String& utf16String, std::string& utf8String); |
| 59 | /// Converts the given UTF-16 encoded wide string into an UTF-8 encoded string. |
| 60 | |
| 61 | static void convert(const UTF32String& utf32String, std::string& utf8String); |
| 62 | /// Converts the given UTF-32 encoded wide string into an UTF-8 encoded string. |
| 63 | |
| 64 | static void convert(const UTF16Char* utf16String, std::size_t length, std::string& utf8String); |
| 65 | /// Converts the given zero-terminated UTF-16 encoded wide character sequence into an UTF-8 encoded string. |
| 66 | |
| 67 | static void convert(const UTF32Char* utf16String, std::size_t length, std::string& utf8String); |
| 68 | /// Converts the given zero-terminated UTF-32 encoded wide character sequence into an UTF-8 encoded string. |
| 69 | |
| 70 | static void convert(const UTF16Char* utf16String, std::string& utf8String); |
| 71 | /// Converts the given UTF-16 encoded zero terminated character sequence into an UTF-8 encoded string. |
| 72 | |
| 73 | static void convert(const UTF32Char* utf32String, std::string& utf8String); |
| 74 | /// Converts the given UTF-32 encoded zero terminated character sequence into an UTF-8 encoded string. |
| 75 | |
| 76 | template <typename F, typename T> |
| 77 | static void toUTF32(const F& f, T& t) |
| 78 | { |
| 79 | convert(f, t); |
| 80 | } |
| 81 | |
| 82 | template <typename F, typename T> |
| 83 | static void toUTF32(const F& f, std::size_t l, T& t) |
| 84 | { |
| 85 | convert(f, l, t); |
| 86 | } |
| 87 | |
| 88 | template <typename F, typename T> |
| 89 | static void toUTF16(const F& f, T& t) |
| 90 | { |
| 91 | convert(f, t); |
| 92 | } |
| 93 | |
| 94 | template <typename F, typename T> |
| 95 | static void toUTF16(const F& f, std::size_t l, T& t) |
| 96 | { |
| 97 | convert(f, l, t); |
| 98 | } |
| 99 | |
| 100 | template <typename F, typename T> |
| 101 | static void toUTF8(const F& f, T& t) |
| 102 | { |
| 103 | convert(f, t); |
| 104 | } |
| 105 | |
| 106 | template <typename F, typename T> |
| 107 | static void toUTF8(const F& f, std::size_t l, T& t) |
| 108 | { |
| 109 | convert(f, l, t); |
| 110 | } |
| 111 | |
| 112 | template <typename T> |
| 113 | static T to(const char* pChar) |
| 114 | { |
| 115 | T utfStr; |
| 116 | Poco::UnicodeConverter::convert(pChar, utfStr); |
| 117 | return utfStr; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | template <typename T> |
| 122 | static T to(const std::string& str) |
| 123 | { |
| 124 | T utfStr; |
| 125 | Poco::UnicodeConverter::convert(str, utfStr); |
| 126 | return utfStr; |
| 127 | } |
| 128 | |
| 129 | template <typename T> |
| 130 | static std::size_t UTFStrlen(const T* ptr) |
| 131 | /// Returns the length (in characters) of a zero-terminated UTF string. |
| 132 | { |
| 133 | if (ptr == 0) return 0; |
| 134 | const T* p; |
| 135 | for (p = ptr; *p; ++p); |
| 136 | return p - ptr; |
| 137 | } |
| 138 | }; |
| 139 | |
| 140 | |
| 141 | } // namespace Poco |
| 142 | |
| 143 | |
| 144 | #endif // POCO_OS_FAMILY_WINDOWS && !POCO_NO_WSTRING |
| 145 | |
| 146 | |
| 147 | #endif // Foundation_UnicodeConverter_INCLUDED |
| 148 | |