1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
// **********************************************************************
//
// Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <Ice/StringConverter.h>
#include <IceUtil/IceUtil.h>
#include <IceUtil/StringUtil.h>
#include <IceUtil/ScopedArray.h>
#include <Ice/LocalException.h>
using namespace IceUtil;
using namespace IceUtilInternal;
using namespace std;
namespace Ice
{
Byte*
UnicodeWstringConverter::toUTF8(const wchar_t* sourceStart,
const wchar_t* sourceEnd,
UTF8Buffer& buffer) const
{
//
// The "chunk size" is the maximum of the number of characters in the
// source and 6 (== max bytes necessary to encode one Unicode character).
//
size_t chunkSize = std::max<size_t>(static_cast<size_t>(sourceEnd - sourceStart), 6);
Byte* targetStart = buffer.getMoreBytes(chunkSize, 0);
Byte* targetEnd = targetStart + chunkSize;
ConversionResult result;
while((result =
convertUTFWstringToUTF8(sourceStart, sourceEnd,
targetStart, targetEnd, lenientConversion))
== targetExhausted)
{
targetStart = buffer.getMoreBytes(chunkSize, targetStart);
targetEnd = targetStart + chunkSize;
}
switch(result)
{
case conversionOK:
break;
case sourceExhausted:
throw StringConversionException(__FILE__, __LINE__, "wide string source exhausted");
case sourceIllegal:
throw StringConversionException(__FILE__, __LINE__, "wide string source illegal");
default:
{
assert(0);
throw StringConversionException(__FILE__, __LINE__);
}
}
return targetStart;
}
void
UnicodeWstringConverter::fromUTF8(const Byte* sourceStart, const Byte* sourceEnd,
wstring& target) const
{
if(sourceStart == sourceEnd)
{
target = L"";
return;
}
ConversionResult result =
convertUTF8ToUTFWstring(sourceStart, sourceEnd, target, lenientConversion);
switch(result)
{
case conversionOK:
break;
case sourceExhausted:
throw StringConversionException(__FILE__, __LINE__, "UTF-8 string source exhausted");
case sourceIllegal:
throw StringConversionException(__FILE__, __LINE__, "UTF-8 string source illegal");
default:
{
assert(0);
throw StringConversionException(__FILE__, __LINE__);
}
}
}
#ifdef _WIN32
WindowsStringConverter::WindowsStringConverter(unsigned int cp) :
_cp(cp)
{
}
Byte*
WindowsStringConverter::toUTF8(const char* sourceStart,
const char* sourceEnd,
UTF8Buffer& buffer) const
{
//
// First convert to UTF-16
//
int sourceSize = sourceEnd - sourceStart;
if(sourceSize == 0)
{
return buffer.getMoreBytes(1, 0);
}
size_t size = 0;
int writtenWchar = 0;
ScopedArray<wchar_t> wbuffer;
do
{
size = size == 0 ? static_cast<size_t>(sourceSize) + 2 : 2 * size;
wbuffer.reset(new wchar_t[size]);
writtenWchar = MultiByteToWideChar(_cp, MB_ERR_INVALID_CHARS, sourceStart,
sourceSize, wbuffer.get(), size);
} while(writtenWchar == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER);
if(writtenWchar == 0)
{
throw StringConversionException(__FILE__, __LINE__, IceUtilInternal::lastErrorToString());
}
//
// Then convert this UTF-16 wbuffer into UTF-8
//
return _unicodeWstringConverter.toUTF8(wbuffer.get(), wbuffer.get() + writtenWchar, buffer);
}
void
WindowsStringConverter::fromUTF8(const Byte* sourceStart, const Byte* sourceEnd,
string& target) const
{
if(sourceStart == sourceEnd)
{
target = "";
return;
}
//
// First convert to wstring (UTF-16)
//
wstring wtarget;
_unicodeWstringConverter.fromUTF8(sourceStart, sourceEnd, wtarget);
//
// And then to a multi-byte narrow string
//
size_t size = 0;
int writtenChar = 0;
ScopedArray<char> buffer;
do
{
size = size == 0 ? static_cast<size_t>(sourceEnd - sourceStart) + 2 : 2 * size;
buffer.reset(new char[size]);
writtenChar = WideCharToMultiByte(_cp, 0, wtarget.data(), wtarget.size(),
buffer.get(), size, 0, 0);
} while(writtenChar == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER);
if(writtenChar == 0)
{
throw StringConversionException(__FILE__, __LINE__, IceUtilInternal::lastErrorToString());
}
target.assign(buffer.get(), writtenChar);
}
#endif
}
|