Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef POCO_NO_WSTRING
00038
00039
00040 #include "Poco/UnicodeConverter.h"
00041 #include "Poco/TextConverter.h"
00042 #include "Poco/TextIterator.h"
00043 #include "Poco/UTF8Encoding.h"
00044 #include "Poco/UTF16Encoding.h"
00045 #include <cstring>
00046 #include <wchar.h>
00047
00048
00049 namespace Poco {
00050
00051
00052 void UnicodeConverter::toUTF16(const std::string& utf8String, std::wstring& utf16String)
00053 {
00054 utf16String.clear();
00055 UTF8Encoding utf8Encoding;
00056 TextIterator it(utf8String, utf8Encoding);
00057 TextIterator end(utf8String);
00058 while (it != end) utf16String += (wchar_t) *it++;
00059 }
00060
00061
00062 void UnicodeConverter::toUTF16(const char* utf8String, int length, std::wstring& utf16String)
00063 {
00064 poco_check_ptr (utf8String);
00065
00066 utf16String.clear();
00067
00068 UTF8Encoding utf8Encoding;
00069 UTF16Encoding utf16Encoding;
00070 const unsigned char* it = (const unsigned char*) utf8String;
00071 const unsigned char* end = (const unsigned char*) utf8String + length;
00072
00073 while (it < end)
00074 {
00075 unsigned char c = *it;
00076 int n = utf8Encoding.characterMap()[c];
00077 int uc = '?';
00078 if (n == -1)
00079 {
00080 ++it;
00081 }
00082 else if (n >= 0)
00083 {
00084 uc = n;
00085 ++it;
00086 }
00087 else
00088 {
00089 if (it - n <= end)
00090 {
00091 uc = utf8Encoding.convert(it);
00092 if (uc == -1) uc = '?';
00093 }
00094 it -= n;
00095 }
00096 utf16String += (wchar_t) uc;
00097 }
00098 }
00099
00100
00101 void UnicodeConverter::toUTF16(const char* utf8String, std::wstring& utf16String)
00102 {
00103 toUTF16(utf8String, (int) std::strlen(utf8String), utf16String);
00104 }
00105
00106
00107 void UnicodeConverter::toUTF8(const std::wstring& utf16String, std::string& utf8String)
00108 {
00109 utf8String.clear();
00110 UTF8Encoding utf8Encoding;
00111 UTF16Encoding utf16Encoding;
00112 TextConverter converter(utf16Encoding, utf8Encoding);
00113 converter.convert(utf16String.data(), (int) utf16String.length()*sizeof(wchar_t), utf8String);
00114 }
00115
00116
00117 void UnicodeConverter::toUTF8(const wchar_t* utf16String, int length, std::string& utf8String)
00118 {
00119 utf8String.clear();
00120 UTF8Encoding utf8Encoding;
00121 UTF16Encoding utf16Encoding;
00122 TextConverter converter(utf16Encoding, utf8Encoding);
00123 converter.convert(utf16String, (int) length*sizeof(wchar_t), utf8String);
00124 }
00125
00126
00127 void UnicodeConverter::toUTF8(const wchar_t* utf16String, std::string& utf8String)
00128 {
00129 toUTF8(utf16String, (int) wcslen(utf16String), utf8String);
00130 }
00131
00132
00133 }
00134
00135
00136 #endif // POCO_NO_WSTRING