diff options
author | Dwayne Boone <dwayne@zeroc.com> | 2007-05-02 19:10:56 +0000 |
---|---|---|
committer | Dwayne Boone <dwayne@zeroc.com> | 2007-05-02 19:10:56 +0000 |
commit | 643bb663b019016db089777143d6ba7e8a4ff0ea (patch) | |
tree | 95aecc5b75c658ae0c8abc287434f604b9eb1bd8 /cppe/src/IceE/StringConverter.cpp | |
parent | MAke wstring optional for IceE (diff) | |
download | ice-643bb663b019016db089777143d6ba7e8a4ff0ea.tar.bz2 ice-643bb663b019016db089777143d6ba7e8a4ff0ea.tar.xz ice-643bb663b019016db089777143d6ba7e8a4ff0ea.zip |
Make Wstring optional for IceE
Diffstat (limited to 'cppe/src/IceE/StringConverter.cpp')
-rwxr-xr-x | cppe/src/IceE/StringConverter.cpp | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/cppe/src/IceE/StringConverter.cpp b/cppe/src/IceE/StringConverter.cpp new file mode 100755 index 00000000000..2ef75cea5b5 --- /dev/null +++ b/cppe/src/IceE/StringConverter.cpp @@ -0,0 +1,91 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved. +// +// This copy of Ice-E is licensed to you under the terms described in the +// ICEE_LICENSE file included in this distribution. +// +// ********************************************************************** + +#include <IceE/Config.h> + +#ifdef ICEE_HAS_WSTRING + +#include <IceE/StringConverter.h> +#include <IceE/Unicode.h> +#include <IceE/LocalException.h> + +using namespace IceUtil; +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 +{ + 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__); + } + } +} + +} + +#endif |